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);
851 void fio_client_sum_jobs_eta(struct jobs_eta *dst, struct jobs_eta *je)
855 dst->nr_running += je->nr_running;
856 dst->nr_ramp += je->nr_ramp;
857 dst->nr_pending += je->nr_pending;
858 dst->files_open += je->files_open;
860 for (i = 0; i < 2; i++) {
861 dst->m_rate[i] += je->m_rate[i];
862 dst->t_rate[i] += je->t_rate[i];
863 dst->m_iops[i] += je->m_iops[i];
864 dst->t_iops[i] += je->t_iops[i];
865 dst->rate[i] += je->rate[i];
866 dst->iops[i] += je->iops[i];
869 dst->elapsed_sec += je->elapsed_sec;
871 if (je->eta_sec > dst->eta_sec)
872 dst->eta_sec = je->eta_sec;
874 dst->nr_threads += je->nr_threads;
875 /* we need to handle je->run_str too ... */
878 void fio_client_dec_jobs_eta(struct client_eta *eta, client_eta_op eta_fn)
880 if (!--eta->pending) {
886 static void remove_reply_cmd(struct fio_client *client, struct fio_net_cmd *cmd)
888 struct fio_net_cmd_reply *reply = NULL;
889 struct flist_head *entry;
891 flist_for_each(entry, &client->cmd_list) {
892 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
894 if (cmd->tag == (uintptr_t) reply)
901 log_err("fio: client: unable to find matching tag (%lx)\n", cmd->tag);
905 flist_del(&reply->list);
906 cmd->tag = reply->saved_tag;
910 int fio_client_wait_for_reply(struct fio_client *client, uint64_t tag)
913 struct fio_net_cmd_reply *reply = NULL;
914 struct flist_head *entry;
916 flist_for_each(entry, &client->cmd_list) {
917 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
919 if (tag == (uintptr_t) reply)
934 static void handle_eta(struct fio_client *client, struct fio_net_cmd *cmd)
936 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
937 struct client_eta *eta = (struct client_eta *) (uintptr_t) cmd->tag;
939 dprint(FD_NET, "client: got eta tag %p, %d\n", eta, eta->pending);
941 assert(client->eta_in_flight == eta);
943 client->eta_in_flight = NULL;
944 flist_del_init(&client->eta_list);
946 if (client->ops->jobs_eta)
947 client->ops->jobs_eta(client, je);
949 fio_client_sum_jobs_eta(&eta->eta, je);
950 fio_client_dec_jobs_eta(eta, client->ops->eta);
953 static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd)
955 struct cmd_probe_pdu *probe = (struct cmd_probe_pdu *) cmd->payload;
956 const char *os, *arch;
959 os = fio_get_os_string(probe->os);
963 arch = fio_get_arch_string(probe->arch);
967 sprintf(bit, "%d-bit", probe->bpp * 8);
969 log_info("hostname=%s, be=%u, %s, os=%s, arch=%s, fio=%u.%u.%u\n",
970 probe->hostname, probe->bigendian, bit, os, arch,
971 probe->fio_major, probe->fio_minor, probe->fio_patch);
974 client->name = strdup((char *) probe->hostname);
977 static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd)
979 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
981 client->state = Client_started;
982 client->jobs = pdu->jobs;
985 static void handle_stop(struct fio_client *client, struct fio_net_cmd *cmd)
988 log_info("client <%s>: exited with error %d\n", client->hostname, client->error);
991 static void convert_stop(struct fio_net_cmd *cmd)
993 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
995 pdu->error = le32_to_cpu(pdu->error);
998 static void convert_text(struct fio_net_cmd *cmd)
1000 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
1002 pdu->level = le32_to_cpu(pdu->level);
1003 pdu->buf_len = le32_to_cpu(pdu->buf_len);
1004 pdu->log_sec = le64_to_cpu(pdu->log_sec);
1005 pdu->log_usec = le64_to_cpu(pdu->log_usec);
1009 * This has been compressed on the server side, since it can be big.
1012 static struct cmd_iolog_pdu *convert_iolog(struct fio_net_cmd *cmd)
1014 struct cmd_iolog_pdu *pdu = (struct cmd_iolog_pdu *) cmd->payload;
1015 struct cmd_iolog_pdu *ret;
1016 uint32_t nr_samples;
1017 unsigned long total;
1022 stream.zalloc = Z_NULL;
1023 stream.zfree = Z_NULL;
1024 stream.opaque = Z_NULL;
1025 stream.avail_in = 0;
1026 stream.next_in = Z_NULL;
1028 if (inflateInit(&stream) != Z_OK)
1032 * Get header first, it's not compressed
1034 nr_samples = le32_to_cpu(pdu->nr_samples);
1036 total = nr_samples * sizeof(struct io_sample);
1037 ret = malloc(total + sizeof(*pdu));
1038 ret->thread_number = le32_to_cpu(pdu->thread_number);
1039 ret->nr_samples = nr_samples;
1040 ret->log_type = le32_to_cpu(pdu->log_type);
1041 strcpy((char *) ret->name, (char *) pdu->name);
1043 p = (void *) ret + sizeof(*pdu);
1045 stream.avail_in = cmd->pdu_len - sizeof(*pdu);
1046 stream.next_in = (void *) pdu + sizeof(*pdu);
1047 while (stream.avail_in) {
1048 unsigned int this_chunk = 65536;
1049 unsigned int this_len;
1052 if (this_chunk > total)
1055 stream.avail_out = this_chunk;
1056 stream.next_out = p;
1057 err = inflate(&stream, Z_NO_FLUSH);
1058 /* may be Z_OK, or Z_STREAM_END */
1060 log_err("fio: inflate error %d\n", err);
1066 this_len = this_chunk - stream.avail_out;
1071 for (i = 0; i < ret->nr_samples; i++) {
1072 struct io_sample *s = &ret->samples[i];
1074 s->time = le64_to_cpu(s->time);
1075 s->val = le64_to_cpu(s->val);
1076 s->ddir = le32_to_cpu(s->ddir);
1077 s->bs = le32_to_cpu(s->bs);
1081 inflateEnd(&stream);
1085 int fio_handle_client(struct fio_client *client)
1087 struct client_ops *ops = client->ops;
1088 struct fio_net_cmd *cmd;
1090 dprint(FD_NET, "client: handle %s\n", client->hostname);
1092 cmd = fio_net_recv_cmd(client->fd);
1096 dprint(FD_NET, "client: got cmd op %s from %s (pdu=%u)\n",
1097 fio_server_op(cmd->opcode), client->hostname, cmd->pdu_len);
1099 switch (cmd->opcode) {
1100 case FIO_NET_CMD_QUIT:
1102 ops->quit(client, cmd);
1103 remove_client(client);
1106 case FIO_NET_CMD_TEXT:
1108 ops->text(client, cmd);
1111 case FIO_NET_CMD_DU: {
1112 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
1114 convert_dus(&du->dus);
1115 convert_agg(&du->agg);
1117 ops->disk_util(client, cmd);
1121 case FIO_NET_CMD_TS: {
1122 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
1124 convert_ts(&p->ts, &p->ts);
1125 convert_gs(&p->rs, &p->rs);
1127 ops->thread_status(client, cmd);
1131 case FIO_NET_CMD_GS: {
1132 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
1136 ops->group_stats(client, cmd);
1140 case FIO_NET_CMD_ETA: {
1141 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
1143 remove_reply_cmd(client, cmd);
1144 convert_jobs_eta(je);
1145 handle_eta(client, cmd);
1149 case FIO_NET_CMD_PROBE:
1150 remove_reply_cmd(client, cmd);
1151 ops->probe(client, cmd);
1154 case FIO_NET_CMD_SERVER_START:
1155 client->state = Client_running;
1157 ops->job_start(client, cmd);
1160 case FIO_NET_CMD_START: {
1161 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
1163 pdu->jobs = le32_to_cpu(pdu->jobs);
1164 ops->start(client, cmd);
1168 case FIO_NET_CMD_STOP: {
1169 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
1172 client->state = Client_stopped;
1173 client->error = le32_to_cpu(pdu->error);
1174 client->signal = le32_to_cpu(pdu->signal);
1175 ops->stop(client, cmd);
1179 case FIO_NET_CMD_ADD_JOB: {
1180 struct cmd_add_job_pdu *pdu = (struct cmd_add_job_pdu *) cmd->payload;
1182 client->thread_number = le32_to_cpu(pdu->thread_number);
1183 client->groupid = le32_to_cpu(pdu->groupid);
1186 ops->add_job(client, cmd);
1190 case FIO_NET_CMD_IOLOG:
1192 struct cmd_iolog_pdu *pdu;
1194 pdu = convert_iolog(cmd);
1195 ops->iolog(client, pdu);
1199 case FIO_NET_CMD_UPDATE_JOB:
1200 ops->update_job(client, cmd);
1201 remove_reply_cmd(client, cmd);
1205 log_err("fio: unknown client op: %s\n", fio_server_op(cmd->opcode));
1213 static void request_client_etas(struct client_ops *ops)
1215 struct fio_client *client;
1216 struct flist_head *entry;
1217 struct client_eta *eta;
1220 dprint(FD_NET, "client: request eta (%d)\n", nr_clients);
1222 eta = malloc(sizeof(*eta));
1223 memset(&eta->eta, 0, sizeof(eta->eta));
1224 eta->pending = nr_clients;
1226 flist_for_each(entry, &client_list) {
1227 client = flist_entry(entry, struct fio_client, list);
1229 if (!flist_empty(&client->eta_list)) {
1233 if (client->state != Client_running)
1236 assert(!client->eta_in_flight);
1237 flist_add_tail(&client->eta_list, &eta_list);
1238 client->eta_in_flight = eta;
1239 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_SEND_ETA,
1240 (uintptr_t) eta, &client->cmd_list);
1244 fio_client_dec_jobs_eta(eta, ops->eta);
1246 dprint(FD_NET, "client: requested eta tag %p\n", eta);
1249 static int client_check_cmd_timeout(struct fio_client *client,
1250 struct timeval *now)
1252 struct fio_net_cmd_reply *reply;
1253 struct flist_head *entry, *tmp;
1256 flist_for_each_safe(entry, tmp, &client->cmd_list) {
1257 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
1259 if (mtime_since(&reply->tv, now) < FIO_NET_CLIENT_TIMEOUT)
1262 log_err("fio: client %s, timeout on cmd %s\n", client->hostname,
1263 fio_server_op(reply->opcode));
1264 flist_del(&reply->list);
1269 return flist_empty(&client->cmd_list) && ret;
1272 static int fio_check_clients_timed_out(void)
1274 struct fio_client *client;
1275 struct flist_head *entry, *tmp;
1279 gettimeofday(&tv, NULL);
1281 flist_for_each_safe(entry, tmp, &client_list) {
1282 client = flist_entry(entry, struct fio_client, list);
1284 if (flist_empty(&client->cmd_list))
1287 if (!client_check_cmd_timeout(client, &tv))
1290 if (client->ops->timed_out)
1291 client->ops->timed_out(client);
1293 log_err("fio: client %s timed out\n", client->hostname);
1295 remove_client(client);
1302 int fio_handle_clients(struct client_ops *ops)
1304 struct pollfd *pfds;
1305 int i, ret = 0, retval = 0;
1307 gettimeofday(&eta_tv, NULL);
1309 pfds = malloc(nr_clients * sizeof(struct pollfd));
1311 sum_stat_clients = nr_clients;
1312 init_thread_stat(&client_ts);
1313 init_group_run_stat(&client_gs);
1315 while (!exit_backend && nr_clients) {
1316 struct flist_head *entry, *tmp;
1317 struct fio_client *client;
1320 flist_for_each_safe(entry, tmp, &client_list) {
1321 client = flist_entry(entry, struct fio_client, list);
1323 if (!client->sent_job && !client->ops->stay_connected &&
1324 flist_empty(&client->cmd_list)) {
1325 remove_client(client);
1329 pfds[i].fd = client->fd;
1330 pfds[i].events = POLLIN;
1337 assert(i == nr_clients);
1342 gettimeofday(&tv, NULL);
1343 if (mtime_since(&eta_tv, &tv) >= ops->eta_msec) {
1344 request_client_etas(ops);
1345 memcpy(&eta_tv, &tv, sizeof(tv));
1347 if (fio_check_clients_timed_out())
1351 ret = poll(pfds, nr_clients, ops->eta_msec);
1355 log_err("fio: poll clients: %s\n", strerror(errno));
1361 for (i = 0; i < nr_clients; i++) {
1362 if (!(pfds[i].revents & POLLIN))
1365 client = find_client_by_fd(pfds[i].fd);
1367 log_err("fio: unknown client fd %d\n", pfds[i].fd);
1370 if (!fio_handle_client(client)) {
1371 log_info("client: host=%s disconnected\n",
1373 remove_client(client);
1375 } else if (client->error)
1377 fio_put_client(client);