1 /* 2 * Copyright (c) 2024 Apple Inc. All rights reserved. 3 * 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ 5 * 6 * This file contains Original Code and/or Modifications of Original Code 7 * as defined in and that are subject to the Apple Public Source License 8 * Version 2.0 (the 'License'). You may not use this file except in 9 * compliance with the License. The rights granted to you under the License 10 * may not be used to create, or enable the creation or redistribution of, 11 * unlawful or unlicensed copies of an Apple operating system, or to 12 * circumvent, violate, or enable the circumvention or violation of, any 13 * terms of an Apple operating system software license agreement. 14 * 15 * Please obtain a copy of the License at 16 * http://www.opensource.apple.com/apsl/ and read it before using this file. 17 * 18 * The Original Code and all software distributed under the License are 19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 23 * Please see the License for the specific language governing rights and 24 * limitations under the License. 25 * 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ 27 * 28 */ 29 #include <darwintest.h> 30 #include <darwintest_perf.h> 31 #include <darwintest_utils.h> 32 #include <fcntl.h> 33 #include <mach/mach.h> 34 #include <mach/mach_vm.h> 35 #include <string.h> 36 #include <sys/mman.h> 37 #include <sys/sysctl.h> 38 #include <sys/syslimits.h> 39 40 /* 41 * Wiring performance micro-benchmark. 42 */ 43 44 T_GLOBAL_META( 45 T_META_NAMESPACE("xnu.vm.perf"), 46 T_META_RADAR_COMPONENT_NAME("xnu"), 47 T_META_RADAR_COMPONENT_VERSION("VM"), 48 T_META_OWNER("jarrad"), 49 T_META_CHECK_LEAKS(false), 50 T_META_TAG_PERF, 51 T_META_REQUIRE_NOT_VIRTUALIZED); 52 53 #define MiB(b) ((uint64_t)b << 20) 54 55 extern int vfs_purge(void); 56 57 T_DECL(mlock_external, 58 "File-backed wire microbenchmark") 59 { 60 void *buf = NULL; 61 int fd, ret; 62 char tmpf[PATH_MAX] = ""; 63 uint8_t pattern[4] = {0xFEu, 0xDCu, 0xBAu, 0x98u}; 64 // This should be kept larger than MAX_UPL_TRANFER_BYTES to ensure clustering 65 // greater than the default fault clustering is tested 66 const size_t vmsize = MiB(512); 67 68 T_SETUPBEGIN; 69 70 buf = malloc(vmsize); 71 T_QUIET; T_ASSERT_NOTNULL(buf, "malloc()"); 72 73 // Create a tmp file and populate it with data 74 strlcpy(tmpf, dt_tmpdir(), PATH_MAX); 75 strlcat(tmpf, "/mlock_external.txt", PATH_MAX); 76 T_QUIET; T_ASSERT_LT(strlen(tmpf), (unsigned long)PATH_MAX, 77 "path exceeds PATH_MAX"); 78 79 fd = open(tmpf, (O_RDWR | O_CREAT)); 80 T_QUIET; T_ASSERT_POSIX_SUCCESS(fd, "open()"); 81 82 memset_pattern4((char *)buf, pattern, vmsize); 83 write(fd, buf, vmsize); 84 fsync(fd); 85 close(fd); 86 free(buf); 87 buf = NULL; 88 89 // Purge to flush the test file from the filecache 90 vfs_purge(); 91 92 fd = open(tmpf, O_RDWR); 93 T_QUIET; T_ASSERT_POSIX_SUCCESS(fd, "open()"); 94 buf = mmap(NULL, vmsize, PROT_READ, 95 (MAP_FILE | MAP_SHARED), fd, 0); 96 T_QUIET; T_ASSERT_NOTNULL(buf, "mmap()"); 97 98 T_SETUPEND; 99 100 dt_stat_time_t mlock_time = dt_stat_time_create("mlock_duration"); 101 dt_stat_set_variable(mlock_time, "buffer_size_mb", 102 (unsigned int)(vmsize >> 20)); 103 104 T_LOG("Collecting measurements..."); 105 while (!dt_stat_stable(mlock_time)) { T_STAT_MEASURE(mlock_time)106 T_STAT_MEASURE(mlock_time) { 107 ret = mlock(buf, vmsize); 108 } 109 T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "mlock()"); 110 111 ret = munlock(buf, vmsize); 112 T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "munlock()"); 113 114 vfs_purge(); 115 } 116 dt_stat_finalize(mlock_time); 117 118 ret = munmap(buf, vmsize); 119 T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "munmap()"); 120 ret = close(fd); 121 T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "close()"); 122 } 123