-
Notifications
You must be signed in to change notification settings - Fork 1
/
io_demo.cpp
174 lines (146 loc) · 4.33 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include <iostream>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <cstdint>
#include <atomic>
#include "io_queue.h"
#define BS (4*1024)
#define NR_THREAD 1
#define IO_NUM 10000000
#define IO_DEPTH 256
std::atomic<uint32_t> nr_flying_io{0};
struct TestEnv {
int index;
Submitter *submitter;
int fd;
};
void *iobuf=nullptr;
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 = cb0;
task->res = -1;
Submitter *submitter = (Submitter *)task->arg;
submitter->Push(task);
}
void countdown(IoTask *task) {
--nr_flying_io;
delete task;
}
void *SendIo(void *arg) {
TestEnv *env = (TestEnv *)arg;
int idx = env->index;
//std::cout << "thread " << env->index << ": read testfile" << std::endl;
srand(time(NULL));
int i = 0;
for (i=0; i < IO_NUM; ++i) {
while (nr_flying_io >= IO_DEPTH)
usleep(100);
++nr_flying_io;
off_t offset = ((off_t)rand() >> 7) << 12 ; // max 67G
//off_t offset = rand();
//std::cout << "rand=" << offset << std::endl;
//offset = offset >> 7;
//std::cout << "reduce=" << offset << std::endl;
//offset = offset << 12;
//std::cout << "offset=" << offset << std::endl;
queueIo(env, offset, BS, false, iobuf, countdown, nullptr);
}
//sleep(3);
return nullptr;
}
int main(int argc, const char* argv[]) {
int ret = 0;
#ifdef ENABLE_URING
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;
}
#else
IoEngine engine = IoEngine::IO_ENGINE_LIBAIO;
#endif
Submitter submitter(engine, IO_DEPTH);
if (submitter.Run())
return -1;
Reaper reaper;
if (reaper.Run(submitter.getIoChannel())) {
submitter.Finish();
return -1;
}
int fd = open("mnt/testfile", O_RDWR | O_CREAT | O_DIRECT, 0644);
if (fd < 0) {
perror("open file");
return -1;
}
if (posix_memalign(&iobuf, getpagesize(), BS)) {
std::cerr << "failed to alloc memory" << std::endl;
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;
}