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