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