11 #include <sys/socket.h>
13 #include <netinet/in.h>
14 #include <arpa/inet.h>
24 static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd);
25 static void handle_ts(struct fio_client *client, struct fio_net_cmd *cmd);
26 static void handle_gs(struct fio_client *client, struct fio_net_cmd *cmd);
27 static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd);
28 static void handle_text(struct fio_client *client, struct fio_net_cmd *cmd);
30 struct client_ops fio_client_ops = {
31 .text_op = handle_text,
32 .disk_util = handle_du,
33 .thread_status = handle_ts,
34 .group_stats = handle_gs,
35 .eta = display_thread_status,
36 .probe = handle_probe,
39 static struct timeval eta_tv;
50 static FLIST_HEAD(client_list);
51 static FLIST_HEAD(eta_list);
53 static FLIST_HEAD(arg_list);
55 struct thread_stat client_ts;
56 struct group_run_stats client_gs;
59 static int sum_stat_nr;
61 #define FIO_CLIENT_HASH_BITS 7
62 #define FIO_CLIENT_HASH_SZ (1 << FIO_CLIENT_HASH_BITS)
63 #define FIO_CLIENT_HASH_MASK (FIO_CLIENT_HASH_SZ - 1)
64 static struct flist_head client_hash[FIO_CLIENT_HASH_SZ];
66 static void fio_client_add_hash(struct fio_client *client)
68 int bucket = hash_long(client->fd, FIO_CLIENT_HASH_BITS);
70 bucket &= FIO_CLIENT_HASH_MASK;
71 flist_add(&client->hash_list, &client_hash[bucket]);
74 static void fio_client_remove_hash(struct fio_client *client)
76 if (!flist_empty(&client->hash_list))
77 flist_del_init(&client->hash_list);
80 static void fio_init fio_client_hash_init(void)
84 for (i = 0; i < FIO_CLIENT_HASH_SZ; i++)
85 INIT_FLIST_HEAD(&client_hash[i]);
88 static struct fio_client *find_client_by_fd(int fd)
90 int bucket = hash_long(fd, FIO_CLIENT_HASH_BITS) & FIO_CLIENT_HASH_MASK;
91 struct fio_client *client;
92 struct flist_head *entry;
94 flist_for_each(entry, &client_hash[bucket]) {
95 client = flist_entry(entry, struct fio_client, hash_list);
97 if (client->fd == fd) {
106 static void remove_client(struct fio_client *client)
108 assert(client->refs);
113 dprint(FD_NET, "client: removed <%s>\n", client->hostname);
114 flist_del(&client->list);
116 fio_client_remove_hash(client);
118 if (!flist_empty(&client->eta_list)) {
119 flist_del_init(&client->eta_list);
120 fio_client_dec_jobs_eta(client->eta_in_flight, client->ops->eta);
123 free(client->hostname);
134 static void put_client(struct fio_client *client)
136 remove_client(client);
139 static void __fio_client_add_cmd_option(struct fio_client *client,
144 index = client->argc++;
145 client->argv = realloc(client->argv, sizeof(char *) * client->argc);
146 client->argv[index] = strdup(opt);
147 dprint(FD_NET, "client: add cmd %d: %s\n", index, opt);
150 void fio_client_add_cmd_option(void *cookie, const char *opt)
152 struct fio_client *client = cookie;
153 struct flist_head *entry;
158 __fio_client_add_cmd_option(client, opt);
161 * Duplicate arguments to shared client group
163 flist_for_each(entry, &arg_list) {
164 client = flist_entry(entry, struct fio_client, arg_list);
166 __fio_client_add_cmd_option(client, opt);
170 struct fio_client *fio_client_add_explicit(struct client_ops *ops,
171 const char *hostname, int type,
174 struct fio_client *client;
176 client = malloc(sizeof(*client));
177 memset(client, 0, sizeof(*client));
179 INIT_FLIST_HEAD(&client->list);
180 INIT_FLIST_HEAD(&client->hash_list);
181 INIT_FLIST_HEAD(&client->arg_list);
182 INIT_FLIST_HEAD(&client->eta_list);
183 INIT_FLIST_HEAD(&client->cmd_list);
185 client->hostname = strdup(hostname);
187 if (type == Fio_client_socket)
192 ipv6 = type == Fio_client_ipv6;
193 if (fio_server_parse_host(hostname, &ipv6,
194 &client->addr.sin_addr,
195 &client->addr6.sin6_addr))
205 __fio_client_add_cmd_option(client, "fio");
207 flist_add(&client->list, &client_list);
209 dprint(FD_NET, "client: added <%s>\n", client->hostname);
216 int fio_client_add(struct client_ops *ops, const char *hostname, void **cookie)
218 struct fio_client *existing = *cookie;
219 struct fio_client *client;
223 * We always add our "exec" name as the option, hence 1
226 if (existing->argc == 1)
227 flist_add_tail(&existing->arg_list, &arg_list);
229 while (!flist_empty(&arg_list))
230 flist_del_init(arg_list.next);
234 client = malloc(sizeof(*client));
235 memset(client, 0, sizeof(*client));
237 INIT_FLIST_HEAD(&client->list);
238 INIT_FLIST_HEAD(&client->hash_list);
239 INIT_FLIST_HEAD(&client->arg_list);
240 INIT_FLIST_HEAD(&client->eta_list);
241 INIT_FLIST_HEAD(&client->cmd_list);
243 if (fio_server_parse_string(hostname, &client->hostname,
244 &client->is_sock, &client->port,
245 &client->addr.sin_addr,
246 &client->addr6.sin6_addr,
254 __fio_client_add_cmd_option(client, "fio");
256 flist_add(&client->list, &client_list);
258 dprint(FD_NET, "client: added <%s>\n", client->hostname);
263 static int fio_client_connect_ip(struct fio_client *client)
265 struct sockaddr *addr;
266 fio_socklen_t socklen;
270 client->addr6.sin6_family = AF_INET6;
271 client->addr6.sin6_port = htons(client->port);
273 addr = (struct sockaddr *) &client->addr6;
274 socklen = sizeof(client->addr6);
276 client->addr.sin_family = AF_INET;
277 client->addr.sin_port = htons(client->port);
279 addr = (struct sockaddr *) &client->addr;
280 socklen = sizeof(client->addr);
283 fd = socket(domain, SOCK_STREAM, 0);
285 log_err("fio: socket: %s\n", strerror(errno));
289 if (connect(fd, addr, socklen) < 0) {
290 log_err("fio: connect: %s\n", strerror(errno));
291 log_err("fio: failed to connect to %s:%u\n", client->hostname,
300 static int fio_client_connect_sock(struct fio_client *client)
302 struct sockaddr_un *addr = &client->addr_un;
306 memset(addr, 0, sizeof(*addr));
307 addr->sun_family = AF_UNIX;
308 strcpy(addr->sun_path, client->hostname);
310 fd = socket(AF_UNIX, SOCK_STREAM, 0);
312 log_err("fio: socket: %s\n", strerror(errno));
316 len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
317 if (connect(fd, (struct sockaddr *) addr, len) < 0) {
318 log_err("fio: connect; %s\n", strerror(errno));
326 static int fio_client_connect(struct fio_client *client)
330 dprint(FD_NET, "client: connect to host %s\n", client->hostname);
333 fd = fio_client_connect_sock(client);
335 fd = fio_client_connect_ip(client);
337 dprint(FD_NET, "client: %s connected %d\n", client->hostname, fd);
343 fio_client_add_hash(client);
344 client->state = Client_connected;
348 void fio_clients_terminate(void)
350 struct flist_head *entry;
351 struct fio_client *client;
353 dprint(FD_NET, "client: terminate clients\n");
355 flist_for_each(entry, &client_list) {
356 client = flist_entry(entry, struct fio_client, list);
358 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_QUIT, 0, NULL);
362 static void sig_int(int sig)
364 dprint(FD_NET, "client: got signal %d\n", sig);
365 fio_clients_terminate();
368 static void client_signal_handler(void)
370 struct sigaction act;
372 memset(&act, 0, sizeof(act));
373 act.sa_handler = sig_int;
374 act.sa_flags = SA_RESTART;
375 sigaction(SIGINT, &act, NULL);
377 memset(&act, 0, sizeof(act));
378 act.sa_handler = sig_int;
379 act.sa_flags = SA_RESTART;
380 sigaction(SIGTERM, &act, NULL);
383 static void probe_client(struct fio_client *client)
385 dprint(FD_NET, "client: send probe\n");
387 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_PROBE, 0, &client->cmd_list);
390 static int send_client_cmd_line(struct fio_client *client)
392 struct cmd_single_line_pdu *cslp;
393 struct cmd_line_pdu *clp;
394 unsigned long offset;
400 dprint(FD_NET, "client: send cmdline %d\n", client->argc);
402 lens = malloc(client->argc * sizeof(unsigned int));
405 * Find out how much mem we need
407 for (i = 0, mem = 0; i < client->argc; i++) {
408 lens[i] = strlen(client->argv[i]) + 1;
413 * We need one cmd_line_pdu, and argc number of cmd_single_line_pdu
415 mem += sizeof(*clp) + (client->argc * sizeof(*cslp));
419 offset = sizeof(*clp);
421 for (i = 0; i < client->argc; i++) {
422 uint16_t arg_len = lens[i];
425 strcpy((char *) cslp->text, client->argv[i]);
426 cslp->len = cpu_to_le16(arg_len);
427 offset += sizeof(*cslp) + arg_len;
431 clp->lines = cpu_to_le16(client->argc);
432 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOBLINE, pdu, mem, 0);
437 int fio_clients_connect(void)
439 struct fio_client *client;
440 struct flist_head *entry, *tmp;
445 WSAStartup(MAKEWORD(2,2), &wsd);
448 dprint(FD_NET, "client: connect all\n");
450 client_signal_handler();
452 flist_for_each_safe(entry, tmp, &client_list) {
453 client = flist_entry(entry, struct fio_client, list);
455 ret = fio_client_connect(client);
457 remove_client(client);
461 probe_client(client);
463 if (client->argc > 1)
464 send_client_cmd_line(client);
471 * Send file contents to server backend. We could use sendfile(), but to remain
472 * more portable lets just read/write the darn thing.
474 static int fio_client_send_ini(struct fio_client *client, const char *filename)
481 dprint(FD_NET, "send ini %s to %s\n", filename, client->hostname);
483 fd = open(filename, O_RDONLY);
485 log_err("fio: job file <%s> open: %s\n", filename, strerror(errno));
489 if (fstat(fd, &sb) < 0) {
490 log_err("fio: job file stat: %s\n", strerror(errno));
495 buf = malloc(sb.st_size);
500 ret = read(fd, p, len);
509 else if (errno == EAGAIN || errno == EINTR)
514 log_err("fio: failed reading job file %s\n", filename);
520 client->sent_job = 1;
521 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, buf, sb.st_size, 0);
527 int fio_clients_send_ini(const char *filename)
529 struct fio_client *client;
530 struct flist_head *entry, *tmp;
532 flist_for_each_safe(entry, tmp, &client_list) {
533 client = flist_entry(entry, struct fio_client, list);
535 if (fio_client_send_ini(client, filename))
536 remove_client(client);
538 client->sent_job = 1;
544 static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
546 dst->max_val = le64_to_cpu(src->max_val);
547 dst->min_val = le64_to_cpu(src->min_val);
548 dst->samples = le64_to_cpu(src->samples);
551 * Floats arrive as IEEE 754 encoded uint64_t, convert back to double
553 dst->mean.u.f = fio_uint64_to_double(le64_to_cpu(dst->mean.u.i));
554 dst->S.u.f = fio_uint64_to_double(le64_to_cpu(dst->S.u.i));
557 static void convert_ts(struct thread_stat *dst, struct thread_stat *src)
561 dst->error = le32_to_cpu(src->error);
562 dst->groupid = le32_to_cpu(src->groupid);
563 dst->pid = le32_to_cpu(src->pid);
564 dst->members = le32_to_cpu(src->members);
566 for (i = 0; i < 2; i++) {
567 convert_io_stat(&dst->clat_stat[i], &src->clat_stat[i]);
568 convert_io_stat(&dst->slat_stat[i], &src->slat_stat[i]);
569 convert_io_stat(&dst->lat_stat[i], &src->lat_stat[i]);
570 convert_io_stat(&dst->bw_stat[i], &src->bw_stat[i]);
573 dst->usr_time = le64_to_cpu(src->usr_time);
574 dst->sys_time = le64_to_cpu(src->sys_time);
575 dst->ctx = le64_to_cpu(src->ctx);
576 dst->minf = le64_to_cpu(src->minf);
577 dst->majf = le64_to_cpu(src->majf);
578 dst->clat_percentiles = le64_to_cpu(src->clat_percentiles);
580 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
581 fio_fp64_t *fps = &src->percentile_list[i];
582 fio_fp64_t *fpd = &dst->percentile_list[i];
584 fpd->u.f = fio_uint64_to_double(le64_to_cpu(fps->u.i));
587 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
588 dst->io_u_map[i] = le32_to_cpu(src->io_u_map[i]);
589 dst->io_u_submit[i] = le32_to_cpu(src->io_u_submit[i]);
590 dst->io_u_complete[i] = le32_to_cpu(src->io_u_complete[i]);
593 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
594 dst->io_u_lat_u[i] = le32_to_cpu(src->io_u_lat_u[i]);
595 dst->io_u_lat_m[i] = le32_to_cpu(src->io_u_lat_m[i]);
598 for (i = 0; i < 2; i++)
599 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
600 dst->io_u_plat[i][j] = le32_to_cpu(src->io_u_plat[i][j]);
602 for (i = 0; i < 3; i++) {
603 dst->total_io_u[i] = le64_to_cpu(src->total_io_u[i]);
604 dst->short_io_u[i] = le64_to_cpu(src->short_io_u[i]);
607 dst->total_submit = le64_to_cpu(src->total_submit);
608 dst->total_complete = le64_to_cpu(src->total_complete);
610 for (i = 0; i < 2; i++) {
611 dst->io_bytes[i] = le64_to_cpu(src->io_bytes[i]);
612 dst->runtime[i] = le64_to_cpu(src->runtime[i]);
615 dst->total_run_time = le64_to_cpu(src->total_run_time);
616 dst->continue_on_error = le16_to_cpu(src->continue_on_error);
617 dst->total_err_count = le64_to_cpu(src->total_err_count);
618 dst->first_error = le32_to_cpu(src->first_error);
619 dst->kb_base = le32_to_cpu(src->kb_base);
622 static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
626 for (i = 0; i < 2; i++) {
627 dst->max_run[i] = le64_to_cpu(src->max_run[i]);
628 dst->min_run[i] = le64_to_cpu(src->min_run[i]);
629 dst->max_bw[i] = le64_to_cpu(src->max_bw[i]);
630 dst->min_bw[i] = le64_to_cpu(src->min_bw[i]);
631 dst->io_kb[i] = le64_to_cpu(src->io_kb[i]);
632 dst->agg[i] = le64_to_cpu(src->agg[i]);
635 dst->kb_base = le32_to_cpu(src->kb_base);
636 dst->groupid = le32_to_cpu(src->groupid);
639 static void handle_ts(struct fio_client *client, struct fio_net_cmd *cmd)
641 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
643 show_thread_status(&p->ts, &p->rs);
645 if (sum_stat_clients == 1)
648 sum_thread_stats(&client_ts, &p->ts, sum_stat_nr);
649 sum_group_stats(&client_gs, &p->rs);
652 client_ts.groupid = p->ts.groupid;
654 if (++sum_stat_nr == sum_stat_clients) {
655 strcpy(client_ts.name, "All clients");
656 show_thread_status(&client_ts, &client_gs);
660 static void handle_gs(struct fio_client *client, struct fio_net_cmd *cmd)
662 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
664 show_group_stats(gs);
667 static void handle_text(struct fio_client *client, struct fio_net_cmd *cmd)
669 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
670 const char *buf = (const char *) pdu->buf;
674 name = client->name ? client->name : client->hostname;
676 if (!client->skip_newline)
677 fprintf(f_out, "<%s> ", name);
678 ret = fwrite(buf, pdu->buf_len, 1, f_out);
680 client->skip_newline = strchr(buf, '\n') == NULL;
683 static void convert_agg(struct disk_util_agg *agg)
687 for (i = 0; i < 2; i++) {
688 agg->ios[i] = le32_to_cpu(agg->ios[i]);
689 agg->merges[i] = le32_to_cpu(agg->merges[i]);
690 agg->sectors[i] = le64_to_cpu(agg->sectors[i]);
691 agg->ticks[i] = le32_to_cpu(agg->ticks[i]);
694 agg->io_ticks = le32_to_cpu(agg->io_ticks);
695 agg->time_in_queue = le32_to_cpu(agg->time_in_queue);
696 agg->slavecount = le32_to_cpu(agg->slavecount);
697 agg->max_util.u.f = fio_uint64_to_double(__le64_to_cpu(agg->max_util.u.i));
700 static void convert_dus(struct disk_util_stat *dus)
704 for (i = 0; i < 2; i++) {
705 dus->ios[i] = le32_to_cpu(dus->ios[i]);
706 dus->merges[i] = le32_to_cpu(dus->merges[i]);
707 dus->sectors[i] = le64_to_cpu(dus->sectors[i]);
708 dus->ticks[i] = le32_to_cpu(dus->ticks[i]);
711 dus->io_ticks = le32_to_cpu(dus->io_ticks);
712 dus->time_in_queue = le32_to_cpu(dus->time_in_queue);
713 dus->msec = le64_to_cpu(dus->msec);
716 static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd)
718 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
720 if (!client->disk_stats_shown) {
721 client->disk_stats_shown = 1;
722 log_info("\nDisk stats (read/write):\n");
725 print_disk_util(&du->dus, &du->agg, terse_output);
728 static void convert_jobs_eta(struct jobs_eta *je)
732 je->nr_running = le32_to_cpu(je->nr_running);
733 je->nr_ramp = le32_to_cpu(je->nr_ramp);
734 je->nr_pending = le32_to_cpu(je->nr_pending);
735 je->files_open = le32_to_cpu(je->files_open);
737 for (i = 0; i < 2; i++) {
738 je->m_rate[i] = le32_to_cpu(je->m_rate[i]);
739 je->t_rate[i] = le32_to_cpu(je->t_rate[i]);
740 je->m_iops[i] = le32_to_cpu(je->m_iops[i]);
741 je->t_iops[i] = le32_to_cpu(je->t_iops[i]);
742 je->rate[i] = le32_to_cpu(je->rate[i]);
743 je->iops[i] = le32_to_cpu(je->iops[i]);
746 je->elapsed_sec = le64_to_cpu(je->elapsed_sec);
747 je->eta_sec = le64_to_cpu(je->eta_sec);
750 void fio_client_sum_jobs_eta(struct jobs_eta *dst, struct jobs_eta *je)
754 dst->nr_running += je->nr_running;
755 dst->nr_ramp += je->nr_ramp;
756 dst->nr_pending += je->nr_pending;
757 dst->files_open += je->files_open;
759 for (i = 0; i < 2; i++) {
760 dst->m_rate[i] += je->m_rate[i];
761 dst->t_rate[i] += je->t_rate[i];
762 dst->m_iops[i] += je->m_iops[i];
763 dst->t_iops[i] += je->t_iops[i];
764 dst->rate[i] += je->rate[i];
765 dst->iops[i] += je->iops[i];
768 dst->elapsed_sec += je->elapsed_sec;
770 if (je->eta_sec > dst->eta_sec)
771 dst->eta_sec = je->eta_sec;
774 void fio_client_dec_jobs_eta(struct client_eta *eta, client_eta_op eta_fn)
776 if (!--eta->pending) {
782 static void remove_reply_cmd(struct fio_client *client, struct fio_net_cmd *cmd)
784 struct fio_net_int_cmd *icmd = NULL;
785 struct flist_head *entry;
787 flist_for_each(entry, &client->cmd_list) {
788 icmd = flist_entry(entry, struct fio_net_int_cmd, list);
790 if (cmd->tag == (uintptr_t) icmd)
797 log_err("fio: client: unable to find matching tag\n");
801 flist_del(&icmd->list);
802 cmd->tag = icmd->saved_tag;
806 static void handle_eta(struct fio_client *client, struct fio_net_cmd *cmd)
808 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
809 struct client_eta *eta = (struct client_eta *) (uintptr_t) cmd->tag;
811 dprint(FD_NET, "client: got eta tag %p, %d\n", eta, eta->pending);
813 assert(client->eta_in_flight == eta);
815 client->eta_in_flight = NULL;
816 flist_del_init(&client->eta_list);
818 fio_client_sum_jobs_eta(&eta->eta, je);
819 fio_client_dec_jobs_eta(eta, client->ops->eta);
822 static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd)
824 struct cmd_probe_pdu *probe = (struct cmd_probe_pdu *) cmd->payload;
825 const char *os, *arch;
828 os = fio_get_os_string(probe->os);
832 arch = fio_get_arch_string(probe->arch);
836 sprintf(bit, "%d-bit", probe->bpp * 8);
838 log_info("hostname=%s, be=%u, %s, os=%s, arch=%s, fio=%u.%u.%u\n",
839 probe->hostname, probe->bigendian, bit, os, arch,
840 probe->fio_major, probe->fio_minor, probe->fio_patch);
843 client->name = strdup((char *) probe->hostname);
846 static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd)
848 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
850 client->state = Client_started;
851 client->jobs = le32_to_cpu(pdu->jobs);
854 static void handle_stop(struct fio_client *client, struct fio_net_cmd *cmd)
856 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
858 client->state = Client_stopped;
859 client->error = le32_to_cpu(pdu->error);
862 log_info("client <%s>: exited with error %d\n", client->hostname, client->error);
865 static void convert_text(struct fio_net_cmd *cmd)
867 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
869 pdu->level = le32_to_cpu(pdu->level);
870 pdu->buf_len = le32_to_cpu(pdu->buf_len);
871 pdu->log_sec = le64_to_cpu(pdu->log_sec);
872 pdu->log_usec = le64_to_cpu(pdu->log_usec);
875 int fio_handle_client(struct fio_client *client)
877 struct client_ops *ops = client->ops;
878 struct fio_net_cmd *cmd;
880 dprint(FD_NET, "client: handle %s\n", client->hostname);
882 cmd = fio_net_recv_cmd(client->fd);
886 dprint(FD_NET, "client: got cmd op %s from %s\n",
887 fio_server_op(cmd->opcode), client->hostname);
889 switch (cmd->opcode) {
890 case FIO_NET_CMD_QUIT:
893 remove_client(client);
896 case FIO_NET_CMD_TEXT:
898 ops->text_op(client, cmd);
901 case FIO_NET_CMD_DU: {
902 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
904 convert_dus(&du->dus);
905 convert_agg(&du->agg);
907 ops->disk_util(client, cmd);
911 case FIO_NET_CMD_TS: {
912 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
914 convert_ts(&p->ts, &p->ts);
915 convert_gs(&p->rs, &p->rs);
917 ops->thread_status(client, cmd);
921 case FIO_NET_CMD_GS: {
922 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
926 ops->group_stats(client, cmd);
930 case FIO_NET_CMD_ETA: {
931 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
933 remove_reply_cmd(client, cmd);
934 convert_jobs_eta(je);
935 handle_eta(client, cmd);
939 case FIO_NET_CMD_PROBE:
940 remove_reply_cmd(client, cmd);
941 ops->probe(client, cmd);
944 case FIO_NET_CMD_RUN:
945 client->state = Client_running;
948 case FIO_NET_CMD_START:
949 handle_start(client, cmd);
952 case FIO_NET_CMD_STOP:
953 handle_stop(client, cmd);
956 case FIO_NET_CMD_ADD_JOB:
958 ops->add_job(client, cmd);
962 log_err("fio: unknown client op: %s\n", fio_server_op(cmd->opcode));
970 static void request_client_etas(struct client_ops *ops)
972 struct fio_client *client;
973 struct flist_head *entry;
974 struct client_eta *eta;
977 dprint(FD_NET, "client: request eta (%d)\n", nr_clients);
979 eta = malloc(sizeof(*eta));
980 memset(&eta->eta, 0, sizeof(eta->eta));
981 eta->pending = nr_clients;
983 flist_for_each(entry, &client_list) {
984 client = flist_entry(entry, struct fio_client, list);
986 if (!flist_empty(&client->eta_list)) {
990 if (client->state != Client_running)
993 assert(!client->eta_in_flight);
994 flist_add_tail(&client->eta_list, &eta_list);
995 client->eta_in_flight = eta;
996 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_SEND_ETA,
997 (uintptr_t) eta, &client->cmd_list);
1001 fio_client_dec_jobs_eta(eta, ops->eta);
1003 dprint(FD_NET, "client: requested eta tag %p\n", eta);
1006 static int client_check_cmd_timeout(struct fio_client *client,
1007 struct timeval *now)
1009 struct fio_net_int_cmd *cmd;
1010 struct flist_head *entry, *tmp;
1013 flist_for_each_safe(entry, tmp, &client->cmd_list) {
1014 cmd = flist_entry(entry, struct fio_net_int_cmd, list);
1016 if (mtime_since(&cmd->tv, now) < FIO_NET_CLIENT_TIMEOUT)
1019 log_err("fio: client %s, timeout on cmd %s\n", client->hostname,
1020 fio_server_op(cmd->cmd.opcode));
1021 flist_del(&cmd->list);
1026 return flist_empty(&client->cmd_list) && ret;
1029 static int fio_check_clients_timed_out(void)
1031 struct fio_client *client;
1032 struct flist_head *entry, *tmp;
1036 gettimeofday(&tv, NULL);
1038 flist_for_each_safe(entry, tmp, &client_list) {
1039 client = flist_entry(entry, struct fio_client, list);
1041 if (flist_empty(&client->cmd_list))
1044 if (!client_check_cmd_timeout(client, &tv))
1047 if (client->ops->timed_out)
1048 client->ops->timed_out(client);
1050 log_err("fio: client %s timed out\n", client->hostname);
1052 remove_client(client);
1059 int fio_handle_clients(struct client_ops *ops)
1061 struct pollfd *pfds;
1062 int i, ret = 0, retval = 0;
1064 gettimeofday(&eta_tv, NULL);
1066 pfds = malloc(nr_clients * sizeof(struct pollfd));
1068 sum_stat_clients = nr_clients;
1069 init_thread_stat(&client_ts);
1070 init_group_run_stat(&client_gs);
1072 while (!exit_backend && nr_clients) {
1073 struct flist_head *entry, *tmp;
1074 struct fio_client *client;
1077 flist_for_each_safe(entry, tmp, &client_list) {
1078 client = flist_entry(entry, struct fio_client, list);
1080 if (!client->sent_job && !client->ops->stay_connected &&
1081 flist_empty(&client->cmd_list)) {
1082 remove_client(client);
1086 pfds[i].fd = client->fd;
1087 pfds[i].events = POLLIN;
1094 assert(i == nr_clients);
1099 gettimeofday(&tv, NULL);
1100 if (mtime_since(&eta_tv, &tv) >= 900) {
1101 request_client_etas(ops);
1102 memcpy(&eta_tv, &tv, sizeof(tv));
1104 if (fio_check_clients_timed_out())
1108 ret = poll(pfds, nr_clients, 100);
1112 log_err("fio: poll clients: %s\n", strerror(errno));
1118 for (i = 0; i < nr_clients; i++) {
1119 if (!(pfds[i].revents & POLLIN))
1122 client = find_client_by_fd(pfds[i].fd);
1124 log_err("fio: unknown client fd %d\n", pfds[i].fd);
1127 if (!fio_handle_client(client)) {
1128 log_info("client: host=%s disconnected\n",
1130 remove_client(client);
1132 } else if (client->error)