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);
29 static void handle_stop(struct fio_client *client, struct fio_net_cmd *cmd);
30 static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd);
32 struct client_ops fio_client_ops = {
33 .text_op = handle_text,
34 .disk_util = handle_du,
35 .thread_status = handle_ts,
36 .group_stats = handle_gs,
38 .start = handle_start,
39 .eta = display_thread_status,
40 .probe = handle_probe,
41 .eta_msec = FIO_CLIENT_DEF_ETA_MSEC,
44 static struct timeval eta_tv;
46 static FLIST_HEAD(client_list);
47 static FLIST_HEAD(eta_list);
49 static FLIST_HEAD(arg_list);
51 struct thread_stat client_ts;
52 struct group_run_stats client_gs;
55 static int sum_stat_nr;
57 #define FIO_CLIENT_HASH_BITS 7
58 #define FIO_CLIENT_HASH_SZ (1 << FIO_CLIENT_HASH_BITS)
59 #define FIO_CLIENT_HASH_MASK (FIO_CLIENT_HASH_SZ - 1)
60 static struct flist_head client_hash[FIO_CLIENT_HASH_SZ];
62 static void fio_client_add_hash(struct fio_client *client)
64 int bucket = hash_long(client->fd, FIO_CLIENT_HASH_BITS);
66 bucket &= FIO_CLIENT_HASH_MASK;
67 flist_add(&client->hash_list, &client_hash[bucket]);
70 static void fio_client_remove_hash(struct fio_client *client)
72 if (!flist_empty(&client->hash_list))
73 flist_del_init(&client->hash_list);
76 static void fio_init fio_client_hash_init(void)
80 for (i = 0; i < FIO_CLIENT_HASH_SZ; i++)
81 INIT_FLIST_HEAD(&client_hash[i]);
84 static struct fio_client *find_client_by_fd(int fd)
86 int bucket = hash_long(fd, FIO_CLIENT_HASH_BITS) & FIO_CLIENT_HASH_MASK;
87 struct fio_client *client;
88 struct flist_head *entry;
90 flist_for_each(entry, &client_hash[bucket]) {
91 client = flist_entry(entry, struct fio_client, hash_list);
94 return fio_get_client(client);
100 void fio_put_client(struct fio_client *client)
105 free(client->hostname);
114 static void remove_client(struct fio_client *client)
116 assert(client->refs);
118 dprint(FD_NET, "client: removed <%s>\n", client->hostname);
120 if (!flist_empty(&client->list))
121 flist_del_init(&client->list);
123 fio_client_remove_hash(client);
125 if (!flist_empty(&client->eta_list)) {
126 flist_del_init(&client->eta_list);
127 fio_client_dec_jobs_eta(client->eta_in_flight, client->ops->eta);
133 fio_put_client(client);
136 struct fio_client *fio_get_client(struct fio_client *client)
142 static void __fio_client_add_cmd_option(struct fio_client *client,
147 index = client->argc++;
148 client->argv = realloc(client->argv, sizeof(char *) * client->argc);
149 client->argv[index] = strdup(opt);
150 dprint(FD_NET, "client: add cmd %d: %s\n", index, opt);
153 void fio_client_add_cmd_option(void *cookie, const char *opt)
155 struct fio_client *client = cookie;
156 struct flist_head *entry;
161 __fio_client_add_cmd_option(client, opt);
164 * Duplicate arguments to shared client group
166 flist_for_each(entry, &arg_list) {
167 client = flist_entry(entry, struct fio_client, arg_list);
169 __fio_client_add_cmd_option(client, opt);
173 struct fio_client *fio_client_add_explicit(struct client_ops *ops,
174 const char *hostname, int type,
177 struct fio_client *client;
179 client = malloc(sizeof(*client));
180 memset(client, 0, sizeof(*client));
182 INIT_FLIST_HEAD(&client->list);
183 INIT_FLIST_HEAD(&client->hash_list);
184 INIT_FLIST_HEAD(&client->arg_list);
185 INIT_FLIST_HEAD(&client->eta_list);
186 INIT_FLIST_HEAD(&client->cmd_list);
188 client->hostname = strdup(hostname);
190 if (type == Fio_client_socket)
195 ipv6 = type == Fio_client_ipv6;
196 if (fio_server_parse_host(hostname, &ipv6,
197 &client->addr.sin_addr,
198 &client->addr6.sin6_addr))
208 __fio_client_add_cmd_option(client, "fio");
210 flist_add(&client->list, &client_list);
212 dprint(FD_NET, "client: added <%s>\n", client->hostname);
219 int fio_client_add(struct client_ops *ops, const char *hostname, void **cookie)
221 struct fio_client *existing = *cookie;
222 struct fio_client *client;
226 * We always add our "exec" name as the option, hence 1
229 if (existing->argc == 1)
230 flist_add_tail(&existing->arg_list, &arg_list);
232 while (!flist_empty(&arg_list))
233 flist_del_init(arg_list.next);
237 client = malloc(sizeof(*client));
238 memset(client, 0, sizeof(*client));
240 INIT_FLIST_HEAD(&client->list);
241 INIT_FLIST_HEAD(&client->hash_list);
242 INIT_FLIST_HEAD(&client->arg_list);
243 INIT_FLIST_HEAD(&client->eta_list);
244 INIT_FLIST_HEAD(&client->cmd_list);
246 if (fio_server_parse_string(hostname, &client->hostname,
247 &client->is_sock, &client->port,
248 &client->addr.sin_addr,
249 &client->addr6.sin6_addr,
257 __fio_client_add_cmd_option(client, "fio");
259 flist_add(&client->list, &client_list);
261 dprint(FD_NET, "client: added <%s>\n", client->hostname);
266 static void probe_client(struct fio_client *client)
268 dprint(FD_NET, "client: send probe\n");
270 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_PROBE, 0, &client->cmd_list);
273 static int fio_client_connect_ip(struct fio_client *client)
275 struct sockaddr *addr;
276 fio_socklen_t socklen;
280 client->addr6.sin6_family = AF_INET6;
281 client->addr6.sin6_port = htons(client->port);
283 addr = (struct sockaddr *) &client->addr6;
284 socklen = sizeof(client->addr6);
286 client->addr.sin_family = AF_INET;
287 client->addr.sin_port = htons(client->port);
289 addr = (struct sockaddr *) &client->addr;
290 socklen = sizeof(client->addr);
293 fd = socket(domain, SOCK_STREAM, 0);
297 log_err("fio: socket: %s\n", strerror(errno));
301 if (connect(fd, addr, socklen) < 0) {
304 log_err("fio: connect: %s\n", strerror(errno));
305 log_err("fio: failed to connect to %s:%u\n", client->hostname,
314 static int fio_client_connect_sock(struct fio_client *client)
316 struct sockaddr_un *addr = &client->addr_un;
320 memset(addr, 0, sizeof(*addr));
321 addr->sun_family = AF_UNIX;
322 strcpy(addr->sun_path, client->hostname);
324 fd = socket(AF_UNIX, SOCK_STREAM, 0);
328 log_err("fio: socket: %s\n", strerror(errno));
332 len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
333 if (connect(fd, (struct sockaddr *) addr, len) < 0) {
336 log_err("fio: connect; %s\n", strerror(errno));
344 int fio_client_connect(struct fio_client *client)
348 dprint(FD_NET, "client: connect to host %s\n", client->hostname);
351 fd = fio_client_connect_sock(client);
353 fd = fio_client_connect_ip(client);
355 dprint(FD_NET, "client: %s connected %d\n", client->hostname, fd);
361 fio_client_add_hash(client);
362 client->state = Client_connected;
364 probe_client(client);
368 void fio_client_terminate(struct fio_client *client)
370 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_QUIT, 0, NULL);
373 void fio_clients_terminate(void)
375 struct flist_head *entry;
376 struct fio_client *client;
378 dprint(FD_NET, "client: terminate clients\n");
380 flist_for_each(entry, &client_list) {
381 client = flist_entry(entry, struct fio_client, list);
382 fio_client_terminate(client);
386 static void sig_int(int sig)
388 dprint(FD_NET, "client: got signal %d\n", sig);
389 fio_clients_terminate();
392 static void client_signal_handler(void)
394 struct sigaction act;
396 memset(&act, 0, sizeof(act));
397 act.sa_handler = sig_int;
398 act.sa_flags = SA_RESTART;
399 sigaction(SIGINT, &act, NULL);
401 memset(&act, 0, sizeof(act));
402 act.sa_handler = sig_int;
403 act.sa_flags = SA_RESTART;
404 sigaction(SIGTERM, &act, NULL);
407 static int send_client_cmd_line(struct fio_client *client)
409 struct cmd_single_line_pdu *cslp;
410 struct cmd_line_pdu *clp;
411 unsigned long offset;
417 dprint(FD_NET, "client: send cmdline %d\n", client->argc);
419 lens = malloc(client->argc * sizeof(unsigned int));
422 * Find out how much mem we need
424 for (i = 0, mem = 0; i < client->argc; i++) {
425 lens[i] = strlen(client->argv[i]) + 1;
430 * We need one cmd_line_pdu, and argc number of cmd_single_line_pdu
432 mem += sizeof(*clp) + (client->argc * sizeof(*cslp));
436 offset = sizeof(*clp);
438 for (i = 0; i < client->argc; i++) {
439 uint16_t arg_len = lens[i];
442 strcpy((char *) cslp->text, client->argv[i]);
443 cslp->len = cpu_to_le16(arg_len);
444 offset += sizeof(*cslp) + arg_len;
448 clp->lines = cpu_to_le16(client->argc);
449 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOBLINE, pdu, mem, 0);
454 int fio_clients_connect(void)
456 struct fio_client *client;
457 struct flist_head *entry, *tmp;
462 WSAStartup(MAKEWORD(2,2), &wsd);
465 dprint(FD_NET, "client: connect all\n");
467 client_signal_handler();
469 flist_for_each_safe(entry, tmp, &client_list) {
470 client = flist_entry(entry, struct fio_client, list);
472 ret = fio_client_connect(client);
474 remove_client(client);
478 if (client->argc > 1)
479 send_client_cmd_line(client);
485 int fio_start_client(struct fio_client *client)
487 dprint(FD_NET, "client: start %s\n", client->hostname);
488 return fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_RUN, 0, NULL);
491 int fio_start_all_clients(void)
493 struct fio_client *client;
494 struct flist_head *entry, *tmp;
497 dprint(FD_NET, "client: start all\n");
499 flist_for_each_safe(entry, tmp, &client_list) {
500 client = flist_entry(entry, struct fio_client, list);
502 ret = fio_start_client(client);
504 remove_client(client);
509 return flist_empty(&client_list);
513 * Send file contents to server backend. We could use sendfile(), but to remain
514 * more portable lets just read/write the darn thing.
516 static int __fio_client_send_ini(struct fio_client *client, const char *filename)
523 dprint(FD_NET, "send ini %s to %s\n", filename, client->hostname);
525 fd = open(filename, O_RDONLY);
529 log_err("fio: job file <%s> open: %s\n", filename, strerror(errno));
533 if (fstat(fd, &sb) < 0) {
536 log_err("fio: job file stat: %s\n", strerror(errno));
541 buf = malloc(sb.st_size);
546 ret = read(fd, p, len);
555 else if (errno == EAGAIN || errno == EINTR)
560 log_err("fio: failed reading job file %s\n", filename);
566 client->sent_job = 1;
567 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, buf, sb.st_size, 0);
573 int fio_client_send_ini(struct fio_client *client, const char *filename)
577 ret = __fio_client_send_ini(client, filename);
579 client->sent_job = 1;
584 int fio_clients_send_ini(const char *filename)
586 struct fio_client *client;
587 struct flist_head *entry, *tmp;
589 flist_for_each_safe(entry, tmp, &client_list) {
590 client = flist_entry(entry, struct fio_client, list);
592 if (fio_client_send_ini(client, filename))
593 remove_client(client);
599 static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
601 dst->max_val = le64_to_cpu(src->max_val);
602 dst->min_val = le64_to_cpu(src->min_val);
603 dst->samples = le64_to_cpu(src->samples);
606 * Floats arrive as IEEE 754 encoded uint64_t, convert back to double
608 dst->mean.u.f = fio_uint64_to_double(le64_to_cpu(dst->mean.u.i));
609 dst->S.u.f = fio_uint64_to_double(le64_to_cpu(dst->S.u.i));
612 static void convert_ts(struct thread_stat *dst, struct thread_stat *src)
616 dst->error = le32_to_cpu(src->error);
617 dst->groupid = le32_to_cpu(src->groupid);
618 dst->pid = le32_to_cpu(src->pid);
619 dst->members = le32_to_cpu(src->members);
621 for (i = 0; i < 2; i++) {
622 convert_io_stat(&dst->clat_stat[i], &src->clat_stat[i]);
623 convert_io_stat(&dst->slat_stat[i], &src->slat_stat[i]);
624 convert_io_stat(&dst->lat_stat[i], &src->lat_stat[i]);
625 convert_io_stat(&dst->bw_stat[i], &src->bw_stat[i]);
628 dst->usr_time = le64_to_cpu(src->usr_time);
629 dst->sys_time = le64_to_cpu(src->sys_time);
630 dst->ctx = le64_to_cpu(src->ctx);
631 dst->minf = le64_to_cpu(src->minf);
632 dst->majf = le64_to_cpu(src->majf);
633 dst->clat_percentiles = le64_to_cpu(src->clat_percentiles);
635 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
636 fio_fp64_t *fps = &src->percentile_list[i];
637 fio_fp64_t *fpd = &dst->percentile_list[i];
639 fpd->u.f = fio_uint64_to_double(le64_to_cpu(fps->u.i));
642 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
643 dst->io_u_map[i] = le32_to_cpu(src->io_u_map[i]);
644 dst->io_u_submit[i] = le32_to_cpu(src->io_u_submit[i]);
645 dst->io_u_complete[i] = le32_to_cpu(src->io_u_complete[i]);
648 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
649 dst->io_u_lat_u[i] = le32_to_cpu(src->io_u_lat_u[i]);
650 dst->io_u_lat_m[i] = le32_to_cpu(src->io_u_lat_m[i]);
653 for (i = 0; i < 2; i++)
654 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
655 dst->io_u_plat[i][j] = le32_to_cpu(src->io_u_plat[i][j]);
657 for (i = 0; i < 3; i++) {
658 dst->total_io_u[i] = le64_to_cpu(src->total_io_u[i]);
659 dst->short_io_u[i] = le64_to_cpu(src->short_io_u[i]);
662 dst->total_submit = le64_to_cpu(src->total_submit);
663 dst->total_complete = le64_to_cpu(src->total_complete);
665 for (i = 0; i < 2; i++) {
666 dst->io_bytes[i] = le64_to_cpu(src->io_bytes[i]);
667 dst->runtime[i] = le64_to_cpu(src->runtime[i]);
670 dst->total_run_time = le64_to_cpu(src->total_run_time);
671 dst->continue_on_error = le16_to_cpu(src->continue_on_error);
672 dst->total_err_count = le64_to_cpu(src->total_err_count);
673 dst->first_error = le32_to_cpu(src->first_error);
674 dst->kb_base = le32_to_cpu(src->kb_base);
677 static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
681 for (i = 0; i < 2; i++) {
682 dst->max_run[i] = le64_to_cpu(src->max_run[i]);
683 dst->min_run[i] = le64_to_cpu(src->min_run[i]);
684 dst->max_bw[i] = le64_to_cpu(src->max_bw[i]);
685 dst->min_bw[i] = le64_to_cpu(src->min_bw[i]);
686 dst->io_kb[i] = le64_to_cpu(src->io_kb[i]);
687 dst->agg[i] = le64_to_cpu(src->agg[i]);
690 dst->kb_base = le32_to_cpu(src->kb_base);
691 dst->groupid = le32_to_cpu(src->groupid);
694 static void handle_ts(struct fio_client *client, struct fio_net_cmd *cmd)
696 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
698 show_thread_status(&p->ts, &p->rs);
700 if (sum_stat_clients == 1)
703 sum_thread_stats(&client_ts, &p->ts, sum_stat_nr);
704 sum_group_stats(&client_gs, &p->rs);
707 client_ts.groupid = p->ts.groupid;
709 if (++sum_stat_nr == sum_stat_clients) {
710 strcpy(client_ts.name, "All clients");
711 show_thread_status(&client_ts, &client_gs);
715 static void handle_gs(struct fio_client *client, struct fio_net_cmd *cmd)
717 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
719 show_group_stats(gs);
722 static void handle_text(struct fio_client *client, struct fio_net_cmd *cmd)
724 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
725 const char *buf = (const char *) pdu->buf;
729 name = client->name ? client->name : client->hostname;
731 if (!client->skip_newline)
732 fprintf(f_out, "<%s> ", name);
733 ret = fwrite(buf, pdu->buf_len, 1, f_out);
735 client->skip_newline = strchr(buf, '\n') == NULL;
738 static void convert_agg(struct disk_util_agg *agg)
742 for (i = 0; i < 2; i++) {
743 agg->ios[i] = le32_to_cpu(agg->ios[i]);
744 agg->merges[i] = le32_to_cpu(agg->merges[i]);
745 agg->sectors[i] = le64_to_cpu(agg->sectors[i]);
746 agg->ticks[i] = le32_to_cpu(agg->ticks[i]);
749 agg->io_ticks = le32_to_cpu(agg->io_ticks);
750 agg->time_in_queue = le32_to_cpu(agg->time_in_queue);
751 agg->slavecount = le32_to_cpu(agg->slavecount);
752 agg->max_util.u.f = fio_uint64_to_double(__le64_to_cpu(agg->max_util.u.i));
755 static void convert_dus(struct disk_util_stat *dus)
759 for (i = 0; i < 2; i++) {
760 dus->ios[i] = le32_to_cpu(dus->ios[i]);
761 dus->merges[i] = le32_to_cpu(dus->merges[i]);
762 dus->sectors[i] = le64_to_cpu(dus->sectors[i]);
763 dus->ticks[i] = le32_to_cpu(dus->ticks[i]);
766 dus->io_ticks = le32_to_cpu(dus->io_ticks);
767 dus->time_in_queue = le32_to_cpu(dus->time_in_queue);
768 dus->msec = le64_to_cpu(dus->msec);
771 static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd)
773 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
775 if (!client->disk_stats_shown) {
776 client->disk_stats_shown = 1;
777 log_info("\nDisk stats (read/write):\n");
780 print_disk_util(&du->dus, &du->agg, terse_output);
783 static void convert_jobs_eta(struct jobs_eta *je)
787 je->nr_running = le32_to_cpu(je->nr_running);
788 je->nr_ramp = le32_to_cpu(je->nr_ramp);
789 je->nr_pending = le32_to_cpu(je->nr_pending);
790 je->files_open = le32_to_cpu(je->files_open);
792 for (i = 0; i < 2; i++) {
793 je->m_rate[i] = le32_to_cpu(je->m_rate[i]);
794 je->t_rate[i] = le32_to_cpu(je->t_rate[i]);
795 je->m_iops[i] = le32_to_cpu(je->m_iops[i]);
796 je->t_iops[i] = le32_to_cpu(je->t_iops[i]);
797 je->rate[i] = le32_to_cpu(je->rate[i]);
798 je->iops[i] = le32_to_cpu(je->iops[i]);
801 je->elapsed_sec = le64_to_cpu(je->elapsed_sec);
802 je->eta_sec = le64_to_cpu(je->eta_sec);
803 je->nr_threads = le32_to_cpu(je->nr_threads);
806 void fio_client_sum_jobs_eta(struct jobs_eta *dst, struct jobs_eta *je)
810 dst->nr_running += je->nr_running;
811 dst->nr_ramp += je->nr_ramp;
812 dst->nr_pending += je->nr_pending;
813 dst->files_open += je->files_open;
815 for (i = 0; i < 2; i++) {
816 dst->m_rate[i] += je->m_rate[i];
817 dst->t_rate[i] += je->t_rate[i];
818 dst->m_iops[i] += je->m_iops[i];
819 dst->t_iops[i] += je->t_iops[i];
820 dst->rate[i] += je->rate[i];
821 dst->iops[i] += je->iops[i];
824 dst->elapsed_sec += je->elapsed_sec;
826 if (je->eta_sec > dst->eta_sec)
827 dst->eta_sec = je->eta_sec;
829 dst->nr_threads += je->nr_threads;
830 /* we need to handle je->run_str too ... */
833 void fio_client_dec_jobs_eta(struct client_eta *eta, client_eta_op eta_fn)
835 if (!--eta->pending) {
841 static void remove_reply_cmd(struct fio_client *client, struct fio_net_cmd *cmd)
843 struct fio_net_int_cmd *icmd = NULL;
844 struct flist_head *entry;
846 flist_for_each(entry, &client->cmd_list) {
847 icmd = flist_entry(entry, struct fio_net_int_cmd, list);
849 if (cmd->tag == (uintptr_t) icmd)
856 log_err("fio: client: unable to find matching tag\n");
860 flist_del(&icmd->list);
861 cmd->tag = icmd->saved_tag;
865 static void handle_eta(struct fio_client *client, struct fio_net_cmd *cmd)
867 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
868 struct client_eta *eta = (struct client_eta *) (uintptr_t) cmd->tag;
870 dprint(FD_NET, "client: got eta tag %p, %d\n", eta, eta->pending);
872 assert(client->eta_in_flight == eta);
874 client->eta_in_flight = NULL;
875 flist_del_init(&client->eta_list);
877 if (client->ops->jobs_eta)
878 client->ops->jobs_eta(client, je);
880 fio_client_sum_jobs_eta(&eta->eta, je);
881 fio_client_dec_jobs_eta(eta, client->ops->eta);
884 static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd)
886 struct cmd_probe_pdu *probe = (struct cmd_probe_pdu *) cmd->payload;
887 const char *os, *arch;
890 os = fio_get_os_string(probe->os);
894 arch = fio_get_arch_string(probe->arch);
898 sprintf(bit, "%d-bit", probe->bpp * 8);
900 log_info("hostname=%s, be=%u, %s, os=%s, arch=%s, fio=%u.%u.%u\n",
901 probe->hostname, probe->bigendian, bit, os, arch,
902 probe->fio_major, probe->fio_minor, probe->fio_patch);
905 client->name = strdup((char *) probe->hostname);
908 static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd)
910 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
912 client->state = Client_started;
913 client->jobs = pdu->jobs;
916 static void handle_stop(struct fio_client *client, struct fio_net_cmd *cmd)
919 log_info("client <%s>: exited with error %d\n", client->hostname, client->error);
922 static void convert_stop(struct fio_net_cmd *cmd)
924 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
926 pdu->error = le32_to_cpu(pdu->error);
929 static void convert_text(struct fio_net_cmd *cmd)
931 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
933 pdu->level = le32_to_cpu(pdu->level);
934 pdu->buf_len = le32_to_cpu(pdu->buf_len);
935 pdu->log_sec = le64_to_cpu(pdu->log_sec);
936 pdu->log_usec = le64_to_cpu(pdu->log_usec);
939 int fio_handle_client(struct fio_client *client)
941 struct client_ops *ops = client->ops;
942 struct fio_net_cmd *cmd;
944 dprint(FD_NET, "client: handle %s\n", client->hostname);
946 cmd = fio_net_recv_cmd(client->fd);
950 dprint(FD_NET, "client: got cmd op %s from %s (pdu=%u)\n",
951 fio_server_op(cmd->opcode), client->hostname, cmd->pdu_len);
953 switch (cmd->opcode) {
954 case FIO_NET_CMD_QUIT:
957 remove_client(client);
960 case FIO_NET_CMD_TEXT:
962 ops->text_op(client, cmd);
965 case FIO_NET_CMD_DU: {
966 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
968 convert_dus(&du->dus);
969 convert_agg(&du->agg);
971 ops->disk_util(client, cmd);
975 case FIO_NET_CMD_TS: {
976 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
978 convert_ts(&p->ts, &p->ts);
979 convert_gs(&p->rs, &p->rs);
981 ops->thread_status(client, cmd);
985 case FIO_NET_CMD_GS: {
986 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
990 ops->group_stats(client, cmd);
994 case FIO_NET_CMD_ETA: {
995 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
997 remove_reply_cmd(client, cmd);
998 convert_jobs_eta(je);
999 handle_eta(client, cmd);
1003 case FIO_NET_CMD_PROBE:
1004 remove_reply_cmd(client, cmd);
1005 ops->probe(client, cmd);
1008 case FIO_NET_CMD_SERVER_START:
1009 client->state = Client_running;
1011 ops->job_start(client, cmd);
1014 case FIO_NET_CMD_START: {
1015 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
1017 pdu->jobs = le32_to_cpu(pdu->jobs);
1018 ops->start(client, cmd);
1022 case FIO_NET_CMD_STOP: {
1023 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
1026 client->state = Client_stopped;
1027 client->error = pdu->error;
1028 ops->stop(client, cmd);
1032 case FIO_NET_CMD_ADD_JOB:
1034 ops->add_job(client, cmd);
1038 log_err("fio: unknown client op: %s\n", fio_server_op(cmd->opcode));
1046 static void request_client_etas(struct client_ops *ops)
1048 struct fio_client *client;
1049 struct flist_head *entry;
1050 struct client_eta *eta;
1053 dprint(FD_NET, "client: request eta (%d)\n", nr_clients);
1055 eta = malloc(sizeof(*eta));
1056 memset(&eta->eta, 0, sizeof(eta->eta));
1057 eta->pending = nr_clients;
1059 flist_for_each(entry, &client_list) {
1060 client = flist_entry(entry, struct fio_client, list);
1062 if (!flist_empty(&client->eta_list)) {
1066 if (client->state != Client_running)
1069 assert(!client->eta_in_flight);
1070 flist_add_tail(&client->eta_list, &eta_list);
1071 client->eta_in_flight = eta;
1072 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_SEND_ETA,
1073 (uintptr_t) eta, &client->cmd_list);
1077 fio_client_dec_jobs_eta(eta, ops->eta);
1079 dprint(FD_NET, "client: requested eta tag %p\n", eta);
1082 static int client_check_cmd_timeout(struct fio_client *client,
1083 struct timeval *now)
1085 struct fio_net_int_cmd *cmd;
1086 struct flist_head *entry, *tmp;
1089 flist_for_each_safe(entry, tmp, &client->cmd_list) {
1090 cmd = flist_entry(entry, struct fio_net_int_cmd, list);
1092 if (mtime_since(&cmd->tv, now) < FIO_NET_CLIENT_TIMEOUT)
1095 log_err("fio: client %s, timeout on cmd %s\n", client->hostname,
1096 fio_server_op(cmd->cmd.opcode));
1097 flist_del(&cmd->list);
1102 return flist_empty(&client->cmd_list) && ret;
1105 static int fio_check_clients_timed_out(void)
1107 struct fio_client *client;
1108 struct flist_head *entry, *tmp;
1112 gettimeofday(&tv, NULL);
1114 flist_for_each_safe(entry, tmp, &client_list) {
1115 client = flist_entry(entry, struct fio_client, list);
1117 if (flist_empty(&client->cmd_list))
1120 if (!client_check_cmd_timeout(client, &tv))
1123 if (client->ops->timed_out)
1124 client->ops->timed_out(client);
1126 log_err("fio: client %s timed out\n", client->hostname);
1128 remove_client(client);
1135 int fio_handle_clients(struct client_ops *ops)
1137 struct pollfd *pfds;
1138 int i, ret = 0, retval = 0;
1140 gettimeofday(&eta_tv, NULL);
1142 pfds = malloc(nr_clients * sizeof(struct pollfd));
1144 sum_stat_clients = nr_clients;
1145 init_thread_stat(&client_ts);
1146 init_group_run_stat(&client_gs);
1148 while (!exit_backend && nr_clients) {
1149 struct flist_head *entry, *tmp;
1150 struct fio_client *client;
1153 flist_for_each_safe(entry, tmp, &client_list) {
1154 client = flist_entry(entry, struct fio_client, list);
1156 if (!client->sent_job && !client->ops->stay_connected &&
1157 flist_empty(&client->cmd_list)) {
1158 remove_client(client);
1162 pfds[i].fd = client->fd;
1163 pfds[i].events = POLLIN;
1170 assert(i == nr_clients);
1175 gettimeofday(&tv, NULL);
1176 if (mtime_since(&eta_tv, &tv) >= ops->eta_msec) {
1177 request_client_etas(ops);
1178 memcpy(&eta_tv, &tv, sizeof(tv));
1180 if (fio_check_clients_timed_out())
1184 ret = poll(pfds, nr_clients, 100);
1188 log_err("fio: poll clients: %s\n", strerror(errno));
1194 for (i = 0; i < nr_clients; i++) {
1195 if (!(pfds[i].revents & POLLIN))
1198 client = find_client_by_fd(pfds[i].fd);
1200 log_err("fio: unknown client fd %d\n", pfds[i].fd);
1203 if (!fio_handle_client(client)) {
1204 log_info("client: host=%s disconnected\n",
1206 remove_client(client);
1208 } else if (client->error)
1210 fio_put_client(client);