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 static struct thread_stat client_ts;
56 static struct group_run_stats client_gs;
57 static int sum_stat_clients;
58 static int sum_stat_nr;
60 #define FIO_CLIENT_HASH_BITS 7
61 #define FIO_CLIENT_HASH_SZ (1 << FIO_CLIENT_HASH_BITS)
62 #define FIO_CLIENT_HASH_MASK (FIO_CLIENT_HASH_SZ - 1)
63 static struct flist_head client_hash[FIO_CLIENT_HASH_SZ];
65 static void fio_client_add_hash(struct fio_client *client)
67 int bucket = hash_long(client->fd, FIO_CLIENT_HASH_BITS);
69 bucket &= FIO_CLIENT_HASH_MASK;
70 flist_add(&client->hash_list, &client_hash[bucket]);
73 static void fio_client_remove_hash(struct fio_client *client)
75 if (!flist_empty(&client->hash_list))
76 flist_del_init(&client->hash_list);
79 static void fio_init fio_client_hash_init(void)
83 for (i = 0; i < FIO_CLIENT_HASH_SZ; i++)
84 INIT_FLIST_HEAD(&client_hash[i]);
87 static struct fio_client *find_client_by_fd(int fd)
89 int bucket = hash_long(fd, FIO_CLIENT_HASH_BITS) & FIO_CLIENT_HASH_MASK;
90 struct fio_client *client;
91 struct flist_head *entry;
93 flist_for_each(entry, &client_hash[bucket]) {
94 client = flist_entry(entry, struct fio_client, hash_list);
96 if (client->fd == fd) {
105 static void remove_client(struct fio_client *client)
107 assert(client->refs);
112 dprint(FD_NET, "client: removed <%s>\n", client->hostname);
113 flist_del(&client->list);
115 fio_client_remove_hash(client);
117 if (!flist_empty(&client->eta_list)) {
118 flist_del_init(&client->eta_list);
119 fio_client_dec_jobs_eta(client->eta_in_flight, client->ops->eta);
122 free(client->hostname);
133 static void put_client(struct fio_client *client)
135 remove_client(client);
138 static void __fio_client_add_cmd_option(struct fio_client *client,
143 index = client->argc++;
144 client->argv = realloc(client->argv, sizeof(char *) * client->argc);
145 client->argv[index] = strdup(opt);
146 dprint(FD_NET, "client: add cmd %d: %s\n", index, opt);
149 void fio_client_add_cmd_option(void *cookie, const char *opt)
151 struct fio_client *client = cookie;
152 struct flist_head *entry;
157 __fio_client_add_cmd_option(client, opt);
160 * Duplicate arguments to shared client group
162 flist_for_each(entry, &arg_list) {
163 client = flist_entry(entry, struct fio_client, arg_list);
165 __fio_client_add_cmd_option(client, opt);
169 struct fio_client *fio_client_add_explicit(struct client_ops *ops,
170 const char *hostname, int type,
173 struct fio_client *client;
175 client = malloc(sizeof(*client));
176 memset(client, 0, sizeof(*client));
178 INIT_FLIST_HEAD(&client->list);
179 INIT_FLIST_HEAD(&client->hash_list);
180 INIT_FLIST_HEAD(&client->arg_list);
181 INIT_FLIST_HEAD(&client->eta_list);
182 INIT_FLIST_HEAD(&client->cmd_list);
184 client->hostname = strdup(hostname);
186 if (type == Fio_client_socket)
191 ipv6 = type == Fio_client_ipv6;
192 if (fio_server_parse_host(hostname, &ipv6,
193 &client->addr.sin_addr,
194 &client->addr6.sin6_addr))
204 __fio_client_add_cmd_option(client, "fio");
206 flist_add(&client->list, &client_list);
208 dprint(FD_NET, "client: added <%s>\n", client->hostname);
215 int fio_client_add(struct client_ops *ops, const char *hostname, void **cookie)
217 struct fio_client *existing = *cookie;
218 struct fio_client *client;
222 * We always add our "exec" name as the option, hence 1
225 if (existing->argc == 1)
226 flist_add_tail(&existing->arg_list, &arg_list);
228 while (!flist_empty(&arg_list))
229 flist_del_init(arg_list.next);
233 client = malloc(sizeof(*client));
234 memset(client, 0, sizeof(*client));
236 INIT_FLIST_HEAD(&client->list);
237 INIT_FLIST_HEAD(&client->hash_list);
238 INIT_FLIST_HEAD(&client->arg_list);
239 INIT_FLIST_HEAD(&client->eta_list);
240 INIT_FLIST_HEAD(&client->cmd_list);
242 if (fio_server_parse_string(hostname, &client->hostname,
243 &client->is_sock, &client->port,
244 &client->addr.sin_addr,
245 &client->addr6.sin6_addr,
253 __fio_client_add_cmd_option(client, "fio");
255 flist_add(&client->list, &client_list);
257 dprint(FD_NET, "client: added <%s>\n", client->hostname);
262 static int fio_client_connect_ip(struct fio_client *client)
264 struct sockaddr *addr;
265 fio_socklen_t socklen;
269 client->addr6.sin6_family = AF_INET6;
270 client->addr6.sin6_port = htons(client->port);
272 addr = (struct sockaddr *) &client->addr6;
273 socklen = sizeof(client->addr6);
275 client->addr.sin_family = AF_INET;
276 client->addr.sin_port = htons(client->port);
278 addr = (struct sockaddr *) &client->addr;
279 socklen = sizeof(client->addr);
282 fd = socket(domain, SOCK_STREAM, 0);
284 log_err("fio: socket: %s\n", strerror(errno));
288 if (connect(fd, addr, socklen) < 0) {
289 log_err("fio: connect: %s\n", strerror(errno));
290 log_err("fio: failed to connect to %s:%u\n", client->hostname,
299 static int fio_client_connect_sock(struct fio_client *client)
301 struct sockaddr_un *addr = &client->addr_un;
305 memset(addr, 0, sizeof(*addr));
306 addr->sun_family = AF_UNIX;
307 strcpy(addr->sun_path, client->hostname);
309 fd = socket(AF_UNIX, SOCK_STREAM, 0);
311 log_err("fio: socket: %s\n", strerror(errno));
315 len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
316 if (connect(fd, (struct sockaddr *) addr, len) < 0) {
317 log_err("fio: connect; %s\n", strerror(errno));
325 static int fio_client_connect(struct fio_client *client)
329 dprint(FD_NET, "client: connect to host %s\n", client->hostname);
332 fd = fio_client_connect_sock(client);
334 fd = fio_client_connect_ip(client);
336 dprint(FD_NET, "client: %s connected %d\n", client->hostname, fd);
342 fio_client_add_hash(client);
343 client->state = Client_connected;
347 void fio_clients_terminate(void)
349 struct flist_head *entry;
350 struct fio_client *client;
352 dprint(FD_NET, "client: terminate clients\n");
354 flist_for_each(entry, &client_list) {
355 client = flist_entry(entry, struct fio_client, list);
357 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_QUIT, 0, NULL);
361 static void sig_int(int sig)
363 dprint(FD_NET, "client: got signal %d\n", sig);
364 fio_clients_terminate();
367 static void client_signal_handler(void)
369 struct sigaction act;
371 memset(&act, 0, sizeof(act));
372 act.sa_handler = sig_int;
373 act.sa_flags = SA_RESTART;
374 sigaction(SIGINT, &act, NULL);
376 memset(&act, 0, sizeof(act));
377 act.sa_handler = sig_int;
378 act.sa_flags = SA_RESTART;
379 sigaction(SIGTERM, &act, NULL);
382 static void probe_client(struct fio_client *client)
384 dprint(FD_NET, "client: send probe\n");
386 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_PROBE, 0, &client->cmd_list);
389 static int send_client_cmd_line(struct fio_client *client)
391 struct cmd_single_line_pdu *cslp;
392 struct cmd_line_pdu *clp;
393 unsigned long offset;
399 dprint(FD_NET, "client: send cmdline %d\n", client->argc);
401 lens = malloc(client->argc * sizeof(unsigned int));
404 * Find out how much mem we need
406 for (i = 0, mem = 0; i < client->argc; i++) {
407 lens[i] = strlen(client->argv[i]) + 1;
412 * We need one cmd_line_pdu, and argc number of cmd_single_line_pdu
414 mem += sizeof(*clp) + (client->argc * sizeof(*cslp));
418 offset = sizeof(*clp);
420 for (i = 0; i < client->argc; i++) {
421 uint16_t arg_len = lens[i];
424 strcpy((char *) cslp->text, client->argv[i]);
425 cslp->len = cpu_to_le16(arg_len);
426 offset += sizeof(*cslp) + arg_len;
430 clp->lines = cpu_to_le16(client->argc);
431 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOBLINE, pdu, mem, 0);
436 int fio_clients_connect(void)
438 struct fio_client *client;
439 struct flist_head *entry, *tmp;
444 WSAStartup(MAKEWORD(2,2), &wsd);
447 dprint(FD_NET, "client: connect all\n");
449 client_signal_handler();
451 flist_for_each_safe(entry, tmp, &client_list) {
452 client = flist_entry(entry, struct fio_client, list);
454 ret = fio_client_connect(client);
456 remove_client(client);
460 probe_client(client);
462 if (client->argc > 1)
463 send_client_cmd_line(client);
470 * Send file contents to server backend. We could use sendfile(), but to remain
471 * more portable lets just read/write the darn thing.
473 static int fio_client_send_ini(struct fio_client *client, const char *filename)
480 dprint(FD_NET, "send ini %s to %s\n", filename, client->hostname);
482 fd = open(filename, O_RDONLY);
484 log_err("fio: job file <%s> open: %s\n", filename, strerror(errno));
488 if (fstat(fd, &sb) < 0) {
489 log_err("fio: job file stat: %s\n", strerror(errno));
494 buf = malloc(sb.st_size);
499 ret = read(fd, p, len);
508 else if (errno == EAGAIN || errno == EINTR)
513 log_err("fio: failed reading job file %s\n", filename);
519 client->sent_job = 1;
520 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, buf, sb.st_size, 0);
526 int fio_clients_send_ini(const char *filename)
528 struct fio_client *client;
529 struct flist_head *entry, *tmp;
531 flist_for_each_safe(entry, tmp, &client_list) {
532 client = flist_entry(entry, struct fio_client, list);
534 if (fio_client_send_ini(client, filename))
535 remove_client(client);
537 client->sent_job = 1;
543 static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
545 dst->max_val = le64_to_cpu(src->max_val);
546 dst->min_val = le64_to_cpu(src->min_val);
547 dst->samples = le64_to_cpu(src->samples);
550 * Floats arrive as IEEE 754 encoded uint64_t, convert back to double
552 dst->mean.u.f = fio_uint64_to_double(le64_to_cpu(dst->mean.u.i));
553 dst->S.u.f = fio_uint64_to_double(le64_to_cpu(dst->S.u.i));
556 static void convert_ts(struct thread_stat *dst, struct thread_stat *src)
560 dst->error = le32_to_cpu(src->error);
561 dst->groupid = le32_to_cpu(src->groupid);
562 dst->pid = le32_to_cpu(src->pid);
563 dst->members = le32_to_cpu(src->members);
565 for (i = 0; i < 2; i++) {
566 convert_io_stat(&dst->clat_stat[i], &src->clat_stat[i]);
567 convert_io_stat(&dst->slat_stat[i], &src->slat_stat[i]);
568 convert_io_stat(&dst->lat_stat[i], &src->lat_stat[i]);
569 convert_io_stat(&dst->bw_stat[i], &src->bw_stat[i]);
572 dst->usr_time = le64_to_cpu(src->usr_time);
573 dst->sys_time = le64_to_cpu(src->sys_time);
574 dst->ctx = le64_to_cpu(src->ctx);
575 dst->minf = le64_to_cpu(src->minf);
576 dst->majf = le64_to_cpu(src->majf);
577 dst->clat_percentiles = le64_to_cpu(src->clat_percentiles);
579 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
580 fio_fp64_t *fps = &src->percentile_list[i];
581 fio_fp64_t *fpd = &dst->percentile_list[i];
583 fpd->u.f = fio_uint64_to_double(le64_to_cpu(fps->u.i));
586 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
587 dst->io_u_map[i] = le32_to_cpu(src->io_u_map[i]);
588 dst->io_u_submit[i] = le32_to_cpu(src->io_u_submit[i]);
589 dst->io_u_complete[i] = le32_to_cpu(src->io_u_complete[i]);
592 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
593 dst->io_u_lat_u[i] = le32_to_cpu(src->io_u_lat_u[i]);
594 dst->io_u_lat_m[i] = le32_to_cpu(src->io_u_lat_m[i]);
597 for (i = 0; i < 2; i++)
598 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
599 dst->io_u_plat[i][j] = le32_to_cpu(src->io_u_plat[i][j]);
601 for (i = 0; i < 3; i++) {
602 dst->total_io_u[i] = le64_to_cpu(src->total_io_u[i]);
603 dst->short_io_u[i] = le64_to_cpu(src->short_io_u[i]);
606 dst->total_submit = le64_to_cpu(src->total_submit);
607 dst->total_complete = le64_to_cpu(src->total_complete);
609 for (i = 0; i < 2; i++) {
610 dst->io_bytes[i] = le64_to_cpu(src->io_bytes[i]);
611 dst->runtime[i] = le64_to_cpu(src->runtime[i]);
614 dst->total_run_time = le64_to_cpu(src->total_run_time);
615 dst->continue_on_error = le16_to_cpu(src->continue_on_error);
616 dst->total_err_count = le64_to_cpu(src->total_err_count);
617 dst->first_error = le32_to_cpu(src->first_error);
618 dst->kb_base = le32_to_cpu(src->kb_base);
621 static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
625 for (i = 0; i < 2; i++) {
626 dst->max_run[i] = le64_to_cpu(src->max_run[i]);
627 dst->min_run[i] = le64_to_cpu(src->min_run[i]);
628 dst->max_bw[i] = le64_to_cpu(src->max_bw[i]);
629 dst->min_bw[i] = le64_to_cpu(src->min_bw[i]);
630 dst->io_kb[i] = le64_to_cpu(src->io_kb[i]);
631 dst->agg[i] = le64_to_cpu(src->agg[i]);
634 dst->kb_base = le32_to_cpu(src->kb_base);
635 dst->groupid = le32_to_cpu(src->groupid);
638 static void handle_ts(struct fio_client *client, struct fio_net_cmd *cmd)
640 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
642 show_thread_status(&p->ts, &p->rs);
644 if (sum_stat_clients == 1)
647 sum_thread_stats(&client_ts, &p->ts, sum_stat_nr);
648 sum_group_stats(&client_gs, &p->rs);
651 client_ts.groupid = p->ts.groupid;
653 if (++sum_stat_nr == sum_stat_clients) {
654 strcpy(client_ts.name, "All clients");
655 show_thread_status(&client_ts, &client_gs);
659 static void handle_gs(struct fio_client *client, struct fio_net_cmd *cmd)
661 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
663 show_group_stats(gs);
666 static void handle_text(struct fio_client *client, struct fio_net_cmd *cmd)
668 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
669 const char *buf = (const char *) pdu->buf;
673 name = client->name ? client->name : client->hostname;
675 if (!client->skip_newline)
676 fprintf(f_out, "<%s> ", name);
677 ret = fwrite(buf, pdu->buf_len, 1, f_out);
679 client->skip_newline = strchr(buf, '\n') == NULL;
682 static void convert_agg(struct disk_util_agg *agg)
686 for (i = 0; i < 2; i++) {
687 agg->ios[i] = le32_to_cpu(agg->ios[i]);
688 agg->merges[i] = le32_to_cpu(agg->merges[i]);
689 agg->sectors[i] = le64_to_cpu(agg->sectors[i]);
690 agg->ticks[i] = le32_to_cpu(agg->ticks[i]);
693 agg->io_ticks = le32_to_cpu(agg->io_ticks);
694 agg->time_in_queue = le32_to_cpu(agg->time_in_queue);
695 agg->slavecount = le32_to_cpu(agg->slavecount);
696 agg->max_util.u.f = fio_uint64_to_double(__le64_to_cpu(agg->max_util.u.i));
699 static void convert_dus(struct disk_util_stat *dus)
703 for (i = 0; i < 2; i++) {
704 dus->ios[i] = le32_to_cpu(dus->ios[i]);
705 dus->merges[i] = le32_to_cpu(dus->merges[i]);
706 dus->sectors[i] = le64_to_cpu(dus->sectors[i]);
707 dus->ticks[i] = le32_to_cpu(dus->ticks[i]);
710 dus->io_ticks = le32_to_cpu(dus->io_ticks);
711 dus->time_in_queue = le32_to_cpu(dus->time_in_queue);
712 dus->msec = le64_to_cpu(dus->msec);
715 static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd)
717 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
719 if (!client->disk_stats_shown) {
720 client->disk_stats_shown = 1;
721 log_info("\nDisk stats (read/write):\n");
724 print_disk_util(&du->dus, &du->agg, terse_output);
727 static void convert_jobs_eta(struct jobs_eta *je)
731 je->nr_running = le32_to_cpu(je->nr_running);
732 je->nr_ramp = le32_to_cpu(je->nr_ramp);
733 je->nr_pending = le32_to_cpu(je->nr_pending);
734 je->files_open = le32_to_cpu(je->files_open);
736 for (i = 0; i < 2; i++) {
737 je->m_rate[i] = le32_to_cpu(je->m_rate[i]);
738 je->t_rate[i] = le32_to_cpu(je->t_rate[i]);
739 je->m_iops[i] = le32_to_cpu(je->m_iops[i]);
740 je->t_iops[i] = le32_to_cpu(je->t_iops[i]);
741 je->rate[i] = le32_to_cpu(je->rate[i]);
742 je->iops[i] = le32_to_cpu(je->iops[i]);
745 je->elapsed_sec = le64_to_cpu(je->elapsed_sec);
746 je->eta_sec = le64_to_cpu(je->eta_sec);
749 void fio_client_sum_jobs_eta(struct jobs_eta *dst, struct jobs_eta *je)
753 dst->nr_running += je->nr_running;
754 dst->nr_ramp += je->nr_ramp;
755 dst->nr_pending += je->nr_pending;
756 dst->files_open += je->files_open;
758 for (i = 0; i < 2; i++) {
759 dst->m_rate[i] += je->m_rate[i];
760 dst->t_rate[i] += je->t_rate[i];
761 dst->m_iops[i] += je->m_iops[i];
762 dst->t_iops[i] += je->t_iops[i];
763 dst->rate[i] += je->rate[i];
764 dst->iops[i] += je->iops[i];
767 dst->elapsed_sec += je->elapsed_sec;
769 if (je->eta_sec > dst->eta_sec)
770 dst->eta_sec = je->eta_sec;
773 void fio_client_dec_jobs_eta(struct client_eta *eta, client_eta_op eta_fn)
775 if (!--eta->pending) {
781 static void remove_reply_cmd(struct fio_client *client, struct fio_net_cmd *cmd)
783 struct fio_net_int_cmd *icmd = NULL;
784 struct flist_head *entry;
786 flist_for_each(entry, &client->cmd_list) {
787 icmd = flist_entry(entry, struct fio_net_int_cmd, list);
789 if (cmd->tag == (uintptr_t) icmd)
796 log_err("fio: client: unable to find matching tag\n");
800 flist_del(&icmd->list);
801 cmd->tag = icmd->saved_tag;
805 static void handle_eta(struct fio_client *client, struct fio_net_cmd *cmd)
807 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
808 struct client_eta *eta = (struct client_eta *) (uintptr_t) cmd->tag;
810 dprint(FD_NET, "client: got eta tag %p, %d\n", eta, eta->pending);
812 assert(client->eta_in_flight == eta);
814 client->eta_in_flight = NULL;
815 flist_del_init(&client->eta_list);
817 fio_client_sum_jobs_eta(&eta->eta, je);
818 fio_client_dec_jobs_eta(eta, client->ops->eta);
821 static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd)
823 struct cmd_probe_pdu *probe = (struct cmd_probe_pdu *) cmd->payload;
824 const char *os, *arch;
827 os = fio_get_os_string(probe->os);
831 arch = fio_get_arch_string(probe->arch);
835 sprintf(bit, "%d-bit", probe->bpp * 8);
837 log_info("hostname=%s, be=%u, %s, os=%s, arch=%s, fio=%u.%u.%u\n",
838 probe->hostname, probe->bigendian, bit, os, arch,
839 probe->fio_major, probe->fio_minor, probe->fio_patch);
842 client->name = strdup((char *) probe->hostname);
845 static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd)
847 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
849 client->state = Client_started;
850 client->jobs = le32_to_cpu(pdu->jobs);
853 static void handle_stop(struct fio_client *client, struct fio_net_cmd *cmd)
855 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
857 client->state = Client_stopped;
858 client->error = le32_to_cpu(pdu->error);
861 log_info("client <%s>: exited with error %d\n", client->hostname, client->error);
864 static void convert_text(struct fio_net_cmd *cmd)
866 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
868 pdu->level = le32_to_cpu(pdu->level);
869 pdu->buf_len = le32_to_cpu(pdu->buf_len);
870 pdu->log_sec = le64_to_cpu(pdu->log_sec);
871 pdu->log_usec = le64_to_cpu(pdu->log_usec);
874 int fio_handle_client(struct fio_client *client)
876 struct client_ops *ops = client->ops;
877 struct fio_net_cmd *cmd;
879 dprint(FD_NET, "client: handle %s\n", client->hostname);
881 cmd = fio_net_recv_cmd(client->fd);
885 dprint(FD_NET, "client: got cmd op %s from %s\n",
886 fio_server_op(cmd->opcode), client->hostname);
888 switch (cmd->opcode) {
889 case FIO_NET_CMD_QUIT:
892 remove_client(client);
895 case FIO_NET_CMD_TEXT:
897 ops->text_op(client, cmd);
900 case FIO_NET_CMD_DU: {
901 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
903 convert_dus(&du->dus);
904 convert_agg(&du->agg);
906 ops->disk_util(client, cmd);
910 case FIO_NET_CMD_TS: {
911 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
913 convert_ts(&p->ts, &p->ts);
914 convert_gs(&p->rs, &p->rs);
916 ops->thread_status(client, cmd);
920 case FIO_NET_CMD_GS: {
921 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
925 ops->group_stats(client, cmd);
929 case FIO_NET_CMD_ETA: {
930 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
932 remove_reply_cmd(client, cmd);
933 convert_jobs_eta(je);
934 handle_eta(client, cmd);
938 case FIO_NET_CMD_PROBE:
939 remove_reply_cmd(client, cmd);
940 ops->probe(client, cmd);
943 case FIO_NET_CMD_RUN:
944 client->state = Client_running;
947 case FIO_NET_CMD_START:
948 handle_start(client, cmd);
951 case FIO_NET_CMD_STOP:
952 handle_stop(client, cmd);
955 case FIO_NET_CMD_ADD_JOB:
957 ops->add_job(client, cmd);
961 log_err("fio: unknown client op: %s\n", fio_server_op(cmd->opcode));
969 static void request_client_etas(struct client_ops *ops)
971 struct fio_client *client;
972 struct flist_head *entry;
973 struct client_eta *eta;
976 dprint(FD_NET, "client: request eta (%d)\n", nr_clients);
978 eta = malloc(sizeof(*eta));
979 memset(&eta->eta, 0, sizeof(eta->eta));
980 eta->pending = nr_clients;
982 flist_for_each(entry, &client_list) {
983 client = flist_entry(entry, struct fio_client, list);
985 if (!flist_empty(&client->eta_list)) {
989 if (client->state != Client_running)
992 assert(!client->eta_in_flight);
993 flist_add_tail(&client->eta_list, &eta_list);
994 client->eta_in_flight = eta;
995 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_SEND_ETA,
996 (uintptr_t) eta, &client->cmd_list);
1000 fio_client_dec_jobs_eta(eta, ops->eta);
1002 dprint(FD_NET, "client: requested eta tag %p\n", eta);
1005 static int client_check_cmd_timeout(struct fio_client *client,
1006 struct timeval *now)
1008 struct fio_net_int_cmd *cmd;
1009 struct flist_head *entry, *tmp;
1012 flist_for_each_safe(entry, tmp, &client->cmd_list) {
1013 cmd = flist_entry(entry, struct fio_net_int_cmd, list);
1015 if (mtime_since(&cmd->tv, now) < FIO_NET_CLIENT_TIMEOUT)
1018 log_err("fio: client %s, timeout on cmd %s\n", client->hostname,
1019 fio_server_op(cmd->cmd.opcode));
1020 flist_del(&cmd->list);
1025 return flist_empty(&client->cmd_list) && ret;
1028 static int fio_check_clients_timed_out(void)
1030 struct fio_client *client;
1031 struct flist_head *entry, *tmp;
1035 gettimeofday(&tv, NULL);
1037 flist_for_each_safe(entry, tmp, &client_list) {
1038 client = flist_entry(entry, struct fio_client, list);
1040 if (flist_empty(&client->cmd_list))
1043 if (!client_check_cmd_timeout(client, &tv))
1046 if (client->ops->timed_out)
1047 client->ops->timed_out(client);
1049 log_err("fio: client %s timed out\n", client->hostname);
1051 remove_client(client);
1058 int fio_handle_clients(struct client_ops *ops)
1060 struct pollfd *pfds;
1061 int i, ret = 0, retval = 0;
1063 gettimeofday(&eta_tv, NULL);
1065 pfds = malloc(nr_clients * sizeof(struct pollfd));
1067 sum_stat_clients = nr_clients;
1068 init_thread_stat(&client_ts);
1069 init_group_run_stat(&client_gs);
1071 while (!exit_backend && nr_clients) {
1072 struct flist_head *entry, *tmp;
1073 struct fio_client *client;
1076 flist_for_each_safe(entry, tmp, &client_list) {
1077 client = flist_entry(entry, struct fio_client, list);
1079 if (!client->sent_job && !client->ops->stay_connected &&
1080 flist_empty(&client->cmd_list)) {
1081 remove_client(client);
1085 pfds[i].fd = client->fd;
1086 pfds[i].events = POLLIN;
1093 assert(i == nr_clients);
1098 gettimeofday(&tv, NULL);
1099 if (mtime_since(&eta_tv, &tv) >= 900) {
1100 request_client_etas(ops);
1101 memcpy(&eta_tv, &tv, sizeof(tv));
1103 if (fio_check_clients_timed_out())
1107 ret = poll(pfds, nr_clients, 100);
1111 log_err("fio: poll clients: %s\n", strerror(errno));
1117 for (i = 0; i < nr_clients; i++) {
1118 if (!(pfds[i].revents & POLLIN))
1121 client = find_client_by_fd(pfds[i].fd);
1123 log_err("fio: unknown client fd %d\n", pfds[i].fd);
1126 if (!fio_handle_client(client)) {
1127 log_info("client: host=%s disconnected\n",
1129 remove_client(client);
1131 } else if (client->error)