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