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);
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 void remove_client(struct fio_client *client)
189 assert(client->refs);
191 dprint(FD_NET, "client: removed <%s>\n", client->hostname);
193 if (!flist_empty(&client->list))
194 flist_del_init(&client->list);
196 fio_client_remove_hash(client);
198 if (!flist_empty(&client->eta_list)) {
199 flist_del_init(&client->eta_list);
200 fio_client_dec_jobs_eta(client->eta_in_flight, client->ops->eta);
206 if (client->ops->removed)
207 client->ops->removed(client);
210 fio_put_client(client);
213 struct fio_client *fio_get_client(struct fio_client *client)
219 static void __fio_client_add_cmd_option(struct fio_client *client,
224 index = client->argc++;
225 client->argv = realloc(client->argv, sizeof(char *) * client->argc);
226 client->argv[index] = strdup(opt);
227 dprint(FD_NET, "client: add cmd %d: %s\n", index, opt);
230 void fio_client_add_cmd_option(void *cookie, const char *opt)
232 struct fio_client *client = cookie;
233 struct flist_head *entry;
238 __fio_client_add_cmd_option(client, opt);
241 * Duplicate arguments to shared client group
243 flist_for_each(entry, &arg_list) {
244 client = flist_entry(entry, struct fio_client, arg_list);
246 __fio_client_add_cmd_option(client, opt);
250 struct fio_client *fio_client_add_explicit(struct client_ops *ops,
251 const char *hostname, int type,
254 struct fio_client *client;
256 client = malloc(sizeof(*client));
257 memset(client, 0, sizeof(*client));
259 INIT_FLIST_HEAD(&client->list);
260 INIT_FLIST_HEAD(&client->hash_list);
261 INIT_FLIST_HEAD(&client->arg_list);
262 INIT_FLIST_HEAD(&client->eta_list);
263 INIT_FLIST_HEAD(&client->cmd_list);
265 client->hostname = strdup(hostname);
267 if (type == Fio_client_socket)
272 ipv6 = type == Fio_client_ipv6;
273 if (fio_server_parse_host(hostname, ipv6,
274 &client->addr.sin_addr,
275 &client->addr6.sin6_addr))
284 client->type = ops->client_type;
286 __fio_client_add_cmd_option(client, "fio");
288 flist_add(&client->list, &client_list);
290 dprint(FD_NET, "client: added <%s>\n", client->hostname);
297 int fio_client_add_ini_file(void *cookie, const char *ini_file, int remote)
299 struct fio_client *client = cookie;
300 struct client_file *cf;
307 dprint(FD_NET, "client <%s>: add ini %s\n", client->hostname, ini_file);
309 new_size = (client->nr_files + 1) * sizeof(struct client_file);
310 new_files = realloc(client->files, new_size);
314 client->files = new_files;
315 cf = &client->files[client->nr_files];
316 cf->file = strdup(ini_file);
322 int fio_client_add(struct client_ops *ops, const char *hostname, void **cookie)
324 struct fio_client *existing = *cookie;
325 struct fio_client *client;
329 * We always add our "exec" name as the option, hence 1
332 if (existing->argc == 1)
333 flist_add_tail(&existing->arg_list, &arg_list);
335 while (!flist_empty(&arg_list))
336 flist_del_init(arg_list.next);
340 client = malloc(sizeof(*client));
341 memset(client, 0, sizeof(*client));
343 INIT_FLIST_HEAD(&client->list);
344 INIT_FLIST_HEAD(&client->hash_list);
345 INIT_FLIST_HEAD(&client->arg_list);
346 INIT_FLIST_HEAD(&client->eta_list);
347 INIT_FLIST_HEAD(&client->cmd_list);
349 if (fio_server_parse_string(hostname, &client->hostname,
350 &client->is_sock, &client->port,
351 &client->addr.sin_addr,
352 &client->addr6.sin6_addr,
359 client->type = ops->client_type;
361 __fio_client_add_cmd_option(client, "fio");
363 flist_add(&client->list, &client_list);
365 dprint(FD_NET, "client: added <%s>\n", client->hostname);
370 static const char *server_name(struct fio_client *client, char *buf,
376 from = inet_ntop(AF_INET6, (struct sockaddr *) &client->addr6.sin6_addr, buf, bufsize);
377 else if (client->is_sock)
380 from = inet_ntop(AF_INET, (struct sockaddr *) &client->addr.sin_addr, buf, bufsize);
385 static void probe_client(struct fio_client *client)
387 struct cmd_client_probe_pdu pdu;
392 dprint(FD_NET, "client: send probe\n");
395 pdu.flags = __le64_to_cpu(FIO_PROBE_FLAG_ZLIB);
400 sname = server_name(client, buf, sizeof(buf));
401 memset(pdu.server, 0, sizeof(pdu.server));
402 strncpy((char *) pdu.server, sname, sizeof(pdu.server) - 1);
404 fio_net_send_cmd(client->fd, FIO_NET_CMD_PROBE, &pdu, sizeof(pdu), &tag, &client->cmd_list);
407 static int fio_client_connect_ip(struct fio_client *client)
409 struct sockaddr *addr;
414 client->addr6.sin6_family = AF_INET6;
415 client->addr6.sin6_port = htons(client->port);
417 addr = (struct sockaddr *) &client->addr6;
418 socklen = sizeof(client->addr6);
420 client->addr.sin_family = AF_INET;
421 client->addr.sin_port = htons(client->port);
423 addr = (struct sockaddr *) &client->addr;
424 socklen = sizeof(client->addr);
427 fd = socket(domain, SOCK_STREAM, 0);
431 log_err("fio: socket: %s\n", strerror(errno));
435 if (connect(fd, addr, socklen) < 0) {
438 log_err("fio: connect: %s\n", strerror(errno));
439 log_err("fio: failed to connect to %s:%u\n", client->hostname,
448 static int fio_client_connect_sock(struct fio_client *client)
450 struct sockaddr_un *addr = &client->addr_un;
454 memset(addr, 0, sizeof(*addr));
455 addr->sun_family = AF_UNIX;
456 strncpy(addr->sun_path, client->hostname, sizeof(addr->sun_path) - 1);
458 fd = socket(AF_UNIX, SOCK_STREAM, 0);
462 log_err("fio: socket: %s\n", strerror(errno));
466 len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
467 if (connect(fd, (struct sockaddr *) addr, len) < 0) {
470 log_err("fio: connect; %s\n", strerror(errno));
478 int fio_client_connect(struct fio_client *client)
482 dprint(FD_NET, "client: connect to host %s\n", client->hostname);
485 fd = fio_client_connect_sock(client);
487 fd = fio_client_connect_ip(client);
489 dprint(FD_NET, "client: %s connected %d\n", client->hostname, fd);
495 fio_client_add_hash(client);
496 client->state = Client_connected;
498 probe_client(client);
502 int fio_client_terminate(struct fio_client *client)
504 return fio_net_send_quit(client->fd);
507 void fio_clients_terminate(void)
509 struct flist_head *entry;
510 struct fio_client *client;
512 dprint(FD_NET, "client: terminate clients\n");
514 flist_for_each(entry, &client_list) {
515 client = flist_entry(entry, struct fio_client, list);
516 fio_client_terminate(client);
520 static void sig_int(int sig)
522 dprint(FD_NET, "client: got signal %d\n", sig);
523 fio_clients_terminate();
526 static void sig_show_status(int sig)
528 show_running_run_stats();
531 static void client_signal_handler(void)
533 struct sigaction act;
535 memset(&act, 0, sizeof(act));
536 act.sa_handler = sig_int;
537 act.sa_flags = SA_RESTART;
538 sigaction(SIGINT, &act, NULL);
540 memset(&act, 0, sizeof(act));
541 act.sa_handler = sig_int;
542 act.sa_flags = SA_RESTART;
543 sigaction(SIGTERM, &act, NULL);
545 /* Windows uses SIGBREAK as a quit signal from other applications */
547 memset(&act, 0, sizeof(act));
548 act.sa_handler = sig_int;
549 act.sa_flags = SA_RESTART;
550 sigaction(SIGBREAK, &act, NULL);
553 memset(&act, 0, sizeof(act));
554 act.sa_handler = sig_show_status;
555 act.sa_flags = SA_RESTART;
556 sigaction(SIGUSR1, &act, NULL);
559 static int send_client_cmd_line(struct fio_client *client)
561 struct cmd_single_line_pdu *cslp;
562 struct cmd_line_pdu *clp;
563 unsigned long offset;
569 dprint(FD_NET, "client: send cmdline %d\n", client->argc);
571 lens = malloc(client->argc * sizeof(unsigned int));
574 * Find out how much mem we need
576 for (i = 0, mem = 0; i < client->argc; i++) {
577 lens[i] = strlen(client->argv[i]) + 1;
582 * We need one cmd_line_pdu, and argc number of cmd_single_line_pdu
584 mem += sizeof(*clp) + (client->argc * sizeof(*cslp));
588 offset = sizeof(*clp);
590 for (i = 0; i < client->argc; i++) {
591 uint16_t arg_len = lens[i];
594 strcpy((char *) cslp->text, client->argv[i]);
595 cslp->len = cpu_to_le16(arg_len);
596 offset += sizeof(*cslp) + arg_len;
600 clp->lines = cpu_to_le16(client->argc);
601 clp->client_type = __cpu_to_le16(client->type);
602 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOBLINE, pdu, mem, NULL, NULL);
607 int fio_clients_connect(void)
609 struct fio_client *client;
610 struct flist_head *entry, *tmp;
615 WSAStartup(MAKEWORD(2, 2), &wsd);
618 dprint(FD_NET, "client: connect all\n");
620 client_signal_handler();
622 flist_for_each_safe(entry, tmp, &client_list) {
623 client = flist_entry(entry, struct fio_client, list);
625 ret = fio_client_connect(client);
627 remove_client(client);
631 if (client->argc > 1)
632 send_client_cmd_line(client);
638 int fio_start_client(struct fio_client *client)
640 dprint(FD_NET, "client: start %s\n", client->hostname);
641 return fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_RUN, 0, NULL);
644 int fio_start_all_clients(void)
646 struct fio_client *client;
647 struct flist_head *entry, *tmp;
650 dprint(FD_NET, "client: start all\n");
652 fio_client_json_init();
654 flist_for_each_safe(entry, tmp, &client_list) {
655 client = flist_entry(entry, struct fio_client, list);
657 ret = fio_start_client(client);
659 remove_client(client);
664 return flist_empty(&client_list);
667 static int __fio_client_send_remote_ini(struct fio_client *client,
668 const char *filename)
670 struct cmd_load_file_pdu *pdu;
674 dprint(FD_NET, "send remote ini %s to %s\n", filename, client->hostname);
676 p_size = sizeof(*pdu) + strlen(filename) + 1;
677 pdu = malloc(p_size);
678 memset(pdu, 0, p_size);
679 pdu->name_len = strlen(filename);
680 strcpy((char *) pdu->file, filename);
681 pdu->client_type = cpu_to_le16((uint16_t) client->type);
683 client->sent_job = 1;
684 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_LOAD_FILE, pdu, p_size,NULL, NULL);
690 * Send file contents to server backend. We could use sendfile(), but to remain
691 * more portable lets just read/write the darn thing.
693 static int __fio_client_send_local_ini(struct fio_client *client,
694 const char *filename)
696 struct cmd_job_pdu *pdu;
704 dprint(FD_NET, "send ini %s to %s\n", filename, client->hostname);
706 fd = open(filename, O_RDONLY);
709 log_err("fio: job file <%s> open: %s\n", filename, strerror(errno));
713 if (fstat(fd, &sb) < 0) {
715 log_err("fio: job file stat: %s\n", strerror(errno));
720 p_size = sb.st_size + sizeof(*pdu);
721 pdu = malloc(p_size);
726 if (read_data(fd, p, len)) {
727 log_err("fio: failed reading job file %s\n", filename);
733 pdu->buf_len = __cpu_to_le32(sb.st_size);
734 pdu->client_type = cpu_to_le32(client->type);
736 client->sent_job = 1;
737 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, pdu, p_size, NULL, NULL);
743 int fio_client_send_ini(struct fio_client *client, const char *filename,
749 ret = __fio_client_send_local_ini(client, filename);
751 ret = __fio_client_send_remote_ini(client, filename);
754 client->sent_job = 1;
759 static int fio_client_send_cf(struct fio_client *client,
760 struct client_file *cf)
762 return fio_client_send_ini(client, cf->file, cf->remote);
765 int fio_clients_send_ini(const char *filename)
767 struct fio_client *client;
768 struct flist_head *entry, *tmp;
770 flist_for_each_safe(entry, tmp, &client_list) {
771 client = flist_entry(entry, struct fio_client, list);
773 if (client->nr_files) {
776 for (i = 0; i < client->nr_files; i++) {
777 struct client_file *cf;
779 cf = &client->files[i];
781 if (fio_client_send_cf(client, cf)) {
782 remove_client(client);
787 if (client->sent_job)
789 if (!filename || fio_client_send_ini(client, filename, 0))
790 remove_client(client);
796 int fio_client_update_options(struct fio_client *client,
797 struct thread_options *o, uint64_t *tag)
799 struct cmd_add_job_pdu pdu;
801 pdu.thread_number = cpu_to_le32(client->thread_number);
802 pdu.groupid = cpu_to_le32(client->groupid);
803 convert_thread_options_to_net(&pdu.top, o);
805 return fio_net_send_cmd(client->fd, FIO_NET_CMD_UPDATE_JOB, &pdu, sizeof(pdu), tag, &client->cmd_list);
808 static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
810 dst->max_val = le64_to_cpu(src->max_val);
811 dst->min_val = le64_to_cpu(src->min_val);
812 dst->samples = le64_to_cpu(src->samples);
815 * Floats arrive as IEEE 754 encoded uint64_t, convert back to double
817 dst->mean.u.f = fio_uint64_to_double(le64_to_cpu(dst->mean.u.i));
818 dst->S.u.f = fio_uint64_to_double(le64_to_cpu(dst->S.u.i));
821 static void convert_ts(struct thread_stat *dst, struct thread_stat *src)
825 dst->error = le32_to_cpu(src->error);
826 dst->thread_number = le32_to_cpu(src->thread_number);
827 dst->groupid = le32_to_cpu(src->groupid);
828 dst->pid = le32_to_cpu(src->pid);
829 dst->members = le32_to_cpu(src->members);
830 dst->unified_rw_rep = le32_to_cpu(src->unified_rw_rep);
832 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
833 convert_io_stat(&dst->clat_stat[i], &src->clat_stat[i]);
834 convert_io_stat(&dst->slat_stat[i], &src->slat_stat[i]);
835 convert_io_stat(&dst->lat_stat[i], &src->lat_stat[i]);
836 convert_io_stat(&dst->bw_stat[i], &src->bw_stat[i]);
839 dst->usr_time = le64_to_cpu(src->usr_time);
840 dst->sys_time = le64_to_cpu(src->sys_time);
841 dst->ctx = le64_to_cpu(src->ctx);
842 dst->minf = le64_to_cpu(src->minf);
843 dst->majf = le64_to_cpu(src->majf);
844 dst->clat_percentiles = le64_to_cpu(src->clat_percentiles);
845 dst->percentile_precision = le64_to_cpu(src->percentile_precision);
847 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
848 fio_fp64_t *fps = &src->percentile_list[i];
849 fio_fp64_t *fpd = &dst->percentile_list[i];
851 fpd->u.f = fio_uint64_to_double(le64_to_cpu(fps->u.i));
854 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
855 dst->io_u_map[i] = le32_to_cpu(src->io_u_map[i]);
856 dst->io_u_submit[i] = le32_to_cpu(src->io_u_submit[i]);
857 dst->io_u_complete[i] = le32_to_cpu(src->io_u_complete[i]);
860 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
861 dst->io_u_lat_u[i] = le32_to_cpu(src->io_u_lat_u[i]);
862 dst->io_u_lat_m[i] = le32_to_cpu(src->io_u_lat_m[i]);
865 for (i = 0; i < DDIR_RWDIR_CNT; i++)
866 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
867 dst->io_u_plat[i][j] = le32_to_cpu(src->io_u_plat[i][j]);
869 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
870 dst->total_io_u[i] = le64_to_cpu(src->total_io_u[i]);
871 dst->short_io_u[i] = le64_to_cpu(src->short_io_u[i]);
872 dst->drop_io_u[i] = le64_to_cpu(src->drop_io_u[i]);
875 dst->total_submit = le64_to_cpu(src->total_submit);
876 dst->total_complete = le64_to_cpu(src->total_complete);
878 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
879 dst->io_bytes[i] = le64_to_cpu(src->io_bytes[i]);
880 dst->runtime[i] = le64_to_cpu(src->runtime[i]);
883 dst->total_run_time = le64_to_cpu(src->total_run_time);
884 dst->continue_on_error = le16_to_cpu(src->continue_on_error);
885 dst->total_err_count = le64_to_cpu(src->total_err_count);
886 dst->first_error = le32_to_cpu(src->first_error);
887 dst->kb_base = le32_to_cpu(src->kb_base);
888 dst->unit_base = le32_to_cpu(src->unit_base);
890 dst->latency_depth = le32_to_cpu(src->latency_depth);
891 dst->latency_target = le64_to_cpu(src->latency_target);
892 dst->latency_window = le64_to_cpu(src->latency_window);
893 dst->latency_percentile.u.f = fio_uint64_to_double(le64_to_cpu(src->latency_percentile.u.i));
896 static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
900 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
901 dst->max_run[i] = le64_to_cpu(src->max_run[i]);
902 dst->min_run[i] = le64_to_cpu(src->min_run[i]);
903 dst->max_bw[i] = le64_to_cpu(src->max_bw[i]);
904 dst->min_bw[i] = le64_to_cpu(src->min_bw[i]);
905 dst->io_kb[i] = le64_to_cpu(src->io_kb[i]);
906 dst->agg[i] = le64_to_cpu(src->agg[i]);
909 dst->kb_base = le32_to_cpu(src->kb_base);
910 dst->unit_base = le32_to_cpu(src->unit_base);
911 dst->groupid = le32_to_cpu(src->groupid);
912 dst->unified_rw_rep = le32_to_cpu(src->unified_rw_rep);
915 static void json_object_add_client_info(struct json_object *obj,
916 struct fio_client *client)
918 const char *hostname = client->hostname ? client->hostname : "";
920 json_object_add_value_string(obj, "hostname", hostname);
921 json_object_add_value_int(obj, "port", client->port);
924 static void handle_ts(struct fio_client *client, struct fio_net_cmd *cmd)
926 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
927 struct json_object *tsobj;
929 tsobj = show_thread_status(&p->ts, &p->rs);
930 client->did_stat = 1;
932 json_object_add_client_info(tsobj, client);
933 json_array_add_value_object(clients_array, tsobj);
936 if (sum_stat_clients <= 1)
939 sum_thread_stats(&client_ts, &p->ts, sum_stat_nr);
940 sum_group_stats(&client_gs, &p->rs);
943 client_ts.thread_number = p->ts.thread_number;
944 client_ts.groupid = p->ts.groupid;
945 client_ts.unified_rw_rep = p->ts.unified_rw_rep;
947 if (++sum_stat_nr == sum_stat_clients) {
948 strcpy(client_ts.name, "All clients");
949 tsobj = show_thread_status(&client_ts, &client_gs);
951 json_object_add_client_info(tsobj, client);
952 json_array_add_value_object(clients_array, tsobj);
957 static void handle_gs(struct fio_client *client, struct fio_net_cmd *cmd)
959 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
961 show_group_stats(gs);
964 static void handle_text(struct fio_client *client, struct fio_net_cmd *cmd)
966 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
967 const char *buf = (const char *) pdu->buf;
971 name = client->name ? client->name : client->hostname;
973 if (!client->skip_newline)
974 fprintf(f_out, "<%s> ", name);
975 ret = fwrite(buf, pdu->buf_len, 1, f_out);
977 client->skip_newline = strchr(buf, '\n') == NULL;
980 static void convert_agg(struct disk_util_agg *agg)
984 for (i = 0; i < 2; i++) {
985 agg->ios[i] = le64_to_cpu(agg->ios[i]);
986 agg->merges[i] = le64_to_cpu(agg->merges[i]);
987 agg->sectors[i] = le64_to_cpu(agg->sectors[i]);
988 agg->ticks[i] = le64_to_cpu(agg->ticks[i]);
991 agg->io_ticks = le64_to_cpu(agg->io_ticks);
992 agg->time_in_queue = le64_to_cpu(agg->time_in_queue);
993 agg->slavecount = le32_to_cpu(agg->slavecount);
994 agg->max_util.u.f = fio_uint64_to_double(le64_to_cpu(agg->max_util.u.i));
997 static void convert_dus(struct disk_util_stat *dus)
1001 for (i = 0; i < 2; i++) {
1002 dus->s.ios[i] = le64_to_cpu(dus->s.ios[i]);
1003 dus->s.merges[i] = le64_to_cpu(dus->s.merges[i]);
1004 dus->s.sectors[i] = le64_to_cpu(dus->s.sectors[i]);
1005 dus->s.ticks[i] = le64_to_cpu(dus->s.ticks[i]);
1008 dus->s.io_ticks = le64_to_cpu(dus->s.io_ticks);
1009 dus->s.time_in_queue = le64_to_cpu(dus->s.time_in_queue);
1010 dus->s.msec = le64_to_cpu(dus->s.msec);
1013 static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd)
1015 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
1017 if (!client->disk_stats_shown) {
1018 client->disk_stats_shown = 1;
1019 log_info("\nDisk stats (read/write):\n");
1022 if (output_format == FIO_OUTPUT_JSON) {
1023 struct json_object *duobj;
1024 json_array_add_disk_util(&du->dus, &du->agg, du_array);
1025 duobj = json_array_last_value_object(du_array);
1026 json_object_add_client_info(duobj, client);
1028 print_disk_util(&du->dus, &du->agg, output_format == FIO_OUTPUT_TERSE);
1031 static void convert_jobs_eta(struct jobs_eta *je)
1035 je->nr_running = le32_to_cpu(je->nr_running);
1036 je->nr_ramp = le32_to_cpu(je->nr_ramp);
1037 je->nr_pending = le32_to_cpu(je->nr_pending);
1038 je->nr_setting_up = le32_to_cpu(je->nr_setting_up);
1039 je->files_open = le32_to_cpu(je->files_open);
1041 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1042 je->m_rate[i] = le32_to_cpu(je->m_rate[i]);
1043 je->t_rate[i] = le32_to_cpu(je->t_rate[i]);
1044 je->m_iops[i] = le32_to_cpu(je->m_iops[i]);
1045 je->t_iops[i] = le32_to_cpu(je->t_iops[i]);
1046 je->rate[i] = le32_to_cpu(je->rate[i]);
1047 je->iops[i] = le32_to_cpu(je->iops[i]);
1050 je->elapsed_sec = le64_to_cpu(je->elapsed_sec);
1051 je->eta_sec = le64_to_cpu(je->eta_sec);
1052 je->nr_threads = le32_to_cpu(je->nr_threads);
1053 je->is_pow2 = le32_to_cpu(je->is_pow2);
1054 je->unit_base = le32_to_cpu(je->unit_base);
1057 void fio_client_sum_jobs_eta(struct jobs_eta *dst, struct jobs_eta *je)
1061 dst->nr_running += je->nr_running;
1062 dst->nr_ramp += je->nr_ramp;
1063 dst->nr_pending += je->nr_pending;
1064 dst->nr_setting_up += je->nr_setting_up;
1065 dst->files_open += je->files_open;
1067 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1068 dst->m_rate[i] += je->m_rate[i];
1069 dst->t_rate[i] += je->t_rate[i];
1070 dst->m_iops[i] += je->m_iops[i];
1071 dst->t_iops[i] += je->t_iops[i];
1072 dst->rate[i] += je->rate[i];
1073 dst->iops[i] += je->iops[i];
1076 dst->elapsed_sec += je->elapsed_sec;
1078 if (je->eta_sec > dst->eta_sec)
1079 dst->eta_sec = je->eta_sec;
1081 dst->nr_threads += je->nr_threads;
1084 * This wont be correct for multiple strings, but at least it
1085 * works for the basic cases.
1087 strcpy((char *) dst->run_str, (char *) je->run_str);
1090 void fio_client_dec_jobs_eta(struct client_eta *eta, client_eta_op eta_fn)
1092 if (!--eta->pending) {
1098 static void remove_reply_cmd(struct fio_client *client, struct fio_net_cmd *cmd)
1100 struct fio_net_cmd_reply *reply = NULL;
1101 struct flist_head *entry;
1103 flist_for_each(entry, &client->cmd_list) {
1104 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
1106 if (cmd->tag == (uintptr_t) reply)
1113 log_err("fio: client: unable to find matching tag (%llx)\n", (unsigned long long) cmd->tag);
1117 flist_del(&reply->list);
1118 cmd->tag = reply->saved_tag;
1122 int fio_client_wait_for_reply(struct fio_client *client, uint64_t tag)
1125 struct fio_net_cmd_reply *reply = NULL;
1126 struct flist_head *entry;
1128 flist_for_each(entry, &client->cmd_list) {
1129 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
1131 if (tag == (uintptr_t) reply)
1146 static void handle_eta(struct fio_client *client, struct fio_net_cmd *cmd)
1148 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
1149 struct client_eta *eta = (struct client_eta *) (uintptr_t) cmd->tag;
1151 dprint(FD_NET, "client: got eta tag %p, %d\n", eta, eta->pending);
1153 assert(client->eta_in_flight == eta);
1155 client->eta_in_flight = NULL;
1156 flist_del_init(&client->eta_list);
1158 if (client->ops->jobs_eta)
1159 client->ops->jobs_eta(client, je);
1161 fio_client_sum_jobs_eta(&eta->eta, je);
1162 fio_client_dec_jobs_eta(eta, client->ops->eta);
1165 static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd)
1167 struct cmd_probe_reply_pdu *probe = (struct cmd_probe_reply_pdu *) cmd->payload;
1168 const char *os, *arch;
1171 os = fio_get_os_string(probe->os);
1175 arch = fio_get_arch_string(probe->arch);
1179 sprintf(bit, "%d-bit", probe->bpp * 8);
1180 probe->flags = le64_to_cpu(probe->flags);
1182 log_info("hostname=%s, be=%u, %s, os=%s, arch=%s, fio=%s, flags=%lx\n",
1183 probe->hostname, probe->bigendian, bit, os, arch,
1184 probe->fio_version, (unsigned long) probe->flags);
1187 client->name = strdup((char *) probe->hostname);
1190 static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd)
1192 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
1194 client->state = Client_started;
1195 client->jobs = le32_to_cpu(pdu->jobs);
1196 client->nr_stat = le32_to_cpu(pdu->stat_outputs);
1198 sum_stat_clients += client->nr_stat;
1201 static void handle_stop(struct fio_client *client, struct fio_net_cmd *cmd)
1204 log_info("client <%s>: exited with error %d\n", client->hostname, client->error);
1207 static void convert_stop(struct fio_net_cmd *cmd)
1209 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
1211 pdu->error = le32_to_cpu(pdu->error);
1214 static void convert_text(struct fio_net_cmd *cmd)
1216 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
1218 pdu->level = le32_to_cpu(pdu->level);
1219 pdu->buf_len = le32_to_cpu(pdu->buf_len);
1220 pdu->log_sec = le64_to_cpu(pdu->log_sec);
1221 pdu->log_usec = le64_to_cpu(pdu->log_usec);
1224 static struct cmd_iolog_pdu *convert_iolog_gz(struct fio_net_cmd *cmd,
1225 struct cmd_iolog_pdu *pdu)
1228 struct cmd_iolog_pdu *ret;
1230 uint32_t nr_samples;
1234 stream.zalloc = Z_NULL;
1235 stream.zfree = Z_NULL;
1236 stream.opaque = Z_NULL;
1237 stream.avail_in = 0;
1238 stream.next_in = Z_NULL;
1240 if (inflateInit(&stream) != Z_OK)
1244 * Get header first, it's not compressed
1246 nr_samples = le64_to_cpu(pdu->nr_samples);
1248 total = nr_samples * __log_entry_sz(le32_to_cpu(pdu->log_offset));
1249 ret = malloc(total + sizeof(*pdu));
1250 ret->nr_samples = nr_samples;
1252 memcpy(ret, pdu, sizeof(*pdu));
1254 p = (void *) ret + sizeof(*pdu);
1256 stream.avail_in = cmd->pdu_len - sizeof(*pdu);
1257 stream.next_in = (void *) pdu + sizeof(*pdu);
1258 while (stream.avail_in) {
1259 unsigned int this_chunk = 65536;
1260 unsigned int this_len;
1263 if (this_chunk > total)
1266 stream.avail_out = this_chunk;
1267 stream.next_out = p;
1268 err = inflate(&stream, Z_NO_FLUSH);
1269 /* may be Z_OK, or Z_STREAM_END */
1271 log_err("fio: inflate error %d\n", err);
1277 this_len = this_chunk - stream.avail_out;
1283 inflateEnd(&stream);
1291 * This has been compressed on the server side, since it can be big.
1294 static struct cmd_iolog_pdu *convert_iolog(struct fio_net_cmd *cmd)
1296 struct cmd_iolog_pdu *pdu = (struct cmd_iolog_pdu *) cmd->payload;
1297 struct cmd_iolog_pdu *ret;
1302 * Convert if compressed and we support it. If it's not
1303 * compressed, we need not do anything.
1305 if (le32_to_cpu(pdu->compressed)) {
1307 log_err("fio: server sent compressed data by mistake\n");
1310 ret = convert_iolog_gz(cmd, pdu);
1312 log_err("fio: failed decompressing log\n");
1318 ret->nr_samples = le64_to_cpu(ret->nr_samples);
1319 ret->thread_number = le32_to_cpu(ret->thread_number);
1320 ret->log_type = le32_to_cpu(ret->log_type);
1321 ret->compressed = le32_to_cpu(ret->compressed);
1322 ret->log_offset = le32_to_cpu(ret->log_offset);
1324 samples = &ret->samples[0];
1325 for (i = 0; i < ret->nr_samples; i++) {
1326 struct io_sample *s;
1328 s = __get_sample(samples, ret->log_offset, i);
1329 s->time = le64_to_cpu(s->time);
1330 s->val = le64_to_cpu(s->val);
1331 s->__ddir = le32_to_cpu(s->__ddir);
1332 s->bs = le32_to_cpu(s->bs);
1334 if (ret->log_offset) {
1335 struct io_sample_offset *so = (void *) s;
1337 so->offset = le64_to_cpu(so->offset);
1344 static void sendfile_reply(int fd, struct cmd_sendfile_reply *rep,
1345 size_t size, uint64_t tag)
1347 rep->error = cpu_to_le32(rep->error);
1348 fio_net_send_cmd(fd, FIO_NET_CMD_SENDFILE, rep, size, &tag, NULL);
1351 static int send_file(struct fio_client *client, struct cmd_sendfile *pdu,
1354 struct cmd_sendfile_reply *rep;
1359 size = sizeof(*rep);
1362 if (stat((char *)pdu->path, &sb) < 0) {
1365 sendfile_reply(client->fd, rep, size, tag);
1371 rep = realloc(rep, size);
1372 rep->size = cpu_to_le32((uint32_t) sb.st_size);
1374 fd = open((char *)pdu->path, O_RDONLY);
1378 rep->error = read_data(fd, &rep->data, sb.st_size);
1379 sendfile_reply(client->fd, rep, size, tag);
1385 int fio_handle_client(struct fio_client *client)
1387 struct client_ops *ops = client->ops;
1388 struct fio_net_cmd *cmd;
1390 dprint(FD_NET, "client: handle %s\n", client->hostname);
1392 cmd = fio_net_recv_cmd(client->fd);
1396 dprint(FD_NET, "client: got cmd op %s from %s (pdu=%u)\n",
1397 fio_server_op(cmd->opcode), client->hostname, cmd->pdu_len);
1399 switch (cmd->opcode) {
1400 case FIO_NET_CMD_QUIT:
1402 ops->quit(client, cmd);
1403 remove_client(client);
1405 case FIO_NET_CMD_TEXT:
1407 ops->text(client, cmd);
1409 case FIO_NET_CMD_DU: {
1410 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
1412 convert_dus(&du->dus);
1413 convert_agg(&du->agg);
1415 ops->disk_util(client, cmd);
1418 case FIO_NET_CMD_TS: {
1419 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
1421 convert_ts(&p->ts, &p->ts);
1422 convert_gs(&p->rs, &p->rs);
1424 ops->thread_status(client, cmd);
1427 case FIO_NET_CMD_GS: {
1428 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
1432 ops->group_stats(client, cmd);
1435 case FIO_NET_CMD_ETA: {
1436 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
1438 remove_reply_cmd(client, cmd);
1439 convert_jobs_eta(je);
1440 handle_eta(client, cmd);
1443 case FIO_NET_CMD_PROBE:
1444 remove_reply_cmd(client, cmd);
1445 ops->probe(client, cmd);
1447 case FIO_NET_CMD_SERVER_START:
1448 client->state = Client_running;
1450 ops->job_start(client, cmd);
1452 case FIO_NET_CMD_START: {
1453 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
1455 pdu->jobs = le32_to_cpu(pdu->jobs);
1456 ops->start(client, cmd);
1459 case FIO_NET_CMD_STOP: {
1460 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
1463 client->state = Client_stopped;
1464 client->error = le32_to_cpu(pdu->error);
1465 client->signal = le32_to_cpu(pdu->signal);
1466 ops->stop(client, cmd);
1469 case FIO_NET_CMD_ADD_JOB: {
1470 struct cmd_add_job_pdu *pdu = (struct cmd_add_job_pdu *) cmd->payload;
1472 client->thread_number = le32_to_cpu(pdu->thread_number);
1473 client->groupid = le32_to_cpu(pdu->groupid);
1476 ops->add_job(client, cmd);
1479 case FIO_NET_CMD_IOLOG:
1481 struct cmd_iolog_pdu *pdu;
1483 pdu = convert_iolog(cmd);
1484 ops->iolog(client, pdu);
1487 case FIO_NET_CMD_UPDATE_JOB:
1488 ops->update_job(client, cmd);
1489 remove_reply_cmd(client, cmd);
1491 case FIO_NET_CMD_VTRIGGER: {
1492 struct all_io_list *pdu = (struct all_io_list *) cmd->payload;
1495 __verify_save_state(pdu, server_name(client, buf, sizeof(buf)));
1496 exec_trigger(trigger_cmd);
1499 case FIO_NET_CMD_SENDFILE: {
1500 struct cmd_sendfile *pdu = (struct cmd_sendfile *) cmd->payload;
1501 send_file(client, pdu, cmd->tag);
1505 log_err("fio: unknown client op: %s\n", fio_server_op(cmd->opcode));
1513 int fio_clients_send_trigger(const char *cmd)
1515 struct flist_head *entry;
1516 struct fio_client *client;
1519 dprint(FD_NET, "client: send vtrigger: %s\n", cmd);
1526 flist_for_each(entry, &client_list) {
1527 struct cmd_vtrigger_pdu *pdu;
1529 client = flist_entry(entry, struct fio_client, list);
1531 pdu = malloc(sizeof(*pdu) + slen);
1532 pdu->len = cpu_to_le16((uint16_t) slen);
1534 memcpy(pdu->cmd, cmd, slen);
1535 fio_net_send_cmd(client->fd, FIO_NET_CMD_VTRIGGER, pdu,
1536 sizeof(*pdu) + slen, NULL, NULL);
1543 static void request_client_etas(struct client_ops *ops)
1545 struct fio_client *client;
1546 struct flist_head *entry;
1547 struct client_eta *eta;
1550 dprint(FD_NET, "client: request eta (%d)\n", nr_clients);
1552 eta = calloc(1, sizeof(*eta) + __THREAD_RUNSTR_SZ(REAL_MAX_JOBS));
1553 eta->pending = nr_clients;
1555 flist_for_each(entry, &client_list) {
1556 client = flist_entry(entry, struct fio_client, list);
1558 if (!flist_empty(&client->eta_list)) {
1562 if (client->state != Client_running)
1565 assert(!client->eta_in_flight);
1566 flist_add_tail(&client->eta_list, &eta_list);
1567 client->eta_in_flight = eta;
1568 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_SEND_ETA,
1569 (uintptr_t) eta, &client->cmd_list);
1573 fio_client_dec_jobs_eta(eta, ops->eta);
1575 dprint(FD_NET, "client: requested eta tag %p\n", eta);
1578 static int client_check_cmd_timeout(struct fio_client *client,
1579 struct timeval *now)
1581 struct fio_net_cmd_reply *reply;
1582 struct flist_head *entry, *tmp;
1585 flist_for_each_safe(entry, tmp, &client->cmd_list) {
1586 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
1588 if (mtime_since(&reply->tv, now) < FIO_NET_CLIENT_TIMEOUT)
1591 log_err("fio: client %s, timeout on cmd %s\n", client->hostname,
1592 fio_server_op(reply->opcode));
1593 flist_del(&reply->list);
1598 return flist_empty(&client->cmd_list) && ret;
1601 static int fio_check_clients_timed_out(void)
1603 struct fio_client *client;
1604 struct flist_head *entry, *tmp;
1608 fio_gettime(&tv, NULL);
1610 flist_for_each_safe(entry, tmp, &client_list) {
1611 client = flist_entry(entry, struct fio_client, list);
1613 if (flist_empty(&client->cmd_list))
1616 if (!client_check_cmd_timeout(client, &tv))
1619 if (client->ops->timed_out)
1620 client->ops->timed_out(client);
1622 log_err("fio: client %s timed out\n", client->hostname);
1624 client->error = ETIMEDOUT;
1625 remove_client(client);
1632 int fio_handle_clients(struct client_ops *ops)
1634 struct pollfd *pfds;
1635 int i, ret = 0, retval = 0;
1637 fio_gettime(&eta_tv, NULL);
1639 pfds = malloc(nr_clients * sizeof(struct pollfd));
1641 init_thread_stat(&client_ts);
1642 init_group_run_stat(&client_gs);
1644 while (!exit_backend && nr_clients) {
1645 struct flist_head *entry, *tmp;
1646 struct fio_client *client;
1649 flist_for_each_safe(entry, tmp, &client_list) {
1650 client = flist_entry(entry, struct fio_client, list);
1652 if (!client->sent_job && !client->ops->stay_connected &&
1653 flist_empty(&client->cmd_list)) {
1654 remove_client(client);
1658 pfds[i].fd = client->fd;
1659 pfds[i].events = POLLIN;
1666 assert(i == nr_clients);
1672 fio_gettime(&tv, NULL);
1673 if (mtime_since(&eta_tv, &tv) >= 900) {
1674 request_client_etas(ops);
1675 memcpy(&eta_tv, &tv, sizeof(tv));
1677 if (fio_check_clients_timed_out())
1681 check_trigger_file();
1683 timeout = min(100u, ops->eta_msec);
1685 ret = poll(pfds, nr_clients, timeout);
1689 log_err("fio: poll clients: %s\n", strerror(errno));
1695 for (i = 0; i < nr_clients; i++) {
1696 if (!(pfds[i].revents & POLLIN))
1699 client = find_client_by_fd(pfds[i].fd);
1701 log_err("fio: unknown client fd %ld\n", (long) pfds[i].fd);
1704 if (!fio_handle_client(client)) {
1705 log_info("client: host=%s disconnected\n",
1707 remove_client(client);
1709 } else if (client->error)
1711 fio_put_client(client);
1715 fio_client_json_fini();
1718 return retval || error_clients;