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;
58 static int do_output_all_clients;
60 #define FIO_CLIENT_HASH_BITS 7
61 #define FIO_CLIENT_HASH_SZ (1 << FIO_CLIENT_HASH_BITS)
62 #define FIO_CLIENT_HASH_MASK (FIO_CLIENT_HASH_SZ - 1)
63 static struct flist_head client_hash[FIO_CLIENT_HASH_SZ];
65 static void fio_client_add_hash(struct fio_client *client)
67 int bucket = hash_long(client->fd, FIO_CLIENT_HASH_BITS);
69 bucket &= FIO_CLIENT_HASH_MASK;
70 flist_add(&client->hash_list, &client_hash[bucket]);
73 static void fio_client_remove_hash(struct fio_client *client)
75 if (!flist_empty(&client->hash_list))
76 flist_del_init(&client->hash_list);
79 static void fio_init fio_client_hash_init(void)
83 for (i = 0; i < FIO_CLIENT_HASH_SZ; i++)
84 INIT_FLIST_HEAD(&client_hash[i]);
87 static struct fio_client *find_client_by_fd(int fd)
89 int bucket = hash_long(fd, FIO_CLIENT_HASH_BITS) & FIO_CLIENT_HASH_MASK;
90 struct fio_client *client;
91 struct flist_head *entry;
93 flist_for_each(entry, &client_hash[bucket]) {
94 client = flist_entry(entry, struct fio_client, hash_list);
96 if (client->fd == fd) {
105 void fio_put_client(struct fio_client *client)
110 free(client->hostname);
115 while (client->nr_ini_file)
116 free(client->ini_file[--client->nr_ini_file]);
117 if (client->ini_file)
118 free(client->ini_file);
120 if (!client->did_stat)
121 sum_stat_clients -= client->nr_stat;
126 static void remove_client(struct fio_client *client)
128 assert(client->refs);
130 dprint(FD_NET, "client: removed <%s>\n", client->hostname);
132 if (!flist_empty(&client->list))
133 flist_del_init(&client->list);
135 fio_client_remove_hash(client);
137 if (!flist_empty(&client->eta_list)) {
138 flist_del_init(&client->eta_list);
139 fio_client_dec_jobs_eta(client->eta_in_flight, client->ops->eta);
145 if (client->ops->removed)
146 client->ops->removed(client);
149 fio_put_client(client);
152 struct fio_client *fio_get_client(struct fio_client *client)
158 static void __fio_client_add_cmd_option(struct fio_client *client,
163 index = client->argc++;
164 client->argv = realloc(client->argv, sizeof(char *) * client->argc);
165 client->argv[index] = strdup(opt);
166 dprint(FD_NET, "client: add cmd %d: %s\n", index, opt);
169 void fio_client_add_cmd_option(void *cookie, const char *opt)
171 struct fio_client *client = cookie;
172 struct flist_head *entry;
177 __fio_client_add_cmd_option(client, opt);
180 * Duplicate arguments to shared client group
182 flist_for_each(entry, &arg_list) {
183 client = flist_entry(entry, struct fio_client, arg_list);
185 __fio_client_add_cmd_option(client, opt);
189 struct fio_client *fio_client_add_explicit(struct client_ops *ops,
190 const char *hostname, int type,
193 struct fio_client *client;
195 client = malloc(sizeof(*client));
196 memset(client, 0, sizeof(*client));
198 INIT_FLIST_HEAD(&client->list);
199 INIT_FLIST_HEAD(&client->hash_list);
200 INIT_FLIST_HEAD(&client->arg_list);
201 INIT_FLIST_HEAD(&client->eta_list);
202 INIT_FLIST_HEAD(&client->cmd_list);
204 client->hostname = strdup(hostname);
206 if (type == Fio_client_socket)
211 ipv6 = type == Fio_client_ipv6;
212 if (fio_server_parse_host(hostname, &ipv6,
213 &client->addr.sin_addr,
214 &client->addr6.sin6_addr))
223 client->type = ops->client_type;
225 __fio_client_add_cmd_option(client, "fio");
227 flist_add(&client->list, &client_list);
229 dprint(FD_NET, "client: added <%s>\n", client->hostname);
236 void fio_client_add_ini_file(void *cookie, const char *ini_file)
238 struct fio_client *client = cookie;
241 dprint(FD_NET, "client <%s>: add ini %s\n", client->hostname, ini_file);
243 new_size = (client->nr_ini_file + 1) * sizeof(char *);
244 client->ini_file = realloc(client->ini_file, new_size);
245 client->ini_file[client->nr_ini_file] = strdup(ini_file);
246 client->nr_ini_file++;
249 int fio_client_add(struct client_ops *ops, const char *hostname, void **cookie)
251 struct fio_client *existing = *cookie;
252 struct fio_client *client;
256 * We always add our "exec" name as the option, hence 1
259 if (existing->argc == 1)
260 flist_add_tail(&existing->arg_list, &arg_list);
262 while (!flist_empty(&arg_list))
263 flist_del_init(arg_list.next);
267 client = malloc(sizeof(*client));
268 memset(client, 0, sizeof(*client));
270 INIT_FLIST_HEAD(&client->list);
271 INIT_FLIST_HEAD(&client->hash_list);
272 INIT_FLIST_HEAD(&client->arg_list);
273 INIT_FLIST_HEAD(&client->eta_list);
274 INIT_FLIST_HEAD(&client->cmd_list);
276 if (fio_server_parse_string(hostname, &client->hostname,
277 &client->is_sock, &client->port,
278 &client->addr.sin_addr,
279 &client->addr6.sin6_addr,
286 client->type = ops->client_type;
288 __fio_client_add_cmd_option(client, "fio");
290 flist_add(&client->list, &client_list);
292 dprint(FD_NET, "client: added <%s>\n", client->hostname);
297 static void probe_client(struct fio_client *client)
299 dprint(FD_NET, "client: send probe\n");
301 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_PROBE, 0, &client->cmd_list);
304 static int fio_client_connect_ip(struct fio_client *client)
306 struct sockaddr *addr;
311 client->addr6.sin6_family = AF_INET6;
312 client->addr6.sin6_port = htons(client->port);
314 addr = (struct sockaddr *) &client->addr6;
315 socklen = sizeof(client->addr6);
317 client->addr.sin_family = AF_INET;
318 client->addr.sin_port = htons(client->port);
320 addr = (struct sockaddr *) &client->addr;
321 socklen = sizeof(client->addr);
324 fd = socket(domain, SOCK_STREAM, 0);
328 log_err("fio: socket: %s\n", strerror(errno));
332 if (connect(fd, addr, socklen) < 0) {
335 log_err("fio: connect: %s\n", strerror(errno));
336 log_err("fio: failed to connect to %s:%u\n", client->hostname,
345 static int fio_client_connect_sock(struct fio_client *client)
347 struct sockaddr_un *addr = &client->addr_un;
351 memset(addr, 0, sizeof(*addr));
352 addr->sun_family = AF_UNIX;
353 strcpy(addr->sun_path, client->hostname);
355 fd = socket(AF_UNIX, SOCK_STREAM, 0);
359 log_err("fio: socket: %s\n", strerror(errno));
363 len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
364 if (connect(fd, (struct sockaddr *) addr, len) < 0) {
367 log_err("fio: connect; %s\n", strerror(errno));
375 int fio_client_connect(struct fio_client *client)
379 dprint(FD_NET, "client: connect to host %s\n", client->hostname);
382 fd = fio_client_connect_sock(client);
384 fd = fio_client_connect_ip(client);
386 dprint(FD_NET, "client: %s connected %d\n", client->hostname, fd);
392 fio_client_add_hash(client);
393 client->state = Client_connected;
395 probe_client(client);
399 int fio_client_terminate(struct fio_client *client)
401 return fio_net_send_quit(client->fd);
404 void fio_clients_terminate(void)
406 struct flist_head *entry;
407 struct fio_client *client;
409 dprint(FD_NET, "client: terminate clients\n");
411 flist_for_each(entry, &client_list) {
412 client = flist_entry(entry, struct fio_client, list);
413 fio_client_terminate(client);
417 static void sig_int(int sig)
419 dprint(FD_NET, "client: got signal %d\n", sig);
420 fio_clients_terminate();
423 static void sig_show_status(int sig)
425 show_running_run_stats();
428 static void client_signal_handler(void)
430 struct sigaction act;
432 memset(&act, 0, sizeof(act));
433 act.sa_handler = sig_int;
434 act.sa_flags = SA_RESTART;
435 sigaction(SIGINT, &act, NULL);
437 memset(&act, 0, sizeof(act));
438 act.sa_handler = sig_int;
439 act.sa_flags = SA_RESTART;
440 sigaction(SIGTERM, &act, NULL);
442 /* Windows uses SIGBREAK as a quit signal from other applications */
444 memset(&act, 0, sizeof(act));
445 act.sa_handler = sig_int;
446 act.sa_flags = SA_RESTART;
447 sigaction(SIGBREAK, &act, NULL);
450 memset(&act, 0, sizeof(act));
451 act.sa_handler = sig_show_status;
452 act.sa_flags = SA_RESTART;
453 sigaction(SIGUSR1, &act, NULL);
456 static int send_client_cmd_line(struct fio_client *client)
458 struct cmd_single_line_pdu *cslp;
459 struct cmd_line_pdu *clp;
460 unsigned long offset;
466 dprint(FD_NET, "client: send cmdline %d\n", client->argc);
468 lens = malloc(client->argc * sizeof(unsigned int));
471 * Find out how much mem we need
473 for (i = 0, mem = 0; i < client->argc; i++) {
474 lens[i] = strlen(client->argv[i]) + 1;
479 * We need one cmd_line_pdu, and argc number of cmd_single_line_pdu
481 mem += sizeof(*clp) + (client->argc * sizeof(*cslp));
485 offset = sizeof(*clp);
487 for (i = 0; i < client->argc; i++) {
488 uint16_t arg_len = lens[i];
491 strcpy((char *) cslp->text, client->argv[i]);
492 cslp->len = cpu_to_le16(arg_len);
493 offset += sizeof(*cslp) + arg_len;
497 clp->lines = cpu_to_le16(client->argc);
498 clp->client_type = __cpu_to_le16(client->type);
499 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOBLINE, pdu, mem, NULL, NULL);
504 int fio_clients_connect(void)
506 struct fio_client *client;
507 struct flist_head *entry, *tmp;
512 WSAStartup(MAKEWORD(2, 2), &wsd);
515 dprint(FD_NET, "client: connect all\n");
517 client_signal_handler();
519 flist_for_each_safe(entry, tmp, &client_list) {
520 client = flist_entry(entry, struct fio_client, list);
522 ret = fio_client_connect(client);
524 remove_client(client);
528 if (client->argc > 1)
529 send_client_cmd_line(client);
535 int fio_start_client(struct fio_client *client)
537 dprint(FD_NET, "client: start %s\n", client->hostname);
538 return fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_RUN, 0, NULL);
541 int fio_start_all_clients(void)
543 struct fio_client *client;
544 struct flist_head *entry, *tmp;
547 dprint(FD_NET, "client: start all\n");
549 flist_for_each_safe(entry, tmp, &client_list) {
550 client = flist_entry(entry, struct fio_client, list);
552 ret = fio_start_client(client);
554 remove_client(client);
559 return flist_empty(&client_list);
563 * Send file contents to server backend. We could use sendfile(), but to remain
564 * more portable lets just read/write the darn thing.
566 static int __fio_client_send_ini(struct fio_client *client, const char *filename)
568 struct cmd_job_pdu *pdu;
576 dprint(FD_NET, "send ini %s to %s\n", filename, client->hostname);
578 fd = open(filename, O_RDONLY);
582 log_err("fio: job file <%s> open: %s\n", filename, strerror(errno));
586 if (fstat(fd, &sb) < 0) {
589 log_err("fio: job file stat: %s\n", strerror(errno));
594 p_size = sb.st_size + sizeof(*pdu);
595 pdu = malloc(p_size);
601 ret = read(fd, p, len);
610 else if (errno == EAGAIN || errno == EINTR)
615 log_err("fio: failed reading job file %s\n", filename);
621 pdu->buf_len = __cpu_to_le32(sb.st_size);
622 pdu->client_type = cpu_to_le32(client->type);
624 client->sent_job = 1;
625 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, pdu, p_size, NULL, NULL);
631 int fio_client_send_ini(struct fio_client *client, const char *filename)
635 ret = __fio_client_send_ini(client, filename);
637 client->sent_job = 1;
642 int fio_clients_send_ini(const char *filename)
644 struct fio_client *client;
645 struct flist_head *entry, *tmp;
647 flist_for_each_safe(entry, tmp, &client_list) {
648 client = flist_entry(entry, struct fio_client, list);
650 if (client->nr_ini_file) {
653 for (i = 0; i < client->nr_ini_file; i++) {
654 const char *ini = client->ini_file[i];
656 if (fio_client_send_ini(client, ini)) {
657 remove_client(client);
661 } else if (!filename || fio_client_send_ini(client, filename))
662 remove_client(client);
668 int fio_client_update_options(struct fio_client *client,
669 struct thread_options *o, uint64_t *tag)
671 struct cmd_add_job_pdu pdu;
673 pdu.thread_number = cpu_to_le32(client->thread_number);
674 pdu.groupid = cpu_to_le32(client->groupid);
675 convert_thread_options_to_net(&pdu.top, o);
677 return fio_net_send_cmd(client->fd, FIO_NET_CMD_UPDATE_JOB, &pdu, sizeof(pdu), tag, &client->cmd_list);
680 static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
682 dst->max_val = le64_to_cpu(src->max_val);
683 dst->min_val = le64_to_cpu(src->min_val);
684 dst->samples = le64_to_cpu(src->samples);
687 * Floats arrive as IEEE 754 encoded uint64_t, convert back to double
689 dst->mean.u.f = fio_uint64_to_double(le64_to_cpu(dst->mean.u.i));
690 dst->S.u.f = fio_uint64_to_double(le64_to_cpu(dst->S.u.i));
693 static void convert_ts(struct thread_stat *dst, struct thread_stat *src)
697 dst->error = le32_to_cpu(src->error);
698 dst->thread_number = le32_to_cpu(src->thread_number);
699 dst->groupid = le32_to_cpu(src->groupid);
700 dst->pid = le32_to_cpu(src->pid);
701 dst->members = le32_to_cpu(src->members);
702 dst->unified_rw_rep = le32_to_cpu(src->unified_rw_rep);
704 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
705 convert_io_stat(&dst->clat_stat[i], &src->clat_stat[i]);
706 convert_io_stat(&dst->slat_stat[i], &src->slat_stat[i]);
707 convert_io_stat(&dst->lat_stat[i], &src->lat_stat[i]);
708 convert_io_stat(&dst->bw_stat[i], &src->bw_stat[i]);
711 dst->usr_time = le64_to_cpu(src->usr_time);
712 dst->sys_time = le64_to_cpu(src->sys_time);
713 dst->ctx = le64_to_cpu(src->ctx);
714 dst->minf = le64_to_cpu(src->minf);
715 dst->majf = le64_to_cpu(src->majf);
716 dst->clat_percentiles = le64_to_cpu(src->clat_percentiles);
718 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
719 fio_fp64_t *fps = &src->percentile_list[i];
720 fio_fp64_t *fpd = &dst->percentile_list[i];
722 fpd->u.f = fio_uint64_to_double(le64_to_cpu(fps->u.i));
725 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
726 dst->io_u_map[i] = le32_to_cpu(src->io_u_map[i]);
727 dst->io_u_submit[i] = le32_to_cpu(src->io_u_submit[i]);
728 dst->io_u_complete[i] = le32_to_cpu(src->io_u_complete[i]);
731 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
732 dst->io_u_lat_u[i] = le32_to_cpu(src->io_u_lat_u[i]);
733 dst->io_u_lat_m[i] = le32_to_cpu(src->io_u_lat_m[i]);
736 for (i = 0; i < DDIR_RWDIR_CNT; i++)
737 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
738 dst->io_u_plat[i][j] = le32_to_cpu(src->io_u_plat[i][j]);
740 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
741 dst->total_io_u[i] = le64_to_cpu(src->total_io_u[i]);
742 dst->short_io_u[i] = le64_to_cpu(src->short_io_u[i]);
745 dst->total_submit = le64_to_cpu(src->total_submit);
746 dst->total_complete = le64_to_cpu(src->total_complete);
748 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
749 dst->io_bytes[i] = le64_to_cpu(src->io_bytes[i]);
750 dst->runtime[i] = le64_to_cpu(src->runtime[i]);
753 dst->total_run_time = le64_to_cpu(src->total_run_time);
754 dst->continue_on_error = le16_to_cpu(src->continue_on_error);
755 dst->total_err_count = le64_to_cpu(src->total_err_count);
756 dst->first_error = le32_to_cpu(src->first_error);
757 dst->kb_base = le32_to_cpu(src->kb_base);
760 static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
764 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
765 dst->max_run[i] = le64_to_cpu(src->max_run[i]);
766 dst->min_run[i] = le64_to_cpu(src->min_run[i]);
767 dst->max_bw[i] = le64_to_cpu(src->max_bw[i]);
768 dst->min_bw[i] = le64_to_cpu(src->min_bw[i]);
769 dst->io_kb[i] = le64_to_cpu(src->io_kb[i]);
770 dst->agg[i] = le64_to_cpu(src->agg[i]);
773 dst->kb_base = le32_to_cpu(src->kb_base);
774 dst->groupid = le32_to_cpu(src->groupid);
775 dst->unified_rw_rep = le32_to_cpu(src->unified_rw_rep);
778 static void handle_ts(struct fio_client *client, struct fio_net_cmd *cmd)
780 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
782 show_thread_status(&p->ts, &p->rs);
783 client->did_stat = 1;
785 if (!do_output_all_clients)
788 sum_thread_stats(&client_ts, &p->ts, sum_stat_nr);
789 sum_group_stats(&client_gs, &p->rs);
792 client_ts.thread_number = p->ts.thread_number;
793 client_ts.groupid = p->ts.groupid;
794 client_ts.unified_rw_rep = p->ts.unified_rw_rep;
796 if (++sum_stat_nr == sum_stat_clients) {
797 strcpy(client_ts.name, "All clients");
798 show_thread_status(&client_ts, &client_gs);
802 static void handle_gs(struct fio_client *client, struct fio_net_cmd *cmd)
804 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
806 show_group_stats(gs);
809 static void handle_text(struct fio_client *client, struct fio_net_cmd *cmd)
811 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
812 const char *buf = (const char *) pdu->buf;
816 name = client->name ? client->name : client->hostname;
818 if (!client->skip_newline)
819 fprintf(f_out, "<%s> ", name);
820 ret = fwrite(buf, pdu->buf_len, 1, f_out);
822 client->skip_newline = strchr(buf, '\n') == NULL;
825 static void convert_agg(struct disk_util_agg *agg)
829 for (i = 0; i < 2; i++) {
830 agg->ios[i] = le32_to_cpu(agg->ios[i]);
831 agg->merges[i] = le32_to_cpu(agg->merges[i]);
832 agg->sectors[i] = le64_to_cpu(agg->sectors[i]);
833 agg->ticks[i] = le32_to_cpu(agg->ticks[i]);
836 agg->io_ticks = le32_to_cpu(agg->io_ticks);
837 agg->time_in_queue = le32_to_cpu(agg->time_in_queue);
838 agg->slavecount = le32_to_cpu(agg->slavecount);
839 agg->max_util.u.f = fio_uint64_to_double(__le64_to_cpu(agg->max_util.u.i));
842 static void convert_dus(struct disk_util_stat *dus)
846 for (i = 0; i < 2; i++) {
847 dus->ios[i] = le32_to_cpu(dus->ios[i]);
848 dus->merges[i] = le32_to_cpu(dus->merges[i]);
849 dus->sectors[i] = le64_to_cpu(dus->sectors[i]);
850 dus->ticks[i] = le32_to_cpu(dus->ticks[i]);
853 dus->io_ticks = le32_to_cpu(dus->io_ticks);
854 dus->time_in_queue = le32_to_cpu(dus->time_in_queue);
855 dus->msec = le64_to_cpu(dus->msec);
858 static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd)
860 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
862 if (!client->disk_stats_shown) {
863 client->disk_stats_shown = 1;
864 log_info("\nDisk stats (read/write):\n");
867 print_disk_util(&du->dus, &du->agg, output_format == FIO_OUTPUT_TERSE);
870 static void convert_jobs_eta(struct jobs_eta *je)
874 je->nr_running = le32_to_cpu(je->nr_running);
875 je->nr_ramp = le32_to_cpu(je->nr_ramp);
876 je->nr_pending = le32_to_cpu(je->nr_pending);
877 je->files_open = le32_to_cpu(je->files_open);
879 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
880 je->m_rate[i] = le32_to_cpu(je->m_rate[i]);
881 je->t_rate[i] = le32_to_cpu(je->t_rate[i]);
882 je->m_iops[i] = le32_to_cpu(je->m_iops[i]);
883 je->t_iops[i] = le32_to_cpu(je->t_iops[i]);
886 je->elapsed_sec = le64_to_cpu(je->elapsed_sec);
887 je->eta_sec = le64_to_cpu(je->eta_sec);
888 je->nr_threads = le32_to_cpu(je->nr_threads);
889 je->is_pow2 = le32_to_cpu(je->is_pow2);
892 void fio_client_sum_jobs_eta(struct jobs_eta *dst, struct jobs_eta *je)
896 dst->nr_running += je->nr_running;
897 dst->nr_ramp += je->nr_ramp;
898 dst->nr_pending += je->nr_pending;
899 dst->files_open += je->files_open;
901 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
902 dst->m_rate[i] += je->m_rate[i];
903 dst->t_rate[i] += je->t_rate[i];
904 dst->m_iops[i] += je->m_iops[i];
905 dst->t_iops[i] += je->t_iops[i];
908 dst->elapsed_sec += je->elapsed_sec;
910 if (je->eta_sec > dst->eta_sec)
911 dst->eta_sec = je->eta_sec;
913 dst->nr_threads += je->nr_threads;
914 /* we need to handle je->run_str too ... */
917 void fio_client_dec_jobs_eta(struct client_eta *eta, client_eta_op eta_fn)
919 if (!--eta->pending) {
925 static void remove_reply_cmd(struct fio_client *client, struct fio_net_cmd *cmd)
927 struct fio_net_cmd_reply *reply = NULL;
928 struct flist_head *entry;
930 flist_for_each(entry, &client->cmd_list) {
931 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
933 if (cmd->tag == (uintptr_t) reply)
940 log_err("fio: client: unable to find matching tag (%lx)\n", cmd->tag);
944 flist_del(&reply->list);
945 cmd->tag = reply->saved_tag;
949 int fio_client_wait_for_reply(struct fio_client *client, uint64_t tag)
952 struct fio_net_cmd_reply *reply = NULL;
953 struct flist_head *entry;
955 flist_for_each(entry, &client->cmd_list) {
956 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
958 if (tag == (uintptr_t) reply)
973 static void handle_eta(struct fio_client *client, struct fio_net_cmd *cmd)
975 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
976 struct client_eta *eta = (struct client_eta *) (uintptr_t) cmd->tag;
978 dprint(FD_NET, "client: got eta tag %p, %d\n", eta, eta->pending);
980 assert(client->eta_in_flight == eta);
982 client->eta_in_flight = NULL;
983 flist_del_init(&client->eta_list);
985 if (client->ops->jobs_eta)
986 client->ops->jobs_eta(client, je);
988 fio_client_sum_jobs_eta(&eta->eta, je);
989 fio_client_dec_jobs_eta(eta, client->ops->eta);
992 static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd)
994 struct cmd_probe_pdu *probe = (struct cmd_probe_pdu *) cmd->payload;
995 const char *os, *arch;
998 os = fio_get_os_string(probe->os);
1002 arch = fio_get_arch_string(probe->arch);
1006 sprintf(bit, "%d-bit", probe->bpp * 8);
1008 log_info("hostname=%s, be=%u, %s, os=%s, arch=%s, fio=%s\n",
1009 probe->hostname, probe->bigendian, bit, os, arch,
1010 probe->fio_version);
1013 client->name = strdup((char *) probe->hostname);
1016 static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd)
1018 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
1020 client->state = Client_started;
1021 client->jobs = le32_to_cpu(pdu->jobs);
1022 client->nr_stat = le32_to_cpu(pdu->stat_outputs);
1024 if (sum_stat_clients > 1)
1025 do_output_all_clients = 1;
1027 sum_stat_clients += client->nr_stat;
1030 static void handle_stop(struct fio_client *client, struct fio_net_cmd *cmd)
1033 log_info("client <%s>: exited with error %d\n", client->hostname, client->error);
1036 static void convert_stop(struct fio_net_cmd *cmd)
1038 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
1040 pdu->error = le32_to_cpu(pdu->error);
1043 static void convert_text(struct fio_net_cmd *cmd)
1045 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
1047 pdu->level = le32_to_cpu(pdu->level);
1048 pdu->buf_len = le32_to_cpu(pdu->buf_len);
1049 pdu->log_sec = le64_to_cpu(pdu->log_sec);
1050 pdu->log_usec = le64_to_cpu(pdu->log_usec);
1054 * This has been compressed on the server side, since it can be big.
1057 static struct cmd_iolog_pdu *convert_iolog(struct fio_net_cmd *cmd)
1059 struct cmd_iolog_pdu *pdu = (struct cmd_iolog_pdu *) cmd->payload;
1060 struct cmd_iolog_pdu *ret;
1061 uint32_t nr_samples;
1062 unsigned long total;
1067 stream.zalloc = Z_NULL;
1068 stream.zfree = Z_NULL;
1069 stream.opaque = Z_NULL;
1070 stream.avail_in = 0;
1071 stream.next_in = Z_NULL;
1073 if (inflateInit(&stream) != Z_OK)
1077 * Get header first, it's not compressed
1079 nr_samples = le32_to_cpu(pdu->nr_samples);
1081 total = nr_samples * sizeof(struct io_sample);
1082 ret = malloc(total + sizeof(*pdu));
1083 ret->thread_number = le32_to_cpu(pdu->thread_number);
1084 ret->nr_samples = nr_samples;
1085 ret->log_type = le32_to_cpu(pdu->log_type);
1086 strcpy((char *) ret->name, (char *) pdu->name);
1088 p = (void *) ret + sizeof(*pdu);
1090 stream.avail_in = cmd->pdu_len - sizeof(*pdu);
1091 stream.next_in = (void *) pdu + sizeof(*pdu);
1092 while (stream.avail_in) {
1093 unsigned int this_chunk = 65536;
1094 unsigned int this_len;
1097 if (this_chunk > total)
1100 stream.avail_out = this_chunk;
1101 stream.next_out = p;
1102 err = inflate(&stream, Z_NO_FLUSH);
1103 /* may be Z_OK, or Z_STREAM_END */
1105 log_err("fio: inflate error %d\n", err);
1111 this_len = this_chunk - stream.avail_out;
1116 for (i = 0; i < ret->nr_samples; i++) {
1117 struct io_sample *s = &ret->samples[i];
1119 s->time = le64_to_cpu(s->time);
1120 s->val = le64_to_cpu(s->val);
1121 s->ddir = le32_to_cpu(s->ddir);
1122 s->bs = le32_to_cpu(s->bs);
1126 inflateEnd(&stream);
1130 int fio_handle_client(struct fio_client *client)
1132 struct client_ops *ops = client->ops;
1133 struct fio_net_cmd *cmd;
1135 dprint(FD_NET, "client: handle %s\n", client->hostname);
1137 cmd = fio_net_recv_cmd(client->fd);
1141 dprint(FD_NET, "client: got cmd op %s from %s (pdu=%u)\n",
1142 fio_server_op(cmd->opcode), client->hostname, cmd->pdu_len);
1144 switch (cmd->opcode) {
1145 case FIO_NET_CMD_QUIT:
1147 ops->quit(client, cmd);
1148 remove_client(client);
1151 case FIO_NET_CMD_TEXT:
1153 ops->text(client, cmd);
1156 case FIO_NET_CMD_DU: {
1157 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
1159 convert_dus(&du->dus);
1160 convert_agg(&du->agg);
1162 ops->disk_util(client, cmd);
1166 case FIO_NET_CMD_TS: {
1167 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
1169 convert_ts(&p->ts, &p->ts);
1170 convert_gs(&p->rs, &p->rs);
1172 ops->thread_status(client, cmd);
1176 case FIO_NET_CMD_GS: {
1177 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
1181 ops->group_stats(client, cmd);
1185 case FIO_NET_CMD_ETA: {
1186 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
1188 remove_reply_cmd(client, cmd);
1189 convert_jobs_eta(je);
1190 handle_eta(client, cmd);
1194 case FIO_NET_CMD_PROBE:
1195 remove_reply_cmd(client, cmd);
1196 ops->probe(client, cmd);
1199 case FIO_NET_CMD_SERVER_START:
1200 client->state = Client_running;
1202 ops->job_start(client, cmd);
1205 case FIO_NET_CMD_START: {
1206 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
1208 pdu->jobs = le32_to_cpu(pdu->jobs);
1209 ops->start(client, cmd);
1213 case FIO_NET_CMD_STOP: {
1214 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
1217 client->state = Client_stopped;
1218 client->error = le32_to_cpu(pdu->error);
1219 client->signal = le32_to_cpu(pdu->signal);
1220 ops->stop(client, cmd);
1224 case FIO_NET_CMD_ADD_JOB: {
1225 struct cmd_add_job_pdu *pdu = (struct cmd_add_job_pdu *) cmd->payload;
1227 client->thread_number = le32_to_cpu(pdu->thread_number);
1228 client->groupid = le32_to_cpu(pdu->groupid);
1231 ops->add_job(client, cmd);
1235 case FIO_NET_CMD_IOLOG:
1237 struct cmd_iolog_pdu *pdu;
1239 pdu = convert_iolog(cmd);
1240 ops->iolog(client, pdu);
1244 case FIO_NET_CMD_UPDATE_JOB:
1245 ops->update_job(client, cmd);
1246 remove_reply_cmd(client, cmd);
1250 log_err("fio: unknown client op: %s\n", fio_server_op(cmd->opcode));
1258 static void request_client_etas(struct client_ops *ops)
1260 struct fio_client *client;
1261 struct flist_head *entry;
1262 struct client_eta *eta;
1265 dprint(FD_NET, "client: request eta (%d)\n", nr_clients);
1267 eta = malloc(sizeof(*eta));
1268 memset(&eta->eta, 0, sizeof(eta->eta));
1269 eta->pending = nr_clients;
1271 flist_for_each(entry, &client_list) {
1272 client = flist_entry(entry, struct fio_client, list);
1274 if (!flist_empty(&client->eta_list)) {
1278 if (client->state != Client_running)
1281 assert(!client->eta_in_flight);
1282 flist_add_tail(&client->eta_list, &eta_list);
1283 client->eta_in_flight = eta;
1284 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_SEND_ETA,
1285 (uintptr_t) eta, &client->cmd_list);
1289 fio_client_dec_jobs_eta(eta, ops->eta);
1291 dprint(FD_NET, "client: requested eta tag %p\n", eta);
1294 static int client_check_cmd_timeout(struct fio_client *client,
1295 struct timeval *now)
1297 struct fio_net_cmd_reply *reply;
1298 struct flist_head *entry, *tmp;
1301 flist_for_each_safe(entry, tmp, &client->cmd_list) {
1302 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
1304 if (mtime_since(&reply->tv, now) < FIO_NET_CLIENT_TIMEOUT)
1307 log_err("fio: client %s, timeout on cmd %s\n", client->hostname,
1308 fio_server_op(reply->opcode));
1309 flist_del(&reply->list);
1314 return flist_empty(&client->cmd_list) && ret;
1317 static int fio_check_clients_timed_out(void)
1319 struct fio_client *client;
1320 struct flist_head *entry, *tmp;
1324 fio_gettime(&tv, NULL);
1326 flist_for_each_safe(entry, tmp, &client_list) {
1327 client = flist_entry(entry, struct fio_client, list);
1329 if (flist_empty(&client->cmd_list))
1332 if (!client_check_cmd_timeout(client, &tv))
1335 if (client->ops->timed_out)
1336 client->ops->timed_out(client);
1338 log_err("fio: client %s timed out\n", client->hostname);
1340 remove_client(client);
1347 int fio_handle_clients(struct client_ops *ops)
1349 struct pollfd *pfds;
1350 int i, ret = 0, retval = 0;
1352 fio_gettime(&eta_tv, NULL);
1354 pfds = malloc(nr_clients * sizeof(struct pollfd));
1356 init_thread_stat(&client_ts);
1357 init_group_run_stat(&client_gs);
1359 while (!exit_backend && nr_clients) {
1360 struct flist_head *entry, *tmp;
1361 struct fio_client *client;
1364 flist_for_each_safe(entry, tmp, &client_list) {
1365 client = flist_entry(entry, struct fio_client, list);
1367 if (!client->sent_job && !client->ops->stay_connected &&
1368 flist_empty(&client->cmd_list)) {
1369 remove_client(client);
1373 pfds[i].fd = client->fd;
1374 pfds[i].events = POLLIN;
1381 assert(i == nr_clients);
1386 fio_gettime(&tv, NULL);
1387 if (mtime_since(&eta_tv, &tv) >= 900) {
1388 request_client_etas(ops);
1389 memcpy(&eta_tv, &tv, sizeof(tv));
1391 if (fio_check_clients_timed_out())
1395 ret = poll(pfds, nr_clients, ops->eta_msec);
1399 log_err("fio: poll clients: %s\n", strerror(errno));
1405 for (i = 0; i < nr_clients; i++) {
1406 if (!(pfds[i].revents & POLLIN))
1409 client = find_client_by_fd(pfds[i].fd);
1411 log_err("fio: unknown client fd %d\n", pfds[i].fd);
1414 if (!fio_handle_client(client)) {
1415 log_info("client: host=%s disconnected\n",
1417 remove_client(client);
1419 } else if (client->error)
1421 fio_put_client(client);