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)
127 fp = fdopen(dup(fd), "r");
133 char buf[OPT_LEN_MAX+1], *sub;
135 if (!fgets(buf, sizeof(buf), fp)) {
137 if (errno == EAGAIN || errno == EINTR)
144 sub = fio_option_dup_subs(buf);
146 if (len + 1 > size) {
147 log_err("fio: no space left to read data\n");
164 static void fio_client_json_init(void)
169 if (!(output_format & FIO_OUTPUT_JSON))
173 os_ctime_r((const time_t *) &time_p, time_buf, sizeof(time_buf));
174 time_buf[strlen(time_buf) - 1] = '\0';
176 root = json_create_object();
177 json_object_add_value_string(root, "fio version", fio_version_string);
178 json_object_add_value_int(root, "timestamp", time_p);
179 json_object_add_value_string(root, "time", time_buf);
181 job_opt_object = json_create_object();
182 json_object_add_value_object(root, "global options", job_opt_object);
183 clients_array = json_create_array();
184 json_object_add_value_array(root, "client_stats", clients_array);
185 du_array = json_create_array();
186 json_object_add_value_array(root, "disk_util", du_array);
189 static void fio_client_json_fini(void)
191 if (!(output_format & FIO_OUTPUT_JSON))
195 json_print_object(root, NULL);
197 json_free_object(root);
199 clients_array = NULL;
203 static struct fio_client *find_client_by_fd(int fd)
205 int bucket = hash_long(fd, FIO_CLIENT_HASH_BITS) & FIO_CLIENT_HASH_MASK;
206 struct fio_client *client;
207 struct flist_head *entry;
209 flist_for_each(entry, &client_hash[bucket]) {
210 client = flist_entry(entry, struct fio_client, hash_list);
212 if (client->fd == fd) {
221 void fio_put_client(struct fio_client *client)
226 free(client->hostname);
231 while (client->nr_files) {
232 struct client_file *cf = &client->files[--client->nr_files];
238 if (client->opt_lists)
239 free(client->opt_lists);
241 if (!client->did_stat)
250 static int fio_client_dec_jobs_eta(struct client_eta *eta, client_eta_op eta_fn)
252 if (!--eta->pending) {
261 static void fio_drain_client_text(struct fio_client *client)
264 struct fio_net_cmd *cmd;
266 cmd = fio_net_recv_cmd(client->fd, false);
270 if (cmd->opcode == FIO_NET_CMD_TEXT) {
272 client->ops->text(client, cmd);
279 static void remove_client(struct fio_client *client)
281 assert(client->refs);
283 dprint(FD_NET, "client: removed <%s>\n", client->hostname);
285 fio_drain_client_text(client);
287 if (!flist_empty(&client->list))
288 flist_del_init(&client->list);
290 fio_client_remove_hash(client);
292 if (!flist_empty(&client->eta_list)) {
293 flist_del_init(&client->eta_list);
294 fio_client_dec_jobs_eta(client->eta_in_flight, client->ops->eta);
300 if (client->ops->removed)
301 client->ops->removed(client);
304 fio_put_client(client);
307 struct fio_client *fio_get_client(struct fio_client *client)
313 static void __fio_client_add_cmd_option(struct fio_client *client,
318 index = client->argc++;
319 client->argv = realloc(client->argv, sizeof(char *) * client->argc);
320 client->argv[index] = strdup(opt);
321 dprint(FD_NET, "client: add cmd %d: %s\n", index, opt);
324 void fio_client_add_cmd_option(void *cookie, const char *opt)
326 struct fio_client *client = cookie;
327 struct flist_head *entry;
332 __fio_client_add_cmd_option(client, opt);
335 * Duplicate arguments to shared client group
337 flist_for_each(entry, &arg_list) {
338 client = flist_entry(entry, struct fio_client, arg_list);
340 __fio_client_add_cmd_option(client, opt);
344 struct fio_client *fio_client_add_explicit(struct client_ops *ops,
345 const char *hostname, int type,
348 struct fio_client *client;
350 client = malloc(sizeof(*client));
351 memset(client, 0, sizeof(*client));
353 INIT_FLIST_HEAD(&client->list);
354 INIT_FLIST_HEAD(&client->hash_list);
355 INIT_FLIST_HEAD(&client->arg_list);
356 INIT_FLIST_HEAD(&client->eta_list);
357 INIT_FLIST_HEAD(&client->cmd_list);
359 client->hostname = strdup(hostname);
361 if (type == Fio_client_socket)
362 client->is_sock = true;
366 ipv6 = type == Fio_client_ipv6;
367 if (fio_server_parse_host(hostname, ipv6,
368 &client->addr.sin_addr,
369 &client->addr6.sin6_addr))
378 client->type = ops->client_type;
380 __fio_client_add_cmd_option(client, "fio");
382 flist_add(&client->list, &client_list);
384 dprint(FD_NET, "client: added <%s>\n", client->hostname);
391 int fio_client_add_ini_file(void *cookie, const char *ini_file, bool remote)
393 struct fio_client *client = cookie;
394 struct client_file *cf;
401 dprint(FD_NET, "client <%s>: add ini %s\n", client->hostname, ini_file);
403 new_size = (client->nr_files + 1) * sizeof(struct client_file);
404 new_files = realloc(client->files, new_size);
408 client->files = new_files;
409 cf = &client->files[client->nr_files];
410 cf->file = strdup(ini_file);
416 int fio_client_add(struct client_ops *ops, const char *hostname, void **cookie)
418 struct fio_client *existing = *cookie;
419 struct fio_client *client;
423 * We always add our "exec" name as the option, hence 1
426 if (existing->argc == 1)
427 flist_add_tail(&existing->arg_list, &arg_list);
429 while (!flist_empty(&arg_list))
430 flist_del_init(arg_list.next);
434 client = malloc(sizeof(*client));
435 memset(client, 0, sizeof(*client));
437 INIT_FLIST_HEAD(&client->list);
438 INIT_FLIST_HEAD(&client->hash_list);
439 INIT_FLIST_HEAD(&client->arg_list);
440 INIT_FLIST_HEAD(&client->eta_list);
441 INIT_FLIST_HEAD(&client->cmd_list);
443 if (fio_server_parse_string(hostname, &client->hostname,
444 &client->is_sock, &client->port,
445 &client->addr.sin_addr,
446 &client->addr6.sin6_addr,
453 client->type = ops->client_type;
455 __fio_client_add_cmd_option(client, "fio");
457 flist_add(&client->list, &client_list);
459 dprint(FD_NET, "client: added <%s>\n", client->hostname);
464 static const char *server_name(struct fio_client *client, char *buf,
470 from = inet_ntop(AF_INET6, (struct sockaddr *) &client->addr6.sin6_addr, buf, bufsize);
471 else if (client->is_sock)
474 from = inet_ntop(AF_INET, (struct sockaddr *) &client->addr.sin_addr, buf, bufsize);
479 static void probe_client(struct fio_client *client)
481 struct cmd_client_probe_pdu pdu;
486 dprint(FD_NET, "client: send probe\n");
489 pdu.flags = __le64_to_cpu(FIO_PROBE_FLAG_ZLIB);
494 sname = server_name(client, buf, sizeof(buf));
495 memset(pdu.server, 0, sizeof(pdu.server));
496 strncpy((char *) pdu.server, sname, sizeof(pdu.server) - 1);
498 fio_net_send_cmd(client->fd, FIO_NET_CMD_PROBE, &pdu, sizeof(pdu), &tag, &client->cmd_list);
501 static int fio_client_connect_ip(struct fio_client *client)
503 struct sockaddr *addr;
508 client->addr6.sin6_family = AF_INET6;
509 client->addr6.sin6_port = htons(client->port);
511 addr = (struct sockaddr *) &client->addr6;
512 socklen = sizeof(client->addr6);
514 client->addr.sin_family = AF_INET;
515 client->addr.sin_port = htons(client->port);
517 addr = (struct sockaddr *) &client->addr;
518 socklen = sizeof(client->addr);
521 fd = socket(domain, SOCK_STREAM, 0);
525 log_err("fio: socket: %s\n", strerror(errno));
529 if (connect(fd, addr, socklen) < 0) {
532 log_err("fio: connect: %s\n", strerror(errno));
533 log_err("fio: failed to connect to %s:%u\n", client->hostname,
542 static int fio_client_connect_sock(struct fio_client *client)
544 struct sockaddr_un *addr = &client->addr_un;
548 memset(addr, 0, sizeof(*addr));
549 addr->sun_family = AF_UNIX;
550 strncpy(addr->sun_path, client->hostname, sizeof(addr->sun_path) - 1);
552 fd = socket(AF_UNIX, SOCK_STREAM, 0);
556 log_err("fio: socket: %s\n", strerror(errno));
560 len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
561 if (connect(fd, (struct sockaddr *) addr, len) < 0) {
564 log_err("fio: connect; %s\n", strerror(errno));
572 int fio_client_connect(struct fio_client *client)
576 dprint(FD_NET, "client: connect to host %s\n", client->hostname);
579 fd = fio_client_connect_sock(client);
581 fd = fio_client_connect_ip(client);
583 dprint(FD_NET, "client: %s connected %d\n", client->hostname, fd);
589 fio_client_add_hash(client);
590 client->state = Client_connected;
592 probe_client(client);
596 int fio_client_terminate(struct fio_client *client)
598 return fio_net_send_quit(client->fd);
601 static void fio_clients_terminate(void)
603 struct flist_head *entry;
604 struct fio_client *client;
606 dprint(FD_NET, "client: terminate clients\n");
608 flist_for_each(entry, &client_list) {
609 client = flist_entry(entry, struct fio_client, list);
610 fio_client_terminate(client);
614 static void sig_int(int sig)
616 dprint(FD_NET, "client: got signal %d\n", sig);
617 fio_clients_terminate();
620 static void client_signal_handler(void)
622 struct sigaction act;
624 memset(&act, 0, sizeof(act));
625 act.sa_handler = sig_int;
626 act.sa_flags = SA_RESTART;
627 sigaction(SIGINT, &act, NULL);
629 memset(&act, 0, sizeof(act));
630 act.sa_handler = sig_int;
631 act.sa_flags = SA_RESTART;
632 sigaction(SIGTERM, &act, NULL);
634 /* Windows uses SIGBREAK as a quit signal from other applications */
636 memset(&act, 0, sizeof(act));
637 act.sa_handler = sig_int;
638 act.sa_flags = SA_RESTART;
639 sigaction(SIGBREAK, &act, NULL);
642 memset(&act, 0, sizeof(act));
643 act.sa_handler = sig_show_status;
644 act.sa_flags = SA_RESTART;
645 sigaction(SIGUSR1, &act, NULL);
648 static int send_client_cmd_line(struct fio_client *client)
650 struct cmd_single_line_pdu *cslp;
651 struct cmd_line_pdu *clp;
652 unsigned long offset;
658 dprint(FD_NET, "client: send cmdline %d\n", client->argc);
660 lens = malloc(client->argc * sizeof(unsigned int));
663 * Find out how much mem we need
665 for (i = 0, mem = 0; i < client->argc; i++) {
666 lens[i] = strlen(client->argv[i]) + 1;
671 * We need one cmd_line_pdu, and argc number of cmd_single_line_pdu
673 mem += sizeof(*clp) + (client->argc * sizeof(*cslp));
677 offset = sizeof(*clp);
679 for (i = 0; i < client->argc; i++) {
680 uint16_t arg_len = lens[i];
683 strcpy((char *) cslp->text, client->argv[i]);
684 cslp->len = cpu_to_le16(arg_len);
685 offset += sizeof(*cslp) + arg_len;
689 clp->lines = cpu_to_le16(client->argc);
690 clp->client_type = __cpu_to_le16(client->type);
691 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOBLINE, pdu, mem, NULL, NULL);
696 int fio_clients_connect(void)
698 struct fio_client *client;
699 struct flist_head *entry, *tmp;
704 WSAStartup(MAKEWORD(2, 2), &wsd);
707 dprint(FD_NET, "client: connect all\n");
709 client_signal_handler();
711 flist_for_each_safe(entry, tmp, &client_list) {
712 client = flist_entry(entry, struct fio_client, list);
714 ret = fio_client_connect(client);
716 remove_client(client);
720 if (client->argc > 1)
721 send_client_cmd_line(client);
727 int fio_start_client(struct fio_client *client)
729 dprint(FD_NET, "client: start %s\n", client->hostname);
730 return fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_RUN, 0, NULL);
733 int fio_start_all_clients(void)
735 struct fio_client *client;
736 struct flist_head *entry, *tmp;
739 dprint(FD_NET, "client: start all\n");
741 fio_client_json_init();
743 flist_for_each_safe(entry, tmp, &client_list) {
744 client = flist_entry(entry, struct fio_client, list);
746 ret = fio_start_client(client);
748 remove_client(client);
753 return flist_empty(&client_list);
756 static int __fio_client_send_remote_ini(struct fio_client *client,
757 const char *filename)
759 struct cmd_load_file_pdu *pdu;
763 dprint(FD_NET, "send remote ini %s to %s\n", filename, client->hostname);
765 p_size = sizeof(*pdu) + strlen(filename) + 1;
766 pdu = malloc(p_size);
767 memset(pdu, 0, p_size);
768 pdu->name_len = strlen(filename);
769 strcpy((char *) pdu->file, filename);
770 pdu->client_type = cpu_to_le16((uint16_t) client->type);
772 client->sent_job = true;
773 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_LOAD_FILE, pdu, p_size,NULL, NULL);
779 * Send file contents to server backend. We could use sendfile(), but to remain
780 * more portable lets just read/write the darn thing.
782 static int __fio_client_send_local_ini(struct fio_client *client,
783 const char *filename)
785 struct cmd_job_pdu *pdu;
793 dprint(FD_NET, "send ini %s to %s\n", filename, client->hostname);
795 fd = open(filename, O_RDONLY);
798 log_err("fio: job file <%s> open: %s\n", filename, strerror(errno));
802 if (fstat(fd, &sb) < 0) {
804 log_err("fio: job file stat: %s\n", strerror(errno));
810 * Add extra space for variable expansion, but doesn't guarantee.
812 sb.st_size += OPT_LEN_MAX;
813 p_size = sb.st_size + sizeof(*pdu);
814 pdu = malloc(p_size);
819 if (read_ini_data(fd, p, len)) {
820 log_err("fio: failed reading job file %s\n", filename);
826 pdu->buf_len = __cpu_to_le32(sb.st_size);
827 pdu->client_type = cpu_to_le32(client->type);
829 client->sent_job = true;
830 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, pdu, p_size, NULL, NULL);
836 int fio_client_send_ini(struct fio_client *client, const char *filename,
842 ret = __fio_client_send_local_ini(client, filename);
844 ret = __fio_client_send_remote_ini(client, filename);
847 client->sent_job = true;
852 static int fio_client_send_cf(struct fio_client *client,
853 struct client_file *cf)
855 return fio_client_send_ini(client, cf->file, cf->remote);
858 int fio_clients_send_ini(const char *filename)
860 struct fio_client *client;
861 struct flist_head *entry, *tmp;
863 flist_for_each_safe(entry, tmp, &client_list) {
866 client = flist_entry(entry, struct fio_client, list);
868 if (client->nr_files) {
871 for (i = 0; i < client->nr_files; i++) {
872 struct client_file *cf;
874 cf = &client->files[i];
876 if (fio_client_send_cf(client, cf)) {
878 remove_client(client);
883 if (client->sent_job || failed)
885 if (!filename || fio_client_send_ini(client, filename, 0))
886 remove_client(client);
892 int fio_client_update_options(struct fio_client *client,
893 struct thread_options *o, uint64_t *tag)
895 struct cmd_add_job_pdu pdu;
897 pdu.thread_number = cpu_to_le32(client->thread_number);
898 pdu.groupid = cpu_to_le32(client->groupid);
899 convert_thread_options_to_net(&pdu.top, o);
901 return fio_net_send_cmd(client->fd, FIO_NET_CMD_UPDATE_JOB, &pdu, sizeof(pdu), tag, &client->cmd_list);
904 static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
906 dst->max_val = le64_to_cpu(src->max_val);
907 dst->min_val = le64_to_cpu(src->min_val);
908 dst->samples = le64_to_cpu(src->samples);
911 * Floats arrive as IEEE 754 encoded uint64_t, convert back to double
913 dst->mean.u.f = fio_uint64_to_double(le64_to_cpu(dst->mean.u.i));
914 dst->S.u.f = fio_uint64_to_double(le64_to_cpu(dst->S.u.i));
917 static void convert_ts(struct thread_stat *dst, struct thread_stat *src)
921 dst->error = le32_to_cpu(src->error);
922 dst->thread_number = le32_to_cpu(src->thread_number);
923 dst->groupid = le32_to_cpu(src->groupid);
924 dst->pid = le32_to_cpu(src->pid);
925 dst->members = le32_to_cpu(src->members);
926 dst->unified_rw_rep = le32_to_cpu(src->unified_rw_rep);
928 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
929 convert_io_stat(&dst->clat_stat[i], &src->clat_stat[i]);
930 convert_io_stat(&dst->slat_stat[i], &src->slat_stat[i]);
931 convert_io_stat(&dst->lat_stat[i], &src->lat_stat[i]);
932 convert_io_stat(&dst->bw_stat[i], &src->bw_stat[i]);
933 convert_io_stat(&dst->iops_stat[i], &src->iops_stat[i]);
936 dst->usr_time = le64_to_cpu(src->usr_time);
937 dst->sys_time = le64_to_cpu(src->sys_time);
938 dst->ctx = le64_to_cpu(src->ctx);
939 dst->minf = le64_to_cpu(src->minf);
940 dst->majf = le64_to_cpu(src->majf);
941 dst->clat_percentiles = le32_to_cpu(src->clat_percentiles);
942 dst->lat_percentiles = le32_to_cpu(src->lat_percentiles);
943 dst->percentile_precision = le64_to_cpu(src->percentile_precision);
945 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
946 fio_fp64_t *fps = &src->percentile_list[i];
947 fio_fp64_t *fpd = &dst->percentile_list[i];
949 fpd->u.f = fio_uint64_to_double(le64_to_cpu(fps->u.i));
952 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
953 dst->io_u_map[i] = le64_to_cpu(src->io_u_map[i]);
954 dst->io_u_submit[i] = le64_to_cpu(src->io_u_submit[i]);
955 dst->io_u_complete[i] = le64_to_cpu(src->io_u_complete[i]);
958 for (i = 0; i < FIO_IO_U_LAT_N_NR; i++)
959 dst->io_u_lat_n[i] = le64_to_cpu(src->io_u_lat_n[i]);
960 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++)
961 dst->io_u_lat_u[i] = le64_to_cpu(src->io_u_lat_u[i]);
962 for (i = 0; i < FIO_IO_U_LAT_M_NR; i++)
963 dst->io_u_lat_m[i] = le64_to_cpu(src->io_u_lat_m[i]);
965 for (i = 0; i < DDIR_RWDIR_CNT; i++)
966 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
967 dst->io_u_plat[i][j] = le64_to_cpu(src->io_u_plat[i][j]);
969 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
970 dst->total_io_u[i] = le64_to_cpu(src->total_io_u[i]);
971 dst->short_io_u[i] = le64_to_cpu(src->short_io_u[i]);
972 dst->drop_io_u[i] = le64_to_cpu(src->drop_io_u[i]);
975 dst->total_submit = le64_to_cpu(src->total_submit);
976 dst->total_complete = le64_to_cpu(src->total_complete);
978 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
979 dst->io_bytes[i] = le64_to_cpu(src->io_bytes[i]);
980 dst->runtime[i] = le64_to_cpu(src->runtime[i]);
983 dst->total_run_time = le64_to_cpu(src->total_run_time);
984 dst->continue_on_error = le16_to_cpu(src->continue_on_error);
985 dst->total_err_count = le64_to_cpu(src->total_err_count);
986 dst->first_error = le32_to_cpu(src->first_error);
987 dst->kb_base = le32_to_cpu(src->kb_base);
988 dst->unit_base = le32_to_cpu(src->unit_base);
990 dst->sig_figs = le32_to_cpu(src->sig_figs);
992 dst->latency_depth = le32_to_cpu(src->latency_depth);
993 dst->latency_target = le64_to_cpu(src->latency_target);
994 dst->latency_window = le64_to_cpu(src->latency_window);
995 dst->latency_percentile.u.f = fio_uint64_to_double(le64_to_cpu(src->latency_percentile.u.i));
997 dst->nr_block_infos = le64_to_cpu(src->nr_block_infos);
998 for (i = 0; i < dst->nr_block_infos; i++)
999 dst->block_infos[i] = le32_to_cpu(src->block_infos[i]);
1001 dst->ss_dur = le64_to_cpu(src->ss_dur);
1002 dst->ss_state = le32_to_cpu(src->ss_state);
1003 dst->ss_head = le32_to_cpu(src->ss_head);
1004 dst->ss_limit.u.f = fio_uint64_to_double(le64_to_cpu(src->ss_limit.u.i));
1005 dst->ss_slope.u.f = fio_uint64_to_double(le64_to_cpu(src->ss_slope.u.i));
1006 dst->ss_deviation.u.f = fio_uint64_to_double(le64_to_cpu(src->ss_deviation.u.i));
1007 dst->ss_criterion.u.f = fio_uint64_to_double(le64_to_cpu(src->ss_criterion.u.i));
1009 if (dst->ss_state & FIO_SS_DATA) {
1010 for (i = 0; i < dst->ss_dur; i++ ) {
1011 dst->ss_iops_data[i] = le64_to_cpu(src->ss_iops_data[i]);
1012 dst->ss_bw_data[i] = le64_to_cpu(src->ss_bw_data[i]);
1017 static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
1021 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1022 dst->max_run[i] = le64_to_cpu(src->max_run[i]);
1023 dst->min_run[i] = le64_to_cpu(src->min_run[i]);
1024 dst->max_bw[i] = le64_to_cpu(src->max_bw[i]);
1025 dst->min_bw[i] = le64_to_cpu(src->min_bw[i]);
1026 dst->iobytes[i] = le64_to_cpu(src->iobytes[i]);
1027 dst->agg[i] = le64_to_cpu(src->agg[i]);
1030 dst->kb_base = le32_to_cpu(src->kb_base);
1031 dst->unit_base = le32_to_cpu(src->unit_base);
1032 dst->sig_figs = le32_to_cpu(src->sig_figs);
1033 dst->groupid = le32_to_cpu(src->groupid);
1034 dst->unified_rw_rep = le32_to_cpu(src->unified_rw_rep);
1037 static void json_object_add_client_info(struct json_object *obj,
1038 struct fio_client *client)
1040 const char *hostname = client->hostname ? client->hostname : "";
1042 json_object_add_value_string(obj, "hostname", hostname);
1043 json_object_add_value_int(obj, "port", client->port);
1046 static void handle_ts(struct fio_client *client, struct fio_net_cmd *cmd)
1048 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
1049 struct flist_head *opt_list = NULL;
1050 struct json_object *tsobj;
1052 if (client->opt_lists && p->ts.thread_number <= client->jobs)
1053 opt_list = &client->opt_lists[p->ts.thread_number - 1];
1055 tsobj = show_thread_status(&p->ts, &p->rs, opt_list, NULL);
1056 client->did_stat = true;
1058 json_object_add_client_info(tsobj, client);
1059 json_array_add_value_object(clients_array, tsobj);
1062 if (sum_stat_clients <= 1)
1065 sum_thread_stats(&client_ts, &p->ts, sum_stat_nr == 1);
1066 sum_group_stats(&client_gs, &p->rs);
1068 client_ts.members++;
1069 client_ts.thread_number = p->ts.thread_number;
1070 client_ts.groupid = p->ts.groupid;
1071 client_ts.unified_rw_rep = p->ts.unified_rw_rep;
1072 client_ts.sig_figs = p->ts.sig_figs;
1074 if (++sum_stat_nr == sum_stat_clients) {
1075 strcpy(client_ts.name, "All clients");
1076 tsobj = show_thread_status(&client_ts, &client_gs, NULL, NULL);
1078 json_object_add_client_info(tsobj, client);
1079 json_array_add_value_object(clients_array, tsobj);
1084 static void handle_gs(struct fio_client *client, struct fio_net_cmd *cmd)
1086 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
1088 if (output_format & FIO_OUTPUT_NORMAL)
1089 show_group_stats(gs, NULL);
1092 static void handle_job_opt(struct fio_client *client, struct fio_net_cmd *cmd)
1094 struct cmd_job_option *pdu = (struct cmd_job_option *) cmd->payload;
1095 struct print_option *p;
1097 if (!job_opt_object)
1100 pdu->global = le16_to_cpu(pdu->global);
1101 pdu->truncated = le16_to_cpu(pdu->truncated);
1102 pdu->groupid = le32_to_cpu(pdu->groupid);
1104 p = malloc(sizeof(*p));
1105 p->name = strdup((char *) pdu->name);
1106 if (pdu->value[0] != '\0')
1107 p->value = strdup((char *) pdu->value);
1112 const char *pos = "";
1117 json_object_add_value_string(job_opt_object, p->name, pos);
1118 } else if (client->opt_lists) {
1119 struct flist_head *opt_list = &client->opt_lists[pdu->groupid];
1121 flist_add_tail(&p->list, opt_list);
1125 static void handle_text(struct fio_client *client, struct fio_net_cmd *cmd)
1127 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
1128 const char *buf = (const char *) pdu->buf;
1132 name = client->name ? client->name : client->hostname;
1134 if (!client->skip_newline)
1135 fprintf(f_out, "<%s> ", name);
1136 ret = fwrite(buf, pdu->buf_len, 1, f_out);
1138 client->skip_newline = strchr(buf, '\n') == NULL;
1141 static void convert_agg(struct disk_util_agg *agg)
1145 for (i = 0; i < 2; i++) {
1146 agg->ios[i] = le64_to_cpu(agg->ios[i]);
1147 agg->merges[i] = le64_to_cpu(agg->merges[i]);
1148 agg->sectors[i] = le64_to_cpu(agg->sectors[i]);
1149 agg->ticks[i] = le64_to_cpu(agg->ticks[i]);
1152 agg->io_ticks = le64_to_cpu(agg->io_ticks);
1153 agg->time_in_queue = le64_to_cpu(agg->time_in_queue);
1154 agg->slavecount = le32_to_cpu(agg->slavecount);
1155 agg->max_util.u.f = fio_uint64_to_double(le64_to_cpu(agg->max_util.u.i));
1158 static void convert_dus(struct disk_util_stat *dus)
1162 for (i = 0; i < 2; i++) {
1163 dus->s.ios[i] = le64_to_cpu(dus->s.ios[i]);
1164 dus->s.merges[i] = le64_to_cpu(dus->s.merges[i]);
1165 dus->s.sectors[i] = le64_to_cpu(dus->s.sectors[i]);
1166 dus->s.ticks[i] = le64_to_cpu(dus->s.ticks[i]);
1169 dus->s.io_ticks = le64_to_cpu(dus->s.io_ticks);
1170 dus->s.time_in_queue = le64_to_cpu(dus->s.time_in_queue);
1171 dus->s.msec = le64_to_cpu(dus->s.msec);
1174 static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd)
1176 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
1178 if (!client->disk_stats_shown) {
1179 client->disk_stats_shown = true;
1180 log_info("\nDisk stats (read/write):\n");
1183 if (output_format & FIO_OUTPUT_JSON) {
1184 struct json_object *duobj;
1185 json_array_add_disk_util(&du->dus, &du->agg, du_array);
1186 duobj = json_array_last_value_object(du_array);
1187 json_object_add_client_info(duobj, client);
1189 if (output_format & FIO_OUTPUT_TERSE)
1190 print_disk_util(&du->dus, &du->agg, 1, NULL);
1191 if (output_format & FIO_OUTPUT_NORMAL)
1192 print_disk_util(&du->dus, &du->agg, 0, NULL);
1195 static void convert_jobs_eta(struct jobs_eta *je)
1199 je->nr_running = le32_to_cpu(je->nr_running);
1200 je->nr_ramp = le32_to_cpu(je->nr_ramp);
1201 je->nr_pending = le32_to_cpu(je->nr_pending);
1202 je->nr_setting_up = le32_to_cpu(je->nr_setting_up);
1203 je->files_open = le32_to_cpu(je->files_open);
1205 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1206 je->m_rate[i] = le64_to_cpu(je->m_rate[i]);
1207 je->t_rate[i] = le64_to_cpu(je->t_rate[i]);
1208 je->m_iops[i] = le32_to_cpu(je->m_iops[i]);
1209 je->t_iops[i] = le32_to_cpu(je->t_iops[i]);
1210 je->rate[i] = le64_to_cpu(je->rate[i]);
1211 je->iops[i] = le32_to_cpu(je->iops[i]);
1214 je->elapsed_sec = le64_to_cpu(je->elapsed_sec);
1215 je->eta_sec = le64_to_cpu(je->eta_sec);
1216 je->nr_threads = le32_to_cpu(je->nr_threads);
1217 je->is_pow2 = le32_to_cpu(je->is_pow2);
1218 je->unit_base = le32_to_cpu(je->unit_base);
1219 je->sig_figs = le32_to_cpu(je->sig_figs);
1222 void fio_client_sum_jobs_eta(struct jobs_eta *dst, struct jobs_eta *je)
1226 dst->nr_running += je->nr_running;
1227 dst->nr_ramp += je->nr_ramp;
1228 dst->nr_pending += je->nr_pending;
1229 dst->nr_setting_up += je->nr_setting_up;
1230 dst->files_open += je->files_open;
1232 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1233 dst->m_rate[i] += je->m_rate[i];
1234 dst->t_rate[i] += je->t_rate[i];
1235 dst->m_iops[i] += je->m_iops[i];
1236 dst->t_iops[i] += je->t_iops[i];
1237 dst->rate[i] += je->rate[i];
1238 dst->iops[i] += je->iops[i];
1241 dst->elapsed_sec += je->elapsed_sec;
1243 if (je->eta_sec > dst->eta_sec)
1244 dst->eta_sec = je->eta_sec;
1246 dst->nr_threads += je->nr_threads;
1249 * This wont be correct for multiple strings, but at least it
1250 * works for the basic cases.
1252 strcpy((char *) dst->run_str, (char *) je->run_str);
1255 static bool remove_reply_cmd(struct fio_client *client, struct fio_net_cmd *cmd)
1257 struct fio_net_cmd_reply *reply = NULL;
1258 struct flist_head *entry;
1260 flist_for_each(entry, &client->cmd_list) {
1261 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
1263 if (cmd->tag == (uintptr_t) reply)
1270 log_err("fio: client: unable to find matching tag (%llx)\n", (unsigned long long) cmd->tag);
1274 flist_del(&reply->list);
1275 cmd->tag = reply->saved_tag;
1280 int fio_client_wait_for_reply(struct fio_client *client, uint64_t tag)
1283 struct fio_net_cmd_reply *reply = NULL;
1284 struct flist_head *entry;
1286 flist_for_each(entry, &client->cmd_list) {
1287 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
1289 if (tag == (uintptr_t) reply)
1304 static void handle_eta(struct fio_client *client, struct fio_net_cmd *cmd)
1306 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
1307 struct client_eta *eta = (struct client_eta *) (uintptr_t) cmd->tag;
1309 dprint(FD_NET, "client: got eta tag %p, %d\n", eta, eta->pending);
1311 assert(client->eta_in_flight == eta);
1313 client->eta_in_flight = NULL;
1314 flist_del_init(&client->eta_list);
1315 client->eta_timeouts = 0;
1317 if (client->ops->jobs_eta)
1318 client->ops->jobs_eta(client, je);
1320 fio_client_sum_jobs_eta(&eta->eta, je);
1321 fio_client_dec_jobs_eta(eta, client->ops->eta);
1324 static void client_flush_hist_samples(FILE *f, int hist_coarseness, void *samples,
1325 uint64_t sample_size)
1327 struct io_sample *s;
1329 uint64_t i, j, nr_samples;
1330 struct io_u_plat_entry *entry;
1331 uint64_t *io_u_plat;
1333 int stride = 1 << hist_coarseness;
1338 s = __get_sample(samples, 0, 0);
1339 log_offset = (s->__ddir & LOG_OFFSET_SAMPLE_BIT) != 0;
1341 nr_samples = sample_size / __log_entry_sz(log_offset);
1343 for (i = 0; i < nr_samples; i++) {
1345 s = (struct io_sample *)((char *)__get_sample(samples, log_offset, i) +
1346 i * sizeof(struct io_u_plat_entry));
1348 entry = s->data.plat_entry;
1349 io_u_plat = entry->io_u_plat;
1351 fprintf(f, "%lu, %u, %u, ", (unsigned long) s->time,
1352 io_sample_ddir(s), s->bs);
1353 for (j = 0; j < FIO_IO_U_PLAT_NR - stride; j += stride) {
1354 fprintf(f, "%llu, ", (unsigned long long)hist_sum(j, stride, io_u_plat, NULL));
1356 fprintf(f, "%llu\n", (unsigned long long)
1357 hist_sum(FIO_IO_U_PLAT_NR - stride, stride, io_u_plat, NULL));
1362 static int fio_client_handle_iolog(struct fio_client *client,
1363 struct fio_net_cmd *cmd)
1365 struct cmd_iolog_pdu *pdu = NULL;
1367 char *log_pathname = NULL;
1370 pdu = convert_iolog(cmd, &store_direct);
1372 log_err("fio: failed converting IO log\n");
1377 /* allocate buffer big enough for next sprintf() call */
1378 log_pathname = malloc(10 + strlen((char *)pdu->name) +
1379 strlen(client->hostname));
1380 if (!log_pathname) {
1381 log_err("fio: memory allocation of unique pathname failed\n");
1385 /* generate a unique pathname for the log file using hostname */
1386 sprintf(log_pathname, "%s.%s", pdu->name, client->hostname);
1393 fd = open((const char *) log_pathname,
1394 O_WRONLY | O_CREAT | O_TRUNC, 0644);
1396 log_err("fio: open log %s: %s\n",
1397 log_pathname, strerror(errno));
1402 sz = cmd->pdu_len - sizeof(*pdu);
1403 wrote = write(fd, pdu->samples, sz);
1407 log_err("fio: short write on compressed log\n");
1415 f = fopen((const char *) log_pathname, "w");
1417 log_err("fio: fopen log %s : %s\n",
1418 log_pathname, strerror(errno));
1423 if (pdu->log_type == IO_LOG_TYPE_HIST) {
1424 client_flush_hist_samples(f, pdu->log_hist_coarseness, pdu->samples,
1425 pdu->nr_samples * sizeof(struct io_sample));
1427 flush_samples(f, pdu->samples,
1428 pdu->nr_samples * sizeof(struct io_sample));
1435 if (pdu && pdu != (void *) cmd->payload)
1444 static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd)
1446 struct cmd_probe_reply_pdu *probe = (struct cmd_probe_reply_pdu *) cmd->payload;
1447 const char *os, *arch;
1450 os = fio_get_os_string(probe->os);
1454 arch = fio_get_arch_string(probe->arch);
1458 sprintf(bit, "%d-bit", probe->bpp * 8);
1459 probe->flags = le64_to_cpu(probe->flags);
1461 log_info("hostname=%s, be=%u, %s, os=%s, arch=%s, fio=%s, flags=%lx\n",
1462 probe->hostname, probe->bigendian, bit, os, arch,
1463 probe->fio_version, (unsigned long) probe->flags);
1466 client->name = strdup((char *) probe->hostname);
1469 static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd)
1471 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
1473 client->state = Client_started;
1474 client->jobs = le32_to_cpu(pdu->jobs);
1475 client->nr_stat = le32_to_cpu(pdu->stat_outputs);
1480 if (client->opt_lists)
1481 free(client->opt_lists);
1483 client->opt_lists = malloc(client->jobs * sizeof(struct flist_head));
1484 for (i = 0; i < client->jobs; i++)
1485 INIT_FLIST_HEAD(&client->opt_lists[i]);
1488 sum_stat_clients += client->nr_stat;
1491 static void handle_stop(struct fio_client *client)
1494 log_info("client <%s>: exited with error %d\n", client->hostname, client->error);
1497 static void convert_stop(struct fio_net_cmd *cmd)
1499 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
1501 pdu->error = le32_to_cpu(pdu->error);
1504 static void convert_text(struct fio_net_cmd *cmd)
1506 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
1508 pdu->level = le32_to_cpu(pdu->level);
1509 pdu->buf_len = le32_to_cpu(pdu->buf_len);
1510 pdu->log_sec = le64_to_cpu(pdu->log_sec);
1511 pdu->log_usec = le64_to_cpu(pdu->log_usec);
1514 static struct cmd_iolog_pdu *convert_iolog_gz(struct fio_net_cmd *cmd,
1515 struct cmd_iolog_pdu *pdu)
1518 struct cmd_iolog_pdu *ret;
1520 uint32_t nr_samples;
1524 stream.zalloc = Z_NULL;
1525 stream.zfree = Z_NULL;
1526 stream.opaque = Z_NULL;
1527 stream.avail_in = 0;
1528 stream.next_in = Z_NULL;
1530 if (inflateInit(&stream) != Z_OK)
1534 * Get header first, it's not compressed
1536 nr_samples = le64_to_cpu(pdu->nr_samples);
1538 if (pdu->log_type == IO_LOG_TYPE_HIST)
1539 total = nr_samples * (__log_entry_sz(le32_to_cpu(pdu->log_offset)) +
1540 sizeof(struct io_u_plat_entry));
1542 total = nr_samples * __log_entry_sz(le32_to_cpu(pdu->log_offset));
1543 ret = malloc(total + sizeof(*pdu));
1544 ret->nr_samples = nr_samples;
1546 memcpy(ret, pdu, sizeof(*pdu));
1548 p = (char *) ret + sizeof(*pdu);
1550 stream.avail_in = cmd->pdu_len - sizeof(*pdu);
1551 stream.next_in = (void *)((char *) pdu + sizeof(*pdu));
1552 while (stream.avail_in) {
1553 unsigned int this_chunk = 65536;
1554 unsigned int this_len;
1557 if (this_chunk > total)
1560 stream.avail_out = this_chunk;
1561 stream.next_out = (void *)p;
1562 err = inflate(&stream, Z_NO_FLUSH);
1563 /* may be Z_OK, or Z_STREAM_END */
1565 log_err("fio: inflate error %d\n", err);
1571 this_len = this_chunk - stream.avail_out;
1577 inflateEnd(&stream);
1585 * This has been compressed on the server side, since it can be big.
1588 static struct cmd_iolog_pdu *convert_iolog(struct fio_net_cmd *cmd,
1591 struct cmd_iolog_pdu *pdu = (struct cmd_iolog_pdu *) cmd->payload;
1592 struct cmd_iolog_pdu *ret;
1597 *store_direct = false;
1600 * Convert if compressed and we support it. If it's not
1601 * compressed, we need not do anything.
1603 compressed = le32_to_cpu(pdu->compressed);
1604 if (compressed == XMIT_COMPRESSED) {
1606 log_err("fio: server sent compressed data by mistake\n");
1609 ret = convert_iolog_gz(cmd, pdu);
1611 log_err("fio: failed decompressing log\n");
1614 } else if (compressed == STORE_COMPRESSED) {
1615 *store_direct = true;
1620 ret->nr_samples = le64_to_cpu(ret->nr_samples);
1621 ret->thread_number = le32_to_cpu(ret->thread_number);
1622 ret->log_type = le32_to_cpu(ret->log_type);
1623 ret->compressed = le32_to_cpu(ret->compressed);
1624 ret->log_offset = le32_to_cpu(ret->log_offset);
1625 ret->log_hist_coarseness = le32_to_cpu(ret->log_hist_coarseness);
1630 samples = &ret->samples[0];
1631 for (i = 0; i < ret->nr_samples; i++) {
1632 struct io_sample *s;
1634 s = __get_sample(samples, ret->log_offset, i);
1635 if (ret->log_type == IO_LOG_TYPE_HIST)
1636 s = (struct io_sample *)((char *)s + sizeof(struct io_u_plat_entry) * i);
1638 s->time = le64_to_cpu(s->time);
1639 s->data.val = le64_to_cpu(s->data.val);
1640 s->__ddir = le32_to_cpu(s->__ddir);
1641 s->bs = le32_to_cpu(s->bs);
1643 if (ret->log_offset) {
1644 struct io_sample_offset *so = (void *) s;
1646 so->offset = le64_to_cpu(so->offset);
1649 if (ret->log_type == IO_LOG_TYPE_HIST) {
1650 s->data.plat_entry = (struct io_u_plat_entry *)(((char *)s) + sizeof(*s));
1651 s->data.plat_entry->list.next = NULL;
1652 s->data.plat_entry->list.prev = NULL;
1659 static void sendfile_reply(int fd, struct cmd_sendfile_reply *rep,
1660 size_t size, uint64_t tag)
1662 rep->error = cpu_to_le32(rep->error);
1663 fio_net_send_cmd(fd, FIO_NET_CMD_SENDFILE, rep, size, &tag, NULL);
1666 static int fio_send_file(struct fio_client *client, struct cmd_sendfile *pdu,
1669 struct cmd_sendfile_reply *rep;
1674 size = sizeof(*rep);
1677 if (stat((char *)pdu->path, &sb) < 0) {
1680 sendfile_reply(client->fd, rep, size, tag);
1686 rep = realloc(rep, size);
1687 rep->size = cpu_to_le32((uint32_t) sb.st_size);
1689 fd = open((char *)pdu->path, O_RDONLY);
1693 rep->error = read_data(fd, &rep->data, sb.st_size);
1694 sendfile_reply(client->fd, rep, size, tag);
1700 int fio_handle_client(struct fio_client *client)
1702 struct client_ops *ops = client->ops;
1703 struct fio_net_cmd *cmd;
1706 dprint(FD_NET, "client: handle %s\n", client->hostname);
1708 cmd = fio_net_recv_cmd(client->fd, true);
1712 dprint(FD_NET, "client: got cmd op %s from %s (pdu=%u)\n",
1713 fio_server_op(cmd->opcode), client->hostname, cmd->pdu_len);
1715 client->last_cmd = cmd->opcode;
1717 switch (cmd->opcode) {
1718 case FIO_NET_CMD_QUIT:
1720 ops->quit(client, cmd);
1721 remove_client(client);
1723 case FIO_NET_CMD_TEXT:
1725 ops->text(client, cmd);
1727 case FIO_NET_CMD_DU: {
1728 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
1730 convert_dus(&du->dus);
1731 convert_agg(&du->agg);
1733 ops->disk_util(client, cmd);
1736 case FIO_NET_CMD_TS: {
1737 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
1739 dprint(FD_NET, "client: ts->ss_state = %u\n", (unsigned int) le32_to_cpu(p->ts.ss_state));
1740 if (le32_to_cpu(p->ts.ss_state) & FIO_SS_DATA) {
1741 dprint(FD_NET, "client: received steadystate ring buffers\n");
1743 size = le64_to_cpu(p->ts.ss_dur);
1744 p->ts.ss_iops_data = (uint64_t *) ((struct cmd_ts_pdu *)cmd->payload + 1);
1745 p->ts.ss_bw_data = p->ts.ss_iops_data + size;
1748 convert_ts(&p->ts, &p->ts);
1749 convert_gs(&p->rs, &p->rs);
1751 ops->thread_status(client, cmd);
1754 case FIO_NET_CMD_GS: {
1755 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
1759 ops->group_stats(client, cmd);
1762 case FIO_NET_CMD_ETA: {
1763 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
1765 if (!remove_reply_cmd(client, cmd))
1767 convert_jobs_eta(je);
1768 handle_eta(client, cmd);
1771 case FIO_NET_CMD_PROBE:
1772 remove_reply_cmd(client, cmd);
1773 ops->probe(client, cmd);
1775 case FIO_NET_CMD_SERVER_START:
1776 client->state = Client_running;
1778 ops->job_start(client, cmd);
1780 case FIO_NET_CMD_START: {
1781 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
1783 pdu->jobs = le32_to_cpu(pdu->jobs);
1784 ops->start(client, cmd);
1787 case FIO_NET_CMD_STOP: {
1788 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
1791 client->state = Client_stopped;
1792 client->error = le32_to_cpu(pdu->error);
1793 client->signal = le32_to_cpu(pdu->signal);
1797 case FIO_NET_CMD_ADD_JOB: {
1798 struct cmd_add_job_pdu *pdu = (struct cmd_add_job_pdu *) cmd->payload;
1800 client->thread_number = le32_to_cpu(pdu->thread_number);
1801 client->groupid = le32_to_cpu(pdu->groupid);
1804 ops->add_job(client, cmd);
1807 case FIO_NET_CMD_IOLOG:
1808 fio_client_handle_iolog(client, cmd);
1810 case FIO_NET_CMD_UPDATE_JOB:
1811 ops->update_job(client, cmd);
1812 remove_reply_cmd(client, cmd);
1814 case FIO_NET_CMD_VTRIGGER: {
1815 struct all_io_list *pdu = (struct all_io_list *) cmd->payload;
1820 strcpy(buf, aux_path);
1824 __verify_save_state(pdu, server_name(client, &buf[off], sizeof(buf) - off));
1825 exec_trigger(trigger_cmd);
1828 case FIO_NET_CMD_SENDFILE: {
1829 struct cmd_sendfile *pdu = (struct cmd_sendfile *) cmd->payload;
1830 fio_send_file(client, pdu, cmd->tag);
1833 case FIO_NET_CMD_JOB_OPT: {
1834 handle_job_opt(client, cmd);
1838 log_err("fio: unknown client op: %s\n", fio_server_op(cmd->opcode));
1846 int fio_clients_send_trigger(const char *cmd)
1848 struct flist_head *entry;
1849 struct fio_client *client;
1852 dprint(FD_NET, "client: send vtrigger: %s\n", cmd);
1859 flist_for_each(entry, &client_list) {
1860 struct cmd_vtrigger_pdu *pdu;
1862 client = flist_entry(entry, struct fio_client, list);
1864 pdu = malloc(sizeof(*pdu) + slen);
1865 pdu->len = cpu_to_le16((uint16_t) slen);
1867 memcpy(pdu->cmd, cmd, slen);
1868 fio_net_send_cmd(client->fd, FIO_NET_CMD_VTRIGGER, pdu,
1869 sizeof(*pdu) + slen, NULL, NULL);
1876 static void request_client_etas(struct client_ops *ops)
1878 struct fio_client *client;
1879 struct flist_head *entry;
1880 struct client_eta *eta;
1883 if (eta_print == FIO_ETA_NEVER)
1886 dprint(FD_NET, "client: request eta (%d)\n", nr_clients);
1888 eta = calloc(1, sizeof(*eta) + __THREAD_RUNSTR_SZ(REAL_MAX_JOBS));
1889 eta->pending = nr_clients;
1891 flist_for_each(entry, &client_list) {
1892 client = flist_entry(entry, struct fio_client, list);
1894 if (!flist_empty(&client->eta_list)) {
1898 if (client->state != Client_running)
1901 assert(!client->eta_in_flight);
1902 flist_add_tail(&client->eta_list, &eta_list);
1903 client->eta_in_flight = eta;
1904 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_SEND_ETA,
1905 (uintptr_t) eta, &client->cmd_list);
1909 if (!fio_client_dec_jobs_eta(eta, ops->eta))
1913 dprint(FD_NET, "client: requested eta tag %p\n", eta);
1917 * A single SEND_ETA timeout isn't fatal. Attempt to recover.
1919 static int handle_cmd_timeout(struct fio_client *client,
1920 struct fio_net_cmd_reply *reply)
1922 uint16_t reply_opcode = reply->opcode;
1924 flist_del(&reply->list);
1927 if (reply_opcode != FIO_NET_CMD_SEND_ETA)
1930 log_info("client <%s>: timeout on SEND_ETA\n", client->hostname);
1932 flist_del_init(&client->eta_list);
1933 if (client->eta_in_flight) {
1934 fio_client_dec_jobs_eta(client->eta_in_flight, client->ops->eta);
1935 client->eta_in_flight = NULL;
1939 * If we fail 5 in a row, give up...
1941 if (client->eta_timeouts++ > 5)
1947 static int client_check_cmd_timeout(struct fio_client *client,
1948 struct timespec *now)
1950 struct fio_net_cmd_reply *reply;
1951 struct flist_head *entry, *tmp;
1954 flist_for_each_safe(entry, tmp, &client->cmd_list) {
1957 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
1959 if (mtime_since(&reply->ts, now) < FIO_NET_CLIENT_TIMEOUT)
1963 if (!handle_cmd_timeout(client, reply))
1966 log_err("fio: client %s, timeout on cmd %s\n", client->hostname,
1971 return flist_empty(&client->cmd_list) && ret;
1974 static int fio_check_clients_timed_out(void)
1976 struct fio_client *client;
1977 struct flist_head *entry, *tmp;
1981 fio_gettime(&ts, NULL);
1983 flist_for_each_safe(entry, tmp, &client_list) {
1984 client = flist_entry(entry, struct fio_client, list);
1986 if (flist_empty(&client->cmd_list))
1989 if (!client_check_cmd_timeout(client, &ts))
1992 if (client->ops->timed_out)
1993 client->ops->timed_out(client);
1995 log_err("fio: client %s timed out\n", client->hostname);
1997 if (client->last_cmd != FIO_NET_CMD_VTRIGGER)
1998 client->error = ETIMEDOUT;
2000 log_info("fio: ignoring timeout due to vtrigger\n");
2001 remove_client(client);
2008 int fio_handle_clients(struct client_ops *ops)
2010 struct pollfd *pfds;
2011 int i, ret = 0, retval = 0;
2013 fio_gettime(&eta_ts, NULL);
2015 pfds = malloc(nr_clients * sizeof(struct pollfd));
2017 init_thread_stat(&client_ts);
2018 init_group_run_stat(&client_gs);
2020 while (!exit_backend && nr_clients) {
2021 struct flist_head *entry, *tmp;
2022 struct fio_client *client;
2025 flist_for_each_safe(entry, tmp, &client_list) {
2026 client = flist_entry(entry, struct fio_client, list);
2028 if (!client->sent_job && !client->ops->stay_connected &&
2029 flist_empty(&client->cmd_list)) {
2030 remove_client(client);
2034 pfds[i].fd = client->fd;
2035 pfds[i].events = POLLIN;
2042 assert(i == nr_clients);
2048 fio_gettime(&ts, NULL);
2049 if (eta_time_within_slack(mtime_since(&eta_ts, &ts))) {
2050 request_client_etas(ops);
2051 memcpy(&eta_ts, &ts, sizeof(ts));
2053 if (fio_check_clients_timed_out())
2057 check_trigger_file();
2059 timeout = min(100u, ops->eta_msec);
2061 ret = poll(pfds, nr_clients, timeout);
2065 log_err("fio: poll clients: %s\n", strerror(errno));
2071 for (i = 0; i < nr_clients; i++) {
2072 if (!(pfds[i].revents & POLLIN))
2075 client = find_client_by_fd(pfds[i].fd);
2077 log_err("fio: unknown client fd %ld\n", (long) pfds[i].fd);
2080 if (!fio_handle_client(client)) {
2081 log_info("client: host=%s disconnected\n",
2083 remove_client(client);
2085 } else if (client->error)
2087 fio_put_client(client);
2091 fio_client_json_fini();
2094 return retval || error_clients;