blob: a5fe76fe9073c2760c38aac13a31fd9c70d4a822 [file] [log] [blame]
Kelvin Zhang1bd2e5b2022-10-03 12:02:34 -07001//
2// Copyright (C) 2022 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
16
17#include <asm-generic/errno-base.h>
18#include <liburing_cpp/IoUring.h>
19#include <string.h>
20
21#include <algorithm>
22#include <iostream>
23#include <memory>
24
25#include "liburing.h"
26#include "liburing_cpp/IoUringCQE.h"
27
28namespace io_uring_cpp {
29
30template <typename T>
31bool IsZeroInitialized(const T& val) {
32 auto begin = reinterpret_cast<const char*>(&val);
33 auto end = begin + sizeof(val);
34 return std::all_of(begin, end, [](const auto& a) { return a == 0; });
35}
36
37class IoUring final : public IoUringInterface {
38 public:
39 ~IoUring() override {
40 if (!IsZeroInitialized(ring)) {
41 if (buffer_registered_) {
42 UnregisterBuffers();
43 }
44 if (files_registered_) {
45 UnregisterFiles();
46 }
47 io_uring_queue_exit(&ring);
48 }
49 }
50 IoUring(const IoUring&) = delete;
51 IoUring(IoUring&& rhs) {
52 ring = rhs.ring;
53 memset(&rhs.ring, 0, sizeof(rhs.ring));
54 }
55 IoUring& operator=(IoUring&& rhs) {
56 std::swap(ring, rhs.ring);
57 return *this;
58 }
59 Errno RegisterBuffers(const struct iovec* iovecs,
60 size_t iovec_size) override {
61 const auto ret =
62 Errno(io_uring_register_buffers(&ring, iovecs, iovec_size));
63 buffer_registered_ = ret.IsOk();
64 return ret;
65 }
66
67 Errno UnregisterBuffers() override {
68 const auto ret = Errno(io_uring_unregister_buffers(&ring));
69 buffer_registered_ = !ret.IsOk();
70 return ret;
71 }
72
73 Errno RegisterFiles(const int* files, size_t files_size) override {
74 const auto ret = Errno(io_uring_register_files(&ring, files, files_size));
75 files_registered_ = ret.IsOk();
76 return ret;
77 }
78
79 Errno UnregisterFiles() {
80 const auto ret = Errno(io_uring_unregister_files(&ring));
81 files_registered_ = !ret.IsOk();
82 return ret;
83 }
84
85 IoUringSQE PrepRead(int fd, void* buf, unsigned nbytes,
86 uint64_t offset) override {
87 auto sqe = io_uring_get_sqe(&ring);
88 if (sqe == nullptr) {
89 return IoUringSQE{nullptr};
90 }
91 io_uring_prep_read(sqe, fd, buf, nbytes, offset);
92 return IoUringSQE{static_cast<void*>(sqe)};
93 }
94 IoUringSQE PrepWrite(int fd, const void* buf, unsigned nbytes,
95 uint64_t offset) override {
96 auto sqe = io_uring_get_sqe(&ring);
97 if (sqe == nullptr) {
98 return IoUringSQE{nullptr};
99 }
100 io_uring_prep_write(sqe, fd, buf, nbytes, offset);
101 return IoUringSQE{static_cast<void*>(sqe)};
102 }
103 IoUringSubmitResult Submit() override {
104 return IoUringSubmitResult{io_uring_submit(&ring)};
105 }
106
107 IoUringSubmitResult SubmitAndWait(size_t completions) override {
108 return IoUringSubmitResult{io_uring_submit_and_wait(&ring, completions)};
109 }
110
111 Result<Errno, std::vector<IoUringCQE>> PopCQE(
112 const unsigned int count) override {
113 std::vector<io_uring_cqe*> cqe_ptrs;
114 cqe_ptrs.resize(count);
115 const auto ret = io_uring_wait_cqe_nr(&ring, cqe_ptrs.data(), count);
116 if (ret != 0) {
117 return {Errno(ret)};
118 }
119 const auto filled = io_uring_peek_batch_cqe(&ring, cqe_ptrs.data(), count);
120 if (filled != count) {
121 return {Errno(EAGAIN)};
122 }
123 std::vector<IoUringCQE> cqes;
124 cqes.reserve(count);
125 for (const auto& cqe : cqe_ptrs) {
126 if (cqe == nullptr) {
127 return {Errno(EAGAIN)};
128 }
129 cqes.push_back(IoUringCQE(cqe->res, cqe->flags, cqe->user_data));
130 io_uring_cqe_seen(&ring, cqe);
131 }
132 return {cqes};
133 }
134
135 Result<Errno, IoUringCQE> PopCQE() override {
136 struct io_uring_cqe* ptr{};
137 const auto ret = io_uring_wait_cqe(&ring, &ptr);
138 if (ret != 0) {
139 return {Errno(ret)};
140 }
141 const auto cqe = IoUringCQE(ptr->res, ptr->flags, ptr->user_data);
142 io_uring_cqe_seen(&ring, ptr);
143 return {cqe};
144 }
145
146 Result<Errno, IoUringCQE> PeekCQE() override {
147 struct io_uring_cqe* ptr{};
148 const auto ret = io_uring_peek_cqe(&ring, &ptr);
149 if (ret != 0) {
150 return {Errno(ret)};
151 }
152 return {IoUringCQE(ptr->res, ptr->flags, ptr->user_data)};
153 }
154
155 IoUring(struct io_uring r) : ring(r) {}
156
157 private:
158 struct io_uring ring {};
159 bool buffer_registered_ = false;
160 bool files_registered_ = false;
161 std::atomic<size_t> request_id_{};
162};
163
164const char* Errno::ErrMsg() {
165 if (error_code == 0) {
166 return nullptr;
167 }
168 return strerror(error_code);
169}
170
171std::ostream& operator<<(std::ostream& out, Errno err) {
172 out << err.ErrCode() << ", " << err.ErrMsg();
173 return out;
174}
175
176std::unique_ptr<IoUringInterface> IoUringInterface::CreateLinuxIoUring(
177 int queue_depth, int flags) {
178 struct io_uring ring {};
179 const auto err = io_uring_queue_init(queue_depth, &ring, flags);
180 if (err) {
181 errno = -err;
182 return {};
183 }
184 return std::unique_ptr<IoUringInterface>(new IoUring(ring));
185}
186
187} // namespace io_uring_cpp