12 #include <netinet/in.h>
13 #include <arpa/inet.h>
18 #include "crc/crc32.h"
22 struct flist_head list;
23 struct sockaddr_in addr;
39 static FLIST_HEAD(client_list);
41 static int handle_client(struct fio_client *client, int one);
43 static struct fio_client *find_client_by_fd(int fd)
45 struct fio_client *client;
46 struct flist_head *entry;
48 flist_for_each(entry, &client_list) {
49 client = flist_entry(entry, struct fio_client, list);
58 static struct fio_client *find_client_by_name(const char *name)
60 struct fio_client *client;
61 struct flist_head *entry;
63 flist_for_each(entry, &client_list) {
64 client = flist_entry(entry, struct fio_client, list);
66 if (!strcmp(name, client->hostname))
73 static void remove_client(struct fio_client *client)
75 dprint(FD_NET, "removed client <%s>\n", client->hostname);
76 flist_del(&client->list);
79 free(client->hostname);
86 static void __fio_client_add_cmd_option(struct fio_client *client,
90 client->argv = realloc(client->argv, sizeof(char *) * client->argc);
91 client->argv[client->argc - 1] = strdup(opt);
94 void fio_client_add_cmd_option(const char *hostname, const char *opt)
96 struct fio_client *client;
98 if (!hostname || !opt)
101 client = find_client_by_name(hostname);
103 log_err("fio: unknown client %s\n", hostname);
107 __fio_client_add_cmd_option(client, opt);
110 void fio_client_add(const char *hostname)
112 struct fio_client *client;
114 dprint(FD_NET, "added client <%s>\n", hostname);
115 client = malloc(sizeof(*client));
116 memset(client, 0, sizeof(*client));
118 client->hostname = strdup(hostname);
121 __fio_client_add_cmd_option(client, "fio");
123 flist_add(&client->list, &client_list);
127 static int fio_client_connect(struct fio_client *client)
131 dprint(FD_NET, "connect to host %s\n", client->hostname);
133 memset(&client->addr, 0, sizeof(client->addr));
134 client->addr.sin_family = AF_INET;
135 client->addr.sin_port = htons(fio_net_port);
137 if (inet_aton(client->hostname, &client->addr.sin_addr) != 1) {
138 struct hostent *hent;
140 hent = gethostbyname(client->hostname);
142 log_err("fio: gethostbyname: %s\n", strerror(errno));
146 memcpy(&client->addr.sin_addr, hent->h_addr, 4);
149 fd = socket(AF_INET, SOCK_STREAM, 0);
151 log_err("fio: socket: %s\n", strerror(errno));
155 if (connect(fd, (struct sockaddr *) &client->addr, sizeof(client->addr)) < 0) {
156 log_err("fio: connect: %s\n", strerror(errno));
157 log_err("fio: failed to connect to %s\n", client->hostname);
162 client->state = Client_connected;
166 void fio_clients_terminate(void)
168 struct flist_head *entry;
169 struct fio_client *client;
171 flist_for_each(entry, &client_list) {
172 client = flist_entry(entry, struct fio_client, list);
174 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_QUIT, 0);
178 static void sig_int(int sig)
180 fio_clients_terminate();
183 static void client_signal_handler(void)
185 struct sigaction act;
187 memset(&act, 0, sizeof(act));
188 act.sa_handler = sig_int;
189 act.sa_flags = SA_RESTART;
190 sigaction(SIGINT, &act, NULL);
192 memset(&act, 0, sizeof(act));
193 act.sa_handler = sig_int;
194 act.sa_flags = SA_RESTART;
195 sigaction(SIGTERM, &act, NULL);
198 static void probe_client(struct fio_client *client)
200 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_PROBE, 0);
201 handle_client(client, 1);
204 static int send_client_cmd_line(struct fio_client *client)
206 struct cmd_line_pdu *pdu;
209 pdu = malloc(sizeof(*pdu));
210 for (i = 0; i < client->argc; i++)
211 strcpy((char *) pdu->argv[i], client->argv[i]);
213 pdu->argc = cpu_to_le16(client->argc);
214 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOBLINE, pdu, sizeof(*pdu));
219 int fio_clients_connect(void)
221 struct fio_client *client;
222 struct flist_head *entry, *tmp;
225 client_signal_handler();
227 flist_for_each_safe(entry, tmp, &client_list) {
228 client = flist_entry(entry, struct fio_client, list);
230 ret = fio_client_connect(client);
232 remove_client(client);
236 probe_client(client);
238 if (client->argc > 1)
239 send_client_cmd_line(client);
246 * Send file contents to server backend. We could use sendfile(), but to remain
247 * more portable lets just read/write the darn thing.
249 static int fio_client_send_ini(struct fio_client *client, const char *filename)
256 dprint(FD_NET, "send ini %s to %s\n", filename, client->hostname);
258 fd = open(filename, O_RDONLY);
260 log_err("fio: job file open: %s\n", strerror(errno));
264 if (fstat(fd, &sb) < 0) {
265 log_err("fio: job file stat: %s\n", strerror(errno));
269 buf = malloc(sb.st_size);
274 ret = read(fd, p, len);
283 else if (errno == EAGAIN || errno == EINTR)
288 log_err("fio: failed reading job file %s\n", filename);
292 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, buf, sb.st_size);
297 int fio_clients_send_ini(const char *filename)
299 struct fio_client *client;
300 struct flist_head *entry, *tmp;
302 flist_for_each_safe(entry, tmp, &client_list) {
303 client = flist_entry(entry, struct fio_client, list);
305 if (fio_client_send_ini(client, filename))
306 remove_client(client);
312 static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
314 dst->max_val = le64_to_cpu(src->max_val);
315 dst->min_val = le64_to_cpu(src->min_val);
316 dst->samples = le64_to_cpu(src->samples);
318 dst->mean = __le64_to_cpu(src->mean);
319 dst->S = __le64_to_cpu(src->S);
322 static void convert_ts(struct thread_stat *dst, struct thread_stat *src)
326 dst->error = le32_to_cpu(src->error);
327 dst->groupid = le32_to_cpu(src->groupid);
328 dst->pid = le32_to_cpu(src->pid);
329 dst->members = le32_to_cpu(src->members);
331 for (i = 0; i < 2; i++) {
332 convert_io_stat(&dst->clat_stat[i], &src->clat_stat[i]);
333 convert_io_stat(&dst->slat_stat[i], &src->slat_stat[i]);
334 convert_io_stat(&dst->lat_stat[i], &src->lat_stat[i]);
335 convert_io_stat(&dst->bw_stat[i], &src->bw_stat[i]);
338 dst->usr_time = le64_to_cpu(src->usr_time);
339 dst->sys_time = le64_to_cpu(src->sys_time);
340 dst->ctx = le64_to_cpu(src->ctx);
341 dst->minf = le64_to_cpu(src->minf);
342 dst->majf = le64_to_cpu(src->majf);
343 dst->clat_percentiles = le64_to_cpu(src->clat_percentiles);
344 dst->percentile_list = NULL;
346 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
347 dst->io_u_map[i] = le32_to_cpu(src->io_u_map[i]);
348 dst->io_u_submit[i] = le32_to_cpu(src->io_u_submit[i]);
349 dst->io_u_complete[i] = le32_to_cpu(src->io_u_complete[i]);
352 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
353 dst->io_u_lat_u[i] = le32_to_cpu(src->io_u_lat_u[i]);
354 dst->io_u_lat_m[i] = le32_to_cpu(src->io_u_lat_m[i]);
357 for (i = 0; i < 2; i++)
358 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
359 dst->io_u_plat[i][j] = le32_to_cpu(src->io_u_plat[i][j]);
361 for (i = 0; i < 3; i++) {
362 dst->total_io_u[i] = le64_to_cpu(src->total_io_u[i]);
363 dst->short_io_u[i] = le64_to_cpu(src->short_io_u[i]);
366 dst->total_submit = le64_to_cpu(src->total_submit);
367 dst->total_complete = le64_to_cpu(src->total_complete);
369 for (i = 0; i < 2; i++) {
370 dst->io_bytes[i] = le64_to_cpu(src->io_bytes[i]);
371 dst->runtime[i] = le64_to_cpu(src->runtime[i]);
374 dst->total_run_time = le64_to_cpu(src->total_run_time);
375 dst->continue_on_error = le16_to_cpu(src->continue_on_error);
376 dst->total_err_count = le64_to_cpu(src->total_err_count);
377 dst->first_error = le32_to_cpu(src->first_error);
378 dst->kb_base = le32_to_cpu(src->kb_base);
381 static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
385 for (i = 0; i < 2; i++) {
386 dst->max_run[i] = le64_to_cpu(src->max_run[i]);
387 dst->min_run[i] = le64_to_cpu(src->min_run[i]);
388 dst->max_bw[i] = le64_to_cpu(src->max_bw[i]);
389 dst->min_bw[i] = le64_to_cpu(src->min_bw[i]);
390 dst->io_kb[i] = le64_to_cpu(src->io_kb[i]);
391 dst->agg[i] = le64_to_cpu(src->agg[i]);
394 dst->kb_base = le32_to_cpu(src->kb_base);
395 dst->groupid = le32_to_cpu(src->groupid);
398 static void handle_ts(struct fio_net_cmd *cmd)
400 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
402 convert_ts(&p->ts, &p->ts);
403 convert_gs(&p->rs, &p->rs);
405 show_thread_status(&p->ts, &p->rs);
408 static void handle_gs(struct fio_net_cmd *cmd)
410 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
413 show_group_stats(gs);
416 static void handle_eta(struct fio_net_cmd *cmd)
418 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
421 je->nr_running = le32_to_cpu(je->nr_running);
422 je->nr_ramp = le32_to_cpu(je->nr_ramp);
423 je->nr_pending = le32_to_cpu(je->nr_pending);
424 je->files_open = le32_to_cpu(je->files_open);
425 je->m_rate = le32_to_cpu(je->m_rate);
426 je->t_rate = le32_to_cpu(je->t_rate);
427 je->m_iops = le32_to_cpu(je->m_iops);
428 je->t_iops = le32_to_cpu(je->t_iops);
430 for (i = 0; i < 2; i++) {
431 je->rate[i] = le32_to_cpu(je->rate[i]);
432 je->iops[i] = le32_to_cpu(je->iops[i]);
435 je->elapsed_sec = le32_to_cpu(je->nr_running);
436 je->eta_sec = le64_to_cpu(je->eta_sec);
438 display_thread_status(je);
441 static void handle_probe(struct fio_net_cmd *cmd)
443 struct cmd_probe_pdu *probe = (struct cmd_probe_pdu *) cmd->payload;
445 log_info("Probe: hostname=%s, fio ver %u.%u.%u\n", probe->hostname,
446 probe->fio_major, probe->fio_minor, probe->fio_patch);
449 static int handle_client(struct fio_client *client, int one)
451 struct fio_net_cmd *cmd;
454 while ((cmd = fio_net_recv_cmd(client->fd, 1)) != NULL) {
455 dprint(FD_NET, "%s: got cmd op %d\n", client->hostname,
458 switch (cmd->opcode) {
459 case FIO_NET_CMD_QUIT:
460 remove_client(client);
464 case FIO_NET_CMD_TEXT:
465 fprintf(f_out, "Client <%s>: ", client->hostname);
466 fwrite(cmd->payload, cmd->pdu_len, 1, f_out);
478 case FIO_NET_CMD_ETA:
482 case FIO_NET_CMD_PROBE:
486 case FIO_NET_CMD_START:
487 client->state = Client_started;
490 case FIO_NET_CMD_STOP:
491 client->state = Client_stopped;
495 log_err("fio: unknown client op: %d\n", cmd->opcode);
507 int fio_handle_clients(void)
509 struct fio_client *client;
510 struct flist_head *entry;
514 pfds = malloc(nr_clients * sizeof(struct pollfd));
516 while (!exit_backend && nr_clients) {
518 flist_for_each(entry, &client_list) {
519 client = flist_entry(entry, struct fio_client, list);
521 pfds[i].fd = client->fd;
522 pfds[i].events = POLLIN;
526 assert(i == nr_clients);
528 ret = poll(pfds, nr_clients, 100);
532 log_err("fio: poll clients: %s\n", strerror(errno));
537 for (i = 0; i < nr_clients; i++) {
538 if (!(pfds[i].revents & POLLIN))
541 client = find_client_by_fd(pfds[i].fd);
543 log_err("fio: unknown client\n");
546 handle_client(client, 0);