9 #include <sys/socket.h>
11 #include <netinet/in.h>
12 #include <arpa/inet.h>
24 #include "verify-state.h"
26 static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd);
27 static void handle_ts(struct fio_client *client, struct fio_net_cmd *cmd);
28 static void handle_gs(struct fio_client *client, struct fio_net_cmd *cmd);
29 static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd);
30 static void handle_text(struct fio_client *client, struct fio_net_cmd *cmd);
31 static void handle_stop(struct fio_client *client);
32 static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd);
34 static void convert_text(struct fio_net_cmd *cmd);
36 struct client_ops fio_client_ops = {
38 .disk_util = handle_du,
39 .thread_status = handle_ts,
40 .group_stats = handle_gs,
42 .start = handle_start,
43 .eta = display_thread_status,
44 .probe = handle_probe,
45 .eta_msec = FIO_CLIENT_DEF_ETA_MSEC,
46 .client_type = FIO_CLIENT_TYPE_CLI,
49 static struct timespec eta_ts;
51 static FLIST_HEAD(client_list);
52 static FLIST_HEAD(eta_list);
54 static FLIST_HEAD(arg_list);
56 struct thread_stat client_ts;
57 struct group_run_stats client_gs;
60 static int sum_stat_nr;
61 static struct json_object *root = NULL;
62 static struct json_object *job_opt_object = NULL;
63 static struct json_array *clients_array = NULL;
64 static struct json_array *du_array = NULL;
66 static int error_clients;
68 #define FIO_CLIENT_HASH_BITS 7
69 #define FIO_CLIENT_HASH_SZ (1 << FIO_CLIENT_HASH_BITS)
70 #define FIO_CLIENT_HASH_MASK (FIO_CLIENT_HASH_SZ - 1)
71 static struct flist_head client_hash[FIO_CLIENT_HASH_SZ];
73 static struct cmd_iolog_pdu *convert_iolog(struct fio_net_cmd *, bool *);
75 static void fio_client_add_hash(struct fio_client *client)
77 int bucket = hash_long(client->fd, FIO_CLIENT_HASH_BITS);
79 bucket &= FIO_CLIENT_HASH_MASK;
80 flist_add(&client->hash_list, &client_hash[bucket]);
83 static void fio_client_remove_hash(struct fio_client *client)
85 if (!flist_empty(&client->hash_list))
86 flist_del_init(&client->hash_list);
89 static void fio_init fio_client_hash_init(void)
93 for (i = 0; i < FIO_CLIENT_HASH_SZ; i++)
94 INIT_FLIST_HEAD(&client_hash[i]);
97 static int read_data(int fd, void *data, size_t size)
102 ret = read(fd, data, size);
104 if (errno == EAGAIN || errno == EINTR)
121 static int read_ini_data(int fd, void *data, size_t size)
132 fp = fdopen(dupfd, "r");
138 char buf[OPT_LEN_MAX+1], *sub;
140 if (!fgets(buf, sizeof(buf), fp)) {
142 if (errno == EAGAIN || errno == EINTR)
149 sub = fio_option_dup_subs(buf);
151 if (len + 1 > size) {
152 log_err("fio: no space left to read data\n");
169 static void fio_client_json_init(void)
174 if (!(output_format & FIO_OUTPUT_JSON))
178 os_ctime_r((const time_t *) &time_p, time_buf, sizeof(time_buf));
179 time_buf[strlen(time_buf) - 1] = '\0';
181 root = json_create_object();
182 json_object_add_value_string(root, "fio version", fio_version_string);
183 json_object_add_value_int(root, "timestamp", time_p);
184 json_object_add_value_string(root, "time", time_buf);
186 job_opt_object = json_create_object();
187 json_object_add_value_object(root, "global options", job_opt_object);
188 clients_array = json_create_array();
189 json_object_add_value_array(root, "client_stats", clients_array);
190 du_array = json_create_array();
191 json_object_add_value_array(root, "disk_util", du_array);
194 static void fio_client_json_fini(void)
196 if (!(output_format & FIO_OUTPUT_JSON))
200 json_print_object(root, NULL);
202 json_free_object(root);
204 clients_array = NULL;
208 static struct fio_client *find_client_by_fd(int fd)
210 int bucket = hash_long(fd, FIO_CLIENT_HASH_BITS) & FIO_CLIENT_HASH_MASK;
211 struct fio_client *client;
212 struct flist_head *entry;
214 flist_for_each(entry, &client_hash[bucket]) {
215 client = flist_entry(entry, struct fio_client, hash_list);
217 if (client->fd == fd) {
226 void fio_put_client(struct fio_client *client)
231 free(client->hostname);
236 while (client->nr_files) {
237 struct client_file *cf = &client->files[--client->nr_files];
243 if (client->opt_lists)
244 free(client->opt_lists);
246 if (!client->did_stat)
255 static int fio_client_dec_jobs_eta(struct client_eta *eta, client_eta_op eta_fn)
257 if (!--eta->pending) {
266 static void fio_drain_client_text(struct fio_client *client)
269 struct fio_net_cmd *cmd;
271 cmd = fio_net_recv_cmd(client->fd, false);
275 if (cmd->opcode == FIO_NET_CMD_TEXT) {
277 client->ops->text(client, cmd);
284 static void remove_client(struct fio_client *client)
286 assert(client->refs);
288 dprint(FD_NET, "client: removed <%s>\n", client->hostname);
290 fio_drain_client_text(client);
292 if (!flist_empty(&client->list))
293 flist_del_init(&client->list);
295 fio_client_remove_hash(client);
297 if (!flist_empty(&client->eta_list)) {
298 flist_del_init(&client->eta_list);
299 fio_client_dec_jobs_eta(client->eta_in_flight, client->ops->eta);
305 if (client->ops->removed)
306 client->ops->removed(client);
309 fio_put_client(client);
312 struct fio_client *fio_get_client(struct fio_client *client)
318 static void __fio_client_add_cmd_option(struct fio_client *client,
323 index = client->argc++;
324 client->argv = realloc(client->argv, sizeof(char *) * client->argc);
325 client->argv[index] = strdup(opt);
326 dprint(FD_NET, "client: add cmd %d: %s\n", index, opt);
329 void fio_client_add_cmd_option(void *cookie, const char *opt)
331 struct fio_client *client = cookie;
332 struct flist_head *entry;
337 __fio_client_add_cmd_option(client, opt);
340 * Duplicate arguments to shared client group
342 flist_for_each(entry, &arg_list) {
343 client = flist_entry(entry, struct fio_client, arg_list);
345 __fio_client_add_cmd_option(client, opt);
349 struct fio_client *fio_client_add_explicit(struct client_ops *ops,
350 const char *hostname, int type,
353 struct fio_client *client;
355 client = malloc(sizeof(*client));
356 memset(client, 0, sizeof(*client));
358 INIT_FLIST_HEAD(&client->list);
359 INIT_FLIST_HEAD(&client->hash_list);
360 INIT_FLIST_HEAD(&client->arg_list);
361 INIT_FLIST_HEAD(&client->eta_list);
362 INIT_FLIST_HEAD(&client->cmd_list);
364 client->hostname = strdup(hostname);
366 if (type == Fio_client_socket)
367 client->is_sock = true;
371 ipv6 = type == Fio_client_ipv6;
372 if (fio_server_parse_host(hostname, ipv6,
373 &client->addr.sin_addr,
374 &client->addr6.sin6_addr))
383 client->type = ops->client_type;
385 __fio_client_add_cmd_option(client, "fio");
387 flist_add(&client->list, &client_list);
389 dprint(FD_NET, "client: added <%s>\n", client->hostname);
396 int fio_client_add_ini_file(void *cookie, const char *ini_file, bool remote)
398 struct fio_client *client = cookie;
399 struct client_file *cf;
406 dprint(FD_NET, "client <%s>: add ini %s\n", client->hostname, ini_file);
408 new_size = (client->nr_files + 1) * sizeof(struct client_file);
409 new_files = realloc(client->files, new_size);
413 client->files = new_files;
414 cf = &client->files[client->nr_files];
415 cf->file = strdup(ini_file);
421 int fio_client_add(struct client_ops *ops, const char *hostname, void **cookie)
423 struct fio_client *existing = *cookie;
424 struct fio_client *client;
428 * We always add our "exec" name as the option, hence 1
431 if (existing->argc == 1)
432 flist_add_tail(&existing->arg_list, &arg_list);
434 while (!flist_empty(&arg_list))
435 flist_del_init(arg_list.next);
439 client = malloc(sizeof(*client));
440 memset(client, 0, sizeof(*client));
442 INIT_FLIST_HEAD(&client->list);
443 INIT_FLIST_HEAD(&client->hash_list);
444 INIT_FLIST_HEAD(&client->arg_list);
445 INIT_FLIST_HEAD(&client->eta_list);
446 INIT_FLIST_HEAD(&client->cmd_list);
448 if (fio_server_parse_string(hostname, &client->hostname,
449 &client->is_sock, &client->port,
450 &client->addr.sin_addr,
451 &client->addr6.sin6_addr,
458 client->type = ops->client_type;
460 __fio_client_add_cmd_option(client, "fio");
462 flist_add(&client->list, &client_list);
464 dprint(FD_NET, "client: added <%s>\n", client->hostname);
469 static const char *server_name(struct fio_client *client, char *buf,
475 from = inet_ntop(AF_INET6, (struct sockaddr *) &client->addr6.sin6_addr, buf, bufsize);
476 else if (client->is_sock)
479 from = inet_ntop(AF_INET, (struct sockaddr *) &client->addr.sin_addr, buf, bufsize);
484 static void probe_client(struct fio_client *client)
486 struct cmd_client_probe_pdu pdu;
491 dprint(FD_NET, "client: send probe\n");
494 pdu.flags = __le64_to_cpu(FIO_PROBE_FLAG_ZLIB);
499 sname = server_name(client, buf, sizeof(buf));
500 memset(pdu.server, 0, sizeof(pdu.server));
501 strncpy((char *) pdu.server, sname, sizeof(pdu.server) - 1);
503 fio_net_send_cmd(client->fd, FIO_NET_CMD_PROBE, &pdu, sizeof(pdu), &tag, &client->cmd_list);
506 static int fio_client_connect_ip(struct fio_client *client)
508 struct sockaddr *addr;
513 client->addr6.sin6_family = AF_INET6;
514 client->addr6.sin6_port = htons(client->port);
516 addr = (struct sockaddr *) &client->addr6;
517 socklen = sizeof(client->addr6);
519 client->addr.sin_family = AF_INET;
520 client->addr.sin_port = htons(client->port);
522 addr = (struct sockaddr *) &client->addr;
523 socklen = sizeof(client->addr);
526 fd = socket(domain, SOCK_STREAM, 0);
530 log_err("fio: socket: %s\n", strerror(errno));
534 if (connect(fd, addr, socklen) < 0) {
537 log_err("fio: connect: %s\n", strerror(errno));
538 log_err("fio: failed to connect to %s:%u\n", client->hostname,
547 static int fio_client_connect_sock(struct fio_client *client)
549 struct sockaddr_un *addr = &client->addr_un;
553 memset(addr, 0, sizeof(*addr));
554 addr->sun_family = AF_UNIX;
555 strncpy(addr->sun_path, client->hostname, sizeof(addr->sun_path) - 1);
557 fd = socket(AF_UNIX, SOCK_STREAM, 0);
561 log_err("fio: socket: %s\n", strerror(errno));
565 len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
566 if (connect(fd, (struct sockaddr *) addr, len) < 0) {
569 log_err("fio: connect; %s\n", strerror(errno));
577 int fio_client_connect(struct fio_client *client)
581 dprint(FD_NET, "client: connect to host %s\n", client->hostname);
584 fd = fio_client_connect_sock(client);
586 fd = fio_client_connect_ip(client);
588 dprint(FD_NET, "client: %s connected %d\n", client->hostname, fd);
594 fio_client_add_hash(client);
595 client->state = Client_connected;
597 probe_client(client);
601 int fio_client_terminate(struct fio_client *client)
603 return fio_net_send_quit(client->fd);
606 static void fio_clients_terminate(void)
608 struct flist_head *entry;
609 struct fio_client *client;
611 dprint(FD_NET, "client: terminate clients\n");
613 flist_for_each(entry, &client_list) {
614 client = flist_entry(entry, struct fio_client, list);
615 fio_client_terminate(client);
619 static void sig_int(int sig)
621 dprint(FD_NET, "client: got signal %d\n", sig);
622 fio_clients_terminate();
625 static void client_signal_handler(void)
627 struct sigaction act;
629 memset(&act, 0, sizeof(act));
630 act.sa_handler = sig_int;
631 act.sa_flags = SA_RESTART;
632 sigaction(SIGINT, &act, NULL);
634 memset(&act, 0, sizeof(act));
635 act.sa_handler = sig_int;
636 act.sa_flags = SA_RESTART;
637 sigaction(SIGTERM, &act, NULL);
639 /* Windows uses SIGBREAK as a quit signal from other applications */
641 memset(&act, 0, sizeof(act));
642 act.sa_handler = sig_int;
643 act.sa_flags = SA_RESTART;
644 sigaction(SIGBREAK, &act, NULL);
647 memset(&act, 0, sizeof(act));
648 act.sa_handler = sig_show_status;
649 act.sa_flags = SA_RESTART;
650 sigaction(SIGUSR1, &act, NULL);
653 static int send_client_cmd_line(struct fio_client *client)
655 struct cmd_single_line_pdu *cslp;
656 struct cmd_line_pdu *clp;
657 unsigned long offset;
663 dprint(FD_NET, "client: send cmdline %d\n", client->argc);
665 lens = malloc(client->argc * sizeof(unsigned int));
668 * Find out how much mem we need
670 for (i = 0, mem = 0; i < client->argc; i++) {
671 lens[i] = strlen(client->argv[i]) + 1;
676 * We need one cmd_line_pdu, and argc number of cmd_single_line_pdu
678 mem += sizeof(*clp) + (client->argc * sizeof(*cslp));
682 offset = sizeof(*clp);
684 for (i = 0; i < client->argc; i++) {
685 uint16_t arg_len = lens[i];
688 strcpy((char *) cslp->text, client->argv[i]);
689 cslp->len = cpu_to_le16(arg_len);
690 offset += sizeof(*cslp) + arg_len;
694 clp->lines = cpu_to_le16(client->argc);
695 clp->client_type = __cpu_to_le16(client->type);
696 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOBLINE, pdu, mem, NULL, NULL);
701 int fio_clients_connect(void)
703 struct fio_client *client;
704 struct flist_head *entry, *tmp;
709 WSAStartup(MAKEWORD(2, 2), &wsd);
712 dprint(FD_NET, "client: connect all\n");
714 client_signal_handler();
716 flist_for_each_safe(entry, tmp, &client_list) {
717 client = flist_entry(entry, struct fio_client, list);
719 ret = fio_client_connect(client);
721 remove_client(client);
725 if (client->argc > 1)
726 send_client_cmd_line(client);
732 int fio_start_client(struct fio_client *client)
734 dprint(FD_NET, "client: start %s\n", client->hostname);
735 return fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_RUN, 0, NULL);
738 int fio_start_all_clients(void)
740 struct fio_client *client;
741 struct flist_head *entry, *tmp;
744 dprint(FD_NET, "client: start all\n");
746 fio_client_json_init();
748 flist_for_each_safe(entry, tmp, &client_list) {
749 client = flist_entry(entry, struct fio_client, list);
751 ret = fio_start_client(client);
753 remove_client(client);
758 return flist_empty(&client_list);
761 static int __fio_client_send_remote_ini(struct fio_client *client,
762 const char *filename)
764 struct cmd_load_file_pdu *pdu;
768 dprint(FD_NET, "send remote ini %s to %s\n", filename, client->hostname);
770 p_size = sizeof(*pdu) + strlen(filename) + 1;
771 pdu = malloc(p_size);
772 memset(pdu, 0, p_size);
773 pdu->name_len = strlen(filename);
774 strcpy((char *) pdu->file, filename);
775 pdu->client_type = cpu_to_le16((uint16_t) client->type);
777 client->sent_job = true;
778 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_LOAD_FILE, pdu, p_size,NULL, NULL);
784 * Send file contents to server backend. We could use sendfile(), but to remain
785 * more portable lets just read/write the darn thing.
787 static int __fio_client_send_local_ini(struct fio_client *client,
788 const char *filename)
790 struct cmd_job_pdu *pdu;
798 dprint(FD_NET, "send ini %s to %s\n", filename, client->hostname);
800 fd = open(filename, O_RDONLY);
803 log_err("fio: job file <%s> open: %s\n", filename, strerror(errno));
807 if (fstat(fd, &sb) < 0) {
809 log_err("fio: job file stat: %s\n", strerror(errno));
815 * Add extra space for variable expansion, but doesn't guarantee.
817 sb.st_size += OPT_LEN_MAX;
818 p_size = sb.st_size + sizeof(*pdu);
819 pdu = malloc(p_size);
824 if (read_ini_data(fd, p, len)) {
825 log_err("fio: failed reading job file %s\n", filename);
831 pdu->buf_len = __cpu_to_le32(sb.st_size);
832 pdu->client_type = cpu_to_le32(client->type);
834 client->sent_job = true;
835 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, pdu, p_size, NULL, NULL);
841 int fio_client_send_ini(struct fio_client *client, const char *filename,
847 ret = __fio_client_send_local_ini(client, filename);
849 ret = __fio_client_send_remote_ini(client, filename);
852 client->sent_job = true;
857 static int fio_client_send_cf(struct fio_client *client,
858 struct client_file *cf)
860 return fio_client_send_ini(client, cf->file, cf->remote);
863 int fio_clients_send_ini(const char *filename)
865 struct fio_client *client;
866 struct flist_head *entry, *tmp;
868 flist_for_each_safe(entry, tmp, &client_list) {
871 client = flist_entry(entry, struct fio_client, list);
873 if (client->nr_files) {
876 for (i = 0; i < client->nr_files; i++) {
877 struct client_file *cf;
879 cf = &client->files[i];
881 if (fio_client_send_cf(client, cf)) {
883 remove_client(client);
888 if (client->sent_job || failed)
890 if (!filename || fio_client_send_ini(client, filename, 0))
891 remove_client(client);
897 int fio_client_update_options(struct fio_client *client,
898 struct thread_options *o, uint64_t *tag)
900 struct cmd_add_job_pdu pdu;
902 pdu.thread_number = cpu_to_le32(client->thread_number);
903 pdu.groupid = cpu_to_le32(client->groupid);
904 convert_thread_options_to_net(&pdu.top, o);
906 return fio_net_send_cmd(client->fd, FIO_NET_CMD_UPDATE_JOB, &pdu, sizeof(pdu), tag, &client->cmd_list);
909 static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
911 dst->max_val = le64_to_cpu(src->max_val);
912 dst->min_val = le64_to_cpu(src->min_val);
913 dst->samples = le64_to_cpu(src->samples);
916 * Floats arrive as IEEE 754 encoded uint64_t, convert back to double
918 dst->mean.u.f = fio_uint64_to_double(le64_to_cpu(dst->mean.u.i));
919 dst->S.u.f = fio_uint64_to_double(le64_to_cpu(dst->S.u.i));
922 static void convert_ts(struct thread_stat *dst, struct thread_stat *src)
926 dst->error = le32_to_cpu(src->error);
927 dst->thread_number = le32_to_cpu(src->thread_number);
928 dst->groupid = le32_to_cpu(src->groupid);
929 dst->pid = le32_to_cpu(src->pid);
930 dst->members = le32_to_cpu(src->members);
931 dst->unified_rw_rep = le32_to_cpu(src->unified_rw_rep);
933 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
934 convert_io_stat(&dst->clat_stat[i], &src->clat_stat[i]);
935 convert_io_stat(&dst->slat_stat[i], &src->slat_stat[i]);
936 convert_io_stat(&dst->lat_stat[i], &src->lat_stat[i]);
937 convert_io_stat(&dst->bw_stat[i], &src->bw_stat[i]);
938 convert_io_stat(&dst->iops_stat[i], &src->iops_stat[i]);
941 dst->usr_time = le64_to_cpu(src->usr_time);
942 dst->sys_time = le64_to_cpu(src->sys_time);
943 dst->ctx = le64_to_cpu(src->ctx);
944 dst->minf = le64_to_cpu(src->minf);
945 dst->majf = le64_to_cpu(src->majf);
946 dst->clat_percentiles = le32_to_cpu(src->clat_percentiles);
947 dst->lat_percentiles = le32_to_cpu(src->lat_percentiles);
948 dst->percentile_precision = le64_to_cpu(src->percentile_precision);
950 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
951 fio_fp64_t *fps = &src->percentile_list[i];
952 fio_fp64_t *fpd = &dst->percentile_list[i];
954 fpd->u.f = fio_uint64_to_double(le64_to_cpu(fps->u.i));
957 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
958 dst->io_u_map[i] = le64_to_cpu(src->io_u_map[i]);
959 dst->io_u_submit[i] = le64_to_cpu(src->io_u_submit[i]);
960 dst->io_u_complete[i] = le64_to_cpu(src->io_u_complete[i]);
963 for (i = 0; i < FIO_IO_U_LAT_N_NR; i++)
964 dst->io_u_lat_n[i] = le64_to_cpu(src->io_u_lat_n[i]);
965 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++)
966 dst->io_u_lat_u[i] = le64_to_cpu(src->io_u_lat_u[i]);
967 for (i = 0; i < FIO_IO_U_LAT_M_NR; i++)
968 dst->io_u_lat_m[i] = le64_to_cpu(src->io_u_lat_m[i]);
970 for (i = 0; i < DDIR_RWDIR_CNT; i++)
971 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
972 dst->io_u_plat[i][j] = le64_to_cpu(src->io_u_plat[i][j]);
974 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
975 dst->total_io_u[i] = le64_to_cpu(src->total_io_u[i]);
976 dst->short_io_u[i] = le64_to_cpu(src->short_io_u[i]);
977 dst->drop_io_u[i] = le64_to_cpu(src->drop_io_u[i]);
980 dst->total_submit = le64_to_cpu(src->total_submit);
981 dst->total_complete = le64_to_cpu(src->total_complete);
983 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
984 dst->io_bytes[i] = le64_to_cpu(src->io_bytes[i]);
985 dst->runtime[i] = le64_to_cpu(src->runtime[i]);
988 dst->total_run_time = le64_to_cpu(src->total_run_time);
989 dst->continue_on_error = le16_to_cpu(src->continue_on_error);
990 dst->total_err_count = le64_to_cpu(src->total_err_count);
991 dst->first_error = le32_to_cpu(src->first_error);
992 dst->kb_base = le32_to_cpu(src->kb_base);
993 dst->unit_base = le32_to_cpu(src->unit_base);
995 dst->sig_figs = le32_to_cpu(src->sig_figs);
997 dst->latency_depth = le32_to_cpu(src->latency_depth);
998 dst->latency_target = le64_to_cpu(src->latency_target);
999 dst->latency_window = le64_to_cpu(src->latency_window);
1000 dst->latency_percentile.u.f = fio_uint64_to_double(le64_to_cpu(src->latency_percentile.u.i));
1002 dst->nr_block_infos = le64_to_cpu(src->nr_block_infos);
1003 for (i = 0; i < dst->nr_block_infos; i++)
1004 dst->block_infos[i] = le32_to_cpu(src->block_infos[i]);
1006 dst->ss_dur = le64_to_cpu(src->ss_dur);
1007 dst->ss_state = le32_to_cpu(src->ss_state);
1008 dst->ss_head = le32_to_cpu(src->ss_head);
1009 dst->ss_limit.u.f = fio_uint64_to_double(le64_to_cpu(src->ss_limit.u.i));
1010 dst->ss_slope.u.f = fio_uint64_to_double(le64_to_cpu(src->ss_slope.u.i));
1011 dst->ss_deviation.u.f = fio_uint64_to_double(le64_to_cpu(src->ss_deviation.u.i));
1012 dst->ss_criterion.u.f = fio_uint64_to_double(le64_to_cpu(src->ss_criterion.u.i));
1014 if (dst->ss_state & FIO_SS_DATA) {
1015 for (i = 0; i < dst->ss_dur; i++ ) {
1016 dst->ss_iops_data[i] = le64_to_cpu(src->ss_iops_data[i]);
1017 dst->ss_bw_data[i] = le64_to_cpu(src->ss_bw_data[i]);
1022 static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
1026 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1027 dst->max_run[i] = le64_to_cpu(src->max_run[i]);
1028 dst->min_run[i] = le64_to_cpu(src->min_run[i]);
1029 dst->max_bw[i] = le64_to_cpu(src->max_bw[i]);
1030 dst->min_bw[i] = le64_to_cpu(src->min_bw[i]);
1031 dst->iobytes[i] = le64_to_cpu(src->iobytes[i]);
1032 dst->agg[i] = le64_to_cpu(src->agg[i]);
1035 dst->kb_base = le32_to_cpu(src->kb_base);
1036 dst->unit_base = le32_to_cpu(src->unit_base);
1037 dst->sig_figs = le32_to_cpu(src->sig_figs);
1038 dst->groupid = le32_to_cpu(src->groupid);
1039 dst->unified_rw_rep = le32_to_cpu(src->unified_rw_rep);
1042 static void json_object_add_client_info(struct json_object *obj,
1043 struct fio_client *client)
1045 const char *hostname = client->hostname ? client->hostname : "";
1047 json_object_add_value_string(obj, "hostname", hostname);
1048 json_object_add_value_int(obj, "port", client->port);
1051 static void handle_ts(struct fio_client *client, struct fio_net_cmd *cmd)
1053 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
1054 struct flist_head *opt_list = NULL;
1055 struct json_object *tsobj;
1057 if (client->opt_lists && p->ts.thread_number <= client->jobs)
1058 opt_list = &client->opt_lists[p->ts.thread_number - 1];
1060 tsobj = show_thread_status(&p->ts, &p->rs, opt_list, NULL);
1061 client->did_stat = true;
1063 json_object_add_client_info(tsobj, client);
1064 json_array_add_value_object(clients_array, tsobj);
1067 if (sum_stat_clients <= 1)
1070 sum_thread_stats(&client_ts, &p->ts, sum_stat_nr == 1);
1071 sum_group_stats(&client_gs, &p->rs);
1073 client_ts.members++;
1074 client_ts.thread_number = p->ts.thread_number;
1075 client_ts.groupid = p->ts.groupid;
1076 client_ts.unified_rw_rep = p->ts.unified_rw_rep;
1077 client_ts.sig_figs = p->ts.sig_figs;
1079 if (++sum_stat_nr == sum_stat_clients) {
1080 strcpy(client_ts.name, "All clients");
1081 tsobj = show_thread_status(&client_ts, &client_gs, NULL, NULL);
1083 json_object_add_client_info(tsobj, client);
1084 json_array_add_value_object(clients_array, tsobj);
1089 static void handle_gs(struct fio_client *client, struct fio_net_cmd *cmd)
1091 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
1093 if (output_format & FIO_OUTPUT_NORMAL)
1094 show_group_stats(gs, NULL);
1097 static void handle_job_opt(struct fio_client *client, struct fio_net_cmd *cmd)
1099 struct cmd_job_option *pdu = (struct cmd_job_option *) cmd->payload;
1100 struct print_option *p;
1102 if (!job_opt_object)
1105 pdu->global = le16_to_cpu(pdu->global);
1106 pdu->truncated = le16_to_cpu(pdu->truncated);
1107 pdu->groupid = le32_to_cpu(pdu->groupid);
1109 p = malloc(sizeof(*p));
1110 p->name = strdup((char *) pdu->name);
1111 if (pdu->value[0] != '\0')
1112 p->value = strdup((char *) pdu->value);
1117 const char *pos = "";
1122 json_object_add_value_string(job_opt_object, p->name, pos);
1123 } else if (client->opt_lists) {
1124 struct flist_head *opt_list = &client->opt_lists[pdu->groupid];
1126 flist_add_tail(&p->list, opt_list);
1130 static void handle_text(struct fio_client *client, struct fio_net_cmd *cmd)
1132 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
1133 const char *buf = (const char *) pdu->buf;
1137 name = client->name ? client->name : client->hostname;
1139 if (!client->skip_newline)
1140 fprintf(f_out, "<%s> ", name);
1141 ret = fwrite(buf, pdu->buf_len, 1, f_out);
1143 client->skip_newline = strchr(buf, '\n') == NULL;
1146 static void convert_agg(struct disk_util_agg *agg)
1150 for (i = 0; i < 2; i++) {
1151 agg->ios[i] = le64_to_cpu(agg->ios[i]);
1152 agg->merges[i] = le64_to_cpu(agg->merges[i]);
1153 agg->sectors[i] = le64_to_cpu(agg->sectors[i]);
1154 agg->ticks[i] = le64_to_cpu(agg->ticks[i]);
1157 agg->io_ticks = le64_to_cpu(agg->io_ticks);
1158 agg->time_in_queue = le64_to_cpu(agg->time_in_queue);
1159 agg->slavecount = le32_to_cpu(agg->slavecount);
1160 agg->max_util.u.f = fio_uint64_to_double(le64_to_cpu(agg->max_util.u.i));
1163 static void convert_dus(struct disk_util_stat *dus)
1167 for (i = 0; i < 2; i++) {
1168 dus->s.ios[i] = le64_to_cpu(dus->s.ios[i]);
1169 dus->s.merges[i] = le64_to_cpu(dus->s.merges[i]);
1170 dus->s.sectors[i] = le64_to_cpu(dus->s.sectors[i]);
1171 dus->s.ticks[i] = le64_to_cpu(dus->s.ticks[i]);
1174 dus->s.io_ticks = le64_to_cpu(dus->s.io_ticks);
1175 dus->s.time_in_queue = le64_to_cpu(dus->s.time_in_queue);
1176 dus->s.msec = le64_to_cpu(dus->s.msec);
1179 static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd)
1181 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
1183 if (!client->disk_stats_shown) {
1184 client->disk_stats_shown = true;
1185 log_info("\nDisk stats (read/write):\n");
1188 if (output_format & FIO_OUTPUT_JSON) {
1189 struct json_object *duobj;
1190 json_array_add_disk_util(&du->dus, &du->agg, du_array);
1191 duobj = json_array_last_value_object(du_array);
1192 json_object_add_client_info(duobj, client);
1194 if (output_format & FIO_OUTPUT_TERSE)
1195 print_disk_util(&du->dus, &du->agg, 1, NULL);
1196 if (output_format & FIO_OUTPUT_NORMAL)
1197 print_disk_util(&du->dus, &du->agg, 0, NULL);
1200 static void convert_jobs_eta(struct jobs_eta *je)
1204 je->nr_running = le32_to_cpu(je->nr_running);
1205 je->nr_ramp = le32_to_cpu(je->nr_ramp);
1206 je->nr_pending = le32_to_cpu(je->nr_pending);
1207 je->nr_setting_up = le32_to_cpu(je->nr_setting_up);
1208 je->files_open = le32_to_cpu(je->files_open);
1210 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1211 je->m_rate[i] = le64_to_cpu(je->m_rate[i]);
1212 je->t_rate[i] = le64_to_cpu(je->t_rate[i]);
1213 je->m_iops[i] = le32_to_cpu(je->m_iops[i]);
1214 je->t_iops[i] = le32_to_cpu(je->t_iops[i]);
1215 je->rate[i] = le64_to_cpu(je->rate[i]);
1216 je->iops[i] = le32_to_cpu(je->iops[i]);
1219 je->elapsed_sec = le64_to_cpu(je->elapsed_sec);
1220 je->eta_sec = le64_to_cpu(je->eta_sec);
1221 je->nr_threads = le32_to_cpu(je->nr_threads);
1222 je->is_pow2 = le32_to_cpu(je->is_pow2);
1223 je->unit_base = le32_to_cpu(je->unit_base);
1224 je->sig_figs = le32_to_cpu(je->sig_figs);
1227 void fio_client_sum_jobs_eta(struct jobs_eta *dst, struct jobs_eta *je)
1231 dst->nr_running += je->nr_running;
1232 dst->nr_ramp += je->nr_ramp;
1233 dst->nr_pending += je->nr_pending;
1234 dst->nr_setting_up += je->nr_setting_up;
1235 dst->files_open += je->files_open;
1237 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1238 dst->m_rate[i] += je->m_rate[i];
1239 dst->t_rate[i] += je->t_rate[i];
1240 dst->m_iops[i] += je->m_iops[i];
1241 dst->t_iops[i] += je->t_iops[i];
1242 dst->rate[i] += je->rate[i];
1243 dst->iops[i] += je->iops[i];
1246 dst->elapsed_sec += je->elapsed_sec;
1248 if (je->eta_sec > dst->eta_sec)
1249 dst->eta_sec = je->eta_sec;
1251 dst->nr_threads += je->nr_threads;
1254 * This wont be correct for multiple strings, but at least it
1255 * works for the basic cases.
1257 strcpy((char *) dst->run_str, (char *) je->run_str);
1260 static bool remove_reply_cmd(struct fio_client *client, struct fio_net_cmd *cmd)
1262 struct fio_net_cmd_reply *reply = NULL;
1263 struct flist_head *entry;
1265 flist_for_each(entry, &client->cmd_list) {
1266 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
1268 if (cmd->tag == (uintptr_t) reply)
1275 log_err("fio: client: unable to find matching tag (%llx)\n", (unsigned long long) cmd->tag);
1279 flist_del(&reply->list);
1280 cmd->tag = reply->saved_tag;
1285 int fio_client_wait_for_reply(struct fio_client *client, uint64_t tag)
1288 struct fio_net_cmd_reply *reply = NULL;
1289 struct flist_head *entry;
1291 flist_for_each(entry, &client->cmd_list) {
1292 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
1294 if (tag == (uintptr_t) reply)
1309 static void handle_eta(struct fio_client *client, struct fio_net_cmd *cmd)
1311 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
1312 struct client_eta *eta = (struct client_eta *) (uintptr_t) cmd->tag;
1314 dprint(FD_NET, "client: got eta tag %p, %d\n", eta, eta->pending);
1316 assert(client->eta_in_flight == eta);
1318 client->eta_in_flight = NULL;
1319 flist_del_init(&client->eta_list);
1320 client->eta_timeouts = 0;
1322 if (client->ops->jobs_eta)
1323 client->ops->jobs_eta(client, je);
1325 fio_client_sum_jobs_eta(&eta->eta, je);
1326 fio_client_dec_jobs_eta(eta, client->ops->eta);
1329 static void client_flush_hist_samples(FILE *f, int hist_coarseness, void *samples,
1330 uint64_t sample_size)
1332 struct io_sample *s;
1334 uint64_t i, j, nr_samples;
1335 struct io_u_plat_entry *entry;
1336 uint64_t *io_u_plat;
1338 int stride = 1 << hist_coarseness;
1343 s = __get_sample(samples, 0, 0);
1344 log_offset = (s->__ddir & LOG_OFFSET_SAMPLE_BIT) != 0;
1346 nr_samples = sample_size / __log_entry_sz(log_offset);
1348 for (i = 0; i < nr_samples; i++) {
1350 s = (struct io_sample *)((char *)__get_sample(samples, log_offset, i) +
1351 i * sizeof(struct io_u_plat_entry));
1353 entry = s->data.plat_entry;
1354 io_u_plat = entry->io_u_plat;
1356 fprintf(f, "%lu, %u, %u, ", (unsigned long) s->time,
1357 io_sample_ddir(s), s->bs);
1358 for (j = 0; j < FIO_IO_U_PLAT_NR - stride; j += stride) {
1359 fprintf(f, "%llu, ", (unsigned long long)hist_sum(j, stride, io_u_plat, NULL));
1361 fprintf(f, "%llu\n", (unsigned long long)
1362 hist_sum(FIO_IO_U_PLAT_NR - stride, stride, io_u_plat, NULL));
1367 static int fio_client_handle_iolog(struct fio_client *client,
1368 struct fio_net_cmd *cmd)
1370 struct cmd_iolog_pdu *pdu = NULL;
1372 char *log_pathname = NULL;
1375 pdu = convert_iolog(cmd, &store_direct);
1377 log_err("fio: failed converting IO log\n");
1382 /* allocate buffer big enough for next sprintf() call */
1383 log_pathname = malloc(10 + strlen((char *)pdu->name) +
1384 strlen(client->hostname));
1385 if (!log_pathname) {
1386 log_err("fio: memory allocation of unique pathname failed\n");
1390 /* generate a unique pathname for the log file using hostname */
1391 sprintf(log_pathname, "%s.%s", pdu->name, client->hostname);
1398 fd = open((const char *) log_pathname,
1399 O_WRONLY | O_CREAT | O_TRUNC, 0644);
1401 log_err("fio: open log %s: %s\n",
1402 log_pathname, strerror(errno));
1407 sz = cmd->pdu_len - sizeof(*pdu);
1408 wrote = write(fd, pdu->samples, sz);
1412 log_err("fio: short write on compressed log\n");
1420 f = fopen((const char *) log_pathname, "w");
1422 log_err("fio: fopen log %s : %s\n",
1423 log_pathname, strerror(errno));
1428 if (pdu->log_type == IO_LOG_TYPE_HIST) {
1429 client_flush_hist_samples(f, pdu->log_hist_coarseness, pdu->samples,
1430 pdu->nr_samples * sizeof(struct io_sample));
1432 flush_samples(f, pdu->samples,
1433 pdu->nr_samples * sizeof(struct io_sample));
1440 if (pdu && pdu != (void *) cmd->payload)
1449 static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd)
1451 struct cmd_probe_reply_pdu *probe = (struct cmd_probe_reply_pdu *) cmd->payload;
1452 const char *os, *arch;
1455 os = fio_get_os_string(probe->os);
1459 arch = fio_get_arch_string(probe->arch);
1463 sprintf(bit, "%d-bit", probe->bpp * 8);
1464 probe->flags = le64_to_cpu(probe->flags);
1466 log_info("hostname=%s, be=%u, %s, os=%s, arch=%s, fio=%s, flags=%lx\n",
1467 probe->hostname, probe->bigendian, bit, os, arch,
1468 probe->fio_version, (unsigned long) probe->flags);
1471 client->name = strdup((char *) probe->hostname);
1474 static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd)
1476 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
1478 client->state = Client_started;
1479 client->jobs = le32_to_cpu(pdu->jobs);
1480 client->nr_stat = le32_to_cpu(pdu->stat_outputs);
1485 if (client->opt_lists)
1486 free(client->opt_lists);
1488 client->opt_lists = malloc(client->jobs * sizeof(struct flist_head));
1489 for (i = 0; i < client->jobs; i++)
1490 INIT_FLIST_HEAD(&client->opt_lists[i]);
1493 sum_stat_clients += client->nr_stat;
1496 static void handle_stop(struct fio_client *client)
1499 log_info("client <%s>: exited with error %d\n", client->hostname, client->error);
1502 static void convert_stop(struct fio_net_cmd *cmd)
1504 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
1506 pdu->error = le32_to_cpu(pdu->error);
1509 static void convert_text(struct fio_net_cmd *cmd)
1511 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
1513 pdu->level = le32_to_cpu(pdu->level);
1514 pdu->buf_len = le32_to_cpu(pdu->buf_len);
1515 pdu->log_sec = le64_to_cpu(pdu->log_sec);
1516 pdu->log_usec = le64_to_cpu(pdu->log_usec);
1519 static struct cmd_iolog_pdu *convert_iolog_gz(struct fio_net_cmd *cmd,
1520 struct cmd_iolog_pdu *pdu)
1523 struct cmd_iolog_pdu *ret;
1525 uint32_t nr_samples;
1529 stream.zalloc = Z_NULL;
1530 stream.zfree = Z_NULL;
1531 stream.opaque = Z_NULL;
1532 stream.avail_in = 0;
1533 stream.next_in = Z_NULL;
1535 if (inflateInit(&stream) != Z_OK)
1539 * Get header first, it's not compressed
1541 nr_samples = le64_to_cpu(pdu->nr_samples);
1543 if (pdu->log_type == IO_LOG_TYPE_HIST)
1544 total = nr_samples * (__log_entry_sz(le32_to_cpu(pdu->log_offset)) +
1545 sizeof(struct io_u_plat_entry));
1547 total = nr_samples * __log_entry_sz(le32_to_cpu(pdu->log_offset));
1548 ret = malloc(total + sizeof(*pdu));
1549 ret->nr_samples = nr_samples;
1551 memcpy(ret, pdu, sizeof(*pdu));
1553 p = (char *) ret + sizeof(*pdu);
1555 stream.avail_in = cmd->pdu_len - sizeof(*pdu);
1556 stream.next_in = (void *)((char *) pdu + sizeof(*pdu));
1557 while (stream.avail_in) {
1558 unsigned int this_chunk = 65536;
1559 unsigned int this_len;
1562 if (this_chunk > total)
1565 stream.avail_out = this_chunk;
1566 stream.next_out = (void *)p;
1567 err = inflate(&stream, Z_NO_FLUSH);
1568 /* may be Z_OK, or Z_STREAM_END */
1570 log_err("fio: inflate error %d\n", err);
1576 this_len = this_chunk - stream.avail_out;
1582 inflateEnd(&stream);
1590 * This has been compressed on the server side, since it can be big.
1593 static struct cmd_iolog_pdu *convert_iolog(struct fio_net_cmd *cmd,
1596 struct cmd_iolog_pdu *pdu = (struct cmd_iolog_pdu *) cmd->payload;
1597 struct cmd_iolog_pdu *ret;
1602 *store_direct = false;
1605 * Convert if compressed and we support it. If it's not
1606 * compressed, we need not do anything.
1608 compressed = le32_to_cpu(pdu->compressed);
1609 if (compressed == XMIT_COMPRESSED) {
1611 log_err("fio: server sent compressed data by mistake\n");
1614 ret = convert_iolog_gz(cmd, pdu);
1616 log_err("fio: failed decompressing log\n");
1619 } else if (compressed == STORE_COMPRESSED) {
1620 *store_direct = true;
1625 ret->nr_samples = le64_to_cpu(ret->nr_samples);
1626 ret->thread_number = le32_to_cpu(ret->thread_number);
1627 ret->log_type = le32_to_cpu(ret->log_type);
1628 ret->compressed = le32_to_cpu(ret->compressed);
1629 ret->log_offset = le32_to_cpu(ret->log_offset);
1630 ret->log_hist_coarseness = le32_to_cpu(ret->log_hist_coarseness);
1635 samples = &ret->samples[0];
1636 for (i = 0; i < ret->nr_samples; i++) {
1637 struct io_sample *s;
1639 s = __get_sample(samples, ret->log_offset, i);
1640 if (ret->log_type == IO_LOG_TYPE_HIST)
1641 s = (struct io_sample *)((char *)s + sizeof(struct io_u_plat_entry) * i);
1643 s->time = le64_to_cpu(s->time);
1644 s->data.val = le64_to_cpu(s->data.val);
1645 s->__ddir = le32_to_cpu(s->__ddir);
1646 s->bs = le32_to_cpu(s->bs);
1648 if (ret->log_offset) {
1649 struct io_sample_offset *so = (void *) s;
1651 so->offset = le64_to_cpu(so->offset);
1654 if (ret->log_type == IO_LOG_TYPE_HIST) {
1655 s->data.plat_entry = (struct io_u_plat_entry *)(((char *)s) + sizeof(*s));
1656 s->data.plat_entry->list.next = NULL;
1657 s->data.plat_entry->list.prev = NULL;
1664 static void sendfile_reply(int fd, struct cmd_sendfile_reply *rep,
1665 size_t size, uint64_t tag)
1667 rep->error = cpu_to_le32(rep->error);
1668 fio_net_send_cmd(fd, FIO_NET_CMD_SENDFILE, rep, size, &tag, NULL);
1671 static int fio_send_file(struct fio_client *client, struct cmd_sendfile *pdu,
1674 struct cmd_sendfile_reply *rep;
1679 size = sizeof(*rep);
1682 if (stat((char *)pdu->path, &sb) < 0) {
1685 sendfile_reply(client->fd, rep, size, tag);
1691 rep = realloc(rep, size);
1692 rep->size = cpu_to_le32((uint32_t) sb.st_size);
1694 fd = open((char *)pdu->path, O_RDONLY);
1698 rep->error = read_data(fd, &rep->data, sb.st_size);
1699 sendfile_reply(client->fd, rep, size, tag);
1705 int fio_handle_client(struct fio_client *client)
1707 struct client_ops *ops = client->ops;
1708 struct fio_net_cmd *cmd;
1711 dprint(FD_NET, "client: handle %s\n", client->hostname);
1713 cmd = fio_net_recv_cmd(client->fd, true);
1717 dprint(FD_NET, "client: got cmd op %s from %s (pdu=%u)\n",
1718 fio_server_op(cmd->opcode), client->hostname, cmd->pdu_len);
1720 client->last_cmd = cmd->opcode;
1722 switch (cmd->opcode) {
1723 case FIO_NET_CMD_QUIT:
1725 ops->quit(client, cmd);
1726 remove_client(client);
1728 case FIO_NET_CMD_TEXT:
1730 ops->text(client, cmd);
1732 case FIO_NET_CMD_DU: {
1733 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
1735 convert_dus(&du->dus);
1736 convert_agg(&du->agg);
1738 ops->disk_util(client, cmd);
1741 case FIO_NET_CMD_TS: {
1742 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
1744 dprint(FD_NET, "client: ts->ss_state = %u\n", (unsigned int) le32_to_cpu(p->ts.ss_state));
1745 if (le32_to_cpu(p->ts.ss_state) & FIO_SS_DATA) {
1746 dprint(FD_NET, "client: received steadystate ring buffers\n");
1748 size = le64_to_cpu(p->ts.ss_dur);
1749 p->ts.ss_iops_data = (uint64_t *) ((struct cmd_ts_pdu *)cmd->payload + 1);
1750 p->ts.ss_bw_data = p->ts.ss_iops_data + size;
1753 convert_ts(&p->ts, &p->ts);
1754 convert_gs(&p->rs, &p->rs);
1756 ops->thread_status(client, cmd);
1759 case FIO_NET_CMD_GS: {
1760 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
1764 ops->group_stats(client, cmd);
1767 case FIO_NET_CMD_ETA: {
1768 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
1770 if (!remove_reply_cmd(client, cmd))
1772 convert_jobs_eta(je);
1773 handle_eta(client, cmd);
1776 case FIO_NET_CMD_PROBE:
1777 remove_reply_cmd(client, cmd);
1778 ops->probe(client, cmd);
1780 case FIO_NET_CMD_SERVER_START:
1781 client->state = Client_running;
1783 ops->job_start(client, cmd);
1785 case FIO_NET_CMD_START: {
1786 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
1788 pdu->jobs = le32_to_cpu(pdu->jobs);
1789 ops->start(client, cmd);
1792 case FIO_NET_CMD_STOP: {
1793 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
1796 client->state = Client_stopped;
1797 client->error = le32_to_cpu(pdu->error);
1798 client->signal = le32_to_cpu(pdu->signal);
1802 case FIO_NET_CMD_ADD_JOB: {
1803 struct cmd_add_job_pdu *pdu = (struct cmd_add_job_pdu *) cmd->payload;
1805 client->thread_number = le32_to_cpu(pdu->thread_number);
1806 client->groupid = le32_to_cpu(pdu->groupid);
1809 ops->add_job(client, cmd);
1812 case FIO_NET_CMD_IOLOG:
1813 fio_client_handle_iolog(client, cmd);
1815 case FIO_NET_CMD_UPDATE_JOB:
1816 ops->update_job(client, cmd);
1817 remove_reply_cmd(client, cmd);
1819 case FIO_NET_CMD_VTRIGGER: {
1820 struct all_io_list *pdu = (struct all_io_list *) cmd->payload;
1825 strcpy(buf, aux_path);
1829 __verify_save_state(pdu, server_name(client, &buf[off], sizeof(buf) - off));
1830 exec_trigger(trigger_cmd);
1833 case FIO_NET_CMD_SENDFILE: {
1834 struct cmd_sendfile *pdu = (struct cmd_sendfile *) cmd->payload;
1835 fio_send_file(client, pdu, cmd->tag);
1838 case FIO_NET_CMD_JOB_OPT: {
1839 handle_job_opt(client, cmd);
1843 log_err("fio: unknown client op: %s\n", fio_server_op(cmd->opcode));
1851 int fio_clients_send_trigger(const char *cmd)
1853 struct flist_head *entry;
1854 struct fio_client *client;
1857 dprint(FD_NET, "client: send vtrigger: %s\n", cmd);
1864 flist_for_each(entry, &client_list) {
1865 struct cmd_vtrigger_pdu *pdu;
1867 client = flist_entry(entry, struct fio_client, list);
1869 pdu = malloc(sizeof(*pdu) + slen);
1870 pdu->len = cpu_to_le16((uint16_t) slen);
1872 memcpy(pdu->cmd, cmd, slen);
1873 fio_net_send_cmd(client->fd, FIO_NET_CMD_VTRIGGER, pdu,
1874 sizeof(*pdu) + slen, NULL, NULL);
1881 static void request_client_etas(struct client_ops *ops)
1883 struct fio_client *client;
1884 struct flist_head *entry;
1885 struct client_eta *eta;
1888 if (eta_print == FIO_ETA_NEVER)
1891 dprint(FD_NET, "client: request eta (%d)\n", nr_clients);
1893 eta = calloc(1, sizeof(*eta) + __THREAD_RUNSTR_SZ(REAL_MAX_JOBS));
1894 eta->pending = nr_clients;
1896 flist_for_each(entry, &client_list) {
1897 client = flist_entry(entry, struct fio_client, list);
1899 if (!flist_empty(&client->eta_list)) {
1903 if (client->state != Client_running)
1906 assert(!client->eta_in_flight);
1907 flist_add_tail(&client->eta_list, &eta_list);
1908 client->eta_in_flight = eta;
1909 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_SEND_ETA,
1910 (uintptr_t) eta, &client->cmd_list);
1914 if (!fio_client_dec_jobs_eta(eta, ops->eta))
1918 dprint(FD_NET, "client: requested eta tag %p\n", eta);
1922 * A single SEND_ETA timeout isn't fatal. Attempt to recover.
1924 static int handle_cmd_timeout(struct fio_client *client,
1925 struct fio_net_cmd_reply *reply)
1927 uint16_t reply_opcode = reply->opcode;
1929 flist_del(&reply->list);
1932 if (reply_opcode != FIO_NET_CMD_SEND_ETA)
1935 log_info("client <%s>: timeout on SEND_ETA\n", client->hostname);
1937 flist_del_init(&client->eta_list);
1938 if (client->eta_in_flight) {
1939 fio_client_dec_jobs_eta(client->eta_in_flight, client->ops->eta);
1940 client->eta_in_flight = NULL;
1944 * If we fail 5 in a row, give up...
1946 if (client->eta_timeouts++ > 5)
1952 static int client_check_cmd_timeout(struct fio_client *client,
1953 struct timespec *now)
1955 struct fio_net_cmd_reply *reply;
1956 struct flist_head *entry, *tmp;
1959 flist_for_each_safe(entry, tmp, &client->cmd_list) {
1962 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
1964 if (mtime_since(&reply->ts, now) < FIO_NET_CLIENT_TIMEOUT)
1968 if (!handle_cmd_timeout(client, reply))
1971 log_err("fio: client %s, timeout on cmd %s\n", client->hostname,
1976 return flist_empty(&client->cmd_list) && ret;
1979 static int fio_check_clients_timed_out(void)
1981 struct fio_client *client;
1982 struct flist_head *entry, *tmp;
1986 fio_gettime(&ts, NULL);
1988 flist_for_each_safe(entry, tmp, &client_list) {
1989 client = flist_entry(entry, struct fio_client, list);
1991 if (flist_empty(&client->cmd_list))
1994 if (!client_check_cmd_timeout(client, &ts))
1997 if (client->ops->timed_out)
1998 client->ops->timed_out(client);
2000 log_err("fio: client %s timed out\n", client->hostname);
2002 if (client->last_cmd != FIO_NET_CMD_VTRIGGER)
2003 client->error = ETIMEDOUT;
2005 log_info("fio: ignoring timeout due to vtrigger\n");
2006 remove_client(client);
2013 int fio_handle_clients(struct client_ops *ops)
2015 struct pollfd *pfds;
2016 int i, ret = 0, retval = 0;
2018 fio_gettime(&eta_ts, NULL);
2020 pfds = malloc(nr_clients * sizeof(struct pollfd));
2022 init_thread_stat(&client_ts);
2023 init_group_run_stat(&client_gs);
2025 while (!exit_backend && nr_clients) {
2026 struct flist_head *entry, *tmp;
2027 struct fio_client *client;
2030 flist_for_each_safe(entry, tmp, &client_list) {
2031 client = flist_entry(entry, struct fio_client, list);
2033 if (!client->sent_job && !client->ops->stay_connected &&
2034 flist_empty(&client->cmd_list)) {
2035 remove_client(client);
2039 pfds[i].fd = client->fd;
2040 pfds[i].events = POLLIN;
2047 assert(i == nr_clients);
2053 fio_gettime(&ts, NULL);
2054 if (eta_time_within_slack(mtime_since(&eta_ts, &ts))) {
2055 request_client_etas(ops);
2056 memcpy(&eta_ts, &ts, sizeof(ts));
2058 if (fio_check_clients_timed_out())
2062 check_trigger_file();
2064 timeout = min(100u, ops->eta_msec);
2066 ret = poll(pfds, nr_clients, timeout);
2070 log_err("fio: poll clients: %s\n", strerror(errno));
2076 for (i = 0; i < nr_clients; i++) {
2077 if (!(pfds[i].revents & POLLIN))
2080 client = find_client_by_fd(pfds[i].fd);
2082 log_err("fio: unknown client fd %ld\n", (long) pfds[i].fd);
2085 if (!fio_handle_client(client)) {
2086 log_info("client: host=%s disconnected\n",
2088 remove_client(client);
2090 } else if (client->error)
2092 fio_put_client(client);
2096 fio_client_json_fini();
2099 return retval || error_clients;