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