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, struct fio_net_cmd *cmd);
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 void fio_client_json_init(void)
126 if (!(output_format & FIO_OUTPUT_JSON))
130 os_ctime_r((const time_t *) &time_p, time_buf, sizeof(time_buf));
131 time_buf[strlen(time_buf) - 1] = '\0';
133 root = json_create_object();
134 json_object_add_value_string(root, "fio version", fio_version_string);
135 json_object_add_value_int(root, "timestamp", time_p);
136 json_object_add_value_string(root, "time", time_buf);
138 job_opt_object = json_create_object();
139 json_object_add_value_object(root, "global options", job_opt_object);
140 clients_array = json_create_array();
141 json_object_add_value_array(root, "client_stats", clients_array);
142 du_array = json_create_array();
143 json_object_add_value_array(root, "disk_util", du_array);
146 static void fio_client_json_fini(void)
148 if (!(output_format & FIO_OUTPUT_JSON))
152 json_print_object(root, NULL);
154 json_free_object(root);
156 clients_array = NULL;
160 static struct fio_client *find_client_by_fd(int fd)
162 int bucket = hash_long(fd, FIO_CLIENT_HASH_BITS) & FIO_CLIENT_HASH_MASK;
163 struct fio_client *client;
164 struct flist_head *entry;
166 flist_for_each(entry, &client_hash[bucket]) {
167 client = flist_entry(entry, struct fio_client, hash_list);
169 if (client->fd == fd) {
178 void fio_put_client(struct fio_client *client)
183 free(client->hostname);
188 while (client->nr_files) {
189 struct client_file *cf = &client->files[--client->nr_files];
195 if (client->opt_lists)
196 free(client->opt_lists);
198 if (!client->did_stat)
207 static int fio_client_dec_jobs_eta(struct client_eta *eta, client_eta_op eta_fn)
209 if (!--eta->pending) {
218 static void fio_drain_client_text(struct fio_client *client)
221 struct fio_net_cmd *cmd;
223 cmd = fio_net_recv_cmd(client->fd, false);
227 if (cmd->opcode == FIO_NET_CMD_TEXT) {
229 client->ops->text(client, cmd);
236 static void remove_client(struct fio_client *client)
238 assert(client->refs);
240 dprint(FD_NET, "client: removed <%s>\n", client->hostname);
242 fio_drain_client_text(client);
244 if (!flist_empty(&client->list))
245 flist_del_init(&client->list);
247 fio_client_remove_hash(client);
249 if (!flist_empty(&client->eta_list)) {
250 flist_del_init(&client->eta_list);
251 fio_client_dec_jobs_eta(client->eta_in_flight, client->ops->eta);
257 if (client->ops->removed)
258 client->ops->removed(client);
261 fio_put_client(client);
264 struct fio_client *fio_get_client(struct fio_client *client)
270 static void __fio_client_add_cmd_option(struct fio_client *client,
275 index = client->argc++;
276 client->argv = realloc(client->argv, sizeof(char *) * client->argc);
277 client->argv[index] = strdup(opt);
278 dprint(FD_NET, "client: add cmd %d: %s\n", index, opt);
281 void fio_client_add_cmd_option(void *cookie, const char *opt)
283 struct fio_client *client = cookie;
284 struct flist_head *entry;
289 __fio_client_add_cmd_option(client, opt);
292 * Duplicate arguments to shared client group
294 flist_for_each(entry, &arg_list) {
295 client = flist_entry(entry, struct fio_client, arg_list);
297 __fio_client_add_cmd_option(client, opt);
301 struct fio_client *fio_client_add_explicit(struct client_ops *ops,
302 const char *hostname, int type,
305 struct fio_client *client;
307 client = malloc(sizeof(*client));
308 memset(client, 0, sizeof(*client));
310 INIT_FLIST_HEAD(&client->list);
311 INIT_FLIST_HEAD(&client->hash_list);
312 INIT_FLIST_HEAD(&client->arg_list);
313 INIT_FLIST_HEAD(&client->eta_list);
314 INIT_FLIST_HEAD(&client->cmd_list);
316 client->hostname = strdup(hostname);
318 if (type == Fio_client_socket)
319 client->is_sock = true;
323 ipv6 = type == Fio_client_ipv6;
324 if (fio_server_parse_host(hostname, ipv6,
325 &client->addr.sin_addr,
326 &client->addr6.sin6_addr))
335 client->type = ops->client_type;
337 __fio_client_add_cmd_option(client, "fio");
339 flist_add(&client->list, &client_list);
341 dprint(FD_NET, "client: added <%s>\n", client->hostname);
348 int fio_client_add_ini_file(void *cookie, const char *ini_file, bool remote)
350 struct fio_client *client = cookie;
351 struct client_file *cf;
358 dprint(FD_NET, "client <%s>: add ini %s\n", client->hostname, ini_file);
360 new_size = (client->nr_files + 1) * sizeof(struct client_file);
361 new_files = realloc(client->files, new_size);
365 client->files = new_files;
366 cf = &client->files[client->nr_files];
367 cf->file = strdup(ini_file);
373 int fio_client_add(struct client_ops *ops, const char *hostname, void **cookie)
375 struct fio_client *existing = *cookie;
376 struct fio_client *client;
380 * We always add our "exec" name as the option, hence 1
383 if (existing->argc == 1)
384 flist_add_tail(&existing->arg_list, &arg_list);
386 while (!flist_empty(&arg_list))
387 flist_del_init(arg_list.next);
391 client = malloc(sizeof(*client));
392 memset(client, 0, sizeof(*client));
394 INIT_FLIST_HEAD(&client->list);
395 INIT_FLIST_HEAD(&client->hash_list);
396 INIT_FLIST_HEAD(&client->arg_list);
397 INIT_FLIST_HEAD(&client->eta_list);
398 INIT_FLIST_HEAD(&client->cmd_list);
400 if (fio_server_parse_string(hostname, &client->hostname,
401 &client->is_sock, &client->port,
402 &client->addr.sin_addr,
403 &client->addr6.sin6_addr,
410 client->type = ops->client_type;
412 __fio_client_add_cmd_option(client, "fio");
414 flist_add(&client->list, &client_list);
416 dprint(FD_NET, "client: added <%s>\n", client->hostname);
421 static const char *server_name(struct fio_client *client, char *buf,
427 from = inet_ntop(AF_INET6, (struct sockaddr *) &client->addr6.sin6_addr, buf, bufsize);
428 else if (client->is_sock)
431 from = inet_ntop(AF_INET, (struct sockaddr *) &client->addr.sin_addr, buf, bufsize);
436 static void probe_client(struct fio_client *client)
438 struct cmd_client_probe_pdu pdu;
443 dprint(FD_NET, "client: send probe\n");
446 pdu.flags = __le64_to_cpu(FIO_PROBE_FLAG_ZLIB);
451 sname = server_name(client, buf, sizeof(buf));
452 memset(pdu.server, 0, sizeof(pdu.server));
453 strncpy((char *) pdu.server, sname, sizeof(pdu.server) - 1);
455 fio_net_send_cmd(client->fd, FIO_NET_CMD_PROBE, &pdu, sizeof(pdu), &tag, &client->cmd_list);
458 static int fio_client_connect_ip(struct fio_client *client)
460 struct sockaddr *addr;
465 client->addr6.sin6_family = AF_INET6;
466 client->addr6.sin6_port = htons(client->port);
468 addr = (struct sockaddr *) &client->addr6;
469 socklen = sizeof(client->addr6);
471 client->addr.sin_family = AF_INET;
472 client->addr.sin_port = htons(client->port);
474 addr = (struct sockaddr *) &client->addr;
475 socklen = sizeof(client->addr);
478 fd = socket(domain, SOCK_STREAM, 0);
482 log_err("fio: socket: %s\n", strerror(errno));
486 if (connect(fd, addr, socklen) < 0) {
489 log_err("fio: connect: %s\n", strerror(errno));
490 log_err("fio: failed to connect to %s:%u\n", client->hostname,
499 static int fio_client_connect_sock(struct fio_client *client)
501 struct sockaddr_un *addr = &client->addr_un;
505 memset(addr, 0, sizeof(*addr));
506 addr->sun_family = AF_UNIX;
507 strncpy(addr->sun_path, client->hostname, sizeof(addr->sun_path) - 1);
509 fd = socket(AF_UNIX, SOCK_STREAM, 0);
513 log_err("fio: socket: %s\n", strerror(errno));
517 len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
518 if (connect(fd, (struct sockaddr *) addr, len) < 0) {
521 log_err("fio: connect; %s\n", strerror(errno));
529 int fio_client_connect(struct fio_client *client)
533 dprint(FD_NET, "client: connect to host %s\n", client->hostname);
536 fd = fio_client_connect_sock(client);
538 fd = fio_client_connect_ip(client);
540 dprint(FD_NET, "client: %s connected %d\n", client->hostname, fd);
546 fio_client_add_hash(client);
547 client->state = Client_connected;
549 probe_client(client);
553 int fio_client_terminate(struct fio_client *client)
555 return fio_net_send_quit(client->fd);
558 static void fio_clients_terminate(void)
560 struct flist_head *entry;
561 struct fio_client *client;
563 dprint(FD_NET, "client: terminate clients\n");
565 flist_for_each(entry, &client_list) {
566 client = flist_entry(entry, struct fio_client, list);
567 fio_client_terminate(client);
571 static void sig_int(int sig)
573 dprint(FD_NET, "client: got signal %d\n", sig);
574 fio_clients_terminate();
577 static void client_signal_handler(void)
579 struct sigaction act;
581 memset(&act, 0, sizeof(act));
582 act.sa_handler = sig_int;
583 act.sa_flags = SA_RESTART;
584 sigaction(SIGINT, &act, NULL);
586 memset(&act, 0, sizeof(act));
587 act.sa_handler = sig_int;
588 act.sa_flags = SA_RESTART;
589 sigaction(SIGTERM, &act, NULL);
591 /* Windows uses SIGBREAK as a quit signal from other applications */
593 memset(&act, 0, sizeof(act));
594 act.sa_handler = sig_int;
595 act.sa_flags = SA_RESTART;
596 sigaction(SIGBREAK, &act, NULL);
599 memset(&act, 0, sizeof(act));
600 act.sa_handler = sig_show_status;
601 act.sa_flags = SA_RESTART;
602 sigaction(SIGUSR1, &act, NULL);
605 static int send_client_cmd_line(struct fio_client *client)
607 struct cmd_single_line_pdu *cslp;
608 struct cmd_line_pdu *clp;
609 unsigned long offset;
615 dprint(FD_NET, "client: send cmdline %d\n", client->argc);
617 lens = malloc(client->argc * sizeof(unsigned int));
620 * Find out how much mem we need
622 for (i = 0, mem = 0; i < client->argc; i++) {
623 lens[i] = strlen(client->argv[i]) + 1;
628 * We need one cmd_line_pdu, and argc number of cmd_single_line_pdu
630 mem += sizeof(*clp) + (client->argc * sizeof(*cslp));
634 offset = sizeof(*clp);
636 for (i = 0; i < client->argc; i++) {
637 uint16_t arg_len = lens[i];
640 strcpy((char *) cslp->text, client->argv[i]);
641 cslp->len = cpu_to_le16(arg_len);
642 offset += sizeof(*cslp) + arg_len;
646 clp->lines = cpu_to_le16(client->argc);
647 clp->client_type = __cpu_to_le16(client->type);
648 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOBLINE, pdu, mem, NULL, NULL);
653 int fio_clients_connect(void)
655 struct fio_client *client;
656 struct flist_head *entry, *tmp;
661 WSAStartup(MAKEWORD(2, 2), &wsd);
664 dprint(FD_NET, "client: connect all\n");
666 client_signal_handler();
668 flist_for_each_safe(entry, tmp, &client_list) {
669 client = flist_entry(entry, struct fio_client, list);
671 ret = fio_client_connect(client);
673 remove_client(client);
677 if (client->argc > 1)
678 send_client_cmd_line(client);
684 int fio_start_client(struct fio_client *client)
686 dprint(FD_NET, "client: start %s\n", client->hostname);
687 return fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_RUN, 0, NULL);
690 int fio_start_all_clients(void)
692 struct fio_client *client;
693 struct flist_head *entry, *tmp;
696 dprint(FD_NET, "client: start all\n");
698 fio_client_json_init();
700 flist_for_each_safe(entry, tmp, &client_list) {
701 client = flist_entry(entry, struct fio_client, list);
703 ret = fio_start_client(client);
705 remove_client(client);
710 return flist_empty(&client_list);
713 static int __fio_client_send_remote_ini(struct fio_client *client,
714 const char *filename)
716 struct cmd_load_file_pdu *pdu;
720 dprint(FD_NET, "send remote ini %s to %s\n", filename, client->hostname);
722 p_size = sizeof(*pdu) + strlen(filename) + 1;
723 pdu = malloc(p_size);
724 memset(pdu, 0, p_size);
725 pdu->name_len = strlen(filename);
726 strcpy((char *) pdu->file, filename);
727 pdu->client_type = cpu_to_le16((uint16_t) client->type);
729 client->sent_job = true;
730 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_LOAD_FILE, pdu, p_size,NULL, NULL);
736 * Send file contents to server backend. We could use sendfile(), but to remain
737 * more portable lets just read/write the darn thing.
739 static int __fio_client_send_local_ini(struct fio_client *client,
740 const char *filename)
742 struct cmd_job_pdu *pdu;
750 dprint(FD_NET, "send ini %s to %s\n", filename, client->hostname);
752 fd = open(filename, O_RDONLY);
755 log_err("fio: job file <%s> open: %s\n", filename, strerror(errno));
759 if (fstat(fd, &sb) < 0) {
761 log_err("fio: job file stat: %s\n", strerror(errno));
766 p_size = sb.st_size + sizeof(*pdu);
767 pdu = malloc(p_size);
772 if (read_data(fd, p, len)) {
773 log_err("fio: failed reading job file %s\n", filename);
779 pdu->buf_len = __cpu_to_le32(sb.st_size);
780 pdu->client_type = cpu_to_le32(client->type);
782 client->sent_job = true;
783 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, pdu, p_size, NULL, NULL);
789 int fio_client_send_ini(struct fio_client *client, const char *filename,
795 ret = __fio_client_send_local_ini(client, filename);
797 ret = __fio_client_send_remote_ini(client, filename);
800 client->sent_job = true;
805 static int fio_client_send_cf(struct fio_client *client,
806 struct client_file *cf)
808 return fio_client_send_ini(client, cf->file, cf->remote);
811 int fio_clients_send_ini(const char *filename)
813 struct fio_client *client;
814 struct flist_head *entry, *tmp;
816 flist_for_each_safe(entry, tmp, &client_list) {
819 client = flist_entry(entry, struct fio_client, list);
821 if (client->nr_files) {
824 for (i = 0; i < client->nr_files; i++) {
825 struct client_file *cf;
827 cf = &client->files[i];
829 if (fio_client_send_cf(client, cf)) {
831 remove_client(client);
836 if (client->sent_job || failed)
838 if (!filename || fio_client_send_ini(client, filename, 0))
839 remove_client(client);
845 int fio_client_update_options(struct fio_client *client,
846 struct thread_options *o, uint64_t *tag)
848 struct cmd_add_job_pdu pdu;
850 pdu.thread_number = cpu_to_le32(client->thread_number);
851 pdu.groupid = cpu_to_le32(client->groupid);
852 convert_thread_options_to_net(&pdu.top, o);
854 return fio_net_send_cmd(client->fd, FIO_NET_CMD_UPDATE_JOB, &pdu, sizeof(pdu), tag, &client->cmd_list);
857 static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
859 dst->max_val = le64_to_cpu(src->max_val);
860 dst->min_val = le64_to_cpu(src->min_val);
861 dst->samples = le64_to_cpu(src->samples);
864 * Floats arrive as IEEE 754 encoded uint64_t, convert back to double
866 dst->mean.u.f = fio_uint64_to_double(le64_to_cpu(dst->mean.u.i));
867 dst->S.u.f = fio_uint64_to_double(le64_to_cpu(dst->S.u.i));
870 static void convert_ts(struct thread_stat *dst, struct thread_stat *src)
874 dst->error = le32_to_cpu(src->error);
875 dst->thread_number = le32_to_cpu(src->thread_number);
876 dst->groupid = le32_to_cpu(src->groupid);
877 dst->pid = le32_to_cpu(src->pid);
878 dst->members = le32_to_cpu(src->members);
879 dst->unified_rw_rep = le32_to_cpu(src->unified_rw_rep);
881 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
882 convert_io_stat(&dst->clat_stat[i], &src->clat_stat[i]);
883 convert_io_stat(&dst->slat_stat[i], &src->slat_stat[i]);
884 convert_io_stat(&dst->lat_stat[i], &src->lat_stat[i]);
885 convert_io_stat(&dst->bw_stat[i], &src->bw_stat[i]);
886 convert_io_stat(&dst->iops_stat[i], &src->iops_stat[i]);
889 dst->usr_time = le64_to_cpu(src->usr_time);
890 dst->sys_time = le64_to_cpu(src->sys_time);
891 dst->ctx = le64_to_cpu(src->ctx);
892 dst->minf = le64_to_cpu(src->minf);
893 dst->majf = le64_to_cpu(src->majf);
894 dst->clat_percentiles = le32_to_cpu(src->clat_percentiles);
895 dst->lat_percentiles = le32_to_cpu(src->lat_percentiles);
896 dst->percentile_precision = le64_to_cpu(src->percentile_precision);
898 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
899 fio_fp64_t *fps = &src->percentile_list[i];
900 fio_fp64_t *fpd = &dst->percentile_list[i];
902 fpd->u.f = fio_uint64_to_double(le64_to_cpu(fps->u.i));
905 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
906 dst->io_u_map[i] = le64_to_cpu(src->io_u_map[i]);
907 dst->io_u_submit[i] = le64_to_cpu(src->io_u_submit[i]);
908 dst->io_u_complete[i] = le64_to_cpu(src->io_u_complete[i]);
911 for (i = 0; i < FIO_IO_U_LAT_N_NR; i++)
912 dst->io_u_lat_n[i] = le64_to_cpu(src->io_u_lat_n[i]);
913 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++)
914 dst->io_u_lat_u[i] = le64_to_cpu(src->io_u_lat_u[i]);
915 for (i = 0; i < FIO_IO_U_LAT_M_NR; i++)
916 dst->io_u_lat_m[i] = le64_to_cpu(src->io_u_lat_m[i]);
918 for (i = 0; i < DDIR_RWDIR_CNT; i++)
919 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
920 dst->io_u_plat[i][j] = le64_to_cpu(src->io_u_plat[i][j]);
922 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
923 dst->total_io_u[i] = le64_to_cpu(src->total_io_u[i]);
924 dst->short_io_u[i] = le64_to_cpu(src->short_io_u[i]);
925 dst->drop_io_u[i] = le64_to_cpu(src->drop_io_u[i]);
928 dst->total_submit = le64_to_cpu(src->total_submit);
929 dst->total_complete = le64_to_cpu(src->total_complete);
931 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
932 dst->io_bytes[i] = le64_to_cpu(src->io_bytes[i]);
933 dst->runtime[i] = le64_to_cpu(src->runtime[i]);
936 dst->total_run_time = le64_to_cpu(src->total_run_time);
937 dst->continue_on_error = le16_to_cpu(src->continue_on_error);
938 dst->total_err_count = le64_to_cpu(src->total_err_count);
939 dst->first_error = le32_to_cpu(src->first_error);
940 dst->kb_base = le32_to_cpu(src->kb_base);
941 dst->unit_base = le32_to_cpu(src->unit_base);
943 dst->sig_figs = le32_to_cpu(src->sig_figs);
945 dst->latency_depth = le32_to_cpu(src->latency_depth);
946 dst->latency_target = le64_to_cpu(src->latency_target);
947 dst->latency_window = le64_to_cpu(src->latency_window);
948 dst->latency_percentile.u.f = fio_uint64_to_double(le64_to_cpu(src->latency_percentile.u.i));
950 dst->nr_block_infos = le64_to_cpu(src->nr_block_infos);
951 for (i = 0; i < dst->nr_block_infos; i++)
952 dst->block_infos[i] = le32_to_cpu(src->block_infos[i]);
954 dst->ss_dur = le64_to_cpu(src->ss_dur);
955 dst->ss_state = le32_to_cpu(src->ss_state);
956 dst->ss_head = le32_to_cpu(src->ss_head);
957 dst->ss_limit.u.f = fio_uint64_to_double(le64_to_cpu(src->ss_limit.u.i));
958 dst->ss_slope.u.f = fio_uint64_to_double(le64_to_cpu(src->ss_slope.u.i));
959 dst->ss_deviation.u.f = fio_uint64_to_double(le64_to_cpu(src->ss_deviation.u.i));
960 dst->ss_criterion.u.f = fio_uint64_to_double(le64_to_cpu(src->ss_criterion.u.i));
962 if (dst->ss_state & FIO_SS_DATA) {
963 for (i = 0; i < dst->ss_dur; i++ ) {
964 dst->ss_iops_data[i] = le64_to_cpu(src->ss_iops_data[i]);
965 dst->ss_bw_data[i] = le64_to_cpu(src->ss_bw_data[i]);
970 static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
974 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
975 dst->max_run[i] = le64_to_cpu(src->max_run[i]);
976 dst->min_run[i] = le64_to_cpu(src->min_run[i]);
977 dst->max_bw[i] = le64_to_cpu(src->max_bw[i]);
978 dst->min_bw[i] = le64_to_cpu(src->min_bw[i]);
979 dst->iobytes[i] = le64_to_cpu(src->iobytes[i]);
980 dst->agg[i] = le64_to_cpu(src->agg[i]);
983 dst->kb_base = le32_to_cpu(src->kb_base);
984 dst->unit_base = le32_to_cpu(src->unit_base);
985 dst->sig_figs = le32_to_cpu(src->sig_figs);
986 dst->groupid = le32_to_cpu(src->groupid);
987 dst->unified_rw_rep = le32_to_cpu(src->unified_rw_rep);
990 static void json_object_add_client_info(struct json_object *obj,
991 struct fio_client *client)
993 const char *hostname = client->hostname ? client->hostname : "";
995 json_object_add_value_string(obj, "hostname", hostname);
996 json_object_add_value_int(obj, "port", client->port);
999 static void handle_ts(struct fio_client *client, struct fio_net_cmd *cmd)
1001 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
1002 struct flist_head *opt_list = NULL;
1003 struct json_object *tsobj;
1005 if (client->opt_lists && p->ts.thread_number <= client->jobs)
1006 opt_list = &client->opt_lists[p->ts.thread_number - 1];
1008 tsobj = show_thread_status(&p->ts, &p->rs, opt_list, NULL);
1009 client->did_stat = true;
1011 json_object_add_client_info(tsobj, client);
1012 json_array_add_value_object(clients_array, tsobj);
1015 if (sum_stat_clients <= 1)
1018 sum_thread_stats(&client_ts, &p->ts, sum_stat_nr == 1);
1019 sum_group_stats(&client_gs, &p->rs);
1021 client_ts.members++;
1022 client_ts.thread_number = p->ts.thread_number;
1023 client_ts.groupid = p->ts.groupid;
1024 client_ts.unified_rw_rep = p->ts.unified_rw_rep;
1025 client_ts.sig_figs = p->ts.sig_figs;
1027 if (++sum_stat_nr == sum_stat_clients) {
1028 strcpy(client_ts.name, "All clients");
1029 tsobj = show_thread_status(&client_ts, &client_gs, NULL, NULL);
1031 json_object_add_client_info(tsobj, client);
1032 json_array_add_value_object(clients_array, tsobj);
1037 static void handle_gs(struct fio_client *client, struct fio_net_cmd *cmd)
1039 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
1041 if (output_format & FIO_OUTPUT_NORMAL)
1042 show_group_stats(gs, NULL);
1045 static void handle_job_opt(struct fio_client *client, struct fio_net_cmd *cmd)
1047 struct cmd_job_option *pdu = (struct cmd_job_option *) cmd->payload;
1048 struct print_option *p;
1050 if (!job_opt_object)
1053 pdu->global = le16_to_cpu(pdu->global);
1054 pdu->truncated = le16_to_cpu(pdu->truncated);
1055 pdu->groupid = le32_to_cpu(pdu->groupid);
1057 p = malloc(sizeof(*p));
1058 p->name = strdup((char *) pdu->name);
1059 if (pdu->value[0] != '\0')
1060 p->value = strdup((char *) pdu->value);
1065 const char *pos = "";
1070 json_object_add_value_string(job_opt_object, p->name, pos);
1071 } else if (client->opt_lists) {
1072 struct flist_head *opt_list = &client->opt_lists[pdu->groupid];
1074 flist_add_tail(&p->list, opt_list);
1078 static void handle_text(struct fio_client *client, struct fio_net_cmd *cmd)
1080 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
1081 const char *buf = (const char *) pdu->buf;
1085 name = client->name ? client->name : client->hostname;
1087 if (!client->skip_newline)
1088 fprintf(f_out, "<%s> ", name);
1089 ret = fwrite(buf, pdu->buf_len, 1, f_out);
1091 client->skip_newline = strchr(buf, '\n') == NULL;
1094 static void convert_agg(struct disk_util_agg *agg)
1098 for (i = 0; i < 2; i++) {
1099 agg->ios[i] = le64_to_cpu(agg->ios[i]);
1100 agg->merges[i] = le64_to_cpu(agg->merges[i]);
1101 agg->sectors[i] = le64_to_cpu(agg->sectors[i]);
1102 agg->ticks[i] = le64_to_cpu(agg->ticks[i]);
1105 agg->io_ticks = le64_to_cpu(agg->io_ticks);
1106 agg->time_in_queue = le64_to_cpu(agg->time_in_queue);
1107 agg->slavecount = le32_to_cpu(agg->slavecount);
1108 agg->max_util.u.f = fio_uint64_to_double(le64_to_cpu(agg->max_util.u.i));
1111 static void convert_dus(struct disk_util_stat *dus)
1115 for (i = 0; i < 2; i++) {
1116 dus->s.ios[i] = le64_to_cpu(dus->s.ios[i]);
1117 dus->s.merges[i] = le64_to_cpu(dus->s.merges[i]);
1118 dus->s.sectors[i] = le64_to_cpu(dus->s.sectors[i]);
1119 dus->s.ticks[i] = le64_to_cpu(dus->s.ticks[i]);
1122 dus->s.io_ticks = le64_to_cpu(dus->s.io_ticks);
1123 dus->s.time_in_queue = le64_to_cpu(dus->s.time_in_queue);
1124 dus->s.msec = le64_to_cpu(dus->s.msec);
1127 static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd)
1129 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
1131 if (!client->disk_stats_shown) {
1132 client->disk_stats_shown = true;
1133 log_info("\nDisk stats (read/write):\n");
1136 if (output_format & FIO_OUTPUT_JSON) {
1137 struct json_object *duobj;
1138 json_array_add_disk_util(&du->dus, &du->agg, du_array);
1139 duobj = json_array_last_value_object(du_array);
1140 json_object_add_client_info(duobj, client);
1142 if (output_format & FIO_OUTPUT_TERSE)
1143 print_disk_util(&du->dus, &du->agg, 1, NULL);
1144 if (output_format & FIO_OUTPUT_NORMAL)
1145 print_disk_util(&du->dus, &du->agg, 0, NULL);
1148 static void convert_jobs_eta(struct jobs_eta *je)
1152 je->nr_running = le32_to_cpu(je->nr_running);
1153 je->nr_ramp = le32_to_cpu(je->nr_ramp);
1154 je->nr_pending = le32_to_cpu(je->nr_pending);
1155 je->nr_setting_up = le32_to_cpu(je->nr_setting_up);
1156 je->files_open = le32_to_cpu(je->files_open);
1158 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1159 je->m_rate[i] = le64_to_cpu(je->m_rate[i]);
1160 je->t_rate[i] = le64_to_cpu(je->t_rate[i]);
1161 je->m_iops[i] = le32_to_cpu(je->m_iops[i]);
1162 je->t_iops[i] = le32_to_cpu(je->t_iops[i]);
1163 je->rate[i] = le64_to_cpu(je->rate[i]);
1164 je->iops[i] = le32_to_cpu(je->iops[i]);
1167 je->elapsed_sec = le64_to_cpu(je->elapsed_sec);
1168 je->eta_sec = le64_to_cpu(je->eta_sec);
1169 je->nr_threads = le32_to_cpu(je->nr_threads);
1170 je->is_pow2 = le32_to_cpu(je->is_pow2);
1171 je->unit_base = le32_to_cpu(je->unit_base);
1172 je->sig_figs = le32_to_cpu(je->sig_figs);
1175 void fio_client_sum_jobs_eta(struct jobs_eta *dst, struct jobs_eta *je)
1179 dst->nr_running += je->nr_running;
1180 dst->nr_ramp += je->nr_ramp;
1181 dst->nr_pending += je->nr_pending;
1182 dst->nr_setting_up += je->nr_setting_up;
1183 dst->files_open += je->files_open;
1185 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1186 dst->m_rate[i] += je->m_rate[i];
1187 dst->t_rate[i] += je->t_rate[i];
1188 dst->m_iops[i] += je->m_iops[i];
1189 dst->t_iops[i] += je->t_iops[i];
1190 dst->rate[i] += je->rate[i];
1191 dst->iops[i] += je->iops[i];
1194 dst->elapsed_sec += je->elapsed_sec;
1196 if (je->eta_sec > dst->eta_sec)
1197 dst->eta_sec = je->eta_sec;
1199 dst->nr_threads += je->nr_threads;
1202 * This wont be correct for multiple strings, but at least it
1203 * works for the basic cases.
1205 strcpy((char *) dst->run_str, (char *) je->run_str);
1208 static bool remove_reply_cmd(struct fio_client *client, struct fio_net_cmd *cmd)
1210 struct fio_net_cmd_reply *reply = NULL;
1211 struct flist_head *entry;
1213 flist_for_each(entry, &client->cmd_list) {
1214 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
1216 if (cmd->tag == (uintptr_t) reply)
1223 log_err("fio: client: unable to find matching tag (%llx)\n", (unsigned long long) cmd->tag);
1227 flist_del(&reply->list);
1228 cmd->tag = reply->saved_tag;
1233 int fio_client_wait_for_reply(struct fio_client *client, uint64_t tag)
1236 struct fio_net_cmd_reply *reply = NULL;
1237 struct flist_head *entry;
1239 flist_for_each(entry, &client->cmd_list) {
1240 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
1242 if (tag == (uintptr_t) reply)
1257 static void handle_eta(struct fio_client *client, struct fio_net_cmd *cmd)
1259 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
1260 struct client_eta *eta = (struct client_eta *) (uintptr_t) cmd->tag;
1262 dprint(FD_NET, "client: got eta tag %p, %d\n", eta, eta->pending);
1264 assert(client->eta_in_flight == eta);
1266 client->eta_in_flight = NULL;
1267 flist_del_init(&client->eta_list);
1268 client->eta_timeouts = 0;
1270 if (client->ops->jobs_eta)
1271 client->ops->jobs_eta(client, je);
1273 fio_client_sum_jobs_eta(&eta->eta, je);
1274 fio_client_dec_jobs_eta(eta, client->ops->eta);
1277 static void client_flush_hist_samples(FILE *f, int hist_coarseness, void *samples,
1278 uint64_t sample_size)
1280 struct io_sample *s;
1282 uint64_t i, j, nr_samples;
1283 struct io_u_plat_entry *entry;
1284 uint64_t *io_u_plat;
1286 int stride = 1 << hist_coarseness;
1291 s = __get_sample(samples, 0, 0);
1292 log_offset = (s->__ddir & LOG_OFFSET_SAMPLE_BIT) != 0;
1294 nr_samples = sample_size / __log_entry_sz(log_offset);
1296 for (i = 0; i < nr_samples; i++) {
1298 s = (struct io_sample *)((char *)__get_sample(samples, log_offset, i) +
1299 i * sizeof(struct io_u_plat_entry));
1301 entry = s->data.plat_entry;
1302 io_u_plat = entry->io_u_plat;
1304 fprintf(f, "%lu, %u, %u, ", (unsigned long) s->time,
1305 io_sample_ddir(s), s->bs);
1306 for (j = 0; j < FIO_IO_U_PLAT_NR - stride; j += stride) {
1307 fprintf(f, "%llu, ", (unsigned long long)hist_sum(j, stride, io_u_plat, NULL));
1309 fprintf(f, "%llu\n", (unsigned long long)
1310 hist_sum(FIO_IO_U_PLAT_NR - stride, stride, io_u_plat, NULL));
1315 static int fio_client_handle_iolog(struct fio_client *client,
1316 struct fio_net_cmd *cmd)
1318 struct cmd_iolog_pdu *pdu = NULL;
1320 char *log_pathname = NULL;
1323 pdu = convert_iolog(cmd, &store_direct);
1325 log_err("fio: failed converting IO log\n");
1330 /* allocate buffer big enough for next sprintf() call */
1331 log_pathname = malloc(10 + strlen((char *)pdu->name) +
1332 strlen(client->hostname));
1333 if (!log_pathname) {
1334 log_err("fio: memory allocation of unique pathname failed\n");
1338 /* generate a unique pathname for the log file using hostname */
1339 sprintf(log_pathname, "%s.%s", pdu->name, client->hostname);
1346 fd = open((const char *) log_pathname,
1347 O_WRONLY | O_CREAT | O_TRUNC, 0644);
1349 log_err("fio: open log %s: %s\n",
1350 log_pathname, strerror(errno));
1355 sz = cmd->pdu_len - sizeof(*pdu);
1356 ret = write(fd, pdu->samples, sz);
1360 log_err("fio: short write on compressed log\n");
1368 f = fopen((const char *) log_pathname, "w");
1370 log_err("fio: fopen log %s : %s\n",
1371 log_pathname, strerror(errno));
1376 if (pdu->log_type == IO_LOG_TYPE_HIST) {
1377 client_flush_hist_samples(f, pdu->log_hist_coarseness, pdu->samples,
1378 pdu->nr_samples * sizeof(struct io_sample));
1380 flush_samples(f, pdu->samples,
1381 pdu->nr_samples * sizeof(struct io_sample));
1388 if (pdu && pdu != (void *) cmd->payload)
1397 static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd)
1399 struct cmd_probe_reply_pdu *probe = (struct cmd_probe_reply_pdu *) cmd->payload;
1400 const char *os, *arch;
1403 os = fio_get_os_string(probe->os);
1407 arch = fio_get_arch_string(probe->arch);
1411 sprintf(bit, "%d-bit", probe->bpp * 8);
1412 probe->flags = le64_to_cpu(probe->flags);
1414 log_info("hostname=%s, be=%u, %s, os=%s, arch=%s, fio=%s, flags=%lx\n",
1415 probe->hostname, probe->bigendian, bit, os, arch,
1416 probe->fio_version, (unsigned long) probe->flags);
1419 client->name = strdup((char *) probe->hostname);
1422 static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd)
1424 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
1426 client->state = Client_started;
1427 client->jobs = le32_to_cpu(pdu->jobs);
1428 client->nr_stat = le32_to_cpu(pdu->stat_outputs);
1433 if (client->opt_lists)
1434 free(client->opt_lists);
1436 client->opt_lists = malloc(client->jobs * sizeof(struct flist_head));
1437 for (i = 0; i < client->jobs; i++)
1438 INIT_FLIST_HEAD(&client->opt_lists[i]);
1441 sum_stat_clients += client->nr_stat;
1444 static void handle_stop(struct fio_client *client, struct fio_net_cmd *cmd)
1447 log_info("client <%s>: exited with error %d\n", client->hostname, client->error);
1450 static void convert_stop(struct fio_net_cmd *cmd)
1452 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
1454 pdu->error = le32_to_cpu(pdu->error);
1457 static void convert_text(struct fio_net_cmd *cmd)
1459 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
1461 pdu->level = le32_to_cpu(pdu->level);
1462 pdu->buf_len = le32_to_cpu(pdu->buf_len);
1463 pdu->log_sec = le64_to_cpu(pdu->log_sec);
1464 pdu->log_usec = le64_to_cpu(pdu->log_usec);
1467 static struct cmd_iolog_pdu *convert_iolog_gz(struct fio_net_cmd *cmd,
1468 struct cmd_iolog_pdu *pdu)
1471 struct cmd_iolog_pdu *ret;
1473 uint32_t nr_samples;
1477 stream.zalloc = Z_NULL;
1478 stream.zfree = Z_NULL;
1479 stream.opaque = Z_NULL;
1480 stream.avail_in = 0;
1481 stream.next_in = Z_NULL;
1483 if (inflateInit(&stream) != Z_OK)
1487 * Get header first, it's not compressed
1489 nr_samples = le64_to_cpu(pdu->nr_samples);
1491 if (pdu->log_type == IO_LOG_TYPE_HIST)
1492 total = nr_samples * (__log_entry_sz(le32_to_cpu(pdu->log_offset)) +
1493 sizeof(struct io_u_plat_entry));
1495 total = nr_samples * __log_entry_sz(le32_to_cpu(pdu->log_offset));
1496 ret = malloc(total + sizeof(*pdu));
1497 ret->nr_samples = nr_samples;
1499 memcpy(ret, pdu, sizeof(*pdu));
1501 p = (char *) ret + sizeof(*pdu);
1503 stream.avail_in = cmd->pdu_len - sizeof(*pdu);
1504 stream.next_in = (void *)((char *) pdu + sizeof(*pdu));
1505 while (stream.avail_in) {
1506 unsigned int this_chunk = 65536;
1507 unsigned int this_len;
1510 if (this_chunk > total)
1513 stream.avail_out = this_chunk;
1514 stream.next_out = (void *)p;
1515 err = inflate(&stream, Z_NO_FLUSH);
1516 /* may be Z_OK, or Z_STREAM_END */
1518 log_err("fio: inflate error %d\n", err);
1524 this_len = this_chunk - stream.avail_out;
1530 inflateEnd(&stream);
1538 * This has been compressed on the server side, since it can be big.
1541 static struct cmd_iolog_pdu *convert_iolog(struct fio_net_cmd *cmd,
1544 struct cmd_iolog_pdu *pdu = (struct cmd_iolog_pdu *) cmd->payload;
1545 struct cmd_iolog_pdu *ret;
1550 *store_direct = false;
1553 * Convert if compressed and we support it. If it's not
1554 * compressed, we need not do anything.
1556 compressed = le32_to_cpu(pdu->compressed);
1557 if (compressed == XMIT_COMPRESSED) {
1559 log_err("fio: server sent compressed data by mistake\n");
1562 ret = convert_iolog_gz(cmd, pdu);
1564 log_err("fio: failed decompressing log\n");
1567 } else if (compressed == STORE_COMPRESSED) {
1568 *store_direct = true;
1573 ret->nr_samples = le64_to_cpu(ret->nr_samples);
1574 ret->thread_number = le32_to_cpu(ret->thread_number);
1575 ret->log_type = le32_to_cpu(ret->log_type);
1576 ret->compressed = le32_to_cpu(ret->compressed);
1577 ret->log_offset = le32_to_cpu(ret->log_offset);
1578 ret->log_hist_coarseness = le32_to_cpu(ret->log_hist_coarseness);
1583 samples = &ret->samples[0];
1584 for (i = 0; i < ret->nr_samples; i++) {
1585 struct io_sample *s;
1587 s = __get_sample(samples, ret->log_offset, i);
1588 if (ret->log_type == IO_LOG_TYPE_HIST)
1589 s = (struct io_sample *)((char *)s + sizeof(struct io_u_plat_entry) * i);
1591 s->time = le64_to_cpu(s->time);
1592 s->data.val = le64_to_cpu(s->data.val);
1593 s->__ddir = le32_to_cpu(s->__ddir);
1594 s->bs = le32_to_cpu(s->bs);
1596 if (ret->log_offset) {
1597 struct io_sample_offset *so = (void *) s;
1599 so->offset = le64_to_cpu(so->offset);
1602 if (ret->log_type == IO_LOG_TYPE_HIST) {
1603 s->data.plat_entry = (struct io_u_plat_entry *)(((char *)s) + sizeof(*s));
1604 s->data.plat_entry->list.next = NULL;
1605 s->data.plat_entry->list.prev = NULL;
1612 static void sendfile_reply(int fd, struct cmd_sendfile_reply *rep,
1613 size_t size, uint64_t tag)
1615 rep->error = cpu_to_le32(rep->error);
1616 fio_net_send_cmd(fd, FIO_NET_CMD_SENDFILE, rep, size, &tag, NULL);
1619 static int fio_send_file(struct fio_client *client, struct cmd_sendfile *pdu,
1622 struct cmd_sendfile_reply *rep;
1627 size = sizeof(*rep);
1630 if (stat((char *)pdu->path, &sb) < 0) {
1633 sendfile_reply(client->fd, rep, size, tag);
1639 rep = realloc(rep, size);
1640 rep->size = cpu_to_le32((uint32_t) sb.st_size);
1642 fd = open((char *)pdu->path, O_RDONLY);
1646 rep->error = read_data(fd, &rep->data, sb.st_size);
1647 sendfile_reply(client->fd, rep, size, tag);
1653 int fio_handle_client(struct fio_client *client)
1655 struct client_ops *ops = client->ops;
1656 struct fio_net_cmd *cmd;
1659 dprint(FD_NET, "client: handle %s\n", client->hostname);
1661 cmd = fio_net_recv_cmd(client->fd, true);
1665 dprint(FD_NET, "client: got cmd op %s from %s (pdu=%u)\n",
1666 fio_server_op(cmd->opcode), client->hostname, cmd->pdu_len);
1668 client->last_cmd = cmd->opcode;
1670 switch (cmd->opcode) {
1671 case FIO_NET_CMD_QUIT:
1673 ops->quit(client, cmd);
1674 remove_client(client);
1676 case FIO_NET_CMD_TEXT:
1678 ops->text(client, cmd);
1680 case FIO_NET_CMD_DU: {
1681 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
1683 convert_dus(&du->dus);
1684 convert_agg(&du->agg);
1686 ops->disk_util(client, cmd);
1689 case FIO_NET_CMD_TS: {
1690 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
1692 dprint(FD_NET, "client: ts->ss_state = %u\n", (unsigned int) le32_to_cpu(p->ts.ss_state));
1693 if (le32_to_cpu(p->ts.ss_state) & FIO_SS_DATA) {
1694 dprint(FD_NET, "client: received steadystate ring buffers\n");
1696 size = le64_to_cpu(p->ts.ss_dur);
1697 p->ts.ss_iops_data = (uint64_t *) ((struct cmd_ts_pdu *)cmd->payload + 1);
1698 p->ts.ss_bw_data = p->ts.ss_iops_data + size;
1701 convert_ts(&p->ts, &p->ts);
1702 convert_gs(&p->rs, &p->rs);
1704 ops->thread_status(client, cmd);
1707 case FIO_NET_CMD_GS: {
1708 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
1712 ops->group_stats(client, cmd);
1715 case FIO_NET_CMD_ETA: {
1716 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
1718 if (!remove_reply_cmd(client, cmd))
1720 convert_jobs_eta(je);
1721 handle_eta(client, cmd);
1724 case FIO_NET_CMD_PROBE:
1725 remove_reply_cmd(client, cmd);
1726 ops->probe(client, cmd);
1728 case FIO_NET_CMD_SERVER_START:
1729 client->state = Client_running;
1731 ops->job_start(client, cmd);
1733 case FIO_NET_CMD_START: {
1734 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
1736 pdu->jobs = le32_to_cpu(pdu->jobs);
1737 ops->start(client, cmd);
1740 case FIO_NET_CMD_STOP: {
1741 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
1744 client->state = Client_stopped;
1745 client->error = le32_to_cpu(pdu->error);
1746 client->signal = le32_to_cpu(pdu->signal);
1747 ops->stop(client, cmd);
1750 case FIO_NET_CMD_ADD_JOB: {
1751 struct cmd_add_job_pdu *pdu = (struct cmd_add_job_pdu *) cmd->payload;
1753 client->thread_number = le32_to_cpu(pdu->thread_number);
1754 client->groupid = le32_to_cpu(pdu->groupid);
1757 ops->add_job(client, cmd);
1760 case FIO_NET_CMD_IOLOG:
1761 fio_client_handle_iolog(client, cmd);
1763 case FIO_NET_CMD_UPDATE_JOB:
1764 ops->update_job(client, cmd);
1765 remove_reply_cmd(client, cmd);
1767 case FIO_NET_CMD_VTRIGGER: {
1768 struct all_io_list *pdu = (struct all_io_list *) cmd->payload;
1773 strcpy(buf, aux_path);
1777 __verify_save_state(pdu, server_name(client, &buf[off], sizeof(buf) - off));
1778 exec_trigger(trigger_cmd);
1781 case FIO_NET_CMD_SENDFILE: {
1782 struct cmd_sendfile *pdu = (struct cmd_sendfile *) cmd->payload;
1783 fio_send_file(client, pdu, cmd->tag);
1786 case FIO_NET_CMD_JOB_OPT: {
1787 handle_job_opt(client, cmd);
1791 log_err("fio: unknown client op: %s\n", fio_server_op(cmd->opcode));
1799 int fio_clients_send_trigger(const char *cmd)
1801 struct flist_head *entry;
1802 struct fio_client *client;
1805 dprint(FD_NET, "client: send vtrigger: %s\n", cmd);
1812 flist_for_each(entry, &client_list) {
1813 struct cmd_vtrigger_pdu *pdu;
1815 client = flist_entry(entry, struct fio_client, list);
1817 pdu = malloc(sizeof(*pdu) + slen);
1818 pdu->len = cpu_to_le16((uint16_t) slen);
1820 memcpy(pdu->cmd, cmd, slen);
1821 fio_net_send_cmd(client->fd, FIO_NET_CMD_VTRIGGER, pdu,
1822 sizeof(*pdu) + slen, NULL, NULL);
1829 static void request_client_etas(struct client_ops *ops)
1831 struct fio_client *client;
1832 struct flist_head *entry;
1833 struct client_eta *eta;
1836 if (eta_print == FIO_ETA_NEVER)
1839 dprint(FD_NET, "client: request eta (%d)\n", nr_clients);
1841 eta = calloc(1, sizeof(*eta) + __THREAD_RUNSTR_SZ(REAL_MAX_JOBS));
1842 eta->pending = nr_clients;
1844 flist_for_each(entry, &client_list) {
1845 client = flist_entry(entry, struct fio_client, list);
1847 if (!flist_empty(&client->eta_list)) {
1851 if (client->state != Client_running)
1854 assert(!client->eta_in_flight);
1855 flist_add_tail(&client->eta_list, &eta_list);
1856 client->eta_in_flight = eta;
1857 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_SEND_ETA,
1858 (uintptr_t) eta, &client->cmd_list);
1862 if (!fio_client_dec_jobs_eta(eta, ops->eta))
1866 dprint(FD_NET, "client: requested eta tag %p\n", eta);
1870 * A single SEND_ETA timeout isn't fatal. Attempt to recover.
1872 static int handle_cmd_timeout(struct fio_client *client,
1873 struct fio_net_cmd_reply *reply)
1875 uint16_t reply_opcode = reply->opcode;
1877 flist_del(&reply->list);
1880 if (reply_opcode != FIO_NET_CMD_SEND_ETA)
1883 log_info("client <%s>: timeout on SEND_ETA\n", client->hostname);
1885 flist_del_init(&client->eta_list);
1886 if (client->eta_in_flight) {
1887 fio_client_dec_jobs_eta(client->eta_in_flight, client->ops->eta);
1888 client->eta_in_flight = NULL;
1892 * If we fail 5 in a row, give up...
1894 if (client->eta_timeouts++ > 5)
1900 static int client_check_cmd_timeout(struct fio_client *client,
1901 struct timespec *now)
1903 struct fio_net_cmd_reply *reply;
1904 struct flist_head *entry, *tmp;
1907 flist_for_each_safe(entry, tmp, &client->cmd_list) {
1910 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
1912 if (mtime_since(&reply->ts, now) < FIO_NET_CLIENT_TIMEOUT)
1916 if (!handle_cmd_timeout(client, reply))
1919 log_err("fio: client %s, timeout on cmd %s\n", client->hostname,
1924 return flist_empty(&client->cmd_list) && ret;
1927 static int fio_check_clients_timed_out(void)
1929 struct fio_client *client;
1930 struct flist_head *entry, *tmp;
1934 fio_gettime(&ts, NULL);
1936 flist_for_each_safe(entry, tmp, &client_list) {
1937 client = flist_entry(entry, struct fio_client, list);
1939 if (flist_empty(&client->cmd_list))
1942 if (!client_check_cmd_timeout(client, &ts))
1945 if (client->ops->timed_out)
1946 client->ops->timed_out(client);
1948 log_err("fio: client %s timed out\n", client->hostname);
1950 if (client->last_cmd != FIO_NET_CMD_VTRIGGER)
1951 client->error = ETIMEDOUT;
1953 log_info("fio: ignoring timeout due to vtrigger\n");
1954 remove_client(client);
1961 int fio_handle_clients(struct client_ops *ops)
1963 struct pollfd *pfds;
1964 int i, ret = 0, retval = 0;
1966 fio_gettime(&eta_ts, NULL);
1968 pfds = malloc(nr_clients * sizeof(struct pollfd));
1970 init_thread_stat(&client_ts);
1971 init_group_run_stat(&client_gs);
1973 while (!exit_backend && nr_clients) {
1974 struct flist_head *entry, *tmp;
1975 struct fio_client *client;
1978 flist_for_each_safe(entry, tmp, &client_list) {
1979 client = flist_entry(entry, struct fio_client, list);
1981 if (!client->sent_job && !client->ops->stay_connected &&
1982 flist_empty(&client->cmd_list)) {
1983 remove_client(client);
1987 pfds[i].fd = client->fd;
1988 pfds[i].events = POLLIN;
1995 assert(i == nr_clients);
2001 fio_gettime(&ts, NULL);
2002 if (eta_time_within_slack(mtime_since(&eta_ts, &ts))) {
2003 request_client_etas(ops);
2004 memcpy(&eta_ts, &ts, sizeof(ts));
2006 if (fio_check_clients_timed_out())
2010 check_trigger_file();
2012 timeout = min(100u, ops->eta_msec);
2014 ret = poll(pfds, nr_clients, timeout);
2018 log_err("fio: poll clients: %s\n", strerror(errno));
2024 for (i = 0; i < nr_clients; i++) {
2025 if (!(pfds[i].revents & POLLIN))
2028 client = find_client_by_fd(pfds[i].fd);
2030 log_err("fio: unknown client fd %ld\n", (long) pfds[i].fd);
2033 if (!fio_handle_client(client)) {
2034 log_info("client: host=%s disconnected\n",
2036 remove_client(client);
2038 } else if (client->error)
2040 fio_put_client(client);
2044 fio_client_json_fini();
2047 return retval || error_clients;