io_uring-bench.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Simple benchmark program that uses the various features of io_uring
  4. * to provide fast random access to a device/file. It has various
  5. * options that are control how we use io_uring, see the OPTIONS section
  6. * below. This uses the raw io_uring interface.
  7. *
  8. * Copyright (C) 2018-2019 Jens Axboe
  9. */
  10. #include <stdio.h>
  11. #include <errno.h>
  12. #include <assert.h>
  13. #include <stdlib.h>
  14. #include <stddef.h>
  15. #include <signal.h>
  16. #include <inttypes.h>
  17. #include <sys/types.h>
  18. #include <sys/stat.h>
  19. #include <sys/ioctl.h>
  20. #include <sys/syscall.h>
  21. #include <sys/resource.h>
  22. #include <sys/mman.h>
  23. #include <sys/uio.h>
  24. #include <linux/fs.h>
  25. #include <fcntl.h>
  26. #include <unistd.h>
  27. #include <string.h>
  28. #include <pthread.h>
  29. #include <sched.h>
  30. #include "liburing.h"
  31. #include "barrier.h"
  32. #define min(a, b) ((a < b) ? (a) : (b))
  33. struct io_sq_ring {
  34. unsigned *head;
  35. unsigned *tail;
  36. unsigned *ring_mask;
  37. unsigned *ring_entries;
  38. unsigned *flags;
  39. unsigned *array;
  40. };
  41. struct io_cq_ring {
  42. unsigned *head;
  43. unsigned *tail;
  44. unsigned *ring_mask;
  45. unsigned *ring_entries;
  46. struct io_uring_cqe *cqes;
  47. };
  48. #define DEPTH 128
  49. #define BATCH_SUBMIT 32
  50. #define BATCH_COMPLETE 32
  51. #define BS 4096
  52. #define MAX_FDS 16
  53. static unsigned sq_ring_mask, cq_ring_mask;
  54. struct file {
  55. unsigned long max_blocks;
  56. unsigned pending_ios;
  57. int real_fd;
  58. int fixed_fd;
  59. };
  60. struct submitter {
  61. pthread_t thread;
  62. int ring_fd;
  63. struct drand48_data rand;
  64. struct io_sq_ring sq_ring;
  65. struct io_uring_sqe *sqes;
  66. struct iovec iovecs[DEPTH];
  67. struct io_cq_ring cq_ring;
  68. int inflight;
  69. unsigned long reaps;
  70. unsigned long done;
  71. unsigned long calls;
  72. volatile int finish;
  73. __s32 *fds;
  74. struct file files[MAX_FDS];
  75. unsigned nr_files;
  76. unsigned cur_file;
  77. };
  78. static struct submitter submitters[1];
  79. static volatile int finish;
  80. /*
  81. * OPTIONS: Set these to test the various features of io_uring.
  82. */
  83. static int polled = 1; /* use IO polling */
  84. static int fixedbufs = 1; /* use fixed user buffers */
  85. static int register_files = 1; /* use fixed files */
  86. static int buffered = 0; /* use buffered IO, not O_DIRECT */
  87. static int sq_thread_poll = 0; /* use kernel submission/poller thread */
  88. static int sq_thread_cpu = -1; /* pin above thread to this CPU */
  89. static int do_nop = 0; /* no-op SQ ring commands */
  90. static int io_uring_register_buffers(struct submitter *s)
  91. {
  92. if (do_nop)
  93. return 0;
  94. return io_uring_register(s->ring_fd, IORING_REGISTER_BUFFERS, s->iovecs,
  95. DEPTH);
  96. }
  97. static int io_uring_register_files(struct submitter *s)
  98. {
  99. unsigned i;
  100. if (do_nop)
  101. return 0;
  102. s->fds = calloc(s->nr_files, sizeof(__s32));
  103. for (i = 0; i < s->nr_files; i++) {
  104. s->fds[i] = s->files[i].real_fd;
  105. s->files[i].fixed_fd = i;
  106. }
  107. return io_uring_register(s->ring_fd, IORING_REGISTER_FILES, s->fds,
  108. s->nr_files);
  109. }
  110. static int lk_gettid(void)
  111. {
  112. return syscall(__NR_gettid);
  113. }
  114. static unsigned file_depth(struct submitter *s)
  115. {
  116. return (DEPTH + s->nr_files - 1) / s->nr_files;
  117. }
  118. static void init_io(struct submitter *s, unsigned index)
  119. {
  120. struct io_uring_sqe *sqe = &s->sqes[index];
  121. unsigned long offset;
  122. struct file *f;
  123. long r;
  124. if (do_nop) {
  125. sqe->opcode = IORING_OP_NOP;
  126. return;
  127. }
  128. if (s->nr_files == 1) {
  129. f = &s->files[0];
  130. } else {
  131. f = &s->files[s->cur_file];
  132. if (f->pending_ios >= file_depth(s)) {
  133. s->cur_file++;
  134. if (s->cur_file == s->nr_files)
  135. s->cur_file = 0;
  136. f = &s->files[s->cur_file];
  137. }
  138. }
  139. f->pending_ios++;
  140. lrand48_r(&s->rand, &r);
  141. offset = (r % (f->max_blocks - 1)) * BS;
  142. if (register_files) {
  143. sqe->flags = IOSQE_FIXED_FILE;
  144. sqe->fd = f->fixed_fd;
  145. } else {
  146. sqe->flags = 0;
  147. sqe->fd = f->real_fd;
  148. }
  149. if (fixedbufs) {
  150. sqe->opcode = IORING_OP_READ_FIXED;
  151. sqe->addr = (unsigned long) s->iovecs[index].iov_base;
  152. sqe->len = BS;
  153. sqe->buf_index = index;
  154. } else {
  155. sqe->opcode = IORING_OP_READV;
  156. sqe->addr = (unsigned long) &s->iovecs[index];
  157. sqe->len = 1;
  158. sqe->buf_index = 0;
  159. }
  160. sqe->ioprio = 0;
  161. sqe->off = offset;
  162. sqe->user_data = (unsigned long) f;
  163. }
  164. static int prep_more_ios(struct submitter *s, unsigned max_ios)
  165. {
  166. struct io_sq_ring *ring = &s->sq_ring;
  167. unsigned index, tail, next_tail, prepped = 0;
  168. next_tail = tail = *ring->tail;
  169. do {
  170. next_tail++;
  171. read_barrier();
  172. if (next_tail == *ring->head)
  173. break;
  174. index = tail & sq_ring_mask;
  175. init_io(s, index);
  176. ring->array[index] = index;
  177. prepped++;
  178. tail = next_tail;
  179. } while (prepped < max_ios);
  180. if (*ring->tail != tail) {
  181. /* order tail store with writes to sqes above */
  182. write_barrier();
  183. *ring->tail = tail;
  184. write_barrier();
  185. }
  186. return prepped;
  187. }
  188. static int get_file_size(struct file *f)
  189. {
  190. struct stat st;
  191. if (fstat(f->real_fd, &st) < 0)
  192. return -1;
  193. if (S_ISBLK(st.st_mode)) {
  194. unsigned long long bytes;
  195. if (ioctl(f->real_fd, BLKGETSIZE64, &bytes) != 0)
  196. return -1;
  197. f->max_blocks = bytes / BS;
  198. return 0;
  199. } else if (S_ISREG(st.st_mode)) {
  200. f->max_blocks = st.st_size / BS;
  201. return 0;
  202. }
  203. return -1;
  204. }
  205. static int reap_events(struct submitter *s)
  206. {
  207. struct io_cq_ring *ring = &s->cq_ring;
  208. struct io_uring_cqe *cqe;
  209. unsigned head, reaped = 0;
  210. head = *ring->head;
  211. do {
  212. struct file *f;
  213. read_barrier();
  214. if (head == *ring->tail)
  215. break;
  216. cqe = &ring->cqes[head & cq_ring_mask];
  217. if (!do_nop) {
  218. f = (struct file *) (uintptr_t) cqe->user_data;
  219. f->pending_ios--;
  220. if (cqe->res != BS) {
  221. printf("io: unexpected ret=%d\n", cqe->res);
  222. if (polled && cqe->res == -EOPNOTSUPP)
  223. printf("Your filesystem doesn't support poll\n");
  224. return -1;
  225. }
  226. }
  227. reaped++;
  228. head++;
  229. } while (1);
  230. s->inflight -= reaped;
  231. *ring->head = head;
  232. write_barrier();
  233. return reaped;
  234. }
  235. static void *submitter_fn(void *data)
  236. {
  237. struct submitter *s = data;
  238. struct io_sq_ring *ring = &s->sq_ring;
  239. int ret, prepped;
  240. printf("submitter=%d\n", lk_gettid());
  241. srand48_r(pthread_self(), &s->rand);
  242. prepped = 0;
  243. do {
  244. int to_wait, to_submit, this_reap, to_prep;
  245. if (!prepped && s->inflight < DEPTH) {
  246. to_prep = min(DEPTH - s->inflight, BATCH_SUBMIT);
  247. prepped = prep_more_ios(s, to_prep);
  248. }
  249. s->inflight += prepped;
  250. submit_more:
  251. to_submit = prepped;
  252. submit:
  253. if (to_submit && (s->inflight + to_submit <= DEPTH))
  254. to_wait = 0;
  255. else
  256. to_wait = min(s->inflight + to_submit, BATCH_COMPLETE);
  257. /*
  258. * Only need to call io_uring_enter if we're not using SQ thread
  259. * poll, or if IORING_SQ_NEED_WAKEUP is set.
  260. */
  261. if (!sq_thread_poll || (*ring->flags & IORING_SQ_NEED_WAKEUP)) {
  262. unsigned flags = 0;
  263. if (to_wait)
  264. flags = IORING_ENTER_GETEVENTS;
  265. if ((*ring->flags & IORING_SQ_NEED_WAKEUP))
  266. flags |= IORING_ENTER_SQ_WAKEUP;
  267. ret = io_uring_enter(s->ring_fd, to_submit, to_wait,
  268. flags, NULL);
  269. s->calls++;
  270. }
  271. /*
  272. * For non SQ thread poll, we already got the events we needed
  273. * through the io_uring_enter() above. For SQ thread poll, we
  274. * need to loop here until we find enough events.
  275. */
  276. this_reap = 0;
  277. do {
  278. int r;
  279. r = reap_events(s);
  280. if (r == -1) {
  281. s->finish = 1;
  282. break;
  283. } else if (r > 0)
  284. this_reap += r;
  285. } while (sq_thread_poll && this_reap < to_wait);
  286. s->reaps += this_reap;
  287. if (ret >= 0) {
  288. if (!ret) {
  289. to_submit = 0;
  290. if (s->inflight)
  291. goto submit;
  292. continue;
  293. } else if (ret < to_submit) {
  294. int diff = to_submit - ret;
  295. s->done += ret;
  296. prepped -= diff;
  297. goto submit_more;
  298. }
  299. s->done += ret;
  300. prepped = 0;
  301. continue;
  302. } else if (ret < 0) {
  303. if (errno == EAGAIN) {
  304. if (s->finish)
  305. break;
  306. if (this_reap)
  307. goto submit;
  308. to_submit = 0;
  309. goto submit;
  310. }
  311. printf("io_submit: %s\n", strerror(errno));
  312. break;
  313. }
  314. } while (!s->finish);
  315. finish = 1;
  316. return NULL;
  317. }
  318. static void sig_int(int sig)
  319. {
  320. printf("Exiting on signal %d\n", sig);
  321. submitters[0].finish = 1;
  322. finish = 1;
  323. }
  324. static void arm_sig_int(void)
  325. {
  326. struct sigaction act;
  327. memset(&act, 0, sizeof(act));
  328. act.sa_handler = sig_int;
  329. act.sa_flags = SA_RESTART;
  330. sigaction(SIGINT, &act, NULL);
  331. }
  332. static int setup_ring(struct submitter *s)
  333. {
  334. struct io_sq_ring *sring = &s->sq_ring;
  335. struct io_cq_ring *cring = &s->cq_ring;
  336. struct io_uring_params p;
  337. int ret, fd;
  338. void *ptr;
  339. memset(&p, 0, sizeof(p));
  340. if (polled && !do_nop)
  341. p.flags |= IORING_SETUP_IOPOLL;
  342. if (sq_thread_poll) {
  343. p.flags |= IORING_SETUP_SQPOLL;
  344. if (sq_thread_cpu != -1) {
  345. p.flags |= IORING_SETUP_SQ_AFF;
  346. p.sq_thread_cpu = sq_thread_cpu;
  347. }
  348. }
  349. fd = io_uring_setup(DEPTH, &p);
  350. if (fd < 0) {
  351. perror("io_uring_setup");
  352. return 1;
  353. }
  354. s->ring_fd = fd;
  355. if (fixedbufs) {
  356. ret = io_uring_register_buffers(s);
  357. if (ret < 0) {
  358. perror("io_uring_register_buffers");
  359. return 1;
  360. }
  361. }
  362. if (register_files) {
  363. ret = io_uring_register_files(s);
  364. if (ret < 0) {
  365. perror("io_uring_register_files");
  366. return 1;
  367. }
  368. }
  369. ptr = mmap(0, p.sq_off.array + p.sq_entries * sizeof(__u32),
  370. PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
  371. IORING_OFF_SQ_RING);
  372. printf("sq_ring ptr = 0x%p\n", ptr);
  373. sring->head = ptr + p.sq_off.head;
  374. sring->tail = ptr + p.sq_off.tail;
  375. sring->ring_mask = ptr + p.sq_off.ring_mask;
  376. sring->ring_entries = ptr + p.sq_off.ring_entries;
  377. sring->flags = ptr + p.sq_off.flags;
  378. sring->array = ptr + p.sq_off.array;
  379. sq_ring_mask = *sring->ring_mask;
  380. s->sqes = mmap(0, p.sq_entries * sizeof(struct io_uring_sqe),
  381. PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
  382. IORING_OFF_SQES);
  383. printf("sqes ptr = 0x%p\n", s->sqes);
  384. ptr = mmap(0, p.cq_off.cqes + p.cq_entries * sizeof(struct io_uring_cqe),
  385. PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
  386. IORING_OFF_CQ_RING);
  387. printf("cq_ring ptr = 0x%p\n", ptr);
  388. cring->head = ptr + p.cq_off.head;
  389. cring->tail = ptr + p.cq_off.tail;
  390. cring->ring_mask = ptr + p.cq_off.ring_mask;
  391. cring->ring_entries = ptr + p.cq_off.ring_entries;
  392. cring->cqes = ptr + p.cq_off.cqes;
  393. cq_ring_mask = *cring->ring_mask;
  394. return 0;
  395. }
  396. static void file_depths(char *buf)
  397. {
  398. struct submitter *s = &submitters[0];
  399. unsigned i;
  400. char *p;
  401. buf[0] = '\0';
  402. p = buf;
  403. for (i = 0; i < s->nr_files; i++) {
  404. struct file *f = &s->files[i];
  405. if (i + 1 == s->nr_files)
  406. p += sprintf(p, "%d", f->pending_ios);
  407. else
  408. p += sprintf(p, "%d, ", f->pending_ios);
  409. }
  410. }
  411. int main(int argc, char *argv[])
  412. {
  413. struct submitter *s = &submitters[0];
  414. unsigned long done, calls, reap;
  415. int err, i, flags, fd;
  416. char *fdepths;
  417. void *ret;
  418. if (!do_nop && argc < 2) {
  419. printf("%s: filename\n", argv[0]);
  420. return 1;
  421. }
  422. flags = O_RDONLY | O_NOATIME;
  423. if (!buffered)
  424. flags |= O_DIRECT;
  425. i = 1;
  426. while (!do_nop && i < argc) {
  427. struct file *f;
  428. if (s->nr_files == MAX_FDS) {
  429. printf("Max number of files (%d) reached\n", MAX_FDS);
  430. break;
  431. }
  432. fd = open(argv[i], flags);
  433. if (fd < 0) {
  434. perror("open");
  435. return 1;
  436. }
  437. f = &s->files[s->nr_files];
  438. f->real_fd = fd;
  439. if (get_file_size(f)) {
  440. printf("failed getting size of device/file\n");
  441. return 1;
  442. }
  443. if (f->max_blocks <= 1) {
  444. printf("Zero file/device size?\n");
  445. return 1;
  446. }
  447. f->max_blocks--;
  448. printf("Added file %s\n", argv[i]);
  449. s->nr_files++;
  450. i++;
  451. }
  452. if (fixedbufs) {
  453. struct rlimit rlim;
  454. rlim.rlim_cur = RLIM_INFINITY;
  455. rlim.rlim_max = RLIM_INFINITY;
  456. if (setrlimit(RLIMIT_MEMLOCK, &rlim) < 0) {
  457. perror("setrlimit");
  458. return 1;
  459. }
  460. }
  461. arm_sig_int();
  462. for (i = 0; i < DEPTH; i++) {
  463. void *buf;
  464. if (posix_memalign(&buf, BS, BS)) {
  465. printf("failed alloc\n");
  466. return 1;
  467. }
  468. s->iovecs[i].iov_base = buf;
  469. s->iovecs[i].iov_len = BS;
  470. }
  471. err = setup_ring(s);
  472. if (err) {
  473. printf("ring setup failed: %s, %d\n", strerror(errno), err);
  474. return 1;
  475. }
  476. printf("polled=%d, fixedbufs=%d, buffered=%d", polled, fixedbufs, buffered);
  477. printf(" QD=%d, sq_ring=%d, cq_ring=%d\n", DEPTH, *s->sq_ring.ring_entries, *s->cq_ring.ring_entries);
  478. pthread_create(&s->thread, NULL, submitter_fn, s);
  479. fdepths = malloc(8 * s->nr_files);
  480. reap = calls = done = 0;
  481. do {
  482. unsigned long this_done = 0;
  483. unsigned long this_reap = 0;
  484. unsigned long this_call = 0;
  485. unsigned long rpc = 0, ipc = 0;
  486. sleep(1);
  487. this_done += s->done;
  488. this_call += s->calls;
  489. this_reap += s->reaps;
  490. if (this_call - calls) {
  491. rpc = (this_done - done) / (this_call - calls);
  492. ipc = (this_reap - reap) / (this_call - calls);
  493. } else
  494. rpc = ipc = -1;
  495. file_depths(fdepths);
  496. printf("IOPS=%lu, IOS/call=%ld/%ld, inflight=%u (%s)\n",
  497. this_done - done, rpc, ipc, s->inflight,
  498. fdepths);
  499. done = this_done;
  500. calls = this_call;
  501. reap = this_reap;
  502. } while (!finish);
  503. pthread_join(s->thread, &ret);
  504. close(s->ring_fd);
  505. free(fdepths);
  506. return 0;
  507. }