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))
123 root = json_create_object();
124 json_object_add_value_string(root, "fio version", fio_version_string);
125 clients_array = json_create_array();
126 json_object_add_value_array(root, "client_stats", clients_array);
127 du_array = json_create_array();
128 json_object_add_value_array(root, "disk_util", du_array);
131 static void fio_client_json_fini(void)
133 if (!(output_format & FIO_OUTPUT_JSON))
136 json_print_object(root, NULL);
138 json_free_object(root);
140 clients_array = NULL;
144 static struct fio_client *find_client_by_fd(int fd)
146 int bucket = hash_long(fd, FIO_CLIENT_HASH_BITS) & FIO_CLIENT_HASH_MASK;
147 struct fio_client *client;
148 struct flist_head *entry;
150 flist_for_each(entry, &client_hash[bucket]) {
151 client = flist_entry(entry, struct fio_client, hash_list);
153 if (client->fd == fd) {
162 void fio_put_client(struct fio_client *client)
167 free(client->hostname);
172 while (client->nr_files) {
173 struct client_file *cf = &client->files[--client->nr_files];
180 if (!client->did_stat)
189 static int fio_client_dec_jobs_eta(struct client_eta *eta, client_eta_op eta_fn)
191 if (!--eta->pending) {
200 static void remove_client(struct fio_client *client)
202 assert(client->refs);
204 dprint(FD_NET, "client: removed <%s>\n", client->hostname);
206 if (!flist_empty(&client->list))
207 flist_del_init(&client->list);
209 fio_client_remove_hash(client);
211 if (!flist_empty(&client->eta_list)) {
212 flist_del_init(&client->eta_list);
213 fio_client_dec_jobs_eta(client->eta_in_flight, client->ops->eta);
219 if (client->ops->removed)
220 client->ops->removed(client);
223 fio_put_client(client);
226 struct fio_client *fio_get_client(struct fio_client *client)
232 static void __fio_client_add_cmd_option(struct fio_client *client,
237 index = client->argc++;
238 client->argv = realloc(client->argv, sizeof(char *) * client->argc);
239 client->argv[index] = strdup(opt);
240 dprint(FD_NET, "client: add cmd %d: %s\n", index, opt);
243 void fio_client_add_cmd_option(void *cookie, const char *opt)
245 struct fio_client *client = cookie;
246 struct flist_head *entry;
251 __fio_client_add_cmd_option(client, opt);
254 * Duplicate arguments to shared client group
256 flist_for_each(entry, &arg_list) {
257 client = flist_entry(entry, struct fio_client, arg_list);
259 __fio_client_add_cmd_option(client, opt);
263 struct fio_client *fio_client_add_explicit(struct client_ops *ops,
264 const char *hostname, int type,
267 struct fio_client *client;
269 client = malloc(sizeof(*client));
270 memset(client, 0, sizeof(*client));
272 INIT_FLIST_HEAD(&client->list);
273 INIT_FLIST_HEAD(&client->hash_list);
274 INIT_FLIST_HEAD(&client->arg_list);
275 INIT_FLIST_HEAD(&client->eta_list);
276 INIT_FLIST_HEAD(&client->cmd_list);
278 client->hostname = strdup(hostname);
280 if (type == Fio_client_socket)
285 ipv6 = type == Fio_client_ipv6;
286 if (fio_server_parse_host(hostname, ipv6,
287 &client->addr.sin_addr,
288 &client->addr6.sin6_addr))
297 client->type = ops->client_type;
299 __fio_client_add_cmd_option(client, "fio");
301 flist_add(&client->list, &client_list);
303 dprint(FD_NET, "client: added <%s>\n", client->hostname);
310 int fio_client_add_ini_file(void *cookie, const char *ini_file, int remote)
312 struct fio_client *client = cookie;
313 struct client_file *cf;
320 dprint(FD_NET, "client <%s>: add ini %s\n", client->hostname, ini_file);
322 new_size = (client->nr_files + 1) * sizeof(struct client_file);
323 new_files = realloc(client->files, new_size);
327 client->files = new_files;
328 cf = &client->files[client->nr_files];
329 cf->file = strdup(ini_file);
335 int fio_client_add(struct client_ops *ops, const char *hostname, void **cookie)
337 struct fio_client *existing = *cookie;
338 struct fio_client *client;
342 * We always add our "exec" name as the option, hence 1
345 if (existing->argc == 1)
346 flist_add_tail(&existing->arg_list, &arg_list);
348 while (!flist_empty(&arg_list))
349 flist_del_init(arg_list.next);
353 client = malloc(sizeof(*client));
354 memset(client, 0, sizeof(*client));
356 INIT_FLIST_HEAD(&client->list);
357 INIT_FLIST_HEAD(&client->hash_list);
358 INIT_FLIST_HEAD(&client->arg_list);
359 INIT_FLIST_HEAD(&client->eta_list);
360 INIT_FLIST_HEAD(&client->cmd_list);
362 if (fio_server_parse_string(hostname, &client->hostname,
363 &client->is_sock, &client->port,
364 &client->addr.sin_addr,
365 &client->addr6.sin6_addr,
372 client->type = ops->client_type;
374 __fio_client_add_cmd_option(client, "fio");
376 flist_add(&client->list, &client_list);
378 dprint(FD_NET, "client: added <%s>\n", client->hostname);
383 static const char *server_name(struct fio_client *client, char *buf,
389 from = inet_ntop(AF_INET6, (struct sockaddr *) &client->addr6.sin6_addr, buf, bufsize);
390 else if (client->is_sock)
393 from = inet_ntop(AF_INET, (struct sockaddr *) &client->addr.sin_addr, buf, bufsize);
398 static void probe_client(struct fio_client *client)
400 struct cmd_client_probe_pdu pdu;
405 dprint(FD_NET, "client: send probe\n");
408 pdu.flags = __le64_to_cpu(FIO_PROBE_FLAG_ZLIB);
413 sname = server_name(client, buf, sizeof(buf));
414 memset(pdu.server, 0, sizeof(pdu.server));
415 strncpy((char *) pdu.server, sname, sizeof(pdu.server) - 1);
417 fio_net_send_cmd(client->fd, FIO_NET_CMD_PROBE, &pdu, sizeof(pdu), &tag, &client->cmd_list);
420 static int fio_client_connect_ip(struct fio_client *client)
422 struct sockaddr *addr;
427 client->addr6.sin6_family = AF_INET6;
428 client->addr6.sin6_port = htons(client->port);
430 addr = (struct sockaddr *) &client->addr6;
431 socklen = sizeof(client->addr6);
433 client->addr.sin_family = AF_INET;
434 client->addr.sin_port = htons(client->port);
436 addr = (struct sockaddr *) &client->addr;
437 socklen = sizeof(client->addr);
440 fd = socket(domain, SOCK_STREAM, 0);
444 log_err("fio: socket: %s\n", strerror(errno));
448 if (connect(fd, addr, socklen) < 0) {
451 log_err("fio: connect: %s\n", strerror(errno));
452 log_err("fio: failed to connect to %s:%u\n", client->hostname,
461 static int fio_client_connect_sock(struct fio_client *client)
463 struct sockaddr_un *addr = &client->addr_un;
467 memset(addr, 0, sizeof(*addr));
468 addr->sun_family = AF_UNIX;
469 strncpy(addr->sun_path, client->hostname, sizeof(addr->sun_path) - 1);
471 fd = socket(AF_UNIX, SOCK_STREAM, 0);
475 log_err("fio: socket: %s\n", strerror(errno));
479 len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
480 if (connect(fd, (struct sockaddr *) addr, len) < 0) {
483 log_err("fio: connect; %s\n", strerror(errno));
491 int fio_client_connect(struct fio_client *client)
495 dprint(FD_NET, "client: connect to host %s\n", client->hostname);
498 fd = fio_client_connect_sock(client);
500 fd = fio_client_connect_ip(client);
502 dprint(FD_NET, "client: %s connected %d\n", client->hostname, fd);
508 fio_client_add_hash(client);
509 client->state = Client_connected;
511 probe_client(client);
515 int fio_client_terminate(struct fio_client *client)
517 return fio_net_send_quit(client->fd);
520 void fio_clients_terminate(void)
522 struct flist_head *entry;
523 struct fio_client *client;
525 dprint(FD_NET, "client: terminate clients\n");
527 flist_for_each(entry, &client_list) {
528 client = flist_entry(entry, struct fio_client, list);
529 fio_client_terminate(client);
533 static void sig_int(int sig)
535 dprint(FD_NET, "client: got signal %d\n", sig);
536 fio_clients_terminate();
539 static void client_signal_handler(void)
541 struct sigaction act;
543 memset(&act, 0, sizeof(act));
544 act.sa_handler = sig_int;
545 act.sa_flags = SA_RESTART;
546 sigaction(SIGINT, &act, NULL);
548 memset(&act, 0, sizeof(act));
549 act.sa_handler = sig_int;
550 act.sa_flags = SA_RESTART;
551 sigaction(SIGTERM, &act, NULL);
553 /* Windows uses SIGBREAK as a quit signal from other applications */
555 memset(&act, 0, sizeof(act));
556 act.sa_handler = sig_int;
557 act.sa_flags = SA_RESTART;
558 sigaction(SIGBREAK, &act, NULL);
561 memset(&act, 0, sizeof(act));
562 act.sa_handler = sig_show_status;
563 act.sa_flags = SA_RESTART;
564 sigaction(SIGUSR1, &act, NULL);
567 static int send_client_cmd_line(struct fio_client *client)
569 struct cmd_single_line_pdu *cslp;
570 struct cmd_line_pdu *clp;
571 unsigned long offset;
577 dprint(FD_NET, "client: send cmdline %d\n", client->argc);
579 lens = malloc(client->argc * sizeof(unsigned int));
582 * Find out how much mem we need
584 for (i = 0, mem = 0; i < client->argc; i++) {
585 lens[i] = strlen(client->argv[i]) + 1;
590 * We need one cmd_line_pdu, and argc number of cmd_single_line_pdu
592 mem += sizeof(*clp) + (client->argc * sizeof(*cslp));
596 offset = sizeof(*clp);
598 for (i = 0; i < client->argc; i++) {
599 uint16_t arg_len = lens[i];
602 strcpy((char *) cslp->text, client->argv[i]);
603 cslp->len = cpu_to_le16(arg_len);
604 offset += sizeof(*cslp) + arg_len;
608 clp->lines = cpu_to_le16(client->argc);
609 clp->client_type = __cpu_to_le16(client->type);
610 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOBLINE, pdu, mem, NULL, NULL);
615 int fio_clients_connect(void)
617 struct fio_client *client;
618 struct flist_head *entry, *tmp;
623 WSAStartup(MAKEWORD(2, 2), &wsd);
626 dprint(FD_NET, "client: connect all\n");
628 client_signal_handler();
630 flist_for_each_safe(entry, tmp, &client_list) {
631 client = flist_entry(entry, struct fio_client, list);
633 ret = fio_client_connect(client);
635 remove_client(client);
639 if (client->argc > 1)
640 send_client_cmd_line(client);
646 int fio_start_client(struct fio_client *client)
648 dprint(FD_NET, "client: start %s\n", client->hostname);
649 return fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_RUN, 0, NULL);
652 int fio_start_all_clients(void)
654 struct fio_client *client;
655 struct flist_head *entry, *tmp;
658 dprint(FD_NET, "client: start all\n");
660 fio_client_json_init();
662 flist_for_each_safe(entry, tmp, &client_list) {
663 client = flist_entry(entry, struct fio_client, list);
665 ret = fio_start_client(client);
667 remove_client(client);
672 return flist_empty(&client_list);
675 static int __fio_client_send_remote_ini(struct fio_client *client,
676 const char *filename)
678 struct cmd_load_file_pdu *pdu;
682 dprint(FD_NET, "send remote ini %s to %s\n", filename, client->hostname);
684 p_size = sizeof(*pdu) + strlen(filename) + 1;
685 pdu = malloc(p_size);
686 memset(pdu, 0, p_size);
687 pdu->name_len = strlen(filename);
688 strcpy((char *) pdu->file, filename);
689 pdu->client_type = cpu_to_le16((uint16_t) client->type);
691 client->sent_job = 1;
692 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_LOAD_FILE, pdu, p_size,NULL, NULL);
698 * Send file contents to server backend. We could use sendfile(), but to remain
699 * more portable lets just read/write the darn thing.
701 static int __fio_client_send_local_ini(struct fio_client *client,
702 const char *filename)
704 struct cmd_job_pdu *pdu;
712 dprint(FD_NET, "send ini %s to %s\n", filename, client->hostname);
714 fd = open(filename, O_RDONLY);
717 log_err("fio: job file <%s> open: %s\n", filename, strerror(errno));
721 if (fstat(fd, &sb) < 0) {
723 log_err("fio: job file stat: %s\n", strerror(errno));
728 p_size = sb.st_size + sizeof(*pdu);
729 pdu = malloc(p_size);
734 if (read_data(fd, p, len)) {
735 log_err("fio: failed reading job file %s\n", filename);
741 pdu->buf_len = __cpu_to_le32(sb.st_size);
742 pdu->client_type = cpu_to_le32(client->type);
744 client->sent_job = 1;
745 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, pdu, p_size, NULL, NULL);
751 int fio_client_send_ini(struct fio_client *client, const char *filename,
757 ret = __fio_client_send_local_ini(client, filename);
759 ret = __fio_client_send_remote_ini(client, filename);
762 client->sent_job = 1;
767 static int fio_client_send_cf(struct fio_client *client,
768 struct client_file *cf)
770 return fio_client_send_ini(client, cf->file, cf->remote);
773 int fio_clients_send_ini(const char *filename)
775 struct fio_client *client;
776 struct flist_head *entry, *tmp;
778 flist_for_each_safe(entry, tmp, &client_list) {
779 client = flist_entry(entry, struct fio_client, list);
781 if (client->nr_files) {
784 for (i = 0; i < client->nr_files; i++) {
785 struct client_file *cf;
787 cf = &client->files[i];
789 if (fio_client_send_cf(client, cf)) {
790 remove_client(client);
795 if (client->sent_job)
797 if (!filename || fio_client_send_ini(client, filename, 0))
798 remove_client(client);
804 int fio_client_update_options(struct fio_client *client,
805 struct thread_options *o, uint64_t *tag)
807 struct cmd_add_job_pdu pdu;
809 pdu.thread_number = cpu_to_le32(client->thread_number);
810 pdu.groupid = cpu_to_le32(client->groupid);
811 convert_thread_options_to_net(&pdu.top, o);
813 return fio_net_send_cmd(client->fd, FIO_NET_CMD_UPDATE_JOB, &pdu, sizeof(pdu), tag, &client->cmd_list);
816 static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
818 dst->max_val = le64_to_cpu(src->max_val);
819 dst->min_val = le64_to_cpu(src->min_val);
820 dst->samples = le64_to_cpu(src->samples);
823 * Floats arrive as IEEE 754 encoded uint64_t, convert back to double
825 dst->mean.u.f = fio_uint64_to_double(le64_to_cpu(dst->mean.u.i));
826 dst->S.u.f = fio_uint64_to_double(le64_to_cpu(dst->S.u.i));
829 static void convert_ts(struct thread_stat *dst, struct thread_stat *src)
833 dst->error = le32_to_cpu(src->error);
834 dst->thread_number = le32_to_cpu(src->thread_number);
835 dst->groupid = le32_to_cpu(src->groupid);
836 dst->pid = le32_to_cpu(src->pid);
837 dst->members = le32_to_cpu(src->members);
838 dst->unified_rw_rep = le32_to_cpu(src->unified_rw_rep);
840 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
841 convert_io_stat(&dst->clat_stat[i], &src->clat_stat[i]);
842 convert_io_stat(&dst->slat_stat[i], &src->slat_stat[i]);
843 convert_io_stat(&dst->lat_stat[i], &src->lat_stat[i]);
844 convert_io_stat(&dst->bw_stat[i], &src->bw_stat[i]);
847 dst->usr_time = le64_to_cpu(src->usr_time);
848 dst->sys_time = le64_to_cpu(src->sys_time);
849 dst->ctx = le64_to_cpu(src->ctx);
850 dst->minf = le64_to_cpu(src->minf);
851 dst->majf = le64_to_cpu(src->majf);
852 dst->clat_percentiles = le64_to_cpu(src->clat_percentiles);
853 dst->percentile_precision = le64_to_cpu(src->percentile_precision);
855 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
856 fio_fp64_t *fps = &src->percentile_list[i];
857 fio_fp64_t *fpd = &dst->percentile_list[i];
859 fpd->u.f = fio_uint64_to_double(le64_to_cpu(fps->u.i));
862 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
863 dst->io_u_map[i] = le32_to_cpu(src->io_u_map[i]);
864 dst->io_u_submit[i] = le32_to_cpu(src->io_u_submit[i]);
865 dst->io_u_complete[i] = le32_to_cpu(src->io_u_complete[i]);
868 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
869 dst->io_u_lat_u[i] = le32_to_cpu(src->io_u_lat_u[i]);
870 dst->io_u_lat_m[i] = le32_to_cpu(src->io_u_lat_m[i]);
873 for (i = 0; i < DDIR_RWDIR_CNT; i++)
874 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
875 dst->io_u_plat[i][j] = le32_to_cpu(src->io_u_plat[i][j]);
877 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
878 dst->total_io_u[i] = le64_to_cpu(src->total_io_u[i]);
879 dst->short_io_u[i] = le64_to_cpu(src->short_io_u[i]);
880 dst->drop_io_u[i] = le64_to_cpu(src->drop_io_u[i]);
883 dst->total_submit = le64_to_cpu(src->total_submit);
884 dst->total_complete = le64_to_cpu(src->total_complete);
886 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
887 dst->io_bytes[i] = le64_to_cpu(src->io_bytes[i]);
888 dst->runtime[i] = le64_to_cpu(src->runtime[i]);
891 dst->total_run_time = le64_to_cpu(src->total_run_time);
892 dst->continue_on_error = le16_to_cpu(src->continue_on_error);
893 dst->total_err_count = le64_to_cpu(src->total_err_count);
894 dst->first_error = le32_to_cpu(src->first_error);
895 dst->kb_base = le32_to_cpu(src->kb_base);
896 dst->unit_base = le32_to_cpu(src->unit_base);
898 dst->latency_depth = le32_to_cpu(src->latency_depth);
899 dst->latency_target = le64_to_cpu(src->latency_target);
900 dst->latency_window = le64_to_cpu(src->latency_window);
901 dst->latency_percentile.u.f = fio_uint64_to_double(le64_to_cpu(src->latency_percentile.u.i));
903 dst->nr_block_infos = le64_to_cpu(src->nr_block_infos);
904 for (i = 0; i < dst->nr_block_infos; i++)
905 dst->block_infos[i] = le32_to_cpu(src->block_infos[i]);
908 static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
912 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
913 dst->max_run[i] = le64_to_cpu(src->max_run[i]);
914 dst->min_run[i] = le64_to_cpu(src->min_run[i]);
915 dst->max_bw[i] = le64_to_cpu(src->max_bw[i]);
916 dst->min_bw[i] = le64_to_cpu(src->min_bw[i]);
917 dst->io_kb[i] = le64_to_cpu(src->io_kb[i]);
918 dst->agg[i] = le64_to_cpu(src->agg[i]);
921 dst->kb_base = le32_to_cpu(src->kb_base);
922 dst->unit_base = le32_to_cpu(src->unit_base);
923 dst->groupid = le32_to_cpu(src->groupid);
924 dst->unified_rw_rep = le32_to_cpu(src->unified_rw_rep);
927 static void json_object_add_client_info(struct json_object *obj,
928 struct fio_client *client)
930 const char *hostname = client->hostname ? client->hostname : "";
932 json_object_add_value_string(obj, "hostname", hostname);
933 json_object_add_value_int(obj, "port", client->port);
936 static void handle_ts(struct fio_client *client, struct fio_net_cmd *cmd)
938 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
939 struct json_object *tsobj;
941 tsobj = show_thread_status(&p->ts, &p->rs, NULL);
942 client->did_stat = 1;
944 json_object_add_client_info(tsobj, client);
945 json_array_add_value_object(clients_array, tsobj);
948 if (sum_stat_clients <= 1)
951 sum_thread_stats(&client_ts, &p->ts, sum_stat_nr == 1);
952 sum_group_stats(&client_gs, &p->rs);
955 client_ts.thread_number = p->ts.thread_number;
956 client_ts.groupid = p->ts.groupid;
957 client_ts.unified_rw_rep = p->ts.unified_rw_rep;
959 if (++sum_stat_nr == sum_stat_clients) {
960 strcpy(client_ts.name, "All clients");
961 tsobj = show_thread_status(&client_ts, &client_gs, NULL);
963 json_object_add_client_info(tsobj, client);
964 json_array_add_value_object(clients_array, tsobj);
969 static void handle_gs(struct fio_client *client, struct fio_net_cmd *cmd)
971 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
973 if (output_format & FIO_OUTPUT_NORMAL)
974 show_group_stats(gs, NULL);
977 static void handle_text(struct fio_client *client, struct fio_net_cmd *cmd)
979 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
980 const char *buf = (const char *) pdu->buf;
984 name = client->name ? client->name : client->hostname;
986 if (!client->skip_newline)
987 fprintf(f_out, "<%s> ", name);
988 ret = fwrite(buf, pdu->buf_len, 1, f_out);
990 client->skip_newline = strchr(buf, '\n') == NULL;
993 static void convert_agg(struct disk_util_agg *agg)
997 for (i = 0; i < 2; i++) {
998 agg->ios[i] = le64_to_cpu(agg->ios[i]);
999 agg->merges[i] = le64_to_cpu(agg->merges[i]);
1000 agg->sectors[i] = le64_to_cpu(agg->sectors[i]);
1001 agg->ticks[i] = le64_to_cpu(agg->ticks[i]);
1004 agg->io_ticks = le64_to_cpu(agg->io_ticks);
1005 agg->time_in_queue = le64_to_cpu(agg->time_in_queue);
1006 agg->slavecount = le32_to_cpu(agg->slavecount);
1007 agg->max_util.u.f = fio_uint64_to_double(le64_to_cpu(agg->max_util.u.i));
1010 static void convert_dus(struct disk_util_stat *dus)
1014 for (i = 0; i < 2; i++) {
1015 dus->s.ios[i] = le64_to_cpu(dus->s.ios[i]);
1016 dus->s.merges[i] = le64_to_cpu(dus->s.merges[i]);
1017 dus->s.sectors[i] = le64_to_cpu(dus->s.sectors[i]);
1018 dus->s.ticks[i] = le64_to_cpu(dus->s.ticks[i]);
1021 dus->s.io_ticks = le64_to_cpu(dus->s.io_ticks);
1022 dus->s.time_in_queue = le64_to_cpu(dus->s.time_in_queue);
1023 dus->s.msec = le64_to_cpu(dus->s.msec);
1026 static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd)
1028 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
1030 if (!client->disk_stats_shown) {
1031 client->disk_stats_shown = 1;
1032 log_info("\nDisk stats (read/write):\n");
1035 if (output_format & FIO_OUTPUT_JSON) {
1036 struct json_object *duobj;
1037 json_array_add_disk_util(&du->dus, &du->agg, du_array);
1038 duobj = json_array_last_value_object(du_array);
1039 json_object_add_client_info(duobj, client);
1041 if (output_format & FIO_OUTPUT_TERSE)
1042 print_disk_util(&du->dus, &du->agg, 1, NULL);
1043 if (output_format & FIO_OUTPUT_NORMAL)
1044 print_disk_util(&du->dus, &du->agg, 0, NULL);
1047 static void convert_jobs_eta(struct jobs_eta *je)
1051 je->nr_running = le32_to_cpu(je->nr_running);
1052 je->nr_ramp = le32_to_cpu(je->nr_ramp);
1053 je->nr_pending = le32_to_cpu(je->nr_pending);
1054 je->nr_setting_up = le32_to_cpu(je->nr_setting_up);
1055 je->files_open = le32_to_cpu(je->files_open);
1057 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1058 je->m_rate[i] = le32_to_cpu(je->m_rate[i]);
1059 je->t_rate[i] = le32_to_cpu(je->t_rate[i]);
1060 je->m_iops[i] = le32_to_cpu(je->m_iops[i]);
1061 je->t_iops[i] = le32_to_cpu(je->t_iops[i]);
1062 je->rate[i] = le32_to_cpu(je->rate[i]);
1063 je->iops[i] = le32_to_cpu(je->iops[i]);
1066 je->elapsed_sec = le64_to_cpu(je->elapsed_sec);
1067 je->eta_sec = le64_to_cpu(je->eta_sec);
1068 je->nr_threads = le32_to_cpu(je->nr_threads);
1069 je->is_pow2 = le32_to_cpu(je->is_pow2);
1070 je->unit_base = le32_to_cpu(je->unit_base);
1073 void fio_client_sum_jobs_eta(struct jobs_eta *dst, struct jobs_eta *je)
1077 dst->nr_running += je->nr_running;
1078 dst->nr_ramp += je->nr_ramp;
1079 dst->nr_pending += je->nr_pending;
1080 dst->nr_setting_up += je->nr_setting_up;
1081 dst->files_open += je->files_open;
1083 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1084 dst->m_rate[i] += je->m_rate[i];
1085 dst->t_rate[i] += je->t_rate[i];
1086 dst->m_iops[i] += je->m_iops[i];
1087 dst->t_iops[i] += je->t_iops[i];
1088 dst->rate[i] += je->rate[i];
1089 dst->iops[i] += je->iops[i];
1092 dst->elapsed_sec += je->elapsed_sec;
1094 if (je->eta_sec > dst->eta_sec)
1095 dst->eta_sec = je->eta_sec;
1097 dst->nr_threads += je->nr_threads;
1100 * This wont be correct for multiple strings, but at least it
1101 * works for the basic cases.
1103 strcpy((char *) dst->run_str, (char *) je->run_str);
1106 static void remove_reply_cmd(struct fio_client *client, struct fio_net_cmd *cmd)
1108 struct fio_net_cmd_reply *reply = NULL;
1109 struct flist_head *entry;
1111 flist_for_each(entry, &client->cmd_list) {
1112 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
1114 if (cmd->tag == (uintptr_t) reply)
1121 log_err("fio: client: unable to find matching tag (%llx)\n", (unsigned long long) cmd->tag);
1125 flist_del(&reply->list);
1126 cmd->tag = reply->saved_tag;
1130 int fio_client_wait_for_reply(struct fio_client *client, uint64_t tag)
1133 struct fio_net_cmd_reply *reply = NULL;
1134 struct flist_head *entry;
1136 flist_for_each(entry, &client->cmd_list) {
1137 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
1139 if (tag == (uintptr_t) reply)
1154 static void handle_eta(struct fio_client *client, struct fio_net_cmd *cmd)
1156 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
1157 struct client_eta *eta = (struct client_eta *) (uintptr_t) cmd->tag;
1159 dprint(FD_NET, "client: got eta tag %p, %d\n", eta, eta->pending);
1161 assert(client->eta_in_flight == eta);
1163 client->eta_in_flight = NULL;
1164 flist_del_init(&client->eta_list);
1165 client->eta_timeouts = 0;
1167 if (client->ops->jobs_eta)
1168 client->ops->jobs_eta(client, je);
1170 fio_client_sum_jobs_eta(&eta->eta, je);
1171 fio_client_dec_jobs_eta(eta, client->ops->eta);
1174 static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd)
1176 struct cmd_probe_reply_pdu *probe = (struct cmd_probe_reply_pdu *) cmd->payload;
1177 const char *os, *arch;
1180 os = fio_get_os_string(probe->os);
1184 arch = fio_get_arch_string(probe->arch);
1188 sprintf(bit, "%d-bit", probe->bpp * 8);
1189 probe->flags = le64_to_cpu(probe->flags);
1191 log_info("hostname=%s, be=%u, %s, os=%s, arch=%s, fio=%s, flags=%lx\n",
1192 probe->hostname, probe->bigendian, bit, os, arch,
1193 probe->fio_version, (unsigned long) probe->flags);
1196 client->name = strdup((char *) probe->hostname);
1199 static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd)
1201 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
1203 client->state = Client_started;
1204 client->jobs = le32_to_cpu(pdu->jobs);
1205 client->nr_stat = le32_to_cpu(pdu->stat_outputs);
1207 sum_stat_clients += client->nr_stat;
1210 static void handle_stop(struct fio_client *client, struct fio_net_cmd *cmd)
1213 log_info("client <%s>: exited with error %d\n", client->hostname, client->error);
1216 static void convert_stop(struct fio_net_cmd *cmd)
1218 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
1220 pdu->error = le32_to_cpu(pdu->error);
1223 static void convert_text(struct fio_net_cmd *cmd)
1225 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
1227 pdu->level = le32_to_cpu(pdu->level);
1228 pdu->buf_len = le32_to_cpu(pdu->buf_len);
1229 pdu->log_sec = le64_to_cpu(pdu->log_sec);
1230 pdu->log_usec = le64_to_cpu(pdu->log_usec);
1233 static struct cmd_iolog_pdu *convert_iolog_gz(struct fio_net_cmd *cmd,
1234 struct cmd_iolog_pdu *pdu)
1237 struct cmd_iolog_pdu *ret;
1239 uint32_t nr_samples;
1243 stream.zalloc = Z_NULL;
1244 stream.zfree = Z_NULL;
1245 stream.opaque = Z_NULL;
1246 stream.avail_in = 0;
1247 stream.next_in = Z_NULL;
1249 if (inflateInit(&stream) != Z_OK)
1253 * Get header first, it's not compressed
1255 nr_samples = le64_to_cpu(pdu->nr_samples);
1257 total = nr_samples * __log_entry_sz(le32_to_cpu(pdu->log_offset));
1258 ret = malloc(total + sizeof(*pdu));
1259 ret->nr_samples = nr_samples;
1261 memcpy(ret, pdu, sizeof(*pdu));
1263 p = (void *) ret + sizeof(*pdu);
1265 stream.avail_in = cmd->pdu_len - sizeof(*pdu);
1266 stream.next_in = (void *) pdu + sizeof(*pdu);
1267 while (stream.avail_in) {
1268 unsigned int this_chunk = 65536;
1269 unsigned int this_len;
1272 if (this_chunk > total)
1275 stream.avail_out = this_chunk;
1276 stream.next_out = p;
1277 err = inflate(&stream, Z_NO_FLUSH);
1278 /* may be Z_OK, or Z_STREAM_END */
1280 log_err("fio: inflate error %d\n", err);
1286 this_len = this_chunk - stream.avail_out;
1292 inflateEnd(&stream);
1300 * This has been compressed on the server side, since it can be big.
1303 static struct cmd_iolog_pdu *convert_iolog(struct fio_net_cmd *cmd)
1305 struct cmd_iolog_pdu *pdu = (struct cmd_iolog_pdu *) cmd->payload;
1306 struct cmd_iolog_pdu *ret;
1311 * Convert if compressed and we support it. If it's not
1312 * compressed, we need not do anything.
1314 if (le32_to_cpu(pdu->compressed)) {
1316 log_err("fio: server sent compressed data by mistake\n");
1319 ret = convert_iolog_gz(cmd, pdu);
1321 log_err("fio: failed decompressing log\n");
1327 ret->nr_samples = le64_to_cpu(ret->nr_samples);
1328 ret->thread_number = le32_to_cpu(ret->thread_number);
1329 ret->log_type = le32_to_cpu(ret->log_type);
1330 ret->compressed = le32_to_cpu(ret->compressed);
1331 ret->log_offset = le32_to_cpu(ret->log_offset);
1333 samples = &ret->samples[0];
1334 for (i = 0; i < ret->nr_samples; i++) {
1335 struct io_sample *s;
1337 s = __get_sample(samples, ret->log_offset, i);
1338 s->time = le64_to_cpu(s->time);
1339 s->val = le64_to_cpu(s->val);
1340 s->__ddir = le32_to_cpu(s->__ddir);
1341 s->bs = le32_to_cpu(s->bs);
1343 if (ret->log_offset) {
1344 struct io_sample_offset *so = (void *) s;
1346 so->offset = le64_to_cpu(so->offset);
1353 static void sendfile_reply(int fd, struct cmd_sendfile_reply *rep,
1354 size_t size, uint64_t tag)
1356 rep->error = cpu_to_le32(rep->error);
1357 fio_net_send_cmd(fd, FIO_NET_CMD_SENDFILE, rep, size, &tag, NULL);
1360 static int send_file(struct fio_client *client, struct cmd_sendfile *pdu,
1363 struct cmd_sendfile_reply *rep;
1368 size = sizeof(*rep);
1371 if (stat((char *)pdu->path, &sb) < 0) {
1374 sendfile_reply(client->fd, rep, size, tag);
1380 rep = realloc(rep, size);
1381 rep->size = cpu_to_le32((uint32_t) sb.st_size);
1383 fd = open((char *)pdu->path, O_RDONLY);
1387 rep->error = read_data(fd, &rep->data, sb.st_size);
1388 sendfile_reply(client->fd, rep, size, tag);
1394 int fio_handle_client(struct fio_client *client)
1396 struct client_ops *ops = client->ops;
1397 struct fio_net_cmd *cmd;
1399 dprint(FD_NET, "client: handle %s\n", client->hostname);
1401 cmd = fio_net_recv_cmd(client->fd);
1405 dprint(FD_NET, "client: got cmd op %s from %s (pdu=%u)\n",
1406 fio_server_op(cmd->opcode), client->hostname, cmd->pdu_len);
1408 switch (cmd->opcode) {
1409 case FIO_NET_CMD_QUIT:
1411 ops->quit(client, cmd);
1412 remove_client(client);
1414 case FIO_NET_CMD_TEXT:
1416 ops->text(client, cmd);
1418 case FIO_NET_CMD_DU: {
1419 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
1421 convert_dus(&du->dus);
1422 convert_agg(&du->agg);
1424 ops->disk_util(client, cmd);
1427 case FIO_NET_CMD_TS: {
1428 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
1430 convert_ts(&p->ts, &p->ts);
1431 convert_gs(&p->rs, &p->rs);
1433 ops->thread_status(client, cmd);
1436 case FIO_NET_CMD_GS: {
1437 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
1441 ops->group_stats(client, cmd);
1444 case FIO_NET_CMD_ETA: {
1445 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
1447 remove_reply_cmd(client, cmd);
1448 convert_jobs_eta(je);
1449 handle_eta(client, cmd);
1452 case FIO_NET_CMD_PROBE:
1453 remove_reply_cmd(client, cmd);
1454 ops->probe(client, cmd);
1456 case FIO_NET_CMD_SERVER_START:
1457 client->state = Client_running;
1459 ops->job_start(client, cmd);
1461 case FIO_NET_CMD_START: {
1462 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
1464 pdu->jobs = le32_to_cpu(pdu->jobs);
1465 ops->start(client, cmd);
1468 case FIO_NET_CMD_STOP: {
1469 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
1472 client->state = Client_stopped;
1473 client->error = le32_to_cpu(pdu->error);
1474 client->signal = le32_to_cpu(pdu->signal);
1475 ops->stop(client, cmd);
1478 case FIO_NET_CMD_ADD_JOB: {
1479 struct cmd_add_job_pdu *pdu = (struct cmd_add_job_pdu *) cmd->payload;
1481 client->thread_number = le32_to_cpu(pdu->thread_number);
1482 client->groupid = le32_to_cpu(pdu->groupid);
1485 ops->add_job(client, cmd);
1488 case FIO_NET_CMD_IOLOG:
1490 struct cmd_iolog_pdu *pdu;
1492 pdu = convert_iolog(cmd);
1493 ops->iolog(client, pdu);
1496 case FIO_NET_CMD_UPDATE_JOB:
1497 ops->update_job(client, cmd);
1498 remove_reply_cmd(client, cmd);
1500 case FIO_NET_CMD_VTRIGGER: {
1501 struct all_io_list *pdu = (struct all_io_list *) cmd->payload;
1506 strcpy(buf, aux_path);
1510 __verify_save_state(pdu, server_name(client, &buf[off], sizeof(buf) - off));
1511 exec_trigger(trigger_cmd);
1514 case FIO_NET_CMD_SENDFILE: {
1515 struct cmd_sendfile *pdu = (struct cmd_sendfile *) cmd->payload;
1516 send_file(client, pdu, cmd->tag);
1520 log_err("fio: unknown client op: %s\n", fio_server_op(cmd->opcode));
1528 int fio_clients_send_trigger(const char *cmd)
1530 struct flist_head *entry;
1531 struct fio_client *client;
1534 dprint(FD_NET, "client: send vtrigger: %s\n", cmd);
1541 flist_for_each(entry, &client_list) {
1542 struct cmd_vtrigger_pdu *pdu;
1544 client = flist_entry(entry, struct fio_client, list);
1546 pdu = malloc(sizeof(*pdu) + slen);
1547 pdu->len = cpu_to_le16((uint16_t) slen);
1549 memcpy(pdu->cmd, cmd, slen);
1550 fio_net_send_cmd(client->fd, FIO_NET_CMD_VTRIGGER, pdu,
1551 sizeof(*pdu) + slen, NULL, NULL);
1558 static void request_client_etas(struct client_ops *ops)
1560 struct fio_client *client;
1561 struct flist_head *entry;
1562 struct client_eta *eta;
1565 dprint(FD_NET, "client: request eta (%d)\n", nr_clients);
1567 eta = calloc(1, sizeof(*eta) + __THREAD_RUNSTR_SZ(REAL_MAX_JOBS));
1568 eta->pending = nr_clients;
1570 flist_for_each(entry, &client_list) {
1571 client = flist_entry(entry, struct fio_client, list);
1573 if (!flist_empty(&client->eta_list)) {
1577 if (client->state != Client_running)
1580 assert(!client->eta_in_flight);
1581 flist_add_tail(&client->eta_list, &eta_list);
1582 client->eta_in_flight = eta;
1583 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_SEND_ETA,
1584 (uintptr_t) eta, &client->cmd_list);
1588 if (!fio_client_dec_jobs_eta(eta, ops->eta))
1592 dprint(FD_NET, "client: requested eta tag %p\n", eta);
1596 * A single SEND_ETA timeout isn't fatal. Attempt to recover.
1598 static int handle_cmd_timeout(struct fio_client *client,
1599 struct fio_net_cmd_reply *reply)
1601 if (reply->opcode != FIO_NET_CMD_SEND_ETA)
1604 log_info("client <%s>: timeout on SEND_ETA\n", client->hostname);
1605 flist_del(&reply->list);
1608 flist_del_init(&client->eta_list);
1609 if (client->eta_in_flight) {
1610 fio_client_dec_jobs_eta(client->eta_in_flight, client->ops->eta);
1611 client->eta_in_flight = NULL;
1615 * If we fail 5 in a row, give up...
1617 if (client->eta_timeouts++ > 5)
1623 static int client_check_cmd_timeout(struct fio_client *client,
1624 struct timeval *now)
1626 struct fio_net_cmd_reply *reply;
1627 struct flist_head *entry, *tmp;
1630 flist_for_each_safe(entry, tmp, &client->cmd_list) {
1631 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
1633 if (mtime_since(&reply->tv, now) < FIO_NET_CLIENT_TIMEOUT)
1636 if (!handle_cmd_timeout(client, reply))
1639 log_err("fio: client %s, timeout on cmd %s\n", client->hostname,
1640 fio_server_op(reply->opcode));
1641 flist_del(&reply->list);
1646 return flist_empty(&client->cmd_list) && ret;
1649 static int fio_check_clients_timed_out(void)
1651 struct fio_client *client;
1652 struct flist_head *entry, *tmp;
1656 fio_gettime(&tv, NULL);
1658 flist_for_each_safe(entry, tmp, &client_list) {
1659 client = flist_entry(entry, struct fio_client, list);
1661 if (flist_empty(&client->cmd_list))
1664 if (!client_check_cmd_timeout(client, &tv))
1667 if (client->ops->timed_out)
1668 client->ops->timed_out(client);
1670 log_err("fio: client %s timed out\n", client->hostname);
1672 client->error = ETIMEDOUT;
1673 remove_client(client);
1680 int fio_handle_clients(struct client_ops *ops)
1682 struct pollfd *pfds;
1683 int i, ret = 0, retval = 0;
1685 fio_gettime(&eta_tv, NULL);
1687 pfds = malloc(nr_clients * sizeof(struct pollfd));
1689 init_thread_stat(&client_ts);
1690 init_group_run_stat(&client_gs);
1692 while (!exit_backend && nr_clients) {
1693 struct flist_head *entry, *tmp;
1694 struct fio_client *client;
1697 flist_for_each_safe(entry, tmp, &client_list) {
1698 client = flist_entry(entry, struct fio_client, list);
1700 if (!client->sent_job && !client->ops->stay_connected &&
1701 flist_empty(&client->cmd_list)) {
1702 remove_client(client);
1706 pfds[i].fd = client->fd;
1707 pfds[i].events = POLLIN;
1714 assert(i == nr_clients);
1720 fio_gettime(&tv, NULL);
1721 if (mtime_since(&eta_tv, &tv) >= 900) {
1722 request_client_etas(ops);
1723 memcpy(&eta_tv, &tv, sizeof(tv));
1725 if (fio_check_clients_timed_out())
1729 check_trigger_file();
1731 timeout = min(100u, ops->eta_msec);
1733 ret = poll(pfds, nr_clients, timeout);
1737 log_err("fio: poll clients: %s\n", strerror(errno));
1743 for (i = 0; i < nr_clients; i++) {
1744 if (!(pfds[i].revents & POLLIN))
1747 client = find_client_by_fd(pfds[i].fd);
1749 log_err("fio: unknown client fd %ld\n", (long) pfds[i].fd);
1752 if (!fio_handle_client(client)) {
1753 log_info("client: host=%s disconnected\n",
1755 remove_client(client);
1757 } else if (client->error)
1759 fio_put_client(client);
1763 fio_client_json_fini();
1766 return retval || error_clients;