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);
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 void __fio_client_add_cmd_option(struct fio_client *client,
95 index = client->argc++;
96 client->argv = realloc(client->argv, sizeof(char *) * client->argc);
97 client->argv[index] = strdup(opt);
98 dprint(FD_NET, "client: add cmd %d: %s\n", index, opt);
101 void fio_client_add_cmd_option(const char *hostname, const char *opt)
103 struct fio_client *client;
105 if (!hostname || !opt)
108 client = find_client_by_name(hostname);
110 log_err("fio: unknown client %s\n", hostname);
114 __fio_client_add_cmd_option(client, opt);
117 void fio_client_add(const char *hostname)
119 struct fio_client *client;
121 dprint(FD_NET, "client: added <%s>\n", hostname);
122 client = malloc(sizeof(*client));
123 memset(client, 0, sizeof(*client));
125 client->hostname = strdup(hostname);
128 __fio_client_add_cmd_option(client, "fio");
130 flist_add(&client->list, &client_list);
134 static int fio_client_connect(struct fio_client *client)
138 dprint(FD_NET, "client: connect to host %s\n", client->hostname);
140 memset(&client->addr, 0, sizeof(client->addr));
141 client->addr.sin_family = AF_INET;
142 client->addr.sin_port = htons(fio_net_port);
144 if (inet_aton(client->hostname, &client->addr.sin_addr) != 1) {
145 struct hostent *hent;
147 hent = gethostbyname(client->hostname);
149 log_err("fio: gethostbyname: %s\n", strerror(errno));
153 memcpy(&client->addr.sin_addr, hent->h_addr, 4);
156 fd = socket(AF_INET, SOCK_STREAM, 0);
158 log_err("fio: socket: %s\n", strerror(errno));
162 if (connect(fd, (struct sockaddr *) &client->addr, sizeof(client->addr)) < 0) {
163 log_err("fio: connect: %s\n", strerror(errno));
164 log_err("fio: failed to connect to %s\n", client->hostname);
169 client->state = Client_connected;
173 void fio_clients_terminate(void)
175 struct flist_head *entry;
176 struct fio_client *client;
178 dprint(FD_NET, "client: terminate clients\n");
180 flist_for_each(entry, &client_list) {
181 client = flist_entry(entry, struct fio_client, list);
183 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_QUIT, 0);
187 static void sig_int(int sig)
189 dprint(FD_NET, "client: got sign %d\n", sig);
190 fio_clients_terminate();
193 static void client_signal_handler(void)
195 struct sigaction act;
197 memset(&act, 0, sizeof(act));
198 act.sa_handler = sig_int;
199 act.sa_flags = SA_RESTART;
200 sigaction(SIGINT, &act, NULL);
202 memset(&act, 0, sizeof(act));
203 act.sa_handler = sig_int;
204 act.sa_flags = SA_RESTART;
205 sigaction(SIGTERM, &act, NULL);
208 static void probe_client(struct fio_client *client)
210 dprint(FD_NET, "client: send probe\n");
212 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_PROBE, 0);
213 handle_client(client, 1);
216 static int send_client_cmd_line(struct fio_client *client)
218 struct cmd_line_pdu *pdu;
221 dprint(FD_NET, "client: send cmdline %d\n", client->argc);
223 pdu = malloc(sizeof(*pdu));
224 for (i = 0; i < client->argc; i++)
225 strcpy((char *) pdu->argv[i], client->argv[i]);
227 pdu->argc = cpu_to_le16(client->argc);
228 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOBLINE, pdu, sizeof(*pdu));
233 int fio_clients_connect(void)
235 struct fio_client *client;
236 struct flist_head *entry, *tmp;
239 dprint(FD_NET, "client: connect all\n");
241 client_signal_handler();
243 flist_for_each_safe(entry, tmp, &client_list) {
244 client = flist_entry(entry, struct fio_client, list);
246 ret = fio_client_connect(client);
248 remove_client(client);
252 probe_client(client);
254 if (client->argc > 1)
255 send_client_cmd_line(client);
262 * Send file contents to server backend. We could use sendfile(), but to remain
263 * more portable lets just read/write the darn thing.
265 static int fio_client_send_ini(struct fio_client *client, const char *filename)
272 dprint(FD_NET, "send ini %s to %s\n", filename, client->hostname);
274 fd = open(filename, O_RDONLY);
276 log_err("fio: job file open: %s\n", strerror(errno));
280 if (fstat(fd, &sb) < 0) {
281 log_err("fio: job file stat: %s\n", strerror(errno));
285 buf = malloc(sb.st_size);
290 ret = read(fd, p, len);
299 else if (errno == EAGAIN || errno == EINTR)
304 log_err("fio: failed reading job file %s\n", filename);
308 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, buf, sb.st_size);
313 int fio_clients_send_ini(const char *filename)
315 struct fio_client *client;
316 struct flist_head *entry, *tmp;
318 flist_for_each_safe(entry, tmp, &client_list) {
319 client = flist_entry(entry, struct fio_client, list);
321 if (fio_client_send_ini(client, filename))
322 remove_client(client);
328 static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
330 dst->max_val = le64_to_cpu(src->max_val);
331 dst->min_val = le64_to_cpu(src->min_val);
332 dst->samples = le64_to_cpu(src->samples);
335 * Floats arrive as IEEE 754 encoded uint64_t, convert back to double
337 dst->mean.u.f = fio_uint64_to_double(le64_to_cpu(dst->mean.u.i));
338 dst->S.u.f = fio_uint64_to_double(le64_to_cpu(dst->S.u.i));
341 static void convert_ts(struct thread_stat *dst, struct thread_stat *src)
345 dst->error = le32_to_cpu(src->error);
346 dst->groupid = le32_to_cpu(src->groupid);
347 dst->pid = le32_to_cpu(src->pid);
348 dst->members = le32_to_cpu(src->members);
350 for (i = 0; i < 2; i++) {
351 convert_io_stat(&dst->clat_stat[i], &src->clat_stat[i]);
352 convert_io_stat(&dst->slat_stat[i], &src->slat_stat[i]);
353 convert_io_stat(&dst->lat_stat[i], &src->lat_stat[i]);
354 convert_io_stat(&dst->bw_stat[i], &src->bw_stat[i]);
357 dst->usr_time = le64_to_cpu(src->usr_time);
358 dst->sys_time = le64_to_cpu(src->sys_time);
359 dst->ctx = le64_to_cpu(src->ctx);
360 dst->minf = le64_to_cpu(src->minf);
361 dst->majf = le64_to_cpu(src->majf);
362 dst->clat_percentiles = le64_to_cpu(src->clat_percentiles);
364 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
365 fio_fp64_t *fps = &src->percentile_list[i];
366 fio_fp64_t *fpd = &dst->percentile_list[i];
368 fpd->u.f = fio_uint64_to_double(le64_to_cpu(fps->u.i));
371 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
372 dst->io_u_map[i] = le32_to_cpu(src->io_u_map[i]);
373 dst->io_u_submit[i] = le32_to_cpu(src->io_u_submit[i]);
374 dst->io_u_complete[i] = le32_to_cpu(src->io_u_complete[i]);
377 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
378 dst->io_u_lat_u[i] = le32_to_cpu(src->io_u_lat_u[i]);
379 dst->io_u_lat_m[i] = le32_to_cpu(src->io_u_lat_m[i]);
382 for (i = 0; i < 2; i++)
383 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
384 dst->io_u_plat[i][j] = le32_to_cpu(src->io_u_plat[i][j]);
386 for (i = 0; i < 3; i++) {
387 dst->total_io_u[i] = le64_to_cpu(src->total_io_u[i]);
388 dst->short_io_u[i] = le64_to_cpu(src->short_io_u[i]);
391 dst->total_submit = le64_to_cpu(src->total_submit);
392 dst->total_complete = le64_to_cpu(src->total_complete);
394 for (i = 0; i < 2; i++) {
395 dst->io_bytes[i] = le64_to_cpu(src->io_bytes[i]);
396 dst->runtime[i] = le64_to_cpu(src->runtime[i]);
399 dst->total_run_time = le64_to_cpu(src->total_run_time);
400 dst->continue_on_error = le16_to_cpu(src->continue_on_error);
401 dst->total_err_count = le64_to_cpu(src->total_err_count);
402 dst->first_error = le32_to_cpu(src->first_error);
403 dst->kb_base = le32_to_cpu(src->kb_base);
406 static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
410 for (i = 0; i < 2; i++) {
411 dst->max_run[i] = le64_to_cpu(src->max_run[i]);
412 dst->min_run[i] = le64_to_cpu(src->min_run[i]);
413 dst->max_bw[i] = le64_to_cpu(src->max_bw[i]);
414 dst->min_bw[i] = le64_to_cpu(src->min_bw[i]);
415 dst->io_kb[i] = le64_to_cpu(src->io_kb[i]);
416 dst->agg[i] = le64_to_cpu(src->agg[i]);
419 dst->kb_base = le32_to_cpu(src->kb_base);
420 dst->groupid = le32_to_cpu(src->groupid);
423 static void handle_ts(struct fio_net_cmd *cmd)
425 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
427 convert_ts(&p->ts, &p->ts);
428 convert_gs(&p->rs, &p->rs);
430 show_thread_status(&p->ts, &p->rs);
433 static void handle_gs(struct fio_net_cmd *cmd)
435 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
438 show_group_stats(gs);
441 static void handle_eta(struct fio_net_cmd *cmd)
443 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
446 je->nr_running = le32_to_cpu(je->nr_running);
447 je->nr_ramp = le32_to_cpu(je->nr_ramp);
448 je->nr_pending = le32_to_cpu(je->nr_pending);
449 je->files_open = le32_to_cpu(je->files_open);
450 je->m_rate = le32_to_cpu(je->m_rate);
451 je->t_rate = le32_to_cpu(je->t_rate);
452 je->m_iops = le32_to_cpu(je->m_iops);
453 je->t_iops = le32_to_cpu(je->t_iops);
455 for (i = 0; i < 2; i++) {
456 je->rate[i] = le32_to_cpu(je->rate[i]);
457 je->iops[i] = le32_to_cpu(je->iops[i]);
460 je->elapsed_sec = le32_to_cpu(je->nr_running);
461 je->eta_sec = le64_to_cpu(je->eta_sec);
463 display_thread_status(je);
466 static void handle_probe(struct fio_net_cmd *cmd)
468 struct cmd_probe_pdu *probe = (struct cmd_probe_pdu *) cmd->payload;
470 log_info("Probe: hostname=%s, be=%u, fio ver %u.%u.%u\n",
471 probe->hostname, probe->bigendian, probe->fio_major,
472 probe->fio_minor, probe->fio_patch);
475 static int handle_client(struct fio_client *client, int one)
477 struct fio_net_cmd *cmd;
480 dprint(FD_NET, "client: handle %s\n", client->hostname);
482 while ((cmd = fio_net_recv_cmd(client->fd, 1)) != NULL) {
483 dprint(FD_NET, "%s: got cmd op %d\n", client->hostname,
486 switch (cmd->opcode) {
487 case FIO_NET_CMD_QUIT:
488 remove_client(client);
492 case FIO_NET_CMD_TEXT: {
493 const char *buf = (const char *) cmd->payload;
496 if (!client->skip_newline)
497 fprintf(f_out, "<%s> ", client->hostname);
498 ret = fwrite(buf, cmd->pdu_len, 1, f_out);
500 client->skip_newline = strchr(buf, '\n') == NULL;
512 case FIO_NET_CMD_ETA:
516 case FIO_NET_CMD_PROBE:
520 case FIO_NET_CMD_START:
521 client->state = Client_started;
524 case FIO_NET_CMD_STOP:
525 client->state = Client_stopped;
529 log_err("fio: unknown client op: %d\n", cmd->opcode);
541 int fio_handle_clients(void)
543 struct fio_client *client;
544 struct flist_head *entry;
548 pfds = malloc(nr_clients * sizeof(struct pollfd));
550 while (!exit_backend && nr_clients) {
552 flist_for_each(entry, &client_list) {
553 client = flist_entry(entry, struct fio_client, list);
555 pfds[i].fd = client->fd;
556 pfds[i].events = POLLIN;
560 assert(i == nr_clients);
563 ret = poll(pfds, nr_clients, 100);
567 log_err("fio: poll clients: %s\n", strerror(errno));
573 for (i = 0; i < nr_clients; i++) {
574 if (!(pfds[i].revents & POLLIN))
577 client = find_client_by_fd(pfds[i].fd);
579 log_err("fio: unknown client\n");
582 handle_client(client, 0);