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