| 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 | |
| 18 | #include "fio.h" |
| 19 | #include "server.h" |
| 20 | #include "flist.h" |
| 21 | #include "hash.h" |
| 22 | |
| 23 | struct client_eta { |
| 24 | struct jobs_eta eta; |
| 25 | unsigned int pending; |
| 26 | }; |
| 27 | |
| 28 | struct fio_client { |
| 29 | struct flist_head list; |
| 30 | struct flist_head hash_list; |
| 31 | struct flist_head arg_list; |
| 32 | struct sockaddr_in addr; |
| 33 | struct sockaddr_un addr_un; |
| 34 | char *hostname; |
| 35 | int port; |
| 36 | int fd; |
| 37 | |
| 38 | char *name; |
| 39 | |
| 40 | int state; |
| 41 | |
| 42 | int skip_newline; |
| 43 | int is_sock; |
| 44 | int disk_stats_shown; |
| 45 | |
| 46 | struct flist_head eta_list; |
| 47 | struct client_eta *eta_in_flight; |
| 48 | |
| 49 | struct flist_head cmd_list; |
| 50 | |
| 51 | uint16_t argc; |
| 52 | char **argv; |
| 53 | }; |
| 54 | |
| 55 | static struct timeval eta_tv; |
| 56 | |
| 57 | enum { |
| 58 | Client_created = 0, |
| 59 | Client_connected = 1, |
| 60 | Client_started = 2, |
| 61 | Client_running = 3, |
| 62 | Client_stopped = 4, |
| 63 | Client_exited = 5, |
| 64 | }; |
| 65 | |
| 66 | static FLIST_HEAD(client_list); |
| 67 | static FLIST_HEAD(eta_list); |
| 68 | |
| 69 | static FLIST_HEAD(arg_list); |
| 70 | |
| 71 | static struct thread_stat client_ts; |
| 72 | static struct group_run_stats client_gs; |
| 73 | static int sum_stat_clients; |
| 74 | static int sum_stat_nr; |
| 75 | |
| 76 | #define FIO_CLIENT_HASH_BITS 7 |
| 77 | #define FIO_CLIENT_HASH_SZ (1 << FIO_CLIENT_HASH_BITS) |
| 78 | #define FIO_CLIENT_HASH_MASK (FIO_CLIENT_HASH_SZ - 1) |
| 79 | static struct flist_head client_hash[FIO_CLIENT_HASH_SZ]; |
| 80 | |
| 81 | static int handle_client(struct fio_client *client); |
| 82 | static void dec_jobs_eta(struct client_eta *eta); |
| 83 | |
| 84 | static void fio_client_add_hash(struct fio_client *client) |
| 85 | { |
| 86 | int bucket = hash_long(client->fd, FIO_CLIENT_HASH_BITS); |
| 87 | |
| 88 | bucket &= FIO_CLIENT_HASH_MASK; |
| 89 | flist_add(&client->hash_list, &client_hash[bucket]); |
| 90 | } |
| 91 | |
| 92 | static void fio_client_remove_hash(struct fio_client *client) |
| 93 | { |
| 94 | if (!flist_empty(&client->hash_list)) |
| 95 | flist_del_init(&client->hash_list); |
| 96 | } |
| 97 | |
| 98 | static void fio_init fio_client_hash_init(void) |
| 99 | { |
| 100 | int i; |
| 101 | |
| 102 | for (i = 0; i < FIO_CLIENT_HASH_SZ; i++) |
| 103 | INIT_FLIST_HEAD(&client_hash[i]); |
| 104 | } |
| 105 | |
| 106 | static struct fio_client *find_client_by_fd(int fd) |
| 107 | { |
| 108 | int bucket = hash_long(fd, FIO_CLIENT_HASH_BITS) & FIO_CLIENT_HASH_MASK; |
| 109 | struct fio_client *client; |
| 110 | struct flist_head *entry; |
| 111 | |
| 112 | flist_for_each(entry, &client_hash[bucket]) { |
| 113 | client = flist_entry(entry, struct fio_client, hash_list); |
| 114 | |
| 115 | if (client->fd == fd) |
| 116 | return client; |
| 117 | } |
| 118 | |
| 119 | return NULL; |
| 120 | } |
| 121 | |
| 122 | static void remove_client(struct fio_client *client) |
| 123 | { |
| 124 | dprint(FD_NET, "client: removed <%s>\n", client->hostname); |
| 125 | flist_del(&client->list); |
| 126 | |
| 127 | fio_client_remove_hash(client); |
| 128 | |
| 129 | if (!flist_empty(&client->eta_list)) { |
| 130 | flist_del_init(&client->eta_list); |
| 131 | dec_jobs_eta(client->eta_in_flight); |
| 132 | } |
| 133 | |
| 134 | free(client->hostname); |
| 135 | if (client->argv) |
| 136 | free(client->argv); |
| 137 | if (client->name) |
| 138 | free(client->name); |
| 139 | |
| 140 | free(client); |
| 141 | nr_clients--; |
| 142 | sum_stat_clients--; |
| 143 | } |
| 144 | |
| 145 | static void __fio_client_add_cmd_option(struct fio_client *client, |
| 146 | const char *opt) |
| 147 | { |
| 148 | int index; |
| 149 | |
| 150 | index = client->argc++; |
| 151 | client->argv = realloc(client->argv, sizeof(char *) * client->argc); |
| 152 | client->argv[index] = strdup(opt); |
| 153 | dprint(FD_NET, "client: add cmd %d: %s\n", index, opt); |
| 154 | } |
| 155 | |
| 156 | void fio_client_add_cmd_option(void *cookie, const char *opt) |
| 157 | { |
| 158 | struct fio_client *client = cookie; |
| 159 | struct flist_head *entry; |
| 160 | |
| 161 | if (!client || !opt) |
| 162 | return; |
| 163 | |
| 164 | __fio_client_add_cmd_option(client, opt); |
| 165 | |
| 166 | /* |
| 167 | * Duplicate arguments to shared client group |
| 168 | */ |
| 169 | flist_for_each(entry, &arg_list) { |
| 170 | client = flist_entry(entry, struct fio_client, arg_list); |
| 171 | |
| 172 | __fio_client_add_cmd_option(client, opt); |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | int fio_client_add(const char *hostname, void **cookie) |
| 177 | { |
| 178 | struct fio_client *existing = *cookie; |
| 179 | struct fio_client *client; |
| 180 | |
| 181 | if (existing) { |
| 182 | /* |
| 183 | * We always add our "exec" name as the option, hence 1 |
| 184 | * means empty. |
| 185 | */ |
| 186 | if (existing->argc == 1) |
| 187 | flist_add_tail(&existing->arg_list, &arg_list); |
| 188 | else { |
| 189 | while (!flist_empty(&arg_list)) |
| 190 | flist_del_init(arg_list.next); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | client = malloc(sizeof(*client)); |
| 195 | memset(client, 0, sizeof(*client)); |
| 196 | |
| 197 | INIT_FLIST_HEAD(&client->list); |
| 198 | INIT_FLIST_HEAD(&client->hash_list); |
| 199 | INIT_FLIST_HEAD(&client->arg_list); |
| 200 | INIT_FLIST_HEAD(&client->eta_list); |
| 201 | INIT_FLIST_HEAD(&client->cmd_list); |
| 202 | |
| 203 | if (fio_server_parse_string(hostname, &client->hostname, |
| 204 | &client->is_sock, &client->port, |
| 205 | &client->addr.sin_addr)) |
| 206 | return -1; |
| 207 | |
| 208 | client->fd = -1; |
| 209 | |
| 210 | __fio_client_add_cmd_option(client, "fio"); |
| 211 | |
| 212 | flist_add(&client->list, &client_list); |
| 213 | nr_clients++; |
| 214 | dprint(FD_NET, "client: added <%s>\n", client->hostname); |
| 215 | *cookie = client; |
| 216 | return 0; |
| 217 | } |
| 218 | |
| 219 | static int fio_client_connect_ip(struct fio_client *client) |
| 220 | { |
| 221 | int fd; |
| 222 | |
| 223 | client->addr.sin_family = AF_INET; |
| 224 | client->addr.sin_port = htons(client->port); |
| 225 | |
| 226 | fd = socket(AF_INET, SOCK_STREAM, 0); |
| 227 | if (fd < 0) { |
| 228 | log_err("fio: socket: %s\n", strerror(errno)); |
| 229 | return -1; |
| 230 | } |
| 231 | |
| 232 | if (connect(fd, (struct sockaddr *) &client->addr, sizeof(client->addr)) < 0) { |
| 233 | log_err("fio: connect: %s\n", strerror(errno)); |
| 234 | log_err("fio: failed to connect to %s:%u\n", client->hostname, |
| 235 | client->port); |
| 236 | close(fd); |
| 237 | return -1; |
| 238 | } |
| 239 | |
| 240 | return fd; |
| 241 | } |
| 242 | |
| 243 | static int fio_client_connect_sock(struct fio_client *client) |
| 244 | { |
| 245 | struct sockaddr_un *addr = &client->addr_un; |
| 246 | fio_socklen_t len; |
| 247 | int fd; |
| 248 | |
| 249 | memset(addr, 0, sizeof(*addr)); |
| 250 | addr->sun_family = AF_UNIX; |
| 251 | strcpy(addr->sun_path, client->hostname); |
| 252 | |
| 253 | fd = socket(AF_UNIX, SOCK_STREAM, 0); |
| 254 | if (fd < 0) { |
| 255 | log_err("fio: socket: %s\n", strerror(errno)); |
| 256 | return -1; |
| 257 | } |
| 258 | |
| 259 | len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1; |
| 260 | if (connect(fd, (struct sockaddr *) addr, len) < 0) { |
| 261 | log_err("fio: connect; %s\n", strerror(errno)); |
| 262 | close(fd); |
| 263 | return -1; |
| 264 | } |
| 265 | |
| 266 | return fd; |
| 267 | } |
| 268 | |
| 269 | static int fio_client_connect(struct fio_client *client) |
| 270 | { |
| 271 | int fd; |
| 272 | |
| 273 | dprint(FD_NET, "client: connect to host %s\n", client->hostname); |
| 274 | |
| 275 | if (client->is_sock) |
| 276 | fd = fio_client_connect_sock(client); |
| 277 | else |
| 278 | fd = fio_client_connect_ip(client); |
| 279 | |
| 280 | dprint(FD_NET, "client: %s connected %d\n", client->hostname, fd); |
| 281 | |
| 282 | if (fd < 0) |
| 283 | return 1; |
| 284 | |
| 285 | client->fd = fd; |
| 286 | fio_client_add_hash(client); |
| 287 | client->state = Client_connected; |
| 288 | return 0; |
| 289 | } |
| 290 | |
| 291 | void fio_clients_terminate(void) |
| 292 | { |
| 293 | struct flist_head *entry; |
| 294 | struct fio_client *client; |
| 295 | |
| 296 | dprint(FD_NET, "client: terminate clients\n"); |
| 297 | |
| 298 | flist_for_each(entry, &client_list) { |
| 299 | client = flist_entry(entry, struct fio_client, list); |
| 300 | |
| 301 | fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_QUIT, 0, NULL); |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | static void sig_int(int sig) |
| 306 | { |
| 307 | dprint(FD_NET, "client: got signal %d\n", sig); |
| 308 | fio_clients_terminate(); |
| 309 | } |
| 310 | |
| 311 | static void client_signal_handler(void) |
| 312 | { |
| 313 | struct sigaction act; |
| 314 | |
| 315 | memset(&act, 0, sizeof(act)); |
| 316 | act.sa_handler = sig_int; |
| 317 | act.sa_flags = SA_RESTART; |
| 318 | sigaction(SIGINT, &act, NULL); |
| 319 | |
| 320 | memset(&act, 0, sizeof(act)); |
| 321 | act.sa_handler = sig_int; |
| 322 | act.sa_flags = SA_RESTART; |
| 323 | sigaction(SIGTERM, &act, NULL); |
| 324 | } |
| 325 | |
| 326 | static void probe_client(struct fio_client *client) |
| 327 | { |
| 328 | dprint(FD_NET, "client: send probe\n"); |
| 329 | |
| 330 | fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_PROBE, 0, &client->cmd_list); |
| 331 | } |
| 332 | |
| 333 | static int send_client_cmd_line(struct fio_client *client) |
| 334 | { |
| 335 | struct cmd_single_line_pdu *cslp; |
| 336 | struct cmd_line_pdu *clp; |
| 337 | unsigned long offset; |
| 338 | unsigned int *lens; |
| 339 | void *pdu; |
| 340 | size_t mem; |
| 341 | int i, ret; |
| 342 | |
| 343 | dprint(FD_NET, "client: send cmdline %d\n", client->argc); |
| 344 | |
| 345 | lens = malloc(client->argc * sizeof(unsigned int)); |
| 346 | |
| 347 | /* |
| 348 | * Find out how much mem we need |
| 349 | */ |
| 350 | for (i = 0, mem = 0; i < client->argc; i++) { |
| 351 | lens[i] = strlen(client->argv[i]) + 1; |
| 352 | mem += lens[i]; |
| 353 | } |
| 354 | |
| 355 | /* |
| 356 | * We need one cmd_line_pdu, and argc number of cmd_single_line_pdu |
| 357 | */ |
| 358 | mem += sizeof(*clp) + (client->argc * sizeof(*cslp)); |
| 359 | |
| 360 | pdu = malloc(mem); |
| 361 | clp = pdu; |
| 362 | offset = sizeof(*clp); |
| 363 | |
| 364 | for (i = 0; i < client->argc; i++) { |
| 365 | uint16_t arg_len = lens[i]; |
| 366 | |
| 367 | cslp = pdu + offset; |
| 368 | strcpy((char *) cslp->text, client->argv[i]); |
| 369 | cslp->len = cpu_to_le16(arg_len); |
| 370 | offset += sizeof(*cslp) + arg_len; |
| 371 | } |
| 372 | |
| 373 | free(lens); |
| 374 | clp->lines = cpu_to_le16(client->argc); |
| 375 | ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOBLINE, pdu, mem, 0); |
| 376 | free(pdu); |
| 377 | return ret; |
| 378 | } |
| 379 | |
| 380 | int fio_clients_connect(void) |
| 381 | { |
| 382 | struct fio_client *client; |
| 383 | struct flist_head *entry, *tmp; |
| 384 | int ret; |
| 385 | |
| 386 | dprint(FD_NET, "client: connect all\n"); |
| 387 | |
| 388 | client_signal_handler(); |
| 389 | |
| 390 | flist_for_each_safe(entry, tmp, &client_list) { |
| 391 | client = flist_entry(entry, struct fio_client, list); |
| 392 | |
| 393 | ret = fio_client_connect(client); |
| 394 | if (ret) { |
| 395 | remove_client(client); |
| 396 | continue; |
| 397 | } |
| 398 | |
| 399 | probe_client(client); |
| 400 | |
| 401 | if (client->argc > 1) |
| 402 | send_client_cmd_line(client); |
| 403 | } |
| 404 | |
| 405 | return !nr_clients; |
| 406 | } |
| 407 | |
| 408 | /* |
| 409 | * Send file contents to server backend. We could use sendfile(), but to remain |
| 410 | * more portable lets just read/write the darn thing. |
| 411 | */ |
| 412 | static int fio_client_send_ini(struct fio_client *client, const char *filename) |
| 413 | { |
| 414 | struct stat sb; |
| 415 | char *p, *buf; |
| 416 | off_t len; |
| 417 | int fd, ret; |
| 418 | |
| 419 | dprint(FD_NET, "send ini %s to %s\n", filename, client->hostname); |
| 420 | |
| 421 | fd = open(filename, O_RDONLY); |
| 422 | if (fd < 0) { |
| 423 | log_err("fio: job file <%s> open: %s\n", filename, strerror(errno)); |
| 424 | return 1; |
| 425 | } |
| 426 | |
| 427 | if (fstat(fd, &sb) < 0) { |
| 428 | log_err("fio: job file stat: %s\n", strerror(errno)); |
| 429 | close(fd); |
| 430 | return 1; |
| 431 | } |
| 432 | |
| 433 | buf = malloc(sb.st_size); |
| 434 | |
| 435 | len = sb.st_size; |
| 436 | p = buf; |
| 437 | do { |
| 438 | ret = read(fd, p, len); |
| 439 | if (ret > 0) { |
| 440 | len -= ret; |
| 441 | if (!len) |
| 442 | break; |
| 443 | p += ret; |
| 444 | continue; |
| 445 | } else if (!ret) |
| 446 | break; |
| 447 | else if (errno == EAGAIN || errno == EINTR) |
| 448 | continue; |
| 449 | } while (1); |
| 450 | |
| 451 | if (len) { |
| 452 | log_err("fio: failed reading job file %s\n", filename); |
| 453 | close(fd); |
| 454 | free(buf); |
| 455 | return 1; |
| 456 | } |
| 457 | |
| 458 | ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, buf, sb.st_size, 0); |
| 459 | free(buf); |
| 460 | close(fd); |
| 461 | return ret; |
| 462 | } |
| 463 | |
| 464 | int fio_clients_send_ini(const char *filename) |
| 465 | { |
| 466 | struct fio_client *client; |
| 467 | struct flist_head *entry, *tmp; |
| 468 | |
| 469 | flist_for_each_safe(entry, tmp, &client_list) { |
| 470 | client = flist_entry(entry, struct fio_client, list); |
| 471 | |
| 472 | if (fio_client_send_ini(client, filename)) |
| 473 | remove_client(client); |
| 474 | } |
| 475 | |
| 476 | return !nr_clients; |
| 477 | } |
| 478 | |
| 479 | static void convert_io_stat(struct io_stat *dst, struct io_stat *src) |
| 480 | { |
| 481 | dst->max_val = le64_to_cpu(src->max_val); |
| 482 | dst->min_val = le64_to_cpu(src->min_val); |
| 483 | dst->samples = le64_to_cpu(src->samples); |
| 484 | |
| 485 | /* |
| 486 | * Floats arrive as IEEE 754 encoded uint64_t, convert back to double |
| 487 | */ |
| 488 | dst->mean.u.f = fio_uint64_to_double(le64_to_cpu(dst->mean.u.i)); |
| 489 | dst->S.u.f = fio_uint64_to_double(le64_to_cpu(dst->S.u.i)); |
| 490 | } |
| 491 | |
| 492 | static void convert_ts(struct thread_stat *dst, struct thread_stat *src) |
| 493 | { |
| 494 | int i, j; |
| 495 | |
| 496 | dst->error = le32_to_cpu(src->error); |
| 497 | dst->groupid = le32_to_cpu(src->groupid); |
| 498 | dst->pid = le32_to_cpu(src->pid); |
| 499 | dst->members = le32_to_cpu(src->members); |
| 500 | |
| 501 | for (i = 0; i < 2; i++) { |
| 502 | convert_io_stat(&dst->clat_stat[i], &src->clat_stat[i]); |
| 503 | convert_io_stat(&dst->slat_stat[i], &src->slat_stat[i]); |
| 504 | convert_io_stat(&dst->lat_stat[i], &src->lat_stat[i]); |
| 505 | convert_io_stat(&dst->bw_stat[i], &src->bw_stat[i]); |
| 506 | } |
| 507 | |
| 508 | dst->usr_time = le64_to_cpu(src->usr_time); |
| 509 | dst->sys_time = le64_to_cpu(src->sys_time); |
| 510 | dst->ctx = le64_to_cpu(src->ctx); |
| 511 | dst->minf = le64_to_cpu(src->minf); |
| 512 | dst->majf = le64_to_cpu(src->majf); |
| 513 | dst->clat_percentiles = le64_to_cpu(src->clat_percentiles); |
| 514 | |
| 515 | for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) { |
| 516 | fio_fp64_t *fps = &src->percentile_list[i]; |
| 517 | fio_fp64_t *fpd = &dst->percentile_list[i]; |
| 518 | |
| 519 | fpd->u.f = fio_uint64_to_double(le64_to_cpu(fps->u.i)); |
| 520 | } |
| 521 | |
| 522 | for (i = 0; i < FIO_IO_U_MAP_NR; i++) { |
| 523 | dst->io_u_map[i] = le32_to_cpu(src->io_u_map[i]); |
| 524 | dst->io_u_submit[i] = le32_to_cpu(src->io_u_submit[i]); |
| 525 | dst->io_u_complete[i] = le32_to_cpu(src->io_u_complete[i]); |
| 526 | } |
| 527 | |
| 528 | for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) { |
| 529 | dst->io_u_lat_u[i] = le32_to_cpu(src->io_u_lat_u[i]); |
| 530 | dst->io_u_lat_m[i] = le32_to_cpu(src->io_u_lat_m[i]); |
| 531 | } |
| 532 | |
| 533 | for (i = 0; i < 2; i++) |
| 534 | for (j = 0; j < FIO_IO_U_PLAT_NR; j++) |
| 535 | dst->io_u_plat[i][j] = le32_to_cpu(src->io_u_plat[i][j]); |
| 536 | |
| 537 | for (i = 0; i < 3; i++) { |
| 538 | dst->total_io_u[i] = le64_to_cpu(src->total_io_u[i]); |
| 539 | dst->short_io_u[i] = le64_to_cpu(src->short_io_u[i]); |
| 540 | } |
| 541 | |
| 542 | dst->total_submit = le64_to_cpu(src->total_submit); |
| 543 | dst->total_complete = le64_to_cpu(src->total_complete); |
| 544 | |
| 545 | for (i = 0; i < 2; i++) { |
| 546 | dst->io_bytes[i] = le64_to_cpu(src->io_bytes[i]); |
| 547 | dst->runtime[i] = le64_to_cpu(src->runtime[i]); |
| 548 | } |
| 549 | |
| 550 | dst->total_run_time = le64_to_cpu(src->total_run_time); |
| 551 | dst->continue_on_error = le16_to_cpu(src->continue_on_error); |
| 552 | dst->total_err_count = le64_to_cpu(src->total_err_count); |
| 553 | dst->first_error = le32_to_cpu(src->first_error); |
| 554 | dst->kb_base = le32_to_cpu(src->kb_base); |
| 555 | } |
| 556 | |
| 557 | static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src) |
| 558 | { |
| 559 | int i; |
| 560 | |
| 561 | for (i = 0; i < 2; i++) { |
| 562 | dst->max_run[i] = le64_to_cpu(src->max_run[i]); |
| 563 | dst->min_run[i] = le64_to_cpu(src->min_run[i]); |
| 564 | dst->max_bw[i] = le64_to_cpu(src->max_bw[i]); |
| 565 | dst->min_bw[i] = le64_to_cpu(src->min_bw[i]); |
| 566 | dst->io_kb[i] = le64_to_cpu(src->io_kb[i]); |
| 567 | dst->agg[i] = le64_to_cpu(src->agg[i]); |
| 568 | } |
| 569 | |
| 570 | dst->kb_base = le32_to_cpu(src->kb_base); |
| 571 | dst->groupid = le32_to_cpu(src->groupid); |
| 572 | } |
| 573 | |
| 574 | static void handle_ts(struct fio_net_cmd *cmd) |
| 575 | { |
| 576 | struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload; |
| 577 | |
| 578 | convert_ts(&p->ts, &p->ts); |
| 579 | convert_gs(&p->rs, &p->rs); |
| 580 | |
| 581 | show_thread_status(&p->ts, &p->rs); |
| 582 | |
| 583 | if (sum_stat_clients == 1) |
| 584 | return; |
| 585 | |
| 586 | sum_thread_stats(&client_ts, &p->ts, sum_stat_nr); |
| 587 | sum_group_stats(&client_gs, &p->rs); |
| 588 | |
| 589 | client_ts.members++; |
| 590 | client_ts.groupid = p->ts.groupid; |
| 591 | |
| 592 | if (++sum_stat_nr == sum_stat_clients) { |
| 593 | strcpy(client_ts.name, "All clients"); |
| 594 | show_thread_status(&client_ts, &client_gs); |
| 595 | } |
| 596 | } |
| 597 | |
| 598 | static void handle_gs(struct fio_net_cmd *cmd) |
| 599 | { |
| 600 | struct group_run_stats *gs = (struct group_run_stats *) cmd->payload; |
| 601 | |
| 602 | convert_gs(gs, gs); |
| 603 | show_group_stats(gs); |
| 604 | } |
| 605 | |
| 606 | static void convert_agg(struct disk_util_agg *agg) |
| 607 | { |
| 608 | int i; |
| 609 | |
| 610 | for (i = 0; i < 2; i++) { |
| 611 | agg->ios[i] = le32_to_cpu(agg->ios[i]); |
| 612 | agg->merges[i] = le32_to_cpu(agg->merges[i]); |
| 613 | agg->sectors[i] = le64_to_cpu(agg->sectors[i]); |
| 614 | agg->ticks[i] = le32_to_cpu(agg->ticks[i]); |
| 615 | } |
| 616 | |
| 617 | agg->io_ticks = le32_to_cpu(agg->io_ticks); |
| 618 | agg->time_in_queue = le32_to_cpu(agg->time_in_queue); |
| 619 | agg->slavecount = le32_to_cpu(agg->slavecount); |
| 620 | agg->max_util.u.f = __le64_to_cpu(fio_uint64_to_double(agg->max_util.u.i)); |
| 621 | } |
| 622 | |
| 623 | static void convert_dus(struct disk_util_stat *dus) |
| 624 | { |
| 625 | int i; |
| 626 | |
| 627 | for (i = 0; i < 2; i++) { |
| 628 | dus->ios[i] = le32_to_cpu(dus->ios[i]); |
| 629 | dus->merges[i] = le32_to_cpu(dus->merges[i]); |
| 630 | dus->sectors[i] = le64_to_cpu(dus->sectors[i]); |
| 631 | dus->ticks[i] = le32_to_cpu(dus->ticks[i]); |
| 632 | } |
| 633 | |
| 634 | dus->io_ticks = le32_to_cpu(dus->io_ticks); |
| 635 | dus->time_in_queue = le32_to_cpu(dus->time_in_queue); |
| 636 | dus->msec = le64_to_cpu(dus->msec); |
| 637 | } |
| 638 | |
| 639 | static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd) |
| 640 | { |
| 641 | struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload; |
| 642 | |
| 643 | convert_dus(&du->dus); |
| 644 | convert_agg(&du->agg); |
| 645 | |
| 646 | if (!client->disk_stats_shown) { |
| 647 | client->disk_stats_shown = 1; |
| 648 | log_info("\nDisk stats (read/write):\n"); |
| 649 | } |
| 650 | |
| 651 | print_disk_util(&du->dus, &du->agg, terse_output); |
| 652 | } |
| 653 | |
| 654 | static void convert_jobs_eta(struct jobs_eta *je) |
| 655 | { |
| 656 | int i; |
| 657 | |
| 658 | je->nr_running = le32_to_cpu(je->nr_running); |
| 659 | je->nr_ramp = le32_to_cpu(je->nr_ramp); |
| 660 | je->nr_pending = le32_to_cpu(je->nr_pending); |
| 661 | je->files_open = le32_to_cpu(je->files_open); |
| 662 | je->m_rate = le32_to_cpu(je->m_rate); |
| 663 | je->t_rate = le32_to_cpu(je->t_rate); |
| 664 | je->m_iops = le32_to_cpu(je->m_iops); |
| 665 | je->t_iops = le32_to_cpu(je->t_iops); |
| 666 | |
| 667 | for (i = 0; i < 2; i++) { |
| 668 | je->rate[i] = le32_to_cpu(je->rate[i]); |
| 669 | je->iops[i] = le32_to_cpu(je->iops[i]); |
| 670 | } |
| 671 | |
| 672 | je->elapsed_sec = le64_to_cpu(je->elapsed_sec); |
| 673 | je->eta_sec = le64_to_cpu(je->eta_sec); |
| 674 | } |
| 675 | |
| 676 | static void sum_jobs_eta(struct jobs_eta *dst, struct jobs_eta *je) |
| 677 | { |
| 678 | int i; |
| 679 | |
| 680 | dst->nr_running += je->nr_running; |
| 681 | dst->nr_ramp += je->nr_ramp; |
| 682 | dst->nr_pending += je->nr_pending; |
| 683 | dst->files_open += je->files_open; |
| 684 | dst->m_rate += je->m_rate; |
| 685 | dst->t_rate += je->t_rate; |
| 686 | dst->m_iops += je->m_iops; |
| 687 | dst->t_iops += je->t_iops; |
| 688 | |
| 689 | for (i = 0; i < 2; i++) { |
| 690 | dst->rate[i] += je->rate[i]; |
| 691 | dst->iops[i] += je->iops[i]; |
| 692 | } |
| 693 | |
| 694 | dst->elapsed_sec += je->elapsed_sec; |
| 695 | |
| 696 | if (je->eta_sec > dst->eta_sec) |
| 697 | dst->eta_sec = je->eta_sec; |
| 698 | } |
| 699 | |
| 700 | static void dec_jobs_eta(struct client_eta *eta) |
| 701 | { |
| 702 | if (!--eta->pending) { |
| 703 | display_thread_status(&eta->eta); |
| 704 | free(eta); |
| 705 | } |
| 706 | } |
| 707 | |
| 708 | static void remove_reply_cmd(struct fio_client *client, struct fio_net_cmd *cmd) |
| 709 | { |
| 710 | struct fio_net_int_cmd *icmd = NULL; |
| 711 | struct flist_head *entry; |
| 712 | |
| 713 | flist_for_each(entry, &client->cmd_list) { |
| 714 | icmd = flist_entry(entry, struct fio_net_int_cmd, list); |
| 715 | |
| 716 | if (cmd->tag == (uintptr_t) icmd) |
| 717 | break; |
| 718 | |
| 719 | icmd = NULL; |
| 720 | } |
| 721 | |
| 722 | if (!icmd) { |
| 723 | log_err("fio: client: unable to find matching tag\n"); |
| 724 | return; |
| 725 | } |
| 726 | |
| 727 | flist_del(&icmd->list); |
| 728 | cmd->tag = icmd->saved_tag; |
| 729 | free(icmd); |
| 730 | } |
| 731 | |
| 732 | static void handle_eta(struct fio_client *client, struct fio_net_cmd *cmd) |
| 733 | { |
| 734 | struct jobs_eta *je = (struct jobs_eta *) cmd->payload; |
| 735 | struct client_eta *eta = (struct client_eta *) (uintptr_t) cmd->tag; |
| 736 | |
| 737 | dprint(FD_NET, "client: got eta tag %p, %d\n", eta, eta->pending); |
| 738 | |
| 739 | assert(client->eta_in_flight == eta); |
| 740 | |
| 741 | client->eta_in_flight = NULL; |
| 742 | flist_del_init(&client->eta_list); |
| 743 | |
| 744 | convert_jobs_eta(je); |
| 745 | sum_jobs_eta(&eta->eta, je); |
| 746 | dec_jobs_eta(eta); |
| 747 | } |
| 748 | |
| 749 | static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd) |
| 750 | { |
| 751 | struct cmd_probe_pdu *probe = (struct cmd_probe_pdu *) cmd->payload; |
| 752 | const char *os, *arch; |
| 753 | char bit[16]; |
| 754 | |
| 755 | os = fio_get_os_string(probe->os); |
| 756 | if (!os) |
| 757 | os = "unknown"; |
| 758 | |
| 759 | arch = fio_get_arch_string(probe->arch); |
| 760 | if (!arch) |
| 761 | os = "unknown"; |
| 762 | |
| 763 | sprintf(bit, "%d-bit", probe->bpp * 8); |
| 764 | |
| 765 | log_info("hostname=%s, be=%u, %s, os=%s, arch=%s, fio=%u.%u.%u\n", |
| 766 | probe->hostname, probe->bigendian, bit, os, arch, |
| 767 | probe->fio_major, probe->fio_minor, probe->fio_patch); |
| 768 | |
| 769 | if (!client->name) |
| 770 | client->name = strdup((char *) probe->hostname); |
| 771 | } |
| 772 | |
| 773 | static int handle_client(struct fio_client *client) |
| 774 | { |
| 775 | struct fio_net_cmd *cmd; |
| 776 | |
| 777 | dprint(FD_NET, "client: handle %s\n", client->hostname); |
| 778 | |
| 779 | cmd = fio_net_recv_cmd(client->fd); |
| 780 | if (!cmd) |
| 781 | return 0; |
| 782 | |
| 783 | dprint(FD_NET, "client: got cmd op %s from %s\n", |
| 784 | fio_server_op(cmd->opcode), client->hostname); |
| 785 | |
| 786 | switch (cmd->opcode) { |
| 787 | case FIO_NET_CMD_QUIT: |
| 788 | remove_client(client); |
| 789 | free(cmd); |
| 790 | break; |
| 791 | case FIO_NET_CMD_TEXT: { |
| 792 | const char *buf = (const char *) cmd->payload; |
| 793 | const char *name; |
| 794 | int fio_unused ret; |
| 795 | |
| 796 | name = client->name ? client->name : client->hostname; |
| 797 | |
| 798 | if (!client->skip_newline) |
| 799 | fprintf(f_out, "<%s> ", name); |
| 800 | ret = fwrite(buf, cmd->pdu_len, 1, f_out); |
| 801 | fflush(f_out); |
| 802 | client->skip_newline = strchr(buf, '\n') == NULL; |
| 803 | free(cmd); |
| 804 | break; |
| 805 | } |
| 806 | case FIO_NET_CMD_DU: |
| 807 | handle_du(client, cmd); |
| 808 | free(cmd); |
| 809 | break; |
| 810 | case FIO_NET_CMD_TS: |
| 811 | handle_ts(cmd); |
| 812 | free(cmd); |
| 813 | break; |
| 814 | case FIO_NET_CMD_GS: |
| 815 | handle_gs(cmd); |
| 816 | free(cmd); |
| 817 | break; |
| 818 | case FIO_NET_CMD_ETA: |
| 819 | remove_reply_cmd(client, cmd); |
| 820 | handle_eta(client, cmd); |
| 821 | free(cmd); |
| 822 | break; |
| 823 | case FIO_NET_CMD_PROBE: |
| 824 | remove_reply_cmd(client, cmd); |
| 825 | handle_probe(client, cmd); |
| 826 | free(cmd); |
| 827 | break; |
| 828 | case FIO_NET_CMD_RUN: |
| 829 | client->state = Client_running; |
| 830 | free(cmd); |
| 831 | break; |
| 832 | case FIO_NET_CMD_START: |
| 833 | client->state = Client_started; |
| 834 | free(cmd); |
| 835 | break; |
| 836 | case FIO_NET_CMD_STOP: |
| 837 | client->state = Client_stopped; |
| 838 | free(cmd); |
| 839 | break; |
| 840 | default: |
| 841 | log_err("fio: unknown client op: %s\n", fio_server_op(cmd->opcode)); |
| 842 | free(cmd); |
| 843 | break; |
| 844 | } |
| 845 | |
| 846 | return 1; |
| 847 | } |
| 848 | |
| 849 | static void request_client_etas(void) |
| 850 | { |
| 851 | struct fio_client *client; |
| 852 | struct flist_head *entry; |
| 853 | struct client_eta *eta; |
| 854 | int skipped = 0; |
| 855 | |
| 856 | dprint(FD_NET, "client: request eta (%d)\n", nr_clients); |
| 857 | |
| 858 | eta = malloc(sizeof(*eta)); |
| 859 | memset(&eta->eta, 0, sizeof(eta->eta)); |
| 860 | eta->pending = nr_clients; |
| 861 | |
| 862 | flist_for_each(entry, &client_list) { |
| 863 | client = flist_entry(entry, struct fio_client, list); |
| 864 | |
| 865 | if (!flist_empty(&client->eta_list)) { |
| 866 | skipped++; |
| 867 | continue; |
| 868 | } |
| 869 | if (client->state != Client_running) |
| 870 | continue; |
| 871 | |
| 872 | assert(!client->eta_in_flight); |
| 873 | flist_add_tail(&client->eta_list, &eta_list); |
| 874 | client->eta_in_flight = eta; |
| 875 | fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_SEND_ETA, |
| 876 | (uintptr_t) eta, &client->cmd_list); |
| 877 | } |
| 878 | |
| 879 | while (skipped--) |
| 880 | dec_jobs_eta(eta); |
| 881 | |
| 882 | dprint(FD_NET, "client: requested eta tag %p\n", eta); |
| 883 | } |
| 884 | |
| 885 | static int client_check_cmd_timeout(struct fio_client *client, |
| 886 | struct timeval *now) |
| 887 | { |
| 888 | struct fio_net_int_cmd *cmd; |
| 889 | struct flist_head *entry, *tmp; |
| 890 | int ret = 0; |
| 891 | |
| 892 | flist_for_each_safe(entry, tmp, &client->cmd_list) { |
| 893 | cmd = flist_entry(entry, struct fio_net_int_cmd, list); |
| 894 | |
| 895 | if (mtime_since(&cmd->tv, now) < FIO_NET_CLIENT_TIMEOUT) |
| 896 | continue; |
| 897 | |
| 898 | log_err("fio: client %s, timeout on cmd %s\n", client->hostname, |
| 899 | fio_server_op(cmd->cmd.opcode)); |
| 900 | flist_del(&cmd->list); |
| 901 | free(cmd); |
| 902 | ret = 1; |
| 903 | } |
| 904 | |
| 905 | return flist_empty(&client->cmd_list) && ret; |
| 906 | } |
| 907 | |
| 908 | static int fio_client_timed_out(void) |
| 909 | { |
| 910 | struct fio_client *client; |
| 911 | struct flist_head *entry, *tmp; |
| 912 | struct timeval tv; |
| 913 | int ret = 0; |
| 914 | |
| 915 | gettimeofday(&tv, NULL); |
| 916 | |
| 917 | flist_for_each_safe(entry, tmp, &client_list) { |
| 918 | client = flist_entry(entry, struct fio_client, list); |
| 919 | |
| 920 | if (flist_empty(&client->cmd_list)) |
| 921 | continue; |
| 922 | |
| 923 | if (!client_check_cmd_timeout(client, &tv)) |
| 924 | continue; |
| 925 | |
| 926 | log_err("fio: client %s timed out\n", client->hostname); |
| 927 | remove_client(client); |
| 928 | ret = 1; |
| 929 | } |
| 930 | |
| 931 | return ret; |
| 932 | } |
| 933 | |
| 934 | int fio_handle_clients(void) |
| 935 | { |
| 936 | struct fio_client *client; |
| 937 | struct flist_head *entry; |
| 938 | struct pollfd *pfds; |
| 939 | int i, ret = 0; |
| 940 | |
| 941 | gettimeofday(&eta_tv, NULL); |
| 942 | |
| 943 | pfds = malloc(nr_clients * sizeof(struct pollfd)); |
| 944 | |
| 945 | sum_stat_clients = nr_clients; |
| 946 | init_thread_stat(&client_ts); |
| 947 | init_group_run_stat(&client_gs); |
| 948 | |
| 949 | while (!exit_backend && nr_clients) { |
| 950 | i = 0; |
| 951 | flist_for_each(entry, &client_list) { |
| 952 | client = flist_entry(entry, struct fio_client, list); |
| 953 | |
| 954 | pfds[i].fd = client->fd; |
| 955 | pfds[i].events = POLLIN; |
| 956 | i++; |
| 957 | } |
| 958 | |
| 959 | assert(i == nr_clients); |
| 960 | |
| 961 | do { |
| 962 | struct timeval tv; |
| 963 | |
| 964 | gettimeofday(&tv, NULL); |
| 965 | if (mtime_since(&eta_tv, &tv) >= 900) { |
| 966 | request_client_etas(); |
| 967 | memcpy(&eta_tv, &tv, sizeof(tv)); |
| 968 | |
| 969 | if (fio_client_timed_out()) |
| 970 | break; |
| 971 | } |
| 972 | |
| 973 | ret = poll(pfds, nr_clients, 100); |
| 974 | if (ret < 0) { |
| 975 | if (errno == EINTR) |
| 976 | continue; |
| 977 | log_err("fio: poll clients: %s\n", strerror(errno)); |
| 978 | break; |
| 979 | } else if (!ret) |
| 980 | continue; |
| 981 | } while (ret <= 0); |
| 982 | |
| 983 | for (i = 0; i < nr_clients; i++) { |
| 984 | if (!(pfds[i].revents & POLLIN)) |
| 985 | continue; |
| 986 | |
| 987 | client = find_client_by_fd(pfds[i].fd); |
| 988 | if (!client) { |
| 989 | log_err("fio: unknown client fd %d\n", pfds[i].fd); |
| 990 | continue; |
| 991 | } |
| 992 | if (!handle_client(client)) { |
| 993 | log_info("client: host=%s disconnected\n", |
| 994 | client->hostname); |
| 995 | remove_client(client); |
| 996 | } |
| 997 | } |
| 998 | } |
| 999 | |
| 1000 | free(pfds); |
| 1001 | return 0; |
| 1002 | } |