11 #include <sys/socket.h>
12 #include <netinet/in.h>
13 #include <arpa/inet.h>
19 #include "crc/crc32.h"
23 struct flist_head list;
24 struct sockaddr_in addr;
43 static FLIST_HEAD(client_list);
45 static int handle_client(struct fio_client *client, int one, int block);
47 static struct fio_client *find_client_by_fd(int fd)
49 struct fio_client *client;
50 struct flist_head *entry;
52 flist_for_each(entry, &client_list) {
53 client = flist_entry(entry, struct fio_client, list);
62 static struct fio_client *find_client_by_name(const char *name)
64 struct fio_client *client;
65 struct flist_head *entry;
67 flist_for_each(entry, &client_list) {
68 client = flist_entry(entry, struct fio_client, list);
70 if (!strcmp(name, client->hostname))
77 static void remove_client(struct fio_client *client)
79 dprint(FD_NET, "client: removed <%s>\n", client->hostname);
80 flist_del(&client->list);
83 free(client->hostname);
90 static int __fio_client_add_cmd_option(struct fio_client *client,
95 if (client->argc == FIO_NET_CMD_JOBLINE_ARGV) {
96 log_err("fio: max cmd line number reached.\n");
97 log_err("fio: cmd line <%s> has been ignored.\n", opt);
101 index = client->argc++;
102 client->argv = realloc(client->argv, sizeof(char *) * client->argc);
103 client->argv[index] = strdup(opt);
104 dprint(FD_NET, "client: add cmd %d: %s\n", index, opt);
108 int fio_client_add_cmd_option(const char *hostname, const char *opt)
110 struct fio_client *client;
112 if (!hostname || !opt)
115 client = find_client_by_name(hostname);
117 log_err("fio: unknown client %s\n", hostname);
121 return __fio_client_add_cmd_option(client, opt);
124 void fio_client_add(const char *hostname)
126 struct fio_client *client;
128 dprint(FD_NET, "client: added <%s>\n", hostname);
129 client = malloc(sizeof(*client));
130 memset(client, 0, sizeof(*client));
132 client->hostname = strdup(hostname);
135 __fio_client_add_cmd_option(client, "fio");
137 flist_add(&client->list, &client_list);
141 static int fio_client_connect(struct fio_client *client)
145 dprint(FD_NET, "client: connect to host %s\n", client->hostname);
147 memset(&client->addr, 0, sizeof(client->addr));
148 client->addr.sin_family = AF_INET;
149 client->addr.sin_port = htons(fio_net_port);
151 if (inet_aton(client->hostname, &client->addr.sin_addr) != 1) {
152 struct hostent *hent;
154 hent = gethostbyname(client->hostname);
156 log_err("fio: gethostbyname: %s\n", strerror(errno));
160 memcpy(&client->addr.sin_addr, hent->h_addr, 4);
163 fd = socket(AF_INET, SOCK_STREAM, 0);
165 log_err("fio: socket: %s\n", strerror(errno));
169 if (connect(fd, (struct sockaddr *) &client->addr, sizeof(client->addr)) < 0) {
170 log_err("fio: connect: %s\n", strerror(errno));
171 log_err("fio: failed to connect to %s\n", client->hostname);
176 client->state = Client_connected;
180 void fio_clients_terminate(void)
182 struct flist_head *entry;
183 struct fio_client *client;
185 dprint(FD_NET, "client: terminate clients\n");
187 flist_for_each(entry, &client_list) {
188 client = flist_entry(entry, struct fio_client, list);
190 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_QUIT, 0);
194 static void sig_int(int sig)
196 dprint(FD_NET, "client: got sign %d\n", sig);
197 fio_clients_terminate();
200 static void client_signal_handler(void)
202 struct sigaction act;
204 memset(&act, 0, sizeof(act));
205 act.sa_handler = sig_int;
206 act.sa_flags = SA_RESTART;
207 sigaction(SIGINT, &act, NULL);
209 memset(&act, 0, sizeof(act));
210 act.sa_handler = sig_int;
211 act.sa_flags = SA_RESTART;
212 sigaction(SIGTERM, &act, NULL);
215 static void probe_client(struct fio_client *client)
217 dprint(FD_NET, "client: send probe\n");
219 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_PROBE, 0);
220 handle_client(client, 1, 1);
223 static int send_client_cmd_line(struct fio_client *client)
225 struct cmd_line_pdu *pdu;
228 dprint(FD_NET, "client: send cmdline %d\n", client->argc);
230 pdu = malloc(sizeof(*pdu));
231 for (i = 0; i < client->argc; i++)
232 strcpy((char *) pdu->argv[i], client->argv[i]);
234 pdu->argc = cpu_to_le16(client->argc);
235 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOBLINE, pdu, sizeof(*pdu));
240 int fio_clients_connect(void)
242 struct fio_client *client;
243 struct flist_head *entry, *tmp;
246 dprint(FD_NET, "client: connect all\n");
248 client_signal_handler();
250 flist_for_each_safe(entry, tmp, &client_list) {
251 client = flist_entry(entry, struct fio_client, list);
253 ret = fio_client_connect(client);
255 remove_client(client);
259 probe_client(client);
261 if (client->argc > 1)
262 send_client_cmd_line(client);
269 * Send file contents to server backend. We could use sendfile(), but to remain
270 * more portable lets just read/write the darn thing.
272 static int fio_client_send_ini(struct fio_client *client, const char *filename)
279 dprint(FD_NET, "send ini %s to %s\n", filename, client->hostname);
281 fd = open(filename, O_RDONLY);
283 log_err("fio: job file open: %s\n", strerror(errno));
287 if (fstat(fd, &sb) < 0) {
288 log_err("fio: job file stat: %s\n", strerror(errno));
292 buf = malloc(sb.st_size);
297 ret = read(fd, p, len);
306 else if (errno == EAGAIN || errno == EINTR)
311 log_err("fio: failed reading job file %s\n", filename);
315 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, buf, sb.st_size);
320 int fio_clients_send_ini(const char *filename)
322 struct fio_client *client;
323 struct flist_head *entry, *tmp;
325 flist_for_each_safe(entry, tmp, &client_list) {
326 client = flist_entry(entry, struct fio_client, list);
328 if (fio_client_send_ini(client, filename))
329 remove_client(client);
335 static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
337 dst->max_val = le64_to_cpu(src->max_val);
338 dst->min_val = le64_to_cpu(src->min_val);
339 dst->samples = le64_to_cpu(src->samples);
342 * Floats arrive as IEEE 754 encoded uint64_t, convert back to double
344 dst->mean.u.f = fio_uint64_to_double(le64_to_cpu(dst->mean.u.i));
345 dst->S.u.f = fio_uint64_to_double(le64_to_cpu(dst->S.u.i));
348 static void convert_ts(struct thread_stat *dst, struct thread_stat *src)
352 dst->error = le32_to_cpu(src->error);
353 dst->groupid = le32_to_cpu(src->groupid);
354 dst->pid = le32_to_cpu(src->pid);
355 dst->members = le32_to_cpu(src->members);
357 for (i = 0; i < 2; i++) {
358 convert_io_stat(&dst->clat_stat[i], &src->clat_stat[i]);
359 convert_io_stat(&dst->slat_stat[i], &src->slat_stat[i]);
360 convert_io_stat(&dst->lat_stat[i], &src->lat_stat[i]);
361 convert_io_stat(&dst->bw_stat[i], &src->bw_stat[i]);
364 dst->usr_time = le64_to_cpu(src->usr_time);
365 dst->sys_time = le64_to_cpu(src->sys_time);
366 dst->ctx = le64_to_cpu(src->ctx);
367 dst->minf = le64_to_cpu(src->minf);
368 dst->majf = le64_to_cpu(src->majf);
369 dst->clat_percentiles = le64_to_cpu(src->clat_percentiles);
371 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
372 fio_fp64_t *fps = &src->percentile_list[i];
373 fio_fp64_t *fpd = &dst->percentile_list[i];
375 fpd->u.f = fio_uint64_to_double(le64_to_cpu(fps->u.i));
378 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
379 dst->io_u_map[i] = le32_to_cpu(src->io_u_map[i]);
380 dst->io_u_submit[i] = le32_to_cpu(src->io_u_submit[i]);
381 dst->io_u_complete[i] = le32_to_cpu(src->io_u_complete[i]);
384 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
385 dst->io_u_lat_u[i] = le32_to_cpu(src->io_u_lat_u[i]);
386 dst->io_u_lat_m[i] = le32_to_cpu(src->io_u_lat_m[i]);
389 for (i = 0; i < 2; i++)
390 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
391 dst->io_u_plat[i][j] = le32_to_cpu(src->io_u_plat[i][j]);
393 for (i = 0; i < 3; i++) {
394 dst->total_io_u[i] = le64_to_cpu(src->total_io_u[i]);
395 dst->short_io_u[i] = le64_to_cpu(src->short_io_u[i]);
398 dst->total_submit = le64_to_cpu(src->total_submit);
399 dst->total_complete = le64_to_cpu(src->total_complete);
401 for (i = 0; i < 2; i++) {
402 dst->io_bytes[i] = le64_to_cpu(src->io_bytes[i]);
403 dst->runtime[i] = le64_to_cpu(src->runtime[i]);
406 dst->total_run_time = le64_to_cpu(src->total_run_time);
407 dst->continue_on_error = le16_to_cpu(src->continue_on_error);
408 dst->total_err_count = le64_to_cpu(src->total_err_count);
409 dst->first_error = le32_to_cpu(src->first_error);
410 dst->kb_base = le32_to_cpu(src->kb_base);
413 static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
417 for (i = 0; i < 2; i++) {
418 dst->max_run[i] = le64_to_cpu(src->max_run[i]);
419 dst->min_run[i] = le64_to_cpu(src->min_run[i]);
420 dst->max_bw[i] = le64_to_cpu(src->max_bw[i]);
421 dst->min_bw[i] = le64_to_cpu(src->min_bw[i]);
422 dst->io_kb[i] = le64_to_cpu(src->io_kb[i]);
423 dst->agg[i] = le64_to_cpu(src->agg[i]);
426 dst->kb_base = le32_to_cpu(src->kb_base);
427 dst->groupid = le32_to_cpu(src->groupid);
430 static void handle_ts(struct fio_net_cmd *cmd)
432 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
434 convert_ts(&p->ts, &p->ts);
435 convert_gs(&p->rs, &p->rs);
437 show_thread_status(&p->ts, &p->rs);
440 static void handle_gs(struct fio_net_cmd *cmd)
442 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
445 show_group_stats(gs);
448 static void handle_eta(struct fio_net_cmd *cmd)
450 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
453 je->nr_running = le32_to_cpu(je->nr_running);
454 je->nr_ramp = le32_to_cpu(je->nr_ramp);
455 je->nr_pending = le32_to_cpu(je->nr_pending);
456 je->files_open = le32_to_cpu(je->files_open);
457 je->m_rate = le32_to_cpu(je->m_rate);
458 je->t_rate = le32_to_cpu(je->t_rate);
459 je->m_iops = le32_to_cpu(je->m_iops);
460 je->t_iops = le32_to_cpu(je->t_iops);
462 for (i = 0; i < 2; i++) {
463 je->rate[i] = le32_to_cpu(je->rate[i]);
464 je->iops[i] = le32_to_cpu(je->iops[i]);
467 je->elapsed_sec = le32_to_cpu(je->nr_running);
468 je->eta_sec = le64_to_cpu(je->eta_sec);
470 display_thread_status(je);
473 static void handle_probe(struct fio_net_cmd *cmd)
475 struct cmd_probe_pdu *probe = (struct cmd_probe_pdu *) cmd->payload;
477 log_info("Probe: hostname=%s, be=%u, fio ver %u.%u.%u\n",
478 probe->hostname, probe->bigendian, probe->fio_major,
479 probe->fio_minor, probe->fio_patch);
482 static int handle_client(struct fio_client *client, int one, int block)
484 struct fio_net_cmd *cmd;
485 int done = 0, did_cmd = 0;
487 dprint(FD_NET, "client: handle %s\n", client->hostname);
489 while ((cmd = fio_net_recv_cmd(client->fd, block)) != NULL) {
492 dprint(FD_NET, "client: got cmd op %d from %s\n",
493 cmd->opcode, client->hostname);
495 switch (cmd->opcode) {
496 case FIO_NET_CMD_QUIT:
497 remove_client(client);
501 case FIO_NET_CMD_TEXT: {
502 const char *buf = (const char *) cmd->payload;
505 if (!client->skip_newline)
506 fprintf(f_out, "<%s> ", client->hostname);
507 ret = fwrite(buf, cmd->pdu_len, 1, f_out);
509 client->skip_newline = strchr(buf, '\n') == NULL;
521 case FIO_NET_CMD_ETA:
525 case FIO_NET_CMD_PROBE:
529 case FIO_NET_CMD_START:
530 client->state = Client_started;
533 case FIO_NET_CMD_STOP:
534 client->state = Client_stopped;
538 log_err("fio: unknown client op: %d\n", cmd->opcode);
550 int fio_handle_clients(void)
552 struct fio_client *client;
553 struct flist_head *entry;
557 pfds = malloc(nr_clients * sizeof(struct pollfd));
559 while (!exit_backend && nr_clients) {
561 flist_for_each(entry, &client_list) {
562 client = flist_entry(entry, struct fio_client, list);
564 pfds[i].fd = client->fd;
565 pfds[i].events = POLLIN;
569 assert(i == nr_clients);
572 ret = poll(pfds, nr_clients, 100);
576 log_err("fio: poll clients: %s\n", strerror(errno));
582 for (i = 0; i < nr_clients; i++) {
583 if (!(pfds[i].revents & POLLIN))
586 client = find_client_by_fd(pfds[i].fd);
588 log_err("fio: unknown client\n");
591 if (!handle_client(client, 0, 0)) {
592 log_info("client: host=%s disconnected\n",
594 remove_client(client);