9 #include <sys/socket.h>
11 #include <netinet/in.h>
12 #include <arpa/inet.h>
24 #include "verify-state.h"
26 static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd);
27 static void handle_ts(struct fio_client *client, struct fio_net_cmd *cmd);
28 static void handle_gs(struct fio_client *client, struct fio_net_cmd *cmd);
29 static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd);
30 static void handle_text(struct fio_client *client, struct fio_net_cmd *cmd);
31 static void handle_stop(struct fio_client *client);
32 static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd);
34 static void convert_text(struct fio_net_cmd *cmd);
36 struct client_ops fio_client_ops = {
38 .disk_util = handle_du,
39 .thread_status = handle_ts,
40 .group_stats = handle_gs,
42 .start = handle_start,
43 .eta = display_thread_status,
44 .probe = handle_probe,
45 .eta_msec = FIO_CLIENT_DEF_ETA_MSEC,
46 .client_type = FIO_CLIENT_TYPE_CLI,
49 static struct timespec eta_ts;
51 static FLIST_HEAD(client_list);
52 static FLIST_HEAD(eta_list);
54 static FLIST_HEAD(arg_list);
56 struct thread_stat client_ts;
57 struct group_run_stats client_gs;
60 static int sum_stat_nr;
61 static struct json_object *root = NULL;
62 static struct json_object *job_opt_object = NULL;
63 static struct json_array *clients_array = NULL;
64 static struct json_array *du_array = NULL;
66 static int error_clients;
68 #define FIO_CLIENT_HASH_BITS 7
69 #define FIO_CLIENT_HASH_SZ (1 << FIO_CLIENT_HASH_BITS)
70 #define FIO_CLIENT_HASH_MASK (FIO_CLIENT_HASH_SZ - 1)
71 static struct flist_head client_hash[FIO_CLIENT_HASH_SZ];
73 static struct cmd_iolog_pdu *convert_iolog(struct fio_net_cmd *, bool *);
75 static void fio_client_add_hash(struct fio_client *client)
77 int bucket = hash_long(client->fd, FIO_CLIENT_HASH_BITS);
79 bucket &= FIO_CLIENT_HASH_MASK;
80 flist_add(&client->hash_list, &client_hash[bucket]);
83 static void fio_client_remove_hash(struct fio_client *client)
85 if (!flist_empty(&client->hash_list))
86 flist_del_init(&client->hash_list);
89 static void fio_init fio_client_hash_init(void)
93 for (i = 0; i < FIO_CLIENT_HASH_SZ; i++)
94 INIT_FLIST_HEAD(&client_hash[i]);
97 static int read_data(int fd, void *data, size_t size)
102 ret = read(fd, data, size);
104 if (errno == EAGAIN || errno == EINTR)
121 static int read_ini_data(int fd, void *data, size_t size)
132 fp = fdopen(dupfd, "r");
141 char buf[OPT_LEN_MAX+1], *sub;
143 if (!fgets(buf, sizeof(buf), fp)) {
145 if (errno == EAGAIN || errno == EINTR)
152 sub = fio_option_dup_subs(buf);
154 if (len + 1 > size) {
155 log_err("fio: no space left to read data\n");
173 static void fio_client_json_init(void)
178 if (!(output_format & FIO_OUTPUT_JSON))
182 os_ctime_r((const time_t *) &time_p, time_buf, sizeof(time_buf));
183 time_buf[strlen(time_buf) - 1] = '\0';
185 root = json_create_object();
186 json_object_add_value_string(root, "fio version", fio_version_string);
187 json_object_add_value_int(root, "timestamp", time_p);
188 json_object_add_value_string(root, "time", time_buf);
190 job_opt_object = json_create_object();
191 json_object_add_value_object(root, "global options", job_opt_object);
192 clients_array = json_create_array();
193 json_object_add_value_array(root, "client_stats", clients_array);
194 du_array = json_create_array();
195 json_object_add_value_array(root, "disk_util", du_array);
198 static void fio_client_json_fini(void)
200 if (!(output_format & FIO_OUTPUT_JSON))
204 json_print_object(root, NULL);
206 json_free_object(root);
208 clients_array = NULL;
212 static struct fio_client *find_client_by_fd(int fd)
214 int bucket = hash_long(fd, FIO_CLIENT_HASH_BITS) & FIO_CLIENT_HASH_MASK;
215 struct fio_client *client;
216 struct flist_head *entry;
218 flist_for_each(entry, &client_hash[bucket]) {
219 client = flist_entry(entry, struct fio_client, hash_list);
221 if (client->fd == fd) {
230 void fio_put_client(struct fio_client *client)
235 free(client->hostname);
240 while (client->nr_files) {
241 struct client_file *cf = &client->files[--client->nr_files];
247 if (client->opt_lists)
248 free(client->opt_lists);
250 if (!client->did_stat)
259 static int fio_client_dec_jobs_eta(struct client_eta *eta, client_eta_op eta_fn)
261 if (!--eta->pending) {
270 static void fio_drain_client_text(struct fio_client *client)
273 struct fio_net_cmd *cmd;
275 cmd = fio_net_recv_cmd(client->fd, false);
279 if (cmd->opcode == FIO_NET_CMD_TEXT) {
281 client->ops->text(client, cmd);
288 static void remove_client(struct fio_client *client)
290 assert(client->refs);
292 dprint(FD_NET, "client: removed <%s>\n", client->hostname);
294 fio_drain_client_text(client);
296 if (!flist_empty(&client->list))
297 flist_del_init(&client->list);
299 fio_client_remove_hash(client);
301 if (!flist_empty(&client->eta_list)) {
302 flist_del_init(&client->eta_list);
303 fio_client_dec_jobs_eta(client->eta_in_flight, client->ops->eta);
309 if (client->ops->removed)
310 client->ops->removed(client);
313 fio_put_client(client);
316 struct fio_client *fio_get_client(struct fio_client *client)
322 static void __fio_client_add_cmd_option(struct fio_client *client,
327 index = client->argc++;
328 client->argv = realloc(client->argv, sizeof(char *) * client->argc);
329 client->argv[index] = strdup(opt);
330 dprint(FD_NET, "client: add cmd %d: %s\n", index, opt);
333 void fio_client_add_cmd_option(void *cookie, const char *opt)
335 struct fio_client *client = cookie;
336 struct flist_head *entry;
341 __fio_client_add_cmd_option(client, opt);
344 * Duplicate arguments to shared client group
346 flist_for_each(entry, &arg_list) {
347 client = flist_entry(entry, struct fio_client, arg_list);
349 __fio_client_add_cmd_option(client, opt);
353 struct fio_client *fio_client_add_explicit(struct client_ops *ops,
354 const char *hostname, int type,
357 struct fio_client *client;
359 client = malloc(sizeof(*client));
360 memset(client, 0, sizeof(*client));
362 INIT_FLIST_HEAD(&client->list);
363 INIT_FLIST_HEAD(&client->hash_list);
364 INIT_FLIST_HEAD(&client->arg_list);
365 INIT_FLIST_HEAD(&client->eta_list);
366 INIT_FLIST_HEAD(&client->cmd_list);
368 client->hostname = strdup(hostname);
370 if (type == Fio_client_socket)
371 client->is_sock = true;
375 ipv6 = type == Fio_client_ipv6;
376 if (fio_server_parse_host(hostname, ipv6,
377 &client->addr.sin_addr,
378 &client->addr6.sin6_addr))
387 client->type = ops->client_type;
389 __fio_client_add_cmd_option(client, "fio");
391 flist_add(&client->list, &client_list);
393 dprint(FD_NET, "client: added <%s>\n", client->hostname);
400 int fio_client_add_ini_file(void *cookie, const char *ini_file, bool remote)
402 struct fio_client *client = cookie;
403 struct client_file *cf;
410 dprint(FD_NET, "client <%s>: add ini %s\n", client->hostname, ini_file);
412 new_size = (client->nr_files + 1) * sizeof(struct client_file);
413 new_files = realloc(client->files, new_size);
417 client->files = new_files;
418 cf = &client->files[client->nr_files];
419 cf->file = strdup(ini_file);
425 int fio_client_add(struct client_ops *ops, const char *hostname, void **cookie)
427 struct fio_client *existing = *cookie;
428 struct fio_client *client;
432 * We always add our "exec" name as the option, hence 1
435 if (existing->argc == 1)
436 flist_add_tail(&existing->arg_list, &arg_list);
438 while (!flist_empty(&arg_list))
439 flist_del_init(arg_list.next);
443 client = malloc(sizeof(*client));
444 memset(client, 0, sizeof(*client));
446 INIT_FLIST_HEAD(&client->list);
447 INIT_FLIST_HEAD(&client->hash_list);
448 INIT_FLIST_HEAD(&client->arg_list);
449 INIT_FLIST_HEAD(&client->eta_list);
450 INIT_FLIST_HEAD(&client->cmd_list);
452 if (fio_server_parse_string(hostname, &client->hostname,
453 &client->is_sock, &client->port,
454 &client->addr.sin_addr,
455 &client->addr6.sin6_addr,
462 client->type = ops->client_type;
464 __fio_client_add_cmd_option(client, "fio");
466 flist_add(&client->list, &client_list);
468 dprint(FD_NET, "client: added <%s>\n", client->hostname);
473 static const char *server_name(struct fio_client *client, char *buf,
479 from = inet_ntop(AF_INET6, (struct sockaddr *) &client->addr6.sin6_addr, buf, bufsize);
480 else if (client->is_sock)
483 from = inet_ntop(AF_INET, (struct sockaddr *) &client->addr.sin_addr, buf, bufsize);
488 static void probe_client(struct fio_client *client)
490 struct cmd_client_probe_pdu pdu;
495 dprint(FD_NET, "client: send probe\n");
498 pdu.flags = __le64_to_cpu(FIO_PROBE_FLAG_ZLIB);
503 sname = server_name(client, buf, sizeof(buf));
504 memset(pdu.server, 0, sizeof(pdu.server));
505 strncpy((char *) pdu.server, sname, sizeof(pdu.server) - 1);
507 fio_net_send_cmd(client->fd, FIO_NET_CMD_PROBE, &pdu, sizeof(pdu), &tag, &client->cmd_list);
510 static int fio_client_connect_ip(struct fio_client *client)
512 struct sockaddr *addr;
517 client->addr6.sin6_family = AF_INET6;
518 client->addr6.sin6_port = htons(client->port);
520 addr = (struct sockaddr *) &client->addr6;
521 socklen = sizeof(client->addr6);
523 client->addr.sin_family = AF_INET;
524 client->addr.sin_port = htons(client->port);
526 addr = (struct sockaddr *) &client->addr;
527 socklen = sizeof(client->addr);
530 fd = socket(domain, SOCK_STREAM, 0);
534 log_err("fio: socket: %s\n", strerror(errno));
538 if (connect(fd, addr, socklen) < 0) {
541 log_err("fio: connect: %s\n", strerror(errno));
542 log_err("fio: failed to connect to %s:%u\n", client->hostname,
551 static int fio_client_connect_sock(struct fio_client *client)
553 struct sockaddr_un *addr = &client->addr_un;
557 memset(addr, 0, sizeof(*addr));
558 addr->sun_family = AF_UNIX;
559 strncpy(addr->sun_path, client->hostname, sizeof(addr->sun_path) - 1);
561 fd = socket(AF_UNIX, SOCK_STREAM, 0);
565 log_err("fio: socket: %s\n", strerror(errno));
569 len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
570 if (connect(fd, (struct sockaddr *) addr, len) < 0) {
573 log_err("fio: connect; %s\n", strerror(errno));
581 int fio_client_connect(struct fio_client *client)
585 dprint(FD_NET, "client: connect to host %s\n", client->hostname);
588 fd = fio_client_connect_sock(client);
590 fd = fio_client_connect_ip(client);
592 dprint(FD_NET, "client: %s connected %d\n", client->hostname, fd);
598 fio_client_add_hash(client);
599 client->state = Client_connected;
601 probe_client(client);
605 int fio_client_terminate(struct fio_client *client)
607 return fio_net_send_quit(client->fd);
610 static void fio_clients_terminate(void)
612 struct flist_head *entry;
613 struct fio_client *client;
615 dprint(FD_NET, "client: terminate clients\n");
617 flist_for_each(entry, &client_list) {
618 client = flist_entry(entry, struct fio_client, list);
619 fio_client_terminate(client);
623 static void sig_int(int sig)
625 dprint(FD_NET, "client: got signal %d\n", sig);
626 fio_clients_terminate();
629 static void client_signal_handler(void)
631 struct sigaction act;
633 memset(&act, 0, sizeof(act));
634 act.sa_handler = sig_int;
635 act.sa_flags = SA_RESTART;
636 sigaction(SIGINT, &act, NULL);
638 memset(&act, 0, sizeof(act));
639 act.sa_handler = sig_int;
640 act.sa_flags = SA_RESTART;
641 sigaction(SIGTERM, &act, NULL);
643 /* Windows uses SIGBREAK as a quit signal from other applications */
645 memset(&act, 0, sizeof(act));
646 act.sa_handler = sig_int;
647 act.sa_flags = SA_RESTART;
648 sigaction(SIGBREAK, &act, NULL);
651 memset(&act, 0, sizeof(act));
652 act.sa_handler = sig_show_status;
653 act.sa_flags = SA_RESTART;
654 sigaction(SIGUSR1, &act, NULL);
657 static int send_client_cmd_line(struct fio_client *client)
659 struct cmd_single_line_pdu *cslp;
660 struct cmd_line_pdu *clp;
661 unsigned long offset;
667 dprint(FD_NET, "client: send cmdline %d\n", client->argc);
669 lens = malloc(client->argc * sizeof(unsigned int));
672 * Find out how much mem we need
674 for (i = 0, mem = 0; i < client->argc; i++) {
675 lens[i] = strlen(client->argv[i]) + 1;
680 * We need one cmd_line_pdu, and argc number of cmd_single_line_pdu
682 mem += sizeof(*clp) + (client->argc * sizeof(*cslp));
686 offset = sizeof(*clp);
688 for (i = 0; i < client->argc; i++) {
689 uint16_t arg_len = lens[i];
692 strcpy((char *) cslp->text, client->argv[i]);
693 cslp->len = cpu_to_le16(arg_len);
694 offset += sizeof(*cslp) + arg_len;
698 clp->lines = cpu_to_le16(client->argc);
699 clp->client_type = __cpu_to_le16(client->type);
700 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOBLINE, pdu, mem, NULL, NULL);
705 int fio_clients_connect(void)
707 struct fio_client *client;
708 struct flist_head *entry, *tmp;
713 WSAStartup(MAKEWORD(2, 2), &wsd);
716 dprint(FD_NET, "client: connect all\n");
718 client_signal_handler();
720 flist_for_each_safe(entry, tmp, &client_list) {
721 client = flist_entry(entry, struct fio_client, list);
723 ret = fio_client_connect(client);
725 remove_client(client);
729 if (client->argc > 1)
730 send_client_cmd_line(client);
736 int fio_start_client(struct fio_client *client)
738 dprint(FD_NET, "client: start %s\n", client->hostname);
739 return fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_RUN, 0, NULL);
742 int fio_start_all_clients(void)
744 struct fio_client *client;
745 struct flist_head *entry, *tmp;
748 dprint(FD_NET, "client: start all\n");
750 fio_client_json_init();
752 flist_for_each_safe(entry, tmp, &client_list) {
753 client = flist_entry(entry, struct fio_client, list);
755 ret = fio_start_client(client);
757 remove_client(client);
762 return flist_empty(&client_list);
765 static int __fio_client_send_remote_ini(struct fio_client *client,
766 const char *filename)
768 struct cmd_load_file_pdu *pdu;
772 dprint(FD_NET, "send remote ini %s to %s\n", filename, client->hostname);
774 p_size = sizeof(*pdu) + strlen(filename) + 1;
775 pdu = malloc(p_size);
776 memset(pdu, 0, p_size);
777 pdu->name_len = strlen(filename);
778 strcpy((char *) pdu->file, filename);
779 pdu->client_type = cpu_to_le16((uint16_t) client->type);
781 client->sent_job = true;
782 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_LOAD_FILE, pdu, p_size,NULL, NULL);
788 * Send file contents to server backend. We could use sendfile(), but to remain
789 * more portable lets just read/write the darn thing.
791 static int __fio_client_send_local_ini(struct fio_client *client,
792 const char *filename)
794 struct cmd_job_pdu *pdu;
802 dprint(FD_NET, "send ini %s to %s\n", filename, client->hostname);
804 fd = open(filename, O_RDONLY);
807 log_err("fio: job file <%s> open: %s\n", filename, strerror(errno));
811 if (fstat(fd, &sb) < 0) {
813 log_err("fio: job file stat: %s\n", strerror(errno));
819 * Add extra space for variable expansion, but doesn't guarantee.
821 sb.st_size += OPT_LEN_MAX;
822 p_size = sb.st_size + sizeof(*pdu);
823 pdu = malloc(p_size);
828 if (read_ini_data(fd, p, len)) {
829 log_err("fio: failed reading job file %s\n", filename);
835 pdu->buf_len = __cpu_to_le32(sb.st_size);
836 pdu->client_type = cpu_to_le32(client->type);
838 client->sent_job = true;
839 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, pdu, p_size, NULL, NULL);
845 int fio_client_send_ini(struct fio_client *client, const char *filename,
851 ret = __fio_client_send_local_ini(client, filename);
853 ret = __fio_client_send_remote_ini(client, filename);
856 client->sent_job = true;
861 static int fio_client_send_cf(struct fio_client *client,
862 struct client_file *cf)
864 return fio_client_send_ini(client, cf->file, cf->remote);
867 int fio_clients_send_ini(const char *filename)
869 struct fio_client *client;
870 struct flist_head *entry, *tmp;
872 flist_for_each_safe(entry, tmp, &client_list) {
875 client = flist_entry(entry, struct fio_client, list);
877 if (client->nr_files) {
880 for (i = 0; i < client->nr_files; i++) {
881 struct client_file *cf;
883 cf = &client->files[i];
885 if (fio_client_send_cf(client, cf)) {
887 remove_client(client);
892 if (client->sent_job || failed)
894 if (!filename || fio_client_send_ini(client, filename, 0))
895 remove_client(client);
901 int fio_client_update_options(struct fio_client *client,
902 struct thread_options *o, uint64_t *tag)
904 struct cmd_add_job_pdu pdu;
906 pdu.thread_number = cpu_to_le32(client->thread_number);
907 pdu.groupid = cpu_to_le32(client->groupid);
908 convert_thread_options_to_net(&pdu.top, o);
910 return fio_net_send_cmd(client->fd, FIO_NET_CMD_UPDATE_JOB, &pdu, sizeof(pdu), tag, &client->cmd_list);
913 static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
915 dst->max_val = le64_to_cpu(src->max_val);
916 dst->min_val = le64_to_cpu(src->min_val);
917 dst->samples = le64_to_cpu(src->samples);
920 * Floats arrive as IEEE 754 encoded uint64_t, convert back to double
922 dst->mean.u.f = fio_uint64_to_double(le64_to_cpu(dst->mean.u.i));
923 dst->S.u.f = fio_uint64_to_double(le64_to_cpu(dst->S.u.i));
926 static void convert_ts(struct thread_stat *dst, struct thread_stat *src)
930 dst->error = le32_to_cpu(src->error);
931 dst->thread_number = le32_to_cpu(src->thread_number);
932 dst->groupid = le32_to_cpu(src->groupid);
933 dst->pid = le32_to_cpu(src->pid);
934 dst->members = le32_to_cpu(src->members);
935 dst->unified_rw_rep = le32_to_cpu(src->unified_rw_rep);
937 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
938 convert_io_stat(&dst->clat_stat[i], &src->clat_stat[i]);
939 convert_io_stat(&dst->slat_stat[i], &src->slat_stat[i]);
940 convert_io_stat(&dst->lat_stat[i], &src->lat_stat[i]);
941 convert_io_stat(&dst->bw_stat[i], &src->bw_stat[i]);
942 convert_io_stat(&dst->iops_stat[i], &src->iops_stat[i]);
945 dst->usr_time = le64_to_cpu(src->usr_time);
946 dst->sys_time = le64_to_cpu(src->sys_time);
947 dst->ctx = le64_to_cpu(src->ctx);
948 dst->minf = le64_to_cpu(src->minf);
949 dst->majf = le64_to_cpu(src->majf);
950 dst->clat_percentiles = le32_to_cpu(src->clat_percentiles);
951 dst->lat_percentiles = le32_to_cpu(src->lat_percentiles);
952 dst->percentile_precision = le64_to_cpu(src->percentile_precision);
954 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
955 fio_fp64_t *fps = &src->percentile_list[i];
956 fio_fp64_t *fpd = &dst->percentile_list[i];
958 fpd->u.f = fio_uint64_to_double(le64_to_cpu(fps->u.i));
961 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
962 dst->io_u_map[i] = le64_to_cpu(src->io_u_map[i]);
963 dst->io_u_submit[i] = le64_to_cpu(src->io_u_submit[i]);
964 dst->io_u_complete[i] = le64_to_cpu(src->io_u_complete[i]);
967 for (i = 0; i < FIO_IO_U_LAT_N_NR; i++)
968 dst->io_u_lat_n[i] = le64_to_cpu(src->io_u_lat_n[i]);
969 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++)
970 dst->io_u_lat_u[i] = le64_to_cpu(src->io_u_lat_u[i]);
971 for (i = 0; i < FIO_IO_U_LAT_M_NR; i++)
972 dst->io_u_lat_m[i] = le64_to_cpu(src->io_u_lat_m[i]);
974 for (i = 0; i < DDIR_RWDIR_CNT; i++)
975 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
976 dst->io_u_plat[i][j] = le64_to_cpu(src->io_u_plat[i][j]);
978 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
979 dst->total_io_u[i] = le64_to_cpu(src->total_io_u[i]);
980 dst->short_io_u[i] = le64_to_cpu(src->short_io_u[i]);
981 dst->drop_io_u[i] = le64_to_cpu(src->drop_io_u[i]);
984 dst->total_submit = le64_to_cpu(src->total_submit);
985 dst->total_complete = le64_to_cpu(src->total_complete);
987 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
988 dst->io_bytes[i] = le64_to_cpu(src->io_bytes[i]);
989 dst->runtime[i] = le64_to_cpu(src->runtime[i]);
992 dst->total_run_time = le64_to_cpu(src->total_run_time);
993 dst->continue_on_error = le16_to_cpu(src->continue_on_error);
994 dst->total_err_count = le64_to_cpu(src->total_err_count);
995 dst->first_error = le32_to_cpu(src->first_error);
996 dst->kb_base = le32_to_cpu(src->kb_base);
997 dst->unit_base = le32_to_cpu(src->unit_base);
999 dst->sig_figs = le32_to_cpu(src->sig_figs);
1001 dst->latency_depth = le32_to_cpu(src->latency_depth);
1002 dst->latency_target = le64_to_cpu(src->latency_target);
1003 dst->latency_window = le64_to_cpu(src->latency_window);
1004 dst->latency_percentile.u.f = fio_uint64_to_double(le64_to_cpu(src->latency_percentile.u.i));
1006 dst->nr_block_infos = le64_to_cpu(src->nr_block_infos);
1007 for (i = 0; i < dst->nr_block_infos; i++)
1008 dst->block_infos[i] = le32_to_cpu(src->block_infos[i]);
1010 dst->ss_dur = le64_to_cpu(src->ss_dur);
1011 dst->ss_state = le32_to_cpu(src->ss_state);
1012 dst->ss_head = le32_to_cpu(src->ss_head);
1013 dst->ss_limit.u.f = fio_uint64_to_double(le64_to_cpu(src->ss_limit.u.i));
1014 dst->ss_slope.u.f = fio_uint64_to_double(le64_to_cpu(src->ss_slope.u.i));
1015 dst->ss_deviation.u.f = fio_uint64_to_double(le64_to_cpu(src->ss_deviation.u.i));
1016 dst->ss_criterion.u.f = fio_uint64_to_double(le64_to_cpu(src->ss_criterion.u.i));
1018 if (dst->ss_state & FIO_SS_DATA) {
1019 for (i = 0; i < dst->ss_dur; i++ ) {
1020 dst->ss_iops_data[i] = le64_to_cpu(src->ss_iops_data[i]);
1021 dst->ss_bw_data[i] = le64_to_cpu(src->ss_bw_data[i]);
1026 static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
1030 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1031 dst->max_run[i] = le64_to_cpu(src->max_run[i]);
1032 dst->min_run[i] = le64_to_cpu(src->min_run[i]);
1033 dst->max_bw[i] = le64_to_cpu(src->max_bw[i]);
1034 dst->min_bw[i] = le64_to_cpu(src->min_bw[i]);
1035 dst->iobytes[i] = le64_to_cpu(src->iobytes[i]);
1036 dst->agg[i] = le64_to_cpu(src->agg[i]);
1039 dst->kb_base = le32_to_cpu(src->kb_base);
1040 dst->unit_base = le32_to_cpu(src->unit_base);
1041 dst->sig_figs = le32_to_cpu(src->sig_figs);
1042 dst->groupid = le32_to_cpu(src->groupid);
1043 dst->unified_rw_rep = le32_to_cpu(src->unified_rw_rep);
1046 static void json_object_add_client_info(struct json_object *obj,
1047 struct fio_client *client)
1049 const char *hostname = client->hostname ? client->hostname : "";
1051 json_object_add_value_string(obj, "hostname", hostname);
1052 json_object_add_value_int(obj, "port", client->port);
1055 static void handle_ts(struct fio_client *client, struct fio_net_cmd *cmd)
1057 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
1058 struct flist_head *opt_list = NULL;
1059 struct json_object *tsobj;
1061 if (client->opt_lists && p->ts.thread_number <= client->jobs)
1062 opt_list = &client->opt_lists[p->ts.thread_number - 1];
1064 tsobj = show_thread_status(&p->ts, &p->rs, opt_list, NULL);
1065 client->did_stat = true;
1067 json_object_add_client_info(tsobj, client);
1068 json_array_add_value_object(clients_array, tsobj);
1071 if (sum_stat_clients <= 1)
1074 sum_thread_stats(&client_ts, &p->ts, sum_stat_nr == 1);
1075 sum_group_stats(&client_gs, &p->rs);
1077 client_ts.members++;
1078 client_ts.thread_number = p->ts.thread_number;
1079 client_ts.groupid = p->ts.groupid;
1080 client_ts.unified_rw_rep = p->ts.unified_rw_rep;
1081 client_ts.sig_figs = p->ts.sig_figs;
1083 if (++sum_stat_nr == sum_stat_clients) {
1084 strcpy(client_ts.name, "All clients");
1085 tsobj = show_thread_status(&client_ts, &client_gs, NULL, NULL);
1087 json_object_add_client_info(tsobj, client);
1088 json_array_add_value_object(clients_array, tsobj);
1093 static void handle_gs(struct fio_client *client, struct fio_net_cmd *cmd)
1095 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
1097 if (output_format & FIO_OUTPUT_NORMAL)
1098 show_group_stats(gs, NULL);
1101 static void handle_job_opt(struct fio_client *client, struct fio_net_cmd *cmd)
1103 struct cmd_job_option *pdu = (struct cmd_job_option *) cmd->payload;
1104 struct print_option *p;
1106 if (!job_opt_object)
1109 pdu->global = le16_to_cpu(pdu->global);
1110 pdu->truncated = le16_to_cpu(pdu->truncated);
1111 pdu->groupid = le32_to_cpu(pdu->groupid);
1113 p = malloc(sizeof(*p));
1114 p->name = strdup((char *) pdu->name);
1115 if (pdu->value[0] != '\0')
1116 p->value = strdup((char *) pdu->value);
1121 const char *pos = "";
1126 json_object_add_value_string(job_opt_object, p->name, pos);
1127 } else if (client->opt_lists) {
1128 struct flist_head *opt_list = &client->opt_lists[pdu->groupid];
1130 flist_add_tail(&p->list, opt_list);
1134 static void handle_text(struct fio_client *client, struct fio_net_cmd *cmd)
1136 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
1137 const char *buf = (const char *) pdu->buf;
1141 name = client->name ? client->name : client->hostname;
1143 if (!client->skip_newline)
1144 fprintf(f_out, "<%s> ", name);
1145 ret = fwrite(buf, pdu->buf_len, 1, f_out);
1147 client->skip_newline = strchr(buf, '\n') == NULL;
1150 static void convert_agg(struct disk_util_agg *agg)
1154 for (i = 0; i < 2; i++) {
1155 agg->ios[i] = le64_to_cpu(agg->ios[i]);
1156 agg->merges[i] = le64_to_cpu(agg->merges[i]);
1157 agg->sectors[i] = le64_to_cpu(agg->sectors[i]);
1158 agg->ticks[i] = le64_to_cpu(agg->ticks[i]);
1161 agg->io_ticks = le64_to_cpu(agg->io_ticks);
1162 agg->time_in_queue = le64_to_cpu(agg->time_in_queue);
1163 agg->slavecount = le32_to_cpu(agg->slavecount);
1164 agg->max_util.u.f = fio_uint64_to_double(le64_to_cpu(agg->max_util.u.i));
1167 static void convert_dus(struct disk_util_stat *dus)
1171 for (i = 0; i < 2; i++) {
1172 dus->s.ios[i] = le64_to_cpu(dus->s.ios[i]);
1173 dus->s.merges[i] = le64_to_cpu(dus->s.merges[i]);
1174 dus->s.sectors[i] = le64_to_cpu(dus->s.sectors[i]);
1175 dus->s.ticks[i] = le64_to_cpu(dus->s.ticks[i]);
1178 dus->s.io_ticks = le64_to_cpu(dus->s.io_ticks);
1179 dus->s.time_in_queue = le64_to_cpu(dus->s.time_in_queue);
1180 dus->s.msec = le64_to_cpu(dus->s.msec);
1183 static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd)
1185 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
1187 if (!client->disk_stats_shown) {
1188 client->disk_stats_shown = true;
1189 log_info("\nDisk stats (read/write):\n");
1192 if (output_format & FIO_OUTPUT_JSON) {
1193 struct json_object *duobj;
1194 json_array_add_disk_util(&du->dus, &du->agg, du_array);
1195 duobj = json_array_last_value_object(du_array);
1196 json_object_add_client_info(duobj, client);
1198 if (output_format & FIO_OUTPUT_TERSE)
1199 print_disk_util(&du->dus, &du->agg, 1, NULL);
1200 if (output_format & FIO_OUTPUT_NORMAL)
1201 print_disk_util(&du->dus, &du->agg, 0, NULL);
1204 static void convert_jobs_eta(struct jobs_eta *je)
1208 je->nr_running = le32_to_cpu(je->nr_running);
1209 je->nr_ramp = le32_to_cpu(je->nr_ramp);
1210 je->nr_pending = le32_to_cpu(je->nr_pending);
1211 je->nr_setting_up = le32_to_cpu(je->nr_setting_up);
1212 je->files_open = le32_to_cpu(je->files_open);
1214 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1215 je->m_rate[i] = le64_to_cpu(je->m_rate[i]);
1216 je->t_rate[i] = le64_to_cpu(je->t_rate[i]);
1217 je->m_iops[i] = le32_to_cpu(je->m_iops[i]);
1218 je->t_iops[i] = le32_to_cpu(je->t_iops[i]);
1219 je->rate[i] = le64_to_cpu(je->rate[i]);
1220 je->iops[i] = le32_to_cpu(je->iops[i]);
1223 je->elapsed_sec = le64_to_cpu(je->elapsed_sec);
1224 je->eta_sec = le64_to_cpu(je->eta_sec);
1225 je->nr_threads = le32_to_cpu(je->nr_threads);
1226 je->is_pow2 = le32_to_cpu(je->is_pow2);
1227 je->unit_base = le32_to_cpu(je->unit_base);
1228 je->sig_figs = le32_to_cpu(je->sig_figs);
1231 void fio_client_sum_jobs_eta(struct jobs_eta *dst, struct jobs_eta *je)
1235 dst->nr_running += je->nr_running;
1236 dst->nr_ramp += je->nr_ramp;
1237 dst->nr_pending += je->nr_pending;
1238 dst->nr_setting_up += je->nr_setting_up;
1239 dst->files_open += je->files_open;
1241 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1242 dst->m_rate[i] += je->m_rate[i];
1243 dst->t_rate[i] += je->t_rate[i];
1244 dst->m_iops[i] += je->m_iops[i];
1245 dst->t_iops[i] += je->t_iops[i];
1246 dst->rate[i] += je->rate[i];
1247 dst->iops[i] += je->iops[i];
1250 dst->elapsed_sec += je->elapsed_sec;
1252 if (je->eta_sec > dst->eta_sec)
1253 dst->eta_sec = je->eta_sec;
1255 dst->nr_threads += je->nr_threads;
1258 * This wont be correct for multiple strings, but at least it
1259 * works for the basic cases.
1261 strcpy((char *) dst->run_str, (char *) je->run_str);
1264 static bool remove_reply_cmd(struct fio_client *client, struct fio_net_cmd *cmd)
1266 struct fio_net_cmd_reply *reply = NULL;
1267 struct flist_head *entry;
1269 flist_for_each(entry, &client->cmd_list) {
1270 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
1272 if (cmd->tag == (uintptr_t) reply)
1279 log_err("fio: client: unable to find matching tag (%llx)\n", (unsigned long long) cmd->tag);
1283 flist_del(&reply->list);
1284 cmd->tag = reply->saved_tag;
1289 int fio_client_wait_for_reply(struct fio_client *client, uint64_t tag)
1292 struct fio_net_cmd_reply *reply = NULL;
1293 struct flist_head *entry;
1295 flist_for_each(entry, &client->cmd_list) {
1296 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
1298 if (tag == (uintptr_t) reply)
1313 static void handle_eta(struct fio_client *client, struct fio_net_cmd *cmd)
1315 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
1316 struct client_eta *eta = (struct client_eta *) (uintptr_t) cmd->tag;
1318 dprint(FD_NET, "client: got eta tag %p, %d\n", eta, eta->pending);
1320 assert(client->eta_in_flight == eta);
1322 client->eta_in_flight = NULL;
1323 flist_del_init(&client->eta_list);
1324 client->eta_timeouts = 0;
1326 if (client->ops->jobs_eta)
1327 client->ops->jobs_eta(client, je);
1329 fio_client_sum_jobs_eta(&eta->eta, je);
1330 fio_client_dec_jobs_eta(eta, client->ops->eta);
1333 static void client_flush_hist_samples(FILE *f, int hist_coarseness, void *samples,
1334 uint64_t sample_size)
1336 struct io_sample *s;
1338 uint64_t i, j, nr_samples;
1339 struct io_u_plat_entry *entry;
1340 uint64_t *io_u_plat;
1342 int stride = 1 << hist_coarseness;
1347 s = __get_sample(samples, 0, 0);
1348 log_offset = (s->__ddir & LOG_OFFSET_SAMPLE_BIT) != 0;
1350 nr_samples = sample_size / __log_entry_sz(log_offset);
1352 for (i = 0; i < nr_samples; i++) {
1354 s = (struct io_sample *)((char *)__get_sample(samples, log_offset, i) +
1355 i * sizeof(struct io_u_plat_entry));
1357 entry = s->data.plat_entry;
1358 io_u_plat = entry->io_u_plat;
1360 fprintf(f, "%lu, %u, %llu, ", (unsigned long) s->time,
1361 io_sample_ddir(s), (unsigned long long) s->bs);
1362 for (j = 0; j < FIO_IO_U_PLAT_NR - stride; j += stride) {
1363 fprintf(f, "%llu, ", (unsigned long long)hist_sum(j, stride, io_u_plat, NULL));
1365 fprintf(f, "%llu\n", (unsigned long long)
1366 hist_sum(FIO_IO_U_PLAT_NR - stride, stride, io_u_plat, NULL));
1371 static int fio_client_handle_iolog(struct fio_client *client,
1372 struct fio_net_cmd *cmd)
1374 struct cmd_iolog_pdu *pdu = NULL;
1376 char *log_pathname = NULL;
1379 pdu = convert_iolog(cmd, &store_direct);
1381 log_err("fio: failed converting IO log\n");
1386 /* allocate buffer big enough for next sprintf() call */
1387 log_pathname = malloc(10 + strlen((char *)pdu->name) +
1388 strlen(client->hostname));
1389 if (!log_pathname) {
1390 log_err("fio: memory allocation of unique pathname failed\n");
1394 /* generate a unique pathname for the log file using hostname */
1395 sprintf(log_pathname, "%s.%s", pdu->name, client->hostname);
1402 fd = open((const char *) log_pathname,
1403 O_WRONLY | O_CREAT | O_TRUNC, 0644);
1405 log_err("fio: open log %s: %s\n",
1406 log_pathname, strerror(errno));
1411 sz = cmd->pdu_len - sizeof(*pdu);
1412 wrote = write(fd, pdu->samples, sz);
1416 log_err("fio: short write on compressed log\n");
1424 f = fopen((const char *) log_pathname, "w");
1426 log_err("fio: fopen log %s : %s\n",
1427 log_pathname, strerror(errno));
1432 if (pdu->log_type == IO_LOG_TYPE_HIST) {
1433 client_flush_hist_samples(f, pdu->log_hist_coarseness, pdu->samples,
1434 pdu->nr_samples * sizeof(struct io_sample));
1436 flush_samples(f, pdu->samples,
1437 pdu->nr_samples * sizeof(struct io_sample));
1444 if (pdu && pdu != (void *) cmd->payload)
1453 static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd)
1455 struct cmd_probe_reply_pdu *probe = (struct cmd_probe_reply_pdu *) cmd->payload;
1456 const char *os, *arch;
1459 os = fio_get_os_string(probe->os);
1463 arch = fio_get_arch_string(probe->arch);
1467 sprintf(bit, "%d-bit", probe->bpp * 8);
1468 probe->flags = le64_to_cpu(probe->flags);
1470 log_info("hostname=%s, be=%u, %s, os=%s, arch=%s, fio=%s, flags=%lx\n",
1471 probe->hostname, probe->bigendian, bit, os, arch,
1472 probe->fio_version, (unsigned long) probe->flags);
1475 client->name = strdup((char *) probe->hostname);
1478 static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd)
1480 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
1482 client->state = Client_started;
1483 client->jobs = le32_to_cpu(pdu->jobs);
1484 client->nr_stat = le32_to_cpu(pdu->stat_outputs);
1489 if (client->opt_lists)
1490 free(client->opt_lists);
1492 client->opt_lists = malloc(client->jobs * sizeof(struct flist_head));
1493 for (i = 0; i < client->jobs; i++)
1494 INIT_FLIST_HEAD(&client->opt_lists[i]);
1497 sum_stat_clients += client->nr_stat;
1500 static void handle_stop(struct fio_client *client)
1503 log_info("client <%s>: exited with error %d\n", client->hostname, client->error);
1506 static void convert_stop(struct fio_net_cmd *cmd)
1508 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
1510 pdu->error = le32_to_cpu(pdu->error);
1513 static void convert_text(struct fio_net_cmd *cmd)
1515 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
1517 pdu->level = le32_to_cpu(pdu->level);
1518 pdu->buf_len = le32_to_cpu(pdu->buf_len);
1519 pdu->log_sec = le64_to_cpu(pdu->log_sec);
1520 pdu->log_usec = le64_to_cpu(pdu->log_usec);
1523 static struct cmd_iolog_pdu *convert_iolog_gz(struct fio_net_cmd *cmd,
1524 struct cmd_iolog_pdu *pdu)
1527 struct cmd_iolog_pdu *ret;
1529 uint32_t nr_samples;
1533 stream.zalloc = Z_NULL;
1534 stream.zfree = Z_NULL;
1535 stream.opaque = Z_NULL;
1536 stream.avail_in = 0;
1537 stream.next_in = Z_NULL;
1539 if (inflateInit(&stream) != Z_OK)
1543 * Get header first, it's not compressed
1545 nr_samples = le64_to_cpu(pdu->nr_samples);
1547 if (pdu->log_type == IO_LOG_TYPE_HIST)
1548 total = nr_samples * (__log_entry_sz(le32_to_cpu(pdu->log_offset)) +
1549 sizeof(struct io_u_plat_entry));
1551 total = nr_samples * __log_entry_sz(le32_to_cpu(pdu->log_offset));
1552 ret = malloc(total + sizeof(*pdu));
1553 ret->nr_samples = nr_samples;
1555 memcpy(ret, pdu, sizeof(*pdu));
1557 p = (char *) ret + sizeof(*pdu);
1559 stream.avail_in = cmd->pdu_len - sizeof(*pdu);
1560 stream.next_in = (void *)((char *) pdu + sizeof(*pdu));
1561 while (stream.avail_in) {
1562 unsigned int this_chunk = 65536;
1563 unsigned int this_len;
1566 if (this_chunk > total)
1569 stream.avail_out = this_chunk;
1570 stream.next_out = (void *)p;
1571 err = inflate(&stream, Z_NO_FLUSH);
1572 /* may be Z_OK, or Z_STREAM_END */
1574 log_err("fio: inflate error %d\n", err);
1580 this_len = this_chunk - stream.avail_out;
1586 inflateEnd(&stream);
1594 * This has been compressed on the server side, since it can be big.
1597 static struct cmd_iolog_pdu *convert_iolog(struct fio_net_cmd *cmd,
1600 struct cmd_iolog_pdu *pdu = (struct cmd_iolog_pdu *) cmd->payload;
1601 struct cmd_iolog_pdu *ret;
1606 *store_direct = false;
1609 * Convert if compressed and we support it. If it's not
1610 * compressed, we need not do anything.
1612 compressed = le32_to_cpu(pdu->compressed);
1613 if (compressed == XMIT_COMPRESSED) {
1615 log_err("fio: server sent compressed data by mistake\n");
1618 ret = convert_iolog_gz(cmd, pdu);
1620 log_err("fio: failed decompressing log\n");
1623 } else if (compressed == STORE_COMPRESSED) {
1624 *store_direct = true;
1629 ret->nr_samples = le64_to_cpu(ret->nr_samples);
1630 ret->thread_number = le32_to_cpu(ret->thread_number);
1631 ret->log_type = le32_to_cpu(ret->log_type);
1632 ret->compressed = le32_to_cpu(ret->compressed);
1633 ret->log_offset = le32_to_cpu(ret->log_offset);
1634 ret->log_hist_coarseness = le32_to_cpu(ret->log_hist_coarseness);
1639 samples = &ret->samples[0];
1640 for (i = 0; i < ret->nr_samples; i++) {
1641 struct io_sample *s;
1643 s = __get_sample(samples, ret->log_offset, i);
1644 if (ret->log_type == IO_LOG_TYPE_HIST)
1645 s = (struct io_sample *)((char *)s + sizeof(struct io_u_plat_entry) * i);
1647 s->time = le64_to_cpu(s->time);
1648 s->data.val = le64_to_cpu(s->data.val);
1649 s->__ddir = le32_to_cpu(s->__ddir);
1650 s->bs = le64_to_cpu(s->bs);
1652 if (ret->log_offset) {
1653 struct io_sample_offset *so = (void *) s;
1655 so->offset = le64_to_cpu(so->offset);
1658 if (ret->log_type == IO_LOG_TYPE_HIST) {
1659 s->data.plat_entry = (struct io_u_plat_entry *)(((char *)s) + sizeof(*s));
1660 s->data.plat_entry->list.next = NULL;
1661 s->data.plat_entry->list.prev = NULL;
1668 static void sendfile_reply(int fd, struct cmd_sendfile_reply *rep,
1669 size_t size, uint64_t tag)
1671 rep->error = cpu_to_le32(rep->error);
1672 fio_net_send_cmd(fd, FIO_NET_CMD_SENDFILE, rep, size, &tag, NULL);
1675 static int fio_send_file(struct fio_client *client, struct cmd_sendfile *pdu,
1678 struct cmd_sendfile_reply *rep;
1683 size = sizeof(*rep);
1686 if (stat((char *)pdu->path, &sb) < 0) {
1689 sendfile_reply(client->fd, rep, size, tag);
1695 rep = realloc(rep, size);
1696 rep->size = cpu_to_le32((uint32_t) sb.st_size);
1698 fd = open((char *)pdu->path, O_RDONLY);
1702 rep->error = read_data(fd, &rep->data, sb.st_size);
1703 sendfile_reply(client->fd, rep, size, tag);
1709 int fio_handle_client(struct fio_client *client)
1711 struct client_ops *ops = client->ops;
1712 struct fio_net_cmd *cmd;
1715 dprint(FD_NET, "client: handle %s\n", client->hostname);
1717 cmd = fio_net_recv_cmd(client->fd, true);
1721 dprint(FD_NET, "client: got cmd op %s from %s (pdu=%u)\n",
1722 fio_server_op(cmd->opcode), client->hostname, cmd->pdu_len);
1724 client->last_cmd = cmd->opcode;
1726 switch (cmd->opcode) {
1727 case FIO_NET_CMD_QUIT:
1729 ops->quit(client, cmd);
1730 remove_client(client);
1732 case FIO_NET_CMD_TEXT:
1734 ops->text(client, cmd);
1736 case FIO_NET_CMD_DU: {
1737 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
1739 convert_dus(&du->dus);
1740 convert_agg(&du->agg);
1742 ops->disk_util(client, cmd);
1745 case FIO_NET_CMD_TS: {
1746 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
1748 dprint(FD_NET, "client: ts->ss_state = %u\n", (unsigned int) le32_to_cpu(p->ts.ss_state));
1749 if (le32_to_cpu(p->ts.ss_state) & FIO_SS_DATA) {
1750 dprint(FD_NET, "client: received steadystate ring buffers\n");
1752 size = le64_to_cpu(p->ts.ss_dur);
1753 p->ts.ss_iops_data = (uint64_t *) ((struct cmd_ts_pdu *)cmd->payload + 1);
1754 p->ts.ss_bw_data = p->ts.ss_iops_data + size;
1757 convert_ts(&p->ts, &p->ts);
1758 convert_gs(&p->rs, &p->rs);
1760 ops->thread_status(client, cmd);
1763 case FIO_NET_CMD_GS: {
1764 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
1768 ops->group_stats(client, cmd);
1771 case FIO_NET_CMD_ETA: {
1772 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
1774 if (!remove_reply_cmd(client, cmd))
1776 convert_jobs_eta(je);
1777 handle_eta(client, cmd);
1780 case FIO_NET_CMD_PROBE:
1781 remove_reply_cmd(client, cmd);
1782 ops->probe(client, cmd);
1784 case FIO_NET_CMD_SERVER_START:
1785 client->state = Client_running;
1787 ops->job_start(client, cmd);
1789 case FIO_NET_CMD_START: {
1790 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
1792 pdu->jobs = le32_to_cpu(pdu->jobs);
1793 ops->start(client, cmd);
1796 case FIO_NET_CMD_STOP: {
1797 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
1800 client->state = Client_stopped;
1801 client->error = le32_to_cpu(pdu->error);
1802 client->signal = le32_to_cpu(pdu->signal);
1806 case FIO_NET_CMD_ADD_JOB: {
1807 struct cmd_add_job_pdu *pdu = (struct cmd_add_job_pdu *) cmd->payload;
1809 client->thread_number = le32_to_cpu(pdu->thread_number);
1810 client->groupid = le32_to_cpu(pdu->groupid);
1813 ops->add_job(client, cmd);
1816 case FIO_NET_CMD_IOLOG:
1817 fio_client_handle_iolog(client, cmd);
1819 case FIO_NET_CMD_UPDATE_JOB:
1820 ops->update_job(client, cmd);
1821 remove_reply_cmd(client, cmd);
1823 case FIO_NET_CMD_VTRIGGER: {
1824 struct all_io_list *pdu = (struct all_io_list *) cmd->payload;
1829 strcpy(buf, aux_path);
1833 __verify_save_state(pdu, server_name(client, &buf[off], sizeof(buf) - off));
1834 exec_trigger(trigger_cmd);
1837 case FIO_NET_CMD_SENDFILE: {
1838 struct cmd_sendfile *pdu = (struct cmd_sendfile *) cmd->payload;
1839 fio_send_file(client, pdu, cmd->tag);
1842 case FIO_NET_CMD_JOB_OPT: {
1843 handle_job_opt(client, cmd);
1847 log_err("fio: unknown client op: %s\n", fio_server_op(cmd->opcode));
1855 int fio_clients_send_trigger(const char *cmd)
1857 struct flist_head *entry;
1858 struct fio_client *client;
1861 dprint(FD_NET, "client: send vtrigger: %s\n", cmd);
1868 flist_for_each(entry, &client_list) {
1869 struct cmd_vtrigger_pdu *pdu;
1871 client = flist_entry(entry, struct fio_client, list);
1873 pdu = malloc(sizeof(*pdu) + slen);
1874 pdu->len = cpu_to_le16((uint16_t) slen);
1876 memcpy(pdu->cmd, cmd, slen);
1877 fio_net_send_cmd(client->fd, FIO_NET_CMD_VTRIGGER, pdu,
1878 sizeof(*pdu) + slen, NULL, NULL);
1885 static void request_client_etas(struct client_ops *ops)
1887 struct fio_client *client;
1888 struct flist_head *entry;
1889 struct client_eta *eta;
1892 if (eta_print == FIO_ETA_NEVER)
1895 dprint(FD_NET, "client: request eta (%d)\n", nr_clients);
1897 eta = calloc(1, sizeof(*eta) + __THREAD_RUNSTR_SZ(REAL_MAX_JOBS));
1898 eta->pending = nr_clients;
1900 flist_for_each(entry, &client_list) {
1901 client = flist_entry(entry, struct fio_client, list);
1903 if (!flist_empty(&client->eta_list)) {
1907 if (client->state != Client_running)
1910 assert(!client->eta_in_flight);
1911 flist_add_tail(&client->eta_list, &eta_list);
1912 client->eta_in_flight = eta;
1913 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_SEND_ETA,
1914 (uintptr_t) eta, &client->cmd_list);
1918 if (!fio_client_dec_jobs_eta(eta, ops->eta))
1922 dprint(FD_NET, "client: requested eta tag %p\n", eta);
1926 * A single SEND_ETA timeout isn't fatal. Attempt to recover.
1928 static int handle_cmd_timeout(struct fio_client *client,
1929 struct fio_net_cmd_reply *reply)
1931 uint16_t reply_opcode = reply->opcode;
1933 flist_del(&reply->list);
1936 if (reply_opcode != FIO_NET_CMD_SEND_ETA)
1939 log_info("client <%s>: timeout on SEND_ETA\n", client->hostname);
1941 flist_del_init(&client->eta_list);
1942 if (client->eta_in_flight) {
1943 fio_client_dec_jobs_eta(client->eta_in_flight, client->ops->eta);
1944 client->eta_in_flight = NULL;
1948 * If we fail 5 in a row, give up...
1950 if (client->eta_timeouts++ > 5)
1956 static int client_check_cmd_timeout(struct fio_client *client,
1957 struct timespec *now)
1959 struct fio_net_cmd_reply *reply;
1960 struct flist_head *entry, *tmp;
1963 flist_for_each_safe(entry, tmp, &client->cmd_list) {
1966 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
1968 if (mtime_since(&reply->ts, now) < FIO_NET_CLIENT_TIMEOUT)
1972 if (!handle_cmd_timeout(client, reply))
1975 log_err("fio: client %s, timeout on cmd %s\n", client->hostname,
1980 return flist_empty(&client->cmd_list) && ret;
1983 static int fio_check_clients_timed_out(void)
1985 struct fio_client *client;
1986 struct flist_head *entry, *tmp;
1990 fio_gettime(&ts, NULL);
1992 flist_for_each_safe(entry, tmp, &client_list) {
1993 client = flist_entry(entry, struct fio_client, list);
1995 if (flist_empty(&client->cmd_list))
1998 if (!client_check_cmd_timeout(client, &ts))
2001 if (client->ops->timed_out)
2002 client->ops->timed_out(client);
2004 log_err("fio: client %s timed out\n", client->hostname);
2006 if (client->last_cmd != FIO_NET_CMD_VTRIGGER)
2007 client->error = ETIMEDOUT;
2009 log_info("fio: ignoring timeout due to vtrigger\n");
2010 remove_client(client);
2017 int fio_handle_clients(struct client_ops *ops)
2019 struct pollfd *pfds;
2020 int i, ret = 0, retval = 0;
2022 fio_gettime(&eta_ts, NULL);
2024 pfds = malloc(nr_clients * sizeof(struct pollfd));
2026 init_thread_stat(&client_ts);
2027 init_group_run_stat(&client_gs);
2029 while (!exit_backend && nr_clients) {
2030 struct flist_head *entry, *tmp;
2031 struct fio_client *client;
2034 flist_for_each_safe(entry, tmp, &client_list) {
2035 client = flist_entry(entry, struct fio_client, list);
2037 if (!client->sent_job && !client->ops->stay_connected &&
2038 flist_empty(&client->cmd_list)) {
2039 remove_client(client);
2043 pfds[i].fd = client->fd;
2044 pfds[i].events = POLLIN;
2051 assert(i == nr_clients);
2057 fio_gettime(&ts, NULL);
2058 if (eta_time_within_slack(mtime_since(&eta_ts, &ts))) {
2059 request_client_etas(ops);
2060 memcpy(&eta_ts, &ts, sizeof(ts));
2062 if (fio_check_clients_timed_out())
2066 check_trigger_file();
2068 timeout = min(100u, ops->eta_msec);
2070 ret = poll(pfds, nr_clients, timeout);
2074 log_err("fio: poll clients: %s\n", strerror(errno));
2080 for (i = 0; i < nr_clients; i++) {
2081 if (!(pfds[i].revents & POLLIN))
2084 client = find_client_by_fd(pfds[i].fd);
2086 log_err("fio: unknown client fd %ld\n", (long) pfds[i].fd);
2089 if (!fio_handle_client(client)) {
2090 log_info("client: host=%s disconnected\n",
2092 remove_client(client);
2094 } else if (client->error)
2096 fio_put_client(client);
2100 fio_client_json_fini();
2103 return retval || error_clients;