-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathsq_poll.c
145 lines (124 loc) · 4.02 KB
/
sq_poll.c
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <liburing.h>
#include <string.h>
#define BUF_SIZE 512
#define FILE_NAME1 "/tmp/io_uring_sq_test.txt"
#define STR1 "What is this life if, full of care,\n"
#define STR2 "We have no time to stand and stare."
void print_sq_poll_kernel_thread_status() {
if (system("ps --ppid 2 | grep io_uring-sq" ) == 0)
printf("Kernel thread io_uring-sq found running...\n");
else
printf("Kernel thread io_uring-sq is not running.\n");
}
int start_fixed_buffer_ops(struct io_uring *ring) {
int fds[1];
char buff1[BUF_SIZE];
char buff2[BUF_SIZE];
char buff3[BUF_SIZE];
char buff4[BUF_SIZE];
struct io_uring_sqe *sqe;
struct io_uring_cqe *cqe;
int str1_sz = strlen(STR1);
int str2_sz = strlen(STR2);
fds[0] = open(FILE_NAME1, O_RDWR | O_TRUNC | O_CREAT, 0644);
if (fds[0] < 0 ) {
perror("open");
return 1;
}
memset(buff1, 0, BUF_SIZE);
memset(buff2, 0, BUF_SIZE);
memset(buff3, 0, BUF_SIZE);
memset(buff4, 0, BUF_SIZE);
strncpy(buff1, STR1, str1_sz);
strncpy(buff2, STR2, str2_sz);
int ret = io_uring_register_files(ring, fds, 1);
if(ret) {
fprintf(stderr, "Error registering files: %s", strerror(-ret));
return 1;
}
sqe = io_uring_get_sqe(ring);
if (!sqe) {
fprintf(stderr, "Could not get SQE.\n");
return 1;
}
io_uring_prep_write(sqe, 0, buff1, str1_sz, 0);
io_uring_sqe_set_flags(sqe, IOSQE_FIXED_FILE);
sqe = io_uring_get_sqe(ring);
if (!sqe) {
fprintf(stderr, "Could not get SQE.\n");
return 1;
}
io_uring_prep_write(sqe, 0, buff2, str2_sz, str1_sz);
io_uring_sqe_set_flags(sqe, IOSQE_FIXED_FILE);
io_uring_submit(ring);
for(int i = 0; i < 2; i ++) {
int ret = io_uring_wait_cqe(ring, &cqe);
if (ret < 0) {
fprintf(stderr, "Error waiting for completion: %s\n",
strerror(-ret));
return 1;
}
/* Now that we have the CQE, let's process the data */
if (cqe->res < 0) {
fprintf(stderr, "Error in async operation: %s\n", strerror(-cqe->res));
}
printf("Result of the operation: %d\n", cqe->res);
io_uring_cqe_seen(ring, cqe);
}
print_sq_poll_kernel_thread_status();
sqe = io_uring_get_sqe(ring);
if (!sqe) {
fprintf(stderr, "Could not get SQE.\n");
return 1;
}
io_uring_prep_read(sqe, 0, buff3, str1_sz, 0);
io_uring_sqe_set_flags(sqe, IOSQE_FIXED_FILE);
sqe = io_uring_get_sqe(ring);
if (!sqe) {
fprintf(stderr, "Could not get SQE.\n");
return 1;
}
io_uring_prep_read(sqe, 0, buff4, str2_sz, str1_sz);
io_uring_sqe_set_flags(sqe, IOSQE_FIXED_FILE);
io_uring_submit(ring);
for(int i = 0; i < 2; i ++) {
int ret = io_uring_wait_cqe(ring, &cqe);
if (ret < 0) {
fprintf(stderr, "Error waiting for completion: %s\n",
strerror(-ret));
return 1;
}
/* Now that we have the CQE, let's process the data */
if (cqe->res < 0) {
fprintf(stderr, "Error in async operation: %s\n", strerror(-cqe->res));
}
printf("Result of the operation: %d\n", cqe->res);
io_uring_cqe_seen(ring, cqe);
}
printf("Contents read from file:\n");
printf("%s%s", buff3, buff4);
}
int main() {
struct io_uring ring;
struct io_uring_params params;
if (geteuid()) {
fprintf(stderr, "You need root privileges to run this program.\n");
return 1;
}
print_sq_poll_kernel_thread_status();
memset(¶ms, 0, sizeof(params));
params.flags |= IORING_SETUP_SQPOLL;
params.sq_thread_idle = 2000;
int ret = io_uring_queue_init_params(8, &ring, ¶ms);
if (ret) {
fprintf(stderr, "Unable to setup io_uring: %s\n", strerror(-ret));
return 1;
}
start_fixed_buffer_ops(&ring);
io_uring_queue_exit(&ring);
return 0;
}