xref: /xnu-11215.61.5/tests/memorystatus/memorystatus_experiment_factors.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_posix.h>
31*4f1223e8SApple OSS Distributions #include <mach/boolean.h>
32*4f1223e8SApple OSS Distributions #include <mach/vm_page_size.h>
33*4f1223e8SApple OSS Distributions #include <stdint.h>
34*4f1223e8SApple OSS Distributions #include <sys/kern_memorystatus.h>
35*4f1223e8SApple OSS Distributions #include <sys/sysctl.h>
36*4f1223e8SApple OSS Distributions 
37*4f1223e8SApple OSS Distributions T_GLOBAL_META(
38*4f1223e8SApple OSS Distributions 	T_META_NAMESPACE("xnu.memorystatus"),
39*4f1223e8SApple OSS Distributions 	T_META_RADAR_COMPONENT_NAME("xnu"),
40*4f1223e8SApple OSS Distributions 	T_META_RADAR_COMPONENT_VERSION("VM"),
41*4f1223e8SApple OSS Distributions 	T_META_CHECK_LEAKS(false),
42*4f1223e8SApple OSS Distributions 	T_META_RUN_CONCURRENTLY(true),
43*4f1223e8SApple OSS Distributions 	T_META_TAG_VM_PREFERRED,
44*4f1223e8SApple OSS Distributions 	T_META_ASROOT(false),
45*4f1223e8SApple OSS Distributions 	T_META_ENABLED(!TARGET_OS_OSX));
46*4f1223e8SApple OSS Distributions 
47*4f1223e8SApple OSS Distributions T_DECL(page_shortage_threshold_update,
48*4f1223e8SApple OSS Distributions     "Verify that page shortage thresholds can be read/written-to")
49*4f1223e8SApple OSS Distributions {
50*4f1223e8SApple OSS Distributions 	int ret;
51*4f1223e8SApple OSS Distributions 	uint32_t threshold_mb, expected_threshold_pages, threshold_pages;
52*4f1223e8SApple OSS Distributions 	size_t threshold_size = sizeof(threshold_size);
53*4f1223e8SApple OSS Distributions 
54*4f1223e8SApple OSS Distributions 	ret = sysctlbyname("kern.memorystatus.critical_threshold_mb",
55*4f1223e8SApple OSS Distributions 	    &threshold_mb, &threshold_size, NULL, 0);
56*4f1223e8SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(ret, "Critical threshold can be read");
57*4f1223e8SApple OSS Distributions 	ret = sysctlbyname("kern.memorystatus.critical_threshold_mb",
58*4f1223e8SApple OSS Distributions 	    NULL, 0, &threshold_mb, threshold_size);
59*4f1223e8SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(ret, "Critical threshold can be written to");
60*4f1223e8SApple OSS Distributions 	expected_threshold_pages = (threshold_mb << 20) / vm_kernel_page_size;
61*4f1223e8SApple OSS Distributions 	ret = sysctlbyname("kern.memorystatus.critical_threshold_pages",
62*4f1223e8SApple OSS Distributions 	    &threshold_pages, &threshold_size, NULL, 0);
63*4f1223e8SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(ret,
64*4f1223e8SApple OSS Distributions 	    "sysctl(kern.memorystatus.critical_threshold_pages)");
65*4f1223e8SApple OSS Distributions 	T_EXPECT_EQ(threshold_pages, expected_threshold_pages,
66*4f1223e8SApple OSS Distributions 	    "Critical threshold is converted to pages");
67*4f1223e8SApple OSS Distributions 
68*4f1223e8SApple OSS Distributions 	ret = sysctlbyname("kern.memorystatus.idle_threshold_mb",
69*4f1223e8SApple OSS Distributions 	    &threshold_mb, &threshold_size, NULL, 0);
70*4f1223e8SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(ret, "Idle threshold can be read");
71*4f1223e8SApple OSS Distributions 	ret = sysctlbyname("kern.memorystatus.idle_threshold_mb",
72*4f1223e8SApple OSS Distributions 	    NULL, 0, &threshold_mb, threshold_size);
73*4f1223e8SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(ret, "Idle threshold can be written to");
74*4f1223e8SApple OSS Distributions 	expected_threshold_pages = (threshold_mb << 20) / vm_kernel_page_size;
75*4f1223e8SApple OSS Distributions 	ret = sysctlbyname("kern.memorystatus.idle_threshold_pages",
76*4f1223e8SApple OSS Distributions 	    &threshold_pages, &threshold_size, NULL, 0);
77*4f1223e8SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(ret,
78*4f1223e8SApple OSS Distributions 	    "sysctl(kern.memorystatus.idle_threshold_pages)");
79*4f1223e8SApple OSS Distributions 	T_EXPECT_EQ(threshold_pages, expected_threshold_pages,
80*4f1223e8SApple OSS Distributions 	    "Idle threshold is converted to pages");
81*4f1223e8SApple OSS Distributions 
82*4f1223e8SApple OSS Distributions 	ret = sysctlbyname("kern.memorystatus.soft_threshold_mb",
83*4f1223e8SApple OSS Distributions 	    &threshold_mb, &threshold_size, NULL, 0);
84*4f1223e8SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(ret, "Soft threshold can be read");
85*4f1223e8SApple OSS Distributions 	ret = sysctlbyname("kern.memorystatus.soft_threshold_mb",
86*4f1223e8SApple OSS Distributions 	    NULL, 0, &threshold_mb, threshold_size);
87*4f1223e8SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(ret, "Soft threshold can be written to");
88*4f1223e8SApple OSS Distributions 	expected_threshold_pages = (threshold_mb << 20) / vm_kernel_page_size;
89*4f1223e8SApple OSS Distributions 	ret = sysctlbyname("kern.memorystatus.soft_threshold_pages",
90*4f1223e8SApple OSS Distributions 	    &threshold_pages, &threshold_size, NULL, 0);
91*4f1223e8SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(ret,
92*4f1223e8SApple OSS Distributions 	    "sysctl(kern.memorystatus.soft_threshold_pages)");
93*4f1223e8SApple OSS Distributions 	T_EXPECT_EQ(threshold_pages, expected_threshold_pages,
94*4f1223e8SApple OSS Distributions 	    "Soft threshold is converted to pages");
95*4f1223e8SApple OSS Distributions }
96*4f1223e8SApple OSS Distributions 
97*4f1223e8SApple OSS Distributions static boolean_t ballast_drained;
98*4f1223e8SApple OSS Distributions static uint32_t prev_offset_mb;
99*4f1223e8SApple OSS Distributions 
100*4f1223e8SApple OSS Distributions static void
ballast_offset_teardown(void)101*4f1223e8SApple OSS Distributions ballast_offset_teardown(void)
102*4f1223e8SApple OSS Distributions {
103*4f1223e8SApple OSS Distributions 	int ret;
104*4f1223e8SApple OSS Distributions 	ret = sysctlbyname("kern.memorystatus.ballast_offset_mb",
105*4f1223e8SApple OSS Distributions 	    NULL, 0, &prev_offset_mb, sizeof(prev_offset_mb));
106*4f1223e8SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "sysctl(kern.memorystatus.ballast_offset_mb)");
107*4f1223e8SApple OSS Distributions 	ret = sysctlbyname("kern.memorystatus.ballast_drained",
108*4f1223e8SApple OSS Distributions 	    NULL, 0, &ballast_drained, sizeof(ballast_drained));
109*4f1223e8SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "sysctl(kern.memorystatus.ballast_drained)");
110*4f1223e8SApple OSS Distributions }
111*4f1223e8SApple OSS Distributions 
112*4f1223e8SApple OSS Distributions T_DECL(ballast_offset, "Verify that the ballast offset can be set and toggled")
113*4f1223e8SApple OSS Distributions {
114*4f1223e8SApple OSS Distributions 	int ret;
115*4f1223e8SApple OSS Distributions 	uint32_t threshold_mb, expected_threshold_pages, threshold_pages;
116*4f1223e8SApple OSS Distributions 	size_t threshold_size = sizeof(threshold_mb);
117*4f1223e8SApple OSS Distributions 	size_t ballast_size = sizeof(ballast_drained);
118*4f1223e8SApple OSS Distributions 
119*4f1223e8SApple OSS Distributions 	ret = sysctlbyname("kern.memorystatus.ballast_offset_mb",
120*4f1223e8SApple OSS Distributions 	    &prev_offset_mb, &threshold_size, NULL, 0);
121*4f1223e8SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(ret, "Ballast offset can be read");
122*4f1223e8SApple OSS Distributions 
123*4f1223e8SApple OSS Distributions 	threshold_mb = 128;
124*4f1223e8SApple OSS Distributions 	ret = sysctlbyname("kern.memorystatus.ballast_offset_mb",
125*4f1223e8SApple OSS Distributions 	    NULL, 0, &threshold_mb, threshold_size);
126*4f1223e8SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(ret, "Ballast offset can be written to");
127*4f1223e8SApple OSS Distributions 
128*4f1223e8SApple OSS Distributions 	expected_threshold_pages = (threshold_mb << 20) / vm_kernel_page_size;
129*4f1223e8SApple OSS Distributions 	ret = sysctlbyname("kern.memorystatus.ballast_offset_pages",
130*4f1223e8SApple OSS Distributions 	    &threshold_pages, &threshold_size, NULL, 0);
131*4f1223e8SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(ret,
132*4f1223e8SApple OSS Distributions 	    "sysctl(kern.memorystatus.ballast_offset_pages)");
133*4f1223e8SApple OSS Distributions 	T_EXPECT_EQ(threshold_pages, expected_threshold_pages,
134*4f1223e8SApple OSS Distributions 	    "Ballast offset is converted to pages");
135*4f1223e8SApple OSS Distributions 
136*4f1223e8SApple OSS Distributions 	ret = sysctlbyname("kern.memorystatus.ballast_drained",
137*4f1223e8SApple OSS Distributions 	    &ballast_drained, &ballast_size, NULL, 0);
138*4f1223e8SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "sysctl(kern.memorystatus.ballast_drained)");
139*4f1223e8SApple OSS Distributions 	T_LOG("Ballast drained: %d", ballast_drained);
140*4f1223e8SApple OSS Distributions 
141*4f1223e8SApple OSS Distributions 	uint32_t critical_before, idle_before, soft_before;
142*4f1223e8SApple OSS Distributions 	ret = sysctlbyname("kern.memorystatus.soft_threshold_pages",
143*4f1223e8SApple OSS Distributions 	    &soft_before, &threshold_size, NULL, 0);
144*4f1223e8SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(ret,
145*4f1223e8SApple OSS Distributions 	    "sysctl(kern.memorystatus.soft_threshold_pages)");
146*4f1223e8SApple OSS Distributions 	ret = sysctlbyname("kern.memorystatus.idle_threshold_pages",
147*4f1223e8SApple OSS Distributions 	    &idle_before, &threshold_size, NULL, 0);
148*4f1223e8SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(ret,
149*4f1223e8SApple OSS Distributions 	    "sysctl(kern.memorystatus.idle_threshold_pages)");
150*4f1223e8SApple OSS Distributions 	ret = sysctlbyname("kern.memorystatus.critical_threshold_pages",
151*4f1223e8SApple OSS Distributions 	    &critical_before, &threshold_size, NULL, 0);
152*4f1223e8SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(ret,
153*4f1223e8SApple OSS Distributions 	    "sysctl(kern.memorystatus.critical_threshold_pages)");
154*4f1223e8SApple OSS Distributions 	T_LOG("Pre-toggle: crit=%u idle=%u soft=%u", critical_before, idle_before, soft_before);
155*4f1223e8SApple OSS Distributions 
156*4f1223e8SApple OSS Distributions 	T_LOG("Toggling ballast");
157*4f1223e8SApple OSS Distributions 	boolean_t toggled_ballast_drained = !ballast_drained;
158*4f1223e8SApple OSS Distributions 	ret = sysctlbyname("kern.memorystatus.ballast_drained",
159*4f1223e8SApple OSS Distributions 	    NULL, 0, &toggled_ballast_drained, sizeof(prev_offset_mb));
160*4f1223e8SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(ret, "sysctl(kern.memorystatus.ballast_drained");
161*4f1223e8SApple OSS Distributions 	T_ATEND(ballast_offset_teardown);
162*4f1223e8SApple OSS Distributions 
163*4f1223e8SApple OSS Distributions 	uint32_t critical_after, idle_after, soft_after;
164*4f1223e8SApple OSS Distributions 	ret = sysctlbyname("kern.memorystatus.soft_threshold_pages",
165*4f1223e8SApple OSS Distributions 	    &soft_after, &threshold_size, NULL, 0);
166*4f1223e8SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(ret,
167*4f1223e8SApple OSS Distributions 	    "sysctl(kern.memorystatus.soft_threshold_pages)");
168*4f1223e8SApple OSS Distributions 	ret = sysctlbyname("kern.memorystatus.idle_threshold_pages",
169*4f1223e8SApple OSS Distributions 	    &idle_after, &threshold_size, NULL, 0);
170*4f1223e8SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(ret,
171*4f1223e8SApple OSS Distributions 	    "sysctl(kern.memorystatus.idle_threshold_pages)");
172*4f1223e8SApple OSS Distributions 	ret = sysctlbyname("kern.memorystatus.critical_threshold_pages",
173*4f1223e8SApple OSS Distributions 	    &critical_after, &threshold_size, NULL, 0);
174*4f1223e8SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(ret,
175*4f1223e8SApple OSS Distributions 	    "sysctl(kern.memorystatus.critical_threshold_pages)");
176*4f1223e8SApple OSS Distributions 	T_LOG("Post-toggle: crit=%u idle=%u soft=%u", critical_after, idle_after, soft_after);
177*4f1223e8SApple OSS Distributions 
178*4f1223e8SApple OSS Distributions 	if (ballast_drained) {
179*4f1223e8SApple OSS Distributions 		T_QUIET; T_ASSERT_GT(soft_before, soft_after, "Soft threshold decreased");
180*4f1223e8SApple OSS Distributions 		T_EXPECT_EQ(soft_before - soft_after, threshold_pages, "Soft threshold is raised by ballast offset");
181*4f1223e8SApple OSS Distributions 		T_QUIET; T_ASSERT_GT(idle_before, idle_after, "Idle threshold decreased");
182*4f1223e8SApple OSS Distributions 		T_EXPECT_EQ(idle_before - idle_after, threshold_pages, "Idle threshold is raised by ballast offset");
183*4f1223e8SApple OSS Distributions 	} else {
184*4f1223e8SApple OSS Distributions 		T_QUIET; T_ASSERT_LT(soft_before, soft_after, "Soft threshold increased");
185*4f1223e8SApple OSS Distributions 		T_EXPECT_EQ(soft_after - soft_before, threshold_pages, "Soft threshold is raised by ballast offset");
186*4f1223e8SApple OSS Distributions 		T_QUIET; T_ASSERT_LT(idle_before, idle_after, "Idle threshold increased");
187*4f1223e8SApple OSS Distributions 		T_EXPECT_EQ(idle_after - idle_before, threshold_pages, "Idle threshold is raised by ballast offset");
188*4f1223e8SApple OSS Distributions 	}
189*4f1223e8SApple OSS Distributions 
190*4f1223e8SApple OSS Distributions 	T_EXPECT_EQ(critical_before, critical_after, "Critical threshold is unchanged");
191*4f1223e8SApple OSS Distributions }
192