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 memset(&act, 0, sizeof(act));
441 act.sa_handler = sig_show_status;
442 act.sa_flags = SA_RESTART;
443 sigaction(SIGUSR1, &act, NULL);
446 static int send_client_cmd_line(struct fio_client *client)
448 struct cmd_single_line_pdu *cslp;
449 struct cmd_line_pdu *clp;
450 unsigned long offset;
456 dprint(FD_NET, "client: send cmdline %d\n", client->argc);
458 lens = malloc(client->argc * sizeof(unsigned int));
461 * Find out how much mem we need
463 for (i = 0, mem = 0; i < client->argc; i++) {
464 lens[i] = strlen(client->argv[i]) + 1;
469 * We need one cmd_line_pdu, and argc number of cmd_single_line_pdu
471 mem += sizeof(*clp) + (client->argc * sizeof(*cslp));
475 offset = sizeof(*clp);
477 for (i = 0; i < client->argc; i++) {
478 uint16_t arg_len = lens[i];
481 strcpy((char *) cslp->text, client->argv[i]);
482 cslp->len = cpu_to_le16(arg_len);
483 offset += sizeof(*cslp) + arg_len;
487 clp->lines = cpu_to_le16(client->argc);
488 clp->client_type = __cpu_to_le16(client->type);
489 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOBLINE, pdu, mem, NULL, NULL);
494 int fio_clients_connect(void)
496 struct fio_client *client;
497 struct flist_head *entry, *tmp;
502 WSAStartup(MAKEWORD(2, 2), &wsd);
505 dprint(FD_NET, "client: connect all\n");
507 client_signal_handler();
509 flist_for_each_safe(entry, tmp, &client_list) {
510 client = flist_entry(entry, struct fio_client, list);
512 ret = fio_client_connect(client);
514 remove_client(client);
518 if (client->argc > 1)
519 send_client_cmd_line(client);
525 int fio_start_client(struct fio_client *client)
527 dprint(FD_NET, "client: start %s\n", client->hostname);
528 return fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_RUN, 0, NULL);
531 int fio_start_all_clients(void)
533 struct fio_client *client;
534 struct flist_head *entry, *tmp;
537 dprint(FD_NET, "client: start all\n");
539 flist_for_each_safe(entry, tmp, &client_list) {
540 client = flist_entry(entry, struct fio_client, list);
542 ret = fio_start_client(client);
544 remove_client(client);
549 return flist_empty(&client_list);
553 * Send file contents to server backend. We could use sendfile(), but to remain
554 * more portable lets just read/write the darn thing.
556 static int __fio_client_send_ini(struct fio_client *client, const char *filename)
558 struct cmd_job_pdu *pdu;
566 dprint(FD_NET, "send ini %s to %s\n", filename, client->hostname);
568 fd = open(filename, O_RDONLY);
572 log_err("fio: job file <%s> open: %s\n", filename, strerror(errno));
576 if (fstat(fd, &sb) < 0) {
579 log_err("fio: job file stat: %s\n", strerror(errno));
584 p_size = sb.st_size + sizeof(*pdu);
585 pdu = malloc(p_size);
591 ret = read(fd, p, len);
600 else if (errno == EAGAIN || errno == EINTR)
605 log_err("fio: failed reading job file %s\n", filename);
611 pdu->buf_len = __cpu_to_le32(sb.st_size);
612 pdu->client_type = cpu_to_le32(client->type);
614 client->sent_job = 1;
615 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, pdu, p_size, NULL, NULL);
621 int fio_client_send_ini(struct fio_client *client, const char *filename)
625 ret = __fio_client_send_ini(client, filename);
627 client->sent_job = 1;
632 int fio_clients_send_ini(const char *filename)
634 struct fio_client *client;
635 struct flist_head *entry, *tmp;
637 flist_for_each_safe(entry, tmp, &client_list) {
638 client = flist_entry(entry, struct fio_client, list);
640 if (client->nr_ini_file) {
643 for (i = 0; i < client->nr_ini_file; i++) {
644 const char *ini = client->ini_file[i];
646 if (fio_client_send_ini(client, ini)) {
647 remove_client(client);
651 } else if (!filename || fio_client_send_ini(client, filename))
652 remove_client(client);
658 int fio_client_update_options(struct fio_client *client,
659 struct thread_options *o, uint64_t *tag)
661 struct cmd_add_job_pdu pdu;
663 pdu.thread_number = cpu_to_le32(client->thread_number);
664 pdu.groupid = cpu_to_le32(client->groupid);
665 convert_thread_options_to_net(&pdu.top, o);
667 return fio_net_send_cmd(client->fd, FIO_NET_CMD_UPDATE_JOB, &pdu, sizeof(pdu), tag, &client->cmd_list);
670 static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
672 dst->max_val = le64_to_cpu(src->max_val);
673 dst->min_val = le64_to_cpu(src->min_val);
674 dst->samples = le64_to_cpu(src->samples);
677 * Floats arrive as IEEE 754 encoded uint64_t, convert back to double
679 dst->mean.u.f = fio_uint64_to_double(le64_to_cpu(dst->mean.u.i));
680 dst->S.u.f = fio_uint64_to_double(le64_to_cpu(dst->S.u.i));
683 static void convert_ts(struct thread_stat *dst, struct thread_stat *src)
687 dst->error = le32_to_cpu(src->error);
688 dst->thread_number = le32_to_cpu(src->thread_number);
689 dst->groupid = le32_to_cpu(src->groupid);
690 dst->pid = le32_to_cpu(src->pid);
691 dst->members = le32_to_cpu(src->members);
693 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
694 convert_io_stat(&dst->clat_stat[i], &src->clat_stat[i]);
695 convert_io_stat(&dst->slat_stat[i], &src->slat_stat[i]);
696 convert_io_stat(&dst->lat_stat[i], &src->lat_stat[i]);
697 convert_io_stat(&dst->bw_stat[i], &src->bw_stat[i]);
700 dst->usr_time = le64_to_cpu(src->usr_time);
701 dst->sys_time = le64_to_cpu(src->sys_time);
702 dst->ctx = le64_to_cpu(src->ctx);
703 dst->minf = le64_to_cpu(src->minf);
704 dst->majf = le64_to_cpu(src->majf);
705 dst->clat_percentiles = le64_to_cpu(src->clat_percentiles);
707 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
708 fio_fp64_t *fps = &src->percentile_list[i];
709 fio_fp64_t *fpd = &dst->percentile_list[i];
711 fpd->u.f = fio_uint64_to_double(le64_to_cpu(fps->u.i));
714 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
715 dst->io_u_map[i] = le32_to_cpu(src->io_u_map[i]);
716 dst->io_u_submit[i] = le32_to_cpu(src->io_u_submit[i]);
717 dst->io_u_complete[i] = le32_to_cpu(src->io_u_complete[i]);
720 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
721 dst->io_u_lat_u[i] = le32_to_cpu(src->io_u_lat_u[i]);
722 dst->io_u_lat_m[i] = le32_to_cpu(src->io_u_lat_m[i]);
725 for (i = 0; i < DDIR_RWDIR_CNT; i++)
726 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
727 dst->io_u_plat[i][j] = le32_to_cpu(src->io_u_plat[i][j]);
729 for (i = 0; i < 3; i++) {
730 dst->total_io_u[i] = le64_to_cpu(src->total_io_u[i]);
731 dst->short_io_u[i] = le64_to_cpu(src->short_io_u[i]);
734 dst->total_submit = le64_to_cpu(src->total_submit);
735 dst->total_complete = le64_to_cpu(src->total_complete);
737 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
738 dst->io_bytes[i] = le64_to_cpu(src->io_bytes[i]);
739 dst->runtime[i] = le64_to_cpu(src->runtime[i]);
742 dst->total_run_time = le64_to_cpu(src->total_run_time);
743 dst->continue_on_error = le16_to_cpu(src->continue_on_error);
744 dst->total_err_count = le64_to_cpu(src->total_err_count);
745 dst->first_error = le32_to_cpu(src->first_error);
746 dst->kb_base = le32_to_cpu(src->kb_base);
749 static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
753 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
754 dst->max_run[i] = le64_to_cpu(src->max_run[i]);
755 dst->min_run[i] = le64_to_cpu(src->min_run[i]);
756 dst->max_bw[i] = le64_to_cpu(src->max_bw[i]);
757 dst->min_bw[i] = le64_to_cpu(src->min_bw[i]);
758 dst->io_kb[i] = le64_to_cpu(src->io_kb[i]);
759 dst->agg[i] = le64_to_cpu(src->agg[i]);
762 dst->kb_base = le32_to_cpu(src->kb_base);
763 dst->groupid = le32_to_cpu(src->groupid);
766 static void handle_ts(struct fio_client *client, struct fio_net_cmd *cmd)
768 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
770 show_thread_status(&p->ts, &p->rs);
772 if (sum_stat_clients == 1)
775 sum_thread_stats(&client_ts, &p->ts, sum_stat_nr);
776 sum_group_stats(&client_gs, &p->rs);
779 client_ts.thread_number = p->ts.thread_number;
780 client_ts.groupid = p->ts.groupid;
782 if (++sum_stat_nr == sum_stat_clients) {
783 strcpy(client_ts.name, "All clients");
784 show_thread_status(&client_ts, &client_gs);
788 static void handle_gs(struct fio_client *client, struct fio_net_cmd *cmd)
790 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
792 show_group_stats(gs);
795 static void handle_text(struct fio_client *client, struct fio_net_cmd *cmd)
797 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
798 const char *buf = (const char *) pdu->buf;
802 name = client->name ? client->name : client->hostname;
804 if (!client->skip_newline)
805 fprintf(f_out, "<%s> ", name);
806 ret = fwrite(buf, pdu->buf_len, 1, f_out);
808 client->skip_newline = strchr(buf, '\n') == NULL;
811 static void convert_agg(struct disk_util_agg *agg)
815 for (i = 0; i < 2; i++) {
816 agg->ios[i] = le32_to_cpu(agg->ios[i]);
817 agg->merges[i] = le32_to_cpu(agg->merges[i]);
818 agg->sectors[i] = le64_to_cpu(agg->sectors[i]);
819 agg->ticks[i] = le32_to_cpu(agg->ticks[i]);
822 agg->io_ticks = le32_to_cpu(agg->io_ticks);
823 agg->time_in_queue = le32_to_cpu(agg->time_in_queue);
824 agg->slavecount = le32_to_cpu(agg->slavecount);
825 agg->max_util.u.f = fio_uint64_to_double(__le64_to_cpu(agg->max_util.u.i));
828 static void convert_dus(struct disk_util_stat *dus)
832 for (i = 0; i < 2; i++) {
833 dus->ios[i] = le32_to_cpu(dus->ios[i]);
834 dus->merges[i] = le32_to_cpu(dus->merges[i]);
835 dus->sectors[i] = le64_to_cpu(dus->sectors[i]);
836 dus->ticks[i] = le32_to_cpu(dus->ticks[i]);
839 dus->io_ticks = le32_to_cpu(dus->io_ticks);
840 dus->time_in_queue = le32_to_cpu(dus->time_in_queue);
841 dus->msec = le64_to_cpu(dus->msec);
844 static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd)
846 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
848 if (!client->disk_stats_shown) {
849 client->disk_stats_shown = 1;
850 log_info("\nDisk stats (read/write):\n");
853 print_disk_util(&du->dus, &du->agg, output_format == FIO_OUTPUT_TERSE);
856 static void convert_jobs_eta(struct jobs_eta *je)
860 je->nr_running = le32_to_cpu(je->nr_running);
861 je->nr_ramp = le32_to_cpu(je->nr_ramp);
862 je->nr_pending = le32_to_cpu(je->nr_pending);
863 je->files_open = le32_to_cpu(je->files_open);
865 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
866 je->m_rate[i] = le32_to_cpu(je->m_rate[i]);
867 je->t_rate[i] = le32_to_cpu(je->t_rate[i]);
868 je->m_iops[i] = le32_to_cpu(je->m_iops[i]);
869 je->t_iops[i] = le32_to_cpu(je->t_iops[i]);
870 je->rate[i] = le32_to_cpu(je->rate[i]);
871 je->iops[i] = le32_to_cpu(je->iops[i]);
874 je->elapsed_sec = le64_to_cpu(je->elapsed_sec);
875 je->eta_sec = le64_to_cpu(je->eta_sec);
876 je->nr_threads = le32_to_cpu(je->nr_threads);
877 je->is_pow2 = le32_to_cpu(je->is_pow2);
880 void fio_client_sum_jobs_eta(struct jobs_eta *dst, struct jobs_eta *je)
884 dst->nr_running += je->nr_running;
885 dst->nr_ramp += je->nr_ramp;
886 dst->nr_pending += je->nr_pending;
887 dst->files_open += je->files_open;
889 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
890 dst->m_rate[i] += je->m_rate[i];
891 dst->t_rate[i] += je->t_rate[i];
892 dst->m_iops[i] += je->m_iops[i];
893 dst->t_iops[i] += je->t_iops[i];
894 dst->rate[i] += je->rate[i];
895 dst->iops[i] += je->iops[i];
898 dst->elapsed_sec += je->elapsed_sec;
900 if (je->eta_sec > dst->eta_sec)
901 dst->eta_sec = je->eta_sec;
903 dst->nr_threads += je->nr_threads;
904 /* we need to handle je->run_str too ... */
907 void fio_client_dec_jobs_eta(struct client_eta *eta, client_eta_op eta_fn)
909 if (!--eta->pending) {
915 static void remove_reply_cmd(struct fio_client *client, struct fio_net_cmd *cmd)
917 struct fio_net_cmd_reply *reply = NULL;
918 struct flist_head *entry;
920 flist_for_each(entry, &client->cmd_list) {
921 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
923 if (cmd->tag == (uintptr_t) reply)
930 log_err("fio: client: unable to find matching tag (%lx)\n", cmd->tag);
934 flist_del(&reply->list);
935 cmd->tag = reply->saved_tag;
939 int fio_client_wait_for_reply(struct fio_client *client, uint64_t tag)
942 struct fio_net_cmd_reply *reply = NULL;
943 struct flist_head *entry;
945 flist_for_each(entry, &client->cmd_list) {
946 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
948 if (tag == (uintptr_t) reply)
963 static void handle_eta(struct fio_client *client, struct fio_net_cmd *cmd)
965 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
966 struct client_eta *eta = (struct client_eta *) (uintptr_t) cmd->tag;
968 dprint(FD_NET, "client: got eta tag %p, %d\n", eta, eta->pending);
970 assert(client->eta_in_flight == eta);
972 client->eta_in_flight = NULL;
973 flist_del_init(&client->eta_list);
975 if (client->ops->jobs_eta)
976 client->ops->jobs_eta(client, je);
978 fio_client_sum_jobs_eta(&eta->eta, je);
979 fio_client_dec_jobs_eta(eta, client->ops->eta);
982 static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd)
984 struct cmd_probe_pdu *probe = (struct cmd_probe_pdu *) cmd->payload;
985 const char *os, *arch;
988 os = fio_get_os_string(probe->os);
992 arch = fio_get_arch_string(probe->arch);
996 sprintf(bit, "%d-bit", probe->bpp * 8);
998 log_info("hostname=%s, be=%u, %s, os=%s, arch=%s, fio=%s\n",
999 probe->hostname, probe->bigendian, bit, os, arch,
1000 probe->fio_version);
1003 client->name = strdup((char *) probe->hostname);
1006 static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd)
1008 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
1010 client->state = Client_started;
1011 client->jobs = pdu->jobs;
1014 static void handle_stop(struct fio_client *client, struct fio_net_cmd *cmd)
1017 log_info("client <%s>: exited with error %d\n", client->hostname, client->error);
1020 static void convert_stop(struct fio_net_cmd *cmd)
1022 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
1024 pdu->error = le32_to_cpu(pdu->error);
1027 static void convert_text(struct fio_net_cmd *cmd)
1029 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
1031 pdu->level = le32_to_cpu(pdu->level);
1032 pdu->buf_len = le32_to_cpu(pdu->buf_len);
1033 pdu->log_sec = le64_to_cpu(pdu->log_sec);
1034 pdu->log_usec = le64_to_cpu(pdu->log_usec);
1038 * This has been compressed on the server side, since it can be big.
1041 static struct cmd_iolog_pdu *convert_iolog(struct fio_net_cmd *cmd)
1043 struct cmd_iolog_pdu *pdu = (struct cmd_iolog_pdu *) cmd->payload;
1044 struct cmd_iolog_pdu *ret;
1045 uint32_t nr_samples;
1046 unsigned long total;
1051 stream.zalloc = Z_NULL;
1052 stream.zfree = Z_NULL;
1053 stream.opaque = Z_NULL;
1054 stream.avail_in = 0;
1055 stream.next_in = Z_NULL;
1057 if (inflateInit(&stream) != Z_OK)
1061 * Get header first, it's not compressed
1063 nr_samples = le32_to_cpu(pdu->nr_samples);
1065 total = nr_samples * sizeof(struct io_sample);
1066 ret = malloc(total + sizeof(*pdu));
1067 ret->thread_number = le32_to_cpu(pdu->thread_number);
1068 ret->nr_samples = nr_samples;
1069 ret->log_type = le32_to_cpu(pdu->log_type);
1070 strcpy((char *) ret->name, (char *) pdu->name);
1072 p = (void *) ret + sizeof(*pdu);
1074 stream.avail_in = cmd->pdu_len - sizeof(*pdu);
1075 stream.next_in = (void *) pdu + sizeof(*pdu);
1076 while (stream.avail_in) {
1077 unsigned int this_chunk = 65536;
1078 unsigned int this_len;
1081 if (this_chunk > total)
1084 stream.avail_out = this_chunk;
1085 stream.next_out = p;
1086 err = inflate(&stream, Z_NO_FLUSH);
1087 /* may be Z_OK, or Z_STREAM_END */
1089 log_err("fio: inflate error %d\n", err);
1095 this_len = this_chunk - stream.avail_out;
1100 for (i = 0; i < ret->nr_samples; i++) {
1101 struct io_sample *s = &ret->samples[i];
1103 s->time = le64_to_cpu(s->time);
1104 s->val = le64_to_cpu(s->val);
1105 s->ddir = le32_to_cpu(s->ddir);
1106 s->bs = le32_to_cpu(s->bs);
1110 inflateEnd(&stream);
1114 int fio_handle_client(struct fio_client *client)
1116 struct client_ops *ops = client->ops;
1117 struct fio_net_cmd *cmd;
1119 dprint(FD_NET, "client: handle %s\n", client->hostname);
1121 cmd = fio_net_recv_cmd(client->fd);
1125 dprint(FD_NET, "client: got cmd op %s from %s (pdu=%u)\n",
1126 fio_server_op(cmd->opcode), client->hostname, cmd->pdu_len);
1128 switch (cmd->opcode) {
1129 case FIO_NET_CMD_QUIT:
1131 ops->quit(client, cmd);
1132 remove_client(client);
1135 case FIO_NET_CMD_TEXT:
1137 ops->text(client, cmd);
1140 case FIO_NET_CMD_DU: {
1141 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
1143 convert_dus(&du->dus);
1144 convert_agg(&du->agg);
1146 ops->disk_util(client, cmd);
1150 case FIO_NET_CMD_TS: {
1151 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
1153 convert_ts(&p->ts, &p->ts);
1154 convert_gs(&p->rs, &p->rs);
1156 ops->thread_status(client, cmd);
1160 case FIO_NET_CMD_GS: {
1161 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
1165 ops->group_stats(client, cmd);
1169 case FIO_NET_CMD_ETA: {
1170 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
1172 remove_reply_cmd(client, cmd);
1173 convert_jobs_eta(je);
1174 handle_eta(client, cmd);
1178 case FIO_NET_CMD_PROBE:
1179 remove_reply_cmd(client, cmd);
1180 ops->probe(client, cmd);
1183 case FIO_NET_CMD_SERVER_START:
1184 client->state = Client_running;
1186 ops->job_start(client, cmd);
1189 case FIO_NET_CMD_START: {
1190 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
1192 pdu->jobs = le32_to_cpu(pdu->jobs);
1193 ops->start(client, cmd);
1197 case FIO_NET_CMD_STOP: {
1198 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
1201 client->state = Client_stopped;
1202 client->error = le32_to_cpu(pdu->error);
1203 client->signal = le32_to_cpu(pdu->signal);
1204 ops->stop(client, cmd);
1208 case FIO_NET_CMD_ADD_JOB: {
1209 struct cmd_add_job_pdu *pdu = (struct cmd_add_job_pdu *) cmd->payload;
1211 client->thread_number = le32_to_cpu(pdu->thread_number);
1212 client->groupid = le32_to_cpu(pdu->groupid);
1215 ops->add_job(client, cmd);
1219 case FIO_NET_CMD_IOLOG:
1221 struct cmd_iolog_pdu *pdu;
1223 pdu = convert_iolog(cmd);
1224 ops->iolog(client, pdu);
1228 case FIO_NET_CMD_UPDATE_JOB:
1229 ops->update_job(client, cmd);
1230 remove_reply_cmd(client, cmd);
1234 log_err("fio: unknown client op: %s\n", fio_server_op(cmd->opcode));
1242 static void request_client_etas(struct client_ops *ops)
1244 struct fio_client *client;
1245 struct flist_head *entry;
1246 struct client_eta *eta;
1249 dprint(FD_NET, "client: request eta (%d)\n", nr_clients);
1251 eta = malloc(sizeof(*eta));
1252 memset(&eta->eta, 0, sizeof(eta->eta));
1253 eta->pending = nr_clients;
1255 flist_for_each(entry, &client_list) {
1256 client = flist_entry(entry, struct fio_client, list);
1258 if (!flist_empty(&client->eta_list)) {
1262 if (client->state != Client_running)
1265 assert(!client->eta_in_flight);
1266 flist_add_tail(&client->eta_list, &eta_list);
1267 client->eta_in_flight = eta;
1268 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_SEND_ETA,
1269 (uintptr_t) eta, &client->cmd_list);
1273 fio_client_dec_jobs_eta(eta, ops->eta);
1275 dprint(FD_NET, "client: requested eta tag %p\n", eta);
1278 static int client_check_cmd_timeout(struct fio_client *client,
1279 struct timeval *now)
1281 struct fio_net_cmd_reply *reply;
1282 struct flist_head *entry, *tmp;
1285 flist_for_each_safe(entry, tmp, &client->cmd_list) {
1286 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
1288 if (mtime_since(&reply->tv, now) < FIO_NET_CLIENT_TIMEOUT)
1291 log_err("fio: client %s, timeout on cmd %s\n", client->hostname,
1292 fio_server_op(reply->opcode));
1293 flist_del(&reply->list);
1298 return flist_empty(&client->cmd_list) && ret;
1301 static int fio_check_clients_timed_out(void)
1303 struct fio_client *client;
1304 struct flist_head *entry, *tmp;
1308 gettimeofday(&tv, NULL);
1310 flist_for_each_safe(entry, tmp, &client_list) {
1311 client = flist_entry(entry, struct fio_client, list);
1313 if (flist_empty(&client->cmd_list))
1316 if (!client_check_cmd_timeout(client, &tv))
1319 if (client->ops->timed_out)
1320 client->ops->timed_out(client);
1322 log_err("fio: client %s timed out\n", client->hostname);
1324 remove_client(client);
1331 int fio_handle_clients(struct client_ops *ops)
1333 struct pollfd *pfds;
1334 int i, ret = 0, retval = 0;
1336 gettimeofday(&eta_tv, NULL);
1338 pfds = malloc(nr_clients * sizeof(struct pollfd));
1340 sum_stat_clients = nr_clients;
1341 init_thread_stat(&client_ts);
1342 init_group_run_stat(&client_gs);
1344 while (!exit_backend && nr_clients) {
1345 struct flist_head *entry, *tmp;
1346 struct fio_client *client;
1349 flist_for_each_safe(entry, tmp, &client_list) {
1350 client = flist_entry(entry, struct fio_client, list);
1352 if (!client->sent_job && !client->ops->stay_connected &&
1353 flist_empty(&client->cmd_list)) {
1354 remove_client(client);
1358 pfds[i].fd = client->fd;
1359 pfds[i].events = POLLIN;
1366 assert(i == nr_clients);
1371 gettimeofday(&tv, NULL);
1372 if (mtime_since(&eta_tv, &tv) >= ops->eta_msec) {
1373 request_client_etas(ops);
1374 memcpy(&eta_tv, &tv, sizeof(tv));
1376 if (fio_check_clients_timed_out())
1380 ret = poll(pfds, nr_clients, ops->eta_msec);
1384 log_err("fio: poll clients: %s\n", strerror(errno));
1390 for (i = 0; i < nr_clients; i++) {
1391 if (!(pfds[i].revents & POLLIN))
1394 client = find_client_by_fd(pfds[i].fd);
1396 log_err("fio: unknown client fd %d\n", pfds[i].fd);
1399 if (!fio_handle_client(client)) {
1400 log_info("client: host=%s disconnected\n",
1402 remove_client(client);
1404 } else if (client->error)
1406 fio_put_client(client);