9 #include <sys/socket.h>
11 #include <netinet/in.h>
12 #include <arpa/inet.h>
24 #include "verify-state.h"
26 static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd);
27 static void handle_ts(struct fio_client *client, struct fio_net_cmd *cmd);
28 static void handle_gs(struct fio_client *client, struct fio_net_cmd *cmd);
29 static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd);
30 static void handle_text(struct fio_client *client, struct fio_net_cmd *cmd);
31 static void handle_stop(struct fio_client *client);
32 static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd);
34 static void convert_text(struct fio_net_cmd *cmd);
35 static void client_display_thread_status(struct jobs_eta *je);
37 struct client_ops fio_client_ops = {
39 .disk_util = handle_du,
40 .thread_status = handle_ts,
41 .group_stats = handle_gs,
43 .start = handle_start,
44 .eta = client_display_thread_status,
45 .probe = handle_probe,
46 .eta_msec = FIO_CLIENT_DEF_ETA_MSEC,
47 .client_type = FIO_CLIENT_TYPE_CLI,
50 static struct timespec eta_ts;
52 static FLIST_HEAD(client_list);
53 static FLIST_HEAD(eta_list);
55 static FLIST_HEAD(arg_list);
57 struct thread_stat client_ts;
58 struct group_run_stats client_gs;
61 static int sum_stat_nr;
62 static struct json_object *root = NULL;
63 static struct json_object *job_opt_object = NULL;
64 static struct json_array *clients_array = NULL;
65 static struct json_array *du_array = NULL;
67 static int error_clients;
69 #define FIO_CLIENT_HASH_BITS 7
70 #define FIO_CLIENT_HASH_SZ (1 << FIO_CLIENT_HASH_BITS)
71 #define FIO_CLIENT_HASH_MASK (FIO_CLIENT_HASH_SZ - 1)
72 static struct flist_head client_hash[FIO_CLIENT_HASH_SZ];
74 static struct cmd_iolog_pdu *convert_iolog(struct fio_net_cmd *, bool *);
76 static void fio_client_add_hash(struct fio_client *client)
78 int bucket = hash_long(client->fd, FIO_CLIENT_HASH_BITS);
80 bucket &= FIO_CLIENT_HASH_MASK;
81 flist_add(&client->hash_list, &client_hash[bucket]);
84 static void fio_client_remove_hash(struct fio_client *client)
86 if (!flist_empty(&client->hash_list))
87 flist_del_init(&client->hash_list);
90 static void fio_init fio_client_hash_init(void)
94 for (i = 0; i < FIO_CLIENT_HASH_SZ; i++)
95 INIT_FLIST_HEAD(&client_hash[i]);
98 static int read_data(int fd, void *data, size_t size)
103 ret = read(fd, data, size);
105 if (errno == EAGAIN || errno == EINTR)
122 static int read_ini_data(int fd, void *data, size_t size)
133 fp = fdopen(dupfd, "r");
142 char buf[OPT_LEN_MAX+1], *sub;
144 if (!fgets(buf, sizeof(buf), fp)) {
146 if (errno == EAGAIN || errno == EINTR)
153 sub = fio_option_dup_subs(buf);
155 if (len + 1 > size) {
156 log_err("fio: no space left to read data\n");
174 static void fio_client_json_init(void)
179 if (!(output_format & FIO_OUTPUT_JSON))
183 os_ctime_r((const time_t *) &time_p, time_buf, sizeof(time_buf));
184 time_buf[strlen(time_buf) - 1] = '\0';
186 root = json_create_object();
187 json_object_add_value_string(root, "fio version", fio_version_string);
188 json_object_add_value_int(root, "timestamp", time_p);
189 json_object_add_value_string(root, "time", time_buf);
191 job_opt_object = json_create_object();
192 json_object_add_value_object(root, "global options", job_opt_object);
193 clients_array = json_create_array();
194 json_object_add_value_array(root, "client_stats", clients_array);
195 du_array = json_create_array();
196 json_object_add_value_array(root, "disk_util", du_array);
199 static void fio_client_json_fini(void)
201 if (!(output_format & FIO_OUTPUT_JSON))
205 json_print_object(root, NULL);
207 json_free_object(root);
209 clients_array = NULL;
213 static struct fio_client *find_client_by_fd(int fd)
215 int bucket = hash_long(fd, FIO_CLIENT_HASH_BITS) & FIO_CLIENT_HASH_MASK;
216 struct fio_client *client;
217 struct flist_head *entry;
219 flist_for_each(entry, &client_hash[bucket]) {
220 client = flist_entry(entry, struct fio_client, hash_list);
222 if (client->fd == fd) {
231 void fio_put_client(struct fio_client *client)
236 free(client->hostname);
241 while (client->nr_files) {
242 struct client_file *cf = &client->files[--client->nr_files];
248 if (client->opt_lists)
249 free(client->opt_lists);
251 if (!client->did_stat)
260 static int fio_client_dec_jobs_eta(struct client_eta *eta, client_eta_op eta_fn)
262 if (!--eta->pending) {
271 static void fio_drain_client_text(struct fio_client *client)
274 struct fio_net_cmd *cmd;
276 cmd = fio_net_recv_cmd(client->fd, false);
280 if (cmd->opcode == FIO_NET_CMD_TEXT) {
282 client->ops->text(client, cmd);
289 static void remove_client(struct fio_client *client)
291 assert(client->refs);
293 dprint(FD_NET, "client: removed <%s>\n", client->hostname);
295 fio_drain_client_text(client);
297 if (!flist_empty(&client->list))
298 flist_del_init(&client->list);
300 fio_client_remove_hash(client);
302 if (!flist_empty(&client->eta_list)) {
303 flist_del_init(&client->eta_list);
304 fio_client_dec_jobs_eta(client->eta_in_flight, client->ops->eta);
310 if (client->ops->removed)
311 client->ops->removed(client);
314 fio_put_client(client);
317 struct fio_client *fio_get_client(struct fio_client *client)
323 static void __fio_client_add_cmd_option(struct fio_client *client,
328 index = client->argc++;
329 client->argv = realloc(client->argv, sizeof(char *) * client->argc);
330 client->argv[index] = strdup(opt);
331 dprint(FD_NET, "client: add cmd %d: %s\n", index, opt);
334 void fio_client_add_cmd_option(void *cookie, const char *opt)
336 struct fio_client *client = cookie;
337 struct flist_head *entry;
342 __fio_client_add_cmd_option(client, opt);
345 * Duplicate arguments to shared client group
347 flist_for_each(entry, &arg_list) {
348 client = flist_entry(entry, struct fio_client, arg_list);
350 __fio_client_add_cmd_option(client, opt);
354 struct fio_client *fio_client_add_explicit(struct client_ops *ops,
355 const char *hostname, int type,
358 struct fio_client *client;
360 client = malloc(sizeof(*client));
361 memset(client, 0, sizeof(*client));
363 INIT_FLIST_HEAD(&client->list);
364 INIT_FLIST_HEAD(&client->hash_list);
365 INIT_FLIST_HEAD(&client->arg_list);
366 INIT_FLIST_HEAD(&client->eta_list);
367 INIT_FLIST_HEAD(&client->cmd_list);
369 client->hostname = strdup(hostname);
371 if (type == Fio_client_socket)
372 client->is_sock = true;
376 ipv6 = type == Fio_client_ipv6;
377 if (fio_server_parse_host(hostname, ipv6,
378 &client->addr.sin_addr,
379 &client->addr6.sin6_addr))
388 client->type = ops->client_type;
390 __fio_client_add_cmd_option(client, "fio");
392 flist_add(&client->list, &client_list);
394 dprint(FD_NET, "client: added <%s>\n", client->hostname);
401 int fio_client_add_ini_file(void *cookie, const char *ini_file, bool remote)
403 struct fio_client *client = cookie;
404 struct client_file *cf;
411 dprint(FD_NET, "client <%s>: add ini %s\n", client->hostname, ini_file);
413 new_size = (client->nr_files + 1) * sizeof(struct client_file);
414 new_files = realloc(client->files, new_size);
418 client->files = new_files;
419 cf = &client->files[client->nr_files];
420 cf->file = strdup(ini_file);
426 int fio_client_add(struct client_ops *ops, const char *hostname, void **cookie)
428 struct fio_client *existing = *cookie;
429 struct fio_client *client;
433 * We always add our "exec" name as the option, hence 1
436 if (existing->argc == 1)
437 flist_add_tail(&existing->arg_list, &arg_list);
439 while (!flist_empty(&arg_list))
440 flist_del_init(arg_list.next);
444 client = malloc(sizeof(*client));
445 memset(client, 0, sizeof(*client));
447 INIT_FLIST_HEAD(&client->list);
448 INIT_FLIST_HEAD(&client->hash_list);
449 INIT_FLIST_HEAD(&client->arg_list);
450 INIT_FLIST_HEAD(&client->eta_list);
451 INIT_FLIST_HEAD(&client->cmd_list);
453 if (fio_server_parse_string(hostname, &client->hostname,
454 &client->is_sock, &client->port,
455 &client->addr.sin_addr,
456 &client->addr6.sin6_addr,
463 client->type = ops->client_type;
465 __fio_client_add_cmd_option(client, "fio");
467 flist_add(&client->list, &client_list);
469 dprint(FD_NET, "client: added <%s>\n", client->hostname);
474 static const char *server_name(struct fio_client *client, char *buf,
480 from = inet_ntop(AF_INET6, (struct sockaddr *) &client->addr6.sin6_addr, buf, bufsize);
481 else if (client->is_sock)
484 from = inet_ntop(AF_INET, (struct sockaddr *) &client->addr.sin_addr, buf, bufsize);
489 static void probe_client(struct fio_client *client)
491 struct cmd_client_probe_pdu pdu;
496 dprint(FD_NET, "client: send probe\n");
499 pdu.flags = __le64_to_cpu(FIO_PROBE_FLAG_ZLIB);
504 sname = server_name(client, buf, sizeof(buf));
505 memset(pdu.server, 0, sizeof(pdu.server));
506 strncpy((char *) pdu.server, sname, sizeof(pdu.server) - 1);
508 fio_net_send_cmd(client->fd, FIO_NET_CMD_PROBE, &pdu, sizeof(pdu), &tag, &client->cmd_list);
511 static int fio_client_connect_ip(struct fio_client *client)
513 struct sockaddr *addr;
518 client->addr6.sin6_family = AF_INET6;
519 client->addr6.sin6_port = htons(client->port);
521 addr = (struct sockaddr *) &client->addr6;
522 socklen = sizeof(client->addr6);
524 client->addr.sin_family = AF_INET;
525 client->addr.sin_port = htons(client->port);
527 addr = (struct sockaddr *) &client->addr;
528 socklen = sizeof(client->addr);
531 fd = socket(domain, SOCK_STREAM, 0);
535 log_err("fio: socket: %s\n", strerror(errno));
539 if (connect(fd, addr, socklen) < 0) {
542 log_err("fio: connect: %s\n", strerror(errno));
543 log_err("fio: failed to connect to %s:%u\n", client->hostname,
552 static int fio_client_connect_sock(struct fio_client *client)
554 struct sockaddr_un *addr = &client->addr_un;
558 memset(addr, 0, sizeof(*addr));
559 addr->sun_family = AF_UNIX;
560 strncpy(addr->sun_path, client->hostname, sizeof(addr->sun_path) - 1);
562 fd = socket(AF_UNIX, SOCK_STREAM, 0);
566 log_err("fio: socket: %s\n", strerror(errno));
570 len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
571 if (connect(fd, (struct sockaddr *) addr, len) < 0) {
574 log_err("fio: connect; %s\n", strerror(errno));
582 int fio_client_connect(struct fio_client *client)
586 dprint(FD_NET, "client: connect to host %s\n", client->hostname);
589 fd = fio_client_connect_sock(client);
591 fd = fio_client_connect_ip(client);
593 dprint(FD_NET, "client: %s connected %d\n", client->hostname, fd);
599 fio_client_add_hash(client);
600 client->state = Client_connected;
602 probe_client(client);
606 int fio_client_terminate(struct fio_client *client)
608 return fio_net_send_quit(client->fd);
611 static void fio_clients_terminate(void)
613 struct flist_head *entry;
614 struct fio_client *client;
616 dprint(FD_NET, "client: terminate clients\n");
618 flist_for_each(entry, &client_list) {
619 client = flist_entry(entry, struct fio_client, list);
620 fio_client_terminate(client);
624 static void sig_int(int sig)
626 dprint(FD_NET, "client: got signal %d\n", sig);
627 fio_clients_terminate();
630 static void client_signal_handler(void)
632 struct sigaction act;
634 memset(&act, 0, sizeof(act));
635 act.sa_handler = sig_int;
636 act.sa_flags = SA_RESTART;
637 sigaction(SIGINT, &act, NULL);
639 memset(&act, 0, sizeof(act));
640 act.sa_handler = sig_int;
641 act.sa_flags = SA_RESTART;
642 sigaction(SIGTERM, &act, NULL);
644 /* Windows uses SIGBREAK as a quit signal from other applications */
646 memset(&act, 0, sizeof(act));
647 act.sa_handler = sig_int;
648 act.sa_flags = SA_RESTART;
649 sigaction(SIGBREAK, &act, NULL);
652 memset(&act, 0, sizeof(act));
653 act.sa_handler = sig_show_status;
654 act.sa_flags = SA_RESTART;
655 sigaction(SIGUSR1, &act, NULL);
658 static int send_client_cmd_line(struct fio_client *client)
660 struct cmd_single_line_pdu *cslp;
661 struct cmd_line_pdu *clp;
662 unsigned long offset;
668 dprint(FD_NET, "client: send cmdline %d\n", client->argc);
670 lens = malloc(client->argc * sizeof(unsigned int));
673 * Find out how much mem we need
675 for (i = 0, mem = 0; i < client->argc; i++) {
676 lens[i] = strlen(client->argv[i]) + 1;
681 * We need one cmd_line_pdu, and argc number of cmd_single_line_pdu
683 mem += sizeof(*clp) + (client->argc * sizeof(*cslp));
687 offset = sizeof(*clp);
689 for (i = 0; i < client->argc; i++) {
690 uint16_t arg_len = lens[i];
693 strcpy((char *) cslp->text, client->argv[i]);
694 cslp->len = cpu_to_le16(arg_len);
695 offset += sizeof(*cslp) + arg_len;
699 clp->lines = cpu_to_le16(client->argc);
700 clp->client_type = __cpu_to_le16(client->type);
701 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOBLINE, pdu, mem, NULL, NULL);
706 int fio_clients_connect(void)
708 struct fio_client *client;
709 struct flist_head *entry, *tmp;
714 WSAStartup(MAKEWORD(2, 2), &wsd);
717 dprint(FD_NET, "client: connect all\n");
719 client_signal_handler();
721 flist_for_each_safe(entry, tmp, &client_list) {
722 client = flist_entry(entry, struct fio_client, list);
724 ret = fio_client_connect(client);
726 remove_client(client);
730 if (client->argc > 1)
731 send_client_cmd_line(client);
737 int fio_start_client(struct fio_client *client)
739 dprint(FD_NET, "client: start %s\n", client->hostname);
740 return fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_RUN, 0, NULL);
743 int fio_start_all_clients(void)
745 struct fio_client *client;
746 struct flist_head *entry, *tmp;
749 dprint(FD_NET, "client: start all\n");
751 fio_client_json_init();
753 flist_for_each_safe(entry, tmp, &client_list) {
754 client = flist_entry(entry, struct fio_client, list);
756 ret = fio_start_client(client);
758 remove_client(client);
763 return flist_empty(&client_list);
766 static int __fio_client_send_remote_ini(struct fio_client *client,
767 const char *filename)
769 struct cmd_load_file_pdu *pdu;
773 dprint(FD_NET, "send remote ini %s to %s\n", filename, client->hostname);
775 p_size = sizeof(*pdu) + strlen(filename) + 1;
776 pdu = malloc(p_size);
777 memset(pdu, 0, p_size);
778 pdu->name_len = strlen(filename);
779 strcpy((char *) pdu->file, filename);
780 pdu->client_type = cpu_to_le16((uint16_t) client->type);
782 client->sent_job = true;
783 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_LOAD_FILE, pdu, p_size,NULL, NULL);
789 * Send file contents to server backend. We could use sendfile(), but to remain
790 * more portable lets just read/write the darn thing.
792 static int __fio_client_send_local_ini(struct fio_client *client,
793 const char *filename)
795 struct cmd_job_pdu *pdu;
803 dprint(FD_NET, "send ini %s to %s\n", filename, client->hostname);
805 fd = open(filename, O_RDONLY);
808 log_err("fio: job file <%s> open: %s\n", filename, strerror(errno));
812 if (fstat(fd, &sb) < 0) {
814 log_err("fio: job file stat: %s\n", strerror(errno));
820 * Add extra space for variable expansion, but doesn't guarantee.
822 sb.st_size += OPT_LEN_MAX;
823 p_size = sb.st_size + sizeof(*pdu);
824 pdu = malloc(p_size);
829 if (read_ini_data(fd, p, len)) {
830 log_err("fio: failed reading job file %s\n", filename);
836 pdu->buf_len = __cpu_to_le32(sb.st_size);
837 pdu->client_type = cpu_to_le32(client->type);
839 client->sent_job = true;
840 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, pdu, p_size, NULL, NULL);
846 int fio_client_send_ini(struct fio_client *client, const char *filename,
852 ret = __fio_client_send_local_ini(client, filename);
854 ret = __fio_client_send_remote_ini(client, filename);
857 client->sent_job = true;
862 static int fio_client_send_cf(struct fio_client *client,
863 struct client_file *cf)
865 return fio_client_send_ini(client, cf->file, cf->remote);
868 int fio_clients_send_ini(const char *filename)
870 struct fio_client *client;
871 struct flist_head *entry, *tmp;
873 flist_for_each_safe(entry, tmp, &client_list) {
876 client = flist_entry(entry, struct fio_client, list);
878 if (client->nr_files) {
881 for (i = 0; i < client->nr_files; i++) {
882 struct client_file *cf;
884 cf = &client->files[i];
886 if (fio_client_send_cf(client, cf)) {
888 remove_client(client);
893 if (client->sent_job || failed)
895 if (!filename || fio_client_send_ini(client, filename, 0))
896 remove_client(client);
902 int fio_client_update_options(struct fio_client *client,
903 struct thread_options *o, uint64_t *tag)
905 struct cmd_add_job_pdu pdu;
907 pdu.thread_number = cpu_to_le32(client->thread_number);
908 pdu.groupid = cpu_to_le32(client->groupid);
909 convert_thread_options_to_net(&pdu.top, o);
911 return fio_net_send_cmd(client->fd, FIO_NET_CMD_UPDATE_JOB, &pdu, sizeof(pdu), tag, &client->cmd_list);
914 static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
916 dst->max_val = le64_to_cpu(src->max_val);
917 dst->min_val = le64_to_cpu(src->min_val);
918 dst->samples = le64_to_cpu(src->samples);
921 * Floats arrive as IEEE 754 encoded uint64_t, convert back to double
923 dst->mean.u.f = fio_uint64_to_double(le64_to_cpu(dst->mean.u.i));
924 dst->S.u.f = fio_uint64_to_double(le64_to_cpu(dst->S.u.i));
927 static void convert_ts(struct thread_stat *dst, struct thread_stat *src)
931 dst->error = le32_to_cpu(src->error);
932 dst->thread_number = le32_to_cpu(src->thread_number);
933 dst->groupid = le32_to_cpu(src->groupid);
934 dst->pid = le32_to_cpu(src->pid);
935 dst->members = le32_to_cpu(src->members);
936 dst->unified_rw_rep = le32_to_cpu(src->unified_rw_rep);
938 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
939 convert_io_stat(&dst->clat_stat[i], &src->clat_stat[i]);
940 convert_io_stat(&dst->slat_stat[i], &src->slat_stat[i]);
941 convert_io_stat(&dst->lat_stat[i], &src->lat_stat[i]);
942 convert_io_stat(&dst->bw_stat[i], &src->bw_stat[i]);
943 convert_io_stat(&dst->iops_stat[i], &src->iops_stat[i]);
946 dst->usr_time = le64_to_cpu(src->usr_time);
947 dst->sys_time = le64_to_cpu(src->sys_time);
948 dst->ctx = le64_to_cpu(src->ctx);
949 dst->minf = le64_to_cpu(src->minf);
950 dst->majf = le64_to_cpu(src->majf);
951 dst->clat_percentiles = le32_to_cpu(src->clat_percentiles);
952 dst->lat_percentiles = le32_to_cpu(src->lat_percentiles);
953 dst->percentile_precision = le64_to_cpu(src->percentile_precision);
955 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
956 fio_fp64_t *fps = &src->percentile_list[i];
957 fio_fp64_t *fpd = &dst->percentile_list[i];
959 fpd->u.f = fio_uint64_to_double(le64_to_cpu(fps->u.i));
962 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
963 dst->io_u_map[i] = le64_to_cpu(src->io_u_map[i]);
964 dst->io_u_submit[i] = le64_to_cpu(src->io_u_submit[i]);
965 dst->io_u_complete[i] = le64_to_cpu(src->io_u_complete[i]);
968 for (i = 0; i < FIO_IO_U_LAT_N_NR; i++)
969 dst->io_u_lat_n[i] = le64_to_cpu(src->io_u_lat_n[i]);
970 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++)
971 dst->io_u_lat_u[i] = le64_to_cpu(src->io_u_lat_u[i]);
972 for (i = 0; i < FIO_IO_U_LAT_M_NR; i++)
973 dst->io_u_lat_m[i] = le64_to_cpu(src->io_u_lat_m[i]);
975 for (i = 0; i < DDIR_RWDIR_CNT; i++)
976 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
977 dst->io_u_plat[i][j] = le64_to_cpu(src->io_u_plat[i][j]);
979 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
980 dst->total_io_u[i] = le64_to_cpu(src->total_io_u[i]);
981 dst->short_io_u[i] = le64_to_cpu(src->short_io_u[i]);
982 dst->drop_io_u[i] = le64_to_cpu(src->drop_io_u[i]);
985 dst->total_submit = le64_to_cpu(src->total_submit);
986 dst->total_complete = le64_to_cpu(src->total_complete);
988 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
989 dst->io_bytes[i] = le64_to_cpu(src->io_bytes[i]);
990 dst->runtime[i] = le64_to_cpu(src->runtime[i]);
993 dst->total_run_time = le64_to_cpu(src->total_run_time);
994 dst->continue_on_error = le16_to_cpu(src->continue_on_error);
995 dst->total_err_count = le64_to_cpu(src->total_err_count);
996 dst->first_error = le32_to_cpu(src->first_error);
997 dst->kb_base = le32_to_cpu(src->kb_base);
998 dst->unit_base = le32_to_cpu(src->unit_base);
1000 dst->sig_figs = le32_to_cpu(src->sig_figs);
1002 dst->latency_depth = le32_to_cpu(src->latency_depth);
1003 dst->latency_target = le64_to_cpu(src->latency_target);
1004 dst->latency_window = le64_to_cpu(src->latency_window);
1005 dst->latency_percentile.u.f = fio_uint64_to_double(le64_to_cpu(src->latency_percentile.u.i));
1007 dst->nr_block_infos = le64_to_cpu(src->nr_block_infos);
1008 for (i = 0; i < dst->nr_block_infos; i++)
1009 dst->block_infos[i] = le32_to_cpu(src->block_infos[i]);
1011 dst->ss_dur = le64_to_cpu(src->ss_dur);
1012 dst->ss_state = le32_to_cpu(src->ss_state);
1013 dst->ss_head = le32_to_cpu(src->ss_head);
1014 dst->ss_limit.u.f = fio_uint64_to_double(le64_to_cpu(src->ss_limit.u.i));
1015 dst->ss_slope.u.f = fio_uint64_to_double(le64_to_cpu(src->ss_slope.u.i));
1016 dst->ss_deviation.u.f = fio_uint64_to_double(le64_to_cpu(src->ss_deviation.u.i));
1017 dst->ss_criterion.u.f = fio_uint64_to_double(le64_to_cpu(src->ss_criterion.u.i));
1019 if (dst->ss_state & FIO_SS_DATA) {
1020 for (i = 0; i < dst->ss_dur; i++ ) {
1021 dst->ss_iops_data[i] = le64_to_cpu(src->ss_iops_data[i]);
1022 dst->ss_bw_data[i] = le64_to_cpu(src->ss_bw_data[i]);
1027 static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
1031 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1032 dst->max_run[i] = le64_to_cpu(src->max_run[i]);
1033 dst->min_run[i] = le64_to_cpu(src->min_run[i]);
1034 dst->max_bw[i] = le64_to_cpu(src->max_bw[i]);
1035 dst->min_bw[i] = le64_to_cpu(src->min_bw[i]);
1036 dst->iobytes[i] = le64_to_cpu(src->iobytes[i]);
1037 dst->agg[i] = le64_to_cpu(src->agg[i]);
1040 dst->kb_base = le32_to_cpu(src->kb_base);
1041 dst->unit_base = le32_to_cpu(src->unit_base);
1042 dst->sig_figs = le32_to_cpu(src->sig_figs);
1043 dst->groupid = le32_to_cpu(src->groupid);
1044 dst->unified_rw_rep = le32_to_cpu(src->unified_rw_rep);
1047 static void json_object_add_client_info(struct json_object *obj,
1048 struct fio_client *client)
1050 const char *hostname = client->hostname ? client->hostname : "";
1052 json_object_add_value_string(obj, "hostname", hostname);
1053 json_object_add_value_int(obj, "port", client->port);
1056 static void handle_ts(struct fio_client *client, struct fio_net_cmd *cmd)
1058 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
1059 struct flist_head *opt_list = NULL;
1060 struct json_object *tsobj;
1062 if (output_format & FIO_OUTPUT_TERSE)
1065 if (client->opt_lists && p->ts.thread_number <= client->jobs)
1066 opt_list = &client->opt_lists[p->ts.thread_number - 1];
1068 tsobj = show_thread_status(&p->ts, &p->rs, opt_list, NULL);
1069 client->did_stat = true;
1071 json_object_add_client_info(tsobj, client);
1072 json_array_add_value_object(clients_array, tsobj);
1075 if (sum_stat_clients <= 1)
1078 sum_thread_stats(&client_ts, &p->ts, sum_stat_nr == 1);
1079 sum_group_stats(&client_gs, &p->rs);
1081 client_ts.members++;
1082 client_ts.thread_number = p->ts.thread_number;
1083 client_ts.groupid = p->ts.groupid;
1084 client_ts.unified_rw_rep = p->ts.unified_rw_rep;
1085 client_ts.sig_figs = p->ts.sig_figs;
1087 if (++sum_stat_nr == sum_stat_clients) {
1088 strcpy(client_ts.name, "All clients");
1089 tsobj = show_thread_status(&client_ts, &client_gs, NULL, NULL);
1091 json_object_add_client_info(tsobj, client);
1092 json_array_add_value_object(clients_array, tsobj);
1097 static void handle_gs(struct fio_client *client, struct fio_net_cmd *cmd)
1099 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
1101 if (output_format & FIO_OUTPUT_TERSE)
1104 if (output_format & FIO_OUTPUT_NORMAL)
1105 show_group_stats(gs, NULL);
1108 static void handle_job_opt(struct fio_client *client, struct fio_net_cmd *cmd)
1110 struct cmd_job_option *pdu = (struct cmd_job_option *) cmd->payload;
1111 struct print_option *p;
1113 if (!job_opt_object)
1116 pdu->global = le16_to_cpu(pdu->global);
1117 pdu->truncated = le16_to_cpu(pdu->truncated);
1118 pdu->groupid = le32_to_cpu(pdu->groupid);
1120 p = malloc(sizeof(*p));
1121 p->name = strdup((char *) pdu->name);
1122 if (pdu->value[0] != '\0')
1123 p->value = strdup((char *) pdu->value);
1128 const char *pos = "";
1133 json_object_add_value_string(job_opt_object, p->name, pos);
1134 } else if (client->opt_lists) {
1135 struct flist_head *opt_list = &client->opt_lists[pdu->groupid];
1137 flist_add_tail(&p->list, opt_list);
1141 static void handle_text(struct fio_client *client, struct fio_net_cmd *cmd)
1143 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
1144 const char *buf = (const char *) pdu->buf;
1147 struct buf_output out;
1149 buf_output_init(&out);
1151 name = client->name ? client->name : client->hostname;
1153 if (!client->skip_newline && !(output_format & FIO_OUTPUT_TERSE))
1154 __log_buf(&out, "<%s> ", name);
1155 __log_buf(&out, "%s", buf);
1156 log_info_buf(out.buf, out.buflen);
1157 buf_output_free(&out);
1158 client->skip_newline = strchr(buf, '\n') == NULL;
1161 static void convert_agg(struct disk_util_agg *agg)
1165 for (i = 0; i < 2; i++) {
1166 agg->ios[i] = le64_to_cpu(agg->ios[i]);
1167 agg->merges[i] = le64_to_cpu(agg->merges[i]);
1168 agg->sectors[i] = le64_to_cpu(agg->sectors[i]);
1169 agg->ticks[i] = le64_to_cpu(agg->ticks[i]);
1172 agg->io_ticks = le64_to_cpu(agg->io_ticks);
1173 agg->time_in_queue = le64_to_cpu(agg->time_in_queue);
1174 agg->slavecount = le32_to_cpu(agg->slavecount);
1175 agg->max_util.u.f = fio_uint64_to_double(le64_to_cpu(agg->max_util.u.i));
1178 static void convert_dus(struct disk_util_stat *dus)
1182 for (i = 0; i < 2; i++) {
1183 dus->s.ios[i] = le64_to_cpu(dus->s.ios[i]);
1184 dus->s.merges[i] = le64_to_cpu(dus->s.merges[i]);
1185 dus->s.sectors[i] = le64_to_cpu(dus->s.sectors[i]);
1186 dus->s.ticks[i] = le64_to_cpu(dus->s.ticks[i]);
1189 dus->s.io_ticks = le64_to_cpu(dus->s.io_ticks);
1190 dus->s.time_in_queue = le64_to_cpu(dus->s.time_in_queue);
1191 dus->s.msec = le64_to_cpu(dus->s.msec);
1194 static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd)
1196 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
1198 if (output_format & FIO_OUTPUT_TERSE)
1201 if (!client->disk_stats_shown) {
1202 client->disk_stats_shown = true;
1203 if (!(output_format & FIO_OUTPUT_JSON))
1204 log_info("\nDisk stats (read/write):\n");
1207 if (output_format & FIO_OUTPUT_JSON) {
1208 struct json_object *duobj;
1209 json_array_add_disk_util(&du->dus, &du->agg, du_array);
1210 duobj = json_array_last_value_object(du_array);
1211 json_object_add_client_info(duobj, client);
1213 if (output_format & FIO_OUTPUT_NORMAL)
1214 print_disk_util(&du->dus, &du->agg, 0, NULL);
1217 static void convert_jobs_eta(struct jobs_eta *je)
1221 je->nr_running = le32_to_cpu(je->nr_running);
1222 je->nr_ramp = le32_to_cpu(je->nr_ramp);
1223 je->nr_pending = le32_to_cpu(je->nr_pending);
1224 je->nr_setting_up = le32_to_cpu(je->nr_setting_up);
1225 je->files_open = le32_to_cpu(je->files_open);
1227 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1228 je->m_rate[i] = le64_to_cpu(je->m_rate[i]);
1229 je->t_rate[i] = le64_to_cpu(je->t_rate[i]);
1230 je->m_iops[i] = le32_to_cpu(je->m_iops[i]);
1231 je->t_iops[i] = le32_to_cpu(je->t_iops[i]);
1232 je->rate[i] = le64_to_cpu(je->rate[i]);
1233 je->iops[i] = le32_to_cpu(je->iops[i]);
1236 je->elapsed_sec = le64_to_cpu(je->elapsed_sec);
1237 je->eta_sec = le64_to_cpu(je->eta_sec);
1238 je->nr_threads = le32_to_cpu(je->nr_threads);
1239 je->is_pow2 = le32_to_cpu(je->is_pow2);
1240 je->unit_base = le32_to_cpu(je->unit_base);
1241 je->sig_figs = le32_to_cpu(je->sig_figs);
1244 void fio_client_sum_jobs_eta(struct jobs_eta *dst, struct jobs_eta *je)
1248 dst->nr_running += je->nr_running;
1249 dst->nr_ramp += je->nr_ramp;
1250 dst->nr_pending += je->nr_pending;
1251 dst->nr_setting_up += je->nr_setting_up;
1252 dst->files_open += je->files_open;
1254 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1255 dst->m_rate[i] += je->m_rate[i];
1256 dst->t_rate[i] += je->t_rate[i];
1257 dst->m_iops[i] += je->m_iops[i];
1258 dst->t_iops[i] += je->t_iops[i];
1259 dst->rate[i] += je->rate[i];
1260 dst->iops[i] += je->iops[i];
1263 dst->elapsed_sec += je->elapsed_sec;
1265 if (je->eta_sec > dst->eta_sec)
1266 dst->eta_sec = je->eta_sec;
1268 dst->nr_threads += je->nr_threads;
1271 * This wont be correct for multiple strings, but at least it
1272 * works for the basic cases.
1274 strcpy((char *) dst->run_str, (char *) je->run_str);
1277 static bool remove_reply_cmd(struct fio_client *client, struct fio_net_cmd *cmd)
1279 struct fio_net_cmd_reply *reply = NULL;
1280 struct flist_head *entry;
1282 flist_for_each(entry, &client->cmd_list) {
1283 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
1285 if (cmd->tag == (uintptr_t) reply)
1292 log_err("fio: client: unable to find matching tag (%llx)\n", (unsigned long long) cmd->tag);
1296 flist_del(&reply->list);
1297 cmd->tag = reply->saved_tag;
1302 int fio_client_wait_for_reply(struct fio_client *client, uint64_t tag)
1305 struct fio_net_cmd_reply *reply = NULL;
1306 struct flist_head *entry;
1308 flist_for_each(entry, &client->cmd_list) {
1309 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
1311 if (tag == (uintptr_t) reply)
1326 static void handle_eta(struct fio_client *client, struct fio_net_cmd *cmd)
1328 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
1329 struct client_eta *eta = (struct client_eta *) (uintptr_t) cmd->tag;
1331 dprint(FD_NET, "client: got eta tag %p, %d\n", eta, eta->pending);
1333 assert(client->eta_in_flight == eta);
1335 client->eta_in_flight = NULL;
1336 flist_del_init(&client->eta_list);
1337 client->eta_timeouts = 0;
1339 if (client->ops->jobs_eta)
1340 client->ops->jobs_eta(client, je);
1342 fio_client_sum_jobs_eta(&eta->eta, je);
1343 fio_client_dec_jobs_eta(eta, client->ops->eta);
1346 static void client_flush_hist_samples(FILE *f, int hist_coarseness, void *samples,
1347 uint64_t sample_size)
1349 struct io_sample *s;
1351 uint64_t i, j, nr_samples;
1352 struct io_u_plat_entry *entry;
1353 uint64_t *io_u_plat;
1355 int stride = 1 << hist_coarseness;
1360 s = __get_sample(samples, 0, 0);
1361 log_offset = (s->__ddir & LOG_OFFSET_SAMPLE_BIT) != 0;
1363 nr_samples = sample_size / __log_entry_sz(log_offset);
1365 for (i = 0; i < nr_samples; i++) {
1367 s = (struct io_sample *)((char *)__get_sample(samples, log_offset, i) +
1368 i * sizeof(struct io_u_plat_entry));
1370 entry = s->data.plat_entry;
1371 io_u_plat = entry->io_u_plat;
1373 fprintf(f, "%lu, %u, %llu, ", (unsigned long) s->time,
1374 io_sample_ddir(s), (unsigned long long) s->bs);
1375 for (j = 0; j < FIO_IO_U_PLAT_NR - stride; j += stride) {
1376 fprintf(f, "%llu, ", (unsigned long long)hist_sum(j, stride, io_u_plat, NULL));
1378 fprintf(f, "%llu\n", (unsigned long long)
1379 hist_sum(FIO_IO_U_PLAT_NR - stride, stride, io_u_plat, NULL));
1384 static int fio_client_handle_iolog(struct fio_client *client,
1385 struct fio_net_cmd *cmd)
1387 struct cmd_iolog_pdu *pdu = NULL;
1389 char *log_pathname = NULL;
1392 pdu = convert_iolog(cmd, &store_direct);
1394 log_err("fio: failed converting IO log\n");
1399 /* allocate buffer big enough for next sprintf() call */
1400 log_pathname = malloc(10 + strlen((char *)pdu->name) +
1401 strlen(client->hostname));
1402 if (!log_pathname) {
1403 log_err("fio: memory allocation of unique pathname failed\n");
1407 /* generate a unique pathname for the log file using hostname */
1408 sprintf(log_pathname, "%s.%s", pdu->name, client->hostname);
1415 fd = open((const char *) log_pathname,
1416 O_WRONLY | O_CREAT | O_TRUNC, 0644);
1418 log_err("fio: open log %s: %s\n",
1419 log_pathname, strerror(errno));
1424 sz = cmd->pdu_len - sizeof(*pdu);
1425 wrote = write(fd, pdu->samples, sz);
1429 log_err("fio: short write on compressed log\n");
1437 f = fopen((const char *) log_pathname, "w");
1439 log_err("fio: fopen log %s : %s\n",
1440 log_pathname, strerror(errno));
1445 if (pdu->log_type == IO_LOG_TYPE_HIST) {
1446 client_flush_hist_samples(f, pdu->log_hist_coarseness, pdu->samples,
1447 pdu->nr_samples * sizeof(struct io_sample));
1449 flush_samples(f, pdu->samples,
1450 pdu->nr_samples * sizeof(struct io_sample));
1457 if (pdu && pdu != (void *) cmd->payload)
1466 static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd)
1468 struct cmd_probe_reply_pdu *probe = (struct cmd_probe_reply_pdu *) cmd->payload;
1469 const char *os, *arch;
1472 if (output_format & FIO_OUTPUT_TERSE)
1475 os = fio_get_os_string(probe->os);
1479 arch = fio_get_arch_string(probe->arch);
1483 sprintf(bit, "%d-bit", probe->bpp * 8);
1484 probe->flags = le64_to_cpu(probe->flags);
1486 if (!(output_format & FIO_OUTPUT_JSON))
1487 log_info("hostname=%s, be=%u, %s, os=%s, arch=%s, fio=%s, flags=%lx\n",
1488 probe->hostname, probe->bigendian, bit, os, arch,
1489 probe->fio_version, (unsigned long) probe->flags);
1492 client->name = strdup((char *) probe->hostname);
1495 static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd)
1497 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
1499 client->state = Client_started;
1500 client->jobs = le32_to_cpu(pdu->jobs);
1501 client->nr_stat = le32_to_cpu(pdu->stat_outputs);
1506 if (client->opt_lists)
1507 free(client->opt_lists);
1509 client->opt_lists = malloc(client->jobs * sizeof(struct flist_head));
1510 for (i = 0; i < client->jobs; i++)
1511 INIT_FLIST_HEAD(&client->opt_lists[i]);
1514 sum_stat_clients += client->nr_stat;
1517 static void handle_stop(struct fio_client *client)
1520 log_info("client <%s>: exited with error %d\n", client->hostname, client->error);
1523 static void convert_stop(struct fio_net_cmd *cmd)
1525 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
1527 pdu->error = le32_to_cpu(pdu->error);
1530 static void convert_text(struct fio_net_cmd *cmd)
1532 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
1534 pdu->level = le32_to_cpu(pdu->level);
1535 pdu->buf_len = le32_to_cpu(pdu->buf_len);
1536 pdu->log_sec = le64_to_cpu(pdu->log_sec);
1537 pdu->log_usec = le64_to_cpu(pdu->log_usec);
1540 static struct cmd_iolog_pdu *convert_iolog_gz(struct fio_net_cmd *cmd,
1541 struct cmd_iolog_pdu *pdu)
1544 struct cmd_iolog_pdu *ret;
1546 uint64_t nr_samples;
1550 stream.zalloc = Z_NULL;
1551 stream.zfree = Z_NULL;
1552 stream.opaque = Z_NULL;
1553 stream.avail_in = 0;
1554 stream.next_in = Z_NULL;
1556 if (inflateInit(&stream) != Z_OK)
1560 * Get header first, it's not compressed
1562 nr_samples = le64_to_cpu(pdu->nr_samples);
1564 if (pdu->log_type == IO_LOG_TYPE_HIST)
1565 total = nr_samples * (__log_entry_sz(le32_to_cpu(pdu->log_offset)) +
1566 sizeof(struct io_u_plat_entry));
1568 total = nr_samples * __log_entry_sz(le32_to_cpu(pdu->log_offset));
1569 ret = malloc(total + sizeof(*pdu));
1570 ret->nr_samples = nr_samples;
1572 memcpy(ret, pdu, sizeof(*pdu));
1574 p = (char *) ret + sizeof(*pdu);
1576 stream.avail_in = cmd->pdu_len - sizeof(*pdu);
1577 stream.next_in = (void *)((char *) pdu + sizeof(*pdu));
1578 while (stream.avail_in) {
1579 unsigned int this_chunk = 65536;
1580 unsigned int this_len;
1583 if (this_chunk > total)
1586 stream.avail_out = this_chunk;
1587 stream.next_out = (void *)p;
1588 err = inflate(&stream, Z_NO_FLUSH);
1589 /* may be Z_OK, or Z_STREAM_END */
1591 log_err("fio: inflate error %d\n", err);
1597 this_len = this_chunk - stream.avail_out;
1603 inflateEnd(&stream);
1611 * This has been compressed on the server side, since it can be big.
1614 static struct cmd_iolog_pdu *convert_iolog(struct fio_net_cmd *cmd,
1617 struct cmd_iolog_pdu *pdu = (struct cmd_iolog_pdu *) cmd->payload;
1618 struct cmd_iolog_pdu *ret;
1623 *store_direct = false;
1626 * Convert if compressed and we support it. If it's not
1627 * compressed, we need not do anything.
1629 compressed = le32_to_cpu(pdu->compressed);
1630 if (compressed == XMIT_COMPRESSED) {
1632 log_err("fio: server sent compressed data by mistake\n");
1635 ret = convert_iolog_gz(cmd, pdu);
1637 log_err("fio: failed decompressing log\n");
1640 } else if (compressed == STORE_COMPRESSED) {
1641 *store_direct = true;
1646 ret->nr_samples = le64_to_cpu(ret->nr_samples);
1647 ret->thread_number = le32_to_cpu(ret->thread_number);
1648 ret->log_type = le32_to_cpu(ret->log_type);
1649 ret->compressed = le32_to_cpu(ret->compressed);
1650 ret->log_offset = le32_to_cpu(ret->log_offset);
1651 ret->log_hist_coarseness = le32_to_cpu(ret->log_hist_coarseness);
1656 samples = &ret->samples[0];
1657 for (i = 0; i < ret->nr_samples; i++) {
1658 struct io_sample *s;
1660 s = __get_sample(samples, ret->log_offset, i);
1661 if (ret->log_type == IO_LOG_TYPE_HIST)
1662 s = (struct io_sample *)((char *)s + sizeof(struct io_u_plat_entry) * i);
1664 s->time = le64_to_cpu(s->time);
1665 s->data.val = le64_to_cpu(s->data.val);
1666 s->__ddir = le32_to_cpu(s->__ddir);
1667 s->bs = le64_to_cpu(s->bs);
1669 if (ret->log_offset) {
1670 struct io_sample_offset *so = (void *) s;
1672 so->offset = le64_to_cpu(so->offset);
1675 if (ret->log_type == IO_LOG_TYPE_HIST) {
1676 s->data.plat_entry = (struct io_u_plat_entry *)(((char *)s) + sizeof(*s));
1677 s->data.plat_entry->list.next = NULL;
1678 s->data.plat_entry->list.prev = NULL;
1685 static void sendfile_reply(int fd, struct cmd_sendfile_reply *rep,
1686 size_t size, uint64_t tag)
1688 rep->error = cpu_to_le32(rep->error);
1689 fio_net_send_cmd(fd, FIO_NET_CMD_SENDFILE, rep, size, &tag, NULL);
1692 static int fio_send_file(struct fio_client *client, struct cmd_sendfile *pdu,
1695 struct cmd_sendfile_reply *rep;
1700 size = sizeof(*rep);
1703 if (stat((char *)pdu->path, &sb) < 0) {
1706 sendfile_reply(client->fd, rep, size, tag);
1712 rep = realloc(rep, size);
1713 rep->size = cpu_to_le32((uint32_t) sb.st_size);
1715 fd = open((char *)pdu->path, O_RDONLY);
1719 rep->error = read_data(fd, &rep->data, sb.st_size);
1720 sendfile_reply(client->fd, rep, size, tag);
1726 int fio_handle_client(struct fio_client *client)
1728 struct client_ops *ops = client->ops;
1729 struct fio_net_cmd *cmd;
1732 dprint(FD_NET, "client: handle %s\n", client->hostname);
1734 cmd = fio_net_recv_cmd(client->fd, true);
1738 dprint(FD_NET, "client: got cmd op %s from %s (pdu=%u)\n",
1739 fio_server_op(cmd->opcode), client->hostname, cmd->pdu_len);
1741 client->last_cmd = cmd->opcode;
1743 switch (cmd->opcode) {
1744 case FIO_NET_CMD_QUIT:
1746 ops->quit(client, cmd);
1747 remove_client(client);
1749 case FIO_NET_CMD_TEXT:
1751 ops->text(client, cmd);
1753 case FIO_NET_CMD_DU: {
1754 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
1756 convert_dus(&du->dus);
1757 convert_agg(&du->agg);
1759 ops->disk_util(client, cmd);
1762 case FIO_NET_CMD_TS: {
1763 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
1765 dprint(FD_NET, "client: ts->ss_state = %u\n", (unsigned int) le32_to_cpu(p->ts.ss_state));
1766 if (le32_to_cpu(p->ts.ss_state) & FIO_SS_DATA) {
1767 dprint(FD_NET, "client: received steadystate ring buffers\n");
1769 size = le64_to_cpu(p->ts.ss_dur);
1770 p->ts.ss_iops_data = (uint64_t *) ((struct cmd_ts_pdu *)cmd->payload + 1);
1771 p->ts.ss_bw_data = p->ts.ss_iops_data + size;
1774 convert_ts(&p->ts, &p->ts);
1775 convert_gs(&p->rs, &p->rs);
1777 ops->thread_status(client, cmd);
1780 case FIO_NET_CMD_GS: {
1781 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
1785 ops->group_stats(client, cmd);
1788 case FIO_NET_CMD_ETA: {
1789 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
1791 if (!remove_reply_cmd(client, cmd))
1793 convert_jobs_eta(je);
1794 handle_eta(client, cmd);
1797 case FIO_NET_CMD_PROBE:
1798 remove_reply_cmd(client, cmd);
1799 ops->probe(client, cmd);
1801 case FIO_NET_CMD_SERVER_START:
1802 client->state = Client_running;
1804 ops->job_start(client, cmd);
1806 case FIO_NET_CMD_START: {
1807 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
1809 pdu->jobs = le32_to_cpu(pdu->jobs);
1810 ops->start(client, cmd);
1813 case FIO_NET_CMD_STOP: {
1814 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
1817 client->state = Client_stopped;
1818 client->error = le32_to_cpu(pdu->error);
1819 client->signal = le32_to_cpu(pdu->signal);
1823 case FIO_NET_CMD_ADD_JOB: {
1824 struct cmd_add_job_pdu *pdu = (struct cmd_add_job_pdu *) cmd->payload;
1826 client->thread_number = le32_to_cpu(pdu->thread_number);
1827 client->groupid = le32_to_cpu(pdu->groupid);
1830 ops->add_job(client, cmd);
1833 case FIO_NET_CMD_IOLOG:
1834 fio_client_handle_iolog(client, cmd);
1836 case FIO_NET_CMD_UPDATE_JOB:
1837 ops->update_job(client, cmd);
1838 remove_reply_cmd(client, cmd);
1840 case FIO_NET_CMD_VTRIGGER: {
1841 struct all_io_list *pdu = (struct all_io_list *) cmd->payload;
1846 strcpy(buf, aux_path);
1850 __verify_save_state(pdu, server_name(client, &buf[off], sizeof(buf) - off));
1851 exec_trigger(trigger_cmd);
1854 case FIO_NET_CMD_SENDFILE: {
1855 struct cmd_sendfile *pdu = (struct cmd_sendfile *) cmd->payload;
1856 fio_send_file(client, pdu, cmd->tag);
1859 case FIO_NET_CMD_JOB_OPT: {
1860 handle_job_opt(client, cmd);
1864 log_err("fio: unknown client op: %s\n", fio_server_op(cmd->opcode));
1872 int fio_clients_send_trigger(const char *cmd)
1874 struct flist_head *entry;
1875 struct fio_client *client;
1878 dprint(FD_NET, "client: send vtrigger: %s\n", cmd);
1885 flist_for_each(entry, &client_list) {
1886 struct cmd_vtrigger_pdu *pdu;
1888 client = flist_entry(entry, struct fio_client, list);
1890 pdu = malloc(sizeof(*pdu) + slen);
1891 pdu->len = cpu_to_le16((uint16_t) slen);
1893 memcpy(pdu->cmd, cmd, slen);
1894 fio_net_send_cmd(client->fd, FIO_NET_CMD_VTRIGGER, pdu,
1895 sizeof(*pdu) + slen, NULL, NULL);
1902 static void request_client_etas(struct client_ops *ops)
1904 struct fio_client *client;
1905 struct flist_head *entry;
1906 struct client_eta *eta;
1909 if (eta_print == FIO_ETA_NEVER)
1912 dprint(FD_NET, "client: request eta (%d)\n", nr_clients);
1914 eta = calloc(1, sizeof(*eta) + __THREAD_RUNSTR_SZ(REAL_MAX_JOBS));
1915 eta->pending = nr_clients;
1917 flist_for_each(entry, &client_list) {
1918 client = flist_entry(entry, struct fio_client, list);
1920 if (!flist_empty(&client->eta_list)) {
1924 if (client->state != Client_running)
1927 assert(!client->eta_in_flight);
1928 flist_add_tail(&client->eta_list, &eta_list);
1929 client->eta_in_flight = eta;
1930 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_SEND_ETA,
1931 (uintptr_t) eta, &client->cmd_list);
1935 if (!fio_client_dec_jobs_eta(eta, ops->eta))
1939 dprint(FD_NET, "client: requested eta tag %p\n", eta);
1943 * A single SEND_ETA timeout isn't fatal. Attempt to recover.
1945 static int handle_cmd_timeout(struct fio_client *client,
1946 struct fio_net_cmd_reply *reply)
1948 uint16_t reply_opcode = reply->opcode;
1950 flist_del(&reply->list);
1953 if (reply_opcode != FIO_NET_CMD_SEND_ETA)
1956 log_info("client <%s>: timeout on SEND_ETA\n", client->hostname);
1958 flist_del_init(&client->eta_list);
1959 if (client->eta_in_flight) {
1960 fio_client_dec_jobs_eta(client->eta_in_flight, client->ops->eta);
1961 client->eta_in_flight = NULL;
1965 * If we fail 5 in a row, give up...
1967 if (client->eta_timeouts++ > 5)
1973 static int client_check_cmd_timeout(struct fio_client *client,
1974 struct timespec *now)
1976 struct fio_net_cmd_reply *reply;
1977 struct flist_head *entry, *tmp;
1980 flist_for_each_safe(entry, tmp, &client->cmd_list) {
1983 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
1985 if (mtime_since(&reply->ts, now) < FIO_NET_CLIENT_TIMEOUT)
1989 if (!handle_cmd_timeout(client, reply))
1992 log_err("fio: client %s, timeout on cmd %s\n", client->hostname,
1997 return flist_empty(&client->cmd_list) && ret;
2000 static int fio_check_clients_timed_out(void)
2002 struct fio_client *client;
2003 struct flist_head *entry, *tmp;
2007 fio_gettime(&ts, NULL);
2009 flist_for_each_safe(entry, tmp, &client_list) {
2010 client = flist_entry(entry, struct fio_client, list);
2012 if (flist_empty(&client->cmd_list))
2015 if (!client_check_cmd_timeout(client, &ts))
2018 if (client->ops->timed_out)
2019 client->ops->timed_out(client);
2021 log_err("fio: client %s timed out\n", client->hostname);
2023 if (client->last_cmd != FIO_NET_CMD_VTRIGGER)
2024 client->error = ETIMEDOUT;
2026 log_info("fio: ignoring timeout due to vtrigger\n");
2027 remove_client(client);
2034 int fio_handle_clients(struct client_ops *ops)
2036 struct pollfd *pfds;
2037 int i, ret = 0, retval = 0;
2039 fio_gettime(&eta_ts, NULL);
2041 pfds = malloc(nr_clients * sizeof(struct pollfd));
2043 init_thread_stat(&client_ts);
2044 init_group_run_stat(&client_gs);
2046 while (!exit_backend && nr_clients) {
2047 struct flist_head *entry, *tmp;
2048 struct fio_client *client;
2051 flist_for_each_safe(entry, tmp, &client_list) {
2052 client = flist_entry(entry, struct fio_client, list);
2054 if (!client->sent_job && !client->ops->stay_connected &&
2055 flist_empty(&client->cmd_list)) {
2056 remove_client(client);
2060 pfds[i].fd = client->fd;
2061 pfds[i].events = POLLIN;
2068 assert(i == nr_clients);
2074 fio_gettime(&ts, NULL);
2075 if (eta_time_within_slack(mtime_since(&eta_ts, &ts))) {
2076 request_client_etas(ops);
2077 memcpy(&eta_ts, &ts, sizeof(ts));
2079 if (fio_check_clients_timed_out())
2083 check_trigger_file();
2085 timeout = min(100u, ops->eta_msec);
2087 ret = poll(pfds, nr_clients, timeout);
2091 log_err("fio: poll clients: %s\n", strerror(errno));
2097 for (i = 0; i < nr_clients; i++) {
2098 if (!(pfds[i].revents & POLLIN))
2101 client = find_client_by_fd(pfds[i].fd);
2103 log_err("fio: unknown client fd %ld\n", (long) pfds[i].fd);
2106 if (!fio_handle_client(client)) {
2107 log_info("client: host=%s disconnected\n",
2109 remove_client(client);
2111 } else if (client->error)
2113 fio_put_client(client);
2117 fio_client_json_fini();
2120 return retval || error_clients;
2123 static void client_display_thread_status(struct jobs_eta *je)
2125 if (!(output_format & FIO_OUTPUT_JSON))
2126 display_thread_status(je);