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;
32 struct sockaddr_in addr;
33 struct sockaddr_un addr_un;
45 struct flist_head eta_list;
46 struct client_eta *eta_in_flight;
48 struct flist_head cmd_list;
54 static struct timeval eta_tv;
64 static FLIST_HEAD(client_list);
65 static FLIST_HEAD(eta_list);
67 static FLIST_HEAD(arg_list);
69 static struct thread_stat client_ts;
70 static struct group_run_stats client_gs;
71 static int sum_stat_clients;
72 static int sum_stat_nr;
74 #define FIO_CLIENT_HASH_BITS 7
75 #define FIO_CLIENT_HASH_SZ (1 << FIO_CLIENT_HASH_BITS)
76 #define FIO_CLIENT_HASH_MASK (FIO_CLIENT_HASH_SZ - 1)
77 static struct flist_head client_hash[FIO_CLIENT_HASH_SZ];
79 static int handle_client(struct fio_client *client);
80 static void dec_jobs_eta(struct client_eta *eta);
82 static void fio_client_add_hash(struct fio_client *client)
84 int bucket = hash_long(client->fd, FIO_CLIENT_HASH_BITS);
86 bucket &= FIO_CLIENT_HASH_MASK;
87 flist_add(&client->hash_list, &client_hash[bucket]);
90 static void fio_client_remove_hash(struct fio_client *client)
92 if (!flist_empty(&client->hash_list))
93 flist_del_init(&client->hash_list);
96 static void fio_init fio_client_hash_init(void)
100 for (i = 0; i < FIO_CLIENT_HASH_SZ; i++)
101 INIT_FLIST_HEAD(&client_hash[i]);
104 static struct fio_client *find_client_by_fd(int fd)
106 int bucket = hash_long(fd, FIO_CLIENT_HASH_BITS) & FIO_CLIENT_HASH_MASK;
107 struct fio_client *client;
108 struct flist_head *entry;
110 flist_for_each(entry, &client_hash[bucket]) {
111 client = flist_entry(entry, struct fio_client, hash_list);
113 if (client->fd == fd)
120 static void remove_client(struct fio_client *client)
122 dprint(FD_NET, "client: removed <%s>\n", client->hostname);
123 flist_del(&client->list);
125 fio_client_remove_hash(client);
127 if (!flist_empty(&client->eta_list)) {
128 flist_del_init(&client->eta_list);
129 dec_jobs_eta(client->eta_in_flight);
132 free(client->hostname);
143 static void __fio_client_add_cmd_option(struct fio_client *client,
148 index = client->argc++;
149 client->argv = realloc(client->argv, sizeof(char *) * client->argc);
150 client->argv[index] = strdup(opt);
151 dprint(FD_NET, "client: add cmd %d: %s\n", index, opt);
154 void fio_client_add_cmd_option(void *cookie, const char *opt)
156 struct fio_client *client = cookie;
157 struct flist_head *entry;
162 __fio_client_add_cmd_option(client, opt);
165 * Duplicate arguments to shared client group
167 flist_for_each(entry, &arg_list) {
168 client = flist_entry(entry, struct fio_client, arg_list);
170 __fio_client_add_cmd_option(client, opt);
174 int fio_client_add(const char *hostname, void **cookie)
176 struct fio_client *existing = *cookie;
177 struct fio_client *client;
181 * We always add our "exec" name as the option, hence 1
184 if (existing->argc == 1)
185 flist_add_tail(&existing->arg_list, &arg_list);
187 while (!flist_empty(&arg_list))
188 flist_del_init(arg_list.next);
192 client = malloc(sizeof(*client));
193 memset(client, 0, sizeof(*client));
195 INIT_FLIST_HEAD(&client->list);
196 INIT_FLIST_HEAD(&client->hash_list);
197 INIT_FLIST_HEAD(&client->arg_list);
198 INIT_FLIST_HEAD(&client->eta_list);
199 INIT_FLIST_HEAD(&client->cmd_list);
201 if (fio_server_parse_string(hostname, &client->hostname,
202 &client->is_sock, &client->port,
203 &client->addr.sin_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);
217 static int fio_client_connect_ip(struct fio_client *client)
221 client->addr.sin_family = AF_INET;
222 client->addr.sin_port = htons(client->port);
224 fd = socket(AF_INET, SOCK_STREAM, 0);
226 log_err("fio: socket: %s\n", strerror(errno));
230 if (connect(fd, (struct sockaddr *) &client->addr, sizeof(client->addr)) < 0) {
231 log_err("fio: connect: %s\n", strerror(errno));
232 log_err("fio: failed to connect to %s:%u\n", client->hostname,
241 static int fio_client_connect_sock(struct fio_client *client)
243 struct sockaddr_un *addr = &client->addr_un;
247 memset(addr, 0, sizeof(*addr));
248 addr->sun_family = AF_UNIX;
249 strcpy(addr->sun_path, client->hostname);
251 fd = socket(AF_UNIX, SOCK_STREAM, 0);
253 log_err("fio: socket: %s\n", strerror(errno));
257 len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
258 if (connect(fd, (struct sockaddr *) addr, len) < 0) {
259 log_err("fio: connect; %s\n", strerror(errno));
267 static int fio_client_connect(struct fio_client *client)
271 dprint(FD_NET, "client: connect to host %s\n", client->hostname);
274 fd = fio_client_connect_sock(client);
276 fd = fio_client_connect_ip(client);
278 dprint(FD_NET, "client: %s connected %d\n", client->hostname, fd);
284 fio_client_add_hash(client);
285 client->state = Client_connected;
289 void fio_clients_terminate(void)
291 struct flist_head *entry;
292 struct fio_client *client;
294 dprint(FD_NET, "client: terminate clients\n");
296 flist_for_each(entry, &client_list) {
297 client = flist_entry(entry, struct fio_client, list);
299 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_QUIT, 0, NULL);
303 static void sig_int(int sig)
305 dprint(FD_NET, "client: got signal %d\n", sig);
306 fio_clients_terminate();
309 static void client_signal_handler(void)
311 struct sigaction act;
313 memset(&act, 0, sizeof(act));
314 act.sa_handler = sig_int;
315 act.sa_flags = SA_RESTART;
316 sigaction(SIGINT, &act, NULL);
318 memset(&act, 0, sizeof(act));
319 act.sa_handler = sig_int;
320 act.sa_flags = SA_RESTART;
321 sigaction(SIGTERM, &act, NULL);
324 static void probe_client(struct fio_client *client)
326 dprint(FD_NET, "client: send probe\n");
328 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_PROBE, 0, &client->cmd_list);
331 static int send_client_cmd_line(struct fio_client *client)
333 struct cmd_single_line_pdu *cslp;
334 struct cmd_line_pdu *clp;
335 unsigned long offset;
341 dprint(FD_NET, "client: send cmdline %d\n", client->argc);
343 lens = malloc(client->argc * sizeof(unsigned int));
346 * Find out how much mem we need
348 for (i = 0, mem = 0; i < client->argc; i++) {
349 lens[i] = strlen(client->argv[i]) + 1;
354 * We need one cmd_line_pdu, and argc number of cmd_single_line_pdu
356 mem += sizeof(*clp) + (client->argc * sizeof(*cslp));
360 offset = sizeof(*clp);
362 for (i = 0; i < client->argc; i++) {
363 uint16_t arg_len = lens[i];
366 strcpy((char *) cslp->text, client->argv[i]);
367 cslp->len = cpu_to_le16(arg_len);
368 offset += sizeof(*cslp) + arg_len;
372 clp->lines = cpu_to_le16(client->argc);
373 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOBLINE, pdu, mem, 0);
378 int fio_clients_connect(void)
380 struct fio_client *client;
381 struct flist_head *entry, *tmp;
384 dprint(FD_NET, "client: connect all\n");
386 client_signal_handler();
388 flist_for_each_safe(entry, tmp, &client_list) {
389 client = flist_entry(entry, struct fio_client, list);
391 ret = fio_client_connect(client);
393 remove_client(client);
397 probe_client(client);
399 if (client->argc > 1)
400 send_client_cmd_line(client);
407 * Send file contents to server backend. We could use sendfile(), but to remain
408 * more portable lets just read/write the darn thing.
410 static int fio_client_send_ini(struct fio_client *client, const char *filename)
417 dprint(FD_NET, "send ini %s to %s\n", filename, client->hostname);
419 fd = open(filename, O_RDONLY);
421 log_err("fio: job file <%s> open: %s\n", filename, strerror(errno));
425 if (fstat(fd, &sb) < 0) {
426 log_err("fio: job file stat: %s\n", strerror(errno));
431 buf = malloc(sb.st_size);
436 ret = read(fd, p, len);
445 else if (errno == EAGAIN || errno == EINTR)
450 log_err("fio: failed reading job file %s\n", filename);
456 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, buf, sb.st_size, 0);
462 int fio_clients_send_ini(const char *filename)
464 struct fio_client *client;
465 struct flist_head *entry, *tmp;
467 flist_for_each_safe(entry, tmp, &client_list) {
468 client = flist_entry(entry, struct fio_client, list);
470 if (fio_client_send_ini(client, filename))
471 remove_client(client);
477 static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
479 dst->max_val = le64_to_cpu(src->max_val);
480 dst->min_val = le64_to_cpu(src->min_val);
481 dst->samples = le64_to_cpu(src->samples);
484 * Floats arrive as IEEE 754 encoded uint64_t, convert back to double
486 dst->mean.u.f = fio_uint64_to_double(le64_to_cpu(dst->mean.u.i));
487 dst->S.u.f = fio_uint64_to_double(le64_to_cpu(dst->S.u.i));
490 static void convert_ts(struct thread_stat *dst, struct thread_stat *src)
494 dst->error = le32_to_cpu(src->error);
495 dst->groupid = le32_to_cpu(src->groupid);
496 dst->pid = le32_to_cpu(src->pid);
497 dst->members = le32_to_cpu(src->members);
499 for (i = 0; i < 2; i++) {
500 convert_io_stat(&dst->clat_stat[i], &src->clat_stat[i]);
501 convert_io_stat(&dst->slat_stat[i], &src->slat_stat[i]);
502 convert_io_stat(&dst->lat_stat[i], &src->lat_stat[i]);
503 convert_io_stat(&dst->bw_stat[i], &src->bw_stat[i]);
506 dst->usr_time = le64_to_cpu(src->usr_time);
507 dst->sys_time = le64_to_cpu(src->sys_time);
508 dst->ctx = le64_to_cpu(src->ctx);
509 dst->minf = le64_to_cpu(src->minf);
510 dst->majf = le64_to_cpu(src->majf);
511 dst->clat_percentiles = le64_to_cpu(src->clat_percentiles);
513 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
514 fio_fp64_t *fps = &src->percentile_list[i];
515 fio_fp64_t *fpd = &dst->percentile_list[i];
517 fpd->u.f = fio_uint64_to_double(le64_to_cpu(fps->u.i));
520 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
521 dst->io_u_map[i] = le32_to_cpu(src->io_u_map[i]);
522 dst->io_u_submit[i] = le32_to_cpu(src->io_u_submit[i]);
523 dst->io_u_complete[i] = le32_to_cpu(src->io_u_complete[i]);
526 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
527 dst->io_u_lat_u[i] = le32_to_cpu(src->io_u_lat_u[i]);
528 dst->io_u_lat_m[i] = le32_to_cpu(src->io_u_lat_m[i]);
531 for (i = 0; i < 2; i++)
532 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
533 dst->io_u_plat[i][j] = le32_to_cpu(src->io_u_plat[i][j]);
535 for (i = 0; i < 3; i++) {
536 dst->total_io_u[i] = le64_to_cpu(src->total_io_u[i]);
537 dst->short_io_u[i] = le64_to_cpu(src->short_io_u[i]);
540 dst->total_submit = le64_to_cpu(src->total_submit);
541 dst->total_complete = le64_to_cpu(src->total_complete);
543 for (i = 0; i < 2; i++) {
544 dst->io_bytes[i] = le64_to_cpu(src->io_bytes[i]);
545 dst->runtime[i] = le64_to_cpu(src->runtime[i]);
548 dst->total_run_time = le64_to_cpu(src->total_run_time);
549 dst->continue_on_error = le16_to_cpu(src->continue_on_error);
550 dst->total_err_count = le64_to_cpu(src->total_err_count);
551 dst->first_error = le32_to_cpu(src->first_error);
552 dst->kb_base = le32_to_cpu(src->kb_base);
555 static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
559 for (i = 0; i < 2; i++) {
560 dst->max_run[i] = le64_to_cpu(src->max_run[i]);
561 dst->min_run[i] = le64_to_cpu(src->min_run[i]);
562 dst->max_bw[i] = le64_to_cpu(src->max_bw[i]);
563 dst->min_bw[i] = le64_to_cpu(src->min_bw[i]);
564 dst->io_kb[i] = le64_to_cpu(src->io_kb[i]);
565 dst->agg[i] = le64_to_cpu(src->agg[i]);
568 dst->kb_base = le32_to_cpu(src->kb_base);
569 dst->groupid = le32_to_cpu(src->groupid);
572 static void handle_ts(struct fio_net_cmd *cmd)
574 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
576 convert_ts(&p->ts, &p->ts);
577 convert_gs(&p->rs, &p->rs);
579 show_thread_status(&p->ts, &p->rs);
581 if (sum_stat_clients == 1)
584 sum_thread_stats(&client_ts, &p->ts, sum_stat_nr);
585 sum_group_stats(&client_gs, &p->rs);
588 client_ts.groupid = p->ts.groupid;
590 if (++sum_stat_nr == sum_stat_clients) {
591 strcpy(client_ts.name, "All clients");
592 show_thread_status(&client_ts, &client_gs);
596 static void handle_gs(struct fio_net_cmd *cmd)
598 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
601 show_group_stats(gs);
604 static void convert_jobs_eta(struct jobs_eta *je)
608 je->nr_running = le32_to_cpu(je->nr_running);
609 je->nr_ramp = le32_to_cpu(je->nr_ramp);
610 je->nr_pending = le32_to_cpu(je->nr_pending);
611 je->files_open = le32_to_cpu(je->files_open);
612 je->m_rate = le32_to_cpu(je->m_rate);
613 je->t_rate = le32_to_cpu(je->t_rate);
614 je->m_iops = le32_to_cpu(je->m_iops);
615 je->t_iops = le32_to_cpu(je->t_iops);
617 for (i = 0; i < 2; i++) {
618 je->rate[i] = le32_to_cpu(je->rate[i]);
619 je->iops[i] = le32_to_cpu(je->iops[i]);
622 je->elapsed_sec = le64_to_cpu(je->elapsed_sec);
623 je->eta_sec = le64_to_cpu(je->eta_sec);
626 static void sum_jobs_eta(struct jobs_eta *dst, struct jobs_eta *je)
630 dst->nr_running += je->nr_running;
631 dst->nr_ramp += je->nr_ramp;
632 dst->nr_pending += je->nr_pending;
633 dst->files_open += je->files_open;
634 dst->m_rate += je->m_rate;
635 dst->t_rate += je->t_rate;
636 dst->m_iops += je->m_iops;
637 dst->t_iops += je->t_iops;
639 for (i = 0; i < 2; i++) {
640 dst->rate[i] += je->rate[i];
641 dst->iops[i] += je->iops[i];
644 dst->elapsed_sec += je->elapsed_sec;
646 if (je->eta_sec > dst->eta_sec)
647 dst->eta_sec = je->eta_sec;
650 static void dec_jobs_eta(struct client_eta *eta)
652 if (!--eta->pending) {
653 display_thread_status(&eta->eta);
658 static void remove_reply_cmd(struct fio_client *client, struct fio_net_cmd *cmd)
660 struct fio_net_int_cmd *icmd = NULL;
661 struct flist_head *entry;
663 flist_for_each(entry, &client->cmd_list) {
664 icmd = flist_entry(entry, struct fio_net_int_cmd, list);
666 if (cmd->tag == (uintptr_t) icmd)
673 log_err("fio: client: unable to find matching tag\n");
677 flist_del(&icmd->list);
678 cmd->tag = icmd->saved_tag;
682 static void handle_eta(struct fio_client *client, struct fio_net_cmd *cmd)
684 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
685 struct client_eta *eta = (struct client_eta *) (uintptr_t) cmd->tag;
687 dprint(FD_NET, "client: got eta tag %p, %d\n", eta, eta->pending);
689 assert(client->eta_in_flight == eta);
691 client->eta_in_flight = NULL;
692 flist_del_init(&client->eta_list);
694 convert_jobs_eta(je);
695 sum_jobs_eta(&eta->eta, je);
699 static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd)
701 struct cmd_probe_pdu *probe = (struct cmd_probe_pdu *) cmd->payload;
702 const char *os, *arch;
705 os = fio_get_os_string(probe->os);
709 arch = fio_get_arch_string(probe->arch);
713 sprintf(bit, "%d-bit", probe->bpp * 8);
715 log_info("hostname=%s, be=%u, %s, os=%s, arch=%s, fio=%u.%u.%u\n",
716 probe->hostname, probe->bigendian, bit, os, arch,
717 probe->fio_major, probe->fio_minor, probe->fio_patch);
720 client->name = strdup((char *) probe->hostname);
723 static int handle_client(struct fio_client *client)
725 struct fio_net_cmd *cmd;
727 dprint(FD_NET, "client: handle %s\n", client->hostname);
729 cmd = fio_net_recv_cmd(client->fd);
733 dprint(FD_NET, "client: got cmd op %s from %s\n",
734 fio_server_op(cmd->opcode), client->hostname);
736 switch (cmd->opcode) {
737 case FIO_NET_CMD_QUIT:
738 remove_client(client);
741 case FIO_NET_CMD_TEXT: {
742 const char *buf = (const char *) cmd->payload;
746 name = client->name ? client->name : client->hostname;
748 if (!client->skip_newline)
749 fprintf(f_out, "<%s> ", name);
750 ret = fwrite(buf, cmd->pdu_len, 1, f_out);
752 client->skip_newline = strchr(buf, '\n') == NULL;
764 case FIO_NET_CMD_ETA:
765 remove_reply_cmd(client, cmd);
766 handle_eta(client, cmd);
769 case FIO_NET_CMD_PROBE:
770 remove_reply_cmd(client, cmd);
771 handle_probe(client, cmd);
774 case FIO_NET_CMD_START:
775 client->state = Client_started;
778 case FIO_NET_CMD_STOP:
779 client->state = Client_stopped;
783 log_err("fio: unknown client op: %s\n", fio_server_op(cmd->opcode));
791 static void request_client_etas(void)
793 struct fio_client *client;
794 struct flist_head *entry;
795 struct client_eta *eta;
798 dprint(FD_NET, "client: request eta (%d)\n", nr_clients);
800 eta = malloc(sizeof(*eta));
801 memset(&eta->eta, 0, sizeof(eta->eta));
802 eta->pending = nr_clients;
804 flist_for_each(entry, &client_list) {
805 client = flist_entry(entry, struct fio_client, list);
807 if (!flist_empty(&client->eta_list)) {
812 assert(!client->eta_in_flight);
813 flist_add_tail(&client->eta_list, &eta_list);
814 client->eta_in_flight = eta;
815 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_SEND_ETA,
816 (uintptr_t) eta, &client->cmd_list);
822 dprint(FD_NET, "client: requested eta tag %p\n", eta);
825 static int client_check_cmd_timeout(struct fio_client *client,
828 struct fio_net_int_cmd *cmd;
829 struct flist_head *entry, *tmp;
832 flist_for_each_safe(entry, tmp, &client->cmd_list) {
833 cmd = flist_entry(entry, struct fio_net_int_cmd, list);
835 if (mtime_since(&cmd->tv, now) < FIO_NET_CLIENT_TIMEOUT)
838 log_err("fio: client %s, timeout on cmd %s\n", client->hostname,
839 fio_server_op(cmd->cmd.opcode));
840 flist_del(&cmd->list);
845 return flist_empty(&client->cmd_list) && ret;
848 static int fio_client_timed_out(void)
850 struct fio_client *client;
851 struct flist_head *entry, *tmp;
855 gettimeofday(&tv, NULL);
857 flist_for_each_safe(entry, tmp, &client_list) {
858 client = flist_entry(entry, struct fio_client, list);
860 if (flist_empty(&client->cmd_list))
863 if (!client_check_cmd_timeout(client, &tv))
866 log_err("fio: client %s timed out\n", client->hostname);
867 remove_client(client);
874 int fio_handle_clients(void)
876 struct fio_client *client;
877 struct flist_head *entry;
881 gettimeofday(&eta_tv, NULL);
883 pfds = malloc(nr_clients * sizeof(struct pollfd));
885 sum_stat_clients = nr_clients;
886 init_thread_stat(&client_ts);
887 init_group_run_stat(&client_gs);
889 while (!exit_backend && nr_clients) {
891 flist_for_each(entry, &client_list) {
892 client = flist_entry(entry, struct fio_client, list);
894 pfds[i].fd = client->fd;
895 pfds[i].events = POLLIN;
899 assert(i == nr_clients);
904 gettimeofday(&tv, NULL);
905 if (mtime_since(&eta_tv, &tv) >= 900) {
906 request_client_etas();
907 memcpy(&eta_tv, &tv, sizeof(tv));
909 if (fio_client_timed_out())
913 ret = poll(pfds, nr_clients, 100);
917 log_err("fio: poll clients: %s\n", strerror(errno));
923 for (i = 0; i < nr_clients; i++) {
924 if (!(pfds[i].revents & POLLIN))
927 client = find_client_by_fd(pfds[i].fd);
929 log_err("fio: unknown client fd %d\n", pfds[i].fd);
932 if (!handle_client(client)) {
933 log_info("client: host=%s disconnected\n",
935 remove_client(client);