Skip to content

Commit 2b80553

Browse files
author
qinyihust
committed
test OK
1 parent 85992f3 commit 2b80553

File tree

5 files changed

+57
-23
lines changed

5 files changed

+57
-23
lines changed

async_io.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ Libaio::~Libaio() {
5151

5252
int Libaio::SubmitIo(IoTask *task) {
5353
iocb *iocbp = new(iocb);
54-
memset(iocbp, 0, sizeof(iocb));
5554
if (task->isRead)
5655
io_prep_preadv(iocbp, task->fd, &(task->iov), 1, task->offset);
5756
else
@@ -75,7 +74,7 @@ IoTask *Libaio::ReapIo() {
7574
iocb *iocbp = event.obj;
7675
assert(task == iocbp->data);
7776
delete iocbp;
78-
task->res = event.res2; // res is r/w length, res2 is operation result
77+
task->res = event.res; // res: +x for done length, -x for error code
7978

8079
return task;
81-
}
80+
}

async_io.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
struct IoTask;
1313
typedef void (*IocbFunc)(IoTask *task);
1414
struct IoTask {
15+
int index; // thread index
1516
int fd;
1617
bool isRead;
1718
off_t first_offset, offset;
@@ -50,4 +51,4 @@ class Libaio: public AsyncIo {
5051
io_context_t ctx_;
5152
};
5253

53-
#endif // ASYNC_IO_H_
54+
#endif // ASYNC_IO_H_

build.sh

100644100755
File mode changed.

io_demo.cpp

+52-19
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
#define QD 64
99
#define BS (4*1024)
10-
#define NR_THREAD 2
10+
#define NR_THREAD 10
1111

1212
struct TestEnv {
1313
int index;
@@ -20,6 +20,7 @@ int queueIo(TestEnv *env, off_t offset, off_t len, bool isRead, void *buf, IocbF
2020
if (!task)
2121
return -1;
2222

23+
task->index = env->index;
2324
task->fd = env->fd;
2425

2526
task->isRead = isRead;
@@ -30,7 +31,7 @@ int queueIo(TestEnv *env, off_t offset, off_t len, bool isRead, void *buf, IocbF
3031
task->iov.iov_len = len;
3132

3233
task->cb = cb;
33-
task->arg = arg;
34+
task->arg = env->submitter;
3435
task->res = -1;
3536

3637
env->submitter->Push(task);
@@ -39,36 +40,66 @@ int queueIo(TestEnv *env, off_t offset, off_t len, bool isRead, void *buf, IocbF
3940
}
4041

4142
void cb1(IoTask *task) {
42-
assert(task->isRead);
43-
std::cout << "read data: " << (static_cast<char *>(task->iov.iov_base))[0]
44-
<< std::endl;
43+
assert (!task->isRead);
44+
unsigned num = ((char *)(task->iov.iov_base))[0];
45+
std::cout << "reaper: write for thread " << task->index <<" done, data="
46+
<< std::hex << num << ", res="<< task->res << std::endl;
4547

4648
delete task;
4749
}
4850

51+
52+
void cb0(IoTask *task) {
53+
assert (task->isRead);
54+
unsigned num = ((char *)(task->iov.iov_base))[0];
55+
std::cout << "reaper: read from thread " << task->index << " done, data="
56+
<< std::hex << num << ", res=" << task->res << std::endl;
57+
num++;
58+
std::cout << "reaper: send write I/O for thread " << task->index << ", data="
59+
<< std::hex << num << std::endl;
60+
// send write I/O
61+
task->isRead = false;
62+
memset(task->iov.iov_base, num, BS);
63+
task->cb = cb1;
64+
task->res = -1;
65+
Submitter *submitter = (Submitter *)task->arg;
66+
submitter->Push(task);
67+
}
68+
4969
void *SendIo(void *arg) {
5070
TestEnv *env = (TestEnv *)arg;
5171
int idx = env->index;
52-
int fd = env->fd;
53-
54-
std::string data(BS, 0);
55-
switch (idx) {
56-
case 0:
57-
data.assign(BS, '1');
58-
queueIo(env, 0, BS, false, (void *)(data.c_str()), nullptr, nullptr);
59-
case 1:
60-
sleep(10);
61-
queueIo(env, 0, BS, true, (void *)data.c_str(), cb1, nullptr);
62-
}
63-
sleep(20);
72+
char *data = nullptr;
73+
posix_memalign((void **)&data, getpagesize(), BS);
74+
memset(data, 0, BS);
75+
std::cout << "thread " << env->index << ": read testfile" << std::endl;
76+
queueIo(env, 0, BS, true, data, cb0, nullptr);
6477

78+
sleep(3);
6579
return nullptr;
6680
}
6781

68-
6982
int main(int argc, const char* argv[]) {
7083
int ret = 0;
71-
Submitter submitter(IoEngine::IO_ENGINE_URING, QD);
84+
85+
if (argc != 2) {
86+
std::cout << "usage: " << argv[0] << " [option]" << std::endl;
87+
std::cout << "option: libaio or uring" << std::endl;
88+
return -1;
89+
}
90+
91+
IoEngine engine = IoEngine::IO_ENGINE_NONE;
92+
if (!strncmp(argv[1], "libaio", 7))
93+
engine = IoEngine::IO_ENGINE_LIBAIO;
94+
else if (!strncmp(argv[1], "uring", 6))
95+
engine = IoEngine::IO_ENGINE_URING;
96+
else {
97+
std::cout << "usage: " << argv[0] << " [option]" << std::endl;
98+
std::cout << "option: libaio or uring" << std::endl;
99+
return -1;
100+
}
101+
102+
Submitter submitter(engine, QD);
72103
if (submitter.Run())
73104
return -1;
74105

@@ -103,6 +134,8 @@ int main(int argc, const char* argv[]) {
103134
pthread_join(tidp[i], nullptr);
104135
reaper.Finish();
105136
submitter.Finish();
137+
fsync(fd);
138+
close(fd);
106139

107140
return ret;
108141
}

io_queue.h

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
enum class IoEngine {
1515
IO_ENGINE_LIBAIO,
1616
IO_ENGINE_URING,
17+
IO_ENGINE_NONE,
1718
};
1819

1920
class Submitter {

0 commit comments

Comments
 (0)