11 #include <sys/socket.h>
13 #include <netinet/in.h>
14 #include <arpa/inet.h>
24 struct flist_head list;
25 struct flist_head hash_list;
26 struct sockaddr_in addr;
27 struct sockaddr_un addr_un;
50 static FLIST_HEAD(client_list);
52 #define FIO_CLIENT_HASH_BITS 7
53 #define FIO_CLIENT_HASH_SZ (1 << FIO_CLIENT_HASH_BITS)
54 #define FIO_CLIENT_HASH_MASK (FIO_CLIENT_HASH_SZ - 1)
55 static struct flist_head client_hash[FIO_CLIENT_HASH_SZ];
57 static int handle_client(struct fio_client *client);
59 static void fio_client_add_hash(struct fio_client *client)
61 int bucket = hash_long(client->fd, FIO_CLIENT_HASH_BITS);
63 bucket &= FIO_CLIENT_HASH_MASK;
64 flist_add(&client->hash_list, &client_hash[bucket]);
67 static void fio_client_remove_hash(struct fio_client *client)
69 if (!flist_empty(&client->hash_list))
70 flist_del_init(&client->hash_list);
73 static void fio_init fio_client_hash_init(void)
77 for (i = 0; i < FIO_CLIENT_HASH_SZ; i++)
78 INIT_FLIST_HEAD(&client_hash[i]);
81 static struct fio_client *find_client_by_fd(int fd)
83 int bucket = hash_long(fd, FIO_CLIENT_HASH_BITS) & FIO_CLIENT_HASH_MASK;
84 struct fio_client *client;
85 struct flist_head *entry;
87 flist_for_each(entry, &client_hash[bucket]) {
88 client = flist_entry(entry, struct fio_client, hash_list);
97 static void remove_client(struct fio_client *client)
99 dprint(FD_NET, "client: removed <%s>\n", client->hostname);
100 flist_del(&client->list);
102 fio_client_remove_hash(client);
104 free(client->hostname);
114 static void __fio_client_add_cmd_option(struct fio_client *client,
119 index = client->argc++;
120 client->argv = realloc(client->argv, sizeof(char *) * client->argc);
121 client->argv[index] = strdup(opt);
122 dprint(FD_NET, "client: add cmd %d: %s\n", index, opt);
125 void fio_client_add_cmd_option(void *cookie, const char *opt)
127 struct fio_client *client = cookie;
132 __fio_client_add_cmd_option(client, opt);
135 int fio_client_add(const char *hostname, void **cookie)
137 struct fio_client *client;
139 client = malloc(sizeof(*client));
140 memset(client, 0, sizeof(*client));
142 INIT_FLIST_HEAD(&client->list);
143 INIT_FLIST_HEAD(&client->hash_list);
145 if (fio_server_parse_string(hostname, &client->hostname,
146 &client->is_sock, &client->port,
147 &client->addr.sin_addr))
152 __fio_client_add_cmd_option(client, "fio");
154 flist_add(&client->list, &client_list);
156 dprint(FD_NET, "client: added <%s>\n", client->hostname);
161 static int fio_client_connect_ip(struct fio_client *client)
165 client->addr.sin_family = AF_INET;
166 client->addr.sin_port = htons(client->port);
168 fd = socket(AF_INET, SOCK_STREAM, 0);
170 log_err("fio: socket: %s\n", strerror(errno));
174 if (connect(fd, (struct sockaddr *) &client->addr, sizeof(client->addr)) < 0) {
175 log_err("fio: connect: %s\n", strerror(errno));
176 log_err("fio: failed to connect to %s:%u\n", client->hostname,
185 static int fio_client_connect_sock(struct fio_client *client)
187 struct sockaddr_un *addr = &client->addr_un;
191 memset(addr, 0, sizeof(*addr));
192 addr->sun_family = AF_UNIX;
193 strcpy(addr->sun_path, client->hostname);
195 fd = socket(AF_UNIX, SOCK_STREAM, 0);
197 log_err("fio: socket: %s\n", strerror(errno));
201 len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
202 if (connect(fd, (struct sockaddr *) addr, len) < 0) {
203 log_err("fio: connect; %s\n", strerror(errno));
211 static int fio_client_connect(struct fio_client *client)
215 dprint(FD_NET, "client: connect to host %s\n", client->hostname);
218 fd = fio_client_connect_sock(client);
220 fd = fio_client_connect_ip(client);
226 fio_client_add_hash(client);
227 client->state = Client_connected;
231 void fio_clients_terminate(void)
233 struct flist_head *entry;
234 struct fio_client *client;
236 dprint(FD_NET, "client: terminate clients\n");
238 flist_for_each(entry, &client_list) {
239 client = flist_entry(entry, struct fio_client, list);
241 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_QUIT, 0);
245 static void sig_int(int sig)
247 dprint(FD_NET, "client: got signal %d\n", sig);
248 fio_clients_terminate();
251 static void client_signal_handler(void)
253 struct sigaction act;
255 memset(&act, 0, sizeof(act));
256 act.sa_handler = sig_int;
257 act.sa_flags = SA_RESTART;
258 sigaction(SIGINT, &act, NULL);
260 memset(&act, 0, sizeof(act));
261 act.sa_handler = sig_int;
262 act.sa_flags = SA_RESTART;
263 sigaction(SIGTERM, &act, NULL);
266 static void probe_client(struct fio_client *client)
268 dprint(FD_NET, "client: send probe\n");
270 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_PROBE, 0);
271 handle_client(client);
274 static int send_client_cmd_line(struct fio_client *client)
276 struct cmd_single_line_pdu *cslp;
277 struct cmd_line_pdu *clp;
278 unsigned long offset;
283 dprint(FD_NET, "client: send cmdline %d\n", client->argc);
286 * Find out how much mem we need
288 for (i = 0, mem = 0; i < client->argc; i++)
289 mem += strlen(client->argv[i]) + 1;
292 * We need one cmd_line_pdu, and argc number of cmd_single_line_pdu
294 mem += sizeof(*clp) + (client->argc * sizeof(*cslp));
298 offset = sizeof(*clp);
300 for (i = 0; i < client->argc; i++) {
301 uint16_t arg_len = strlen(client->argv[i]) + 1;
304 strcpy((char *) cslp->text, client->argv[i]);
305 cslp->len = cpu_to_le16(arg_len);
306 offset += sizeof(*cslp) + arg_len;
309 clp->lines = cpu_to_le16(client->argc);
310 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOBLINE, pdu, mem);
315 int fio_clients_connect(void)
317 struct fio_client *client;
318 struct flist_head *entry, *tmp;
321 dprint(FD_NET, "client: connect all\n");
323 client_signal_handler();
325 flist_for_each_safe(entry, tmp, &client_list) {
326 client = flist_entry(entry, struct fio_client, list);
328 ret = fio_client_connect(client);
330 remove_client(client);
334 probe_client(client);
336 if (client->argc > 1)
337 send_client_cmd_line(client);
344 * Send file contents to server backend. We could use sendfile(), but to remain
345 * more portable lets just read/write the darn thing.
347 static int fio_client_send_ini(struct fio_client *client, const char *filename)
354 dprint(FD_NET, "send ini %s to %s\n", filename, client->hostname);
356 fd = open(filename, O_RDONLY);
358 log_err("fio: job file <%s> open: %s\n", filename, strerror(errno));
362 if (fstat(fd, &sb) < 0) {
363 log_err("fio: job file stat: %s\n", strerror(errno));
368 buf = malloc(sb.st_size);
373 ret = read(fd, p, len);
382 else if (errno == EAGAIN || errno == EINTR)
387 log_err("fio: failed reading job file %s\n", filename);
393 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, buf, sb.st_size);
399 int fio_clients_send_ini(const char *filename)
401 struct fio_client *client;
402 struct flist_head *entry, *tmp;
404 flist_for_each_safe(entry, tmp, &client_list) {
405 client = flist_entry(entry, struct fio_client, list);
407 if (fio_client_send_ini(client, filename))
408 remove_client(client);
414 static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
416 dst->max_val = le64_to_cpu(src->max_val);
417 dst->min_val = le64_to_cpu(src->min_val);
418 dst->samples = le64_to_cpu(src->samples);
421 * Floats arrive as IEEE 754 encoded uint64_t, convert back to double
423 dst->mean.u.f = fio_uint64_to_double(le64_to_cpu(dst->mean.u.i));
424 dst->S.u.f = fio_uint64_to_double(le64_to_cpu(dst->S.u.i));
427 static void convert_ts(struct thread_stat *dst, struct thread_stat *src)
431 dst->error = le32_to_cpu(src->error);
432 dst->groupid = le32_to_cpu(src->groupid);
433 dst->pid = le32_to_cpu(src->pid);
434 dst->members = le32_to_cpu(src->members);
436 for (i = 0; i < 2; i++) {
437 convert_io_stat(&dst->clat_stat[i], &src->clat_stat[i]);
438 convert_io_stat(&dst->slat_stat[i], &src->slat_stat[i]);
439 convert_io_stat(&dst->lat_stat[i], &src->lat_stat[i]);
440 convert_io_stat(&dst->bw_stat[i], &src->bw_stat[i]);
443 dst->usr_time = le64_to_cpu(src->usr_time);
444 dst->sys_time = le64_to_cpu(src->sys_time);
445 dst->ctx = le64_to_cpu(src->ctx);
446 dst->minf = le64_to_cpu(src->minf);
447 dst->majf = le64_to_cpu(src->majf);
448 dst->clat_percentiles = le64_to_cpu(src->clat_percentiles);
450 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
451 fio_fp64_t *fps = &src->percentile_list[i];
452 fio_fp64_t *fpd = &dst->percentile_list[i];
454 fpd->u.f = fio_uint64_to_double(le64_to_cpu(fps->u.i));
457 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
458 dst->io_u_map[i] = le32_to_cpu(src->io_u_map[i]);
459 dst->io_u_submit[i] = le32_to_cpu(src->io_u_submit[i]);
460 dst->io_u_complete[i] = le32_to_cpu(src->io_u_complete[i]);
463 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
464 dst->io_u_lat_u[i] = le32_to_cpu(src->io_u_lat_u[i]);
465 dst->io_u_lat_m[i] = le32_to_cpu(src->io_u_lat_m[i]);
468 for (i = 0; i < 2; i++)
469 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
470 dst->io_u_plat[i][j] = le32_to_cpu(src->io_u_plat[i][j]);
472 for (i = 0; i < 3; i++) {
473 dst->total_io_u[i] = le64_to_cpu(src->total_io_u[i]);
474 dst->short_io_u[i] = le64_to_cpu(src->short_io_u[i]);
477 dst->total_submit = le64_to_cpu(src->total_submit);
478 dst->total_complete = le64_to_cpu(src->total_complete);
480 for (i = 0; i < 2; i++) {
481 dst->io_bytes[i] = le64_to_cpu(src->io_bytes[i]);
482 dst->runtime[i] = le64_to_cpu(src->runtime[i]);
485 dst->total_run_time = le64_to_cpu(src->total_run_time);
486 dst->continue_on_error = le16_to_cpu(src->continue_on_error);
487 dst->total_err_count = le64_to_cpu(src->total_err_count);
488 dst->first_error = le32_to_cpu(src->first_error);
489 dst->kb_base = le32_to_cpu(src->kb_base);
492 static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
496 for (i = 0; i < 2; i++) {
497 dst->max_run[i] = le64_to_cpu(src->max_run[i]);
498 dst->min_run[i] = le64_to_cpu(src->min_run[i]);
499 dst->max_bw[i] = le64_to_cpu(src->max_bw[i]);
500 dst->min_bw[i] = le64_to_cpu(src->min_bw[i]);
501 dst->io_kb[i] = le64_to_cpu(src->io_kb[i]);
502 dst->agg[i] = le64_to_cpu(src->agg[i]);
505 dst->kb_base = le32_to_cpu(src->kb_base);
506 dst->groupid = le32_to_cpu(src->groupid);
509 static void handle_ts(struct fio_net_cmd *cmd)
511 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
513 convert_ts(&p->ts, &p->ts);
514 convert_gs(&p->rs, &p->rs);
516 show_thread_status(&p->ts, &p->rs);
519 static void handle_gs(struct fio_net_cmd *cmd)
521 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
524 show_group_stats(gs);
527 static void handle_eta(struct fio_net_cmd *cmd)
529 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
532 je->nr_running = le32_to_cpu(je->nr_running);
533 je->nr_ramp = le32_to_cpu(je->nr_ramp);
534 je->nr_pending = le32_to_cpu(je->nr_pending);
535 je->files_open = le32_to_cpu(je->files_open);
536 je->m_rate = le32_to_cpu(je->m_rate);
537 je->t_rate = le32_to_cpu(je->t_rate);
538 je->m_iops = le32_to_cpu(je->m_iops);
539 je->t_iops = le32_to_cpu(je->t_iops);
541 for (i = 0; i < 2; i++) {
542 je->rate[i] = le32_to_cpu(je->rate[i]);
543 je->iops[i] = le32_to_cpu(je->iops[i]);
546 je->elapsed_sec = le64_to_cpu(je->elapsed_sec);
547 je->eta_sec = le64_to_cpu(je->eta_sec);
549 display_thread_status(je);
552 static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd)
554 struct cmd_probe_pdu *probe = (struct cmd_probe_pdu *) cmd->payload;
555 const char *os, *arch;
557 os = fio_get_os_string(probe->os);
561 arch = fio_get_arch_string(probe->arch);
565 log_info("hostname=%s, be=%u, os=%s, arch=%s, fio=%u.%u.%u\n",
566 probe->hostname, probe->bigendian, os, arch, probe->fio_major,
567 probe->fio_minor, probe->fio_patch);
570 client->name = strdup((char *) probe->hostname);
573 static int handle_client(struct fio_client *client)
575 struct fio_net_cmd *cmd;
577 dprint(FD_NET, "client: handle %s\n", client->hostname);
579 cmd = fio_net_recv_cmd(client->fd);
583 dprint(FD_NET, "client: got cmd op %d from %s\n",
584 cmd->opcode, client->hostname);
586 switch (cmd->opcode) {
587 case FIO_NET_CMD_QUIT:
588 remove_client(client);
591 case FIO_NET_CMD_TEXT: {
592 const char *buf = (const char *) cmd->payload;
596 name = client->name ? client->name : client->hostname;
598 if (!client->skip_newline)
599 fprintf(f_out, "<%s> ", name);
600 ret = fwrite(buf, cmd->pdu_len, 1, f_out);
602 client->skip_newline = strchr(buf, '\n') == NULL;
614 case FIO_NET_CMD_ETA:
618 case FIO_NET_CMD_PROBE:
619 handle_probe(client, cmd);
622 case FIO_NET_CMD_START:
623 client->state = Client_started;
626 case FIO_NET_CMD_STOP:
627 client->state = Client_stopped;
631 log_err("fio: unknown client op: %d\n", cmd->opcode);
639 int fio_handle_clients(void)
641 struct fio_client *client;
642 struct flist_head *entry;
646 pfds = malloc(nr_clients * sizeof(struct pollfd));
648 while (!exit_backend && nr_clients) {
650 flist_for_each(entry, &client_list) {
651 client = flist_entry(entry, struct fio_client, list);
653 pfds[i].fd = client->fd;
654 pfds[i].events = POLLIN;
658 assert(i == nr_clients);
661 ret = poll(pfds, nr_clients, 100);
665 log_err("fio: poll clients: %s\n", strerror(errno));
671 for (i = 0; i < nr_clients; i++) {
672 if (!(pfds[i].revents & POLLIN))
675 client = find_client_by_fd(pfds[i].fd);
677 log_err("fio: unknown client fd %d\n", pfds[i].fd);
680 if (!handle_client(client)) {
681 log_info("client: host=%s disconnected\n",
683 remove_client(client);