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