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