forked from scylladb/seastar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfsqual.cc
104 lines (98 loc) · 3.25 KB
/
fsqual.cc
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
/*
* Copyright 2016 ScyllaDB
*/
/*
* This file is open source software, licensed to you under the terms
* of the Apache License, Version 2.0 (the "License"). See the NOTICE file
* distributed with this work for additional information regarding copyright
* ownership. You may not use this file except in compliance with the License.
*
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
#include <seastar/core/posix.hh>
#include <seastar/util/defer.hh>
#include <seastar/core/linux-aio.hh>
#include <sys/time.h>
#include <sys/resource.h>
#include <fcntl.h>
#include <iostream>
#include <unistd.h>
#include <cstdlib>
#include <type_traits>
#include <seastar/core/fsqual.hh>
namespace seastar {
using namespace seastar::internal;
using namespace seastar::internal::linux_abi;
// Runs func(), and also adds the number of context switches
// that happened during func() to counter.
template <typename Counter, typename Func>
typename std::invoke_result_t<Func>
with_ctxsw_counting(Counter& counter, Func&& func) {
struct count_guard {
Counter& counter;
count_guard(Counter& counter) : counter(counter) {
counter -= nvcsw();
}
~count_guard() {
counter += nvcsw();
}
static Counter nvcsw() {
struct rusage usage;
getrusage(RUSAGE_THREAD, &usage);
return usage.ru_nvcsw;
}
};
count_guard g(counter);
return func();
}
bool filesystem_has_good_aio_support(sstring directory, bool verbose) {
aio_context_t ioctx = {};
auto r = io_setup(1, &ioctx);
throw_system_error_on(r == -1, "io_setup");
auto cleanup = defer([&] () noexcept { io_destroy(ioctx); });
auto fname = directory + "/fsqual.tmp";
auto fd = file_desc::open(fname, O_CREAT|O_EXCL|O_RDWR|O_DIRECT, 0600);
unlink(fname.c_str());
auto nr = 1000;
fd.truncate(nr * 4096);
auto bufsize = 4096;
auto ctxsw = 0;
auto buf = aligned_alloc(4096, 4096);
for (int i = 0; i < nr; ++i) {
struct iocb cmd;
cmd = make_write_iocb(fd.get(), bufsize*i, buf, bufsize);
struct iocb* cmds[1] = { &cmd };
with_ctxsw_counting(ctxsw, [&] {
auto r = io_submit(ioctx, 1, cmds);
throw_system_error_on(r == -1, "io_submit");
assert(r == 1);
});
struct io_event ioev;
int n = -1;
do {
n = io_getevents(ioctx, 1, 1, &ioev, nullptr);
throw_system_error_on((n == -1) && (errno != EINTR) , "io_getevents");
} while (n == -1);
assert(n == 1);
throw_kernel_error(long(ioev.res));
assert(long(ioev.res) == bufsize);
}
auto rate = float(ctxsw) / nr;
bool ok = rate < 0.1;
if (verbose) {
auto verdict = ok ? "GOOD" : "BAD";
std::cout << "context switch per appending io: " << rate
<< " (" << verdict << ")\n";
}
return ok;
}
}