11 #include <sys/socket.h>
13 #include <netinet/in.h>
14 #include <arpa/inet.h>
27 static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd);
28 static void handle_ts(struct fio_client *client, struct fio_net_cmd *cmd);
29 static void handle_gs(struct fio_client *client, struct fio_net_cmd *cmd);
30 static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd);
31 static void handle_text(struct fio_client *client, struct fio_net_cmd *cmd);
32 static void handle_stop(struct fio_client *client, struct fio_net_cmd *cmd);
33 static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd);
35 struct client_ops fio_client_ops = {
37 .disk_util = handle_du,
38 .thread_status = handle_ts,
39 .group_stats = handle_gs,
41 .start = handle_start,
42 .eta = display_thread_status,
43 .probe = handle_probe,
44 .eta_msec = FIO_CLIENT_DEF_ETA_MSEC,
45 .client_type = FIO_CLIENT_TYPE_CLI,
48 static struct timeval eta_tv;
50 static FLIST_HEAD(client_list);
51 static FLIST_HEAD(eta_list);
53 static FLIST_HEAD(arg_list);
55 struct thread_stat client_ts;
56 struct group_run_stats client_gs;
59 static int sum_stat_nr;
60 static int do_output_all_clients;
62 #define FIO_CLIENT_HASH_BITS 7
63 #define FIO_CLIENT_HASH_SZ (1 << FIO_CLIENT_HASH_BITS)
64 #define FIO_CLIENT_HASH_MASK (FIO_CLIENT_HASH_SZ - 1)
65 static struct flist_head client_hash[FIO_CLIENT_HASH_SZ];
67 static void fio_client_add_hash(struct fio_client *client)
69 int bucket = hash_long(client->fd, FIO_CLIENT_HASH_BITS);
71 bucket &= FIO_CLIENT_HASH_MASK;
72 flist_add(&client->hash_list, &client_hash[bucket]);
75 static void fio_client_remove_hash(struct fio_client *client)
77 if (!flist_empty(&client->hash_list))
78 flist_del_init(&client->hash_list);
81 static void fio_init fio_client_hash_init(void)
85 for (i = 0; i < FIO_CLIENT_HASH_SZ; i++)
86 INIT_FLIST_HEAD(&client_hash[i]);
89 static struct fio_client *find_client_by_fd(int fd)
91 int bucket = hash_long(fd, FIO_CLIENT_HASH_BITS) & FIO_CLIENT_HASH_MASK;
92 struct fio_client *client;
93 struct flist_head *entry;
95 flist_for_each(entry, &client_hash[bucket]) {
96 client = flist_entry(entry, struct fio_client, hash_list);
98 if (client->fd == fd) {
107 void fio_put_client(struct fio_client *client)
112 free(client->hostname);
117 while (client->nr_ini_file)
118 free(client->ini_file[--client->nr_ini_file]);
119 if (client->ini_file)
120 free(client->ini_file);
122 if (!client->did_stat)
123 sum_stat_clients -= client->nr_stat;
128 static void remove_client(struct fio_client *client)
130 assert(client->refs);
132 dprint(FD_NET, "client: removed <%s>\n", client->hostname);
134 if (!flist_empty(&client->list))
135 flist_del_init(&client->list);
137 fio_client_remove_hash(client);
139 if (!flist_empty(&client->eta_list)) {
140 flist_del_init(&client->eta_list);
141 fio_client_dec_jobs_eta(client->eta_in_flight, client->ops->eta);
147 if (client->ops->removed)
148 client->ops->removed(client);
151 fio_put_client(client);
154 struct fio_client *fio_get_client(struct fio_client *client)
160 static void __fio_client_add_cmd_option(struct fio_client *client,
165 index = client->argc++;
166 client->argv = realloc(client->argv, sizeof(char *) * client->argc);
167 client->argv[index] = strdup(opt);
168 dprint(FD_NET, "client: add cmd %d: %s\n", index, opt);
171 void fio_client_add_cmd_option(void *cookie, const char *opt)
173 struct fio_client *client = cookie;
174 struct flist_head *entry;
179 __fio_client_add_cmd_option(client, opt);
182 * Duplicate arguments to shared client group
184 flist_for_each(entry, &arg_list) {
185 client = flist_entry(entry, struct fio_client, arg_list);
187 __fio_client_add_cmd_option(client, opt);
191 struct fio_client *fio_client_add_explicit(struct client_ops *ops,
192 const char *hostname, int type,
195 struct fio_client *client;
197 client = malloc(sizeof(*client));
198 memset(client, 0, sizeof(*client));
200 INIT_FLIST_HEAD(&client->list);
201 INIT_FLIST_HEAD(&client->hash_list);
202 INIT_FLIST_HEAD(&client->arg_list);
203 INIT_FLIST_HEAD(&client->eta_list);
204 INIT_FLIST_HEAD(&client->cmd_list);
206 client->hostname = strdup(hostname);
208 if (type == Fio_client_socket)
213 ipv6 = type == Fio_client_ipv6;
214 if (fio_server_parse_host(hostname, &ipv6,
215 &client->addr.sin_addr,
216 &client->addr6.sin6_addr))
225 client->type = ops->client_type;
227 __fio_client_add_cmd_option(client, "fio");
229 flist_add(&client->list, &client_list);
231 dprint(FD_NET, "client: added <%s>\n", client->hostname);
238 void fio_client_add_ini_file(void *cookie, const char *ini_file)
240 struct fio_client *client = cookie;
243 dprint(FD_NET, "client <%s>: add ini %s\n", client->hostname, ini_file);
245 new_size = (client->nr_ini_file + 1) * sizeof(char *);
246 client->ini_file = realloc(client->ini_file, new_size);
247 client->ini_file[client->nr_ini_file] = strdup(ini_file);
248 client->nr_ini_file++;
251 int fio_client_add(struct client_ops *ops, const char *hostname, void **cookie)
253 struct fio_client *existing = *cookie;
254 struct fio_client *client;
258 * We always add our "exec" name as the option, hence 1
261 if (existing->argc == 1)
262 flist_add_tail(&existing->arg_list, &arg_list);
264 while (!flist_empty(&arg_list))
265 flist_del_init(arg_list.next);
269 client = malloc(sizeof(*client));
270 memset(client, 0, sizeof(*client));
272 INIT_FLIST_HEAD(&client->list);
273 INIT_FLIST_HEAD(&client->hash_list);
274 INIT_FLIST_HEAD(&client->arg_list);
275 INIT_FLIST_HEAD(&client->eta_list);
276 INIT_FLIST_HEAD(&client->cmd_list);
278 if (fio_server_parse_string(hostname, &client->hostname,
279 &client->is_sock, &client->port,
280 &client->addr.sin_addr,
281 &client->addr6.sin6_addr,
288 client->type = ops->client_type;
290 __fio_client_add_cmd_option(client, "fio");
292 flist_add(&client->list, &client_list);
294 dprint(FD_NET, "client: added <%s>\n", client->hostname);
299 static void probe_client(struct fio_client *client)
301 struct cmd_client_probe_pdu pdu;
304 dprint(FD_NET, "client: send probe\n");
307 pdu.flags = __le64_to_cpu(FIO_PROBE_FLAG_ZLIB);
312 fio_net_send_cmd(client->fd, FIO_NET_CMD_PROBE, &pdu, sizeof(pdu), &tag, &client->cmd_list);
315 static int fio_client_connect_ip(struct fio_client *client)
317 struct sockaddr *addr;
322 client->addr6.sin6_family = AF_INET6;
323 client->addr6.sin6_port = htons(client->port);
325 addr = (struct sockaddr *) &client->addr6;
326 socklen = sizeof(client->addr6);
328 client->addr.sin_family = AF_INET;
329 client->addr.sin_port = htons(client->port);
331 addr = (struct sockaddr *) &client->addr;
332 socklen = sizeof(client->addr);
335 fd = socket(domain, SOCK_STREAM, 0);
339 log_err("fio: socket: %s\n", strerror(errno));
343 if (connect(fd, addr, socklen) < 0) {
346 log_err("fio: connect: %s\n", strerror(errno));
347 log_err("fio: failed to connect to %s:%u\n", client->hostname,
356 static int fio_client_connect_sock(struct fio_client *client)
358 struct sockaddr_un *addr = &client->addr_un;
362 memset(addr, 0, sizeof(*addr));
363 addr->sun_family = AF_UNIX;
364 strcpy(addr->sun_path, client->hostname);
366 fd = socket(AF_UNIX, SOCK_STREAM, 0);
370 log_err("fio: socket: %s\n", strerror(errno));
374 len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
375 if (connect(fd, (struct sockaddr *) addr, len) < 0) {
378 log_err("fio: connect; %s\n", strerror(errno));
386 int fio_client_connect(struct fio_client *client)
390 dprint(FD_NET, "client: connect to host %s\n", client->hostname);
393 fd = fio_client_connect_sock(client);
395 fd = fio_client_connect_ip(client);
397 dprint(FD_NET, "client: %s connected %d\n", client->hostname, fd);
403 fio_client_add_hash(client);
404 client->state = Client_connected;
406 probe_client(client);
410 int fio_client_terminate(struct fio_client *client)
412 return fio_net_send_quit(client->fd);
415 void fio_clients_terminate(void)
417 struct flist_head *entry;
418 struct fio_client *client;
420 dprint(FD_NET, "client: terminate clients\n");
422 flist_for_each(entry, &client_list) {
423 client = flist_entry(entry, struct fio_client, list);
424 fio_client_terminate(client);
428 static void sig_int(int sig)
430 dprint(FD_NET, "client: got signal %d\n", sig);
431 fio_clients_terminate();
434 static void sig_show_status(int sig)
436 show_running_run_stats();
439 static void client_signal_handler(void)
441 struct sigaction act;
443 memset(&act, 0, sizeof(act));
444 act.sa_handler = sig_int;
445 act.sa_flags = SA_RESTART;
446 sigaction(SIGINT, &act, NULL);
448 memset(&act, 0, sizeof(act));
449 act.sa_handler = sig_int;
450 act.sa_flags = SA_RESTART;
451 sigaction(SIGTERM, &act, NULL);
453 /* Windows uses SIGBREAK as a quit signal from other applications */
455 memset(&act, 0, sizeof(act));
456 act.sa_handler = sig_int;
457 act.sa_flags = SA_RESTART;
458 sigaction(SIGBREAK, &act, NULL);
461 memset(&act, 0, sizeof(act));
462 act.sa_handler = sig_show_status;
463 act.sa_flags = SA_RESTART;
464 sigaction(SIGUSR1, &act, NULL);
467 static int send_client_cmd_line(struct fio_client *client)
469 struct cmd_single_line_pdu *cslp;
470 struct cmd_line_pdu *clp;
471 unsigned long offset;
477 dprint(FD_NET, "client: send cmdline %d\n", client->argc);
479 lens = malloc(client->argc * sizeof(unsigned int));
482 * Find out how much mem we need
484 for (i = 0, mem = 0; i < client->argc; i++) {
485 lens[i] = strlen(client->argv[i]) + 1;
490 * We need one cmd_line_pdu, and argc number of cmd_single_line_pdu
492 mem += sizeof(*clp) + (client->argc * sizeof(*cslp));
496 offset = sizeof(*clp);
498 for (i = 0; i < client->argc; i++) {
499 uint16_t arg_len = lens[i];
502 strcpy((char *) cslp->text, client->argv[i]);
503 cslp->len = cpu_to_le16(arg_len);
504 offset += sizeof(*cslp) + arg_len;
508 clp->lines = cpu_to_le16(client->argc);
509 clp->client_type = __cpu_to_le16(client->type);
510 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOBLINE, pdu, mem, NULL, NULL);
515 int fio_clients_connect(void)
517 struct fio_client *client;
518 struct flist_head *entry, *tmp;
523 WSAStartup(MAKEWORD(2, 2), &wsd);
526 dprint(FD_NET, "client: connect all\n");
528 client_signal_handler();
530 flist_for_each_safe(entry, tmp, &client_list) {
531 client = flist_entry(entry, struct fio_client, list);
533 ret = fio_client_connect(client);
535 remove_client(client);
539 if (client->argc > 1)
540 send_client_cmd_line(client);
546 int fio_start_client(struct fio_client *client)
548 dprint(FD_NET, "client: start %s\n", client->hostname);
549 return fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_RUN, 0, NULL);
552 int fio_start_all_clients(void)
554 struct fio_client *client;
555 struct flist_head *entry, *tmp;
558 dprint(FD_NET, "client: start all\n");
560 flist_for_each_safe(entry, tmp, &client_list) {
561 client = flist_entry(entry, struct fio_client, list);
563 ret = fio_start_client(client);
565 remove_client(client);
570 return flist_empty(&client_list);
574 * Send file contents to server backend. We could use sendfile(), but to remain
575 * more portable lets just read/write the darn thing.
577 static int __fio_client_send_ini(struct fio_client *client, const char *filename)
579 struct cmd_job_pdu *pdu;
587 dprint(FD_NET, "send ini %s to %s\n", filename, client->hostname);
589 fd = open(filename, O_RDONLY);
593 log_err("fio: job file <%s> open: %s\n", filename, strerror(errno));
597 if (fstat(fd, &sb) < 0) {
600 log_err("fio: job file stat: %s\n", strerror(errno));
605 p_size = sb.st_size + sizeof(*pdu);
606 pdu = malloc(p_size);
612 ret = read(fd, p, len);
621 else if (errno == EAGAIN || errno == EINTR)
626 log_err("fio: failed reading job file %s\n", filename);
632 pdu->buf_len = __cpu_to_le32(sb.st_size);
633 pdu->client_type = cpu_to_le32(client->type);
635 client->sent_job = 1;
636 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, pdu, p_size, NULL, NULL);
642 int fio_client_send_ini(struct fio_client *client, const char *filename)
646 ret = __fio_client_send_ini(client, filename);
648 client->sent_job = 1;
653 int fio_clients_send_ini(const char *filename)
655 struct fio_client *client;
656 struct flist_head *entry, *tmp;
658 flist_for_each_safe(entry, tmp, &client_list) {
659 client = flist_entry(entry, struct fio_client, list);
661 if (client->nr_ini_file) {
664 for (i = 0; i < client->nr_ini_file; i++) {
665 const char *ini = client->ini_file[i];
667 if (fio_client_send_ini(client, ini)) {
668 remove_client(client);
672 } else if (!filename || fio_client_send_ini(client, filename))
673 remove_client(client);
679 int fio_client_update_options(struct fio_client *client,
680 struct thread_options *o, uint64_t *tag)
682 struct cmd_add_job_pdu pdu;
684 pdu.thread_number = cpu_to_le32(client->thread_number);
685 pdu.groupid = cpu_to_le32(client->groupid);
686 convert_thread_options_to_net(&pdu.top, o);
688 return fio_net_send_cmd(client->fd, FIO_NET_CMD_UPDATE_JOB, &pdu, sizeof(pdu), tag, &client->cmd_list);
691 static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
693 dst->max_val = le64_to_cpu(src->max_val);
694 dst->min_val = le64_to_cpu(src->min_val);
695 dst->samples = le64_to_cpu(src->samples);
698 * Floats arrive as IEEE 754 encoded uint64_t, convert back to double
700 dst->mean.u.f = fio_uint64_to_double(le64_to_cpu(dst->mean.u.i));
701 dst->S.u.f = fio_uint64_to_double(le64_to_cpu(dst->S.u.i));
704 static void convert_ts(struct thread_stat *dst, struct thread_stat *src)
708 dst->error = le32_to_cpu(src->error);
709 dst->thread_number = le32_to_cpu(src->thread_number);
710 dst->groupid = le32_to_cpu(src->groupid);
711 dst->pid = le32_to_cpu(src->pid);
712 dst->members = le32_to_cpu(src->members);
713 dst->unified_rw_rep = le32_to_cpu(src->unified_rw_rep);
715 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
716 convert_io_stat(&dst->clat_stat[i], &src->clat_stat[i]);
717 convert_io_stat(&dst->slat_stat[i], &src->slat_stat[i]);
718 convert_io_stat(&dst->lat_stat[i], &src->lat_stat[i]);
719 convert_io_stat(&dst->bw_stat[i], &src->bw_stat[i]);
722 dst->usr_time = le64_to_cpu(src->usr_time);
723 dst->sys_time = le64_to_cpu(src->sys_time);
724 dst->ctx = le64_to_cpu(src->ctx);
725 dst->minf = le64_to_cpu(src->minf);
726 dst->majf = le64_to_cpu(src->majf);
727 dst->clat_percentiles = le64_to_cpu(src->clat_percentiles);
729 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
730 fio_fp64_t *fps = &src->percentile_list[i];
731 fio_fp64_t *fpd = &dst->percentile_list[i];
733 fpd->u.f = fio_uint64_to_double(le64_to_cpu(fps->u.i));
736 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
737 dst->io_u_map[i] = le32_to_cpu(src->io_u_map[i]);
738 dst->io_u_submit[i] = le32_to_cpu(src->io_u_submit[i]);
739 dst->io_u_complete[i] = le32_to_cpu(src->io_u_complete[i]);
742 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
743 dst->io_u_lat_u[i] = le32_to_cpu(src->io_u_lat_u[i]);
744 dst->io_u_lat_m[i] = le32_to_cpu(src->io_u_lat_m[i]);
747 for (i = 0; i < DDIR_RWDIR_CNT; i++)
748 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
749 dst->io_u_plat[i][j] = le32_to_cpu(src->io_u_plat[i][j]);
751 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
752 dst->total_io_u[i] = le64_to_cpu(src->total_io_u[i]);
753 dst->short_io_u[i] = le64_to_cpu(src->short_io_u[i]);
756 dst->total_submit = le64_to_cpu(src->total_submit);
757 dst->total_complete = le64_to_cpu(src->total_complete);
759 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
760 dst->io_bytes[i] = le64_to_cpu(src->io_bytes[i]);
761 dst->runtime[i] = le64_to_cpu(src->runtime[i]);
764 dst->total_run_time = le64_to_cpu(src->total_run_time);
765 dst->continue_on_error = le16_to_cpu(src->continue_on_error);
766 dst->total_err_count = le64_to_cpu(src->total_err_count);
767 dst->first_error = le32_to_cpu(src->first_error);
768 dst->kb_base = le32_to_cpu(src->kb_base);
769 dst->unit_base = le32_to_cpu(src->unit_base);
772 static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
776 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
777 dst->max_run[i] = le64_to_cpu(src->max_run[i]);
778 dst->min_run[i] = le64_to_cpu(src->min_run[i]);
779 dst->max_bw[i] = le64_to_cpu(src->max_bw[i]);
780 dst->min_bw[i] = le64_to_cpu(src->min_bw[i]);
781 dst->io_kb[i] = le64_to_cpu(src->io_kb[i]);
782 dst->agg[i] = le64_to_cpu(src->agg[i]);
785 dst->kb_base = le32_to_cpu(src->kb_base);
786 dst->unit_base = le32_to_cpu(src->unit_base);
787 dst->groupid = le32_to_cpu(src->groupid);
788 dst->unified_rw_rep = le32_to_cpu(src->unified_rw_rep);
791 static void handle_ts(struct fio_client *client, struct fio_net_cmd *cmd)
793 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
795 show_thread_status(&p->ts, &p->rs);
796 client->did_stat = 1;
798 if (!do_output_all_clients)
801 sum_thread_stats(&client_ts, &p->ts, sum_stat_nr);
802 sum_group_stats(&client_gs, &p->rs);
805 client_ts.thread_number = p->ts.thread_number;
806 client_ts.groupid = p->ts.groupid;
807 client_ts.unified_rw_rep = p->ts.unified_rw_rep;
809 if (++sum_stat_nr == sum_stat_clients) {
810 strcpy(client_ts.name, "All clients");
811 show_thread_status(&client_ts, &client_gs);
815 static void handle_gs(struct fio_client *client, struct fio_net_cmd *cmd)
817 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
819 show_group_stats(gs);
822 static void handle_text(struct fio_client *client, struct fio_net_cmd *cmd)
824 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
825 const char *buf = (const char *) pdu->buf;
829 name = client->name ? client->name : client->hostname;
831 if (!client->skip_newline)
832 fprintf(f_out, "<%s> ", name);
833 ret = fwrite(buf, pdu->buf_len, 1, f_out);
835 client->skip_newline = strchr(buf, '\n') == NULL;
838 static void convert_agg(struct disk_util_agg *agg)
842 for (i = 0; i < 2; i++) {
843 agg->ios[i] = le32_to_cpu(agg->ios[i]);
844 agg->merges[i] = le32_to_cpu(agg->merges[i]);
845 agg->sectors[i] = le64_to_cpu(agg->sectors[i]);
846 agg->ticks[i] = le32_to_cpu(agg->ticks[i]);
849 agg->io_ticks = le32_to_cpu(agg->io_ticks);
850 agg->time_in_queue = le32_to_cpu(agg->time_in_queue);
851 agg->slavecount = le32_to_cpu(agg->slavecount);
852 agg->max_util.u.f = fio_uint64_to_double(__le64_to_cpu(agg->max_util.u.i));
855 static void convert_dus(struct disk_util_stat *dus)
859 for (i = 0; i < 2; i++) {
860 dus->ios[i] = le32_to_cpu(dus->ios[i]);
861 dus->merges[i] = le32_to_cpu(dus->merges[i]);
862 dus->sectors[i] = le64_to_cpu(dus->sectors[i]);
863 dus->ticks[i] = le32_to_cpu(dus->ticks[i]);
866 dus->io_ticks = le32_to_cpu(dus->io_ticks);
867 dus->time_in_queue = le32_to_cpu(dus->time_in_queue);
868 dus->msec = le64_to_cpu(dus->msec);
871 static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd)
873 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
875 if (!client->disk_stats_shown) {
876 client->disk_stats_shown = 1;
877 log_info("\nDisk stats (read/write):\n");
880 print_disk_util(&du->dus, &du->agg, output_format == FIO_OUTPUT_TERSE);
883 static void convert_jobs_eta(struct jobs_eta *je)
887 je->nr_running = le32_to_cpu(je->nr_running);
888 je->nr_ramp = le32_to_cpu(je->nr_ramp);
889 je->nr_pending = le32_to_cpu(je->nr_pending);
890 je->files_open = le32_to_cpu(je->files_open);
892 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
893 je->m_rate[i] = le32_to_cpu(je->m_rate[i]);
894 je->t_rate[i] = le32_to_cpu(je->t_rate[i]);
895 je->m_iops[i] = le32_to_cpu(je->m_iops[i]);
896 je->t_iops[i] = le32_to_cpu(je->t_iops[i]);
897 je->rate[i] = le32_to_cpu(je->rate[i]);
898 je->iops[i] = le32_to_cpu(je->iops[i]);
901 je->elapsed_sec = le64_to_cpu(je->elapsed_sec);
902 je->eta_sec = le64_to_cpu(je->eta_sec);
903 je->nr_threads = le32_to_cpu(je->nr_threads);
904 je->is_pow2 = le32_to_cpu(je->is_pow2);
905 je->unit_base = le32_to_cpu(je->unit_base);
908 void fio_client_sum_jobs_eta(struct jobs_eta *dst, struct jobs_eta *je)
912 dst->nr_running += je->nr_running;
913 dst->nr_ramp += je->nr_ramp;
914 dst->nr_pending += je->nr_pending;
915 dst->files_open += je->files_open;
917 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
918 dst->m_rate[i] += je->m_rate[i];
919 dst->t_rate[i] += je->t_rate[i];
920 dst->m_iops[i] += je->m_iops[i];
921 dst->t_iops[i] += je->t_iops[i];
922 dst->rate[i] += je->rate[i];
923 dst->iops[i] += je->iops[i];
926 dst->elapsed_sec += je->elapsed_sec;
928 if (je->eta_sec > dst->eta_sec)
929 dst->eta_sec = je->eta_sec;
931 dst->nr_threads += je->nr_threads;
932 /* we need to handle je->run_str too ... */
935 void fio_client_dec_jobs_eta(struct client_eta *eta, client_eta_op eta_fn)
937 if (!--eta->pending) {
943 static void remove_reply_cmd(struct fio_client *client, struct fio_net_cmd *cmd)
945 struct fio_net_cmd_reply *reply = NULL;
946 struct flist_head *entry;
948 flist_for_each(entry, &client->cmd_list) {
949 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
951 if (cmd->tag == (uintptr_t) reply)
958 log_err("fio: client: unable to find matching tag (%lx)\n", cmd->tag);
962 flist_del(&reply->list);
963 cmd->tag = reply->saved_tag;
967 int fio_client_wait_for_reply(struct fio_client *client, uint64_t tag)
970 struct fio_net_cmd_reply *reply = NULL;
971 struct flist_head *entry;
973 flist_for_each(entry, &client->cmd_list) {
974 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
976 if (tag == (uintptr_t) reply)
991 static void handle_eta(struct fio_client *client, struct fio_net_cmd *cmd)
993 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
994 struct client_eta *eta = (struct client_eta *) (uintptr_t) cmd->tag;
996 dprint(FD_NET, "client: got eta tag %p, %d\n", eta, eta->pending);
998 assert(client->eta_in_flight == eta);
1000 client->eta_in_flight = NULL;
1001 flist_del_init(&client->eta_list);
1003 if (client->ops->jobs_eta)
1004 client->ops->jobs_eta(client, je);
1006 fio_client_sum_jobs_eta(&eta->eta, je);
1007 fio_client_dec_jobs_eta(eta, client->ops->eta);
1010 static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd)
1012 struct cmd_probe_reply_pdu *probe = (struct cmd_probe_reply_pdu *) cmd->payload;
1013 const char *os, *arch;
1016 os = fio_get_os_string(probe->os);
1020 arch = fio_get_arch_string(probe->arch);
1024 sprintf(bit, "%d-bit", probe->bpp * 8);
1025 probe->flags = le64_to_cpu(probe->flags);
1027 log_info("hostname=%s, be=%u, %s, os=%s, arch=%s, fio=%s, flags=%lx\n",
1028 probe->hostname, probe->bigendian, bit, os, arch,
1029 probe->fio_version, (unsigned long) probe->flags);
1032 client->name = strdup((char *) probe->hostname);
1035 static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd)
1037 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
1039 client->state = Client_started;
1040 client->jobs = le32_to_cpu(pdu->jobs);
1041 client->nr_stat = le32_to_cpu(pdu->stat_outputs);
1043 if (sum_stat_clients > 1)
1044 do_output_all_clients = 1;
1046 sum_stat_clients += client->nr_stat;
1049 static void handle_stop(struct fio_client *client, struct fio_net_cmd *cmd)
1052 log_info("client <%s>: exited with error %d\n", client->hostname, client->error);
1055 static void convert_stop(struct fio_net_cmd *cmd)
1057 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
1059 pdu->error = le32_to_cpu(pdu->error);
1062 static void convert_text(struct fio_net_cmd *cmd)
1064 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
1066 pdu->level = le32_to_cpu(pdu->level);
1067 pdu->buf_len = le32_to_cpu(pdu->buf_len);
1068 pdu->log_sec = le64_to_cpu(pdu->log_sec);
1069 pdu->log_usec = le64_to_cpu(pdu->log_usec);
1072 static struct cmd_iolog_pdu *convert_iolog_gz(struct fio_net_cmd *cmd,
1073 struct cmd_iolog_pdu *pdu)
1076 struct cmd_iolog_pdu *ret;
1078 uint32_t nr_samples;
1082 stream.zalloc = Z_NULL;
1083 stream.zfree = Z_NULL;
1084 stream.opaque = Z_NULL;
1085 stream.avail_in = 0;
1086 stream.next_in = Z_NULL;
1088 if (inflateInit(&stream) != Z_OK)
1092 * Get header first, it's not compressed
1094 nr_samples = le32_to_cpu(pdu->nr_samples);
1096 total = nr_samples * sizeof(struct io_sample);
1097 ret = malloc(total + sizeof(*pdu));
1098 ret->nr_samples = nr_samples;
1100 memcpy(ret, pdu, sizeof(*pdu));
1102 p = (void *) ret + sizeof(*pdu);
1104 stream.avail_in = cmd->pdu_len - sizeof(*pdu);
1105 stream.next_in = (void *) pdu + sizeof(*pdu);
1106 while (stream.avail_in) {
1107 unsigned int this_chunk = 65536;
1108 unsigned int this_len;
1111 if (this_chunk > total)
1114 stream.avail_out = this_chunk;
1115 stream.next_out = p;
1116 err = inflate(&stream, Z_NO_FLUSH);
1117 /* may be Z_OK, or Z_STREAM_END */
1119 log_err("fio: inflate error %d\n", err);
1125 this_len = this_chunk - stream.avail_out;
1131 inflateEnd(&stream);
1139 * This has been compressed on the server side, since it can be big.
1142 static struct cmd_iolog_pdu *convert_iolog(struct fio_net_cmd *cmd)
1144 struct cmd_iolog_pdu *pdu = (struct cmd_iolog_pdu *) cmd->payload;
1145 struct cmd_iolog_pdu *ret;
1149 * Convert if compressed and we support it. If it's not
1150 * compressed, we need not do anything.
1152 if (le32_to_cpu(pdu->compressed)) {
1154 log_err("fio: server sent compressed data by mistake\n");
1157 ret = convert_iolog_gz(cmd, pdu);
1159 log_err("fio: failed decompressing log\n");
1165 ret->thread_number = le32_to_cpu(ret->thread_number);
1166 ret->nr_samples = le32_to_cpu(ret->nr_samples);
1167 ret->log_type = le32_to_cpu(ret->log_type);
1168 ret->compressed = le32_to_cpu(ret->compressed);
1170 for (i = 0; i < ret->nr_samples; i++) {
1171 struct io_sample *s = &ret->samples[i];
1173 s->time = le64_to_cpu(s->time);
1174 s->val = le64_to_cpu(s->val);
1175 s->ddir = le32_to_cpu(s->ddir);
1176 s->bs = le32_to_cpu(s->bs);
1182 int fio_handle_client(struct fio_client *client)
1184 struct client_ops *ops = client->ops;
1185 struct fio_net_cmd *cmd;
1187 dprint(FD_NET, "client: handle %s\n", client->hostname);
1189 cmd = fio_net_recv_cmd(client->fd);
1193 dprint(FD_NET, "client: got cmd op %s from %s (pdu=%u)\n",
1194 fio_server_op(cmd->opcode), client->hostname, cmd->pdu_len);
1196 switch (cmd->opcode) {
1197 case FIO_NET_CMD_QUIT:
1199 ops->quit(client, cmd);
1200 remove_client(client);
1203 case FIO_NET_CMD_TEXT:
1205 ops->text(client, cmd);
1208 case FIO_NET_CMD_DU: {
1209 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
1211 convert_dus(&du->dus);
1212 convert_agg(&du->agg);
1214 ops->disk_util(client, cmd);
1218 case FIO_NET_CMD_TS: {
1219 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
1221 convert_ts(&p->ts, &p->ts);
1222 convert_gs(&p->rs, &p->rs);
1224 ops->thread_status(client, cmd);
1228 case FIO_NET_CMD_GS: {
1229 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
1233 ops->group_stats(client, cmd);
1237 case FIO_NET_CMD_ETA: {
1238 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
1240 remove_reply_cmd(client, cmd);
1241 convert_jobs_eta(je);
1242 handle_eta(client, cmd);
1246 case FIO_NET_CMD_PROBE:
1247 remove_reply_cmd(client, cmd);
1248 ops->probe(client, cmd);
1251 case FIO_NET_CMD_SERVER_START:
1252 client->state = Client_running;
1254 ops->job_start(client, cmd);
1257 case FIO_NET_CMD_START: {
1258 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
1260 pdu->jobs = le32_to_cpu(pdu->jobs);
1261 ops->start(client, cmd);
1265 case FIO_NET_CMD_STOP: {
1266 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
1269 client->state = Client_stopped;
1270 client->error = le32_to_cpu(pdu->error);
1271 client->signal = le32_to_cpu(pdu->signal);
1272 ops->stop(client, cmd);
1276 case FIO_NET_CMD_ADD_JOB: {
1277 struct cmd_add_job_pdu *pdu = (struct cmd_add_job_pdu *) cmd->payload;
1279 client->thread_number = le32_to_cpu(pdu->thread_number);
1280 client->groupid = le32_to_cpu(pdu->groupid);
1283 ops->add_job(client, cmd);
1287 case FIO_NET_CMD_IOLOG:
1289 struct cmd_iolog_pdu *pdu;
1291 pdu = convert_iolog(cmd);
1292 ops->iolog(client, pdu);
1296 case FIO_NET_CMD_UPDATE_JOB:
1297 ops->update_job(client, cmd);
1298 remove_reply_cmd(client, cmd);
1302 log_err("fio: unknown client op: %s\n", fio_server_op(cmd->opcode));
1310 static void request_client_etas(struct client_ops *ops)
1312 struct fio_client *client;
1313 struct flist_head *entry;
1314 struct client_eta *eta;
1317 dprint(FD_NET, "client: request eta (%d)\n", nr_clients);
1319 eta = malloc(sizeof(*eta));
1320 memset(&eta->eta, 0, sizeof(eta->eta));
1321 eta->pending = nr_clients;
1323 flist_for_each(entry, &client_list) {
1324 client = flist_entry(entry, struct fio_client, list);
1326 if (!flist_empty(&client->eta_list)) {
1330 if (client->state != Client_running)
1333 assert(!client->eta_in_flight);
1334 flist_add_tail(&client->eta_list, &eta_list);
1335 client->eta_in_flight = eta;
1336 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_SEND_ETA,
1337 (uintptr_t) eta, &client->cmd_list);
1341 fio_client_dec_jobs_eta(eta, ops->eta);
1343 dprint(FD_NET, "client: requested eta tag %p\n", eta);
1346 static int client_check_cmd_timeout(struct fio_client *client,
1347 struct timeval *now)
1349 struct fio_net_cmd_reply *reply;
1350 struct flist_head *entry, *tmp;
1353 flist_for_each_safe(entry, tmp, &client->cmd_list) {
1354 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
1356 if (mtime_since(&reply->tv, now) < FIO_NET_CLIENT_TIMEOUT)
1359 log_err("fio: client %s, timeout on cmd %s\n", client->hostname,
1360 fio_server_op(reply->opcode));
1361 flist_del(&reply->list);
1366 return flist_empty(&client->cmd_list) && ret;
1369 static int fio_check_clients_timed_out(void)
1371 struct fio_client *client;
1372 struct flist_head *entry, *tmp;
1376 fio_gettime(&tv, NULL);
1378 flist_for_each_safe(entry, tmp, &client_list) {
1379 client = flist_entry(entry, struct fio_client, list);
1381 if (flist_empty(&client->cmd_list))
1384 if (!client_check_cmd_timeout(client, &tv))
1387 if (client->ops->timed_out)
1388 client->ops->timed_out(client);
1390 log_err("fio: client %s timed out\n", client->hostname);
1392 remove_client(client);
1399 int fio_handle_clients(struct client_ops *ops)
1401 struct pollfd *pfds;
1402 int i, ret = 0, retval = 0;
1404 fio_gettime(&eta_tv, NULL);
1406 pfds = malloc(nr_clients * sizeof(struct pollfd));
1408 init_thread_stat(&client_ts);
1409 init_group_run_stat(&client_gs);
1411 while (!exit_backend && nr_clients) {
1412 struct flist_head *entry, *tmp;
1413 struct fio_client *client;
1416 flist_for_each_safe(entry, tmp, &client_list) {
1417 client = flist_entry(entry, struct fio_client, list);
1419 if (!client->sent_job && !client->ops->stay_connected &&
1420 flist_empty(&client->cmd_list)) {
1421 remove_client(client);
1425 pfds[i].fd = client->fd;
1426 pfds[i].events = POLLIN;
1433 assert(i == nr_clients);
1438 fio_gettime(&tv, NULL);
1439 if (mtime_since(&eta_tv, &tv) >= 900) {
1440 request_client_etas(ops);
1441 memcpy(&eta_tv, &tv, sizeof(tv));
1443 if (fio_check_clients_timed_out())
1447 ret = poll(pfds, nr_clients, ops->eta_msec);
1451 log_err("fio: poll clients: %s\n", strerror(errno));
1457 for (i = 0; i < nr_clients; i++) {
1458 if (!(pfds[i].revents & POLLIN))
1461 client = find_client_by_fd(pfds[i].fd);
1463 log_err("fio: unknown client fd %d\n", pfds[i].fd);
1466 if (!fio_handle_client(client)) {
1467 log_info("client: host=%s disconnected\n",
1469 remove_client(client);
1471 } else if (client->error)
1473 fio_put_client(client);