11 #include <sys/socket.h>
13 #include <netinet/in.h>
14 #include <arpa/inet.h>
25 static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd);
26 static void handle_ts(struct fio_client *client, struct fio_net_cmd *cmd);
27 static void handle_gs(struct fio_client *client, struct fio_net_cmd *cmd);
28 static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd);
29 static void handle_text(struct fio_client *client, struct fio_net_cmd *cmd);
30 static void handle_stop(struct fio_client *client, struct fio_net_cmd *cmd);
31 static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd);
33 struct client_ops fio_client_ops = {
35 .disk_util = handle_du,
36 .thread_status = handle_ts,
37 .group_stats = handle_gs,
39 .start = handle_start,
40 .eta = display_thread_status,
41 .probe = handle_probe,
42 .eta_msec = FIO_CLIENT_DEF_ETA_MSEC,
43 .client_type = FIO_CLIENT_TYPE_CLI,
46 static struct timeval eta_tv;
48 static FLIST_HEAD(client_list);
49 static FLIST_HEAD(eta_list);
51 static FLIST_HEAD(arg_list);
53 struct thread_stat client_ts;
54 struct group_run_stats client_gs;
57 static int sum_stat_nr;
59 #define FIO_CLIENT_HASH_BITS 7
60 #define FIO_CLIENT_HASH_SZ (1 << FIO_CLIENT_HASH_BITS)
61 #define FIO_CLIENT_HASH_MASK (FIO_CLIENT_HASH_SZ - 1)
62 static struct flist_head client_hash[FIO_CLIENT_HASH_SZ];
64 static void fio_client_add_hash(struct fio_client *client)
66 int bucket = hash_long(client->fd, FIO_CLIENT_HASH_BITS);
68 bucket &= FIO_CLIENT_HASH_MASK;
69 flist_add(&client->hash_list, &client_hash[bucket]);
72 static void fio_client_remove_hash(struct fio_client *client)
74 if (!flist_empty(&client->hash_list))
75 flist_del_init(&client->hash_list);
78 static void fio_init fio_client_hash_init(void)
82 for (i = 0; i < FIO_CLIENT_HASH_SZ; i++)
83 INIT_FLIST_HEAD(&client_hash[i]);
86 static struct fio_client *find_client_by_fd(int fd)
88 int bucket = hash_long(fd, FIO_CLIENT_HASH_BITS) & FIO_CLIENT_HASH_MASK;
89 struct fio_client *client;
90 struct flist_head *entry;
92 flist_for_each(entry, &client_hash[bucket]) {
93 client = flist_entry(entry, struct fio_client, hash_list);
95 if (client->fd == fd) {
104 void fio_put_client(struct fio_client *client)
109 free(client->hostname);
118 static void remove_client(struct fio_client *client)
120 assert(client->refs);
122 dprint(FD_NET, "client: removed <%s>\n", client->hostname);
124 if (!flist_empty(&client->list))
125 flist_del_init(&client->list);
127 fio_client_remove_hash(client);
129 if (!flist_empty(&client->eta_list)) {
130 flist_del_init(&client->eta_list);
131 fio_client_dec_jobs_eta(client->eta_in_flight, client->ops->eta);
137 fio_put_client(client);
140 struct fio_client *fio_get_client(struct fio_client *client)
146 static void __fio_client_add_cmd_option(struct fio_client *client,
151 index = client->argc++;
152 client->argv = realloc(client->argv, sizeof(char *) * client->argc);
153 client->argv[index] = strdup(opt);
154 dprint(FD_NET, "client: add cmd %d: %s\n", index, opt);
157 void fio_client_add_cmd_option(void *cookie, const char *opt)
159 struct fio_client *client = cookie;
160 struct flist_head *entry;
165 __fio_client_add_cmd_option(client, opt);
168 * Duplicate arguments to shared client group
170 flist_for_each(entry, &arg_list) {
171 client = flist_entry(entry, struct fio_client, arg_list);
173 __fio_client_add_cmd_option(client, opt);
177 struct fio_client *fio_client_add_explicit(struct client_ops *ops,
178 const char *hostname, int type,
181 struct fio_client *client;
183 client = malloc(sizeof(*client));
184 memset(client, 0, sizeof(*client));
186 INIT_FLIST_HEAD(&client->list);
187 INIT_FLIST_HEAD(&client->hash_list);
188 INIT_FLIST_HEAD(&client->arg_list);
189 INIT_FLIST_HEAD(&client->eta_list);
190 INIT_FLIST_HEAD(&client->cmd_list);
192 client->hostname = strdup(hostname);
194 if (type == Fio_client_socket)
199 ipv6 = type == Fio_client_ipv6;
200 if (fio_server_parse_host(hostname, &ipv6,
201 &client->addr.sin_addr,
202 &client->addr6.sin6_addr))
211 client->type = ops->client_type;
213 __fio_client_add_cmd_option(client, "fio");
215 flist_add(&client->list, &client_list);
217 dprint(FD_NET, "client: added <%s>\n", client->hostname);
224 int fio_client_add(struct client_ops *ops, const char *hostname, void **cookie)
226 struct fio_client *existing = *cookie;
227 struct fio_client *client;
231 * We always add our "exec" name as the option, hence 1
234 if (existing->argc == 1)
235 flist_add_tail(&existing->arg_list, &arg_list);
237 while (!flist_empty(&arg_list))
238 flist_del_init(arg_list.next);
242 client = malloc(sizeof(*client));
243 memset(client, 0, sizeof(*client));
245 INIT_FLIST_HEAD(&client->list);
246 INIT_FLIST_HEAD(&client->hash_list);
247 INIT_FLIST_HEAD(&client->arg_list);
248 INIT_FLIST_HEAD(&client->eta_list);
249 INIT_FLIST_HEAD(&client->cmd_list);
251 if (fio_server_parse_string(hostname, &client->hostname,
252 &client->is_sock, &client->port,
253 &client->addr.sin_addr,
254 &client->addr6.sin6_addr,
261 client->type = ops->client_type;
263 __fio_client_add_cmd_option(client, "fio");
265 flist_add(&client->list, &client_list);
267 dprint(FD_NET, "client: added <%s>\n", client->hostname);
272 static void probe_client(struct fio_client *client)
274 dprint(FD_NET, "client: send probe\n");
276 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_PROBE, 0, &client->cmd_list);
279 static int fio_client_connect_ip(struct fio_client *client)
281 struct sockaddr *addr;
282 fio_socklen_t socklen;
286 client->addr6.sin6_family = AF_INET6;
287 client->addr6.sin6_port = htons(client->port);
289 addr = (struct sockaddr *) &client->addr6;
290 socklen = sizeof(client->addr6);
292 client->addr.sin_family = AF_INET;
293 client->addr.sin_port = htons(client->port);
295 addr = (struct sockaddr *) &client->addr;
296 socklen = sizeof(client->addr);
299 fd = socket(domain, SOCK_STREAM, 0);
303 log_err("fio: socket: %s\n", strerror(errno));
307 if (connect(fd, addr, socklen) < 0) {
310 log_err("fio: connect: %s\n", strerror(errno));
311 log_err("fio: failed to connect to %s:%u\n", client->hostname,
320 static int fio_client_connect_sock(struct fio_client *client)
322 struct sockaddr_un *addr = &client->addr_un;
326 memset(addr, 0, sizeof(*addr));
327 addr->sun_family = AF_UNIX;
328 strcpy(addr->sun_path, client->hostname);
330 fd = socket(AF_UNIX, SOCK_STREAM, 0);
334 log_err("fio: socket: %s\n", strerror(errno));
338 len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
339 if (connect(fd, (struct sockaddr *) addr, len) < 0) {
342 log_err("fio: connect; %s\n", strerror(errno));
350 int fio_client_connect(struct fio_client *client)
354 dprint(FD_NET, "client: connect to host %s\n", client->hostname);
357 fd = fio_client_connect_sock(client);
359 fd = fio_client_connect_ip(client);
361 dprint(FD_NET, "client: %s connected %d\n", client->hostname, fd);
367 fio_client_add_hash(client);
368 client->state = Client_connected;
370 probe_client(client);
374 void fio_client_terminate(struct fio_client *client)
376 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_QUIT, 0, NULL);
379 void fio_clients_terminate(void)
381 struct flist_head *entry;
382 struct fio_client *client;
384 dprint(FD_NET, "client: terminate clients\n");
386 flist_for_each(entry, &client_list) {
387 client = flist_entry(entry, struct fio_client, list);
388 fio_client_terminate(client);
392 static void sig_int(int sig)
394 dprint(FD_NET, "client: got signal %d\n", sig);
395 fio_clients_terminate();
398 static void client_signal_handler(void)
400 struct sigaction act;
402 memset(&act, 0, sizeof(act));
403 act.sa_handler = sig_int;
404 act.sa_flags = SA_RESTART;
405 sigaction(SIGINT, &act, NULL);
407 memset(&act, 0, sizeof(act));
408 act.sa_handler = sig_int;
409 act.sa_flags = SA_RESTART;
410 sigaction(SIGTERM, &act, NULL);
413 static int send_client_cmd_line(struct fio_client *client)
415 struct cmd_single_line_pdu *cslp;
416 struct cmd_line_pdu *clp;
417 unsigned long offset;
423 dprint(FD_NET, "client: send cmdline %d\n", client->argc);
425 lens = malloc(client->argc * sizeof(unsigned int));
428 * Find out how much mem we need
430 for (i = 0, mem = 0; i < client->argc; i++) {
431 lens[i] = strlen(client->argv[i]) + 1;
436 * We need one cmd_line_pdu, and argc number of cmd_single_line_pdu
438 mem += sizeof(*clp) + (client->argc * sizeof(*cslp));
442 offset = sizeof(*clp);
444 for (i = 0; i < client->argc; i++) {
445 uint16_t arg_len = lens[i];
448 strcpy((char *) cslp->text, client->argv[i]);
449 cslp->len = cpu_to_le16(arg_len);
450 offset += sizeof(*cslp) + arg_len;
454 clp->lines = cpu_to_le16(client->argc);
455 clp->client_type = __cpu_to_le16(client->type);
456 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOBLINE, pdu, mem, 0);
461 int fio_clients_connect(void)
463 struct fio_client *client;
464 struct flist_head *entry, *tmp;
469 WSAStartup(MAKEWORD(2,2), &wsd);
472 dprint(FD_NET, "client: connect all\n");
474 client_signal_handler();
476 flist_for_each_safe(entry, tmp, &client_list) {
477 client = flist_entry(entry, struct fio_client, list);
479 ret = fio_client_connect(client);
481 remove_client(client);
485 if (client->argc > 1)
486 send_client_cmd_line(client);
492 int fio_start_client(struct fio_client *client)
494 dprint(FD_NET, "client: start %s\n", client->hostname);
495 return fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_RUN, 0, NULL);
498 int fio_start_all_clients(void)
500 struct fio_client *client;
501 struct flist_head *entry, *tmp;
504 dprint(FD_NET, "client: start all\n");
506 flist_for_each_safe(entry, tmp, &client_list) {
507 client = flist_entry(entry, struct fio_client, list);
509 ret = fio_start_client(client);
511 remove_client(client);
516 return flist_empty(&client_list);
520 * Send file contents to server backend. We could use sendfile(), but to remain
521 * more portable lets just read/write the darn thing.
523 static int __fio_client_send_ini(struct fio_client *client, const char *filename)
525 struct cmd_job_pdu *pdu;
533 dprint(FD_NET, "send ini %s to %s\n", filename, client->hostname);
535 fd = open(filename, O_RDONLY);
539 log_err("fio: job file <%s> open: %s\n", filename, strerror(errno));
543 if (fstat(fd, &sb) < 0) {
546 log_err("fio: job file stat: %s\n", strerror(errno));
551 p_size = sb.st_size + sizeof(*pdu);
552 pdu = malloc(p_size);
558 ret = read(fd, p, len);
567 else if (errno == EAGAIN || errno == EINTR)
572 log_err("fio: failed reading job file %s\n", filename);
578 pdu->buf_len = __cpu_to_le32(sb.st_size);
579 pdu->client_type = cpu_to_le32(client->type);
581 client->sent_job = 1;
582 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, pdu, p_size, 0);
588 int fio_client_send_ini(struct fio_client *client, const char *filename)
592 ret = __fio_client_send_ini(client, filename);
594 client->sent_job = 1;
599 int fio_clients_send_ini(const char *filename)
601 struct fio_client *client;
602 struct flist_head *entry, *tmp;
604 flist_for_each_safe(entry, tmp, &client_list) {
605 client = flist_entry(entry, struct fio_client, list);
607 if (fio_client_send_ini(client, filename))
608 remove_client(client);
614 static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
616 dst->max_val = le64_to_cpu(src->max_val);
617 dst->min_val = le64_to_cpu(src->min_val);
618 dst->samples = le64_to_cpu(src->samples);
621 * Floats arrive as IEEE 754 encoded uint64_t, convert back to double
623 dst->mean.u.f = fio_uint64_to_double(le64_to_cpu(dst->mean.u.i));
624 dst->S.u.f = fio_uint64_to_double(le64_to_cpu(dst->S.u.i));
627 static void convert_ts(struct thread_stat *dst, struct thread_stat *src)
631 dst->error = le32_to_cpu(src->error);
632 dst->groupid = le32_to_cpu(src->groupid);
633 dst->pid = le32_to_cpu(src->pid);
634 dst->members = le32_to_cpu(src->members);
636 for (i = 0; i < 2; i++) {
637 convert_io_stat(&dst->clat_stat[i], &src->clat_stat[i]);
638 convert_io_stat(&dst->slat_stat[i], &src->slat_stat[i]);
639 convert_io_stat(&dst->lat_stat[i], &src->lat_stat[i]);
640 convert_io_stat(&dst->bw_stat[i], &src->bw_stat[i]);
643 dst->usr_time = le64_to_cpu(src->usr_time);
644 dst->sys_time = le64_to_cpu(src->sys_time);
645 dst->ctx = le64_to_cpu(src->ctx);
646 dst->minf = le64_to_cpu(src->minf);
647 dst->majf = le64_to_cpu(src->majf);
648 dst->clat_percentiles = le64_to_cpu(src->clat_percentiles);
650 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
651 fio_fp64_t *fps = &src->percentile_list[i];
652 fio_fp64_t *fpd = &dst->percentile_list[i];
654 fpd->u.f = fio_uint64_to_double(le64_to_cpu(fps->u.i));
657 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
658 dst->io_u_map[i] = le32_to_cpu(src->io_u_map[i]);
659 dst->io_u_submit[i] = le32_to_cpu(src->io_u_submit[i]);
660 dst->io_u_complete[i] = le32_to_cpu(src->io_u_complete[i]);
663 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
664 dst->io_u_lat_u[i] = le32_to_cpu(src->io_u_lat_u[i]);
665 dst->io_u_lat_m[i] = le32_to_cpu(src->io_u_lat_m[i]);
668 for (i = 0; i < 2; i++)
669 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
670 dst->io_u_plat[i][j] = le32_to_cpu(src->io_u_plat[i][j]);
672 for (i = 0; i < 3; i++) {
673 dst->total_io_u[i] = le64_to_cpu(src->total_io_u[i]);
674 dst->short_io_u[i] = le64_to_cpu(src->short_io_u[i]);
677 dst->total_submit = le64_to_cpu(src->total_submit);
678 dst->total_complete = le64_to_cpu(src->total_complete);
680 for (i = 0; i < 2; i++) {
681 dst->io_bytes[i] = le64_to_cpu(src->io_bytes[i]);
682 dst->runtime[i] = le64_to_cpu(src->runtime[i]);
685 dst->total_run_time = le64_to_cpu(src->total_run_time);
686 dst->continue_on_error = le16_to_cpu(src->continue_on_error);
687 dst->total_err_count = le64_to_cpu(src->total_err_count);
688 dst->first_error = le32_to_cpu(src->first_error);
689 dst->kb_base = le32_to_cpu(src->kb_base);
692 static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
696 for (i = 0; i < 2; i++) {
697 dst->max_run[i] = le64_to_cpu(src->max_run[i]);
698 dst->min_run[i] = le64_to_cpu(src->min_run[i]);
699 dst->max_bw[i] = le64_to_cpu(src->max_bw[i]);
700 dst->min_bw[i] = le64_to_cpu(src->min_bw[i]);
701 dst->io_kb[i] = le64_to_cpu(src->io_kb[i]);
702 dst->agg[i] = le64_to_cpu(src->agg[i]);
705 dst->kb_base = le32_to_cpu(src->kb_base);
706 dst->groupid = le32_to_cpu(src->groupid);
709 static void handle_ts(struct fio_client *client, struct fio_net_cmd *cmd)
711 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
713 show_thread_status(&p->ts, &p->rs);
715 if (sum_stat_clients == 1)
718 sum_thread_stats(&client_ts, &p->ts, sum_stat_nr);
719 sum_group_stats(&client_gs, &p->rs);
722 client_ts.groupid = p->ts.groupid;
724 if (++sum_stat_nr == sum_stat_clients) {
725 strcpy(client_ts.name, "All clients");
726 show_thread_status(&client_ts, &client_gs);
730 static void handle_gs(struct fio_client *client, struct fio_net_cmd *cmd)
732 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
734 show_group_stats(gs);
737 static void handle_text(struct fio_client *client, struct fio_net_cmd *cmd)
739 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
740 const char *buf = (const char *) pdu->buf;
744 name = client->name ? client->name : client->hostname;
746 if (!client->skip_newline)
747 fprintf(f_out, "<%s> ", name);
748 ret = fwrite(buf, pdu->buf_len, 1, f_out);
750 client->skip_newline = strchr(buf, '\n') == NULL;
753 static void convert_agg(struct disk_util_agg *agg)
757 for (i = 0; i < 2; i++) {
758 agg->ios[i] = le32_to_cpu(agg->ios[i]);
759 agg->merges[i] = le32_to_cpu(agg->merges[i]);
760 agg->sectors[i] = le64_to_cpu(agg->sectors[i]);
761 agg->ticks[i] = le32_to_cpu(agg->ticks[i]);
764 agg->io_ticks = le32_to_cpu(agg->io_ticks);
765 agg->time_in_queue = le32_to_cpu(agg->time_in_queue);
766 agg->slavecount = le32_to_cpu(agg->slavecount);
767 agg->max_util.u.f = fio_uint64_to_double(__le64_to_cpu(agg->max_util.u.i));
770 static void convert_dus(struct disk_util_stat *dus)
774 for (i = 0; i < 2; i++) {
775 dus->ios[i] = le32_to_cpu(dus->ios[i]);
776 dus->merges[i] = le32_to_cpu(dus->merges[i]);
777 dus->sectors[i] = le64_to_cpu(dus->sectors[i]);
778 dus->ticks[i] = le32_to_cpu(dus->ticks[i]);
781 dus->io_ticks = le32_to_cpu(dus->io_ticks);
782 dus->time_in_queue = le32_to_cpu(dus->time_in_queue);
783 dus->msec = le64_to_cpu(dus->msec);
786 static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd)
788 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
790 if (!client->disk_stats_shown) {
791 client->disk_stats_shown = 1;
792 log_info("\nDisk stats (read/write):\n");
795 print_disk_util(&du->dus, &du->agg, terse_output);
798 static void convert_jobs_eta(struct jobs_eta *je)
802 je->nr_running = le32_to_cpu(je->nr_running);
803 je->nr_ramp = le32_to_cpu(je->nr_ramp);
804 je->nr_pending = le32_to_cpu(je->nr_pending);
805 je->files_open = le32_to_cpu(je->files_open);
807 for (i = 0; i < 2; i++) {
808 je->m_rate[i] = le32_to_cpu(je->m_rate[i]);
809 je->t_rate[i] = le32_to_cpu(je->t_rate[i]);
810 je->m_iops[i] = le32_to_cpu(je->m_iops[i]);
811 je->t_iops[i] = le32_to_cpu(je->t_iops[i]);
812 je->rate[i] = le32_to_cpu(je->rate[i]);
813 je->iops[i] = le32_to_cpu(je->iops[i]);
816 je->elapsed_sec = le64_to_cpu(je->elapsed_sec);
817 je->eta_sec = le64_to_cpu(je->eta_sec);
818 je->nr_threads = le32_to_cpu(je->nr_threads);
821 void fio_client_sum_jobs_eta(struct jobs_eta *dst, struct jobs_eta *je)
825 dst->nr_running += je->nr_running;
826 dst->nr_ramp += je->nr_ramp;
827 dst->nr_pending += je->nr_pending;
828 dst->files_open += je->files_open;
830 for (i = 0; i < 2; i++) {
831 dst->m_rate[i] += je->m_rate[i];
832 dst->t_rate[i] += je->t_rate[i];
833 dst->m_iops[i] += je->m_iops[i];
834 dst->t_iops[i] += je->t_iops[i];
835 dst->rate[i] += je->rate[i];
836 dst->iops[i] += je->iops[i];
839 dst->elapsed_sec += je->elapsed_sec;
841 if (je->eta_sec > dst->eta_sec)
842 dst->eta_sec = je->eta_sec;
844 dst->nr_threads += je->nr_threads;
845 /* we need to handle je->run_str too ... */
848 void fio_client_dec_jobs_eta(struct client_eta *eta, client_eta_op eta_fn)
850 if (!--eta->pending) {
856 static void remove_reply_cmd(struct fio_client *client, struct fio_net_cmd *cmd)
858 struct fio_net_int_cmd *icmd = NULL;
859 struct flist_head *entry;
861 flist_for_each(entry, &client->cmd_list) {
862 icmd = flist_entry(entry, struct fio_net_int_cmd, list);
864 if (cmd->tag == (uintptr_t) icmd)
871 log_err("fio: client: unable to find matching tag\n");
875 flist_del(&icmd->list);
876 cmd->tag = icmd->saved_tag;
880 static void handle_eta(struct fio_client *client, struct fio_net_cmd *cmd)
882 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
883 struct client_eta *eta = (struct client_eta *) (uintptr_t) cmd->tag;
885 dprint(FD_NET, "client: got eta tag %p, %d\n", eta, eta->pending);
887 assert(client->eta_in_flight == eta);
889 client->eta_in_flight = NULL;
890 flist_del_init(&client->eta_list);
892 if (client->ops->jobs_eta)
893 client->ops->jobs_eta(client, je);
895 fio_client_sum_jobs_eta(&eta->eta, je);
896 fio_client_dec_jobs_eta(eta, client->ops->eta);
899 static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd)
901 struct cmd_probe_pdu *probe = (struct cmd_probe_pdu *) cmd->payload;
902 const char *os, *arch;
905 os = fio_get_os_string(probe->os);
909 arch = fio_get_arch_string(probe->arch);
913 sprintf(bit, "%d-bit", probe->bpp * 8);
915 log_info("hostname=%s, be=%u, %s, os=%s, arch=%s, fio=%u.%u.%u\n",
916 probe->hostname, probe->bigendian, bit, os, arch,
917 probe->fio_major, probe->fio_minor, probe->fio_patch);
920 client->name = strdup((char *) probe->hostname);
923 static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd)
925 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
927 client->state = Client_started;
928 client->jobs = pdu->jobs;
931 static void handle_stop(struct fio_client *client, struct fio_net_cmd *cmd)
934 log_info("client <%s>: exited with error %d\n", client->hostname, client->error);
937 static void convert_stop(struct fio_net_cmd *cmd)
939 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
941 pdu->error = le32_to_cpu(pdu->error);
944 static void convert_text(struct fio_net_cmd *cmd)
946 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
948 pdu->level = le32_to_cpu(pdu->level);
949 pdu->buf_len = le32_to_cpu(pdu->buf_len);
950 pdu->log_sec = le64_to_cpu(pdu->log_sec);
951 pdu->log_usec = le64_to_cpu(pdu->log_usec);
955 * This has been compressed on the server side, since it can be big.
958 static struct cmd_iolog_pdu *convert_iolog(struct fio_net_cmd *cmd)
960 struct cmd_iolog_pdu *pdu = (struct cmd_iolog_pdu *) cmd->payload;
961 struct cmd_iolog_pdu *ret;
967 stream.zalloc = Z_NULL;
968 stream.zfree = Z_NULL;
969 stream.opaque = Z_NULL;
971 stream.next_in = Z_NULL;
973 if (inflateInit(&stream) != Z_OK)
977 * Everything beyond the first entry is compressed.
979 nr_samples = le32_to_cpu(pdu->nr_samples);
981 total = sizeof(*pdu) + nr_samples * sizeof(struct io_sample);
983 ret->nr_samples = nr_samples;
984 p = (void *) ret + sizeof(pdu->nr_samples);
986 stream.avail_in = cmd->pdu_len - sizeof(pdu->nr_samples);
987 stream.next_in = (void *) pdu + sizeof(pdu->nr_samples);
988 while (stream.avail_in) {
989 unsigned int this_chunk = 65536;
990 unsigned int this_len;
993 if (this_chunk > total)
996 stream.avail_out = this_chunk;
998 err = inflate(&stream, Z_NO_FLUSH);
1000 log_err("fio: inflate error %d\n", err);
1004 this_len = this_chunk - stream.avail_out;
1009 ret->log_type = cpu_to_le32(ret->log_type);
1011 inflateEnd(&stream);
1015 int fio_handle_client(struct fio_client *client)
1017 struct client_ops *ops = client->ops;
1018 struct fio_net_cmd *cmd;
1020 dprint(FD_NET, "client: handle %s\n", client->hostname);
1022 cmd = fio_net_recv_cmd(client->fd);
1026 dprint(FD_NET, "client: got cmd op %s from %s (pdu=%u)\n",
1027 fio_server_op(cmd->opcode), client->hostname, cmd->pdu_len);
1029 switch (cmd->opcode) {
1030 case FIO_NET_CMD_QUIT:
1032 ops->quit(client, cmd);
1033 remove_client(client);
1036 case FIO_NET_CMD_TEXT:
1038 ops->text(client, cmd);
1041 case FIO_NET_CMD_DU: {
1042 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
1044 convert_dus(&du->dus);
1045 convert_agg(&du->agg);
1047 ops->disk_util(client, cmd);
1051 case FIO_NET_CMD_TS: {
1052 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
1054 convert_ts(&p->ts, &p->ts);
1055 convert_gs(&p->rs, &p->rs);
1057 ops->thread_status(client, cmd);
1061 case FIO_NET_CMD_GS: {
1062 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
1066 ops->group_stats(client, cmd);
1070 case FIO_NET_CMD_ETA: {
1071 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
1073 remove_reply_cmd(client, cmd);
1074 convert_jobs_eta(je);
1075 handle_eta(client, cmd);
1079 case FIO_NET_CMD_PROBE:
1080 remove_reply_cmd(client, cmd);
1081 ops->probe(client, cmd);
1084 case FIO_NET_CMD_SERVER_START:
1085 client->state = Client_running;
1087 ops->job_start(client, cmd);
1090 case FIO_NET_CMD_START: {
1091 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
1093 pdu->jobs = le32_to_cpu(pdu->jobs);
1094 ops->start(client, cmd);
1098 case FIO_NET_CMD_STOP: {
1099 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
1102 client->state = Client_stopped;
1103 client->error = pdu->error;
1104 ops->stop(client, cmd);
1108 case FIO_NET_CMD_ADD_JOB:
1110 ops->add_job(client, cmd);
1113 case FIO_NET_CMD_IOLOG:
1115 struct cmd_iolog_pdu *pdu;
1117 pdu = convert_iolog(cmd);
1118 ops->iolog(client, pdu);
1123 log_err("fio: unknown client op: %s\n", fio_server_op(cmd->opcode));
1131 static void request_client_etas(struct client_ops *ops)
1133 struct fio_client *client;
1134 struct flist_head *entry;
1135 struct client_eta *eta;
1138 dprint(FD_NET, "client: request eta (%d)\n", nr_clients);
1140 eta = malloc(sizeof(*eta));
1141 memset(&eta->eta, 0, sizeof(eta->eta));
1142 eta->pending = nr_clients;
1144 flist_for_each(entry, &client_list) {
1145 client = flist_entry(entry, struct fio_client, list);
1147 if (!flist_empty(&client->eta_list)) {
1151 if (client->state != Client_running)
1154 assert(!client->eta_in_flight);
1155 flist_add_tail(&client->eta_list, &eta_list);
1156 client->eta_in_flight = eta;
1157 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_SEND_ETA,
1158 (uintptr_t) eta, &client->cmd_list);
1162 fio_client_dec_jobs_eta(eta, ops->eta);
1164 dprint(FD_NET, "client: requested eta tag %p\n", eta);
1167 static int client_check_cmd_timeout(struct fio_client *client,
1168 struct timeval *now)
1170 struct fio_net_int_cmd *cmd;
1171 struct flist_head *entry, *tmp;
1174 flist_for_each_safe(entry, tmp, &client->cmd_list) {
1175 cmd = flist_entry(entry, struct fio_net_int_cmd, list);
1177 if (mtime_since(&cmd->tv, now) < FIO_NET_CLIENT_TIMEOUT)
1180 log_err("fio: client %s, timeout on cmd %s\n", client->hostname,
1181 fio_server_op(cmd->cmd.opcode));
1182 flist_del(&cmd->list);
1187 return flist_empty(&client->cmd_list) && ret;
1190 static int fio_check_clients_timed_out(void)
1192 struct fio_client *client;
1193 struct flist_head *entry, *tmp;
1197 gettimeofday(&tv, NULL);
1199 flist_for_each_safe(entry, tmp, &client_list) {
1200 client = flist_entry(entry, struct fio_client, list);
1202 if (flist_empty(&client->cmd_list))
1205 if (!client_check_cmd_timeout(client, &tv))
1208 if (client->ops->timed_out)
1209 client->ops->timed_out(client);
1211 log_err("fio: client %s timed out\n", client->hostname);
1213 remove_client(client);
1220 int fio_handle_clients(struct client_ops *ops)
1222 struct pollfd *pfds;
1223 int i, ret = 0, retval = 0;
1225 gettimeofday(&eta_tv, NULL);
1227 pfds = malloc(nr_clients * sizeof(struct pollfd));
1229 sum_stat_clients = nr_clients;
1230 init_thread_stat(&client_ts);
1231 init_group_run_stat(&client_gs);
1233 while (!exit_backend && nr_clients) {
1234 struct flist_head *entry, *tmp;
1235 struct fio_client *client;
1238 flist_for_each_safe(entry, tmp, &client_list) {
1239 client = flist_entry(entry, struct fio_client, list);
1241 if (!client->sent_job && !client->ops->stay_connected &&
1242 flist_empty(&client->cmd_list)) {
1243 remove_client(client);
1247 pfds[i].fd = client->fd;
1248 pfds[i].events = POLLIN;
1255 assert(i == nr_clients);
1260 gettimeofday(&tv, NULL);
1261 if (mtime_since(&eta_tv, &tv) >= ops->eta_msec) {
1262 request_client_etas(ops);
1263 memcpy(&eta_tv, &tv, sizeof(tv));
1265 if (fio_check_clients_timed_out())
1269 ret = poll(pfds, nr_clients, 100);
1273 log_err("fio: poll clients: %s\n", strerror(errno));
1279 for (i = 0; i < nr_clients; i++) {
1280 if (!(pfds[i].revents & POLLIN))
1283 client = find_client_by_fd(pfds[i].fd);
1285 log_err("fio: unknown client fd %d\n", pfds[i].fd);
1288 if (!fio_handle_client(client)) {
1289 log_info("client: host=%s disconnected\n",
1291 remove_client(client);
1293 } else if (client->error)
1295 fio_put_client(client);