File: poll-bench.c

package info (click to toggle)
liburing 2.13-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,212 kB
  • sloc: ansic: 58,515; sh: 816; makefile: 598; cpp: 32
file content (101 lines) | stat: -rw-r--r-- 2,129 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/* SPDX-License-Identifier: MIT */
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <poll.h>
#include <sys/time.h>
#include <sys/wait.h>

#include "liburing.h"

static char buf[4096];
static unsigned long runtime_ms = 10000;

static unsigned long gettimeofday_ms(void)
{
	struct timeval tv;

	gettimeofday(&tv, NULL);
	return (tv.tv_sec * 1000) + (tv.tv_usec / 1000);
}

int main(void)
{
	unsigned long tstop;
	unsigned long nr_reqs = 0;
	struct io_uring_cqe *cqe;
	struct io_uring_sqe *sqe;
	struct io_uring ring;
	int pipe1[2];
	int ret, i, qd = 32;

	if (pipe(pipe1) != 0) {
		perror("pipe");
		return 1;
	}

	ret = io_uring_queue_init(1024, &ring, IORING_SETUP_SINGLE_ISSUER);
	if (ret == -EINVAL) {
		fprintf(stderr, "can't single\n");
		ret = io_uring_queue_init(1024, &ring, 0);
	}
	if (ret) {
		fprintf(stderr, "child: ring setup failed: %d\n", ret);
		return 1;
	}

	ret = io_uring_register_files(&ring, pipe1, 2);
	if (ret < 0) {
		fprintf(stderr, "io_uring_register_files failed\n");
		return 1;
	}

	ret = io_uring_register_ring_fd(&ring);
	if (ret < 0) {
		fprintf(stderr, "io_uring_register_ring_fd failed\n");
		return 1;
	}

	tstop = gettimeofday_ms() + runtime_ms;
	do {
		for (i = 0; i < qd; i++) {
			sqe = io_uring_get_sqe(&ring);
			io_uring_prep_poll_add(sqe, 0, POLLIN);
			sqe->flags |= IOSQE_FIXED_FILE;
			sqe->user_data = 1;
		}

		ret = io_uring_submit(&ring);
		if (ret != qd) {
			fprintf(stderr, "child: sqe submit failed: %d\n", ret);
			return 1;
		}

		ret = write(pipe1[1], buf, 1);
		if (ret != 1) {
			fprintf(stderr, "write failed %i\n", errno);
			return 1;
		}
		ret = read(pipe1[0], buf, 1);
		if (ret != 1) {
			fprintf(stderr, "read failed %i\n", errno);
			return 1;
		}

		for (i = 0; i < qd; i++) {
			ret = io_uring_wait_cqe(&ring, &cqe);
			if (ret < 0) {
				fprintf(stderr, "child: wait completion %d\n", ret);
				break;
			}
			io_uring_cqe_seen(&ring, cqe);
			nr_reqs++;
		}
	} while (gettimeofday_ms() < tstop);

	fprintf(stderr, "requests/s: %lu\n", nr_reqs * 1000UL / runtime_ms);
	return 0;
}