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 #define FIO_CLIENT_HASH_BITS 7
70 #define FIO_CLIENT_HASH_SZ (1 << FIO_CLIENT_HASH_BITS)
71 #define FIO_CLIENT_HASH_MASK (FIO_CLIENT_HASH_SZ - 1)
72 static struct flist_head client_hash[FIO_CLIENT_HASH_SZ];
74 static int handle_client(struct fio_client *client);
75 static void dec_jobs_eta(struct client_eta *eta);
77 static void fio_client_add_hash(struct fio_client *client)
79 int bucket = hash_long(client->fd, FIO_CLIENT_HASH_BITS);
81 bucket &= FIO_CLIENT_HASH_MASK;
82 flist_add(&client->hash_list, &client_hash[bucket]);
85 static void fio_client_remove_hash(struct fio_client *client)
87 if (!flist_empty(&client->hash_list))
88 flist_del_init(&client->hash_list);
91 static void fio_init fio_client_hash_init(void)
95 for (i = 0; i < FIO_CLIENT_HASH_SZ; i++)
96 INIT_FLIST_HEAD(&client_hash[i]);
99 static struct fio_client *find_client_by_fd(int fd)
101 int bucket = hash_long(fd, FIO_CLIENT_HASH_BITS) & FIO_CLIENT_HASH_MASK;
102 struct fio_client *client;
103 struct flist_head *entry;
105 flist_for_each(entry, &client_hash[bucket]) {
106 client = flist_entry(entry, struct fio_client, hash_list);
108 if (client->fd == fd)
115 static void remove_client(struct fio_client *client)
117 dprint(FD_NET, "client: removed <%s>\n", client->hostname);
118 flist_del(&client->list);
120 fio_client_remove_hash(client);
122 if (!flist_empty(&client->eta_list)) {
123 flist_del_init(&client->eta_list);
124 dec_jobs_eta(client->eta_in_flight);
127 free(client->hostname);
137 static void __fio_client_add_cmd_option(struct fio_client *client,
142 index = client->argc++;
143 client->argv = realloc(client->argv, sizeof(char *) * client->argc);
144 client->argv[index] = strdup(opt);
145 dprint(FD_NET, "client: add cmd %d: %s\n", index, opt);
148 void fio_client_add_cmd_option(void *cookie, const char *opt)
150 struct fio_client *client = cookie;
151 struct flist_head *entry;
156 __fio_client_add_cmd_option(client, opt);
159 * Duplicate arguments to shared client group
161 flist_for_each(entry, &arg_list) {
162 client = flist_entry(entry, struct fio_client, arg_list);
164 __fio_client_add_cmd_option(client, opt);
168 int fio_client_add(const char *hostname, void **cookie)
170 struct fio_client *existing = *cookie;
171 struct fio_client *client;
175 * We always add our "exec" name as the option, hence 1
178 if (existing->argc == 1)
179 flist_add_tail(&existing->arg_list, &arg_list);
181 while (!flist_empty(&arg_list))
182 flist_del_init(arg_list.next);
186 client = malloc(sizeof(*client));
187 memset(client, 0, sizeof(*client));
189 INIT_FLIST_HEAD(&client->list);
190 INIT_FLIST_HEAD(&client->hash_list);
191 INIT_FLIST_HEAD(&client->arg_list);
192 INIT_FLIST_HEAD(&client->eta_list);
193 INIT_FLIST_HEAD(&client->cmd_list);
195 if (fio_server_parse_string(hostname, &client->hostname,
196 &client->is_sock, &client->port,
197 &client->addr.sin_addr))
202 __fio_client_add_cmd_option(client, "fio");
204 flist_add(&client->list, &client_list);
206 dprint(FD_NET, "client: added <%s>\n", client->hostname);
211 static int fio_client_connect_ip(struct fio_client *client)
215 client->addr.sin_family = AF_INET;
216 client->addr.sin_port = htons(client->port);
218 fd = socket(AF_INET, SOCK_STREAM, 0);
220 log_err("fio: socket: %s\n", strerror(errno));
224 if (connect(fd, (struct sockaddr *) &client->addr, sizeof(client->addr)) < 0) {
225 log_err("fio: connect: %s\n", strerror(errno));
226 log_err("fio: failed to connect to %s:%u\n", client->hostname,
235 static int fio_client_connect_sock(struct fio_client *client)
237 struct sockaddr_un *addr = &client->addr_un;
241 memset(addr, 0, sizeof(*addr));
242 addr->sun_family = AF_UNIX;
243 strcpy(addr->sun_path, client->hostname);
245 fd = socket(AF_UNIX, SOCK_STREAM, 0);
247 log_err("fio: socket: %s\n", strerror(errno));
251 len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
252 if (connect(fd, (struct sockaddr *) addr, len) < 0) {
253 log_err("fio: connect; %s\n", strerror(errno));
261 static int fio_client_connect(struct fio_client *client)
265 dprint(FD_NET, "client: connect to host %s\n", client->hostname);
268 fd = fio_client_connect_sock(client);
270 fd = fio_client_connect_ip(client);
272 dprint(FD_NET, "client: %s connected %d\n", client->hostname, fd);
278 fio_client_add_hash(client);
279 client->state = Client_connected;
283 void fio_clients_terminate(void)
285 struct flist_head *entry;
286 struct fio_client *client;
288 dprint(FD_NET, "client: terminate clients\n");
290 flist_for_each(entry, &client_list) {
291 client = flist_entry(entry, struct fio_client, list);
293 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_QUIT, 0, NULL);
297 static void sig_int(int sig)
299 dprint(FD_NET, "client: got signal %d\n", sig);
300 fio_clients_terminate();
303 static void client_signal_handler(void)
305 struct sigaction act;
307 memset(&act, 0, sizeof(act));
308 act.sa_handler = sig_int;
309 act.sa_flags = SA_RESTART;
310 sigaction(SIGINT, &act, NULL);
312 memset(&act, 0, sizeof(act));
313 act.sa_handler = sig_int;
314 act.sa_flags = SA_RESTART;
315 sigaction(SIGTERM, &act, NULL);
318 static void probe_client(struct fio_client *client)
320 dprint(FD_NET, "client: send probe\n");
322 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_PROBE, 0, &client->cmd_list);
325 static int send_client_cmd_line(struct fio_client *client)
327 struct cmd_single_line_pdu *cslp;
328 struct cmd_line_pdu *clp;
329 unsigned long offset;
335 dprint(FD_NET, "client: send cmdline %d\n", client->argc);
337 lens = malloc(client->argc * sizeof(unsigned int));
340 * Find out how much mem we need
342 for (i = 0, mem = 0; i < client->argc; i++) {
343 lens[i] = strlen(client->argv[i]) + 1;
348 * We need one cmd_line_pdu, and argc number of cmd_single_line_pdu
350 mem += sizeof(*clp) + (client->argc * sizeof(*cslp));
354 offset = sizeof(*clp);
356 for (i = 0; i < client->argc; i++) {
357 uint16_t arg_len = lens[i];
360 strcpy((char *) cslp->text, client->argv[i]);
361 cslp->len = cpu_to_le16(arg_len);
362 offset += sizeof(*cslp) + arg_len;
366 clp->lines = cpu_to_le16(client->argc);
367 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOBLINE, pdu, mem, 0);
372 int fio_clients_connect(void)
374 struct fio_client *client;
375 struct flist_head *entry, *tmp;
378 dprint(FD_NET, "client: connect all\n");
380 client_signal_handler();
382 flist_for_each_safe(entry, tmp, &client_list) {
383 client = flist_entry(entry, struct fio_client, list);
385 ret = fio_client_connect(client);
387 remove_client(client);
391 probe_client(client);
393 if (client->argc > 1)
394 send_client_cmd_line(client);
401 * Send file contents to server backend. We could use sendfile(), but to remain
402 * more portable lets just read/write the darn thing.
404 static int fio_client_send_ini(struct fio_client *client, const char *filename)
411 dprint(FD_NET, "send ini %s to %s\n", filename, client->hostname);
413 fd = open(filename, O_RDONLY);
415 log_err("fio: job file <%s> open: %s\n", filename, strerror(errno));
419 if (fstat(fd, &sb) < 0) {
420 log_err("fio: job file stat: %s\n", strerror(errno));
425 buf = malloc(sb.st_size);
430 ret = read(fd, p, len);
439 else if (errno == EAGAIN || errno == EINTR)
444 log_err("fio: failed reading job file %s\n", filename);
450 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, buf, sb.st_size, 0);
456 int fio_clients_send_ini(const char *filename)
458 struct fio_client *client;
459 struct flist_head *entry, *tmp;
461 flist_for_each_safe(entry, tmp, &client_list) {
462 client = flist_entry(entry, struct fio_client, list);
464 if (fio_client_send_ini(client, filename))
465 remove_client(client);
471 static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
473 dst->max_val = le64_to_cpu(src->max_val);
474 dst->min_val = le64_to_cpu(src->min_val);
475 dst->samples = le64_to_cpu(src->samples);
478 * Floats arrive as IEEE 754 encoded uint64_t, convert back to double
480 dst->mean.u.f = fio_uint64_to_double(le64_to_cpu(dst->mean.u.i));
481 dst->S.u.f = fio_uint64_to_double(le64_to_cpu(dst->S.u.i));
484 static void convert_ts(struct thread_stat *dst, struct thread_stat *src)
488 dst->error = le32_to_cpu(src->error);
489 dst->groupid = le32_to_cpu(src->groupid);
490 dst->pid = le32_to_cpu(src->pid);
491 dst->members = le32_to_cpu(src->members);
493 for (i = 0; i < 2; i++) {
494 convert_io_stat(&dst->clat_stat[i], &src->clat_stat[i]);
495 convert_io_stat(&dst->slat_stat[i], &src->slat_stat[i]);
496 convert_io_stat(&dst->lat_stat[i], &src->lat_stat[i]);
497 convert_io_stat(&dst->bw_stat[i], &src->bw_stat[i]);
500 dst->usr_time = le64_to_cpu(src->usr_time);
501 dst->sys_time = le64_to_cpu(src->sys_time);
502 dst->ctx = le64_to_cpu(src->ctx);
503 dst->minf = le64_to_cpu(src->minf);
504 dst->majf = le64_to_cpu(src->majf);
505 dst->clat_percentiles = le64_to_cpu(src->clat_percentiles);
507 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
508 fio_fp64_t *fps = &src->percentile_list[i];
509 fio_fp64_t *fpd = &dst->percentile_list[i];
511 fpd->u.f = fio_uint64_to_double(le64_to_cpu(fps->u.i));
514 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
515 dst->io_u_map[i] = le32_to_cpu(src->io_u_map[i]);
516 dst->io_u_submit[i] = le32_to_cpu(src->io_u_submit[i]);
517 dst->io_u_complete[i] = le32_to_cpu(src->io_u_complete[i]);
520 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
521 dst->io_u_lat_u[i] = le32_to_cpu(src->io_u_lat_u[i]);
522 dst->io_u_lat_m[i] = le32_to_cpu(src->io_u_lat_m[i]);
525 for (i = 0; i < 2; i++)
526 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
527 dst->io_u_plat[i][j] = le32_to_cpu(src->io_u_plat[i][j]);
529 for (i = 0; i < 3; i++) {
530 dst->total_io_u[i] = le64_to_cpu(src->total_io_u[i]);
531 dst->short_io_u[i] = le64_to_cpu(src->short_io_u[i]);
534 dst->total_submit = le64_to_cpu(src->total_submit);
535 dst->total_complete = le64_to_cpu(src->total_complete);
537 for (i = 0; i < 2; i++) {
538 dst->io_bytes[i] = le64_to_cpu(src->io_bytes[i]);
539 dst->runtime[i] = le64_to_cpu(src->runtime[i]);
542 dst->total_run_time = le64_to_cpu(src->total_run_time);
543 dst->continue_on_error = le16_to_cpu(src->continue_on_error);
544 dst->total_err_count = le64_to_cpu(src->total_err_count);
545 dst->first_error = le32_to_cpu(src->first_error);
546 dst->kb_base = le32_to_cpu(src->kb_base);
549 static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
553 for (i = 0; i < 2; i++) {
554 dst->max_run[i] = le64_to_cpu(src->max_run[i]);
555 dst->min_run[i] = le64_to_cpu(src->min_run[i]);
556 dst->max_bw[i] = le64_to_cpu(src->max_bw[i]);
557 dst->min_bw[i] = le64_to_cpu(src->min_bw[i]);
558 dst->io_kb[i] = le64_to_cpu(src->io_kb[i]);
559 dst->agg[i] = le64_to_cpu(src->agg[i]);
562 dst->kb_base = le32_to_cpu(src->kb_base);
563 dst->groupid = le32_to_cpu(src->groupid);
566 static void handle_ts(struct fio_net_cmd *cmd)
568 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
570 convert_ts(&p->ts, &p->ts);
571 convert_gs(&p->rs, &p->rs);
573 show_thread_status(&p->ts, &p->rs);
576 static void handle_gs(struct fio_net_cmd *cmd)
578 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
581 show_group_stats(gs);
584 static void convert_jobs_eta(struct jobs_eta *je)
588 je->nr_running = le32_to_cpu(je->nr_running);
589 je->nr_ramp = le32_to_cpu(je->nr_ramp);
590 je->nr_pending = le32_to_cpu(je->nr_pending);
591 je->files_open = le32_to_cpu(je->files_open);
592 je->m_rate = le32_to_cpu(je->m_rate);
593 je->t_rate = le32_to_cpu(je->t_rate);
594 je->m_iops = le32_to_cpu(je->m_iops);
595 je->t_iops = le32_to_cpu(je->t_iops);
597 for (i = 0; i < 2; i++) {
598 je->rate[i] = le32_to_cpu(je->rate[i]);
599 je->iops[i] = le32_to_cpu(je->iops[i]);
602 je->elapsed_sec = le64_to_cpu(je->elapsed_sec);
603 je->eta_sec = le64_to_cpu(je->eta_sec);
606 static void sum_jobs_eta(struct jobs_eta *dst, struct jobs_eta *je)
610 dst->nr_running += je->nr_running;
611 dst->nr_ramp += je->nr_ramp;
612 dst->nr_pending += je->nr_pending;
613 dst->files_open += je->files_open;
614 dst->m_rate += je->m_rate;
615 dst->t_rate += je->t_rate;
616 dst->m_iops += je->m_iops;
617 dst->t_iops += je->t_iops;
619 for (i = 0; i < 2; i++) {
620 dst->rate[i] += je->rate[i];
621 dst->iops[i] += je->iops[i];
624 dst->elapsed_sec += je->elapsed_sec;
626 if (je->eta_sec > dst->eta_sec)
627 dst->eta_sec = je->eta_sec;
630 static void dec_jobs_eta(struct client_eta *eta)
632 if (!--eta->pending) {
633 display_thread_status(&eta->eta);
638 static void remove_reply_cmd(struct fio_client *client, struct fio_net_cmd *cmd)
640 struct fio_net_int_cmd *icmd = NULL;
641 struct flist_head *entry;
643 flist_for_each(entry, &client->cmd_list) {
644 icmd = flist_entry(entry, struct fio_net_int_cmd, list);
646 if (cmd->tag == (uint64_t) icmd)
653 log_err("fio: client: unable to find matching tag\n");
657 flist_del(&icmd->list);
658 cmd->tag = icmd->saved_tag;
662 static void handle_eta(struct fio_client *client, struct fio_net_cmd *cmd)
664 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
665 struct client_eta *eta = (struct client_eta *) cmd->tag;
667 dprint(FD_NET, "client: got eta tag %p, %d\n", eta, eta->pending);
669 assert(client->eta_in_flight == eta);
671 client->eta_in_flight = NULL;
672 flist_del_init(&client->eta_list);
674 convert_jobs_eta(je);
675 sum_jobs_eta(&eta->eta, je);
679 static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd)
681 struct cmd_probe_pdu *probe = (struct cmd_probe_pdu *) cmd->payload;
682 const char *os, *arch;
684 os = fio_get_os_string(probe->os);
688 arch = fio_get_arch_string(probe->arch);
692 log_info("hostname=%s, be=%u, os=%s, arch=%s, fio=%u.%u.%u\n",
693 probe->hostname, probe->bigendian, os, arch, probe->fio_major,
694 probe->fio_minor, probe->fio_patch);
697 client->name = strdup((char *) probe->hostname);
700 static int handle_client(struct fio_client *client)
702 struct fio_net_cmd *cmd;
704 dprint(FD_NET, "client: handle %s\n", client->hostname);
706 cmd = fio_net_recv_cmd(client->fd);
710 dprint(FD_NET, "client: got cmd op %s from %s\n",
711 fio_server_op(cmd->opcode), client->hostname);
713 switch (cmd->opcode) {
714 case FIO_NET_CMD_QUIT:
715 remove_client(client);
718 case FIO_NET_CMD_TEXT: {
719 const char *buf = (const char *) cmd->payload;
723 name = client->name ? client->name : client->hostname;
725 if (!client->skip_newline)
726 fprintf(f_out, "<%s> ", name);
727 ret = fwrite(buf, cmd->pdu_len, 1, f_out);
729 client->skip_newline = strchr(buf, '\n') == NULL;
741 case FIO_NET_CMD_ETA:
742 remove_reply_cmd(client, cmd);
743 handle_eta(client, cmd);
746 case FIO_NET_CMD_PROBE:
747 remove_reply_cmd(client, cmd);
748 handle_probe(client, cmd);
751 case FIO_NET_CMD_START:
752 client->state = Client_started;
755 case FIO_NET_CMD_STOP:
756 client->state = Client_stopped;
760 log_err("fio: unknown client op: %s\n", fio_server_op(cmd->opcode));
768 static void request_client_etas(void)
770 struct fio_client *client;
771 struct flist_head *entry;
772 struct client_eta *eta;
775 dprint(FD_NET, "client: request eta (%d)\n", nr_clients);
777 eta = malloc(sizeof(*eta));
778 memset(&eta->eta, 0, sizeof(eta->eta));
779 eta->pending = nr_clients;
781 flist_for_each(entry, &client_list) {
782 client = flist_entry(entry, struct fio_client, list);
784 if (!flist_empty(&client->eta_list)) {
789 assert(!client->eta_in_flight);
790 flist_add_tail(&client->eta_list, &eta_list);
791 client->eta_in_flight = eta;
792 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_SEND_ETA,
793 (uint64_t) eta, &client->cmd_list);
799 dprint(FD_NET, "client: requested eta tag %p\n", eta);
802 static int client_check_cmd_timeout(struct fio_client *client,
805 struct fio_net_int_cmd *cmd;
806 struct flist_head *entry, *tmp;
809 flist_for_each_safe(entry, tmp, &client->cmd_list) {
810 cmd = flist_entry(entry, struct fio_net_int_cmd, list);
812 if (mtime_since(&cmd->tv, now) < FIO_NET_CLIENT_TIMEOUT)
815 log_err("fio: client %s, timeout on cmd %s\n", client->hostname,
816 fio_server_op(cmd->cmd.opcode));
817 flist_del(&cmd->list);
822 return flist_empty(&client->cmd_list) && ret;
825 static int fio_client_timed_out(void)
827 struct fio_client *client;
828 struct flist_head *entry, *tmp;
832 gettimeofday(&tv, NULL);
834 flist_for_each_safe(entry, tmp, &client_list) {
835 client = flist_entry(entry, struct fio_client, list);
837 if (flist_empty(&client->cmd_list))
840 if (!client_check_cmd_timeout(client, &tv))
843 log_err("fio: client %s timed out\n", client->hostname);
844 remove_client(client);
851 int fio_handle_clients(void)
853 struct fio_client *client;
854 struct flist_head *entry;
858 gettimeofday(&eta_tv, NULL);
860 pfds = malloc(nr_clients * sizeof(struct pollfd));
862 while (!exit_backend && nr_clients) {
864 flist_for_each(entry, &client_list) {
865 client = flist_entry(entry, struct fio_client, list);
867 pfds[i].fd = client->fd;
868 pfds[i].events = POLLIN;
872 assert(i == nr_clients);
877 gettimeofday(&tv, NULL);
878 if (mtime_since(&eta_tv, &tv) >= 900) {
879 request_client_etas();
880 memcpy(&eta_tv, &tv, sizeof(tv));
882 if (fio_client_timed_out())
886 ret = poll(pfds, nr_clients, 100);
890 log_err("fio: poll clients: %s\n", strerror(errno));
896 for (i = 0; i < nr_clients; i++) {
897 if (!(pfds[i].revents & POLLIN))
900 client = find_client_by_fd(pfds[i].fd);
902 log_err("fio: unknown client fd %d\n", pfds[i].fd);
905 if (!handle_client(client)) {
906 log_info("client: host=%s disconnected\n",
908 remove_client(client);