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