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