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