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 buf_output allclients;
63 static struct json_object *root = NULL;
64 static struct json_object *job_opt_object = NULL;
65 static struct json_array *clients_array = NULL;
66 static struct json_array *du_array = NULL;
68 static int error_clients;
70 #define FIO_CLIENT_HASH_BITS 7
71 #define FIO_CLIENT_HASH_SZ (1 << FIO_CLIENT_HASH_BITS)
72 #define FIO_CLIENT_HASH_MASK (FIO_CLIENT_HASH_SZ - 1)
73 static struct flist_head client_hash[FIO_CLIENT_HASH_SZ];
75 static struct cmd_iolog_pdu *convert_iolog(struct fio_net_cmd *, bool *);
77 static void fio_client_add_hash(struct fio_client *client)
79 int bucket = hash_long(client->fd, FIO_CLIENT_HASH_BITS);
81 bucket &= FIO_CLIENT_HASH_MASK;
82 flist_add(&client->hash_list, &client_hash[bucket]);
85 static void fio_client_remove_hash(struct fio_client *client)
87 if (!flist_empty(&client->hash_list))
88 flist_del_init(&client->hash_list);
91 static void fio_init fio_client_hash_init(void)
95 for (i = 0; i < FIO_CLIENT_HASH_SZ; i++)
96 INIT_FLIST_HEAD(&client_hash[i]);
99 static int read_data(int fd, void *data, size_t size)
104 ret = read(fd, data, size);
106 if (errno == EAGAIN || errno == EINTR)
123 static int read_ini_data(int fd, void *data, size_t size)
134 fp = fdopen(dupfd, "r");
143 char buf[OPT_LEN_MAX+1], *sub;
145 if (!fgets(buf, sizeof(buf), fp)) {
147 if (errno == EAGAIN || errno == EINTR)
154 sub = fio_option_dup_subs(buf);
156 if (len + 1 > size) {
157 log_err("fio: no space left to read data\n");
175 static void fio_client_json_init(void)
180 if (!(output_format & FIO_OUTPUT_JSON))
184 os_ctime_r((const time_t *) &time_p, time_buf, sizeof(time_buf));
185 time_buf[strlen(time_buf) - 1] = '\0';
187 root = json_create_object();
188 json_object_add_value_string(root, "fio version", fio_version_string);
189 json_object_add_value_int(root, "timestamp", time_p);
190 json_object_add_value_string(root, "time", time_buf);
192 job_opt_object = json_create_object();
193 json_object_add_value_object(root, "global options", job_opt_object);
194 clients_array = json_create_array();
195 json_object_add_value_array(root, "client_stats", clients_array);
196 du_array = json_create_array();
197 json_object_add_value_array(root, "disk_util", du_array);
200 static void fio_client_json_fini(void)
202 struct buf_output out;
207 buf_output_init(&out);
209 __log_buf(&out, "\n");
210 json_print_object(root, &out);
211 __log_buf(&out, "\n");
212 log_info_buf(out.buf, out.buflen);
214 buf_output_free(&out);
216 json_free_object(root);
218 job_opt_object = NULL;
219 clients_array = NULL;
223 static struct fio_client *find_client_by_fd(int fd)
225 int bucket = hash_long(fd, FIO_CLIENT_HASH_BITS) & FIO_CLIENT_HASH_MASK;
226 struct fio_client *client;
227 struct flist_head *entry;
229 flist_for_each(entry, &client_hash[bucket]) {
230 client = flist_entry(entry, struct fio_client, hash_list);
232 if (client->fd == fd) {
241 void fio_put_client(struct fio_client *client)
246 log_info_buf(client->buf.buf, client->buf.buflen);
247 buf_output_free(&client->buf);
249 free(client->hostname);
254 while (client->nr_files) {
255 struct client_file *cf = &client->files[--client->nr_files];
261 if (client->opt_lists)
262 free(client->opt_lists);
264 if (!client->did_stat)
273 static int fio_client_dec_jobs_eta(struct client_eta *eta, client_eta_op eta_fn)
275 if (!--eta->pending) {
284 static void fio_drain_client_text(struct fio_client *client)
287 struct fio_net_cmd *cmd;
289 cmd = fio_net_recv_cmd(client->fd, false);
293 if (cmd->opcode == FIO_NET_CMD_TEXT) {
295 client->ops->text(client, cmd);
302 static void remove_client(struct fio_client *client)
304 assert(client->refs);
306 dprint(FD_NET, "client: removed <%s>\n", client->hostname);
308 fio_drain_client_text(client);
310 if (!flist_empty(&client->list))
311 flist_del_init(&client->list);
313 fio_client_remove_hash(client);
315 if (!flist_empty(&client->eta_list)) {
316 flist_del_init(&client->eta_list);
317 fio_client_dec_jobs_eta(client->eta_in_flight, client->ops->eta);
323 if (client->ops->removed)
324 client->ops->removed(client);
327 fio_put_client(client);
330 struct fio_client *fio_get_client(struct fio_client *client)
336 static void __fio_client_add_cmd_option(struct fio_client *client,
341 index = client->argc++;
342 client->argv = realloc(client->argv, sizeof(char *) * client->argc);
343 client->argv[index] = strdup(opt);
344 dprint(FD_NET, "client: add cmd %d: %s\n", index, opt);
347 void fio_client_add_cmd_option(void *cookie, const char *opt)
349 struct fio_client *client = cookie;
350 struct flist_head *entry;
355 __fio_client_add_cmd_option(client, opt);
358 * Duplicate arguments to shared client group
360 flist_for_each(entry, &arg_list) {
361 client = flist_entry(entry, struct fio_client, arg_list);
363 __fio_client_add_cmd_option(client, opt);
367 static struct fio_client *get_new_client(void)
369 struct fio_client *client;
371 client = malloc(sizeof(*client));
372 memset(client, 0, sizeof(*client));
374 INIT_FLIST_HEAD(&client->list);
375 INIT_FLIST_HEAD(&client->hash_list);
376 INIT_FLIST_HEAD(&client->arg_list);
377 INIT_FLIST_HEAD(&client->eta_list);
378 INIT_FLIST_HEAD(&client->cmd_list);
380 buf_output_init(&client->buf);
385 struct fio_client *fio_client_add_explicit(struct client_ops *ops,
386 const char *hostname, int type,
389 struct fio_client *client;
391 client = get_new_client();
393 if (type == Fio_client_socket)
394 client->is_sock = true;
398 ipv6 = type == Fio_client_ipv6;
399 if (fio_server_parse_host(hostname, ipv6,
400 &client->addr.sin_addr,
401 &client->addr6.sin6_addr))
410 client->type = ops->client_type;
411 client->hostname = strdup(hostname);
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,
474 fio_put_client(client);
481 client->type = ops->client_type;
483 __fio_client_add_cmd_option(client, "fio");
485 flist_add(&client->list, &client_list);
487 dprint(FD_NET, "client: added <%s>\n", client->hostname);
492 static const char *server_name(struct fio_client *client, char *buf,
498 from = inet_ntop(AF_INET6, (struct sockaddr *) &client->addr6.sin6_addr, buf, bufsize);
499 else if (client->is_sock)
502 from = inet_ntop(AF_INET, (struct sockaddr *) &client->addr.sin_addr, buf, bufsize);
507 static void probe_client(struct fio_client *client)
509 struct cmd_client_probe_pdu pdu;
514 dprint(FD_NET, "client: send probe\n");
517 pdu.flags = __le64_to_cpu(FIO_PROBE_FLAG_ZLIB);
522 sname = server_name(client, buf, sizeof(buf));
523 memset(pdu.server, 0, sizeof(pdu.server));
524 snprintf((char *) pdu.server, sizeof(pdu.server), "%s", sname);
526 fio_net_send_cmd(client->fd, FIO_NET_CMD_PROBE, &pdu, sizeof(pdu), &tag, &client->cmd_list);
529 static int fio_client_connect_ip(struct fio_client *client)
531 struct sockaddr *addr;
536 client->addr6.sin6_family = AF_INET6;
537 client->addr6.sin6_port = htons(client->port);
539 addr = (struct sockaddr *) &client->addr6;
540 socklen = sizeof(client->addr6);
542 client->addr.sin_family = AF_INET;
543 client->addr.sin_port = htons(client->port);
545 addr = (struct sockaddr *) &client->addr;
546 socklen = sizeof(client->addr);
549 fd = socket(domain, SOCK_STREAM, 0);
553 log_err("fio: socket: %s\n", strerror(errno));
557 if (connect(fd, addr, socklen) < 0) {
560 log_err("fio: connect: %s\n", strerror(errno));
561 log_err("fio: failed to connect to %s:%u\n", client->hostname,
570 static int fio_client_connect_sock(struct fio_client *client)
572 struct sockaddr_un *addr = &client->addr_un;
576 memset(addr, 0, sizeof(*addr));
577 addr->sun_family = AF_UNIX;
578 snprintf(addr->sun_path, sizeof(addr->sun_path), "%s",
581 fd = socket(AF_UNIX, SOCK_STREAM, 0);
585 log_err("fio: socket: %s\n", strerror(errno));
589 len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
590 if (connect(fd, (struct sockaddr *) addr, len) < 0) {
593 log_err("fio: connect; %s\n", strerror(errno));
601 int fio_client_connect(struct fio_client *client)
605 dprint(FD_NET, "client: connect to host %s\n", client->hostname);
608 fd = fio_client_connect_sock(client);
610 fd = fio_client_connect_ip(client);
612 dprint(FD_NET, "client: %s connected %d\n", client->hostname, fd);
618 fio_client_add_hash(client);
619 client->state = Client_connected;
621 probe_client(client);
625 int fio_client_terminate(struct fio_client *client)
627 return fio_net_send_quit(client->fd);
630 static void fio_clients_terminate(void)
632 struct flist_head *entry;
633 struct fio_client *client;
635 dprint(FD_NET, "client: terminate clients\n");
637 flist_for_each(entry, &client_list) {
638 client = flist_entry(entry, struct fio_client, list);
639 fio_client_terminate(client);
643 static void sig_int(int sig)
645 dprint(FD_NET, "client: got signal %d\n", sig);
646 fio_clients_terminate();
649 static void client_signal_handler(void)
651 struct sigaction act;
653 memset(&act, 0, sizeof(act));
654 act.sa_handler = sig_int;
655 act.sa_flags = SA_RESTART;
656 sigaction(SIGINT, &act, NULL);
658 memset(&act, 0, sizeof(act));
659 act.sa_handler = sig_int;
660 act.sa_flags = SA_RESTART;
661 sigaction(SIGTERM, &act, NULL);
663 /* Windows uses SIGBREAK as a quit signal from other applications */
665 memset(&act, 0, sizeof(act));
666 act.sa_handler = sig_int;
667 act.sa_flags = SA_RESTART;
668 sigaction(SIGBREAK, &act, NULL);
671 memset(&act, 0, sizeof(act));
672 act.sa_handler = sig_show_status;
673 act.sa_flags = SA_RESTART;
674 sigaction(SIGUSR1, &act, NULL);
677 static int send_client_cmd_line(struct fio_client *client)
679 struct cmd_single_line_pdu *cslp;
680 struct cmd_line_pdu *clp;
681 unsigned long offset;
687 dprint(FD_NET, "client: send cmdline %d\n", client->argc);
689 lens = malloc(client->argc * sizeof(unsigned int));
692 * Find out how much mem we need
694 for (i = 0, mem = 0; i < client->argc; i++) {
695 lens[i] = strlen(client->argv[i]) + 1;
700 * We need one cmd_line_pdu, and argc number of cmd_single_line_pdu
702 mem += sizeof(*clp) + (client->argc * sizeof(*cslp));
706 offset = sizeof(*clp);
708 for (i = 0; i < client->argc; i++) {
709 uint16_t arg_len = lens[i];
712 strcpy((char *) cslp->text, client->argv[i]);
713 cslp->len = cpu_to_le16(arg_len);
714 offset += sizeof(*cslp) + arg_len;
718 clp->lines = cpu_to_le16(client->argc);
719 clp->client_type = __cpu_to_le16(client->type);
720 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOBLINE, pdu, mem, NULL, NULL);
725 int fio_clients_connect(void)
727 struct fio_client *client;
728 struct flist_head *entry, *tmp;
733 WSAStartup(MAKEWORD(2, 2), &wsd);
736 dprint(FD_NET, "client: connect all\n");
738 client_signal_handler();
740 flist_for_each_safe(entry, tmp, &client_list) {
741 client = flist_entry(entry, struct fio_client, list);
743 ret = fio_client_connect(client);
745 remove_client(client);
749 if (client->argc > 1)
750 send_client_cmd_line(client);
756 int fio_start_client(struct fio_client *client)
758 dprint(FD_NET, "client: start %s\n", client->hostname);
759 return fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_RUN, 0, NULL);
762 int fio_start_all_clients(void)
764 struct fio_client *client;
765 struct flist_head *entry, *tmp;
768 dprint(FD_NET, "client: start all\n");
770 fio_client_json_init();
772 flist_for_each_safe(entry, tmp, &client_list) {
773 client = flist_entry(entry, struct fio_client, list);
775 ret = fio_start_client(client);
777 remove_client(client);
782 return flist_empty(&client_list);
785 static int __fio_client_send_remote_ini(struct fio_client *client,
786 const char *filename)
788 struct cmd_load_file_pdu *pdu;
792 dprint(FD_NET, "send remote ini %s to %s\n", filename, client->hostname);
794 p_size = sizeof(*pdu) + strlen(filename) + 1;
795 pdu = malloc(p_size);
796 memset(pdu, 0, p_size);
797 pdu->name_len = strlen(filename);
798 strcpy((char *) pdu->file, filename);
799 pdu->client_type = cpu_to_le16((uint16_t) client->type);
801 client->sent_job = true;
802 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_LOAD_FILE, pdu, p_size,NULL, NULL);
808 * Send file contents to server backend. We could use sendfile(), but to remain
809 * more portable lets just read/write the darn thing.
811 static int __fio_client_send_local_ini(struct fio_client *client,
812 const char *filename)
814 struct cmd_job_pdu *pdu;
822 dprint(FD_NET, "send ini %s to %s\n", filename, client->hostname);
824 fd = open(filename, O_RDONLY);
827 log_err("fio: job file <%s> open: %s\n", filename, strerror(errno));
831 if (fstat(fd, &sb) < 0) {
833 log_err("fio: job file stat: %s\n", strerror(errno));
839 * Add extra space for variable expansion, but doesn't guarantee.
841 sb.st_size += OPT_LEN_MAX;
842 p_size = sb.st_size + sizeof(*pdu);
843 pdu = malloc(p_size);
848 if (read_ini_data(fd, p, len)) {
849 log_err("fio: failed reading job file %s\n", filename);
855 pdu->buf_len = __cpu_to_le32(sb.st_size);
856 pdu->client_type = cpu_to_le32(client->type);
858 client->sent_job = true;
859 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, pdu, p_size, NULL, NULL);
865 int fio_client_send_ini(struct fio_client *client, const char *filename,
871 ret = __fio_client_send_local_ini(client, filename);
873 ret = __fio_client_send_remote_ini(client, filename);
876 client->sent_job = true;
881 static int fio_client_send_cf(struct fio_client *client,
882 struct client_file *cf)
884 return fio_client_send_ini(client, cf->file, cf->remote);
887 int fio_clients_send_ini(const char *filename)
889 struct fio_client *client;
890 struct flist_head *entry, *tmp;
892 flist_for_each_safe(entry, tmp, &client_list) {
895 client = flist_entry(entry, struct fio_client, list);
897 if (client->nr_files) {
900 for (i = 0; i < client->nr_files; i++) {
901 struct client_file *cf;
903 cf = &client->files[i];
905 if (fio_client_send_cf(client, cf)) {
907 remove_client(client);
912 if (client->sent_job || failed)
914 if (!filename || fio_client_send_ini(client, filename, 0))
915 remove_client(client);
921 int fio_client_update_options(struct fio_client *client,
922 struct thread_options *o, uint64_t *tag)
924 struct cmd_add_job_pdu pdu;
926 pdu.thread_number = cpu_to_le32(client->thread_number);
927 pdu.groupid = cpu_to_le32(client->groupid);
928 convert_thread_options_to_net(&pdu.top, o);
930 return fio_net_send_cmd(client->fd, FIO_NET_CMD_UPDATE_JOB, &pdu, sizeof(pdu), tag, &client->cmd_list);
933 static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
935 dst->max_val = le64_to_cpu(src->max_val);
936 dst->min_val = le64_to_cpu(src->min_val);
937 dst->samples = le64_to_cpu(src->samples);
940 * Floats arrive as IEEE 754 encoded uint64_t, convert back to double
942 dst->mean.u.f = fio_uint64_to_double(le64_to_cpu(dst->mean.u.i));
943 dst->S.u.f = fio_uint64_to_double(le64_to_cpu(dst->S.u.i));
946 static void convert_ts(struct thread_stat *dst, struct thread_stat *src)
950 dst->error = le32_to_cpu(src->error);
951 dst->thread_number = le32_to_cpu(src->thread_number);
952 dst->groupid = le32_to_cpu(src->groupid);
953 dst->pid = le32_to_cpu(src->pid);
954 dst->members = le32_to_cpu(src->members);
955 dst->unified_rw_rep = le32_to_cpu(src->unified_rw_rep);
957 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
958 convert_io_stat(&dst->clat_stat[i], &src->clat_stat[i]);
959 convert_io_stat(&dst->slat_stat[i], &src->slat_stat[i]);
960 convert_io_stat(&dst->lat_stat[i], &src->lat_stat[i]);
961 convert_io_stat(&dst->bw_stat[i], &src->bw_stat[i]);
962 convert_io_stat(&dst->iops_stat[i], &src->iops_stat[i]);
964 convert_io_stat(&dst->sync_stat, &src->sync_stat);
966 dst->usr_time = le64_to_cpu(src->usr_time);
967 dst->sys_time = le64_to_cpu(src->sys_time);
968 dst->ctx = le64_to_cpu(src->ctx);
969 dst->minf = le64_to_cpu(src->minf);
970 dst->majf = le64_to_cpu(src->majf);
971 dst->clat_percentiles = le32_to_cpu(src->clat_percentiles);
972 dst->lat_percentiles = le32_to_cpu(src->lat_percentiles);
973 dst->slat_percentiles = le32_to_cpu(src->slat_percentiles);
974 dst->percentile_precision = le64_to_cpu(src->percentile_precision);
976 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
977 fio_fp64_t *fps = &src->percentile_list[i];
978 fio_fp64_t *fpd = &dst->percentile_list[i];
980 fpd->u.f = fio_uint64_to_double(le64_to_cpu(fps->u.i));
983 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
984 dst->io_u_map[i] = le64_to_cpu(src->io_u_map[i]);
985 dst->io_u_submit[i] = le64_to_cpu(src->io_u_submit[i]);
986 dst->io_u_complete[i] = le64_to_cpu(src->io_u_complete[i]);
989 for (i = 0; i < FIO_IO_U_LAT_N_NR; i++)
990 dst->io_u_lat_n[i] = le64_to_cpu(src->io_u_lat_n[i]);
991 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++)
992 dst->io_u_lat_u[i] = le64_to_cpu(src->io_u_lat_u[i]);
993 for (i = 0; i < FIO_IO_U_LAT_M_NR; i++)
994 dst->io_u_lat_m[i] = le64_to_cpu(src->io_u_lat_m[i]);
996 for (i = 0; i < FIO_LAT_CNT; i++)
997 for (j = 0; j < DDIR_RWDIR_CNT; j++)
998 for (k = 0; k < FIO_IO_U_PLAT_NR; k++)
999 dst->io_u_plat[i][j][k] = le64_to_cpu(src->io_u_plat[i][j][k]);
1001 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
1002 dst->io_u_sync_plat[j] = le64_to_cpu(src->io_u_sync_plat[j]);
1004 for (i = 0; i < DDIR_RWDIR_SYNC_CNT; i++)
1005 dst->total_io_u[i] = le64_to_cpu(src->total_io_u[i]);
1007 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1008 dst->short_io_u[i] = le64_to_cpu(src->short_io_u[i]);
1009 dst->drop_io_u[i] = le64_to_cpu(src->drop_io_u[i]);
1012 dst->total_submit = le64_to_cpu(src->total_submit);
1013 dst->total_complete = le64_to_cpu(src->total_complete);
1014 dst->nr_zone_resets = le64_to_cpu(src->nr_zone_resets);
1016 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1017 dst->io_bytes[i] = le64_to_cpu(src->io_bytes[i]);
1018 dst->runtime[i] = le64_to_cpu(src->runtime[i]);
1021 dst->total_run_time = le64_to_cpu(src->total_run_time);
1022 dst->continue_on_error = le16_to_cpu(src->continue_on_error);
1023 dst->total_err_count = le64_to_cpu(src->total_err_count);
1024 dst->first_error = le32_to_cpu(src->first_error);
1025 dst->kb_base = le32_to_cpu(src->kb_base);
1026 dst->unit_base = le32_to_cpu(src->unit_base);
1028 dst->sig_figs = le32_to_cpu(src->sig_figs);
1030 dst->latency_depth = le32_to_cpu(src->latency_depth);
1031 dst->latency_target = le64_to_cpu(src->latency_target);
1032 dst->latency_window = le64_to_cpu(src->latency_window);
1033 dst->latency_percentile.u.f = fio_uint64_to_double(le64_to_cpu(src->latency_percentile.u.i));
1035 dst->nr_block_infos = le64_to_cpu(src->nr_block_infos);
1036 for (i = 0; i < dst->nr_block_infos; i++)
1037 dst->block_infos[i] = le32_to_cpu(src->block_infos[i]);
1038 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1039 for (j = 0; j < FIO_IO_U_PLAT_NR; j++) {
1040 dst->io_u_plat_high_prio[i][j] = le64_to_cpu(src->io_u_plat_high_prio[i][j]);
1041 dst->io_u_plat_low_prio[i][j] = le64_to_cpu(src->io_u_plat_low_prio[i][j]);
1043 convert_io_stat(&dst->clat_high_prio_stat[i], &src->clat_high_prio_stat[i]);
1044 convert_io_stat(&dst->clat_low_prio_stat[i], &src->clat_low_prio_stat[i]);
1047 dst->ss_dur = le64_to_cpu(src->ss_dur);
1048 dst->ss_state = le32_to_cpu(src->ss_state);
1049 dst->ss_head = le32_to_cpu(src->ss_head);
1050 dst->ss_limit.u.f = fio_uint64_to_double(le64_to_cpu(src->ss_limit.u.i));
1051 dst->ss_slope.u.f = fio_uint64_to_double(le64_to_cpu(src->ss_slope.u.i));
1052 dst->ss_deviation.u.f = fio_uint64_to_double(le64_to_cpu(src->ss_deviation.u.i));
1053 dst->ss_criterion.u.f = fio_uint64_to_double(le64_to_cpu(src->ss_criterion.u.i));
1055 if (dst->ss_state & FIO_SS_DATA) {
1056 for (i = 0; i < dst->ss_dur; i++ ) {
1057 dst->ss_iops_data[i] = le64_to_cpu(src->ss_iops_data[i]);
1058 dst->ss_bw_data[i] = le64_to_cpu(src->ss_bw_data[i]);
1062 dst->cachehit = le64_to_cpu(src->cachehit);
1063 dst->cachemiss = le64_to_cpu(src->cachemiss);
1066 static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
1070 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1071 dst->max_run[i] = le64_to_cpu(src->max_run[i]);
1072 dst->min_run[i] = le64_to_cpu(src->min_run[i]);
1073 dst->max_bw[i] = le64_to_cpu(src->max_bw[i]);
1074 dst->min_bw[i] = le64_to_cpu(src->min_bw[i]);
1075 dst->iobytes[i] = le64_to_cpu(src->iobytes[i]);
1076 dst->agg[i] = le64_to_cpu(src->agg[i]);
1079 dst->kb_base = le32_to_cpu(src->kb_base);
1080 dst->unit_base = le32_to_cpu(src->unit_base);
1081 dst->sig_figs = le32_to_cpu(src->sig_figs);
1082 dst->groupid = le32_to_cpu(src->groupid);
1083 dst->unified_rw_rep = le32_to_cpu(src->unified_rw_rep);
1086 static void json_object_add_client_info(struct json_object *obj,
1087 struct fio_client *client)
1089 const char *hostname = client->hostname ? client->hostname : "";
1091 json_object_add_value_string(obj, "hostname", hostname);
1092 json_object_add_value_int(obj, "port", client->port);
1095 static void handle_ts(struct fio_client *client, struct fio_net_cmd *cmd)
1097 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
1098 struct flist_head *opt_list = NULL;
1099 struct json_object *tsobj;
1101 if (client->opt_lists && p->ts.thread_number <= client->jobs)
1102 opt_list = &client->opt_lists[p->ts.thread_number - 1];
1104 tsobj = show_thread_status(&p->ts, &p->rs, opt_list, &client->buf);
1105 client->did_stat = true;
1107 json_object_add_client_info(tsobj, client);
1108 json_array_add_value_object(clients_array, tsobj);
1111 if (sum_stat_clients <= 1)
1114 sum_thread_stats(&client_ts, &p->ts, sum_stat_nr == 1);
1115 sum_group_stats(&client_gs, &p->rs);
1117 client_ts.members++;
1118 client_ts.thread_number = p->ts.thread_number;
1119 client_ts.groupid = p->ts.groupid;
1120 client_ts.unified_rw_rep = p->ts.unified_rw_rep;
1121 client_ts.sig_figs = p->ts.sig_figs;
1123 if (++sum_stat_nr == sum_stat_clients) {
1124 strcpy(client_ts.name, "All clients");
1125 tsobj = show_thread_status(&client_ts, &client_gs, NULL, &allclients);
1127 json_object_add_client_info(tsobj, client);
1128 json_array_add_value_object(clients_array, tsobj);
1133 static void handle_gs(struct fio_client *client, struct fio_net_cmd *cmd)
1135 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
1137 if (output_format & FIO_OUTPUT_NORMAL)
1138 show_group_stats(gs, &client->buf);
1141 static void handle_job_opt(struct fio_client *client, struct fio_net_cmd *cmd)
1143 struct cmd_job_option *pdu = (struct cmd_job_option *) cmd->payload;
1145 pdu->global = le16_to_cpu(pdu->global);
1146 pdu->truncated = le16_to_cpu(pdu->truncated);
1147 pdu->groupid = le32_to_cpu(pdu->groupid);
1150 if (!job_opt_object)
1153 json_object_add_value_string(job_opt_object,
1154 (const char *)pdu->name,
1155 (const char *)pdu->value);
1156 } else if (client->opt_lists) {
1157 struct flist_head *opt_list = &client->opt_lists[pdu->groupid];
1158 struct print_option *p;
1160 p = malloc(sizeof(*p));
1161 p->name = strdup((const char *)pdu->name);
1162 p->value = pdu->value[0] ? strdup((const char *)pdu->value) :
1164 flist_add_tail(&p->list, opt_list);
1168 static void handle_text(struct fio_client *client, struct fio_net_cmd *cmd)
1170 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
1171 const char *buf = (const char *) pdu->buf;
1174 struct buf_output out;
1176 buf_output_init(&out);
1178 name = client->name ? client->name : client->hostname;
1180 if (!client->skip_newline && !(output_format & FIO_OUTPUT_TERSE))
1181 __log_buf(&out, "<%s> ", name);
1182 __log_buf(&out, "%s", buf);
1183 log_info_buf(out.buf, out.buflen);
1184 buf_output_free(&out);
1185 client->skip_newline = strchr(buf, '\n') == NULL;
1188 static void convert_agg(struct disk_util_agg *agg)
1192 for (i = 0; i < 2; i++) {
1193 agg->ios[i] = le64_to_cpu(agg->ios[i]);
1194 agg->merges[i] = le64_to_cpu(agg->merges[i]);
1195 agg->sectors[i] = le64_to_cpu(agg->sectors[i]);
1196 agg->ticks[i] = le64_to_cpu(agg->ticks[i]);
1199 agg->io_ticks = le64_to_cpu(agg->io_ticks);
1200 agg->time_in_queue = le64_to_cpu(agg->time_in_queue);
1201 agg->slavecount = le32_to_cpu(agg->slavecount);
1202 agg->max_util.u.f = fio_uint64_to_double(le64_to_cpu(agg->max_util.u.i));
1205 static void convert_dus(struct disk_util_stat *dus)
1209 for (i = 0; i < 2; i++) {
1210 dus->s.ios[i] = le64_to_cpu(dus->s.ios[i]);
1211 dus->s.merges[i] = le64_to_cpu(dus->s.merges[i]);
1212 dus->s.sectors[i] = le64_to_cpu(dus->s.sectors[i]);
1213 dus->s.ticks[i] = le64_to_cpu(dus->s.ticks[i]);
1216 dus->s.io_ticks = le64_to_cpu(dus->s.io_ticks);
1217 dus->s.time_in_queue = le64_to_cpu(dus->s.time_in_queue);
1218 dus->s.msec = le64_to_cpu(dus->s.msec);
1221 static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd)
1223 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
1225 if (!client->disk_stats_shown)
1226 client->disk_stats_shown = true;
1228 if (output_format & FIO_OUTPUT_JSON) {
1229 struct json_object *duobj;
1231 json_array_add_disk_util(&du->dus, &du->agg, du_array);
1232 duobj = json_array_last_value_object(du_array);
1233 json_object_add_client_info(duobj, client);
1235 if (output_format & FIO_OUTPUT_NORMAL) {
1236 __log_buf(&client->buf, "\nDisk stats (read/write):\n");
1237 print_disk_util(&du->dus, &du->agg, 0, &client->buf);
1239 if (output_format & FIO_OUTPUT_TERSE && terse_version >= 3) {
1240 print_disk_util(&du->dus, &du->agg, 1, &client->buf);
1241 __log_buf(&client->buf, "\n");
1245 static void convert_jobs_eta(struct jobs_eta *je)
1249 je->nr_running = le32_to_cpu(je->nr_running);
1250 je->nr_ramp = le32_to_cpu(je->nr_ramp);
1251 je->nr_pending = le32_to_cpu(je->nr_pending);
1252 je->nr_setting_up = le32_to_cpu(je->nr_setting_up);
1253 je->files_open = le32_to_cpu(je->files_open);
1255 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1256 je->m_rate[i] = le64_to_cpu(je->m_rate[i]);
1257 je->t_rate[i] = le64_to_cpu(je->t_rate[i]);
1258 je->m_iops[i] = le32_to_cpu(je->m_iops[i]);
1259 je->t_iops[i] = le32_to_cpu(je->t_iops[i]);
1260 je->rate[i] = le64_to_cpu(je->rate[i]);
1261 je->iops[i] = le32_to_cpu(je->iops[i]);
1264 je->elapsed_sec = le64_to_cpu(je->elapsed_sec);
1265 je->eta_sec = le64_to_cpu(je->eta_sec);
1266 je->nr_threads = le32_to_cpu(je->nr_threads);
1267 je->is_pow2 = le32_to_cpu(je->is_pow2);
1268 je->unit_base = le32_to_cpu(je->unit_base);
1269 je->sig_figs = le32_to_cpu(je->sig_figs);
1272 void fio_client_sum_jobs_eta(struct jobs_eta *dst, struct jobs_eta *je)
1276 dst->nr_running += je->nr_running;
1277 dst->nr_ramp += je->nr_ramp;
1278 dst->nr_pending += je->nr_pending;
1279 dst->nr_setting_up += je->nr_setting_up;
1280 dst->files_open += je->files_open;
1282 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1283 dst->m_rate[i] += je->m_rate[i];
1284 dst->t_rate[i] += je->t_rate[i];
1285 dst->m_iops[i] += je->m_iops[i];
1286 dst->t_iops[i] += je->t_iops[i];
1287 dst->rate[i] += je->rate[i];
1288 dst->iops[i] += je->iops[i];
1291 dst->elapsed_sec += je->elapsed_sec;
1293 if (je->eta_sec > dst->eta_sec)
1294 dst->eta_sec = je->eta_sec;
1296 dst->nr_threads += je->nr_threads;
1299 * This wont be correct for multiple strings, but at least it
1300 * works for the basic cases.
1302 strcpy((char *) dst->run_str, (char *) je->run_str);
1305 static bool remove_reply_cmd(struct fio_client *client, struct fio_net_cmd *cmd)
1307 struct fio_net_cmd_reply *reply = NULL;
1308 struct flist_head *entry;
1310 flist_for_each(entry, &client->cmd_list) {
1311 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
1313 if (cmd->tag == (uintptr_t) reply)
1320 log_err("fio: client: unable to find matching tag (%llx)\n", (unsigned long long) cmd->tag);
1324 flist_del(&reply->list);
1325 cmd->tag = reply->saved_tag;
1330 int fio_client_wait_for_reply(struct fio_client *client, uint64_t tag)
1333 struct fio_net_cmd_reply *reply = NULL;
1334 struct flist_head *entry;
1336 flist_for_each(entry, &client->cmd_list) {
1337 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
1339 if (tag == (uintptr_t) reply)
1354 static void handle_eta(struct fio_client *client, struct fio_net_cmd *cmd)
1356 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
1357 struct client_eta *eta = (struct client_eta *) (uintptr_t) cmd->tag;
1359 dprint(FD_NET, "client: got eta tag %p, %d\n", eta, eta->pending);
1361 assert(client->eta_in_flight == eta);
1363 client->eta_in_flight = NULL;
1364 flist_del_init(&client->eta_list);
1365 client->eta_timeouts = 0;
1367 if (client->ops->jobs_eta)
1368 client->ops->jobs_eta(client, je);
1370 fio_client_sum_jobs_eta(&eta->eta, je);
1371 fio_client_dec_jobs_eta(eta, client->ops->eta);
1374 static void client_flush_hist_samples(FILE *f, int hist_coarseness, void *samples,
1375 uint64_t sample_size)
1377 struct io_sample *s;
1379 uint64_t i, j, nr_samples;
1380 struct io_u_plat_entry *entry;
1381 uint64_t *io_u_plat;
1383 int stride = 1 << hist_coarseness;
1388 s = __get_sample(samples, 0, 0);
1389 log_offset = (s->__ddir & LOG_OFFSET_SAMPLE_BIT) != 0;
1391 nr_samples = sample_size / __log_entry_sz(log_offset);
1393 for (i = 0; i < nr_samples; i++) {
1395 s = (struct io_sample *)((char *)__get_sample(samples, log_offset, i) +
1396 i * sizeof(struct io_u_plat_entry));
1398 entry = s->data.plat_entry;
1399 io_u_plat = entry->io_u_plat;
1401 fprintf(f, "%lu, %u, %llu, ", (unsigned long) s->time,
1402 io_sample_ddir(s), (unsigned long long) s->bs);
1403 for (j = 0; j < FIO_IO_U_PLAT_NR - stride; j += stride) {
1404 fprintf(f, "%llu, ", (unsigned long long)hist_sum(j, stride, io_u_plat, NULL));
1406 fprintf(f, "%llu\n", (unsigned long long)
1407 hist_sum(FIO_IO_U_PLAT_NR - stride, stride, io_u_plat, NULL));
1412 static int fio_client_handle_iolog(struct fio_client *client,
1413 struct fio_net_cmd *cmd)
1415 struct cmd_iolog_pdu *pdu = NULL;
1417 char *log_pathname = NULL;
1420 pdu = convert_iolog(cmd, &store_direct);
1422 log_err("fio: failed converting IO log\n");
1427 /* allocate buffer big enough for next sprintf() call */
1428 log_pathname = malloc(10 + strlen((char *)pdu->name) +
1429 strlen(client->hostname));
1430 if (!log_pathname) {
1431 log_err("fio: memory allocation of unique pathname failed\n");
1435 /* generate a unique pathname for the log file using hostname */
1436 sprintf(log_pathname, "%s.%s", pdu->name, client->hostname);
1443 fd = open((const char *) log_pathname,
1444 O_WRONLY | O_CREAT | O_TRUNC, 0644);
1446 log_err("fio: open log %s: %s\n",
1447 log_pathname, strerror(errno));
1452 sz = cmd->pdu_len - sizeof(*pdu);
1453 wrote = write(fd, pdu->samples, sz);
1457 log_err("fio: short write on compressed log\n");
1465 f = fopen((const char *) log_pathname, "w");
1467 log_err("fio: fopen log %s : %s\n",
1468 log_pathname, strerror(errno));
1473 if (pdu->log_type == IO_LOG_TYPE_HIST) {
1474 client_flush_hist_samples(f, pdu->log_hist_coarseness, pdu->samples,
1475 pdu->nr_samples * sizeof(struct io_sample));
1477 flush_samples(f, pdu->samples,
1478 pdu->nr_samples * sizeof(struct io_sample));
1485 if (pdu && pdu != (void *) cmd->payload)
1494 static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd)
1496 struct cmd_probe_reply_pdu *probe = (struct cmd_probe_reply_pdu *) cmd->payload;
1497 const char *os, *arch;
1500 os = fio_get_os_string(probe->os);
1504 arch = fio_get_arch_string(probe->arch);
1508 sprintf(bit, "%d-bit", probe->bpp * 8);
1509 probe->flags = le64_to_cpu(probe->flags);
1511 if (output_format & FIO_OUTPUT_NORMAL) {
1512 log_info("hostname=%s, be=%u, %s, os=%s, arch=%s, fio=%s, flags=%lx\n",
1513 probe->hostname, probe->bigendian, bit, os, arch,
1514 probe->fio_version, (unsigned long) probe->flags);
1518 client->name = strdup((char *) probe->hostname);
1521 static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd)
1523 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
1525 client->state = Client_started;
1526 client->jobs = le32_to_cpu(pdu->jobs);
1527 client->nr_stat = le32_to_cpu(pdu->stat_outputs);
1532 if (client->opt_lists)
1533 free(client->opt_lists);
1535 client->opt_lists = malloc(client->jobs * sizeof(struct flist_head));
1536 for (i = 0; i < client->jobs; i++)
1537 INIT_FLIST_HEAD(&client->opt_lists[i]);
1540 sum_stat_clients += client->nr_stat;
1543 static void handle_stop(struct fio_client *client)
1546 log_info("client <%s>: exited with error %d\n", client->hostname, client->error);
1549 static void convert_stop(struct fio_net_cmd *cmd)
1551 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
1553 pdu->error = le32_to_cpu(pdu->error);
1556 static void convert_text(struct fio_net_cmd *cmd)
1558 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
1560 pdu->level = le32_to_cpu(pdu->level);
1561 pdu->buf_len = le32_to_cpu(pdu->buf_len);
1562 pdu->log_sec = le64_to_cpu(pdu->log_sec);
1563 pdu->log_usec = le64_to_cpu(pdu->log_usec);
1566 static struct cmd_iolog_pdu *convert_iolog_gz(struct fio_net_cmd *cmd,
1567 struct cmd_iolog_pdu *pdu)
1570 struct cmd_iolog_pdu *ret;
1572 uint64_t nr_samples;
1576 stream.zalloc = Z_NULL;
1577 stream.zfree = Z_NULL;
1578 stream.opaque = Z_NULL;
1579 stream.avail_in = 0;
1580 stream.next_in = Z_NULL;
1582 if (inflateInit(&stream) != Z_OK)
1586 * Get header first, it's not compressed
1588 nr_samples = le64_to_cpu(pdu->nr_samples);
1590 if (pdu->log_type == IO_LOG_TYPE_HIST)
1591 total = nr_samples * (__log_entry_sz(le32_to_cpu(pdu->log_offset)) +
1592 sizeof(struct io_u_plat_entry));
1594 total = nr_samples * __log_entry_sz(le32_to_cpu(pdu->log_offset));
1595 ret = malloc(total + sizeof(*pdu));
1596 ret->nr_samples = nr_samples;
1598 memcpy(ret, pdu, sizeof(*pdu));
1600 p = (char *) ret + sizeof(*pdu);
1602 stream.avail_in = cmd->pdu_len - sizeof(*pdu);
1603 stream.next_in = (void *)((char *) pdu + sizeof(*pdu));
1604 while (stream.avail_in) {
1605 unsigned int this_chunk = 65536;
1606 unsigned int this_len;
1609 if (this_chunk > total)
1612 stream.avail_out = this_chunk;
1613 stream.next_out = (void *)p;
1614 err = inflate(&stream, Z_NO_FLUSH);
1615 /* may be Z_OK, or Z_STREAM_END */
1618 * Z_STREAM_ERROR and Z_BUF_ERROR can safely be
1620 if (err == Z_STREAM_ERROR || err == Z_BUF_ERROR)
1622 log_err("fio: inflate error %d\n", err);
1628 this_len = this_chunk - stream.avail_out;
1634 inflateEnd(&stream);
1642 * This has been compressed on the server side, since it can be big.
1645 static struct cmd_iolog_pdu *convert_iolog(struct fio_net_cmd *cmd,
1648 struct cmd_iolog_pdu *pdu = (struct cmd_iolog_pdu *) cmd->payload;
1649 struct cmd_iolog_pdu *ret;
1654 *store_direct = false;
1657 * Convert if compressed and we support it. If it's not
1658 * compressed, we need not do anything.
1660 compressed = le32_to_cpu(pdu->compressed);
1661 if (compressed == XMIT_COMPRESSED) {
1663 log_err("fio: server sent compressed data by mistake\n");
1666 ret = convert_iolog_gz(cmd, pdu);
1668 log_err("fio: failed decompressing log\n");
1671 } else if (compressed == STORE_COMPRESSED) {
1672 *store_direct = true;
1677 ret->nr_samples = le64_to_cpu(ret->nr_samples);
1678 ret->thread_number = le32_to_cpu(ret->thread_number);
1679 ret->log_type = le32_to_cpu(ret->log_type);
1680 ret->compressed = le32_to_cpu(ret->compressed);
1681 ret->log_offset = le32_to_cpu(ret->log_offset);
1682 ret->log_hist_coarseness = le32_to_cpu(ret->log_hist_coarseness);
1687 samples = &ret->samples[0];
1688 for (i = 0; i < ret->nr_samples; i++) {
1689 struct io_sample *s;
1691 s = __get_sample(samples, ret->log_offset, i);
1692 if (ret->log_type == IO_LOG_TYPE_HIST)
1693 s = (struct io_sample *)((char *)s + sizeof(struct io_u_plat_entry) * i);
1695 s->time = le64_to_cpu(s->time);
1696 s->data.val = le64_to_cpu(s->data.val);
1697 s->__ddir = __le32_to_cpu(s->__ddir);
1698 s->bs = le64_to_cpu(s->bs);
1700 if (ret->log_offset) {
1701 struct io_sample_offset *so = (void *) s;
1703 so->offset = le64_to_cpu(so->offset);
1706 if (ret->log_type == IO_LOG_TYPE_HIST) {
1707 s->data.plat_entry = (struct io_u_plat_entry *)(((char *)s) + sizeof(*s));
1708 s->data.plat_entry->list.next = NULL;
1709 s->data.plat_entry->list.prev = NULL;
1716 static void sendfile_reply(int fd, struct cmd_sendfile_reply *rep,
1717 size_t size, uint64_t tag)
1719 rep->error = cpu_to_le32(rep->error);
1720 fio_net_send_cmd(fd, FIO_NET_CMD_SENDFILE, rep, size, &tag, NULL);
1723 static int fio_send_file(struct fio_client *client, struct cmd_sendfile *pdu,
1726 struct cmd_sendfile_reply *rep;
1731 size = sizeof(*rep);
1734 if (stat((char *)pdu->path, &sb) < 0) {
1737 sendfile_reply(client->fd, rep, size, tag);
1743 rep = realloc(rep, size);
1744 rep->size = cpu_to_le32((uint32_t) sb.st_size);
1746 fd = open((char *)pdu->path, O_RDONLY);
1750 rep->error = read_data(fd, &rep->data, sb.st_size);
1751 sendfile_reply(client->fd, rep, size, tag);
1757 int fio_handle_client(struct fio_client *client)
1759 struct client_ops *ops = client->ops;
1760 struct fio_net_cmd *cmd;
1763 dprint(FD_NET, "client: handle %s\n", client->hostname);
1765 cmd = fio_net_recv_cmd(client->fd, true);
1769 dprint(FD_NET, "client: got cmd op %s from %s (pdu=%u)\n",
1770 fio_server_op(cmd->opcode), client->hostname, cmd->pdu_len);
1772 client->last_cmd = cmd->opcode;
1774 switch (cmd->opcode) {
1775 case FIO_NET_CMD_QUIT:
1777 ops->quit(client, cmd);
1778 remove_client(client);
1780 case FIO_NET_CMD_TEXT:
1782 ops->text(client, cmd);
1784 case FIO_NET_CMD_DU: {
1785 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
1787 convert_dus(&du->dus);
1788 convert_agg(&du->agg);
1790 ops->disk_util(client, cmd);
1793 case FIO_NET_CMD_TS: {
1794 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
1796 dprint(FD_NET, "client: ts->ss_state = %u\n", (unsigned int) le32_to_cpu(p->ts.ss_state));
1797 if (le32_to_cpu(p->ts.ss_state) & FIO_SS_DATA) {
1798 dprint(FD_NET, "client: received steadystate ring buffers\n");
1800 size = le64_to_cpu(p->ts.ss_dur);
1801 p->ts.ss_iops_data = (uint64_t *) ((struct cmd_ts_pdu *)cmd->payload + 1);
1802 p->ts.ss_bw_data = p->ts.ss_iops_data + size;
1805 convert_ts(&p->ts, &p->ts);
1806 convert_gs(&p->rs, &p->rs);
1808 ops->thread_status(client, cmd);
1811 case FIO_NET_CMD_GS: {
1812 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
1816 ops->group_stats(client, cmd);
1819 case FIO_NET_CMD_ETA: {
1820 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
1822 if (!remove_reply_cmd(client, cmd))
1824 convert_jobs_eta(je);
1825 handle_eta(client, cmd);
1828 case FIO_NET_CMD_PROBE:
1829 remove_reply_cmd(client, cmd);
1830 ops->probe(client, cmd);
1832 case FIO_NET_CMD_SERVER_START:
1833 client->state = Client_running;
1835 ops->job_start(client, cmd);
1837 case FIO_NET_CMD_START: {
1838 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
1840 pdu->jobs = le32_to_cpu(pdu->jobs);
1841 ops->start(client, cmd);
1844 case FIO_NET_CMD_STOP: {
1845 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
1848 client->state = Client_stopped;
1849 client->error = le32_to_cpu(pdu->error);
1850 client->signal = le32_to_cpu(pdu->signal);
1854 case FIO_NET_CMD_ADD_JOB: {
1855 struct cmd_add_job_pdu *pdu = (struct cmd_add_job_pdu *) cmd->payload;
1857 client->thread_number = le32_to_cpu(pdu->thread_number);
1858 client->groupid = le32_to_cpu(pdu->groupid);
1861 ops->add_job(client, cmd);
1864 case FIO_NET_CMD_IOLOG:
1865 fio_client_handle_iolog(client, cmd);
1867 case FIO_NET_CMD_UPDATE_JOB:
1868 ops->update_job(client, cmd);
1869 remove_reply_cmd(client, cmd);
1871 case FIO_NET_CMD_VTRIGGER: {
1872 struct all_io_list *pdu = (struct all_io_list *) cmd->payload;
1877 strcpy(buf, aux_path);
1881 __verify_save_state(pdu, server_name(client, &buf[off], sizeof(buf) - off));
1882 exec_trigger(trigger_cmd);
1885 case FIO_NET_CMD_SENDFILE: {
1886 struct cmd_sendfile *pdu = (struct cmd_sendfile *) cmd->payload;
1887 fio_send_file(client, pdu, cmd->tag);
1890 case FIO_NET_CMD_JOB_OPT: {
1891 handle_job_opt(client, cmd);
1895 log_err("fio: unknown client op: %s\n", fio_server_op(cmd->opcode));
1903 int fio_clients_send_trigger(const char *cmd)
1905 struct flist_head *entry;
1906 struct fio_client *client;
1909 dprint(FD_NET, "client: send vtrigger: %s\n", cmd);
1916 flist_for_each(entry, &client_list) {
1917 struct cmd_vtrigger_pdu *pdu;
1919 client = flist_entry(entry, struct fio_client, list);
1921 pdu = malloc(sizeof(*pdu) + slen);
1922 pdu->len = cpu_to_le16((uint16_t) slen);
1924 memcpy(pdu->cmd, cmd, slen);
1925 fio_net_send_cmd(client->fd, FIO_NET_CMD_VTRIGGER, pdu,
1926 sizeof(*pdu) + slen, NULL, NULL);
1933 static void request_client_etas(struct client_ops *ops)
1935 struct fio_client *client;
1936 struct flist_head *entry;
1937 struct client_eta *eta;
1940 if (eta_print == FIO_ETA_NEVER)
1943 dprint(FD_NET, "client: request eta (%d)\n", nr_clients);
1945 eta = calloc(1, sizeof(*eta) + __THREAD_RUNSTR_SZ(REAL_MAX_JOBS));
1946 eta->pending = nr_clients;
1948 flist_for_each(entry, &client_list) {
1949 client = flist_entry(entry, struct fio_client, list);
1951 if (!flist_empty(&client->eta_list)) {
1955 if (client->state != Client_running)
1958 assert(!client->eta_in_flight);
1959 flist_add_tail(&client->eta_list, &eta_list);
1960 client->eta_in_flight = eta;
1961 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_SEND_ETA,
1962 (uintptr_t) eta, &client->cmd_list);
1966 if (!fio_client_dec_jobs_eta(eta, ops->eta))
1970 dprint(FD_NET, "client: requested eta tag %p\n", eta);
1974 * A single SEND_ETA timeout isn't fatal. Attempt to recover.
1976 static int handle_cmd_timeout(struct fio_client *client,
1977 struct fio_net_cmd_reply *reply)
1979 uint16_t reply_opcode = reply->opcode;
1981 flist_del(&reply->list);
1984 if (reply_opcode != FIO_NET_CMD_SEND_ETA)
1987 log_info("client <%s>: timeout on SEND_ETA\n", client->hostname);
1989 flist_del_init(&client->eta_list);
1990 if (client->eta_in_flight) {
1991 fio_client_dec_jobs_eta(client->eta_in_flight, client->ops->eta);
1992 client->eta_in_flight = NULL;
1996 * If we fail 5 in a row, give up...
1998 if (client->eta_timeouts++ > 5)
2004 static int client_check_cmd_timeout(struct fio_client *client,
2005 struct timespec *now)
2007 struct fio_net_cmd_reply *reply;
2008 struct flist_head *entry, *tmp;
2011 flist_for_each_safe(entry, tmp, &client->cmd_list) {
2014 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
2016 if (mtime_since(&reply->ts, now) < FIO_NET_CLIENT_TIMEOUT)
2020 if (!handle_cmd_timeout(client, reply))
2023 log_err("fio: client %s, timeout on cmd %s\n", client->hostname,
2028 return flist_empty(&client->cmd_list) && ret;
2031 static int fio_check_clients_timed_out(void)
2033 struct fio_client *client;
2034 struct flist_head *entry, *tmp;
2038 fio_gettime(&ts, NULL);
2040 flist_for_each_safe(entry, tmp, &client_list) {
2041 client = flist_entry(entry, struct fio_client, list);
2043 if (flist_empty(&client->cmd_list))
2046 if (!client_check_cmd_timeout(client, &ts))
2049 if (client->ops->timed_out)
2050 client->ops->timed_out(client);
2052 log_err("fio: client %s timed out\n", client->hostname);
2054 if (client->last_cmd != FIO_NET_CMD_VTRIGGER)
2055 client->error = ETIMEDOUT;
2057 log_info("fio: ignoring timeout due to vtrigger\n");
2058 remove_client(client);
2065 int fio_handle_clients(struct client_ops *ops)
2067 struct pollfd *pfds;
2068 int i, ret = 0, retval = 0;
2070 fio_gettime(&eta_ts, NULL);
2072 pfds = malloc(nr_clients * sizeof(struct pollfd));
2074 init_thread_stat(&client_ts);
2075 init_group_run_stat(&client_gs);
2077 while (!exit_backend && nr_clients) {
2078 struct flist_head *entry, *tmp;
2079 struct fio_client *client;
2082 flist_for_each_safe(entry, tmp, &client_list) {
2083 client = flist_entry(entry, struct fio_client, list);
2085 if (!client->sent_job && !client->ops->stay_connected &&
2086 flist_empty(&client->cmd_list)) {
2087 remove_client(client);
2091 pfds[i].fd = client->fd;
2092 pfds[i].events = POLLIN;
2099 assert(i == nr_clients);
2105 fio_gettime(&ts, NULL);
2106 if (eta_time_within_slack(mtime_since(&eta_ts, &ts))) {
2107 request_client_etas(ops);
2108 memcpy(&eta_ts, &ts, sizeof(ts));
2110 if (fio_check_clients_timed_out())
2114 check_trigger_file();
2116 timeout = min(100u, ops->eta_msec);
2118 ret = poll(pfds, nr_clients, timeout);
2122 log_err("fio: poll clients: %s\n", strerror(errno));
2128 for (i = 0; i < nr_clients; i++) {
2129 if (!(pfds[i].revents & POLLIN))
2132 client = find_client_by_fd(pfds[i].fd);
2134 log_err("fio: unknown client fd %ld\n", (long) pfds[i].fd);
2137 if (!fio_handle_client(client)) {
2138 log_info("client: host=%s disconnected\n",
2140 remove_client(client);
2142 } else if (client->error)
2144 fio_put_client(client);
2148 log_info_buf(allclients.buf, allclients.buflen);
2149 buf_output_free(&allclients);
2151 fio_client_json_fini();
2154 return retval || error_clients;
2157 static void client_display_thread_status(struct jobs_eta *je)
2159 if (!(output_format & FIO_OUTPUT_JSON))
2160 display_thread_status(je);