11 #include <sys/socket.h>
13 #include <netinet/in.h>
14 #include <arpa/inet.h>
29 struct flist_head list;
30 struct flist_head hash_list;
31 struct flist_head arg_list;
33 struct sockaddr_in addr;
34 struct sockaddr_in6 addr6;
35 struct sockaddr_un addr_un;
56 struct flist_head eta_list;
57 struct client_eta *eta_in_flight;
59 struct flist_head cmd_list;
65 unsigned int nr_ini_file;
68 static struct timeval eta_tv;
79 static FLIST_HEAD(client_list);
80 static FLIST_HEAD(eta_list);
82 static FLIST_HEAD(arg_list);
84 static struct thread_stat client_ts;
85 static struct group_run_stats client_gs;
86 static int sum_stat_clients = 0;
87 static int sum_stat_nr;
88 static int do_output_all_clients;
90 #define FIO_CLIENT_HASH_BITS 7
91 #define FIO_CLIENT_HASH_SZ (1 << FIO_CLIENT_HASH_BITS)
92 #define FIO_CLIENT_HASH_MASK (FIO_CLIENT_HASH_SZ - 1)
93 static struct flist_head client_hash[FIO_CLIENT_HASH_SZ];
95 static int handle_client(struct fio_client *client);
96 static void dec_jobs_eta(struct client_eta *eta);
98 static void fio_client_add_hash(struct fio_client *client)
100 int bucket = hash_long(client->fd, FIO_CLIENT_HASH_BITS);
102 bucket &= FIO_CLIENT_HASH_MASK;
103 flist_add(&client->hash_list, &client_hash[bucket]);
106 static void fio_client_remove_hash(struct fio_client *client)
108 if (!flist_empty(&client->hash_list))
109 flist_del_init(&client->hash_list);
112 static void fio_init fio_client_hash_init(void)
116 for (i = 0; i < FIO_CLIENT_HASH_SZ; i++)
117 INIT_FLIST_HEAD(&client_hash[i]);
120 static struct fio_client *find_client_by_fd(int fd)
122 int bucket = hash_long(fd, FIO_CLIENT_HASH_BITS) & FIO_CLIENT_HASH_MASK;
123 struct fio_client *client;
124 struct flist_head *entry;
126 flist_for_each(entry, &client_hash[bucket]) {
127 client = flist_entry(entry, struct fio_client, hash_list);
129 if (client->fd == fd) {
138 static void remove_client(struct fio_client *client)
140 assert(client->refs);
145 dprint(FD_NET, "client: removed <%s>\n", client->hostname);
146 flist_del(&client->list);
148 fio_client_remove_hash(client);
150 if (!flist_empty(&client->eta_list)) {
151 flist_del_init(&client->eta_list);
152 dec_jobs_eta(client->eta_in_flight);
155 free(client->hostname);
160 while (client->nr_ini_file)
161 free(client->ini_file[--client->nr_ini_file]);
162 if (client->ini_file)
163 free(client->ini_file);
165 if (!client->did_stat)
166 sum_stat_clients -= client->nr_stat;
172 static void put_client(struct fio_client *client)
174 remove_client(client);
177 static void __fio_client_add_cmd_option(struct fio_client *client,
182 index = client->argc++;
183 client->argv = realloc(client->argv, sizeof(char *) * client->argc);
184 client->argv[index] = strdup(opt);
185 dprint(FD_NET, "client: add cmd %d: %s\n", index, opt);
188 void fio_client_add_cmd_option(void *cookie, const char *opt)
190 struct fio_client *client = cookie;
191 struct flist_head *entry;
196 __fio_client_add_cmd_option(client, opt);
199 * Duplicate arguments to shared client group
201 flist_for_each(entry, &arg_list) {
202 client = flist_entry(entry, struct fio_client, arg_list);
204 __fio_client_add_cmd_option(client, opt);
208 void fio_client_add_ini_file(void *cookie, const char *ini_file)
210 struct fio_client *client = cookie;
213 dprint(FD_NET, "client <%s>: add ini %s\n", client->hostname, ini_file);
215 new_size = (client->nr_ini_file + 1) * sizeof(char *);
216 client->ini_file = realloc(client->ini_file, new_size);
217 client->ini_file[client->nr_ini_file] = strdup(ini_file);
218 client->nr_ini_file++;
221 int fio_client_add(const char *hostname, void **cookie)
223 struct fio_client *existing = *cookie;
224 struct fio_client *client;
228 * We always add our "exec" name as the option, hence 1
231 if (existing->argc == 1)
232 flist_add_tail(&existing->arg_list, &arg_list);
234 while (!flist_empty(&arg_list))
235 flist_del_init(arg_list.next);
239 client = malloc(sizeof(*client));
240 memset(client, 0, sizeof(*client));
242 INIT_FLIST_HEAD(&client->list);
243 INIT_FLIST_HEAD(&client->hash_list);
244 INIT_FLIST_HEAD(&client->arg_list);
245 INIT_FLIST_HEAD(&client->eta_list);
246 INIT_FLIST_HEAD(&client->cmd_list);
248 if (fio_server_parse_string(hostname, &client->hostname,
249 &client->is_sock, &client->port,
250 &client->addr.sin_addr,
251 &client->addr6.sin6_addr,
258 __fio_client_add_cmd_option(client, "fio");
260 flist_add(&client->list, &client_list);
262 dprint(FD_NET, "client: added <%s>\n", client->hostname);
267 static int fio_client_connect_ip(struct fio_client *client)
269 struct sockaddr *addr;
274 client->addr6.sin6_family = AF_INET6;
275 client->addr6.sin6_port = htons(client->port);
277 addr = (struct sockaddr *) &client->addr6;
278 socklen = sizeof(client->addr6);
280 client->addr.sin_family = AF_INET;
281 client->addr.sin_port = htons(client->port);
283 addr = (struct sockaddr *) &client->addr;
284 socklen = sizeof(client->addr);
287 fd = socket(domain, SOCK_STREAM, 0);
289 log_err("fio: socket: %s\n", strerror(errno));
293 if (connect(fd, addr, socklen) < 0) {
294 log_err("fio: connect: %s\n", strerror(errno));
295 log_err("fio: failed to connect to %s:%u\n", client->hostname,
304 static int fio_client_connect_sock(struct fio_client *client)
306 struct sockaddr_un *addr = &client->addr_un;
310 memset(addr, 0, sizeof(*addr));
311 addr->sun_family = AF_UNIX;
312 strcpy(addr->sun_path, client->hostname);
314 fd = socket(AF_UNIX, SOCK_STREAM, 0);
316 log_err("fio: socket: %s\n", strerror(errno));
320 len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
321 if (connect(fd, (struct sockaddr *) addr, len) < 0) {
322 log_err("fio: connect; %s\n", strerror(errno));
330 static int fio_client_connect(struct fio_client *client)
334 dprint(FD_NET, "client: connect to host %s\n", client->hostname);
337 fd = fio_client_connect_sock(client);
339 fd = fio_client_connect_ip(client);
341 dprint(FD_NET, "client: %s connected %d\n", client->hostname, fd);
347 fio_client_add_hash(client);
348 client->state = Client_connected;
352 void fio_clients_terminate(void)
354 struct flist_head *entry;
355 struct fio_client *client;
357 dprint(FD_NET, "client: terminate clients\n");
359 flist_for_each(entry, &client_list) {
360 client = flist_entry(entry, struct fio_client, list);
362 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_QUIT, 0, NULL);
366 static void sig_int(int sig)
368 dprint(FD_NET, "client: got signal %d\n", sig);
369 fio_clients_terminate();
372 static void sig_show_status(int sig)
374 show_running_run_stats();
377 static void client_signal_handler(void)
379 struct sigaction act;
381 memset(&act, 0, sizeof(act));
382 act.sa_handler = sig_int;
383 act.sa_flags = SA_RESTART;
384 sigaction(SIGINT, &act, NULL);
386 memset(&act, 0, sizeof(act));
387 act.sa_handler = sig_int;
388 act.sa_flags = SA_RESTART;
389 sigaction(SIGTERM, &act, NULL);
391 /* Windows uses SIGBREAK as a quit signal from other applications */
393 memset(&act, 0, sizeof(act));
394 act.sa_handler = sig_int;
395 act.sa_flags = SA_RESTART;
396 sigaction(SIGBREAK, &act, NULL);
399 memset(&act, 0, sizeof(act));
400 act.sa_handler = sig_show_status;
401 act.sa_flags = SA_RESTART;
402 sigaction(SIGUSR1, &act, NULL);
405 static void probe_client(struct fio_client *client)
407 dprint(FD_NET, "client: send probe\n");
409 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_PROBE, 0, &client->cmd_list);
412 static int send_client_cmd_line(struct fio_client *client)
414 struct cmd_single_line_pdu *cslp;
415 struct cmd_line_pdu *clp;
416 unsigned long offset;
422 dprint(FD_NET, "client: send cmdline %d\n", client->argc);
424 lens = malloc(client->argc * sizeof(unsigned int));
427 * Find out how much mem we need
429 for (i = 0, mem = 0; i < client->argc; i++) {
430 lens[i] = strlen(client->argv[i]) + 1;
435 * We need one cmd_line_pdu, and argc number of cmd_single_line_pdu
437 mem += sizeof(*clp) + (client->argc * sizeof(*cslp));
441 offset = sizeof(*clp);
443 for (i = 0; i < client->argc; i++) {
444 uint16_t arg_len = lens[i];
447 strcpy((char *) cslp->text, client->argv[i]);
448 cslp->len = cpu_to_le16(arg_len);
449 offset += sizeof(*cslp) + arg_len;
453 clp->lines = cpu_to_le16(client->argc);
454 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOBLINE, pdu, mem, 0);
459 int fio_clients_connect(void)
461 struct fio_client *client;
462 struct flist_head *entry, *tmp;
467 WSAStartup(MAKEWORD(2,2), &wsd);
470 dprint(FD_NET, "client: connect all\n");
472 client_signal_handler();
474 flist_for_each_safe(entry, tmp, &client_list) {
475 client = flist_entry(entry, struct fio_client, list);
477 ret = fio_client_connect(client);
479 remove_client(client);
483 probe_client(client);
485 if (client->argc > 1)
486 send_client_cmd_line(client);
493 * Send file contents to server backend. We could use sendfile(), but to remain
494 * more portable lets just read/write the darn thing.
496 static int fio_client_send_ini(struct fio_client *client, const char *filename)
503 dprint(FD_NET, "send ini %s to %s\n", filename, client->hostname);
505 fd = open(filename, O_RDONLY);
507 log_err("fio: job file <%s> open: %s\n", filename, strerror(errno));
511 if (fstat(fd, &sb) < 0) {
512 log_err("fio: job file stat: %s\n", strerror(errno));
517 buf = malloc(sb.st_size);
522 ret = read(fd, p, len);
531 else if (errno == EAGAIN || errno == EINTR)
536 log_err("fio: failed reading job file %s\n", filename);
542 client->sent_job = 1;
543 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, buf, sb.st_size, 0);
549 int fio_clients_send_ini(const char *filename)
551 struct fio_client *client;
552 struct flist_head *entry, *tmp;
554 flist_for_each_safe(entry, tmp, &client_list) {
555 client = flist_entry(entry, struct fio_client, list);
557 if (client->nr_ini_file) {
560 for (i = 0; i < client->nr_ini_file; i++) {
561 const char *ini = client->ini_file[i];
563 if (fio_client_send_ini(client, ini)) {
564 remove_client(client);
568 } else if (!filename || fio_client_send_ini(client, filename))
569 remove_client(client);
571 client->sent_job = 1;
577 static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
579 dst->max_val = le64_to_cpu(src->max_val);
580 dst->min_val = le64_to_cpu(src->min_val);
581 dst->samples = le64_to_cpu(src->samples);
584 * Floats arrive as IEEE 754 encoded uint64_t, convert back to double
586 dst->mean.u.f = fio_uint64_to_double(le64_to_cpu(dst->mean.u.i));
587 dst->S.u.f = fio_uint64_to_double(le64_to_cpu(dst->S.u.i));
590 static void convert_ts(struct thread_stat *dst, struct thread_stat *src)
594 dst->error = le32_to_cpu(src->error);
595 dst->groupid = le32_to_cpu(src->groupid);
596 dst->pid = le32_to_cpu(src->pid);
597 dst->members = le32_to_cpu(src->members);
598 dst->unified_rw_rep = le32_to_cpu(src->unified_rw_rep);
600 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
601 convert_io_stat(&dst->clat_stat[i], &src->clat_stat[i]);
602 convert_io_stat(&dst->slat_stat[i], &src->slat_stat[i]);
603 convert_io_stat(&dst->lat_stat[i], &src->lat_stat[i]);
604 convert_io_stat(&dst->bw_stat[i], &src->bw_stat[i]);
607 dst->usr_time = le64_to_cpu(src->usr_time);
608 dst->sys_time = le64_to_cpu(src->sys_time);
609 dst->ctx = le64_to_cpu(src->ctx);
610 dst->minf = le64_to_cpu(src->minf);
611 dst->majf = le64_to_cpu(src->majf);
612 dst->clat_percentiles = le64_to_cpu(src->clat_percentiles);
614 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
615 fio_fp64_t *fps = &src->percentile_list[i];
616 fio_fp64_t *fpd = &dst->percentile_list[i];
618 fpd->u.f = fio_uint64_to_double(le64_to_cpu(fps->u.i));
621 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
622 dst->io_u_map[i] = le32_to_cpu(src->io_u_map[i]);
623 dst->io_u_submit[i] = le32_to_cpu(src->io_u_submit[i]);
624 dst->io_u_complete[i] = le32_to_cpu(src->io_u_complete[i]);
627 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
628 dst->io_u_lat_u[i] = le32_to_cpu(src->io_u_lat_u[i]);
629 dst->io_u_lat_m[i] = le32_to_cpu(src->io_u_lat_m[i]);
632 for (i = 0; i < DDIR_RWDIR_CNT; i++)
633 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
634 dst->io_u_plat[i][j] = le32_to_cpu(src->io_u_plat[i][j]);
636 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
637 dst->total_io_u[i] = le64_to_cpu(src->total_io_u[i]);
638 dst->short_io_u[i] = le64_to_cpu(src->short_io_u[i]);
641 dst->total_submit = le64_to_cpu(src->total_submit);
642 dst->total_complete = le64_to_cpu(src->total_complete);
644 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
645 dst->io_bytes[i] = le64_to_cpu(src->io_bytes[i]);
646 dst->runtime[i] = le64_to_cpu(src->runtime[i]);
649 dst->total_run_time = le64_to_cpu(src->total_run_time);
650 dst->continue_on_error = le16_to_cpu(src->continue_on_error);
651 dst->total_err_count = le64_to_cpu(src->total_err_count);
652 dst->first_error = le32_to_cpu(src->first_error);
653 dst->kb_base = le32_to_cpu(src->kb_base);
654 dst->unit_base = le32_to_cpu(src->unit_base);
657 static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
661 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
662 dst->max_run[i] = le64_to_cpu(src->max_run[i]);
663 dst->min_run[i] = le64_to_cpu(src->min_run[i]);
664 dst->max_bw[i] = le64_to_cpu(src->max_bw[i]);
665 dst->min_bw[i] = le64_to_cpu(src->min_bw[i]);
666 dst->io_kb[i] = le64_to_cpu(src->io_kb[i]);
667 dst->agg[i] = le64_to_cpu(src->agg[i]);
670 dst->kb_base = le32_to_cpu(src->kb_base);
671 dst->unit_base = le32_to_cpu(src->unit_base);
672 dst->groupid = le32_to_cpu(src->groupid);
673 dst->unified_rw_rep = le32_to_cpu(src->unified_rw_rep);
676 static void handle_ts(struct fio_client *client, struct fio_net_cmd *cmd)
678 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
680 convert_ts(&p->ts, &p->ts);
681 convert_gs(&p->rs, &p->rs);
683 show_thread_status(&p->ts, &p->rs);
684 client->did_stat = 1;
686 if (!do_output_all_clients)
689 sum_thread_stats(&client_ts, &p->ts, sum_stat_nr);
690 sum_group_stats(&client_gs, &p->rs);
693 client_ts.groupid = p->ts.groupid;
694 client_ts.unified_rw_rep = p->ts.unified_rw_rep;
696 if (++sum_stat_nr == sum_stat_clients) {
697 strcpy(client_ts.name, "All clients");
698 show_thread_status(&client_ts, &client_gs);
702 static void handle_gs(struct fio_net_cmd *cmd)
704 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
707 show_group_stats(gs);
710 static void convert_agg(struct disk_util_agg *agg)
714 for (i = 0; i < 2; i++) {
715 agg->ios[i] = le32_to_cpu(agg->ios[i]);
716 agg->merges[i] = le32_to_cpu(agg->merges[i]);
717 agg->sectors[i] = le64_to_cpu(agg->sectors[i]);
718 agg->ticks[i] = le32_to_cpu(agg->ticks[i]);
721 agg->io_ticks = le32_to_cpu(agg->io_ticks);
722 agg->time_in_queue = le32_to_cpu(agg->time_in_queue);
723 agg->slavecount = le32_to_cpu(agg->slavecount);
724 agg->max_util.u.f = fio_uint64_to_double(__le64_to_cpu(agg->max_util.u.i));
727 static void convert_dus(struct disk_util_stat *dus)
731 for (i = 0; i < 2; i++) {
732 dus->ios[i] = le32_to_cpu(dus->ios[i]);
733 dus->merges[i] = le32_to_cpu(dus->merges[i]);
734 dus->sectors[i] = le64_to_cpu(dus->sectors[i]);
735 dus->ticks[i] = le32_to_cpu(dus->ticks[i]);
738 dus->io_ticks = le32_to_cpu(dus->io_ticks);
739 dus->time_in_queue = le32_to_cpu(dus->time_in_queue);
740 dus->msec = le64_to_cpu(dus->msec);
743 static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd)
745 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
747 convert_dus(&du->dus);
748 convert_agg(&du->agg);
750 if (!client->disk_stats_shown) {
751 client->disk_stats_shown = 1;
752 log_info("\nDisk stats (read/write):\n");
755 print_disk_util(&du->dus, &du->agg, output_format == FIO_OUTPUT_TERSE);
758 static void convert_jobs_eta(struct jobs_eta *je)
762 je->nr_running = le32_to_cpu(je->nr_running);
763 je->nr_ramp = le32_to_cpu(je->nr_ramp);
764 je->nr_pending = le32_to_cpu(je->nr_pending);
765 je->files_open = le32_to_cpu(je->files_open);
766 je->m_rate = le32_to_cpu(je->m_rate);
767 je->t_rate = le32_to_cpu(je->t_rate);
768 je->m_iops = le32_to_cpu(je->m_iops);
769 je->t_iops = le32_to_cpu(je->t_iops);
771 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
772 je->rate[i] = le32_to_cpu(je->rate[i]);
773 je->iops[i] = le32_to_cpu(je->iops[i]);
776 je->elapsed_sec = le64_to_cpu(je->elapsed_sec);
777 je->eta_sec = le64_to_cpu(je->eta_sec);
778 je->is_pow2 = le32_to_cpu(je->is_pow2);
779 je->unit_base = le32_to_cpu(je->unit_base);
782 static void sum_jobs_eta(struct jobs_eta *dst, struct jobs_eta *je)
786 dst->nr_running += je->nr_running;
787 dst->nr_ramp += je->nr_ramp;
788 dst->nr_pending += je->nr_pending;
789 dst->files_open += je->files_open;
790 dst->m_rate += je->m_rate;
791 dst->t_rate += je->t_rate;
792 dst->m_iops += je->m_iops;
793 dst->t_iops += je->t_iops;
795 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
796 dst->rate[i] += je->rate[i];
797 dst->iops[i] += je->iops[i];
800 dst->elapsed_sec += je->elapsed_sec;
802 if (je->eta_sec > dst->eta_sec)
803 dst->eta_sec = je->eta_sec;
806 static void dec_jobs_eta(struct client_eta *eta)
808 if (!--eta->pending) {
809 display_thread_status(&eta->eta);
814 static void remove_reply_cmd(struct fio_client *client, struct fio_net_cmd *cmd)
816 struct fio_net_int_cmd *icmd = NULL;
817 struct flist_head *entry;
819 flist_for_each(entry, &client->cmd_list) {
820 icmd = flist_entry(entry, struct fio_net_int_cmd, list);
822 if (cmd->tag == (uintptr_t) icmd)
829 log_err("fio: client: unable to find matching tag\n");
833 flist_del(&icmd->list);
834 cmd->tag = icmd->saved_tag;
838 static void handle_eta(struct fio_client *client, struct fio_net_cmd *cmd)
840 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
841 struct client_eta *eta = (struct client_eta *) (uintptr_t) cmd->tag;
843 dprint(FD_NET, "client: got eta tag %p, %d\n", eta, eta->pending);
845 assert(client->eta_in_flight == eta);
847 client->eta_in_flight = NULL;
848 flist_del_init(&client->eta_list);
850 convert_jobs_eta(je);
851 sum_jobs_eta(&eta->eta, je);
855 static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd)
857 struct cmd_probe_pdu *probe = (struct cmd_probe_pdu *) cmd->payload;
858 const char *os, *arch;
861 os = fio_get_os_string(probe->os);
865 arch = fio_get_arch_string(probe->arch);
869 sprintf(bit, "%d-bit", probe->bpp * 8);
871 log_info("hostname=%s, be=%u, %s, os=%s, arch=%s, fio=%s\n",
872 probe->hostname, probe->bigendian, bit, os, arch,
876 client->name = strdup((char *) probe->hostname);
879 static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd)
881 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
883 client->state = Client_started;
884 client->jobs = le32_to_cpu(pdu->jobs);
885 client->nr_stat = le32_to_cpu(pdu->stat_outputs);
887 if (sum_stat_clients > 1)
888 do_output_all_clients = 1;
890 sum_stat_clients += client->nr_stat;
893 static void handle_stop(struct fio_client *client, struct fio_net_cmd *cmd)
895 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
897 client->state = Client_stopped;
898 client->error = le32_to_cpu(pdu->error);
901 log_info("client <%s>: exited with error %d\n", client->hostname, client->error);
904 static int handle_client(struct fio_client *client)
906 struct fio_net_cmd *cmd;
908 dprint(FD_NET, "client: handle %s\n", client->hostname);
910 cmd = fio_net_recv_cmd(client->fd);
914 dprint(FD_NET, "client: got cmd op %s from %s\n",
915 fio_server_op(cmd->opcode), client->hostname);
917 switch (cmd->opcode) {
918 case FIO_NET_CMD_QUIT:
919 remove_client(client);
922 case FIO_NET_CMD_TEXT: {
923 const char *buf = (const char *) cmd->payload;
927 name = client->name ? client->name : client->hostname;
929 if (!client->skip_newline)
930 fprintf(f_out, "<%s> ", name);
931 ret = fwrite(buf, cmd->pdu_len, 1, f_out);
933 client->skip_newline = strchr(buf, '\n') == NULL;
938 handle_du(client, cmd);
942 handle_ts(client, cmd);
949 case FIO_NET_CMD_ETA:
950 remove_reply_cmd(client, cmd);
951 handle_eta(client, cmd);
954 case FIO_NET_CMD_PROBE:
955 remove_reply_cmd(client, cmd);
956 handle_probe(client, cmd);
959 case FIO_NET_CMD_RUN:
960 client->state = Client_running;
963 case FIO_NET_CMD_START:
964 handle_start(client, cmd);
967 case FIO_NET_CMD_STOP:
968 handle_stop(client, cmd);
972 log_err("fio: unknown client op: %s\n", fio_server_op(cmd->opcode));
980 static void request_client_etas(void)
982 struct fio_client *client;
983 struct flist_head *entry;
984 struct client_eta *eta;
987 dprint(FD_NET, "client: request eta (%d)\n", nr_clients);
989 eta = malloc(sizeof(*eta));
990 memset(&eta->eta, 0, sizeof(eta->eta));
991 eta->pending = nr_clients;
993 flist_for_each(entry, &client_list) {
994 client = flist_entry(entry, struct fio_client, list);
996 if (!flist_empty(&client->eta_list)) {
1000 if (client->state != Client_running)
1003 assert(!client->eta_in_flight);
1004 flist_add_tail(&client->eta_list, &eta_list);
1005 client->eta_in_flight = eta;
1006 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_SEND_ETA,
1007 (uintptr_t) eta, &client->cmd_list);
1013 dprint(FD_NET, "client: requested eta tag %p\n", eta);
1016 static int client_check_cmd_timeout(struct fio_client *client,
1017 struct timeval *now)
1019 struct fio_net_int_cmd *cmd;
1020 struct flist_head *entry, *tmp;
1023 flist_for_each_safe(entry, tmp, &client->cmd_list) {
1024 cmd = flist_entry(entry, struct fio_net_int_cmd, list);
1026 if (mtime_since(&cmd->tv, now) < FIO_NET_CLIENT_TIMEOUT)
1029 log_err("fio: client %s, timeout on cmd %s\n", client->hostname,
1030 fio_server_op(cmd->cmd.opcode));
1031 flist_del(&cmd->list);
1036 return flist_empty(&client->cmd_list) && ret;
1039 static int fio_client_timed_out(void)
1041 struct fio_client *client;
1042 struct flist_head *entry, *tmp;
1046 fio_gettime(&tv, NULL);
1048 flist_for_each_safe(entry, tmp, &client_list) {
1049 client = flist_entry(entry, struct fio_client, list);
1051 if (flist_empty(&client->cmd_list))
1054 if (!client_check_cmd_timeout(client, &tv))
1057 log_err("fio: client %s timed out\n", client->hostname);
1058 remove_client(client);
1065 int fio_handle_clients(void)
1067 struct pollfd *pfds;
1068 int i, ret = 0, retval = 0;
1070 fio_gettime(&eta_tv, NULL);
1072 pfds = malloc(nr_clients * sizeof(struct pollfd));
1074 init_thread_stat(&client_ts);
1075 init_group_run_stat(&client_gs);
1077 while (!exit_backend && nr_clients) {
1078 struct flist_head *entry, *tmp;
1079 struct fio_client *client;
1082 flist_for_each_safe(entry, tmp, &client_list) {
1083 client = flist_entry(entry, struct fio_client, list);
1085 if (!client->sent_job &&
1086 flist_empty(&client->cmd_list)) {
1087 remove_client(client);
1091 pfds[i].fd = client->fd;
1092 pfds[i].events = POLLIN;
1099 assert(i == nr_clients);
1104 fio_gettime(&tv, NULL);
1105 if (mtime_since(&eta_tv, &tv) >= 900) {
1106 request_client_etas();
1107 memcpy(&eta_tv, &tv, sizeof(tv));
1109 if (fio_client_timed_out())
1113 ret = poll(pfds, nr_clients, 100);
1117 log_err("fio: poll clients: %s\n", strerror(errno));
1123 for (i = 0; i < nr_clients; i++) {
1124 if (!(pfds[i].revents & POLLIN))
1127 client = find_client_by_fd(pfds[i].fd);
1129 log_err("fio: unknown client fd %d\n", pfds[i].fd);
1132 if (!handle_client(client)) {
1133 log_info("client: host=%s disconnected\n",
1135 remove_client(client);
1137 } else if (client->error)