2 * Read a file and write the contents to stdout. If a given read takes
3 * longer than 'max_us' time, then we schedule a new thread to handle
4 * the next read. This avoids the coordinated omission problem, where
5 * one request appears to take a long time, but in reality a lot of
6 * requests would have been slow, but we don't notice since new submissions
7 * are not being issued if just 1 is held up.
11 * $ time (./read-to-pipe-async -f randfile.gz | gzip -dc > outfile; sync)
13 * This will read randfile.gz and log the latencies of doing so, while
14 * piping the output to gzip to decompress it. Any latencies over max_us
15 * are logged when they happen, and latency buckets are displayed at the
18 * gcc -Wall -g -O2 -o read-to-pipe-async read-to-pipe-async.c -lpthread
20 * Copyright (C) 2016 Jens Axboe
28 #include <sys/types.h>
39 static int max_us = 10000;
41 static int separate_writer = 1;
44 #define PLAT_VAL (1 << PLAT_BITS)
45 #define PLAT_GROUP_NR 19
46 #define PLAT_NR (PLAT_GROUP_NR * PLAT_VAL)
47 #define PLAT_LIST_MAX 20
50 unsigned int plat[PLAT_NR];
51 unsigned int nr_samples;
57 static double plist[PLAT_LIST_MAX] = { 50.0, 75.0, 90.0, 95.0, 99.0, 99.5, 99.9, 99.99, 99.999, 99.9999, };
64 pthread_mutex_t done_lock;
65 pthread_cond_t done_cond;
69 struct writer_thread {
70 struct flist_head list;
71 struct flist_head done_list;
73 struct thread_data thread;
76 struct reader_thread {
77 struct flist_head list;
78 struct flist_head done_list;
83 struct thread_data thread;
87 struct flist_head list;
93 struct writer_thread *writer;
94 struct reader_thread *reader;
100 static struct reader_thread reader_thread;
101 static struct writer_thread writer_thread;
103 uint64_t utime_since(const struct timeval *s, const struct timeval *e)
108 sec = e->tv_sec - s->tv_sec;
109 usec = e->tv_usec - s->tv_usec;
110 if (sec > 0 && usec < 0) {
115 if (sec < 0 || (sec == 0 && usec < 0))
118 ret = sec * 1000000ULL + usec;
123 static struct work_item *find_seq(struct writer_thread *w, unsigned int seq)
125 struct work_item *work;
126 struct flist_head *entry;
128 if (flist_empty(&w->list))
131 flist_for_each(entry, &w->list) {
132 work = flist_entry(entry, struct work_item, list);
133 if (work->seq == seq)
140 static unsigned int plat_val_to_idx(unsigned int val)
142 unsigned int msb, error_bits, base, offset;
144 /* Find MSB starting from bit 0 */
148 msb = sizeof(val)*8 - __builtin_clz(val) - 1;
151 * MSB <= (PLAT_BITS-1), cannot be rounded off. Use
152 * all bits of the sample as index
154 if (msb <= PLAT_BITS)
157 /* Compute the number of error bits to discard*/
158 error_bits = msb - PLAT_BITS;
160 /* Compute the number of buckets before the group */
161 base = (error_bits + 1) << PLAT_BITS;
164 * Discard the error bits and apply the mask to find the
165 * index for the buckets in the group
167 offset = (PLAT_VAL - 1) & (val >> error_bits);
169 /* Make sure the index does not exceed (array size - 1) */
170 return (base + offset) < (PLAT_NR - 1) ?
171 (base + offset) : (PLAT_NR - 1);
175 * Convert the given index of the bucket array to the value
176 * represented by the bucket
178 static unsigned int plat_idx_to_val(unsigned int idx)
180 unsigned int error_bits, k, base;
182 assert(idx < PLAT_NR);
184 /* MSB <= (PLAT_BITS-1), cannot be rounded off. Use
185 * all bits of the sample as index */
186 if (idx < (PLAT_VAL << 1))
189 /* Find the group and compute the minimum value of that group */
190 error_bits = (idx >> PLAT_BITS) - 1;
191 base = 1 << (error_bits + PLAT_BITS);
193 /* Find its bucket number of the group */
196 /* Return the mean of the range of the bucket */
197 return base + ((k + 0.5) * (1 << error_bits));
200 static void add_lat(struct stats *s, unsigned int us, const char *name)
210 fprintf(stderr, "%s latency=%u usec\n", name, us);
214 lat_index = plat_val_to_idx(us);
215 __sync_fetch_and_add(&s->plat[lat_index], 1);
216 __sync_fetch_and_add(&s->nr_samples, 1);
219 static int write_work(struct work_item *work)
224 gettimeofday(&s, NULL);
225 ret = write(STDOUT_FILENO, work->buf, work->buf_size);
226 gettimeofday(&e, NULL);
227 assert(ret == work->buf_size);
229 add_lat(&work->writer->s, utime_since(&s, &e), "write");
230 return work->seq + 1;
233 static void *writer_fn(void *data)
235 struct writer_thread *wt = data;
236 struct work_item *work;
237 unsigned int seq = 1;
240 while (!wt->thread.exit || !flist_empty(&wt->list)) {
241 pthread_mutex_lock(&wt->thread.lock);
244 flist_add_tail(&work->list, &wt->done_list);
248 work = find_seq(wt, seq);
250 flist_del_init(&work->list);
252 pthread_cond_wait(&wt->thread.cond, &wt->thread.lock);
254 pthread_mutex_unlock(&wt->thread.lock);
257 seq = write_work(work);
261 pthread_cond_signal(&wt->thread.done_cond);
265 static void reader_work(struct work_item *work)
273 gettimeofday(&s, NULL);
275 left = work->buf_size;
279 ret = pread(work->fd, buf, left, off);
281 fprintf(stderr, "zero read\n");
283 } else if (ret < 0) {
284 fprintf(stderr, "errno=%d\n", errno);
292 gettimeofday(&e, NULL);
294 add_lat(&work->reader->s, utime_since(&s, &e), "read");
296 pthread_cond_signal(&work->cond);
298 if (separate_writer) {
299 pthread_mutex_lock(&work->writer->thread.lock);
300 flist_add_tail(&work->list, &work->writer->list);
301 pthread_mutex_unlock(&work->writer->thread.lock);
302 pthread_cond_signal(&work->writer->thread.cond);
304 struct reader_thread *rt = work->reader;
305 struct work_item *next = NULL;
306 struct flist_head *entry;
309 * Write current work if it matches in sequence.
311 if (work->seq == rt->write_seq)
314 pthread_mutex_lock(&rt->thread.lock);
316 flist_add_tail(&work->list, &rt->done_list);
319 * See if the next work item is here, if so, write it
322 flist_for_each(entry, &rt->done_list) {
323 next = flist_entry(entry, struct work_item, list);
324 if (next->seq == rt->write_seq) {
326 flist_del(&work->list);
331 pthread_mutex_unlock(&rt->thread.lock);
336 __sync_fetch_and_add(&rt->write_seq, 1);
341 static void *reader_one_off(void *data)
347 static void *reader_fn(void *data)
349 struct reader_thread *rt = data;
350 struct work_item *work;
352 while (!rt->thread.exit || !flist_empty(&rt->list)) {
354 pthread_mutex_lock(&rt->thread.lock);
355 if (!flist_empty(&rt->list)) {
356 work = flist_first_entry(&rt->list, struct work_item, list);
357 flist_del_init(&work->list);
359 pthread_cond_wait(&rt->thread.cond, &rt->thread.lock);
360 pthread_mutex_unlock(&rt->thread.lock);
370 pthread_cond_signal(&rt->thread.done_cond);
374 static void queue_work(struct reader_thread *rt, struct work_item *work)
377 pthread_mutex_lock(&rt->thread.lock);
378 flist_add_tail(&work->list, &rt->list);
379 pthread_mutex_unlock(&rt->thread.lock);
382 pthread_create(&rt->thread.thread, NULL, reader_fn, rt);
383 } else if (!rt->busy && !pthread_mutex_trylock(&rt->thread.lock)) {
384 flist_add_tail(&work->list, &rt->list);
385 pthread_mutex_unlock(&rt->thread.lock);
387 pthread_cond_signal(&rt->thread.cond);
389 int ret = pthread_create(&work->thread, NULL, reader_one_off, work);
391 fprintf(stderr, "pthread_create=%d\n", ret);
393 pthread_detach(work->thread);
397 static unsigned int calc_percentiles(unsigned int *io_u_plat, unsigned long nr,
398 unsigned int **output)
400 unsigned long sum = 0;
401 unsigned int len, i, j = 0;
402 unsigned int oval_len = 0;
403 unsigned int *ovals = NULL;
407 while (len < PLAT_LIST_MAX && plist[len] != 0.0)
414 * Calculate bucket values, note down max and min values
417 for (i = 0; i < PLAT_NR && !is_last; i++) {
419 while (sum >= (plist[j] / 100.0 * nr)) {
420 assert(plist[j] <= 100.0);
424 ovals = realloc(ovals, oval_len * sizeof(unsigned int));
427 ovals[j] = plat_idx_to_val(i);
428 is_last = (j == len - 1);
440 static void show_latencies(struct stats *s, const char *msg)
442 unsigned int *ovals = NULL;
445 len = calc_percentiles(s->plat, s->nr_samples, &ovals);
447 fprintf(stderr, "Latency percentiles (usec) (%s)\n", msg);
448 for (i = 0; i < len; i++)
449 fprintf(stderr, "\t%2.4fth: %u\n", plist[i], ovals[i]);
455 fprintf(stderr, "\tOver=%u, min=%u, max=%u\n", s->over, s->min, s->max);
458 static void init_thread(struct thread_data *thread)
460 pthread_cond_init(&thread->cond, NULL);
461 pthread_cond_init(&thread->done_cond, NULL);
462 pthread_mutex_init(&thread->lock, NULL);
463 pthread_mutex_init(&thread->done_lock, NULL);
467 static void exit_thread(struct thread_data *thread,
468 void fn(struct writer_thread *),
469 struct writer_thread *wt)
472 pthread_cond_signal(&thread->cond);
474 while (!thread->done) {
475 pthread_mutex_lock(&thread->done_lock);
480 clock_gettime(CLOCK_REALTIME, &t);
484 pthread_cond_timedwait(&thread->done_cond, &thread->done_lock, &t);
487 pthread_cond_wait(&thread->done_cond, &thread->done_lock);
489 pthread_mutex_unlock(&thread->done_lock);
493 static int usage(char *argv[])
495 fprintf(stderr, "%s: [-b blocksize] [-t max usec] [-w separate writer] -f file\n", argv[0]);
499 static int parse_options(int argc, char *argv[])
503 while ((c = getopt(argc, argv, "f:b:t:w:")) != -1) {
506 file = strdup(optarg);
512 max_us = atoi(optarg);
515 separate_writer = atoi(optarg);
516 if (!separate_writer)
517 fprintf(stderr, "inline writing is broken\n");
531 static void prune_done_entries(struct writer_thread *wt)
535 if (flist_empty(&wt->done_list))
538 if (pthread_mutex_trylock(&wt->thread.lock))
541 if (!flist_empty(&wt->done_list))
542 flist_splice_init(&wt->done_list, &list);
543 pthread_mutex_unlock(&wt->thread.lock);
545 while (!flist_empty(&list)) {
546 struct work_item *work;
548 work = flist_first_entry(&list, struct work_item, list);
549 flist_del(&work->list);
551 pthread_cond_destroy(&work->cond);
552 pthread_mutex_destroy(&work->lock);
558 int main(int argc, char *argv[])
560 struct timeval s, re, we;
561 struct reader_thread *rt;
562 struct writer_thread *wt;
569 if (parse_options(argc, argv))
572 fd = open(file, O_RDONLY);
578 if (fstat(fd, &sb) < 0) {
584 init_thread(&wt->thread);
585 INIT_FLIST_HEAD(&wt->list);
586 INIT_FLIST_HEAD(&wt->done_list);
589 pthread_create(&wt->thread.thread, NULL, writer_fn, wt);
592 init_thread(&rt->thread);
593 INIT_FLIST_HEAD(&rt->list);
594 INIT_FLIST_HEAD(&rt->done_list);
603 gettimeofday(&s, NULL);
606 struct work_item *work;
610 prune_done_entries(wt);
612 this_len = sb.st_size;
616 work = calloc(1, sizeof(*work));
617 work->buf = malloc(this_len);
618 work->buf_size = this_len;
624 pthread_cond_init(&work->cond, NULL);
625 pthread_mutex_init(&work->lock, NULL);
627 queue_work(rt, work);
629 clock_gettime(CLOCK_REALTIME, &t);
630 t.tv_nsec += max_us * 1000ULL;
631 if (t.tv_nsec >= 1000000000ULL) {
632 t.tv_nsec -= 1000000000ULL;
636 pthread_mutex_lock(&work->lock);
637 pthread_cond_timedwait(&work->cond, &work->lock, &t);
638 pthread_mutex_unlock(&work->lock);
641 sb.st_size -= this_len;
645 exit_thread(&rt->thread, NULL, NULL);
646 gettimeofday(&re, NULL);
648 exit_thread(&wt->thread, prune_done_entries, wt);
649 gettimeofday(&we, NULL);
651 show_latencies(&rt->s, "READERS");
652 show_latencies(&wt->s, "WRITERS");
655 rate = (bytes * 1000UL * 1000UL) / utime_since(&s, &re);
656 fprintf(stderr, "Read rate (KB/sec) : %lu\n", rate);
657 rate = (bytes * 1000UL * 1000UL) / utime_since(&s, &we);
658 fprintf(stderr, "Write rate (KB/sec): %lu\n", rate);