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 = NULL;
289 if (fio_server_poll_fd(client->fd, POLLIN, 0))
290 cmd = fio_net_recv_cmd(client->fd, false);
294 if (cmd->opcode == FIO_NET_CMD_TEXT) {
296 client->ops->text(client, cmd);
303 static void remove_client(struct fio_client *client)
305 assert(client->refs);
307 dprint(FD_NET, "client: removed <%s>\n", client->hostname);
309 fio_drain_client_text(client);
311 if (!flist_empty(&client->list))
312 flist_del_init(&client->list);
314 fio_client_remove_hash(client);
316 if (!flist_empty(&client->eta_list)) {
317 flist_del_init(&client->eta_list);
318 fio_client_dec_jobs_eta(client->eta_in_flight, client->ops->eta);
324 if (client->ops->removed)
325 client->ops->removed(client);
328 fio_put_client(client);
331 struct fio_client *fio_get_client(struct fio_client *client)
337 static void __fio_client_add_cmd_option(struct fio_client *client,
342 index = client->argc++;
343 client->argv = realloc(client->argv, sizeof(char *) * client->argc);
344 client->argv[index] = strdup(opt);
345 dprint(FD_NET, "client: add cmd %d: %s\n", index, opt);
348 void fio_client_add_cmd_option(void *cookie, const char *opt)
350 struct fio_client *client = cookie;
351 struct flist_head *entry;
356 __fio_client_add_cmd_option(client, opt);
359 * Duplicate arguments to shared client group
361 flist_for_each(entry, &arg_list) {
362 client = flist_entry(entry, struct fio_client, arg_list);
364 __fio_client_add_cmd_option(client, opt);
368 static struct fio_client *get_new_client(void)
370 struct fio_client *client;
372 client = malloc(sizeof(*client));
373 memset(client, 0, sizeof(*client));
375 INIT_FLIST_HEAD(&client->list);
376 INIT_FLIST_HEAD(&client->hash_list);
377 INIT_FLIST_HEAD(&client->arg_list);
378 INIT_FLIST_HEAD(&client->eta_list);
379 INIT_FLIST_HEAD(&client->cmd_list);
381 buf_output_init(&client->buf);
386 struct fio_client *fio_client_add_explicit(struct client_ops *ops,
387 const char *hostname, int type,
390 struct fio_client *client;
392 client = get_new_client();
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;
412 client->hostname = strdup(hostname);
414 __fio_client_add_cmd_option(client, "fio");
416 flist_add(&client->list, &client_list);
418 dprint(FD_NET, "client: added <%s>\n", client->hostname);
425 int fio_client_add_ini_file(void *cookie, const char *ini_file, bool remote)
427 struct fio_client *client = cookie;
428 struct client_file *cf;
435 dprint(FD_NET, "client <%s>: add ini %s\n", client->hostname, ini_file);
437 new_size = (client->nr_files + 1) * sizeof(struct client_file);
438 new_files = realloc(client->files, new_size);
442 client->files = new_files;
443 cf = &client->files[client->nr_files];
444 cf->file = strdup(ini_file);
450 int fio_client_add(struct client_ops *ops, const char *hostname, void **cookie)
452 struct fio_client *existing = *cookie;
453 struct fio_client *client;
457 * We always add our "exec" name as the option, hence 1
460 if (existing->argc == 1)
461 flist_add_tail(&existing->arg_list, &arg_list);
463 while (!flist_empty(&arg_list))
464 flist_del_init(arg_list.next);
468 client = get_new_client();
470 if (fio_server_parse_string(hostname, &client->hostname,
471 &client->is_sock, &client->port,
472 &client->addr.sin_addr,
473 &client->addr6.sin6_addr,
475 fio_put_client(client);
482 client->type = ops->client_type;
484 __fio_client_add_cmd_option(client, "fio");
486 flist_add(&client->list, &client_list);
488 dprint(FD_NET, "client: added <%s>\n", client->hostname);
493 static const char *server_name(struct fio_client *client, char *buf,
499 from = inet_ntop(AF_INET6, (struct sockaddr *) &client->addr6.sin6_addr, buf, bufsize);
500 else if (client->is_sock)
503 from = inet_ntop(AF_INET, (struct sockaddr *) &client->addr.sin_addr, buf, bufsize);
508 static void probe_client(struct fio_client *client)
510 struct cmd_client_probe_pdu pdu;
515 dprint(FD_NET, "client: send probe\n");
518 pdu.flags = __le64_to_cpu(FIO_PROBE_FLAG_ZLIB);
523 sname = server_name(client, buf, sizeof(buf));
524 memset(pdu.server, 0, sizeof(pdu.server));
525 snprintf((char *) pdu.server, sizeof(pdu.server), "%s", sname);
527 fio_net_send_cmd(client->fd, FIO_NET_CMD_PROBE, &pdu, sizeof(pdu), &tag, &client->cmd_list);
530 static int fio_client_connect_ip(struct fio_client *client)
532 struct sockaddr *addr;
537 client->addr6.sin6_family = AF_INET6;
538 client->addr6.sin6_port = htons(client->port);
540 addr = (struct sockaddr *) &client->addr6;
541 socklen = sizeof(client->addr6);
543 client->addr.sin_family = AF_INET;
544 client->addr.sin_port = htons(client->port);
546 addr = (struct sockaddr *) &client->addr;
547 socklen = sizeof(client->addr);
550 fd = socket(domain, SOCK_STREAM, 0);
554 log_err("fio: socket: %s\n", strerror(errno));
558 if (connect(fd, addr, socklen) < 0) {
561 log_err("fio: connect: %s\n", strerror(errno));
562 log_err("fio: failed to connect to %s:%u\n", client->hostname,
571 static int fio_client_connect_sock(struct fio_client *client)
573 struct sockaddr_un *addr = &client->addr_un;
577 memset(addr, 0, sizeof(*addr));
578 addr->sun_family = AF_UNIX;
579 snprintf(addr->sun_path, sizeof(addr->sun_path), "%s",
582 fd = socket(AF_UNIX, SOCK_STREAM, 0);
586 log_err("fio: socket: %s\n", strerror(errno));
590 len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
591 if (connect(fd, (struct sockaddr *) addr, len) < 0) {
594 log_err("fio: connect; %s\n", strerror(errno));
602 int fio_client_connect(struct fio_client *client)
606 dprint(FD_NET, "client: connect to host %s\n", client->hostname);
609 fd = fio_client_connect_sock(client);
611 fd = fio_client_connect_ip(client);
613 dprint(FD_NET, "client: %s connected %d\n", client->hostname, fd);
619 fio_client_add_hash(client);
620 client->state = Client_connected;
622 probe_client(client);
626 int fio_client_terminate(struct fio_client *client)
628 return fio_net_send_quit(client->fd);
631 static void fio_clients_terminate(void)
633 struct flist_head *entry;
634 struct fio_client *client;
636 dprint(FD_NET, "client: terminate clients\n");
638 flist_for_each(entry, &client_list) {
639 client = flist_entry(entry, struct fio_client, list);
640 fio_client_terminate(client);
644 static void sig_int(int sig)
646 dprint(FD_NET, "client: got signal %d\n", sig);
647 fio_clients_terminate();
650 static void client_signal_handler(void)
652 struct sigaction act;
654 memset(&act, 0, sizeof(act));
655 act.sa_handler = sig_int;
656 act.sa_flags = SA_RESTART;
657 sigaction(SIGINT, &act, NULL);
659 memset(&act, 0, sizeof(act));
660 act.sa_handler = sig_int;
661 act.sa_flags = SA_RESTART;
662 sigaction(SIGTERM, &act, NULL);
664 /* Windows uses SIGBREAK as a quit signal from other applications */
666 memset(&act, 0, sizeof(act));
667 act.sa_handler = sig_int;
668 act.sa_flags = SA_RESTART;
669 sigaction(SIGBREAK, &act, NULL);
672 memset(&act, 0, sizeof(act));
673 act.sa_handler = sig_show_status;
674 act.sa_flags = SA_RESTART;
675 sigaction(SIGUSR1, &act, NULL);
678 static int send_client_cmd_line(struct fio_client *client)
680 struct cmd_single_line_pdu *cslp;
681 struct cmd_line_pdu *clp;
682 unsigned long offset;
688 dprint(FD_NET, "client: send cmdline %d\n", client->argc);
690 lens = malloc(client->argc * sizeof(unsigned int));
693 * Find out how much mem we need
695 for (i = 0, mem = 0; i < client->argc; i++) {
696 lens[i] = strlen(client->argv[i]) + 1;
701 * We need one cmd_line_pdu, and argc number of cmd_single_line_pdu
703 mem += sizeof(*clp) + (client->argc * sizeof(*cslp));
707 offset = sizeof(*clp);
709 for (i = 0; i < client->argc; i++) {
710 uint16_t arg_len = lens[i];
713 strcpy((char *) cslp->text, client->argv[i]);
714 cslp->len = cpu_to_le16(arg_len);
715 offset += sizeof(*cslp) + arg_len;
719 clp->lines = cpu_to_le16(client->argc);
720 clp->client_type = __cpu_to_le16(client->type);
721 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOBLINE, pdu, mem, NULL, NULL);
726 int fio_clients_connect(void)
728 struct fio_client *client;
729 struct flist_head *entry, *tmp;
734 WSAStartup(MAKEWORD(2, 2), &wsd);
737 dprint(FD_NET, "client: connect all\n");
739 client_signal_handler();
741 flist_for_each_safe(entry, tmp, &client_list) {
742 client = flist_entry(entry, struct fio_client, list);
744 ret = fio_client_connect(client);
746 remove_client(client);
750 if (client->argc > 1)
751 send_client_cmd_line(client);
757 int fio_start_client(struct fio_client *client)
759 dprint(FD_NET, "client: start %s\n", client->hostname);
760 return fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_RUN, 0, NULL);
763 int fio_start_all_clients(void)
765 struct fio_client *client;
766 struct flist_head *entry, *tmp;
769 dprint(FD_NET, "client: start all\n");
771 fio_client_json_init();
773 flist_for_each_safe(entry, tmp, &client_list) {
774 client = flist_entry(entry, struct fio_client, list);
776 ret = fio_start_client(client);
778 remove_client(client);
783 return flist_empty(&client_list);
786 static int __fio_client_send_remote_ini(struct fio_client *client,
787 const char *filename)
789 struct cmd_load_file_pdu *pdu;
793 dprint(FD_NET, "send remote ini %s to %s\n", filename, client->hostname);
795 p_size = sizeof(*pdu) + strlen(filename) + 1;
796 pdu = malloc(p_size);
797 memset(pdu, 0, p_size);
798 pdu->name_len = strlen(filename);
799 strcpy((char *) pdu->file, filename);
800 pdu->client_type = cpu_to_le16((uint16_t) client->type);
802 client->sent_job = true;
803 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_LOAD_FILE, pdu, p_size,NULL, NULL);
809 * Send file contents to server backend. We could use sendfile(), but to remain
810 * more portable lets just read/write the darn thing.
812 static int __fio_client_send_local_ini(struct fio_client *client,
813 const char *filename)
815 struct cmd_job_pdu *pdu;
823 dprint(FD_NET, "send ini %s to %s\n", filename, client->hostname);
825 fd = open(filename, O_RDONLY);
828 log_err("fio: job file <%s> open: %s\n", filename, strerror(errno));
832 if (fstat(fd, &sb) < 0) {
834 log_err("fio: job file stat: %s\n", strerror(errno));
840 * Add extra space for variable expansion, but doesn't guarantee.
842 sb.st_size += OPT_LEN_MAX;
843 p_size = sb.st_size + sizeof(*pdu);
844 pdu = malloc(p_size);
849 if (read_ini_data(fd, p, len)) {
850 log_err("fio: failed reading job file %s\n", filename);
856 pdu->buf_len = __cpu_to_le32(sb.st_size);
857 pdu->client_type = cpu_to_le32(client->type);
859 client->sent_job = true;
860 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, pdu, p_size, NULL, NULL);
866 int fio_client_send_ini(struct fio_client *client, const char *filename,
872 ret = __fio_client_send_local_ini(client, filename);
874 ret = __fio_client_send_remote_ini(client, filename);
877 client->sent_job = true;
882 static int fio_client_send_cf(struct fio_client *client,
883 struct client_file *cf)
885 return fio_client_send_ini(client, cf->file, cf->remote);
888 int fio_clients_send_ini(const char *filename)
890 struct fio_client *client;
891 struct flist_head *entry, *tmp;
893 flist_for_each_safe(entry, tmp, &client_list) {
896 client = flist_entry(entry, struct fio_client, list);
898 if (client->nr_files) {
901 for (i = 0; i < client->nr_files; i++) {
902 struct client_file *cf;
904 cf = &client->files[i];
906 if (fio_client_send_cf(client, cf)) {
908 remove_client(client);
913 if (client->sent_job || failed)
915 if (!filename || fio_client_send_ini(client, filename, 0))
916 remove_client(client);
922 int fio_client_update_options(struct fio_client *client,
923 struct thread_options *o, uint64_t *tag)
925 struct cmd_add_job_pdu pdu;
927 pdu.thread_number = cpu_to_le32(client->thread_number);
928 pdu.groupid = cpu_to_le32(client->groupid);
929 convert_thread_options_to_net(&pdu.top, o);
931 return fio_net_send_cmd(client->fd, FIO_NET_CMD_UPDATE_JOB, &pdu, sizeof(pdu), tag, &client->cmd_list);
934 static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
936 dst->max_val = le64_to_cpu(src->max_val);
937 dst->min_val = le64_to_cpu(src->min_val);
938 dst->samples = le64_to_cpu(src->samples);
941 * Floats arrive as IEEE 754 encoded uint64_t, convert back to double
943 dst->mean.u.f = fio_uint64_to_double(le64_to_cpu(dst->mean.u.i));
944 dst->S.u.f = fio_uint64_to_double(le64_to_cpu(dst->S.u.i));
947 static void convert_ts(struct thread_stat *dst, struct thread_stat *src)
951 dst->error = le32_to_cpu(src->error);
952 dst->thread_number = le32_to_cpu(src->thread_number);
953 dst->groupid = le32_to_cpu(src->groupid);
954 dst->pid = le32_to_cpu(src->pid);
955 dst->members = le32_to_cpu(src->members);
956 dst->unified_rw_rep = le32_to_cpu(src->unified_rw_rep);
957 dst->ioprio = le32_to_cpu(src->ioprio);
959 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
960 convert_io_stat(&dst->clat_stat[i], &src->clat_stat[i]);
961 convert_io_stat(&dst->slat_stat[i], &src->slat_stat[i]);
962 convert_io_stat(&dst->lat_stat[i], &src->lat_stat[i]);
963 convert_io_stat(&dst->bw_stat[i], &src->bw_stat[i]);
964 convert_io_stat(&dst->iops_stat[i], &src->iops_stat[i]);
966 convert_io_stat(&dst->sync_stat, &src->sync_stat);
968 dst->usr_time = le64_to_cpu(src->usr_time);
969 dst->sys_time = le64_to_cpu(src->sys_time);
970 dst->ctx = le64_to_cpu(src->ctx);
971 dst->minf = le64_to_cpu(src->minf);
972 dst->majf = le64_to_cpu(src->majf);
973 dst->clat_percentiles = le32_to_cpu(src->clat_percentiles);
974 dst->lat_percentiles = le32_to_cpu(src->lat_percentiles);
975 dst->slat_percentiles = le32_to_cpu(src->slat_percentiles);
976 dst->percentile_precision = le64_to_cpu(src->percentile_precision);
978 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
979 fio_fp64_t *fps = &src->percentile_list[i];
980 fio_fp64_t *fpd = &dst->percentile_list[i];
982 fpd->u.f = fio_uint64_to_double(le64_to_cpu(fps->u.i));
985 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
986 dst->io_u_map[i] = le64_to_cpu(src->io_u_map[i]);
987 dst->io_u_submit[i] = le64_to_cpu(src->io_u_submit[i]);
988 dst->io_u_complete[i] = le64_to_cpu(src->io_u_complete[i]);
991 for (i = 0; i < FIO_IO_U_LAT_N_NR; i++)
992 dst->io_u_lat_n[i] = le64_to_cpu(src->io_u_lat_n[i]);
993 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++)
994 dst->io_u_lat_u[i] = le64_to_cpu(src->io_u_lat_u[i]);
995 for (i = 0; i < FIO_IO_U_LAT_M_NR; i++)
996 dst->io_u_lat_m[i] = le64_to_cpu(src->io_u_lat_m[i]);
998 for (i = 0; i < FIO_LAT_CNT; i++)
999 for (j = 0; j < DDIR_RWDIR_CNT; j++)
1000 for (k = 0; k < FIO_IO_U_PLAT_NR; k++)
1001 dst->io_u_plat[i][j][k] = le64_to_cpu(src->io_u_plat[i][j][k]);
1003 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
1004 dst->io_u_sync_plat[j] = le64_to_cpu(src->io_u_sync_plat[j]);
1006 for (i = 0; i < DDIR_RWDIR_SYNC_CNT; i++)
1007 dst->total_io_u[i] = le64_to_cpu(src->total_io_u[i]);
1009 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1010 dst->short_io_u[i] = le64_to_cpu(src->short_io_u[i]);
1011 dst->drop_io_u[i] = le64_to_cpu(src->drop_io_u[i]);
1014 dst->total_submit = le64_to_cpu(src->total_submit);
1015 dst->total_complete = le64_to_cpu(src->total_complete);
1016 dst->nr_zone_resets = le64_to_cpu(src->nr_zone_resets);
1018 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1019 dst->io_bytes[i] = le64_to_cpu(src->io_bytes[i]);
1020 dst->runtime[i] = le64_to_cpu(src->runtime[i]);
1023 dst->total_run_time = le64_to_cpu(src->total_run_time);
1024 dst->continue_on_error = le16_to_cpu(src->continue_on_error);
1025 dst->total_err_count = le64_to_cpu(src->total_err_count);
1026 dst->first_error = le32_to_cpu(src->first_error);
1027 dst->kb_base = le32_to_cpu(src->kb_base);
1028 dst->unit_base = le32_to_cpu(src->unit_base);
1030 dst->sig_figs = le32_to_cpu(src->sig_figs);
1032 dst->latency_depth = le32_to_cpu(src->latency_depth);
1033 dst->latency_target = le64_to_cpu(src->latency_target);
1034 dst->latency_window = le64_to_cpu(src->latency_window);
1035 dst->latency_percentile.u.f = fio_uint64_to_double(le64_to_cpu(src->latency_percentile.u.i));
1037 dst->nr_block_infos = le64_to_cpu(src->nr_block_infos);
1038 for (i = 0; i < dst->nr_block_infos; i++)
1039 dst->block_infos[i] = le32_to_cpu(src->block_infos[i]);
1040 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1041 for (j = 0; j < FIO_IO_U_PLAT_NR; j++) {
1042 dst->io_u_plat_high_prio[i][j] = le64_to_cpu(src->io_u_plat_high_prio[i][j]);
1043 dst->io_u_plat_low_prio[i][j] = le64_to_cpu(src->io_u_plat_low_prio[i][j]);
1045 convert_io_stat(&dst->clat_high_prio_stat[i], &src->clat_high_prio_stat[i]);
1046 convert_io_stat(&dst->clat_low_prio_stat[i], &src->clat_low_prio_stat[i]);
1049 dst->ss_dur = le64_to_cpu(src->ss_dur);
1050 dst->ss_state = le32_to_cpu(src->ss_state);
1051 dst->ss_head = le32_to_cpu(src->ss_head);
1052 dst->ss_limit.u.f = fio_uint64_to_double(le64_to_cpu(src->ss_limit.u.i));
1053 dst->ss_slope.u.f = fio_uint64_to_double(le64_to_cpu(src->ss_slope.u.i));
1054 dst->ss_deviation.u.f = fio_uint64_to_double(le64_to_cpu(src->ss_deviation.u.i));
1055 dst->ss_criterion.u.f = fio_uint64_to_double(le64_to_cpu(src->ss_criterion.u.i));
1057 if (dst->ss_state & FIO_SS_DATA) {
1058 for (i = 0; i < dst->ss_dur; i++ ) {
1059 dst->ss_iops_data[i] = le64_to_cpu(src->ss_iops_data[i]);
1060 dst->ss_bw_data[i] = le64_to_cpu(src->ss_bw_data[i]);
1064 dst->cachehit = le64_to_cpu(src->cachehit);
1065 dst->cachemiss = le64_to_cpu(src->cachemiss);
1068 static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
1072 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1073 dst->max_run[i] = le64_to_cpu(src->max_run[i]);
1074 dst->min_run[i] = le64_to_cpu(src->min_run[i]);
1075 dst->max_bw[i] = le64_to_cpu(src->max_bw[i]);
1076 dst->min_bw[i] = le64_to_cpu(src->min_bw[i]);
1077 dst->iobytes[i] = le64_to_cpu(src->iobytes[i]);
1078 dst->agg[i] = le64_to_cpu(src->agg[i]);
1081 dst->kb_base = le32_to_cpu(src->kb_base);
1082 dst->unit_base = le32_to_cpu(src->unit_base);
1083 dst->sig_figs = le32_to_cpu(src->sig_figs);
1084 dst->groupid = le32_to_cpu(src->groupid);
1085 dst->unified_rw_rep = le32_to_cpu(src->unified_rw_rep);
1088 static void json_object_add_client_info(struct json_object *obj,
1089 struct fio_client *client)
1091 const char *hostname = client->hostname ? client->hostname : "";
1093 json_object_add_value_string(obj, "hostname", hostname);
1094 json_object_add_value_int(obj, "port", client->port);
1097 static void handle_ts(struct fio_client *client, struct fio_net_cmd *cmd)
1099 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
1100 struct flist_head *opt_list = NULL;
1101 struct json_object *tsobj;
1103 if (client->opt_lists && p->ts.thread_number <= client->jobs)
1104 opt_list = &client->opt_lists[p->ts.thread_number - 1];
1106 tsobj = show_thread_status(&p->ts, &p->rs, opt_list, &client->buf);
1107 client->did_stat = true;
1109 json_object_add_client_info(tsobj, client);
1110 json_array_add_value_object(clients_array, tsobj);
1113 if (sum_stat_clients <= 1)
1116 sum_thread_stats(&client_ts, &p->ts);
1117 sum_group_stats(&client_gs, &p->rs);
1119 client_ts.members++;
1120 client_ts.thread_number = p->ts.thread_number;
1121 client_ts.groupid = p->ts.groupid;
1122 client_ts.unified_rw_rep = p->ts.unified_rw_rep;
1123 client_ts.sig_figs = p->ts.sig_figs;
1125 if (++sum_stat_nr == sum_stat_clients) {
1126 strcpy(client_ts.name, "All clients");
1127 tsobj = show_thread_status(&client_ts, &client_gs, NULL, &allclients);
1129 json_object_add_client_info(tsobj, client);
1130 json_array_add_value_object(clients_array, tsobj);
1135 static void handle_gs(struct fio_client *client, struct fio_net_cmd *cmd)
1137 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
1139 if (output_format & FIO_OUTPUT_NORMAL)
1140 show_group_stats(gs, &client->buf);
1143 static void handle_job_opt(struct fio_client *client, struct fio_net_cmd *cmd)
1145 struct cmd_job_option *pdu = (struct cmd_job_option *) cmd->payload;
1147 pdu->global = le16_to_cpu(pdu->global);
1148 pdu->truncated = le16_to_cpu(pdu->truncated);
1149 pdu->groupid = le32_to_cpu(pdu->groupid);
1152 if (!job_opt_object)
1155 json_object_add_value_string(job_opt_object,
1156 (const char *)pdu->name,
1157 (const char *)pdu->value);
1158 } else if (client->opt_lists) {
1159 struct flist_head *opt_list = &client->opt_lists[pdu->groupid];
1160 struct print_option *p;
1162 p = malloc(sizeof(*p));
1163 p->name = strdup((const char *)pdu->name);
1164 p->value = pdu->value[0] ? strdup((const char *)pdu->value) :
1166 flist_add_tail(&p->list, opt_list);
1170 static void handle_text(struct fio_client *client, struct fio_net_cmd *cmd)
1172 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
1173 const char *buf = (const char *) pdu->buf;
1176 struct buf_output out;
1178 buf_output_init(&out);
1180 name = client->name ? client->name : client->hostname;
1182 if (!client->skip_newline && !(output_format & FIO_OUTPUT_TERSE))
1183 __log_buf(&out, "<%s> ", name);
1184 __log_buf(&out, "%s", buf);
1185 log_info_buf(out.buf, out.buflen);
1186 buf_output_free(&out);
1187 client->skip_newline = strchr(buf, '\n') == NULL;
1190 static void convert_agg(struct disk_util_agg *agg)
1194 for (i = 0; i < 2; i++) {
1195 agg->ios[i] = le64_to_cpu(agg->ios[i]);
1196 agg->merges[i] = le64_to_cpu(agg->merges[i]);
1197 agg->sectors[i] = le64_to_cpu(agg->sectors[i]);
1198 agg->ticks[i] = le64_to_cpu(agg->ticks[i]);
1201 agg->io_ticks = le64_to_cpu(agg->io_ticks);
1202 agg->time_in_queue = le64_to_cpu(agg->time_in_queue);
1203 agg->slavecount = le32_to_cpu(agg->slavecount);
1204 agg->max_util.u.f = fio_uint64_to_double(le64_to_cpu(agg->max_util.u.i));
1207 static void convert_dus(struct disk_util_stat *dus)
1211 for (i = 0; i < 2; i++) {
1212 dus->s.ios[i] = le64_to_cpu(dus->s.ios[i]);
1213 dus->s.merges[i] = le64_to_cpu(dus->s.merges[i]);
1214 dus->s.sectors[i] = le64_to_cpu(dus->s.sectors[i]);
1215 dus->s.ticks[i] = le64_to_cpu(dus->s.ticks[i]);
1218 dus->s.io_ticks = le64_to_cpu(dus->s.io_ticks);
1219 dus->s.time_in_queue = le64_to_cpu(dus->s.time_in_queue);
1220 dus->s.msec = le64_to_cpu(dus->s.msec);
1223 static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd)
1225 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
1227 if (!client->disk_stats_shown)
1228 client->disk_stats_shown = true;
1230 if (output_format & FIO_OUTPUT_JSON) {
1231 struct json_object *duobj;
1233 json_array_add_disk_util(&du->dus, &du->agg, du_array);
1234 duobj = json_array_last_value_object(du_array);
1235 json_object_add_client_info(duobj, client);
1237 if (output_format & FIO_OUTPUT_NORMAL) {
1238 __log_buf(&client->buf, "\nDisk stats (read/write):\n");
1239 print_disk_util(&du->dus, &du->agg, 0, &client->buf);
1241 if (output_format & FIO_OUTPUT_TERSE && terse_version >= 3) {
1242 print_disk_util(&du->dus, &du->agg, 1, &client->buf);
1243 __log_buf(&client->buf, "\n");
1247 static void convert_jobs_eta(struct jobs_eta *je)
1251 je->nr_running = le32_to_cpu(je->nr_running);
1252 je->nr_ramp = le32_to_cpu(je->nr_ramp);
1253 je->nr_pending = le32_to_cpu(je->nr_pending);
1254 je->nr_setting_up = le32_to_cpu(je->nr_setting_up);
1255 je->files_open = le32_to_cpu(je->files_open);
1257 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1258 je->m_rate[i] = le64_to_cpu(je->m_rate[i]);
1259 je->t_rate[i] = le64_to_cpu(je->t_rate[i]);
1260 je->m_iops[i] = le32_to_cpu(je->m_iops[i]);
1261 je->t_iops[i] = le32_to_cpu(je->t_iops[i]);
1262 je->rate[i] = le64_to_cpu(je->rate[i]);
1263 je->iops[i] = le32_to_cpu(je->iops[i]);
1266 je->elapsed_sec = le64_to_cpu(je->elapsed_sec);
1267 je->eta_sec = le64_to_cpu(je->eta_sec);
1268 je->nr_threads = le32_to_cpu(je->nr_threads);
1269 je->is_pow2 = le32_to_cpu(je->is_pow2);
1270 je->unit_base = le32_to_cpu(je->unit_base);
1271 je->sig_figs = le32_to_cpu(je->sig_figs);
1274 void fio_client_sum_jobs_eta(struct jobs_eta *dst, struct jobs_eta *je)
1278 dst->nr_running += je->nr_running;
1279 dst->nr_ramp += je->nr_ramp;
1280 dst->nr_pending += je->nr_pending;
1281 dst->nr_setting_up += je->nr_setting_up;
1282 dst->files_open += je->files_open;
1284 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1285 dst->m_rate[i] += je->m_rate[i];
1286 dst->t_rate[i] += je->t_rate[i];
1287 dst->m_iops[i] += je->m_iops[i];
1288 dst->t_iops[i] += je->t_iops[i];
1289 dst->rate[i] += je->rate[i];
1290 dst->iops[i] += je->iops[i];
1293 dst->elapsed_sec += je->elapsed_sec;
1295 if (je->eta_sec > dst->eta_sec)
1296 dst->eta_sec = je->eta_sec;
1298 dst->nr_threads += je->nr_threads;
1301 * This wont be correct for multiple strings, but at least it
1302 * works for the basic cases.
1304 strcpy((char *) dst->run_str, (char *) je->run_str);
1307 static bool remove_reply_cmd(struct fio_client *client, struct fio_net_cmd *cmd)
1309 struct fio_net_cmd_reply *reply = NULL;
1310 struct flist_head *entry;
1312 flist_for_each(entry, &client->cmd_list) {
1313 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
1315 if (cmd->tag == (uintptr_t) reply)
1322 log_err("fio: client: unable to find matching tag (%llx)\n", (unsigned long long) cmd->tag);
1326 flist_del(&reply->list);
1327 cmd->tag = reply->saved_tag;
1332 int fio_client_wait_for_reply(struct fio_client *client, uint64_t tag)
1335 struct fio_net_cmd_reply *reply = NULL;
1336 struct flist_head *entry;
1338 flist_for_each(entry, &client->cmd_list) {
1339 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
1341 if (tag == (uintptr_t) reply)
1356 static void handle_eta(struct fio_client *client, struct fio_net_cmd *cmd)
1358 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
1359 struct client_eta *eta = (struct client_eta *) (uintptr_t) cmd->tag;
1361 dprint(FD_NET, "client: got eta tag %p, %d\n", eta, eta->pending);
1363 assert(client->eta_in_flight == eta);
1365 client->eta_in_flight = NULL;
1366 flist_del_init(&client->eta_list);
1367 client->eta_timeouts = 0;
1369 if (client->ops->jobs_eta)
1370 client->ops->jobs_eta(client, je);
1372 fio_client_sum_jobs_eta(&eta->eta, je);
1373 fio_client_dec_jobs_eta(eta, client->ops->eta);
1376 static void client_flush_hist_samples(FILE *f, int hist_coarseness, void *samples,
1377 uint64_t sample_size)
1379 struct io_sample *s;
1381 uint64_t i, j, nr_samples;
1382 struct io_u_plat_entry *entry;
1383 uint64_t *io_u_plat;
1385 int stride = 1 << hist_coarseness;
1390 s = __get_sample(samples, 0, 0);
1391 log_offset = (s->__ddir & LOG_OFFSET_SAMPLE_BIT) != 0;
1393 nr_samples = sample_size / __log_entry_sz(log_offset);
1395 for (i = 0; i < nr_samples; i++) {
1397 s = (struct io_sample *)((char *)__get_sample(samples, log_offset, i) +
1398 i * sizeof(struct io_u_plat_entry));
1400 entry = s->data.plat_entry;
1401 io_u_plat = entry->io_u_plat;
1403 fprintf(f, "%lu, %u, %llu, ", (unsigned long) s->time,
1404 io_sample_ddir(s), (unsigned long long) s->bs);
1405 for (j = 0; j < FIO_IO_U_PLAT_NR - stride; j += stride) {
1406 fprintf(f, "%llu, ", (unsigned long long)hist_sum(j, stride, io_u_plat, NULL));
1408 fprintf(f, "%llu\n", (unsigned long long)
1409 hist_sum(FIO_IO_U_PLAT_NR - stride, stride, io_u_plat, NULL));
1414 static int fio_client_handle_iolog(struct fio_client *client,
1415 struct fio_net_cmd *cmd)
1417 struct cmd_iolog_pdu *pdu = NULL;
1419 char *log_pathname = NULL;
1422 pdu = convert_iolog(cmd, &store_direct);
1424 log_err("fio: failed converting IO log\n");
1429 /* allocate buffer big enough for next sprintf() call */
1430 log_pathname = malloc(10 + strlen((char *)pdu->name) +
1431 strlen(client->hostname));
1432 if (!log_pathname) {
1433 log_err("fio: memory allocation of unique pathname failed\n");
1437 /* generate a unique pathname for the log file using hostname */
1438 sprintf(log_pathname, "%s.%s", pdu->name, client->hostname);
1445 fd = open((const char *) log_pathname,
1446 O_WRONLY | O_CREAT | O_TRUNC, 0644);
1448 log_err("fio: open log %s: %s\n",
1449 log_pathname, strerror(errno));
1454 sz = cmd->pdu_len - sizeof(*pdu);
1455 wrote = write(fd, pdu->samples, sz);
1459 log_err("fio: short write on compressed log\n");
1467 f = fopen((const char *) log_pathname, "w");
1469 log_err("fio: fopen log %s : %s\n",
1470 log_pathname, strerror(errno));
1475 if (pdu->log_type == IO_LOG_TYPE_HIST) {
1476 client_flush_hist_samples(f, pdu->log_hist_coarseness, pdu->samples,
1477 pdu->nr_samples * sizeof(struct io_sample));
1479 flush_samples(f, pdu->samples,
1480 pdu->nr_samples * sizeof(struct io_sample));
1487 if (pdu && pdu != (void *) cmd->payload)
1496 static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd)
1498 struct cmd_probe_reply_pdu *probe = (struct cmd_probe_reply_pdu *) cmd->payload;
1499 const char *os, *arch;
1502 os = fio_get_os_string(probe->os);
1506 arch = fio_get_arch_string(probe->arch);
1510 sprintf(bit, "%d-bit", probe->bpp * 8);
1511 probe->flags = le64_to_cpu(probe->flags);
1513 if (output_format & FIO_OUTPUT_NORMAL) {
1514 log_info("hostname=%s, be=%u, %s, os=%s, arch=%s, fio=%s, flags=%lx\n",
1515 probe->hostname, probe->bigendian, bit, os, arch,
1516 probe->fio_version, (unsigned long) probe->flags);
1520 client->name = strdup((char *) probe->hostname);
1523 static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd)
1525 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
1527 client->state = Client_started;
1528 client->jobs = le32_to_cpu(pdu->jobs);
1529 client->nr_stat = le32_to_cpu(pdu->stat_outputs);
1534 if (client->opt_lists)
1535 free(client->opt_lists);
1537 client->opt_lists = malloc(client->jobs * sizeof(struct flist_head));
1538 for (i = 0; i < client->jobs; i++)
1539 INIT_FLIST_HEAD(&client->opt_lists[i]);
1542 sum_stat_clients += client->nr_stat;
1545 static void handle_stop(struct fio_client *client)
1548 log_info("client <%s>: exited with error %d\n", client->hostname, client->error);
1551 static void convert_stop(struct fio_net_cmd *cmd)
1553 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
1555 pdu->error = le32_to_cpu(pdu->error);
1558 static void convert_text(struct fio_net_cmd *cmd)
1560 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
1562 pdu->level = le32_to_cpu(pdu->level);
1563 pdu->buf_len = le32_to_cpu(pdu->buf_len);
1564 pdu->log_sec = le64_to_cpu(pdu->log_sec);
1565 pdu->log_usec = le64_to_cpu(pdu->log_usec);
1568 static struct cmd_iolog_pdu *convert_iolog_gz(struct fio_net_cmd *cmd,
1569 struct cmd_iolog_pdu *pdu)
1572 struct cmd_iolog_pdu *ret;
1574 uint64_t nr_samples;
1578 stream.zalloc = Z_NULL;
1579 stream.zfree = Z_NULL;
1580 stream.opaque = Z_NULL;
1581 stream.avail_in = 0;
1582 stream.next_in = Z_NULL;
1584 if (inflateInit(&stream) != Z_OK)
1588 * Get header first, it's not compressed
1590 nr_samples = le64_to_cpu(pdu->nr_samples);
1592 if (pdu->log_type == IO_LOG_TYPE_HIST)
1593 total = nr_samples * (__log_entry_sz(le32_to_cpu(pdu->log_offset)) +
1594 sizeof(struct io_u_plat_entry));
1596 total = nr_samples * __log_entry_sz(le32_to_cpu(pdu->log_offset));
1597 ret = malloc(total + sizeof(*pdu));
1598 ret->nr_samples = nr_samples;
1600 memcpy(ret, pdu, sizeof(*pdu));
1602 p = (char *) ret + sizeof(*pdu);
1604 stream.avail_in = cmd->pdu_len - sizeof(*pdu);
1605 stream.next_in = (void *)((char *) pdu + sizeof(*pdu));
1606 while (stream.avail_in) {
1607 unsigned int this_chunk = 65536;
1608 unsigned int this_len;
1611 if (this_chunk > total)
1614 stream.avail_out = this_chunk;
1615 stream.next_out = (void *)p;
1616 err = inflate(&stream, Z_NO_FLUSH);
1617 /* may be Z_OK, or Z_STREAM_END */
1620 * Z_STREAM_ERROR and Z_BUF_ERROR can safely be
1622 if (err == Z_STREAM_ERROR || err == Z_BUF_ERROR)
1624 log_err("fio: inflate error %d\n", err);
1630 this_len = this_chunk - stream.avail_out;
1636 inflateEnd(&stream);
1644 * This has been compressed on the server side, since it can be big.
1647 static struct cmd_iolog_pdu *convert_iolog(struct fio_net_cmd *cmd,
1650 struct cmd_iolog_pdu *pdu = (struct cmd_iolog_pdu *) cmd->payload;
1651 struct cmd_iolog_pdu *ret;
1656 *store_direct = false;
1659 * Convert if compressed and we support it. If it's not
1660 * compressed, we need not do anything.
1662 compressed = le32_to_cpu(pdu->compressed);
1663 if (compressed == XMIT_COMPRESSED) {
1665 log_err("fio: server sent compressed data by mistake\n");
1668 ret = convert_iolog_gz(cmd, pdu);
1670 log_err("fio: failed decompressing log\n");
1673 } else if (compressed == STORE_COMPRESSED) {
1674 *store_direct = true;
1679 ret->nr_samples = le64_to_cpu(ret->nr_samples);
1680 ret->thread_number = le32_to_cpu(ret->thread_number);
1681 ret->log_type = le32_to_cpu(ret->log_type);
1682 ret->compressed = le32_to_cpu(ret->compressed);
1683 ret->log_offset = le32_to_cpu(ret->log_offset);
1684 ret->log_prio = le32_to_cpu(ret->log_prio);
1685 ret->log_hist_coarseness = le32_to_cpu(ret->log_hist_coarseness);
1690 samples = &ret->samples[0];
1691 for (i = 0; i < ret->nr_samples; i++) {
1692 struct io_sample *s;
1694 s = __get_sample(samples, ret->log_offset, i);
1695 if (ret->log_type == IO_LOG_TYPE_HIST)
1696 s = (struct io_sample *)((char *)s + sizeof(struct io_u_plat_entry) * i);
1698 s->time = le64_to_cpu(s->time);
1699 s->data.val = le64_to_cpu(s->data.val);
1700 s->__ddir = __le32_to_cpu(s->__ddir);
1701 s->bs = le64_to_cpu(s->bs);
1702 s->priority = le16_to_cpu(s->priority);
1704 if (ret->log_offset) {
1705 struct io_sample_offset *so = (void *) s;
1707 so->offset = le64_to_cpu(so->offset);
1710 if (ret->log_type == IO_LOG_TYPE_HIST) {
1711 s->data.plat_entry = (struct io_u_plat_entry *)(((char *)s) + sizeof(*s));
1712 s->data.plat_entry->list.next = NULL;
1713 s->data.plat_entry->list.prev = NULL;
1720 static void sendfile_reply(int fd, struct cmd_sendfile_reply *rep,
1721 size_t size, uint64_t tag)
1723 rep->error = cpu_to_le32(rep->error);
1724 fio_net_send_cmd(fd, FIO_NET_CMD_SENDFILE, rep, size, &tag, NULL);
1727 static int fio_send_file(struct fio_client *client, struct cmd_sendfile *pdu,
1730 struct cmd_sendfile_reply *rep;
1735 size = sizeof(*rep);
1738 if (stat((char *)pdu->path, &sb) < 0) {
1741 sendfile_reply(client->fd, rep, size, tag);
1747 rep = realloc(rep, size);
1748 rep->size = cpu_to_le32((uint32_t) sb.st_size);
1750 fd = open((char *)pdu->path, O_RDONLY);
1754 rep->error = read_data(fd, &rep->data, sb.st_size);
1755 sendfile_reply(client->fd, rep, size, tag);
1761 int fio_handle_client(struct fio_client *client)
1763 struct client_ops *ops = client->ops;
1764 struct fio_net_cmd *cmd;
1767 dprint(FD_NET, "client: handle %s\n", client->hostname);
1769 cmd = fio_net_recv_cmd(client->fd, true);
1773 dprint(FD_NET, "client: got cmd op %s from %s (pdu=%u)\n",
1774 fio_server_op(cmd->opcode), client->hostname, cmd->pdu_len);
1776 client->last_cmd = cmd->opcode;
1778 switch (cmd->opcode) {
1779 case FIO_NET_CMD_QUIT:
1781 ops->quit(client, cmd);
1782 remove_client(client);
1784 case FIO_NET_CMD_TEXT:
1786 ops->text(client, cmd);
1788 case FIO_NET_CMD_DU: {
1789 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
1791 convert_dus(&du->dus);
1792 convert_agg(&du->agg);
1794 ops->disk_util(client, cmd);
1797 case FIO_NET_CMD_TS: {
1798 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
1800 dprint(FD_NET, "client: ts->ss_state = %u\n", (unsigned int) le32_to_cpu(p->ts.ss_state));
1801 if (le32_to_cpu(p->ts.ss_state) & FIO_SS_DATA) {
1802 dprint(FD_NET, "client: received steadystate ring buffers\n");
1804 size = le64_to_cpu(p->ts.ss_dur);
1805 p->ts.ss_iops_data = (uint64_t *) ((struct cmd_ts_pdu *)cmd->payload + 1);
1806 p->ts.ss_bw_data = p->ts.ss_iops_data + size;
1809 convert_ts(&p->ts, &p->ts);
1810 convert_gs(&p->rs, &p->rs);
1812 ops->thread_status(client, cmd);
1815 case FIO_NET_CMD_GS: {
1816 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
1820 ops->group_stats(client, cmd);
1823 case FIO_NET_CMD_ETA: {
1824 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
1826 if (!remove_reply_cmd(client, cmd))
1828 convert_jobs_eta(je);
1829 handle_eta(client, cmd);
1832 case FIO_NET_CMD_PROBE:
1833 remove_reply_cmd(client, cmd);
1834 ops->probe(client, cmd);
1836 case FIO_NET_CMD_SERVER_START:
1837 client->state = Client_running;
1839 ops->job_start(client, cmd);
1841 case FIO_NET_CMD_START: {
1842 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
1844 pdu->jobs = le32_to_cpu(pdu->jobs);
1845 ops->start(client, cmd);
1848 case FIO_NET_CMD_STOP: {
1849 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
1852 client->state = Client_stopped;
1853 client->error = le32_to_cpu(pdu->error);
1854 client->signal = le32_to_cpu(pdu->signal);
1858 case FIO_NET_CMD_ADD_JOB: {
1859 struct cmd_add_job_pdu *pdu = (struct cmd_add_job_pdu *) cmd->payload;
1861 client->thread_number = le32_to_cpu(pdu->thread_number);
1862 client->groupid = le32_to_cpu(pdu->groupid);
1865 ops->add_job(client, cmd);
1868 case FIO_NET_CMD_IOLOG:
1869 fio_client_handle_iolog(client, cmd);
1871 case FIO_NET_CMD_UPDATE_JOB:
1872 ops->update_job(client, cmd);
1873 remove_reply_cmd(client, cmd);
1875 case FIO_NET_CMD_VTRIGGER: {
1876 struct all_io_list *pdu = (struct all_io_list *) cmd->payload;
1881 strcpy(buf, aux_path);
1885 __verify_save_state(pdu, server_name(client, &buf[off], sizeof(buf) - off));
1886 exec_trigger(trigger_cmd);
1889 case FIO_NET_CMD_SENDFILE: {
1890 struct cmd_sendfile *pdu = (struct cmd_sendfile *) cmd->payload;
1891 fio_send_file(client, pdu, cmd->tag);
1894 case FIO_NET_CMD_JOB_OPT: {
1895 handle_job_opt(client, cmd);
1899 log_err("fio: unknown client op: %s\n", fio_server_op(cmd->opcode));
1907 int fio_clients_send_trigger(const char *cmd)
1909 struct flist_head *entry;
1910 struct fio_client *client;
1913 dprint(FD_NET, "client: send vtrigger: %s\n", cmd);
1920 flist_for_each(entry, &client_list) {
1921 struct cmd_vtrigger_pdu *pdu;
1923 client = flist_entry(entry, struct fio_client, list);
1925 pdu = malloc(sizeof(*pdu) + slen);
1926 pdu->len = cpu_to_le16((uint16_t) slen);
1928 memcpy(pdu->cmd, cmd, slen);
1929 fio_net_send_cmd(client->fd, FIO_NET_CMD_VTRIGGER, pdu,
1930 sizeof(*pdu) + slen, NULL, NULL);
1937 static void request_client_etas(struct client_ops *ops)
1939 struct fio_client *client;
1940 struct flist_head *entry;
1941 struct client_eta *eta;
1944 if (eta_print == FIO_ETA_NEVER)
1947 dprint(FD_NET, "client: request eta (%d)\n", nr_clients);
1949 eta = calloc(1, sizeof(*eta) + __THREAD_RUNSTR_SZ(REAL_MAX_JOBS));
1950 eta->pending = nr_clients;
1952 flist_for_each(entry, &client_list) {
1953 client = flist_entry(entry, struct fio_client, list);
1955 if (!flist_empty(&client->eta_list)) {
1959 if (client->state != Client_running)
1962 assert(!client->eta_in_flight);
1963 flist_add_tail(&client->eta_list, &eta_list);
1964 client->eta_in_flight = eta;
1965 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_SEND_ETA,
1966 (uintptr_t) eta, &client->cmd_list);
1970 if (!fio_client_dec_jobs_eta(eta, ops->eta))
1974 dprint(FD_NET, "client: requested eta tag %p\n", eta);
1978 * A single SEND_ETA timeout isn't fatal. Attempt to recover.
1980 static int handle_cmd_timeout(struct fio_client *client,
1981 struct fio_net_cmd_reply *reply)
1983 uint16_t reply_opcode = reply->opcode;
1985 flist_del(&reply->list);
1988 if (reply_opcode != FIO_NET_CMD_SEND_ETA)
1991 log_info("client <%s>: timeout on SEND_ETA\n", client->hostname);
1993 flist_del_init(&client->eta_list);
1994 if (client->eta_in_flight) {
1995 fio_client_dec_jobs_eta(client->eta_in_flight, client->ops->eta);
1996 client->eta_in_flight = NULL;
2000 * If we fail 5 in a row, give up...
2002 if (client->eta_timeouts++ > 5)
2008 static int client_check_cmd_timeout(struct fio_client *client,
2009 struct timespec *now)
2011 struct fio_net_cmd_reply *reply;
2012 struct flist_head *entry, *tmp;
2015 flist_for_each_safe(entry, tmp, &client->cmd_list) {
2018 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
2020 if (mtime_since(&reply->ts, now) < FIO_NET_CLIENT_TIMEOUT)
2024 if (!handle_cmd_timeout(client, reply))
2027 log_err("fio: client %s, timeout on cmd %s\n", client->hostname,
2032 return flist_empty(&client->cmd_list) && ret;
2035 static int fio_check_clients_timed_out(void)
2037 struct fio_client *client;
2038 struct flist_head *entry, *tmp;
2042 fio_gettime(&ts, NULL);
2044 flist_for_each_safe(entry, tmp, &client_list) {
2045 client = flist_entry(entry, struct fio_client, list);
2047 if (flist_empty(&client->cmd_list))
2050 if (!client_check_cmd_timeout(client, &ts))
2053 if (client->ops->timed_out)
2054 client->ops->timed_out(client);
2056 log_err("fio: client %s timed out\n", client->hostname);
2058 if (client->last_cmd != FIO_NET_CMD_VTRIGGER)
2059 client->error = ETIMEDOUT;
2061 log_info("fio: ignoring timeout due to vtrigger\n");
2062 remove_client(client);
2069 int fio_handle_clients(struct client_ops *ops)
2071 struct pollfd *pfds;
2072 int i, ret = 0, retval = 0;
2074 fio_gettime(&eta_ts, NULL);
2076 pfds = malloc(nr_clients * sizeof(struct pollfd));
2078 init_thread_stat(&client_ts);
2079 init_group_run_stat(&client_gs);
2081 while (!exit_backend && nr_clients) {
2082 struct flist_head *entry, *tmp;
2083 struct fio_client *client;
2086 flist_for_each_safe(entry, tmp, &client_list) {
2087 client = flist_entry(entry, struct fio_client, list);
2089 if (!client->sent_job && !client->ops->stay_connected &&
2090 flist_empty(&client->cmd_list)) {
2091 remove_client(client);
2095 pfds[i].fd = client->fd;
2096 pfds[i].events = POLLIN;
2103 assert(i == nr_clients);
2109 fio_gettime(&ts, NULL);
2110 if (eta_time_within_slack(mtime_since(&eta_ts, &ts))) {
2111 request_client_etas(ops);
2112 memcpy(&eta_ts, &ts, sizeof(ts));
2114 if (fio_check_clients_timed_out())
2118 check_trigger_file();
2120 timeout = min(100u, ops->eta_msec);
2122 ret = poll(pfds, nr_clients, timeout);
2126 log_err("fio: poll clients: %s\n", strerror(errno));
2132 for (i = 0; i < nr_clients; i++) {
2133 if (!(pfds[i].revents & POLLIN))
2136 client = find_client_by_fd(pfds[i].fd);
2138 log_err("fio: unknown client fd %ld\n", (long) pfds[i].fd);
2141 if (!fio_handle_client(client)) {
2142 log_info("client: host=%s disconnected\n",
2144 remove_client(client);
2146 } else if (client->error)
2148 fio_put_client(client);
2152 log_info_buf(allclients.buf, allclients.buflen);
2153 buf_output_free(&allclients);
2155 fio_client_json_fini();
2158 return retval || error_clients;
2161 static void client_display_thread_status(struct jobs_eta *je)
2163 if (!(output_format & FIO_OUTPUT_JSON))
2164 display_thread_status(je);