11 #include <sys/socket.h>
13 #include <netinet/in.h>
14 #include <arpa/inet.h>
28 static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd);
29 static void handle_ts(struct fio_client *client, struct fio_net_cmd *cmd);
30 static void handle_gs(struct fio_client *client, struct fio_net_cmd *cmd);
31 static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd);
32 static void handle_text(struct fio_client *client, struct fio_net_cmd *cmd);
33 static void handle_stop(struct fio_client *client, struct fio_net_cmd *cmd);
34 static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd);
36 static void convert_text(struct fio_net_cmd *cmd);
38 struct client_ops fio_client_ops = {
40 .disk_util = handle_du,
41 .thread_status = handle_ts,
42 .group_stats = handle_gs,
44 .start = handle_start,
45 .eta = display_thread_status,
46 .probe = handle_probe,
47 .eta_msec = FIO_CLIENT_DEF_ETA_MSEC,
48 .client_type = FIO_CLIENT_TYPE_CLI,
51 static struct timespec eta_ts;
53 static FLIST_HEAD(client_list);
54 static FLIST_HEAD(eta_list);
56 static FLIST_HEAD(arg_list);
58 struct thread_stat client_ts;
59 struct group_run_stats client_gs;
62 static int sum_stat_nr;
63 static struct json_object *root = NULL;
64 static struct json_object *job_opt_object = NULL;
65 static struct json_array *clients_array = NULL;
66 static struct json_array *du_array = NULL;
68 static int error_clients;
70 #define FIO_CLIENT_HASH_BITS 7
71 #define FIO_CLIENT_HASH_SZ (1 << FIO_CLIENT_HASH_BITS)
72 #define FIO_CLIENT_HASH_MASK (FIO_CLIENT_HASH_SZ - 1)
73 static struct flist_head client_hash[FIO_CLIENT_HASH_SZ];
75 static struct cmd_iolog_pdu *convert_iolog(struct fio_net_cmd *, bool *);
77 static void fio_client_add_hash(struct fio_client *client)
79 int bucket = hash_long(client->fd, FIO_CLIENT_HASH_BITS);
81 bucket &= FIO_CLIENT_HASH_MASK;
82 flist_add(&client->hash_list, &client_hash[bucket]);
85 static void fio_client_remove_hash(struct fio_client *client)
87 if (!flist_empty(&client->hash_list))
88 flist_del_init(&client->hash_list);
91 static void fio_init fio_client_hash_init(void)
95 for (i = 0; i < FIO_CLIENT_HASH_SZ; i++)
96 INIT_FLIST_HEAD(&client_hash[i]);
99 static int read_data(int fd, void *data, size_t size)
104 ret = read(fd, data, size);
106 if (errno == EAGAIN || errno == EINTR)
123 static void fio_client_json_init(void)
128 if (!(output_format & FIO_OUTPUT_JSON))
132 os_ctime_r((const time_t *) &time_p, time_buf, sizeof(time_buf));
133 time_buf[strlen(time_buf) - 1] = '\0';
135 root = json_create_object();
136 json_object_add_value_string(root, "fio version", fio_version_string);
137 json_object_add_value_int(root, "timestamp", time_p);
138 json_object_add_value_string(root, "time", time_buf);
140 job_opt_object = json_create_object();
141 json_object_add_value_object(root, "global options", job_opt_object);
142 clients_array = json_create_array();
143 json_object_add_value_array(root, "client_stats", clients_array);
144 du_array = json_create_array();
145 json_object_add_value_array(root, "disk_util", du_array);
148 static void fio_client_json_fini(void)
150 if (!(output_format & FIO_OUTPUT_JSON))
154 json_print_object(root, NULL);
156 json_free_object(root);
158 clients_array = NULL;
162 static struct fio_client *find_client_by_fd(int fd)
164 int bucket = hash_long(fd, FIO_CLIENT_HASH_BITS) & FIO_CLIENT_HASH_MASK;
165 struct fio_client *client;
166 struct flist_head *entry;
168 flist_for_each(entry, &client_hash[bucket]) {
169 client = flist_entry(entry, struct fio_client, hash_list);
171 if (client->fd == fd) {
180 void fio_put_client(struct fio_client *client)
185 free(client->hostname);
190 while (client->nr_files) {
191 struct client_file *cf = &client->files[--client->nr_files];
197 if (client->opt_lists)
198 free(client->opt_lists);
200 if (!client->did_stat)
209 static int fio_client_dec_jobs_eta(struct client_eta *eta, client_eta_op eta_fn)
211 if (!--eta->pending) {
220 static void fio_drain_client_text(struct fio_client *client)
223 struct fio_net_cmd *cmd;
225 cmd = fio_net_recv_cmd(client->fd, false);
229 if (cmd->opcode == FIO_NET_CMD_TEXT) {
231 client->ops->text(client, cmd);
238 static void remove_client(struct fio_client *client)
240 assert(client->refs);
242 dprint(FD_NET, "client: removed <%s>\n", client->hostname);
244 fio_drain_client_text(client);
246 if (!flist_empty(&client->list))
247 flist_del_init(&client->list);
249 fio_client_remove_hash(client);
251 if (!flist_empty(&client->eta_list)) {
252 flist_del_init(&client->eta_list);
253 fio_client_dec_jobs_eta(client->eta_in_flight, client->ops->eta);
259 if (client->ops->removed)
260 client->ops->removed(client);
263 fio_put_client(client);
266 struct fio_client *fio_get_client(struct fio_client *client)
272 static void __fio_client_add_cmd_option(struct fio_client *client,
277 index = client->argc++;
278 client->argv = realloc(client->argv, sizeof(char *) * client->argc);
279 client->argv[index] = strdup(opt);
280 dprint(FD_NET, "client: add cmd %d: %s\n", index, opt);
283 void fio_client_add_cmd_option(void *cookie, const char *opt)
285 struct fio_client *client = cookie;
286 struct flist_head *entry;
291 __fio_client_add_cmd_option(client, opt);
294 * Duplicate arguments to shared client group
296 flist_for_each(entry, &arg_list) {
297 client = flist_entry(entry, struct fio_client, arg_list);
299 __fio_client_add_cmd_option(client, opt);
303 struct fio_client *fio_client_add_explicit(struct client_ops *ops,
304 const char *hostname, int type,
307 struct fio_client *client;
309 client = malloc(sizeof(*client));
310 memset(client, 0, sizeof(*client));
312 INIT_FLIST_HEAD(&client->list);
313 INIT_FLIST_HEAD(&client->hash_list);
314 INIT_FLIST_HEAD(&client->arg_list);
315 INIT_FLIST_HEAD(&client->eta_list);
316 INIT_FLIST_HEAD(&client->cmd_list);
318 client->hostname = strdup(hostname);
320 if (type == Fio_client_socket)
321 client->is_sock = true;
325 ipv6 = type == Fio_client_ipv6;
326 if (fio_server_parse_host(hostname, ipv6,
327 &client->addr.sin_addr,
328 &client->addr6.sin6_addr))
337 client->type = ops->client_type;
339 __fio_client_add_cmd_option(client, "fio");
341 flist_add(&client->list, &client_list);
343 dprint(FD_NET, "client: added <%s>\n", client->hostname);
350 int fio_client_add_ini_file(void *cookie, const char *ini_file, bool remote)
352 struct fio_client *client = cookie;
353 struct client_file *cf;
360 dprint(FD_NET, "client <%s>: add ini %s\n", client->hostname, ini_file);
362 new_size = (client->nr_files + 1) * sizeof(struct client_file);
363 new_files = realloc(client->files, new_size);
367 client->files = new_files;
368 cf = &client->files[client->nr_files];
369 cf->file = strdup(ini_file);
375 int fio_client_add(struct client_ops *ops, const char *hostname, void **cookie)
377 struct fio_client *existing = *cookie;
378 struct fio_client *client;
382 * We always add our "exec" name as the option, hence 1
385 if (existing->argc == 1)
386 flist_add_tail(&existing->arg_list, &arg_list);
388 while (!flist_empty(&arg_list))
389 flist_del_init(arg_list.next);
393 client = malloc(sizeof(*client));
394 memset(client, 0, sizeof(*client));
396 INIT_FLIST_HEAD(&client->list);
397 INIT_FLIST_HEAD(&client->hash_list);
398 INIT_FLIST_HEAD(&client->arg_list);
399 INIT_FLIST_HEAD(&client->eta_list);
400 INIT_FLIST_HEAD(&client->cmd_list);
402 if (fio_server_parse_string(hostname, &client->hostname,
403 &client->is_sock, &client->port,
404 &client->addr.sin_addr,
405 &client->addr6.sin6_addr,
412 client->type = ops->client_type;
414 __fio_client_add_cmd_option(client, "fio");
416 flist_add(&client->list, &client_list);
418 dprint(FD_NET, "client: added <%s>\n", client->hostname);
423 static const char *server_name(struct fio_client *client, char *buf,
429 from = inet_ntop(AF_INET6, (struct sockaddr *) &client->addr6.sin6_addr, buf, bufsize);
430 else if (client->is_sock)
433 from = inet_ntop(AF_INET, (struct sockaddr *) &client->addr.sin_addr, buf, bufsize);
438 static void probe_client(struct fio_client *client)
440 struct cmd_client_probe_pdu pdu;
445 dprint(FD_NET, "client: send probe\n");
448 pdu.flags = __le64_to_cpu(FIO_PROBE_FLAG_ZLIB);
453 sname = server_name(client, buf, sizeof(buf));
454 memset(pdu.server, 0, sizeof(pdu.server));
455 strncpy((char *) pdu.server, sname, sizeof(pdu.server) - 1);
457 fio_net_send_cmd(client->fd, FIO_NET_CMD_PROBE, &pdu, sizeof(pdu), &tag, &client->cmd_list);
460 static int fio_client_connect_ip(struct fio_client *client)
462 struct sockaddr *addr;
467 client->addr6.sin6_family = AF_INET6;
468 client->addr6.sin6_port = htons(client->port);
470 addr = (struct sockaddr *) &client->addr6;
471 socklen = sizeof(client->addr6);
473 client->addr.sin_family = AF_INET;
474 client->addr.sin_port = htons(client->port);
476 addr = (struct sockaddr *) &client->addr;
477 socklen = sizeof(client->addr);
480 fd = socket(domain, SOCK_STREAM, 0);
484 log_err("fio: socket: %s\n", strerror(errno));
488 if (connect(fd, addr, socklen) < 0) {
491 log_err("fio: connect: %s\n", strerror(errno));
492 log_err("fio: failed to connect to %s:%u\n", client->hostname,
501 static int fio_client_connect_sock(struct fio_client *client)
503 struct sockaddr_un *addr = &client->addr_un;
507 memset(addr, 0, sizeof(*addr));
508 addr->sun_family = AF_UNIX;
509 strncpy(addr->sun_path, client->hostname, sizeof(addr->sun_path) - 1);
511 fd = socket(AF_UNIX, SOCK_STREAM, 0);
515 log_err("fio: socket: %s\n", strerror(errno));
519 len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
520 if (connect(fd, (struct sockaddr *) addr, len) < 0) {
523 log_err("fio: connect; %s\n", strerror(errno));
531 int fio_client_connect(struct fio_client *client)
535 dprint(FD_NET, "client: connect to host %s\n", client->hostname);
538 fd = fio_client_connect_sock(client);
540 fd = fio_client_connect_ip(client);
542 dprint(FD_NET, "client: %s connected %d\n", client->hostname, fd);
548 fio_client_add_hash(client);
549 client->state = Client_connected;
551 probe_client(client);
555 int fio_client_terminate(struct fio_client *client)
557 return fio_net_send_quit(client->fd);
560 static void fio_clients_terminate(void)
562 struct flist_head *entry;
563 struct fio_client *client;
565 dprint(FD_NET, "client: terminate clients\n");
567 flist_for_each(entry, &client_list) {
568 client = flist_entry(entry, struct fio_client, list);
569 fio_client_terminate(client);
573 static void sig_int(int sig)
575 dprint(FD_NET, "client: got signal %d\n", sig);
576 fio_clients_terminate();
579 static void client_signal_handler(void)
581 struct sigaction act;
583 memset(&act, 0, sizeof(act));
584 act.sa_handler = sig_int;
585 act.sa_flags = SA_RESTART;
586 sigaction(SIGINT, &act, NULL);
588 memset(&act, 0, sizeof(act));
589 act.sa_handler = sig_int;
590 act.sa_flags = SA_RESTART;
591 sigaction(SIGTERM, &act, NULL);
593 /* Windows uses SIGBREAK as a quit signal from other applications */
595 memset(&act, 0, sizeof(act));
596 act.sa_handler = sig_int;
597 act.sa_flags = SA_RESTART;
598 sigaction(SIGBREAK, &act, NULL);
601 memset(&act, 0, sizeof(act));
602 act.sa_handler = sig_show_status;
603 act.sa_flags = SA_RESTART;
604 sigaction(SIGUSR1, &act, NULL);
607 static int send_client_cmd_line(struct fio_client *client)
609 struct cmd_single_line_pdu *cslp;
610 struct cmd_line_pdu *clp;
611 unsigned long offset;
617 dprint(FD_NET, "client: send cmdline %d\n", client->argc);
619 lens = malloc(client->argc * sizeof(unsigned int));
622 * Find out how much mem we need
624 for (i = 0, mem = 0; i < client->argc; i++) {
625 lens[i] = strlen(client->argv[i]) + 1;
630 * We need one cmd_line_pdu, and argc number of cmd_single_line_pdu
632 mem += sizeof(*clp) + (client->argc * sizeof(*cslp));
636 offset = sizeof(*clp);
638 for (i = 0; i < client->argc; i++) {
639 uint16_t arg_len = lens[i];
642 strcpy((char *) cslp->text, client->argv[i]);
643 cslp->len = cpu_to_le16(arg_len);
644 offset += sizeof(*cslp) + arg_len;
648 clp->lines = cpu_to_le16(client->argc);
649 clp->client_type = __cpu_to_le16(client->type);
650 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOBLINE, pdu, mem, NULL, NULL);
655 int fio_clients_connect(void)
657 struct fio_client *client;
658 struct flist_head *entry, *tmp;
663 WSAStartup(MAKEWORD(2, 2), &wsd);
666 dprint(FD_NET, "client: connect all\n");
668 client_signal_handler();
670 flist_for_each_safe(entry, tmp, &client_list) {
671 client = flist_entry(entry, struct fio_client, list);
673 ret = fio_client_connect(client);
675 remove_client(client);
679 if (client->argc > 1)
680 send_client_cmd_line(client);
686 int fio_start_client(struct fio_client *client)
688 dprint(FD_NET, "client: start %s\n", client->hostname);
689 return fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_RUN, 0, NULL);
692 int fio_start_all_clients(void)
694 struct fio_client *client;
695 struct flist_head *entry, *tmp;
698 dprint(FD_NET, "client: start all\n");
700 fio_client_json_init();
702 flist_for_each_safe(entry, tmp, &client_list) {
703 client = flist_entry(entry, struct fio_client, list);
705 ret = fio_start_client(client);
707 remove_client(client);
712 return flist_empty(&client_list);
715 static int __fio_client_send_remote_ini(struct fio_client *client,
716 const char *filename)
718 struct cmd_load_file_pdu *pdu;
722 dprint(FD_NET, "send remote ini %s to %s\n", filename, client->hostname);
724 p_size = sizeof(*pdu) + strlen(filename) + 1;
725 pdu = malloc(p_size);
726 memset(pdu, 0, p_size);
727 pdu->name_len = strlen(filename);
728 strcpy((char *) pdu->file, filename);
729 pdu->client_type = cpu_to_le16((uint16_t) client->type);
731 client->sent_job = true;
732 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_LOAD_FILE, pdu, p_size,NULL, NULL);
738 * Send file contents to server backend. We could use sendfile(), but to remain
739 * more portable lets just read/write the darn thing.
741 static int __fio_client_send_local_ini(struct fio_client *client,
742 const char *filename)
744 struct cmd_job_pdu *pdu;
752 dprint(FD_NET, "send ini %s to %s\n", filename, client->hostname);
754 fd = open(filename, O_RDONLY);
757 log_err("fio: job file <%s> open: %s\n", filename, strerror(errno));
761 if (fstat(fd, &sb) < 0) {
763 log_err("fio: job file stat: %s\n", strerror(errno));
768 p_size = sb.st_size + sizeof(*pdu);
769 pdu = malloc(p_size);
774 if (read_data(fd, p, len)) {
775 log_err("fio: failed reading job file %s\n", filename);
781 pdu->buf_len = __cpu_to_le32(sb.st_size);
782 pdu->client_type = cpu_to_le32(client->type);
784 client->sent_job = true;
785 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, pdu, p_size, NULL, NULL);
791 int fio_client_send_ini(struct fio_client *client, const char *filename,
797 ret = __fio_client_send_local_ini(client, filename);
799 ret = __fio_client_send_remote_ini(client, filename);
802 client->sent_job = true;
807 static int fio_client_send_cf(struct fio_client *client,
808 struct client_file *cf)
810 return fio_client_send_ini(client, cf->file, cf->remote);
813 int fio_clients_send_ini(const char *filename)
815 struct fio_client *client;
816 struct flist_head *entry, *tmp;
818 flist_for_each_safe(entry, tmp, &client_list) {
821 client = flist_entry(entry, struct fio_client, list);
823 if (client->nr_files) {
826 for (i = 0; i < client->nr_files; i++) {
827 struct client_file *cf;
829 cf = &client->files[i];
831 if (fio_client_send_cf(client, cf)) {
833 remove_client(client);
838 if (client->sent_job || failed)
840 if (!filename || fio_client_send_ini(client, filename, 0))
841 remove_client(client);
847 int fio_client_update_options(struct fio_client *client,
848 struct thread_options *o, uint64_t *tag)
850 struct cmd_add_job_pdu pdu;
852 pdu.thread_number = cpu_to_le32(client->thread_number);
853 pdu.groupid = cpu_to_le32(client->groupid);
854 convert_thread_options_to_net(&pdu.top, o);
856 return fio_net_send_cmd(client->fd, FIO_NET_CMD_UPDATE_JOB, &pdu, sizeof(pdu), tag, &client->cmd_list);
859 static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
861 dst->max_val = le64_to_cpu(src->max_val);
862 dst->min_val = le64_to_cpu(src->min_val);
863 dst->samples = le64_to_cpu(src->samples);
866 * Floats arrive as IEEE 754 encoded uint64_t, convert back to double
868 dst->mean.u.f = fio_uint64_to_double(le64_to_cpu(dst->mean.u.i));
869 dst->S.u.f = fio_uint64_to_double(le64_to_cpu(dst->S.u.i));
872 static void convert_ts(struct thread_stat *dst, struct thread_stat *src)
876 dst->error = le32_to_cpu(src->error);
877 dst->thread_number = le32_to_cpu(src->thread_number);
878 dst->groupid = le32_to_cpu(src->groupid);
879 dst->pid = le32_to_cpu(src->pid);
880 dst->members = le32_to_cpu(src->members);
881 dst->unified_rw_rep = le32_to_cpu(src->unified_rw_rep);
883 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
884 convert_io_stat(&dst->clat_stat[i], &src->clat_stat[i]);
885 convert_io_stat(&dst->slat_stat[i], &src->slat_stat[i]);
886 convert_io_stat(&dst->lat_stat[i], &src->lat_stat[i]);
887 convert_io_stat(&dst->bw_stat[i], &src->bw_stat[i]);
888 convert_io_stat(&dst->iops_stat[i], &src->iops_stat[i]);
891 dst->usr_time = le64_to_cpu(src->usr_time);
892 dst->sys_time = le64_to_cpu(src->sys_time);
893 dst->ctx = le64_to_cpu(src->ctx);
894 dst->minf = le64_to_cpu(src->minf);
895 dst->majf = le64_to_cpu(src->majf);
896 dst->clat_percentiles = le32_to_cpu(src->clat_percentiles);
897 dst->lat_percentiles = le32_to_cpu(src->lat_percentiles);
898 dst->percentile_precision = le64_to_cpu(src->percentile_precision);
900 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
901 fio_fp64_t *fps = &src->percentile_list[i];
902 fio_fp64_t *fpd = &dst->percentile_list[i];
904 fpd->u.f = fio_uint64_to_double(le64_to_cpu(fps->u.i));
907 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
908 dst->io_u_map[i] = le32_to_cpu(src->io_u_map[i]);
909 dst->io_u_submit[i] = le32_to_cpu(src->io_u_submit[i]);
910 dst->io_u_complete[i] = le32_to_cpu(src->io_u_complete[i]);
913 for (i = 0; i < FIO_IO_U_LAT_N_NR; i++)
914 dst->io_u_lat_n[i] = le32_to_cpu(src->io_u_lat_n[i]);
915 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++)
916 dst->io_u_lat_u[i] = le32_to_cpu(src->io_u_lat_u[i]);
917 for (i = 0; i < FIO_IO_U_LAT_M_NR; i++)
918 dst->io_u_lat_m[i] = le32_to_cpu(src->io_u_lat_m[i]);
920 for (i = 0; i < DDIR_RWDIR_CNT; i++)
921 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
922 dst->io_u_plat[i][j] = le32_to_cpu(src->io_u_plat[i][j]);
924 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
925 dst->total_io_u[i] = le64_to_cpu(src->total_io_u[i]);
926 dst->short_io_u[i] = le64_to_cpu(src->short_io_u[i]);
927 dst->drop_io_u[i] = le64_to_cpu(src->drop_io_u[i]);
930 dst->total_submit = le64_to_cpu(src->total_submit);
931 dst->total_complete = le64_to_cpu(src->total_complete);
933 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
934 dst->io_bytes[i] = le64_to_cpu(src->io_bytes[i]);
935 dst->runtime[i] = le64_to_cpu(src->runtime[i]);
938 dst->total_run_time = le64_to_cpu(src->total_run_time);
939 dst->continue_on_error = le16_to_cpu(src->continue_on_error);
940 dst->total_err_count = le64_to_cpu(src->total_err_count);
941 dst->first_error = le32_to_cpu(src->first_error);
942 dst->kb_base = le32_to_cpu(src->kb_base);
943 dst->unit_base = le32_to_cpu(src->unit_base);
945 dst->sig_figs = le32_to_cpu(src->sig_figs);
947 dst->latency_depth = le32_to_cpu(src->latency_depth);
948 dst->latency_target = le64_to_cpu(src->latency_target);
949 dst->latency_window = le64_to_cpu(src->latency_window);
950 dst->latency_percentile.u.f = fio_uint64_to_double(le64_to_cpu(src->latency_percentile.u.i));
952 dst->nr_block_infos = le64_to_cpu(src->nr_block_infos);
953 for (i = 0; i < dst->nr_block_infos; i++)
954 dst->block_infos[i] = le32_to_cpu(src->block_infos[i]);
956 dst->ss_dur = le64_to_cpu(src->ss_dur);
957 dst->ss_state = le32_to_cpu(src->ss_state);
958 dst->ss_head = le32_to_cpu(src->ss_head);
959 dst->ss_limit.u.f = fio_uint64_to_double(le64_to_cpu(src->ss_limit.u.i));
960 dst->ss_slope.u.f = fio_uint64_to_double(le64_to_cpu(src->ss_slope.u.i));
961 dst->ss_deviation.u.f = fio_uint64_to_double(le64_to_cpu(src->ss_deviation.u.i));
962 dst->ss_criterion.u.f = fio_uint64_to_double(le64_to_cpu(src->ss_criterion.u.i));
964 if (dst->ss_state & FIO_SS_DATA) {
965 for (i = 0; i < dst->ss_dur; i++ ) {
966 dst->ss_iops_data[i] = le64_to_cpu(src->ss_iops_data[i]);
967 dst->ss_bw_data[i] = le64_to_cpu(src->ss_bw_data[i]);
972 static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
976 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
977 dst->max_run[i] = le64_to_cpu(src->max_run[i]);
978 dst->min_run[i] = le64_to_cpu(src->min_run[i]);
979 dst->max_bw[i] = le64_to_cpu(src->max_bw[i]);
980 dst->min_bw[i] = le64_to_cpu(src->min_bw[i]);
981 dst->iobytes[i] = le64_to_cpu(src->iobytes[i]);
982 dst->agg[i] = le64_to_cpu(src->agg[i]);
985 dst->kb_base = le32_to_cpu(src->kb_base);
986 dst->unit_base = le32_to_cpu(src->unit_base);
987 dst->sig_figs = le32_to_cpu(src->sig_figs);
988 dst->groupid = le32_to_cpu(src->groupid);
989 dst->unified_rw_rep = le32_to_cpu(src->unified_rw_rep);
992 static void json_object_add_client_info(struct json_object *obj,
993 struct fio_client *client)
995 const char *hostname = client->hostname ? client->hostname : "";
997 json_object_add_value_string(obj, "hostname", hostname);
998 json_object_add_value_int(obj, "port", client->port);
1001 static void handle_ts(struct fio_client *client, struct fio_net_cmd *cmd)
1003 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
1004 struct flist_head *opt_list = NULL;
1005 struct json_object *tsobj;
1007 if (client->opt_lists && p->ts.thread_number <= client->jobs)
1008 opt_list = &client->opt_lists[p->ts.thread_number - 1];
1010 tsobj = show_thread_status(&p->ts, &p->rs, opt_list, NULL);
1011 client->did_stat = true;
1013 json_object_add_client_info(tsobj, client);
1014 json_array_add_value_object(clients_array, tsobj);
1017 if (sum_stat_clients <= 1)
1020 sum_thread_stats(&client_ts, &p->ts, sum_stat_nr == 1);
1021 sum_group_stats(&client_gs, &p->rs);
1023 client_ts.members++;
1024 client_ts.thread_number = p->ts.thread_number;
1025 client_ts.groupid = p->ts.groupid;
1026 client_ts.unified_rw_rep = p->ts.unified_rw_rep;
1027 client_ts.sig_figs = p->ts.sig_figs;
1029 if (++sum_stat_nr == sum_stat_clients) {
1030 strcpy(client_ts.name, "All clients");
1031 tsobj = show_thread_status(&client_ts, &client_gs, NULL, NULL);
1033 json_object_add_client_info(tsobj, client);
1034 json_array_add_value_object(clients_array, tsobj);
1039 static void handle_gs(struct fio_client *client, struct fio_net_cmd *cmd)
1041 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
1043 if (output_format & FIO_OUTPUT_NORMAL)
1044 show_group_stats(gs, NULL);
1047 static void handle_job_opt(struct fio_client *client, struct fio_net_cmd *cmd)
1049 struct cmd_job_option *pdu = (struct cmd_job_option *) cmd->payload;
1050 struct print_option *p;
1052 if (!job_opt_object)
1055 pdu->global = le16_to_cpu(pdu->global);
1056 pdu->truncated = le16_to_cpu(pdu->truncated);
1057 pdu->groupid = le32_to_cpu(pdu->groupid);
1059 p = malloc(sizeof(*p));
1060 p->name = strdup((char *) pdu->name);
1061 if (pdu->value[0] != '\0')
1062 p->value = strdup((char *) pdu->value);
1067 const char *pos = "";
1072 json_object_add_value_string(job_opt_object, p->name, pos);
1073 } else if (client->opt_lists) {
1074 struct flist_head *opt_list = &client->opt_lists[pdu->groupid];
1076 flist_add_tail(&p->list, opt_list);
1080 static void handle_text(struct fio_client *client, struct fio_net_cmd *cmd)
1082 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
1083 const char *buf = (const char *) pdu->buf;
1087 name = client->name ? client->name : client->hostname;
1089 if (!client->skip_newline)
1090 fprintf(f_out, "<%s> ", name);
1091 ret = fwrite(buf, pdu->buf_len, 1, f_out);
1093 client->skip_newline = strchr(buf, '\n') == NULL;
1096 static void convert_agg(struct disk_util_agg *agg)
1100 for (i = 0; i < 2; i++) {
1101 agg->ios[i] = le64_to_cpu(agg->ios[i]);
1102 agg->merges[i] = le64_to_cpu(agg->merges[i]);
1103 agg->sectors[i] = le64_to_cpu(agg->sectors[i]);
1104 agg->ticks[i] = le64_to_cpu(agg->ticks[i]);
1107 agg->io_ticks = le64_to_cpu(agg->io_ticks);
1108 agg->time_in_queue = le64_to_cpu(agg->time_in_queue);
1109 agg->slavecount = le32_to_cpu(agg->slavecount);
1110 agg->max_util.u.f = fio_uint64_to_double(le64_to_cpu(agg->max_util.u.i));
1113 static void convert_dus(struct disk_util_stat *dus)
1117 for (i = 0; i < 2; i++) {
1118 dus->s.ios[i] = le64_to_cpu(dus->s.ios[i]);
1119 dus->s.merges[i] = le64_to_cpu(dus->s.merges[i]);
1120 dus->s.sectors[i] = le64_to_cpu(dus->s.sectors[i]);
1121 dus->s.ticks[i] = le64_to_cpu(dus->s.ticks[i]);
1124 dus->s.io_ticks = le64_to_cpu(dus->s.io_ticks);
1125 dus->s.time_in_queue = le64_to_cpu(dus->s.time_in_queue);
1126 dus->s.msec = le64_to_cpu(dus->s.msec);
1129 static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd)
1131 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
1133 if (!client->disk_stats_shown) {
1134 client->disk_stats_shown = true;
1135 log_info("\nDisk stats (read/write):\n");
1138 if (output_format & FIO_OUTPUT_JSON) {
1139 struct json_object *duobj;
1140 json_array_add_disk_util(&du->dus, &du->agg, du_array);
1141 duobj = json_array_last_value_object(du_array);
1142 json_object_add_client_info(duobj, client);
1144 if (output_format & FIO_OUTPUT_TERSE)
1145 print_disk_util(&du->dus, &du->agg, 1, NULL);
1146 if (output_format & FIO_OUTPUT_NORMAL)
1147 print_disk_util(&du->dus, &du->agg, 0, NULL);
1150 static void convert_jobs_eta(struct jobs_eta *je)
1154 je->nr_running = le32_to_cpu(je->nr_running);
1155 je->nr_ramp = le32_to_cpu(je->nr_ramp);
1156 je->nr_pending = le32_to_cpu(je->nr_pending);
1157 je->nr_setting_up = le32_to_cpu(je->nr_setting_up);
1158 je->files_open = le32_to_cpu(je->files_open);
1160 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1161 je->m_rate[i] = le64_to_cpu(je->m_rate[i]);
1162 je->t_rate[i] = le64_to_cpu(je->t_rate[i]);
1163 je->m_iops[i] = le32_to_cpu(je->m_iops[i]);
1164 je->t_iops[i] = le32_to_cpu(je->t_iops[i]);
1165 je->rate[i] = le64_to_cpu(je->rate[i]);
1166 je->iops[i] = le32_to_cpu(je->iops[i]);
1169 je->elapsed_sec = le64_to_cpu(je->elapsed_sec);
1170 je->eta_sec = le64_to_cpu(je->eta_sec);
1171 je->nr_threads = le32_to_cpu(je->nr_threads);
1172 je->is_pow2 = le32_to_cpu(je->is_pow2);
1173 je->unit_base = le32_to_cpu(je->unit_base);
1174 je->sig_figs = le32_to_cpu(je->sig_figs);
1177 void fio_client_sum_jobs_eta(struct jobs_eta *dst, struct jobs_eta *je)
1181 dst->nr_running += je->nr_running;
1182 dst->nr_ramp += je->nr_ramp;
1183 dst->nr_pending += je->nr_pending;
1184 dst->nr_setting_up += je->nr_setting_up;
1185 dst->files_open += je->files_open;
1187 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1188 dst->m_rate[i] += je->m_rate[i];
1189 dst->t_rate[i] += je->t_rate[i];
1190 dst->m_iops[i] += je->m_iops[i];
1191 dst->t_iops[i] += je->t_iops[i];
1192 dst->rate[i] += je->rate[i];
1193 dst->iops[i] += je->iops[i];
1196 dst->elapsed_sec += je->elapsed_sec;
1198 if (je->eta_sec > dst->eta_sec)
1199 dst->eta_sec = je->eta_sec;
1201 dst->nr_threads += je->nr_threads;
1204 * This wont be correct for multiple strings, but at least it
1205 * works for the basic cases.
1207 strcpy((char *) dst->run_str, (char *) je->run_str);
1210 static bool remove_reply_cmd(struct fio_client *client, struct fio_net_cmd *cmd)
1212 struct fio_net_cmd_reply *reply = NULL;
1213 struct flist_head *entry;
1215 flist_for_each(entry, &client->cmd_list) {
1216 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
1218 if (cmd->tag == (uintptr_t) reply)
1225 log_err("fio: client: unable to find matching tag (%llx)\n", (unsigned long long) cmd->tag);
1229 flist_del(&reply->list);
1230 cmd->tag = reply->saved_tag;
1235 int fio_client_wait_for_reply(struct fio_client *client, uint64_t tag)
1238 struct fio_net_cmd_reply *reply = NULL;
1239 struct flist_head *entry;
1241 flist_for_each(entry, &client->cmd_list) {
1242 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
1244 if (tag == (uintptr_t) reply)
1259 static void handle_eta(struct fio_client *client, struct fio_net_cmd *cmd)
1261 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
1262 struct client_eta *eta = (struct client_eta *) (uintptr_t) cmd->tag;
1264 dprint(FD_NET, "client: got eta tag %p, %d\n", eta, eta->pending);
1266 assert(client->eta_in_flight == eta);
1268 client->eta_in_flight = NULL;
1269 flist_del_init(&client->eta_list);
1270 client->eta_timeouts = 0;
1272 if (client->ops->jobs_eta)
1273 client->ops->jobs_eta(client, je);
1275 fio_client_sum_jobs_eta(&eta->eta, je);
1276 fio_client_dec_jobs_eta(eta, client->ops->eta);
1279 static void client_flush_hist_samples(FILE *f, int hist_coarseness, void *samples,
1280 uint64_t sample_size)
1282 struct io_sample *s;
1284 uint64_t i, j, nr_samples;
1285 struct io_u_plat_entry *entry;
1286 unsigned int *io_u_plat;
1288 int stride = 1 << hist_coarseness;
1293 s = __get_sample(samples, 0, 0);
1294 log_offset = (s->__ddir & LOG_OFFSET_SAMPLE_BIT) != 0;
1296 nr_samples = sample_size / __log_entry_sz(log_offset);
1298 for (i = 0; i < nr_samples; i++) {
1300 s = (struct io_sample *)((char *)__get_sample(samples, log_offset, i) +
1301 i * sizeof(struct io_u_plat_entry));
1303 entry = s->data.plat_entry;
1304 io_u_plat = entry->io_u_plat;
1306 fprintf(f, "%lu, %u, %u, ", (unsigned long) s->time,
1307 io_sample_ddir(s), s->bs);
1308 for (j = 0; j < FIO_IO_U_PLAT_NR - stride; j += stride) {
1309 fprintf(f, "%lu, ", hist_sum(j, stride, io_u_plat, NULL));
1311 fprintf(f, "%lu\n", (unsigned long)
1312 hist_sum(FIO_IO_U_PLAT_NR - stride, stride, io_u_plat, NULL));
1317 static int fio_client_handle_iolog(struct fio_client *client,
1318 struct fio_net_cmd *cmd)
1320 struct cmd_iolog_pdu *pdu = NULL;
1322 char *log_pathname = NULL;
1325 pdu = convert_iolog(cmd, &store_direct);
1327 log_err("fio: failed converting IO log\n");
1332 /* allocate buffer big enough for next sprintf() call */
1333 log_pathname = malloc(10 + strlen((char *)pdu->name) +
1334 strlen(client->hostname));
1335 if (!log_pathname) {
1336 log_err("fio: memory allocation of unique pathname failed\n");
1340 /* generate a unique pathname for the log file using hostname */
1341 sprintf(log_pathname, "%s.%s", pdu->name, client->hostname);
1348 fd = open((const char *) log_pathname,
1349 O_WRONLY | O_CREAT | O_TRUNC, 0644);
1351 log_err("fio: open log %s: %s\n",
1352 log_pathname, strerror(errno));
1357 sz = cmd->pdu_len - sizeof(*pdu);
1358 ret = write(fd, pdu->samples, sz);
1362 log_err("fio: short write on compressed log\n");
1370 f = fopen((const char *) log_pathname, "w");
1372 log_err("fio: fopen log %s : %s\n",
1373 log_pathname, strerror(errno));
1378 if (pdu->log_type == IO_LOG_TYPE_HIST) {
1379 client_flush_hist_samples(f, pdu->log_hist_coarseness, pdu->samples,
1380 pdu->nr_samples * sizeof(struct io_sample));
1382 flush_samples(f, pdu->samples,
1383 pdu->nr_samples * sizeof(struct io_sample));
1390 if (pdu && pdu != (void *) cmd->payload)
1399 static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd)
1401 struct cmd_probe_reply_pdu *probe = (struct cmd_probe_reply_pdu *) cmd->payload;
1402 const char *os, *arch;
1405 os = fio_get_os_string(probe->os);
1409 arch = fio_get_arch_string(probe->arch);
1413 sprintf(bit, "%d-bit", probe->bpp * 8);
1414 probe->flags = le64_to_cpu(probe->flags);
1416 log_info("hostname=%s, be=%u, %s, os=%s, arch=%s, fio=%s, flags=%lx\n",
1417 probe->hostname, probe->bigendian, bit, os, arch,
1418 probe->fio_version, (unsigned long) probe->flags);
1421 client->name = strdup((char *) probe->hostname);
1424 static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd)
1426 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
1428 client->state = Client_started;
1429 client->jobs = le32_to_cpu(pdu->jobs);
1430 client->nr_stat = le32_to_cpu(pdu->stat_outputs);
1435 if (client->opt_lists)
1436 free(client->opt_lists);
1438 client->opt_lists = malloc(client->jobs * sizeof(struct flist_head));
1439 for (i = 0; i < client->jobs; i++)
1440 INIT_FLIST_HEAD(&client->opt_lists[i]);
1443 sum_stat_clients += client->nr_stat;
1446 static void handle_stop(struct fio_client *client, struct fio_net_cmd *cmd)
1449 log_info("client <%s>: exited with error %d\n", client->hostname, client->error);
1452 static void convert_stop(struct fio_net_cmd *cmd)
1454 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
1456 pdu->error = le32_to_cpu(pdu->error);
1459 static void convert_text(struct fio_net_cmd *cmd)
1461 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
1463 pdu->level = le32_to_cpu(pdu->level);
1464 pdu->buf_len = le32_to_cpu(pdu->buf_len);
1465 pdu->log_sec = le64_to_cpu(pdu->log_sec);
1466 pdu->log_usec = le64_to_cpu(pdu->log_usec);
1469 static struct cmd_iolog_pdu *convert_iolog_gz(struct fio_net_cmd *cmd,
1470 struct cmd_iolog_pdu *pdu)
1473 struct cmd_iolog_pdu *ret;
1475 uint32_t nr_samples;
1479 stream.zalloc = Z_NULL;
1480 stream.zfree = Z_NULL;
1481 stream.opaque = Z_NULL;
1482 stream.avail_in = 0;
1483 stream.next_in = Z_NULL;
1485 if (inflateInit(&stream) != Z_OK)
1489 * Get header first, it's not compressed
1491 nr_samples = le64_to_cpu(pdu->nr_samples);
1493 if (pdu->log_type == IO_LOG_TYPE_HIST)
1494 total = nr_samples * (__log_entry_sz(le32_to_cpu(pdu->log_offset)) +
1495 sizeof(struct io_u_plat_entry));
1497 total = nr_samples * __log_entry_sz(le32_to_cpu(pdu->log_offset));
1498 ret = malloc(total + sizeof(*pdu));
1499 ret->nr_samples = nr_samples;
1501 memcpy(ret, pdu, sizeof(*pdu));
1503 p = (char *) ret + sizeof(*pdu);
1505 stream.avail_in = cmd->pdu_len - sizeof(*pdu);
1506 stream.next_in = (void *)((char *) pdu + sizeof(*pdu));
1507 while (stream.avail_in) {
1508 unsigned int this_chunk = 65536;
1509 unsigned int this_len;
1512 if (this_chunk > total)
1515 stream.avail_out = this_chunk;
1516 stream.next_out = (void *)p;
1517 err = inflate(&stream, Z_NO_FLUSH);
1518 /* may be Z_OK, or Z_STREAM_END */
1520 log_err("fio: inflate error %d\n", err);
1526 this_len = this_chunk - stream.avail_out;
1532 inflateEnd(&stream);
1540 * This has been compressed on the server side, since it can be big.
1543 static struct cmd_iolog_pdu *convert_iolog(struct fio_net_cmd *cmd,
1546 struct cmd_iolog_pdu *pdu = (struct cmd_iolog_pdu *) cmd->payload;
1547 struct cmd_iolog_pdu *ret;
1552 *store_direct = false;
1555 * Convert if compressed and we support it. If it's not
1556 * compressed, we need not do anything.
1558 compressed = le32_to_cpu(pdu->compressed);
1559 if (compressed == XMIT_COMPRESSED) {
1561 log_err("fio: server sent compressed data by mistake\n");
1564 ret = convert_iolog_gz(cmd, pdu);
1566 log_err("fio: failed decompressing log\n");
1569 } else if (compressed == STORE_COMPRESSED) {
1570 *store_direct = true;
1575 ret->nr_samples = le64_to_cpu(ret->nr_samples);
1576 ret->thread_number = le32_to_cpu(ret->thread_number);
1577 ret->log_type = le32_to_cpu(ret->log_type);
1578 ret->compressed = le32_to_cpu(ret->compressed);
1579 ret->log_offset = le32_to_cpu(ret->log_offset);
1580 ret->log_hist_coarseness = le32_to_cpu(ret->log_hist_coarseness);
1585 samples = &ret->samples[0];
1586 for (i = 0; i < ret->nr_samples; i++) {
1587 struct io_sample *s;
1589 s = __get_sample(samples, ret->log_offset, i);
1590 if (ret->log_type == IO_LOG_TYPE_HIST)
1591 s = (struct io_sample *)((char *)s + sizeof(struct io_u_plat_entry) * i);
1593 s->time = le64_to_cpu(s->time);
1594 s->data.val = le64_to_cpu(s->data.val);
1595 s->__ddir = le32_to_cpu(s->__ddir);
1596 s->bs = le32_to_cpu(s->bs);
1598 if (ret->log_offset) {
1599 struct io_sample_offset *so = (void *) s;
1601 so->offset = le64_to_cpu(so->offset);
1604 if (ret->log_type == IO_LOG_TYPE_HIST) {
1605 s->data.plat_entry = (struct io_u_plat_entry *)(((char *)s) + sizeof(*s));
1606 s->data.plat_entry->list.next = NULL;
1607 s->data.plat_entry->list.prev = NULL;
1614 static void sendfile_reply(int fd, struct cmd_sendfile_reply *rep,
1615 size_t size, uint64_t tag)
1617 rep->error = cpu_to_le32(rep->error);
1618 fio_net_send_cmd(fd, FIO_NET_CMD_SENDFILE, rep, size, &tag, NULL);
1621 static int fio_send_file(struct fio_client *client, struct cmd_sendfile *pdu,
1624 struct cmd_sendfile_reply *rep;
1629 size = sizeof(*rep);
1632 if (stat((char *)pdu->path, &sb) < 0) {
1635 sendfile_reply(client->fd, rep, size, tag);
1641 rep = realloc(rep, size);
1642 rep->size = cpu_to_le32((uint32_t) sb.st_size);
1644 fd = open((char *)pdu->path, O_RDONLY);
1648 rep->error = read_data(fd, &rep->data, sb.st_size);
1649 sendfile_reply(client->fd, rep, size, tag);
1655 int fio_handle_client(struct fio_client *client)
1657 struct client_ops *ops = client->ops;
1658 struct fio_net_cmd *cmd;
1661 dprint(FD_NET, "client: handle %s\n", client->hostname);
1663 cmd = fio_net_recv_cmd(client->fd, true);
1667 dprint(FD_NET, "client: got cmd op %s from %s (pdu=%u)\n",
1668 fio_server_op(cmd->opcode), client->hostname, cmd->pdu_len);
1670 client->last_cmd = cmd->opcode;
1672 switch (cmd->opcode) {
1673 case FIO_NET_CMD_QUIT:
1675 ops->quit(client, cmd);
1676 remove_client(client);
1678 case FIO_NET_CMD_TEXT:
1680 ops->text(client, cmd);
1682 case FIO_NET_CMD_DU: {
1683 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
1685 convert_dus(&du->dus);
1686 convert_agg(&du->agg);
1688 ops->disk_util(client, cmd);
1691 case FIO_NET_CMD_TS: {
1692 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
1694 dprint(FD_NET, "client: ts->ss_state = %u\n", (unsigned int) le32_to_cpu(p->ts.ss_state));
1695 if (le32_to_cpu(p->ts.ss_state) & FIO_SS_DATA) {
1696 dprint(FD_NET, "client: received steadystate ring buffers\n");
1698 size = le64_to_cpu(p->ts.ss_dur);
1699 p->ts.ss_iops_data = (uint64_t *) ((struct cmd_ts_pdu *)cmd->payload + 1);
1700 p->ts.ss_bw_data = p->ts.ss_iops_data + size;
1703 convert_ts(&p->ts, &p->ts);
1704 convert_gs(&p->rs, &p->rs);
1706 ops->thread_status(client, cmd);
1709 case FIO_NET_CMD_GS: {
1710 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
1714 ops->group_stats(client, cmd);
1717 case FIO_NET_CMD_ETA: {
1718 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
1720 if (!remove_reply_cmd(client, cmd))
1722 convert_jobs_eta(je);
1723 handle_eta(client, cmd);
1726 case FIO_NET_CMD_PROBE:
1727 remove_reply_cmd(client, cmd);
1728 ops->probe(client, cmd);
1730 case FIO_NET_CMD_SERVER_START:
1731 client->state = Client_running;
1733 ops->job_start(client, cmd);
1735 case FIO_NET_CMD_START: {
1736 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
1738 pdu->jobs = le32_to_cpu(pdu->jobs);
1739 ops->start(client, cmd);
1742 case FIO_NET_CMD_STOP: {
1743 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
1746 client->state = Client_stopped;
1747 client->error = le32_to_cpu(pdu->error);
1748 client->signal = le32_to_cpu(pdu->signal);
1749 ops->stop(client, cmd);
1752 case FIO_NET_CMD_ADD_JOB: {
1753 struct cmd_add_job_pdu *pdu = (struct cmd_add_job_pdu *) cmd->payload;
1755 client->thread_number = le32_to_cpu(pdu->thread_number);
1756 client->groupid = le32_to_cpu(pdu->groupid);
1759 ops->add_job(client, cmd);
1762 case FIO_NET_CMD_IOLOG:
1763 fio_client_handle_iolog(client, cmd);
1765 case FIO_NET_CMD_UPDATE_JOB:
1766 ops->update_job(client, cmd);
1767 remove_reply_cmd(client, cmd);
1769 case FIO_NET_CMD_VTRIGGER: {
1770 struct all_io_list *pdu = (struct all_io_list *) cmd->payload;
1775 strcpy(buf, aux_path);
1779 __verify_save_state(pdu, server_name(client, &buf[off], sizeof(buf) - off));
1780 exec_trigger(trigger_cmd);
1783 case FIO_NET_CMD_SENDFILE: {
1784 struct cmd_sendfile *pdu = (struct cmd_sendfile *) cmd->payload;
1785 fio_send_file(client, pdu, cmd->tag);
1788 case FIO_NET_CMD_JOB_OPT: {
1789 handle_job_opt(client, cmd);
1793 log_err("fio: unknown client op: %s\n", fio_server_op(cmd->opcode));
1801 int fio_clients_send_trigger(const char *cmd)
1803 struct flist_head *entry;
1804 struct fio_client *client;
1807 dprint(FD_NET, "client: send vtrigger: %s\n", cmd);
1814 flist_for_each(entry, &client_list) {
1815 struct cmd_vtrigger_pdu *pdu;
1817 client = flist_entry(entry, struct fio_client, list);
1819 pdu = malloc(sizeof(*pdu) + slen);
1820 pdu->len = cpu_to_le16((uint16_t) slen);
1822 memcpy(pdu->cmd, cmd, slen);
1823 fio_net_send_cmd(client->fd, FIO_NET_CMD_VTRIGGER, pdu,
1824 sizeof(*pdu) + slen, NULL, NULL);
1831 static void request_client_etas(struct client_ops *ops)
1833 struct fio_client *client;
1834 struct flist_head *entry;
1835 struct client_eta *eta;
1838 if (eta_print == FIO_ETA_NEVER)
1841 dprint(FD_NET, "client: request eta (%d)\n", nr_clients);
1843 eta = calloc(1, sizeof(*eta) + __THREAD_RUNSTR_SZ(REAL_MAX_JOBS));
1844 eta->pending = nr_clients;
1846 flist_for_each(entry, &client_list) {
1847 client = flist_entry(entry, struct fio_client, list);
1849 if (!flist_empty(&client->eta_list)) {
1853 if (client->state != Client_running)
1856 assert(!client->eta_in_flight);
1857 flist_add_tail(&client->eta_list, &eta_list);
1858 client->eta_in_flight = eta;
1859 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_SEND_ETA,
1860 (uintptr_t) eta, &client->cmd_list);
1864 if (!fio_client_dec_jobs_eta(eta, ops->eta))
1868 dprint(FD_NET, "client: requested eta tag %p\n", eta);
1872 * A single SEND_ETA timeout isn't fatal. Attempt to recover.
1874 static int handle_cmd_timeout(struct fio_client *client,
1875 struct fio_net_cmd_reply *reply)
1877 uint16_t reply_opcode = reply->opcode;
1879 flist_del(&reply->list);
1882 if (reply_opcode != FIO_NET_CMD_SEND_ETA)
1885 log_info("client <%s>: timeout on SEND_ETA\n", client->hostname);
1887 flist_del_init(&client->eta_list);
1888 if (client->eta_in_flight) {
1889 fio_client_dec_jobs_eta(client->eta_in_flight, client->ops->eta);
1890 client->eta_in_flight = NULL;
1894 * If we fail 5 in a row, give up...
1896 if (client->eta_timeouts++ > 5)
1902 static int client_check_cmd_timeout(struct fio_client *client,
1903 struct timespec *now)
1905 struct fio_net_cmd_reply *reply;
1906 struct flist_head *entry, *tmp;
1909 flist_for_each_safe(entry, tmp, &client->cmd_list) {
1912 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
1914 if (mtime_since(&reply->ts, now) < FIO_NET_CLIENT_TIMEOUT)
1918 if (!handle_cmd_timeout(client, reply))
1921 log_err("fio: client %s, timeout on cmd %s\n", client->hostname,
1926 return flist_empty(&client->cmd_list) && ret;
1929 static int fio_check_clients_timed_out(void)
1931 struct fio_client *client;
1932 struct flist_head *entry, *tmp;
1936 fio_gettime(&ts, NULL);
1938 flist_for_each_safe(entry, tmp, &client_list) {
1939 client = flist_entry(entry, struct fio_client, list);
1941 if (flist_empty(&client->cmd_list))
1944 if (!client_check_cmd_timeout(client, &ts))
1947 if (client->ops->timed_out)
1948 client->ops->timed_out(client);
1950 log_err("fio: client %s timed out\n", client->hostname);
1952 if (client->last_cmd != FIO_NET_CMD_VTRIGGER)
1953 client->error = ETIMEDOUT;
1955 log_info("fio: ignoring timeout due to vtrigger\n");
1956 remove_client(client);
1963 int fio_handle_clients(struct client_ops *ops)
1965 struct pollfd *pfds;
1966 int i, ret = 0, retval = 0;
1968 fio_gettime(&eta_ts, NULL);
1970 pfds = malloc(nr_clients * sizeof(struct pollfd));
1972 init_thread_stat(&client_ts);
1973 init_group_run_stat(&client_gs);
1975 while (!exit_backend && nr_clients) {
1976 struct flist_head *entry, *tmp;
1977 struct fio_client *client;
1980 flist_for_each_safe(entry, tmp, &client_list) {
1981 client = flist_entry(entry, struct fio_client, list);
1983 if (!client->sent_job && !client->ops->stay_connected &&
1984 flist_empty(&client->cmd_list)) {
1985 remove_client(client);
1989 pfds[i].fd = client->fd;
1990 pfds[i].events = POLLIN;
1997 assert(i == nr_clients);
2003 fio_gettime(&ts, NULL);
2004 if (eta_time_within_slack(mtime_since(&eta_ts, &ts))) {
2005 request_client_etas(ops);
2006 memcpy(&eta_ts, &ts, sizeof(ts));
2008 if (fio_check_clients_timed_out())
2012 check_trigger_file();
2014 timeout = min(100u, ops->eta_msec);
2016 ret = poll(pfds, nr_clients, timeout);
2020 log_err("fio: poll clients: %s\n", strerror(errno));
2026 for (i = 0; i < nr_clients; i++) {
2027 if (!(pfds[i].revents & POLLIN))
2030 client = find_client_by_fd(pfds[i].fd);
2032 log_err("fio: unknown client fd %ld\n", (long) pfds[i].fd);
2035 if (!fio_handle_client(client)) {
2036 log_info("client: host=%s disconnected\n",
2038 remove_client(client);
2040 } else if (client->error)
2042 fio_put_client(client);
2046 fio_client_json_fini();
2049 return retval || error_clients;