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 T_META_ENABLED(false /* rdar://133954365 */)) 60 { 61 void *buf = NULL; 62 int fd, ret; 63 char tmpf[PATH_MAX] = ""; 64 uint8_t pattern[4] = {0xFEu, 0xDCu, 0xBAu, 0x98u}; 65 // This should be kept larger than MAX_UPL_TRANFER_BYTES to ensure clustering 66 // greater than the default fault clustering is tested 67 const size_t vmsize = MiB(512); 68 69 T_SETUPBEGIN; 70 71 buf = malloc(vmsize); 72 T_QUIET; T_ASSERT_NOTNULL(buf, "malloc()"); 73 74 // Create a tmp file and populate it with data 75 strlcpy(tmpf, dt_tmpdir(), PATH_MAX); 76 strlcat(tmpf, "/mlock_external.txt", PATH_MAX); 77 T_QUIET; T_ASSERT_LT(strlen(tmpf), (unsigned long)PATH_MAX, 78 "path exceeds PATH_MAX"); 79 80 fd = open(tmpf, (O_RDWR | O_CREAT)); 81 T_QUIET; T_ASSERT_POSIX_SUCCESS(fd, "open()"); 82 83 memset_pattern4((char *)buf, pattern, vmsize); 84 write(fd, buf, vmsize); 85 fsync(fd); 86 close(fd); 87 free(buf); 88 buf = NULL; 89 90 // Purge to flush the test file from the filecache 91 vfs_purge(); 92 93 fd = open(tmpf, O_RDWR); 94 T_QUIET; T_ASSERT_POSIX_SUCCESS(fd, "open()"); 95 buf = mmap(NULL, vmsize, PROT_READ, 96 (MAP_FILE | MAP_SHARED), fd, 0); 97 T_QUIET; T_ASSERT_NOTNULL(buf, "mmap()"); 98 99 T_SETUPEND; 100 101 dt_stat_time_t mlock_time = dt_stat_time_create("mlock_duration"); 102 dt_stat_set_variable(mlock_time, "buffer_size_mb", 103 (unsigned int)(vmsize >> 20)); 104 105 T_LOG("Collecting measurements..."); 106 while (!dt_stat_stable(mlock_time)) { T_STAT_MEASURE(mlock_time)107 T_STAT_MEASURE(mlock_time) { 108 ret = mlock(buf, vmsize); 109 } 110 T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "mlock()"); 111 112 ret = munlock(buf, vmsize); 113 T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "munlock()"); 114 115 vfs_purge(); 116 } 117 dt_stat_finalize(mlock_time); 118 119 ret = munmap(buf, vmsize); 120 T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "munmap()"); 121 ret = close(fd); 122 T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "close()"); 123 } 124