Skip to content

Instantly share code, notes, and snippets.

@royguo
Last active March 21, 2024 09:50
Show Gist options
  • Save royguo/f22d83063f3f379027fa48afac370208 to your computer and use it in GitHub Desktop.
Save royguo/f22d83063f3f379027fa48afac370208 to your computer and use it in GitHub Desktop.
#include "io_engine.h"
namespace demo {
Status PosixAIOEngine::AsyncWrite(int fd, uint64_t offset, const char* buffer, uint64_t size,
char** cb) {
// TODO Check io_depth
requests_.push_back({.aio_req_ = {}, .type_ = kAsyncWrite});
auto& request = requests_.back();
memset(&request.aio_req_, 0, sizeof(struct aiocb));
request.aio_req_.aio_offset = (off_t)offset;
request.aio_req_.aio_buf = (void*)buffer;
request.aio_req_.aio_nbytes = size;
request.aio_req_.aio_fildes = fd;
int ret = aio_write(&request.aio_req_);
if (ret == -1) {
auto msg = "aio_write failed, err msg: " + std::string(strerror(errno));
LOG(ERROR, msg);
return Status::IOError(msg);
}
return Status::OK();
}
Status PosixAIOEngine::AsyncRead(int fd, uint64_t offset, char* buffer, uint64_t size, char** cb) {
requests_.push_back({.aio_req_ = {}, .type_ = kAsyncRead});
auto& request = requests_.back();
memset(&request.aio_req_, 0, sizeof(struct aiocb));
request.aio_req_.aio_offset = (off_t)offset;
request.aio_req_.aio_buf = (void*)buffer;
request.aio_req_.aio_nbytes = size;
request.aio_req_.aio_fildes = fd;
int ret = aio_read(&request.aio_req_);
if (ret == -1) {
LOG(ERROR, "aio_read failed, err: {}, offset: {}, sz: {}", strerror(errno), offset, size);
requests_.pop_back();
return Status::IOError("aio read failed!");
}
return Status::OK();
}
// TODO Probably we should check the event by their submission order?
uint32_t PosixAIOEngine::Poll() {
uint32_t cnt = 0;
auto it = requests_.begin();
while (it != requests_.end()) {
auto& req = *it;
int ret = aio_error(&req.aio_req_);
if (ret == 0) {
LOG(INFO, IORequest::GetTypeName(req.type_) +
", callback offset: " + std::to_string(req.aio_req_.aio_offset));
uint64_t finish_code = aio_return(&req.aio_req_);
if (finish_code != 0) {
LOG(ERROR, "return code error : {}, offset: {}", strerror(errno), req.aio_req_.aio_offset);
}
it = requests_.erase(it);
cnt++;
} else {
++it;
}
}
return cnt;
}
}
@royguo
Copy link
Author

royguo commented Mar 21, 2024

Error one line 56:

return code error : Inappropriate ioctl for device, offset: 20480

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment