11 #include <sys/socket.h>
13 #include <netinet/in.h>
14 #include <arpa/inet.h>
24 struct flist_head list;
25 struct flist_head fd_hash_list;
26 struct flist_head name_hash_list;
27 struct sockaddr_in addr;
28 struct sockaddr_un addr_un;
48 static FLIST_HEAD(client_list);
50 #define FIO_CLIENT_HASH_BITS 7
51 #define FIO_CLIENT_HASH_SZ (1 << FIO_CLIENT_HASH_BITS)
52 #define FIO_CLIENT_HASH_MASK (FIO_CLIENT_HASH_SZ - 1)
53 static struct flist_head client_fd_hash[FIO_CLIENT_HASH_SZ];
54 static struct flist_head client_name_hash[FIO_CLIENT_HASH_SZ];
56 static int handle_client(struct fio_client *client);
58 static void fio_client_add_fd_hash(struct fio_client *client)
60 int bucket = hash_long(client->fd, FIO_CLIENT_HASH_BITS);
62 bucket &= FIO_CLIENT_HASH_MASK;
63 flist_add(&client->fd_hash_list, &client_fd_hash[bucket]);
66 static void fio_client_remove_fd_hash(struct fio_client *client)
68 if (!flist_empty(&client->fd_hash_list))
69 flist_del_init(&client->fd_hash_list);
72 static void fio_client_add_name_hash(struct fio_client *client)
74 int bucket = jhash(client->hostname, strlen(client->hostname), 0);
76 bucket &= FIO_CLIENT_HASH_MASK;
77 flist_add(&client->name_hash_list, &client_name_hash[bucket]);
80 static void fio_client_remove_name_hash(struct fio_client *client)
82 if (!flist_empty(&client->name_hash_list))
83 flist_del_init(&client->name_hash_list);
86 static void fio_init fio_client_hash_init(void)
90 for (i = 0; i < FIO_CLIENT_HASH_SZ; i++) {
91 INIT_FLIST_HEAD(&client_fd_hash[i]);
92 INIT_FLIST_HEAD(&client_name_hash[i]);
96 static struct fio_client *find_client_by_fd(int fd)
98 int bucket = hash_long(fd, FIO_CLIENT_HASH_BITS) & FIO_CLIENT_HASH_MASK;
99 struct fio_client *client;
100 struct flist_head *entry;
102 flist_for_each(entry, &client_fd_hash[bucket]) {
103 client = flist_entry(entry, struct fio_client, fd_hash_list);
105 if (client->fd == fd)
112 static struct fio_client *find_client_by_name(const char *name)
114 int bucket = jhash(name, strlen(name), 0) & FIO_CLIENT_HASH_BITS;
115 struct fio_client *client;
116 struct flist_head *entry;
118 flist_for_each(entry, &client_name_hash[bucket]) {
119 client = flist_entry(entry, struct fio_client, name_hash_list);
121 if (!strcmp(name, client->hostname))
128 static void remove_client(struct fio_client *client)
130 dprint(FD_NET, "client: removed <%s>\n", client->hostname);
131 flist_del(&client->list);
133 fio_client_remove_fd_hash(client);
134 fio_client_remove_name_hash(client);
136 free(client->hostname);
144 static int __fio_client_add_cmd_option(struct fio_client *client,
149 if (client->argc == FIO_NET_CMD_JOBLINE_ARGV) {
150 log_err("fio: max cmd line number reached.\n");
151 log_err("fio: cmd line <%s> has been ignored.\n", opt);
155 index = client->argc++;
156 client->argv = realloc(client->argv, sizeof(char *) * client->argc);
157 client->argv[index] = strdup(opt);
158 dprint(FD_NET, "client: add cmd %d: %s\n", index, opt);
162 int fio_client_add_cmd_option(const char *hostname, const char *opt)
164 struct fio_client *client;
166 if (!hostname || !opt)
169 client = find_client_by_name(hostname);
171 log_err("fio: unknown client %s\n", hostname);
175 return __fio_client_add_cmd_option(client, opt);
178 void fio_client_add(const char *hostname)
180 struct fio_client *client;
182 dprint(FD_NET, "client: added <%s>\n", hostname);
183 client = malloc(sizeof(*client));
184 memset(client, 0, sizeof(*client));
186 INIT_FLIST_HEAD(&client->list);
187 INIT_FLIST_HEAD(&client->fd_hash_list);
188 INIT_FLIST_HEAD(&client->name_hash_list);
190 if (!strncmp(hostname, "sock:", 5)) {
191 client->hostname = strdup(hostname + 5);
194 client->hostname = strdup(hostname);
198 fio_client_add_name_hash(client);
200 __fio_client_add_cmd_option(client, "fio");
202 flist_add(&client->list, &client_list);
206 static int fio_client_connect_ip(struct fio_client *client)
210 client->addr.sin_family = AF_INET;
211 client->addr.sin_port = htons(fio_net_port);
213 if (inet_aton(client->hostname, &client->addr.sin_addr) != 1) {
214 struct hostent *hent;
216 hent = gethostbyname(client->hostname);
218 log_err("fio: gethostbyname: %s\n", strerror(errno));
219 log_err("fio: failed looking up hostname %s\n",
224 memcpy(&client->addr.sin_addr, hent->h_addr, 4);
227 fd = socket(AF_INET, SOCK_STREAM, 0);
229 log_err("fio: socket: %s\n", strerror(errno));
233 if (connect(fd, (struct sockaddr *) &client->addr, sizeof(client->addr)) < 0) {
234 log_err("fio: connect: %s\n", strerror(errno));
235 log_err("fio: failed to connect to %s\n", client->hostname);
243 static int fio_client_connect_sock(struct fio_client *client)
245 struct sockaddr_un *addr = &client->addr_un;
249 memset(addr, 0, sizeof(*addr));
250 addr->sun_family = AF_UNIX;
251 strcpy(addr->sun_path, client->hostname);
253 fd = socket(AF_UNIX, SOCK_STREAM, 0);
255 log_err("fio: socket: %s\n", strerror(errno));
259 len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
260 if (connect(fd, (struct sockaddr *) addr, len) < 0) {
261 log_err("fio: connect; %s\n", strerror(errno));
269 static int fio_client_connect(struct fio_client *client)
273 dprint(FD_NET, "client: connect to host %s\n", client->hostname);
275 memset(&client->addr, 0, sizeof(client->addr));
278 fd = fio_client_connect_sock(client);
280 fd = fio_client_connect_ip(client);
286 fio_client_add_fd_hash(client);
287 client->state = Client_connected;
291 void fio_clients_terminate(void)
293 struct flist_head *entry;
294 struct fio_client *client;
296 dprint(FD_NET, "client: terminate clients\n");
298 flist_for_each(entry, &client_list) {
299 client = flist_entry(entry, struct fio_client, list);
301 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_QUIT, 0);
305 static void sig_int(int sig)
307 dprint(FD_NET, "client: got sign %d\n", sig);
308 fio_clients_terminate();
311 static void client_signal_handler(void)
313 struct sigaction act;
315 memset(&act, 0, sizeof(act));
316 act.sa_handler = sig_int;
317 act.sa_flags = SA_RESTART;
318 sigaction(SIGINT, &act, NULL);
320 memset(&act, 0, sizeof(act));
321 act.sa_handler = sig_int;
322 act.sa_flags = SA_RESTART;
323 sigaction(SIGTERM, &act, NULL);
326 static void probe_client(struct fio_client *client)
328 dprint(FD_NET, "client: send probe\n");
330 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_PROBE, 0);
331 handle_client(client);
334 static int send_client_cmd_line(struct fio_client *client)
336 struct cmd_line_pdu *pdu;
339 dprint(FD_NET, "client: send cmdline %d\n", client->argc);
341 pdu = malloc(sizeof(*pdu));
342 for (i = 0; i < client->argc; i++)
343 strcpy((char *) pdu->argv[i], client->argv[i]);
345 pdu->argc = cpu_to_le16(client->argc);
346 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOBLINE, pdu, sizeof(*pdu));
351 int fio_clients_connect(void)
353 struct fio_client *client;
354 struct flist_head *entry, *tmp;
357 dprint(FD_NET, "client: connect all\n");
359 client_signal_handler();
361 flist_for_each_safe(entry, tmp, &client_list) {
362 client = flist_entry(entry, struct fio_client, list);
364 ret = fio_client_connect(client);
366 remove_client(client);
370 probe_client(client);
372 if (client->argc > 1)
373 send_client_cmd_line(client);
380 * Send file contents to server backend. We could use sendfile(), but to remain
381 * more portable lets just read/write the darn thing.
383 static int fio_client_send_ini(struct fio_client *client, const char *filename)
390 dprint(FD_NET, "send ini %s to %s\n", filename, client->hostname);
392 fd = open(filename, O_RDONLY);
394 log_err("fio: job file <%s> open: %s\n", filename, strerror(errno));
398 if (fstat(fd, &sb) < 0) {
399 log_err("fio: job file stat: %s\n", strerror(errno));
404 buf = malloc(sb.st_size);
409 ret = read(fd, p, len);
418 else if (errno == EAGAIN || errno == EINTR)
423 log_err("fio: failed reading job file %s\n", filename);
428 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, buf, sb.st_size);
434 int fio_clients_send_ini(const char *filename)
436 struct fio_client *client;
437 struct flist_head *entry, *tmp;
439 flist_for_each_safe(entry, tmp, &client_list) {
440 client = flist_entry(entry, struct fio_client, list);
442 if (fio_client_send_ini(client, filename))
443 remove_client(client);
449 static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
451 dst->max_val = le64_to_cpu(src->max_val);
452 dst->min_val = le64_to_cpu(src->min_val);
453 dst->samples = le64_to_cpu(src->samples);
456 * Floats arrive as IEEE 754 encoded uint64_t, convert back to double
458 dst->mean.u.f = fio_uint64_to_double(le64_to_cpu(dst->mean.u.i));
459 dst->S.u.f = fio_uint64_to_double(le64_to_cpu(dst->S.u.i));
462 static void convert_ts(struct thread_stat *dst, struct thread_stat *src)
466 dst->error = le32_to_cpu(src->error);
467 dst->groupid = le32_to_cpu(src->groupid);
468 dst->pid = le32_to_cpu(src->pid);
469 dst->members = le32_to_cpu(src->members);
471 for (i = 0; i < 2; i++) {
472 convert_io_stat(&dst->clat_stat[i], &src->clat_stat[i]);
473 convert_io_stat(&dst->slat_stat[i], &src->slat_stat[i]);
474 convert_io_stat(&dst->lat_stat[i], &src->lat_stat[i]);
475 convert_io_stat(&dst->bw_stat[i], &src->bw_stat[i]);
478 dst->usr_time = le64_to_cpu(src->usr_time);
479 dst->sys_time = le64_to_cpu(src->sys_time);
480 dst->ctx = le64_to_cpu(src->ctx);
481 dst->minf = le64_to_cpu(src->minf);
482 dst->majf = le64_to_cpu(src->majf);
483 dst->clat_percentiles = le64_to_cpu(src->clat_percentiles);
485 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
486 fio_fp64_t *fps = &src->percentile_list[i];
487 fio_fp64_t *fpd = &dst->percentile_list[i];
489 fpd->u.f = fio_uint64_to_double(le64_to_cpu(fps->u.i));
492 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
493 dst->io_u_map[i] = le32_to_cpu(src->io_u_map[i]);
494 dst->io_u_submit[i] = le32_to_cpu(src->io_u_submit[i]);
495 dst->io_u_complete[i] = le32_to_cpu(src->io_u_complete[i]);
498 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
499 dst->io_u_lat_u[i] = le32_to_cpu(src->io_u_lat_u[i]);
500 dst->io_u_lat_m[i] = le32_to_cpu(src->io_u_lat_m[i]);
503 for (i = 0; i < 2; i++)
504 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
505 dst->io_u_plat[i][j] = le32_to_cpu(src->io_u_plat[i][j]);
507 for (i = 0; i < 3; i++) {
508 dst->total_io_u[i] = le64_to_cpu(src->total_io_u[i]);
509 dst->short_io_u[i] = le64_to_cpu(src->short_io_u[i]);
512 dst->total_submit = le64_to_cpu(src->total_submit);
513 dst->total_complete = le64_to_cpu(src->total_complete);
515 for (i = 0; i < 2; i++) {
516 dst->io_bytes[i] = le64_to_cpu(src->io_bytes[i]);
517 dst->runtime[i] = le64_to_cpu(src->runtime[i]);
520 dst->total_run_time = le64_to_cpu(src->total_run_time);
521 dst->continue_on_error = le16_to_cpu(src->continue_on_error);
522 dst->total_err_count = le64_to_cpu(src->total_err_count);
523 dst->first_error = le32_to_cpu(src->first_error);
524 dst->kb_base = le32_to_cpu(src->kb_base);
527 static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
531 for (i = 0; i < 2; i++) {
532 dst->max_run[i] = le64_to_cpu(src->max_run[i]);
533 dst->min_run[i] = le64_to_cpu(src->min_run[i]);
534 dst->max_bw[i] = le64_to_cpu(src->max_bw[i]);
535 dst->min_bw[i] = le64_to_cpu(src->min_bw[i]);
536 dst->io_kb[i] = le64_to_cpu(src->io_kb[i]);
537 dst->agg[i] = le64_to_cpu(src->agg[i]);
540 dst->kb_base = le32_to_cpu(src->kb_base);
541 dst->groupid = le32_to_cpu(src->groupid);
544 static void handle_ts(struct fio_net_cmd *cmd)
546 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
548 convert_ts(&p->ts, &p->ts);
549 convert_gs(&p->rs, &p->rs);
551 show_thread_status(&p->ts, &p->rs);
554 static void handle_gs(struct fio_net_cmd *cmd)
556 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
559 show_group_stats(gs);
562 static void handle_eta(struct fio_net_cmd *cmd)
564 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
567 je->nr_running = le32_to_cpu(je->nr_running);
568 je->nr_ramp = le32_to_cpu(je->nr_ramp);
569 je->nr_pending = le32_to_cpu(je->nr_pending);
570 je->files_open = le32_to_cpu(je->files_open);
571 je->m_rate = le32_to_cpu(je->m_rate);
572 je->t_rate = le32_to_cpu(je->t_rate);
573 je->m_iops = le32_to_cpu(je->m_iops);
574 je->t_iops = le32_to_cpu(je->t_iops);
576 for (i = 0; i < 2; i++) {
577 je->rate[i] = le32_to_cpu(je->rate[i]);
578 je->iops[i] = le32_to_cpu(je->iops[i]);
581 je->elapsed_sec = le32_to_cpu(je->nr_running);
582 je->eta_sec = le64_to_cpu(je->eta_sec);
584 display_thread_status(je);
587 static void handle_probe(struct fio_net_cmd *cmd)
589 struct cmd_probe_pdu *probe = (struct cmd_probe_pdu *) cmd->payload;
591 log_info("Probe: hostname=%s, be=%u, fio ver %u.%u.%u\n",
592 probe->hostname, probe->bigendian, probe->fio_major,
593 probe->fio_minor, probe->fio_patch);
596 static int handle_client(struct fio_client *client)
598 struct fio_net_cmd *cmd;
600 dprint(FD_NET, "client: handle %s\n", client->hostname);
602 cmd = fio_net_recv_cmd(client->fd);
606 dprint(FD_NET, "client: got cmd op %d from %s\n",
607 cmd->opcode, client->hostname);
609 switch (cmd->opcode) {
610 case FIO_NET_CMD_QUIT:
611 remove_client(client);
614 case FIO_NET_CMD_TEXT: {
615 const char *buf = (const char *) cmd->payload;
618 if (!client->skip_newline)
619 fprintf(f_out, "<%s> ", client->hostname);
620 ret = fwrite(buf, cmd->pdu_len, 1, f_out);
622 client->skip_newline = strchr(buf, '\n') == NULL;
634 case FIO_NET_CMD_ETA:
638 case FIO_NET_CMD_PROBE:
642 case FIO_NET_CMD_START:
643 client->state = Client_started;
646 case FIO_NET_CMD_STOP:
647 client->state = Client_stopped;
651 log_err("fio: unknown client op: %d\n", cmd->opcode);
659 int fio_handle_clients(void)
661 struct fio_client *client;
662 struct flist_head *entry;
666 pfds = malloc(nr_clients * sizeof(struct pollfd));
668 while (!exit_backend && nr_clients) {
670 flist_for_each(entry, &client_list) {
671 client = flist_entry(entry, struct fio_client, list);
673 pfds[i].fd = client->fd;
674 pfds[i].events = POLLIN;
678 assert(i == nr_clients);
681 ret = poll(pfds, nr_clients, 100);
685 log_err("fio: poll clients: %s\n", strerror(errno));
691 for (i = 0; i < nr_clients; i++) {
692 if (!(pfds[i].revents & POLLIN))
695 client = find_client_by_fd(pfds[i].fd);
697 log_err("fio: unknown client fd %d\n", pfds[i].fd);
700 if (!handle_client(client)) {
701 log_info("client: host=%s disconnected\n",
703 remove_client(client);