forked from deepseek-ai/3FS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReadBench.cc
More file actions
213 lines (197 loc) · 7.85 KB
/
Copy pathReadBench.cc
File metadata and controls
213 lines (197 loc) · 7.85 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
#include "client/cli/admin/ReadBench.h"
#include <boost/iostreams/device/array.hpp>
#include <boost/iostreams/stream.hpp>
#include <folly/Random.h>
#include <folly/ScopeGuard.h>
#include <folly/executors/CPUThreadPoolExecutor.h>
#include <folly/executors/GlobalExecutor.h>
#include <folly/experimental/coro/BlockingWait.h>
#include <folly/experimental/coro/Collect.h>
#include <folly/experimental/coro/Invoke.h>
#include <fstream>
#include <sstream>
#include <thread>
#include "AdminEnv.h"
#include "client/cli/admin/FileWrapper.h"
#include "client/cli/admin/Layout.h"
#include "client/cli/common/Dispatcher.h"
#include "client/cli/common/Utils.h"
#include "client/storage/StorageClient.h"
#include "client/storage/TargetSelection.h"
#include "common/serde/Serde.h"
#include "common/utils/Duration.h"
#include "common/utils/MagicEnum.hpp"
#include "common/utils/Result.h"
#include "fbs/meta/Common.h"
#include "fbs/storage/Common.h"
namespace hf3fs::client::cli {
namespace {
auto getParser() {
argparse::ArgumentParser parser("read-bench");
parser.add_argument("path");
parser.add_argument("--threads").default_value(uint32_t{16}).scan<'u', uint32_t>();
parser.add_argument("--coroutines").default_value(uint32_t{1}).scan<'u', uint32_t>();
parser.add_argument("--seconds").default_value(uint32_t{60}).scan<'u', uint32_t>();
parser.add_argument("--write").default_value(false).implicit_value(true);
parser.add_argument("--bs");
parser.add_argument("--iodepth");
parser.add_argument("--mode");
return parser;
}
CoTryTask<Dispatcher::OutputTable> handle(IEnv &ienv,
const argparse::ArgumentParser &parser,
const Dispatcher::Args &args) {
auto &env = dynamic_cast<AdminEnv &>(ienv);
ENSURE_USAGE(args.empty());
Dispatcher::OutputTable table;
// 1. parse arguments.
Path path = parser.get<std::string>("path");
auto threads = parser.get<uint32_t>("--threads");
auto coroutines = parser.get<uint32_t>("--coroutines");
auto deadline = RelativeTime::now() + 1_s * parser.get<uint32_t>("--seconds");
auto modeStr = parser.present<std::string>("--mode").value_or("Default");
auto mode = magic_enum::enum_cast<storage::client::TargetSelectionMode>(modeStr);
auto isWrite = parser.get<bool>("--write");
if (!mode) {
co_return makeError(StorageClientCode::kRoutingError, "mode is invalid");
}
size_t blockSize = 4_KB;
if (auto p = parser.present<std::string>("--bs")) {
auto result = Size::from(*p);
CO_RETURN_AND_LOG_ON_ERROR(result);
blockSize = *result;
}
size_t iodepth = 1024;
if (auto p = parser.present<std::string>("--iodepth")) {
auto result = Size::from(*p);
CO_RETURN_AND_LOG_ON_ERROR(result);
iodepth = *result;
}
auto pool = std::make_unique<folly::CPUThreadPoolExecutor>(std::make_pair(threads, threads),
std::make_shared<folly::NamedThreadFactory>("Pool"));
// 2. open files.
std::string prev;
std::vector<FileWrapper> files;
while (true) {
auto res = co_await env.metaClientGetter()->list(env.userInfo, env.currentDirId, path, prev, 0, false);
CO_RETURN_AND_LOG_ON_ERROR(res);
const auto &lsp = res.value();
for (const auto &entry : lsp.entries) {
if (entry.isFile()) {
auto openResult = co_await FileWrapper::openOrCreateFile(env, path / entry.name);
CO_RETURN_AND_LOG_ON_ERROR(openResult);
files.push_back(std::move(*openResult));
}
}
if (lsp.more) {
prev = lsp.entries.at(lsp.entries.size() - 1).name;
} else {
break;
}
}
// 3. read.
auto storageClient = env.storageClientGetter();
std::vector<CoTryTask<Void>> total;
std::atomic<size_t> readBytes{};
total.reserve(coroutines);
co_await env.mgmtdClientGetter()->refreshRoutingInfo(true);
auto rdmabufPool = net::RDMABufPool::create(64_MB, 512);
for (auto i = 0u; i < coroutines; ++i) {
total.push_back(folly::coro::co_invoke([&]() -> CoTryTask<Void> {
std::ofstream out("/dev/null");
std::vector<storage::client::ReadIO> readIOs;
std::vector<storage::client::WriteIO> writeIOs;
std::vector<storage::client::IOBuffer> buffers;
net::RDMABuf current;
storage::client::ReadOptions readOptions;
storage::client::WriteOptions writeOptions;
readOptions.targetSelection().set_mode(*mode);
readIOs.reserve(1024);
writeIOs.reserve(1024);
buffers.reserve(1024);
while (RelativeTime::now() <= deadline) {
readIOs.clear();
writeIOs.clear();
buffers.clear();
current = {};
auto routingInfo = env.mgmtdClientGetter()->getRoutingInfo()->raw();
if (UNLIKELY(routingInfo == nullptr)) {
co_return makeError(StorageClientCode::kRoutingError, "routing info is null");
}
for (auto i = 0u; i < iodepth; ++i) {
auto &file = files[folly::Random::rand32() % files.size()];
if (UNLIKELY(file.length() == 0)) {
continue;
}
auto chunkSize = file.file().layout.chunkSize;
auto offset = folly::Random::rand32() * blockSize % file.length();
auto chainResult = file.file().getChainId(file.inode(), offset, *routingInfo, 0);
CO_RETURN_AND_LOG_ON_ERROR(chainResult);
auto chunk =
file.file().getChunkId(file.inode().id, offset).then([](auto chunk) { return storage::ChunkId(chunk); });
CO_RETURN_AND_LOG_ON_ERROR(chunk);
auto l = std::min(file.file().length - offset, blockSize);
if (current.size() < l) {
current = co_await rdmabufPool->allocate();
if (UNLIKELY(!current)) {
XLOGF(FATAL, "allocate buffer failed");
}
buffers.emplace_back(current);
}
if (isWrite) {
writeIOs.push_back(storageClient->createWriteIO(*chainResult,
*chunk,
offset % chunkSize,
l,
chunkSize,
current.ptr(),
&buffers.back()));
} else {
readIOs.push_back(
storageClient
->createReadIO(*chainResult, *chunk, offset % chunkSize, l, current.ptr(), &buffers.back()));
}
current.advance(l);
}
if (isWrite) {
auto writeResult = co_await storageClient->batchWrite(writeIOs, env.userInfo, writeOptions);
CO_RETURN_AND_LOG_ON_ERROR(writeResult);
} else {
auto readResult = co_await storageClient->batchRead(readIOs, env.userInfo, readOptions);
CO_RETURN_AND_LOG_ON_ERROR(readResult);
}
size_t succBytes = 0;
for (auto &io : readIOs) {
if (LIKELY(bool(io.result.lengthInfo))) {
succBytes += *io.result.lengthInfo;
}
}
for (auto &io : writeIOs) {
if (LIKELY(bool(io.result.lengthInfo))) {
succBytes += *io.result.lengthInfo;
}
}
readBytes += succBytes;
}
co_return Void{};
}));
}
std::atomic<bool> stop = false;
std::jthread t([&] {
while (!stop) {
std::this_thread::sleep_for(1_s);
XLOGF(WARNING, "{}/s", Size::around(readBytes.exchange(0)));
}
});
auto results = co_await folly::coro::collectAllRange(std::move(total)).scheduleOn(pool.get());
for (auto result : results) {
CO_RETURN_AND_LOG_ON_ERROR(result);
}
stop = true;
co_return table;
}
} // namespace
CoTryTask<void> registerReadBenchHandler(Dispatcher &dispatcher) {
co_return co_await dispatcher.registerHandler(getParser, handle);
}
} // namespace hf3fs::client::cli