1 #include <darwintest.h> 2 #include <inttypes.h> 3 #if __arm64__ 4 #include <mach/arm/processor_info.h> 5 #endif /* __arm64__ */ 6 #include <mach/mach.h> 7 #include <stdlib.h> 8 #include <unistd.h> 9 10 T_GLOBAL_META(T_META_ASROOT(true), 11 T_META_RUN_CONCURRENTLY(true)); 12 13 T_DECL(processor_cpu_stat64, 14 "ensure 64-bit processor statistics are reported correctly", 15 T_META_NAMESPACE("xnu.arm"), 16 T_META_RADAR_COMPONENT_NAME("xnu"), 17 T_META_RADAR_COMPONENT_VERSION("arm"), 18 T_META_OWNER("mwm"), 19 T_META_TAG_VM_PREFERRED) 20 { 21 #if !__arm64__ 22 T_SKIP("processor statistics only available on ARM"); 23 #else /* !__arm64__ */ 24 host_t host = mach_host_self(); 25 host_t priv_port = MACH_PORT_NULL; 26 27 kern_return_t kr = host_get_host_priv_port(host, &priv_port); 28 T_QUIET; 29 T_ASSERT_MACH_SUCCESS(kr, "host_get_host_priv_port"); 30 T_QUIET; 31 T_ASSERT_NE(priv_port, MACH_PORT_NULL, "valid host priv port"); 32 33 processor_port_array_t cpu_ports = NULL; 34 mach_msg_type_number_t cpu_count = 0; 35 kr = host_processors(priv_port, &cpu_ports, &cpu_count); 36 T_QUIET; 37 T_ASSERT_MACH_SUCCESS(kr, "host_processors"); 38 T_QUIET; 39 T_ASSERT_NOTNULL(cpu_ports, "valid processor port array"); 40 T_QUIET; 41 T_ASSERT_GT(cpu_count, (mach_msg_type_number_t)0, 42 "non-zero CPU count"); 43 44 T_LOG("found %d CPUs", cpu_count); 45 46 struct processor_cpu_stat64 *prestats = calloc(cpu_count, 47 sizeof(*prestats)); 48 T_WITH_ERRNO; 49 T_QUIET; 50 T_ASSERT_NOTNULL(prestats, "allocate space for stats (pre)"); 51 memset(prestats, 0xff, cpu_count * sizeof(*prestats)); 52 53 for (int i = 0; i < (int)cpu_count; i++) { 54 mach_msg_type_number_t info_count = PROCESSOR_CPU_STAT64_COUNT; 55 kr = processor_info(cpu_ports[i], PROCESSOR_CPU_STAT64, &host, 56 (processor_info_t)&prestats[i], &info_count); 57 T_ASSERT_MACH_SUCCESS(kr, 58 "processor_info(%d, PROCESSOR_CPU_STAT64, ...)", i); 59 60 T_QUIET; 61 T_ASSERT_EQ(info_count, PROCESSOR_CPU_STAT64_COUNT, 62 "received enough CPU statistics"); 63 } 64 65 sleep(1); 66 67 struct processor_cpu_stat64 *poststats = calloc(cpu_count - 1, 68 sizeof(*poststats)); 69 T_WITH_ERRNO; 70 T_QUIET; 71 T_ASSERT_NOTNULL(poststats, "allocate space for stats (post)"); 72 73 for (int i = 0; i < (int)cpu_count; i++) { 74 mach_msg_type_number_t info_count = PROCESSOR_CPU_STAT64_COUNT; 75 kr = processor_info(cpu_ports[i], PROCESSOR_CPU_STAT64, &host, 76 (processor_info_t)&poststats[i], &info_count); 77 T_ASSERT_MACH_SUCCESS(kr, 78 "processor_info(%d, PROCESSOR_CPU_STAT64, ...)", i); 79 80 T_QUIET; 81 T_ASSERT_EQ(info_count, PROCESSOR_CPU_STAT64_COUNT, 82 "received enough CPU statistics"); 83 } 84 85 for (int i = 0; i < (int)cpu_count; i++) { 86 #define CHECK_STAT_FIELD(field) \ 87 T_EXPECT_GE(poststats[i].field, prestats[i].field, \ 88 "CPU %d's " #field " is monotonically increasing (+%" PRIu64 \ 89 ")", i, poststats[i].field - prestats[i].field) 90 91 CHECK_STAT_FIELD(irq_ex_cnt); 92 CHECK_STAT_FIELD(ipi_cnt); 93 CHECK_STAT_FIELD(timer_cnt); 94 CHECK_STAT_FIELD(undef_ex_cnt); 95 CHECK_STAT_FIELD(unaligned_cnt); 96 CHECK_STAT_FIELD(vfp_cnt); 97 CHECK_STAT_FIELD(vfp_shortv_cnt); 98 CHECK_STAT_FIELD(data_ex_cnt); 99 CHECK_STAT_FIELD(instr_ex_cnt); 100 CHECK_STAT_FIELD(pmi_cnt); 101 102 #undef CHECK_STAT_FIELD 103 } 104 105 free(prestats); 106 free(poststats); 107 #endif /* __arm64__ */ 108 } 109 110 111 T_DECL(processor_cpu_info_order, 112 "ensure host_processor_info iterates CPU in CPU ID order", T_META_TAG_VM_PREFERRED) 113 { 114 host_t host = mach_host_self(); 115 host_t priv_port = MACH_PORT_NULL; 116 117 kern_return_t kr = host_get_host_priv_port(host, &priv_port); 118 T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "host_get_host_priv_port"); 119 T_QUIET; T_ASSERT_NE(priv_port, MACH_PORT_NULL, "valid host priv port"); 120 121 processor_info_array_t info_array = NULL; 122 mach_msg_type_number_t info_count = 0; 123 natural_t processor_count = 0; 124 125 kr = host_processor_info(mach_host_self(), PROCESSOR_BASIC_INFO, &processor_count, 126 &info_array, &info_count); 127 128 T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "host_processor_info(PROCESSOR_BASIC_INFO)"); 129 T_QUIET; T_ASSERT_NOTNULL(info_array, "valid processor port array"); 130 T_QUIET; T_ASSERT_GT(info_count, (mach_msg_type_number_t)0, "non-zero array"); 131 T_QUIET; T_ASSERT_GT(processor_count, (natural_t)0, "non-zero processor_count"); 132 133 processor_basic_info_t basic_info_array = (processor_basic_info_t)info_array; 134 135 for (natural_t i = 0; i < processor_count; i++) { 136 struct processor_basic_info* processor_info = &basic_info_array[i]; 137 138 natural_t slot_num = (natural_t)processor_info->slot_num; 139 140 T_ASSERT_EQ(slot_num, i, "CPU ID must equal array index"); 141 } 142 } 143