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;
64 #define FIO_CLIENT_HASH_BITS 7
65 #define FIO_CLIENT_HASH_SZ (1 << FIO_CLIENT_HASH_BITS)
66 #define FIO_CLIENT_HASH_MASK (FIO_CLIENT_HASH_SZ - 1)
67 static struct flist_head client_hash[FIO_CLIENT_HASH_SZ];
69 static void fio_client_add_hash(struct fio_client *client)
71 int bucket = hash_long(client->fd, FIO_CLIENT_HASH_BITS);
73 bucket &= FIO_CLIENT_HASH_MASK;
74 flist_add(&client->hash_list, &client_hash[bucket]);
77 static void fio_client_remove_hash(struct fio_client *client)
79 if (!flist_empty(&client->hash_list))
80 flist_del_init(&client->hash_list);
83 static void fio_init fio_client_hash_init(void)
87 for (i = 0; i < FIO_CLIENT_HASH_SZ; i++)
88 INIT_FLIST_HEAD(&client_hash[i]);
91 static void fio_client_json_init(void)
93 if (output_format != FIO_OUTPUT_JSON)
95 root = json_create_object();
96 json_object_add_value_string(root, "fio version", fio_version_string);
97 clients_array = json_create_array();
98 json_object_add_value_array(root, "client_stats", clients_array);
99 du_array = json_create_array();
100 json_object_add_value_array(root, "disk_util", du_array);
103 static void fio_client_json_fini(void)
105 if (output_format != FIO_OUTPUT_JSON)
107 json_print_object(root);
109 json_free_object(root);
111 clients_array = NULL;
115 static struct fio_client *find_client_by_fd(int fd)
117 int bucket = hash_long(fd, FIO_CLIENT_HASH_BITS) & FIO_CLIENT_HASH_MASK;
118 struct fio_client *client;
119 struct flist_head *entry;
121 flist_for_each(entry, &client_hash[bucket]) {
122 client = flist_entry(entry, struct fio_client, hash_list);
124 if (client->fd == fd) {
133 void fio_put_client(struct fio_client *client)
138 free(client->hostname);
143 while (client->nr_files) {
144 struct client_file *cf = &client->files[--client->nr_files];
151 if (!client->did_stat)
157 static void remove_client(struct fio_client *client)
159 assert(client->refs);
161 dprint(FD_NET, "client: removed <%s>\n", client->hostname);
163 if (!flist_empty(&client->list))
164 flist_del_init(&client->list);
166 fio_client_remove_hash(client);
168 if (!flist_empty(&client->eta_list)) {
169 flist_del_init(&client->eta_list);
170 fio_client_dec_jobs_eta(client->eta_in_flight, client->ops->eta);
176 if (client->ops->removed)
177 client->ops->removed(client);
180 fio_put_client(client);
183 struct fio_client *fio_get_client(struct fio_client *client)
189 static void __fio_client_add_cmd_option(struct fio_client *client,
194 index = client->argc++;
195 client->argv = realloc(client->argv, sizeof(char *) * client->argc);
196 client->argv[index] = strdup(opt);
197 dprint(FD_NET, "client: add cmd %d: %s\n", index, opt);
200 void fio_client_add_cmd_option(void *cookie, const char *opt)
202 struct fio_client *client = cookie;
203 struct flist_head *entry;
208 __fio_client_add_cmd_option(client, opt);
211 * Duplicate arguments to shared client group
213 flist_for_each(entry, &arg_list) {
214 client = flist_entry(entry, struct fio_client, arg_list);
216 __fio_client_add_cmd_option(client, opt);
220 struct fio_client *fio_client_add_explicit(struct client_ops *ops,
221 const char *hostname, int type,
224 struct fio_client *client;
226 client = malloc(sizeof(*client));
227 memset(client, 0, sizeof(*client));
229 INIT_FLIST_HEAD(&client->list);
230 INIT_FLIST_HEAD(&client->hash_list);
231 INIT_FLIST_HEAD(&client->arg_list);
232 INIT_FLIST_HEAD(&client->eta_list);
233 INIT_FLIST_HEAD(&client->cmd_list);
235 client->hostname = strdup(hostname);
237 if (type == Fio_client_socket)
242 ipv6 = type == Fio_client_ipv6;
243 if (fio_server_parse_host(hostname, ipv6,
244 &client->addr.sin_addr,
245 &client->addr6.sin6_addr))
254 client->type = ops->client_type;
256 __fio_client_add_cmd_option(client, "fio");
258 flist_add(&client->list, &client_list);
260 dprint(FD_NET, "client: added <%s>\n", client->hostname);
267 int fio_client_add_ini_file(void *cookie, const char *ini_file, int remote)
269 struct fio_client *client = cookie;
270 struct client_file *cf;
277 dprint(FD_NET, "client <%s>: add ini %s\n", client->hostname, ini_file);
279 new_size = (client->nr_files + 1) * sizeof(struct client_file);
280 new_files = realloc(client->files, new_size);
284 client->files = new_files;
285 cf = &client->files[client->nr_files];
286 cf->file = strdup(ini_file);
292 int fio_client_add(struct client_ops *ops, const char *hostname, void **cookie)
294 struct fio_client *existing = *cookie;
295 struct fio_client *client;
299 * We always add our "exec" name as the option, hence 1
302 if (existing->argc == 1)
303 flist_add_tail(&existing->arg_list, &arg_list);
305 while (!flist_empty(&arg_list))
306 flist_del_init(arg_list.next);
310 client = malloc(sizeof(*client));
311 memset(client, 0, sizeof(*client));
313 INIT_FLIST_HEAD(&client->list);
314 INIT_FLIST_HEAD(&client->hash_list);
315 INIT_FLIST_HEAD(&client->arg_list);
316 INIT_FLIST_HEAD(&client->eta_list);
317 INIT_FLIST_HEAD(&client->cmd_list);
319 if (fio_server_parse_string(hostname, &client->hostname,
320 &client->is_sock, &client->port,
321 &client->addr.sin_addr,
322 &client->addr6.sin6_addr,
329 client->type = ops->client_type;
331 __fio_client_add_cmd_option(client, "fio");
333 flist_add(&client->list, &client_list);
335 dprint(FD_NET, "client: added <%s>\n", client->hostname);
340 static void probe_client(struct fio_client *client)
342 struct cmd_client_probe_pdu pdu;
345 dprint(FD_NET, "client: send probe\n");
348 pdu.flags = __le64_to_cpu(FIO_PROBE_FLAG_ZLIB);
353 fio_net_send_cmd(client->fd, FIO_NET_CMD_PROBE, &pdu, sizeof(pdu), &tag, &client->cmd_list);
356 static int fio_client_connect_ip(struct fio_client *client)
358 struct sockaddr *addr;
363 client->addr6.sin6_family = AF_INET6;
364 client->addr6.sin6_port = htons(client->port);
366 addr = (struct sockaddr *) &client->addr6;
367 socklen = sizeof(client->addr6);
369 client->addr.sin_family = AF_INET;
370 client->addr.sin_port = htons(client->port);
372 addr = (struct sockaddr *) &client->addr;
373 socklen = sizeof(client->addr);
376 fd = socket(domain, SOCK_STREAM, 0);
380 log_err("fio: socket: %s\n", strerror(errno));
384 if (connect(fd, addr, socklen) < 0) {
387 log_err("fio: connect: %s\n", strerror(errno));
388 log_err("fio: failed to connect to %s:%u\n", client->hostname,
397 static int fio_client_connect_sock(struct fio_client *client)
399 struct sockaddr_un *addr = &client->addr_un;
403 memset(addr, 0, sizeof(*addr));
404 addr->sun_family = AF_UNIX;
405 strncpy(addr->sun_path, client->hostname, sizeof(addr->sun_path) - 1);
407 fd = socket(AF_UNIX, SOCK_STREAM, 0);
411 log_err("fio: socket: %s\n", strerror(errno));
415 len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
416 if (connect(fd, (struct sockaddr *) addr, len) < 0) {
419 log_err("fio: connect; %s\n", strerror(errno));
427 int fio_client_connect(struct fio_client *client)
431 dprint(FD_NET, "client: connect to host %s\n", client->hostname);
434 fd = fio_client_connect_sock(client);
436 fd = fio_client_connect_ip(client);
438 dprint(FD_NET, "client: %s connected %d\n", client->hostname, fd);
444 fio_client_add_hash(client);
445 client->state = Client_connected;
447 probe_client(client);
451 int fio_client_terminate(struct fio_client *client)
453 return fio_net_send_quit(client->fd);
456 void fio_clients_terminate(void)
458 struct flist_head *entry;
459 struct fio_client *client;
461 dprint(FD_NET, "client: terminate clients\n");
463 flist_for_each(entry, &client_list) {
464 client = flist_entry(entry, struct fio_client, list);
465 fio_client_terminate(client);
469 static void sig_int(int sig)
471 dprint(FD_NET, "client: got signal %d\n", sig);
472 fio_clients_terminate();
475 static void sig_show_status(int sig)
477 show_running_run_stats();
480 static void client_signal_handler(void)
482 struct sigaction act;
484 memset(&act, 0, sizeof(act));
485 act.sa_handler = sig_int;
486 act.sa_flags = SA_RESTART;
487 sigaction(SIGINT, &act, NULL);
489 memset(&act, 0, sizeof(act));
490 act.sa_handler = sig_int;
491 act.sa_flags = SA_RESTART;
492 sigaction(SIGTERM, &act, NULL);
494 /* Windows uses SIGBREAK as a quit signal from other applications */
496 memset(&act, 0, sizeof(act));
497 act.sa_handler = sig_int;
498 act.sa_flags = SA_RESTART;
499 sigaction(SIGBREAK, &act, NULL);
502 memset(&act, 0, sizeof(act));
503 act.sa_handler = sig_show_status;
504 act.sa_flags = SA_RESTART;
505 sigaction(SIGUSR1, &act, NULL);
508 static int send_client_cmd_line(struct fio_client *client)
510 struct cmd_single_line_pdu *cslp;
511 struct cmd_line_pdu *clp;
512 unsigned long offset;
518 dprint(FD_NET, "client: send cmdline %d\n", client->argc);
520 lens = malloc(client->argc * sizeof(unsigned int));
523 * Find out how much mem we need
525 for (i = 0, mem = 0; i < client->argc; i++) {
526 lens[i] = strlen(client->argv[i]) + 1;
531 * We need one cmd_line_pdu, and argc number of cmd_single_line_pdu
533 mem += sizeof(*clp) + (client->argc * sizeof(*cslp));
537 offset = sizeof(*clp);
539 for (i = 0; i < client->argc; i++) {
540 uint16_t arg_len = lens[i];
543 strcpy((char *) cslp->text, client->argv[i]);
544 cslp->len = cpu_to_le16(arg_len);
545 offset += sizeof(*cslp) + arg_len;
549 clp->lines = cpu_to_le16(client->argc);
550 clp->client_type = __cpu_to_le16(client->type);
551 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOBLINE, pdu, mem, NULL, NULL);
556 int fio_clients_connect(void)
558 struct fio_client *client;
559 struct flist_head *entry, *tmp;
564 WSAStartup(MAKEWORD(2, 2), &wsd);
567 dprint(FD_NET, "client: connect all\n");
569 client_signal_handler();
571 flist_for_each_safe(entry, tmp, &client_list) {
572 client = flist_entry(entry, struct fio_client, list);
574 ret = fio_client_connect(client);
576 remove_client(client);
580 if (client->argc > 1)
581 send_client_cmd_line(client);
587 int fio_start_client(struct fio_client *client)
589 dprint(FD_NET, "client: start %s\n", client->hostname);
590 return fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_RUN, 0, NULL);
593 int fio_start_all_clients(void)
595 struct fio_client *client;
596 struct flist_head *entry, *tmp;
599 dprint(FD_NET, "client: start all\n");
601 fio_client_json_init();
603 flist_for_each_safe(entry, tmp, &client_list) {
604 client = flist_entry(entry, struct fio_client, list);
606 ret = fio_start_client(client);
608 remove_client(client);
613 return flist_empty(&client_list);
616 static int __fio_client_send_remote_ini(struct fio_client *client,
617 const char *filename)
619 struct cmd_load_file_pdu *pdu;
623 dprint(FD_NET, "send remote ini %s to %s\n", filename, client->hostname);
625 p_size = sizeof(*pdu) + strlen(filename) + 1;
626 pdu = malloc(p_size);
627 memset(pdu, 0, p_size);
628 pdu->name_len = strlen(filename);
629 strcpy((char *) pdu->file, filename);
630 pdu->client_type = cpu_to_le16((uint16_t) client->type);
632 client->sent_job = 1;
633 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_LOAD_FILE, pdu, p_size,NULL, NULL);
639 * Send file contents to server backend. We could use sendfile(), but to remain
640 * more portable lets just read/write the darn thing.
642 static int __fio_client_send_local_ini(struct fio_client *client,
643 const char *filename)
645 struct cmd_job_pdu *pdu;
653 dprint(FD_NET, "send ini %s to %s\n", filename, client->hostname);
655 fd = open(filename, O_RDONLY);
659 log_err("fio: job file <%s> open: %s\n", filename, strerror(errno));
663 if (fstat(fd, &sb) < 0) {
666 log_err("fio: job file stat: %s\n", strerror(errno));
671 p_size = sb.st_size + sizeof(*pdu);
672 pdu = malloc(p_size);
678 ret = read(fd, p, len);
687 else if (errno == EAGAIN || errno == EINTR)
692 log_err("fio: failed reading job file %s\n", filename);
698 pdu->buf_len = __cpu_to_le32(sb.st_size);
699 pdu->client_type = cpu_to_le32(client->type);
701 client->sent_job = 1;
702 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, pdu, p_size, NULL, NULL);
708 int fio_client_send_ini(struct fio_client *client, const char *filename,
714 ret = __fio_client_send_local_ini(client, filename);
716 ret = __fio_client_send_remote_ini(client, filename);
719 client->sent_job = 1;
724 static int fio_client_send_cf(struct fio_client *client,
725 struct client_file *cf)
727 return fio_client_send_ini(client, cf->file, cf->remote);
730 int fio_clients_send_ini(const char *filename)
732 struct fio_client *client;
733 struct flist_head *entry, *tmp;
735 flist_for_each_safe(entry, tmp, &client_list) {
736 client = flist_entry(entry, struct fio_client, list);
738 if (client->nr_files) {
741 for (i = 0; i < client->nr_files; i++) {
742 struct client_file *cf;
744 cf = &client->files[i];
746 if (fio_client_send_cf(client, cf)) {
747 remove_client(client);
752 if (client->sent_job)
754 if (!filename || fio_client_send_ini(client, filename, 0))
755 remove_client(client);
761 int fio_client_update_options(struct fio_client *client,
762 struct thread_options *o, uint64_t *tag)
764 struct cmd_add_job_pdu pdu;
766 pdu.thread_number = cpu_to_le32(client->thread_number);
767 pdu.groupid = cpu_to_le32(client->groupid);
768 convert_thread_options_to_net(&pdu.top, o);
770 return fio_net_send_cmd(client->fd, FIO_NET_CMD_UPDATE_JOB, &pdu, sizeof(pdu), tag, &client->cmd_list);
773 static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
775 dst->max_val = le64_to_cpu(src->max_val);
776 dst->min_val = le64_to_cpu(src->min_val);
777 dst->samples = le64_to_cpu(src->samples);
780 * Floats arrive as IEEE 754 encoded uint64_t, convert back to double
782 dst->mean.u.f = fio_uint64_to_double(le64_to_cpu(dst->mean.u.i));
783 dst->S.u.f = fio_uint64_to_double(le64_to_cpu(dst->S.u.i));
786 static void convert_ts(struct thread_stat *dst, struct thread_stat *src)
790 dst->error = le32_to_cpu(src->error);
791 dst->thread_number = le32_to_cpu(src->thread_number);
792 dst->groupid = le32_to_cpu(src->groupid);
793 dst->pid = le32_to_cpu(src->pid);
794 dst->members = le32_to_cpu(src->members);
795 dst->unified_rw_rep = le32_to_cpu(src->unified_rw_rep);
797 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
798 convert_io_stat(&dst->clat_stat[i], &src->clat_stat[i]);
799 convert_io_stat(&dst->slat_stat[i], &src->slat_stat[i]);
800 convert_io_stat(&dst->lat_stat[i], &src->lat_stat[i]);
801 convert_io_stat(&dst->bw_stat[i], &src->bw_stat[i]);
804 dst->usr_time = le64_to_cpu(src->usr_time);
805 dst->sys_time = le64_to_cpu(src->sys_time);
806 dst->ctx = le64_to_cpu(src->ctx);
807 dst->minf = le64_to_cpu(src->minf);
808 dst->majf = le64_to_cpu(src->majf);
809 dst->clat_percentiles = le64_to_cpu(src->clat_percentiles);
811 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
812 fio_fp64_t *fps = &src->percentile_list[i];
813 fio_fp64_t *fpd = &dst->percentile_list[i];
815 fpd->u.f = fio_uint64_to_double(le64_to_cpu(fps->u.i));
818 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
819 dst->io_u_map[i] = le32_to_cpu(src->io_u_map[i]);
820 dst->io_u_submit[i] = le32_to_cpu(src->io_u_submit[i]);
821 dst->io_u_complete[i] = le32_to_cpu(src->io_u_complete[i]);
824 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
825 dst->io_u_lat_u[i] = le32_to_cpu(src->io_u_lat_u[i]);
826 dst->io_u_lat_m[i] = le32_to_cpu(src->io_u_lat_m[i]);
829 for (i = 0; i < DDIR_RWDIR_CNT; i++)
830 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
831 dst->io_u_plat[i][j] = le32_to_cpu(src->io_u_plat[i][j]);
833 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
834 dst->total_io_u[i] = le64_to_cpu(src->total_io_u[i]);
835 dst->short_io_u[i] = le64_to_cpu(src->short_io_u[i]);
836 dst->drop_io_u[i] = le64_to_cpu(src->drop_io_u[i]);
839 dst->total_submit = le64_to_cpu(src->total_submit);
840 dst->total_complete = le64_to_cpu(src->total_complete);
842 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
843 dst->io_bytes[i] = le64_to_cpu(src->io_bytes[i]);
844 dst->runtime[i] = le64_to_cpu(src->runtime[i]);
847 dst->total_run_time = le64_to_cpu(src->total_run_time);
848 dst->continue_on_error = le16_to_cpu(src->continue_on_error);
849 dst->total_err_count = le64_to_cpu(src->total_err_count);
850 dst->first_error = le32_to_cpu(src->first_error);
851 dst->kb_base = le32_to_cpu(src->kb_base);
852 dst->unit_base = le32_to_cpu(src->unit_base);
854 dst->latency_depth = le32_to_cpu(src->latency_depth);
855 dst->latency_target = le64_to_cpu(src->latency_target);
856 dst->latency_window = le64_to_cpu(src->latency_window);
857 dst->latency_percentile.u.f = fio_uint64_to_double(le64_to_cpu(src->latency_percentile.u.i));
860 static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
864 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
865 dst->max_run[i] = le64_to_cpu(src->max_run[i]);
866 dst->min_run[i] = le64_to_cpu(src->min_run[i]);
867 dst->max_bw[i] = le64_to_cpu(src->max_bw[i]);
868 dst->min_bw[i] = le64_to_cpu(src->min_bw[i]);
869 dst->io_kb[i] = le64_to_cpu(src->io_kb[i]);
870 dst->agg[i] = le64_to_cpu(src->agg[i]);
873 dst->kb_base = le32_to_cpu(src->kb_base);
874 dst->unit_base = le32_to_cpu(src->unit_base);
875 dst->groupid = le32_to_cpu(src->groupid);
876 dst->unified_rw_rep = le32_to_cpu(src->unified_rw_rep);
879 static void json_object_add_client_info(struct json_object *obj,
880 struct fio_client *client)
882 const char *hostname = client->hostname ? client->hostname : "";
884 json_object_add_value_string(obj, "hostname", hostname);
885 json_object_add_value_int(obj, "port", client->port);
888 static void handle_ts(struct fio_client *client, struct fio_net_cmd *cmd)
890 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
891 struct json_object *tsobj;
893 tsobj = show_thread_status(&p->ts, &p->rs);
894 client->did_stat = 1;
896 json_object_add_client_info(tsobj, client);
897 json_array_add_value_object(clients_array, tsobj);
900 if (sum_stat_clients <= 1)
903 sum_thread_stats(&client_ts, &p->ts, sum_stat_nr);
904 sum_group_stats(&client_gs, &p->rs);
907 client_ts.thread_number = p->ts.thread_number;
908 client_ts.groupid = p->ts.groupid;
909 client_ts.unified_rw_rep = p->ts.unified_rw_rep;
911 if (++sum_stat_nr == sum_stat_clients) {
912 strcpy(client_ts.name, "All clients");
913 tsobj = show_thread_status(&client_ts, &client_gs);
915 json_object_add_client_info(tsobj, client);
916 json_array_add_value_object(clients_array, tsobj);
921 static void handle_gs(struct fio_client *client, struct fio_net_cmd *cmd)
923 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
925 show_group_stats(gs);
928 static void handle_text(struct fio_client *client, struct fio_net_cmd *cmd)
930 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
931 const char *buf = (const char *) pdu->buf;
935 name = client->name ? client->name : client->hostname;
937 if (!client->skip_newline)
938 fprintf(f_out, "<%s> ", name);
939 ret = fwrite(buf, pdu->buf_len, 1, f_out);
941 client->skip_newline = strchr(buf, '\n') == NULL;
944 static void convert_agg(struct disk_util_agg *agg)
948 for (i = 0; i < 2; i++) {
949 agg->ios[i] = le32_to_cpu(agg->ios[i]);
950 agg->merges[i] = le32_to_cpu(agg->merges[i]);
951 agg->sectors[i] = le64_to_cpu(agg->sectors[i]);
952 agg->ticks[i] = le32_to_cpu(agg->ticks[i]);
955 agg->io_ticks = le32_to_cpu(agg->io_ticks);
956 agg->time_in_queue = le32_to_cpu(agg->time_in_queue);
957 agg->slavecount = le32_to_cpu(agg->slavecount);
958 agg->max_util.u.f = fio_uint64_to_double(le64_to_cpu(agg->max_util.u.i));
961 static void convert_dus(struct disk_util_stat *dus)
965 for (i = 0; i < 2; i++) {
966 dus->s.ios[i] = le32_to_cpu(dus->s.ios[i]);
967 dus->s.merges[i] = le32_to_cpu(dus->s.merges[i]);
968 dus->s.sectors[i] = le64_to_cpu(dus->s.sectors[i]);
969 dus->s.ticks[i] = le32_to_cpu(dus->s.ticks[i]);
972 dus->s.io_ticks = le32_to_cpu(dus->s.io_ticks);
973 dus->s.time_in_queue = le32_to_cpu(dus->s.time_in_queue);
974 dus->s.msec = le64_to_cpu(dus->s.msec);
977 static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd)
979 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
981 if (!client->disk_stats_shown) {
982 client->disk_stats_shown = 1;
983 log_info("\nDisk stats (read/write):\n");
986 if (output_format == FIO_OUTPUT_JSON) {
987 struct json_object *duobj;
988 json_array_add_disk_util(&du->dus, &du->agg, du_array);
989 duobj = json_array_last_value_object(du_array);
990 json_object_add_client_info(duobj, client);
992 print_disk_util(&du->dus, &du->agg, output_format == FIO_OUTPUT_TERSE);
995 static void convert_jobs_eta(struct jobs_eta *je)
999 je->nr_running = le32_to_cpu(je->nr_running);
1000 je->nr_ramp = le32_to_cpu(je->nr_ramp);
1001 je->nr_pending = le32_to_cpu(je->nr_pending);
1002 je->nr_setting_up = le32_to_cpu(je->nr_setting_up);
1003 je->files_open = le32_to_cpu(je->files_open);
1005 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1006 je->m_rate[i] = le32_to_cpu(je->m_rate[i]);
1007 je->t_rate[i] = le32_to_cpu(je->t_rate[i]);
1008 je->m_iops[i] = le32_to_cpu(je->m_iops[i]);
1009 je->t_iops[i] = le32_to_cpu(je->t_iops[i]);
1010 je->rate[i] = le32_to_cpu(je->rate[i]);
1011 je->iops[i] = le32_to_cpu(je->iops[i]);
1014 je->elapsed_sec = le64_to_cpu(je->elapsed_sec);
1015 je->eta_sec = le64_to_cpu(je->eta_sec);
1016 je->nr_threads = le32_to_cpu(je->nr_threads);
1017 je->is_pow2 = le32_to_cpu(je->is_pow2);
1018 je->unit_base = le32_to_cpu(je->unit_base);
1021 void fio_client_sum_jobs_eta(struct jobs_eta *dst, struct jobs_eta *je)
1025 dst->nr_running += je->nr_running;
1026 dst->nr_ramp += je->nr_ramp;
1027 dst->nr_pending += je->nr_pending;
1028 dst->nr_setting_up += je->nr_setting_up;
1029 dst->files_open += je->files_open;
1031 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1032 dst->m_rate[i] += je->m_rate[i];
1033 dst->t_rate[i] += je->t_rate[i];
1034 dst->m_iops[i] += je->m_iops[i];
1035 dst->t_iops[i] += je->t_iops[i];
1036 dst->rate[i] += je->rate[i];
1037 dst->iops[i] += je->iops[i];
1040 dst->elapsed_sec += je->elapsed_sec;
1042 if (je->eta_sec > dst->eta_sec)
1043 dst->eta_sec = je->eta_sec;
1045 dst->nr_threads += je->nr_threads;
1048 * This wont be correct for multiple strings, but at least it
1049 * works for the basic cases.
1051 strcpy((char *) dst->run_str, (char *) je->run_str);
1054 void fio_client_dec_jobs_eta(struct client_eta *eta, client_eta_op eta_fn)
1056 if (!--eta->pending) {
1062 static void remove_reply_cmd(struct fio_client *client, struct fio_net_cmd *cmd)
1064 struct fio_net_cmd_reply *reply = NULL;
1065 struct flist_head *entry;
1067 flist_for_each(entry, &client->cmd_list) {
1068 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
1070 if (cmd->tag == (uintptr_t) reply)
1077 log_err("fio: client: unable to find matching tag (%llx)\n", (unsigned long long) cmd->tag);
1081 flist_del(&reply->list);
1082 cmd->tag = reply->saved_tag;
1086 int fio_client_wait_for_reply(struct fio_client *client, uint64_t tag)
1089 struct fio_net_cmd_reply *reply = NULL;
1090 struct flist_head *entry;
1092 flist_for_each(entry, &client->cmd_list) {
1093 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
1095 if (tag == (uintptr_t) reply)
1110 static void handle_eta(struct fio_client *client, struct fio_net_cmd *cmd)
1112 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
1113 struct client_eta *eta = (struct client_eta *) (uintptr_t) cmd->tag;
1115 dprint(FD_NET, "client: got eta tag %p, %d\n", eta, eta->pending);
1117 assert(client->eta_in_flight == eta);
1119 client->eta_in_flight = NULL;
1120 flist_del_init(&client->eta_list);
1122 if (client->ops->jobs_eta)
1123 client->ops->jobs_eta(client, je);
1125 fio_client_sum_jobs_eta(&eta->eta, je);
1126 fio_client_dec_jobs_eta(eta, client->ops->eta);
1129 static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd)
1131 struct cmd_probe_reply_pdu *probe = (struct cmd_probe_reply_pdu *) cmd->payload;
1132 const char *os, *arch;
1135 os = fio_get_os_string(probe->os);
1139 arch = fio_get_arch_string(probe->arch);
1143 sprintf(bit, "%d-bit", probe->bpp * 8);
1144 probe->flags = le64_to_cpu(probe->flags);
1146 log_info("hostname=%s, be=%u, %s, os=%s, arch=%s, fio=%s, flags=%lx\n",
1147 probe->hostname, probe->bigendian, bit, os, arch,
1148 probe->fio_version, (unsigned long) probe->flags);
1151 client->name = strdup((char *) probe->hostname);
1154 static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd)
1156 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
1158 client->state = Client_started;
1159 client->jobs = le32_to_cpu(pdu->jobs);
1160 client->nr_stat = le32_to_cpu(pdu->stat_outputs);
1162 sum_stat_clients += client->nr_stat;
1165 static void handle_stop(struct fio_client *client, struct fio_net_cmd *cmd)
1168 log_info("client <%s>: exited with error %d\n", client->hostname, client->error);
1171 static void convert_stop(struct fio_net_cmd *cmd)
1173 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
1175 pdu->error = le32_to_cpu(pdu->error);
1178 static void convert_text(struct fio_net_cmd *cmd)
1180 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
1182 pdu->level = le32_to_cpu(pdu->level);
1183 pdu->buf_len = le32_to_cpu(pdu->buf_len);
1184 pdu->log_sec = le64_to_cpu(pdu->log_sec);
1185 pdu->log_usec = le64_to_cpu(pdu->log_usec);
1188 static struct cmd_iolog_pdu *convert_iolog_gz(struct fio_net_cmd *cmd,
1189 struct cmd_iolog_pdu *pdu)
1192 struct cmd_iolog_pdu *ret;
1194 uint32_t nr_samples;
1198 stream.zalloc = Z_NULL;
1199 stream.zfree = Z_NULL;
1200 stream.opaque = Z_NULL;
1201 stream.avail_in = 0;
1202 stream.next_in = Z_NULL;
1204 if (inflateInit(&stream) != Z_OK)
1208 * Get header first, it's not compressed
1210 nr_samples = le64_to_cpu(pdu->nr_samples);
1212 total = nr_samples * __log_entry_sz(le32_to_cpu(pdu->log_offset));
1213 ret = malloc(total + sizeof(*pdu));
1214 ret->nr_samples = nr_samples;
1216 memcpy(ret, pdu, sizeof(*pdu));
1218 p = (void *) ret + sizeof(*pdu);
1220 stream.avail_in = cmd->pdu_len - sizeof(*pdu);
1221 stream.next_in = (void *) pdu + sizeof(*pdu);
1222 while (stream.avail_in) {
1223 unsigned int this_chunk = 65536;
1224 unsigned int this_len;
1227 if (this_chunk > total)
1230 stream.avail_out = this_chunk;
1231 stream.next_out = p;
1232 err = inflate(&stream, Z_NO_FLUSH);
1233 /* may be Z_OK, or Z_STREAM_END */
1235 log_err("fio: inflate error %d\n", err);
1241 this_len = this_chunk - stream.avail_out;
1247 inflateEnd(&stream);
1255 * This has been compressed on the server side, since it can be big.
1258 static struct cmd_iolog_pdu *convert_iolog(struct fio_net_cmd *cmd)
1260 struct cmd_iolog_pdu *pdu = (struct cmd_iolog_pdu *) cmd->payload;
1261 struct cmd_iolog_pdu *ret;
1266 * Convert if compressed and we support it. If it's not
1267 * compressed, we need not do anything.
1269 if (le32_to_cpu(pdu->compressed)) {
1271 log_err("fio: server sent compressed data by mistake\n");
1274 ret = convert_iolog_gz(cmd, pdu);
1276 log_err("fio: failed decompressing log\n");
1282 ret->nr_samples = le64_to_cpu(ret->nr_samples);
1283 ret->thread_number = le32_to_cpu(ret->thread_number);
1284 ret->log_type = le32_to_cpu(ret->log_type);
1285 ret->compressed = le32_to_cpu(ret->compressed);
1286 ret->log_offset = le32_to_cpu(ret->log_offset);
1288 samples = &ret->samples[0];
1289 for (i = 0; i < ret->nr_samples; i++) {
1290 struct io_sample *s;
1292 s = __get_sample(samples, ret->log_offset, i);
1293 s->time = le64_to_cpu(s->time);
1294 s->val = le64_to_cpu(s->val);
1295 s->__ddir = le32_to_cpu(s->__ddir);
1296 s->bs = le32_to_cpu(s->bs);
1298 if (ret->log_offset) {
1299 struct io_sample_offset *so = (void *) s;
1301 so->offset = le64_to_cpu(so->offset);
1308 int fio_handle_client(struct fio_client *client)
1310 struct client_ops *ops = client->ops;
1311 struct fio_net_cmd *cmd;
1313 dprint(FD_NET, "client: handle %s\n", client->hostname);
1315 cmd = fio_net_recv_cmd(client->fd);
1319 dprint(FD_NET, "client: got cmd op %s from %s (pdu=%u)\n",
1320 fio_server_op(cmd->opcode), client->hostname, cmd->pdu_len);
1322 switch (cmd->opcode) {
1323 case FIO_NET_CMD_QUIT:
1325 ops->quit(client, cmd);
1326 remove_client(client);
1329 case FIO_NET_CMD_TEXT:
1331 ops->text(client, cmd);
1334 case FIO_NET_CMD_DU: {
1335 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
1337 convert_dus(&du->dus);
1338 convert_agg(&du->agg);
1340 ops->disk_util(client, cmd);
1344 case FIO_NET_CMD_TS: {
1345 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
1347 convert_ts(&p->ts, &p->ts);
1348 convert_gs(&p->rs, &p->rs);
1350 ops->thread_status(client, cmd);
1354 case FIO_NET_CMD_GS: {
1355 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
1359 ops->group_stats(client, cmd);
1363 case FIO_NET_CMD_ETA: {
1364 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
1366 remove_reply_cmd(client, cmd);
1367 convert_jobs_eta(je);
1368 handle_eta(client, cmd);
1372 case FIO_NET_CMD_PROBE:
1373 remove_reply_cmd(client, cmd);
1374 ops->probe(client, cmd);
1377 case FIO_NET_CMD_SERVER_START:
1378 client->state = Client_running;
1380 ops->job_start(client, cmd);
1383 case FIO_NET_CMD_START: {
1384 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
1386 pdu->jobs = le32_to_cpu(pdu->jobs);
1387 ops->start(client, cmd);
1391 case FIO_NET_CMD_STOP: {
1392 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
1395 client->state = Client_stopped;
1396 client->error = le32_to_cpu(pdu->error);
1397 client->signal = le32_to_cpu(pdu->signal);
1398 ops->stop(client, cmd);
1402 case FIO_NET_CMD_ADD_JOB: {
1403 struct cmd_add_job_pdu *pdu = (struct cmd_add_job_pdu *) cmd->payload;
1405 client->thread_number = le32_to_cpu(pdu->thread_number);
1406 client->groupid = le32_to_cpu(pdu->groupid);
1409 ops->add_job(client, cmd);
1413 case FIO_NET_CMD_IOLOG:
1415 struct cmd_iolog_pdu *pdu;
1417 pdu = convert_iolog(cmd);
1418 ops->iolog(client, pdu);
1422 case FIO_NET_CMD_UPDATE_JOB:
1423 ops->update_job(client, cmd);
1424 remove_reply_cmd(client, cmd);
1428 log_err("fio: unknown client op: %s\n", fio_server_op(cmd->opcode));
1436 static void request_client_etas(struct client_ops *ops)
1438 struct fio_client *client;
1439 struct flist_head *entry;
1440 struct client_eta *eta;
1443 dprint(FD_NET, "client: request eta (%d)\n", nr_clients);
1445 eta = calloc(1, sizeof(*eta) + __THREAD_RUNSTR_SZ(REAL_MAX_JOBS));
1446 eta->pending = nr_clients;
1448 flist_for_each(entry, &client_list) {
1449 client = flist_entry(entry, struct fio_client, list);
1451 if (!flist_empty(&client->eta_list)) {
1455 if (client->state != Client_running)
1458 assert(!client->eta_in_flight);
1459 flist_add_tail(&client->eta_list, &eta_list);
1460 client->eta_in_flight = eta;
1461 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_SEND_ETA,
1462 (uintptr_t) eta, &client->cmd_list);
1466 fio_client_dec_jobs_eta(eta, ops->eta);
1468 dprint(FD_NET, "client: requested eta tag %p\n", eta);
1471 static int client_check_cmd_timeout(struct fio_client *client,
1472 struct timeval *now)
1474 struct fio_net_cmd_reply *reply;
1475 struct flist_head *entry, *tmp;
1478 flist_for_each_safe(entry, tmp, &client->cmd_list) {
1479 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
1481 if (mtime_since(&reply->tv, now) < FIO_NET_CLIENT_TIMEOUT)
1484 log_err("fio: client %s, timeout on cmd %s\n", client->hostname,
1485 fio_server_op(reply->opcode));
1486 flist_del(&reply->list);
1491 return flist_empty(&client->cmd_list) && ret;
1494 static int fio_check_clients_timed_out(void)
1496 struct fio_client *client;
1497 struct flist_head *entry, *tmp;
1501 fio_gettime(&tv, NULL);
1503 flist_for_each_safe(entry, tmp, &client_list) {
1504 client = flist_entry(entry, struct fio_client, list);
1506 if (flist_empty(&client->cmd_list))
1509 if (!client_check_cmd_timeout(client, &tv))
1512 if (client->ops->timed_out)
1513 client->ops->timed_out(client);
1515 log_err("fio: client %s timed out\n", client->hostname);
1517 remove_client(client);
1524 int fio_handle_clients(struct client_ops *ops)
1526 struct pollfd *pfds;
1527 int i, ret = 0, retval = 0;
1529 fio_gettime(&eta_tv, NULL);
1531 pfds = malloc(nr_clients * sizeof(struct pollfd));
1533 init_thread_stat(&client_ts);
1534 init_group_run_stat(&client_gs);
1536 while (!exit_backend && nr_clients) {
1537 struct flist_head *entry, *tmp;
1538 struct fio_client *client;
1541 flist_for_each_safe(entry, tmp, &client_list) {
1542 client = flist_entry(entry, struct fio_client, list);
1544 if (!client->sent_job && !client->ops->stay_connected &&
1545 flist_empty(&client->cmd_list)) {
1546 remove_client(client);
1550 pfds[i].fd = client->fd;
1551 pfds[i].events = POLLIN;
1558 assert(i == nr_clients);
1563 fio_gettime(&tv, NULL);
1564 if (mtime_since(&eta_tv, &tv) >= 900) {
1565 request_client_etas(ops);
1566 memcpy(&eta_tv, &tv, sizeof(tv));
1568 if (fio_check_clients_timed_out())
1572 ret = poll(pfds, nr_clients, ops->eta_msec);
1576 log_err("fio: poll clients: %s\n", strerror(errno));
1582 for (i = 0; i < nr_clients; i++) {
1583 if (!(pfds[i].revents & POLLIN))
1586 client = find_client_by_fd(pfds[i].fd);
1588 log_err("fio: unknown client fd %ld\n", (long) pfds[i].fd);
1591 if (!fio_handle_client(client)) {
1592 log_info("client: host=%s disconnected\n",
1594 remove_client(client);
1596 } else if (client->error)
1598 fio_put_client(client);
1602 fio_client_json_fini();