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);
35 static void client_display_thread_status(struct jobs_eta *je);
37 struct client_ops fio_client_ops = {
39 .disk_util = handle_du,
40 .thread_status = handle_ts,
41 .group_stats = handle_gs,
43 .start = handle_start,
44 .eta = client_display_thread_status,
45 .probe = handle_probe,
46 .eta_msec = FIO_CLIENT_DEF_ETA_MSEC,
47 .client_type = FIO_CLIENT_TYPE_CLI,
50 static struct timespec eta_ts;
52 static FLIST_HEAD(client_list);
53 static FLIST_HEAD(eta_list);
55 static FLIST_HEAD(arg_list);
57 struct thread_stat client_ts;
58 struct group_run_stats client_gs;
61 static int sum_stat_nr;
62 static struct json_object *root = NULL;
63 static struct json_object *job_opt_object = NULL;
64 static struct json_array *clients_array = NULL;
65 static struct json_array *du_array = NULL;
67 static int error_clients;
69 #define FIO_CLIENT_HASH_BITS 7
70 #define FIO_CLIENT_HASH_SZ (1 << FIO_CLIENT_HASH_BITS)
71 #define FIO_CLIENT_HASH_MASK (FIO_CLIENT_HASH_SZ - 1)
72 static struct flist_head client_hash[FIO_CLIENT_HASH_SZ];
74 static struct cmd_iolog_pdu *convert_iolog(struct fio_net_cmd *, bool *);
76 static void fio_client_add_hash(struct fio_client *client)
78 int bucket = hash_long(client->fd, FIO_CLIENT_HASH_BITS);
80 bucket &= FIO_CLIENT_HASH_MASK;
81 flist_add(&client->hash_list, &client_hash[bucket]);
84 static void fio_client_remove_hash(struct fio_client *client)
86 if (!flist_empty(&client->hash_list))
87 flist_del_init(&client->hash_list);
90 static void fio_init fio_client_hash_init(void)
94 for (i = 0; i < FIO_CLIENT_HASH_SZ; i++)
95 INIT_FLIST_HEAD(&client_hash[i]);
98 static int read_data(int fd, void *data, size_t size)
103 ret = read(fd, data, size);
105 if (errno == EAGAIN || errno == EINTR)
122 static int read_ini_data(int fd, void *data, size_t size)
133 fp = fdopen(dupfd, "r");
142 char buf[OPT_LEN_MAX+1], *sub;
144 if (!fgets(buf, sizeof(buf), fp)) {
146 if (errno == EAGAIN || errno == EINTR)
153 sub = fio_option_dup_subs(buf);
155 if (len + 1 > size) {
156 log_err("fio: no space left to read data\n");
174 static void fio_client_json_init(void)
179 if (!(output_format & FIO_OUTPUT_JSON))
183 os_ctime_r((const time_t *) &time_p, time_buf, sizeof(time_buf));
184 time_buf[strlen(time_buf) - 1] = '\0';
186 root = json_create_object();
187 json_object_add_value_string(root, "fio version", fio_version_string);
188 json_object_add_value_int(root, "timestamp", time_p);
189 json_object_add_value_string(root, "time", time_buf);
191 job_opt_object = json_create_object();
192 json_object_add_value_object(root, "global options", job_opt_object);
193 clients_array = json_create_array();
194 json_object_add_value_array(root, "client_stats", clients_array);
195 du_array = json_create_array();
196 json_object_add_value_array(root, "disk_util", du_array);
199 static void fio_client_json_fini(void)
201 struct buf_output out;
206 buf_output_init(&out);
208 __log_buf(&out, "\n");
209 json_print_object(root, &out);
210 __log_buf(&out, "\n");
211 log_info_buf(out.buf, out.buflen);
213 buf_output_free(&out);
215 json_free_object(root);
217 job_opt_object = NULL;
218 clients_array = NULL;
222 static struct fio_client *find_client_by_fd(int fd)
224 int bucket = hash_long(fd, FIO_CLIENT_HASH_BITS) & FIO_CLIENT_HASH_MASK;
225 struct fio_client *client;
226 struct flist_head *entry;
228 flist_for_each(entry, &client_hash[bucket]) {
229 client = flist_entry(entry, struct fio_client, hash_list);
231 if (client->fd == fd) {
240 void fio_put_client(struct fio_client *client)
245 log_info_buf(client->buf.buf, client->buf.buflen);
246 buf_output_free(&client->buf);
248 free(client->hostname);
253 while (client->nr_files) {
254 struct client_file *cf = &client->files[--client->nr_files];
260 if (client->opt_lists)
261 free(client->opt_lists);
263 if (!client->did_stat)
272 static int fio_client_dec_jobs_eta(struct client_eta *eta, client_eta_op eta_fn)
274 if (!--eta->pending) {
283 static void fio_drain_client_text(struct fio_client *client)
286 struct fio_net_cmd *cmd;
288 cmd = fio_net_recv_cmd(client->fd, false);
292 if (cmd->opcode == FIO_NET_CMD_TEXT) {
294 client->ops->text(client, cmd);
301 static void remove_client(struct fio_client *client)
303 assert(client->refs);
305 dprint(FD_NET, "client: removed <%s>\n", client->hostname);
307 fio_drain_client_text(client);
309 if (!flist_empty(&client->list))
310 flist_del_init(&client->list);
312 fio_client_remove_hash(client);
314 if (!flist_empty(&client->eta_list)) {
315 flist_del_init(&client->eta_list);
316 fio_client_dec_jobs_eta(client->eta_in_flight, client->ops->eta);
322 if (client->ops->removed)
323 client->ops->removed(client);
326 fio_put_client(client);
329 struct fio_client *fio_get_client(struct fio_client *client)
335 static void __fio_client_add_cmd_option(struct fio_client *client,
340 index = client->argc++;
341 client->argv = realloc(client->argv, sizeof(char *) * client->argc);
342 client->argv[index] = strdup(opt);
343 dprint(FD_NET, "client: add cmd %d: %s\n", index, opt);
346 void fio_client_add_cmd_option(void *cookie, const char *opt)
348 struct fio_client *client = cookie;
349 struct flist_head *entry;
354 __fio_client_add_cmd_option(client, opt);
357 * Duplicate arguments to shared client group
359 flist_for_each(entry, &arg_list) {
360 client = flist_entry(entry, struct fio_client, arg_list);
362 __fio_client_add_cmd_option(client, opt);
366 static struct fio_client *get_new_client(void)
368 struct fio_client *client;
370 client = malloc(sizeof(*client));
371 memset(client, 0, sizeof(*client));
373 INIT_FLIST_HEAD(&client->list);
374 INIT_FLIST_HEAD(&client->hash_list);
375 INIT_FLIST_HEAD(&client->arg_list);
376 INIT_FLIST_HEAD(&client->eta_list);
377 INIT_FLIST_HEAD(&client->cmd_list);
379 buf_output_init(&client->buf);
384 struct fio_client *fio_client_add_explicit(struct client_ops *ops,
385 const char *hostname, int type,
388 struct fio_client *client;
390 client = get_new_client();
392 client->hostname = strdup(hostname);
394 if (type == Fio_client_socket)
395 client->is_sock = true;
399 ipv6 = type == Fio_client_ipv6;
400 if (fio_server_parse_host(hostname, ipv6,
401 &client->addr.sin_addr,
402 &client->addr6.sin6_addr))
411 client->type = ops->client_type;
413 __fio_client_add_cmd_option(client, "fio");
415 flist_add(&client->list, &client_list);
417 dprint(FD_NET, "client: added <%s>\n", client->hostname);
424 int fio_client_add_ini_file(void *cookie, const char *ini_file, bool remote)
426 struct fio_client *client = cookie;
427 struct client_file *cf;
434 dprint(FD_NET, "client <%s>: add ini %s\n", client->hostname, ini_file);
436 new_size = (client->nr_files + 1) * sizeof(struct client_file);
437 new_files = realloc(client->files, new_size);
441 client->files = new_files;
442 cf = &client->files[client->nr_files];
443 cf->file = strdup(ini_file);
449 int fio_client_add(struct client_ops *ops, const char *hostname, void **cookie)
451 struct fio_client *existing = *cookie;
452 struct fio_client *client;
456 * We always add our "exec" name as the option, hence 1
459 if (existing->argc == 1)
460 flist_add_tail(&existing->arg_list, &arg_list);
462 while (!flist_empty(&arg_list))
463 flist_del_init(arg_list.next);
467 client = get_new_client();
469 if (fio_server_parse_string(hostname, &client->hostname,
470 &client->is_sock, &client->port,
471 &client->addr.sin_addr,
472 &client->addr6.sin6_addr,
479 client->type = ops->client_type;
481 __fio_client_add_cmd_option(client, "fio");
483 flist_add(&client->list, &client_list);
485 dprint(FD_NET, "client: added <%s>\n", client->hostname);
490 static const char *server_name(struct fio_client *client, char *buf,
496 from = inet_ntop(AF_INET6, (struct sockaddr *) &client->addr6.sin6_addr, buf, bufsize);
497 else if (client->is_sock)
500 from = inet_ntop(AF_INET, (struct sockaddr *) &client->addr.sin_addr, buf, bufsize);
505 static void probe_client(struct fio_client *client)
507 struct cmd_client_probe_pdu pdu;
512 dprint(FD_NET, "client: send probe\n");
515 pdu.flags = __le64_to_cpu(FIO_PROBE_FLAG_ZLIB);
520 sname = server_name(client, buf, sizeof(buf));
521 memset(pdu.server, 0, sizeof(pdu.server));
522 strncpy((char *) pdu.server, sname, sizeof(pdu.server) - 1);
524 fio_net_send_cmd(client->fd, FIO_NET_CMD_PROBE, &pdu, sizeof(pdu), &tag, &client->cmd_list);
527 static int fio_client_connect_ip(struct fio_client *client)
529 struct sockaddr *addr;
534 client->addr6.sin6_family = AF_INET6;
535 client->addr6.sin6_port = htons(client->port);
537 addr = (struct sockaddr *) &client->addr6;
538 socklen = sizeof(client->addr6);
540 client->addr.sin_family = AF_INET;
541 client->addr.sin_port = htons(client->port);
543 addr = (struct sockaddr *) &client->addr;
544 socklen = sizeof(client->addr);
547 fd = socket(domain, SOCK_STREAM, 0);
551 log_err("fio: socket: %s\n", strerror(errno));
555 if (connect(fd, addr, socklen) < 0) {
558 log_err("fio: connect: %s\n", strerror(errno));
559 log_err("fio: failed to connect to %s:%u\n", client->hostname,
568 static int fio_client_connect_sock(struct fio_client *client)
570 struct sockaddr_un *addr = &client->addr_un;
574 memset(addr, 0, sizeof(*addr));
575 addr->sun_family = AF_UNIX;
576 strncpy(addr->sun_path, client->hostname, sizeof(addr->sun_path) - 1);
578 fd = socket(AF_UNIX, SOCK_STREAM, 0);
582 log_err("fio: socket: %s\n", strerror(errno));
586 len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
587 if (connect(fd, (struct sockaddr *) addr, len) < 0) {
590 log_err("fio: connect; %s\n", strerror(errno));
598 int fio_client_connect(struct fio_client *client)
602 dprint(FD_NET, "client: connect to host %s\n", client->hostname);
605 fd = fio_client_connect_sock(client);
607 fd = fio_client_connect_ip(client);
609 dprint(FD_NET, "client: %s connected %d\n", client->hostname, fd);
615 fio_client_add_hash(client);
616 client->state = Client_connected;
618 probe_client(client);
622 int fio_client_terminate(struct fio_client *client)
624 return fio_net_send_quit(client->fd);
627 static void fio_clients_terminate(void)
629 struct flist_head *entry;
630 struct fio_client *client;
632 dprint(FD_NET, "client: terminate clients\n");
634 flist_for_each(entry, &client_list) {
635 client = flist_entry(entry, struct fio_client, list);
636 fio_client_terminate(client);
640 static void sig_int(int sig)
642 dprint(FD_NET, "client: got signal %d\n", sig);
643 fio_clients_terminate();
646 static void client_signal_handler(void)
648 struct sigaction act;
650 memset(&act, 0, sizeof(act));
651 act.sa_handler = sig_int;
652 act.sa_flags = SA_RESTART;
653 sigaction(SIGINT, &act, NULL);
655 memset(&act, 0, sizeof(act));
656 act.sa_handler = sig_int;
657 act.sa_flags = SA_RESTART;
658 sigaction(SIGTERM, &act, NULL);
660 /* Windows uses SIGBREAK as a quit signal from other applications */
662 memset(&act, 0, sizeof(act));
663 act.sa_handler = sig_int;
664 act.sa_flags = SA_RESTART;
665 sigaction(SIGBREAK, &act, NULL);
668 memset(&act, 0, sizeof(act));
669 act.sa_handler = sig_show_status;
670 act.sa_flags = SA_RESTART;
671 sigaction(SIGUSR1, &act, NULL);
674 static int send_client_cmd_line(struct fio_client *client)
676 struct cmd_single_line_pdu *cslp;
677 struct cmd_line_pdu *clp;
678 unsigned long offset;
684 dprint(FD_NET, "client: send cmdline %d\n", client->argc);
686 lens = malloc(client->argc * sizeof(unsigned int));
689 * Find out how much mem we need
691 for (i = 0, mem = 0; i < client->argc; i++) {
692 lens[i] = strlen(client->argv[i]) + 1;
697 * We need one cmd_line_pdu, and argc number of cmd_single_line_pdu
699 mem += sizeof(*clp) + (client->argc * sizeof(*cslp));
703 offset = sizeof(*clp);
705 for (i = 0; i < client->argc; i++) {
706 uint16_t arg_len = lens[i];
709 strcpy((char *) cslp->text, client->argv[i]);
710 cslp->len = cpu_to_le16(arg_len);
711 offset += sizeof(*cslp) + arg_len;
715 clp->lines = cpu_to_le16(client->argc);
716 clp->client_type = __cpu_to_le16(client->type);
717 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOBLINE, pdu, mem, NULL, NULL);
722 int fio_clients_connect(void)
724 struct fio_client *client;
725 struct flist_head *entry, *tmp;
730 WSAStartup(MAKEWORD(2, 2), &wsd);
733 dprint(FD_NET, "client: connect all\n");
735 client_signal_handler();
737 flist_for_each_safe(entry, tmp, &client_list) {
738 client = flist_entry(entry, struct fio_client, list);
740 ret = fio_client_connect(client);
742 remove_client(client);
746 if (client->argc > 1)
747 send_client_cmd_line(client);
753 int fio_start_client(struct fio_client *client)
755 dprint(FD_NET, "client: start %s\n", client->hostname);
756 return fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_RUN, 0, NULL);
759 int fio_start_all_clients(void)
761 struct fio_client *client;
762 struct flist_head *entry, *tmp;
765 dprint(FD_NET, "client: start all\n");
767 fio_client_json_init();
769 flist_for_each_safe(entry, tmp, &client_list) {
770 client = flist_entry(entry, struct fio_client, list);
772 ret = fio_start_client(client);
774 remove_client(client);
779 return flist_empty(&client_list);
782 static int __fio_client_send_remote_ini(struct fio_client *client,
783 const char *filename)
785 struct cmd_load_file_pdu *pdu;
789 dprint(FD_NET, "send remote ini %s to %s\n", filename, client->hostname);
791 p_size = sizeof(*pdu) + strlen(filename) + 1;
792 pdu = malloc(p_size);
793 memset(pdu, 0, p_size);
794 pdu->name_len = strlen(filename);
795 strcpy((char *) pdu->file, filename);
796 pdu->client_type = cpu_to_le16((uint16_t) client->type);
798 client->sent_job = true;
799 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_LOAD_FILE, pdu, p_size,NULL, NULL);
805 * Send file contents to server backend. We could use sendfile(), but to remain
806 * more portable lets just read/write the darn thing.
808 static int __fio_client_send_local_ini(struct fio_client *client,
809 const char *filename)
811 struct cmd_job_pdu *pdu;
819 dprint(FD_NET, "send ini %s to %s\n", filename, client->hostname);
821 fd = open(filename, O_RDONLY);
824 log_err("fio: job file <%s> open: %s\n", filename, strerror(errno));
828 if (fstat(fd, &sb) < 0) {
830 log_err("fio: job file stat: %s\n", strerror(errno));
836 * Add extra space for variable expansion, but doesn't guarantee.
838 sb.st_size += OPT_LEN_MAX;
839 p_size = sb.st_size + sizeof(*pdu);
840 pdu = malloc(p_size);
845 if (read_ini_data(fd, p, len)) {
846 log_err("fio: failed reading job file %s\n", filename);
852 pdu->buf_len = __cpu_to_le32(sb.st_size);
853 pdu->client_type = cpu_to_le32(client->type);
855 client->sent_job = true;
856 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, pdu, p_size, NULL, NULL);
862 int fio_client_send_ini(struct fio_client *client, const char *filename,
868 ret = __fio_client_send_local_ini(client, filename);
870 ret = __fio_client_send_remote_ini(client, filename);
873 client->sent_job = true;
878 static int fio_client_send_cf(struct fio_client *client,
879 struct client_file *cf)
881 return fio_client_send_ini(client, cf->file, cf->remote);
884 int fio_clients_send_ini(const char *filename)
886 struct fio_client *client;
887 struct flist_head *entry, *tmp;
889 flist_for_each_safe(entry, tmp, &client_list) {
892 client = flist_entry(entry, struct fio_client, list);
894 if (client->nr_files) {
897 for (i = 0; i < client->nr_files; i++) {
898 struct client_file *cf;
900 cf = &client->files[i];
902 if (fio_client_send_cf(client, cf)) {
904 remove_client(client);
909 if (client->sent_job || failed)
911 if (!filename || fio_client_send_ini(client, filename, 0))
912 remove_client(client);
918 int fio_client_update_options(struct fio_client *client,
919 struct thread_options *o, uint64_t *tag)
921 struct cmd_add_job_pdu pdu;
923 pdu.thread_number = cpu_to_le32(client->thread_number);
924 pdu.groupid = cpu_to_le32(client->groupid);
925 convert_thread_options_to_net(&pdu.top, o);
927 return fio_net_send_cmd(client->fd, FIO_NET_CMD_UPDATE_JOB, &pdu, sizeof(pdu), tag, &client->cmd_list);
930 static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
932 dst->max_val = le64_to_cpu(src->max_val);
933 dst->min_val = le64_to_cpu(src->min_val);
934 dst->samples = le64_to_cpu(src->samples);
937 * Floats arrive as IEEE 754 encoded uint64_t, convert back to double
939 dst->mean.u.f = fio_uint64_to_double(le64_to_cpu(dst->mean.u.i));
940 dst->S.u.f = fio_uint64_to_double(le64_to_cpu(dst->S.u.i));
943 static void convert_ts(struct thread_stat *dst, struct thread_stat *src)
947 dst->error = le32_to_cpu(src->error);
948 dst->thread_number = le32_to_cpu(src->thread_number);
949 dst->groupid = le32_to_cpu(src->groupid);
950 dst->pid = le32_to_cpu(src->pid);
951 dst->members = le32_to_cpu(src->members);
952 dst->unified_rw_rep = le32_to_cpu(src->unified_rw_rep);
954 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
955 convert_io_stat(&dst->clat_stat[i], &src->clat_stat[i]);
956 convert_io_stat(&dst->slat_stat[i], &src->slat_stat[i]);
957 convert_io_stat(&dst->lat_stat[i], &src->lat_stat[i]);
958 convert_io_stat(&dst->bw_stat[i], &src->bw_stat[i]);
959 convert_io_stat(&dst->iops_stat[i], &src->iops_stat[i]);
962 dst->usr_time = le64_to_cpu(src->usr_time);
963 dst->sys_time = le64_to_cpu(src->sys_time);
964 dst->ctx = le64_to_cpu(src->ctx);
965 dst->minf = le64_to_cpu(src->minf);
966 dst->majf = le64_to_cpu(src->majf);
967 dst->clat_percentiles = le32_to_cpu(src->clat_percentiles);
968 dst->lat_percentiles = le32_to_cpu(src->lat_percentiles);
969 dst->percentile_precision = le64_to_cpu(src->percentile_precision);
971 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
972 fio_fp64_t *fps = &src->percentile_list[i];
973 fio_fp64_t *fpd = &dst->percentile_list[i];
975 fpd->u.f = fio_uint64_to_double(le64_to_cpu(fps->u.i));
978 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
979 dst->io_u_map[i] = le64_to_cpu(src->io_u_map[i]);
980 dst->io_u_submit[i] = le64_to_cpu(src->io_u_submit[i]);
981 dst->io_u_complete[i] = le64_to_cpu(src->io_u_complete[i]);
984 for (i = 0; i < FIO_IO_U_LAT_N_NR; i++)
985 dst->io_u_lat_n[i] = le64_to_cpu(src->io_u_lat_n[i]);
986 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++)
987 dst->io_u_lat_u[i] = le64_to_cpu(src->io_u_lat_u[i]);
988 for (i = 0; i < FIO_IO_U_LAT_M_NR; i++)
989 dst->io_u_lat_m[i] = le64_to_cpu(src->io_u_lat_m[i]);
991 for (i = 0; i < DDIR_RWDIR_CNT; i++)
992 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
993 dst->io_u_plat[i][j] = le64_to_cpu(src->io_u_plat[i][j]);
995 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
996 dst->total_io_u[i] = le64_to_cpu(src->total_io_u[i]);
997 dst->short_io_u[i] = le64_to_cpu(src->short_io_u[i]);
998 dst->drop_io_u[i] = le64_to_cpu(src->drop_io_u[i]);
1001 dst->total_submit = le64_to_cpu(src->total_submit);
1002 dst->total_complete = le64_to_cpu(src->total_complete);
1003 dst->nr_zone_resets = le64_to_cpu(src->nr_zone_resets);
1005 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1006 dst->io_bytes[i] = le64_to_cpu(src->io_bytes[i]);
1007 dst->runtime[i] = le64_to_cpu(src->runtime[i]);
1010 dst->total_run_time = le64_to_cpu(src->total_run_time);
1011 dst->continue_on_error = le16_to_cpu(src->continue_on_error);
1012 dst->total_err_count = le64_to_cpu(src->total_err_count);
1013 dst->first_error = le32_to_cpu(src->first_error);
1014 dst->kb_base = le32_to_cpu(src->kb_base);
1015 dst->unit_base = le32_to_cpu(src->unit_base);
1017 dst->sig_figs = le32_to_cpu(src->sig_figs);
1019 dst->latency_depth = le32_to_cpu(src->latency_depth);
1020 dst->latency_target = le64_to_cpu(src->latency_target);
1021 dst->latency_window = le64_to_cpu(src->latency_window);
1022 dst->latency_percentile.u.f = fio_uint64_to_double(le64_to_cpu(src->latency_percentile.u.i));
1024 dst->nr_block_infos = le64_to_cpu(src->nr_block_infos);
1025 for (i = 0; i < dst->nr_block_infos; i++)
1026 dst->block_infos[i] = le32_to_cpu(src->block_infos[i]);
1028 dst->ss_dur = le64_to_cpu(src->ss_dur);
1029 dst->ss_state = le32_to_cpu(src->ss_state);
1030 dst->ss_head = le32_to_cpu(src->ss_head);
1031 dst->ss_limit.u.f = fio_uint64_to_double(le64_to_cpu(src->ss_limit.u.i));
1032 dst->ss_slope.u.f = fio_uint64_to_double(le64_to_cpu(src->ss_slope.u.i));
1033 dst->ss_deviation.u.f = fio_uint64_to_double(le64_to_cpu(src->ss_deviation.u.i));
1034 dst->ss_criterion.u.f = fio_uint64_to_double(le64_to_cpu(src->ss_criterion.u.i));
1036 if (dst->ss_state & FIO_SS_DATA) {
1037 for (i = 0; i < dst->ss_dur; i++ ) {
1038 dst->ss_iops_data[i] = le64_to_cpu(src->ss_iops_data[i]);
1039 dst->ss_bw_data[i] = le64_to_cpu(src->ss_bw_data[i]);
1043 dst->cachehit = le64_to_cpu(src->cachehit);
1044 dst->cachemiss = le64_to_cpu(src->cachemiss);
1047 static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
1051 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1052 dst->max_run[i] = le64_to_cpu(src->max_run[i]);
1053 dst->min_run[i] = le64_to_cpu(src->min_run[i]);
1054 dst->max_bw[i] = le64_to_cpu(src->max_bw[i]);
1055 dst->min_bw[i] = le64_to_cpu(src->min_bw[i]);
1056 dst->iobytes[i] = le64_to_cpu(src->iobytes[i]);
1057 dst->agg[i] = le64_to_cpu(src->agg[i]);
1060 dst->kb_base = le32_to_cpu(src->kb_base);
1061 dst->unit_base = le32_to_cpu(src->unit_base);
1062 dst->sig_figs = le32_to_cpu(src->sig_figs);
1063 dst->groupid = le32_to_cpu(src->groupid);
1064 dst->unified_rw_rep = le32_to_cpu(src->unified_rw_rep);
1067 static void json_object_add_client_info(struct json_object *obj,
1068 struct fio_client *client)
1070 const char *hostname = client->hostname ? client->hostname : "";
1072 json_object_add_value_string(obj, "hostname", hostname);
1073 json_object_add_value_int(obj, "port", client->port);
1076 static void handle_ts(struct fio_client *client, struct fio_net_cmd *cmd)
1078 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
1079 struct flist_head *opt_list = NULL;
1080 struct json_object *tsobj;
1082 if (client->opt_lists && p->ts.thread_number <= client->jobs)
1083 opt_list = &client->opt_lists[p->ts.thread_number - 1];
1085 tsobj = show_thread_status(&p->ts, &p->rs, opt_list, &client->buf);
1086 client->did_stat = true;
1088 json_object_add_client_info(tsobj, client);
1089 json_array_add_value_object(clients_array, tsobj);
1092 if (sum_stat_clients <= 1)
1095 sum_thread_stats(&client_ts, &p->ts, sum_stat_nr == 1);
1096 sum_group_stats(&client_gs, &p->rs);
1098 client_ts.members++;
1099 client_ts.thread_number = p->ts.thread_number;
1100 client_ts.groupid = p->ts.groupid;
1101 client_ts.unified_rw_rep = p->ts.unified_rw_rep;
1102 client_ts.sig_figs = p->ts.sig_figs;
1104 if (++sum_stat_nr == sum_stat_clients) {
1105 strcpy(client_ts.name, "All clients");
1106 tsobj = show_thread_status(&client_ts, &client_gs, NULL, &client->buf);
1108 json_object_add_client_info(tsobj, client);
1109 json_array_add_value_object(clients_array, tsobj);
1114 static void handle_gs(struct fio_client *client, struct fio_net_cmd *cmd)
1116 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
1118 if (output_format & FIO_OUTPUT_NORMAL)
1119 show_group_stats(gs, &client->buf);
1122 static void handle_job_opt(struct fio_client *client, struct fio_net_cmd *cmd)
1124 struct cmd_job_option *pdu = (struct cmd_job_option *) cmd->payload;
1125 struct print_option *p;
1127 if (!job_opt_object)
1130 pdu->global = le16_to_cpu(pdu->global);
1131 pdu->truncated = le16_to_cpu(pdu->truncated);
1132 pdu->groupid = le32_to_cpu(pdu->groupid);
1134 p = malloc(sizeof(*p));
1135 p->name = strdup((char *) pdu->name);
1136 if (pdu->value[0] != '\0')
1137 p->value = strdup((char *) pdu->value);
1142 const char *pos = "";
1147 json_object_add_value_string(job_opt_object, p->name, pos);
1148 } else if (client->opt_lists) {
1149 struct flist_head *opt_list = &client->opt_lists[pdu->groupid];
1151 flist_add_tail(&p->list, opt_list);
1155 static void handle_text(struct fio_client *client, struct fio_net_cmd *cmd)
1157 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
1158 const char *buf = (const char *) pdu->buf;
1161 struct buf_output out;
1163 buf_output_init(&out);
1165 name = client->name ? client->name : client->hostname;
1167 if (!client->skip_newline && !(output_format & FIO_OUTPUT_TERSE))
1168 __log_buf(&out, "<%s> ", name);
1169 __log_buf(&out, "%s", buf);
1170 log_info_buf(out.buf, out.buflen);
1171 buf_output_free(&out);
1172 client->skip_newline = strchr(buf, '\n') == NULL;
1175 static void convert_agg(struct disk_util_agg *agg)
1179 for (i = 0; i < 2; i++) {
1180 agg->ios[i] = le64_to_cpu(agg->ios[i]);
1181 agg->merges[i] = le64_to_cpu(agg->merges[i]);
1182 agg->sectors[i] = le64_to_cpu(agg->sectors[i]);
1183 agg->ticks[i] = le64_to_cpu(agg->ticks[i]);
1186 agg->io_ticks = le64_to_cpu(agg->io_ticks);
1187 agg->time_in_queue = le64_to_cpu(agg->time_in_queue);
1188 agg->slavecount = le32_to_cpu(agg->slavecount);
1189 agg->max_util.u.f = fio_uint64_to_double(le64_to_cpu(agg->max_util.u.i));
1192 static void convert_dus(struct disk_util_stat *dus)
1196 for (i = 0; i < 2; i++) {
1197 dus->s.ios[i] = le64_to_cpu(dus->s.ios[i]);
1198 dus->s.merges[i] = le64_to_cpu(dus->s.merges[i]);
1199 dus->s.sectors[i] = le64_to_cpu(dus->s.sectors[i]);
1200 dus->s.ticks[i] = le64_to_cpu(dus->s.ticks[i]);
1203 dus->s.io_ticks = le64_to_cpu(dus->s.io_ticks);
1204 dus->s.time_in_queue = le64_to_cpu(dus->s.time_in_queue);
1205 dus->s.msec = le64_to_cpu(dus->s.msec);
1208 static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd)
1210 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
1212 if (!client->disk_stats_shown)
1213 client->disk_stats_shown = true;
1215 if (output_format & FIO_OUTPUT_JSON) {
1216 struct json_object *duobj;
1218 json_array_add_disk_util(&du->dus, &du->agg, du_array);
1219 duobj = json_array_last_value_object(du_array);
1220 json_object_add_client_info(duobj, client);
1221 } else if (output_format & FIO_OUTPUT_TERSE)
1222 print_disk_util(&du->dus, &du->agg, 1, &client->buf);
1223 else if (output_format & FIO_OUTPUT_NORMAL) {
1224 __log_buf(&client->buf, "\nDisk stats (read/write):\n");
1225 print_disk_util(&du->dus, &du->agg, 0, &client->buf);
1229 static void convert_jobs_eta(struct jobs_eta *je)
1233 je->nr_running = le32_to_cpu(je->nr_running);
1234 je->nr_ramp = le32_to_cpu(je->nr_ramp);
1235 je->nr_pending = le32_to_cpu(je->nr_pending);
1236 je->nr_setting_up = le32_to_cpu(je->nr_setting_up);
1237 je->files_open = le32_to_cpu(je->files_open);
1239 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1240 je->m_rate[i] = le64_to_cpu(je->m_rate[i]);
1241 je->t_rate[i] = le64_to_cpu(je->t_rate[i]);
1242 je->m_iops[i] = le32_to_cpu(je->m_iops[i]);
1243 je->t_iops[i] = le32_to_cpu(je->t_iops[i]);
1244 je->rate[i] = le64_to_cpu(je->rate[i]);
1245 je->iops[i] = le32_to_cpu(je->iops[i]);
1248 je->elapsed_sec = le64_to_cpu(je->elapsed_sec);
1249 je->eta_sec = le64_to_cpu(je->eta_sec);
1250 je->nr_threads = le32_to_cpu(je->nr_threads);
1251 je->is_pow2 = le32_to_cpu(je->is_pow2);
1252 je->unit_base = le32_to_cpu(je->unit_base);
1253 je->sig_figs = le32_to_cpu(je->sig_figs);
1256 void fio_client_sum_jobs_eta(struct jobs_eta *dst, struct jobs_eta *je)
1260 dst->nr_running += je->nr_running;
1261 dst->nr_ramp += je->nr_ramp;
1262 dst->nr_pending += je->nr_pending;
1263 dst->nr_setting_up += je->nr_setting_up;
1264 dst->files_open += je->files_open;
1266 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1267 dst->m_rate[i] += je->m_rate[i];
1268 dst->t_rate[i] += je->t_rate[i];
1269 dst->m_iops[i] += je->m_iops[i];
1270 dst->t_iops[i] += je->t_iops[i];
1271 dst->rate[i] += je->rate[i];
1272 dst->iops[i] += je->iops[i];
1275 dst->elapsed_sec += je->elapsed_sec;
1277 if (je->eta_sec > dst->eta_sec)
1278 dst->eta_sec = je->eta_sec;
1280 dst->nr_threads += je->nr_threads;
1283 * This wont be correct for multiple strings, but at least it
1284 * works for the basic cases.
1286 strcpy((char *) dst->run_str, (char *) je->run_str);
1289 static bool remove_reply_cmd(struct fio_client *client, struct fio_net_cmd *cmd)
1291 struct fio_net_cmd_reply *reply = NULL;
1292 struct flist_head *entry;
1294 flist_for_each(entry, &client->cmd_list) {
1295 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
1297 if (cmd->tag == (uintptr_t) reply)
1304 log_err("fio: client: unable to find matching tag (%llx)\n", (unsigned long long) cmd->tag);
1308 flist_del(&reply->list);
1309 cmd->tag = reply->saved_tag;
1314 int fio_client_wait_for_reply(struct fio_client *client, uint64_t tag)
1317 struct fio_net_cmd_reply *reply = NULL;
1318 struct flist_head *entry;
1320 flist_for_each(entry, &client->cmd_list) {
1321 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
1323 if (tag == (uintptr_t) reply)
1338 static void handle_eta(struct fio_client *client, struct fio_net_cmd *cmd)
1340 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
1341 struct client_eta *eta = (struct client_eta *) (uintptr_t) cmd->tag;
1343 dprint(FD_NET, "client: got eta tag %p, %d\n", eta, eta->pending);
1345 assert(client->eta_in_flight == eta);
1347 client->eta_in_flight = NULL;
1348 flist_del_init(&client->eta_list);
1349 client->eta_timeouts = 0;
1351 if (client->ops->jobs_eta)
1352 client->ops->jobs_eta(client, je);
1354 fio_client_sum_jobs_eta(&eta->eta, je);
1355 fio_client_dec_jobs_eta(eta, client->ops->eta);
1358 static void client_flush_hist_samples(FILE *f, int hist_coarseness, void *samples,
1359 uint64_t sample_size)
1361 struct io_sample *s;
1363 uint64_t i, j, nr_samples;
1364 struct io_u_plat_entry *entry;
1365 uint64_t *io_u_plat;
1367 int stride = 1 << hist_coarseness;
1372 s = __get_sample(samples, 0, 0);
1373 log_offset = (s->__ddir & LOG_OFFSET_SAMPLE_BIT) != 0;
1375 nr_samples = sample_size / __log_entry_sz(log_offset);
1377 for (i = 0; i < nr_samples; i++) {
1379 s = (struct io_sample *)((char *)__get_sample(samples, log_offset, i) +
1380 i * sizeof(struct io_u_plat_entry));
1382 entry = s->data.plat_entry;
1383 io_u_plat = entry->io_u_plat;
1385 fprintf(f, "%lu, %u, %llu, ", (unsigned long) s->time,
1386 io_sample_ddir(s), (unsigned long long) s->bs);
1387 for (j = 0; j < FIO_IO_U_PLAT_NR - stride; j += stride) {
1388 fprintf(f, "%llu, ", (unsigned long long)hist_sum(j, stride, io_u_plat, NULL));
1390 fprintf(f, "%llu\n", (unsigned long long)
1391 hist_sum(FIO_IO_U_PLAT_NR - stride, stride, io_u_plat, NULL));
1396 static int fio_client_handle_iolog(struct fio_client *client,
1397 struct fio_net_cmd *cmd)
1399 struct cmd_iolog_pdu *pdu = NULL;
1401 char *log_pathname = NULL;
1404 pdu = convert_iolog(cmd, &store_direct);
1406 log_err("fio: failed converting IO log\n");
1411 /* allocate buffer big enough for next sprintf() call */
1412 log_pathname = malloc(10 + strlen((char *)pdu->name) +
1413 strlen(client->hostname));
1414 if (!log_pathname) {
1415 log_err("fio: memory allocation of unique pathname failed\n");
1419 /* generate a unique pathname for the log file using hostname */
1420 sprintf(log_pathname, "%s.%s", pdu->name, client->hostname);
1427 fd = open((const char *) log_pathname,
1428 O_WRONLY | O_CREAT | O_TRUNC, 0644);
1430 log_err("fio: open log %s: %s\n",
1431 log_pathname, strerror(errno));
1436 sz = cmd->pdu_len - sizeof(*pdu);
1437 wrote = write(fd, pdu->samples, sz);
1441 log_err("fio: short write on compressed log\n");
1449 f = fopen((const char *) log_pathname, "w");
1451 log_err("fio: fopen log %s : %s\n",
1452 log_pathname, strerror(errno));
1457 if (pdu->log_type == IO_LOG_TYPE_HIST) {
1458 client_flush_hist_samples(f, pdu->log_hist_coarseness, pdu->samples,
1459 pdu->nr_samples * sizeof(struct io_sample));
1461 flush_samples(f, pdu->samples,
1462 pdu->nr_samples * sizeof(struct io_sample));
1469 if (pdu && pdu != (void *) cmd->payload)
1478 static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd)
1480 struct cmd_probe_reply_pdu *probe = (struct cmd_probe_reply_pdu *) cmd->payload;
1481 const char *os, *arch;
1484 os = fio_get_os_string(probe->os);
1488 arch = fio_get_arch_string(probe->arch);
1492 sprintf(bit, "%d-bit", probe->bpp * 8);
1493 probe->flags = le64_to_cpu(probe->flags);
1495 if (output_format & FIO_OUTPUT_NORMAL) {
1496 log_info("hostname=%s, be=%u, %s, os=%s, arch=%s, fio=%s, flags=%lx\n",
1497 probe->hostname, probe->bigendian, bit, os, arch,
1498 probe->fio_version, (unsigned long) probe->flags);
1502 client->name = strdup((char *) probe->hostname);
1505 static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd)
1507 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
1509 client->state = Client_started;
1510 client->jobs = le32_to_cpu(pdu->jobs);
1511 client->nr_stat = le32_to_cpu(pdu->stat_outputs);
1516 if (client->opt_lists)
1517 free(client->opt_lists);
1519 client->opt_lists = malloc(client->jobs * sizeof(struct flist_head));
1520 for (i = 0; i < client->jobs; i++)
1521 INIT_FLIST_HEAD(&client->opt_lists[i]);
1524 sum_stat_clients += client->nr_stat;
1527 static void handle_stop(struct fio_client *client)
1530 log_info("client <%s>: exited with error %d\n", client->hostname, client->error);
1533 static void convert_stop(struct fio_net_cmd *cmd)
1535 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
1537 pdu->error = le32_to_cpu(pdu->error);
1540 static void convert_text(struct fio_net_cmd *cmd)
1542 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
1544 pdu->level = le32_to_cpu(pdu->level);
1545 pdu->buf_len = le32_to_cpu(pdu->buf_len);
1546 pdu->log_sec = le64_to_cpu(pdu->log_sec);
1547 pdu->log_usec = le64_to_cpu(pdu->log_usec);
1550 static struct cmd_iolog_pdu *convert_iolog_gz(struct fio_net_cmd *cmd,
1551 struct cmd_iolog_pdu *pdu)
1554 struct cmd_iolog_pdu *ret;
1556 uint64_t nr_samples;
1560 stream.zalloc = Z_NULL;
1561 stream.zfree = Z_NULL;
1562 stream.opaque = Z_NULL;
1563 stream.avail_in = 0;
1564 stream.next_in = Z_NULL;
1566 if (inflateInit(&stream) != Z_OK)
1570 * Get header first, it's not compressed
1572 nr_samples = le64_to_cpu(pdu->nr_samples);
1574 if (pdu->log_type == IO_LOG_TYPE_HIST)
1575 total = nr_samples * (__log_entry_sz(le32_to_cpu(pdu->log_offset)) +
1576 sizeof(struct io_u_plat_entry));
1578 total = nr_samples * __log_entry_sz(le32_to_cpu(pdu->log_offset));
1579 ret = malloc(total + sizeof(*pdu));
1580 ret->nr_samples = nr_samples;
1582 memcpy(ret, pdu, sizeof(*pdu));
1584 p = (char *) ret + sizeof(*pdu);
1586 stream.avail_in = cmd->pdu_len - sizeof(*pdu);
1587 stream.next_in = (void *)((char *) pdu + sizeof(*pdu));
1588 while (stream.avail_in) {
1589 unsigned int this_chunk = 65536;
1590 unsigned int this_len;
1593 if (this_chunk > total)
1596 stream.avail_out = this_chunk;
1597 stream.next_out = (void *)p;
1598 err = inflate(&stream, Z_NO_FLUSH);
1599 /* may be Z_OK, or Z_STREAM_END */
1602 * Z_STREAM_ERROR and Z_BUF_ERROR can safely be
1604 if (err == Z_STREAM_ERROR || err == Z_BUF_ERROR)
1606 log_err("fio: inflate error %d\n", err);
1612 this_len = this_chunk - stream.avail_out;
1618 inflateEnd(&stream);
1626 * This has been compressed on the server side, since it can be big.
1629 static struct cmd_iolog_pdu *convert_iolog(struct fio_net_cmd *cmd,
1632 struct cmd_iolog_pdu *pdu = (struct cmd_iolog_pdu *) cmd->payload;
1633 struct cmd_iolog_pdu *ret;
1638 *store_direct = false;
1641 * Convert if compressed and we support it. If it's not
1642 * compressed, we need not do anything.
1644 compressed = le32_to_cpu(pdu->compressed);
1645 if (compressed == XMIT_COMPRESSED) {
1647 log_err("fio: server sent compressed data by mistake\n");
1650 ret = convert_iolog_gz(cmd, pdu);
1652 log_err("fio: failed decompressing log\n");
1655 } else if (compressed == STORE_COMPRESSED) {
1656 *store_direct = true;
1661 ret->nr_samples = le64_to_cpu(ret->nr_samples);
1662 ret->thread_number = le32_to_cpu(ret->thread_number);
1663 ret->log_type = le32_to_cpu(ret->log_type);
1664 ret->compressed = le32_to_cpu(ret->compressed);
1665 ret->log_offset = le32_to_cpu(ret->log_offset);
1666 ret->log_hist_coarseness = le32_to_cpu(ret->log_hist_coarseness);
1671 samples = &ret->samples[0];
1672 for (i = 0; i < ret->nr_samples; i++) {
1673 struct io_sample *s;
1675 s = __get_sample(samples, ret->log_offset, i);
1676 if (ret->log_type == IO_LOG_TYPE_HIST)
1677 s = (struct io_sample *)((char *)s + sizeof(struct io_u_plat_entry) * i);
1679 s->time = le64_to_cpu(s->time);
1680 s->data.val = le64_to_cpu(s->data.val);
1681 s->__ddir = le32_to_cpu(s->__ddir);
1682 s->bs = le64_to_cpu(s->bs);
1684 if (ret->log_offset) {
1685 struct io_sample_offset *so = (void *) s;
1687 so->offset = le64_to_cpu(so->offset);
1690 if (ret->log_type == IO_LOG_TYPE_HIST) {
1691 s->data.plat_entry = (struct io_u_plat_entry *)(((char *)s) + sizeof(*s));
1692 s->data.plat_entry->list.next = NULL;
1693 s->data.plat_entry->list.prev = NULL;
1700 static void sendfile_reply(int fd, struct cmd_sendfile_reply *rep,
1701 size_t size, uint64_t tag)
1703 rep->error = cpu_to_le32(rep->error);
1704 fio_net_send_cmd(fd, FIO_NET_CMD_SENDFILE, rep, size, &tag, NULL);
1707 static int fio_send_file(struct fio_client *client, struct cmd_sendfile *pdu,
1710 struct cmd_sendfile_reply *rep;
1715 size = sizeof(*rep);
1718 if (stat((char *)pdu->path, &sb) < 0) {
1721 sendfile_reply(client->fd, rep, size, tag);
1727 rep = realloc(rep, size);
1728 rep->size = cpu_to_le32((uint32_t) sb.st_size);
1730 fd = open((char *)pdu->path, O_RDONLY);
1734 rep->error = read_data(fd, &rep->data, sb.st_size);
1735 sendfile_reply(client->fd, rep, size, tag);
1741 int fio_handle_client(struct fio_client *client)
1743 struct client_ops *ops = client->ops;
1744 struct fio_net_cmd *cmd;
1747 dprint(FD_NET, "client: handle %s\n", client->hostname);
1749 cmd = fio_net_recv_cmd(client->fd, true);
1753 dprint(FD_NET, "client: got cmd op %s from %s (pdu=%u)\n",
1754 fio_server_op(cmd->opcode), client->hostname, cmd->pdu_len);
1756 client->last_cmd = cmd->opcode;
1758 switch (cmd->opcode) {
1759 case FIO_NET_CMD_QUIT:
1761 ops->quit(client, cmd);
1762 remove_client(client);
1764 case FIO_NET_CMD_TEXT:
1766 ops->text(client, cmd);
1768 case FIO_NET_CMD_DU: {
1769 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
1771 convert_dus(&du->dus);
1772 convert_agg(&du->agg);
1774 ops->disk_util(client, cmd);
1777 case FIO_NET_CMD_TS: {
1778 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
1780 dprint(FD_NET, "client: ts->ss_state = %u\n", (unsigned int) le32_to_cpu(p->ts.ss_state));
1781 if (le32_to_cpu(p->ts.ss_state) & FIO_SS_DATA) {
1782 dprint(FD_NET, "client: received steadystate ring buffers\n");
1784 size = le64_to_cpu(p->ts.ss_dur);
1785 p->ts.ss_iops_data = (uint64_t *) ((struct cmd_ts_pdu *)cmd->payload + 1);
1786 p->ts.ss_bw_data = p->ts.ss_iops_data + size;
1789 convert_ts(&p->ts, &p->ts);
1790 convert_gs(&p->rs, &p->rs);
1792 ops->thread_status(client, cmd);
1795 case FIO_NET_CMD_GS: {
1796 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
1800 ops->group_stats(client, cmd);
1803 case FIO_NET_CMD_ETA: {
1804 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
1806 if (!remove_reply_cmd(client, cmd))
1808 convert_jobs_eta(je);
1809 handle_eta(client, cmd);
1812 case FIO_NET_CMD_PROBE:
1813 remove_reply_cmd(client, cmd);
1814 ops->probe(client, cmd);
1816 case FIO_NET_CMD_SERVER_START:
1817 client->state = Client_running;
1819 ops->job_start(client, cmd);
1821 case FIO_NET_CMD_START: {
1822 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
1824 pdu->jobs = le32_to_cpu(pdu->jobs);
1825 ops->start(client, cmd);
1828 case FIO_NET_CMD_STOP: {
1829 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
1832 client->state = Client_stopped;
1833 client->error = le32_to_cpu(pdu->error);
1834 client->signal = le32_to_cpu(pdu->signal);
1838 case FIO_NET_CMD_ADD_JOB: {
1839 struct cmd_add_job_pdu *pdu = (struct cmd_add_job_pdu *) cmd->payload;
1841 client->thread_number = le32_to_cpu(pdu->thread_number);
1842 client->groupid = le32_to_cpu(pdu->groupid);
1845 ops->add_job(client, cmd);
1848 case FIO_NET_CMD_IOLOG:
1849 fio_client_handle_iolog(client, cmd);
1851 case FIO_NET_CMD_UPDATE_JOB:
1852 ops->update_job(client, cmd);
1853 remove_reply_cmd(client, cmd);
1855 case FIO_NET_CMD_VTRIGGER: {
1856 struct all_io_list *pdu = (struct all_io_list *) cmd->payload;
1861 strcpy(buf, aux_path);
1865 __verify_save_state(pdu, server_name(client, &buf[off], sizeof(buf) - off));
1866 exec_trigger(trigger_cmd);
1869 case FIO_NET_CMD_SENDFILE: {
1870 struct cmd_sendfile *pdu = (struct cmd_sendfile *) cmd->payload;
1871 fio_send_file(client, pdu, cmd->tag);
1874 case FIO_NET_CMD_JOB_OPT: {
1875 handle_job_opt(client, cmd);
1879 log_err("fio: unknown client op: %s\n", fio_server_op(cmd->opcode));
1887 int fio_clients_send_trigger(const char *cmd)
1889 struct flist_head *entry;
1890 struct fio_client *client;
1893 dprint(FD_NET, "client: send vtrigger: %s\n", cmd);
1900 flist_for_each(entry, &client_list) {
1901 struct cmd_vtrigger_pdu *pdu;
1903 client = flist_entry(entry, struct fio_client, list);
1905 pdu = malloc(sizeof(*pdu) + slen);
1906 pdu->len = cpu_to_le16((uint16_t) slen);
1908 memcpy(pdu->cmd, cmd, slen);
1909 fio_net_send_cmd(client->fd, FIO_NET_CMD_VTRIGGER, pdu,
1910 sizeof(*pdu) + slen, NULL, NULL);
1917 static void request_client_etas(struct client_ops *ops)
1919 struct fio_client *client;
1920 struct flist_head *entry;
1921 struct client_eta *eta;
1924 if (eta_print == FIO_ETA_NEVER)
1927 dprint(FD_NET, "client: request eta (%d)\n", nr_clients);
1929 eta = calloc(1, sizeof(*eta) + __THREAD_RUNSTR_SZ(REAL_MAX_JOBS));
1930 eta->pending = nr_clients;
1932 flist_for_each(entry, &client_list) {
1933 client = flist_entry(entry, struct fio_client, list);
1935 if (!flist_empty(&client->eta_list)) {
1939 if (client->state != Client_running)
1942 assert(!client->eta_in_flight);
1943 flist_add_tail(&client->eta_list, &eta_list);
1944 client->eta_in_flight = eta;
1945 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_SEND_ETA,
1946 (uintptr_t) eta, &client->cmd_list);
1950 if (!fio_client_dec_jobs_eta(eta, ops->eta))
1954 dprint(FD_NET, "client: requested eta tag %p\n", eta);
1958 * A single SEND_ETA timeout isn't fatal. Attempt to recover.
1960 static int handle_cmd_timeout(struct fio_client *client,
1961 struct fio_net_cmd_reply *reply)
1963 uint16_t reply_opcode = reply->opcode;
1965 flist_del(&reply->list);
1968 if (reply_opcode != FIO_NET_CMD_SEND_ETA)
1971 log_info("client <%s>: timeout on SEND_ETA\n", client->hostname);
1973 flist_del_init(&client->eta_list);
1974 if (client->eta_in_flight) {
1975 fio_client_dec_jobs_eta(client->eta_in_flight, client->ops->eta);
1976 client->eta_in_flight = NULL;
1980 * If we fail 5 in a row, give up...
1982 if (client->eta_timeouts++ > 5)
1988 static int client_check_cmd_timeout(struct fio_client *client,
1989 struct timespec *now)
1991 struct fio_net_cmd_reply *reply;
1992 struct flist_head *entry, *tmp;
1995 flist_for_each_safe(entry, tmp, &client->cmd_list) {
1998 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
2000 if (mtime_since(&reply->ts, now) < FIO_NET_CLIENT_TIMEOUT)
2004 if (!handle_cmd_timeout(client, reply))
2007 log_err("fio: client %s, timeout on cmd %s\n", client->hostname,
2012 return flist_empty(&client->cmd_list) && ret;
2015 static int fio_check_clients_timed_out(void)
2017 struct fio_client *client;
2018 struct flist_head *entry, *tmp;
2022 fio_gettime(&ts, NULL);
2024 flist_for_each_safe(entry, tmp, &client_list) {
2025 client = flist_entry(entry, struct fio_client, list);
2027 if (flist_empty(&client->cmd_list))
2030 if (!client_check_cmd_timeout(client, &ts))
2033 if (client->ops->timed_out)
2034 client->ops->timed_out(client);
2036 log_err("fio: client %s timed out\n", client->hostname);
2038 if (client->last_cmd != FIO_NET_CMD_VTRIGGER)
2039 client->error = ETIMEDOUT;
2041 log_info("fio: ignoring timeout due to vtrigger\n");
2042 remove_client(client);
2049 int fio_handle_clients(struct client_ops *ops)
2051 struct pollfd *pfds;
2052 int i, ret = 0, retval = 0;
2054 fio_gettime(&eta_ts, NULL);
2056 pfds = malloc(nr_clients * sizeof(struct pollfd));
2058 init_thread_stat(&client_ts);
2059 init_group_run_stat(&client_gs);
2061 while (!exit_backend && nr_clients) {
2062 struct flist_head *entry, *tmp;
2063 struct fio_client *client;
2066 flist_for_each_safe(entry, tmp, &client_list) {
2067 client = flist_entry(entry, struct fio_client, list);
2069 if (!client->sent_job && !client->ops->stay_connected &&
2070 flist_empty(&client->cmd_list)) {
2071 remove_client(client);
2075 pfds[i].fd = client->fd;
2076 pfds[i].events = POLLIN;
2083 assert(i == nr_clients);
2089 fio_gettime(&ts, NULL);
2090 if (eta_time_within_slack(mtime_since(&eta_ts, &ts))) {
2091 request_client_etas(ops);
2092 memcpy(&eta_ts, &ts, sizeof(ts));
2094 if (fio_check_clients_timed_out())
2098 check_trigger_file();
2100 timeout = min(100u, ops->eta_msec);
2102 ret = poll(pfds, nr_clients, timeout);
2106 log_err("fio: poll clients: %s\n", strerror(errno));
2112 for (i = 0; i < nr_clients; i++) {
2113 if (!(pfds[i].revents & POLLIN))
2116 client = find_client_by_fd(pfds[i].fd);
2118 log_err("fio: unknown client fd %ld\n", (long) pfds[i].fd);
2121 if (!fio_handle_client(client)) {
2122 log_info("client: host=%s disconnected\n",
2124 remove_client(client);
2126 } else if (client->error)
2128 fio_put_client(client);
2132 fio_client_json_fini();
2135 return retval || error_clients;
2138 static void client_display_thread_status(struct jobs_eta *je)
2140 if (!(output_format & FIO_OUTPUT_JSON))
2141 display_thread_status(je);