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