forked from qinyihust/uring_sample
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathio_demo.cpp
141 lines (117 loc) · 3.49 KB
/
io_demo.cpp
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
#include <iostream>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include "io_queue.h"
#define QD 64
#define BS (4*1024)
#define NR_THREAD 10
struct TestEnv {
int index;
Submitter *submitter;
int fd;
};
int queueIo(TestEnv *env, off_t offset, off_t len, bool isRead, void *buf, IocbFunc cb, void *arg) {
IoTask *task = (IoTask *)malloc(sizeof(*task));
if (!task)
return -1;
task->index = env->index;
task->fd = env->fd;
task->isRead = isRead;
task->offset = offset;
task->first_offset = offset;
task->first_len = len;
task->iov.iov_base = buf;
task->iov.iov_len = len;
task->cb = cb;
task->arg = env->submitter;
task->res = -1;
env->submitter->Push(task);
return 0;
}
void cb1(IoTask *task) {
assert (!task->isRead);
unsigned num = ((char *)(task->iov.iov_base))[0];
std::cout << "reaper: write for thread " << task->index <<" done, data="
<< std::hex << num << ", res="<< task->res << std::endl;
delete task;
}
void cb0(IoTask *task) {
assert (task->isRead);
unsigned num = ((char *)(task->iov.iov_base))[0];
std::cout << "reaper: read from thread " << task->index << " done, data="
<< std::hex << num << ", res=" << task->res << std::endl;
num++;
std::cout << "reaper: send write I/O for thread " << task->index << ", data="
<< std::hex << num << std::endl;
// send write I/O
task->isRead = false;
memset(task->iov.iov_base, num, BS);
task->cb = cb1;
task->res = -1;
Submitter *submitter = (Submitter *)task->arg;
submitter->Push(task);
}
void *SendIo(void *arg) {
TestEnv *env = (TestEnv *)arg;
int idx = env->index;
char *data = nullptr;
posix_memalign((void **)&data, getpagesize(), BS);
memset(data, 0, BS);
std::cout << "thread " << env->index << ": read testfile" << std::endl;
queueIo(env, 0, BS, true, data, cb0, nullptr);
sleep(3);
return nullptr;
}
int main(int argc, const char* argv[]) {
int ret = 0;
if (argc != 2) {
std::cout << "usage: " << argv[0] << " [option]" << std::endl;
std::cout << "option: libaio or uring" << std::endl;
return -1;
}
IoEngine engine = IoEngine::IO_ENGINE_NONE;
if (!strncmp(argv[1], "libaio", 7))
engine = IoEngine::IO_ENGINE_LIBAIO;
else if (!strncmp(argv[1], "uring", 6))
engine = IoEngine::IO_ENGINE_URING;
else {
std::cout << "usage: " << argv[0] << " [option]" << std::endl;
std::cout << "option: libaio or uring" << std::endl;
return -1;
}
Submitter submitter(engine, QD);
if (submitter.Run())
return -1;
Reaper reaper;
if (reaper.Run(submitter.getIoChannel())) {
submitter.Finish();
return -1;
}
int fd = open("testfile", O_RDWR | O_CREAT | O_DIRECT, 0644);
if (fd < 0) {
perror("open file");
return -1;
}
pthread_t tidp[NR_THREAD];
TestEnv TestEnv[NR_THREAD];
for (int i = 0; i < NR_THREAD; i++) {
TestEnv[i].index = i;
TestEnv[i].submitter = &submitter;
TestEnv[i].fd = fd;
if (pthread_create(&(tidp[i]), nullptr, SendIo, &(TestEnv[i]))) {
std::cerr << "failed to create thread " << i << ", "
<< strerror(errno);
ret = -1;
goto out;
}
}
out:
for (int i = 0; i < NR_THREAD; i++)
pthread_join(tidp[i], nullptr);
reaper.Finish();
submitter.Finish();
fsync(fd);
close(fd);
return ret;
}