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