• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SPDX-License-Identifier: MIT */
2 #include <errno.h>
3 #include <stdio.h>
4 #include <unistd.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <signal.h>
8 #include <poll.h>
9 #include <sys/time.h>
10 #include <sys/wait.h>
11 
12 #include "liburing.h"
13 
14 static unsigned long runtime_ms = 10000;
15 
gettimeofday_ms(void)16 static unsigned long gettimeofday_ms(void)
17 {
18 	struct timeval tv;
19 
20 	gettimeofday(&tv, NULL);
21 	return (tv.tv_sec * 1000) + (tv.tv_usec / 1000);
22 }
23 
main(void)24 int main(void)
25 {
26 	unsigned long tstop;
27 	unsigned long nr_reqs = 0;
28 	struct io_uring_cqe *cqe;
29 	struct io_uring_sqe *sqe;
30 	struct io_uring ring;
31 	int pipe1[2];
32 	int ret, i, qd = 32;
33 	int table_size = 128;
34 
35 	if (pipe(pipe1) != 0) {
36 		perror("pipe");
37 		return 1;
38 	}
39 
40 	ret = io_uring_queue_init(1024, &ring, IORING_SETUP_SINGLE_ISSUER |
41 					       IORING_SETUP_DEFER_TASKRUN);
42 	if (ret) {
43 		fprintf(stderr, "io_uring_queue_init failed: %d\n", ret);
44 		return 1;
45 	}
46 	ret = io_uring_register_ring_fd(&ring);
47 	if (ret < 0) {
48 		fprintf(stderr, "io_uring_register_ring_fd failed\n");
49 		return 1;
50 	}
51 	ret = io_uring_register_files_sparse(&ring, table_size);
52 	if (ret < 0) {
53 		fprintf(stderr, "io_uring_register_files_sparse failed\n");
54 		return 1;
55 	}
56 
57 	for (i = 0; i < table_size; i++) {
58 		ret = io_uring_register_files_update(&ring, i, pipe1, 1);
59 		if (ret < 0) {
60 			fprintf(stderr, "io_uring_register_files_update failed\n");
61 			return 1;
62 		}
63 	}
64 
65 	srand(time(NULL));
66 
67 	tstop = gettimeofday_ms() + runtime_ms;
68 	do {
69 		int off = rand();
70 
71 		for (i = 0; i < qd; i++) {
72 			sqe = io_uring_get_sqe(&ring);
73 			int roff = (off + i) % table_size;
74 			io_uring_prep_files_update(sqe, pipe1, 1, roff);
75 		}
76 
77 		ret = io_uring_submit(&ring);
78 		if (ret != qd) {
79 			fprintf(stderr, "child: sqe submit failed: %d\n", ret);
80 			return 1;
81 		}
82 
83 		for (i = 0; i < qd; i++) {
84 			ret = io_uring_wait_cqe(&ring, &cqe);
85 			if (ret < 0) {
86 				fprintf(stderr, "child: wait completion %d\n", ret);
87 				break;
88 			}
89 			io_uring_cqe_seen(&ring, cqe);
90 			nr_reqs++;
91 		}
92 	} while (gettimeofday_ms() < tstop);
93 
94 	fprintf(stderr, "max updates/s: %lu\n", nr_reqs * 1000UL / runtime_ms);
95 
96 	io_uring_queue_exit(&ring);
97 	close(pipe1[0]);
98 	close(pipe1[1]);
99 	return 0;
100 }
101