11 #include <sys/socket.h>
13 #include <netinet/in.h>
14 #include <arpa/inet.h>
25 static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd);
26 static void handle_ts(struct fio_client *client, struct fio_net_cmd *cmd);
27 static void handle_gs(struct fio_client *client, struct fio_net_cmd *cmd);
28 static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd);
29 static void handle_text(struct fio_client *client, struct fio_net_cmd *cmd);
30 static void handle_stop(struct fio_client *client, struct fio_net_cmd *cmd);
31 static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd);
33 struct client_ops fio_client_ops = {
35 .disk_util = handle_du,
36 .thread_status = handle_ts,
37 .group_stats = handle_gs,
39 .start = handle_start,
40 .eta = display_thread_status,
41 .probe = handle_probe,
42 .eta_msec = FIO_CLIENT_DEF_ETA_MSEC,
43 .client_type = FIO_CLIENT_TYPE_CLI,
46 static struct timeval eta_tv;
48 static FLIST_HEAD(client_list);
49 static FLIST_HEAD(eta_list);
51 static FLIST_HEAD(arg_list);
53 struct thread_stat client_ts;
54 struct group_run_stats client_gs;
57 static int sum_stat_nr;
59 #define FIO_CLIENT_HASH_BITS 7
60 #define FIO_CLIENT_HASH_SZ (1 << FIO_CLIENT_HASH_BITS)
61 #define FIO_CLIENT_HASH_MASK (FIO_CLIENT_HASH_SZ - 1)
62 static struct flist_head client_hash[FIO_CLIENT_HASH_SZ];
64 static void fio_client_add_hash(struct fio_client *client)
66 int bucket = hash_long(client->fd, FIO_CLIENT_HASH_BITS);
68 bucket &= FIO_CLIENT_HASH_MASK;
69 flist_add(&client->hash_list, &client_hash[bucket]);
72 static void fio_client_remove_hash(struct fio_client *client)
74 if (!flist_empty(&client->hash_list))
75 flist_del_init(&client->hash_list);
78 static void fio_init fio_client_hash_init(void)
82 for (i = 0; i < FIO_CLIENT_HASH_SZ; i++)
83 INIT_FLIST_HEAD(&client_hash[i]);
86 static struct fio_client *find_client_by_fd(int fd)
88 int bucket = hash_long(fd, FIO_CLIENT_HASH_BITS) & FIO_CLIENT_HASH_MASK;
89 struct fio_client *client;
90 struct flist_head *entry;
92 flist_for_each(entry, &client_hash[bucket]) {
93 client = flist_entry(entry, struct fio_client, hash_list);
95 if (client->fd == fd) {
104 void fio_put_client(struct fio_client *client)
109 free(client->hostname);
114 while (client->nr_ini_file)
115 free(client->ini_file[--client->nr_ini_file]);
116 if (client->ini_file)
117 free(client->ini_file);
122 static void remove_client(struct fio_client *client)
124 assert(client->refs);
126 dprint(FD_NET, "client: removed <%s>\n", client->hostname);
128 if (!flist_empty(&client->list))
129 flist_del_init(&client->list);
131 fio_client_remove_hash(client);
133 if (!flist_empty(&client->eta_list)) {
134 flist_del_init(&client->eta_list);
135 fio_client_dec_jobs_eta(client->eta_in_flight, client->ops->eta);
141 if (client->ops->removed)
142 client->ops->removed(client);
147 fio_put_client(client);
150 struct fio_client *fio_get_client(struct fio_client *client)
156 static void __fio_client_add_cmd_option(struct fio_client *client,
161 index = client->argc++;
162 client->argv = realloc(client->argv, sizeof(char *) * client->argc);
163 client->argv[index] = strdup(opt);
164 dprint(FD_NET, "client: add cmd %d: %s\n", index, opt);
167 void fio_client_add_cmd_option(void *cookie, const char *opt)
169 struct fio_client *client = cookie;
170 struct flist_head *entry;
175 __fio_client_add_cmd_option(client, opt);
178 * Duplicate arguments to shared client group
180 flist_for_each(entry, &arg_list) {
181 client = flist_entry(entry, struct fio_client, arg_list);
183 __fio_client_add_cmd_option(client, opt);
187 struct fio_client *fio_client_add_explicit(struct client_ops *ops,
188 const char *hostname, int type,
191 struct fio_client *client;
193 client = malloc(sizeof(*client));
194 memset(client, 0, sizeof(*client));
196 INIT_FLIST_HEAD(&client->list);
197 INIT_FLIST_HEAD(&client->hash_list);
198 INIT_FLIST_HEAD(&client->arg_list);
199 INIT_FLIST_HEAD(&client->eta_list);
200 INIT_FLIST_HEAD(&client->cmd_list);
202 client->hostname = strdup(hostname);
204 if (type == Fio_client_socket)
209 ipv6 = type == Fio_client_ipv6;
210 if (fio_server_parse_host(hostname, &ipv6,
211 &client->addr.sin_addr,
212 &client->addr6.sin6_addr))
221 client->type = ops->client_type;
223 __fio_client_add_cmd_option(client, "fio");
225 flist_add(&client->list, &client_list);
227 dprint(FD_NET, "client: added <%s>\n", client->hostname);
234 void fio_client_add_ini_file(void *cookie, const char *ini_file)
236 struct fio_client *client = cookie;
239 dprint(FD_NET, "client <%s>: add ini %s\n", client->hostname, ini_file);
241 new_size = (client->nr_ini_file + 1) * sizeof(char *);
242 client->ini_file = realloc(client->ini_file, new_size);
243 client->ini_file[client->nr_ini_file] = strdup(ini_file);
244 client->nr_ini_file++;
247 int fio_client_add(struct client_ops *ops, const char *hostname, void **cookie)
249 struct fio_client *existing = *cookie;
250 struct fio_client *client;
254 * We always add our "exec" name as the option, hence 1
257 if (existing->argc == 1)
258 flist_add_tail(&existing->arg_list, &arg_list);
260 while (!flist_empty(&arg_list))
261 flist_del_init(arg_list.next);
265 client = malloc(sizeof(*client));
266 memset(client, 0, sizeof(*client));
268 INIT_FLIST_HEAD(&client->list);
269 INIT_FLIST_HEAD(&client->hash_list);
270 INIT_FLIST_HEAD(&client->arg_list);
271 INIT_FLIST_HEAD(&client->eta_list);
272 INIT_FLIST_HEAD(&client->cmd_list);
274 if (fio_server_parse_string(hostname, &client->hostname,
275 &client->is_sock, &client->port,
276 &client->addr.sin_addr,
277 &client->addr6.sin6_addr,
284 client->type = ops->client_type;
286 __fio_client_add_cmd_option(client, "fio");
288 flist_add(&client->list, &client_list);
290 dprint(FD_NET, "client: added <%s>\n", client->hostname);
295 static void probe_client(struct fio_client *client)
297 dprint(FD_NET, "client: send probe\n");
299 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_PROBE, 0, &client->cmd_list);
302 static int fio_client_connect_ip(struct fio_client *client)
304 struct sockaddr *addr;
305 fio_socklen_t socklen;
309 client->addr6.sin6_family = AF_INET6;
310 client->addr6.sin6_port = htons(client->port);
312 addr = (struct sockaddr *) &client->addr6;
313 socklen = sizeof(client->addr6);
315 client->addr.sin_family = AF_INET;
316 client->addr.sin_port = htons(client->port);
318 addr = (struct sockaddr *) &client->addr;
319 socklen = sizeof(client->addr);
322 fd = socket(domain, SOCK_STREAM, 0);
326 log_err("fio: socket: %s\n", strerror(errno));
330 if (connect(fd, addr, socklen) < 0) {
333 log_err("fio: connect: %s\n", strerror(errno));
334 log_err("fio: failed to connect to %s:%u\n", client->hostname,
343 static int fio_client_connect_sock(struct fio_client *client)
345 struct sockaddr_un *addr = &client->addr_un;
349 memset(addr, 0, sizeof(*addr));
350 addr->sun_family = AF_UNIX;
351 strcpy(addr->sun_path, client->hostname);
353 fd = socket(AF_UNIX, SOCK_STREAM, 0);
357 log_err("fio: socket: %s\n", strerror(errno));
361 len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
362 if (connect(fd, (struct sockaddr *) addr, len) < 0) {
365 log_err("fio: connect; %s\n", strerror(errno));
373 int fio_client_connect(struct fio_client *client)
377 dprint(FD_NET, "client: connect to host %s\n", client->hostname);
380 fd = fio_client_connect_sock(client);
382 fd = fio_client_connect_ip(client);
384 dprint(FD_NET, "client: %s connected %d\n", client->hostname, fd);
390 fio_client_add_hash(client);
391 client->state = Client_connected;
393 probe_client(client);
397 int fio_client_terminate(struct fio_client *client)
399 return fio_net_send_quit(client->fd);
402 void fio_clients_terminate(void)
404 struct flist_head *entry;
405 struct fio_client *client;
407 dprint(FD_NET, "client: terminate clients\n");
409 flist_for_each(entry, &client_list) {
410 client = flist_entry(entry, struct fio_client, list);
411 fio_client_terminate(client);
415 static void sig_int(int sig)
417 dprint(FD_NET, "client: got signal %d\n", sig);
418 fio_clients_terminate();
421 static void sig_show_status(int sig)
423 show_running_run_stats();
426 static void client_signal_handler(void)
428 struct sigaction act;
430 memset(&act, 0, sizeof(act));
431 act.sa_handler = sig_int;
432 act.sa_flags = SA_RESTART;
433 sigaction(SIGINT, &act, NULL);
435 memset(&act, 0, sizeof(act));
436 act.sa_handler = sig_int;
437 act.sa_flags = SA_RESTART;
438 sigaction(SIGTERM, &act, NULL);
440 /* Windows uses SIGBREAK as a quit signal from other applications */
442 memset(&act, 0, sizeof(act));
443 act.sa_handler = sig_int;
444 act.sa_flags = SA_RESTART;
445 sigaction(SIGBREAK, &act, NULL);
448 memset(&act, 0, sizeof(act));
449 act.sa_handler = sig_show_status;
450 act.sa_flags = SA_RESTART;
451 sigaction(SIGUSR1, &act, NULL);
454 static int send_client_cmd_line(struct fio_client *client)
456 struct cmd_single_line_pdu *cslp;
457 struct cmd_line_pdu *clp;
458 unsigned long offset;
464 dprint(FD_NET, "client: send cmdline %d\n", client->argc);
466 lens = malloc(client->argc * sizeof(unsigned int));
469 * Find out how much mem we need
471 for (i = 0, mem = 0; i < client->argc; i++) {
472 lens[i] = strlen(client->argv[i]) + 1;
477 * We need one cmd_line_pdu, and argc number of cmd_single_line_pdu
479 mem += sizeof(*clp) + (client->argc * sizeof(*cslp));
483 offset = sizeof(*clp);
485 for (i = 0; i < client->argc; i++) {
486 uint16_t arg_len = lens[i];
489 strcpy((char *) cslp->text, client->argv[i]);
490 cslp->len = cpu_to_le16(arg_len);
491 offset += sizeof(*cslp) + arg_len;
495 clp->lines = cpu_to_le16(client->argc);
496 clp->client_type = __cpu_to_le16(client->type);
497 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOBLINE, pdu, mem, NULL, NULL);
502 int fio_clients_connect(void)
504 struct fio_client *client;
505 struct flist_head *entry, *tmp;
510 WSAStartup(MAKEWORD(2, 2), &wsd);
513 dprint(FD_NET, "client: connect all\n");
515 client_signal_handler();
517 flist_for_each_safe(entry, tmp, &client_list) {
518 client = flist_entry(entry, struct fio_client, list);
520 ret = fio_client_connect(client);
522 remove_client(client);
526 if (client->argc > 1)
527 send_client_cmd_line(client);
533 int fio_start_client(struct fio_client *client)
535 dprint(FD_NET, "client: start %s\n", client->hostname);
536 return fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_RUN, 0, NULL);
539 int fio_start_all_clients(void)
541 struct fio_client *client;
542 struct flist_head *entry, *tmp;
545 dprint(FD_NET, "client: start all\n");
547 flist_for_each_safe(entry, tmp, &client_list) {
548 client = flist_entry(entry, struct fio_client, list);
550 ret = fio_start_client(client);
552 remove_client(client);
557 return flist_empty(&client_list);
561 * Send file contents to server backend. We could use sendfile(), but to remain
562 * more portable lets just read/write the darn thing.
564 static int __fio_client_send_ini(struct fio_client *client, const char *filename)
566 struct cmd_job_pdu *pdu;
574 dprint(FD_NET, "send ini %s to %s\n", filename, client->hostname);
576 fd = open(filename, O_RDONLY);
580 log_err("fio: job file <%s> open: %s\n", filename, strerror(errno));
584 if (fstat(fd, &sb) < 0) {
587 log_err("fio: job file stat: %s\n", strerror(errno));
592 p_size = sb.st_size + sizeof(*pdu);
593 pdu = malloc(p_size);
599 ret = read(fd, p, len);
608 else if (errno == EAGAIN || errno == EINTR)
613 log_err("fio: failed reading job file %s\n", filename);
619 pdu->buf_len = __cpu_to_le32(sb.st_size);
620 pdu->client_type = cpu_to_le32(client->type);
622 client->sent_job = 1;
623 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, pdu, p_size, NULL, NULL);
629 int fio_client_send_ini(struct fio_client *client, const char *filename)
633 ret = __fio_client_send_ini(client, filename);
635 client->sent_job = 1;
640 int fio_clients_send_ini(const char *filename)
642 struct fio_client *client;
643 struct flist_head *entry, *tmp;
645 flist_for_each_safe(entry, tmp, &client_list) {
646 client = flist_entry(entry, struct fio_client, list);
648 if (client->nr_ini_file) {
651 for (i = 0; i < client->nr_ini_file; i++) {
652 const char *ini = client->ini_file[i];
654 if (fio_client_send_ini(client, ini)) {
655 remove_client(client);
659 } else if (!filename || fio_client_send_ini(client, filename))
660 remove_client(client);
666 int fio_client_update_options(struct fio_client *client,
667 struct thread_options *o, uint64_t *tag)
669 struct cmd_add_job_pdu pdu;
671 pdu.thread_number = cpu_to_le32(client->thread_number);
672 pdu.groupid = cpu_to_le32(client->groupid);
673 convert_thread_options_to_net(&pdu.top, o);
675 return fio_net_send_cmd(client->fd, FIO_NET_CMD_UPDATE_JOB, &pdu, sizeof(pdu), tag, &client->cmd_list);
678 static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
680 dst->max_val = le64_to_cpu(src->max_val);
681 dst->min_val = le64_to_cpu(src->min_val);
682 dst->samples = le64_to_cpu(src->samples);
685 * Floats arrive as IEEE 754 encoded uint64_t, convert back to double
687 dst->mean.u.f = fio_uint64_to_double(le64_to_cpu(dst->mean.u.i));
688 dst->S.u.f = fio_uint64_to_double(le64_to_cpu(dst->S.u.i));
691 static void convert_ts(struct thread_stat *dst, struct thread_stat *src)
695 dst->error = le32_to_cpu(src->error);
696 dst->thread_number = le32_to_cpu(src->thread_number);
697 dst->groupid = le32_to_cpu(src->groupid);
698 dst->pid = le32_to_cpu(src->pid);
699 dst->members = le32_to_cpu(src->members);
701 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
702 convert_io_stat(&dst->clat_stat[i], &src->clat_stat[i]);
703 convert_io_stat(&dst->slat_stat[i], &src->slat_stat[i]);
704 convert_io_stat(&dst->lat_stat[i], &src->lat_stat[i]);
705 convert_io_stat(&dst->bw_stat[i], &src->bw_stat[i]);
708 dst->usr_time = le64_to_cpu(src->usr_time);
709 dst->sys_time = le64_to_cpu(src->sys_time);
710 dst->ctx = le64_to_cpu(src->ctx);
711 dst->minf = le64_to_cpu(src->minf);
712 dst->majf = le64_to_cpu(src->majf);
713 dst->clat_percentiles = le64_to_cpu(src->clat_percentiles);
715 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
716 fio_fp64_t *fps = &src->percentile_list[i];
717 fio_fp64_t *fpd = &dst->percentile_list[i];
719 fpd->u.f = fio_uint64_to_double(le64_to_cpu(fps->u.i));
722 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
723 dst->io_u_map[i] = le32_to_cpu(src->io_u_map[i]);
724 dst->io_u_submit[i] = le32_to_cpu(src->io_u_submit[i]);
725 dst->io_u_complete[i] = le32_to_cpu(src->io_u_complete[i]);
728 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
729 dst->io_u_lat_u[i] = le32_to_cpu(src->io_u_lat_u[i]);
730 dst->io_u_lat_m[i] = le32_to_cpu(src->io_u_lat_m[i]);
733 for (i = 0; i < DDIR_RWDIR_CNT; i++)
734 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
735 dst->io_u_plat[i][j] = le32_to_cpu(src->io_u_plat[i][j]);
737 for (i = 0; i < 3; i++) {
738 dst->total_io_u[i] = le64_to_cpu(src->total_io_u[i]);
739 dst->short_io_u[i] = le64_to_cpu(src->short_io_u[i]);
742 dst->total_submit = le64_to_cpu(src->total_submit);
743 dst->total_complete = le64_to_cpu(src->total_complete);
745 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
746 dst->io_bytes[i] = le64_to_cpu(src->io_bytes[i]);
747 dst->runtime[i] = le64_to_cpu(src->runtime[i]);
750 dst->total_run_time = le64_to_cpu(src->total_run_time);
751 dst->continue_on_error = le16_to_cpu(src->continue_on_error);
752 dst->total_err_count = le64_to_cpu(src->total_err_count);
753 dst->first_error = le32_to_cpu(src->first_error);
754 dst->kb_base = le32_to_cpu(src->kb_base);
757 static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
761 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
762 dst->max_run[i] = le64_to_cpu(src->max_run[i]);
763 dst->min_run[i] = le64_to_cpu(src->min_run[i]);
764 dst->max_bw[i] = le64_to_cpu(src->max_bw[i]);
765 dst->min_bw[i] = le64_to_cpu(src->min_bw[i]);
766 dst->io_kb[i] = le64_to_cpu(src->io_kb[i]);
767 dst->agg[i] = le64_to_cpu(src->agg[i]);
770 dst->kb_base = le32_to_cpu(src->kb_base);
771 dst->groupid = le32_to_cpu(src->groupid);
774 static void handle_ts(struct fio_client *client, struct fio_net_cmd *cmd)
776 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
778 show_thread_status(&p->ts, &p->rs);
780 if (sum_stat_clients == 1)
783 sum_thread_stats(&client_ts, &p->ts, sum_stat_nr);
784 sum_group_stats(&client_gs, &p->rs);
787 client_ts.thread_number = p->ts.thread_number;
788 client_ts.groupid = p->ts.groupid;
790 if (++sum_stat_nr == sum_stat_clients) {
791 strcpy(client_ts.name, "All clients");
792 show_thread_status(&client_ts, &client_gs);
796 static void handle_gs(struct fio_client *client, struct fio_net_cmd *cmd)
798 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
800 show_group_stats(gs);
803 static void handle_text(struct fio_client *client, struct fio_net_cmd *cmd)
805 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
806 const char *buf = (const char *) pdu->buf;
810 name = client->name ? client->name : client->hostname;
812 if (!client->skip_newline)
813 fprintf(f_out, "<%s> ", name);
814 ret = fwrite(buf, pdu->buf_len, 1, f_out);
816 client->skip_newline = strchr(buf, '\n') == NULL;
819 static void convert_agg(struct disk_util_agg *agg)
823 for (i = 0; i < 2; i++) {
824 agg->ios[i] = le32_to_cpu(agg->ios[i]);
825 agg->merges[i] = le32_to_cpu(agg->merges[i]);
826 agg->sectors[i] = le64_to_cpu(agg->sectors[i]);
827 agg->ticks[i] = le32_to_cpu(agg->ticks[i]);
830 agg->io_ticks = le32_to_cpu(agg->io_ticks);
831 agg->time_in_queue = le32_to_cpu(agg->time_in_queue);
832 agg->slavecount = le32_to_cpu(agg->slavecount);
833 agg->max_util.u.f = fio_uint64_to_double(__le64_to_cpu(agg->max_util.u.i));
836 static void convert_dus(struct disk_util_stat *dus)
840 for (i = 0; i < 2; i++) {
841 dus->ios[i] = le32_to_cpu(dus->ios[i]);
842 dus->merges[i] = le32_to_cpu(dus->merges[i]);
843 dus->sectors[i] = le64_to_cpu(dus->sectors[i]);
844 dus->ticks[i] = le32_to_cpu(dus->ticks[i]);
847 dus->io_ticks = le32_to_cpu(dus->io_ticks);
848 dus->time_in_queue = le32_to_cpu(dus->time_in_queue);
849 dus->msec = le64_to_cpu(dus->msec);
852 static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd)
854 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
856 if (!client->disk_stats_shown) {
857 client->disk_stats_shown = 1;
858 log_info("\nDisk stats (read/write):\n");
861 print_disk_util(&du->dus, &du->agg, output_format == FIO_OUTPUT_TERSE);
864 static void convert_jobs_eta(struct jobs_eta *je)
868 je->nr_running = le32_to_cpu(je->nr_running);
869 je->nr_ramp = le32_to_cpu(je->nr_ramp);
870 je->nr_pending = le32_to_cpu(je->nr_pending);
871 je->files_open = le32_to_cpu(je->files_open);
873 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
874 je->m_rate[i] = le32_to_cpu(je->m_rate[i]);
875 je->t_rate[i] = le32_to_cpu(je->t_rate[i]);
876 je->m_iops[i] = le32_to_cpu(je->m_iops[i]);
877 je->t_iops[i] = le32_to_cpu(je->t_iops[i]);
878 je->rate[i] = le32_to_cpu(je->rate[i]);
879 je->iops[i] = le32_to_cpu(je->iops[i]);
882 je->elapsed_sec = le64_to_cpu(je->elapsed_sec);
883 je->eta_sec = le64_to_cpu(je->eta_sec);
884 je->nr_threads = le32_to_cpu(je->nr_threads);
885 je->is_pow2 = le32_to_cpu(je->is_pow2);
888 void fio_client_sum_jobs_eta(struct jobs_eta *dst, struct jobs_eta *je)
892 dst->nr_running += je->nr_running;
893 dst->nr_ramp += je->nr_ramp;
894 dst->nr_pending += je->nr_pending;
895 dst->files_open += je->files_open;
897 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
898 dst->m_rate[i] += je->m_rate[i];
899 dst->t_rate[i] += je->t_rate[i];
900 dst->m_iops[i] += je->m_iops[i];
901 dst->t_iops[i] += je->t_iops[i];
902 dst->rate[i] += je->rate[i];
903 dst->iops[i] += je->iops[i];
906 dst->elapsed_sec += je->elapsed_sec;
908 if (je->eta_sec > dst->eta_sec)
909 dst->eta_sec = je->eta_sec;
911 dst->nr_threads += je->nr_threads;
912 /* we need to handle je->run_str too ... */
915 void fio_client_dec_jobs_eta(struct client_eta *eta, client_eta_op eta_fn)
917 if (!--eta->pending) {
923 static void remove_reply_cmd(struct fio_client *client, struct fio_net_cmd *cmd)
925 struct fio_net_cmd_reply *reply = NULL;
926 struct flist_head *entry;
928 flist_for_each(entry, &client->cmd_list) {
929 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
931 if (cmd->tag == (uintptr_t) reply)
938 log_err("fio: client: unable to find matching tag (%lx)\n", cmd->tag);
942 flist_del(&reply->list);
943 cmd->tag = reply->saved_tag;
947 int fio_client_wait_for_reply(struct fio_client *client, uint64_t tag)
950 struct fio_net_cmd_reply *reply = NULL;
951 struct flist_head *entry;
953 flist_for_each(entry, &client->cmd_list) {
954 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
956 if (tag == (uintptr_t) reply)
971 static void handle_eta(struct fio_client *client, struct fio_net_cmd *cmd)
973 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
974 struct client_eta *eta = (struct client_eta *) (uintptr_t) cmd->tag;
976 dprint(FD_NET, "client: got eta tag %p, %d\n", eta, eta->pending);
978 assert(client->eta_in_flight == eta);
980 client->eta_in_flight = NULL;
981 flist_del_init(&client->eta_list);
983 if (client->ops->jobs_eta)
984 client->ops->jobs_eta(client, je);
986 fio_client_sum_jobs_eta(&eta->eta, je);
987 fio_client_dec_jobs_eta(eta, client->ops->eta);
990 static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd)
992 struct cmd_probe_pdu *probe = (struct cmd_probe_pdu *) cmd->payload;
993 const char *os, *arch;
996 os = fio_get_os_string(probe->os);
1000 arch = fio_get_arch_string(probe->arch);
1004 sprintf(bit, "%d-bit", probe->bpp * 8);
1006 log_info("hostname=%s, be=%u, %s, os=%s, arch=%s, fio=%s\n",
1007 probe->hostname, probe->bigendian, bit, os, arch,
1008 probe->fio_version);
1011 client->name = strdup((char *) probe->hostname);
1014 static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd)
1016 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
1018 client->state = Client_started;
1019 client->jobs = pdu->jobs;
1022 static void handle_stop(struct fio_client *client, struct fio_net_cmd *cmd)
1025 log_info("client <%s>: exited with error %d\n", client->hostname, client->error);
1028 static void convert_stop(struct fio_net_cmd *cmd)
1030 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
1032 pdu->error = le32_to_cpu(pdu->error);
1035 static void convert_text(struct fio_net_cmd *cmd)
1037 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
1039 pdu->level = le32_to_cpu(pdu->level);
1040 pdu->buf_len = le32_to_cpu(pdu->buf_len);
1041 pdu->log_sec = le64_to_cpu(pdu->log_sec);
1042 pdu->log_usec = le64_to_cpu(pdu->log_usec);
1046 * This has been compressed on the server side, since it can be big.
1049 static struct cmd_iolog_pdu *convert_iolog(struct fio_net_cmd *cmd)
1051 struct cmd_iolog_pdu *pdu = (struct cmd_iolog_pdu *) cmd->payload;
1052 struct cmd_iolog_pdu *ret;
1053 uint32_t nr_samples;
1054 unsigned long total;
1059 stream.zalloc = Z_NULL;
1060 stream.zfree = Z_NULL;
1061 stream.opaque = Z_NULL;
1062 stream.avail_in = 0;
1063 stream.next_in = Z_NULL;
1065 if (inflateInit(&stream) != Z_OK)
1069 * Get header first, it's not compressed
1071 nr_samples = le32_to_cpu(pdu->nr_samples);
1073 total = nr_samples * sizeof(struct io_sample);
1074 ret = malloc(total + sizeof(*pdu));
1075 ret->thread_number = le32_to_cpu(pdu->thread_number);
1076 ret->nr_samples = nr_samples;
1077 ret->log_type = le32_to_cpu(pdu->log_type);
1078 strcpy((char *) ret->name, (char *) pdu->name);
1080 p = (void *) ret + sizeof(*pdu);
1082 stream.avail_in = cmd->pdu_len - sizeof(*pdu);
1083 stream.next_in = (void *) pdu + sizeof(*pdu);
1084 while (stream.avail_in) {
1085 unsigned int this_chunk = 65536;
1086 unsigned int this_len;
1089 if (this_chunk > total)
1092 stream.avail_out = this_chunk;
1093 stream.next_out = p;
1094 err = inflate(&stream, Z_NO_FLUSH);
1095 /* may be Z_OK, or Z_STREAM_END */
1097 log_err("fio: inflate error %d\n", err);
1103 this_len = this_chunk - stream.avail_out;
1108 for (i = 0; i < ret->nr_samples; i++) {
1109 struct io_sample *s = &ret->samples[i];
1111 s->time = le64_to_cpu(s->time);
1112 s->val = le64_to_cpu(s->val);
1113 s->ddir = le32_to_cpu(s->ddir);
1114 s->bs = le32_to_cpu(s->bs);
1118 inflateEnd(&stream);
1122 int fio_handle_client(struct fio_client *client)
1124 struct client_ops *ops = client->ops;
1125 struct fio_net_cmd *cmd;
1127 dprint(FD_NET, "client: handle %s\n", client->hostname);
1129 cmd = fio_net_recv_cmd(client->fd);
1133 dprint(FD_NET, "client: got cmd op %s from %s (pdu=%u)\n",
1134 fio_server_op(cmd->opcode), client->hostname, cmd->pdu_len);
1136 switch (cmd->opcode) {
1137 case FIO_NET_CMD_QUIT:
1139 ops->quit(client, cmd);
1140 remove_client(client);
1143 case FIO_NET_CMD_TEXT:
1145 ops->text(client, cmd);
1148 case FIO_NET_CMD_DU: {
1149 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
1151 convert_dus(&du->dus);
1152 convert_agg(&du->agg);
1154 ops->disk_util(client, cmd);
1158 case FIO_NET_CMD_TS: {
1159 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
1161 convert_ts(&p->ts, &p->ts);
1162 convert_gs(&p->rs, &p->rs);
1164 ops->thread_status(client, cmd);
1168 case FIO_NET_CMD_GS: {
1169 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
1173 ops->group_stats(client, cmd);
1177 case FIO_NET_CMD_ETA: {
1178 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
1180 remove_reply_cmd(client, cmd);
1181 convert_jobs_eta(je);
1182 handle_eta(client, cmd);
1186 case FIO_NET_CMD_PROBE:
1187 remove_reply_cmd(client, cmd);
1188 ops->probe(client, cmd);
1191 case FIO_NET_CMD_SERVER_START:
1192 client->state = Client_running;
1194 ops->job_start(client, cmd);
1197 case FIO_NET_CMD_START: {
1198 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
1200 pdu->jobs = le32_to_cpu(pdu->jobs);
1201 ops->start(client, cmd);
1205 case FIO_NET_CMD_STOP: {
1206 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
1209 client->state = Client_stopped;
1210 client->error = le32_to_cpu(pdu->error);
1211 client->signal = le32_to_cpu(pdu->signal);
1212 ops->stop(client, cmd);
1216 case FIO_NET_CMD_ADD_JOB: {
1217 struct cmd_add_job_pdu *pdu = (struct cmd_add_job_pdu *) cmd->payload;
1219 client->thread_number = le32_to_cpu(pdu->thread_number);
1220 client->groupid = le32_to_cpu(pdu->groupid);
1223 ops->add_job(client, cmd);
1227 case FIO_NET_CMD_IOLOG:
1229 struct cmd_iolog_pdu *pdu;
1231 pdu = convert_iolog(cmd);
1232 ops->iolog(client, pdu);
1236 case FIO_NET_CMD_UPDATE_JOB:
1237 ops->update_job(client, cmd);
1238 remove_reply_cmd(client, cmd);
1242 log_err("fio: unknown client op: %s\n", fio_server_op(cmd->opcode));
1250 static void request_client_etas(struct client_ops *ops)
1252 struct fio_client *client;
1253 struct flist_head *entry;
1254 struct client_eta *eta;
1257 dprint(FD_NET, "client: request eta (%d)\n", nr_clients);
1259 eta = malloc(sizeof(*eta));
1260 memset(&eta->eta, 0, sizeof(eta->eta));
1261 eta->pending = nr_clients;
1263 flist_for_each(entry, &client_list) {
1264 client = flist_entry(entry, struct fio_client, list);
1266 if (!flist_empty(&client->eta_list)) {
1270 if (client->state != Client_running)
1273 assert(!client->eta_in_flight);
1274 flist_add_tail(&client->eta_list, &eta_list);
1275 client->eta_in_flight = eta;
1276 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_SEND_ETA,
1277 (uintptr_t) eta, &client->cmd_list);
1281 fio_client_dec_jobs_eta(eta, ops->eta);
1283 dprint(FD_NET, "client: requested eta tag %p\n", eta);
1286 static int client_check_cmd_timeout(struct fio_client *client,
1287 struct timeval *now)
1289 struct fio_net_cmd_reply *reply;
1290 struct flist_head *entry, *tmp;
1293 flist_for_each_safe(entry, tmp, &client->cmd_list) {
1294 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
1296 if (mtime_since(&reply->tv, now) < FIO_NET_CLIENT_TIMEOUT)
1299 log_err("fio: client %s, timeout on cmd %s\n", client->hostname,
1300 fio_server_op(reply->opcode));
1301 flist_del(&reply->list);
1306 return flist_empty(&client->cmd_list) && ret;
1309 static int fio_check_clients_timed_out(void)
1311 struct fio_client *client;
1312 struct flist_head *entry, *tmp;
1316 gettimeofday(&tv, NULL);
1318 flist_for_each_safe(entry, tmp, &client_list) {
1319 client = flist_entry(entry, struct fio_client, list);
1321 if (flist_empty(&client->cmd_list))
1324 if (!client_check_cmd_timeout(client, &tv))
1327 if (client->ops->timed_out)
1328 client->ops->timed_out(client);
1330 log_err("fio: client %s timed out\n", client->hostname);
1332 remove_client(client);
1339 int fio_handle_clients(struct client_ops *ops)
1341 struct pollfd *pfds;
1342 int i, ret = 0, retval = 0;
1344 gettimeofday(&eta_tv, NULL);
1346 pfds = malloc(nr_clients * sizeof(struct pollfd));
1348 sum_stat_clients = nr_clients;
1349 init_thread_stat(&client_ts);
1350 init_group_run_stat(&client_gs);
1352 while (!exit_backend && nr_clients) {
1353 struct flist_head *entry, *tmp;
1354 struct fio_client *client;
1357 flist_for_each_safe(entry, tmp, &client_list) {
1358 client = flist_entry(entry, struct fio_client, list);
1360 if (!client->sent_job && !client->ops->stay_connected &&
1361 flist_empty(&client->cmd_list)) {
1362 remove_client(client);
1366 pfds[i].fd = client->fd;
1367 pfds[i].events = POLLIN;
1374 assert(i == nr_clients);
1379 gettimeofday(&tv, NULL);
1380 if (mtime_since(&eta_tv, &tv) >= ops->eta_msec) {
1381 request_client_etas(ops);
1382 memcpy(&eta_tv, &tv, sizeof(tv));
1384 if (fio_check_clients_timed_out())
1388 ret = poll(pfds, nr_clients, ops->eta_msec);
1392 log_err("fio: poll clients: %s\n", strerror(errno));
1398 for (i = 0; i < nr_clients; i++) {
1399 if (!(pfds[i].revents & POLLIN))
1402 client = find_client_by_fd(pfds[i].fd);
1404 log_err("fio: unknown client fd %d\n", pfds[i].fd);
1407 if (!fio_handle_client(client)) {
1408 log_info("client: host=%s disconnected\n",
1410 remove_client(client);
1412 } else if (client->error)
1414 fio_put_client(client);