xref: /xnu-10002.41.9/tests/task_suspend_stats.c (revision 699cd48037512bf4380799317ca44ca453c82f57)
1*699cd480SApple OSS Distributions /*
2*699cd480SApple OSS Distributions  * Copyright (c) 2023 Apple Inc. All rights reserved.
3*699cd480SApple OSS Distributions  *
4*699cd480SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*699cd480SApple OSS Distributions  *
6*699cd480SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*699cd480SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*699cd480SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*699cd480SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*699cd480SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*699cd480SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*699cd480SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*699cd480SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*699cd480SApple OSS Distributions  *
15*699cd480SApple OSS Distributions  * Please obtain a copy of the License at
16*699cd480SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*699cd480SApple OSS Distributions  *
18*699cd480SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*699cd480SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*699cd480SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*699cd480SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*699cd480SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*699cd480SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*699cd480SApple OSS Distributions  * limitations under the License.
25*699cd480SApple OSS Distributions  *
26*699cd480SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*699cd480SApple OSS Distributions  */
28*699cd480SApple OSS Distributions #include <libproc.h>
29*699cd480SApple OSS Distributions #include <mach/mach.h>
30*699cd480SApple OSS Distributions #include <mach/mach_time.h>
31*699cd480SApple OSS Distributions #include <mach/task.h>
32*699cd480SApple OSS Distributions #include <mach/task_info.h>
33*699cd480SApple OSS Distributions #include <mach-o/dyld.h>
34*699cd480SApple OSS Distributions #include <notify.h>
35*699cd480SApple OSS Distributions #include <signal.h>
36*699cd480SApple OSS Distributions #include <stdint.h>
37*699cd480SApple OSS Distributions #include <strings.h>
38*699cd480SApple OSS Distributions #include <sys/sysctl.h>
39*699cd480SApple OSS Distributions #include <sys/types.h>
40*699cd480SApple OSS Distributions #include <TargetConditionals.h>
41*699cd480SApple OSS Distributions #include <unistd.h>
42*699cd480SApple OSS Distributions 
43*699cd480SApple OSS Distributions #include <darwintest.h>
44*699cd480SApple OSS Distributions #include <darwintest_utils.h>
45*699cd480SApple OSS Distributions 
46*699cd480SApple OSS Distributions T_GLOBAL_META(
47*699cd480SApple OSS Distributions 	T_META_NAMESPACE("kern.task"),
48*699cd480SApple OSS Distributions 	T_META_RADAR_COMPONENT_NAME("xnu"),
49*699cd480SApple OSS Distributions 	T_META_RADAR_COMPONENT_VERSION("performance"),
50*699cd480SApple OSS Distributions 	T_META_OWNER("jarrad"),
51*699cd480SApple OSS Distributions 	T_META_RUN_CONCURRENTLY(true),
52*699cd480SApple OSS Distributions 	T_META_ASROOT(true),
53*699cd480SApple OSS Distributions 	// rdar://112041307
54*699cd480SApple OSS Distributions 	T_META_REQUIRES_SYSCTL_EQ("kern.hv_vmm_present", 0));
55*699cd480SApple OSS Distributions 
56*699cd480SApple OSS Distributions // sleep for 1 sec between suspend/resume
57*699cd480SApple OSS Distributions static unsigned int sleep_duration = 1;
58*699cd480SApple OSS Distributions 
59*699cd480SApple OSS Distributions static uint64_t
get_thread_id(void)60*699cd480SApple OSS Distributions get_thread_id(void)
61*699cd480SApple OSS Distributions {
62*699cd480SApple OSS Distributions 	kern_return_t kr;
63*699cd480SApple OSS Distributions 	mach_msg_type_number_t count = THREAD_IDENTIFIER_INFO_COUNT;
64*699cd480SApple OSS Distributions 	thread_identifier_info_data_t data;
65*699cd480SApple OSS Distributions 	kr = thread_info(mach_thread_self(), THREAD_IDENTIFIER_INFO, (thread_info_t)&data, &count);
66*699cd480SApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "thread_info");
67*699cd480SApple OSS Distributions 	return data.thread_id;
68*699cd480SApple OSS Distributions }
69*699cd480SApple OSS Distributions 
70*699cd480SApple OSS Distributions static void
get_procname(char * dest,size_t size)71*699cd480SApple OSS Distributions get_procname(char *dest, size_t size)
72*699cd480SApple OSS Distributions {
73*699cd480SApple OSS Distributions 	int ret;
74*699cd480SApple OSS Distributions 	ret = proc_name(getpid(), dest, (uint32_t)size);
75*699cd480SApple OSS Distributions 	T_QUIET; T_ASSERT_GE(ret, 0, "proc_name");
76*699cd480SApple OSS Distributions }
77*699cd480SApple OSS Distributions 
78*699cd480SApple OSS Distributions static void
get_stats(task_t task,task_suspend_stats_t _Nonnull out)79*699cd480SApple OSS Distributions get_stats(task_t task, task_suspend_stats_t _Nonnull out)
80*699cd480SApple OSS Distributions {
81*699cd480SApple OSS Distributions 	kern_return_t kr;
82*699cd480SApple OSS Distributions 	mach_msg_type_number_t count = TASK_SUSPEND_STATS_INFO_COUNT;
83*699cd480SApple OSS Distributions 
84*699cd480SApple OSS Distributions 	kr = task_info(task, TASK_SUSPEND_STATS_INFO, (task_info_t)out, &count);
85*699cd480SApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "task_info(TASK_SUSPEND_STATS_INFO)");
86*699cd480SApple OSS Distributions }
87*699cd480SApple OSS Distributions 
88*699cd480SApple OSS Distributions static void
get_sources(task_t task,task_suspend_source_t _Nonnull out)89*699cd480SApple OSS Distributions get_sources(task_t task, task_suspend_source_t _Nonnull out)
90*699cd480SApple OSS Distributions {
91*699cd480SApple OSS Distributions 	kern_return_t kr;
92*699cd480SApple OSS Distributions 	mach_msg_type_number_t count = TASK_SUSPEND_SOURCES_INFO_COUNT;
93*699cd480SApple OSS Distributions 
94*699cd480SApple OSS Distributions 	kr = task_info(task, TASK_SUSPEND_SOURCES_INFO, (task_info_t)out, &count);
95*699cd480SApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "task_info(TASK_SUSPEND_SOURCES_INFO)");
96*699cd480SApple OSS Distributions }
97*699cd480SApple OSS Distributions 
98*699cd480SApple OSS Distributions static void
log_stats(mach_timebase_info_data_t timebase,uint64_t now,const char * name,task_suspend_stats_t _Nonnull stats)99*699cd480SApple OSS Distributions log_stats(mach_timebase_info_data_t timebase, uint64_t now, const char *name, task_suspend_stats_t _Nonnull stats)
100*699cd480SApple OSS Distributions {
101*699cd480SApple OSS Distributions 	uint64_t last_start_ago = (now - stats->tss_last_start) * timebase.numer / timebase.denom;
102*699cd480SApple OSS Distributions 	uint64_t last_end_ago = (now - stats->tss_last_end) * timebase.numer / timebase.denom;
103*699cd480SApple OSS Distributions 	uint64_t last_duration = (stats->tss_last_end - stats->tss_last_start) * timebase.numer / timebase.denom;
104*699cd480SApple OSS Distributions 	uint64_t total_duration = (stats->tss_duration) * timebase.numer / timebase.denom;
105*699cd480SApple OSS Distributions 
106*699cd480SApple OSS Distributions 	uint64_t nanosec = 1000000000llu;
107*699cd480SApple OSS Distributions 	T_LOG("%s: %8lld suspensions, %10lld.%09lld total secs, last start %lld.%09lld secs ago, last end %lld.%09lld secs ago, %lld.%09lld secs long",
108*699cd480SApple OSS Distributions 	    name, stats->tss_count,
109*699cd480SApple OSS Distributions 	    total_duration / nanosec, total_duration % nanosec,
110*699cd480SApple OSS Distributions 	    last_start_ago / nanosec, last_start_ago % nanosec,
111*699cd480SApple OSS Distributions 	    last_end_ago / nanosec, last_end_ago % nanosec,
112*699cd480SApple OSS Distributions 	    last_duration / nanosec, last_duration % nanosec);
113*699cd480SApple OSS Distributions }
114*699cd480SApple OSS Distributions 
115*699cd480SApple OSS Distributions static void
log_sources(mach_timebase_info_data_t timebase,uint64_t now,const char * name,task_suspend_source_array_t _Nonnull sources)116*699cd480SApple OSS Distributions log_sources(mach_timebase_info_data_t timebase, uint64_t now, const char *name, task_suspend_source_array_t _Nonnull sources)
117*699cd480SApple OSS Distributions {
118*699cd480SApple OSS Distributions 	uint64_t nanosec = 1000000000llu;
119*699cd480SApple OSS Distributions 	for (int i = 0; i < TASK_SUSPEND_SOURCES_MAX; i++) {
120*699cd480SApple OSS Distributions 		task_suspend_source_t source = &sources[i];
121*699cd480SApple OSS Distributions 		uint64_t source_ago = (now - source->tss_time) * timebase.numer / timebase.denom;
122*699cd480SApple OSS Distributions 		T_LOG("%s suspender #%d: start %lld.%09lld secs ago, pid %d, tid %llu, procname \"%s\"",
123*699cd480SApple OSS Distributions 		    name, i, source_ago / nanosec, source_ago % nanosec, source->tss_pid, source->tss_tid, source->tss_procname);
124*699cd480SApple OSS Distributions 	}
125*699cd480SApple OSS Distributions }
126*699cd480SApple OSS Distributions 
127*699cd480SApple OSS Distributions static void
log_time(mach_timebase_info_data_t timebase,uint64_t now,uint64_t time,const char * name)128*699cd480SApple OSS Distributions log_time(mach_timebase_info_data_t timebase, uint64_t now, uint64_t time, const char *name)
129*699cd480SApple OSS Distributions {
130*699cd480SApple OSS Distributions 	uint64_t nanosec = 1000000000llu;
131*699cd480SApple OSS Distributions 	uint64_t time_ago = (now - time) * timebase.numer / timebase.denom;
132*699cd480SApple OSS Distributions 	T_LOG("%s: %lld.%09lld secs ago",
133*699cd480SApple OSS Distributions 	    name, time_ago / nanosec, time_ago % nanosec);
134*699cd480SApple OSS Distributions }
135*699cd480SApple OSS Distributions 
136*699cd480SApple OSS Distributions static void
verify_source(task_suspend_source_t source)137*699cd480SApple OSS Distributions verify_source(task_suspend_source_t source)
138*699cd480SApple OSS Distributions {
139*699cd480SApple OSS Distributions 	char procname[65];
140*699cd480SApple OSS Distributions 	get_procname(procname, sizeof(procname));
141*699cd480SApple OSS Distributions 
142*699cd480SApple OSS Distributions 	T_EXPECT_EQ(source->tss_pid, getpid(), "suspend source should mark suspender as current task");
143*699cd480SApple OSS Distributions 	T_EXPECT_EQ(source->tss_tid, get_thread_id(), "suspend source should mark suspender as current thread");
144*699cd480SApple OSS Distributions 	T_EXPECT_GT(source->tss_time, 0ull, "suspend source should have non-zero time");
145*699cd480SApple OSS Distributions 	T_EXPECT_EQ_STR(source->tss_procname, procname, "suspend source should have procname matching current proc");
146*699cd480SApple OSS Distributions }
147*699cd480SApple OSS Distributions 
148*699cd480SApple OSS Distributions static void
verify_stats(mach_timebase_info_data_t timebase,task_suspend_stats_t _Nonnull pre,task_suspend_stats_t _Nonnull post)149*699cd480SApple OSS Distributions verify_stats(mach_timebase_info_data_t timebase, task_suspend_stats_t _Nonnull pre, task_suspend_stats_t _Nonnull post)
150*699cd480SApple OSS Distributions {
151*699cd480SApple OSS Distributions 	uint64_t now = mach_absolute_time();
152*699cd480SApple OSS Distributions 
153*699cd480SApple OSS Distributions 	log_stats(timebase, now, "  pre", pre);
154*699cd480SApple OSS Distributions 	log_stats(timebase, now, " post", post);
155*699cd480SApple OSS Distributions 
156*699cd480SApple OSS Distributions 	int64_t delta_suspensions = (int64_t)(post->tss_count - pre->tss_count);
157*699cd480SApple OSS Distributions 	int64_t delta_duration = (int64_t)(post->tss_duration - pre->tss_duration) * (int64_t)timebase.numer / (int64_t)timebase.denom;
158*699cd480SApple OSS Distributions 	int64_t delta_nsec = delta_duration % 1000000000ll;
159*699cd480SApple OSS Distributions 	if (delta_nsec < 0) {
160*699cd480SApple OSS Distributions 		delta_nsec += 1000000000ll;
161*699cd480SApple OSS Distributions 	}
162*699cd480SApple OSS Distributions 	T_LOG("delta: %+8lld suspensions, %+10lld.%09lld total nsecs", delta_suspensions, delta_duration / 1000000000ll, delta_nsec);
163*699cd480SApple OSS Distributions 
164*699cd480SApple OSS Distributions 	T_EXPECT_LT(pre->tss_count, post->tss_count, "suspension count should increase when task is suspended");
165*699cd480SApple OSS Distributions 	T_EXPECT_LT(pre->tss_duration, post->tss_duration, "suspension duration should increase when task is suspended");
166*699cd480SApple OSS Distributions 	T_EXPECT_LT(post->tss_last_start, post->tss_last_end, "post: suspension should take time");
167*699cd480SApple OSS Distributions }
168*699cd480SApple OSS Distributions 
169*699cd480SApple OSS Distributions static int
spawn_helper(char * helper)170*699cd480SApple OSS Distributions spawn_helper(char *helper)
171*699cd480SApple OSS Distributions {
172*699cd480SApple OSS Distributions 	char **launch_tool_args;
173*699cd480SApple OSS Distributions 	char testpath[PATH_MAX];
174*699cd480SApple OSS Distributions 	uint32_t testpath_buf_size;
175*699cd480SApple OSS Distributions 	int ret;
176*699cd480SApple OSS Distributions 	pid_t child_pid;
177*699cd480SApple OSS Distributions 
178*699cd480SApple OSS Distributions 	testpath_buf_size = sizeof(testpath);
179*699cd480SApple OSS Distributions 	ret = _NSGetExecutablePath(testpath, &testpath_buf_size);
180*699cd480SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_ZERO(ret, "_NSGetExecutablePath");
181*699cd480SApple OSS Distributions 	T_LOG("Executable path: %s", testpath);
182*699cd480SApple OSS Distributions 	launch_tool_args = (char *[]){
183*699cd480SApple OSS Distributions 		testpath,
184*699cd480SApple OSS Distributions 		"-n",
185*699cd480SApple OSS Distributions 		helper,
186*699cd480SApple OSS Distributions 		NULL
187*699cd480SApple OSS Distributions 	};
188*699cd480SApple OSS Distributions 
189*699cd480SApple OSS Distributions 	// Spawn the child process
190*699cd480SApple OSS Distributions 	ret = dt_launch_tool(&child_pid, launch_tool_args, false, NULL, NULL);
191*699cd480SApple OSS Distributions 	if (ret != 0) {
192*699cd480SApple OSS Distributions 		T_LOG("dt_launch tool returned %d with error code %d", ret, errno);
193*699cd480SApple OSS Distributions 	}
194*699cd480SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(child_pid, "dt_launch_tool");
195*699cd480SApple OSS Distributions 
196*699cd480SApple OSS Distributions 	return child_pid;
197*699cd480SApple OSS Distributions }
198*699cd480SApple OSS Distributions 
199*699cd480SApple OSS Distributions static void
wakeup_helper(pid_t pid)200*699cd480SApple OSS Distributions wakeup_helper(pid_t pid)
201*699cd480SApple OSS Distributions {
202*699cd480SApple OSS Distributions 	int ret = kill(pid, SIGKILL);
203*699cd480SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_ZERO(ret, "kill()");
204*699cd480SApple OSS Distributions }
205*699cd480SApple OSS Distributions 
206*699cd480SApple OSS Distributions T_HELPER_DECL(wait_for_finished,
207*699cd480SApple OSS Distributions     "spin waiting for signal",
208*699cd480SApple OSS Distributions     T_META_CHECK_LEAKS(false))
209*699cd480SApple OSS Distributions {
210*699cd480SApple OSS Distributions 	pause();
211*699cd480SApple OSS Distributions 	exit(0);
212*699cd480SApple OSS Distributions }
213*699cd480SApple OSS Distributions 
214*699cd480SApple OSS Distributions T_DECL(get_suspend_stats,
215*699cd480SApple OSS Distributions     "test that task suspension statistics can be read out")
216*699cd480SApple OSS Distributions {
217*699cd480SApple OSS Distributions 	mach_timebase_info_data_t timebase = {0, 0};
218*699cd480SApple OSS Distributions 	mach_timebase_info(&timebase);
219*699cd480SApple OSS Distributions 
220*699cd480SApple OSS Distributions 	struct task_suspend_stats_s stats;
221*699cd480SApple OSS Distributions 
222*699cd480SApple OSS Distributions 	get_stats(current_task(), &stats);
223*699cd480SApple OSS Distributions 	log_stats(timebase, mach_absolute_time(), "stats", &stats);
224*699cd480SApple OSS Distributions }
225*699cd480SApple OSS Distributions 
226*699cd480SApple OSS Distributions T_DECL(get_suspend_sources,
227*699cd480SApple OSS Distributions     "test that task suspension debug info can be read out")
228*699cd480SApple OSS Distributions {
229*699cd480SApple OSS Distributions 	mach_timebase_info_data_t timebase = {0, 0};
230*699cd480SApple OSS Distributions 	mach_timebase_info(&timebase);
231*699cd480SApple OSS Distributions 
232*699cd480SApple OSS Distributions 	task_suspend_source_array_t sources;
233*699cd480SApple OSS Distributions 
234*699cd480SApple OSS Distributions 	get_sources(current_task(), sources);
235*699cd480SApple OSS Distributions 	log_sources(timebase, mach_absolute_time(), "sources", sources);
236*699cd480SApple OSS Distributions }
237*699cd480SApple OSS Distributions 
238*699cd480SApple OSS Distributions T_DECL(suspend_stats_update_on_pidsuspend,
239*699cd480SApple OSS Distributions     "test that task suspension info are updated on pidsuspend")
240*699cd480SApple OSS Distributions {
241*699cd480SApple OSS Distributions 	kern_return_t kr;
242*699cd480SApple OSS Distributions 	pid_t child_pid;
243*699cd480SApple OSS Distributions 	task_t child_task;
244*699cd480SApple OSS Distributions 	int rc;
245*699cd480SApple OSS Distributions 	struct task_suspend_stats_s pre, post;
246*699cd480SApple OSS Distributions 	task_suspend_source_array_t sources;
247*699cd480SApple OSS Distributions 	mach_timebase_info_data_t timebase = {0, 0};
248*699cd480SApple OSS Distributions 
249*699cd480SApple OSS Distributions 	mach_timebase_info(&timebase);
250*699cd480SApple OSS Distributions 
251*699cd480SApple OSS Distributions 	child_pid = spawn_helper("wait_for_finished");
252*699cd480SApple OSS Distributions 	kr = task_for_pid(mach_task_self(), child_pid, &child_task);
253*699cd480SApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "task_for_pid(%d)", child_pid);
254*699cd480SApple OSS Distributions 
255*699cd480SApple OSS Distributions 	get_stats(child_task, &pre);
256*699cd480SApple OSS Distributions 
257*699cd480SApple OSS Distributions 	T_LOG("Suspending helper...");
258*699cd480SApple OSS Distributions 	rc = pid_suspend(child_pid);
259*699cd480SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(rc, "pid_suspend");
260*699cd480SApple OSS Distributions 
261*699cd480SApple OSS Distributions 	T_LOG("Sleeping %d sec...", sleep_duration);
262*699cd480SApple OSS Distributions 	sleep(sleep_duration);
263*699cd480SApple OSS Distributions 
264*699cd480SApple OSS Distributions 	T_LOG("Resuming helper...");
265*699cd480SApple OSS Distributions 	rc = pid_resume(child_pid);
266*699cd480SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(rc, "pid_resume");
267*699cd480SApple OSS Distributions 
268*699cd480SApple OSS Distributions 	get_stats(child_task, &post);
269*699cd480SApple OSS Distributions 	verify_stats(timebase, &pre, &post);
270*699cd480SApple OSS Distributions 	get_sources(child_task, sources);
271*699cd480SApple OSS Distributions 	log_sources(timebase, mach_absolute_time(), "sources", sources);
272*699cd480SApple OSS Distributions 	verify_source(&sources[0]);
273*699cd480SApple OSS Distributions 
274*699cd480SApple OSS Distributions 	wakeup_helper(child_pid);
275*699cd480SApple OSS Distributions 	mach_port_deallocate(mach_task_self(), child_task);
276*699cd480SApple OSS Distributions }
277*699cd480SApple OSS Distributions 
278*699cd480SApple OSS Distributions T_DECL(suspend_stats_update_on_task_suspend,
279*699cd480SApple OSS Distributions     "test that task suspension info are updated on task_suspend")
280*699cd480SApple OSS Distributions {
281*699cd480SApple OSS Distributions 	kern_return_t kr;
282*699cd480SApple OSS Distributions 	pid_t child_pid;
283*699cd480SApple OSS Distributions 	task_t child_task;
284*699cd480SApple OSS Distributions 	struct task_suspend_stats_s pre, post;
285*699cd480SApple OSS Distributions 	task_suspend_source_array_t sources;
286*699cd480SApple OSS Distributions 	mach_timebase_info_data_t timebase = {0, 0};
287*699cd480SApple OSS Distributions 
288*699cd480SApple OSS Distributions 	mach_timebase_info(&timebase);
289*699cd480SApple OSS Distributions 
290*699cd480SApple OSS Distributions 	child_pid = spawn_helper("wait_for_finished");
291*699cd480SApple OSS Distributions 	kr = task_for_pid(mach_task_self(), child_pid, &child_task);
292*699cd480SApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "task_for_pid(%d)", child_pid);
293*699cd480SApple OSS Distributions 
294*699cd480SApple OSS Distributions 	get_stats(child_task, &pre);
295*699cd480SApple OSS Distributions 
296*699cd480SApple OSS Distributions 	T_LOG("Suspending helper...");
297*699cd480SApple OSS Distributions 	kr = task_suspend(child_task);
298*699cd480SApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "task_suspend");
299*699cd480SApple OSS Distributions 
300*699cd480SApple OSS Distributions 	T_LOG("Sleeping %d sec...", sleep_duration);
301*699cd480SApple OSS Distributions 	sleep(sleep_duration);
302*699cd480SApple OSS Distributions 
303*699cd480SApple OSS Distributions 	T_LOG("Resuming helper...");
304*699cd480SApple OSS Distributions 	kr = task_resume(child_task);
305*699cd480SApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "task_resume");
306*699cd480SApple OSS Distributions 
307*699cd480SApple OSS Distributions 	get_stats(child_task, &post);
308*699cd480SApple OSS Distributions 	verify_stats(timebase, &pre, &post);
309*699cd480SApple OSS Distributions 	get_sources(child_task, sources);
310*699cd480SApple OSS Distributions 	log_sources(timebase, mach_absolute_time(), "sources", sources);
311*699cd480SApple OSS Distributions 	verify_source(&sources[0]);
312*699cd480SApple OSS Distributions 
313*699cd480SApple OSS Distributions 	wakeup_helper(child_pid);
314*699cd480SApple OSS Distributions 	mach_port_deallocate(mach_task_self(), child_task);
315*699cd480SApple OSS Distributions }
316*699cd480SApple OSS Distributions 
317*699cd480SApple OSS Distributions T_DECL(suspend_stats_update_on_forkcorpse,
318*699cd480SApple OSS Distributions     "test that task suspension info are updated on fork corpse")
319*699cd480SApple OSS Distributions {
320*699cd480SApple OSS Distributions 	kern_return_t kr;
321*699cd480SApple OSS Distributions 	pid_t child_pid;
322*699cd480SApple OSS Distributions 	task_t child_task;
323*699cd480SApple OSS Distributions 	mach_port_t cp;
324*699cd480SApple OSS Distributions 	struct task_suspend_stats_s pre, post;
325*699cd480SApple OSS Distributions 	task_suspend_source_array_t sources;
326*699cd480SApple OSS Distributions 	mach_timebase_info_data_t timebase = {0, 0};
327*699cd480SApple OSS Distributions 
328*699cd480SApple OSS Distributions 	mach_timebase_info(&timebase);
329*699cd480SApple OSS Distributions 
330*699cd480SApple OSS Distributions 	child_pid = spawn_helper("wait_for_finished");
331*699cd480SApple OSS Distributions 	kr = task_for_pid(mach_task_self(), child_pid, &child_task);
332*699cd480SApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "task_for_pid(%d)", child_pid);
333*699cd480SApple OSS Distributions 
334*699cd480SApple OSS Distributions 	get_stats(child_task, &pre);
335*699cd480SApple OSS Distributions 
336*699cd480SApple OSS Distributions 	T_LOG("Generating corpse of helper...");
337*699cd480SApple OSS Distributions 	kr = task_generate_corpse(child_task, &cp);
338*699cd480SApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "task_generate_corpse");
339*699cd480SApple OSS Distributions 
340*699cd480SApple OSS Distributions 	get_stats(child_task, &post);
341*699cd480SApple OSS Distributions 	verify_stats(timebase, &pre, &post);
342*699cd480SApple OSS Distributions 	get_sources(child_task, sources);
343*699cd480SApple OSS Distributions 	log_sources(timebase, mach_absolute_time(), "sources", sources);
344*699cd480SApple OSS Distributions 	verify_source(&sources[0]);
345*699cd480SApple OSS Distributions 
346*699cd480SApple OSS Distributions 	kr = mach_port_deallocate(mach_task_self(), cp);
347*699cd480SApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "mach_port_deallocate");
348*699cd480SApple OSS Distributions 
349*699cd480SApple OSS Distributions 	wakeup_helper(child_pid);
350*699cd480SApple OSS Distributions 	kr = mach_port_deallocate(mach_task_self(), child_task);
351*699cd480SApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "mach_port_deallocate");
352*699cd480SApple OSS Distributions }
353*699cd480SApple OSS Distributions 
354*699cd480SApple OSS Distributions #define NUM_SUSPEND_RESUME (TASK_SUSPEND_SOURCES_MAX * 2)
355*699cd480SApple OSS Distributions T_DECL(suspend_source_created_for_every_suspend,
356*699cd480SApple OSS Distributions     "test that suspend sources are created for every suspend call")
357*699cd480SApple OSS Distributions {
358*699cd480SApple OSS Distributions 	kern_return_t kr;
359*699cd480SApple OSS Distributions 	pid_t child_pid;
360*699cd480SApple OSS Distributions 	task_t child_task;
361*699cd480SApple OSS Distributions 	struct task_suspend_stats_s pre, post;
362*699cd480SApple OSS Distributions 	task_suspend_source_array_t sources;
363*699cd480SApple OSS Distributions 	mach_timebase_info_data_t timebase = {0, 0};
364*699cd480SApple OSS Distributions 	uint64_t first_suspend_time = 0;
365*699cd480SApple OSS Distributions 
366*699cd480SApple OSS Distributions 	mach_timebase_info(&timebase);
367*699cd480SApple OSS Distributions 
368*699cd480SApple OSS Distributions 	child_pid = spawn_helper("wait_for_finished");
369*699cd480SApple OSS Distributions 	kr = task_for_pid(mach_task_self(), child_pid, &child_task);
370*699cd480SApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "task_for_pid(%d)", child_pid);
371*699cd480SApple OSS Distributions 
372*699cd480SApple OSS Distributions 	get_stats(child_task, &pre);
373*699cd480SApple OSS Distributions 
374*699cd480SApple OSS Distributions 	T_LOG("Suspending helper...");
375*699cd480SApple OSS Distributions 
376*699cd480SApple OSS Distributions 	for (int i = 0; i < NUM_SUSPEND_RESUME; i++) {
377*699cd480SApple OSS Distributions 		kr = task_suspend(child_task);
378*699cd480SApple OSS Distributions 		T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "task_suspend");
379*699cd480SApple OSS Distributions 		if (i == 0) {
380*699cd480SApple OSS Distributions 			first_suspend_time = mach_absolute_time();
381*699cd480SApple OSS Distributions 		}
382*699cd480SApple OSS Distributions 	}
383*699cd480SApple OSS Distributions 
384*699cd480SApple OSS Distributions 	T_LOG("Sleeping %d sec...", sleep_duration);
385*699cd480SApple OSS Distributions 	sleep(sleep_duration);
386*699cd480SApple OSS Distributions 
387*699cd480SApple OSS Distributions 	T_LOG("Resuming helper...");
388*699cd480SApple OSS Distributions 	for (int i = 0; i < NUM_SUSPEND_RESUME; i++) {
389*699cd480SApple OSS Distributions 		kr = task_resume(child_task);
390*699cd480SApple OSS Distributions 		T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "task_resume");
391*699cd480SApple OSS Distributions 	}
392*699cd480SApple OSS Distributions 
393*699cd480SApple OSS Distributions 	uint64_t now = mach_absolute_time();
394*699cd480SApple OSS Distributions 
395*699cd480SApple OSS Distributions 	get_stats(child_task, &post);
396*699cd480SApple OSS Distributions 	verify_stats(timebase, &pre, &post);
397*699cd480SApple OSS Distributions 	get_sources(child_task, sources);
398*699cd480SApple OSS Distributions 	log_sources(timebase, now, "sources", sources);
399*699cd480SApple OSS Distributions 	log_time(timebase, now, first_suspend_time, "first_suspend");
400*699cd480SApple OSS Distributions 
401*699cd480SApple OSS Distributions 	for (int i = 0; i < TASK_SUSPEND_SOURCES_MAX; i++) {
402*699cd480SApple OSS Distributions 		T_LOG("Verifying suspender no. %d", i);
403*699cd480SApple OSS Distributions 		task_suspend_source_t source = &sources[i];
404*699cd480SApple OSS Distributions 		verify_source(source);
405*699cd480SApple OSS Distributions 		T_EXPECT_LT(source->tss_time, post.tss_last_end, "suspend source timestamp should be < last suspension end");
406*699cd480SApple OSS Distributions 		T_EXPECT_GT(source->tss_time, first_suspend_time, "suspend source timestamp should be > first suspend");
407*699cd480SApple OSS Distributions 	}
408*699cd480SApple OSS Distributions 
409*699cd480SApple OSS Distributions 	wakeup_helper(child_pid);
410*699cd480SApple OSS Distributions 	mach_port_deallocate(mach_task_self(), child_task);
411*699cd480SApple OSS Distributions }
412