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;
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_hash[FIO_CLIENT_HASH_SZ];
55 static int handle_client(struct fio_client *client);
57 static void fio_client_add_hash(struct fio_client *client)
59 int bucket = hash_long(client->fd, FIO_CLIENT_HASH_BITS);
61 bucket &= FIO_CLIENT_HASH_MASK;
62 flist_add(&client->hash_list, &client_hash[bucket]);
65 static void fio_client_remove_hash(struct fio_client *client)
67 if (!flist_empty(&client->hash_list))
68 flist_del_init(&client->hash_list);
71 static void fio_init fio_client_hash_init(void)
75 for (i = 0; i < FIO_CLIENT_HASH_SZ; i++)
76 INIT_FLIST_HEAD(&client_hash[i]);
79 static struct fio_client *find_client_by_fd(int fd)
81 int bucket = hash_long(fd, FIO_CLIENT_HASH_BITS) & FIO_CLIENT_HASH_MASK;
82 struct fio_client *client;
83 struct flist_head *entry;
85 flist_for_each(entry, &client_hash[bucket]) {
86 client = flist_entry(entry, struct fio_client, hash_list);
95 static void remove_client(struct fio_client *client)
97 dprint(FD_NET, "client: removed <%s>\n", client->hostname);
98 flist_del(&client->list);
100 fio_client_remove_hash(client);
102 free(client->hostname);
110 static int __fio_client_add_cmd_option(struct fio_client *client,
115 if (client->argc == FIO_NET_CMD_JOBLINE_ARGV) {
116 log_err("fio: max cmd line number reached.\n");
117 log_err("fio: cmd line <%s> has been ignored.\n", opt);
121 index = client->argc++;
122 client->argv = realloc(client->argv, sizeof(char *) * client->argc);
123 client->argv[index] = strdup(opt);
124 dprint(FD_NET, "client: add cmd %d: %s\n", index, opt);
128 int fio_client_add_cmd_option(void *cookie, const char *opt)
130 struct fio_client *client = cookie;
135 return __fio_client_add_cmd_option(client, opt);
138 int fio_client_add(const char *hostname, void **cookie)
140 struct fio_client *client;
142 client = malloc(sizeof(*client));
143 memset(client, 0, sizeof(*client));
145 INIT_FLIST_HEAD(&client->list);
146 INIT_FLIST_HEAD(&client->hash_list);
148 if (fio_server_parse_string(hostname, &client->hostname,
149 &client->is_sock, &client->port,
150 &client->addr.sin_addr))
155 __fio_client_add_cmd_option(client, "fio");
157 flist_add(&client->list, &client_list);
159 dprint(FD_NET, "client: added <%s>\n", client->hostname);
164 static int fio_client_connect_ip(struct fio_client *client)
168 client->addr.sin_family = AF_INET;
169 client->addr.sin_port = htons(client->port);
171 fd = socket(AF_INET, SOCK_STREAM, 0);
173 log_err("fio: socket: %s\n", strerror(errno));
177 if (connect(fd, (struct sockaddr *) &client->addr, sizeof(client->addr)) < 0) {
178 log_err("fio: connect: %s\n", strerror(errno));
179 log_err("fio: failed to connect to %s\n", client->hostname);
187 static int fio_client_connect_sock(struct fio_client *client)
189 struct sockaddr_un *addr = &client->addr_un;
193 memset(addr, 0, sizeof(*addr));
194 addr->sun_family = AF_UNIX;
195 strcpy(addr->sun_path, client->hostname);
197 fd = socket(AF_UNIX, SOCK_STREAM, 0);
199 log_err("fio: socket: %s\n", strerror(errno));
203 len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
204 if (connect(fd, (struct sockaddr *) addr, len) < 0) {
205 log_err("fio: connect; %s\n", strerror(errno));
213 static int fio_client_connect(struct fio_client *client)
217 dprint(FD_NET, "client: connect to host %s\n", client->hostname);
219 memset(&client->addr, 0, sizeof(client->addr));
222 fd = fio_client_connect_sock(client);
224 fd = fio_client_connect_ip(client);
230 fio_client_add_hash(client);
231 client->state = Client_connected;
235 void fio_clients_terminate(void)
237 struct flist_head *entry;
238 struct fio_client *client;
240 dprint(FD_NET, "client: terminate clients\n");
242 flist_for_each(entry, &client_list) {
243 client = flist_entry(entry, struct fio_client, list);
245 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_QUIT, 0);
249 static void sig_int(int sig)
251 dprint(FD_NET, "client: got signal %d\n", sig);
252 fio_clients_terminate();
255 static void client_signal_handler(void)
257 struct sigaction act;
259 memset(&act, 0, sizeof(act));
260 act.sa_handler = sig_int;
261 act.sa_flags = SA_RESTART;
262 sigaction(SIGINT, &act, NULL);
264 memset(&act, 0, sizeof(act));
265 act.sa_handler = sig_int;
266 act.sa_flags = SA_RESTART;
267 sigaction(SIGTERM, &act, NULL);
270 static void probe_client(struct fio_client *client)
272 dprint(FD_NET, "client: send probe\n");
274 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_PROBE, 0);
275 handle_client(client);
278 static int send_client_cmd_line(struct fio_client *client)
280 struct cmd_line_pdu *pdu;
283 dprint(FD_NET, "client: send cmdline %d\n", client->argc);
285 pdu = malloc(sizeof(*pdu));
286 for (i = 0; i < client->argc; i++)
287 strcpy((char *) pdu->argv[i], client->argv[i]);
289 pdu->argc = cpu_to_le16(client->argc);
290 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOBLINE, pdu, sizeof(*pdu));
295 int fio_clients_connect(void)
297 struct fio_client *client;
298 struct flist_head *entry, *tmp;
301 dprint(FD_NET, "client: connect all\n");
303 client_signal_handler();
305 flist_for_each_safe(entry, tmp, &client_list) {
306 client = flist_entry(entry, struct fio_client, list);
308 ret = fio_client_connect(client);
310 remove_client(client);
314 probe_client(client);
316 if (client->argc > 1)
317 send_client_cmd_line(client);
324 * Send file contents to server backend. We could use sendfile(), but to remain
325 * more portable lets just read/write the darn thing.
327 static int fio_client_send_ini(struct fio_client *client, const char *filename)
334 dprint(FD_NET, "send ini %s to %s\n", filename, client->hostname);
336 fd = open(filename, O_RDONLY);
338 log_err("fio: job file <%s> open: %s\n", filename, strerror(errno));
342 if (fstat(fd, &sb) < 0) {
343 log_err("fio: job file stat: %s\n", strerror(errno));
348 buf = malloc(sb.st_size);
353 ret = read(fd, p, len);
362 else if (errno == EAGAIN || errno == EINTR)
367 log_err("fio: failed reading job file %s\n", filename);
373 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, buf, sb.st_size);
379 int fio_clients_send_ini(const char *filename)
381 struct fio_client *client;
382 struct flist_head *entry, *tmp;
384 flist_for_each_safe(entry, tmp, &client_list) {
385 client = flist_entry(entry, struct fio_client, list);
387 if (fio_client_send_ini(client, filename))
388 remove_client(client);
394 static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
396 dst->max_val = le64_to_cpu(src->max_val);
397 dst->min_val = le64_to_cpu(src->min_val);
398 dst->samples = le64_to_cpu(src->samples);
401 * Floats arrive as IEEE 754 encoded uint64_t, convert back to double
403 dst->mean.u.f = fio_uint64_to_double(le64_to_cpu(dst->mean.u.i));
404 dst->S.u.f = fio_uint64_to_double(le64_to_cpu(dst->S.u.i));
407 static void convert_ts(struct thread_stat *dst, struct thread_stat *src)
411 dst->error = le32_to_cpu(src->error);
412 dst->groupid = le32_to_cpu(src->groupid);
413 dst->pid = le32_to_cpu(src->pid);
414 dst->members = le32_to_cpu(src->members);
416 for (i = 0; i < 2; i++) {
417 convert_io_stat(&dst->clat_stat[i], &src->clat_stat[i]);
418 convert_io_stat(&dst->slat_stat[i], &src->slat_stat[i]);
419 convert_io_stat(&dst->lat_stat[i], &src->lat_stat[i]);
420 convert_io_stat(&dst->bw_stat[i], &src->bw_stat[i]);
423 dst->usr_time = le64_to_cpu(src->usr_time);
424 dst->sys_time = le64_to_cpu(src->sys_time);
425 dst->ctx = le64_to_cpu(src->ctx);
426 dst->minf = le64_to_cpu(src->minf);
427 dst->majf = le64_to_cpu(src->majf);
428 dst->clat_percentiles = le64_to_cpu(src->clat_percentiles);
430 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
431 fio_fp64_t *fps = &src->percentile_list[i];
432 fio_fp64_t *fpd = &dst->percentile_list[i];
434 fpd->u.f = fio_uint64_to_double(le64_to_cpu(fps->u.i));
437 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
438 dst->io_u_map[i] = le32_to_cpu(src->io_u_map[i]);
439 dst->io_u_submit[i] = le32_to_cpu(src->io_u_submit[i]);
440 dst->io_u_complete[i] = le32_to_cpu(src->io_u_complete[i]);
443 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
444 dst->io_u_lat_u[i] = le32_to_cpu(src->io_u_lat_u[i]);
445 dst->io_u_lat_m[i] = le32_to_cpu(src->io_u_lat_m[i]);
448 for (i = 0; i < 2; i++)
449 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
450 dst->io_u_plat[i][j] = le32_to_cpu(src->io_u_plat[i][j]);
452 for (i = 0; i < 3; i++) {
453 dst->total_io_u[i] = le64_to_cpu(src->total_io_u[i]);
454 dst->short_io_u[i] = le64_to_cpu(src->short_io_u[i]);
457 dst->total_submit = le64_to_cpu(src->total_submit);
458 dst->total_complete = le64_to_cpu(src->total_complete);
460 for (i = 0; i < 2; i++) {
461 dst->io_bytes[i] = le64_to_cpu(src->io_bytes[i]);
462 dst->runtime[i] = le64_to_cpu(src->runtime[i]);
465 dst->total_run_time = le64_to_cpu(src->total_run_time);
466 dst->continue_on_error = le16_to_cpu(src->continue_on_error);
467 dst->total_err_count = le64_to_cpu(src->total_err_count);
468 dst->first_error = le32_to_cpu(src->first_error);
469 dst->kb_base = le32_to_cpu(src->kb_base);
472 static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
476 for (i = 0; i < 2; i++) {
477 dst->max_run[i] = le64_to_cpu(src->max_run[i]);
478 dst->min_run[i] = le64_to_cpu(src->min_run[i]);
479 dst->max_bw[i] = le64_to_cpu(src->max_bw[i]);
480 dst->min_bw[i] = le64_to_cpu(src->min_bw[i]);
481 dst->io_kb[i] = le64_to_cpu(src->io_kb[i]);
482 dst->agg[i] = le64_to_cpu(src->agg[i]);
485 dst->kb_base = le32_to_cpu(src->kb_base);
486 dst->groupid = le32_to_cpu(src->groupid);
489 static void handle_ts(struct fio_net_cmd *cmd)
491 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
493 convert_ts(&p->ts, &p->ts);
494 convert_gs(&p->rs, &p->rs);
496 show_thread_status(&p->ts, &p->rs);
499 static void handle_gs(struct fio_net_cmd *cmd)
501 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
504 show_group_stats(gs);
507 static void handle_eta(struct fio_net_cmd *cmd)
509 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
512 je->nr_running = le32_to_cpu(je->nr_running);
513 je->nr_ramp = le32_to_cpu(je->nr_ramp);
514 je->nr_pending = le32_to_cpu(je->nr_pending);
515 je->files_open = le32_to_cpu(je->files_open);
516 je->m_rate = le32_to_cpu(je->m_rate);
517 je->t_rate = le32_to_cpu(je->t_rate);
518 je->m_iops = le32_to_cpu(je->m_iops);
519 je->t_iops = le32_to_cpu(je->t_iops);
521 for (i = 0; i < 2; i++) {
522 je->rate[i] = le32_to_cpu(je->rate[i]);
523 je->iops[i] = le32_to_cpu(je->iops[i]);
526 je->elapsed_sec = le32_to_cpu(je->nr_running);
527 je->eta_sec = le64_to_cpu(je->eta_sec);
529 display_thread_status(je);
532 static void handle_probe(struct fio_net_cmd *cmd)
534 struct cmd_probe_pdu *probe = (struct cmd_probe_pdu *) cmd->payload;
536 log_info("Probe: hostname=%s, be=%u, fio ver %u.%u.%u\n",
537 probe->hostname, probe->bigendian, probe->fio_major,
538 probe->fio_minor, probe->fio_patch);
541 static int handle_client(struct fio_client *client)
543 struct fio_net_cmd *cmd;
545 dprint(FD_NET, "client: handle %s\n", client->hostname);
547 cmd = fio_net_recv_cmd(client->fd);
551 dprint(FD_NET, "client: got cmd op %d from %s\n",
552 cmd->opcode, client->hostname);
554 switch (cmd->opcode) {
555 case FIO_NET_CMD_QUIT:
556 remove_client(client);
559 case FIO_NET_CMD_TEXT: {
560 const char *buf = (const char *) cmd->payload;
563 if (!client->skip_newline)
564 fprintf(f_out, "<%s> ", client->hostname);
565 ret = fwrite(buf, cmd->pdu_len, 1, f_out);
567 client->skip_newline = strchr(buf, '\n') == NULL;
579 case FIO_NET_CMD_ETA:
583 case FIO_NET_CMD_PROBE:
587 case FIO_NET_CMD_START:
588 client->state = Client_started;
591 case FIO_NET_CMD_STOP:
592 client->state = Client_stopped;
596 log_err("fio: unknown client op: %d\n", cmd->opcode);
604 int fio_handle_clients(void)
606 struct fio_client *client;
607 struct flist_head *entry;
611 pfds = malloc(nr_clients * sizeof(struct pollfd));
613 while (!exit_backend && nr_clients) {
615 flist_for_each(entry, &client_list) {
616 client = flist_entry(entry, struct fio_client, list);
618 pfds[i].fd = client->fd;
619 pfds[i].events = POLLIN;
623 assert(i == nr_clients);
626 ret = poll(pfds, nr_clients, 100);
630 log_err("fio: poll clients: %s\n", strerror(errno));
636 for (i = 0; i < nr_clients; i++) {
637 if (!(pfds[i].revents & POLLIN))
640 client = find_client_by_fd(pfds[i].fd);
642 log_err("fio: unknown client fd %d\n", pfds[i].fd);
645 if (!handle_client(client)) {
646 log_info("client: host=%s disconnected\n",
648 remove_client(client);