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