xref: /xnu-11417.121.6/tests/memorystatus/memorystatus_experiment_factors.c (revision a1e26a70f38d1d7daa7b49b258e2f8538ad81650)
1*a1e26a70SApple OSS Distributions /*
2*a1e26a70SApple OSS Distributions  * Copyright (c) 2024 Apple Inc. All rights reserved.
3*a1e26a70SApple OSS Distributions  *
4*a1e26a70SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*a1e26a70SApple OSS Distributions  *
6*a1e26a70SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*a1e26a70SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*a1e26a70SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*a1e26a70SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*a1e26a70SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*a1e26a70SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*a1e26a70SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*a1e26a70SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*a1e26a70SApple OSS Distributions  *
15*a1e26a70SApple OSS Distributions  * Please obtain a copy of the License at
16*a1e26a70SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*a1e26a70SApple OSS Distributions  *
18*a1e26a70SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*a1e26a70SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*a1e26a70SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*a1e26a70SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*a1e26a70SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*a1e26a70SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*a1e26a70SApple OSS Distributions  * limitations under the License.
25*a1e26a70SApple OSS Distributions  *
26*a1e26a70SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*a1e26a70SApple OSS Distributions  */
28*a1e26a70SApple OSS Distributions 
29*a1e26a70SApple OSS Distributions #include <darwintest.h>
30*a1e26a70SApple OSS Distributions #include <darwintest_posix.h>
31*a1e26a70SApple OSS Distributions #include <mach/boolean.h>
32*a1e26a70SApple OSS Distributions #include <mach/vm_page_size.h>
33*a1e26a70SApple OSS Distributions #include <stdint.h>
34*a1e26a70SApple OSS Distributions #include <sys/kern_memorystatus.h>
35*a1e26a70SApple OSS Distributions #include <sys/sysctl.h>
36*a1e26a70SApple OSS Distributions 
37*a1e26a70SApple OSS Distributions T_GLOBAL_META(
38*a1e26a70SApple OSS Distributions 	T_META_NAMESPACE("xnu.memorystatus"),
39*a1e26a70SApple OSS Distributions 	T_META_RADAR_COMPONENT_NAME("xnu"),
40*a1e26a70SApple OSS Distributions 	T_META_RADAR_COMPONENT_VERSION("VM"),
41*a1e26a70SApple OSS Distributions 	T_META_CHECK_LEAKS(false),
42*a1e26a70SApple OSS Distributions 	T_META_RUN_CONCURRENTLY(true),
43*a1e26a70SApple OSS Distributions 	T_META_TAG_VM_PREFERRED,
44*a1e26a70SApple OSS Distributions 	T_META_ASROOT(false),
45*a1e26a70SApple OSS Distributions 	T_META_ENABLED(!TARGET_OS_OSX));
46*a1e26a70SApple OSS Distributions 
47*a1e26a70SApple OSS Distributions T_DECL(page_shortage_threshold_update,
48*a1e26a70SApple OSS Distributions     "Verify that page shortage thresholds can be read/written-to")
49*a1e26a70SApple OSS Distributions {
50*a1e26a70SApple OSS Distributions 	int ret;
51*a1e26a70SApple OSS Distributions 	uint32_t threshold_mb, expected_threshold_pages, threshold_pages;
52*a1e26a70SApple OSS Distributions 	size_t threshold_size = sizeof(threshold_size);
53*a1e26a70SApple OSS Distributions 
54*a1e26a70SApple OSS Distributions 	ret = sysctlbyname("kern.memorystatus.critical_threshold_mb",
55*a1e26a70SApple OSS Distributions 	    &threshold_mb, &threshold_size, NULL, 0);
56*a1e26a70SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(ret, "Critical threshold can be read");
57*a1e26a70SApple OSS Distributions 	ret = sysctlbyname("kern.memorystatus.critical_threshold_mb",
58*a1e26a70SApple OSS Distributions 	    NULL, 0, &threshold_mb, threshold_size);
59*a1e26a70SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(ret, "Critical threshold can be written to");
60*a1e26a70SApple OSS Distributions 	expected_threshold_pages = (threshold_mb << 20) / vm_kernel_page_size;
61*a1e26a70SApple OSS Distributions 	ret = sysctlbyname("kern.memorystatus.critical_threshold_pages",
62*a1e26a70SApple OSS Distributions 	    &threshold_pages, &threshold_size, NULL, 0);
63*a1e26a70SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(ret,
64*a1e26a70SApple OSS Distributions 	    "sysctl(kern.memorystatus.critical_threshold_pages)");
65*a1e26a70SApple OSS Distributions 	T_EXPECT_EQ(threshold_pages, expected_threshold_pages,
66*a1e26a70SApple OSS Distributions 	    "Critical threshold is converted to pages");
67*a1e26a70SApple OSS Distributions 
68*a1e26a70SApple OSS Distributions 	ret = sysctlbyname("kern.memorystatus.idle_threshold_mb",
69*a1e26a70SApple OSS Distributions 	    &threshold_mb, &threshold_size, NULL, 0);
70*a1e26a70SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(ret, "Idle threshold can be read");
71*a1e26a70SApple OSS Distributions 	ret = sysctlbyname("kern.memorystatus.idle_threshold_mb",
72*a1e26a70SApple OSS Distributions 	    NULL, 0, &threshold_mb, threshold_size);
73*a1e26a70SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(ret, "Idle threshold can be written to");
74*a1e26a70SApple OSS Distributions 	expected_threshold_pages = (threshold_mb << 20) / vm_kernel_page_size;
75*a1e26a70SApple OSS Distributions 	ret = sysctlbyname("kern.memorystatus.idle_threshold_pages",
76*a1e26a70SApple OSS Distributions 	    &threshold_pages, &threshold_size, NULL, 0);
77*a1e26a70SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(ret,
78*a1e26a70SApple OSS Distributions 	    "sysctl(kern.memorystatus.idle_threshold_pages)");
79*a1e26a70SApple OSS Distributions 	T_EXPECT_EQ(threshold_pages, expected_threshold_pages,
80*a1e26a70SApple OSS Distributions 	    "Idle threshold is converted to pages");
81*a1e26a70SApple OSS Distributions 
82*a1e26a70SApple OSS Distributions 	ret = sysctlbyname("kern.memorystatus.soft_threshold_mb",
83*a1e26a70SApple OSS Distributions 	    &threshold_mb, &threshold_size, NULL, 0);
84*a1e26a70SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(ret, "Soft threshold can be read");
85*a1e26a70SApple OSS Distributions 	ret = sysctlbyname("kern.memorystatus.soft_threshold_mb",
86*a1e26a70SApple OSS Distributions 	    NULL, 0, &threshold_mb, threshold_size);
87*a1e26a70SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(ret, "Soft threshold can be written to");
88*a1e26a70SApple OSS Distributions 	expected_threshold_pages = (threshold_mb << 20) / vm_kernel_page_size;
89*a1e26a70SApple OSS Distributions 	ret = sysctlbyname("kern.memorystatus.soft_threshold_pages",
90*a1e26a70SApple OSS Distributions 	    &threshold_pages, &threshold_size, NULL, 0);
91*a1e26a70SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(ret,
92*a1e26a70SApple OSS Distributions 	    "sysctl(kern.memorystatus.soft_threshold_pages)");
93*a1e26a70SApple OSS Distributions 	T_EXPECT_EQ(threshold_pages, expected_threshold_pages,
94*a1e26a70SApple OSS Distributions 	    "Soft threshold is converted to pages");
95*a1e26a70SApple OSS Distributions }
96*a1e26a70SApple OSS Distributions 
97*a1e26a70SApple OSS Distributions static boolean_t ballast_drained;
98*a1e26a70SApple OSS Distributions static uint32_t prev_offset_mb;
99*a1e26a70SApple OSS Distributions 
100*a1e26a70SApple OSS Distributions static void
ballast_offset_teardown(void)101*a1e26a70SApple OSS Distributions ballast_offset_teardown(void)
102*a1e26a70SApple OSS Distributions {
103*a1e26a70SApple OSS Distributions 	int ret;
104*a1e26a70SApple OSS Distributions 	ret = sysctlbyname("kern.memorystatus.ballast_offset_mb",
105*a1e26a70SApple OSS Distributions 	    NULL, 0, &prev_offset_mb, sizeof(prev_offset_mb));
106*a1e26a70SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "sysctl(kern.memorystatus.ballast_offset_mb)");
107*a1e26a70SApple OSS Distributions 	ret = sysctlbyname("kern.memorystatus.ballast_drained",
108*a1e26a70SApple OSS Distributions 	    NULL, 0, &ballast_drained, sizeof(ballast_drained));
109*a1e26a70SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "sysctl(kern.memorystatus.ballast_drained)");
110*a1e26a70SApple OSS Distributions }
111*a1e26a70SApple OSS Distributions 
112*a1e26a70SApple OSS Distributions T_DECL(ballast_offset, "Verify that the ballast offset can be set and toggled")
113*a1e26a70SApple OSS Distributions {
114*a1e26a70SApple OSS Distributions 	int ret;
115*a1e26a70SApple OSS Distributions 	uint32_t threshold_mb, expected_threshold_pages, threshold_pages;
116*a1e26a70SApple OSS Distributions 	size_t threshold_size = sizeof(threshold_mb);
117*a1e26a70SApple OSS Distributions 	size_t ballast_size = sizeof(ballast_drained);
118*a1e26a70SApple OSS Distributions 
119*a1e26a70SApple OSS Distributions 	ret = sysctlbyname("kern.memorystatus.ballast_offset_mb",
120*a1e26a70SApple OSS Distributions 	    &prev_offset_mb, &threshold_size, NULL, 0);
121*a1e26a70SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(ret, "Ballast offset can be read");
122*a1e26a70SApple OSS Distributions 
123*a1e26a70SApple OSS Distributions 	threshold_mb = 128;
124*a1e26a70SApple OSS Distributions 	ret = sysctlbyname("kern.memorystatus.ballast_offset_mb",
125*a1e26a70SApple OSS Distributions 	    NULL, 0, &threshold_mb, threshold_size);
126*a1e26a70SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(ret, "Ballast offset can be written to");
127*a1e26a70SApple OSS Distributions 
128*a1e26a70SApple OSS Distributions 	expected_threshold_pages = (threshold_mb << 20) / vm_kernel_page_size;
129*a1e26a70SApple OSS Distributions 	ret = sysctlbyname("kern.memorystatus.ballast_offset_pages",
130*a1e26a70SApple OSS Distributions 	    &threshold_pages, &threshold_size, NULL, 0);
131*a1e26a70SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(ret,
132*a1e26a70SApple OSS Distributions 	    "sysctl(kern.memorystatus.ballast_offset_pages)");
133*a1e26a70SApple OSS Distributions 	T_EXPECT_EQ(threshold_pages, expected_threshold_pages,
134*a1e26a70SApple OSS Distributions 	    "Ballast offset is converted to pages");
135*a1e26a70SApple OSS Distributions 
136*a1e26a70SApple OSS Distributions 	ret = sysctlbyname("kern.memorystatus.ballast_drained",
137*a1e26a70SApple OSS Distributions 	    &ballast_drained, &ballast_size, NULL, 0);
138*a1e26a70SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "sysctl(kern.memorystatus.ballast_drained)");
139*a1e26a70SApple OSS Distributions 	T_LOG("Ballast drained: %d", ballast_drained);
140*a1e26a70SApple OSS Distributions 
141*a1e26a70SApple OSS Distributions 	uint32_t critical_before, idle_before, soft_before;
142*a1e26a70SApple OSS Distributions 	ret = sysctlbyname("kern.memorystatus.soft_threshold_pages",
143*a1e26a70SApple OSS Distributions 	    &soft_before, &threshold_size, NULL, 0);
144*a1e26a70SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(ret,
145*a1e26a70SApple OSS Distributions 	    "sysctl(kern.memorystatus.soft_threshold_pages)");
146*a1e26a70SApple OSS Distributions 	ret = sysctlbyname("kern.memorystatus.idle_threshold_pages",
147*a1e26a70SApple OSS Distributions 	    &idle_before, &threshold_size, NULL, 0);
148*a1e26a70SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(ret,
149*a1e26a70SApple OSS Distributions 	    "sysctl(kern.memorystatus.idle_threshold_pages)");
150*a1e26a70SApple OSS Distributions 	ret = sysctlbyname("kern.memorystatus.critical_threshold_pages",
151*a1e26a70SApple OSS Distributions 	    &critical_before, &threshold_size, NULL, 0);
152*a1e26a70SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(ret,
153*a1e26a70SApple OSS Distributions 	    "sysctl(kern.memorystatus.critical_threshold_pages)");
154*a1e26a70SApple OSS Distributions 	T_LOG("Pre-toggle: crit=%u idle=%u soft=%u", critical_before, idle_before, soft_before);
155*a1e26a70SApple OSS Distributions 
156*a1e26a70SApple OSS Distributions 	T_LOG("Toggling ballast");
157*a1e26a70SApple OSS Distributions 	boolean_t toggled_ballast_drained = !ballast_drained;
158*a1e26a70SApple OSS Distributions 	ret = sysctlbyname("kern.memorystatus.ballast_drained",
159*a1e26a70SApple OSS Distributions 	    NULL, 0, &toggled_ballast_drained, sizeof(prev_offset_mb));
160*a1e26a70SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(ret, "sysctl(kern.memorystatus.ballast_drained");
161*a1e26a70SApple OSS Distributions 	T_ATEND(ballast_offset_teardown);
162*a1e26a70SApple OSS Distributions 
163*a1e26a70SApple OSS Distributions 	uint32_t critical_after, idle_after, soft_after;
164*a1e26a70SApple OSS Distributions 	ret = sysctlbyname("kern.memorystatus.soft_threshold_pages",
165*a1e26a70SApple OSS Distributions 	    &soft_after, &threshold_size, NULL, 0);
166*a1e26a70SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(ret,
167*a1e26a70SApple OSS Distributions 	    "sysctl(kern.memorystatus.soft_threshold_pages)");
168*a1e26a70SApple OSS Distributions 	ret = sysctlbyname("kern.memorystatus.idle_threshold_pages",
169*a1e26a70SApple OSS Distributions 	    &idle_after, &threshold_size, NULL, 0);
170*a1e26a70SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(ret,
171*a1e26a70SApple OSS Distributions 	    "sysctl(kern.memorystatus.idle_threshold_pages)");
172*a1e26a70SApple OSS Distributions 	ret = sysctlbyname("kern.memorystatus.critical_threshold_pages",
173*a1e26a70SApple OSS Distributions 	    &critical_after, &threshold_size, NULL, 0);
174*a1e26a70SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(ret,
175*a1e26a70SApple OSS Distributions 	    "sysctl(kern.memorystatus.critical_threshold_pages)");
176*a1e26a70SApple OSS Distributions 	T_LOG("Post-toggle: crit=%u idle=%u soft=%u", critical_after, idle_after, soft_after);
177*a1e26a70SApple OSS Distributions 
178*a1e26a70SApple OSS Distributions 	if (ballast_drained) {
179*a1e26a70SApple OSS Distributions 		T_QUIET; T_ASSERT_GT(soft_before, soft_after, "Soft threshold decreased");
180*a1e26a70SApple OSS Distributions 		T_EXPECT_EQ(soft_before - soft_after, threshold_pages, "Soft threshold is raised by ballast offset");
181*a1e26a70SApple OSS Distributions 		T_QUIET; T_ASSERT_GT(idle_before, idle_after, "Idle threshold decreased");
182*a1e26a70SApple OSS Distributions 		T_EXPECT_EQ(idle_before - idle_after, threshold_pages, "Idle threshold is raised by ballast offset");
183*a1e26a70SApple OSS Distributions 	} else {
184*a1e26a70SApple OSS Distributions 		T_QUIET; T_ASSERT_LT(soft_before, soft_after, "Soft threshold increased");
185*a1e26a70SApple OSS Distributions 		T_EXPECT_EQ(soft_after - soft_before, threshold_pages, "Soft threshold is raised by ballast offset");
186*a1e26a70SApple OSS Distributions 		T_QUIET; T_ASSERT_LT(idle_before, idle_after, "Idle threshold increased");
187*a1e26a70SApple OSS Distributions 		T_EXPECT_EQ(idle_after - idle_before, threshold_pages, "Idle threshold is raised by ballast offset");
188*a1e26a70SApple OSS Distributions 	}
189*a1e26a70SApple OSS Distributions 
190*a1e26a70SApple OSS Distributions 	T_EXPECT_EQ(critical_before, critical_after, "Critical threshold is unchanged");
191*a1e26a70SApple OSS Distributions }
192