1*2c2f96dcSApple OSS Distributions #include <unistd.h>
2*2c2f96dcSApple OSS Distributions #include <stdio.h>
3*2c2f96dcSApple OSS Distributions #include <stdlib.h>
4*2c2f96dcSApple OSS Distributions #include <fcntl.h>
5*2c2f96dcSApple OSS Distributions #include <pthread.h>
6*2c2f96dcSApple OSS Distributions #include <errno.h>
7*2c2f96dcSApple OSS Distributions #include <err.h>
8*2c2f96dcSApple OSS Distributions #include <string.h>
9*2c2f96dcSApple OSS Distributions #include <assert.h>
10*2c2f96dcSApple OSS Distributions #include <sysexits.h>
11*2c2f96dcSApple OSS Distributions #include <getopt.h>
12*2c2f96dcSApple OSS Distributions #include <spawn.h>
13*2c2f96dcSApple OSS Distributions #include <stdbool.h>
14*2c2f96dcSApple OSS Distributions #include <sys/sysctl.h>
15*2c2f96dcSApple OSS Distributions #include <mach/mach_time.h>
16*2c2f96dcSApple OSS Distributions #include <mach/mach.h>
17*2c2f96dcSApple OSS Distributions #include <mach/semaphore.h>
18*2c2f96dcSApple OSS Distributions #include <TargetConditionals.h>
19*2c2f96dcSApple OSS Distributions
20*2c2f96dcSApple OSS Distributions #ifdef T_NAMESPACE
21*2c2f96dcSApple OSS Distributions #undef T_NAMESPACE
22*2c2f96dcSApple OSS Distributions #endif
23*2c2f96dcSApple OSS Distributions
24*2c2f96dcSApple OSS Distributions #include <darwintest.h>
25*2c2f96dcSApple OSS Distributions #include <stdatomic.h>
26*2c2f96dcSApple OSS Distributions
27*2c2f96dcSApple OSS Distributions #define MAX_THREADS 32
28*2c2f96dcSApple OSS Distributions #define SPIN_SECS 6
29*2c2f96dcSApple OSS Distributions #define THR_SPINNER_PRI 63
30*2c2f96dcSApple OSS Distributions #define THR_MANAGER_PRI 62
31*2c2f96dcSApple OSS Distributions #define WARMUP_ITERATIONS 100
32*2c2f96dcSApple OSS Distributions #define FILE_SIZE (16384 * 4096)
33*2c2f96dcSApple OSS Distributions #define IO_SIZE 4096
34*2c2f96dcSApple OSS Distributions #define IO_COUNT 2500
35*2c2f96dcSApple OSS Distributions
36*2c2f96dcSApple OSS Distributions static mach_timebase_info_data_t timebase_info;
37*2c2f96dcSApple OSS Distributions static semaphore_t semaphore;
38*2c2f96dcSApple OSS Distributions static semaphore_t worker_sem;
39*2c2f96dcSApple OSS Distributions static uint32_t g_numcpus;
40*2c2f96dcSApple OSS Distributions static _Atomic uint32_t keep_going = 1;
41*2c2f96dcSApple OSS Distributions int test_file_fd = 0;
42*2c2f96dcSApple OSS Distributions char *data_buf = NULL;
43*2c2f96dcSApple OSS Distributions extern char **environ;
44*2c2f96dcSApple OSS Distributions
45*2c2f96dcSApple OSS Distributions static struct {
46*2c2f96dcSApple OSS Distributions pthread_t thread;
47*2c2f96dcSApple OSS Distributions } threads[MAX_THREADS];
48*2c2f96dcSApple OSS Distributions
49*2c2f96dcSApple OSS Distributions static uint64_t
nanos_to_abs(uint64_t nanos)50*2c2f96dcSApple OSS Distributions nanos_to_abs(uint64_t nanos)
51*2c2f96dcSApple OSS Distributions {
52*2c2f96dcSApple OSS Distributions return nanos * timebase_info.denom / timebase_info.numer;
53*2c2f96dcSApple OSS Distributions }
54*2c2f96dcSApple OSS Distributions
55*2c2f96dcSApple OSS Distributions static void
io_perf_test_io_init(void)56*2c2f96dcSApple OSS Distributions io_perf_test_io_init(void)
57*2c2f96dcSApple OSS Distributions {
58*2c2f96dcSApple OSS Distributions int spawn_ret, pid;
59*2c2f96dcSApple OSS Distributions char *const mount_args[] = {"/usr/local/sbin/mount_nand.sh", NULL};
60*2c2f96dcSApple OSS Distributions spawn_ret = posix_spawn(&pid, mount_args[0], NULL, NULL, mount_args, environ);
61*2c2f96dcSApple OSS Distributions if (spawn_ret < 0) {
62*2c2f96dcSApple OSS Distributions T_SKIP("NAND mounting in LTE not possible on this device. Skipping test!");
63*2c2f96dcSApple OSS Distributions }
64*2c2f96dcSApple OSS Distributions waitpid(pid, &spawn_ret, 0);
65*2c2f96dcSApple OSS Distributions if (WIFEXITED(spawn_ret) && !WEXITSTATUS(spawn_ret)) {
66*2c2f96dcSApple OSS Distributions T_PASS("NAND mounted successfully");
67*2c2f96dcSApple OSS Distributions } else {
68*2c2f96dcSApple OSS Distributions T_SKIP("Unable to mount NAND. Skipping test!");
69*2c2f96dcSApple OSS Distributions }
70*2c2f96dcSApple OSS Distributions
71*2c2f96dcSApple OSS Distributions /* Mark the main thread as fixed priority */
72*2c2f96dcSApple OSS Distributions struct sched_param param = {.sched_priority = THR_MANAGER_PRI};
73*2c2f96dcSApple OSS Distributions T_ASSERT_POSIX_ZERO(pthread_setschedparam(pthread_self(), SCHED_FIFO, ¶m),
74*2c2f96dcSApple OSS Distributions "pthread_setschedparam");
75*2c2f96dcSApple OSS Distributions
76*2c2f96dcSApple OSS Distributions /* Set I/O Policy to Tier 0 */
77*2c2f96dcSApple OSS Distributions T_ASSERT_POSIX_ZERO(setiopolicy_np(IOPOL_TYPE_DISK, IOPOL_SCOPE_PROCESS,
78*2c2f96dcSApple OSS Distributions IOPOL_IMPORTANT), "setiopolicy");
79*2c2f96dcSApple OSS Distributions
80*2c2f96dcSApple OSS Distributions /* Create data buffer */
81*2c2f96dcSApple OSS Distributions data_buf = malloc(IO_SIZE * 16);
82*2c2f96dcSApple OSS Distributions T_ASSERT_NOTNULL(data_buf, "Data buffer allocation");
83*2c2f96dcSApple OSS Distributions
84*2c2f96dcSApple OSS Distributions int rndfd = open("/dev/urandom", O_RDONLY, S_IRUSR);
85*2c2f96dcSApple OSS Distributions T_ASSERT_POSIX_SUCCESS(rndfd, "Open /dev/urandom");
86*2c2f96dcSApple OSS Distributions T_ASSERT_GE_INT((int)read(rndfd, data_buf, IO_SIZE * 16), 0, "read /dev/urandom");
87*2c2f96dcSApple OSS Distributions close(rndfd);
88*2c2f96dcSApple OSS Distributions
89*2c2f96dcSApple OSS Distributions /* Create test file */
90*2c2f96dcSApple OSS Distributions int fd = open("/mnt2/test", O_CREAT | O_WRONLY, S_IRUSR);
91*2c2f96dcSApple OSS Distributions T_ASSERT_POSIX_SUCCESS(fd, 0, "Open /mnt2/test for writing!");
92*2c2f96dcSApple OSS Distributions
93*2c2f96dcSApple OSS Distributions T_ASSERT_POSIX_ZERO(fcntl(fd, F_NOCACHE, 1), "fcntl F_NOCACHE enable");
94*2c2f96dcSApple OSS Distributions for (int size = 0; size < FILE_SIZE;) {
95*2c2f96dcSApple OSS Distributions T_QUIET;
96*2c2f96dcSApple OSS Distributions T_ASSERT_GE_INT((int)write(fd, data_buf, IO_SIZE * 16), 0, "write test file");
97*2c2f96dcSApple OSS Distributions size += (IO_SIZE * 16);
98*2c2f96dcSApple OSS Distributions }
99*2c2f96dcSApple OSS Distributions close(fd);
100*2c2f96dcSApple OSS Distributions sync();
101*2c2f96dcSApple OSS Distributions }
102*2c2f96dcSApple OSS Distributions
103*2c2f96dcSApple OSS Distributions static pthread_t
create_thread(uint32_t thread_id,uint32_t priority,bool fixpri,void * (* start_routine)(void *))104*2c2f96dcSApple OSS Distributions create_thread(uint32_t thread_id, uint32_t priority, bool fixpri,
105*2c2f96dcSApple OSS Distributions void *(*start_routine)(void *))
106*2c2f96dcSApple OSS Distributions {
107*2c2f96dcSApple OSS Distributions int rv;
108*2c2f96dcSApple OSS Distributions pthread_t new_thread;
109*2c2f96dcSApple OSS Distributions struct sched_param param = { .sched_priority = (int)priority };
110*2c2f96dcSApple OSS Distributions pthread_attr_t attr;
111*2c2f96dcSApple OSS Distributions
112*2c2f96dcSApple OSS Distributions T_ASSERT_POSIX_ZERO(pthread_attr_init(&attr), "pthread_attr_init");
113*2c2f96dcSApple OSS Distributions
114*2c2f96dcSApple OSS Distributions T_ASSERT_POSIX_ZERO(pthread_attr_setschedparam(&attr, ¶m),
115*2c2f96dcSApple OSS Distributions "pthread_attr_setschedparam");
116*2c2f96dcSApple OSS Distributions
117*2c2f96dcSApple OSS Distributions if (fixpri) {
118*2c2f96dcSApple OSS Distributions T_ASSERT_POSIX_ZERO(pthread_attr_setschedpolicy(&attr, SCHED_RR),
119*2c2f96dcSApple OSS Distributions "pthread_attr_setschedpolicy");
120*2c2f96dcSApple OSS Distributions }
121*2c2f96dcSApple OSS Distributions
122*2c2f96dcSApple OSS Distributions T_ASSERT_POSIX_ZERO(pthread_create(&new_thread, &attr, start_routine,
123*2c2f96dcSApple OSS Distributions (void*)(uintptr_t)thread_id), "pthread_create");
124*2c2f96dcSApple OSS Distributions
125*2c2f96dcSApple OSS Distributions T_ASSERT_POSIX_ZERO(pthread_attr_destroy(&attr), "pthread_attr_destroy");
126*2c2f96dcSApple OSS Distributions
127*2c2f96dcSApple OSS Distributions threads[thread_id].thread = new_thread;
128*2c2f96dcSApple OSS Distributions
129*2c2f96dcSApple OSS Distributions return new_thread;
130*2c2f96dcSApple OSS Distributions }
131*2c2f96dcSApple OSS Distributions
132*2c2f96dcSApple OSS Distributions /* Spin until a specified number of seconds elapses */
133*2c2f96dcSApple OSS Distributions static void
spin_for_duration(uint32_t seconds)134*2c2f96dcSApple OSS Distributions spin_for_duration(uint32_t seconds)
135*2c2f96dcSApple OSS Distributions {
136*2c2f96dcSApple OSS Distributions uint64_t duration = nanos_to_abs((uint64_t)seconds * NSEC_PER_SEC);
137*2c2f96dcSApple OSS Distributions uint64_t current_time = mach_absolute_time();
138*2c2f96dcSApple OSS Distributions uint64_t timeout = duration + current_time;
139*2c2f96dcSApple OSS Distributions
140*2c2f96dcSApple OSS Distributions uint64_t spin_count = 0;
141*2c2f96dcSApple OSS Distributions
142*2c2f96dcSApple OSS Distributions while (mach_absolute_time() < timeout && atomic_load_explicit(&keep_going,
143*2c2f96dcSApple OSS Distributions memory_order_relaxed)) {
144*2c2f96dcSApple OSS Distributions spin_count++;
145*2c2f96dcSApple OSS Distributions }
146*2c2f96dcSApple OSS Distributions }
147*2c2f96dcSApple OSS Distributions
148*2c2f96dcSApple OSS Distributions static void *
spin_thread(void * arg)149*2c2f96dcSApple OSS Distributions spin_thread(void *arg)
150*2c2f96dcSApple OSS Distributions {
151*2c2f96dcSApple OSS Distributions uint32_t thread_id = (uint32_t) arg;
152*2c2f96dcSApple OSS Distributions char name[30] = "";
153*2c2f96dcSApple OSS Distributions
154*2c2f96dcSApple OSS Distributions snprintf(name, sizeof(name), "spin thread %2d", thread_id);
155*2c2f96dcSApple OSS Distributions pthread_setname_np(name);
156*2c2f96dcSApple OSS Distributions T_ASSERT_MACH_SUCCESS(semaphore_wait_signal(semaphore, worker_sem),
157*2c2f96dcSApple OSS Distributions "semaphore_wait_signal");
158*2c2f96dcSApple OSS Distributions spin_for_duration(SPIN_SECS);
159*2c2f96dcSApple OSS Distributions return NULL;
160*2c2f96dcSApple OSS Distributions }
161*2c2f96dcSApple OSS Distributions
162*2c2f96dcSApple OSS Distributions void
perform_io(dt_stat_time_t stat)163*2c2f96dcSApple OSS Distributions perform_io(dt_stat_time_t stat)
164*2c2f96dcSApple OSS Distributions {
165*2c2f96dcSApple OSS Distributions /* Open the test data file */
166*2c2f96dcSApple OSS Distributions int test_file_fd = open("/mnt2/test", O_RDONLY);
167*2c2f96dcSApple OSS Distributions T_WITH_ERRNO;
168*2c2f96dcSApple OSS Distributions T_ASSERT_POSIX_SUCCESS(test_file_fd, "Open test data file");
169*2c2f96dcSApple OSS Distributions
170*2c2f96dcSApple OSS Distributions /* Disable caching and read-ahead for the file */
171*2c2f96dcSApple OSS Distributions T_ASSERT_POSIX_ZERO(fcntl(test_file_fd, F_NOCACHE, 1), "fcntl F_NOCACHE enable");
172*2c2f96dcSApple OSS Distributions T_ASSERT_POSIX_ZERO(fcntl(test_file_fd, F_RDAHEAD, 0), "fcntl F_RDAHEAD disable");
173*2c2f96dcSApple OSS Distributions
174*2c2f96dcSApple OSS Distributions uint32_t count = 0;
175*2c2f96dcSApple OSS Distributions int ret;
176*2c2f96dcSApple OSS Distributions
177*2c2f96dcSApple OSS Distributions for (int i = 0; i < WARMUP_ITERATIONS; i++) {
178*2c2f96dcSApple OSS Distributions /* Warmup loop */
179*2c2f96dcSApple OSS Distributions read(test_file_fd, data_buf, IO_SIZE);
180*2c2f96dcSApple OSS Distributions }
181*2c2f96dcSApple OSS Distributions
182*2c2f96dcSApple OSS Distributions do {
183*2c2f96dcSApple OSS Distributions T_STAT_MEASURE(stat) {
184*2c2f96dcSApple OSS Distributions ret = read(test_file_fd, data_buf, IO_SIZE);
185*2c2f96dcSApple OSS Distributions }
186*2c2f96dcSApple OSS Distributions if (ret == 0) {
187*2c2f96dcSApple OSS Distributions T_QUIET;
188*2c2f96dcSApple OSS Distributions T_ASSERT_POSIX_SUCCESS(lseek(test_file_fd, 0, SEEK_SET), "lseek begin");
189*2c2f96dcSApple OSS Distributions } else if (ret < 0) {
190*2c2f96dcSApple OSS Distributions T_FAIL("read failure");
191*2c2f96dcSApple OSS Distributions T_END;
192*2c2f96dcSApple OSS Distributions }
193*2c2f96dcSApple OSS Distributions count++;
194*2c2f96dcSApple OSS Distributions } while (count < IO_COUNT);
195*2c2f96dcSApple OSS Distributions close(test_file_fd);
196*2c2f96dcSApple OSS Distributions }
197*2c2f96dcSApple OSS Distributions
198*2c2f96dcSApple OSS Distributions T_GLOBAL_META(T_META_NAMESPACE("xnu.io"), T_META_TAG_PERF);
199*2c2f96dcSApple OSS Distributions
200*2c2f96dcSApple OSS Distributions /* Disable the test on MacOS for now */
201*2c2f96dcSApple OSS Distributions T_DECL(read_perf, "Sequential Uncached Read Performance", T_META_TYPE_PERF, T_META_CHECK_LEAKS(NO), T_META_ASROOT(YES), T_META_LTEPHASE(LTE_POSTINIT))
202*2c2f96dcSApple OSS Distributions {
203*2c2f96dcSApple OSS Distributions #if !CONFIG_EMBEDDED
204*2c2f96dcSApple OSS Distributions T_SKIP("Not supported on MacOS");
205*2c2f96dcSApple OSS Distributions #endif /* !CONFIG_EMBEDDED */
206*2c2f96dcSApple OSS Distributions
207*2c2f96dcSApple OSS Distributions io_perf_test_io_init();
208*2c2f96dcSApple OSS Distributions pthread_setname_np("main thread");
209*2c2f96dcSApple OSS Distributions
210*2c2f96dcSApple OSS Distributions T_ASSERT_MACH_SUCCESS(mach_timebase_info(&timebase_info), "mach_timebase_info");
211*2c2f96dcSApple OSS Distributions
212*2c2f96dcSApple OSS Distributions dt_stat_time_t seq_noload = dt_stat_time_create("sequential read latency (CPU idle)");
213*2c2f96dcSApple OSS Distributions perform_io(seq_noload);
214*2c2f96dcSApple OSS Distributions dt_stat_finalize(seq_noload);
215*2c2f96dcSApple OSS Distributions
216*2c2f96dcSApple OSS Distributions /*
217*2c2f96dcSApple OSS Distributions * We create spinner threads for this test so that all other cores are
218*2c2f96dcSApple OSS Distributions * busy. That way the I/O issue thread has to context switch to the
219*2c2f96dcSApple OSS Distributions * IOWorkLoop thread and back for the I/O.
220*2c2f96dcSApple OSS Distributions */
221*2c2f96dcSApple OSS Distributions T_ASSERT_MACH_SUCCESS(semaphore_create(mach_task_self(), &semaphore,
222*2c2f96dcSApple OSS Distributions SYNC_POLICY_FIFO, 0), "semaphore_create");
223*2c2f96dcSApple OSS Distributions
224*2c2f96dcSApple OSS Distributions T_ASSERT_MACH_SUCCESS(semaphore_create(mach_task_self(), &worker_sem,
225*2c2f96dcSApple OSS Distributions SYNC_POLICY_FIFO, 0), "semaphore_create");
226*2c2f96dcSApple OSS Distributions
227*2c2f96dcSApple OSS Distributions size_t ncpu_size = sizeof(g_numcpus);
228*2c2f96dcSApple OSS Distributions T_ASSERT_POSIX_SUCCESS(sysctlbyname("hw.ncpu", &g_numcpus, &ncpu_size, NULL, 0),
229*2c2f96dcSApple OSS Distributions "sysctlbyname(hw.ncpu)");
230*2c2f96dcSApple OSS Distributions
231*2c2f96dcSApple OSS Distributions T_LOG("hw.ncpu: %d\n", g_numcpus);
232*2c2f96dcSApple OSS Distributions uint32_t n_spinners = g_numcpus - 1;
233*2c2f96dcSApple OSS Distributions
234*2c2f96dcSApple OSS Distributions for (uint32_t thread_id = 0; thread_id < n_spinners; thread_id++) {
235*2c2f96dcSApple OSS Distributions threads[thread_id].thread = create_thread(thread_id, THR_SPINNER_PRI,
236*2c2f96dcSApple OSS Distributions true, &spin_thread);
237*2c2f96dcSApple OSS Distributions }
238*2c2f96dcSApple OSS Distributions
239*2c2f96dcSApple OSS Distributions for (uint32_t thread_id = 0; thread_id < n_spinners; thread_id++) {
240*2c2f96dcSApple OSS Distributions T_ASSERT_MACH_SUCCESS(semaphore_wait(worker_sem), "semaphore_wait");
241*2c2f96dcSApple OSS Distributions }
242*2c2f96dcSApple OSS Distributions
243*2c2f96dcSApple OSS Distributions T_ASSERT_MACH_SUCCESS(semaphore_signal_all(semaphore), "semaphore_signal");
244*2c2f96dcSApple OSS Distributions
245*2c2f96dcSApple OSS Distributions dt_stat_time_t seq_load = dt_stat_time_create("sequential read latency (Single CPU)");
246*2c2f96dcSApple OSS Distributions perform_io(seq_load);
247*2c2f96dcSApple OSS Distributions dt_stat_finalize(seq_load);
248*2c2f96dcSApple OSS Distributions
249*2c2f96dcSApple OSS Distributions atomic_store_explicit(&keep_going, 0, memory_order_relaxed);
250*2c2f96dcSApple OSS Distributions for (uint32_t thread_id = 0; thread_id < n_spinners; thread_id++) {
251*2c2f96dcSApple OSS Distributions T_ASSERT_POSIX_ZERO(pthread_join(threads[thread_id].thread, NULL),
252*2c2f96dcSApple OSS Distributions "pthread_join %d", thread_id);
253*2c2f96dcSApple OSS Distributions }
254*2c2f96dcSApple OSS Distributions }
255