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 #define FIO_CLIENT_HASH_BITS 7
66 #define FIO_CLIENT_HASH_SZ (1 << FIO_CLIENT_HASH_BITS)
67 #define FIO_CLIENT_HASH_MASK (FIO_CLIENT_HASH_SZ - 1)
68 static struct flist_head client_hash[FIO_CLIENT_HASH_SZ];
70 static void fio_client_add_hash(struct fio_client *client)
72 int bucket = hash_long(client->fd, FIO_CLIENT_HASH_BITS);
74 bucket &= FIO_CLIENT_HASH_MASK;
75 flist_add(&client->hash_list, &client_hash[bucket]);
78 static void fio_client_remove_hash(struct fio_client *client)
80 if (!flist_empty(&client->hash_list))
81 flist_del_init(&client->hash_list);
84 static void fio_init fio_client_hash_init(void)
88 for (i = 0; i < FIO_CLIENT_HASH_SZ; i++)
89 INIT_FLIST_HEAD(&client_hash[i]);
92 static int read_data(int fd, void *data, size_t size)
97 ret = read(fd, data, size);
99 if (errno == EAGAIN || errno == EINTR)
116 static void fio_client_json_init(void)
118 if (output_format != FIO_OUTPUT_JSON)
120 root = json_create_object();
121 json_object_add_value_string(root, "fio version", fio_version_string);
122 clients_array = json_create_array();
123 json_object_add_value_array(root, "client_stats", clients_array);
124 du_array = json_create_array();
125 json_object_add_value_array(root, "disk_util", du_array);
128 static void fio_client_json_fini(void)
130 if (output_format != FIO_OUTPUT_JSON)
132 json_print_object(root);
134 json_free_object(root);
136 clients_array = NULL;
140 static struct fio_client *find_client_by_fd(int fd)
142 int bucket = hash_long(fd, FIO_CLIENT_HASH_BITS) & FIO_CLIENT_HASH_MASK;
143 struct fio_client *client;
144 struct flist_head *entry;
146 flist_for_each(entry, &client_hash[bucket]) {
147 client = flist_entry(entry, struct fio_client, hash_list);
149 if (client->fd == fd) {
158 void fio_put_client(struct fio_client *client)
163 free(client->hostname);
168 while (client->nr_files) {
169 struct client_file *cf = &client->files[--client->nr_files];
176 if (!client->did_stat)
182 static void remove_client(struct fio_client *client)
184 assert(client->refs);
186 dprint(FD_NET, "client: removed <%s>\n", client->hostname);
188 if (!flist_empty(&client->list))
189 flist_del_init(&client->list);
191 fio_client_remove_hash(client);
193 if (!flist_empty(&client->eta_list)) {
194 flist_del_init(&client->eta_list);
195 fio_client_dec_jobs_eta(client->eta_in_flight, client->ops->eta);
201 if (client->ops->removed)
202 client->ops->removed(client);
205 fio_put_client(client);
208 struct fio_client *fio_get_client(struct fio_client *client)
214 static void __fio_client_add_cmd_option(struct fio_client *client,
219 index = client->argc++;
220 client->argv = realloc(client->argv, sizeof(char *) * client->argc);
221 client->argv[index] = strdup(opt);
222 dprint(FD_NET, "client: add cmd %d: %s\n", index, opt);
225 void fio_client_add_cmd_option(void *cookie, const char *opt)
227 struct fio_client *client = cookie;
228 struct flist_head *entry;
233 __fio_client_add_cmd_option(client, opt);
236 * Duplicate arguments to shared client group
238 flist_for_each(entry, &arg_list) {
239 client = flist_entry(entry, struct fio_client, arg_list);
241 __fio_client_add_cmd_option(client, opt);
245 struct fio_client *fio_client_add_explicit(struct client_ops *ops,
246 const char *hostname, int type,
249 struct fio_client *client;
251 client = malloc(sizeof(*client));
252 memset(client, 0, sizeof(*client));
254 INIT_FLIST_HEAD(&client->list);
255 INIT_FLIST_HEAD(&client->hash_list);
256 INIT_FLIST_HEAD(&client->arg_list);
257 INIT_FLIST_HEAD(&client->eta_list);
258 INIT_FLIST_HEAD(&client->cmd_list);
260 client->hostname = strdup(hostname);
262 if (type == Fio_client_socket)
267 ipv6 = type == Fio_client_ipv6;
268 if (fio_server_parse_host(hostname, ipv6,
269 &client->addr.sin_addr,
270 &client->addr6.sin6_addr))
279 client->type = ops->client_type;
281 __fio_client_add_cmd_option(client, "fio");
283 flist_add(&client->list, &client_list);
285 dprint(FD_NET, "client: added <%s>\n", client->hostname);
292 int fio_client_add_ini_file(void *cookie, const char *ini_file, int remote)
294 struct fio_client *client = cookie;
295 struct client_file *cf;
302 dprint(FD_NET, "client <%s>: add ini %s\n", client->hostname, ini_file);
304 new_size = (client->nr_files + 1) * sizeof(struct client_file);
305 new_files = realloc(client->files, new_size);
309 client->files = new_files;
310 cf = &client->files[client->nr_files];
311 cf->file = strdup(ini_file);
317 int fio_client_add(struct client_ops *ops, const char *hostname, void **cookie)
319 struct fio_client *existing = *cookie;
320 struct fio_client *client;
324 * We always add our "exec" name as the option, hence 1
327 if (existing->argc == 1)
328 flist_add_tail(&existing->arg_list, &arg_list);
330 while (!flist_empty(&arg_list))
331 flist_del_init(arg_list.next);
335 client = malloc(sizeof(*client));
336 memset(client, 0, sizeof(*client));
338 INIT_FLIST_HEAD(&client->list);
339 INIT_FLIST_HEAD(&client->hash_list);
340 INIT_FLIST_HEAD(&client->arg_list);
341 INIT_FLIST_HEAD(&client->eta_list);
342 INIT_FLIST_HEAD(&client->cmd_list);
344 if (fio_server_parse_string(hostname, &client->hostname,
345 &client->is_sock, &client->port,
346 &client->addr.sin_addr,
347 &client->addr6.sin6_addr,
354 client->type = ops->client_type;
356 __fio_client_add_cmd_option(client, "fio");
358 flist_add(&client->list, &client_list);
360 dprint(FD_NET, "client: added <%s>\n", client->hostname);
365 static const char *server_name(struct fio_client *client, char *buf,
371 from = inet_ntop(AF_INET6, (struct sockaddr *) &client->addr6.sin6_addr, buf, bufsize);
372 else if (client->is_sock)
375 from = inet_ntop(AF_INET, (struct sockaddr *) &client->addr.sin_addr, buf, bufsize);
380 static void probe_client(struct fio_client *client)
382 struct cmd_client_probe_pdu pdu;
387 dprint(FD_NET, "client: send probe\n");
390 pdu.flags = __le64_to_cpu(FIO_PROBE_FLAG_ZLIB);
395 sname = server_name(client, buf, sizeof(buf));
396 memset(pdu.server, 0, sizeof(pdu.server));
397 strncpy((char *) pdu.server, sname, sizeof(pdu.server) - 1);
399 fio_net_send_cmd(client->fd, FIO_NET_CMD_PROBE, &pdu, sizeof(pdu), &tag, &client->cmd_list);
402 static int fio_client_connect_ip(struct fio_client *client)
404 struct sockaddr *addr;
409 client->addr6.sin6_family = AF_INET6;
410 client->addr6.sin6_port = htons(client->port);
412 addr = (struct sockaddr *) &client->addr6;
413 socklen = sizeof(client->addr6);
415 client->addr.sin_family = AF_INET;
416 client->addr.sin_port = htons(client->port);
418 addr = (struct sockaddr *) &client->addr;
419 socklen = sizeof(client->addr);
422 fd = socket(domain, SOCK_STREAM, 0);
426 log_err("fio: socket: %s\n", strerror(errno));
430 if (connect(fd, addr, socklen) < 0) {
433 log_err("fio: connect: %s\n", strerror(errno));
434 log_err("fio: failed to connect to %s:%u\n", client->hostname,
443 static int fio_client_connect_sock(struct fio_client *client)
445 struct sockaddr_un *addr = &client->addr_un;
449 memset(addr, 0, sizeof(*addr));
450 addr->sun_family = AF_UNIX;
451 strncpy(addr->sun_path, client->hostname, sizeof(addr->sun_path) - 1);
453 fd = socket(AF_UNIX, SOCK_STREAM, 0);
457 log_err("fio: socket: %s\n", strerror(errno));
461 len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
462 if (connect(fd, (struct sockaddr *) addr, len) < 0) {
465 log_err("fio: connect; %s\n", strerror(errno));
473 int fio_client_connect(struct fio_client *client)
477 dprint(FD_NET, "client: connect to host %s\n", client->hostname);
480 fd = fio_client_connect_sock(client);
482 fd = fio_client_connect_ip(client);
484 dprint(FD_NET, "client: %s connected %d\n", client->hostname, fd);
490 fio_client_add_hash(client);
491 client->state = Client_connected;
493 probe_client(client);
497 int fio_client_terminate(struct fio_client *client)
499 return fio_net_send_quit(client->fd);
502 void fio_clients_terminate(void)
504 struct flist_head *entry;
505 struct fio_client *client;
507 dprint(FD_NET, "client: terminate clients\n");
509 flist_for_each(entry, &client_list) {
510 client = flist_entry(entry, struct fio_client, list);
511 fio_client_terminate(client);
515 static void sig_int(int sig)
517 dprint(FD_NET, "client: got signal %d\n", sig);
518 fio_clients_terminate();
521 static void sig_show_status(int sig)
523 show_running_run_stats();
526 static void client_signal_handler(void)
528 struct sigaction act;
530 memset(&act, 0, sizeof(act));
531 act.sa_handler = sig_int;
532 act.sa_flags = SA_RESTART;
533 sigaction(SIGINT, &act, NULL);
535 memset(&act, 0, sizeof(act));
536 act.sa_handler = sig_int;
537 act.sa_flags = SA_RESTART;
538 sigaction(SIGTERM, &act, NULL);
540 /* Windows uses SIGBREAK as a quit signal from other applications */
542 memset(&act, 0, sizeof(act));
543 act.sa_handler = sig_int;
544 act.sa_flags = SA_RESTART;
545 sigaction(SIGBREAK, &act, NULL);
548 memset(&act, 0, sizeof(act));
549 act.sa_handler = sig_show_status;
550 act.sa_flags = SA_RESTART;
551 sigaction(SIGUSR1, &act, NULL);
554 static int send_client_cmd_line(struct fio_client *client)
556 struct cmd_single_line_pdu *cslp;
557 struct cmd_line_pdu *clp;
558 unsigned long offset;
564 dprint(FD_NET, "client: send cmdline %d\n", client->argc);
566 lens = malloc(client->argc * sizeof(unsigned int));
569 * Find out how much mem we need
571 for (i = 0, mem = 0; i < client->argc; i++) {
572 lens[i] = strlen(client->argv[i]) + 1;
577 * We need one cmd_line_pdu, and argc number of cmd_single_line_pdu
579 mem += sizeof(*clp) + (client->argc * sizeof(*cslp));
583 offset = sizeof(*clp);
585 for (i = 0; i < client->argc; i++) {
586 uint16_t arg_len = lens[i];
589 strcpy((char *) cslp->text, client->argv[i]);
590 cslp->len = cpu_to_le16(arg_len);
591 offset += sizeof(*cslp) + arg_len;
595 clp->lines = cpu_to_le16(client->argc);
596 clp->client_type = __cpu_to_le16(client->type);
597 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOBLINE, pdu, mem, NULL, NULL);
602 int fio_clients_connect(void)
604 struct fio_client *client;
605 struct flist_head *entry, *tmp;
610 WSAStartup(MAKEWORD(2, 2), &wsd);
613 dprint(FD_NET, "client: connect all\n");
615 client_signal_handler();
617 flist_for_each_safe(entry, tmp, &client_list) {
618 client = flist_entry(entry, struct fio_client, list);
620 ret = fio_client_connect(client);
622 remove_client(client);
626 if (client->argc > 1)
627 send_client_cmd_line(client);
633 int fio_start_client(struct fio_client *client)
635 dprint(FD_NET, "client: start %s\n", client->hostname);
636 return fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_RUN, 0, NULL);
639 int fio_start_all_clients(void)
641 struct fio_client *client;
642 struct flist_head *entry, *tmp;
645 dprint(FD_NET, "client: start all\n");
647 fio_client_json_init();
649 flist_for_each_safe(entry, tmp, &client_list) {
650 client = flist_entry(entry, struct fio_client, list);
652 ret = fio_start_client(client);
654 remove_client(client);
659 return flist_empty(&client_list);
662 static int __fio_client_send_remote_ini(struct fio_client *client,
663 const char *filename)
665 struct cmd_load_file_pdu *pdu;
669 dprint(FD_NET, "send remote ini %s to %s\n", filename, client->hostname);
671 p_size = sizeof(*pdu) + strlen(filename) + 1;
672 pdu = malloc(p_size);
673 memset(pdu, 0, p_size);
674 pdu->name_len = strlen(filename);
675 strcpy((char *) pdu->file, filename);
676 pdu->client_type = cpu_to_le16((uint16_t) client->type);
678 client->sent_job = 1;
679 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_LOAD_FILE, pdu, p_size,NULL, NULL);
685 * Send file contents to server backend. We could use sendfile(), but to remain
686 * more portable lets just read/write the darn thing.
688 static int __fio_client_send_local_ini(struct fio_client *client,
689 const char *filename)
691 struct cmd_job_pdu *pdu;
699 dprint(FD_NET, "send ini %s to %s\n", filename, client->hostname);
701 fd = open(filename, O_RDONLY);
704 log_err("fio: job file <%s> open: %s\n", filename, strerror(errno));
708 if (fstat(fd, &sb) < 0) {
710 log_err("fio: job file stat: %s\n", strerror(errno));
715 p_size = sb.st_size + sizeof(*pdu);
716 pdu = malloc(p_size);
721 if (read_data(fd, p, len)) {
722 log_err("fio: failed reading job file %s\n", filename);
728 pdu->buf_len = __cpu_to_le32(sb.st_size);
729 pdu->client_type = cpu_to_le32(client->type);
731 client->sent_job = 1;
732 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, pdu, p_size, NULL, NULL);
738 int fio_client_send_ini(struct fio_client *client, const char *filename,
744 ret = __fio_client_send_local_ini(client, filename);
746 ret = __fio_client_send_remote_ini(client, filename);
749 client->sent_job = 1;
754 static int fio_client_send_cf(struct fio_client *client,
755 struct client_file *cf)
757 return fio_client_send_ini(client, cf->file, cf->remote);
760 int fio_clients_send_ini(const char *filename)
762 struct fio_client *client;
763 struct flist_head *entry, *tmp;
765 flist_for_each_safe(entry, tmp, &client_list) {
766 client = flist_entry(entry, struct fio_client, list);
768 if (client->nr_files) {
771 for (i = 0; i < client->nr_files; i++) {
772 struct client_file *cf;
774 cf = &client->files[i];
776 if (fio_client_send_cf(client, cf)) {
777 remove_client(client);
782 if (client->sent_job)
784 if (!filename || fio_client_send_ini(client, filename, 0))
785 remove_client(client);
791 int fio_client_update_options(struct fio_client *client,
792 struct thread_options *o, uint64_t *tag)
794 struct cmd_add_job_pdu pdu;
796 pdu.thread_number = cpu_to_le32(client->thread_number);
797 pdu.groupid = cpu_to_le32(client->groupid);
798 convert_thread_options_to_net(&pdu.top, o);
800 return fio_net_send_cmd(client->fd, FIO_NET_CMD_UPDATE_JOB, &pdu, sizeof(pdu), tag, &client->cmd_list);
803 static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
805 dst->max_val = le64_to_cpu(src->max_val);
806 dst->min_val = le64_to_cpu(src->min_val);
807 dst->samples = le64_to_cpu(src->samples);
810 * Floats arrive as IEEE 754 encoded uint64_t, convert back to double
812 dst->mean.u.f = fio_uint64_to_double(le64_to_cpu(dst->mean.u.i));
813 dst->S.u.f = fio_uint64_to_double(le64_to_cpu(dst->S.u.i));
816 static void convert_ts(struct thread_stat *dst, struct thread_stat *src)
820 dst->error = le32_to_cpu(src->error);
821 dst->thread_number = le32_to_cpu(src->thread_number);
822 dst->groupid = le32_to_cpu(src->groupid);
823 dst->pid = le32_to_cpu(src->pid);
824 dst->members = le32_to_cpu(src->members);
825 dst->unified_rw_rep = le32_to_cpu(src->unified_rw_rep);
827 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
828 convert_io_stat(&dst->clat_stat[i], &src->clat_stat[i]);
829 convert_io_stat(&dst->slat_stat[i], &src->slat_stat[i]);
830 convert_io_stat(&dst->lat_stat[i], &src->lat_stat[i]);
831 convert_io_stat(&dst->bw_stat[i], &src->bw_stat[i]);
834 dst->usr_time = le64_to_cpu(src->usr_time);
835 dst->sys_time = le64_to_cpu(src->sys_time);
836 dst->ctx = le64_to_cpu(src->ctx);
837 dst->minf = le64_to_cpu(src->minf);
838 dst->majf = le64_to_cpu(src->majf);
839 dst->clat_percentiles = le64_to_cpu(src->clat_percentiles);
840 dst->percentile_precision = le64_to_cpu(src->percentile_precision);
842 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
843 fio_fp64_t *fps = &src->percentile_list[i];
844 fio_fp64_t *fpd = &dst->percentile_list[i];
846 fpd->u.f = fio_uint64_to_double(le64_to_cpu(fps->u.i));
849 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
850 dst->io_u_map[i] = le32_to_cpu(src->io_u_map[i]);
851 dst->io_u_submit[i] = le32_to_cpu(src->io_u_submit[i]);
852 dst->io_u_complete[i] = le32_to_cpu(src->io_u_complete[i]);
855 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
856 dst->io_u_lat_u[i] = le32_to_cpu(src->io_u_lat_u[i]);
857 dst->io_u_lat_m[i] = le32_to_cpu(src->io_u_lat_m[i]);
860 for (i = 0; i < DDIR_RWDIR_CNT; i++)
861 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
862 dst->io_u_plat[i][j] = le32_to_cpu(src->io_u_plat[i][j]);
864 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
865 dst->total_io_u[i] = le64_to_cpu(src->total_io_u[i]);
866 dst->short_io_u[i] = le64_to_cpu(src->short_io_u[i]);
867 dst->drop_io_u[i] = le64_to_cpu(src->drop_io_u[i]);
870 dst->total_submit = le64_to_cpu(src->total_submit);
871 dst->total_complete = le64_to_cpu(src->total_complete);
873 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
874 dst->io_bytes[i] = le64_to_cpu(src->io_bytes[i]);
875 dst->runtime[i] = le64_to_cpu(src->runtime[i]);
878 dst->total_run_time = le64_to_cpu(src->total_run_time);
879 dst->continue_on_error = le16_to_cpu(src->continue_on_error);
880 dst->total_err_count = le64_to_cpu(src->total_err_count);
881 dst->first_error = le32_to_cpu(src->first_error);
882 dst->kb_base = le32_to_cpu(src->kb_base);
883 dst->unit_base = le32_to_cpu(src->unit_base);
885 dst->latency_depth = le32_to_cpu(src->latency_depth);
886 dst->latency_target = le64_to_cpu(src->latency_target);
887 dst->latency_window = le64_to_cpu(src->latency_window);
888 dst->latency_percentile.u.f = fio_uint64_to_double(le64_to_cpu(src->latency_percentile.u.i));
891 static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
895 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
896 dst->max_run[i] = le64_to_cpu(src->max_run[i]);
897 dst->min_run[i] = le64_to_cpu(src->min_run[i]);
898 dst->max_bw[i] = le64_to_cpu(src->max_bw[i]);
899 dst->min_bw[i] = le64_to_cpu(src->min_bw[i]);
900 dst->io_kb[i] = le64_to_cpu(src->io_kb[i]);
901 dst->agg[i] = le64_to_cpu(src->agg[i]);
904 dst->kb_base = le32_to_cpu(src->kb_base);
905 dst->unit_base = le32_to_cpu(src->unit_base);
906 dst->groupid = le32_to_cpu(src->groupid);
907 dst->unified_rw_rep = le32_to_cpu(src->unified_rw_rep);
910 static void json_object_add_client_info(struct json_object *obj,
911 struct fio_client *client)
913 const char *hostname = client->hostname ? client->hostname : "";
915 json_object_add_value_string(obj, "hostname", hostname);
916 json_object_add_value_int(obj, "port", client->port);
919 static void handle_ts(struct fio_client *client, struct fio_net_cmd *cmd)
921 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
922 struct json_object *tsobj;
924 tsobj = show_thread_status(&p->ts, &p->rs);
925 client->did_stat = 1;
927 json_object_add_client_info(tsobj, client);
928 json_array_add_value_object(clients_array, tsobj);
931 if (sum_stat_clients <= 1)
934 sum_thread_stats(&client_ts, &p->ts, sum_stat_nr);
935 sum_group_stats(&client_gs, &p->rs);
938 client_ts.thread_number = p->ts.thread_number;
939 client_ts.groupid = p->ts.groupid;
940 client_ts.unified_rw_rep = p->ts.unified_rw_rep;
942 if (++sum_stat_nr == sum_stat_clients) {
943 strcpy(client_ts.name, "All clients");
944 tsobj = show_thread_status(&client_ts, &client_gs);
946 json_object_add_client_info(tsobj, client);
947 json_array_add_value_object(clients_array, tsobj);
952 static void handle_gs(struct fio_client *client, struct fio_net_cmd *cmd)
954 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
956 show_group_stats(gs);
959 static void handle_text(struct fio_client *client, struct fio_net_cmd *cmd)
961 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
962 const char *buf = (const char *) pdu->buf;
966 name = client->name ? client->name : client->hostname;
968 if (!client->skip_newline)
969 fprintf(f_out, "<%s> ", name);
970 ret = fwrite(buf, pdu->buf_len, 1, f_out);
972 client->skip_newline = strchr(buf, '\n') == NULL;
975 static void convert_agg(struct disk_util_agg *agg)
979 for (i = 0; i < 2; i++) {
980 agg->ios[i] = le64_to_cpu(agg->ios[i]);
981 agg->merges[i] = le64_to_cpu(agg->merges[i]);
982 agg->sectors[i] = le64_to_cpu(agg->sectors[i]);
983 agg->ticks[i] = le64_to_cpu(agg->ticks[i]);
986 agg->io_ticks = le64_to_cpu(agg->io_ticks);
987 agg->time_in_queue = le64_to_cpu(agg->time_in_queue);
988 agg->slavecount = le32_to_cpu(agg->slavecount);
989 agg->max_util.u.f = fio_uint64_to_double(le64_to_cpu(agg->max_util.u.i));
992 static void convert_dus(struct disk_util_stat *dus)
996 for (i = 0; i < 2; i++) {
997 dus->s.ios[i] = le64_to_cpu(dus->s.ios[i]);
998 dus->s.merges[i] = le64_to_cpu(dus->s.merges[i]);
999 dus->s.sectors[i] = le64_to_cpu(dus->s.sectors[i]);
1000 dus->s.ticks[i] = le64_to_cpu(dus->s.ticks[i]);
1003 dus->s.io_ticks = le64_to_cpu(dus->s.io_ticks);
1004 dus->s.time_in_queue = le64_to_cpu(dus->s.time_in_queue);
1005 dus->s.msec = le64_to_cpu(dus->s.msec);
1008 static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd)
1010 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
1012 if (!client->disk_stats_shown) {
1013 client->disk_stats_shown = 1;
1014 log_info("\nDisk stats (read/write):\n");
1017 if (output_format == FIO_OUTPUT_JSON) {
1018 struct json_object *duobj;
1019 json_array_add_disk_util(&du->dus, &du->agg, du_array);
1020 duobj = json_array_last_value_object(du_array);
1021 json_object_add_client_info(duobj, client);
1023 print_disk_util(&du->dus, &du->agg, output_format == FIO_OUTPUT_TERSE);
1026 static void convert_jobs_eta(struct jobs_eta *je)
1030 je->nr_running = le32_to_cpu(je->nr_running);
1031 je->nr_ramp = le32_to_cpu(je->nr_ramp);
1032 je->nr_pending = le32_to_cpu(je->nr_pending);
1033 je->nr_setting_up = le32_to_cpu(je->nr_setting_up);
1034 je->files_open = le32_to_cpu(je->files_open);
1036 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1037 je->m_rate[i] = le32_to_cpu(je->m_rate[i]);
1038 je->t_rate[i] = le32_to_cpu(je->t_rate[i]);
1039 je->m_iops[i] = le32_to_cpu(je->m_iops[i]);
1040 je->t_iops[i] = le32_to_cpu(je->t_iops[i]);
1041 je->rate[i] = le32_to_cpu(je->rate[i]);
1042 je->iops[i] = le32_to_cpu(je->iops[i]);
1045 je->elapsed_sec = le64_to_cpu(je->elapsed_sec);
1046 je->eta_sec = le64_to_cpu(je->eta_sec);
1047 je->nr_threads = le32_to_cpu(je->nr_threads);
1048 je->is_pow2 = le32_to_cpu(je->is_pow2);
1049 je->unit_base = le32_to_cpu(je->unit_base);
1052 void fio_client_sum_jobs_eta(struct jobs_eta *dst, struct jobs_eta *je)
1056 dst->nr_running += je->nr_running;
1057 dst->nr_ramp += je->nr_ramp;
1058 dst->nr_pending += je->nr_pending;
1059 dst->nr_setting_up += je->nr_setting_up;
1060 dst->files_open += je->files_open;
1062 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1063 dst->m_rate[i] += je->m_rate[i];
1064 dst->t_rate[i] += je->t_rate[i];
1065 dst->m_iops[i] += je->m_iops[i];
1066 dst->t_iops[i] += je->t_iops[i];
1067 dst->rate[i] += je->rate[i];
1068 dst->iops[i] += je->iops[i];
1071 dst->elapsed_sec += je->elapsed_sec;
1073 if (je->eta_sec > dst->eta_sec)
1074 dst->eta_sec = je->eta_sec;
1076 dst->nr_threads += je->nr_threads;
1079 * This wont be correct for multiple strings, but at least it
1080 * works for the basic cases.
1082 strcpy((char *) dst->run_str, (char *) je->run_str);
1085 void fio_client_dec_jobs_eta(struct client_eta *eta, client_eta_op eta_fn)
1087 if (!--eta->pending) {
1093 static void remove_reply_cmd(struct fio_client *client, struct fio_net_cmd *cmd)
1095 struct fio_net_cmd_reply *reply = NULL;
1096 struct flist_head *entry;
1098 flist_for_each(entry, &client->cmd_list) {
1099 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
1101 if (cmd->tag == (uintptr_t) reply)
1108 log_err("fio: client: unable to find matching tag (%llx)\n", (unsigned long long) cmd->tag);
1112 flist_del(&reply->list);
1113 cmd->tag = reply->saved_tag;
1117 int fio_client_wait_for_reply(struct fio_client *client, uint64_t tag)
1120 struct fio_net_cmd_reply *reply = NULL;
1121 struct flist_head *entry;
1123 flist_for_each(entry, &client->cmd_list) {
1124 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
1126 if (tag == (uintptr_t) reply)
1141 static void handle_eta(struct fio_client *client, struct fio_net_cmd *cmd)
1143 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
1144 struct client_eta *eta = (struct client_eta *) (uintptr_t) cmd->tag;
1146 dprint(FD_NET, "client: got eta tag %p, %d\n", eta, eta->pending);
1148 assert(client->eta_in_flight == eta);
1150 client->eta_in_flight = NULL;
1151 flist_del_init(&client->eta_list);
1153 if (client->ops->jobs_eta)
1154 client->ops->jobs_eta(client, je);
1156 fio_client_sum_jobs_eta(&eta->eta, je);
1157 fio_client_dec_jobs_eta(eta, client->ops->eta);
1160 static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd)
1162 struct cmd_probe_reply_pdu *probe = (struct cmd_probe_reply_pdu *) cmd->payload;
1163 const char *os, *arch;
1166 os = fio_get_os_string(probe->os);
1170 arch = fio_get_arch_string(probe->arch);
1174 sprintf(bit, "%d-bit", probe->bpp * 8);
1175 probe->flags = le64_to_cpu(probe->flags);
1177 log_info("hostname=%s, be=%u, %s, os=%s, arch=%s, fio=%s, flags=%lx\n",
1178 probe->hostname, probe->bigendian, bit, os, arch,
1179 probe->fio_version, (unsigned long) probe->flags);
1182 client->name = strdup((char *) probe->hostname);
1185 static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd)
1187 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
1189 client->state = Client_started;
1190 client->jobs = le32_to_cpu(pdu->jobs);
1191 client->nr_stat = le32_to_cpu(pdu->stat_outputs);
1193 sum_stat_clients += client->nr_stat;
1196 static void handle_stop(struct fio_client *client, struct fio_net_cmd *cmd)
1199 log_info("client <%s>: exited with error %d\n", client->hostname, client->error);
1202 static void convert_stop(struct fio_net_cmd *cmd)
1204 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
1206 pdu->error = le32_to_cpu(pdu->error);
1209 static void convert_text(struct fio_net_cmd *cmd)
1211 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
1213 pdu->level = le32_to_cpu(pdu->level);
1214 pdu->buf_len = le32_to_cpu(pdu->buf_len);
1215 pdu->log_sec = le64_to_cpu(pdu->log_sec);
1216 pdu->log_usec = le64_to_cpu(pdu->log_usec);
1219 static struct cmd_iolog_pdu *convert_iolog_gz(struct fio_net_cmd *cmd,
1220 struct cmd_iolog_pdu *pdu)
1223 struct cmd_iolog_pdu *ret;
1225 uint32_t nr_samples;
1229 stream.zalloc = Z_NULL;
1230 stream.zfree = Z_NULL;
1231 stream.opaque = Z_NULL;
1232 stream.avail_in = 0;
1233 stream.next_in = Z_NULL;
1235 if (inflateInit(&stream) != Z_OK)
1239 * Get header first, it's not compressed
1241 nr_samples = le64_to_cpu(pdu->nr_samples);
1243 total = nr_samples * __log_entry_sz(le32_to_cpu(pdu->log_offset));
1244 ret = malloc(total + sizeof(*pdu));
1245 ret->nr_samples = nr_samples;
1247 memcpy(ret, pdu, sizeof(*pdu));
1249 p = (void *) ret + sizeof(*pdu);
1251 stream.avail_in = cmd->pdu_len - sizeof(*pdu);
1252 stream.next_in = (void *) pdu + sizeof(*pdu);
1253 while (stream.avail_in) {
1254 unsigned int this_chunk = 65536;
1255 unsigned int this_len;
1258 if (this_chunk > total)
1261 stream.avail_out = this_chunk;
1262 stream.next_out = p;
1263 err = inflate(&stream, Z_NO_FLUSH);
1264 /* may be Z_OK, or Z_STREAM_END */
1266 log_err("fio: inflate error %d\n", err);
1272 this_len = this_chunk - stream.avail_out;
1278 inflateEnd(&stream);
1286 * This has been compressed on the server side, since it can be big.
1289 static struct cmd_iolog_pdu *convert_iolog(struct fio_net_cmd *cmd)
1291 struct cmd_iolog_pdu *pdu = (struct cmd_iolog_pdu *) cmd->payload;
1292 struct cmd_iolog_pdu *ret;
1297 * Convert if compressed and we support it. If it's not
1298 * compressed, we need not do anything.
1300 if (le32_to_cpu(pdu->compressed)) {
1302 log_err("fio: server sent compressed data by mistake\n");
1305 ret = convert_iolog_gz(cmd, pdu);
1307 log_err("fio: failed decompressing log\n");
1313 ret->nr_samples = le64_to_cpu(ret->nr_samples);
1314 ret->thread_number = le32_to_cpu(ret->thread_number);
1315 ret->log_type = le32_to_cpu(ret->log_type);
1316 ret->compressed = le32_to_cpu(ret->compressed);
1317 ret->log_offset = le32_to_cpu(ret->log_offset);
1319 samples = &ret->samples[0];
1320 for (i = 0; i < ret->nr_samples; i++) {
1321 struct io_sample *s;
1323 s = __get_sample(samples, ret->log_offset, i);
1324 s->time = le64_to_cpu(s->time);
1325 s->val = le64_to_cpu(s->val);
1326 s->__ddir = le32_to_cpu(s->__ddir);
1327 s->bs = le32_to_cpu(s->bs);
1329 if (ret->log_offset) {
1330 struct io_sample_offset *so = (void *) s;
1332 so->offset = le64_to_cpu(so->offset);
1339 static void sendfile_reply(int fd, struct cmd_sendfile_reply *rep,
1340 size_t size, uint64_t tag)
1342 rep->error = cpu_to_le32(rep->error);
1343 fio_net_send_cmd(fd, FIO_NET_CMD_SENDFILE, rep, size, &tag, NULL);
1346 static int send_file(struct fio_client *client, struct cmd_sendfile *pdu,
1349 struct cmd_sendfile_reply *rep;
1354 size = sizeof(*rep);
1357 if (stat((char *)pdu->path, &sb) < 0) {
1360 sendfile_reply(client->fd, rep, size, tag);
1366 rep = realloc(rep, size);
1367 rep->size = cpu_to_le32((uint32_t) sb.st_size);
1369 fd = open((char *)pdu->path, O_RDONLY);
1373 rep->error = read_data(fd, &rep->data, sb.st_size);
1374 sendfile_reply(client->fd, rep, size, tag);
1380 int fio_handle_client(struct fio_client *client)
1382 struct client_ops *ops = client->ops;
1383 struct fio_net_cmd *cmd;
1385 dprint(FD_NET, "client: handle %s\n", client->hostname);
1387 cmd = fio_net_recv_cmd(client->fd);
1391 dprint(FD_NET, "client: got cmd op %s from %s (pdu=%u)\n",
1392 fio_server_op(cmd->opcode), client->hostname, cmd->pdu_len);
1394 switch (cmd->opcode) {
1395 case FIO_NET_CMD_QUIT:
1397 ops->quit(client, cmd);
1398 remove_client(client);
1400 case FIO_NET_CMD_TEXT:
1402 ops->text(client, cmd);
1404 case FIO_NET_CMD_DU: {
1405 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
1407 convert_dus(&du->dus);
1408 convert_agg(&du->agg);
1410 ops->disk_util(client, cmd);
1413 case FIO_NET_CMD_TS: {
1414 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
1416 convert_ts(&p->ts, &p->ts);
1417 convert_gs(&p->rs, &p->rs);
1419 ops->thread_status(client, cmd);
1422 case FIO_NET_CMD_GS: {
1423 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
1427 ops->group_stats(client, cmd);
1430 case FIO_NET_CMD_ETA: {
1431 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
1433 remove_reply_cmd(client, cmd);
1434 convert_jobs_eta(je);
1435 handle_eta(client, cmd);
1438 case FIO_NET_CMD_PROBE:
1439 remove_reply_cmd(client, cmd);
1440 ops->probe(client, cmd);
1442 case FIO_NET_CMD_SERVER_START:
1443 client->state = Client_running;
1445 ops->job_start(client, cmd);
1447 case FIO_NET_CMD_START: {
1448 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
1450 pdu->jobs = le32_to_cpu(pdu->jobs);
1451 ops->start(client, cmd);
1454 case FIO_NET_CMD_STOP: {
1455 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
1458 client->state = Client_stopped;
1459 client->error = le32_to_cpu(pdu->error);
1460 client->signal = le32_to_cpu(pdu->signal);
1461 ops->stop(client, cmd);
1464 case FIO_NET_CMD_ADD_JOB: {
1465 struct cmd_add_job_pdu *pdu = (struct cmd_add_job_pdu *) cmd->payload;
1467 client->thread_number = le32_to_cpu(pdu->thread_number);
1468 client->groupid = le32_to_cpu(pdu->groupid);
1471 ops->add_job(client, cmd);
1474 case FIO_NET_CMD_IOLOG:
1476 struct cmd_iolog_pdu *pdu;
1478 pdu = convert_iolog(cmd);
1479 ops->iolog(client, pdu);
1482 case FIO_NET_CMD_UPDATE_JOB:
1483 ops->update_job(client, cmd);
1484 remove_reply_cmd(client, cmd);
1486 case FIO_NET_CMD_VTRIGGER: {
1487 struct all_io_list *pdu = (struct all_io_list *) cmd->payload;
1490 __verify_save_state(pdu, server_name(client, buf, sizeof(buf)));
1491 exec_trigger(trigger_cmd);
1494 case FIO_NET_CMD_SENDFILE: {
1495 struct cmd_sendfile *pdu = (struct cmd_sendfile *) cmd->payload;
1496 send_file(client, pdu, cmd->tag);
1500 log_err("fio: unknown client op: %s\n", fio_server_op(cmd->opcode));
1508 int fio_clients_send_trigger(const char *cmd)
1510 struct flist_head *entry;
1511 struct fio_client *client;
1514 dprint(FD_NET, "client: send vtrigger: %s\n", cmd);
1521 flist_for_each(entry, &client_list) {
1522 struct cmd_vtrigger_pdu *pdu;
1524 client = flist_entry(entry, struct fio_client, list);
1526 pdu = malloc(sizeof(*pdu) + slen);
1527 pdu->len = cpu_to_le16((uint16_t) slen);
1529 memcpy(pdu->cmd, cmd, slen);
1530 fio_net_send_cmd(client->fd, FIO_NET_CMD_VTRIGGER, pdu,
1531 sizeof(*pdu) + slen, NULL, NULL);
1538 static void request_client_etas(struct client_ops *ops)
1540 struct fio_client *client;
1541 struct flist_head *entry;
1542 struct client_eta *eta;
1545 dprint(FD_NET, "client: request eta (%d)\n", nr_clients);
1547 eta = calloc(1, sizeof(*eta) + __THREAD_RUNSTR_SZ(REAL_MAX_JOBS));
1548 eta->pending = nr_clients;
1550 flist_for_each(entry, &client_list) {
1551 client = flist_entry(entry, struct fio_client, list);
1553 if (!flist_empty(&client->eta_list)) {
1557 if (client->state != Client_running)
1560 assert(!client->eta_in_flight);
1561 flist_add_tail(&client->eta_list, &eta_list);
1562 client->eta_in_flight = eta;
1563 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_SEND_ETA,
1564 (uintptr_t) eta, &client->cmd_list);
1568 fio_client_dec_jobs_eta(eta, ops->eta);
1570 dprint(FD_NET, "client: requested eta tag %p\n", eta);
1573 static int client_check_cmd_timeout(struct fio_client *client,
1574 struct timeval *now)
1576 struct fio_net_cmd_reply *reply;
1577 struct flist_head *entry, *tmp;
1580 flist_for_each_safe(entry, tmp, &client->cmd_list) {
1581 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
1583 if (mtime_since(&reply->tv, now) < FIO_NET_CLIENT_TIMEOUT)
1586 log_err("fio: client %s, timeout on cmd %s\n", client->hostname,
1587 fio_server_op(reply->opcode));
1588 flist_del(&reply->list);
1593 return flist_empty(&client->cmd_list) && ret;
1596 static int fio_check_clients_timed_out(void)
1598 struct fio_client *client;
1599 struct flist_head *entry, *tmp;
1603 fio_gettime(&tv, NULL);
1605 flist_for_each_safe(entry, tmp, &client_list) {
1606 client = flist_entry(entry, struct fio_client, list);
1608 if (flist_empty(&client->cmd_list))
1611 if (!client_check_cmd_timeout(client, &tv))
1614 if (client->ops->timed_out)
1615 client->ops->timed_out(client);
1617 log_err("fio: client %s timed out\n", client->hostname);
1619 remove_client(client);
1626 int fio_handle_clients(struct client_ops *ops)
1628 struct pollfd *pfds;
1629 int i, ret = 0, retval = 0;
1631 fio_gettime(&eta_tv, NULL);
1633 pfds = malloc(nr_clients * sizeof(struct pollfd));
1635 init_thread_stat(&client_ts);
1636 init_group_run_stat(&client_gs);
1638 while (!exit_backend && nr_clients) {
1639 struct flist_head *entry, *tmp;
1640 struct fio_client *client;
1643 flist_for_each_safe(entry, tmp, &client_list) {
1644 client = flist_entry(entry, struct fio_client, list);
1646 if (!client->sent_job && !client->ops->stay_connected &&
1647 flist_empty(&client->cmd_list)) {
1648 remove_client(client);
1652 pfds[i].fd = client->fd;
1653 pfds[i].events = POLLIN;
1660 assert(i == nr_clients);
1666 fio_gettime(&tv, NULL);
1667 if (mtime_since(&eta_tv, &tv) >= 900) {
1668 request_client_etas(ops);
1669 memcpy(&eta_tv, &tv, sizeof(tv));
1671 if (fio_check_clients_timed_out())
1675 check_trigger_file();
1677 timeout = min(100u, ops->eta_msec);
1679 ret = poll(pfds, nr_clients, timeout);
1683 log_err("fio: poll clients: %s\n", strerror(errno));
1689 for (i = 0; i < nr_clients; i++) {
1690 if (!(pfds[i].revents & POLLIN))
1693 client = find_client_by_fd(pfds[i].fd);
1695 log_err("fio: unknown client fd %ld\n", (long) pfds[i].fd);
1698 if (!fio_handle_client(client)) {
1699 log_info("client: host=%s disconnected\n",
1701 remove_client(client);
1703 } else if (client->error)
1705 fio_put_client(client);
1709 fio_client_json_fini();