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
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
/*
* Ceph - scalable distributed file system
*
* Copyright (C) 2018 Red Hat, Inc.
*
* This is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1, as published by the Free Software
* Foundation. See file COPYING.
*
*/
#include "include/rados/librados.hpp"
#include "rgw_aio_throttle.h"
#include "rgw_rados.h"
namespace rgw {
void AioThrottle::aio_cb(void *cb, void *arg)
{
Pending& p = *static_cast<Pending*>(arg);
p.result = p.completion->get_return_value();
p.parent->put(p);
}
bool AioThrottle::waiter_ready() const
{
switch (waiter) {
case Wait::Available: return is_available();
case Wait::Completion: return has_completion();
case Wait::Drained: return is_drained();
default: return false;
}
}
AioResultList AioThrottle::submit(RGWSI_RADOS::Obj& obj,
librados::ObjectWriteOperation *op,
uint64_t cost, uint64_t id)
{
auto p = std::make_unique<Pending>();
p->obj = obj;
p->id = id;
p->cost = cost;
if (cost > window) {
p->result = -EDEADLK; // would never succeed
std::unique_lock lock{mutex};
completed.push_back(*p);
} else {
get(*p);
p->result = obj.aio_operate(p->completion, op);
if (p->result < 0) {
put(*p);
}
}
p.release();
std::unique_lock lock{mutex};
return std::move(completed);
}
AioResultList AioThrottle::submit(RGWSI_RADOS::Obj& obj,
librados::ObjectReadOperation *op,
uint64_t cost, uint64_t id)
{
auto p = std::make_unique<Pending>();
p->obj = obj;
p->id = id;
p->cost = cost;
if (cost > window) {
p->result = -EDEADLK; // would never succeed
std::unique_lock lock{mutex};
completed.push_back(*p);
} else {
get(*p);
p->result = obj.aio_operate(p->completion, op, &p->data);
if (p->result < 0) {
put(*p);
}
}
p.release();
std::unique_lock lock{mutex};
return std::move(completed);
}
void AioThrottle::get(Pending& p)
{
std::unique_lock lock{mutex};
// wait for the write size to become available
pending_size += p.cost;
if (!is_available()) {
ceph_assert(waiter == Wait::None);
waiter = Wait::Available;
cond.wait(lock, [this] { return is_available(); });
waiter = Wait::None;
}
// register the pending write and attach a completion
p.parent = this;
p.completion = librados::Rados::aio_create_completion(&p, nullptr, aio_cb);
pending.push_back(p);
}
void AioThrottle::put(Pending& p)
{
p.completion->release();
p.completion = nullptr;
std::scoped_lock lock{mutex};
// move from pending to completed
pending.erase(pending.iterator_to(p));
completed.push_back(p);
pending_size -= p.cost;
if (waiter_ready()) {
cond.notify_one();
}
}
AioResultList AioThrottle::poll()
{
std::unique_lock lock{mutex};
return std::move(completed);
}
AioResultList AioThrottle::wait()
{
std::unique_lock lock{mutex};
if (completed.empty() && !pending.empty()) {
ceph_assert(waiter == Wait::None);
waiter = Wait::Completion;
cond.wait(lock, [this] { return has_completion(); });
waiter = Wait::None;
}
return std::move(completed);
}
AioResultList AioThrottle::drain()
{
std::unique_lock lock{mutex};
if (!pending.empty()) {
ceph_assert(waiter == Wait::None);
waiter = Wait::Drained;
cond.wait(lock, [this] { return is_drained(); });
waiter = Wait::None;
}
return std::move(completed);
}
} // namespace rgw
|