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->nr_setting_up = le32_to_cpu(je->nr_setting_up);
891 je->files_open = le32_to_cpu(je->files_open);
893 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
894 je->m_rate[i] = le32_to_cpu(je->m_rate[i]);
895 je->t_rate[i] = le32_to_cpu(je->t_rate[i]);
896 je->m_iops[i] = le32_to_cpu(je->m_iops[i]);
897 je->t_iops[i] = le32_to_cpu(je->t_iops[i]);
898 je->rate[i] = le32_to_cpu(je->rate[i]);
899 je->iops[i] = le32_to_cpu(je->iops[i]);
902 je->elapsed_sec = le64_to_cpu(je->elapsed_sec);
903 je->eta_sec = le64_to_cpu(je->eta_sec);
904 je->nr_threads = le32_to_cpu(je->nr_threads);
905 je->is_pow2 = le32_to_cpu(je->is_pow2);
906 je->unit_base = le32_to_cpu(je->unit_base);
909 void fio_client_sum_jobs_eta(struct jobs_eta *dst, struct jobs_eta *je)
913 dst->nr_running += je->nr_running;
914 dst->nr_ramp += je->nr_ramp;
915 dst->nr_pending += je->nr_pending;
916 dst->nr_setting_up += je->nr_setting_up;
917 dst->files_open += je->files_open;
919 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
920 dst->m_rate[i] += je->m_rate[i];
921 dst->t_rate[i] += je->t_rate[i];
922 dst->m_iops[i] += je->m_iops[i];
923 dst->t_iops[i] += je->t_iops[i];
924 dst->rate[i] += je->rate[i];
925 dst->iops[i] += je->iops[i];
928 dst->elapsed_sec += je->elapsed_sec;
930 if (je->eta_sec > dst->eta_sec)
931 dst->eta_sec = je->eta_sec;
933 dst->nr_threads += je->nr_threads;
934 /* we need to handle je->run_str too ... */
937 void fio_client_dec_jobs_eta(struct client_eta *eta, client_eta_op eta_fn)
939 if (!--eta->pending) {
945 static void remove_reply_cmd(struct fio_client *client, struct fio_net_cmd *cmd)
947 struct fio_net_cmd_reply *reply = NULL;
948 struct flist_head *entry;
950 flist_for_each(entry, &client->cmd_list) {
951 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
953 if (cmd->tag == (uintptr_t) reply)
960 log_err("fio: client: unable to find matching tag (%llx)\n", (unsigned long long) cmd->tag);
964 flist_del(&reply->list);
965 cmd->tag = reply->saved_tag;
969 int fio_client_wait_for_reply(struct fio_client *client, uint64_t tag)
972 struct fio_net_cmd_reply *reply = NULL;
973 struct flist_head *entry;
975 flist_for_each(entry, &client->cmd_list) {
976 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
978 if (tag == (uintptr_t) reply)
993 static void handle_eta(struct fio_client *client, struct fio_net_cmd *cmd)
995 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
996 struct client_eta *eta = (struct client_eta *) (uintptr_t) cmd->tag;
998 dprint(FD_NET, "client: got eta tag %p, %d\n", eta, eta->pending);
1000 assert(client->eta_in_flight == eta);
1002 client->eta_in_flight = NULL;
1003 flist_del_init(&client->eta_list);
1005 if (client->ops->jobs_eta)
1006 client->ops->jobs_eta(client, je);
1008 fio_client_sum_jobs_eta(&eta->eta, je);
1009 fio_client_dec_jobs_eta(eta, client->ops->eta);
1012 static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd)
1014 struct cmd_probe_reply_pdu *probe = (struct cmd_probe_reply_pdu *) cmd->payload;
1015 const char *os, *arch;
1018 os = fio_get_os_string(probe->os);
1022 arch = fio_get_arch_string(probe->arch);
1026 sprintf(bit, "%d-bit", probe->bpp * 8);
1027 probe->flags = le64_to_cpu(probe->flags);
1029 log_info("hostname=%s, be=%u, %s, os=%s, arch=%s, fio=%s, flags=%lx\n",
1030 probe->hostname, probe->bigendian, bit, os, arch,
1031 probe->fio_version, (unsigned long) probe->flags);
1034 client->name = strdup((char *) probe->hostname);
1037 static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd)
1039 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
1041 client->state = Client_started;
1042 client->jobs = le32_to_cpu(pdu->jobs);
1043 client->nr_stat = le32_to_cpu(pdu->stat_outputs);
1045 if (sum_stat_clients > 1)
1046 do_output_all_clients = 1;
1048 sum_stat_clients += client->nr_stat;
1051 static void handle_stop(struct fio_client *client, struct fio_net_cmd *cmd)
1054 log_info("client <%s>: exited with error %d\n", client->hostname, client->error);
1057 static void convert_stop(struct fio_net_cmd *cmd)
1059 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
1061 pdu->error = le32_to_cpu(pdu->error);
1064 static void convert_text(struct fio_net_cmd *cmd)
1066 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
1068 pdu->level = le32_to_cpu(pdu->level);
1069 pdu->buf_len = le32_to_cpu(pdu->buf_len);
1070 pdu->log_sec = le64_to_cpu(pdu->log_sec);
1071 pdu->log_usec = le64_to_cpu(pdu->log_usec);
1074 static struct cmd_iolog_pdu *convert_iolog_gz(struct fio_net_cmd *cmd,
1075 struct cmd_iolog_pdu *pdu)
1078 struct cmd_iolog_pdu *ret;
1080 uint32_t nr_samples;
1084 stream.zalloc = Z_NULL;
1085 stream.zfree = Z_NULL;
1086 stream.opaque = Z_NULL;
1087 stream.avail_in = 0;
1088 stream.next_in = Z_NULL;
1090 if (inflateInit(&stream) != Z_OK)
1094 * Get header first, it's not compressed
1096 nr_samples = le32_to_cpu(pdu->nr_samples);
1098 total = nr_samples * sizeof(struct io_sample);
1099 ret = malloc(total + sizeof(*pdu));
1100 ret->nr_samples = nr_samples;
1102 memcpy(ret, pdu, sizeof(*pdu));
1104 p = (void *) ret + sizeof(*pdu);
1106 stream.avail_in = cmd->pdu_len - sizeof(*pdu);
1107 stream.next_in = (void *) pdu + sizeof(*pdu);
1108 while (stream.avail_in) {
1109 unsigned int this_chunk = 65536;
1110 unsigned int this_len;
1113 if (this_chunk > total)
1116 stream.avail_out = this_chunk;
1117 stream.next_out = p;
1118 err = inflate(&stream, Z_NO_FLUSH);
1119 /* may be Z_OK, or Z_STREAM_END */
1121 log_err("fio: inflate error %d\n", err);
1127 this_len = this_chunk - stream.avail_out;
1133 inflateEnd(&stream);
1141 * This has been compressed on the server side, since it can be big.
1144 static struct cmd_iolog_pdu *convert_iolog(struct fio_net_cmd *cmd)
1146 struct cmd_iolog_pdu *pdu = (struct cmd_iolog_pdu *) cmd->payload;
1147 struct cmd_iolog_pdu *ret;
1151 * Convert if compressed and we support it. If it's not
1152 * compressed, we need not do anything.
1154 if (le32_to_cpu(pdu->compressed)) {
1156 log_err("fio: server sent compressed data by mistake\n");
1159 ret = convert_iolog_gz(cmd, pdu);
1161 log_err("fio: failed decompressing log\n");
1167 ret->thread_number = le32_to_cpu(ret->thread_number);
1168 ret->nr_samples = le32_to_cpu(ret->nr_samples);
1169 ret->log_type = le32_to_cpu(ret->log_type);
1170 ret->compressed = le32_to_cpu(ret->compressed);
1172 for (i = 0; i < ret->nr_samples; i++) {
1173 struct io_sample *s = &ret->samples[i];
1175 s->time = le64_to_cpu(s->time);
1176 s->val = le64_to_cpu(s->val);
1177 s->ddir = le32_to_cpu(s->ddir);
1178 s->bs = le32_to_cpu(s->bs);
1184 int fio_handle_client(struct fio_client *client)
1186 struct client_ops *ops = client->ops;
1187 struct fio_net_cmd *cmd;
1189 dprint(FD_NET, "client: handle %s\n", client->hostname);
1191 cmd = fio_net_recv_cmd(client->fd);
1195 dprint(FD_NET, "client: got cmd op %s from %s (pdu=%u)\n",
1196 fio_server_op(cmd->opcode), client->hostname, cmd->pdu_len);
1198 switch (cmd->opcode) {
1199 case FIO_NET_CMD_QUIT:
1201 ops->quit(client, cmd);
1202 remove_client(client);
1205 case FIO_NET_CMD_TEXT:
1207 ops->text(client, cmd);
1210 case FIO_NET_CMD_DU: {
1211 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
1213 convert_dus(&du->dus);
1214 convert_agg(&du->agg);
1216 ops->disk_util(client, cmd);
1220 case FIO_NET_CMD_TS: {
1221 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
1223 convert_ts(&p->ts, &p->ts);
1224 convert_gs(&p->rs, &p->rs);
1226 ops->thread_status(client, cmd);
1230 case FIO_NET_CMD_GS: {
1231 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
1235 ops->group_stats(client, cmd);
1239 case FIO_NET_CMD_ETA: {
1240 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
1242 remove_reply_cmd(client, cmd);
1243 convert_jobs_eta(je);
1244 handle_eta(client, cmd);
1248 case FIO_NET_CMD_PROBE:
1249 remove_reply_cmd(client, cmd);
1250 ops->probe(client, cmd);
1253 case FIO_NET_CMD_SERVER_START:
1254 client->state = Client_running;
1256 ops->job_start(client, cmd);
1259 case FIO_NET_CMD_START: {
1260 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
1262 pdu->jobs = le32_to_cpu(pdu->jobs);
1263 ops->start(client, cmd);
1267 case FIO_NET_CMD_STOP: {
1268 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
1271 client->state = Client_stopped;
1272 client->error = le32_to_cpu(pdu->error);
1273 client->signal = le32_to_cpu(pdu->signal);
1274 ops->stop(client, cmd);
1278 case FIO_NET_CMD_ADD_JOB: {
1279 struct cmd_add_job_pdu *pdu = (struct cmd_add_job_pdu *) cmd->payload;
1281 client->thread_number = le32_to_cpu(pdu->thread_number);
1282 client->groupid = le32_to_cpu(pdu->groupid);
1285 ops->add_job(client, cmd);
1289 case FIO_NET_CMD_IOLOG:
1291 struct cmd_iolog_pdu *pdu;
1293 pdu = convert_iolog(cmd);
1294 ops->iolog(client, pdu);
1298 case FIO_NET_CMD_UPDATE_JOB:
1299 ops->update_job(client, cmd);
1300 remove_reply_cmd(client, cmd);
1304 log_err("fio: unknown client op: %s\n", fio_server_op(cmd->opcode));
1312 static void request_client_etas(struct client_ops *ops)
1314 struct fio_client *client;
1315 struct flist_head *entry;
1316 struct client_eta *eta;
1319 dprint(FD_NET, "client: request eta (%d)\n", nr_clients);
1321 eta = malloc(sizeof(*eta));
1322 memset(&eta->eta, 0, sizeof(eta->eta));
1323 eta->pending = nr_clients;
1325 flist_for_each(entry, &client_list) {
1326 client = flist_entry(entry, struct fio_client, list);
1328 if (!flist_empty(&client->eta_list)) {
1332 if (client->state != Client_running)
1335 assert(!client->eta_in_flight);
1336 flist_add_tail(&client->eta_list, &eta_list);
1337 client->eta_in_flight = eta;
1338 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_SEND_ETA,
1339 (uintptr_t) eta, &client->cmd_list);
1343 fio_client_dec_jobs_eta(eta, ops->eta);
1345 dprint(FD_NET, "client: requested eta tag %p\n", eta);
1348 static int client_check_cmd_timeout(struct fio_client *client,
1349 struct timeval *now)
1351 struct fio_net_cmd_reply *reply;
1352 struct flist_head *entry, *tmp;
1355 flist_for_each_safe(entry, tmp, &client->cmd_list) {
1356 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
1358 if (mtime_since(&reply->tv, now) < FIO_NET_CLIENT_TIMEOUT)
1361 log_err("fio: client %s, timeout on cmd %s\n", client->hostname,
1362 fio_server_op(reply->opcode));
1363 flist_del(&reply->list);
1368 return flist_empty(&client->cmd_list) && ret;
1371 static int fio_check_clients_timed_out(void)
1373 struct fio_client *client;
1374 struct flist_head *entry, *tmp;
1378 fio_gettime(&tv, NULL);
1380 flist_for_each_safe(entry, tmp, &client_list) {
1381 client = flist_entry(entry, struct fio_client, list);
1383 if (flist_empty(&client->cmd_list))
1386 if (!client_check_cmd_timeout(client, &tv))
1389 if (client->ops->timed_out)
1390 client->ops->timed_out(client);
1392 log_err("fio: client %s timed out\n", client->hostname);
1394 remove_client(client);
1401 int fio_handle_clients(struct client_ops *ops)
1403 struct pollfd *pfds;
1404 int i, ret = 0, retval = 0;
1406 fio_gettime(&eta_tv, NULL);
1408 pfds = malloc(nr_clients * sizeof(struct pollfd));
1410 init_thread_stat(&client_ts);
1411 init_group_run_stat(&client_gs);
1413 while (!exit_backend && nr_clients) {
1414 struct flist_head *entry, *tmp;
1415 struct fio_client *client;
1418 flist_for_each_safe(entry, tmp, &client_list) {
1419 client = flist_entry(entry, struct fio_client, list);
1421 if (!client->sent_job && !client->ops->stay_connected &&
1422 flist_empty(&client->cmd_list)) {
1423 remove_client(client);
1427 pfds[i].fd = client->fd;
1428 pfds[i].events = POLLIN;
1435 assert(i == nr_clients);
1440 fio_gettime(&tv, NULL);
1441 if (mtime_since(&eta_tv, &tv) >= 900) {
1442 request_client_etas(ops);
1443 memcpy(&eta_tv, &tv, sizeof(tv));
1445 if (fio_check_clients_timed_out())
1449 ret = poll(pfds, nr_clients, ops->eta_msec);
1453 log_err("fio: poll clients: %s\n", strerror(errno));
1459 for (i = 0; i < nr_clients; i++) {
1460 if (!(pfds[i].revents & POLLIN))
1463 client = find_client_by_fd(pfds[i].fd);
1465 log_err("fio: unknown client fd %d\n", pfds[i].fd);
1468 if (!fio_handle_client(client)) {
1469 log_info("client: host=%s disconnected\n",
1471 remove_client(client);
1473 } else if (client->error)
1475 fio_put_client(client);