1*2c2f96dcSApple OSS Distributions #include <darwintest.h>
2*2c2f96dcSApple OSS Distributions #include <darwintest_utils.h>
3*2c2f96dcSApple OSS Distributions #include <perfdata/perfdata.h>
4*2c2f96dcSApple OSS Distributions #include <stdio.h>
5*2c2f96dcSApple OSS Distributions #include <stdlib.h>
6*2c2f96dcSApple OSS Distributions #include <stdbool.h>
7*2c2f96dcSApple OSS Distributions #include <errno.h>
8*2c2f96dcSApple OSS Distributions #include "test_utils.h"
9*2c2f96dcSApple OSS Distributions
10*2c2f96dcSApple OSS Distributions #if defined(__arm64__)
11*2c2f96dcSApple OSS Distributions T_GLOBAL_META(
12*2c2f96dcSApple OSS Distributions T_META_TAG_PERF,
13*2c2f96dcSApple OSS Distributions T_META_RUN_CONCURRENTLY(false),
14*2c2f96dcSApple OSS Distributions T_META_BOOTARGS_SET("enable_skstsct=1"),
15*2c2f96dcSApple OSS Distributions T_META_CHECK_LEAKS(false),
16*2c2f96dcSApple OSS Distributions T_META_ASROOT(true),
17*2c2f96dcSApple OSS Distributions T_META_REQUIRES_SYSCTL_EQ("kern.hv_vmm_present", 0),
18*2c2f96dcSApple OSS Distributions T_META_RADAR_COMPONENT_NAME("xnu"),
19*2c2f96dcSApple OSS Distributions T_META_RADAR_COMPONENT_VERSION("scheduler"),
20*2c2f96dcSApple OSS Distributions T_META_OWNER("ngamble")
21*2c2f96dcSApple OSS Distributions );
22*2c2f96dcSApple OSS Distributions #else
23*2c2f96dcSApple OSS Distributions T_GLOBAL_META(
24*2c2f96dcSApple OSS Distributions T_META_TAG_PERF,
25*2c2f96dcSApple OSS Distributions T_META_RUN_CONCURRENTLY(false),
26*2c2f96dcSApple OSS Distributions T_META_CHECK_LEAKS(false),
27*2c2f96dcSApple OSS Distributions T_META_ASROOT(true),
28*2c2f96dcSApple OSS Distributions T_META_REQUIRES_SYSCTL_EQ("kern.hv_vmm_present", 0),
29*2c2f96dcSApple OSS Distributions T_META_RADAR_COMPONENT_NAME("xnu"),
30*2c2f96dcSApple OSS Distributions T_META_RADAR_COMPONENT_VERSION("scheduler"),
31*2c2f96dcSApple OSS Distributions T_META_OWNER("ngamble")
32*2c2f96dcSApple OSS Distributions );
33*2c2f96dcSApple OSS Distributions #endif
34*2c2f96dcSApple OSS Distributions
35*2c2f96dcSApple OSS Distributions static void
log_cmd(char ** cmd)36*2c2f96dcSApple OSS Distributions log_cmd(char **cmd)
37*2c2f96dcSApple OSS Distributions {
38*2c2f96dcSApple OSS Distributions #define MAX_CMD_STR 1024
39*2c2f96dcSApple OSS Distributions char cmd_str[MAX_CMD_STR] = "";
40*2c2f96dcSApple OSS Distributions char *s;
41*2c2f96dcSApple OSS Distributions
42*2c2f96dcSApple OSS Distributions while ((s = *cmd) != NULL) {
43*2c2f96dcSApple OSS Distributions strlcat(cmd_str, s, MAX_CMD_STR);
44*2c2f96dcSApple OSS Distributions strlcat(cmd_str, " ", MAX_CMD_STR);
45*2c2f96dcSApple OSS Distributions cmd++;
46*2c2f96dcSApple OSS Distributions }
47*2c2f96dcSApple OSS Distributions T_LOG("%s\n", cmd_str);
48*2c2f96dcSApple OSS Distributions }
49*2c2f96dcSApple OSS Distributions
50*2c2f96dcSApple OSS Distributions static void
run_zn(char * name,char ** cmd)51*2c2f96dcSApple OSS Distributions run_zn(char *name, char **cmd)
52*2c2f96dcSApple OSS Distributions {
53*2c2f96dcSApple OSS Distributions char tracefile_path[MAXPATHLEN] = "zn.artrace";
54*2c2f96dcSApple OSS Distributions snprintf(tracefile_path, MAXPATHLEN, "%s.artrace", name);
55*2c2f96dcSApple OSS Distributions
56*2c2f96dcSApple OSS Distributions int ret = dt_resultfile(tracefile_path, sizeof(tracefile_path));
57*2c2f96dcSApple OSS Distributions if (ret) {
58*2c2f96dcSApple OSS Distributions T_ASSERT_FAIL("get file path for trace file failed with errno %d", errno);
59*2c2f96dcSApple OSS Distributions }
60*2c2f96dcSApple OSS Distributions
61*2c2f96dcSApple OSS Distributions cmd[3] = tracefile_path;
62*2c2f96dcSApple OSS Distributions log_cmd(cmd);
63*2c2f96dcSApple OSS Distributions
64*2c2f96dcSApple OSS Distributions __block bool test_failed = true;
65*2c2f96dcSApple OSS Distributions __block bool test_skipped = false;
66*2c2f96dcSApple OSS Distributions
67*2c2f96dcSApple OSS Distributions pid_t test_pid;
68*2c2f96dcSApple OSS Distributions test_pid = dt_launch_tool_pipe(cmd, false, NULL, ^bool (__unused char *data, __unused size_t data_size, __unused dt_pipe_data_handler_context_t *context) {
69*2c2f96dcSApple OSS Distributions T_LOG("%s", data);
70*2c2f96dcSApple OSS Distributions if (strstr(data, "TEST PASSED")) {
71*2c2f96dcSApple OSS Distributions test_failed = false;
72*2c2f96dcSApple OSS Distributions }
73*2c2f96dcSApple OSS Distributions if (strstr(data, "TEST FAILED")) {
74*2c2f96dcSApple OSS Distributions test_failed = true;
75*2c2f96dcSApple OSS Distributions }
76*2c2f96dcSApple OSS Distributions if (strstr(data, "TEST SKIPPED")) {
77*2c2f96dcSApple OSS Distributions test_skipped = true;
78*2c2f96dcSApple OSS Distributions }
79*2c2f96dcSApple OSS Distributions return false;
80*2c2f96dcSApple OSS Distributions }, ^bool (__unused char *data, __unused size_t data_size, __unused dt_pipe_data_handler_context_t *context) {
81*2c2f96dcSApple OSS Distributions T_LOG("%s", data);
82*2c2f96dcSApple OSS Distributions return false;
83*2c2f96dcSApple OSS Distributions }, BUFFER_PATTERN_LINE, NULL);
84*2c2f96dcSApple OSS Distributions
85*2c2f96dcSApple OSS Distributions if (test_pid == 0) {
86*2c2f96dcSApple OSS Distributions T_ASSERT_FAIL("dt_launch_tool_pipe() failed unexpectedly with errno %d", errno);
87*2c2f96dcSApple OSS Distributions }
88*2c2f96dcSApple OSS Distributions
89*2c2f96dcSApple OSS Distributions int exitstatus;
90*2c2f96dcSApple OSS Distributions dt_waitpid(test_pid, &exitstatus, NULL, 0);
91*2c2f96dcSApple OSS Distributions if (exitstatus != 0) {
92*2c2f96dcSApple OSS Distributions T_LOG("ktrace artrace exitstatus=%d\n", exitstatus);
93*2c2f96dcSApple OSS Distributions }
94*2c2f96dcSApple OSS Distributions if (test_skipped) {
95*2c2f96dcSApple OSS Distributions unlink(tracefile_path);
96*2c2f96dcSApple OSS Distributions T_SKIP("%s", name);
97*2c2f96dcSApple OSS Distributions } else if (test_failed) {
98*2c2f96dcSApple OSS Distributions T_FAIL("%s", name);
99*2c2f96dcSApple OSS Distributions } else {
100*2c2f96dcSApple OSS Distributions unlink(tracefile_path);
101*2c2f96dcSApple OSS Distributions T_PASS("%s", name);
102*2c2f96dcSApple OSS Distributions }
103*2c2f96dcSApple OSS Distributions
104*2c2f96dcSApple OSS Distributions pdwriter_t writer = pdwriter_open_tmp("xnu", name, 0, 0, NULL, 0);
105*2c2f96dcSApple OSS Distributions T_WITH_ERRNO;
106*2c2f96dcSApple OSS Distributions T_ASSERT_NOTNULL(writer, "pdwriter_open_tmp");
107*2c2f96dcSApple OSS Distributions pdwriter_new_value(writer, "scheduler_ok", PDUNIT_CUSTOM(passing), !test_failed);
108*2c2f96dcSApple OSS Distributions pdwriter_close(writer);
109*2c2f96dcSApple OSS Distributions T_END;
110*2c2f96dcSApple OSS Distributions }
111*2c2f96dcSApple OSS Distributions
112*2c2f96dcSApple OSS Distributions T_DECL(zn_rt, "Schedule 1 RT thread per performance core, and test max latency", T_META_ENABLED(!TARGET_OS_TV), XNU_T_META_SOC_SPECIFIC)
113*2c2f96dcSApple OSS Distributions {
114*2c2f96dcSApple OSS Distributions char *cmd[] = {"/usr/bin/ktrace", "artrace", "-o", "zn.artrace", "-c",
115*2c2f96dcSApple OSS Distributions "/AppleInternal/CoreOS/tests/xnu/zero-to-n/zn",
116*2c2f96dcSApple OSS Distributions "0", "broadcast-single-sem", "realtime", "1000",
117*2c2f96dcSApple OSS Distributions "--spin-time", "200000",
118*2c2f96dcSApple OSS Distributions "--spin-all",
119*2c2f96dcSApple OSS Distributions "--test-rt",
120*2c2f96dcSApple OSS Distributions #if defined(__x86_64__)
121*2c2f96dcSApple OSS Distributions "--trace", "2000000",
122*2c2f96dcSApple OSS Distributions #else
123*2c2f96dcSApple OSS Distributions "--trace", "500000",
124*2c2f96dcSApple OSS Distributions #endif
125*2c2f96dcSApple OSS Distributions NULL};
126*2c2f96dcSApple OSS Distributions
127*2c2f96dcSApple OSS Distributions run_zn("zn_rt", cmd);
128*2c2f96dcSApple OSS Distributions }
129*2c2f96dcSApple OSS Distributions
130*2c2f96dcSApple OSS Distributions T_DECL(zn_rt_smt, "Schedule 1 RT thread per primary core, verify that the secondaries are idle iff the RT threads are running", T_META_ENABLED(TARGET_CPU_X86_64))
131*2c2f96dcSApple OSS Distributions {
132*2c2f96dcSApple OSS Distributions char *cmd[] = {"/usr/bin/ktrace", "artrace", "-o", "zn.artrace", "-c",
133*2c2f96dcSApple OSS Distributions "/AppleInternal/CoreOS/tests/xnu/zero-to-n/zn",
134*2c2f96dcSApple OSS Distributions "0", "broadcast-single-sem", "realtime", "1000",
135*2c2f96dcSApple OSS Distributions "--spin-time", "200000",
136*2c2f96dcSApple OSS Distributions "--spin-all",
137*2c2f96dcSApple OSS Distributions "--churn-pri", "4",
138*2c2f96dcSApple OSS Distributions "--test-rt-smt",
139*2c2f96dcSApple OSS Distributions "--trace", "2000000",
140*2c2f96dcSApple OSS Distributions NULL};
141*2c2f96dcSApple OSS Distributions
142*2c2f96dcSApple OSS Distributions run_zn("zn_rt_smt", cmd);
143*2c2f96dcSApple OSS Distributions }
144*2c2f96dcSApple OSS Distributions
145*2c2f96dcSApple OSS Distributions T_DECL(zn_rt_avoid0, "Schedule 1 RT thread per primary core except for CPU 0", T_META_ASROOT(true), T_META_ENABLED(TARGET_CPU_X86_64))
146*2c2f96dcSApple OSS Distributions {
147*2c2f96dcSApple OSS Distributions char *cmd[] = {"/usr/bin/ktrace", "artrace", "-o", "zn.artrace", "-c",
148*2c2f96dcSApple OSS Distributions "/AppleInternal/CoreOS/tests/xnu/zero-to-n/zn",
149*2c2f96dcSApple OSS Distributions "0", "broadcast-single-sem", "realtime", "1000",
150*2c2f96dcSApple OSS Distributions "--spin-time", "200000",
151*2c2f96dcSApple OSS Distributions "--spin-all",
152*2c2f96dcSApple OSS Distributions "--test-rt-avoid0",
153*2c2f96dcSApple OSS Distributions "--trace", "2000000",
154*2c2f96dcSApple OSS Distributions NULL};
155*2c2f96dcSApple OSS Distributions
156*2c2f96dcSApple OSS Distributions run_zn("zn_rt_avoid0", cmd);
157*2c2f96dcSApple OSS Distributions }
158*2c2f96dcSApple OSS Distributions
159*2c2f96dcSApple OSS Distributions T_DECL(zn_rt_apt, "Emulate AVID Pro Tools with default latency deadlines", T_META_ENABLED(!TARGET_OS_TV))
160*2c2f96dcSApple OSS Distributions {
161*2c2f96dcSApple OSS Distributions char *cmd[] = {"/usr/bin/ktrace", "artrace", "-o", "zn.artrace", "-c",
162*2c2f96dcSApple OSS Distributions "/AppleInternal/CoreOS/tests/xnu/zero-to-n/zn",
163*2c2f96dcSApple OSS Distributions "0", "chain", "realtime", "1000",
164*2c2f96dcSApple OSS Distributions "--extra-thread-count", "-3",
165*2c2f96dcSApple OSS Distributions "--spin-time", "200000",
166*2c2f96dcSApple OSS Distributions "--spin-all",
167*2c2f96dcSApple OSS Distributions "--churn-pri", "31", "--churn-random",
168*2c2f96dcSApple OSS Distributions "--test-rt",
169*2c2f96dcSApple OSS Distributions #if defined(__x86_64__)
170*2c2f96dcSApple OSS Distributions "--trace", "2000000",
171*2c2f96dcSApple OSS Distributions #else
172*2c2f96dcSApple OSS Distributions "--trace", "500000",
173*2c2f96dcSApple OSS Distributions #endif
174*2c2f96dcSApple OSS Distributions NULL};
175*2c2f96dcSApple OSS Distributions
176*2c2f96dcSApple OSS Distributions run_zn("zn_rt_apt", cmd);
177*2c2f96dcSApple OSS Distributions }
178*2c2f96dcSApple OSS Distributions
179*2c2f96dcSApple OSS Distributions T_DECL(zn_rt_apt_ll, "Emulate AVID Pro Tools with low latency deadlines", XNU_T_META_SOC_SPECIFIC)
180*2c2f96dcSApple OSS Distributions {
181*2c2f96dcSApple OSS Distributions char *cmd[] = {"/usr/bin/ktrace", "artrace", "-o", "zn.artrace", "-c",
182*2c2f96dcSApple OSS Distributions "/AppleInternal/CoreOS/tests/xnu/zero-to-n/zn",
183*2c2f96dcSApple OSS Distributions "0", "chain", "realtime", "1000",
184*2c2f96dcSApple OSS Distributions "--extra-thread-count", "-3",
185*2c2f96dcSApple OSS Distributions "--spin-time", "200000",
186*2c2f96dcSApple OSS Distributions "--spin-all",
187*2c2f96dcSApple OSS Distributions "--churn-pri", "31", "--churn-random",
188*2c2f96dcSApple OSS Distributions "--test-rt",
189*2c2f96dcSApple OSS Distributions "--rt-ll",
190*2c2f96dcSApple OSS Distributions "--trace", "500000",
191*2c2f96dcSApple OSS Distributions NULL};
192*2c2f96dcSApple OSS Distributions
193*2c2f96dcSApple OSS Distributions run_zn("zn_rt_apt_ll", cmd);
194*2c2f96dcSApple OSS Distributions }
195*2c2f96dcSApple OSS Distributions
196*2c2f96dcSApple OSS Distributions T_DECL(zn_rt_edf, "Test max latency of earliest deadline RT threads in the presence of later deadline threads", T_META_ENABLED(!TARGET_OS_TV), XNU_T_META_SOC_SPECIFIC)
197*2c2f96dcSApple OSS Distributions {
198*2c2f96dcSApple OSS Distributions char *cmd[] = {"/usr/bin/ktrace", "artrace", "-o", "zn.artrace", "-c",
199*2c2f96dcSApple OSS Distributions "/AppleInternal/CoreOS/tests/xnu/zero-to-n/zn",
200*2c2f96dcSApple OSS Distributions "0", "broadcast-single-sem", "realtime", "1000",
201*2c2f96dcSApple OSS Distributions "--extra-thread-count", "-1",
202*2c2f96dcSApple OSS Distributions "--spin-time", "200000",
203*2c2f96dcSApple OSS Distributions "--spin-all",
204*2c2f96dcSApple OSS Distributions "--rt-churn",
205*2c2f96dcSApple OSS Distributions "--test-rt",
206*2c2f96dcSApple OSS Distributions #if defined(__x86_64__)
207*2c2f96dcSApple OSS Distributions "--trace", "2000000",
208*2c2f96dcSApple OSS Distributions #else
209*2c2f96dcSApple OSS Distributions "--trace", "500000",
210*2c2f96dcSApple OSS Distributions #endif
211*2c2f96dcSApple OSS Distributions NULL};
212*2c2f96dcSApple OSS Distributions
213*2c2f96dcSApple OSS Distributions run_zn("zn_rt_edf", cmd);
214*2c2f96dcSApple OSS Distributions }
215