11 #include <sys/socket.h>
13 #include <netinet/in.h>
14 #include <arpa/inet.h>
27 static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd);
28 static void handle_ts(struct fio_client *client, struct fio_net_cmd *cmd);
29 static void handle_gs(struct fio_client *client, struct fio_net_cmd *cmd);
30 static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd);
31 static void handle_text(struct fio_client *client, struct fio_net_cmd *cmd);
32 static void handle_stop(struct fio_client *client, struct fio_net_cmd *cmd);
33 static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd);
35 struct client_ops fio_client_ops = {
37 .disk_util = handle_du,
38 .thread_status = handle_ts,
39 .group_stats = handle_gs,
41 .start = handle_start,
42 .eta = display_thread_status,
43 .probe = handle_probe,
44 .eta_msec = FIO_CLIENT_DEF_ETA_MSEC,
45 .client_type = FIO_CLIENT_TYPE_CLI,
48 static struct timeval eta_tv;
50 static FLIST_HEAD(client_list);
51 static FLIST_HEAD(eta_list);
53 static FLIST_HEAD(arg_list);
55 struct thread_stat client_ts;
56 struct group_run_stats client_gs;
59 static int sum_stat_nr;
60 static struct json_object *root = NULL;
61 static struct json_array *clients_array = NULL;
62 static struct json_array *du_array = NULL;
63 static int do_output_all_clients;
65 #define FIO_CLIENT_HASH_BITS 7
66 #define FIO_CLIENT_HASH_SZ (1 << FIO_CLIENT_HASH_BITS)
67 #define FIO_CLIENT_HASH_MASK (FIO_CLIENT_HASH_SZ - 1)
68 static struct flist_head client_hash[FIO_CLIENT_HASH_SZ];
70 static void fio_client_add_hash(struct fio_client *client)
72 int bucket = hash_long(client->fd, FIO_CLIENT_HASH_BITS);
74 bucket &= FIO_CLIENT_HASH_MASK;
75 flist_add(&client->hash_list, &client_hash[bucket]);
78 static void fio_client_remove_hash(struct fio_client *client)
80 if (!flist_empty(&client->hash_list))
81 flist_del_init(&client->hash_list);
84 static void fio_init fio_client_hash_init(void)
88 for (i = 0; i < FIO_CLIENT_HASH_SZ; i++)
89 INIT_FLIST_HEAD(&client_hash[i]);
92 static void fio_client_json_init(void)
94 if (output_format != FIO_OUTPUT_JSON)
96 root = json_create_object();
97 json_object_add_value_string(root, "fio version", fio_version_string);
98 clients_array = json_create_array();
99 json_object_add_value_array(root, "client_stats", clients_array);
100 du_array = json_create_array();
101 json_object_add_value_array(root, "disk_util", du_array);
104 static void fio_client_json_fini(void)
106 if (output_format != FIO_OUTPUT_JSON)
108 json_print_object(root);
110 json_free_object(root);
112 clients_array = NULL;
116 static struct fio_client *find_client_by_fd(int fd)
118 int bucket = hash_long(fd, FIO_CLIENT_HASH_BITS) & FIO_CLIENT_HASH_MASK;
119 struct fio_client *client;
120 struct flist_head *entry;
122 flist_for_each(entry, &client_hash[bucket]) {
123 client = flist_entry(entry, struct fio_client, hash_list);
125 if (client->fd == fd) {
134 void fio_put_client(struct fio_client *client)
139 free(client->hostname);
144 while (client->nr_files) {
145 struct client_file *cf = &client->files[--client->nr_files];
152 if (!client->did_stat)
153 sum_stat_clients -= client->nr_stat;
158 static void remove_client(struct fio_client *client)
160 assert(client->refs);
162 dprint(FD_NET, "client: removed <%s>\n", client->hostname);
164 if (!flist_empty(&client->list))
165 flist_del_init(&client->list);
167 fio_client_remove_hash(client);
169 if (!flist_empty(&client->eta_list)) {
170 flist_del_init(&client->eta_list);
171 fio_client_dec_jobs_eta(client->eta_in_flight, client->ops->eta);
177 if (client->ops->removed)
178 client->ops->removed(client);
181 fio_put_client(client);
184 struct fio_client *fio_get_client(struct fio_client *client)
190 static void __fio_client_add_cmd_option(struct fio_client *client,
195 index = client->argc++;
196 client->argv = realloc(client->argv, sizeof(char *) * client->argc);
197 client->argv[index] = strdup(opt);
198 dprint(FD_NET, "client: add cmd %d: %s\n", index, opt);
201 void fio_client_add_cmd_option(void *cookie, const char *opt)
203 struct fio_client *client = cookie;
204 struct flist_head *entry;
209 __fio_client_add_cmd_option(client, opt);
212 * Duplicate arguments to shared client group
214 flist_for_each(entry, &arg_list) {
215 client = flist_entry(entry, struct fio_client, arg_list);
217 __fio_client_add_cmd_option(client, opt);
221 struct fio_client *fio_client_add_explicit(struct client_ops *ops,
222 const char *hostname, int type,
225 struct fio_client *client;
227 client = malloc(sizeof(*client));
228 memset(client, 0, sizeof(*client));
230 INIT_FLIST_HEAD(&client->list);
231 INIT_FLIST_HEAD(&client->hash_list);
232 INIT_FLIST_HEAD(&client->arg_list);
233 INIT_FLIST_HEAD(&client->eta_list);
234 INIT_FLIST_HEAD(&client->cmd_list);
236 client->hostname = strdup(hostname);
238 if (type == Fio_client_socket)
243 ipv6 = type == Fio_client_ipv6;
244 if (fio_server_parse_host(hostname, ipv6,
245 &client->addr.sin_addr,
246 &client->addr6.sin6_addr))
255 client->type = ops->client_type;
257 __fio_client_add_cmd_option(client, "fio");
259 flist_add(&client->list, &client_list);
261 dprint(FD_NET, "client: added <%s>\n", client->hostname);
268 int fio_client_add_ini_file(void *cookie, const char *ini_file, int remote)
270 struct fio_client *client = cookie;
271 struct client_file *cf;
275 dprint(FD_NET, "client <%s>: add ini %s\n", client->hostname, ini_file);
277 new_size = (client->nr_files + 1) * sizeof(struct client_file);
278 new_files = realloc(client->files, new_size);
282 client->files = new_files;
283 cf = &client->files[client->nr_files];
284 cf->file = strdup(ini_file);
290 int fio_client_add(struct client_ops *ops, const char *hostname, void **cookie)
292 struct fio_client *existing = *cookie;
293 struct fio_client *client;
297 * We always add our "exec" name as the option, hence 1
300 if (existing->argc == 1)
301 flist_add_tail(&existing->arg_list, &arg_list);
303 while (!flist_empty(&arg_list))
304 flist_del_init(arg_list.next);
308 client = malloc(sizeof(*client));
309 memset(client, 0, sizeof(*client));
311 INIT_FLIST_HEAD(&client->list);
312 INIT_FLIST_HEAD(&client->hash_list);
313 INIT_FLIST_HEAD(&client->arg_list);
314 INIT_FLIST_HEAD(&client->eta_list);
315 INIT_FLIST_HEAD(&client->cmd_list);
317 if (fio_server_parse_string(hostname, &client->hostname,
318 &client->is_sock, &client->port,
319 &client->addr.sin_addr,
320 &client->addr6.sin6_addr,
327 client->type = ops->client_type;
329 __fio_client_add_cmd_option(client, "fio");
331 flist_add(&client->list, &client_list);
333 dprint(FD_NET, "client: added <%s>\n", client->hostname);
338 static void probe_client(struct fio_client *client)
340 struct cmd_client_probe_pdu pdu;
343 dprint(FD_NET, "client: send probe\n");
346 pdu.flags = __le64_to_cpu(FIO_PROBE_FLAG_ZLIB);
351 fio_net_send_cmd(client->fd, FIO_NET_CMD_PROBE, &pdu, sizeof(pdu), &tag, &client->cmd_list);
354 static int fio_client_connect_ip(struct fio_client *client)
356 struct sockaddr *addr;
361 client->addr6.sin6_family = AF_INET6;
362 client->addr6.sin6_port = htons(client->port);
364 addr = (struct sockaddr *) &client->addr6;
365 socklen = sizeof(client->addr6);
367 client->addr.sin_family = AF_INET;
368 client->addr.sin_port = htons(client->port);
370 addr = (struct sockaddr *) &client->addr;
371 socklen = sizeof(client->addr);
374 fd = socket(domain, SOCK_STREAM, 0);
378 log_err("fio: socket: %s\n", strerror(errno));
382 if (connect(fd, addr, socklen) < 0) {
385 log_err("fio: connect: %s\n", strerror(errno));
386 log_err("fio: failed to connect to %s:%u\n", client->hostname,
395 static int fio_client_connect_sock(struct fio_client *client)
397 struct sockaddr_un *addr = &client->addr_un;
401 memset(addr, 0, sizeof(*addr));
402 addr->sun_family = AF_UNIX;
403 strncpy(addr->sun_path, client->hostname, sizeof(addr->sun_path) - 1);
405 fd = socket(AF_UNIX, SOCK_STREAM, 0);
409 log_err("fio: socket: %s\n", strerror(errno));
413 len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
414 if (connect(fd, (struct sockaddr *) addr, len) < 0) {
417 log_err("fio: connect; %s\n", strerror(errno));
425 int fio_client_connect(struct fio_client *client)
429 dprint(FD_NET, "client: connect to host %s\n", client->hostname);
432 fd = fio_client_connect_sock(client);
434 fd = fio_client_connect_ip(client);
436 dprint(FD_NET, "client: %s connected %d\n", client->hostname, fd);
442 fio_client_add_hash(client);
443 client->state = Client_connected;
445 probe_client(client);
449 int fio_client_terminate(struct fio_client *client)
451 return fio_net_send_quit(client->fd);
454 void fio_clients_terminate(void)
456 struct flist_head *entry;
457 struct fio_client *client;
459 dprint(FD_NET, "client: terminate clients\n");
461 flist_for_each(entry, &client_list) {
462 client = flist_entry(entry, struct fio_client, list);
463 fio_client_terminate(client);
467 static void sig_int(int sig)
469 dprint(FD_NET, "client: got signal %d\n", sig);
470 fio_clients_terminate();
473 static void sig_show_status(int sig)
475 show_running_run_stats();
478 static void client_signal_handler(void)
480 struct sigaction act;
482 memset(&act, 0, sizeof(act));
483 act.sa_handler = sig_int;
484 act.sa_flags = SA_RESTART;
485 sigaction(SIGINT, &act, NULL);
487 memset(&act, 0, sizeof(act));
488 act.sa_handler = sig_int;
489 act.sa_flags = SA_RESTART;
490 sigaction(SIGTERM, &act, NULL);
492 /* Windows uses SIGBREAK as a quit signal from other applications */
494 memset(&act, 0, sizeof(act));
495 act.sa_handler = sig_int;
496 act.sa_flags = SA_RESTART;
497 sigaction(SIGBREAK, &act, NULL);
500 memset(&act, 0, sizeof(act));
501 act.sa_handler = sig_show_status;
502 act.sa_flags = SA_RESTART;
503 sigaction(SIGUSR1, &act, NULL);
506 static int send_client_cmd_line(struct fio_client *client)
508 struct cmd_single_line_pdu *cslp;
509 struct cmd_line_pdu *clp;
510 unsigned long offset;
516 dprint(FD_NET, "client: send cmdline %d\n", client->argc);
518 lens = malloc(client->argc * sizeof(unsigned int));
521 * Find out how much mem we need
523 for (i = 0, mem = 0; i < client->argc; i++) {
524 lens[i] = strlen(client->argv[i]) + 1;
529 * We need one cmd_line_pdu, and argc number of cmd_single_line_pdu
531 mem += sizeof(*clp) + (client->argc * sizeof(*cslp));
535 offset = sizeof(*clp);
537 for (i = 0; i < client->argc; i++) {
538 uint16_t arg_len = lens[i];
541 strcpy((char *) cslp->text, client->argv[i]);
542 cslp->len = cpu_to_le16(arg_len);
543 offset += sizeof(*cslp) + arg_len;
547 clp->lines = cpu_to_le16(client->argc);
548 clp->client_type = __cpu_to_le16(client->type);
549 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOBLINE, pdu, mem, NULL, NULL);
554 int fio_clients_connect(void)
556 struct fio_client *client;
557 struct flist_head *entry, *tmp;
562 WSAStartup(MAKEWORD(2, 2), &wsd);
565 dprint(FD_NET, "client: connect all\n");
567 client_signal_handler();
569 flist_for_each_safe(entry, tmp, &client_list) {
570 client = flist_entry(entry, struct fio_client, list);
572 ret = fio_client_connect(client);
574 remove_client(client);
578 if (client->argc > 1)
579 send_client_cmd_line(client);
585 int fio_start_client(struct fio_client *client)
587 dprint(FD_NET, "client: start %s\n", client->hostname);
588 return fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_RUN, 0, NULL);
591 int fio_start_all_clients(void)
593 struct fio_client *client;
594 struct flist_head *entry, *tmp;
597 dprint(FD_NET, "client: start all\n");
599 fio_client_json_init();
601 flist_for_each_safe(entry, tmp, &client_list) {
602 client = flist_entry(entry, struct fio_client, list);
604 ret = fio_start_client(client);
606 remove_client(client);
611 return flist_empty(&client_list);
614 static int __fio_client_send_remote_ini(struct fio_client *client,
615 const char *filename)
617 struct cmd_load_file_pdu *pdu;
621 dprint(FD_NET, "send remote ini %s to %s\n", filename, client->hostname);
623 p_size = sizeof(*pdu) + strlen(filename) + 1;
624 pdu = malloc(p_size);
625 memset(pdu, 0, p_size);
626 pdu->name_len = strlen(filename);
627 strcpy((char *) pdu->file, filename);
628 pdu->client_type = cpu_to_le16((uint16_t) client->type);
630 client->sent_job = 1;
631 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_LOAD_FILE, pdu, p_size,NULL, NULL);
637 * Send file contents to server backend. We could use sendfile(), but to remain
638 * more portable lets just read/write the darn thing.
640 static int __fio_client_send_local_ini(struct fio_client *client,
641 const char *filename)
643 struct cmd_job_pdu *pdu;
651 dprint(FD_NET, "send ini %s to %s\n", filename, client->hostname);
653 fd = open(filename, O_RDONLY);
657 log_err("fio: job file <%s> open: %s\n", filename, strerror(errno));
661 if (fstat(fd, &sb) < 0) {
664 log_err("fio: job file stat: %s\n", strerror(errno));
669 p_size = sb.st_size + sizeof(*pdu);
670 pdu = malloc(p_size);
676 ret = read(fd, p, len);
685 else if (errno == EAGAIN || errno == EINTR)
690 log_err("fio: failed reading job file %s\n", filename);
696 pdu->buf_len = __cpu_to_le32(sb.st_size);
697 pdu->client_type = cpu_to_le32(client->type);
699 client->sent_job = 1;
700 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, pdu, p_size, NULL, NULL);
706 int fio_client_send_ini(struct fio_client *client, const char *filename,
712 ret = __fio_client_send_local_ini(client, filename);
714 ret = __fio_client_send_remote_ini(client, filename);
717 client->sent_job = 1;
722 static int fio_client_send_cf(struct fio_client *client,
723 struct client_file *cf)
725 return fio_client_send_ini(client, cf->file, cf->remote);
728 int fio_clients_send_ini(const char *filename)
730 struct fio_client *client;
731 struct flist_head *entry, *tmp;
733 flist_for_each_safe(entry, tmp, &client_list) {
734 client = flist_entry(entry, struct fio_client, list);
736 if (client->nr_files) {
739 for (i = 0; i < client->nr_files; i++) {
740 struct client_file *cf;
742 cf = &client->files[i];
744 if (fio_client_send_cf(client, cf)) {
745 remove_client(client);
750 if (client->sent_job)
752 if (!filename || fio_client_send_ini(client, filename, 0))
753 remove_client(client);
759 int fio_client_update_options(struct fio_client *client,
760 struct thread_options *o, uint64_t *tag)
762 struct cmd_add_job_pdu pdu;
764 pdu.thread_number = cpu_to_le32(client->thread_number);
765 pdu.groupid = cpu_to_le32(client->groupid);
766 convert_thread_options_to_net(&pdu.top, o);
768 return fio_net_send_cmd(client->fd, FIO_NET_CMD_UPDATE_JOB, &pdu, sizeof(pdu), tag, &client->cmd_list);
771 static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
773 dst->max_val = le64_to_cpu(src->max_val);
774 dst->min_val = le64_to_cpu(src->min_val);
775 dst->samples = le64_to_cpu(src->samples);
778 * Floats arrive as IEEE 754 encoded uint64_t, convert back to double
780 dst->mean.u.f = fio_uint64_to_double(le64_to_cpu(dst->mean.u.i));
781 dst->S.u.f = fio_uint64_to_double(le64_to_cpu(dst->S.u.i));
784 static void convert_ts(struct thread_stat *dst, struct thread_stat *src)
788 dst->error = le32_to_cpu(src->error);
789 dst->thread_number = le32_to_cpu(src->thread_number);
790 dst->groupid = le32_to_cpu(src->groupid);
791 dst->pid = le32_to_cpu(src->pid);
792 dst->members = le32_to_cpu(src->members);
793 dst->unified_rw_rep = le32_to_cpu(src->unified_rw_rep);
795 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
796 convert_io_stat(&dst->clat_stat[i], &src->clat_stat[i]);
797 convert_io_stat(&dst->slat_stat[i], &src->slat_stat[i]);
798 convert_io_stat(&dst->lat_stat[i], &src->lat_stat[i]);
799 convert_io_stat(&dst->bw_stat[i], &src->bw_stat[i]);
802 dst->usr_time = le64_to_cpu(src->usr_time);
803 dst->sys_time = le64_to_cpu(src->sys_time);
804 dst->ctx = le64_to_cpu(src->ctx);
805 dst->minf = le64_to_cpu(src->minf);
806 dst->majf = le64_to_cpu(src->majf);
807 dst->clat_percentiles = le64_to_cpu(src->clat_percentiles);
809 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
810 fio_fp64_t *fps = &src->percentile_list[i];
811 fio_fp64_t *fpd = &dst->percentile_list[i];
813 fpd->u.f = fio_uint64_to_double(le64_to_cpu(fps->u.i));
816 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
817 dst->io_u_map[i] = le32_to_cpu(src->io_u_map[i]);
818 dst->io_u_submit[i] = le32_to_cpu(src->io_u_submit[i]);
819 dst->io_u_complete[i] = le32_to_cpu(src->io_u_complete[i]);
822 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
823 dst->io_u_lat_u[i] = le32_to_cpu(src->io_u_lat_u[i]);
824 dst->io_u_lat_m[i] = le32_to_cpu(src->io_u_lat_m[i]);
827 for (i = 0; i < DDIR_RWDIR_CNT; i++)
828 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
829 dst->io_u_plat[i][j] = le32_to_cpu(src->io_u_plat[i][j]);
831 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
832 dst->total_io_u[i] = le64_to_cpu(src->total_io_u[i]);
833 dst->short_io_u[i] = le64_to_cpu(src->short_io_u[i]);
834 dst->drop_io_u[i] = le64_to_cpu(src->drop_io_u[i]);
837 dst->total_submit = le64_to_cpu(src->total_submit);
838 dst->total_complete = le64_to_cpu(src->total_complete);
840 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
841 dst->io_bytes[i] = le64_to_cpu(src->io_bytes[i]);
842 dst->runtime[i] = le64_to_cpu(src->runtime[i]);
845 dst->total_run_time = le64_to_cpu(src->total_run_time);
846 dst->continue_on_error = le16_to_cpu(src->continue_on_error);
847 dst->total_err_count = le64_to_cpu(src->total_err_count);
848 dst->first_error = le32_to_cpu(src->first_error);
849 dst->kb_base = le32_to_cpu(src->kb_base);
850 dst->unit_base = le32_to_cpu(src->unit_base);
852 dst->latency_depth = le32_to_cpu(src->latency_depth);
853 dst->latency_target = le64_to_cpu(src->latency_target);
854 dst->latency_window = le64_to_cpu(src->latency_window);
855 dst->latency_percentile.u.f = fio_uint64_to_double(le64_to_cpu(src->latency_percentile.u.i));
858 static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
862 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
863 dst->max_run[i] = le64_to_cpu(src->max_run[i]);
864 dst->min_run[i] = le64_to_cpu(src->min_run[i]);
865 dst->max_bw[i] = le64_to_cpu(src->max_bw[i]);
866 dst->min_bw[i] = le64_to_cpu(src->min_bw[i]);
867 dst->io_kb[i] = le64_to_cpu(src->io_kb[i]);
868 dst->agg[i] = le64_to_cpu(src->agg[i]);
871 dst->kb_base = le32_to_cpu(src->kb_base);
872 dst->unit_base = le32_to_cpu(src->unit_base);
873 dst->groupid = le32_to_cpu(src->groupid);
874 dst->unified_rw_rep = le32_to_cpu(src->unified_rw_rep);
877 static void json_object_add_client_info(struct json_object *obj,
878 struct fio_client *client)
880 const char *hostname = client->hostname ? client->hostname : "";
882 json_object_add_value_string(obj, "hostname", hostname);
883 json_object_add_value_int(obj, "port", client->port);
886 static void handle_ts(struct fio_client *client, struct fio_net_cmd *cmd)
888 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
889 struct json_object *tsobj;
891 tsobj = show_thread_status(&p->ts, &p->rs);
892 client->did_stat = 1;
894 json_object_add_client_info(tsobj, client);
895 json_array_add_value_object(clients_array, tsobj);
898 if (!do_output_all_clients)
901 sum_thread_stats(&client_ts, &p->ts, sum_stat_nr);
902 sum_group_stats(&client_gs, &p->rs);
905 client_ts.thread_number = p->ts.thread_number;
906 client_ts.groupid = p->ts.groupid;
907 client_ts.unified_rw_rep = p->ts.unified_rw_rep;
909 if (++sum_stat_nr == sum_stat_clients) {
910 strcpy(client_ts.name, "All clients");
911 tsobj = show_thread_status(&client_ts, &client_gs);
913 json_object_add_client_info(tsobj, client);
914 json_array_add_value_object(clients_array, tsobj);
919 static void handle_gs(struct fio_client *client, struct fio_net_cmd *cmd)
921 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
923 show_group_stats(gs);
926 static void handle_text(struct fio_client *client, struct fio_net_cmd *cmd)
928 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
929 const char *buf = (const char *) pdu->buf;
933 name = client->name ? client->name : client->hostname;
935 if (!client->skip_newline)
936 fprintf(f_out, "<%s> ", name);
937 ret = fwrite(buf, pdu->buf_len, 1, f_out);
939 client->skip_newline = strchr(buf, '\n') == NULL;
942 static void convert_agg(struct disk_util_agg *agg)
946 for (i = 0; i < 2; i++) {
947 agg->ios[i] = le32_to_cpu(agg->ios[i]);
948 agg->merges[i] = le32_to_cpu(agg->merges[i]);
949 agg->sectors[i] = le64_to_cpu(agg->sectors[i]);
950 agg->ticks[i] = le32_to_cpu(agg->ticks[i]);
953 agg->io_ticks = le32_to_cpu(agg->io_ticks);
954 agg->time_in_queue = le32_to_cpu(agg->time_in_queue);
955 agg->slavecount = le32_to_cpu(agg->slavecount);
956 agg->max_util.u.f = fio_uint64_to_double(__le64_to_cpu(agg->max_util.u.i));
959 static void convert_dus(struct disk_util_stat *dus)
963 for (i = 0; i < 2; i++) {
964 dus->s.ios[i] = le32_to_cpu(dus->s.ios[i]);
965 dus->s.merges[i] = le32_to_cpu(dus->s.merges[i]);
966 dus->s.sectors[i] = le64_to_cpu(dus->s.sectors[i]);
967 dus->s.ticks[i] = le32_to_cpu(dus->s.ticks[i]);
970 dus->s.io_ticks = le32_to_cpu(dus->s.io_ticks);
971 dus->s.time_in_queue = le32_to_cpu(dus->s.time_in_queue);
972 dus->s.msec = le64_to_cpu(dus->s.msec);
975 static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd)
977 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
979 if (!client->disk_stats_shown) {
980 client->disk_stats_shown = 1;
981 log_info("\nDisk stats (read/write):\n");
984 if (output_format == FIO_OUTPUT_JSON) {
985 struct json_object *duobj;
986 json_array_add_disk_util(&du->dus, &du->agg, du_array);
987 duobj = json_array_last_value_object(du_array);
988 json_object_add_client_info(duobj, client);
990 print_disk_util(&du->dus, &du->agg, output_format == FIO_OUTPUT_TERSE);
993 static void convert_jobs_eta(struct jobs_eta *je)
997 je->nr_running = le32_to_cpu(je->nr_running);
998 je->nr_ramp = le32_to_cpu(je->nr_ramp);
999 je->nr_pending = le32_to_cpu(je->nr_pending);
1000 je->nr_setting_up = le32_to_cpu(je->nr_setting_up);
1001 je->files_open = le32_to_cpu(je->files_open);
1003 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1004 je->m_rate[i] = le32_to_cpu(je->m_rate[i]);
1005 je->t_rate[i] = le32_to_cpu(je->t_rate[i]);
1006 je->m_iops[i] = le32_to_cpu(je->m_iops[i]);
1007 je->t_iops[i] = le32_to_cpu(je->t_iops[i]);
1008 je->rate[i] = le32_to_cpu(je->rate[i]);
1009 je->iops[i] = le32_to_cpu(je->iops[i]);
1012 je->elapsed_sec = le64_to_cpu(je->elapsed_sec);
1013 je->eta_sec = le64_to_cpu(je->eta_sec);
1014 je->nr_threads = le32_to_cpu(je->nr_threads);
1015 je->is_pow2 = le32_to_cpu(je->is_pow2);
1016 je->unit_base = le32_to_cpu(je->unit_base);
1019 void fio_client_sum_jobs_eta(struct jobs_eta *dst, struct jobs_eta *je)
1023 dst->nr_running += je->nr_running;
1024 dst->nr_ramp += je->nr_ramp;
1025 dst->nr_pending += je->nr_pending;
1026 dst->nr_setting_up += je->nr_setting_up;
1027 dst->files_open += je->files_open;
1029 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1030 dst->m_rate[i] += je->m_rate[i];
1031 dst->t_rate[i] += je->t_rate[i];
1032 dst->m_iops[i] += je->m_iops[i];
1033 dst->t_iops[i] += je->t_iops[i];
1034 dst->rate[i] += je->rate[i];
1035 dst->iops[i] += je->iops[i];
1038 dst->elapsed_sec += je->elapsed_sec;
1040 if (je->eta_sec > dst->eta_sec)
1041 dst->eta_sec = je->eta_sec;
1043 dst->nr_threads += je->nr_threads;
1046 * This wont be correct for multiple strings, but at least it
1047 * works for the basic cases.
1049 strcpy((char *) dst->run_str, (char *) je->run_str);
1052 void fio_client_dec_jobs_eta(struct client_eta *eta, client_eta_op eta_fn)
1054 if (!--eta->pending) {
1060 static void remove_reply_cmd(struct fio_client *client, struct fio_net_cmd *cmd)
1062 struct fio_net_cmd_reply *reply = NULL;
1063 struct flist_head *entry;
1065 flist_for_each(entry, &client->cmd_list) {
1066 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
1068 if (cmd->tag == (uintptr_t) reply)
1075 log_err("fio: client: unable to find matching tag (%llx)\n", (unsigned long long) cmd->tag);
1079 flist_del(&reply->list);
1080 cmd->tag = reply->saved_tag;
1084 int fio_client_wait_for_reply(struct fio_client *client, uint64_t tag)
1087 struct fio_net_cmd_reply *reply = NULL;
1088 struct flist_head *entry;
1090 flist_for_each(entry, &client->cmd_list) {
1091 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
1093 if (tag == (uintptr_t) reply)
1108 static void handle_eta(struct fio_client *client, struct fio_net_cmd *cmd)
1110 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
1111 struct client_eta *eta = (struct client_eta *) (uintptr_t) cmd->tag;
1113 dprint(FD_NET, "client: got eta tag %p, %d\n", eta, eta->pending);
1115 assert(client->eta_in_flight == eta);
1117 client->eta_in_flight = NULL;
1118 flist_del_init(&client->eta_list);
1120 if (client->ops->jobs_eta)
1121 client->ops->jobs_eta(client, je);
1123 fio_client_sum_jobs_eta(&eta->eta, je);
1124 fio_client_dec_jobs_eta(eta, client->ops->eta);
1127 static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd)
1129 struct cmd_probe_reply_pdu *probe = (struct cmd_probe_reply_pdu *) cmd->payload;
1130 const char *os, *arch;
1133 os = fio_get_os_string(probe->os);
1137 arch = fio_get_arch_string(probe->arch);
1141 sprintf(bit, "%d-bit", probe->bpp * 8);
1142 probe->flags = le64_to_cpu(probe->flags);
1144 log_info("hostname=%s, be=%u, %s, os=%s, arch=%s, fio=%s, flags=%lx\n",
1145 probe->hostname, probe->bigendian, bit, os, arch,
1146 probe->fio_version, (unsigned long) probe->flags);
1149 client->name = strdup((char *) probe->hostname);
1152 static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd)
1154 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
1156 client->state = Client_started;
1157 client->jobs = le32_to_cpu(pdu->jobs);
1158 client->nr_stat = le32_to_cpu(pdu->stat_outputs);
1160 if (sum_stat_clients > 1)
1161 do_output_all_clients = 1;
1163 sum_stat_clients += client->nr_stat;
1166 static void handle_stop(struct fio_client *client, struct fio_net_cmd *cmd)
1169 log_info("client <%s>: exited with error %d\n", client->hostname, client->error);
1172 static void convert_stop(struct fio_net_cmd *cmd)
1174 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
1176 pdu->error = le32_to_cpu(pdu->error);
1179 static void convert_text(struct fio_net_cmd *cmd)
1181 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
1183 pdu->level = le32_to_cpu(pdu->level);
1184 pdu->buf_len = le32_to_cpu(pdu->buf_len);
1185 pdu->log_sec = le64_to_cpu(pdu->log_sec);
1186 pdu->log_usec = le64_to_cpu(pdu->log_usec);
1189 static struct cmd_iolog_pdu *convert_iolog_gz(struct fio_net_cmd *cmd,
1190 struct cmd_iolog_pdu *pdu)
1193 struct cmd_iolog_pdu *ret;
1195 uint32_t nr_samples;
1199 stream.zalloc = Z_NULL;
1200 stream.zfree = Z_NULL;
1201 stream.opaque = Z_NULL;
1202 stream.avail_in = 0;
1203 stream.next_in = Z_NULL;
1205 if (inflateInit(&stream) != Z_OK)
1209 * Get header first, it's not compressed
1211 nr_samples = le64_to_cpu(pdu->nr_samples);
1213 total = nr_samples * __log_entry_sz(le32_to_cpu(pdu->log_offset));
1214 ret = malloc(total + sizeof(*pdu));
1215 ret->nr_samples = nr_samples;
1217 memcpy(ret, pdu, sizeof(*pdu));
1219 p = (void *) ret + sizeof(*pdu);
1221 stream.avail_in = cmd->pdu_len - sizeof(*pdu);
1222 stream.next_in = (void *) pdu + sizeof(*pdu);
1223 while (stream.avail_in) {
1224 unsigned int this_chunk = 65536;
1225 unsigned int this_len;
1228 if (this_chunk > total)
1231 stream.avail_out = this_chunk;
1232 stream.next_out = p;
1233 err = inflate(&stream, Z_NO_FLUSH);
1234 /* may be Z_OK, or Z_STREAM_END */
1236 log_err("fio: inflate error %d\n", err);
1242 this_len = this_chunk - stream.avail_out;
1248 inflateEnd(&stream);
1256 * This has been compressed on the server side, since it can be big.
1259 static struct cmd_iolog_pdu *convert_iolog(struct fio_net_cmd *cmd)
1261 struct cmd_iolog_pdu *pdu = (struct cmd_iolog_pdu *) cmd->payload;
1262 struct cmd_iolog_pdu *ret;
1267 * Convert if compressed and we support it. If it's not
1268 * compressed, we need not do anything.
1270 if (le32_to_cpu(pdu->compressed)) {
1272 log_err("fio: server sent compressed data by mistake\n");
1275 ret = convert_iolog_gz(cmd, pdu);
1277 log_err("fio: failed decompressing log\n");
1283 ret->nr_samples = le64_to_cpu(ret->nr_samples);
1284 ret->thread_number = le32_to_cpu(ret->thread_number);
1285 ret->log_type = le32_to_cpu(ret->log_type);
1286 ret->compressed = le32_to_cpu(ret->compressed);
1287 ret->log_offset = le32_to_cpu(ret->log_offset);
1289 samples = &ret->samples[0];
1290 for (i = 0; i < ret->nr_samples; i++) {
1291 struct io_sample *s;
1293 s = __get_sample(samples, ret->log_offset, i);
1294 s->time = le64_to_cpu(s->time);
1295 s->val = le64_to_cpu(s->val);
1296 s->__ddir = le32_to_cpu(s->__ddir);
1297 s->bs = le32_to_cpu(s->bs);
1299 if (ret->log_offset) {
1300 struct io_sample_offset *so = (void *) s;
1302 so->offset = le64_to_cpu(so->offset);
1309 int fio_handle_client(struct fio_client *client)
1311 struct client_ops *ops = client->ops;
1312 struct fio_net_cmd *cmd;
1314 dprint(FD_NET, "client: handle %s\n", client->hostname);
1316 cmd = fio_net_recv_cmd(client->fd);
1320 dprint(FD_NET, "client: got cmd op %s from %s (pdu=%u)\n",
1321 fio_server_op(cmd->opcode), client->hostname, cmd->pdu_len);
1323 switch (cmd->opcode) {
1324 case FIO_NET_CMD_QUIT:
1326 ops->quit(client, cmd);
1327 remove_client(client);
1330 case FIO_NET_CMD_TEXT:
1332 ops->text(client, cmd);
1335 case FIO_NET_CMD_DU: {
1336 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
1338 convert_dus(&du->dus);
1339 convert_agg(&du->agg);
1341 ops->disk_util(client, cmd);
1345 case FIO_NET_CMD_TS: {
1346 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
1348 convert_ts(&p->ts, &p->ts);
1349 convert_gs(&p->rs, &p->rs);
1351 ops->thread_status(client, cmd);
1355 case FIO_NET_CMD_GS: {
1356 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
1360 ops->group_stats(client, cmd);
1364 case FIO_NET_CMD_ETA: {
1365 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
1367 remove_reply_cmd(client, cmd);
1368 convert_jobs_eta(je);
1369 handle_eta(client, cmd);
1373 case FIO_NET_CMD_PROBE:
1374 remove_reply_cmd(client, cmd);
1375 ops->probe(client, cmd);
1378 case FIO_NET_CMD_SERVER_START:
1379 client->state = Client_running;
1381 ops->job_start(client, cmd);
1384 case FIO_NET_CMD_START: {
1385 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
1387 pdu->jobs = le32_to_cpu(pdu->jobs);
1388 ops->start(client, cmd);
1392 case FIO_NET_CMD_STOP: {
1393 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
1396 client->state = Client_stopped;
1397 client->error = le32_to_cpu(pdu->error);
1398 client->signal = le32_to_cpu(pdu->signal);
1399 ops->stop(client, cmd);
1403 case FIO_NET_CMD_ADD_JOB: {
1404 struct cmd_add_job_pdu *pdu = (struct cmd_add_job_pdu *) cmd->payload;
1406 client->thread_number = le32_to_cpu(pdu->thread_number);
1407 client->groupid = le32_to_cpu(pdu->groupid);
1410 ops->add_job(client, cmd);
1414 case FIO_NET_CMD_IOLOG:
1416 struct cmd_iolog_pdu *pdu;
1418 pdu = convert_iolog(cmd);
1419 ops->iolog(client, pdu);
1423 case FIO_NET_CMD_UPDATE_JOB:
1424 ops->update_job(client, cmd);
1425 remove_reply_cmd(client, cmd);
1429 log_err("fio: unknown client op: %s\n", fio_server_op(cmd->opcode));
1437 static void request_client_etas(struct client_ops *ops)
1439 struct fio_client *client;
1440 struct flist_head *entry;
1441 struct client_eta *eta;
1444 dprint(FD_NET, "client: request eta (%d)\n", nr_clients);
1446 eta = calloc(1, sizeof(*eta) + __THREAD_RUNSTR_SZ(REAL_MAX_JOBS));
1447 eta->pending = nr_clients;
1449 flist_for_each(entry, &client_list) {
1450 client = flist_entry(entry, struct fio_client, list);
1452 if (!flist_empty(&client->eta_list)) {
1456 if (client->state != Client_running)
1459 assert(!client->eta_in_flight);
1460 flist_add_tail(&client->eta_list, &eta_list);
1461 client->eta_in_flight = eta;
1462 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_SEND_ETA,
1463 (uintptr_t) eta, &client->cmd_list);
1467 fio_client_dec_jobs_eta(eta, ops->eta);
1469 dprint(FD_NET, "client: requested eta tag %p\n", eta);
1472 static int client_check_cmd_timeout(struct fio_client *client,
1473 struct timeval *now)
1475 struct fio_net_cmd_reply *reply;
1476 struct flist_head *entry, *tmp;
1479 flist_for_each_safe(entry, tmp, &client->cmd_list) {
1480 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
1482 if (mtime_since(&reply->tv, now) < FIO_NET_CLIENT_TIMEOUT)
1485 log_err("fio: client %s, timeout on cmd %s\n", client->hostname,
1486 fio_server_op(reply->opcode));
1487 flist_del(&reply->list);
1492 return flist_empty(&client->cmd_list) && ret;
1495 static int fio_check_clients_timed_out(void)
1497 struct fio_client *client;
1498 struct flist_head *entry, *tmp;
1502 fio_gettime(&tv, NULL);
1504 flist_for_each_safe(entry, tmp, &client_list) {
1505 client = flist_entry(entry, struct fio_client, list);
1507 if (flist_empty(&client->cmd_list))
1510 if (!client_check_cmd_timeout(client, &tv))
1513 if (client->ops->timed_out)
1514 client->ops->timed_out(client);
1516 log_err("fio: client %s timed out\n", client->hostname);
1518 remove_client(client);
1525 int fio_handle_clients(struct client_ops *ops)
1527 struct pollfd *pfds;
1528 int i, ret = 0, retval = 0;
1530 fio_gettime(&eta_tv, NULL);
1532 pfds = malloc(nr_clients * sizeof(struct pollfd));
1534 init_thread_stat(&client_ts);
1535 init_group_run_stat(&client_gs);
1537 while (!exit_backend && nr_clients) {
1538 struct flist_head *entry, *tmp;
1539 struct fio_client *client;
1542 flist_for_each_safe(entry, tmp, &client_list) {
1543 client = flist_entry(entry, struct fio_client, list);
1545 if (!client->sent_job && !client->ops->stay_connected &&
1546 flist_empty(&client->cmd_list)) {
1547 remove_client(client);
1551 pfds[i].fd = client->fd;
1552 pfds[i].events = POLLIN;
1559 assert(i == nr_clients);
1564 fio_gettime(&tv, NULL);
1565 if (mtime_since(&eta_tv, &tv) >= 900) {
1566 request_client_etas(ops);
1567 memcpy(&eta_tv, &tv, sizeof(tv));
1569 if (fio_check_clients_timed_out())
1573 ret = poll(pfds, nr_clients, ops->eta_msec);
1577 log_err("fio: poll clients: %s\n", strerror(errno));
1583 for (i = 0; i < nr_clients; i++) {
1584 if (!(pfds[i].revents & POLLIN))
1587 client = find_client_by_fd(pfds[i].fd);
1589 log_err("fio: unknown client fd %ld\n", (long) pfds[i].fd);
1592 if (!fio_handle_client(client)) {
1593 log_info("client: host=%s disconnected\n",
1595 remove_client(client);
1597 } else if (client->error)
1599 fio_put_client(client);
1603 fio_client_json_fini();