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