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