11 #include <sys/socket.h>
13 #include <netinet/in.h>
14 #include <arpa/inet.h>
28 static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd);
29 static void handle_ts(struct fio_client *client, struct fio_net_cmd *cmd);
30 static void handle_gs(struct fio_client *client, struct fio_net_cmd *cmd);
31 static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd);
32 static void handle_text(struct fio_client *client, struct fio_net_cmd *cmd);
33 static void handle_stop(struct fio_client *client, struct fio_net_cmd *cmd);
34 static void handle_start(struct fio_client *client, 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 timeval eta_tv;
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_array *clients_array = NULL;
63 static struct json_array *du_array = NULL;
65 static int error_clients;
67 #define FIO_CLIENT_HASH_BITS 7
68 #define FIO_CLIENT_HASH_SZ (1 << FIO_CLIENT_HASH_BITS)
69 #define FIO_CLIENT_HASH_MASK (FIO_CLIENT_HASH_SZ - 1)
70 static struct flist_head client_hash[FIO_CLIENT_HASH_SZ];
72 static void fio_client_add_hash(struct fio_client *client)
74 int bucket = hash_long(client->fd, FIO_CLIENT_HASH_BITS);
76 bucket &= FIO_CLIENT_HASH_MASK;
77 flist_add(&client->hash_list, &client_hash[bucket]);
80 static void fio_client_remove_hash(struct fio_client *client)
82 if (!flist_empty(&client->hash_list))
83 flist_del_init(&client->hash_list);
86 static void fio_init fio_client_hash_init(void)
90 for (i = 0; i < FIO_CLIENT_HASH_SZ; i++)
91 INIT_FLIST_HEAD(&client_hash[i]);
94 static int read_data(int fd, void *data, size_t size)
99 ret = read(fd, data, size);
101 if (errno == EAGAIN || errno == EINTR)
118 static void fio_client_json_init(void)
120 if (!(output_format & FIO_OUTPUT_JSON))
122 root = json_create_object();
123 json_object_add_value_string(root, "fio version", fio_version_string);
124 clients_array = json_create_array();
125 json_object_add_value_array(root, "client_stats", clients_array);
126 du_array = json_create_array();
127 json_object_add_value_array(root, "disk_util", du_array);
130 static void fio_client_json_fini(void)
132 if (!(output_format & FIO_OUTPUT_JSON))
134 json_print_object(root, NULL);
136 json_free_object(root);
138 clients_array = NULL;
142 static struct fio_client *find_client_by_fd(int fd)
144 int bucket = hash_long(fd, FIO_CLIENT_HASH_BITS) & FIO_CLIENT_HASH_MASK;
145 struct fio_client *client;
146 struct flist_head *entry;
148 flist_for_each(entry, &client_hash[bucket]) {
149 client = flist_entry(entry, struct fio_client, hash_list);
151 if (client->fd == fd) {
160 void fio_put_client(struct fio_client *client)
165 free(client->hostname);
170 while (client->nr_files) {
171 struct client_file *cf = &client->files[--client->nr_files];
178 if (!client->did_stat)
187 static int fio_client_dec_jobs_eta(struct client_eta *eta, client_eta_op eta_fn)
189 if (!--eta->pending) {
198 static void remove_client(struct fio_client *client)
200 assert(client->refs);
202 dprint(FD_NET, "client: removed <%s>\n", client->hostname);
204 if (!flist_empty(&client->list))
205 flist_del_init(&client->list);
207 fio_client_remove_hash(client);
209 if (!flist_empty(&client->eta_list)) {
210 flist_del_init(&client->eta_list);
211 fio_client_dec_jobs_eta(client->eta_in_flight, client->ops->eta);
217 if (client->ops->removed)
218 client->ops->removed(client);
221 fio_put_client(client);
224 struct fio_client *fio_get_client(struct fio_client *client)
230 static void __fio_client_add_cmd_option(struct fio_client *client,
235 index = client->argc++;
236 client->argv = realloc(client->argv, sizeof(char *) * client->argc);
237 client->argv[index] = strdup(opt);
238 dprint(FD_NET, "client: add cmd %d: %s\n", index, opt);
241 void fio_client_add_cmd_option(void *cookie, const char *opt)
243 struct fio_client *client = cookie;
244 struct flist_head *entry;
249 __fio_client_add_cmd_option(client, opt);
252 * Duplicate arguments to shared client group
254 flist_for_each(entry, &arg_list) {
255 client = flist_entry(entry, struct fio_client, arg_list);
257 __fio_client_add_cmd_option(client, opt);
261 struct fio_client *fio_client_add_explicit(struct client_ops *ops,
262 const char *hostname, int type,
265 struct fio_client *client;
267 client = malloc(sizeof(*client));
268 memset(client, 0, sizeof(*client));
270 INIT_FLIST_HEAD(&client->list);
271 INIT_FLIST_HEAD(&client->hash_list);
272 INIT_FLIST_HEAD(&client->arg_list);
273 INIT_FLIST_HEAD(&client->eta_list);
274 INIT_FLIST_HEAD(&client->cmd_list);
276 client->hostname = strdup(hostname);
278 if (type == Fio_client_socket)
283 ipv6 = type == Fio_client_ipv6;
284 if (fio_server_parse_host(hostname, ipv6,
285 &client->addr.sin_addr,
286 &client->addr6.sin6_addr))
295 client->type = ops->client_type;
297 __fio_client_add_cmd_option(client, "fio");
299 flist_add(&client->list, &client_list);
301 dprint(FD_NET, "client: added <%s>\n", client->hostname);
308 int fio_client_add_ini_file(void *cookie, const char *ini_file, int remote)
310 struct fio_client *client = cookie;
311 struct client_file *cf;
318 dprint(FD_NET, "client <%s>: add ini %s\n", client->hostname, ini_file);
320 new_size = (client->nr_files + 1) * sizeof(struct client_file);
321 new_files = realloc(client->files, new_size);
325 client->files = new_files;
326 cf = &client->files[client->nr_files];
327 cf->file = strdup(ini_file);
333 int fio_client_add(struct client_ops *ops, const char *hostname, void **cookie)
335 struct fio_client *existing = *cookie;
336 struct fio_client *client;
340 * We always add our "exec" name as the option, hence 1
343 if (existing->argc == 1)
344 flist_add_tail(&existing->arg_list, &arg_list);
346 while (!flist_empty(&arg_list))
347 flist_del_init(arg_list.next);
351 client = malloc(sizeof(*client));
352 memset(client, 0, sizeof(*client));
354 INIT_FLIST_HEAD(&client->list);
355 INIT_FLIST_HEAD(&client->hash_list);
356 INIT_FLIST_HEAD(&client->arg_list);
357 INIT_FLIST_HEAD(&client->eta_list);
358 INIT_FLIST_HEAD(&client->cmd_list);
360 if (fio_server_parse_string(hostname, &client->hostname,
361 &client->is_sock, &client->port,
362 &client->addr.sin_addr,
363 &client->addr6.sin6_addr,
370 client->type = ops->client_type;
372 __fio_client_add_cmd_option(client, "fio");
374 flist_add(&client->list, &client_list);
376 dprint(FD_NET, "client: added <%s>\n", client->hostname);
381 static const char *server_name(struct fio_client *client, char *buf,
387 from = inet_ntop(AF_INET6, (struct sockaddr *) &client->addr6.sin6_addr, buf, bufsize);
388 else if (client->is_sock)
391 from = inet_ntop(AF_INET, (struct sockaddr *) &client->addr.sin_addr, buf, bufsize);
396 static void probe_client(struct fio_client *client)
398 struct cmd_client_probe_pdu pdu;
403 dprint(FD_NET, "client: send probe\n");
406 pdu.flags = __le64_to_cpu(FIO_PROBE_FLAG_ZLIB);
411 sname = server_name(client, buf, sizeof(buf));
412 memset(pdu.server, 0, sizeof(pdu.server));
413 strncpy((char *) pdu.server, sname, sizeof(pdu.server) - 1);
415 fio_net_send_cmd(client->fd, FIO_NET_CMD_PROBE, &pdu, sizeof(pdu), &tag, &client->cmd_list);
418 static int fio_client_connect_ip(struct fio_client *client)
420 struct sockaddr *addr;
425 client->addr6.sin6_family = AF_INET6;
426 client->addr6.sin6_port = htons(client->port);
428 addr = (struct sockaddr *) &client->addr6;
429 socklen = sizeof(client->addr6);
431 client->addr.sin_family = AF_INET;
432 client->addr.sin_port = htons(client->port);
434 addr = (struct sockaddr *) &client->addr;
435 socklen = sizeof(client->addr);
438 fd = socket(domain, SOCK_STREAM, 0);
442 log_err("fio: socket: %s\n", strerror(errno));
446 if (connect(fd, addr, socklen) < 0) {
449 log_err("fio: connect: %s\n", strerror(errno));
450 log_err("fio: failed to connect to %s:%u\n", client->hostname,
459 static int fio_client_connect_sock(struct fio_client *client)
461 struct sockaddr_un *addr = &client->addr_un;
465 memset(addr, 0, sizeof(*addr));
466 addr->sun_family = AF_UNIX;
467 strncpy(addr->sun_path, client->hostname, sizeof(addr->sun_path) - 1);
469 fd = socket(AF_UNIX, SOCK_STREAM, 0);
473 log_err("fio: socket: %s\n", strerror(errno));
477 len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
478 if (connect(fd, (struct sockaddr *) addr, len) < 0) {
481 log_err("fio: connect; %s\n", strerror(errno));
489 int fio_client_connect(struct fio_client *client)
493 dprint(FD_NET, "client: connect to host %s\n", client->hostname);
496 fd = fio_client_connect_sock(client);
498 fd = fio_client_connect_ip(client);
500 dprint(FD_NET, "client: %s connected %d\n", client->hostname, fd);
506 fio_client_add_hash(client);
507 client->state = Client_connected;
509 probe_client(client);
513 int fio_client_terminate(struct fio_client *client)
515 return fio_net_send_quit(client->fd);
518 void fio_clients_terminate(void)
520 struct flist_head *entry;
521 struct fio_client *client;
523 dprint(FD_NET, "client: terminate clients\n");
525 flist_for_each(entry, &client_list) {
526 client = flist_entry(entry, struct fio_client, list);
527 fio_client_terminate(client);
531 static void sig_int(int sig)
533 dprint(FD_NET, "client: got signal %d\n", sig);
534 fio_clients_terminate();
537 static void client_signal_handler(void)
539 struct sigaction act;
541 memset(&act, 0, sizeof(act));
542 act.sa_handler = sig_int;
543 act.sa_flags = SA_RESTART;
544 sigaction(SIGINT, &act, NULL);
546 memset(&act, 0, sizeof(act));
547 act.sa_handler = sig_int;
548 act.sa_flags = SA_RESTART;
549 sigaction(SIGTERM, &act, NULL);
551 /* Windows uses SIGBREAK as a quit signal from other applications */
553 memset(&act, 0, sizeof(act));
554 act.sa_handler = sig_int;
555 act.sa_flags = SA_RESTART;
556 sigaction(SIGBREAK, &act, NULL);
559 memset(&act, 0, sizeof(act));
560 act.sa_handler = sig_show_status;
561 act.sa_flags = SA_RESTART;
562 sigaction(SIGUSR1, &act, NULL);
565 static int send_client_cmd_line(struct fio_client *client)
567 struct cmd_single_line_pdu *cslp;
568 struct cmd_line_pdu *clp;
569 unsigned long offset;
575 dprint(FD_NET, "client: send cmdline %d\n", client->argc);
577 lens = malloc(client->argc * sizeof(unsigned int));
580 * Find out how much mem we need
582 for (i = 0, mem = 0; i < client->argc; i++) {
583 lens[i] = strlen(client->argv[i]) + 1;
588 * We need one cmd_line_pdu, and argc number of cmd_single_line_pdu
590 mem += sizeof(*clp) + (client->argc * sizeof(*cslp));
594 offset = sizeof(*clp);
596 for (i = 0; i < client->argc; i++) {
597 uint16_t arg_len = lens[i];
600 strcpy((char *) cslp->text, client->argv[i]);
601 cslp->len = cpu_to_le16(arg_len);
602 offset += sizeof(*cslp) + arg_len;
606 clp->lines = cpu_to_le16(client->argc);
607 clp->client_type = __cpu_to_le16(client->type);
608 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOBLINE, pdu, mem, NULL, NULL);
613 int fio_clients_connect(void)
615 struct fio_client *client;
616 struct flist_head *entry, *tmp;
621 WSAStartup(MAKEWORD(2, 2), &wsd);
624 dprint(FD_NET, "client: connect all\n");
626 client_signal_handler();
628 flist_for_each_safe(entry, tmp, &client_list) {
629 client = flist_entry(entry, struct fio_client, list);
631 ret = fio_client_connect(client);
633 remove_client(client);
637 if (client->argc > 1)
638 send_client_cmd_line(client);
644 int fio_start_client(struct fio_client *client)
646 dprint(FD_NET, "client: start %s\n", client->hostname);
647 return fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_RUN, 0, NULL);
650 int fio_start_all_clients(void)
652 struct fio_client *client;
653 struct flist_head *entry, *tmp;
656 dprint(FD_NET, "client: start all\n");
658 fio_client_json_init();
660 flist_for_each_safe(entry, tmp, &client_list) {
661 client = flist_entry(entry, struct fio_client, list);
663 ret = fio_start_client(client);
665 remove_client(client);
670 return flist_empty(&client_list);
673 static int __fio_client_send_remote_ini(struct fio_client *client,
674 const char *filename)
676 struct cmd_load_file_pdu *pdu;
680 dprint(FD_NET, "send remote ini %s to %s\n", filename, client->hostname);
682 p_size = sizeof(*pdu) + strlen(filename) + 1;
683 pdu = malloc(p_size);
684 memset(pdu, 0, p_size);
685 pdu->name_len = strlen(filename);
686 strcpy((char *) pdu->file, filename);
687 pdu->client_type = cpu_to_le16((uint16_t) client->type);
689 client->sent_job = 1;
690 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_LOAD_FILE, pdu, p_size,NULL, NULL);
696 * Send file contents to server backend. We could use sendfile(), but to remain
697 * more portable lets just read/write the darn thing.
699 static int __fio_client_send_local_ini(struct fio_client *client,
700 const char *filename)
702 struct cmd_job_pdu *pdu;
710 dprint(FD_NET, "send ini %s to %s\n", filename, client->hostname);
712 fd = open(filename, O_RDONLY);
715 log_err("fio: job file <%s> open: %s\n", filename, strerror(errno));
719 if (fstat(fd, &sb) < 0) {
721 log_err("fio: job file stat: %s\n", strerror(errno));
726 p_size = sb.st_size + sizeof(*pdu);
727 pdu = malloc(p_size);
732 if (read_data(fd, p, len)) {
733 log_err("fio: failed reading job file %s\n", filename);
739 pdu->buf_len = __cpu_to_le32(sb.st_size);
740 pdu->client_type = cpu_to_le32(client->type);
742 client->sent_job = 1;
743 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, pdu, p_size, NULL, NULL);
749 int fio_client_send_ini(struct fio_client *client, const char *filename,
755 ret = __fio_client_send_local_ini(client, filename);
757 ret = __fio_client_send_remote_ini(client, filename);
760 client->sent_job = 1;
765 static int fio_client_send_cf(struct fio_client *client,
766 struct client_file *cf)
768 return fio_client_send_ini(client, cf->file, cf->remote);
771 int fio_clients_send_ini(const char *filename)
773 struct fio_client *client;
774 struct flist_head *entry, *tmp;
776 flist_for_each_safe(entry, tmp, &client_list) {
777 client = flist_entry(entry, struct fio_client, list);
779 if (client->nr_files) {
782 for (i = 0; i < client->nr_files; i++) {
783 struct client_file *cf;
785 cf = &client->files[i];
787 if (fio_client_send_cf(client, cf)) {
788 remove_client(client);
793 if (client->sent_job)
795 if (!filename || fio_client_send_ini(client, filename, 0))
796 remove_client(client);
802 int fio_client_update_options(struct fio_client *client,
803 struct thread_options *o, uint64_t *tag)
805 struct cmd_add_job_pdu pdu;
807 pdu.thread_number = cpu_to_le32(client->thread_number);
808 pdu.groupid = cpu_to_le32(client->groupid);
809 convert_thread_options_to_net(&pdu.top, o);
811 return fio_net_send_cmd(client->fd, FIO_NET_CMD_UPDATE_JOB, &pdu, sizeof(pdu), tag, &client->cmd_list);
814 static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
816 dst->max_val = le64_to_cpu(src->max_val);
817 dst->min_val = le64_to_cpu(src->min_val);
818 dst->samples = le64_to_cpu(src->samples);
821 * Floats arrive as IEEE 754 encoded uint64_t, convert back to double
823 dst->mean.u.f = fio_uint64_to_double(le64_to_cpu(dst->mean.u.i));
824 dst->S.u.f = fio_uint64_to_double(le64_to_cpu(dst->S.u.i));
827 static void convert_ts(struct thread_stat *dst, struct thread_stat *src)
831 dst->error = le32_to_cpu(src->error);
832 dst->thread_number = le32_to_cpu(src->thread_number);
833 dst->groupid = le32_to_cpu(src->groupid);
834 dst->pid = le32_to_cpu(src->pid);
835 dst->members = le32_to_cpu(src->members);
836 dst->unified_rw_rep = le32_to_cpu(src->unified_rw_rep);
838 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
839 convert_io_stat(&dst->clat_stat[i], &src->clat_stat[i]);
840 convert_io_stat(&dst->slat_stat[i], &src->slat_stat[i]);
841 convert_io_stat(&dst->lat_stat[i], &src->lat_stat[i]);
842 convert_io_stat(&dst->bw_stat[i], &src->bw_stat[i]);
845 dst->usr_time = le64_to_cpu(src->usr_time);
846 dst->sys_time = le64_to_cpu(src->sys_time);
847 dst->ctx = le64_to_cpu(src->ctx);
848 dst->minf = le64_to_cpu(src->minf);
849 dst->majf = le64_to_cpu(src->majf);
850 dst->clat_percentiles = le64_to_cpu(src->clat_percentiles);
851 dst->percentile_precision = le64_to_cpu(src->percentile_precision);
853 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
854 fio_fp64_t *fps = &src->percentile_list[i];
855 fio_fp64_t *fpd = &dst->percentile_list[i];
857 fpd->u.f = fio_uint64_to_double(le64_to_cpu(fps->u.i));
860 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
861 dst->io_u_map[i] = le32_to_cpu(src->io_u_map[i]);
862 dst->io_u_submit[i] = le32_to_cpu(src->io_u_submit[i]);
863 dst->io_u_complete[i] = le32_to_cpu(src->io_u_complete[i]);
866 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
867 dst->io_u_lat_u[i] = le32_to_cpu(src->io_u_lat_u[i]);
868 dst->io_u_lat_m[i] = le32_to_cpu(src->io_u_lat_m[i]);
871 for (i = 0; i < DDIR_RWDIR_CNT; i++)
872 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
873 dst->io_u_plat[i][j] = le32_to_cpu(src->io_u_plat[i][j]);
875 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
876 dst->total_io_u[i] = le64_to_cpu(src->total_io_u[i]);
877 dst->short_io_u[i] = le64_to_cpu(src->short_io_u[i]);
878 dst->drop_io_u[i] = le64_to_cpu(src->drop_io_u[i]);
881 dst->total_submit = le64_to_cpu(src->total_submit);
882 dst->total_complete = le64_to_cpu(src->total_complete);
884 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
885 dst->io_bytes[i] = le64_to_cpu(src->io_bytes[i]);
886 dst->runtime[i] = le64_to_cpu(src->runtime[i]);
889 dst->total_run_time = le64_to_cpu(src->total_run_time);
890 dst->continue_on_error = le16_to_cpu(src->continue_on_error);
891 dst->total_err_count = le64_to_cpu(src->total_err_count);
892 dst->first_error = le32_to_cpu(src->first_error);
893 dst->kb_base = le32_to_cpu(src->kb_base);
894 dst->unit_base = le32_to_cpu(src->unit_base);
896 dst->latency_depth = le32_to_cpu(src->latency_depth);
897 dst->latency_target = le64_to_cpu(src->latency_target);
898 dst->latency_window = le64_to_cpu(src->latency_window);
899 dst->latency_percentile.u.f = fio_uint64_to_double(le64_to_cpu(src->latency_percentile.u.i));
901 dst->nr_block_infos = le64_to_cpu(src->nr_block_infos);
902 for (i = 0; i < dst->nr_block_infos; i++)
903 dst->block_infos[i] = le32_to_cpu(src->block_infos[i]);
906 static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
910 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
911 dst->max_run[i] = le64_to_cpu(src->max_run[i]);
912 dst->min_run[i] = le64_to_cpu(src->min_run[i]);
913 dst->max_bw[i] = le64_to_cpu(src->max_bw[i]);
914 dst->min_bw[i] = le64_to_cpu(src->min_bw[i]);
915 dst->io_kb[i] = le64_to_cpu(src->io_kb[i]);
916 dst->agg[i] = le64_to_cpu(src->agg[i]);
919 dst->kb_base = le32_to_cpu(src->kb_base);
920 dst->unit_base = le32_to_cpu(src->unit_base);
921 dst->groupid = le32_to_cpu(src->groupid);
922 dst->unified_rw_rep = le32_to_cpu(src->unified_rw_rep);
925 static void json_object_add_client_info(struct json_object *obj,
926 struct fio_client *client)
928 const char *hostname = client->hostname ? client->hostname : "";
930 json_object_add_value_string(obj, "hostname", hostname);
931 json_object_add_value_int(obj, "port", client->port);
934 static void handle_ts(struct fio_client *client, struct fio_net_cmd *cmd)
936 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
937 struct json_object *tsobj;
939 tsobj = show_thread_status(&p->ts, &p->rs, NULL);
940 client->did_stat = 1;
942 json_object_add_client_info(tsobj, client);
943 json_array_add_value_object(clients_array, tsobj);
946 if (sum_stat_clients <= 1)
949 sum_thread_stats(&client_ts, &p->ts, sum_stat_nr == 1);
950 sum_group_stats(&client_gs, &p->rs);
953 client_ts.thread_number = p->ts.thread_number;
954 client_ts.groupid = p->ts.groupid;
955 client_ts.unified_rw_rep = p->ts.unified_rw_rep;
957 if (++sum_stat_nr == sum_stat_clients) {
958 strcpy(client_ts.name, "All clients");
959 tsobj = show_thread_status(&client_ts, &client_gs, NULL);
961 json_object_add_client_info(tsobj, client);
962 json_array_add_value_object(clients_array, tsobj);
967 static void handle_gs(struct fio_client *client, struct fio_net_cmd *cmd)
969 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
971 show_group_stats(gs, NULL);
974 static void handle_text(struct fio_client *client, struct fio_net_cmd *cmd)
976 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
977 const char *buf = (const char *) pdu->buf;
981 name = client->name ? client->name : client->hostname;
983 if (!client->skip_newline)
984 fprintf(f_out, "<%s> ", name);
985 ret = fwrite(buf, pdu->buf_len, 1, f_out);
987 client->skip_newline = strchr(buf, '\n') == NULL;
990 static void convert_agg(struct disk_util_agg *agg)
994 for (i = 0; i < 2; i++) {
995 agg->ios[i] = le64_to_cpu(agg->ios[i]);
996 agg->merges[i] = le64_to_cpu(agg->merges[i]);
997 agg->sectors[i] = le64_to_cpu(agg->sectors[i]);
998 agg->ticks[i] = le64_to_cpu(agg->ticks[i]);
1001 agg->io_ticks = le64_to_cpu(agg->io_ticks);
1002 agg->time_in_queue = le64_to_cpu(agg->time_in_queue);
1003 agg->slavecount = le32_to_cpu(agg->slavecount);
1004 agg->max_util.u.f = fio_uint64_to_double(le64_to_cpu(agg->max_util.u.i));
1007 static void convert_dus(struct disk_util_stat *dus)
1011 for (i = 0; i < 2; i++) {
1012 dus->s.ios[i] = le64_to_cpu(dus->s.ios[i]);
1013 dus->s.merges[i] = le64_to_cpu(dus->s.merges[i]);
1014 dus->s.sectors[i] = le64_to_cpu(dus->s.sectors[i]);
1015 dus->s.ticks[i] = le64_to_cpu(dus->s.ticks[i]);
1018 dus->s.io_ticks = le64_to_cpu(dus->s.io_ticks);
1019 dus->s.time_in_queue = le64_to_cpu(dus->s.time_in_queue);
1020 dus->s.msec = le64_to_cpu(dus->s.msec);
1023 static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd)
1025 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
1027 if (!client->disk_stats_shown) {
1028 client->disk_stats_shown = 1;
1029 log_info("\nDisk stats (read/write):\n");
1032 if (output_format & FIO_OUTPUT_JSON) {
1033 struct json_object *duobj;
1034 json_array_add_disk_util(&du->dus, &du->agg, du_array);
1035 duobj = json_array_last_value_object(du_array);
1036 json_object_add_client_info(duobj, client);
1038 if (output_format & FIO_OUTPUT_TERSE)
1039 print_disk_util(&du->dus, &du->agg, 1, NULL);
1040 if (output_format & FIO_OUTPUT_NORMAL)
1041 print_disk_util(&du->dus, &du->agg, 0, NULL);
1044 static void convert_jobs_eta(struct jobs_eta *je)
1048 je->nr_running = le32_to_cpu(je->nr_running);
1049 je->nr_ramp = le32_to_cpu(je->nr_ramp);
1050 je->nr_pending = le32_to_cpu(je->nr_pending);
1051 je->nr_setting_up = le32_to_cpu(je->nr_setting_up);
1052 je->files_open = le32_to_cpu(je->files_open);
1054 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1055 je->m_rate[i] = le32_to_cpu(je->m_rate[i]);
1056 je->t_rate[i] = le32_to_cpu(je->t_rate[i]);
1057 je->m_iops[i] = le32_to_cpu(je->m_iops[i]);
1058 je->t_iops[i] = le32_to_cpu(je->t_iops[i]);
1059 je->rate[i] = le32_to_cpu(je->rate[i]);
1060 je->iops[i] = le32_to_cpu(je->iops[i]);
1063 je->elapsed_sec = le64_to_cpu(je->elapsed_sec);
1064 je->eta_sec = le64_to_cpu(je->eta_sec);
1065 je->nr_threads = le32_to_cpu(je->nr_threads);
1066 je->is_pow2 = le32_to_cpu(je->is_pow2);
1067 je->unit_base = le32_to_cpu(je->unit_base);
1070 void fio_client_sum_jobs_eta(struct jobs_eta *dst, struct jobs_eta *je)
1074 dst->nr_running += je->nr_running;
1075 dst->nr_ramp += je->nr_ramp;
1076 dst->nr_pending += je->nr_pending;
1077 dst->nr_setting_up += je->nr_setting_up;
1078 dst->files_open += je->files_open;
1080 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1081 dst->m_rate[i] += je->m_rate[i];
1082 dst->t_rate[i] += je->t_rate[i];
1083 dst->m_iops[i] += je->m_iops[i];
1084 dst->t_iops[i] += je->t_iops[i];
1085 dst->rate[i] += je->rate[i];
1086 dst->iops[i] += je->iops[i];
1089 dst->elapsed_sec += je->elapsed_sec;
1091 if (je->eta_sec > dst->eta_sec)
1092 dst->eta_sec = je->eta_sec;
1094 dst->nr_threads += je->nr_threads;
1097 * This wont be correct for multiple strings, but at least it
1098 * works for the basic cases.
1100 strcpy((char *) dst->run_str, (char *) je->run_str);
1103 static void remove_reply_cmd(struct fio_client *client, struct fio_net_cmd *cmd)
1105 struct fio_net_cmd_reply *reply = NULL;
1106 struct flist_head *entry;
1108 flist_for_each(entry, &client->cmd_list) {
1109 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
1111 if (cmd->tag == (uintptr_t) reply)
1118 log_err("fio: client: unable to find matching tag (%llx)\n", (unsigned long long) cmd->tag);
1122 flist_del(&reply->list);
1123 cmd->tag = reply->saved_tag;
1127 int fio_client_wait_for_reply(struct fio_client *client, uint64_t tag)
1130 struct fio_net_cmd_reply *reply = NULL;
1131 struct flist_head *entry;
1133 flist_for_each(entry, &client->cmd_list) {
1134 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
1136 if (tag == (uintptr_t) reply)
1151 static void handle_eta(struct fio_client *client, struct fio_net_cmd *cmd)
1153 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
1154 struct client_eta *eta = (struct client_eta *) (uintptr_t) cmd->tag;
1156 dprint(FD_NET, "client: got eta tag %p, %d\n", eta, eta->pending);
1158 assert(client->eta_in_flight == eta);
1160 client->eta_in_flight = NULL;
1161 flist_del_init(&client->eta_list);
1162 client->eta_timeouts = 0;
1164 if (client->ops->jobs_eta)
1165 client->ops->jobs_eta(client, je);
1167 fio_client_sum_jobs_eta(&eta->eta, je);
1168 fio_client_dec_jobs_eta(eta, client->ops->eta);
1171 static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd)
1173 struct cmd_probe_reply_pdu *probe = (struct cmd_probe_reply_pdu *) cmd->payload;
1174 const char *os, *arch;
1177 os = fio_get_os_string(probe->os);
1181 arch = fio_get_arch_string(probe->arch);
1185 sprintf(bit, "%d-bit", probe->bpp * 8);
1186 probe->flags = le64_to_cpu(probe->flags);
1188 log_info("hostname=%s, be=%u, %s, os=%s, arch=%s, fio=%s, flags=%lx\n",
1189 probe->hostname, probe->bigendian, bit, os, arch,
1190 probe->fio_version, (unsigned long) probe->flags);
1193 client->name = strdup((char *) probe->hostname);
1196 static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd)
1198 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
1200 client->state = Client_started;
1201 client->jobs = le32_to_cpu(pdu->jobs);
1202 client->nr_stat = le32_to_cpu(pdu->stat_outputs);
1204 sum_stat_clients += client->nr_stat;
1207 static void handle_stop(struct fio_client *client, struct fio_net_cmd *cmd)
1210 log_info("client <%s>: exited with error %d\n", client->hostname, client->error);
1213 static void convert_stop(struct fio_net_cmd *cmd)
1215 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
1217 pdu->error = le32_to_cpu(pdu->error);
1220 static void convert_text(struct fio_net_cmd *cmd)
1222 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
1224 pdu->level = le32_to_cpu(pdu->level);
1225 pdu->buf_len = le32_to_cpu(pdu->buf_len);
1226 pdu->log_sec = le64_to_cpu(pdu->log_sec);
1227 pdu->log_usec = le64_to_cpu(pdu->log_usec);
1230 static struct cmd_iolog_pdu *convert_iolog_gz(struct fio_net_cmd *cmd,
1231 struct cmd_iolog_pdu *pdu)
1234 struct cmd_iolog_pdu *ret;
1236 uint32_t nr_samples;
1240 stream.zalloc = Z_NULL;
1241 stream.zfree = Z_NULL;
1242 stream.opaque = Z_NULL;
1243 stream.avail_in = 0;
1244 stream.next_in = Z_NULL;
1246 if (inflateInit(&stream) != Z_OK)
1250 * Get header first, it's not compressed
1252 nr_samples = le64_to_cpu(pdu->nr_samples);
1254 total = nr_samples * __log_entry_sz(le32_to_cpu(pdu->log_offset));
1255 ret = malloc(total + sizeof(*pdu));
1256 ret->nr_samples = nr_samples;
1258 memcpy(ret, pdu, sizeof(*pdu));
1260 p = (void *) ret + sizeof(*pdu);
1262 stream.avail_in = cmd->pdu_len - sizeof(*pdu);
1263 stream.next_in = (void *) pdu + sizeof(*pdu);
1264 while (stream.avail_in) {
1265 unsigned int this_chunk = 65536;
1266 unsigned int this_len;
1269 if (this_chunk > total)
1272 stream.avail_out = this_chunk;
1273 stream.next_out = p;
1274 err = inflate(&stream, Z_NO_FLUSH);
1275 /* may be Z_OK, or Z_STREAM_END */
1277 log_err("fio: inflate error %d\n", err);
1283 this_len = this_chunk - stream.avail_out;
1289 inflateEnd(&stream);
1297 * This has been compressed on the server side, since it can be big.
1300 static struct cmd_iolog_pdu *convert_iolog(struct fio_net_cmd *cmd)
1302 struct cmd_iolog_pdu *pdu = (struct cmd_iolog_pdu *) cmd->payload;
1303 struct cmd_iolog_pdu *ret;
1308 * Convert if compressed and we support it. If it's not
1309 * compressed, we need not do anything.
1311 if (le32_to_cpu(pdu->compressed)) {
1313 log_err("fio: server sent compressed data by mistake\n");
1316 ret = convert_iolog_gz(cmd, pdu);
1318 log_err("fio: failed decompressing log\n");
1324 ret->nr_samples = le64_to_cpu(ret->nr_samples);
1325 ret->thread_number = le32_to_cpu(ret->thread_number);
1326 ret->log_type = le32_to_cpu(ret->log_type);
1327 ret->compressed = le32_to_cpu(ret->compressed);
1328 ret->log_offset = le32_to_cpu(ret->log_offset);
1330 samples = &ret->samples[0];
1331 for (i = 0; i < ret->nr_samples; i++) {
1332 struct io_sample *s;
1334 s = __get_sample(samples, ret->log_offset, i);
1335 s->time = le64_to_cpu(s->time);
1336 s->val = le64_to_cpu(s->val);
1337 s->__ddir = le32_to_cpu(s->__ddir);
1338 s->bs = le32_to_cpu(s->bs);
1340 if (ret->log_offset) {
1341 struct io_sample_offset *so = (void *) s;
1343 so->offset = le64_to_cpu(so->offset);
1350 static void sendfile_reply(int fd, struct cmd_sendfile_reply *rep,
1351 size_t size, uint64_t tag)
1353 rep->error = cpu_to_le32(rep->error);
1354 fio_net_send_cmd(fd, FIO_NET_CMD_SENDFILE, rep, size, &tag, NULL);
1357 static int send_file(struct fio_client *client, struct cmd_sendfile *pdu,
1360 struct cmd_sendfile_reply *rep;
1365 size = sizeof(*rep);
1368 if (stat((char *)pdu->path, &sb) < 0) {
1371 sendfile_reply(client->fd, rep, size, tag);
1377 rep = realloc(rep, size);
1378 rep->size = cpu_to_le32((uint32_t) sb.st_size);
1380 fd = open((char *)pdu->path, O_RDONLY);
1384 rep->error = read_data(fd, &rep->data, sb.st_size);
1385 sendfile_reply(client->fd, rep, size, tag);
1391 int fio_handle_client(struct fio_client *client)
1393 struct client_ops *ops = client->ops;
1394 struct fio_net_cmd *cmd;
1396 dprint(FD_NET, "client: handle %s\n", client->hostname);
1398 cmd = fio_net_recv_cmd(client->fd);
1402 dprint(FD_NET, "client: got cmd op %s from %s (pdu=%u)\n",
1403 fio_server_op(cmd->opcode), client->hostname, cmd->pdu_len);
1405 switch (cmd->opcode) {
1406 case FIO_NET_CMD_QUIT:
1408 ops->quit(client, cmd);
1409 remove_client(client);
1411 case FIO_NET_CMD_TEXT:
1413 ops->text(client, cmd);
1415 case FIO_NET_CMD_DU: {
1416 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
1418 convert_dus(&du->dus);
1419 convert_agg(&du->agg);
1421 ops->disk_util(client, cmd);
1424 case FIO_NET_CMD_TS: {
1425 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
1427 convert_ts(&p->ts, &p->ts);
1428 convert_gs(&p->rs, &p->rs);
1430 ops->thread_status(client, cmd);
1433 case FIO_NET_CMD_GS: {
1434 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
1438 ops->group_stats(client, cmd);
1441 case FIO_NET_CMD_ETA: {
1442 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
1444 remove_reply_cmd(client, cmd);
1445 convert_jobs_eta(je);
1446 handle_eta(client, cmd);
1449 case FIO_NET_CMD_PROBE:
1450 remove_reply_cmd(client, cmd);
1451 ops->probe(client, cmd);
1453 case FIO_NET_CMD_SERVER_START:
1454 client->state = Client_running;
1456 ops->job_start(client, cmd);
1458 case FIO_NET_CMD_START: {
1459 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
1461 pdu->jobs = le32_to_cpu(pdu->jobs);
1462 ops->start(client, cmd);
1465 case FIO_NET_CMD_STOP: {
1466 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
1469 client->state = Client_stopped;
1470 client->error = le32_to_cpu(pdu->error);
1471 client->signal = le32_to_cpu(pdu->signal);
1472 ops->stop(client, cmd);
1475 case FIO_NET_CMD_ADD_JOB: {
1476 struct cmd_add_job_pdu *pdu = (struct cmd_add_job_pdu *) cmd->payload;
1478 client->thread_number = le32_to_cpu(pdu->thread_number);
1479 client->groupid = le32_to_cpu(pdu->groupid);
1482 ops->add_job(client, cmd);
1485 case FIO_NET_CMD_IOLOG:
1487 struct cmd_iolog_pdu *pdu;
1489 pdu = convert_iolog(cmd);
1490 ops->iolog(client, pdu);
1493 case FIO_NET_CMD_UPDATE_JOB:
1494 ops->update_job(client, cmd);
1495 remove_reply_cmd(client, cmd);
1497 case FIO_NET_CMD_VTRIGGER: {
1498 struct all_io_list *pdu = (struct all_io_list *) cmd->payload;
1503 strcpy(buf, aux_path);
1507 __verify_save_state(pdu, server_name(client, &buf[off], sizeof(buf) - off));
1508 exec_trigger(trigger_cmd);
1511 case FIO_NET_CMD_SENDFILE: {
1512 struct cmd_sendfile *pdu = (struct cmd_sendfile *) cmd->payload;
1513 send_file(client, pdu, cmd->tag);
1517 log_err("fio: unknown client op: %s\n", fio_server_op(cmd->opcode));
1525 int fio_clients_send_trigger(const char *cmd)
1527 struct flist_head *entry;
1528 struct fio_client *client;
1531 dprint(FD_NET, "client: send vtrigger: %s\n", cmd);
1538 flist_for_each(entry, &client_list) {
1539 struct cmd_vtrigger_pdu *pdu;
1541 client = flist_entry(entry, struct fio_client, list);
1543 pdu = malloc(sizeof(*pdu) + slen);
1544 pdu->len = cpu_to_le16((uint16_t) slen);
1546 memcpy(pdu->cmd, cmd, slen);
1547 fio_net_send_cmd(client->fd, FIO_NET_CMD_VTRIGGER, pdu,
1548 sizeof(*pdu) + slen, NULL, NULL);
1555 static void request_client_etas(struct client_ops *ops)
1557 struct fio_client *client;
1558 struct flist_head *entry;
1559 struct client_eta *eta;
1562 dprint(FD_NET, "client: request eta (%d)\n", nr_clients);
1564 eta = calloc(1, sizeof(*eta) + __THREAD_RUNSTR_SZ(REAL_MAX_JOBS));
1565 eta->pending = nr_clients;
1567 flist_for_each(entry, &client_list) {
1568 client = flist_entry(entry, struct fio_client, list);
1570 if (!flist_empty(&client->eta_list)) {
1574 if (client->state != Client_running)
1577 assert(!client->eta_in_flight);
1578 flist_add_tail(&client->eta_list, &eta_list);
1579 client->eta_in_flight = eta;
1580 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_SEND_ETA,
1581 (uintptr_t) eta, &client->cmd_list);
1585 if (!fio_client_dec_jobs_eta(eta, ops->eta))
1589 dprint(FD_NET, "client: requested eta tag %p\n", eta);
1593 * A single SEND_ETA timeout isn't fatal. Attempt to recover.
1595 static int handle_cmd_timeout(struct fio_client *client,
1596 struct fio_net_cmd_reply *reply)
1598 if (reply->opcode != FIO_NET_CMD_SEND_ETA)
1601 log_info("client <%s>: timeout on SEND_ETA\n", client->hostname);
1602 flist_del(&reply->list);
1605 flist_del_init(&client->eta_list);
1606 if (client->eta_in_flight) {
1607 fio_client_dec_jobs_eta(client->eta_in_flight, client->ops->eta);
1608 client->eta_in_flight = NULL;
1612 * If we fail 5 in a row, give up...
1614 if (client->eta_timeouts++ > 5)
1620 static int client_check_cmd_timeout(struct fio_client *client,
1621 struct timeval *now)
1623 struct fio_net_cmd_reply *reply;
1624 struct flist_head *entry, *tmp;
1627 flist_for_each_safe(entry, tmp, &client->cmd_list) {
1628 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
1630 if (mtime_since(&reply->tv, now) < FIO_NET_CLIENT_TIMEOUT)
1633 if (!handle_cmd_timeout(client, reply))
1636 log_err("fio: client %s, timeout on cmd %s\n", client->hostname,
1637 fio_server_op(reply->opcode));
1638 flist_del(&reply->list);
1643 return flist_empty(&client->cmd_list) && ret;
1646 static int fio_check_clients_timed_out(void)
1648 struct fio_client *client;
1649 struct flist_head *entry, *tmp;
1653 fio_gettime(&tv, NULL);
1655 flist_for_each_safe(entry, tmp, &client_list) {
1656 client = flist_entry(entry, struct fio_client, list);
1658 if (flist_empty(&client->cmd_list))
1661 if (!client_check_cmd_timeout(client, &tv))
1664 if (client->ops->timed_out)
1665 client->ops->timed_out(client);
1667 log_err("fio: client %s timed out\n", client->hostname);
1669 client->error = ETIMEDOUT;
1670 remove_client(client);
1677 int fio_handle_clients(struct client_ops *ops)
1679 struct pollfd *pfds;
1680 int i, ret = 0, retval = 0;
1682 fio_gettime(&eta_tv, NULL);
1684 pfds = malloc(nr_clients * sizeof(struct pollfd));
1686 init_thread_stat(&client_ts);
1687 init_group_run_stat(&client_gs);
1689 while (!exit_backend && nr_clients) {
1690 struct flist_head *entry, *tmp;
1691 struct fio_client *client;
1694 flist_for_each_safe(entry, tmp, &client_list) {
1695 client = flist_entry(entry, struct fio_client, list);
1697 if (!client->sent_job && !client->ops->stay_connected &&
1698 flist_empty(&client->cmd_list)) {
1699 remove_client(client);
1703 pfds[i].fd = client->fd;
1704 pfds[i].events = POLLIN;
1711 assert(i == nr_clients);
1717 fio_gettime(&tv, NULL);
1718 if (mtime_since(&eta_tv, &tv) >= 900) {
1719 request_client_etas(ops);
1720 memcpy(&eta_tv, &tv, sizeof(tv));
1722 if (fio_check_clients_timed_out())
1726 check_trigger_file();
1728 timeout = min(100u, ops->eta_msec);
1730 ret = poll(pfds, nr_clients, timeout);
1734 log_err("fio: poll clients: %s\n", strerror(errno));
1740 for (i = 0; i < nr_clients; i++) {
1741 if (!(pfds[i].revents & POLLIN))
1744 client = find_client_by_fd(pfds[i].fd);
1746 log_err("fio: unknown client fd %ld\n", (long) pfds[i].fd);
1749 if (!fio_handle_client(client)) {
1750 log_info("client: host=%s disconnected\n",
1752 remove_client(client);
1754 } else if (client->error)
1756 fio_put_client(client);
1760 fio_client_json_fini();
1763 return retval || error_clients;