forked from google/distbench
-
Notifications
You must be signed in to change notification settings - Fork 0
/
distbench_utils.cc
354 lines (304 loc) · 11.9 KB
/
distbench_utils.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
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
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://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 "distbench_utils.h"
#include "interface_lookup.h"
#include "absl/strings/str_split.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/str_format.h"
#include "glog/logging.h"
namespace std {
ostream& operator<< (ostream &out, grpc::Status const& c)
{
return out << "(grpc::status" << c.error_message() << ")";
}
}
namespace distbench {
static bool use_ipv4_first = false;
void set_use_ipv4_first(bool _use_ipv4_first) {
use_ipv4_first = _use_ipv4_first;
}
std::string Hostname() {
char hostname[4096] = {};
if (gethostname(hostname, sizeof(hostname))) {
LOG(ERROR) << errno;
}
return hostname;
}
grpc::ChannelArguments DistbenchCustomChannelArguments() {
grpc::ChannelArguments args;
args.SetInt(GRPC_ARG_MAX_RECEIVE_MESSAGE_LENGTH,
std::numeric_limits<int32_t>::max());
return args;
}
std::shared_ptr<grpc::ChannelCredentials> MakeChannelCredentials() {
// grpc::SslCredentialsOptions sec_ops;
// return grpc::SslCredentials(sec_ops);
return grpc::InsecureChannelCredentials();
}
std::shared_ptr<grpc::ServerCredentials> MakeServerCredentials() {
// grpc::SslServerCredentialsOptions sec_ops;
// return grpc::SslServerCredentials(sec_ops);
return grpc::InsecureServerCredentials();
}
std::thread RunRegisteredThread(const std::string& thread_name,
std::function<void()> f) {
return std::thread([=]() {
f();
});
}
void InitLibs(const char* argv0) {
// Extra library initialization can go here
::google::InitGoogleLogging(argv0);
}
std::string IpAddressForDevice(std::string_view netdev) {
net_base::IPAddress ip;
if (use_ipv4_first) {
CHECK(net_base::InterfaceLookup::MyIPv4Address(&ip) ||
net_base::InterfaceLookup::MyIPv6Address(&ip));
} else {
CHECK(net_base::InterfaceLookup::MyIPv6Address(&ip) ||
net_base::InterfaceLookup::MyIPv4Address(&ip));
}
return ip.ToString();
}
std::string SocketAddressForDevice(std::string_view netdev, int port) {
net_base::IPAddress ip;
if (use_ipv4_first &&
net_base::InterfaceLookup::MyIPv4Address(&ip))
return absl::StrCat(ip.ToString(), ":", port);
if (net_base::InterfaceLookup::MyIPv6Address(&ip))
return absl::StrCat("[", ip.ToString(), "]:", port);
if (net_base::InterfaceLookup::MyIPv4Address(&ip))
return absl::StrCat(ip.ToString(), ":", port);
LOG(FATAL) << "Could not get ip v4/v6 address";
}
std::string ServiceInstanceName(std::string_view service_type, int instance) {
CHECK(!service_type.empty());
CHECK_GE(instance, 0);
return absl::StrCat(service_type, "/", instance);
}
std::map<std::string, int> EnumerateServiceTypes(
const DistributedSystemDescription& config) {
std::map<std::string, int> ret;
for (const auto& service : config.services()) {
// LOG(INFO) << "service " << service.name() << " = " << ret.size();
ret[service.name()] = ret.size();
}
return ret;
}
std::map<std::string, int> EnumerateServiceSizes(
const DistributedSystemDescription& config) {
std::map<std::string, int> ret;
for (const auto& service : config.services()) {
// LOG(INFO) << "service " << service.name() << " = " << ret.size();
ret[service.name()] = service.count();
}
return ret;
}
std::map<std::string, int> EnumerateRpcs(
const DistributedSystemDescription& config) {
std::map<std::string, int> ret;
for (const auto& rpc : config.rpc_descriptions()) {
ret[rpc.name()] = ret.size();
}
return ret;
}
std::map<std::string, int> EnumerateServiceInstanceIds(
const DistributedSystemDescription& config) {
std::map<std::string, int> ret;
for (const auto& service : config.services()) {
for (int i = 0; i < service.count(); ++i) {
std::string instance = ServiceInstanceName(service.name(), i);
// LOG(INFO) << "service " << instance << " = " << ret.size();
ret[instance] = ret.size();
}
}
return ret;
}
ServiceSpec GetServiceSpec(std::string_view name,
const DistributedSystemDescription& config) {
for (const auto& service : config.services()) {
if (service.name() == name) {
return service;
}
}
LOG(FATAL) << "Service not found: " << name;
}
namespace {
std::string LatencySummary(std::vector<int64_t> latencies) {
std::string ret;
CHECK(!latencies.empty());
size_t N = latencies.size();
absl::StrAppendFormat(&ret, "N: %ld", N);
absl::StrAppendFormat(&ret, " min: %ldns", *latencies.begin());
absl::StrAppendFormat(&ret, " median: %ldns", latencies[N * 0.5]);
absl::StrAppendFormat(&ret, " 90%%: %ldns", latencies[N * 0.9]);
absl::StrAppendFormat(&ret, " 99%%: %ldns", latencies[N * 0.99]);
absl::StrAppendFormat(&ret, " 99.9%%: %ldns", latencies[N * 0.999]);
absl::StrAppendFormat(&ret, " max: %ldns", *latencies.rbegin());
return ret;
}
struct rpc_traffic_summary {
int64_t nb_rpcs = 0;
int64_t request_size = 0;
int64_t response_size = 0;
};
typedef std::pair<std::string, std::string> t_string_pair;
struct instance_summary {
int64_t tx_payload_bytes = 0;
int64_t rx_payload_bytes = 0;
};
void AddCommunicationSummaryTo(std::vector<std::string> &ret,
double total_time_seconds,
std::map<t_string_pair, rpc_traffic_summary> perf_map) {
ret.push_back("Communication summary:");
constexpr double MiB = 1024 * 1024;
for (auto& perf_r : perf_map) {
std::string name = perf_r.first.first + " -> " + perf_r.first.second;
auto& perf = perf_r.second;
std::string str{};
absl::StrAppendFormat(&str,
" %s: RPCs: %d (%3.2f kQPS) "
"Request: %3.1f MiB/s Response: %3.1f MiB/s",
name, perf.nb_rpcs, (double)perf.nb_rpcs / total_time_seconds / 1000.,
(double)perf.request_size / MiB / total_time_seconds,
(double)perf.response_size / MiB / total_time_seconds);
ret.push_back(str);
}
}
void AddInstanceSummaryTo(std::vector<std::string> &ret,
double total_time_seconds,
std::map<t_string_pair, rpc_traffic_summary> perf_map) {
std::map<std::string, instance_summary> instance_summary_map;
int64_t total_rpcs = 0;
int64_t total_tx_bytes = 0;
for (auto& perf_r : perf_map) {
std::string initiator_name = perf_r.first.first;
std::string target_name = perf_r.first.second;
auto &perf = perf_r.second;
instance_summary inst_summary{};
total_rpcs += perf.nb_rpcs;
total_tx_bytes += perf.request_size + perf.response_size;
auto is = instance_summary_map.find(initiator_name);
if (is != instance_summary_map.end())
inst_summary = is->second;
inst_summary.tx_payload_bytes += perf.request_size;
inst_summary.rx_payload_bytes += perf.response_size;
instance_summary_map[initiator_name] = inst_summary;
inst_summary = instance_summary{};
auto ist = instance_summary_map.find(target_name);
if (ist != instance_summary_map.end())
inst_summary = ist->second;
inst_summary.tx_payload_bytes += perf.response_size;
inst_summary.rx_payload_bytes += perf.request_size;
instance_summary_map[target_name] = inst_summary;
}
ret.push_back("Instance summary:");
constexpr int64_t MiB = 1024 * 1024;
for (auto& instance_sum: instance_summary_map) {
instance_summary inst_summary = instance_sum.second;
std::string str{};
absl::StrAppendFormat(&str, " %s: Tx: %3.1f MiB/s, Rx:%3.1f MiB/s",
instance_sum.first,
(double)inst_summary.tx_payload_bytes / MiB / total_time_seconds,
(double)inst_summary.rx_payload_bytes / MiB / total_time_seconds);
ret.push_back(str);
}
std::string str{};
ret.push_back("Global summary:");
absl::StrAppendFormat(&str, " Total time: %3.3fs", total_time_seconds);
ret.push_back(str);
str = "";
absl::StrAppendFormat(&str, " Total Tx: %d MiB (%3.1f MiB/s), ",
total_tx_bytes / MiB,
(double)total_tx_bytes / MiB / total_time_seconds);
absl::StrAppendFormat(&str,
"Total Nb RPCs: %d (%3.2f kQPS)",
total_rpcs, (double)total_rpcs / 1000 / total_time_seconds);
ret.push_back(str);
}
} // anonymous namespace
std::vector<std::string> SummarizeTestResult(const TestResult& test_result) {
std::map<std::string, std::vector<int64_t>> latency_map;
std::map<t_string_pair, rpc_traffic_summary> perf_map;
int64_t test_time = 0;
for (const auto& instance_log : test_result.service_logs().instance_logs()) {
const std::string& initiator_instance_name = instance_log.first;
for (const auto& peer_log : instance_log.second.peer_logs()) {
const std::string& target_instance_name = peer_log.first;
int64_t start_timestamp_ns = std::numeric_limits<int64_t>::max();
int64_t end_timestamp_ns = std::numeric_limits<int64_t>::min();
rpc_traffic_summary perf_record{};
for (const auto& rpc_log : peer_log.second.rpc_logs()) {
std::string rpc_name =
test_result.traffic_config().rpc_descriptions(rpc_log.first).name();
std::vector<int64_t>& latencies = latency_map[rpc_name];
perf_record.nb_rpcs += rpc_log.second.successful_rpc_samples().size();
for (const auto& sample : rpc_log.second.successful_rpc_samples()) {
int64_t rpc_start_timestamp_ns = sample.start_timestamp_ns();
int64_t rpc_latency_ns = sample.latency_ns();
int64_t rpc_request_size = sample.request_size();
int64_t rpc_response_size = sample.response_size();
start_timestamp_ns = std::min(rpc_start_timestamp_ns,
start_timestamp_ns);
end_timestamp_ns = std::max(rpc_start_timestamp_ns + rpc_latency_ns,
end_timestamp_ns);
perf_record.request_size += rpc_request_size;
perf_record.response_size += rpc_response_size;
latencies.push_back(rpc_latency_ns);
}
}
if (start_timestamp_ns != std::numeric_limits<int64_t>::max())
test_time = std::max(test_time,
end_timestamp_ns - start_timestamp_ns);
t_string_pair key_traffic_sum =
std::make_pair(initiator_instance_name, target_instance_name);
perf_map[key_traffic_sum] = perf_record;
}
}
std::vector<std::string> ret;
ret.push_back("RPC latency summary:");
for (auto& latencies : latency_map) {
std::string str{};
std::sort(latencies.second.begin(), latencies.second.end());
absl::StrAppendFormat(
&str, " %s: %s", latencies.first, LatencySummary(latencies.second));
ret.push_back(str);
}
double total_time_seconds = (double)test_time / 1000 / 1000 / 1000;
AddCommunicationSummaryTo(ret, total_time_seconds, perf_map);
AddInstanceSummaryTo(ret, total_time_seconds, perf_map);
return ret;
}
grpc::Status Annotate(const grpc::Status& status, std::string_view context) {
return grpc::Status(
status.error_code(), absl::StrCat(context, status.error_message()));
}
grpc::Status abslStatusToGrpcStatus(const absl::Status &status) {
if (status.ok())
return grpc::Status::OK;
std::string message = std::string(status.message());
// GRPC and ABSL (currently) share the same error codes
grpc::StatusCode code = (grpc::StatusCode)status.code();
return grpc::Status(code, message);
}
absl::Status grpcStatusToAbslStatus(const grpc::Status &status) {
if (status.ok())
return absl::OkStatus();
std::string message = status.error_message();
// GRPC and ABSL (currently) share the same error codes
absl::StatusCode code = (absl::StatusCode)status.error_code();
return absl::Status(code, message);
}
} // namespace distbench