xref: /xnu-10002.81.5/tests/game_mode.c (revision 5e3eaea39dcf651e66cb99ba7d70e32cc4a99587)
1*5e3eaea3SApple OSS Distributions /*
2*5e3eaea3SApple OSS Distributions  * Copyright (c) 2023 Apple Inc. All rights reserved.
3*5e3eaea3SApple OSS Distributions  *
4*5e3eaea3SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*5e3eaea3SApple OSS Distributions  *
6*5e3eaea3SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*5e3eaea3SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*5e3eaea3SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*5e3eaea3SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*5e3eaea3SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*5e3eaea3SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*5e3eaea3SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*5e3eaea3SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*5e3eaea3SApple OSS Distributions  *
15*5e3eaea3SApple OSS Distributions  * Please obtain a copy of the License at
16*5e3eaea3SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*5e3eaea3SApple OSS Distributions  *
18*5e3eaea3SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*5e3eaea3SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*5e3eaea3SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*5e3eaea3SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*5e3eaea3SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*5e3eaea3SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*5e3eaea3SApple OSS Distributions  * limitations under the License.
25*5e3eaea3SApple OSS Distributions  *
26*5e3eaea3SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*5e3eaea3SApple OSS Distributions  */
28*5e3eaea3SApple OSS Distributions 
29*5e3eaea3SApple OSS Distributions /* test that the header doesn't implicitly depend on others */
30*5e3eaea3SApple OSS Distributions #include <sys/resource_private.h>
31*5e3eaea3SApple OSS Distributions #include <sys/resource.h>
32*5e3eaea3SApple OSS Distributions 
33*5e3eaea3SApple OSS Distributions #include <mach/coalition.h>
34*5e3eaea3SApple OSS Distributions #include <sys/coalition.h>
35*5e3eaea3SApple OSS Distributions #include <libproc.h>
36*5e3eaea3SApple OSS Distributions 
37*5e3eaea3SApple OSS Distributions #include <sys/types.h>
38*5e3eaea3SApple OSS Distributions #include <unistd.h>
39*5e3eaea3SApple OSS Distributions 
40*5e3eaea3SApple OSS Distributions #include <darwintest.h>
41*5e3eaea3SApple OSS Distributions #include <darwintest_utils.h>
42*5e3eaea3SApple OSS Distributions 
43*5e3eaea3SApple OSS Distributions /* TODO: can this come from the right header? */
44*5e3eaea3SApple OSS Distributions #define THREAD_GROUP_FLAGS_GAME_MODE            0x800
45*5e3eaea3SApple OSS Distributions 
46*5e3eaea3SApple OSS Distributions T_GLOBAL_META(T_META_NAMESPACE("xnu.scheduler"),
47*5e3eaea3SApple OSS Distributions     T_META_RADAR_COMPONENT_NAME("xnu"),
48*5e3eaea3SApple OSS Distributions     T_META_RADAR_COMPONENT_VERSION("scheduler"),
49*5e3eaea3SApple OSS Distributions     T_META_OWNER("chimene"),
50*5e3eaea3SApple OSS Distributions     T_META_RUN_CONCURRENTLY(false));
51*5e3eaea3SApple OSS Distributions 
52*5e3eaea3SApple OSS Distributions static void
check_game_mode(bool expected_mode)53*5e3eaea3SApple OSS Distributions check_game_mode(bool expected_mode)
54*5e3eaea3SApple OSS Distributions {
55*5e3eaea3SApple OSS Distributions 	int game_mode = getpriority(PRIO_DARWIN_GAME_MODE, 0);
56*5e3eaea3SApple OSS Distributions 
57*5e3eaea3SApple OSS Distributions 	T_QUIET;
58*5e3eaea3SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(game_mode, "getpriority(PRIO_DARWIN_GAME_MODE)");
59*5e3eaea3SApple OSS Distributions 
60*5e3eaea3SApple OSS Distributions 	T_LOG("pid %d: game mode is: %d", getpid(), game_mode);
61*5e3eaea3SApple OSS Distributions 
62*5e3eaea3SApple OSS Distributions 	if (expected_mode) {
63*5e3eaea3SApple OSS Distributions 		T_QUIET;
64*5e3eaea3SApple OSS Distributions 		T_ASSERT_EQ(game_mode, PRIO_DARWIN_GAME_MODE_ON, "should be on");
65*5e3eaea3SApple OSS Distributions 	} else {
66*5e3eaea3SApple OSS Distributions 		T_QUIET;
67*5e3eaea3SApple OSS Distributions 		T_ASSERT_EQ(game_mode, PRIO_DARWIN_GAME_MODE_OFF, "should be off");
68*5e3eaea3SApple OSS Distributions 	}
69*5e3eaea3SApple OSS Distributions }
70*5e3eaea3SApple OSS Distributions 
71*5e3eaea3SApple OSS Distributions T_DECL(entitled_game_mode, "game mode bit should be settable while entitled")
72*5e3eaea3SApple OSS Distributions {
73*5e3eaea3SApple OSS Distributions 	T_LOG("uid: %d", getuid());
74*5e3eaea3SApple OSS Distributions 
75*5e3eaea3SApple OSS Distributions 	check_game_mode(false);
76*5e3eaea3SApple OSS Distributions 
77*5e3eaea3SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(setpriority(PRIO_DARWIN_GAME_MODE, 0, PRIO_DARWIN_GAME_MODE_ON),
78*5e3eaea3SApple OSS Distributions 	    "setpriority(PRIO_DARWIN_GAME_MODE, 0, PRIO_DARWIN_GAME_MODE_ON)");
79*5e3eaea3SApple OSS Distributions 
80*5e3eaea3SApple OSS Distributions 	check_game_mode(true);
81*5e3eaea3SApple OSS Distributions 
82*5e3eaea3SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(setpriority(PRIO_DARWIN_GAME_MODE, 0, PRIO_DARWIN_GAME_MODE_OFF),
83*5e3eaea3SApple OSS Distributions 	    "setpriority(PRIO_DARWIN_GAME_MODE, 0, PRIO_DARWIN_GAME_MODE_OFF)");
84*5e3eaea3SApple OSS Distributions 
85*5e3eaea3SApple OSS Distributions 	check_game_mode(false);
86*5e3eaea3SApple OSS Distributions }
87*5e3eaea3SApple OSS Distributions 
88*5e3eaea3SApple OSS Distributions T_DECL(entitled_game_mode_read_root, "game mode bit should be readable as root",
89*5e3eaea3SApple OSS Distributions     T_META_ASROOT(true))
90*5e3eaea3SApple OSS Distributions {
91*5e3eaea3SApple OSS Distributions 	T_LOG("uid: %d", getuid());
92*5e3eaea3SApple OSS Distributions 
93*5e3eaea3SApple OSS Distributions 	check_game_mode(false);
94*5e3eaea3SApple OSS Distributions }
95*5e3eaea3SApple OSS Distributions 
96*5e3eaea3SApple OSS Distributions T_DECL(entitled_game_mode_read_notroot, "game mode bit should be readable as not root but entitled",
97*5e3eaea3SApple OSS Distributions     T_META_ASROOT(false))
98*5e3eaea3SApple OSS Distributions {
99*5e3eaea3SApple OSS Distributions 	T_LOG("uid: %d", getuid());
100*5e3eaea3SApple OSS Distributions 
101*5e3eaea3SApple OSS Distributions 	check_game_mode(false);
102*5e3eaea3SApple OSS Distributions }
103*5e3eaea3SApple OSS Distributions 
104*5e3eaea3SApple OSS Distributions static struct coalinfo_debuginfo
get_coal_debuginfo(uint64_t coal_id,char * prefix)105*5e3eaea3SApple OSS Distributions get_coal_debuginfo(uint64_t coal_id, char* prefix)
106*5e3eaea3SApple OSS Distributions {
107*5e3eaea3SApple OSS Distributions 	struct coalinfo_debuginfo coaldebuginfo = {};
108*5e3eaea3SApple OSS Distributions 
109*5e3eaea3SApple OSS Distributions 	int ret = coalition_info_debug_info(coal_id, &coaldebuginfo, sizeof(coaldebuginfo));
110*5e3eaea3SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "coalition_info_debug_info");
111*5e3eaea3SApple OSS Distributions 
112*5e3eaea3SApple OSS Distributions 	T_LOG("coal(%s): %lld, game count %d, flags 0x%x (group 0x%llx, rec %d, focal: %d, nonfocal %d)",
113*5e3eaea3SApple OSS Distributions 	    prefix, coal_id,
114*5e3eaea3SApple OSS Distributions 	    coaldebuginfo.game_task_count, coaldebuginfo.thread_group_flags,
115*5e3eaea3SApple OSS Distributions 	    coaldebuginfo.thread_group_id, coaldebuginfo.thread_group_recommendation,
116*5e3eaea3SApple OSS Distributions 	    coaldebuginfo.focal_task_count, coaldebuginfo.nonfocal_task_count);
117*5e3eaea3SApple OSS Distributions 
118*5e3eaea3SApple OSS Distributions 	return coaldebuginfo;
119*5e3eaea3SApple OSS Distributions }
120*5e3eaea3SApple OSS Distributions 
121*5e3eaea3SApple OSS Distributions static void
check_game_mode_count(uint32_t expected_count)122*5e3eaea3SApple OSS Distributions check_game_mode_count(uint32_t expected_count)
123*5e3eaea3SApple OSS Distributions {
124*5e3eaea3SApple OSS Distributions 	struct proc_pidcoalitioninfo idinfo = {};
125*5e3eaea3SApple OSS Distributions 
126*5e3eaea3SApple OSS Distributions 	int ret = proc_pidinfo(getpid(), PROC_PIDCOALITIONINFO, 0,
127*5e3eaea3SApple OSS Distributions 	    &idinfo, sizeof(idinfo));
128*5e3eaea3SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "proc_pidinfo(... PROC_PIDCOALITIONINFO ...)");
129*5e3eaea3SApple OSS Distributions 
130*5e3eaea3SApple OSS Distributions 	uint64_t res_id = idinfo.coalition_id[COALITION_TYPE_RESOURCE];
131*5e3eaea3SApple OSS Distributions 	uint64_t jet_id = idinfo.coalition_id[COALITION_TYPE_JETSAM];
132*5e3eaea3SApple OSS Distributions 
133*5e3eaea3SApple OSS Distributions 	struct coalinfo_debuginfo coaldebuginfo_res = get_coal_debuginfo(res_id, "res");
134*5e3eaea3SApple OSS Distributions 	struct coalinfo_debuginfo coaldebuginfo_jet = get_coal_debuginfo(jet_id, "jet");
135*5e3eaea3SApple OSS Distributions 
136*5e3eaea3SApple OSS Distributions 	if (expected_count) {
137*5e3eaea3SApple OSS Distributions 		// see COALITION_FOCAL_TASKS_ACCOUNTING for this difference
138*5e3eaea3SApple OSS Distributions #if TARGET_OS_OSX
139*5e3eaea3SApple OSS Distributions 		T_ASSERT_EQ(coaldebuginfo_res.game_task_count, expected_count, "should have game in res coalition");
140*5e3eaea3SApple OSS Distributions 		T_QUIET; T_ASSERT_EQ(coaldebuginfo_jet.game_task_count, 0, "should not have game in jet coalition");
141*5e3eaea3SApple OSS Distributions #else
142*5e3eaea3SApple OSS Distributions 		T_QUIET; T_ASSERT_EQ(coaldebuginfo_res.game_task_count, 0, "should not have game in res coalition");
143*5e3eaea3SApple OSS Distributions 		T_ASSERT_EQ(coaldebuginfo_jet.game_task_count, expected_count, "should have game in jet coalition");
144*5e3eaea3SApple OSS Distributions #endif
145*5e3eaea3SApple OSS Distributions 		T_ASSERT_BITS_SET(coaldebuginfo_jet.thread_group_flags, THREAD_GROUP_FLAGS_GAME_MODE,
146*5e3eaea3SApple OSS Distributions 		    "should have game mode flag in jet coalition"); \
147*5e3eaea3SApple OSS Distributions 	} else {
148*5e3eaea3SApple OSS Distributions #if TARGET_OS_OSX
149*5e3eaea3SApple OSS Distributions 		T_ASSERT_EQ(coaldebuginfo_res.game_task_count, 0, "should not have game in res coalition");
150*5e3eaea3SApple OSS Distributions 		T_QUIET; T_ASSERT_EQ(coaldebuginfo_jet.game_task_count, 0, "should not have game in jet coalition");
151*5e3eaea3SApple OSS Distributions #else
152*5e3eaea3SApple OSS Distributions 		T_QUIET; T_ASSERT_EQ(coaldebuginfo_res.game_task_count, 0, "should not have game in res coalition");
153*5e3eaea3SApple OSS Distributions 		T_ASSERT_EQ(coaldebuginfo_jet.game_task_count, 0, "should not have game in jet coalition");
154*5e3eaea3SApple OSS Distributions #endif
155*5e3eaea3SApple OSS Distributions 		T_ASSERT_BITS_NOTSET(coaldebuginfo_jet.thread_group_flags, THREAD_GROUP_FLAGS_GAME_MODE,
156*5e3eaea3SApple OSS Distributions 		    "should not have game mode flag in jet coalition"); \
157*5e3eaea3SApple OSS Distributions 	}
158*5e3eaea3SApple OSS Distributions 
159*5e3eaea3SApple OSS Distributions 	T_QUIET; T_ASSERT_BITS_NOTSET(coaldebuginfo_res.thread_group_flags, THREAD_GROUP_FLAGS_GAME_MODE,
160*5e3eaea3SApple OSS Distributions 	    "should never have game mode flag in res coalition"); \
161*5e3eaea3SApple OSS Distributions }
162*5e3eaea3SApple OSS Distributions 
163*5e3eaea3SApple OSS Distributions static void
skip_if_unsupported(void)164*5e3eaea3SApple OSS Distributions skip_if_unsupported(void)
165*5e3eaea3SApple OSS Distributions {
166*5e3eaea3SApple OSS Distributions 	int r;
167*5e3eaea3SApple OSS Distributions 	int supported = 0;
168*5e3eaea3SApple OSS Distributions 	size_t supported_size = sizeof(supported);
169*5e3eaea3SApple OSS Distributions 
170*5e3eaea3SApple OSS Distributions 	r = sysctlbyname("kern.thread_groups_supported", &supported, &supported_size,
171*5e3eaea3SApple OSS Distributions 	    NULL, 0);
172*5e3eaea3SApple OSS Distributions 	if (r < 0) {
173*5e3eaea3SApple OSS Distributions 		T_WITH_ERRNO;
174*5e3eaea3SApple OSS Distributions 		T_SKIP("could not find \"kern.thread_groups_supported\" sysctl");
175*5e3eaea3SApple OSS Distributions 	}
176*5e3eaea3SApple OSS Distributions 
177*5e3eaea3SApple OSS Distributions 	if (!supported) {
178*5e3eaea3SApple OSS Distributions 		T_SKIP("test was run even though kern.thread_groups_supported is not 1, see rdar://111297938");
179*5e3eaea3SApple OSS Distributions 	}
180*5e3eaea3SApple OSS Distributions 
181*5e3eaea3SApple OSS Distributions 	r = sysctlbyname("kern.development", &supported, &supported_size,
182*5e3eaea3SApple OSS Distributions 	    NULL, 0);
183*5e3eaea3SApple OSS Distributions 	if (r < 0) {
184*5e3eaea3SApple OSS Distributions 		T_WITH_ERRNO;
185*5e3eaea3SApple OSS Distributions 		T_SKIP("could not find \"kern.development\" sysctl");
186*5e3eaea3SApple OSS Distributions 	}
187*5e3eaea3SApple OSS Distributions 
188*5e3eaea3SApple OSS Distributions 	if (!supported) {
189*5e3eaea3SApple OSS Distributions 		T_SKIP("test was run even though kern.development is not 1, see rdar://111297938");
190*5e3eaea3SApple OSS Distributions 	}
191*5e3eaea3SApple OSS Distributions }
192*5e3eaea3SApple OSS Distributions 
193*5e3eaea3SApple OSS Distributions T_DECL(entitled_game_mode_check_count, "game mode bit should affect coalition thread group flags",
194*5e3eaea3SApple OSS Distributions     T_META_REQUIRES_SYSCTL_EQ("kern.development", 1), T_META_REQUIRES_SYSCTL_EQ("kern.thread_groups_supported", 1))
195*5e3eaea3SApple OSS Distributions {
196*5e3eaea3SApple OSS Distributions 	T_LOG("uid: %d", getuid());
197*5e3eaea3SApple OSS Distributions 	skip_if_unsupported();
198*5e3eaea3SApple OSS Distributions 
199*5e3eaea3SApple OSS Distributions 	check_game_mode(false);
200*5e3eaea3SApple OSS Distributions 	check_game_mode_count(0);
201*5e3eaea3SApple OSS Distributions 
202*5e3eaea3SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(setpriority(PRIO_DARWIN_GAME_MODE, 0, PRIO_DARWIN_GAME_MODE_ON),
203*5e3eaea3SApple OSS Distributions 	    "setpriority(PRIO_DARWIN_GAME_MODE_ON)");
204*5e3eaea3SApple OSS Distributions 
205*5e3eaea3SApple OSS Distributions 	check_game_mode(true);
206*5e3eaea3SApple OSS Distributions 	check_game_mode_count(1);
207*5e3eaea3SApple OSS Distributions 
208*5e3eaea3SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(setpriority(PRIO_DARWIN_GAME_MODE, 0, PRIO_DARWIN_GAME_MODE_OFF),
209*5e3eaea3SApple OSS Distributions 	    "setpriority(PRIO_DARWIN_GAME_MODE_OFF)");
210*5e3eaea3SApple OSS Distributions 
211*5e3eaea3SApple OSS Distributions 	check_game_mode(false);
212*5e3eaea3SApple OSS Distributions 	check_game_mode_count(0);
213*5e3eaea3SApple OSS Distributions }
214*5e3eaea3SApple OSS Distributions 
215*5e3eaea3SApple OSS Distributions T_DECL(game_mode_child_exit, "game mode bit should disappear when child exits",
216*5e3eaea3SApple OSS Distributions     T_META_REQUIRES_SYSCTL_EQ("kern.development", 1), T_META_REQUIRES_SYSCTL_EQ("kern.thread_groups_supported", 1))
217*5e3eaea3SApple OSS Distributions {
218*5e3eaea3SApple OSS Distributions 	T_LOG("uid: %d", getuid());
219*5e3eaea3SApple OSS Distributions 	skip_if_unsupported();
220*5e3eaea3SApple OSS Distributions 
221*5e3eaea3SApple OSS Distributions 	check_game_mode(false);
222*5e3eaea3SApple OSS Distributions 	check_game_mode_count(0);
223*5e3eaea3SApple OSS Distributions 
224*5e3eaea3SApple OSS Distributions 	T_LOG("Spawning child");
225*5e3eaea3SApple OSS Distributions 
226*5e3eaea3SApple OSS Distributions 	pid_t child_pid = fork();
227*5e3eaea3SApple OSS Distributions 
228*5e3eaea3SApple OSS Distributions 	if (child_pid == 0) {
229*5e3eaea3SApple OSS Distributions 		/* child process */
230*5e3eaea3SApple OSS Distributions 
231*5e3eaea3SApple OSS Distributions 		check_game_mode(false);
232*5e3eaea3SApple OSS Distributions 		check_game_mode_count(0);
233*5e3eaea3SApple OSS Distributions 
234*5e3eaea3SApple OSS Distributions 		T_ASSERT_POSIX_SUCCESS(setpriority(PRIO_DARWIN_GAME_MODE, 0, PRIO_DARWIN_GAME_MODE_ON),
235*5e3eaea3SApple OSS Distributions 		    "setpriority(PRIO_DARWIN_GAME_MODE_ON)");
236*5e3eaea3SApple OSS Distributions 
237*5e3eaea3SApple OSS Distributions 		check_game_mode(true);
238*5e3eaea3SApple OSS Distributions 		check_game_mode_count(1);
239*5e3eaea3SApple OSS Distributions 
240*5e3eaea3SApple OSS Distributions 		T_LOG("Exit pid %d with the game mode bit on", getpid());
241*5e3eaea3SApple OSS Distributions 
242*5e3eaea3SApple OSS Distributions 		exit(0);
243*5e3eaea3SApple OSS Distributions 	} else {
244*5e3eaea3SApple OSS Distributions 		T_ASSERT_POSIX_SUCCESS(child_pid, "fork, pid %d", child_pid);
245*5e3eaea3SApple OSS Distributions 
246*5e3eaea3SApple OSS Distributions 		/* wait for child process to exit */
247*5e3eaea3SApple OSS Distributions 		int exit_status = 0, signum = 0;
248*5e3eaea3SApple OSS Distributions 
249*5e3eaea3SApple OSS Distributions 		T_ASSERT_TRUE(dt_waitpid(child_pid, &exit_status, &signum, 5),
250*5e3eaea3SApple OSS Distributions 		    "wait for child (%d) complete", child_pid);
251*5e3eaea3SApple OSS Distributions 
252*5e3eaea3SApple OSS Distributions 		T_QUIET; T_ASSERT_EQ(exit_status, 0, "dt_waitpid: exit_status");
253*5e3eaea3SApple OSS Distributions 		T_QUIET; T_ASSERT_EQ(signum, 0, "dt_waitpid: signum");
254*5e3eaea3SApple OSS Distributions 	}
255*5e3eaea3SApple OSS Distributions 
256*5e3eaea3SApple OSS Distributions 	check_game_mode(false);
257*5e3eaea3SApple OSS Distributions 	check_game_mode_count(0);
258*5e3eaea3SApple OSS Distributions }
259*5e3eaea3SApple OSS Distributions 
260*5e3eaea3SApple OSS Distributions 
261*5e3eaea3SApple OSS Distributions T_DECL(game_mode_double_set_and_child_exit, "game mode bit on parent should stay when game mode child exits",
262*5e3eaea3SApple OSS Distributions     T_META_REQUIRES_SYSCTL_EQ("kern.development", 1), T_META_REQUIRES_SYSCTL_EQ("kern.thread_groups_supported", 1))
263*5e3eaea3SApple OSS Distributions {
264*5e3eaea3SApple OSS Distributions 	T_LOG("uid: %d", getuid());
265*5e3eaea3SApple OSS Distributions 	skip_if_unsupported();
266*5e3eaea3SApple OSS Distributions 
267*5e3eaea3SApple OSS Distributions 	check_game_mode(false);
268*5e3eaea3SApple OSS Distributions 	check_game_mode_count(0);
269*5e3eaea3SApple OSS Distributions 
270*5e3eaea3SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(setpriority(PRIO_DARWIN_GAME_MODE, 0, PRIO_DARWIN_GAME_MODE_ON),
271*5e3eaea3SApple OSS Distributions 	    "setpriority(PRIO_DARWIN_GAME_MODE_ON)");
272*5e3eaea3SApple OSS Distributions 
273*5e3eaea3SApple OSS Distributions 	check_game_mode(true);
274*5e3eaea3SApple OSS Distributions 	check_game_mode_count(1);
275*5e3eaea3SApple OSS Distributions 
276*5e3eaea3SApple OSS Distributions 	pid_t child_pid = fork();
277*5e3eaea3SApple OSS Distributions 
278*5e3eaea3SApple OSS Distributions 	if (child_pid == 0) {
279*5e3eaea3SApple OSS Distributions 		/* child process */
280*5e3eaea3SApple OSS Distributions 
281*5e3eaea3SApple OSS Distributions 		check_game_mode(false);
282*5e3eaea3SApple OSS Distributions 		check_game_mode_count(1);
283*5e3eaea3SApple OSS Distributions 
284*5e3eaea3SApple OSS Distributions 		T_ASSERT_POSIX_SUCCESS(setpriority(PRIO_DARWIN_GAME_MODE, 0, PRIO_DARWIN_GAME_MODE_ON),
285*5e3eaea3SApple OSS Distributions 		    "setpriority(PRIO_DARWIN_GAME_MODE_ON)");
286*5e3eaea3SApple OSS Distributions 
287*5e3eaea3SApple OSS Distributions 		check_game_mode(true);
288*5e3eaea3SApple OSS Distributions 		check_game_mode_count(2);
289*5e3eaea3SApple OSS Distributions 
290*5e3eaea3SApple OSS Distributions 		T_LOG("Exit pid %d with the game mode bit on", getpid());
291*5e3eaea3SApple OSS Distributions 		exit(0);
292*5e3eaea3SApple OSS Distributions 	} else {
293*5e3eaea3SApple OSS Distributions 		T_ASSERT_POSIX_SUCCESS(child_pid, "fork, pid %d", child_pid);
294*5e3eaea3SApple OSS Distributions 
295*5e3eaea3SApple OSS Distributions 		/* wait for child process to exit */
296*5e3eaea3SApple OSS Distributions 		int exit_status = 0, signum = 0;
297*5e3eaea3SApple OSS Distributions 
298*5e3eaea3SApple OSS Distributions 		T_ASSERT_TRUE(dt_waitpid(child_pid, &exit_status, &signum, 5),
299*5e3eaea3SApple OSS Distributions 		    "wait for child (%d) complete", child_pid);
300*5e3eaea3SApple OSS Distributions 
301*5e3eaea3SApple OSS Distributions 		T_QUIET; T_ASSERT_EQ(exit_status, 0, "dt_waitpid: exit_status");
302*5e3eaea3SApple OSS Distributions 		T_QUIET; T_ASSERT_EQ(signum, 0, "dt_waitpid: signum");
303*5e3eaea3SApple OSS Distributions 	}
304*5e3eaea3SApple OSS Distributions 
305*5e3eaea3SApple OSS Distributions 	check_game_mode(true);
306*5e3eaea3SApple OSS Distributions 	check_game_mode_count(1);
307*5e3eaea3SApple OSS Distributions 
308*5e3eaea3SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(setpriority(PRIO_DARWIN_GAME_MODE, 0, PRIO_DARWIN_GAME_MODE_OFF),
309*5e3eaea3SApple OSS Distributions 	    "setpriority(PRIO_DARWIN_GAME_MODE_OFF)");
310*5e3eaea3SApple OSS Distributions 
311*5e3eaea3SApple OSS Distributions 	check_game_mode(false);
312*5e3eaea3SApple OSS Distributions 	check_game_mode_count(0);
313*5e3eaea3SApple OSS Distributions }
314*5e3eaea3SApple OSS Distributions 
315*5e3eaea3SApple OSS Distributions T_DECL(game_mode_double_set_and_child_unset, "game mode bit on parent should stay when game mode child unsets",
316*5e3eaea3SApple OSS Distributions     T_META_REQUIRES_SYSCTL_EQ("kern.development", 1), T_META_REQUIRES_SYSCTL_EQ("kern.thread_groups_supported", 1))
317*5e3eaea3SApple OSS Distributions {
318*5e3eaea3SApple OSS Distributions 	T_LOG("uid: %d", getuid());
319*5e3eaea3SApple OSS Distributions 	skip_if_unsupported();
320*5e3eaea3SApple OSS Distributions 
321*5e3eaea3SApple OSS Distributions 	check_game_mode(false);
322*5e3eaea3SApple OSS Distributions 	check_game_mode_count(0);
323*5e3eaea3SApple OSS Distributions 
324*5e3eaea3SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(setpriority(PRIO_DARWIN_GAME_MODE, 0, PRIO_DARWIN_GAME_MODE_ON),
325*5e3eaea3SApple OSS Distributions 	    "setpriority(PRIO_DARWIN_GAME_MODE_ON)");
326*5e3eaea3SApple OSS Distributions 
327*5e3eaea3SApple OSS Distributions 	check_game_mode(true);
328*5e3eaea3SApple OSS Distributions 	check_game_mode_count(1);
329*5e3eaea3SApple OSS Distributions 
330*5e3eaea3SApple OSS Distributions 	pid_t child_pid = fork();
331*5e3eaea3SApple OSS Distributions 
332*5e3eaea3SApple OSS Distributions 	if (child_pid == 0) {
333*5e3eaea3SApple OSS Distributions 		/* child process */
334*5e3eaea3SApple OSS Distributions 
335*5e3eaea3SApple OSS Distributions 		check_game_mode(false);
336*5e3eaea3SApple OSS Distributions 		check_game_mode_count(1);
337*5e3eaea3SApple OSS Distributions 
338*5e3eaea3SApple OSS Distributions 		T_ASSERT_POSIX_SUCCESS(setpriority(PRIO_DARWIN_GAME_MODE, 0, PRIO_DARWIN_GAME_MODE_ON),
339*5e3eaea3SApple OSS Distributions 		    "setpriority(PRIO_DARWIN_GAME_MODE_ON)");
340*5e3eaea3SApple OSS Distributions 
341*5e3eaea3SApple OSS Distributions 		check_game_mode(true);
342*5e3eaea3SApple OSS Distributions 		check_game_mode_count(2);
343*5e3eaea3SApple OSS Distributions 
344*5e3eaea3SApple OSS Distributions 		T_ASSERT_POSIX_SUCCESS(setpriority(PRIO_DARWIN_GAME_MODE, 0, PRIO_DARWIN_GAME_MODE_OFF),
345*5e3eaea3SApple OSS Distributions 		    "setpriority(PRIO_DARWIN_GAME_MODE_OFF)");
346*5e3eaea3SApple OSS Distributions 
347*5e3eaea3SApple OSS Distributions 		check_game_mode(false);
348*5e3eaea3SApple OSS Distributions 		check_game_mode_count(1);
349*5e3eaea3SApple OSS Distributions 
350*5e3eaea3SApple OSS Distributions 		T_LOG("Exit pid %d with the game mode bit off", getpid());
351*5e3eaea3SApple OSS Distributions 		exit(0);
352*5e3eaea3SApple OSS Distributions 	} else {
353*5e3eaea3SApple OSS Distributions 		T_ASSERT_POSIX_SUCCESS(child_pid, "fork, pid %d", child_pid);
354*5e3eaea3SApple OSS Distributions 
355*5e3eaea3SApple OSS Distributions 		/* wait for child process to exit */
356*5e3eaea3SApple OSS Distributions 		int exit_status = 0, signum = 0;
357*5e3eaea3SApple OSS Distributions 
358*5e3eaea3SApple OSS Distributions 		T_ASSERT_TRUE(dt_waitpid(child_pid, &exit_status, &signum, 5),
359*5e3eaea3SApple OSS Distributions 		    "wait for child (%d) complete", child_pid);
360*5e3eaea3SApple OSS Distributions 
361*5e3eaea3SApple OSS Distributions 		T_QUIET; T_ASSERT_EQ(exit_status, 0, "dt_waitpid: exit_status");
362*5e3eaea3SApple OSS Distributions 		T_QUIET; T_ASSERT_EQ(signum, 0, "dt_waitpid: signum");
363*5e3eaea3SApple OSS Distributions 	}
364*5e3eaea3SApple OSS Distributions 
365*5e3eaea3SApple OSS Distributions 	check_game_mode(true);
366*5e3eaea3SApple OSS Distributions 	check_game_mode_count(1);
367*5e3eaea3SApple OSS Distributions 
368*5e3eaea3SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(setpriority(PRIO_DARWIN_GAME_MODE, 0, PRIO_DARWIN_GAME_MODE_OFF),
369*5e3eaea3SApple OSS Distributions 	    "setpriority(PRIO_DARWIN_GAME_MODE_OFF)");
370*5e3eaea3SApple OSS Distributions 
371*5e3eaea3SApple OSS Distributions 	check_game_mode(false);
372*5e3eaea3SApple OSS Distributions 	check_game_mode_count(0);
373*5e3eaea3SApple OSS Distributions }
374