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