-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathping_when_any.cpp
More file actions
162 lines (149 loc) · 5.95 KB
/
ping_when_any.cpp
File metadata and controls
162 lines (149 loc) · 5.95 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
#include <arpa/inet.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
#include <iostream>
#include <ranges>
#include <format>
#include <cassert>
#include <exec/when_any.hpp>
#include <exec/repeat_effect_until.hpp>
#include "uring_exec.hpp"
using uring_exec::io_uring_exec;
constexpr auto noop = [](auto &&...) {};
stdexec::sender
auto ping(io_uring_exec::scheduler scheduler,
int client_fd, int blocksize) {
return
stdexec::just(std::vector<char>(blocksize, 'x'), size_t{}, size_t{})
| stdexec::let_value([=](auto &buf, auto &r, auto &w) {
return
stdexec::just()
| stdexec::let_value([=, &buf] {
return uring_exec::async_write(scheduler, client_fd, buf.data(), buf.size());
})
| stdexec::let_value([=, &buf, &w](int written_bytes) {
w += written_bytes;
return uring_exec::async_read(scheduler, client_fd, buf.data(), written_bytes);
})
| stdexec::let_value([&r, &w](int read_bytes) {
r += read_bytes;
return stdexec::just(r, w);
})
| stdexec::let_error([&](auto &&) {
return stdexec::just(r, w);
})
| stdexec::let_stopped([&] {
return stdexec::just(r, w);
});
});
}
stdexec::sender
auto client(io_uring_exec::scheduler scheduler,
auto endpoint, int blocksize,
std::atomic<size_t> &r, std::atomic<size_t> &w) {
auto make_addr = [](auto endpoint) {
auto [host, port] = endpoint;
sockaddr_in addr_in {};
addr_in.sin_family = AF_INET;
inet_pton(AF_INET, host, &addr_in.sin_addr);
addr_in.sin_port = htons(port);
auto addr = std::bit_cast<sockaddr>(addr_in);
return addr;
};
return
stdexec::just()
| stdexec::let_value([=] {
return uring_exec::async_socket(scheduler, AF_INET, SOCK_STREAM, IPPROTO_TCP, 0);
})
| stdexec::let_value([=, addr = make_addr(endpoint)](int client_fd) {
return uring_exec::async_connect(scheduler, client_fd, &addr, sizeof addr)
| stdexec::let_value([=](auto&&) {
return stdexec::just(client_fd, size_t{}, size_t{});
});
})
| stdexec::let_value([=, &r, &w](int client_fd, size_t &client_read, size_t &client_written) {
auto collect = [&, client_fd](auto &&...) {
constexpr auto mo = std::memory_order::relaxed;
r.fetch_add(client_read, mo);
w.fetch_add(client_written, mo);
return stdexec::just(client_fd);
};
return
ping(scheduler, client_fd, blocksize)
| stdexec::let_value([&](size_t read_bytes, size_t written_bytes) {
client_read += read_bytes;
client_written += written_bytes;
return stdexec::just(false);
})
| exec::repeat_effect_until()
| stdexec::let_value(collect)
| stdexec::let_stopped(collect)
| stdexec::let_error(collect);
})
| stdexec::let_value([=](int client_fd) {
return uring_exec::async_close(scheduler, client_fd);
})
| stdexec::upon_error(noop)
| stdexec::upon_stopped(noop)
| stdexec::then(noop);
}
int main(int argc, char *argv[]) {
if(argc <= 5) {
auto message = std::format(
"usage: {} <port> <threads> <blocksize> <sessions> <timeout>", argv[0]);
std::cerr << message << std::endl;
return -1;
}
auto host = "127.0.0.1";
auto atoies = [&](auto ...idxes) { return std::tuple{atoi(argv[idxes])...}; };
auto [port, threads, blocksize, sessions, timeout] = atoies(1, 2, 3, 4, 5);
assert(timeout >= 1);
auto sb = uring_exec::signal_blocker<uring_exec::sigmask_exclusive>(SIGINT);
io_uring_exec uring({.uring_entries=512});
std::vector<std::jthread> thread_pool(threads);
for(auto &&j : thread_pool) {
j = std::jthread([&](auto stop_token) { uring.run(stop_token); });
}
exec::async_scope scope;
stdexec::scheduler auto scheduler = uring.get_scheduler();
auto endpoint = std::tuple(host, port);
std::atomic<size_t> r {};
std::atomic<size_t> w {};
for(auto n = sessions; n--;) {
stdexec::sender auto s =
stdexec::starts_on(scheduler, client(scheduler, endpoint, blocksize, r, w));
scope.spawn(std::move(s));
}
stdexec::sender auto deadline =
stdexec::schedule(scheduler)
| stdexec::let_value([=] {
return uring_exec::async_wait(scheduler, std::chrono::seconds(timeout));
})
| stdexec::then([&](auto&&) { scope.request_stop(); });
stdexec::sender auto timed_execution = exec::when_any(std::move(deadline), scope.on_empty());
stdexec::sync_wait(std::move(timed_execution));
double read_bytes = r.load();
double written_bytes = w.load();
auto stringify = [](double bytes, int timeout) {
bytes /= timeout;
auto conv = [&, base = 1024] {
for(auto i = 0; i < 6; i++) {
if(bytes < base) return std::tuple(bytes, "BKMGTP"[i]);
bytes /= base;
}
return std::tuple(bytes, 'E');
};
auto [good_bytes, unit] = conv();
auto suffix = ('B'==unit ? "" : "iB");
return std::format("{:.3f} {}{}", good_bytes, unit, suffix);
};
auto println = [](auto ...args) {
(std::cout << ... << args) << std::endl;
};
println("done.");
println("read: ", stringify(read_bytes, 1));
println("write: ", stringify(written_bytes, 1));
println("throughput (read): ", stringify(read_bytes, timeout), "/s");
println("throughput (write): ", stringify(written_bytes, timeout), "/s");
}