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 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 | #include <darwintest.h> #include <darwintest_utils.h> #include <errno.h> #include <mach/mach.h> #include <mach/mach_error.h> #include <mach/policy.h> #include <mach/task_info.h> #include <mach/thread_info.h> #include <signal.h> #include <stdio.h> #include <stdlib.h> #include <sys/mman.h> #include <sys/sysctl.h> #include <unistd.h> #include "test_utils.h" T_GLOBAL_META(T_META_RUN_CONCURRENTLY(true), T_META_TAG_VM_PREFERRED); /* ************************************************************************************* * Test the task_info API. * * This is a functional test of the following APIs: * TASK_BASIC_INFO_32 * TASK_BASIC2_INFO_32 * TASK_BASIC_INFO_64 * TASK_BASIC_INFO_64_2 * TASK_POWER_INFO_V2 * TASK_FLAGS_INFO * TASK_AFFINITY_TAG_INFO * TASK_THREAD_TIMES_INFO * TASK_ABSOLUTE_TIME_INFO * <rdar://problem/22242021> Add tests to increase code coverage for the task_info API * ************************************************************************************* */ #define TESTPHYSFOOTPRINTVAL 5 #define CANARY 0x0f0f0f0f0f0f0f0fULL #if !defined(CONFIG_EMBEDDED) #define ABSOLUTE_MIN_USER_TIME_DIFF 150 #define ABSOLUTE_MIN_SYSTEM_TIME_DIFF 300 #endif enum info_kind { INFO_32, INFO_64, INFO_32_2, INFO_64_2, INFO_MACH, INFO_MAX }; enum info_get { GET_SUSPEND_COUNT, GET_RESIDENT_SIZE, GET_VIRTUAL_SIZE, GET_USER_TIME, GET_SYS_TIME, GET_POLICY, GET_MAX_RES }; /* * This function uses CPU cycles by doing a factorial computation. */ static void do_factorial_task(void); void test_task_basic_info_32(void); void test_task_basic_info_64(void); void task_basic_info_32_debug(void); void task_basic2_info_32_warmup(void); void test_task_basic_info(enum info_kind kind); uint64_t info_get(enum info_kind kind, enum info_get get, void * data); T_DECL(task_vm_info, "tests task vm info", T_META_ASROOT(true), T_META_LTEPHASE(LTE_POSTINIT)) { kern_return_t err; task_vm_info_data_t vm_info; mach_msg_type_number_t count = TASK_VM_INFO_COUNT; err = task_info(mach_task_self(), TASK_VM_INFO_PURGEABLE, (task_info_t)&vm_info, &count); T_ASSERT_MACH_SUCCESS(err, "verify task_info call succeeded"); T_EXPECT_NE(vm_info.virtual_size, 0ULL, "task_info return value !=0 for virtual_size\n"); T_EXPECT_NE(vm_info.phys_footprint, 0ULL, "task_info return value !=0 for phys_footprint\n"); /* * Test the REV0 version of TASK_VM_INFO. It should not change the value of phys_footprint. */ count = TASK_VM_INFO_REV0_COUNT; vm_info.phys_footprint = TESTPHYSFOOTPRINTVAL; vm_info.min_address = CANARY; vm_info.max_address = CANARY; err = task_info(mach_task_self(), TASK_VM_INFO_PURGEABLE, (task_info_t)&vm_info, &count); T_ASSERT_MACH_SUCCESS(err, "verify task_info call succeeded"); T_EXPECT_EQ(count, TASK_VM_INFO_REV0_COUNT, "task_info count(%d) is equal to TASK_VM_INFO_REV0_COUNT", count); T_EXPECT_NE(vm_info.virtual_size, 0ULL, "task_info --rev0 call does not return 0 for virtual_size"); T_EXPECT_EQ(vm_info.phys_footprint, (unsigned long long)TESTPHYSFOOTPRINTVAL, "task_info --rev0 call returned value %llu for vm_info.phys_footprint. Expected %u since this value should not be " "modified by rev0", vm_info.phys_footprint, TESTPHYSFOOTPRINTVAL); T_EXPECT_EQ(vm_info.min_address, CANARY, "task_info --rev0 call returned value 0x%llx for vm_info.min_address. Expected 0x%llx since this value should not " "be modified by rev0", vm_info.min_address, CANARY); T_EXPECT_EQ(vm_info.max_address, CANARY, "task_info --rev0 call returned value 0x%llx for vm_info.max_address. Expected 0x%llx since this value should not " "be modified by rev0", vm_info.max_address, CANARY); /* * Test the REV1 version of TASK_VM_INFO. */ count = TASK_VM_INFO_REV1_COUNT; vm_info.phys_footprint = TESTPHYSFOOTPRINTVAL; vm_info.min_address = CANARY; vm_info.max_address = CANARY; err = task_info(mach_task_self(), TASK_VM_INFO_PURGEABLE, (task_info_t)&vm_info, &count); T_ASSERT_MACH_SUCCESS(err, "verify task_info call succeeded"); T_EXPECT_EQ(count, TASK_VM_INFO_REV1_COUNT, "task_info count(%d) is equal to TASK_VM_INFO_REV1_COUNT", count); T_EXPECT_NE(vm_info.virtual_size, 0ULL, "task_info --rev1 call does not return 0 for virtual_size"); T_EXPECT_NE(vm_info.phys_footprint, (unsigned long long)TESTPHYSFOOTPRINTVAL, "task_info --rev1 call returned value %llu for vm_info.phys_footprint. Expected value is anything other than %u " "since this value should not be modified by rev1", vm_info.phys_footprint, TESTPHYSFOOTPRINTVAL); T_EXPECT_EQ(vm_info.min_address, CANARY, "task_info --rev1 call returned value 0x%llx for vm_info.min_address. Expected 0x%llx since this value should not " "be modified by rev1", vm_info.min_address, CANARY); T_EXPECT_EQ(vm_info.max_address, CANARY, "task_info --rev1 call returned value 0x%llx for vm_info.max_address. Expected 0x%llx since this value should not " "be modified by rev1", vm_info.max_address, CANARY); /* * Test the REV2 version of TASK_VM_INFO. */ count = TASK_VM_INFO_REV2_COUNT; vm_info.phys_footprint = TESTPHYSFOOTPRINTVAL; vm_info.min_address = CANARY; vm_info.max_address = CANARY; err = task_info(mach_task_self(), TASK_VM_INFO_PURGEABLE, (task_info_t)&vm_info, &count); T_ASSERT_MACH_SUCCESS(err, "verify task_info call succeeded"); T_EXPECT_EQ(count, TASK_VM_INFO_REV2_COUNT, "task_info count(%d) is equal to TASK_VM_INFO_REV2_COUNT\n", count); T_EXPECT_NE(vm_info.virtual_size, 0ULL, "task_info --rev2 call does not return 0 for virtual_size\n"); T_EXPECT_NE(vm_info.phys_footprint, (unsigned long long)TESTPHYSFOOTPRINTVAL, "task_info --rev2 call returned value %llu for vm_info.phys_footprint. Expected anything other than %u since this " "value should be modified by rev2", vm_info.phys_footprint, TESTPHYSFOOTPRINTVAL); T_EXPECT_NE(vm_info.min_address, CANARY, "task_info --rev2 call returned value 0x%llx for vm_info.min_address. Expected anything other than 0x%llx since " "this value should be modified by rev2", vm_info.min_address, CANARY); T_EXPECT_NE(vm_info.max_address, CANARY, "task_info --rev2 call returned value 0x%llx for vm_info.max_address. Expected anything other than 0x%llx since " "this value should be modified by rev2", vm_info.max_address, CANARY); /* * Test the REV4 version of TASK_VM_INFO. */ count = TASK_VM_INFO_REV4_COUNT; vm_info.phys_footprint = TESTPHYSFOOTPRINTVAL; vm_info.min_address = CANARY; vm_info.max_address = CANARY; vm_info.limit_bytes_remaining = CANARY; err = task_info(mach_task_self(), TASK_VM_INFO_PURGEABLE, (task_info_t)&vm_info, &count); T_ASSERT_MACH_SUCCESS(err, "verify task_info call succeeded"); T_EXPECT_EQ(count, TASK_VM_INFO_REV4_COUNT, "task_info count(%d) is equal to TASK_VM_INFO_REV4_COUNT\n", count); T_EXPECT_NE(vm_info.phys_footprint, (unsigned long long)TESTPHYSFOOTPRINTVAL, "task_info --rev4 call returned value %llu for vm_info.phys_footprint. Expected anything other than %u since this " "value should be modified by rev4", vm_info.phys_footprint, TESTPHYSFOOTPRINTVAL); T_EXPECT_NE(vm_info.min_address, CANARY, "task_info --rev4 call returned value 0x%llx for vm_info.min_address. Expected anything other than 0x%llx since " "this value should be modified by rev4", vm_info.min_address, CANARY); T_EXPECT_NE(vm_info.max_address, CANARY, "task_info --rev4 call returned value 0x%llx for vm_info.max_address. Expected anything other than 0x%llx since " "this value should be modified by rev4", vm_info.max_address, CANARY); T_EXPECT_NE(vm_info.limit_bytes_remaining, CANARY, "task_info --rev4 call returned value 0x%llx for vm_info.limit_bytes_remaining. Expected anything other than 0x%llx since " "this value should be modified by rev4", vm_info.limit_bytes_remaining, CANARY); } T_DECL(host_debug_info, "tests host debug info", T_META_ASROOT(true), T_META_LTEPHASE(LTE_POSTINIT)) { T_SETUPBEGIN; int is_dev = is_development_kernel(); T_QUIET; T_ASSERT_TRUE(is_dev, "verify development kernel is running"); T_SETUPEND; kern_return_t err; mach_port_t host; host_debug_info_internal_data_t debug_info; mach_msg_type_number_t count = HOST_DEBUG_INFO_INTERNAL_COUNT; host = mach_host_self(); err = host_info(host, HOST_DEBUG_INFO_INTERNAL, (host_info_t)&debug_info, &count); T_ASSERT_MACH_SUCCESS(err, "verify host_info call succeeded"); } T_DECL(task_debug_info, "tests task debug info", T_META_ASROOT(true), T_META_LTEPHASE(LTE_POSTINIT)) { T_SETUPBEGIN; int is_dev = is_development_kernel(); T_QUIET; T_ASSERT_TRUE(is_dev, "verify development kernel is running"); T_SETUPEND; kern_return_t err; task_debug_info_internal_data_t debug_info; mach_msg_type_number_t count = TASK_DEBUG_INFO_INTERNAL_COUNT; err = task_info(mach_task_self(), TASK_DEBUG_INFO_INTERNAL, (task_info_t)&debug_info, &count); T_ASSERT_MACH_SUCCESS(err, "verify task_info call succeeded"); } T_DECL(thread_debug_info, "tests thread debug info", T_META_ASROOT(true), T_META_LTEPHASE(LTE_POSTINIT)) { T_SETUPBEGIN; int is_dev = is_development_kernel(); T_QUIET; T_ASSERT_TRUE(is_dev, "verify development kernel is running"); T_SETUPEND; kern_return_t err; thread_debug_info_internal_data_t debug_info; mach_msg_type_number_t count = THREAD_DEBUG_INFO_INTERNAL_COUNT; err = thread_info(mach_thread_self(), THREAD_DEBUG_INFO_INTERNAL, (thread_info_t)&debug_info, &count); T_ASSERT_MACH_SUCCESS(err, "verify task_info call succeeded"); } static void do_factorial_task() { int number = 20; int factorial = 1; int i; for (i = 1; i <= number; i++) { factorial *= i; } return; } T_DECL(task_thread_times_info, "tests task thread times info", T_META_ASROOT(true), T_META_LTEPHASE(LTE_POSTINIT)) { T_SETUPBEGIN; int is_dev = is_development_kernel(); T_QUIET; T_ASSERT_TRUE(is_dev, "verify development kernel is running"); T_SETUPEND; kern_return_t err; task_thread_times_info_data_t thread_times_info_data; task_thread_times_info_data_t thread_times_info_data_new; mach_msg_type_number_t count = TASK_THREAD_TIMES_INFO_COUNT; err = task_info(mach_task_self(), TASK_THREAD_TIMES_INFO, (task_info_t)&thread_times_info_data, &count); T_ASSERT_MACH_SUCCESS(err, "verify task_info call succeeded"); do_factorial_task(); err = task_info(mach_task_self(), TASK_THREAD_TIMES_INFO, (task_info_t)&thread_times_info_data_new, &count); T_ASSERT_MACH_SUCCESS(err, "verify task_info call succeeded"); /* * The difference is observed to be less than 30 microseconds for user_time * and less than 50 microseconds for system_time. This observation was done for over * 1000 runs. */ T_EXPECT_FALSE((thread_times_info_data_new.user_time.seconds - thread_times_info_data.user_time.seconds) != 0 || (thread_times_info_data_new.system_time.seconds - thread_times_info_data.system_time.seconds) != 0, "Tests whether the difference between thread times is greater than the allowed limit"); /* * This is a negative case. */ count--; err = task_info(mach_task_self(), TASK_THREAD_TIMES_INFO, (task_info_t)&thread_times_info_data, &count); T_ASSERT_MACH_ERROR(err, KERN_INVALID_ARGUMENT, "Negative test case: task_info should verify that count is at least equal to what is defined in API."); } T_DECL(task_absolutetime_info, "tests task absolute time info", T_META_ASROOT(true), T_META_LTEPHASE(LTE_POSTINIT)) { T_SETUPBEGIN; int is_dev = is_development_kernel(); T_QUIET; T_ASSERT_TRUE(is_dev, "verify development kernel is running"); T_SETUPEND; kern_return_t err; uint64_t user_time_diff, system_time_diff; task_absolutetime_info_data_t absolute_time_info_data; task_absolutetime_info_data_t absolute_time_info_data_new; mach_msg_type_number_t count = TASK_ABSOLUTETIME_INFO_COUNT; err = task_info(mach_task_self(), TASK_ABSOLUTETIME_INFO, (task_info_t)&absolute_time_info_data, &count); T_ASSERT_MACH_SUCCESS(err, "verify task_info call succeeded"); do_factorial_task(); err = task_info(mach_task_self(), TASK_ABSOLUTETIME_INFO, (task_info_t)&absolute_time_info_data_new, &count); T_ASSERT_MACH_SUCCESS(err, "verify task_info call succeeded"); user_time_diff = absolute_time_info_data_new.total_user - absolute_time_info_data.total_user; system_time_diff = absolute_time_info_data_new.total_system - absolute_time_info_data.total_system; #if !defined(__arm64__) /* * On embedded devices the difference is always zero. * On non-embedded devices the difference occurs in this range. This was observed over ~10000 runs. */ T_EXPECT_FALSE(user_time_diff < ABSOLUTE_MIN_USER_TIME_DIFF || system_time_diff < ABSOLUTE_MIN_SYSTEM_TIME_DIFF, "Tests whether the difference between thread times is greater than the expected range"); #endif if (absolute_time_info_data.threads_user <= 0) { int precise_time_val = 0; size_t len = sizeof(size_t); T_LOG("User threads time is zero. This should only happen rarely and when precise_user_time is off"); err = sysctlbyname("kern.precise_user_kernel_time", &precise_time_val, &len, NULL, 0); T_EXPECT_POSIX_SUCCESS(err, "performing sysctl to check precise_user_time"); T_LOG("kern.precise_user_kernel_time val = %d", precise_time_val); T_EXPECT_FALSE(precise_time_val, "user thread time should only be zero when precise_user_kernel_time is disabled"); } else { T_PASS("task_info should return non-zero value for user threads time = %llu", absolute_time_info_data.threads_user); } #if !defined(__arm64__) /* * On iOS, system threads are always zero. On OS X this value can be some large positive number. * There is no real way to estimate the exact amount. */ T_EXPECT_NE(absolute_time_info_data.threads_system, 0ULL, "task_info should return non-zero value for system threads time = %llu", absolute_time_info_data.threads_system); #endif /* * This is a negative case. */ count--; err = task_info(mach_task_self(), TASK_ABSOLUTETIME_INFO, (task_info_t)&absolute_time_info_data_new, &count); T_ASSERT_MACH_ERROR(err, KERN_INVALID_ARGUMENT, "Negative test case: task_info should verify that count is at least equal to what is defined in API."); } T_DECL(task_affinity_tag_info, "tests task_affinity_tag_info", T_META_ASROOT(true), T_META_LTEPHASE(LTE_POSTINIT)) { T_SETUPBEGIN; int is_dev = is_development_kernel(); T_QUIET; T_ASSERT_TRUE(is_dev, "verify development kernel is running"); T_SETUPEND; kern_return_t err; task_affinity_tag_info_data_t affinity_tag_info_data; mach_msg_type_number_t count = TASK_AFFINITY_TAG_INFO_COUNT; err = task_info(mach_task_self(), TASK_AFFINITY_TAG_INFO, (task_info_t)&affinity_tag_info_data, &count); T_ASSERT_MACH_SUCCESS(err, "verify task_info call succeeded"); /* * The affinity is not set by default, hence expecting a zero value. */ T_ASSERT_FALSE(affinity_tag_info_data.min != 0 || affinity_tag_info_data.max != 0, "task_info call returns non-zero min or max value"); /* * This is a negative case. */ count--; err = task_info(mach_task_self(), TASK_AFFINITY_TAG_INFO, (task_info_t)&affinity_tag_info_data, &count); T_ASSERT_MACH_ERROR(err, KERN_INVALID_ARGUMENT, "Negative test case: task_info should verify that count is at least equal to what is defined in API."); } T_DECL(task_flags_info, "tests task_flags_info", T_META_ASROOT(true), T_META_LTEPHASE(LTE_POSTINIT)) { T_SETUPBEGIN; int is_dev = is_development_kernel(); T_QUIET; T_ASSERT_TRUE(is_dev, "verify development kernel is running"); T_SETUPEND; kern_return_t err; task_flags_info_data_t flags_info_data; mach_msg_type_number_t count = TASK_FLAGS_INFO_COUNT; err = task_info(mach_task_self(), TASK_FLAGS_INFO, (task_info_t)&flags_info_data, &count); T_ASSERT_MACH_SUCCESS(err, "verify task_info call succeeded"); /* Change for 32-bit arch possibility?*/ T_ASSERT_EQ((flags_info_data.flags & (unsigned int)(~(TF_LP64 | TF_64B_DATA))), 0U, "task_info should only give out 64-bit addr/data flags"); /* * This is a negative case. */ count--; err = task_info(mach_task_self(), TASK_FLAGS_INFO, (task_info_t)&flags_info_data, &count); T_ASSERT_MACH_ERROR(err, KERN_INVALID_ARGUMENT, "Negative test case: task_info should verify that count is at least equal to what is defined in API."); } T_DECL(task_power_info_v2, "tests task_power_info_v2", T_META_ASROOT(true), T_META_LTEPHASE(LTE_POSTINIT), T_META_TAG_VM_NOT_ELIGIBLE) { T_SETUPBEGIN; int is_dev = is_development_kernel(); T_QUIET; T_ASSERT_TRUE(is_dev, "verify development kernel is running"); T_SETUPEND; kern_return_t err; task_power_info_v2_data_t power_info_data_v2; task_power_info_v2_data_t power_info_data_v2_new; mach_msg_type_number_t count = TASK_POWER_INFO_V2_COUNT; sleep(1); err = task_info(mach_task_self(), TASK_POWER_INFO_V2, (task_info_t)&power_info_data_v2, &count); T_ASSERT_MACH_SUCCESS(err, "verify task_info call succeeded"); T_ASSERT_LE(power_info_data_v2.gpu_energy.task_gpu_utilisation, 0ULL, "verified task_info call shows zero GPU utilization for non-GPU task"); do_factorial_task(); /* * Verify the cpu_energy parameters. */ err = task_info(mach_task_self(), TASK_POWER_INFO_V2, (task_info_t)&power_info_data_v2_new, &count); T_ASSERT_MACH_SUCCESS(err, "verify task_info call succeeded"); #if !defined(__arm64__) /* * iOS does not have system_time. */ T_ASSERT_GT(power_info_data_v2_new.cpu_energy.total_user, power_info_data_v2.cpu_energy.total_user, "task_info call returns valid user time"); T_ASSERT_GT(power_info_data_v2_new.cpu_energy.total_system, power_info_data_v2.cpu_energy.total_system, "task_info call returns valid system time"); #endif T_ASSERT_GE(power_info_data_v2.cpu_energy.task_interrupt_wakeups, 1ULL, "verify task_info call returns non-zero value for interrupt_wakeup (ret value = %llu)", power_info_data_v2.cpu_energy.task_interrupt_wakeups); #if !defined(__arm64__) if (power_info_data_v2.cpu_energy.task_platform_idle_wakeups != 0) { T_LOG("task_info call returned %llu for platform_idle_wakeup", power_info_data_v2.cpu_energy.task_platform_idle_wakeups); } #endif count = TASK_POWER_INFO_V2_COUNT_OLD; err = task_info(mach_task_self(), TASK_POWER_INFO_V2, (task_info_t)&power_info_data_v2, &count); T_ASSERT_MACH_SUCCESS(err, "verify task_info call succeeded"); /* * This is a negative case. */ count--; err = task_info(mach_task_self(), TASK_POWER_INFO_V2, (task_info_t)&power_info_data_v2, &count); T_ASSERT_MACH_ERROR(err, KERN_INVALID_ARGUMENT, "Negative test case: task_info should verify that count is at least equal to what is defined in API. Call " "returns errno %d:%s", err, mach_error_string(err)); } T_DECL(test_task_basic_info_32, "tests TASK_BASIC_INFO_32", T_META_ASROOT(true), T_META_LTEPHASE(LTE_POSTINIT), T_META_TAG_VM_PREFERRED) { test_task_basic_info(INFO_32); } T_DECL(test_task_basic_info_32_2, "tests TASK_BASIC_INFO_32_2", T_META_ASROOT(true), T_META_LTEPHASE(LTE_POSTINIT), T_META_TAG_VM_PREFERRED) { test_task_basic_info(INFO_32_2); } #if defined(__arm64__) T_DECL(test_task_basic_info_64i_2, "tests TASK_BASIC_INFO_64_2", T_META_ASROOT(true), T_META_LTEPHASE(LTE_POSTINIT), T_META_TAG_VM_PREFERRED) { test_task_basic_info(INFO_64_2); } #else T_DECL(test_task_basic_info_64, "tests TASK_BASIC_INFO_64", T_META_ASROOT(true), T_META_LTEPHASE(LTE_POSTINIT)) { test_task_basic_info(INFO_64); } #endif /* defined(__arm64__) */ T_DECL(test_mach_task_basic_info, "tests MACH_TASK_BASIC_INFO", T_META_ASROOT(true), T_META_LTEPHASE(LTE_POSTINIT)) { test_task_basic_info(INFO_MACH); } void test_task_basic_info(enum info_kind kind) { #define BEFORE 0 #define AFTER 1 T_SETUPBEGIN; int is_dev = is_development_kernel(); T_QUIET; T_ASSERT_TRUE(is_dev, "verify development kernel is running"); T_SETUPEND; task_info_t info_data[2]; task_basic_info_32_data_t basic_info_32_data[2]; #if defined(__arm64__) task_basic_info_64_2_data_t basic_info_64_2_data[2]; #else task_basic_info_64_data_t basic_info_64_data[2]; #endif /* defined(__arm64__) */ mach_task_basic_info_data_t mach_basic_info_data[2]; kern_return_t kr; mach_msg_type_number_t count; task_flavor_t flavor = 0; integer_t suspend_count; uint64_t resident_size_diff; uint64_t virtual_size_diff; void * tmp_map = NULL; pid_t child_pid; mach_port_name_t child_task; /*for dt_waitpid*/ int timeout = 10; // change to max timeout int exit_status = 0; switch (kind) { case INFO_32: case INFO_32_2: info_data[BEFORE] = (task_info_t)&basic_info_32_data[BEFORE]; info_data[AFTER] = (task_info_t)&basic_info_32_data[AFTER]; count = TASK_BASIC_INFO_32_COUNT; flavor = TASK_BASIC_INFO_32; if (kind == INFO_32_2) { flavor = TASK_BASIC2_INFO_32; } break; #if defined(__arm64__) case INFO_64: T_ASSERT_FAIL("invalid basic info kind"); break; case INFO_64_2: info_data[BEFORE] = (task_info_t)&basic_info_64_2_data[BEFORE]; info_data[AFTER] = (task_info_t)&basic_info_64_2_data[AFTER]; count = TASK_BASIC_INFO_64_2_COUNT; flavor = TASK_BASIC_INFO_64_2; break; #else case INFO_64: info_data[BEFORE] = (task_info_t)&basic_info_64_data[BEFORE]; info_data[AFTER] = (task_info_t)&basic_info_64_data[AFTER]; count = TASK_BASIC_INFO_64_COUNT; flavor = TASK_BASIC_INFO_64; break; case INFO_64_2: T_ASSERT_FAIL("invalid basic info kind"); break; #endif /* defined(__arm64__) */ case INFO_MACH: info_data[BEFORE] = (task_info_t)&mach_basic_info_data[BEFORE]; info_data[AFTER] = (task_info_t)&mach_basic_info_data[AFTER]; count = MACH_TASK_BASIC_INFO_COUNT; flavor = MACH_TASK_BASIC_INFO; break; case INFO_MAX: default: T_ASSERT_FAIL("invalid basic info kind"); break; } kr = task_info(mach_task_self(), flavor, info_data[BEFORE], &count); T_ASSERT_MACH_SUCCESS(kr, "verify task_info succeeded"); do_factorial_task(); /* * Allocate virtual and resident memory. */ tmp_map = mmap(0, PAGE_SIZE, PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0); T_WITH_ERRNO; T_EXPECT_NE(tmp_map, MAP_FAILED, "verify mmap call is successful"); memset(tmp_map, 'm', PAGE_SIZE); child_pid = fork(); T_ASSERT_POSIX_SUCCESS(child_pid, "verify process can be forked"); if (child_pid == 0) { /* * This will suspend the child process. */ kr = task_suspend(mach_task_self()); exit(kr); } /* * Wait for the child process to suspend itself. */ sleep(1); kr = task_for_pid(mach_task_self(), child_pid, &child_task); T_ASSERT_MACH_SUCCESS(kr, "verify task_for_pid succeeded. check sudo if failed"); /* * Verify the suspend_count for child and resume it. */ kr = task_info(child_task, flavor, info_data[AFTER], &count); T_ASSERT_MACH_SUCCESS(kr, "verify task_info call succeeded"); suspend_count = (integer_t)(info_get(kind, GET_SUSPEND_COUNT, info_data[AFTER])); T_ASSERT_EQ(suspend_count, 1, "verify task_info shows correct suspend_count"); kr = task_resume(child_task); T_ASSERT_MACH_SUCCESS(kr, "verify task_resume succeeded"); /* * reap kr from task_suspend call in child */ if (dt_waitpid(child_pid, &exit_status, NULL, timeout)) { T_ASSERT_MACH_SUCCESS(exit_status, "verify child task_suspend is successful"); } else { T_FAIL("dt_waitpid failed"); } kr = task_info(mach_task_self(), flavor, info_data[AFTER], &count); T_ASSERT_MACH_SUCCESS(kr, "verify task_info call succeeded"); resident_size_diff = info_get(kind, GET_RESIDENT_SIZE, info_data[AFTER]) - info_get(kind, GET_RESIDENT_SIZE, info_data[BEFORE]); virtual_size_diff = info_get(kind, GET_VIRTUAL_SIZE, info_data[AFTER]) - info_get(kind, GET_VIRTUAL_SIZE, info_data[BEFORE]); /* * INFO_32_2 gets the max resident size instead of the current resident size * 32 KB tolerance built into test. The returned value is generally between 0 and 16384 * * max resident size is a discrete field in INFO_MACH, so it's handled differently */ if (kind == INFO_32_2) { T_EXPECT_EQ(resident_size_diff % 4096, 0ULL, "verify task_info returns valid max resident_size"); T_EXPECT_GE(resident_size_diff, 0ULL, "verify task_info returns non-negative max resident_size"); T_EXPECT_GE(virtual_size_diff, (unsigned long long)PAGE_SIZE, "verify task_info returns valid virtual_size"); } else { T_EXPECT_GE(resident_size_diff, (unsigned long long)PAGE_SIZE, "task_info returns valid resident_size"); T_EXPECT_GE(virtual_size_diff, (unsigned long long)PAGE_SIZE, "task_info returns valid virtual_size"); } if (kind == INFO_MACH) { resident_size_diff = info_get(kind, GET_MAX_RES, info_data[AFTER]) - info_get(kind, GET_MAX_RES, info_data[BEFORE]); T_EXPECT_EQ(resident_size_diff % 4096, 0ULL, "verify task_info returns valid max resident_size"); T_EXPECT_GE(resident_size_diff, 0ULL, "verify task_info returns non-negative max resident_size"); T_EXPECT_GE(info_get(kind, GET_MAX_RES, info_data[AFTER]), info_get(kind, GET_RESIDENT_SIZE, info_data[AFTER]), "verify max resident size is greater than or equal to curr resident size"); } do_factorial_task(); /* * These counters give time for threads that have terminated. We dont have any, so checking for zero. */ time_value_t * user_tv = (time_value_t *)(info_get(kind, GET_USER_TIME, info_data[BEFORE])); T_EXPECT_EQ((user_tv->seconds + user_tv->microseconds / 1000000), 0, "verify task_info shows valid user time"); time_value_t * sys_tv = (time_value_t *)(info_get(kind, GET_SYS_TIME, info_data[BEFORE])); T_EXPECT_EQ(sys_tv->seconds + (sys_tv->microseconds / 1000000), 0, "verify task_info shows valid system time"); /* * The default value for non-kernel tasks is TIMESHARE. */ policy_t pt = (policy_t)info_get(kind, GET_POLICY, info_data[BEFORE]); T_EXPECT_EQ(pt, POLICY_TIMESHARE, "verify task_info shows valid policy"); /* * This is a negative case. */ count--; kr = task_info(mach_task_self(), flavor, info_data[AFTER], &count); T_ASSERT_MACH_ERROR(kr, KERN_INVALID_ARGUMENT, "Negative test case: task_info should verify that count is at least equal to what is defined in API"); /* * deallocate memory */ munmap(tmp_map, PAGE_SIZE); return; #undef BEFORE #undef AFTER } T_DECL(test_sigcont_task_suspend_resume, "test to verify that SIGCONT on task_suspend()-ed process works", T_META_ASROOT(true), T_META_LTEPHASE(LTE_POSTINIT)) { T_SETUPBEGIN; int is_dev = is_development_kernel(); T_QUIET; T_ASSERT_TRUE(is_dev, "verify development kernel is running"); T_SETUPEND; mach_task_basic_info_data_t mach_basic_info_data; task_info_t info_data = (task_info_t)&mach_basic_info_data; task_debug_info_internal_data_t debug_info; mach_msg_type_number_t debug_count = TASK_DEBUG_INFO_INTERNAL_COUNT; kern_return_t kr; int posix_ret; mach_msg_type_number_t count = MACH_TASK_BASIC_INFO_COUNT; task_flavor_t flavor = MACH_TASK_BASIC_INFO; integer_t suspend_count; integer_t debug_suspend_count; pid_t child_pid = 0; mach_port_name_t child_task; /*for dt_waitpid*/ int timeout = 5; int exit_status = 0; int signal_no = 0; child_pid = fork(); T_ASSERT_POSIX_SUCCESS(child_pid, "verify process can be forked"); if (child_pid == 0) { /* * This will suspend the child process. */ kr = task_suspend(mach_task_self()); /* * When child resumes, it exits immediately */ exit(kr); } /* * Wait for the child process to suspend itself. */ sleep(1); kr = task_for_pid(mach_task_self(), child_pid, &child_task); T_ASSERT_MACH_SUCCESS(kr, "verify task_for_pid succeeded. check sudo if failed"); /* * Verify the suspend_count for child and resume it. */ kr = task_info(child_task, flavor, info_data, &count); T_ASSERT_MACH_SUCCESS(kr, "verify task_info call succeeded"); suspend_count = (integer_t)(info_get(INFO_MACH, GET_SUSPEND_COUNT, info_data)); T_ASSERT_EQ(suspend_count, 1, "verify task_info shows correct suspend_count (1) (actually user stop count) "); kr = task_info(child_task, TASK_DEBUG_INFO_INTERNAL, (task_info_t)&debug_info, &debug_count); T_ASSERT_MACH_SUCCESS(kr, "verify task_info call succeeded"); debug_suspend_count = debug_info.suspend_count; T_ASSERT_EQ(debug_info.suspend_count, 1, "verify debug_info shows correct suspend_count(1)"); posix_ret = kill(child_pid, SIGCONT); T_ASSERT_POSIX_SUCCESS(posix_ret, "verify signal call succeeded"); /* * reap kr from task_suspend call in child */ dt_waitpid(child_pid, &exit_status, &signal_no, timeout); T_ASSERT_EQ(signal_no, 0, "child should be resumed and exit without signal"); T_ASSERT_EQ(exit_status, 0, "child should exit with 0"); } T_DECL(test_sigcont_task_suspend2_resume, "test to verify that SIGCONT on task_suspend2()-ed process doesn't work", T_META_ASROOT(true), T_META_LTEPHASE(LTE_POSTINIT)) { T_SETUPBEGIN; int is_dev = is_development_kernel(); T_QUIET; T_ASSERT_TRUE(is_dev, "verify development kernel is running"); T_SETUPEND; mach_task_basic_info_data_t mach_basic_info_data; task_info_t info_data = (task_info_t)&mach_basic_info_data; task_debug_info_internal_data_t debug_info; mach_msg_type_number_t debug_count = TASK_DEBUG_INFO_INTERNAL_COUNT; kern_return_t kr; int posix_ret; mach_msg_type_number_t count = MACH_TASK_BASIC_INFO_COUNT; task_flavor_t flavor = MACH_TASK_BASIC_INFO; integer_t suspend_count = 0; integer_t debug_suspend_count = 0; pid_t child_pid = 0; mach_port_name_t child_task; task_suspension_token_t child_token = 0xFFFFF; /* * for dt_waitpid * We expect the test to fail right now, so I've set timeout to * be shorter than we may want it to be when the issue is fixed */ int timeout = 1; int exit_status = 0; int signal_no = 0; /* for pipe */ int fd[2]; pipe(fd); int pipe_msg = 0; child_pid = fork(); T_ASSERT_POSIX_SUCCESS(child_pid, "verify process can be forked %d", child_pid); if (child_pid == 0) { close(fd[1]); T_LOG("Waiting to read from parent..."); read(fd[0], &pipe_msg, sizeof(pipe_msg)); T_LOG("Done reading from parent, about to exit..."); exit(0); } /* * Wait for child to fork and block on read */ sleep(1); close(fd[0]); kr = task_for_pid(mach_task_self(), child_pid, &child_task); T_ASSERT_MACH_SUCCESS(kr, "verify task_for_pid succeeded. check sudo if failed"); kr = task_info(child_task, TASK_DEBUG_INFO_INTERNAL, (task_info_t)&debug_info, &debug_count); T_ASSERT_MACH_SUCCESS(kr, "verify task_info call succeeded"); debug_suspend_count = debug_info.suspend_count; T_EXPECT_EQ(debug_suspend_count, 0, "verify debug_info shows correct (true) suspend_count(0)"); kr = task_suspend2(child_task, &child_token); T_ASSERT_MACH_SUCCESS(kr, "verify task_suspend2 call succeeded"); kr = task_info(child_task, TASK_DEBUG_INFO_INTERNAL, (task_info_t)&debug_info, &debug_count); T_ASSERT_MACH_SUCCESS(kr, "verify task_info call succeeded"); debug_suspend_count = debug_info.suspend_count; T_ASSERT_EQ(debug_suspend_count, 1, "verify debug_info shows correct (true) suspend_count(1)"); /* * Verify the suspend_count for child and resume it. */ kr = task_info(child_task, flavor, info_data, &count); T_ASSERT_MACH_SUCCESS(kr, "verify task_info call succeeded"); suspend_count = (integer_t)(info_get(INFO_MACH, GET_SUSPEND_COUNT, info_data)); T_EXPECT_EQ(suspend_count, 1, "verify task_info shows correct (user_stop_count) suspend_count (1)"); posix_ret = kill(child_pid, SIGCONT); T_ASSERT_POSIX_SUCCESS(posix_ret, "verify signal call succeeded"); kr = task_info(child_task, TASK_DEBUG_INFO_INTERNAL, (task_info_t)&debug_info, &debug_count); T_EXPECT_MACH_SUCCESS(kr, "verify task_info call succeeded"); debug_suspend_count = debug_info.suspend_count; T_EXPECTFAIL_WITH_RADAR(33166654); T_EXPECT_EQ(debug_suspend_count, 1, "verify debug_info shows correct (true) suspend_count (1)"); suspend_count = (integer_t)(info_get(INFO_MACH, GET_SUSPEND_COUNT, info_data)); T_ASSERT_EQ(suspend_count, 1, "verify task_info shows correct (user_stop_count) suspend_count (1) after SIG_CONT"); kr = task_resume(child_task); T_EXPECTFAIL_WITH_RADAR(33166654); T_EXPECT_MACH_SUCCESS(kr, "verify task_resume succeeded"); /* * reap kr from task_suspend call in child */ dt_waitpid(child_pid, &exit_status, &signal_no, timeout); T_ASSERT_EQ(signal_no, SIG_DT_TIMEOUT, "dt_waitpid timed out as expected"); // Resume properly using token and then wait kr = task_resume2(child_token); T_EXPECTFAIL_WITH_RADAR(33166654); T_ASSERT_MACH_SUCCESS(kr, "verify task_resume2 succeeded"); write(fd[1], &pipe_msg, sizeof(pipe_msg)); /* * reap kr from task_suspend call in child */ dt_waitpid(child_pid, &exit_status, &signal_no, timeout); T_ASSERT_EQ(signal_no, 0, "child should be resumed and no signal should be returned"); T_ASSERT_EQ(exit_status, 0, "child should exit with 0"); } uint64_t info_get(enum info_kind kind, enum info_get get, void * data) { switch (get) { case GET_SUSPEND_COUNT: switch (kind) { case INFO_32: case INFO_32_2: return (uint64_t)(((task_basic_info_32_t)data)->suspend_count); #if defined(__arm64__) case INFO_64: T_ASSERT_FAIL("illegal info_get %d %d", kind, get); break; case INFO_64_2: return (uint64_t)(((task_basic_info_64_2_t)data)->suspend_count); #else case INFO_64: return (uint64_t)(((task_basic_info_64_t)data)->suspend_count); case INFO_64_2: T_ASSERT_FAIL("illegal info_get %d %d", kind, get); break; #endif /* defined(__arm64__) */ case INFO_MACH: return (uint64_t)(((mach_task_basic_info_t)data)->suspend_count); case INFO_MAX: default: T_ASSERT_FAIL("unhandled info_get %d %d", kind, get); } case GET_RESIDENT_SIZE: switch (kind) { case INFO_32: case INFO_32_2: return (uint64_t)(((task_basic_info_32_t)data)->resident_size); #if defined(__arm64__) case INFO_64: T_ASSERT_FAIL("illegal info_get %d %d", kind, get); break; case INFO_64_2: return (uint64_t)(((task_basic_info_64_2_t)data)->resident_size); #else case INFO_64: return (uint64_t)(((task_basic_info_64_t)data)->resident_size); case INFO_64_2: T_ASSERT_FAIL("illegal info_get %d %d", kind, get); break; #endif /* defined(__arm64__) */ case INFO_MACH: return (uint64_t)(((mach_task_basic_info_t)data)->resident_size); case INFO_MAX: default: T_ASSERT_FAIL("unhandled info_get %d %d", kind, get); } case GET_VIRTUAL_SIZE: switch (kind) { case INFO_32: case INFO_32_2: return (uint64_t)(((task_basic_info_32_t)data)->virtual_size); #if defined(__arm64__) case INFO_64: T_ASSERT_FAIL("illegal info_get %d %d", kind, get); break; case INFO_64_2: return (uint64_t)(((task_basic_info_64_2_t)data)->virtual_size); #else case INFO_64: return (uint64_t)(((task_basic_info_64_t)data)->virtual_size); case INFO_64_2: T_ASSERT_FAIL("illegal info_get %d %d", kind, get); break; #endif /* defined(__arm64__) */ case INFO_MACH: return (uint64_t)(((mach_task_basic_info_t)data)->virtual_size); case INFO_MAX: default: T_ASSERT_FAIL("unhandled info_get %d %d", kind, get); } case GET_USER_TIME: switch (kind) { case INFO_32: case INFO_32_2: return (uint64_t) &(((task_basic_info_32_t)data)->user_time); #if defined(__arm64__) case INFO_64: T_ASSERT_FAIL("illegal info_get %d %d", kind, get); break; case INFO_64_2: return (uint64_t) &(((task_basic_info_64_2_t)data)->user_time); #else case INFO_64: return (uint64_t) &(((task_basic_info_64_t)data)->user_time); case INFO_64_2: T_ASSERT_FAIL("illegal info_get %d %d", kind, get); break; #endif /* defined(__arm64__) */ case INFO_MACH: return (uint64_t) &(((mach_task_basic_info_t)data)->user_time); case INFO_MAX: default: T_ASSERT_FAIL("unhandled info_get %d %d", kind, get); } case GET_SYS_TIME: switch (kind) { case INFO_32: case INFO_32_2: return (uint64_t) &(((task_basic_info_32_t)data)->system_time); #if defined(__arm64__) case INFO_64: T_ASSERT_FAIL("illegal info_get %d %d", kind, get); break; case INFO_64_2: return (uint64_t) &(((task_basic_info_64_2_t)data)->system_time); #else case INFO_64: return (uint64_t) &(((task_basic_info_64_t)data)->system_time); case INFO_64_2: T_ASSERT_FAIL("illegal info_get %d %d", kind, get); break; #endif /* defined(__arm64__) */ case INFO_MACH: return (uint64_t) &(((mach_task_basic_info_t)data)->user_time); case INFO_MAX: default: T_ASSERT_FAIL("unhandled info_get %d %d", kind, get); } case GET_POLICY: switch (kind) { case INFO_32: case INFO_32_2: return (uint64_t)(((task_basic_info_32_t)data)->policy); #if defined(__arm64__) case INFO_64: T_ASSERT_FAIL("illegal info_get %d %d", kind, get); break; case INFO_64_2: return (uint64_t)(((task_basic_info_64_2_t)data)->policy); #else case INFO_64: return (uint64_t)(((task_basic_info_64_t)data)->policy); case INFO_64_2: T_ASSERT_FAIL("illegal info_get %d %d", kind, get); break; #endif /* defined(__arm64__) */ case INFO_MACH: return (uint64_t)(((mach_task_basic_info_t)data)->policy); case INFO_MAX: default: T_ASSERT_FAIL("unhandled info_get %d %d", kind, get); } case GET_MAX_RES: switch (kind) { case INFO_32: case INFO_32_2: case INFO_64: case INFO_64_2: T_ASSERT_FAIL("illegal info_get %d %d", kind, get); case INFO_MACH: return (uint64_t)(((mach_task_basic_info_t)data)->resident_size_max); case INFO_MAX: default: T_ASSERT_FAIL("unhandled info_get %d %d", kind, get); } } __builtin_unreachable(); } |