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 client_signal_handler(void)
406 struct sigaction act;
408 memset(&act, 0, sizeof(act));
409 act.sa_handler = sig_int;
410 act.sa_flags = SA_RESTART;
411 sigaction(SIGINT, &act, NULL);
413 memset(&act, 0, sizeof(act));
414 act.sa_handler = sig_int;
415 act.sa_flags = SA_RESTART;
416 sigaction(SIGTERM, &act, NULL);
419 static int send_client_cmd_line(struct fio_client *client)
421 struct cmd_single_line_pdu *cslp;
422 struct cmd_line_pdu *clp;
423 unsigned long offset;
429 dprint(FD_NET, "client: send cmdline %d\n", client->argc);
431 lens = malloc(client->argc * sizeof(unsigned int));
434 * Find out how much mem we need
436 for (i = 0, mem = 0; i < client->argc; i++) {
437 lens[i] = strlen(client->argv[i]) + 1;
442 * We need one cmd_line_pdu, and argc number of cmd_single_line_pdu
444 mem += sizeof(*clp) + (client->argc * sizeof(*cslp));
448 offset = sizeof(*clp);
450 for (i = 0; i < client->argc; i++) {
451 uint16_t arg_len = lens[i];
454 strcpy((char *) cslp->text, client->argv[i]);
455 cslp->len = cpu_to_le16(arg_len);
456 offset += sizeof(*cslp) + arg_len;
460 clp->lines = cpu_to_le16(client->argc);
461 clp->client_type = __cpu_to_le16(client->type);
462 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOBLINE, pdu, mem, NULL, NULL);
467 int fio_clients_connect(void)
469 struct fio_client *client;
470 struct flist_head *entry, *tmp;
475 WSAStartup(MAKEWORD(2, 2), &wsd);
478 dprint(FD_NET, "client: connect all\n");
480 client_signal_handler();
482 flist_for_each_safe(entry, tmp, &client_list) {
483 client = flist_entry(entry, struct fio_client, list);
485 ret = fio_client_connect(client);
487 remove_client(client);
491 if (client->argc > 1)
492 send_client_cmd_line(client);
498 int fio_start_client(struct fio_client *client)
500 dprint(FD_NET, "client: start %s\n", client->hostname);
501 return fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_RUN, 0, NULL);
504 int fio_start_all_clients(void)
506 struct fio_client *client;
507 struct flist_head *entry, *tmp;
510 dprint(FD_NET, "client: start all\n");
512 flist_for_each_safe(entry, tmp, &client_list) {
513 client = flist_entry(entry, struct fio_client, list);
515 ret = fio_start_client(client);
517 remove_client(client);
522 return flist_empty(&client_list);
526 * Send file contents to server backend. We could use sendfile(), but to remain
527 * more portable lets just read/write the darn thing.
529 static int __fio_client_send_ini(struct fio_client *client, const char *filename)
531 struct cmd_job_pdu *pdu;
539 dprint(FD_NET, "send ini %s to %s\n", filename, client->hostname);
541 fd = open(filename, O_RDONLY);
545 log_err("fio: job file <%s> open: %s\n", filename, strerror(errno));
549 if (fstat(fd, &sb) < 0) {
552 log_err("fio: job file stat: %s\n", strerror(errno));
557 p_size = sb.st_size + sizeof(*pdu);
558 pdu = malloc(p_size);
564 ret = read(fd, p, len);
573 else if (errno == EAGAIN || errno == EINTR)
578 log_err("fio: failed reading job file %s\n", filename);
584 pdu->buf_len = __cpu_to_le32(sb.st_size);
585 pdu->client_type = cpu_to_le32(client->type);
587 client->sent_job = 1;
588 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, pdu, p_size, NULL, NULL);
594 int fio_client_send_ini(struct fio_client *client, const char *filename)
598 ret = __fio_client_send_ini(client, filename);
600 client->sent_job = 1;
605 int fio_clients_send_ini(const char *filename)
607 struct fio_client *client;
608 struct flist_head *entry, *tmp;
610 flist_for_each_safe(entry, tmp, &client_list) {
611 client = flist_entry(entry, struct fio_client, list);
613 if (fio_client_send_ini(client, filename))
614 remove_client(client);
620 int fio_client_update_options(struct fio_client *client,
621 struct thread_options *o, uint64_t *tag)
623 struct cmd_add_job_pdu pdu;
625 pdu.thread_number = cpu_to_le32(client->thread_number);
626 pdu.groupid = cpu_to_le32(client->groupid);
627 convert_thread_options_to_net(&pdu.top, o);
629 return fio_net_send_cmd(client->fd, FIO_NET_CMD_UPDATE_JOB, &pdu, sizeof(pdu), tag, &client->cmd_list);
632 static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
634 dst->max_val = le64_to_cpu(src->max_val);
635 dst->min_val = le64_to_cpu(src->min_val);
636 dst->samples = le64_to_cpu(src->samples);
639 * Floats arrive as IEEE 754 encoded uint64_t, convert back to double
641 dst->mean.u.f = fio_uint64_to_double(le64_to_cpu(dst->mean.u.i));
642 dst->S.u.f = fio_uint64_to_double(le64_to_cpu(dst->S.u.i));
645 static void convert_ts(struct thread_stat *dst, struct thread_stat *src)
649 dst->error = le32_to_cpu(src->error);
650 dst->thread_number = le32_to_cpu(src->thread_number);
651 dst->groupid = le32_to_cpu(src->groupid);
652 dst->pid = le32_to_cpu(src->pid);
653 dst->members = le32_to_cpu(src->members);
655 for (i = 0; i < 2; i++) {
656 convert_io_stat(&dst->clat_stat[i], &src->clat_stat[i]);
657 convert_io_stat(&dst->slat_stat[i], &src->slat_stat[i]);
658 convert_io_stat(&dst->lat_stat[i], &src->lat_stat[i]);
659 convert_io_stat(&dst->bw_stat[i], &src->bw_stat[i]);
662 dst->usr_time = le64_to_cpu(src->usr_time);
663 dst->sys_time = le64_to_cpu(src->sys_time);
664 dst->ctx = le64_to_cpu(src->ctx);
665 dst->minf = le64_to_cpu(src->minf);
666 dst->majf = le64_to_cpu(src->majf);
667 dst->clat_percentiles = le64_to_cpu(src->clat_percentiles);
669 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
670 fio_fp64_t *fps = &src->percentile_list[i];
671 fio_fp64_t *fpd = &dst->percentile_list[i];
673 fpd->u.f = fio_uint64_to_double(le64_to_cpu(fps->u.i));
676 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
677 dst->io_u_map[i] = le32_to_cpu(src->io_u_map[i]);
678 dst->io_u_submit[i] = le32_to_cpu(src->io_u_submit[i]);
679 dst->io_u_complete[i] = le32_to_cpu(src->io_u_complete[i]);
682 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
683 dst->io_u_lat_u[i] = le32_to_cpu(src->io_u_lat_u[i]);
684 dst->io_u_lat_m[i] = le32_to_cpu(src->io_u_lat_m[i]);
687 for (i = 0; i < 2; i++)
688 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
689 dst->io_u_plat[i][j] = le32_to_cpu(src->io_u_plat[i][j]);
691 for (i = 0; i < 3; i++) {
692 dst->total_io_u[i] = le64_to_cpu(src->total_io_u[i]);
693 dst->short_io_u[i] = le64_to_cpu(src->short_io_u[i]);
696 dst->total_submit = le64_to_cpu(src->total_submit);
697 dst->total_complete = le64_to_cpu(src->total_complete);
699 for (i = 0; i < 2; i++) {
700 dst->io_bytes[i] = le64_to_cpu(src->io_bytes[i]);
701 dst->runtime[i] = le64_to_cpu(src->runtime[i]);
704 dst->total_run_time = le64_to_cpu(src->total_run_time);
705 dst->continue_on_error = le16_to_cpu(src->continue_on_error);
706 dst->total_err_count = le64_to_cpu(src->total_err_count);
707 dst->first_error = le32_to_cpu(src->first_error);
708 dst->kb_base = le32_to_cpu(src->kb_base);
711 static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
715 for (i = 0; i < 2; i++) {
716 dst->max_run[i] = le64_to_cpu(src->max_run[i]);
717 dst->min_run[i] = le64_to_cpu(src->min_run[i]);
718 dst->max_bw[i] = le64_to_cpu(src->max_bw[i]);
719 dst->min_bw[i] = le64_to_cpu(src->min_bw[i]);
720 dst->io_kb[i] = le64_to_cpu(src->io_kb[i]);
721 dst->agg[i] = le64_to_cpu(src->agg[i]);
724 dst->kb_base = le32_to_cpu(src->kb_base);
725 dst->groupid = le32_to_cpu(src->groupid);
728 static void handle_ts(struct fio_client *client, struct fio_net_cmd *cmd)
730 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
732 show_thread_status(&p->ts, &p->rs);
734 if (sum_stat_clients == 1)
737 sum_thread_stats(&client_ts, &p->ts, sum_stat_nr);
738 sum_group_stats(&client_gs, &p->rs);
741 client_ts.thread_number = p->ts.thread_number;
742 client_ts.groupid = p->ts.groupid;
744 if (++sum_stat_nr == sum_stat_clients) {
745 strcpy(client_ts.name, "All clients");
746 show_thread_status(&client_ts, &client_gs);
750 static void handle_gs(struct fio_client *client, struct fio_net_cmd *cmd)
752 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
754 show_group_stats(gs);
757 static void handle_text(struct fio_client *client, struct fio_net_cmd *cmd)
759 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
760 const char *buf = (const char *) pdu->buf;
764 name = client->name ? client->name : client->hostname;
766 if (!client->skip_newline)
767 fprintf(f_out, "<%s> ", name);
768 ret = fwrite(buf, pdu->buf_len, 1, f_out);
770 client->skip_newline = strchr(buf, '\n') == NULL;
773 static void convert_agg(struct disk_util_agg *agg)
777 for (i = 0; i < 2; i++) {
778 agg->ios[i] = le32_to_cpu(agg->ios[i]);
779 agg->merges[i] = le32_to_cpu(agg->merges[i]);
780 agg->sectors[i] = le64_to_cpu(agg->sectors[i]);
781 agg->ticks[i] = le32_to_cpu(agg->ticks[i]);
784 agg->io_ticks = le32_to_cpu(agg->io_ticks);
785 agg->time_in_queue = le32_to_cpu(agg->time_in_queue);
786 agg->slavecount = le32_to_cpu(agg->slavecount);
787 agg->max_util.u.f = fio_uint64_to_double(__le64_to_cpu(agg->max_util.u.i));
790 static void convert_dus(struct disk_util_stat *dus)
794 for (i = 0; i < 2; i++) {
795 dus->ios[i] = le32_to_cpu(dus->ios[i]);
796 dus->merges[i] = le32_to_cpu(dus->merges[i]);
797 dus->sectors[i] = le64_to_cpu(dus->sectors[i]);
798 dus->ticks[i] = le32_to_cpu(dus->ticks[i]);
801 dus->io_ticks = le32_to_cpu(dus->io_ticks);
802 dus->time_in_queue = le32_to_cpu(dus->time_in_queue);
803 dus->msec = le64_to_cpu(dus->msec);
806 static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd)
808 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
810 if (!client->disk_stats_shown) {
811 client->disk_stats_shown = 1;
812 log_info("\nDisk stats (read/write):\n");
815 print_disk_util(&du->dus, &du->agg, terse_output);
818 static void convert_jobs_eta(struct jobs_eta *je)
822 je->nr_running = le32_to_cpu(je->nr_running);
823 je->nr_ramp = le32_to_cpu(je->nr_ramp);
824 je->nr_pending = le32_to_cpu(je->nr_pending);
825 je->files_open = le32_to_cpu(je->files_open);
827 for (i = 0; i < 2; i++) {
828 je->m_rate[i] = le32_to_cpu(je->m_rate[i]);
829 je->t_rate[i] = le32_to_cpu(je->t_rate[i]);
830 je->m_iops[i] = le32_to_cpu(je->m_iops[i]);
831 je->t_iops[i] = le32_to_cpu(je->t_iops[i]);
832 je->rate[i] = le32_to_cpu(je->rate[i]);
833 je->iops[i] = le32_to_cpu(je->iops[i]);
836 je->elapsed_sec = le64_to_cpu(je->elapsed_sec);
837 je->eta_sec = le64_to_cpu(je->eta_sec);
838 je->nr_threads = le32_to_cpu(je->nr_threads);
841 void fio_client_sum_jobs_eta(struct jobs_eta *dst, struct jobs_eta *je)
845 dst->nr_running += je->nr_running;
846 dst->nr_ramp += je->nr_ramp;
847 dst->nr_pending += je->nr_pending;
848 dst->files_open += je->files_open;
850 for (i = 0; i < 2; i++) {
851 dst->m_rate[i] += je->m_rate[i];
852 dst->t_rate[i] += je->t_rate[i];
853 dst->m_iops[i] += je->m_iops[i];
854 dst->t_iops[i] += je->t_iops[i];
855 dst->rate[i] += je->rate[i];
856 dst->iops[i] += je->iops[i];
859 dst->elapsed_sec += je->elapsed_sec;
861 if (je->eta_sec > dst->eta_sec)
862 dst->eta_sec = je->eta_sec;
864 dst->nr_threads += je->nr_threads;
865 /* we need to handle je->run_str too ... */
868 void fio_client_dec_jobs_eta(struct client_eta *eta, client_eta_op eta_fn)
870 if (!--eta->pending) {
876 static void remove_reply_cmd(struct fio_client *client, struct fio_net_cmd *cmd)
878 struct fio_net_cmd_reply *reply = NULL;
879 struct flist_head *entry;
881 flist_for_each(entry, &client->cmd_list) {
882 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
884 if (cmd->tag == (uintptr_t) reply)
891 log_err("fio: client: unable to find matching tag (%lx)\n", cmd->tag);
895 flist_del(&reply->list);
896 cmd->tag = reply->saved_tag;
900 int fio_client_wait_for_reply(struct fio_client *client, uint64_t tag)
903 struct fio_net_cmd_reply *reply = NULL;
904 struct flist_head *entry;
906 flist_for_each(entry, &client->cmd_list) {
907 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
909 if (tag == (uintptr_t) reply)
924 static void handle_eta(struct fio_client *client, struct fio_net_cmd *cmd)
926 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
927 struct client_eta *eta = (struct client_eta *) (uintptr_t) cmd->tag;
929 dprint(FD_NET, "client: got eta tag %p, %d\n", eta, eta->pending);
931 assert(client->eta_in_flight == eta);
933 client->eta_in_flight = NULL;
934 flist_del_init(&client->eta_list);
936 if (client->ops->jobs_eta)
937 client->ops->jobs_eta(client, je);
939 fio_client_sum_jobs_eta(&eta->eta, je);
940 fio_client_dec_jobs_eta(eta, client->ops->eta);
943 static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd)
945 struct cmd_probe_pdu *probe = (struct cmd_probe_pdu *) cmd->payload;
946 const char *os, *arch;
949 os = fio_get_os_string(probe->os);
953 arch = fio_get_arch_string(probe->arch);
957 sprintf(bit, "%d-bit", probe->bpp * 8);
959 log_info("hostname=%s, be=%u, %s, os=%s, arch=%s, fio=%u.%u.%u\n",
960 probe->hostname, probe->bigendian, bit, os, arch,
961 probe->fio_major, probe->fio_minor, probe->fio_patch);
964 client->name = strdup((char *) probe->hostname);
967 static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd)
969 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
971 client->state = Client_started;
972 client->jobs = pdu->jobs;
975 static void handle_stop(struct fio_client *client, struct fio_net_cmd *cmd)
978 log_info("client <%s>: exited with error %d\n", client->hostname, client->error);
981 static void convert_stop(struct fio_net_cmd *cmd)
983 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
985 pdu->error = le32_to_cpu(pdu->error);
988 static void convert_text(struct fio_net_cmd *cmd)
990 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
992 pdu->level = le32_to_cpu(pdu->level);
993 pdu->buf_len = le32_to_cpu(pdu->buf_len);
994 pdu->log_sec = le64_to_cpu(pdu->log_sec);
995 pdu->log_usec = le64_to_cpu(pdu->log_usec);
999 * This has been compressed on the server side, since it can be big.
1002 static struct cmd_iolog_pdu *convert_iolog(struct fio_net_cmd *cmd)
1004 struct cmd_iolog_pdu *pdu = (struct cmd_iolog_pdu *) cmd->payload;
1005 struct cmd_iolog_pdu *ret;
1006 uint32_t nr_samples;
1007 unsigned long total;
1012 stream.zalloc = Z_NULL;
1013 stream.zfree = Z_NULL;
1014 stream.opaque = Z_NULL;
1015 stream.avail_in = 0;
1016 stream.next_in = Z_NULL;
1018 if (inflateInit(&stream) != Z_OK)
1022 * Get header first, it's not compressed
1024 nr_samples = le32_to_cpu(pdu->nr_samples);
1026 total = nr_samples * sizeof(struct io_sample);
1027 ret = malloc(total + sizeof(*pdu));
1028 ret->thread_number = le32_to_cpu(pdu->thread_number);
1029 ret->nr_samples = nr_samples;
1030 ret->log_type = le32_to_cpu(pdu->log_type);
1031 strcpy((char *) ret->name, (char *) pdu->name);
1033 p = (void *) ret + sizeof(*pdu);
1035 stream.avail_in = cmd->pdu_len - sizeof(*pdu);
1036 stream.next_in = (void *) pdu + sizeof(*pdu);
1037 while (stream.avail_in) {
1038 unsigned int this_chunk = 65536;
1039 unsigned int this_len;
1042 if (this_chunk > total)
1045 stream.avail_out = this_chunk;
1046 stream.next_out = p;
1047 err = inflate(&stream, Z_NO_FLUSH);
1048 /* may be Z_OK, or Z_STREAM_END */
1050 log_err("fio: inflate error %d\n", err);
1056 this_len = this_chunk - stream.avail_out;
1061 for (i = 0; i < ret->nr_samples; i++) {
1062 struct io_sample *s = &ret->samples[i];
1064 s->time = le64_to_cpu(s->time);
1065 s->val = le64_to_cpu(s->val);
1066 s->ddir = le32_to_cpu(s->ddir);
1067 s->bs = le32_to_cpu(s->bs);
1071 inflateEnd(&stream);
1075 int fio_handle_client(struct fio_client *client)
1077 struct client_ops *ops = client->ops;
1078 struct fio_net_cmd *cmd;
1080 dprint(FD_NET, "client: handle %s\n", client->hostname);
1082 cmd = fio_net_recv_cmd(client->fd);
1086 dprint(FD_NET, "client: got cmd op %s from %s (pdu=%u)\n",
1087 fio_server_op(cmd->opcode), client->hostname, cmd->pdu_len);
1089 switch (cmd->opcode) {
1090 case FIO_NET_CMD_QUIT:
1092 ops->quit(client, cmd);
1093 remove_client(client);
1096 case FIO_NET_CMD_TEXT:
1098 ops->text(client, cmd);
1101 case FIO_NET_CMD_DU: {
1102 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
1104 convert_dus(&du->dus);
1105 convert_agg(&du->agg);
1107 ops->disk_util(client, cmd);
1111 case FIO_NET_CMD_TS: {
1112 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
1114 convert_ts(&p->ts, &p->ts);
1115 convert_gs(&p->rs, &p->rs);
1117 ops->thread_status(client, cmd);
1121 case FIO_NET_CMD_GS: {
1122 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
1126 ops->group_stats(client, cmd);
1130 case FIO_NET_CMD_ETA: {
1131 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
1133 remove_reply_cmd(client, cmd);
1134 convert_jobs_eta(je);
1135 handle_eta(client, cmd);
1139 case FIO_NET_CMD_PROBE:
1140 remove_reply_cmd(client, cmd);
1141 ops->probe(client, cmd);
1144 case FIO_NET_CMD_SERVER_START:
1145 client->state = Client_running;
1147 ops->job_start(client, cmd);
1150 case FIO_NET_CMD_START: {
1151 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
1153 pdu->jobs = le32_to_cpu(pdu->jobs);
1154 ops->start(client, cmd);
1158 case FIO_NET_CMD_STOP: {
1159 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
1162 client->state = Client_stopped;
1163 client->error = le32_to_cpu(pdu->error);
1164 client->signal = le32_to_cpu(pdu->signal);
1165 ops->stop(client, cmd);
1169 case FIO_NET_CMD_ADD_JOB: {
1170 struct cmd_add_job_pdu *pdu = (struct cmd_add_job_pdu *) cmd->payload;
1172 client->thread_number = le32_to_cpu(pdu->thread_number);
1173 client->groupid = le32_to_cpu(pdu->groupid);
1176 ops->add_job(client, cmd);
1180 case FIO_NET_CMD_IOLOG:
1182 struct cmd_iolog_pdu *pdu;
1184 pdu = convert_iolog(cmd);
1185 ops->iolog(client, pdu);
1189 case FIO_NET_CMD_UPDATE_JOB:
1190 ops->update_job(client, cmd);
1191 remove_reply_cmd(client, cmd);
1195 log_err("fio: unknown client op: %s\n", fio_server_op(cmd->opcode));
1203 static void request_client_etas(struct client_ops *ops)
1205 struct fio_client *client;
1206 struct flist_head *entry;
1207 struct client_eta *eta;
1210 dprint(FD_NET, "client: request eta (%d)\n", nr_clients);
1212 eta = malloc(sizeof(*eta));
1213 memset(&eta->eta, 0, sizeof(eta->eta));
1214 eta->pending = nr_clients;
1216 flist_for_each(entry, &client_list) {
1217 client = flist_entry(entry, struct fio_client, list);
1219 if (!flist_empty(&client->eta_list)) {
1223 if (client->state != Client_running)
1226 assert(!client->eta_in_flight);
1227 flist_add_tail(&client->eta_list, &eta_list);
1228 client->eta_in_flight = eta;
1229 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_SEND_ETA,
1230 (uintptr_t) eta, &client->cmd_list);
1234 fio_client_dec_jobs_eta(eta, ops->eta);
1236 dprint(FD_NET, "client: requested eta tag %p\n", eta);
1239 static int client_check_cmd_timeout(struct fio_client *client,
1240 struct timeval *now)
1242 struct fio_net_cmd_reply *reply;
1243 struct flist_head *entry, *tmp;
1246 flist_for_each_safe(entry, tmp, &client->cmd_list) {
1247 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
1249 if (mtime_since(&reply->tv, now) < FIO_NET_CLIENT_TIMEOUT)
1252 log_err("fio: client %s, timeout on cmd %s\n", client->hostname,
1253 fio_server_op(reply->opcode));
1254 flist_del(&reply->list);
1259 return flist_empty(&client->cmd_list) && ret;
1262 static int fio_check_clients_timed_out(void)
1264 struct fio_client *client;
1265 struct flist_head *entry, *tmp;
1269 gettimeofday(&tv, NULL);
1271 flist_for_each_safe(entry, tmp, &client_list) {
1272 client = flist_entry(entry, struct fio_client, list);
1274 if (flist_empty(&client->cmd_list))
1277 if (!client_check_cmd_timeout(client, &tv))
1280 if (client->ops->timed_out)
1281 client->ops->timed_out(client);
1283 log_err("fio: client %s timed out\n", client->hostname);
1285 remove_client(client);
1292 int fio_handle_clients(struct client_ops *ops)
1294 struct pollfd *pfds;
1295 int i, ret = 0, retval = 0;
1297 gettimeofday(&eta_tv, NULL);
1299 pfds = malloc(nr_clients * sizeof(struct pollfd));
1301 sum_stat_clients = nr_clients;
1302 init_thread_stat(&client_ts);
1303 init_group_run_stat(&client_gs);
1305 while (!exit_backend && nr_clients) {
1306 struct flist_head *entry, *tmp;
1307 struct fio_client *client;
1310 flist_for_each_safe(entry, tmp, &client_list) {
1311 client = flist_entry(entry, struct fio_client, list);
1313 if (!client->sent_job && !client->ops->stay_connected &&
1314 flist_empty(&client->cmd_list)) {
1315 remove_client(client);
1319 pfds[i].fd = client->fd;
1320 pfds[i].events = POLLIN;
1327 assert(i == nr_clients);
1332 gettimeofday(&tv, NULL);
1333 if (mtime_since(&eta_tv, &tv) >= ops->eta_msec) {
1334 request_client_etas(ops);
1335 memcpy(&eta_tv, &tv, sizeof(tv));
1337 if (fio_check_clients_timed_out())
1341 ret = poll(pfds, nr_clients, ops->eta_msec);
1345 log_err("fio: poll clients: %s\n", strerror(errno));
1351 for (i = 0; i < nr_clients; i++) {
1352 if (!(pfds[i].revents & POLLIN))
1355 client = find_client_by_fd(pfds[i].fd);
1357 log_err("fio: unknown client fd %d\n", pfds[i].fd);
1360 if (!fio_handle_client(client)) {
1361 log_info("client: host=%s disconnected\n",
1363 remove_client(client);
1365 } else if (client->error)
1367 fio_put_client(client);