1 #include <darwintest.h>
4 #include <mach/arm/processor_info.h>
10 T_GLOBAL_META(T_META_ASROOT(true),
11 T_META_RUN_CONCURRENTLY(true));
13 T_DECL(processor_cpu_stat64
,
14 "ensure 64-bit processor statistics are reported correctly",
15 T_META_NAMESPACE("xnu.arm"))
18 T_SKIP("processor statistics only available on ARM");
19 #else /* !__arm64__ */
20 host_t host
= mach_host_self();
21 host_t priv_port
= MACH_PORT_NULL
;
23 kern_return_t kr
= host_get_host_priv_port(host
, &priv_port
);
25 T_ASSERT_MACH_SUCCESS(kr
, "host_get_host_priv_port");
27 T_ASSERT_NE(priv_port
, MACH_PORT_NULL
, "valid host priv port");
29 processor_port_array_t cpu_ports
= NULL
;
30 mach_msg_type_number_t cpu_count
= 0;
31 kr
= host_processors(priv_port
, &cpu_ports
, &cpu_count
);
33 T_ASSERT_MACH_SUCCESS(kr
, "host_processors");
35 T_ASSERT_NOTNULL(cpu_ports
, "valid processor port array");
37 T_ASSERT_GT(cpu_count
, (mach_msg_type_number_t
)0,
38 "non-zero CPU count");
40 T_LOG("found %d CPUs", cpu_count
);
42 struct processor_cpu_stat64
*prestats
= calloc(cpu_count
,
46 T_ASSERT_NOTNULL(prestats
, "allocate space for stats (pre)");
47 memset(prestats
, 0xff, cpu_count
* sizeof(*prestats
));
49 for (int i
= 0; i
< (int)cpu_count
; i
++) {
50 mach_msg_type_number_t info_count
= PROCESSOR_CPU_STAT64_COUNT
;
51 kr
= processor_info(cpu_ports
[i
], PROCESSOR_CPU_STAT64
, &host
,
52 (processor_info_t
)&prestats
[i
], &info_count
);
53 T_ASSERT_MACH_SUCCESS(kr
,
54 "processor_info(%d, PROCESSOR_CPU_STAT64, ...)", i
);
57 T_ASSERT_EQ(info_count
, PROCESSOR_CPU_STAT64_COUNT
,
58 "received enough CPU statistics");
63 struct processor_cpu_stat64
*poststats
= calloc(cpu_count
- 1,
67 T_ASSERT_NOTNULL(poststats
, "allocate space for stats (post)");
69 for (int i
= 0; i
< (int)cpu_count
; i
++) {
70 mach_msg_type_number_t info_count
= PROCESSOR_CPU_STAT64_COUNT
;
71 kr
= processor_info(cpu_ports
[i
], PROCESSOR_CPU_STAT64
, &host
,
72 (processor_info_t
)&poststats
[i
], &info_count
);
73 T_ASSERT_MACH_SUCCESS(kr
,
74 "processor_info(%d, PROCESSOR_CPU_STAT64, ...)", i
);
77 T_ASSERT_EQ(info_count
, PROCESSOR_CPU_STAT64_COUNT
,
78 "received enough CPU statistics");
81 for (int i
= 0; i
< (int)cpu_count
; i
++) {
82 #define CHECK_STAT_FIELD(field) \
83 T_EXPECT_GE(poststats[i].field, prestats[i].field, \
84 "CPU %d's " #field " is monotonically increasing (+%" PRIu64 \
85 ")", i, poststats[i].field - prestats[i].field)
87 CHECK_STAT_FIELD(irq_ex_cnt
);
88 CHECK_STAT_FIELD(ipi_cnt
);
89 CHECK_STAT_FIELD(timer_cnt
);
90 CHECK_STAT_FIELD(undef_ex_cnt
);
91 CHECK_STAT_FIELD(unaligned_cnt
);
92 CHECK_STAT_FIELD(vfp_cnt
);
93 CHECK_STAT_FIELD(vfp_shortv_cnt
);
94 CHECK_STAT_FIELD(data_ex_cnt
);
95 CHECK_STAT_FIELD(instr_ex_cnt
);
96 CHECK_STAT_FIELD(pmi_cnt
);
98 #undef CHECK_STAT_FIELD
103 #endif /* __arm64__ */
107 T_DECL(processor_cpu_info_order
,
108 "ensure host_processor_info iterates CPU in CPU ID order")
110 host_t host
= mach_host_self();
111 host_t priv_port
= MACH_PORT_NULL
;
113 kern_return_t kr
= host_get_host_priv_port(host
, &priv_port
);
114 T_QUIET
; T_ASSERT_MACH_SUCCESS(kr
, "host_get_host_priv_port");
115 T_QUIET
; T_ASSERT_NE(priv_port
, MACH_PORT_NULL
, "valid host priv port");
117 processor_info_array_t info_array
= NULL
;
118 mach_msg_type_number_t info_count
= 0;
119 natural_t processor_count
= 0;
121 kr
= host_processor_info(mach_host_self(), PROCESSOR_BASIC_INFO
, &processor_count
,
122 &info_array
, &info_count
);
124 T_QUIET
; T_ASSERT_MACH_SUCCESS(kr
, "host_processor_info(PROCESSOR_BASIC_INFO)");
125 T_QUIET
; T_ASSERT_NOTNULL(info_array
, "valid processor port array");
126 T_QUIET
; T_ASSERT_GT(info_count
, (mach_msg_type_number_t
)0, "non-zero array");
127 T_QUIET
; T_ASSERT_GT(processor_count
, (natural_t
)0, "non-zero processor_count");
129 processor_basic_info_t basic_info_array
= (processor_basic_info_t
)info_array
;
131 for (natural_t i
= 0; i
< processor_count
; i
++) {
132 struct processor_basic_info
* processor_info
= &basic_info_array
[i
];
134 natural_t slot_num
= (natural_t
)processor_info
->slot_num
;
136 T_ASSERT_EQ(slot_num
, i
, "CPU ID must equal array index");