11 #include <sys/socket.h>
13 #include <netinet/in.h>
14 #include <arpa/inet.h>
25 static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd);
26 static void handle_ts(struct fio_client *client, struct fio_net_cmd *cmd);
27 static void handle_gs(struct fio_client *client, struct fio_net_cmd *cmd);
28 static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd);
29 static void handle_text(struct fio_client *client, struct fio_net_cmd *cmd);
30 static void handle_stop(struct fio_client *client, struct fio_net_cmd *cmd);
31 static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd);
33 struct client_ops fio_client_ops = {
35 .disk_util = handle_du,
36 .thread_status = handle_ts,
37 .group_stats = handle_gs,
39 .start = handle_start,
40 .eta = display_thread_status,
41 .probe = handle_probe,
42 .eta_msec = FIO_CLIENT_DEF_ETA_MSEC,
43 .client_type = FIO_CLIENT_TYPE_CLI,
46 static struct timeval eta_tv;
48 static FLIST_HEAD(client_list);
49 static FLIST_HEAD(eta_list);
51 static FLIST_HEAD(arg_list);
53 struct thread_stat client_ts;
54 struct group_run_stats client_gs;
57 static int sum_stat_nr;
59 #define FIO_CLIENT_HASH_BITS 7
60 #define FIO_CLIENT_HASH_SZ (1 << FIO_CLIENT_HASH_BITS)
61 #define FIO_CLIENT_HASH_MASK (FIO_CLIENT_HASH_SZ - 1)
62 static struct flist_head client_hash[FIO_CLIENT_HASH_SZ];
64 static void fio_client_add_hash(struct fio_client *client)
66 int bucket = hash_long(client->fd, FIO_CLIENT_HASH_BITS);
68 bucket &= FIO_CLIENT_HASH_MASK;
69 flist_add(&client->hash_list, &client_hash[bucket]);
72 static void fio_client_remove_hash(struct fio_client *client)
74 if (!flist_empty(&client->hash_list))
75 flist_del_init(&client->hash_list);
78 static void fio_init fio_client_hash_init(void)
82 for (i = 0; i < FIO_CLIENT_HASH_SZ; i++)
83 INIT_FLIST_HEAD(&client_hash[i]);
86 static struct fio_client *find_client_by_fd(int fd)
88 int bucket = hash_long(fd, FIO_CLIENT_HASH_BITS) & FIO_CLIENT_HASH_MASK;
89 struct fio_client *client;
90 struct flist_head *entry;
92 flist_for_each(entry, &client_hash[bucket]) {
93 client = flist_entry(entry, struct fio_client, hash_list);
95 if (client->fd == fd) {
104 void fio_put_client(struct fio_client *client)
109 free(client->hostname);
118 static void remove_client(struct fio_client *client)
120 assert(client->refs);
122 dprint(FD_NET, "client: removed <%s>\n", client->hostname);
124 if (!flist_empty(&client->list))
125 flist_del_init(&client->list);
127 fio_client_remove_hash(client);
129 if (!flist_empty(&client->eta_list)) {
130 flist_del_init(&client->eta_list);
131 fio_client_dec_jobs_eta(client->eta_in_flight, client->ops->eta);
137 if (client->ops->removed)
138 client->ops->removed(client);
143 fio_put_client(client);
146 struct fio_client *fio_get_client(struct fio_client *client)
152 static void __fio_client_add_cmd_option(struct fio_client *client,
157 index = client->argc++;
158 client->argv = realloc(client->argv, sizeof(char *) * client->argc);
159 client->argv[index] = strdup(opt);
160 dprint(FD_NET, "client: add cmd %d: %s\n", index, opt);
163 void fio_client_add_cmd_option(void *cookie, const char *opt)
165 struct fio_client *client = cookie;
166 struct flist_head *entry;
171 __fio_client_add_cmd_option(client, opt);
174 * Duplicate arguments to shared client group
176 flist_for_each(entry, &arg_list) {
177 client = flist_entry(entry, struct fio_client, arg_list);
179 __fio_client_add_cmd_option(client, opt);
183 struct fio_client *fio_client_add_explicit(struct client_ops *ops,
184 const char *hostname, int type,
187 struct fio_client *client;
189 client = malloc(sizeof(*client));
190 memset(client, 0, sizeof(*client));
192 INIT_FLIST_HEAD(&client->list);
193 INIT_FLIST_HEAD(&client->hash_list);
194 INIT_FLIST_HEAD(&client->arg_list);
195 INIT_FLIST_HEAD(&client->eta_list);
196 INIT_FLIST_HEAD(&client->cmd_list);
198 client->hostname = strdup(hostname);
200 if (type == Fio_client_socket)
205 ipv6 = type == Fio_client_ipv6;
206 if (fio_server_parse_host(hostname, &ipv6,
207 &client->addr.sin_addr,
208 &client->addr6.sin6_addr))
217 client->type = ops->client_type;
219 __fio_client_add_cmd_option(client, "fio");
221 flist_add(&client->list, &client_list);
223 dprint(FD_NET, "client: added <%s>\n", client->hostname);
230 int fio_client_add(struct client_ops *ops, const char *hostname, void **cookie)
232 struct fio_client *existing = *cookie;
233 struct fio_client *client;
237 * We always add our "exec" name as the option, hence 1
240 if (existing->argc == 1)
241 flist_add_tail(&existing->arg_list, &arg_list);
243 while (!flist_empty(&arg_list))
244 flist_del_init(arg_list.next);
248 client = malloc(sizeof(*client));
249 memset(client, 0, sizeof(*client));
251 INIT_FLIST_HEAD(&client->list);
252 INIT_FLIST_HEAD(&client->hash_list);
253 INIT_FLIST_HEAD(&client->arg_list);
254 INIT_FLIST_HEAD(&client->eta_list);
255 INIT_FLIST_HEAD(&client->cmd_list);
257 if (fio_server_parse_string(hostname, &client->hostname,
258 &client->is_sock, &client->port,
259 &client->addr.sin_addr,
260 &client->addr6.sin6_addr,
267 client->type = ops->client_type;
269 __fio_client_add_cmd_option(client, "fio");
271 flist_add(&client->list, &client_list);
273 dprint(FD_NET, "client: added <%s>\n", client->hostname);
278 static void probe_client(struct fio_client *client)
280 dprint(FD_NET, "client: send probe\n");
282 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_PROBE, 0, &client->cmd_list);
285 static int fio_client_connect_ip(struct fio_client *client)
287 struct sockaddr *addr;
288 fio_socklen_t socklen;
292 client->addr6.sin6_family = AF_INET6;
293 client->addr6.sin6_port = htons(client->port);
295 addr = (struct sockaddr *) &client->addr6;
296 socklen = sizeof(client->addr6);
298 client->addr.sin_family = AF_INET;
299 client->addr.sin_port = htons(client->port);
301 addr = (struct sockaddr *) &client->addr;
302 socklen = sizeof(client->addr);
305 fd = socket(domain, SOCK_STREAM, 0);
309 log_err("fio: socket: %s\n", strerror(errno));
313 if (connect(fd, addr, socklen) < 0) {
316 log_err("fio: connect: %s\n", strerror(errno));
317 log_err("fio: failed to connect to %s:%u\n", client->hostname,
326 static int fio_client_connect_sock(struct fio_client *client)
328 struct sockaddr_un *addr = &client->addr_un;
332 memset(addr, 0, sizeof(*addr));
333 addr->sun_family = AF_UNIX;
334 strcpy(addr->sun_path, client->hostname);
336 fd = socket(AF_UNIX, SOCK_STREAM, 0);
340 log_err("fio: socket: %s\n", strerror(errno));
344 len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
345 if (connect(fd, (struct sockaddr *) addr, len) < 0) {
348 log_err("fio: connect; %s\n", strerror(errno));
356 int fio_client_connect(struct fio_client *client)
360 dprint(FD_NET, "client: connect to host %s\n", client->hostname);
363 fd = fio_client_connect_sock(client);
365 fd = fio_client_connect_ip(client);
367 dprint(FD_NET, "client: %s connected %d\n", client->hostname, fd);
373 fio_client_add_hash(client);
374 client->state = Client_connected;
376 probe_client(client);
380 int fio_client_terminate(struct fio_client *client)
382 return fio_net_send_quit(client->fd);
385 void fio_clients_terminate(void)
387 struct flist_head *entry;
388 struct fio_client *client;
390 dprint(FD_NET, "client: terminate clients\n");
392 flist_for_each(entry, &client_list) {
393 client = flist_entry(entry, struct fio_client, list);
394 fio_client_terminate(client);
398 static void sig_int(int sig)
400 dprint(FD_NET, "client: got signal %d\n", sig);
401 fio_clients_terminate();
404 static void sig_show_status(int sig)
406 show_running_run_stats();
409 static void client_signal_handler(void)
411 struct sigaction act;
413 memset(&act, 0, sizeof(act));
414 act.sa_handler = sig_int;
415 act.sa_flags = SA_RESTART;
416 sigaction(SIGINT, &act, NULL);
418 memset(&act, 0, sizeof(act));
419 act.sa_handler = sig_int;
420 act.sa_flags = SA_RESTART;
421 sigaction(SIGTERM, &act, NULL);
423 memset(&act, 0, sizeof(act));
424 act.sa_handler = sig_show_status;
425 act.sa_flags = SA_RESTART;
426 sigaction(SIGUSR1, &act, NULL);
429 static int send_client_cmd_line(struct fio_client *client)
431 struct cmd_single_line_pdu *cslp;
432 struct cmd_line_pdu *clp;
433 unsigned long offset;
439 dprint(FD_NET, "client: send cmdline %d\n", client->argc);
441 lens = malloc(client->argc * sizeof(unsigned int));
444 * Find out how much mem we need
446 for (i = 0, mem = 0; i < client->argc; i++) {
447 lens[i] = strlen(client->argv[i]) + 1;
452 * We need one cmd_line_pdu, and argc number of cmd_single_line_pdu
454 mem += sizeof(*clp) + (client->argc * sizeof(*cslp));
458 offset = sizeof(*clp);
460 for (i = 0; i < client->argc; i++) {
461 uint16_t arg_len = lens[i];
464 strcpy((char *) cslp->text, client->argv[i]);
465 cslp->len = cpu_to_le16(arg_len);
466 offset += sizeof(*cslp) + arg_len;
470 clp->lines = cpu_to_le16(client->argc);
471 clp->client_type = __cpu_to_le16(client->type);
472 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOBLINE, pdu, mem, NULL, NULL);
477 int fio_clients_connect(void)
479 struct fio_client *client;
480 struct flist_head *entry, *tmp;
485 WSAStartup(MAKEWORD(2, 2), &wsd);
488 dprint(FD_NET, "client: connect all\n");
490 client_signal_handler();
492 flist_for_each_safe(entry, tmp, &client_list) {
493 client = flist_entry(entry, struct fio_client, list);
495 ret = fio_client_connect(client);
497 remove_client(client);
501 if (client->argc > 1)
502 send_client_cmd_line(client);
508 int fio_start_client(struct fio_client *client)
510 dprint(FD_NET, "client: start %s\n", client->hostname);
511 return fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_RUN, 0, NULL);
514 int fio_start_all_clients(void)
516 struct fio_client *client;
517 struct flist_head *entry, *tmp;
520 dprint(FD_NET, "client: start all\n");
522 flist_for_each_safe(entry, tmp, &client_list) {
523 client = flist_entry(entry, struct fio_client, list);
525 ret = fio_start_client(client);
527 remove_client(client);
532 return flist_empty(&client_list);
536 * Send file contents to server backend. We could use sendfile(), but to remain
537 * more portable lets just read/write the darn thing.
539 static int __fio_client_send_ini(struct fio_client *client, const char *filename)
541 struct cmd_job_pdu *pdu;
549 dprint(FD_NET, "send ini %s to %s\n", filename, client->hostname);
551 fd = open(filename, O_RDONLY);
555 log_err("fio: job file <%s> open: %s\n", filename, strerror(errno));
559 if (fstat(fd, &sb) < 0) {
562 log_err("fio: job file stat: %s\n", strerror(errno));
567 p_size = sb.st_size + sizeof(*pdu);
568 pdu = malloc(p_size);
574 ret = read(fd, p, len);
583 else if (errno == EAGAIN || errno == EINTR)
588 log_err("fio: failed reading job file %s\n", filename);
594 pdu->buf_len = __cpu_to_le32(sb.st_size);
595 pdu->client_type = cpu_to_le32(client->type);
597 client->sent_job = 1;
598 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, pdu, p_size, NULL, NULL);
604 int fio_client_send_ini(struct fio_client *client, const char *filename)
608 ret = __fio_client_send_ini(client, filename);
610 client->sent_job = 1;
615 int fio_clients_send_ini(const char *filename)
617 struct fio_client *client;
618 struct flist_head *entry, *tmp;
620 flist_for_each_safe(entry, tmp, &client_list) {
621 client = flist_entry(entry, struct fio_client, list);
623 if (fio_client_send_ini(client, filename))
624 remove_client(client);
630 int fio_client_update_options(struct fio_client *client,
631 struct thread_options *o, uint64_t *tag)
633 struct cmd_add_job_pdu pdu;
635 pdu.thread_number = cpu_to_le32(client->thread_number);
636 pdu.groupid = cpu_to_le32(client->groupid);
637 convert_thread_options_to_net(&pdu.top, o);
639 return fio_net_send_cmd(client->fd, FIO_NET_CMD_UPDATE_JOB, &pdu, sizeof(pdu), tag, &client->cmd_list);
642 static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
644 dst->max_val = le64_to_cpu(src->max_val);
645 dst->min_val = le64_to_cpu(src->min_val);
646 dst->samples = le64_to_cpu(src->samples);
649 * Floats arrive as IEEE 754 encoded uint64_t, convert back to double
651 dst->mean.u.f = fio_uint64_to_double(le64_to_cpu(dst->mean.u.i));
652 dst->S.u.f = fio_uint64_to_double(le64_to_cpu(dst->S.u.i));
655 static void convert_ts(struct thread_stat *dst, struct thread_stat *src)
659 dst->error = le32_to_cpu(src->error);
660 dst->thread_number = le32_to_cpu(src->thread_number);
661 dst->groupid = le32_to_cpu(src->groupid);
662 dst->pid = le32_to_cpu(src->pid);
663 dst->members = le32_to_cpu(src->members);
665 for (i = 0; i < 2; i++) {
666 convert_io_stat(&dst->clat_stat[i], &src->clat_stat[i]);
667 convert_io_stat(&dst->slat_stat[i], &src->slat_stat[i]);
668 convert_io_stat(&dst->lat_stat[i], &src->lat_stat[i]);
669 convert_io_stat(&dst->bw_stat[i], &src->bw_stat[i]);
672 dst->usr_time = le64_to_cpu(src->usr_time);
673 dst->sys_time = le64_to_cpu(src->sys_time);
674 dst->ctx = le64_to_cpu(src->ctx);
675 dst->minf = le64_to_cpu(src->minf);
676 dst->majf = le64_to_cpu(src->majf);
677 dst->clat_percentiles = le64_to_cpu(src->clat_percentiles);
679 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
680 fio_fp64_t *fps = &src->percentile_list[i];
681 fio_fp64_t *fpd = &dst->percentile_list[i];
683 fpd->u.f = fio_uint64_to_double(le64_to_cpu(fps->u.i));
686 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
687 dst->io_u_map[i] = le32_to_cpu(src->io_u_map[i]);
688 dst->io_u_submit[i] = le32_to_cpu(src->io_u_submit[i]);
689 dst->io_u_complete[i] = le32_to_cpu(src->io_u_complete[i]);
692 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
693 dst->io_u_lat_u[i] = le32_to_cpu(src->io_u_lat_u[i]);
694 dst->io_u_lat_m[i] = le32_to_cpu(src->io_u_lat_m[i]);
697 for (i = 0; i < 2; i++)
698 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
699 dst->io_u_plat[i][j] = le32_to_cpu(src->io_u_plat[i][j]);
701 for (i = 0; i < 3; i++) {
702 dst->total_io_u[i] = le64_to_cpu(src->total_io_u[i]);
703 dst->short_io_u[i] = le64_to_cpu(src->short_io_u[i]);
706 dst->total_submit = le64_to_cpu(src->total_submit);
707 dst->total_complete = le64_to_cpu(src->total_complete);
709 for (i = 0; i < 2; i++) {
710 dst->io_bytes[i] = le64_to_cpu(src->io_bytes[i]);
711 dst->runtime[i] = le64_to_cpu(src->runtime[i]);
714 dst->total_run_time = le64_to_cpu(src->total_run_time);
715 dst->continue_on_error = le16_to_cpu(src->continue_on_error);
716 dst->total_err_count = le64_to_cpu(src->total_err_count);
717 dst->first_error = le32_to_cpu(src->first_error);
718 dst->kb_base = le32_to_cpu(src->kb_base);
721 static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
725 for (i = 0; i < 2; i++) {
726 dst->max_run[i] = le64_to_cpu(src->max_run[i]);
727 dst->min_run[i] = le64_to_cpu(src->min_run[i]);
728 dst->max_bw[i] = le64_to_cpu(src->max_bw[i]);
729 dst->min_bw[i] = le64_to_cpu(src->min_bw[i]);
730 dst->io_kb[i] = le64_to_cpu(src->io_kb[i]);
731 dst->agg[i] = le64_to_cpu(src->agg[i]);
734 dst->kb_base = le32_to_cpu(src->kb_base);
735 dst->groupid = le32_to_cpu(src->groupid);
738 static void handle_ts(struct fio_client *client, struct fio_net_cmd *cmd)
740 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
742 show_thread_status(&p->ts, &p->rs);
744 if (sum_stat_clients == 1)
747 sum_thread_stats(&client_ts, &p->ts, sum_stat_nr);
748 sum_group_stats(&client_gs, &p->rs);
751 client_ts.thread_number = p->ts.thread_number;
752 client_ts.groupid = p->ts.groupid;
754 if (++sum_stat_nr == sum_stat_clients) {
755 strcpy(client_ts.name, "All clients");
756 show_thread_status(&client_ts, &client_gs);
760 static void handle_gs(struct fio_client *client, struct fio_net_cmd *cmd)
762 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
764 show_group_stats(gs);
767 static void handle_text(struct fio_client *client, struct fio_net_cmd *cmd)
769 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
770 const char *buf = (const char *) pdu->buf;
774 name = client->name ? client->name : client->hostname;
776 if (!client->skip_newline)
777 fprintf(f_out, "<%s> ", name);
778 ret = fwrite(buf, pdu->buf_len, 1, f_out);
780 client->skip_newline = strchr(buf, '\n') == NULL;
783 static void convert_agg(struct disk_util_agg *agg)
787 for (i = 0; i < 2; i++) {
788 agg->ios[i] = le32_to_cpu(agg->ios[i]);
789 agg->merges[i] = le32_to_cpu(agg->merges[i]);
790 agg->sectors[i] = le64_to_cpu(agg->sectors[i]);
791 agg->ticks[i] = le32_to_cpu(agg->ticks[i]);
794 agg->io_ticks = le32_to_cpu(agg->io_ticks);
795 agg->time_in_queue = le32_to_cpu(agg->time_in_queue);
796 agg->slavecount = le32_to_cpu(agg->slavecount);
797 agg->max_util.u.f = fio_uint64_to_double(__le64_to_cpu(agg->max_util.u.i));
800 static void convert_dus(struct disk_util_stat *dus)
804 for (i = 0; i < 2; i++) {
805 dus->ios[i] = le32_to_cpu(dus->ios[i]);
806 dus->merges[i] = le32_to_cpu(dus->merges[i]);
807 dus->sectors[i] = le64_to_cpu(dus->sectors[i]);
808 dus->ticks[i] = le32_to_cpu(dus->ticks[i]);
811 dus->io_ticks = le32_to_cpu(dus->io_ticks);
812 dus->time_in_queue = le32_to_cpu(dus->time_in_queue);
813 dus->msec = le64_to_cpu(dus->msec);
816 static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd)
818 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
820 if (!client->disk_stats_shown) {
821 client->disk_stats_shown = 1;
822 log_info("\nDisk stats (read/write):\n");
825 print_disk_util(&du->dus, &du->agg, terse_output);
828 static void convert_jobs_eta(struct jobs_eta *je)
832 je->nr_running = le32_to_cpu(je->nr_running);
833 je->nr_ramp = le32_to_cpu(je->nr_ramp);
834 je->nr_pending = le32_to_cpu(je->nr_pending);
835 je->files_open = le32_to_cpu(je->files_open);
837 for (i = 0; i < 2; i++) {
838 je->m_rate[i] = le32_to_cpu(je->m_rate[i]);
839 je->t_rate[i] = le32_to_cpu(je->t_rate[i]);
840 je->m_iops[i] = le32_to_cpu(je->m_iops[i]);
841 je->t_iops[i] = le32_to_cpu(je->t_iops[i]);
842 je->rate[i] = le32_to_cpu(je->rate[i]);
843 je->iops[i] = le32_to_cpu(je->iops[i]);
846 je->elapsed_sec = le64_to_cpu(je->elapsed_sec);
847 je->eta_sec = le64_to_cpu(je->eta_sec);
848 je->nr_threads = le32_to_cpu(je->nr_threads);
849 je->is_pow2 = le32_to_cpu(je->is_pow2);
852 void fio_client_sum_jobs_eta(struct jobs_eta *dst, struct jobs_eta *je)
856 dst->nr_running += je->nr_running;
857 dst->nr_ramp += je->nr_ramp;
858 dst->nr_pending += je->nr_pending;
859 dst->files_open += je->files_open;
861 for (i = 0; i < 2; i++) {
862 dst->m_rate[i] += je->m_rate[i];
863 dst->t_rate[i] += je->t_rate[i];
864 dst->m_iops[i] += je->m_iops[i];
865 dst->t_iops[i] += je->t_iops[i];
866 dst->rate[i] += je->rate[i];
867 dst->iops[i] += je->iops[i];
870 dst->elapsed_sec += je->elapsed_sec;
872 if (je->eta_sec > dst->eta_sec)
873 dst->eta_sec = je->eta_sec;
875 dst->nr_threads += je->nr_threads;
876 /* we need to handle je->run_str too ... */
879 void fio_client_dec_jobs_eta(struct client_eta *eta, client_eta_op eta_fn)
881 if (!--eta->pending) {
887 static void remove_reply_cmd(struct fio_client *client, struct fio_net_cmd *cmd)
889 struct fio_net_cmd_reply *reply = NULL;
890 struct flist_head *entry;
892 flist_for_each(entry, &client->cmd_list) {
893 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
895 if (cmd->tag == (uintptr_t) reply)
902 log_err("fio: client: unable to find matching tag (%lx)\n", cmd->tag);
906 flist_del(&reply->list);
907 cmd->tag = reply->saved_tag;
911 int fio_client_wait_for_reply(struct fio_client *client, uint64_t tag)
914 struct fio_net_cmd_reply *reply = NULL;
915 struct flist_head *entry;
917 flist_for_each(entry, &client->cmd_list) {
918 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
920 if (tag == (uintptr_t) reply)
935 static void handle_eta(struct fio_client *client, struct fio_net_cmd *cmd)
937 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
938 struct client_eta *eta = (struct client_eta *) (uintptr_t) cmd->tag;
940 dprint(FD_NET, "client: got eta tag %p, %d\n", eta, eta->pending);
942 assert(client->eta_in_flight == eta);
944 client->eta_in_flight = NULL;
945 flist_del_init(&client->eta_list);
947 if (client->ops->jobs_eta)
948 client->ops->jobs_eta(client, je);
950 fio_client_sum_jobs_eta(&eta->eta, je);
951 fio_client_dec_jobs_eta(eta, client->ops->eta);
954 static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd)
956 struct cmd_probe_pdu *probe = (struct cmd_probe_pdu *) cmd->payload;
957 const char *os, *arch;
960 os = fio_get_os_string(probe->os);
964 arch = fio_get_arch_string(probe->arch);
968 sprintf(bit, "%d-bit", probe->bpp * 8);
970 log_info("hostname=%s, be=%u, %s, os=%s, arch=%s, fio=%s\n",
971 probe->hostname, probe->bigendian, bit, os, arch,
975 client->name = strdup((char *) probe->hostname);
978 static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd)
980 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
982 client->state = Client_started;
983 client->jobs = pdu->jobs;
986 static void handle_stop(struct fio_client *client, struct fio_net_cmd *cmd)
989 log_info("client <%s>: exited with error %d\n", client->hostname, client->error);
992 static void convert_stop(struct fio_net_cmd *cmd)
994 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
996 pdu->error = le32_to_cpu(pdu->error);
999 static void convert_text(struct fio_net_cmd *cmd)
1001 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
1003 pdu->level = le32_to_cpu(pdu->level);
1004 pdu->buf_len = le32_to_cpu(pdu->buf_len);
1005 pdu->log_sec = le64_to_cpu(pdu->log_sec);
1006 pdu->log_usec = le64_to_cpu(pdu->log_usec);
1010 * This has been compressed on the server side, since it can be big.
1013 static struct cmd_iolog_pdu *convert_iolog(struct fio_net_cmd *cmd)
1015 struct cmd_iolog_pdu *pdu = (struct cmd_iolog_pdu *) cmd->payload;
1016 struct cmd_iolog_pdu *ret;
1017 uint32_t nr_samples;
1018 unsigned long total;
1023 stream.zalloc = Z_NULL;
1024 stream.zfree = Z_NULL;
1025 stream.opaque = Z_NULL;
1026 stream.avail_in = 0;
1027 stream.next_in = Z_NULL;
1029 if (inflateInit(&stream) != Z_OK)
1033 * Get header first, it's not compressed
1035 nr_samples = le32_to_cpu(pdu->nr_samples);
1037 total = nr_samples * sizeof(struct io_sample);
1038 ret = malloc(total + sizeof(*pdu));
1039 ret->thread_number = le32_to_cpu(pdu->thread_number);
1040 ret->nr_samples = nr_samples;
1041 ret->log_type = le32_to_cpu(pdu->log_type);
1042 strcpy((char *) ret->name, (char *) pdu->name);
1044 p = (void *) ret + sizeof(*pdu);
1046 stream.avail_in = cmd->pdu_len - sizeof(*pdu);
1047 stream.next_in = (void *) pdu + sizeof(*pdu);
1048 while (stream.avail_in) {
1049 unsigned int this_chunk = 65536;
1050 unsigned int this_len;
1053 if (this_chunk > total)
1056 stream.avail_out = this_chunk;
1057 stream.next_out = p;
1058 err = inflate(&stream, Z_NO_FLUSH);
1059 /* may be Z_OK, or Z_STREAM_END */
1061 log_err("fio: inflate error %d\n", err);
1067 this_len = this_chunk - stream.avail_out;
1072 for (i = 0; i < ret->nr_samples; i++) {
1073 struct io_sample *s = &ret->samples[i];
1075 s->time = le64_to_cpu(s->time);
1076 s->val = le64_to_cpu(s->val);
1077 s->ddir = le32_to_cpu(s->ddir);
1078 s->bs = le32_to_cpu(s->bs);
1082 inflateEnd(&stream);
1086 int fio_handle_client(struct fio_client *client)
1088 struct client_ops *ops = client->ops;
1089 struct fio_net_cmd *cmd;
1091 dprint(FD_NET, "client: handle %s\n", client->hostname);
1093 cmd = fio_net_recv_cmd(client->fd);
1097 dprint(FD_NET, "client: got cmd op %s from %s (pdu=%u)\n",
1098 fio_server_op(cmd->opcode), client->hostname, cmd->pdu_len);
1100 switch (cmd->opcode) {
1101 case FIO_NET_CMD_QUIT:
1103 ops->quit(client, cmd);
1104 remove_client(client);
1107 case FIO_NET_CMD_TEXT:
1109 ops->text(client, cmd);
1112 case FIO_NET_CMD_DU: {
1113 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
1115 convert_dus(&du->dus);
1116 convert_agg(&du->agg);
1118 ops->disk_util(client, cmd);
1122 case FIO_NET_CMD_TS: {
1123 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
1125 convert_ts(&p->ts, &p->ts);
1126 convert_gs(&p->rs, &p->rs);
1128 ops->thread_status(client, cmd);
1132 case FIO_NET_CMD_GS: {
1133 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
1137 ops->group_stats(client, cmd);
1141 case FIO_NET_CMD_ETA: {
1142 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
1144 remove_reply_cmd(client, cmd);
1145 convert_jobs_eta(je);
1146 handle_eta(client, cmd);
1150 case FIO_NET_CMD_PROBE:
1151 remove_reply_cmd(client, cmd);
1152 ops->probe(client, cmd);
1155 case FIO_NET_CMD_SERVER_START:
1156 client->state = Client_running;
1158 ops->job_start(client, cmd);
1161 case FIO_NET_CMD_START: {
1162 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
1164 pdu->jobs = le32_to_cpu(pdu->jobs);
1165 ops->start(client, cmd);
1169 case FIO_NET_CMD_STOP: {
1170 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
1173 client->state = Client_stopped;
1174 client->error = le32_to_cpu(pdu->error);
1175 client->signal = le32_to_cpu(pdu->signal);
1176 ops->stop(client, cmd);
1180 case FIO_NET_CMD_ADD_JOB: {
1181 struct cmd_add_job_pdu *pdu = (struct cmd_add_job_pdu *) cmd->payload;
1183 client->thread_number = le32_to_cpu(pdu->thread_number);
1184 client->groupid = le32_to_cpu(pdu->groupid);
1187 ops->add_job(client, cmd);
1191 case FIO_NET_CMD_IOLOG:
1193 struct cmd_iolog_pdu *pdu;
1195 pdu = convert_iolog(cmd);
1196 ops->iolog(client, pdu);
1200 case FIO_NET_CMD_UPDATE_JOB:
1201 ops->update_job(client, cmd);
1202 remove_reply_cmd(client, cmd);
1206 log_err("fio: unknown client op: %s\n", fio_server_op(cmd->opcode));
1214 static void request_client_etas(struct client_ops *ops)
1216 struct fio_client *client;
1217 struct flist_head *entry;
1218 struct client_eta *eta;
1221 dprint(FD_NET, "client: request eta (%d)\n", nr_clients);
1223 eta = malloc(sizeof(*eta));
1224 memset(&eta->eta, 0, sizeof(eta->eta));
1225 eta->pending = nr_clients;
1227 flist_for_each(entry, &client_list) {
1228 client = flist_entry(entry, struct fio_client, list);
1230 if (!flist_empty(&client->eta_list)) {
1234 if (client->state != Client_running)
1237 assert(!client->eta_in_flight);
1238 flist_add_tail(&client->eta_list, &eta_list);
1239 client->eta_in_flight = eta;
1240 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_SEND_ETA,
1241 (uintptr_t) eta, &client->cmd_list);
1245 fio_client_dec_jobs_eta(eta, ops->eta);
1247 dprint(FD_NET, "client: requested eta tag %p\n", eta);
1250 static int client_check_cmd_timeout(struct fio_client *client,
1251 struct timeval *now)
1253 struct fio_net_cmd_reply *reply;
1254 struct flist_head *entry, *tmp;
1257 flist_for_each_safe(entry, tmp, &client->cmd_list) {
1258 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
1260 if (mtime_since(&reply->tv, now) < FIO_NET_CLIENT_TIMEOUT)
1263 log_err("fio: client %s, timeout on cmd %s\n", client->hostname,
1264 fio_server_op(reply->opcode));
1265 flist_del(&reply->list);
1270 return flist_empty(&client->cmd_list) && ret;
1273 static int fio_check_clients_timed_out(void)
1275 struct fio_client *client;
1276 struct flist_head *entry, *tmp;
1280 gettimeofday(&tv, NULL);
1282 flist_for_each_safe(entry, tmp, &client_list) {
1283 client = flist_entry(entry, struct fio_client, list);
1285 if (flist_empty(&client->cmd_list))
1288 if (!client_check_cmd_timeout(client, &tv))
1291 if (client->ops->timed_out)
1292 client->ops->timed_out(client);
1294 log_err("fio: client %s timed out\n", client->hostname);
1296 remove_client(client);
1303 int fio_handle_clients(struct client_ops *ops)
1305 struct pollfd *pfds;
1306 int i, ret = 0, retval = 0;
1308 gettimeofday(&eta_tv, NULL);
1310 pfds = malloc(nr_clients * sizeof(struct pollfd));
1312 sum_stat_clients = nr_clients;
1313 init_thread_stat(&client_ts);
1314 init_group_run_stat(&client_gs);
1316 while (!exit_backend && nr_clients) {
1317 struct flist_head *entry, *tmp;
1318 struct fio_client *client;
1321 flist_for_each_safe(entry, tmp, &client_list) {
1322 client = flist_entry(entry, struct fio_client, list);
1324 if (!client->sent_job && !client->ops->stay_connected &&
1325 flist_empty(&client->cmd_list)) {
1326 remove_client(client);
1330 pfds[i].fd = client->fd;
1331 pfds[i].events = POLLIN;
1338 assert(i == nr_clients);
1343 gettimeofday(&tv, NULL);
1344 if (mtime_since(&eta_tv, &tv) >= ops->eta_msec) {
1345 request_client_etas(ops);
1346 memcpy(&eta_tv, &tv, sizeof(tv));
1348 if (fio_check_clients_timed_out())
1352 ret = poll(pfds, nr_clients, ops->eta_msec);
1356 log_err("fio: poll clients: %s\n", strerror(errno));
1362 for (i = 0; i < nr_clients; i++) {
1363 if (!(pfds[i].revents & POLLIN))
1366 client = find_client_by_fd(pfds[i].fd);
1368 log_err("fio: unknown client fd %d\n", pfds[i].fd);
1371 if (!fio_handle_client(client)) {
1372 log_info("client: host=%s disconnected\n",
1374 remove_client(client);
1376 } else if (client->error)
1378 fio_put_client(client);