| 1 |
/* |
| 2 |
* |
| 3 |
* Copyright 2015 gRPC authors. |
| 4 |
* |
| 5 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 |
* you may not use this file except in compliance with the License. |
| 7 |
* You may obtain a copy of the License at |
| 8 |
* |
| 9 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
* |
| 11 |
* Unless required by applicable law or agreed to in writing, software |
| 12 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
* See the License for the specific language governing permissions and |
| 15 |
* limitations under the License. |
| 16 |
* |
| 17 |
*/ |
| 18 |
|
| 19 |
/* |
| 20 |
Basic I/O ping-pong benchmarks. |
| 21 |
|
| 22 |
The goal here is to establish lower bounds on how fast the stack could get by |
| 23 |
measuring the cost of using various I/O strategies to do a basic |
| 24 |
request-response loop. |
| 25 |
*/ |
| 26 |
|
| 27 |
#include <errno.h> |
| 28 |
#include <netinet/in.h> |
| 29 |
#include <poll.h> |
| 30 |
#include <stdio.h> |
| 31 |
#include <string.h> |
| 32 |
#include <unistd.h> |
| 33 |
|
| 34 |
#include <grpc/grpc.h> |
| 35 |
#ifdef __linux__ |
| 36 |
#include <sys/epoll.h> |
| 37 |
#endif |
| 38 |
#include <sys/socket.h> |
| 39 |
|
| 40 |
#include <string> |
| 41 |
|
| 42 |
#include <grpc/support/alloc.h> |
| 43 |
#include <grpc/support/log.h> |
| 44 |
#include <grpc/support/time.h> |
| 45 |
|
| 46 |
#include "src/core/lib/gpr/useful.h" |
| 47 |
#include "src/core/lib/gprpp/strerror.h" |
| 48 |
#include "src/core/lib/gprpp/thd.h" |
| 49 |
#include "src/core/lib/iomgr/error.h" |
| 50 |
#include "src/core/lib/iomgr/socket_utils_posix.h" |
| 51 |
#include "test/core/util/cmdline.h" |
| 52 |
#include "test/core/util/histogram.h" |
| 53 |
|
| 54 |
typedef struct fd_pair { |
| 55 |
int read_fd; |
| 56 |
int write_fd; |
| 57 |
} fd_pair; |
| 58 |
|
| 59 |
typedef struct thread_args { |
| 60 |
fd_pair fds; |
| 61 |
size_t msg_size; |
| 62 |
int (*read_bytes)(struct thread_args* args, char* buf); |
| 63 |
int (*write_bytes)(struct thread_args* args, char* buf); |
| 64 |
int (*setup)(struct thread_args* args); |
| 65 |
int epoll_fd; |
| 66 |
const char* strategy_name; |
| 67 |
} thread_args; |
| 68 |
|
| 69 |
/* |
| 70 |
Read strategies |
| 71 |
|
| 72 |
There are a number of read strategies, each of which has a blocking and |
| 73 |
non-blocking version. |
| 74 |
*/ |
| 75 |
|
| 76 |
/* Basic call to read() */ |
| 77 |
static int read_bytes(int fd, char* buf, size_t read_size, int spin) { |
| 78 |
size_t bytes_read = 0; |
| 79 |
ssize_t err; |
| 80 |
do { |
| 81 |
err = read(fd, buf + bytes_read, read_size - bytes_read); |
| 82 |
if (err < 0) { |
| 83 |
if (errno == EINTR) { |
| 84 |
continue; |
| 85 |
} else { |
| 86 |
if (errno == EAGAIN && spin == 1) { |
| 87 |
continue; |
| 88 |
} |
| 89 |
gpr_log(GPR_ERROR, "Read failed: %s", |
| 90 |
grpc_core::StrError(errno).c_str()); |
| 91 |
return -1; |
| 92 |
} |
| 93 |
} else { |
| 94 |
bytes_read += static_cast<size_t>(err); |
| 95 |
} |
| 96 |
} while (bytes_read < read_size); |
| 97 |
return 0; |
| 98 |
} |
| 99 |
|
| 100 |
static int blocking_read_bytes(thread_args* args, char* buf) { |
| 101 |
return read_bytes(args->fds.read_fd, buf, args->msg_size, 0); |
| 102 |
} |
| 103 |
|
| 104 |
static int spin_read_bytes(thread_args* args, char* buf) { |
| 105 |
return read_bytes(args->fds.read_fd, buf, args->msg_size, 1); |
| 106 |
} |
| 107 |
|
| 108 |
/* Call poll() to monitor a non-blocking fd */ |
| 109 |
static int poll_read_bytes(int fd, char* buf, size_t read_size, int spin) { |
| 110 |
struct pollfd pfd; |
| 111 |
size_t bytes_read = 0; |
| 112 |
int err; |
| 113 |
ssize_t err2; |
| 114 |
|
| 115 |
pfd.fd = fd; |
| 116 |
pfd.events = POLLIN; |
| 117 |
do { |
| 118 |
err = poll(&pfd, 1, spin ? 0 : -1); |
| 119 |
if (err < 0) { |
| 120 |
if (errno == EINTR) { |
| 121 |
continue; |
| 122 |
} else { |
| 123 |
gpr_log(GPR_ERROR, "Poll failed: %s", |
| 124 |
grpc_core::StrError(errno).c_str()); |
| 125 |
return -1; |
| 126 |
} |
| 127 |
} |
| 128 |
if (err == 0 && spin) continue; |
| 129 |
GPR_ASSERT(err == 1); |
| 130 |
GPR_ASSERT(pfd.revents == POLLIN); |
| 131 |
do { |
| 132 |
err2 = read(fd, buf + bytes_read, read_size - bytes_read); |
| 133 |
} while (err2 < 0 && errno == EINTR); |
| 134 |
if (err2 < 0 && errno != EAGAIN) { |
| 135 |
gpr_log(GPR_ERROR, "Read failed: %s", grpc_core::StrError(errno).c_str()); |
| 136 |
return -1; |
| 137 |
} |
| 138 |
bytes_read += static_cast<size_t>(err2); |
| 139 |
} while (bytes_read < read_size); |
| 140 |
return 0; |
| 141 |
} |
| 142 |
|
| 143 |
static int poll_read_bytes_blocking(struct thread_args* args, char* buf) { |
| 144 |
return poll_read_bytes(args->fds.read_fd, buf, args->msg_size, 0); |
| 145 |
} |
| 146 |
|
| 147 |
static int poll_read_bytes_spin(struct thread_args* args, char* buf) { |
| 148 |
return poll_read_bytes(args->fds.read_fd, buf, args->msg_size, 1); |
| 149 |
} |
| 150 |
|
| 151 |
#ifdef __linux__ |
| 152 |
/* Call epoll_wait() to monitor a non-blocking fd */ |
| 153 |
static int epoll_read_bytes(struct thread_args* args, char* buf, int spin) { |
| 154 |
struct epoll_event ev; |
| 155 |
size_t bytes_read = 0; |
| 156 |
int err; |
| 157 |
ssize_t err2; |
| 158 |
size_t read_size = args->msg_size; |
| 159 |
|
| 160 |
do { |
| 161 |
err = epoll_wait(args->epoll_fd, &ev, 1, spin ? 0 : -1); |
| 162 |
if (err < 0) { |
| 163 |
if (errno == EINTR) continue; |
| 164 |
gpr_log(GPR_ERROR, "epoll_wait failed: %s", |
| 165 |
grpc_core::StrError(errno).c_str()); |
| 166 |
return -1; |
| 167 |
} |
| 168 |
if (err == 0 && spin) continue; |
| 169 |
GPR_ASSERT(err == 1); |
| 170 |
GPR_ASSERT(ev.events & EPOLLIN); |
| 171 |
GPR_ASSERT(ev.data.fd == args->fds.read_fd); |
| 172 |
do { |
| 173 |
do { |
| 174 |
err2 = |
| 175 |
read(args->fds.read_fd, buf + bytes_read, read_size - bytes_read); |
| 176 |
} while (err2 < 0 && errno == EINTR); |
| 177 |
if (errno == EAGAIN) break; |
| 178 |
bytes_read += static_cast<size_t>(err2); |
| 179 |
/* TODO(klempner): This should really be doing an extra call after we are |
| 180 |
done to ensure we see an EAGAIN */ |
| 181 |
} while (bytes_read < read_size); |
| 182 |
} while (bytes_read < read_size); |
| 183 |
GPR_ASSERT(bytes_read == read_size); |
| 184 |
return 0; |
| 185 |
} |
| 186 |
|
| 187 |
static int epoll_read_bytes_blocking(struct thread_args* args, char* buf) { |
| 188 |
return epoll_read_bytes(args, buf, 0); |
| 189 |
} |
| 190 |
|
| 191 |
static int epoll_read_bytes_spin(struct thread_args* args, char* buf) { |
| 192 |
return epoll_read_bytes(args, buf, 1); |
| 193 |
} |
| 194 |
#endif /* __linux__ */ |
| 195 |
|
| 196 |
/* Write out bytes. |
| 197 |
At this point we only have one strategy, since in the common case these |
| 198 |
writes go directly out to the kernel. |
| 199 |
*/ |
| 200 |
static int blocking_write_bytes(struct thread_args* args, char* buf) { |
| 201 |
size_t bytes_written = 0; |
| 202 |
ssize_t err; |
| 203 |
size_t write_size = args->msg_size; |
| 204 |
do { |
| 205 |
err = write(args->fds.write_fd, buf + bytes_written, |
| 206 |
write_size - bytes_written); |
| 207 |
if (err < 0) { |
| 208 |
if (errno == EINTR) { |
| 209 |
continue; |
| 210 |
} else { |
| 211 |
gpr_log(GPR_ERROR, "Read failed: %s", |
| 212 |
grpc_core::StrError(errno).c_str()); |
| 213 |
return -1; |
| 214 |
} |
| 215 |
} else { |
| 216 |
bytes_written += static_cast<size_t>(err); |
| 217 |
} |
| 218 |
} while (bytes_written < write_size); |
| 219 |
return 0; |
| 220 |
} |
| 221 |
|
| 222 |
/* |
| 223 |
Initialization code |
| 224 |
|
| 225 |
These are called at the beginning of the client and server thread, depending |
| 226 |
on the scenario we're using. |
| 227 |
*/ |
| 228 |
static int set_socket_nonblocking(thread_args* args) { |
| 229 |
if (!GRPC_LOG_IF_ERROR("Unable to set read socket nonblocking", |
| 230 |
grpc_set_socket_nonblocking(args->fds.read_fd, 1))) { |
| 231 |
return -1; |
| 232 |
} |
| 233 |
if (!GRPC_LOG_IF_ERROR("Unable to set write socket nonblocking", |
| 234 |
grpc_set_socket_nonblocking(args->fds.write_fd, 1))) { |
| 235 |
return -1; |
| 236 |
} |
| 237 |
return 0; |
| 238 |
} |
| 239 |
|
| 240 |
static int do_nothing(thread_args* /*args*/) { return 0; } |
| 241 |
|
| 242 |
#ifdef __linux__ |
| 243 |
/* Special case for epoll, where we need to create the fd ahead of time. */ |
| 244 |
static int epoll_setup(thread_args* args) { |
| 245 |
int epoll_fd; |
| 246 |
struct epoll_event ev; |
| 247 |
set_socket_nonblocking(args); |
| 248 |
epoll_fd = epoll_create(1); |
| 249 |
if (epoll_fd < 0) { |
| 250 |
gpr_log(GPR_ERROR, "epoll_create: %s", grpc_core::StrError(errno).c_str()); |
| 251 |
return -1; |
| 252 |
} |
| 253 |
|
| 254 |
args->epoll_fd = epoll_fd; |
| 255 |
|
| 256 |
ev.events = EPOLLIN | EPOLLET; |
| 257 |
ev.data.fd = args->fds.read_fd; |
| 258 |
if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, args->fds.read_fd, &ev) < 0) { |
| 259 |
gpr_log(GPR_ERROR, "epoll_ctl: %s", grpc_core::StrError(errno).c_str()); |
| 260 |
} |
| 261 |
return 0; |
| 262 |
} |
| 263 |
#endif |
| 264 |
|
| 265 |
static void server_thread(thread_args* args) { |
| 266 |
char* buf = static_cast<char*>(gpr_malloc(args->msg_size)); |
| 267 |
if (args->setup(args) < 0) { |
| 268 |
gpr_log(GPR_ERROR, "Setup failed"); |
| 269 |
} |
| 270 |
for (;;) { |
| 271 |
if (args->read_bytes(args, buf) < 0) { |
| 272 |
gpr_log(GPR_ERROR, "Server read failed"); |
| 273 |
gpr_free(buf); |
| 274 |
return; |
| 275 |
} |
| 276 |
if (args->write_bytes(args, buf) < 0) { |
| 277 |
gpr_log(GPR_ERROR, "Server write failed"); |
| 278 |
gpr_free(buf); |
| 279 |
return; |
| 280 |
} |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
static void server_thread_wrap(void* arg) { |
| 285 |
thread_args* args = static_cast<thread_args*>(arg); |
| 286 |
server_thread(args); |
| 287 |
} |
| 288 |
|
| 289 |
static void print_histogram(grpc_histogram* histogram) { |
| 290 |
/* TODO(klempner): Print more detailed information, such as detailed histogram |
| 291 |
buckets */ |
| 292 |
gpr_log(GPR_INFO, "latency (50/95/99/99.9): %f/%f/%f/%f", |
| 293 |
grpc_histogram_percentile(histogram, 50), |
| 294 |
grpc_histogram_percentile(histogram, 95), |
| 295 |
grpc_histogram_percentile(histogram, 99), |
| 296 |
grpc_histogram_percentile(histogram, 99.9)); |
| 297 |
} |
| 298 |
|
| 299 |
static double now(void) { |
| 300 |
gpr_timespec tv = gpr_now(GPR_CLOCK_REALTIME); |
| 301 |
return 1e9 * static_cast<double>(tv.tv_sec) + static_cast<double>(tv.tv_nsec); |
| 302 |
} |
| 303 |
|
| 304 |
static void client_thread(thread_args* args) { |
| 305 |
char* buf = static_cast<char*>(gpr_malloc(args->msg_size * sizeof(char))); |
| 306 |
memset(buf, 0, args->msg_size * sizeof(char)); |
| 307 |
grpc_histogram* histogram = grpc_histogram_create(0.01, 60e9); |
| 308 |
double start_time; |
| 309 |
double end_time; |
| 310 |
double interval; |
| 311 |
const int kNumIters = 100000; |
| 312 |
int i; |
| 313 |
|
| 314 |
if (args->setup(args) < 0) { |
| 315 |
gpr_log(GPR_ERROR, "Setup failed"); |
| 316 |
} |
| 317 |
for (i = 0; i < kNumIters; ++i) { |
| 318 |
start_time = now(); |
| 319 |
if (args->write_bytes(args, buf) < 0) { |
| 320 |
gpr_log(GPR_ERROR, "Client write failed"); |
| 321 |
goto error; |
| 322 |
} |
| 323 |
if (args->read_bytes(args, buf) < 0) { |
| 324 |
gpr_log(GPR_ERROR, "Client read failed"); |
| 325 |
goto error; |
| 326 |
} |
| 327 |
end_time = now(); |
| 328 |
if (i > kNumIters / 2) { |
| 329 |
interval = end_time - start_time; |
| 330 |
grpc_histogram_add(histogram, interval); |
| 331 |
} |
| 332 |
} |
| 333 |
print_histogram(histogram); |
| 334 |
error: |
| 335 |
gpr_free(buf); |
| 336 |
grpc_histogram_destroy(histogram); |
| 337 |
} |
| 338 |
|
| 339 |
/* This roughly matches tcp_server's create_listening_socket */ |
| 340 |
static int create_listening_socket(struct sockaddr* port, socklen_t len) { |
| 341 |
int fd = socket(port->sa_family, SOCK_STREAM, 0); |
| 342 |
if (fd < 0) { |
| 343 |
gpr_log(GPR_ERROR, "Unable to create socket: %s", |
| 344 |
grpc_core::StrError(errno).c_str()); |
| 345 |
goto error; |
| 346 |
} |
| 347 |
|
| 348 |
if (!GRPC_LOG_IF_ERROR("Failed to set listening socket cloexec", |
| 349 |
grpc_set_socket_cloexec(fd, 1))) { |
| 350 |
goto error; |
| 351 |
} |
| 352 |
if (!GRPC_LOG_IF_ERROR("Failed to set listening socket low latency", |
| 353 |
grpc_set_socket_low_latency(fd, 1))) { |
| 354 |
goto error; |
| 355 |
} |
| 356 |
if (!GRPC_LOG_IF_ERROR("Failed to set listening socket reuse addr", |
| 357 |
grpc_set_socket_reuse_addr(fd, 1))) { |
| 358 |
goto error; |
| 359 |
} |
| 360 |
|
| 361 |
if (bind(fd, port, len) < 0) { |
| 362 |
gpr_log(GPR_ERROR, "bind: %s", grpc_core::StrError(errno).c_str()); |
| 363 |
goto error; |
| 364 |
} |
| 365 |
|
| 366 |
if (listen(fd, 1) < 0) { |
| 367 |
gpr_log(GPR_ERROR, "listen: %s", grpc_core::StrError(errno).c_str()); |
| 368 |
goto error; |
| 369 |
} |
| 370 |
|
| 371 |
if (getsockname(fd, port, &len) < 0) { |
| 372 |
gpr_log(GPR_ERROR, "getsockname: %s", grpc_core::StrError(errno).c_str()); |
| 373 |
goto error; |
| 374 |
} |
| 375 |
|
| 376 |
return fd; |
| 377 |
|
| 378 |
error: |
| 379 |
if (fd >= 0) { |
| 380 |
close(fd); |
| 381 |
} |
| 382 |
return -1; |
| 383 |
} |
| 384 |
|
| 385 |
static int connect_client(struct sockaddr* addr, socklen_t len) { |
| 386 |
int fd = socket(addr->sa_family, SOCK_STREAM, 0); |
| 387 |
int err; |
| 388 |
if (fd < 0) { |
| 389 |
gpr_log(GPR_ERROR, "Unable to create socket: %s", |
| 390 |
grpc_core::StrError(errno).c_str()); |
| 391 |
goto error; |
| 392 |
} |
| 393 |
|
| 394 |
if (!GRPC_LOG_IF_ERROR("Failed to set connecting socket cloexec", |
| 395 |
grpc_set_socket_cloexec(fd, 1))) { |
| 396 |
goto error; |
| 397 |
} |
| 398 |
if (!GRPC_LOG_IF_ERROR("Failed to set connecting socket low latency", |
| 399 |
grpc_set_socket_low_latency(fd, 1))) { |
| 400 |
goto error; |
| 401 |
} |
| 402 |
|
| 403 |
do { |
| 404 |
err = connect(fd, addr, len); |
| 405 |
} while (err < 0 && errno == EINTR); |
| 406 |
|
| 407 |
if (err < 0) { |
| 408 |
gpr_log(GPR_ERROR, "connect error: %s", grpc_core::StrError(errno).c_str()); |
| 409 |
goto error; |
| 410 |
} |
| 411 |
return fd; |
| 412 |
|
| 413 |
error: |
| 414 |
if (fd >= 0) { |
| 415 |
close(fd); |
| 416 |
} |
| 417 |
return -1; |
| 418 |
} |
| 419 |
|
| 420 |
static int accept_server(int listen_fd) { |
| 421 |
int fd = accept(listen_fd, nullptr, nullptr); |
| 422 |
if (fd < 0) { |
| 423 |
gpr_log(GPR_ERROR, "Accept failed: %s", grpc_core::StrError(errno).c_str()); |
| 424 |
return -1; |
| 425 |
} |
| 426 |
return fd; |
| 427 |
} |
| 428 |
|
| 429 |
static int create_sockets_tcp(fd_pair* client_fds, fd_pair* server_fds) { |
| 430 |
int listen_fd = -1; |
| 431 |
int client_fd = -1; |
| 432 |
int server_fd = -1; |
| 433 |
|
| 434 |
struct sockaddr_in port; |
| 435 |
struct sockaddr* sa_port = reinterpret_cast<struct sockaddr*>(&port); |
| 436 |
|
| 437 |
port.sin_family = AF_INET; |
| 438 |
port.sin_port = 0; |
| 439 |
port.sin_addr.s_addr = INADDR_ANY; |
| 440 |
|
| 441 |
listen_fd = create_listening_socket(sa_port, sizeof(port)); |
| 442 |
if (listen_fd == -1) { |
| 443 |
gpr_log(GPR_ERROR, "Listen failed"); |
| 444 |
goto error; |
| 445 |
} |
| 446 |
|
| 447 |
client_fd = connect_client(sa_port, sizeof(port)); |
| 448 |
if (client_fd == -1) { |
| 449 |
gpr_log(GPR_ERROR, "Connect failed"); |
| 450 |
goto error; |
| 451 |
} |
| 452 |
|
| 453 |
server_fd = accept_server(listen_fd); |
| 454 |
if (server_fd == -1) { |
| 455 |
gpr_log(GPR_ERROR, "Accept failed"); |
| 456 |
goto error; |
| 457 |
} |
| 458 |
|
| 459 |
client_fds->read_fd = client_fd; |
| 460 |
client_fds->write_fd = client_fd; |
| 461 |
server_fds->read_fd = server_fd; |
| 462 |
server_fds->write_fd = server_fd; |
| 463 |
close(listen_fd); |
| 464 |
return 0; |
| 465 |
|
| 466 |
error: |
| 467 |
if (listen_fd != -1) { |
| 468 |
close(listen_fd); |
| 469 |
} |
| 470 |
if (client_fd != -1) { |
| 471 |
close(client_fd); |
| 472 |
} |
| 473 |
if (server_fd != -1) { |
| 474 |
close(server_fd); |
| 475 |
} |
| 476 |
return -1; |
| 477 |
} |
| 478 |
|
| 479 |
static int create_sockets_socketpair(fd_pair* client_fds, fd_pair* server_fds) { |
| 480 |
int fds[2]; |
| 481 |
if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds) < 0) { |
| 482 |
gpr_log(GPR_ERROR, "socketpair: %s", grpc_core::StrError(errno).c_str()); |
| 483 |
return -1; |
| 484 |
} |
| 485 |
|
| 486 |
client_fds->read_fd = fds[0]; |
| 487 |
client_fds->write_fd = fds[0]; |
| 488 |
server_fds->read_fd = fds[1]; |
| 489 |
server_fds->write_fd = fds[1]; |
| 490 |
return 0; |
| 491 |
} |
| 492 |
|
| 493 |
static int create_sockets_pipe(fd_pair* client_fds, fd_pair* server_fds) { |
| 494 |
int cfds[2]; |
| 495 |
int sfds[2]; |
| 496 |
if (pipe(cfds) < 0) { |
| 497 |
gpr_log(GPR_ERROR, "pipe: %s", grpc_core::StrError(errno).c_str()); |
| 498 |
return -1; |
| 499 |
} |
| 500 |
|
| 501 |
if (pipe(sfds) < 0) { |
| 502 |
gpr_log(GPR_ERROR, "pipe: %s", grpc_core::StrError(errno).c_str()); |
| 503 |
return -1; |
| 504 |
} |
| 505 |
|
| 506 |
client_fds->read_fd = cfds[0]; |
| 507 |
client_fds->write_fd = cfds[1]; |
| 508 |
server_fds->read_fd = sfds[0]; |
| 509 |
server_fds->write_fd = sfds[1]; |
| 510 |
return 0; |
| 511 |
} |
| 512 |
|
| 513 |
static const char* read_strategy_usage = |
| 514 |
"Strategy for doing reads, which is one of:\n" |
| 515 |
" blocking: blocking read calls\n" |
| 516 |
" same_thread_poll: poll() call on same thread \n" |
| 517 |
#ifdef __linux__ |
| 518 |
" same_thread_epoll: epoll_wait() on same thread \n" |
| 519 |
#endif |
| 520 |
" spin_read: spinning non-blocking read() calls \n" |
| 521 |
" spin_poll: spinning 0 timeout poll() calls \n" |
| 522 |
#ifdef __linux__ |
| 523 |
" spin_epoll: spinning 0 timeout epoll_wait() calls \n" |
| 524 |
#endif |
| 525 |
""; |
| 526 |
|
| 527 |
static const char* socket_type_usage = |
| 528 |
"Type of socket used, one of:\n" |
| 529 |
" tcp: fds are endpoints of a TCP connection\n" |
| 530 |
" socketpair: fds come from socketpair()\n" |
| 531 |
" pipe: fds come from pipe()\n"; |
| 532 |
|
| 533 |
void print_usage(char* argv0) { |
| 534 |
fprintf(stderr, "%s usage:\n\n", argv0); |
| 535 |
fprintf(stderr, "%s read_strategy socket_type msg_size\n\n", argv0); |
| 536 |
fprintf(stderr, "where read_strategy is one of:\n"); |
| 537 |
fprintf(stderr, " blocking: blocking read calls\n"); |
| 538 |
fprintf(stderr, " same_thread_poll: poll() call on same thread \n"); |
| 539 |
#ifdef __linux__ |
| 540 |
fprintf(stderr, " same_thread_epoll: epoll_wait() on same thread \n"); |
| 541 |
#endif |
| 542 |
fprintf(stderr, " spin_read: spinning non-blocking read() calls \n"); |
| 543 |
fprintf(stderr, " spin_poll: spinning 0 timeout poll() calls \n"); |
| 544 |
#ifdef __linux__ |
| 545 |
fprintf(stderr, " spin_epoll: spinning 0 timeout epoll_wait() calls \n"); |
| 546 |
#endif |
| 547 |
fprintf(stderr, "and socket_type is one of:\n"); |
| 548 |
fprintf(stderr, " tcp: fds are endpoints of a TCP connection\n"); |
| 549 |
fprintf(stderr, " socketpair: fds come from socketpair()\n"); |
| 550 |
fprintf(stderr, " pipe: fds come from pipe()\n"); |
| 551 |
fflush(stderr); |
| 552 |
} |
| 553 |
|
| 554 |
typedef struct test_strategy { |
| 555 |
const char* name; |
| 556 |
int (*read_strategy)(struct thread_args* args, char* buf); |
| 557 |
int (*setup)(struct thread_args* args); |
| 558 |
} test_strategy; |
| 559 |
|
| 560 |
static test_strategy test_strategies[] = { |
| 561 |
{"blocking", blocking_read_bytes, do_nothing}, |
| 562 |
{"same_thread_poll", poll_read_bytes_blocking, set_socket_nonblocking}, |
| 563 |
#ifdef __linux__ |
| 564 |
{"same_thread_epoll", epoll_read_bytes_blocking, epoll_setup}, |
| 565 |
{"spin_epoll", epoll_read_bytes_spin, epoll_setup}, |
| 566 |
#endif /* __linux__ */ |
| 567 |
{"spin_read", spin_read_bytes, set_socket_nonblocking}, |
| 568 |
{"spin_poll", poll_read_bytes_spin, set_socket_nonblocking}}; |
| 569 |
|
| 570 |
static const char* socket_types[] = {"tcp", "socketpair", "pipe"}; |
| 571 |
|
| 572 |
int create_socket(const char* socket_type, fd_pair* client_fds, |
| 573 |
fd_pair* server_fds) { |
| 574 |
if (strcmp(socket_type, "tcp") == 0) { |
| 575 |
create_sockets_tcp(client_fds, server_fds); |
| 576 |
} else if (strcmp(socket_type, "socketpair") == 0) { |
| 577 |
create_sockets_socketpair(client_fds, server_fds); |
| 578 |
} else if (strcmp(socket_type, "pipe") == 0) { |
| 579 |
create_sockets_pipe(client_fds, server_fds); |
| 580 |
} else { |
| 581 |
fprintf(stderr, "Invalid socket type %s\n", socket_type); |
| 582 |
fflush(stderr); |
| 583 |
return -1; |
| 584 |
} |
| 585 |
return 0; |
| 586 |
} |
| 587 |
|
| 588 |
static int run_benchmark(const char* socket_type, thread_args* client_args, |
| 589 |
thread_args* server_args) { |
| 590 |
int rv = 0; |
| 591 |
|
| 592 |
rv = create_socket(socket_type, &client_args->fds, &server_args->fds); |
| 593 |
if (rv < 0) { |
| 594 |
return rv; |
| 595 |
} |
| 596 |
|
| 597 |
gpr_log(GPR_INFO, "Starting test %s %s %zu", client_args->strategy_name, |
| 598 |
socket_type, client_args->msg_size); |
| 599 |
|
| 600 |
grpc_core::Thread server("server_thread", server_thread_wrap, server_args); |
| 601 |
server.Start(); |
| 602 |
client_thread(client_args); |
| 603 |
server.Join(); |
| 604 |
|
| 605 |
return 0; |
| 606 |
} |
| 607 |
|
| 608 |
static int run_all_benchmarks(size_t msg_size) { |
| 609 |
int error = 0; |
| 610 |
size_t i; |
| 611 |
for (i = 0; i < GPR_ARRAY_SIZE(test_strategies); ++i) { |
| 612 |
test_strategy* strategy = &test_strategies[i]; |
| 613 |
size_t j; |
| 614 |
for (j = 0; j < GPR_ARRAY_SIZE(socket_types); ++j) { |
| 615 |
thread_args* client_args = |
| 616 |
static_cast<thread_args*>(gpr_malloc(sizeof(thread_args))); |
| 617 |
thread_args* server_args = |
| 618 |
static_cast<thread_args*>(gpr_malloc(sizeof(thread_args))); |
| 619 |
const char* socket_type = socket_types[j]; |
| 620 |
|
| 621 |
client_args->read_bytes = strategy->read_strategy; |
| 622 |
client_args->write_bytes = blocking_write_bytes; |
| 623 |
client_args->setup = strategy->setup; |
| 624 |
client_args->msg_size = msg_size; |
| 625 |
client_args->strategy_name = strategy->name; |
| 626 |
server_args->read_bytes = strategy->read_strategy; |
| 627 |
server_args->write_bytes = blocking_write_bytes; |
| 628 |
server_args->setup = strategy->setup; |
| 629 |
server_args->msg_size = msg_size; |
| 630 |
server_args->strategy_name = strategy->name; |
| 631 |
error = run_benchmark(socket_type, client_args, server_args); |
| 632 |
if (error < 0) { |
| 633 |
return error; |
| 634 |
} |
| 635 |
} |
| 636 |
} |
| 637 |
return error; |
| 638 |
} |
| 639 |
|
| 640 |
int main(int argc, char** argv) { |
| 641 |
thread_args* client_args = |
| 642 |
static_cast<thread_args*>(gpr_malloc(sizeof(thread_args))); |
| 643 |
thread_args* server_args = |
| 644 |
static_cast<thread_args*>(gpr_malloc(sizeof(thread_args))); |
| 645 |
int msg_size = -1; |
| 646 |
const char* read_strategy = nullptr; |
| 647 |
const char* socket_type = nullptr; |
| 648 |
size_t i; |
| 649 |
const test_strategy* strategy = nullptr; |
| 650 |
int error = 0; |
| 651 |
|
| 652 |
gpr_cmdline* cmdline = |
| 653 |
gpr_cmdline_create("low_level_ping_pong network benchmarking tool"); |
| 654 |
|
| 655 |
gpr_cmdline_add_int(cmdline, "msg_size", "Size of sent messages", &msg_size); |
| 656 |
gpr_cmdline_add_string(cmdline, "read_strategy", read_strategy_usage, |
| 657 |
&read_strategy); |
| 658 |
gpr_cmdline_add_string(cmdline, "socket_type", socket_type_usage, |
| 659 |
&socket_type); |
| 660 |
|
| 661 |
gpr_cmdline_parse(cmdline, argc, argv); |
| 662 |
|
| 663 |
if (msg_size == -1) { |
| 664 |
msg_size = 50; |
| 665 |
} |
| 666 |
|
| 667 |
if (read_strategy == nullptr) { |
| 668 |
gpr_log(GPR_INFO, "No strategy specified, running all benchmarks"); |
| 669 |
return run_all_benchmarks(static_cast<size_t>(msg_size)); |
| 670 |
} |
| 671 |
|
| 672 |
if (socket_type == nullptr) { |
| 673 |
socket_type = "tcp"; |
| 674 |
} |
| 675 |
if (msg_size <= 0) { |
| 676 |
fprintf(stderr, "msg_size must be > 0\n"); |
| 677 |
fflush(stderr); |
| 678 |
print_usage(argv[0]); |
| 679 |
return -1; |
| 680 |
} |
| 681 |
|
| 682 |
for (i = 0; i < GPR_ARRAY_SIZE(test_strategies); ++i) { |
| 683 |
if (strcmp(test_strategies[i].name, read_strategy) == 0) { |
| 684 |
strategy = &test_strategies[i]; |
| 685 |
} |
| 686 |
} |
| 687 |
if (strategy == nullptr) { |
| 688 |
fprintf(stderr, "Invalid read strategy %s\n", read_strategy); |
| 689 |
fflush(stderr); |
| 690 |
return -1; |
| 691 |
} |
| 692 |
|
| 693 |
client_args->read_bytes = strategy->read_strategy; |
| 694 |
client_args->write_bytes = blocking_write_bytes; |
| 695 |
client_args->setup = strategy->setup; |
| 696 |
client_args->msg_size = static_cast<size_t>(msg_size); |
| 697 |
client_args->strategy_name = read_strategy; |
| 698 |
server_args->read_bytes = strategy->read_strategy; |
| 699 |
server_args->write_bytes = blocking_write_bytes; |
| 700 |
server_args->setup = strategy->setup; |
| 701 |
server_args->msg_size = static_cast<size_t>(msg_size); |
| 702 |
server_args->strategy_name = read_strategy; |
| 703 |
|
| 704 |
error = run_benchmark(socket_type, client_args, server_args); |
| 705 |
|
| 706 |
gpr_cmdline_destroy(cmdline); |
| 707 |
return error; |
| 708 |
} |