-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathfuse_io_context.h
More file actions
executable file
·529 lines (445 loc) · 14.6 KB
/
fuse_io_context.h
File metadata and controls
executable file
·529 lines (445 loc) · 14.6 KB
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
// SPDX-License-Identifier: LGPL-3.0-or-later
// Copyright (C) Martin Raiber
#pragma once
#include <coroutine>
#include <vector>
#include <liburing.h>
#include <utility>
#include <assert.h>
#include <iostream>
#include <unistd.h>
#include <memory>
#define DBG_PRINT(x)
/*
//for clang and libc++
namespace std
{
template<typename T = void>
using coroutine_handle = std::experimental::coroutine_handle<T>;
using suspend_never = std::experimental::suspend_never;
using suspend_always = std::experimental::suspend_always;
}
*/
static uint64_t handle_v(std::coroutine_handle<> p_awaiter)
{
return (uint64_t)*((uint64_t*)&p_awaiter);
}
struct fuse_io_context
{
template<typename T>
struct IoUringAwaiter
{
struct IoUringAwaiterGlobalRes
{
std::coroutine_handle<> awaiter;
uint8_t tocomplete;
};
struct IoUringAwaiterRes
{
IoUringAwaiterRes() noexcept
: res(-1) {}
int res;
IoUringAwaiterGlobalRes* gres;
};
IoUringAwaiter(std::vector<io_uring_sqe*> sqes) noexcept
{
awaiter_res.resize(sqes.size());
global_res.tocomplete = sqes.size();
for(size_t i=0;i<sqes.size();++i)
{
awaiter_res[i].gres = &global_res;
sqes[i]->user_data = reinterpret_cast<uint64_t>(&awaiter_res[i]);
}
}
IoUringAwaiter(IoUringAwaiter const&) = delete;
IoUringAwaiter(IoUringAwaiter&& other) = delete;
IoUringAwaiter& operator=(IoUringAwaiter&&) = delete;
IoUringAwaiter& operator=(IoUringAwaiter const&) = delete;
bool await_ready() const noexcept
{
return false;
}
void await_suspend(std::coroutine_handle<> p_awaiter) noexcept
{
DBG_PRINT(std::cout << "Await suspend io "<< handle_v(p_awaiter) << std::endl);
global_res.awaiter = p_awaiter;
}
template<typename U = T, std::enable_if_t<std::is_same<U, std::vector<io_uring_sqe*> >::value, int> = 0>
std::vector<int> await_resume() const noexcept
{
std::vector<int> res;
for(auto& sr: awaiter_res)
{
res.push_back(sr.res);
}
return res;
}
template<typename U = T, std::enable_if_t<std::is_same<U, io_uring_sqe*>::value, int> = 0>
int await_resume() const noexcept
{
return awaiter_res[0].res;
}
template<typename U = T, std::enable_if_t<std::is_same<U, std::pair<io_uring_sqe*, io_uring_sqe*> >::value, int> = 0>
std::pair<int, int> await_resume() const noexcept
{
return std::make_pair(awaiter_res[0].res, awaiter_res[1].res);
}
private:
IoUringAwaiterGlobalRes global_res;
std::vector<IoUringAwaiterRes> awaiter_res;
};
[[nodiscard]] auto complete(std::vector<io_uring_sqe*> sqes)
{
return IoUringAwaiter<std::vector<io_uring_sqe*> >(sqes);
}
[[nodiscard]] auto complete(io_uring_sqe* sqe)
{
return IoUringAwaiter<io_uring_sqe*>({sqe});
}
[[nodiscard]] auto complete(std::pair<io_uring_sqe*, io_uring_sqe*> sqes)
{
return IoUringAwaiter<std::pair<io_uring_sqe*, io_uring_sqe*> >({sqes.first, sqes.second});
}
io_uring_sqe* get_sqe(unsigned int peek=1) noexcept
{
if(peek>1)
{
while(true)
{
struct io_uring_sq *sq = &fuse_ring.ring->sq;
unsigned int head = io_uring_smp_load_acquire(sq->khead);
unsigned int tail = sq->sqe_tail;
struct io_uring_sqe *__sqe = NULL;
if (tail - head < *sq->kring_entries &&
*sq->kring_entries - (tail - head) >= peek)
{
io_uring_sqe* sqe = &sq->sqes[sq->sqe_tail & *sq->kring_mask];
sq->sqe_tail = tail + 1;
return sqe;
}
int rc = io_uring_submit(fuse_ring.ring);
if(rc<0 && errno!=EBUSY)
{
perror("io_uring_submit failed in get_sqe");
return nullptr;
}
else if(rc<0)
{
std::cout << "io_uring_submit: EBUSY" << std::endl;
sleep(0);
}
}
}
fuse_ring.ring_submit=true;
auto ret = io_uring_get_sqe(fuse_ring.ring);
if(ret==nullptr)
{
/* Needs newer Linux 5.10
int rc = io_uring_sqring_wait(fuse_ring.ring);
if(rc<0)
{
return nullptr;
}
else if(rc==0)
{*/
int rc = io_uring_submit(fuse_ring.ring);
if(rc<0)
{
perror("io_uring_submit failed in get_sqe");
return nullptr;
}
do
{
ret = io_uring_get_sqe(fuse_ring.ring);
} while (ret==nullptr);
//}
}
return ret;
}
struct MallocItem
{
MallocItem* next;
};
thread_local static MallocItem* malloc_cache_head;
static constexpr size_t malloc_cache_item_size = 500;
static void clear_malloc_cache()
{
while(malloc_cache_head)
{
MallocItem* next = malloc_cache_head->next;
delete []reinterpret_cast<char*>(malloc_cache_head);
malloc_cache_head = next;
}
}
static void* malloc_cache()
{
if(malloc_cache_head!=nullptr)
{
MallocItem* ret = malloc_cache_head;
malloc_cache_head = malloc_cache_head->next;
return ret;
}
return new char[malloc_cache_item_size];
}
static void malloc_cache_free(void* data)
{
MallocItem* mi = reinterpret_cast<MallocItem*>(data);
mi->next = malloc_cache_head;
malloc_cache_head = mi;
}
template<typename T>
struct io_uring_promise_type_base
{
using promise_type = io_uring_promise_type_base<T>;
using handle = std::coroutine_handle<promise_type>;
enum class e_res_state
{
Init,
Detached,
Res
};
io_uring_promise_type_base()
: res_state(e_res_state::Init) {}
auto initial_suspend() {
return std::suspend_never{};
}
auto final_suspend() noexcept {
struct final_awaiter : std::suspend_always
{
final_awaiter(promise_type* promise)
:promise(promise) {}
void await_suspend(std::coroutine_handle<> p_awaiter) const noexcept
{
if(promise->res_state==e_res_state::Detached)
{
DBG_PRINT(std::cout << "promise final detached" << std::endl);
if(promise->awaiter)
promise->awaiter.destroy();
handle::from_promise(*promise).destroy();
}
else if(promise->awaiter)
{
DBG_PRINT(std::cout << "promise final await resume" << std::endl);
promise->awaiter.resume();
}
else
{
DBG_PRINT(std::cout << "promise final no awaiter" << std::endl);
}
}
private:
promise_type* promise;
};
return final_awaiter(this);
}
void unhandled_exception()
{
abort();
}
void* operator new(std::size_t count)
{
if(count <= malloc_cache_item_size)
{
return malloc_cache();
}
return ::new char[count];
}
void operator delete(void* ptr, std::size_t sz) noexcept
{
if(sz <= malloc_cache_item_size)
{
malloc_cache_free(ptr);
return;
}
::delete (sz, reinterpret_cast<char*>(ptr));
}
std::coroutine_handle<> awaiter;
e_res_state res_state;
};
template<typename T>
struct io_uring_promise_type : io_uring_promise_type_base<T>
{
using handle = std::coroutine_handle<io_uring_promise_type<T> >;
T res;
auto get_return_object()
{
return io_uring_task<T>{handle::from_promise(*this)};
}
void return_value(T v)
{
if(io_uring_promise_type_base<T>::res_state!=io_uring_promise_type_base<T>::e_res_state::Detached)
{
io_uring_promise_type_base<T>::res_state = io_uring_promise_type_base<T>::e_res_state::Res;
res = std::move(v);
}
}
};
template<typename T>
struct [[nodiscard]] io_uring_task
{
using promise_type = io_uring_promise_type<T>;
using handle = std::coroutine_handle<promise_type>;
io_uring_task(io_uring_task const&) = delete;
io_uring_task(io_uring_task&& other) noexcept
: coro_h(std::exchange(other.coro_h, {}))
{
}
io_uring_task& operator=(io_uring_task&&) = delete;
io_uring_task& operator=(io_uring_task const&) = delete;
io_uring_task(handle h) noexcept
: coro_h(h)
{
}
~io_uring_task() noexcept
{
if(coro_h)
{
if(!coro_h.done())
{
DBG_PRINT(std::cout << "Detach" << std::endl);
coro_h.promise().res_state = promise_type::e_res_state::Detached;
}
else
{
DBG_PRINT(std::cout << "Destroy" << std::endl);
coro_h.destroy();
}
}
}
bool has_res() const noexcept
{
return coro_h.promise().res_state == promise_type::e_res_state::Res;
}
bool await_ready() const noexcept
{
bool r = has_res();
if(r)
{
DBG_PRINT(std::cout << "Await is ready" << std::endl);
}
else
{
DBG_PRINT(std::cout << "Await is not ready" << std::endl);
}
return r;
}
template<typename U>
void await_suspend(std::coroutine_handle<io_uring_promise_type<U> > p_awaiter) noexcept
{
DBG_PRINT(std::cout << "Await task " << (uint64_t)this << " suspend "<< handle_v(p_awaiter) << " prev " << handle_v(coro_h.promise().awaiter) << std::endl);
coro_h.promise().awaiter = p_awaiter;
}
template<typename U = T, std::enable_if_t<std::is_same<U, void>::value, int> = 0>
void await_resume() const noexcept
{
assert(has_res());
}
template<typename U = T, std::enable_if_t<!std::is_same<U, void>::value, int> = 0>
T await_resume() const noexcept
{
assert(has_res());
return std::move(coro_h.promise().res);
}
protected:
handle coro_h;
};
struct FuseIo
{
int fuse_fd;
int pipe[2];
char* header_buf;
size_t header_buf_idx;
char* scratch_buf;
size_t scratch_buf_idx;
};
struct FuseIoVal
{
FuseIoVal(fuse_io_context& io_service,
std::unique_ptr<FuseIo> fuse_io)
: io_service(io_service),
fuse_io(std::move(fuse_io))
{
}
~FuseIoVal()
{
io_service.release_fuse_io(std::move(fuse_io));
}
FuseIo& get() const noexcept
{
return *fuse_io.get();
}
FuseIo* operator->() const noexcept
{
return fuse_io.get();
}
private:
fuse_io_context& io_service;
std::unique_ptr<FuseIo> fuse_io;
};
struct FuseRing
{
FuseRing()
: ring(nullptr), ring_submit(false),
max_bufsize(1*1024*1024), backing_fd(-1),
backing_fd_orig(-1), backing_f_size(0)
{}
FuseRing(FuseRing&&) = default;
FuseRing(FuseRing const&) = delete;
FuseRing& operator=(FuseRing&&) = delete;
FuseRing& operator=(FuseRing const&) = delete;
std::vector<std::unique_ptr<FuseIo> > ios;
struct io_uring* ring;
bool ring_submit;
size_t max_bufsize;
int backing_fd;
int backing_fd_orig;
uint64_t backing_f_size;
};
FuseRing fuse_ring;
fuse_io_context(FuseRing fuse_ring);
fuse_io_context(fuse_io_context const&) = delete;
fuse_io_context(fuse_io_context&& other) = delete;
fuse_io_context& operator=(fuse_io_context&&) = delete;
fuse_io_context& operator=(fuse_io_context const&) = delete;
typedef fuse_io_context::io_uring_task<int> (*queue_fuse_read_t)(fuse_io_context& io);
int run(queue_fuse_read_t queue_read);
FuseIoVal get_fuse_io()
{
std::unique_ptr<FuseIo> fuse_io = std::move(fuse_ring.ios.back());
fuse_ring.ios.pop_back();
return FuseIoVal(*this, std::move(fuse_io));
}
void release_fuse_io(std::unique_ptr<FuseIo> fuse_io)
{
fuse_ring.ios.push_back(std::move(fuse_io));
}
private:
template<typename T>
struct io_uring_task_discard : io_uring_task<T>
{
io_uring_task_discard(io_uring_task<T>&& other) noexcept
: io_uring_task<T>(std::move(other))
{
}
};
fuse_io_context::io_uring_task_discard<int> queue_read_set_rc(queue_fuse_read_t queue_read);
int fuseuring_handle_cqe(struct io_uring_cqe *cqe);
int fuseuring_submit(bool block);
int last_rc;
};
template<>
struct fuse_io_context::io_uring_promise_type<void> : fuse_io_context::io_uring_promise_type_base<void>
{
using handle = std::coroutine_handle<fuse_io_context::io_uring_promise_type<void> >;
auto get_return_object()
{
return io_uring_task<void>{handle::from_promise(*this)};
}
void return_void()
{
if(fuse_io_context::io_uring_promise_type_base<void>::res_state!=fuse_io_context::io_uring_promise_type_base<void>::e_res_state::Detached)
{
fuse_io_context::io_uring_promise_type_base<void>::res_state = fuse_io_context::io_uring_promise_type_base<void>::e_res_state::Res;
}
}
};