11 #include <sys/socket.h>
13 #include <netinet/in.h>
14 #include <arpa/inet.h>
24 static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd);
25 static void handle_ts(struct fio_client *client, struct fio_net_cmd *cmd);
26 static void handle_gs(struct fio_client *client, struct fio_net_cmd *cmd);
27 static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd);
28 static void handle_text(struct fio_client *client, struct fio_net_cmd *cmd);
29 static void handle_stop(struct fio_client *client, struct fio_net_cmd *cmd);
30 static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd);
32 struct client_ops fio_client_ops = {
34 .disk_util = handle_du,
35 .thread_status = handle_ts,
36 .group_stats = handle_gs,
38 .start = handle_start,
39 .eta = display_thread_status,
40 .probe = handle_probe,
41 .eta_msec = FIO_CLIENT_DEF_ETA_MSEC,
42 .client_type = FIO_CLIENT_TYPE_CLI,
45 static struct timeval eta_tv;
47 static FLIST_HEAD(client_list);
48 static FLIST_HEAD(eta_list);
50 static FLIST_HEAD(arg_list);
52 struct thread_stat client_ts;
53 struct group_run_stats client_gs;
56 static int sum_stat_nr;
58 #define FIO_CLIENT_HASH_BITS 7
59 #define FIO_CLIENT_HASH_SZ (1 << FIO_CLIENT_HASH_BITS)
60 #define FIO_CLIENT_HASH_MASK (FIO_CLIENT_HASH_SZ - 1)
61 static struct flist_head client_hash[FIO_CLIENT_HASH_SZ];
63 static void fio_client_add_hash(struct fio_client *client)
65 int bucket = hash_long(client->fd, FIO_CLIENT_HASH_BITS);
67 bucket &= FIO_CLIENT_HASH_MASK;
68 flist_add(&client->hash_list, &client_hash[bucket]);
71 static void fio_client_remove_hash(struct fio_client *client)
73 if (!flist_empty(&client->hash_list))
74 flist_del_init(&client->hash_list);
77 static void fio_init fio_client_hash_init(void)
81 for (i = 0; i < FIO_CLIENT_HASH_SZ; i++)
82 INIT_FLIST_HEAD(&client_hash[i]);
85 static struct fio_client *find_client_by_fd(int fd)
87 int bucket = hash_long(fd, FIO_CLIENT_HASH_BITS) & FIO_CLIENT_HASH_MASK;
88 struct fio_client *client;
89 struct flist_head *entry;
91 flist_for_each(entry, &client_hash[bucket]) {
92 client = flist_entry(entry, struct fio_client, hash_list);
94 if (client->fd == fd) {
103 void fio_put_client(struct fio_client *client)
108 free(client->hostname);
117 static void remove_client(struct fio_client *client)
119 assert(client->refs);
121 dprint(FD_NET, "client: removed <%s>\n", client->hostname);
123 if (!flist_empty(&client->list))
124 flist_del_init(&client->list);
126 fio_client_remove_hash(client);
128 if (!flist_empty(&client->eta_list)) {
129 flist_del_init(&client->eta_list);
130 fio_client_dec_jobs_eta(client->eta_in_flight, client->ops->eta);
136 fio_put_client(client);
139 struct fio_client *fio_get_client(struct fio_client *client)
145 static void __fio_client_add_cmd_option(struct fio_client *client,
150 index = client->argc++;
151 client->argv = realloc(client->argv, sizeof(char *) * client->argc);
152 client->argv[index] = strdup(opt);
153 dprint(FD_NET, "client: add cmd %d: %s\n", index, opt);
156 void fio_client_add_cmd_option(void *cookie, const char *opt)
158 struct fio_client *client = cookie;
159 struct flist_head *entry;
164 __fio_client_add_cmd_option(client, opt);
167 * Duplicate arguments to shared client group
169 flist_for_each(entry, &arg_list) {
170 client = flist_entry(entry, struct fio_client, arg_list);
172 __fio_client_add_cmd_option(client, opt);
176 struct fio_client *fio_client_add_explicit(struct client_ops *ops,
177 const char *hostname, int type,
180 struct fio_client *client;
182 client = malloc(sizeof(*client));
183 memset(client, 0, sizeof(*client));
185 INIT_FLIST_HEAD(&client->list);
186 INIT_FLIST_HEAD(&client->hash_list);
187 INIT_FLIST_HEAD(&client->arg_list);
188 INIT_FLIST_HEAD(&client->eta_list);
189 INIT_FLIST_HEAD(&client->cmd_list);
191 client->hostname = strdup(hostname);
193 if (type == Fio_client_socket)
198 ipv6 = type == Fio_client_ipv6;
199 if (fio_server_parse_host(hostname, &ipv6,
200 &client->addr.sin_addr,
201 &client->addr6.sin6_addr))
210 client->type = ops->client_type;
212 __fio_client_add_cmd_option(client, "fio");
214 flist_add(&client->list, &client_list);
216 dprint(FD_NET, "client: added <%s>\n", client->hostname);
223 int fio_client_add(struct client_ops *ops, const char *hostname, void **cookie)
225 struct fio_client *existing = *cookie;
226 struct fio_client *client;
230 * We always add our "exec" name as the option, hence 1
233 if (existing->argc == 1)
234 flist_add_tail(&existing->arg_list, &arg_list);
236 while (!flist_empty(&arg_list))
237 flist_del_init(arg_list.next);
241 client = malloc(sizeof(*client));
242 memset(client, 0, sizeof(*client));
244 INIT_FLIST_HEAD(&client->list);
245 INIT_FLIST_HEAD(&client->hash_list);
246 INIT_FLIST_HEAD(&client->arg_list);
247 INIT_FLIST_HEAD(&client->eta_list);
248 INIT_FLIST_HEAD(&client->cmd_list);
250 if (fio_server_parse_string(hostname, &client->hostname,
251 &client->is_sock, &client->port,
252 &client->addr.sin_addr,
253 &client->addr6.sin6_addr,
260 client->type = ops->client_type;
262 __fio_client_add_cmd_option(client, "fio");
264 flist_add(&client->list, &client_list);
266 dprint(FD_NET, "client: added <%s>\n", client->hostname);
271 static void probe_client(struct fio_client *client)
273 dprint(FD_NET, "client: send probe\n");
275 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_PROBE, 0, &client->cmd_list);
278 static int fio_client_connect_ip(struct fio_client *client)
280 struct sockaddr *addr;
281 fio_socklen_t socklen;
285 client->addr6.sin6_family = AF_INET6;
286 client->addr6.sin6_port = htons(client->port);
288 addr = (struct sockaddr *) &client->addr6;
289 socklen = sizeof(client->addr6);
291 client->addr.sin_family = AF_INET;
292 client->addr.sin_port = htons(client->port);
294 addr = (struct sockaddr *) &client->addr;
295 socklen = sizeof(client->addr);
298 fd = socket(domain, SOCK_STREAM, 0);
302 log_err("fio: socket: %s\n", strerror(errno));
306 if (connect(fd, addr, socklen) < 0) {
309 log_err("fio: connect: %s\n", strerror(errno));
310 log_err("fio: failed to connect to %s:%u\n", client->hostname,
319 static int fio_client_connect_sock(struct fio_client *client)
321 struct sockaddr_un *addr = &client->addr_un;
325 memset(addr, 0, sizeof(*addr));
326 addr->sun_family = AF_UNIX;
327 strcpy(addr->sun_path, client->hostname);
329 fd = socket(AF_UNIX, SOCK_STREAM, 0);
333 log_err("fio: socket: %s\n", strerror(errno));
337 len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
338 if (connect(fd, (struct sockaddr *) addr, len) < 0) {
341 log_err("fio: connect; %s\n", strerror(errno));
349 int fio_client_connect(struct fio_client *client)
353 dprint(FD_NET, "client: connect to host %s\n", client->hostname);
356 fd = fio_client_connect_sock(client);
358 fd = fio_client_connect_ip(client);
360 dprint(FD_NET, "client: %s connected %d\n", client->hostname, fd);
366 fio_client_add_hash(client);
367 client->state = Client_connected;
369 probe_client(client);
373 void fio_client_terminate(struct fio_client *client)
375 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_QUIT, 0, NULL);
378 void fio_clients_terminate(void)
380 struct flist_head *entry;
381 struct fio_client *client;
383 dprint(FD_NET, "client: terminate clients\n");
385 flist_for_each(entry, &client_list) {
386 client = flist_entry(entry, struct fio_client, list);
387 fio_client_terminate(client);
391 static void sig_int(int sig)
393 dprint(FD_NET, "client: got signal %d\n", sig);
394 fio_clients_terminate();
397 static void client_signal_handler(void)
399 struct sigaction act;
401 memset(&act, 0, sizeof(act));
402 act.sa_handler = sig_int;
403 act.sa_flags = SA_RESTART;
404 sigaction(SIGINT, &act, NULL);
406 memset(&act, 0, sizeof(act));
407 act.sa_handler = sig_int;
408 act.sa_flags = SA_RESTART;
409 sigaction(SIGTERM, &act, NULL);
412 static int send_client_cmd_line(struct fio_client *client)
414 struct cmd_single_line_pdu *cslp;
415 struct cmd_line_pdu *clp;
416 unsigned long offset;
422 dprint(FD_NET, "client: send cmdline %d\n", client->argc);
424 lens = malloc(client->argc * sizeof(unsigned int));
427 * Find out how much mem we need
429 for (i = 0, mem = 0; i < client->argc; i++) {
430 lens[i] = strlen(client->argv[i]) + 1;
435 * We need one cmd_line_pdu, and argc number of cmd_single_line_pdu
437 mem += sizeof(*clp) + (client->argc * sizeof(*cslp));
441 offset = sizeof(*clp);
443 for (i = 0; i < client->argc; i++) {
444 uint16_t arg_len = lens[i];
447 strcpy((char *) cslp->text, client->argv[i]);
448 cslp->len = cpu_to_le16(arg_len);
449 offset += sizeof(*cslp) + arg_len;
453 clp->lines = cpu_to_le16(client->argc);
454 clp->client_type = __cpu_to_le16(client->type);
455 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOBLINE, pdu, mem, 0);
460 int fio_clients_connect(void)
462 struct fio_client *client;
463 struct flist_head *entry, *tmp;
468 WSAStartup(MAKEWORD(2,2), &wsd);
471 dprint(FD_NET, "client: connect all\n");
473 client_signal_handler();
475 flist_for_each_safe(entry, tmp, &client_list) {
476 client = flist_entry(entry, struct fio_client, list);
478 ret = fio_client_connect(client);
480 remove_client(client);
484 if (client->argc > 1)
485 send_client_cmd_line(client);
491 int fio_start_client(struct fio_client *client)
493 dprint(FD_NET, "client: start %s\n", client->hostname);
494 return fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_RUN, 0, NULL);
497 int fio_start_all_clients(void)
499 struct fio_client *client;
500 struct flist_head *entry, *tmp;
503 dprint(FD_NET, "client: start all\n");
505 flist_for_each_safe(entry, tmp, &client_list) {
506 client = flist_entry(entry, struct fio_client, list);
508 ret = fio_start_client(client);
510 remove_client(client);
515 return flist_empty(&client_list);
519 * Send file contents to server backend. We could use sendfile(), but to remain
520 * more portable lets just read/write the darn thing.
522 static int __fio_client_send_ini(struct fio_client *client, const char *filename)
524 struct cmd_job_pdu *pdu;
532 dprint(FD_NET, "send ini %s to %s\n", filename, client->hostname);
534 fd = open(filename, O_RDONLY);
538 log_err("fio: job file <%s> open: %s\n", filename, strerror(errno));
542 if (fstat(fd, &sb) < 0) {
545 log_err("fio: job file stat: %s\n", strerror(errno));
550 p_size = sb.st_size + sizeof(*pdu);
551 pdu = malloc(p_size);
557 ret = read(fd, p, len);
566 else if (errno == EAGAIN || errno == EINTR)
571 log_err("fio: failed reading job file %s\n", filename);
577 pdu->buf_len = __cpu_to_le32(sb.st_size);
578 pdu->client_type = cpu_to_le32(client->type);
580 client->sent_job = 1;
581 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, pdu, p_size, 0);
587 int fio_client_send_ini(struct fio_client *client, const char *filename)
591 ret = __fio_client_send_ini(client, filename);
593 client->sent_job = 1;
598 int fio_clients_send_ini(const char *filename)
600 struct fio_client *client;
601 struct flist_head *entry, *tmp;
603 flist_for_each_safe(entry, tmp, &client_list) {
604 client = flist_entry(entry, struct fio_client, list);
606 if (fio_client_send_ini(client, filename))
607 remove_client(client);
613 static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
615 dst->max_val = le64_to_cpu(src->max_val);
616 dst->min_val = le64_to_cpu(src->min_val);
617 dst->samples = le64_to_cpu(src->samples);
620 * Floats arrive as IEEE 754 encoded uint64_t, convert back to double
622 dst->mean.u.f = fio_uint64_to_double(le64_to_cpu(dst->mean.u.i));
623 dst->S.u.f = fio_uint64_to_double(le64_to_cpu(dst->S.u.i));
626 static void convert_ts(struct thread_stat *dst, struct thread_stat *src)
630 dst->error = le32_to_cpu(src->error);
631 dst->groupid = le32_to_cpu(src->groupid);
632 dst->pid = le32_to_cpu(src->pid);
633 dst->members = le32_to_cpu(src->members);
635 for (i = 0; i < 2; i++) {
636 convert_io_stat(&dst->clat_stat[i], &src->clat_stat[i]);
637 convert_io_stat(&dst->slat_stat[i], &src->slat_stat[i]);
638 convert_io_stat(&dst->lat_stat[i], &src->lat_stat[i]);
639 convert_io_stat(&dst->bw_stat[i], &src->bw_stat[i]);
642 dst->usr_time = le64_to_cpu(src->usr_time);
643 dst->sys_time = le64_to_cpu(src->sys_time);
644 dst->ctx = le64_to_cpu(src->ctx);
645 dst->minf = le64_to_cpu(src->minf);
646 dst->majf = le64_to_cpu(src->majf);
647 dst->clat_percentiles = le64_to_cpu(src->clat_percentiles);
649 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
650 fio_fp64_t *fps = &src->percentile_list[i];
651 fio_fp64_t *fpd = &dst->percentile_list[i];
653 fpd->u.f = fio_uint64_to_double(le64_to_cpu(fps->u.i));
656 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
657 dst->io_u_map[i] = le32_to_cpu(src->io_u_map[i]);
658 dst->io_u_submit[i] = le32_to_cpu(src->io_u_submit[i]);
659 dst->io_u_complete[i] = le32_to_cpu(src->io_u_complete[i]);
662 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
663 dst->io_u_lat_u[i] = le32_to_cpu(src->io_u_lat_u[i]);
664 dst->io_u_lat_m[i] = le32_to_cpu(src->io_u_lat_m[i]);
667 for (i = 0; i < 2; i++)
668 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
669 dst->io_u_plat[i][j] = le32_to_cpu(src->io_u_plat[i][j]);
671 for (i = 0; i < 3; i++) {
672 dst->total_io_u[i] = le64_to_cpu(src->total_io_u[i]);
673 dst->short_io_u[i] = le64_to_cpu(src->short_io_u[i]);
676 dst->total_submit = le64_to_cpu(src->total_submit);
677 dst->total_complete = le64_to_cpu(src->total_complete);
679 for (i = 0; i < 2; i++) {
680 dst->io_bytes[i] = le64_to_cpu(src->io_bytes[i]);
681 dst->runtime[i] = le64_to_cpu(src->runtime[i]);
684 dst->total_run_time = le64_to_cpu(src->total_run_time);
685 dst->continue_on_error = le16_to_cpu(src->continue_on_error);
686 dst->total_err_count = le64_to_cpu(src->total_err_count);
687 dst->first_error = le32_to_cpu(src->first_error);
688 dst->kb_base = le32_to_cpu(src->kb_base);
691 static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
695 for (i = 0; i < 2; i++) {
696 dst->max_run[i] = le64_to_cpu(src->max_run[i]);
697 dst->min_run[i] = le64_to_cpu(src->min_run[i]);
698 dst->max_bw[i] = le64_to_cpu(src->max_bw[i]);
699 dst->min_bw[i] = le64_to_cpu(src->min_bw[i]);
700 dst->io_kb[i] = le64_to_cpu(src->io_kb[i]);
701 dst->agg[i] = le64_to_cpu(src->agg[i]);
704 dst->kb_base = le32_to_cpu(src->kb_base);
705 dst->groupid = le32_to_cpu(src->groupid);
708 static void handle_ts(struct fio_client *client, struct fio_net_cmd *cmd)
710 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
712 show_thread_status(&p->ts, &p->rs);
714 if (sum_stat_clients == 1)
717 sum_thread_stats(&client_ts, &p->ts, sum_stat_nr);
718 sum_group_stats(&client_gs, &p->rs);
721 client_ts.groupid = p->ts.groupid;
723 if (++sum_stat_nr == sum_stat_clients) {
724 strcpy(client_ts.name, "All clients");
725 show_thread_status(&client_ts, &client_gs);
729 static void handle_gs(struct fio_client *client, struct fio_net_cmd *cmd)
731 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
733 show_group_stats(gs);
736 static void handle_text(struct fio_client *client, struct fio_net_cmd *cmd)
738 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
739 const char *buf = (const char *) pdu->buf;
743 name = client->name ? client->name : client->hostname;
745 if (!client->skip_newline)
746 fprintf(f_out, "<%s> ", name);
747 ret = fwrite(buf, pdu->buf_len, 1, f_out);
749 client->skip_newline = strchr(buf, '\n') == NULL;
752 static void convert_agg(struct disk_util_agg *agg)
756 for (i = 0; i < 2; i++) {
757 agg->ios[i] = le32_to_cpu(agg->ios[i]);
758 agg->merges[i] = le32_to_cpu(agg->merges[i]);
759 agg->sectors[i] = le64_to_cpu(agg->sectors[i]);
760 agg->ticks[i] = le32_to_cpu(agg->ticks[i]);
763 agg->io_ticks = le32_to_cpu(agg->io_ticks);
764 agg->time_in_queue = le32_to_cpu(agg->time_in_queue);
765 agg->slavecount = le32_to_cpu(agg->slavecount);
766 agg->max_util.u.f = fio_uint64_to_double(__le64_to_cpu(agg->max_util.u.i));
769 static void convert_dus(struct disk_util_stat *dus)
773 for (i = 0; i < 2; i++) {
774 dus->ios[i] = le32_to_cpu(dus->ios[i]);
775 dus->merges[i] = le32_to_cpu(dus->merges[i]);
776 dus->sectors[i] = le64_to_cpu(dus->sectors[i]);
777 dus->ticks[i] = le32_to_cpu(dus->ticks[i]);
780 dus->io_ticks = le32_to_cpu(dus->io_ticks);
781 dus->time_in_queue = le32_to_cpu(dus->time_in_queue);
782 dus->msec = le64_to_cpu(dus->msec);
785 static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd)
787 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
789 if (!client->disk_stats_shown) {
790 client->disk_stats_shown = 1;
791 log_info("\nDisk stats (read/write):\n");
794 print_disk_util(&du->dus, &du->agg, terse_output);
797 static void convert_jobs_eta(struct jobs_eta *je)
801 je->nr_running = le32_to_cpu(je->nr_running);
802 je->nr_ramp = le32_to_cpu(je->nr_ramp);
803 je->nr_pending = le32_to_cpu(je->nr_pending);
804 je->files_open = le32_to_cpu(je->files_open);
806 for (i = 0; i < 2; i++) {
807 je->m_rate[i] = le32_to_cpu(je->m_rate[i]);
808 je->t_rate[i] = le32_to_cpu(je->t_rate[i]);
809 je->m_iops[i] = le32_to_cpu(je->m_iops[i]);
810 je->t_iops[i] = le32_to_cpu(je->t_iops[i]);
811 je->rate[i] = le32_to_cpu(je->rate[i]);
812 je->iops[i] = le32_to_cpu(je->iops[i]);
815 je->elapsed_sec = le64_to_cpu(je->elapsed_sec);
816 je->eta_sec = le64_to_cpu(je->eta_sec);
817 je->nr_threads = le32_to_cpu(je->nr_threads);
820 void fio_client_sum_jobs_eta(struct jobs_eta *dst, struct jobs_eta *je)
824 dst->nr_running += je->nr_running;
825 dst->nr_ramp += je->nr_ramp;
826 dst->nr_pending += je->nr_pending;
827 dst->files_open += je->files_open;
829 for (i = 0; i < 2; i++) {
830 dst->m_rate[i] += je->m_rate[i];
831 dst->t_rate[i] += je->t_rate[i];
832 dst->m_iops[i] += je->m_iops[i];
833 dst->t_iops[i] += je->t_iops[i];
834 dst->rate[i] += je->rate[i];
835 dst->iops[i] += je->iops[i];
838 dst->elapsed_sec += je->elapsed_sec;
840 if (je->eta_sec > dst->eta_sec)
841 dst->eta_sec = je->eta_sec;
843 dst->nr_threads += je->nr_threads;
844 /* we need to handle je->run_str too ... */
847 void fio_client_dec_jobs_eta(struct client_eta *eta, client_eta_op eta_fn)
849 if (!--eta->pending) {
855 static void remove_reply_cmd(struct fio_client *client, struct fio_net_cmd *cmd)
857 struct fio_net_int_cmd *icmd = NULL;
858 struct flist_head *entry;
860 flist_for_each(entry, &client->cmd_list) {
861 icmd = flist_entry(entry, struct fio_net_int_cmd, list);
863 if (cmd->tag == (uintptr_t) icmd)
870 log_err("fio: client: unable to find matching tag\n");
874 flist_del(&icmd->list);
875 cmd->tag = icmd->saved_tag;
879 static void handle_eta(struct fio_client *client, struct fio_net_cmd *cmd)
881 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
882 struct client_eta *eta = (struct client_eta *) (uintptr_t) cmd->tag;
884 dprint(FD_NET, "client: got eta tag %p, %d\n", eta, eta->pending);
886 assert(client->eta_in_flight == eta);
888 client->eta_in_flight = NULL;
889 flist_del_init(&client->eta_list);
891 if (client->ops->jobs_eta)
892 client->ops->jobs_eta(client, je);
894 fio_client_sum_jobs_eta(&eta->eta, je);
895 fio_client_dec_jobs_eta(eta, client->ops->eta);
898 static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd)
900 struct cmd_probe_pdu *probe = (struct cmd_probe_pdu *) cmd->payload;
901 const char *os, *arch;
904 os = fio_get_os_string(probe->os);
908 arch = fio_get_arch_string(probe->arch);
912 sprintf(bit, "%d-bit", probe->bpp * 8);
914 log_info("hostname=%s, be=%u, %s, os=%s, arch=%s, fio=%u.%u.%u\n",
915 probe->hostname, probe->bigendian, bit, os, arch,
916 probe->fio_major, probe->fio_minor, probe->fio_patch);
919 client->name = strdup((char *) probe->hostname);
922 static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd)
924 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
926 client->state = Client_started;
927 client->jobs = pdu->jobs;
930 static void handle_stop(struct fio_client *client, struct fio_net_cmd *cmd)
933 log_info("client <%s>: exited with error %d\n", client->hostname, client->error);
936 static void convert_stop(struct fio_net_cmd *cmd)
938 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
940 pdu->error = le32_to_cpu(pdu->error);
943 static void convert_text(struct fio_net_cmd *cmd)
945 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
947 pdu->level = le32_to_cpu(pdu->level);
948 pdu->buf_len = le32_to_cpu(pdu->buf_len);
949 pdu->log_sec = le64_to_cpu(pdu->log_sec);
950 pdu->log_usec = le64_to_cpu(pdu->log_usec);
953 int fio_handle_client(struct fio_client *client)
955 struct client_ops *ops = client->ops;
956 struct fio_net_cmd *cmd;
958 dprint(FD_NET, "client: handle %s\n", client->hostname);
960 cmd = fio_net_recv_cmd(client->fd);
964 dprint(FD_NET, "client: got cmd op %s from %s (pdu=%u)\n",
965 fio_server_op(cmd->opcode), client->hostname, cmd->pdu_len);
967 switch (cmd->opcode) {
968 case FIO_NET_CMD_QUIT:
970 ops->quit(client, cmd);
971 remove_client(client);
974 case FIO_NET_CMD_TEXT:
976 ops->text(client, cmd);
979 case FIO_NET_CMD_DU: {
980 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
982 convert_dus(&du->dus);
983 convert_agg(&du->agg);
985 ops->disk_util(client, cmd);
989 case FIO_NET_CMD_TS: {
990 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
992 convert_ts(&p->ts, &p->ts);
993 convert_gs(&p->rs, &p->rs);
995 ops->thread_status(client, cmd);
999 case FIO_NET_CMD_GS: {
1000 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
1004 ops->group_stats(client, cmd);
1008 case FIO_NET_CMD_ETA: {
1009 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
1011 remove_reply_cmd(client, cmd);
1012 convert_jobs_eta(je);
1013 handle_eta(client, cmd);
1017 case FIO_NET_CMD_PROBE:
1018 remove_reply_cmd(client, cmd);
1019 ops->probe(client, cmd);
1022 case FIO_NET_CMD_SERVER_START:
1023 client->state = Client_running;
1025 ops->job_start(client, cmd);
1028 case FIO_NET_CMD_START: {
1029 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
1031 pdu->jobs = le32_to_cpu(pdu->jobs);
1032 ops->start(client, cmd);
1036 case FIO_NET_CMD_STOP: {
1037 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
1040 client->state = Client_stopped;
1041 client->error = pdu->error;
1042 ops->stop(client, cmd);
1046 case FIO_NET_CMD_ADD_JOB:
1048 ops->add_job(client, cmd);
1052 log_err("fio: unknown client op: %s\n", fio_server_op(cmd->opcode));
1060 static void request_client_etas(struct client_ops *ops)
1062 struct fio_client *client;
1063 struct flist_head *entry;
1064 struct client_eta *eta;
1067 dprint(FD_NET, "client: request eta (%d)\n", nr_clients);
1069 eta = malloc(sizeof(*eta));
1070 memset(&eta->eta, 0, sizeof(eta->eta));
1071 eta->pending = nr_clients;
1073 flist_for_each(entry, &client_list) {
1074 client = flist_entry(entry, struct fio_client, list);
1076 if (!flist_empty(&client->eta_list)) {
1080 if (client->state != Client_running)
1083 assert(!client->eta_in_flight);
1084 flist_add_tail(&client->eta_list, &eta_list);
1085 client->eta_in_flight = eta;
1086 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_SEND_ETA,
1087 (uintptr_t) eta, &client->cmd_list);
1091 fio_client_dec_jobs_eta(eta, ops->eta);
1093 dprint(FD_NET, "client: requested eta tag %p\n", eta);
1096 static int client_check_cmd_timeout(struct fio_client *client,
1097 struct timeval *now)
1099 struct fio_net_int_cmd *cmd;
1100 struct flist_head *entry, *tmp;
1103 flist_for_each_safe(entry, tmp, &client->cmd_list) {
1104 cmd = flist_entry(entry, struct fio_net_int_cmd, list);
1106 if (mtime_since(&cmd->tv, now) < FIO_NET_CLIENT_TIMEOUT)
1109 log_err("fio: client %s, timeout on cmd %s\n", client->hostname,
1110 fio_server_op(cmd->cmd.opcode));
1111 flist_del(&cmd->list);
1116 return flist_empty(&client->cmd_list) && ret;
1119 static int fio_check_clients_timed_out(void)
1121 struct fio_client *client;
1122 struct flist_head *entry, *tmp;
1126 gettimeofday(&tv, NULL);
1128 flist_for_each_safe(entry, tmp, &client_list) {
1129 client = flist_entry(entry, struct fio_client, list);
1131 if (flist_empty(&client->cmd_list))
1134 if (!client_check_cmd_timeout(client, &tv))
1137 if (client->ops->timed_out)
1138 client->ops->timed_out(client);
1140 log_err("fio: client %s timed out\n", client->hostname);
1142 remove_client(client);
1149 int fio_handle_clients(struct client_ops *ops)
1151 struct pollfd *pfds;
1152 int i, ret = 0, retval = 0;
1154 gettimeofday(&eta_tv, NULL);
1156 pfds = malloc(nr_clients * sizeof(struct pollfd));
1158 sum_stat_clients = nr_clients;
1159 init_thread_stat(&client_ts);
1160 init_group_run_stat(&client_gs);
1162 while (!exit_backend && nr_clients) {
1163 struct flist_head *entry, *tmp;
1164 struct fio_client *client;
1167 flist_for_each_safe(entry, tmp, &client_list) {
1168 client = flist_entry(entry, struct fio_client, list);
1170 if (!client->sent_job && !client->ops->stay_connected &&
1171 flist_empty(&client->cmd_list)) {
1172 remove_client(client);
1176 pfds[i].fd = client->fd;
1177 pfds[i].events = POLLIN;
1184 assert(i == nr_clients);
1189 gettimeofday(&tv, NULL);
1190 if (mtime_since(&eta_tv, &tv) >= ops->eta_msec) {
1191 request_client_etas(ops);
1192 memcpy(&eta_tv, &tv, sizeof(tv));
1194 if (fio_check_clients_timed_out())
1198 ret = poll(pfds, nr_clients, 100);
1202 log_err("fio: poll clients: %s\n", strerror(errno));
1208 for (i = 0; i < nr_clients; i++) {
1209 if (!(pfds[i].revents & POLLIN))
1212 client = find_client_by_fd(pfds[i].fd);
1214 log_err("fio: unknown client fd %d\n", pfds[i].fd);
1217 if (!fio_handle_client(client)) {
1218 log_info("client: host=%s disconnected\n",
1220 remove_client(client);
1222 } else if (client->error)
1224 fio_put_client(client);