| 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <unistd.h> |
| 4 | #include <limits.h> |
| 5 | #include <errno.h> |
| 6 | #include <fcntl.h> |
| 7 | #include <sys/poll.h> |
| 8 | #include <sys/types.h> |
| 9 | #include <sys/stat.h> |
| 10 | #include <sys/wait.h> |
| 11 | #include <sys/socket.h> |
| 12 | #include <sys/un.h> |
| 13 | #include <netinet/in.h> |
| 14 | #include <arpa/inet.h> |
| 15 | #include <netdb.h> |
| 16 | #include <signal.h> |
| 17 | #ifdef CONFIG_ZLIB |
| 18 | #include <zlib.h> |
| 19 | #endif |
| 20 | |
| 21 | #include "fio.h" |
| 22 | #include "client.h" |
| 23 | #include "server.h" |
| 24 | #include "flist.h" |
| 25 | #include "hash.h" |
| 26 | #include "verify.h" |
| 27 | |
| 28 | static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd); |
| 29 | static void handle_ts(struct fio_client *client, struct fio_net_cmd *cmd); |
| 30 | static void handle_gs(struct fio_client *client, struct fio_net_cmd *cmd); |
| 31 | static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd); |
| 32 | static void handle_text(struct fio_client *client, struct fio_net_cmd *cmd); |
| 33 | static void handle_stop(struct fio_client *client, struct fio_net_cmd *cmd); |
| 34 | static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd); |
| 35 | |
| 36 | struct client_ops fio_client_ops = { |
| 37 | .text = handle_text, |
| 38 | .disk_util = handle_du, |
| 39 | .thread_status = handle_ts, |
| 40 | .group_stats = handle_gs, |
| 41 | .stop = handle_stop, |
| 42 | .start = handle_start, |
| 43 | .eta = display_thread_status, |
| 44 | .probe = handle_probe, |
| 45 | .eta_msec = FIO_CLIENT_DEF_ETA_MSEC, |
| 46 | .client_type = FIO_CLIENT_TYPE_CLI, |
| 47 | }; |
| 48 | |
| 49 | static struct timeval eta_tv; |
| 50 | |
| 51 | static FLIST_HEAD(client_list); |
| 52 | static FLIST_HEAD(eta_list); |
| 53 | |
| 54 | static FLIST_HEAD(arg_list); |
| 55 | |
| 56 | struct thread_stat client_ts; |
| 57 | struct group_run_stats client_gs; |
| 58 | int sum_stat_clients; |
| 59 | |
| 60 | static int sum_stat_nr; |
| 61 | static struct json_object *root = NULL; |
| 62 | static struct json_object *job_opt_object = NULL; |
| 63 | static struct json_array *clients_array = NULL; |
| 64 | static struct json_array *du_array = NULL; |
| 65 | |
| 66 | static int error_clients; |
| 67 | |
| 68 | #define FIO_CLIENT_HASH_BITS 7 |
| 69 | #define FIO_CLIENT_HASH_SZ (1 << FIO_CLIENT_HASH_BITS) |
| 70 | #define FIO_CLIENT_HASH_MASK (FIO_CLIENT_HASH_SZ - 1) |
| 71 | static struct flist_head client_hash[FIO_CLIENT_HASH_SZ]; |
| 72 | |
| 73 | static void fio_client_add_hash(struct fio_client *client) |
| 74 | { |
| 75 | int bucket = hash_long(client->fd, FIO_CLIENT_HASH_BITS); |
| 76 | |
| 77 | bucket &= FIO_CLIENT_HASH_MASK; |
| 78 | flist_add(&client->hash_list, &client_hash[bucket]); |
| 79 | } |
| 80 | |
| 81 | static void fio_client_remove_hash(struct fio_client *client) |
| 82 | { |
| 83 | if (!flist_empty(&client->hash_list)) |
| 84 | flist_del_init(&client->hash_list); |
| 85 | } |
| 86 | |
| 87 | static void fio_init fio_client_hash_init(void) |
| 88 | { |
| 89 | int i; |
| 90 | |
| 91 | for (i = 0; i < FIO_CLIENT_HASH_SZ; i++) |
| 92 | INIT_FLIST_HEAD(&client_hash[i]); |
| 93 | } |
| 94 | |
| 95 | static int read_data(int fd, void *data, size_t size) |
| 96 | { |
| 97 | ssize_t ret; |
| 98 | |
| 99 | while (size) { |
| 100 | ret = read(fd, data, size); |
| 101 | if (ret < 0) { |
| 102 | if (errno == EAGAIN || errno == EINTR) |
| 103 | continue; |
| 104 | break; |
| 105 | } else if (!ret) |
| 106 | break; |
| 107 | else { |
| 108 | data += ret; |
| 109 | size -= ret; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | if (size) |
| 114 | return EAGAIN; |
| 115 | |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | static void fio_client_json_init(void) |
| 120 | { |
| 121 | char time_buf[32]; |
| 122 | time_t time_p; |
| 123 | |
| 124 | if (!(output_format & FIO_OUTPUT_JSON)) |
| 125 | return; |
| 126 | |
| 127 | time(&time_p); |
| 128 | os_ctime_r((const time_t *) &time_p, time_buf, sizeof(time_buf)); |
| 129 | time_buf[strlen(time_buf) - 1] = '\0'; |
| 130 | |
| 131 | root = json_create_object(); |
| 132 | json_object_add_value_string(root, "fio version", fio_version_string); |
| 133 | json_object_add_value_int(root, "timestamp", time_p); |
| 134 | json_object_add_value_string(root, "time", time_buf); |
| 135 | |
| 136 | job_opt_object = json_create_object(); |
| 137 | json_object_add_value_object(root, "global options", job_opt_object); |
| 138 | clients_array = json_create_array(); |
| 139 | json_object_add_value_array(root, "client_stats", clients_array); |
| 140 | du_array = json_create_array(); |
| 141 | json_object_add_value_array(root, "disk_util", du_array); |
| 142 | } |
| 143 | |
| 144 | static void fio_client_json_fini(void) |
| 145 | { |
| 146 | if (!(output_format & FIO_OUTPUT_JSON)) |
| 147 | return; |
| 148 | |
| 149 | log_info("\n"); |
| 150 | json_print_object(root, NULL); |
| 151 | log_info("\n"); |
| 152 | json_free_object(root); |
| 153 | root = NULL; |
| 154 | clients_array = NULL; |
| 155 | du_array = NULL; |
| 156 | } |
| 157 | |
| 158 | static struct fio_client *find_client_by_fd(int fd) |
| 159 | { |
| 160 | int bucket = hash_long(fd, FIO_CLIENT_HASH_BITS) & FIO_CLIENT_HASH_MASK; |
| 161 | struct fio_client *client; |
| 162 | struct flist_head *entry; |
| 163 | |
| 164 | flist_for_each(entry, &client_hash[bucket]) { |
| 165 | client = flist_entry(entry, struct fio_client, hash_list); |
| 166 | |
| 167 | if (client->fd == fd) { |
| 168 | client->refs++; |
| 169 | return client; |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | return NULL; |
| 174 | } |
| 175 | |
| 176 | void fio_put_client(struct fio_client *client) |
| 177 | { |
| 178 | if (--client->refs) |
| 179 | return; |
| 180 | |
| 181 | free(client->hostname); |
| 182 | if (client->argv) |
| 183 | free(client->argv); |
| 184 | if (client->name) |
| 185 | free(client->name); |
| 186 | while (client->nr_files) { |
| 187 | struct client_file *cf = &client->files[--client->nr_files]; |
| 188 | |
| 189 | free(cf->file); |
| 190 | } |
| 191 | if (client->files) |
| 192 | free(client->files); |
| 193 | if (client->opt_lists) |
| 194 | free(client->opt_lists); |
| 195 | |
| 196 | if (!client->did_stat) |
| 197 | sum_stat_clients--; |
| 198 | |
| 199 | if (client->error) |
| 200 | error_clients++; |
| 201 | |
| 202 | free(client); |
| 203 | } |
| 204 | |
| 205 | static int fio_client_dec_jobs_eta(struct client_eta *eta, client_eta_op eta_fn) |
| 206 | { |
| 207 | if (!--eta->pending) { |
| 208 | eta_fn(&eta->eta); |
| 209 | free(eta); |
| 210 | return 0; |
| 211 | } |
| 212 | |
| 213 | return 1; |
| 214 | } |
| 215 | |
| 216 | static void remove_client(struct fio_client *client) |
| 217 | { |
| 218 | assert(client->refs); |
| 219 | |
| 220 | dprint(FD_NET, "client: removed <%s>\n", client->hostname); |
| 221 | |
| 222 | if (!flist_empty(&client->list)) |
| 223 | flist_del_init(&client->list); |
| 224 | |
| 225 | fio_client_remove_hash(client); |
| 226 | |
| 227 | if (!flist_empty(&client->eta_list)) { |
| 228 | flist_del_init(&client->eta_list); |
| 229 | fio_client_dec_jobs_eta(client->eta_in_flight, client->ops->eta); |
| 230 | } |
| 231 | |
| 232 | close(client->fd); |
| 233 | client->fd = -1; |
| 234 | |
| 235 | if (client->ops->removed) |
| 236 | client->ops->removed(client); |
| 237 | |
| 238 | nr_clients--; |
| 239 | fio_put_client(client); |
| 240 | } |
| 241 | |
| 242 | struct fio_client *fio_get_client(struct fio_client *client) |
| 243 | { |
| 244 | client->refs++; |
| 245 | return client; |
| 246 | } |
| 247 | |
| 248 | static void __fio_client_add_cmd_option(struct fio_client *client, |
| 249 | const char *opt) |
| 250 | { |
| 251 | int index; |
| 252 | |
| 253 | index = client->argc++; |
| 254 | client->argv = realloc(client->argv, sizeof(char *) * client->argc); |
| 255 | client->argv[index] = strdup(opt); |
| 256 | dprint(FD_NET, "client: add cmd %d: %s\n", index, opt); |
| 257 | } |
| 258 | |
| 259 | void fio_client_add_cmd_option(void *cookie, const char *opt) |
| 260 | { |
| 261 | struct fio_client *client = cookie; |
| 262 | struct flist_head *entry; |
| 263 | |
| 264 | if (!client || !opt) |
| 265 | return; |
| 266 | |
| 267 | __fio_client_add_cmd_option(client, opt); |
| 268 | |
| 269 | /* |
| 270 | * Duplicate arguments to shared client group |
| 271 | */ |
| 272 | flist_for_each(entry, &arg_list) { |
| 273 | client = flist_entry(entry, struct fio_client, arg_list); |
| 274 | |
| 275 | __fio_client_add_cmd_option(client, opt); |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | struct fio_client *fio_client_add_explicit(struct client_ops *ops, |
| 280 | const char *hostname, int type, |
| 281 | int port) |
| 282 | { |
| 283 | struct fio_client *client; |
| 284 | |
| 285 | client = malloc(sizeof(*client)); |
| 286 | memset(client, 0, sizeof(*client)); |
| 287 | |
| 288 | INIT_FLIST_HEAD(&client->list); |
| 289 | INIT_FLIST_HEAD(&client->hash_list); |
| 290 | INIT_FLIST_HEAD(&client->arg_list); |
| 291 | INIT_FLIST_HEAD(&client->eta_list); |
| 292 | INIT_FLIST_HEAD(&client->cmd_list); |
| 293 | |
| 294 | client->hostname = strdup(hostname); |
| 295 | |
| 296 | if (type == Fio_client_socket) |
| 297 | client->is_sock = 1; |
| 298 | else { |
| 299 | int ipv6; |
| 300 | |
| 301 | ipv6 = type == Fio_client_ipv6; |
| 302 | if (fio_server_parse_host(hostname, ipv6, |
| 303 | &client->addr.sin_addr, |
| 304 | &client->addr6.sin6_addr)) |
| 305 | goto err; |
| 306 | |
| 307 | client->port = port; |
| 308 | } |
| 309 | |
| 310 | client->fd = -1; |
| 311 | client->ops = ops; |
| 312 | client->refs = 1; |
| 313 | client->type = ops->client_type; |
| 314 | |
| 315 | __fio_client_add_cmd_option(client, "fio"); |
| 316 | |
| 317 | flist_add(&client->list, &client_list); |
| 318 | nr_clients++; |
| 319 | dprint(FD_NET, "client: added <%s>\n", client->hostname); |
| 320 | return client; |
| 321 | err: |
| 322 | free(client); |
| 323 | return NULL; |
| 324 | } |
| 325 | |
| 326 | int fio_client_add_ini_file(void *cookie, const char *ini_file, int remote) |
| 327 | { |
| 328 | struct fio_client *client = cookie; |
| 329 | struct client_file *cf; |
| 330 | size_t new_size; |
| 331 | void *new_files; |
| 332 | |
| 333 | if (!client) |
| 334 | return 1; |
| 335 | |
| 336 | dprint(FD_NET, "client <%s>: add ini %s\n", client->hostname, ini_file); |
| 337 | |
| 338 | new_size = (client->nr_files + 1) * sizeof(struct client_file); |
| 339 | new_files = realloc(client->files, new_size); |
| 340 | if (!new_files) |
| 341 | return 1; |
| 342 | |
| 343 | client->files = new_files; |
| 344 | cf = &client->files[client->nr_files]; |
| 345 | cf->file = strdup(ini_file); |
| 346 | cf->remote = remote; |
| 347 | client->nr_files++; |
| 348 | return 0; |
| 349 | } |
| 350 | |
| 351 | int fio_client_add(struct client_ops *ops, const char *hostname, void **cookie) |
| 352 | { |
| 353 | struct fio_client *existing = *cookie; |
| 354 | struct fio_client *client; |
| 355 | |
| 356 | if (existing) { |
| 357 | /* |
| 358 | * We always add our "exec" name as the option, hence 1 |
| 359 | * means empty. |
| 360 | */ |
| 361 | if (existing->argc == 1) |
| 362 | flist_add_tail(&existing->arg_list, &arg_list); |
| 363 | else { |
| 364 | while (!flist_empty(&arg_list)) |
| 365 | flist_del_init(arg_list.next); |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | client = malloc(sizeof(*client)); |
| 370 | memset(client, 0, sizeof(*client)); |
| 371 | |
| 372 | INIT_FLIST_HEAD(&client->list); |
| 373 | INIT_FLIST_HEAD(&client->hash_list); |
| 374 | INIT_FLIST_HEAD(&client->arg_list); |
| 375 | INIT_FLIST_HEAD(&client->eta_list); |
| 376 | INIT_FLIST_HEAD(&client->cmd_list); |
| 377 | |
| 378 | if (fio_server_parse_string(hostname, &client->hostname, |
| 379 | &client->is_sock, &client->port, |
| 380 | &client->addr.sin_addr, |
| 381 | &client->addr6.sin6_addr, |
| 382 | &client->ipv6)) |
| 383 | return -1; |
| 384 | |
| 385 | client->fd = -1; |
| 386 | client->ops = ops; |
| 387 | client->refs = 1; |
| 388 | client->type = ops->client_type; |
| 389 | |
| 390 | __fio_client_add_cmd_option(client, "fio"); |
| 391 | |
| 392 | flist_add(&client->list, &client_list); |
| 393 | nr_clients++; |
| 394 | dprint(FD_NET, "client: added <%s>\n", client->hostname); |
| 395 | *cookie = client; |
| 396 | return 0; |
| 397 | } |
| 398 | |
| 399 | static const char *server_name(struct fio_client *client, char *buf, |
| 400 | size_t bufsize) |
| 401 | { |
| 402 | const char *from; |
| 403 | |
| 404 | if (client->ipv6) |
| 405 | from = inet_ntop(AF_INET6, (struct sockaddr *) &client->addr6.sin6_addr, buf, bufsize); |
| 406 | else if (client->is_sock) |
| 407 | from = "sock"; |
| 408 | else |
| 409 | from = inet_ntop(AF_INET, (struct sockaddr *) &client->addr.sin_addr, buf, bufsize); |
| 410 | |
| 411 | return from; |
| 412 | } |
| 413 | |
| 414 | static void probe_client(struct fio_client *client) |
| 415 | { |
| 416 | struct cmd_client_probe_pdu pdu; |
| 417 | const char *sname; |
| 418 | uint64_t tag; |
| 419 | char buf[64]; |
| 420 | |
| 421 | dprint(FD_NET, "client: send probe\n"); |
| 422 | |
| 423 | #ifdef CONFIG_ZLIB |
| 424 | pdu.flags = __le64_to_cpu(FIO_PROBE_FLAG_ZLIB); |
| 425 | #else |
| 426 | pdu.flags = 0; |
| 427 | #endif |
| 428 | |
| 429 | sname = server_name(client, buf, sizeof(buf)); |
| 430 | memset(pdu.server, 0, sizeof(pdu.server)); |
| 431 | strncpy((char *) pdu.server, sname, sizeof(pdu.server) - 1); |
| 432 | |
| 433 | fio_net_send_cmd(client->fd, FIO_NET_CMD_PROBE, &pdu, sizeof(pdu), &tag, &client->cmd_list); |
| 434 | } |
| 435 | |
| 436 | static int fio_client_connect_ip(struct fio_client *client) |
| 437 | { |
| 438 | struct sockaddr *addr; |
| 439 | socklen_t socklen; |
| 440 | int fd, domain; |
| 441 | |
| 442 | if (client->ipv6) { |
| 443 | client->addr6.sin6_family = AF_INET6; |
| 444 | client->addr6.sin6_port = htons(client->port); |
| 445 | domain = AF_INET6; |
| 446 | addr = (struct sockaddr *) &client->addr6; |
| 447 | socklen = sizeof(client->addr6); |
| 448 | } else { |
| 449 | client->addr.sin_family = AF_INET; |
| 450 | client->addr.sin_port = htons(client->port); |
| 451 | domain = AF_INET; |
| 452 | addr = (struct sockaddr *) &client->addr; |
| 453 | socklen = sizeof(client->addr); |
| 454 | } |
| 455 | |
| 456 | fd = socket(domain, SOCK_STREAM, 0); |
| 457 | if (fd < 0) { |
| 458 | int ret = -errno; |
| 459 | |
| 460 | log_err("fio: socket: %s\n", strerror(errno)); |
| 461 | return ret; |
| 462 | } |
| 463 | |
| 464 | if (connect(fd, addr, socklen) < 0) { |
| 465 | int ret = -errno; |
| 466 | |
| 467 | log_err("fio: connect: %s\n", strerror(errno)); |
| 468 | log_err("fio: failed to connect to %s:%u\n", client->hostname, |
| 469 | client->port); |
| 470 | close(fd); |
| 471 | return ret; |
| 472 | } |
| 473 | |
| 474 | return fd; |
| 475 | } |
| 476 | |
| 477 | static int fio_client_connect_sock(struct fio_client *client) |
| 478 | { |
| 479 | struct sockaddr_un *addr = &client->addr_un; |
| 480 | socklen_t len; |
| 481 | int fd; |
| 482 | |
| 483 | memset(addr, 0, sizeof(*addr)); |
| 484 | addr->sun_family = AF_UNIX; |
| 485 | strncpy(addr->sun_path, client->hostname, sizeof(addr->sun_path) - 1); |
| 486 | |
| 487 | fd = socket(AF_UNIX, SOCK_STREAM, 0); |
| 488 | if (fd < 0) { |
| 489 | int ret = -errno; |
| 490 | |
| 491 | log_err("fio: socket: %s\n", strerror(errno)); |
| 492 | return ret; |
| 493 | } |
| 494 | |
| 495 | len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1; |
| 496 | if (connect(fd, (struct sockaddr *) addr, len) < 0) { |
| 497 | int ret = -errno; |
| 498 | |
| 499 | log_err("fio: connect; %s\n", strerror(errno)); |
| 500 | close(fd); |
| 501 | return ret; |
| 502 | } |
| 503 | |
| 504 | return fd; |
| 505 | } |
| 506 | |
| 507 | int fio_client_connect(struct fio_client *client) |
| 508 | { |
| 509 | int fd; |
| 510 | |
| 511 | dprint(FD_NET, "client: connect to host %s\n", client->hostname); |
| 512 | |
| 513 | if (client->is_sock) |
| 514 | fd = fio_client_connect_sock(client); |
| 515 | else |
| 516 | fd = fio_client_connect_ip(client); |
| 517 | |
| 518 | dprint(FD_NET, "client: %s connected %d\n", client->hostname, fd); |
| 519 | |
| 520 | if (fd < 0) |
| 521 | return fd; |
| 522 | |
| 523 | client->fd = fd; |
| 524 | fio_client_add_hash(client); |
| 525 | client->state = Client_connected; |
| 526 | |
| 527 | probe_client(client); |
| 528 | return 0; |
| 529 | } |
| 530 | |
| 531 | int fio_client_terminate(struct fio_client *client) |
| 532 | { |
| 533 | return fio_net_send_quit(client->fd); |
| 534 | } |
| 535 | |
| 536 | void fio_clients_terminate(void) |
| 537 | { |
| 538 | struct flist_head *entry; |
| 539 | struct fio_client *client; |
| 540 | |
| 541 | dprint(FD_NET, "client: terminate clients\n"); |
| 542 | |
| 543 | flist_for_each(entry, &client_list) { |
| 544 | client = flist_entry(entry, struct fio_client, list); |
| 545 | fio_client_terminate(client); |
| 546 | } |
| 547 | } |
| 548 | |
| 549 | static void sig_int(int sig) |
| 550 | { |
| 551 | dprint(FD_NET, "client: got signal %d\n", sig); |
| 552 | fio_clients_terminate(); |
| 553 | } |
| 554 | |
| 555 | static void client_signal_handler(void) |
| 556 | { |
| 557 | struct sigaction act; |
| 558 | |
| 559 | memset(&act, 0, sizeof(act)); |
| 560 | act.sa_handler = sig_int; |
| 561 | act.sa_flags = SA_RESTART; |
| 562 | sigaction(SIGINT, &act, NULL); |
| 563 | |
| 564 | memset(&act, 0, sizeof(act)); |
| 565 | act.sa_handler = sig_int; |
| 566 | act.sa_flags = SA_RESTART; |
| 567 | sigaction(SIGTERM, &act, NULL); |
| 568 | |
| 569 | /* Windows uses SIGBREAK as a quit signal from other applications */ |
| 570 | #ifdef WIN32 |
| 571 | memset(&act, 0, sizeof(act)); |
| 572 | act.sa_handler = sig_int; |
| 573 | act.sa_flags = SA_RESTART; |
| 574 | sigaction(SIGBREAK, &act, NULL); |
| 575 | #endif |
| 576 | |
| 577 | memset(&act, 0, sizeof(act)); |
| 578 | act.sa_handler = sig_show_status; |
| 579 | act.sa_flags = SA_RESTART; |
| 580 | sigaction(SIGUSR1, &act, NULL); |
| 581 | } |
| 582 | |
| 583 | static int send_client_cmd_line(struct fio_client *client) |
| 584 | { |
| 585 | struct cmd_single_line_pdu *cslp; |
| 586 | struct cmd_line_pdu *clp; |
| 587 | unsigned long offset; |
| 588 | unsigned int *lens; |
| 589 | void *pdu; |
| 590 | size_t mem; |
| 591 | int i, ret; |
| 592 | |
| 593 | dprint(FD_NET, "client: send cmdline %d\n", client->argc); |
| 594 | |
| 595 | lens = malloc(client->argc * sizeof(unsigned int)); |
| 596 | |
| 597 | /* |
| 598 | * Find out how much mem we need |
| 599 | */ |
| 600 | for (i = 0, mem = 0; i < client->argc; i++) { |
| 601 | lens[i] = strlen(client->argv[i]) + 1; |
| 602 | mem += lens[i]; |
| 603 | } |
| 604 | |
| 605 | /* |
| 606 | * We need one cmd_line_pdu, and argc number of cmd_single_line_pdu |
| 607 | */ |
| 608 | mem += sizeof(*clp) + (client->argc * sizeof(*cslp)); |
| 609 | |
| 610 | pdu = malloc(mem); |
| 611 | clp = pdu; |
| 612 | offset = sizeof(*clp); |
| 613 | |
| 614 | for (i = 0; i < client->argc; i++) { |
| 615 | uint16_t arg_len = lens[i]; |
| 616 | |
| 617 | cslp = pdu + offset; |
| 618 | strcpy((char *) cslp->text, client->argv[i]); |
| 619 | cslp->len = cpu_to_le16(arg_len); |
| 620 | offset += sizeof(*cslp) + arg_len; |
| 621 | } |
| 622 | |
| 623 | free(lens); |
| 624 | clp->lines = cpu_to_le16(client->argc); |
| 625 | clp->client_type = __cpu_to_le16(client->type); |
| 626 | ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOBLINE, pdu, mem, NULL, NULL); |
| 627 | free(pdu); |
| 628 | return ret; |
| 629 | } |
| 630 | |
| 631 | int fio_clients_connect(void) |
| 632 | { |
| 633 | struct fio_client *client; |
| 634 | struct flist_head *entry, *tmp; |
| 635 | int ret; |
| 636 | |
| 637 | #ifdef WIN32 |
| 638 | WSADATA wsd; |
| 639 | WSAStartup(MAKEWORD(2, 2), &wsd); |
| 640 | #endif |
| 641 | |
| 642 | dprint(FD_NET, "client: connect all\n"); |
| 643 | |
| 644 | client_signal_handler(); |
| 645 | |
| 646 | flist_for_each_safe(entry, tmp, &client_list) { |
| 647 | client = flist_entry(entry, struct fio_client, list); |
| 648 | |
| 649 | ret = fio_client_connect(client); |
| 650 | if (ret) { |
| 651 | remove_client(client); |
| 652 | continue; |
| 653 | } |
| 654 | |
| 655 | if (client->argc > 1) |
| 656 | send_client_cmd_line(client); |
| 657 | } |
| 658 | |
| 659 | return !nr_clients; |
| 660 | } |
| 661 | |
| 662 | int fio_start_client(struct fio_client *client) |
| 663 | { |
| 664 | dprint(FD_NET, "client: start %s\n", client->hostname); |
| 665 | return fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_RUN, 0, NULL); |
| 666 | } |
| 667 | |
| 668 | int fio_start_all_clients(void) |
| 669 | { |
| 670 | struct fio_client *client; |
| 671 | struct flist_head *entry, *tmp; |
| 672 | int ret; |
| 673 | |
| 674 | dprint(FD_NET, "client: start all\n"); |
| 675 | |
| 676 | fio_client_json_init(); |
| 677 | |
| 678 | flist_for_each_safe(entry, tmp, &client_list) { |
| 679 | client = flist_entry(entry, struct fio_client, list); |
| 680 | |
| 681 | ret = fio_start_client(client); |
| 682 | if (ret) { |
| 683 | remove_client(client); |
| 684 | continue; |
| 685 | } |
| 686 | } |
| 687 | |
| 688 | return flist_empty(&client_list); |
| 689 | } |
| 690 | |
| 691 | static int __fio_client_send_remote_ini(struct fio_client *client, |
| 692 | const char *filename) |
| 693 | { |
| 694 | struct cmd_load_file_pdu *pdu; |
| 695 | size_t p_size; |
| 696 | int ret; |
| 697 | |
| 698 | dprint(FD_NET, "send remote ini %s to %s\n", filename, client->hostname); |
| 699 | |
| 700 | p_size = sizeof(*pdu) + strlen(filename) + 1; |
| 701 | pdu = malloc(p_size); |
| 702 | memset(pdu, 0, p_size); |
| 703 | pdu->name_len = strlen(filename); |
| 704 | strcpy((char *) pdu->file, filename); |
| 705 | pdu->client_type = cpu_to_le16((uint16_t) client->type); |
| 706 | |
| 707 | client->sent_job = 1; |
| 708 | ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_LOAD_FILE, pdu, p_size,NULL, NULL); |
| 709 | free(pdu); |
| 710 | return ret; |
| 711 | } |
| 712 | |
| 713 | /* |
| 714 | * Send file contents to server backend. We could use sendfile(), but to remain |
| 715 | * more portable lets just read/write the darn thing. |
| 716 | */ |
| 717 | static int __fio_client_send_local_ini(struct fio_client *client, |
| 718 | const char *filename) |
| 719 | { |
| 720 | struct cmd_job_pdu *pdu; |
| 721 | size_t p_size; |
| 722 | struct stat sb; |
| 723 | char *p; |
| 724 | void *buf; |
| 725 | off_t len; |
| 726 | int fd, ret; |
| 727 | |
| 728 | dprint(FD_NET, "send ini %s to %s\n", filename, client->hostname); |
| 729 | |
| 730 | fd = open(filename, O_RDONLY); |
| 731 | if (fd < 0) { |
| 732 | ret = -errno; |
| 733 | log_err("fio: job file <%s> open: %s\n", filename, strerror(errno)); |
| 734 | return ret; |
| 735 | } |
| 736 | |
| 737 | if (fstat(fd, &sb) < 0) { |
| 738 | ret = -errno; |
| 739 | log_err("fio: job file stat: %s\n", strerror(errno)); |
| 740 | close(fd); |
| 741 | return ret; |
| 742 | } |
| 743 | |
| 744 | p_size = sb.st_size + sizeof(*pdu); |
| 745 | pdu = malloc(p_size); |
| 746 | buf = pdu->buf; |
| 747 | |
| 748 | len = sb.st_size; |
| 749 | p = buf; |
| 750 | if (read_data(fd, p, len)) { |
| 751 | log_err("fio: failed reading job file %s\n", filename); |
| 752 | close(fd); |
| 753 | free(pdu); |
| 754 | return 1; |
| 755 | } |
| 756 | |
| 757 | pdu->buf_len = __cpu_to_le32(sb.st_size); |
| 758 | pdu->client_type = cpu_to_le32(client->type); |
| 759 | |
| 760 | client->sent_job = 1; |
| 761 | ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, pdu, p_size, NULL, NULL); |
| 762 | free(pdu); |
| 763 | close(fd); |
| 764 | return ret; |
| 765 | } |
| 766 | |
| 767 | int fio_client_send_ini(struct fio_client *client, const char *filename, |
| 768 | int remote) |
| 769 | { |
| 770 | int ret; |
| 771 | |
| 772 | if (!remote) |
| 773 | ret = __fio_client_send_local_ini(client, filename); |
| 774 | else |
| 775 | ret = __fio_client_send_remote_ini(client, filename); |
| 776 | |
| 777 | if (!ret) |
| 778 | client->sent_job = 1; |
| 779 | |
| 780 | return ret; |
| 781 | } |
| 782 | |
| 783 | static int fio_client_send_cf(struct fio_client *client, |
| 784 | struct client_file *cf) |
| 785 | { |
| 786 | return fio_client_send_ini(client, cf->file, cf->remote); |
| 787 | } |
| 788 | |
| 789 | int fio_clients_send_ini(const char *filename) |
| 790 | { |
| 791 | struct fio_client *client; |
| 792 | struct flist_head *entry, *tmp; |
| 793 | |
| 794 | flist_for_each_safe(entry, tmp, &client_list) { |
| 795 | client = flist_entry(entry, struct fio_client, list); |
| 796 | |
| 797 | if (client->nr_files) { |
| 798 | int i; |
| 799 | |
| 800 | for (i = 0; i < client->nr_files; i++) { |
| 801 | struct client_file *cf; |
| 802 | |
| 803 | cf = &client->files[i]; |
| 804 | |
| 805 | if (fio_client_send_cf(client, cf)) { |
| 806 | remove_client(client); |
| 807 | break; |
| 808 | } |
| 809 | } |
| 810 | } |
| 811 | if (client->sent_job) |
| 812 | continue; |
| 813 | if (!filename || fio_client_send_ini(client, filename, 0)) |
| 814 | remove_client(client); |
| 815 | } |
| 816 | |
| 817 | return !nr_clients; |
| 818 | } |
| 819 | |
| 820 | int fio_client_update_options(struct fio_client *client, |
| 821 | struct thread_options *o, uint64_t *tag) |
| 822 | { |
| 823 | struct cmd_add_job_pdu pdu; |
| 824 | |
| 825 | pdu.thread_number = cpu_to_le32(client->thread_number); |
| 826 | pdu.groupid = cpu_to_le32(client->groupid); |
| 827 | convert_thread_options_to_net(&pdu.top, o); |
| 828 | |
| 829 | return fio_net_send_cmd(client->fd, FIO_NET_CMD_UPDATE_JOB, &pdu, sizeof(pdu), tag, &client->cmd_list); |
| 830 | } |
| 831 | |
| 832 | static void convert_io_stat(struct io_stat *dst, struct io_stat *src) |
| 833 | { |
| 834 | dst->max_val = le64_to_cpu(src->max_val); |
| 835 | dst->min_val = le64_to_cpu(src->min_val); |
| 836 | dst->samples = le64_to_cpu(src->samples); |
| 837 | |
| 838 | /* |
| 839 | * Floats arrive as IEEE 754 encoded uint64_t, convert back to double |
| 840 | */ |
| 841 | dst->mean.u.f = fio_uint64_to_double(le64_to_cpu(dst->mean.u.i)); |
| 842 | dst->S.u.f = fio_uint64_to_double(le64_to_cpu(dst->S.u.i)); |
| 843 | } |
| 844 | |
| 845 | static void convert_ts(struct thread_stat *dst, struct thread_stat *src) |
| 846 | { |
| 847 | int i, j; |
| 848 | |
| 849 | dst->error = le32_to_cpu(src->error); |
| 850 | dst->thread_number = le32_to_cpu(src->thread_number); |
| 851 | dst->groupid = le32_to_cpu(src->groupid); |
| 852 | dst->pid = le32_to_cpu(src->pid); |
| 853 | dst->members = le32_to_cpu(src->members); |
| 854 | dst->unified_rw_rep = le32_to_cpu(src->unified_rw_rep); |
| 855 | |
| 856 | for (i = 0; i < DDIR_RWDIR_CNT; i++) { |
| 857 | convert_io_stat(&dst->clat_stat[i], &src->clat_stat[i]); |
| 858 | convert_io_stat(&dst->slat_stat[i], &src->slat_stat[i]); |
| 859 | convert_io_stat(&dst->lat_stat[i], &src->lat_stat[i]); |
| 860 | convert_io_stat(&dst->bw_stat[i], &src->bw_stat[i]); |
| 861 | } |
| 862 | |
| 863 | dst->usr_time = le64_to_cpu(src->usr_time); |
| 864 | dst->sys_time = le64_to_cpu(src->sys_time); |
| 865 | dst->ctx = le64_to_cpu(src->ctx); |
| 866 | dst->minf = le64_to_cpu(src->minf); |
| 867 | dst->majf = le64_to_cpu(src->majf); |
| 868 | dst->clat_percentiles = le64_to_cpu(src->clat_percentiles); |
| 869 | dst->percentile_precision = le64_to_cpu(src->percentile_precision); |
| 870 | |
| 871 | for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) { |
| 872 | fio_fp64_t *fps = &src->percentile_list[i]; |
| 873 | fio_fp64_t *fpd = &dst->percentile_list[i]; |
| 874 | |
| 875 | fpd->u.f = fio_uint64_to_double(le64_to_cpu(fps->u.i)); |
| 876 | } |
| 877 | |
| 878 | for (i = 0; i < FIO_IO_U_MAP_NR; i++) { |
| 879 | dst->io_u_map[i] = le32_to_cpu(src->io_u_map[i]); |
| 880 | dst->io_u_submit[i] = le32_to_cpu(src->io_u_submit[i]); |
| 881 | dst->io_u_complete[i] = le32_to_cpu(src->io_u_complete[i]); |
| 882 | } |
| 883 | |
| 884 | for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) { |
| 885 | dst->io_u_lat_u[i] = le32_to_cpu(src->io_u_lat_u[i]); |
| 886 | dst->io_u_lat_m[i] = le32_to_cpu(src->io_u_lat_m[i]); |
| 887 | } |
| 888 | |
| 889 | for (i = 0; i < DDIR_RWDIR_CNT; i++) |
| 890 | for (j = 0; j < FIO_IO_U_PLAT_NR; j++) |
| 891 | dst->io_u_plat[i][j] = le32_to_cpu(src->io_u_plat[i][j]); |
| 892 | |
| 893 | for (i = 0; i < DDIR_RWDIR_CNT; i++) { |
| 894 | dst->total_io_u[i] = le64_to_cpu(src->total_io_u[i]); |
| 895 | dst->short_io_u[i] = le64_to_cpu(src->short_io_u[i]); |
| 896 | dst->drop_io_u[i] = le64_to_cpu(src->drop_io_u[i]); |
| 897 | } |
| 898 | |
| 899 | dst->total_submit = le64_to_cpu(src->total_submit); |
| 900 | dst->total_complete = le64_to_cpu(src->total_complete); |
| 901 | |
| 902 | for (i = 0; i < DDIR_RWDIR_CNT; i++) { |
| 903 | dst->io_bytes[i] = le64_to_cpu(src->io_bytes[i]); |
| 904 | dst->runtime[i] = le64_to_cpu(src->runtime[i]); |
| 905 | } |
| 906 | |
| 907 | dst->total_run_time = le64_to_cpu(src->total_run_time); |
| 908 | dst->continue_on_error = le16_to_cpu(src->continue_on_error); |
| 909 | dst->total_err_count = le64_to_cpu(src->total_err_count); |
| 910 | dst->first_error = le32_to_cpu(src->first_error); |
| 911 | dst->kb_base = le32_to_cpu(src->kb_base); |
| 912 | dst->unit_base = le32_to_cpu(src->unit_base); |
| 913 | |
| 914 | dst->latency_depth = le32_to_cpu(src->latency_depth); |
| 915 | dst->latency_target = le64_to_cpu(src->latency_target); |
| 916 | dst->latency_window = le64_to_cpu(src->latency_window); |
| 917 | dst->latency_percentile.u.f = fio_uint64_to_double(le64_to_cpu(src->latency_percentile.u.i)); |
| 918 | |
| 919 | dst->nr_block_infos = le64_to_cpu(src->nr_block_infos); |
| 920 | for (i = 0; i < dst->nr_block_infos; i++) |
| 921 | dst->block_infos[i] = le32_to_cpu(src->block_infos[i]); |
| 922 | } |
| 923 | |
| 924 | static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src) |
| 925 | { |
| 926 | int i; |
| 927 | |
| 928 | for (i = 0; i < DDIR_RWDIR_CNT; i++) { |
| 929 | dst->max_run[i] = le64_to_cpu(src->max_run[i]); |
| 930 | dst->min_run[i] = le64_to_cpu(src->min_run[i]); |
| 931 | dst->max_bw[i] = le64_to_cpu(src->max_bw[i]); |
| 932 | dst->min_bw[i] = le64_to_cpu(src->min_bw[i]); |
| 933 | dst->io_kb[i] = le64_to_cpu(src->io_kb[i]); |
| 934 | dst->agg[i] = le64_to_cpu(src->agg[i]); |
| 935 | } |
| 936 | |
| 937 | dst->kb_base = le32_to_cpu(src->kb_base); |
| 938 | dst->unit_base = le32_to_cpu(src->unit_base); |
| 939 | dst->groupid = le32_to_cpu(src->groupid); |
| 940 | dst->unified_rw_rep = le32_to_cpu(src->unified_rw_rep); |
| 941 | } |
| 942 | |
| 943 | static void json_object_add_client_info(struct json_object *obj, |
| 944 | struct fio_client *client) |
| 945 | { |
| 946 | const char *hostname = client->hostname ? client->hostname : ""; |
| 947 | |
| 948 | json_object_add_value_string(obj, "hostname", hostname); |
| 949 | json_object_add_value_int(obj, "port", client->port); |
| 950 | } |
| 951 | |
| 952 | static void handle_ts(struct fio_client *client, struct fio_net_cmd *cmd) |
| 953 | { |
| 954 | struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload; |
| 955 | struct flist_head *opt_list = NULL; |
| 956 | struct json_object *tsobj; |
| 957 | |
| 958 | if (client->opt_lists && p->ts.thread_number <= client->jobs) |
| 959 | opt_list = &client->opt_lists[p->ts.thread_number - 1]; |
| 960 | |
| 961 | tsobj = show_thread_status(&p->ts, &p->rs, opt_list, NULL); |
| 962 | client->did_stat = 1; |
| 963 | if (tsobj) { |
| 964 | json_object_add_client_info(tsobj, client); |
| 965 | json_array_add_value_object(clients_array, tsobj); |
| 966 | } |
| 967 | |
| 968 | if (sum_stat_clients <= 1) |
| 969 | return; |
| 970 | |
| 971 | sum_thread_stats(&client_ts, &p->ts, sum_stat_nr == 1); |
| 972 | sum_group_stats(&client_gs, &p->rs); |
| 973 | |
| 974 | client_ts.members++; |
| 975 | client_ts.thread_number = p->ts.thread_number; |
| 976 | client_ts.groupid = p->ts.groupid; |
| 977 | client_ts.unified_rw_rep = p->ts.unified_rw_rep; |
| 978 | |
| 979 | if (++sum_stat_nr == sum_stat_clients) { |
| 980 | strcpy(client_ts.name, "All clients"); |
| 981 | tsobj = show_thread_status(&client_ts, &client_gs, NULL, NULL); |
| 982 | if (tsobj) { |
| 983 | json_object_add_client_info(tsobj, client); |
| 984 | json_array_add_value_object(clients_array, tsobj); |
| 985 | } |
| 986 | } |
| 987 | } |
| 988 | |
| 989 | static void handle_gs(struct fio_client *client, struct fio_net_cmd *cmd) |
| 990 | { |
| 991 | struct group_run_stats *gs = (struct group_run_stats *) cmd->payload; |
| 992 | |
| 993 | if (output_format & FIO_OUTPUT_NORMAL) |
| 994 | show_group_stats(gs, NULL); |
| 995 | } |
| 996 | |
| 997 | static void handle_job_opt(struct fio_client *client, struct fio_net_cmd *cmd) |
| 998 | { |
| 999 | struct cmd_job_option *pdu = (struct cmd_job_option *) cmd->payload; |
| 1000 | struct print_option *p; |
| 1001 | |
| 1002 | if (!job_opt_object) |
| 1003 | return; |
| 1004 | |
| 1005 | pdu->global = le16_to_cpu(pdu->global); |
| 1006 | pdu->truncated = le16_to_cpu(pdu->truncated); |
| 1007 | pdu->groupid = le32_to_cpu(pdu->groupid); |
| 1008 | |
| 1009 | p = malloc(sizeof(*p)); |
| 1010 | p->name = strdup((char *) pdu->name); |
| 1011 | if (pdu->value[0] != '\0') |
| 1012 | p->value = strdup((char *) pdu->value); |
| 1013 | else |
| 1014 | p->value = NULL; |
| 1015 | |
| 1016 | if (pdu->global) { |
| 1017 | const char *pos = ""; |
| 1018 | |
| 1019 | if (p->value) |
| 1020 | pos = p->value; |
| 1021 | |
| 1022 | json_object_add_value_string(job_opt_object, p->name, pos); |
| 1023 | } else if (client->opt_lists) { |
| 1024 | struct flist_head *opt_list = &client->opt_lists[pdu->groupid]; |
| 1025 | |
| 1026 | flist_add_tail(&p->list, opt_list); |
| 1027 | } |
| 1028 | } |
| 1029 | |
| 1030 | static void handle_text(struct fio_client *client, struct fio_net_cmd *cmd) |
| 1031 | { |
| 1032 | struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload; |
| 1033 | const char *buf = (const char *) pdu->buf; |
| 1034 | const char *name; |
| 1035 | int fio_unused ret; |
| 1036 | |
| 1037 | name = client->name ? client->name : client->hostname; |
| 1038 | |
| 1039 | if (!client->skip_newline) |
| 1040 | fprintf(f_out, "<%s> ", name); |
| 1041 | ret = fwrite(buf, pdu->buf_len, 1, f_out); |
| 1042 | fflush(f_out); |
| 1043 | client->skip_newline = strchr(buf, '\n') == NULL; |
| 1044 | } |
| 1045 | |
| 1046 | static void convert_agg(struct disk_util_agg *agg) |
| 1047 | { |
| 1048 | int i; |
| 1049 | |
| 1050 | for (i = 0; i < 2; i++) { |
| 1051 | agg->ios[i] = le64_to_cpu(agg->ios[i]); |
| 1052 | agg->merges[i] = le64_to_cpu(agg->merges[i]); |
| 1053 | agg->sectors[i] = le64_to_cpu(agg->sectors[i]); |
| 1054 | agg->ticks[i] = le64_to_cpu(agg->ticks[i]); |
| 1055 | } |
| 1056 | |
| 1057 | agg->io_ticks = le64_to_cpu(agg->io_ticks); |
| 1058 | agg->time_in_queue = le64_to_cpu(agg->time_in_queue); |
| 1059 | agg->slavecount = le32_to_cpu(agg->slavecount); |
| 1060 | agg->max_util.u.f = fio_uint64_to_double(le64_to_cpu(agg->max_util.u.i)); |
| 1061 | } |
| 1062 | |
| 1063 | static void convert_dus(struct disk_util_stat *dus) |
| 1064 | { |
| 1065 | int i; |
| 1066 | |
| 1067 | for (i = 0; i < 2; i++) { |
| 1068 | dus->s.ios[i] = le64_to_cpu(dus->s.ios[i]); |
| 1069 | dus->s.merges[i] = le64_to_cpu(dus->s.merges[i]); |
| 1070 | dus->s.sectors[i] = le64_to_cpu(dus->s.sectors[i]); |
| 1071 | dus->s.ticks[i] = le64_to_cpu(dus->s.ticks[i]); |
| 1072 | } |
| 1073 | |
| 1074 | dus->s.io_ticks = le64_to_cpu(dus->s.io_ticks); |
| 1075 | dus->s.time_in_queue = le64_to_cpu(dus->s.time_in_queue); |
| 1076 | dus->s.msec = le64_to_cpu(dus->s.msec); |
| 1077 | } |
| 1078 | |
| 1079 | static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd) |
| 1080 | { |
| 1081 | struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload; |
| 1082 | |
| 1083 | if (!client->disk_stats_shown) { |
| 1084 | client->disk_stats_shown = 1; |
| 1085 | log_info("\nDisk stats (read/write):\n"); |
| 1086 | } |
| 1087 | |
| 1088 | if (output_format & FIO_OUTPUT_JSON) { |
| 1089 | struct json_object *duobj; |
| 1090 | json_array_add_disk_util(&du->dus, &du->agg, du_array); |
| 1091 | duobj = json_array_last_value_object(du_array); |
| 1092 | json_object_add_client_info(duobj, client); |
| 1093 | } |
| 1094 | if (output_format & FIO_OUTPUT_TERSE) |
| 1095 | print_disk_util(&du->dus, &du->agg, 1, NULL); |
| 1096 | if (output_format & FIO_OUTPUT_NORMAL) |
| 1097 | print_disk_util(&du->dus, &du->agg, 0, NULL); |
| 1098 | } |
| 1099 | |
| 1100 | static void convert_jobs_eta(struct jobs_eta *je) |
| 1101 | { |
| 1102 | int i; |
| 1103 | |
| 1104 | je->nr_running = le32_to_cpu(je->nr_running); |
| 1105 | je->nr_ramp = le32_to_cpu(je->nr_ramp); |
| 1106 | je->nr_pending = le32_to_cpu(je->nr_pending); |
| 1107 | je->nr_setting_up = le32_to_cpu(je->nr_setting_up); |
| 1108 | je->files_open = le32_to_cpu(je->files_open); |
| 1109 | |
| 1110 | for (i = 0; i < DDIR_RWDIR_CNT; i++) { |
| 1111 | je->m_rate[i] = le32_to_cpu(je->m_rate[i]); |
| 1112 | je->t_rate[i] = le32_to_cpu(je->t_rate[i]); |
| 1113 | je->m_iops[i] = le32_to_cpu(je->m_iops[i]); |
| 1114 | je->t_iops[i] = le32_to_cpu(je->t_iops[i]); |
| 1115 | je->rate[i] = le32_to_cpu(je->rate[i]); |
| 1116 | je->iops[i] = le32_to_cpu(je->iops[i]); |
| 1117 | } |
| 1118 | |
| 1119 | je->elapsed_sec = le64_to_cpu(je->elapsed_sec); |
| 1120 | je->eta_sec = le64_to_cpu(je->eta_sec); |
| 1121 | je->nr_threads = le32_to_cpu(je->nr_threads); |
| 1122 | je->is_pow2 = le32_to_cpu(je->is_pow2); |
| 1123 | je->unit_base = le32_to_cpu(je->unit_base); |
| 1124 | } |
| 1125 | |
| 1126 | void fio_client_sum_jobs_eta(struct jobs_eta *dst, struct jobs_eta *je) |
| 1127 | { |
| 1128 | int i; |
| 1129 | |
| 1130 | dst->nr_running += je->nr_running; |
| 1131 | dst->nr_ramp += je->nr_ramp; |
| 1132 | dst->nr_pending += je->nr_pending; |
| 1133 | dst->nr_setting_up += je->nr_setting_up; |
| 1134 | dst->files_open += je->files_open; |
| 1135 | |
| 1136 | for (i = 0; i < DDIR_RWDIR_CNT; i++) { |
| 1137 | dst->m_rate[i] += je->m_rate[i]; |
| 1138 | dst->t_rate[i] += je->t_rate[i]; |
| 1139 | dst->m_iops[i] += je->m_iops[i]; |
| 1140 | dst->t_iops[i] += je->t_iops[i]; |
| 1141 | dst->rate[i] += je->rate[i]; |
| 1142 | dst->iops[i] += je->iops[i]; |
| 1143 | } |
| 1144 | |
| 1145 | dst->elapsed_sec += je->elapsed_sec; |
| 1146 | |
| 1147 | if (je->eta_sec > dst->eta_sec) |
| 1148 | dst->eta_sec = je->eta_sec; |
| 1149 | |
| 1150 | dst->nr_threads += je->nr_threads; |
| 1151 | |
| 1152 | /* |
| 1153 | * This wont be correct for multiple strings, but at least it |
| 1154 | * works for the basic cases. |
| 1155 | */ |
| 1156 | strcpy((char *) dst->run_str, (char *) je->run_str); |
| 1157 | } |
| 1158 | |
| 1159 | static void remove_reply_cmd(struct fio_client *client, struct fio_net_cmd *cmd) |
| 1160 | { |
| 1161 | struct fio_net_cmd_reply *reply = NULL; |
| 1162 | struct flist_head *entry; |
| 1163 | |
| 1164 | flist_for_each(entry, &client->cmd_list) { |
| 1165 | reply = flist_entry(entry, struct fio_net_cmd_reply, list); |
| 1166 | |
| 1167 | if (cmd->tag == (uintptr_t) reply) |
| 1168 | break; |
| 1169 | |
| 1170 | reply = NULL; |
| 1171 | } |
| 1172 | |
| 1173 | if (!reply) { |
| 1174 | log_err("fio: client: unable to find matching tag (%llx)\n", (unsigned long long) cmd->tag); |
| 1175 | return; |
| 1176 | } |
| 1177 | |
| 1178 | flist_del(&reply->list); |
| 1179 | cmd->tag = reply->saved_tag; |
| 1180 | free(reply); |
| 1181 | } |
| 1182 | |
| 1183 | int fio_client_wait_for_reply(struct fio_client *client, uint64_t tag) |
| 1184 | { |
| 1185 | do { |
| 1186 | struct fio_net_cmd_reply *reply = NULL; |
| 1187 | struct flist_head *entry; |
| 1188 | |
| 1189 | flist_for_each(entry, &client->cmd_list) { |
| 1190 | reply = flist_entry(entry, struct fio_net_cmd_reply, list); |
| 1191 | |
| 1192 | if (tag == (uintptr_t) reply) |
| 1193 | break; |
| 1194 | |
| 1195 | reply = NULL; |
| 1196 | } |
| 1197 | |
| 1198 | if (!reply) |
| 1199 | break; |
| 1200 | |
| 1201 | usleep(1000); |
| 1202 | } while (1); |
| 1203 | |
| 1204 | return 0; |
| 1205 | } |
| 1206 | |
| 1207 | static void handle_eta(struct fio_client *client, struct fio_net_cmd *cmd) |
| 1208 | { |
| 1209 | struct jobs_eta *je = (struct jobs_eta *) cmd->payload; |
| 1210 | struct client_eta *eta = (struct client_eta *) (uintptr_t) cmd->tag; |
| 1211 | |
| 1212 | dprint(FD_NET, "client: got eta tag %p, %d\n", eta, eta->pending); |
| 1213 | |
| 1214 | assert(client->eta_in_flight == eta); |
| 1215 | |
| 1216 | client->eta_in_flight = NULL; |
| 1217 | flist_del_init(&client->eta_list); |
| 1218 | client->eta_timeouts = 0; |
| 1219 | |
| 1220 | if (client->ops->jobs_eta) |
| 1221 | client->ops->jobs_eta(client, je); |
| 1222 | |
| 1223 | fio_client_sum_jobs_eta(&eta->eta, je); |
| 1224 | fio_client_dec_jobs_eta(eta, client->ops->eta); |
| 1225 | } |
| 1226 | |
| 1227 | static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd) |
| 1228 | { |
| 1229 | struct cmd_probe_reply_pdu *probe = (struct cmd_probe_reply_pdu *) cmd->payload; |
| 1230 | const char *os, *arch; |
| 1231 | char bit[16]; |
| 1232 | |
| 1233 | os = fio_get_os_string(probe->os); |
| 1234 | if (!os) |
| 1235 | os = "unknown"; |
| 1236 | |
| 1237 | arch = fio_get_arch_string(probe->arch); |
| 1238 | if (!arch) |
| 1239 | os = "unknown"; |
| 1240 | |
| 1241 | sprintf(bit, "%d-bit", probe->bpp * 8); |
| 1242 | probe->flags = le64_to_cpu(probe->flags); |
| 1243 | |
| 1244 | log_info("hostname=%s, be=%u, %s, os=%s, arch=%s, fio=%s, flags=%lx\n", |
| 1245 | probe->hostname, probe->bigendian, bit, os, arch, |
| 1246 | probe->fio_version, (unsigned long) probe->flags); |
| 1247 | |
| 1248 | if (!client->name) |
| 1249 | client->name = strdup((char *) probe->hostname); |
| 1250 | } |
| 1251 | |
| 1252 | static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd) |
| 1253 | { |
| 1254 | struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload; |
| 1255 | |
| 1256 | client->state = Client_started; |
| 1257 | client->jobs = le32_to_cpu(pdu->jobs); |
| 1258 | client->nr_stat = le32_to_cpu(pdu->stat_outputs); |
| 1259 | |
| 1260 | if (client->jobs) { |
| 1261 | int i; |
| 1262 | |
| 1263 | if (client->opt_lists) |
| 1264 | free(client->opt_lists); |
| 1265 | |
| 1266 | client->opt_lists = malloc(client->jobs * sizeof(struct flist_head)); |
| 1267 | for (i = 0; i < client->jobs; i++) |
| 1268 | INIT_FLIST_HEAD(&client->opt_lists[i]); |
| 1269 | } |
| 1270 | |
| 1271 | sum_stat_clients += client->nr_stat; |
| 1272 | } |
| 1273 | |
| 1274 | static void handle_stop(struct fio_client *client, struct fio_net_cmd *cmd) |
| 1275 | { |
| 1276 | if (client->error) |
| 1277 | log_info("client <%s>: exited with error %d\n", client->hostname, client->error); |
| 1278 | } |
| 1279 | |
| 1280 | static void convert_stop(struct fio_net_cmd *cmd) |
| 1281 | { |
| 1282 | struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload; |
| 1283 | |
| 1284 | pdu->error = le32_to_cpu(pdu->error); |
| 1285 | } |
| 1286 | |
| 1287 | static void convert_text(struct fio_net_cmd *cmd) |
| 1288 | { |
| 1289 | struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload; |
| 1290 | |
| 1291 | pdu->level = le32_to_cpu(pdu->level); |
| 1292 | pdu->buf_len = le32_to_cpu(pdu->buf_len); |
| 1293 | pdu->log_sec = le64_to_cpu(pdu->log_sec); |
| 1294 | pdu->log_usec = le64_to_cpu(pdu->log_usec); |
| 1295 | } |
| 1296 | |
| 1297 | static struct cmd_iolog_pdu *convert_iolog_gz(struct fio_net_cmd *cmd, |
| 1298 | struct cmd_iolog_pdu *pdu) |
| 1299 | { |
| 1300 | #ifdef CONFIG_ZLIB |
| 1301 | struct cmd_iolog_pdu *ret; |
| 1302 | z_stream stream; |
| 1303 | uint32_t nr_samples; |
| 1304 | size_t total; |
| 1305 | void *p; |
| 1306 | |
| 1307 | stream.zalloc = Z_NULL; |
| 1308 | stream.zfree = Z_NULL; |
| 1309 | stream.opaque = Z_NULL; |
| 1310 | stream.avail_in = 0; |
| 1311 | stream.next_in = Z_NULL; |
| 1312 | |
| 1313 | if (inflateInit(&stream) != Z_OK) |
| 1314 | return NULL; |
| 1315 | |
| 1316 | /* |
| 1317 | * Get header first, it's not compressed |
| 1318 | */ |
| 1319 | nr_samples = le64_to_cpu(pdu->nr_samples); |
| 1320 | |
| 1321 | total = nr_samples * __log_entry_sz(le32_to_cpu(pdu->log_offset)); |
| 1322 | ret = malloc(total + sizeof(*pdu)); |
| 1323 | ret->nr_samples = nr_samples; |
| 1324 | |
| 1325 | memcpy(ret, pdu, sizeof(*pdu)); |
| 1326 | |
| 1327 | p = (void *) ret + sizeof(*pdu); |
| 1328 | |
| 1329 | stream.avail_in = cmd->pdu_len - sizeof(*pdu); |
| 1330 | stream.next_in = (void *) pdu + sizeof(*pdu); |
| 1331 | while (stream.avail_in) { |
| 1332 | unsigned int this_chunk = 65536; |
| 1333 | unsigned int this_len; |
| 1334 | int err; |
| 1335 | |
| 1336 | if (this_chunk > total) |
| 1337 | this_chunk = total; |
| 1338 | |
| 1339 | stream.avail_out = this_chunk; |
| 1340 | stream.next_out = p; |
| 1341 | err = inflate(&stream, Z_NO_FLUSH); |
| 1342 | /* may be Z_OK, or Z_STREAM_END */ |
| 1343 | if (err < 0) { |
| 1344 | log_err("fio: inflate error %d\n", err); |
| 1345 | free(ret); |
| 1346 | ret = NULL; |
| 1347 | goto err; |
| 1348 | } |
| 1349 | |
| 1350 | this_len = this_chunk - stream.avail_out; |
| 1351 | p += this_len; |
| 1352 | total -= this_len; |
| 1353 | } |
| 1354 | |
| 1355 | err: |
| 1356 | inflateEnd(&stream); |
| 1357 | return ret; |
| 1358 | #else |
| 1359 | return NULL; |
| 1360 | #endif |
| 1361 | } |
| 1362 | |
| 1363 | /* |
| 1364 | * This has been compressed on the server side, since it can be big. |
| 1365 | * Uncompress here. |
| 1366 | */ |
| 1367 | static struct cmd_iolog_pdu *convert_iolog(struct fio_net_cmd *cmd) |
| 1368 | { |
| 1369 | struct cmd_iolog_pdu *pdu = (struct cmd_iolog_pdu *) cmd->payload; |
| 1370 | struct cmd_iolog_pdu *ret; |
| 1371 | uint64_t i; |
| 1372 | void *samples; |
| 1373 | |
| 1374 | /* |
| 1375 | * Convert if compressed and we support it. If it's not |
| 1376 | * compressed, we need not do anything. |
| 1377 | */ |
| 1378 | if (le32_to_cpu(pdu->compressed)) { |
| 1379 | #ifndef CONFIG_ZLIB |
| 1380 | log_err("fio: server sent compressed data by mistake\n"); |
| 1381 | return NULL; |
| 1382 | #endif |
| 1383 | ret = convert_iolog_gz(cmd, pdu); |
| 1384 | if (!ret) { |
| 1385 | log_err("fio: failed decompressing log\n"); |
| 1386 | return NULL; |
| 1387 | } |
| 1388 | } else |
| 1389 | ret = pdu; |
| 1390 | |
| 1391 | ret->nr_samples = le64_to_cpu(ret->nr_samples); |
| 1392 | ret->thread_number = le32_to_cpu(ret->thread_number); |
| 1393 | ret->log_type = le32_to_cpu(ret->log_type); |
| 1394 | ret->compressed = le32_to_cpu(ret->compressed); |
| 1395 | ret->log_offset = le32_to_cpu(ret->log_offset); |
| 1396 | |
| 1397 | samples = &ret->samples[0]; |
| 1398 | for (i = 0; i < ret->nr_samples; i++) { |
| 1399 | struct io_sample *s; |
| 1400 | |
| 1401 | s = __get_sample(samples, ret->log_offset, i); |
| 1402 | s->time = le64_to_cpu(s->time); |
| 1403 | s->val = le64_to_cpu(s->val); |
| 1404 | s->__ddir = le32_to_cpu(s->__ddir); |
| 1405 | s->bs = le32_to_cpu(s->bs); |
| 1406 | |
| 1407 | if (ret->log_offset) { |
| 1408 | struct io_sample_offset *so = (void *) s; |
| 1409 | |
| 1410 | so->offset = le64_to_cpu(so->offset); |
| 1411 | } |
| 1412 | } |
| 1413 | |
| 1414 | return ret; |
| 1415 | } |
| 1416 | |
| 1417 | static void sendfile_reply(int fd, struct cmd_sendfile_reply *rep, |
| 1418 | size_t size, uint64_t tag) |
| 1419 | { |
| 1420 | rep->error = cpu_to_le32(rep->error); |
| 1421 | fio_net_send_cmd(fd, FIO_NET_CMD_SENDFILE, rep, size, &tag, NULL); |
| 1422 | } |
| 1423 | |
| 1424 | static int send_file(struct fio_client *client, struct cmd_sendfile *pdu, |
| 1425 | uint64_t tag) |
| 1426 | { |
| 1427 | struct cmd_sendfile_reply *rep; |
| 1428 | struct stat sb; |
| 1429 | size_t size; |
| 1430 | int fd; |
| 1431 | |
| 1432 | size = sizeof(*rep); |
| 1433 | rep = malloc(size); |
| 1434 | |
| 1435 | if (stat((char *)pdu->path, &sb) < 0) { |
| 1436 | fail: |
| 1437 | rep->error = errno; |
| 1438 | sendfile_reply(client->fd, rep, size, tag); |
| 1439 | free(rep); |
| 1440 | return 1; |
| 1441 | } |
| 1442 | |
| 1443 | size += sb.st_size; |
| 1444 | rep = realloc(rep, size); |
| 1445 | rep->size = cpu_to_le32((uint32_t) sb.st_size); |
| 1446 | |
| 1447 | fd = open((char *)pdu->path, O_RDONLY); |
| 1448 | if (fd == -1 ) |
| 1449 | goto fail; |
| 1450 | |
| 1451 | rep->error = read_data(fd, &rep->data, sb.st_size); |
| 1452 | sendfile_reply(client->fd, rep, size, tag); |
| 1453 | free(rep); |
| 1454 | close(fd); |
| 1455 | return 0; |
| 1456 | } |
| 1457 | |
| 1458 | int fio_handle_client(struct fio_client *client) |
| 1459 | { |
| 1460 | struct client_ops *ops = client->ops; |
| 1461 | struct fio_net_cmd *cmd; |
| 1462 | |
| 1463 | dprint(FD_NET, "client: handle %s\n", client->hostname); |
| 1464 | |
| 1465 | cmd = fio_net_recv_cmd(client->fd); |
| 1466 | if (!cmd) |
| 1467 | return 0; |
| 1468 | |
| 1469 | dprint(FD_NET, "client: got cmd op %s from %s (pdu=%u)\n", |
| 1470 | fio_server_op(cmd->opcode), client->hostname, cmd->pdu_len); |
| 1471 | |
| 1472 | switch (cmd->opcode) { |
| 1473 | case FIO_NET_CMD_QUIT: |
| 1474 | if (ops->quit) |
| 1475 | ops->quit(client, cmd); |
| 1476 | remove_client(client); |
| 1477 | break; |
| 1478 | case FIO_NET_CMD_TEXT: |
| 1479 | convert_text(cmd); |
| 1480 | ops->text(client, cmd); |
| 1481 | break; |
| 1482 | case FIO_NET_CMD_DU: { |
| 1483 | struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload; |
| 1484 | |
| 1485 | convert_dus(&du->dus); |
| 1486 | convert_agg(&du->agg); |
| 1487 | |
| 1488 | ops->disk_util(client, cmd); |
| 1489 | break; |
| 1490 | } |
| 1491 | case FIO_NET_CMD_TS: { |
| 1492 | struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload; |
| 1493 | |
| 1494 | convert_ts(&p->ts, &p->ts); |
| 1495 | convert_gs(&p->rs, &p->rs); |
| 1496 | |
| 1497 | ops->thread_status(client, cmd); |
| 1498 | break; |
| 1499 | } |
| 1500 | case FIO_NET_CMD_GS: { |
| 1501 | struct group_run_stats *gs = (struct group_run_stats *) cmd->payload; |
| 1502 | |
| 1503 | convert_gs(gs, gs); |
| 1504 | |
| 1505 | ops->group_stats(client, cmd); |
| 1506 | break; |
| 1507 | } |
| 1508 | case FIO_NET_CMD_ETA: { |
| 1509 | struct jobs_eta *je = (struct jobs_eta *) cmd->payload; |
| 1510 | |
| 1511 | remove_reply_cmd(client, cmd); |
| 1512 | convert_jobs_eta(je); |
| 1513 | handle_eta(client, cmd); |
| 1514 | break; |
| 1515 | } |
| 1516 | case FIO_NET_CMD_PROBE: |
| 1517 | remove_reply_cmd(client, cmd); |
| 1518 | ops->probe(client, cmd); |
| 1519 | break; |
| 1520 | case FIO_NET_CMD_SERVER_START: |
| 1521 | client->state = Client_running; |
| 1522 | if (ops->job_start) |
| 1523 | ops->job_start(client, cmd); |
| 1524 | break; |
| 1525 | case FIO_NET_CMD_START: { |
| 1526 | struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload; |
| 1527 | |
| 1528 | pdu->jobs = le32_to_cpu(pdu->jobs); |
| 1529 | ops->start(client, cmd); |
| 1530 | break; |
| 1531 | } |
| 1532 | case FIO_NET_CMD_STOP: { |
| 1533 | struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload; |
| 1534 | |
| 1535 | convert_stop(cmd); |
| 1536 | client->state = Client_stopped; |
| 1537 | client->error = le32_to_cpu(pdu->error); |
| 1538 | client->signal = le32_to_cpu(pdu->signal); |
| 1539 | ops->stop(client, cmd); |
| 1540 | break; |
| 1541 | } |
| 1542 | case FIO_NET_CMD_ADD_JOB: { |
| 1543 | struct cmd_add_job_pdu *pdu = (struct cmd_add_job_pdu *) cmd->payload; |
| 1544 | |
| 1545 | client->thread_number = le32_to_cpu(pdu->thread_number); |
| 1546 | client->groupid = le32_to_cpu(pdu->groupid); |
| 1547 | |
| 1548 | if (ops->add_job) |
| 1549 | ops->add_job(client, cmd); |
| 1550 | break; |
| 1551 | } |
| 1552 | case FIO_NET_CMD_IOLOG: |
| 1553 | if (ops->iolog) { |
| 1554 | struct cmd_iolog_pdu *pdu; |
| 1555 | |
| 1556 | pdu = convert_iolog(cmd); |
| 1557 | ops->iolog(client, pdu); |
| 1558 | } |
| 1559 | break; |
| 1560 | case FIO_NET_CMD_UPDATE_JOB: |
| 1561 | ops->update_job(client, cmd); |
| 1562 | remove_reply_cmd(client, cmd); |
| 1563 | break; |
| 1564 | case FIO_NET_CMD_VTRIGGER: { |
| 1565 | struct all_io_list *pdu = (struct all_io_list *) cmd->payload; |
| 1566 | char buf[128]; |
| 1567 | int off = 0; |
| 1568 | |
| 1569 | if (aux_path) { |
| 1570 | strcpy(buf, aux_path); |
| 1571 | off = strlen(buf); |
| 1572 | } |
| 1573 | |
| 1574 | __verify_save_state(pdu, server_name(client, &buf[off], sizeof(buf) - off)); |
| 1575 | exec_trigger(trigger_cmd); |
| 1576 | break; |
| 1577 | } |
| 1578 | case FIO_NET_CMD_SENDFILE: { |
| 1579 | struct cmd_sendfile *pdu = (struct cmd_sendfile *) cmd->payload; |
| 1580 | send_file(client, pdu, cmd->tag); |
| 1581 | break; |
| 1582 | } |
| 1583 | case FIO_NET_CMD_JOB_OPT: { |
| 1584 | handle_job_opt(client, cmd); |
| 1585 | break; |
| 1586 | } |
| 1587 | default: |
| 1588 | log_err("fio: unknown client op: %s\n", fio_server_op(cmd->opcode)); |
| 1589 | break; |
| 1590 | } |
| 1591 | |
| 1592 | free(cmd); |
| 1593 | return 1; |
| 1594 | } |
| 1595 | |
| 1596 | int fio_clients_send_trigger(const char *cmd) |
| 1597 | { |
| 1598 | struct flist_head *entry; |
| 1599 | struct fio_client *client; |
| 1600 | size_t slen; |
| 1601 | |
| 1602 | dprint(FD_NET, "client: send vtrigger: %s\n", cmd); |
| 1603 | |
| 1604 | if (!cmd) |
| 1605 | slen = 0; |
| 1606 | else |
| 1607 | slen = strlen(cmd); |
| 1608 | |
| 1609 | flist_for_each(entry, &client_list) { |
| 1610 | struct cmd_vtrigger_pdu *pdu; |
| 1611 | |
| 1612 | client = flist_entry(entry, struct fio_client, list); |
| 1613 | |
| 1614 | pdu = malloc(sizeof(*pdu) + slen); |
| 1615 | pdu->len = cpu_to_le16((uint16_t) slen); |
| 1616 | if (slen) |
| 1617 | memcpy(pdu->cmd, cmd, slen); |
| 1618 | fio_net_send_cmd(client->fd, FIO_NET_CMD_VTRIGGER, pdu, |
| 1619 | sizeof(*pdu) + slen, NULL, NULL); |
| 1620 | free(pdu); |
| 1621 | } |
| 1622 | |
| 1623 | return 0; |
| 1624 | } |
| 1625 | |
| 1626 | static void request_client_etas(struct client_ops *ops) |
| 1627 | { |
| 1628 | struct fio_client *client; |
| 1629 | struct flist_head *entry; |
| 1630 | struct client_eta *eta; |
| 1631 | int skipped = 0; |
| 1632 | |
| 1633 | dprint(FD_NET, "client: request eta (%d)\n", nr_clients); |
| 1634 | |
| 1635 | eta = calloc(1, sizeof(*eta) + __THREAD_RUNSTR_SZ(REAL_MAX_JOBS)); |
| 1636 | eta->pending = nr_clients; |
| 1637 | |
| 1638 | flist_for_each(entry, &client_list) { |
| 1639 | client = flist_entry(entry, struct fio_client, list); |
| 1640 | |
| 1641 | if (!flist_empty(&client->eta_list)) { |
| 1642 | skipped++; |
| 1643 | continue; |
| 1644 | } |
| 1645 | if (client->state != Client_running) |
| 1646 | continue; |
| 1647 | |
| 1648 | assert(!client->eta_in_flight); |
| 1649 | flist_add_tail(&client->eta_list, &eta_list); |
| 1650 | client->eta_in_flight = eta; |
| 1651 | fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_SEND_ETA, |
| 1652 | (uintptr_t) eta, &client->cmd_list); |
| 1653 | } |
| 1654 | |
| 1655 | while (skipped--) { |
| 1656 | if (!fio_client_dec_jobs_eta(eta, ops->eta)) |
| 1657 | break; |
| 1658 | } |
| 1659 | |
| 1660 | dprint(FD_NET, "client: requested eta tag %p\n", eta); |
| 1661 | } |
| 1662 | |
| 1663 | /* |
| 1664 | * A single SEND_ETA timeout isn't fatal. Attempt to recover. |
| 1665 | */ |
| 1666 | static int handle_cmd_timeout(struct fio_client *client, |
| 1667 | struct fio_net_cmd_reply *reply) |
| 1668 | { |
| 1669 | flist_del(&reply->list); |
| 1670 | free(reply); |
| 1671 | |
| 1672 | if (reply->opcode != FIO_NET_CMD_SEND_ETA) |
| 1673 | return 1; |
| 1674 | |
| 1675 | log_info("client <%s>: timeout on SEND_ETA\n", client->hostname); |
| 1676 | |
| 1677 | flist_del_init(&client->eta_list); |
| 1678 | if (client->eta_in_flight) { |
| 1679 | fio_client_dec_jobs_eta(client->eta_in_flight, client->ops->eta); |
| 1680 | client->eta_in_flight = NULL; |
| 1681 | } |
| 1682 | |
| 1683 | /* |
| 1684 | * If we fail 5 in a row, give up... |
| 1685 | */ |
| 1686 | if (client->eta_timeouts++ > 5) |
| 1687 | return 1; |
| 1688 | |
| 1689 | return 0; |
| 1690 | } |
| 1691 | |
| 1692 | static int client_check_cmd_timeout(struct fio_client *client, |
| 1693 | struct timeval *now) |
| 1694 | { |
| 1695 | struct fio_net_cmd_reply *reply; |
| 1696 | struct flist_head *entry, *tmp; |
| 1697 | int ret = 0; |
| 1698 | |
| 1699 | flist_for_each_safe(entry, tmp, &client->cmd_list) { |
| 1700 | reply = flist_entry(entry, struct fio_net_cmd_reply, list); |
| 1701 | |
| 1702 | if (mtime_since(&reply->tv, now) < FIO_NET_CLIENT_TIMEOUT) |
| 1703 | continue; |
| 1704 | |
| 1705 | if (!handle_cmd_timeout(client, reply)) |
| 1706 | continue; |
| 1707 | |
| 1708 | log_err("fio: client %s, timeout on cmd %s\n", client->hostname, |
| 1709 | fio_server_op(reply->opcode)); |
| 1710 | ret = 1; |
| 1711 | } |
| 1712 | |
| 1713 | return flist_empty(&client->cmd_list) && ret; |
| 1714 | } |
| 1715 | |
| 1716 | static int fio_check_clients_timed_out(void) |
| 1717 | { |
| 1718 | struct fio_client *client; |
| 1719 | struct flist_head *entry, *tmp; |
| 1720 | struct timeval tv; |
| 1721 | int ret = 0; |
| 1722 | |
| 1723 | fio_gettime(&tv, NULL); |
| 1724 | |
| 1725 | flist_for_each_safe(entry, tmp, &client_list) { |
| 1726 | client = flist_entry(entry, struct fio_client, list); |
| 1727 | |
| 1728 | if (flist_empty(&client->cmd_list)) |
| 1729 | continue; |
| 1730 | |
| 1731 | if (!client_check_cmd_timeout(client, &tv)) |
| 1732 | continue; |
| 1733 | |
| 1734 | if (client->ops->timed_out) |
| 1735 | client->ops->timed_out(client); |
| 1736 | else |
| 1737 | log_err("fio: client %s timed out\n", client->hostname); |
| 1738 | |
| 1739 | client->error = ETIMEDOUT; |
| 1740 | remove_client(client); |
| 1741 | ret = 1; |
| 1742 | } |
| 1743 | |
| 1744 | return ret; |
| 1745 | } |
| 1746 | |
| 1747 | int fio_handle_clients(struct client_ops *ops) |
| 1748 | { |
| 1749 | struct pollfd *pfds; |
| 1750 | int i, ret = 0, retval = 0; |
| 1751 | |
| 1752 | fio_gettime(&eta_tv, NULL); |
| 1753 | |
| 1754 | pfds = malloc(nr_clients * sizeof(struct pollfd)); |
| 1755 | |
| 1756 | init_thread_stat(&client_ts); |
| 1757 | init_group_run_stat(&client_gs); |
| 1758 | |
| 1759 | while (!exit_backend && nr_clients) { |
| 1760 | struct flist_head *entry, *tmp; |
| 1761 | struct fio_client *client; |
| 1762 | |
| 1763 | i = 0; |
| 1764 | flist_for_each_safe(entry, tmp, &client_list) { |
| 1765 | client = flist_entry(entry, struct fio_client, list); |
| 1766 | |
| 1767 | if (!client->sent_job && !client->ops->stay_connected && |
| 1768 | flist_empty(&client->cmd_list)) { |
| 1769 | remove_client(client); |
| 1770 | continue; |
| 1771 | } |
| 1772 | |
| 1773 | pfds[i].fd = client->fd; |
| 1774 | pfds[i].events = POLLIN; |
| 1775 | i++; |
| 1776 | } |
| 1777 | |
| 1778 | if (!nr_clients) |
| 1779 | break; |
| 1780 | |
| 1781 | assert(i == nr_clients); |
| 1782 | |
| 1783 | do { |
| 1784 | struct timeval tv; |
| 1785 | int timeout; |
| 1786 | |
| 1787 | fio_gettime(&tv, NULL); |
| 1788 | if (mtime_since(&eta_tv, &tv) >= 900) { |
| 1789 | request_client_etas(ops); |
| 1790 | memcpy(&eta_tv, &tv, sizeof(tv)); |
| 1791 | |
| 1792 | if (fio_check_clients_timed_out()) |
| 1793 | break; |
| 1794 | } |
| 1795 | |
| 1796 | check_trigger_file(); |
| 1797 | |
| 1798 | timeout = min(100u, ops->eta_msec); |
| 1799 | |
| 1800 | ret = poll(pfds, nr_clients, timeout); |
| 1801 | if (ret < 0) { |
| 1802 | if (errno == EINTR) |
| 1803 | continue; |
| 1804 | log_err("fio: poll clients: %s\n", strerror(errno)); |
| 1805 | break; |
| 1806 | } else if (!ret) |
| 1807 | continue; |
| 1808 | } while (ret <= 0); |
| 1809 | |
| 1810 | for (i = 0; i < nr_clients; i++) { |
| 1811 | if (!(pfds[i].revents & POLLIN)) |
| 1812 | continue; |
| 1813 | |
| 1814 | client = find_client_by_fd(pfds[i].fd); |
| 1815 | if (!client) { |
| 1816 | log_err("fio: unknown client fd %ld\n", (long) pfds[i].fd); |
| 1817 | continue; |
| 1818 | } |
| 1819 | if (!fio_handle_client(client)) { |
| 1820 | log_info("client: host=%s disconnected\n", |
| 1821 | client->hostname); |
| 1822 | remove_client(client); |
| 1823 | retval = 1; |
| 1824 | } else if (client->error) |
| 1825 | retval = 1; |
| 1826 | fio_put_client(client); |
| 1827 | } |
| 1828 | } |
| 1829 | |
| 1830 | fio_client_json_fini(); |
| 1831 | |
| 1832 | free(pfds); |
| 1833 | return retval || error_clients; |
| 1834 | } |