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;
41 static FLIST_HEAD(client_list);
43 static int handle_client(struct fio_client *client, int one);
45 static struct fio_client *find_client_by_fd(int fd)
47 struct fio_client *client;
48 struct flist_head *entry;
50 flist_for_each(entry, &client_list) {
51 client = flist_entry(entry, struct fio_client, list);
60 static struct fio_client *find_client_by_name(const char *name)
62 struct fio_client *client;
63 struct flist_head *entry;
65 flist_for_each(entry, &client_list) {
66 client = flist_entry(entry, struct fio_client, list);
68 if (!strcmp(name, client->hostname))
75 static void remove_client(struct fio_client *client)
77 dprint(FD_NET, "removed client <%s>\n", client->hostname);
78 flist_del(&client->list);
81 free(client->hostname);
88 static void __fio_client_add_cmd_option(struct fio_client *client,
92 client->argv = realloc(client->argv, sizeof(char *) * client->argc);
93 client->argv[client->argc - 1] = strdup(opt);
96 void fio_client_add_cmd_option(const char *hostname, const char *opt)
98 struct fio_client *client;
100 if (!hostname || !opt)
103 client = find_client_by_name(hostname);
105 log_err("fio: unknown client %s\n", hostname);
109 __fio_client_add_cmd_option(client, opt);
112 void fio_client_add(const char *hostname)
114 struct fio_client *client;
116 dprint(FD_NET, "added client <%s>\n", hostname);
117 client = malloc(sizeof(*client));
118 memset(client, 0, sizeof(*client));
120 client->hostname = strdup(hostname);
123 __fio_client_add_cmd_option(client, "fio");
125 flist_add(&client->list, &client_list);
129 static int fio_client_connect(struct fio_client *client)
133 dprint(FD_NET, "connect to host %s\n", client->hostname);
135 memset(&client->addr, 0, sizeof(client->addr));
136 client->addr.sin_family = AF_INET;
137 client->addr.sin_port = htons(fio_net_port);
139 if (inet_aton(client->hostname, &client->addr.sin_addr) != 1) {
140 struct hostent *hent;
142 hent = gethostbyname(client->hostname);
144 log_err("fio: gethostbyname: %s\n", strerror(errno));
148 memcpy(&client->addr.sin_addr, hent->h_addr, 4);
151 fd = socket(AF_INET, SOCK_STREAM, 0);
153 log_err("fio: socket: %s\n", strerror(errno));
157 if (connect(fd, (struct sockaddr *) &client->addr, sizeof(client->addr)) < 0) {
158 log_err("fio: connect: %s\n", strerror(errno));
159 log_err("fio: failed to connect to %s\n", client->hostname);
164 client->state = Client_connected;
168 void fio_clients_terminate(void)
170 struct flist_head *entry;
171 struct fio_client *client;
173 flist_for_each(entry, &client_list) {
174 client = flist_entry(entry, struct fio_client, list);
176 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_QUIT, 0);
180 static void sig_int(int sig)
182 fio_clients_terminate();
185 static void client_signal_handler(void)
187 struct sigaction act;
189 memset(&act, 0, sizeof(act));
190 act.sa_handler = sig_int;
191 act.sa_flags = SA_RESTART;
192 sigaction(SIGINT, &act, NULL);
194 memset(&act, 0, sizeof(act));
195 act.sa_handler = sig_int;
196 act.sa_flags = SA_RESTART;
197 sigaction(SIGTERM, &act, NULL);
200 static void probe_client(struct fio_client *client)
202 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_PROBE, 0);
203 handle_client(client, 1);
206 static int send_client_cmd_line(struct fio_client *client)
208 struct cmd_line_pdu *pdu;
211 pdu = malloc(sizeof(*pdu));
212 for (i = 0; i < client->argc; i++)
213 strcpy((char *) pdu->argv[i], client->argv[i]);
215 pdu->argc = cpu_to_le16(client->argc);
216 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOBLINE, pdu, sizeof(*pdu));
221 int fio_clients_connect(void)
223 struct fio_client *client;
224 struct flist_head *entry, *tmp;
227 client_signal_handler();
229 flist_for_each_safe(entry, tmp, &client_list) {
230 client = flist_entry(entry, struct fio_client, list);
232 ret = fio_client_connect(client);
234 remove_client(client);
238 probe_client(client);
240 if (client->argc > 1)
241 send_client_cmd_line(client);
248 * Send file contents to server backend. We could use sendfile(), but to remain
249 * more portable lets just read/write the darn thing.
251 static int fio_client_send_ini(struct fio_client *client, const char *filename)
258 dprint(FD_NET, "send ini %s to %s\n", filename, client->hostname);
260 fd = open(filename, O_RDONLY);
262 log_err("fio: job file open: %s\n", strerror(errno));
266 if (fstat(fd, &sb) < 0) {
267 log_err("fio: job file stat: %s\n", strerror(errno));
271 buf = malloc(sb.st_size);
276 ret = read(fd, p, len);
285 else if (errno == EAGAIN || errno == EINTR)
290 log_err("fio: failed reading job file %s\n", filename);
294 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, buf, sb.st_size);
299 int fio_clients_send_ini(const char *filename)
301 struct fio_client *client;
302 struct flist_head *entry, *tmp;
304 flist_for_each_safe(entry, tmp, &client_list) {
305 client = flist_entry(entry, struct fio_client, list);
307 if (fio_client_send_ini(client, filename))
308 remove_client(client);
314 static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
316 dst->max_val = le64_to_cpu(src->max_val);
317 dst->min_val = le64_to_cpu(src->min_val);
318 dst->samples = le64_to_cpu(src->samples);
320 dst->mean = __le64_to_cpu(src->mean);
321 dst->S = __le64_to_cpu(src->S);
324 static void convert_ts(struct thread_stat *dst, struct thread_stat *src)
328 dst->error = le32_to_cpu(src->error);
329 dst->groupid = le32_to_cpu(src->groupid);
330 dst->pid = le32_to_cpu(src->pid);
331 dst->members = le32_to_cpu(src->members);
333 for (i = 0; i < 2; i++) {
334 convert_io_stat(&dst->clat_stat[i], &src->clat_stat[i]);
335 convert_io_stat(&dst->slat_stat[i], &src->slat_stat[i]);
336 convert_io_stat(&dst->lat_stat[i], &src->lat_stat[i]);
337 convert_io_stat(&dst->bw_stat[i], &src->bw_stat[i]);
340 dst->usr_time = le64_to_cpu(src->usr_time);
341 dst->sys_time = le64_to_cpu(src->sys_time);
342 dst->ctx = le64_to_cpu(src->ctx);
343 dst->minf = le64_to_cpu(src->minf);
344 dst->majf = le64_to_cpu(src->majf);
345 dst->clat_percentiles = le64_to_cpu(src->clat_percentiles);
346 dst->percentile_list = NULL;
348 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
349 dst->io_u_map[i] = le32_to_cpu(src->io_u_map[i]);
350 dst->io_u_submit[i] = le32_to_cpu(src->io_u_submit[i]);
351 dst->io_u_complete[i] = le32_to_cpu(src->io_u_complete[i]);
354 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
355 dst->io_u_lat_u[i] = le32_to_cpu(src->io_u_lat_u[i]);
356 dst->io_u_lat_m[i] = le32_to_cpu(src->io_u_lat_m[i]);
359 for (i = 0; i < 2; i++)
360 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
361 dst->io_u_plat[i][j] = le32_to_cpu(src->io_u_plat[i][j]);
363 for (i = 0; i < 3; i++) {
364 dst->total_io_u[i] = le64_to_cpu(src->total_io_u[i]);
365 dst->short_io_u[i] = le64_to_cpu(src->short_io_u[i]);
368 dst->total_submit = le64_to_cpu(src->total_submit);
369 dst->total_complete = le64_to_cpu(src->total_complete);
371 for (i = 0; i < 2; i++) {
372 dst->io_bytes[i] = le64_to_cpu(src->io_bytes[i]);
373 dst->runtime[i] = le64_to_cpu(src->runtime[i]);
376 dst->total_run_time = le64_to_cpu(src->total_run_time);
377 dst->continue_on_error = le16_to_cpu(src->continue_on_error);
378 dst->total_err_count = le64_to_cpu(src->total_err_count);
379 dst->first_error = le32_to_cpu(src->first_error);
380 dst->kb_base = le32_to_cpu(src->kb_base);
383 static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
387 for (i = 0; i < 2; i++) {
388 dst->max_run[i] = le64_to_cpu(src->max_run[i]);
389 dst->min_run[i] = le64_to_cpu(src->min_run[i]);
390 dst->max_bw[i] = le64_to_cpu(src->max_bw[i]);
391 dst->min_bw[i] = le64_to_cpu(src->min_bw[i]);
392 dst->io_kb[i] = le64_to_cpu(src->io_kb[i]);
393 dst->agg[i] = le64_to_cpu(src->agg[i]);
396 dst->kb_base = le32_to_cpu(src->kb_base);
397 dst->groupid = le32_to_cpu(src->groupid);
400 static void handle_ts(struct fio_net_cmd *cmd)
402 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
404 convert_ts(&p->ts, &p->ts);
405 convert_gs(&p->rs, &p->rs);
407 show_thread_status(&p->ts, &p->rs);
410 static void handle_gs(struct fio_net_cmd *cmd)
412 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
415 show_group_stats(gs);
418 static void handle_eta(struct fio_net_cmd *cmd)
420 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
423 je->nr_running = le32_to_cpu(je->nr_running);
424 je->nr_ramp = le32_to_cpu(je->nr_ramp);
425 je->nr_pending = le32_to_cpu(je->nr_pending);
426 je->files_open = le32_to_cpu(je->files_open);
427 je->m_rate = le32_to_cpu(je->m_rate);
428 je->t_rate = le32_to_cpu(je->t_rate);
429 je->m_iops = le32_to_cpu(je->m_iops);
430 je->t_iops = le32_to_cpu(je->t_iops);
432 for (i = 0; i < 2; i++) {
433 je->rate[i] = le32_to_cpu(je->rate[i]);
434 je->iops[i] = le32_to_cpu(je->iops[i]);
437 je->elapsed_sec = le32_to_cpu(je->nr_running);
438 je->eta_sec = le64_to_cpu(je->eta_sec);
440 display_thread_status(je);
443 static void handle_probe(struct fio_net_cmd *cmd)
445 struct cmd_probe_pdu *probe = (struct cmd_probe_pdu *) cmd->payload;
447 log_info("Probe: hostname=%s, fio ver %u.%u.%u\n", probe->hostname,
448 probe->fio_major, probe->fio_minor, probe->fio_patch);
451 static int handle_client(struct fio_client *client, int one)
453 struct fio_net_cmd *cmd;
456 while ((cmd = fio_net_recv_cmd(client->fd, 1)) != NULL) {
457 dprint(FD_NET, "%s: got cmd op %d\n", client->hostname,
460 switch (cmd->opcode) {
461 case FIO_NET_CMD_QUIT:
462 remove_client(client);
466 case FIO_NET_CMD_TEXT:
467 fprintf(f_out, "Client <%s>: ", client->hostname);
468 fwrite(cmd->payload, cmd->pdu_len, 1, f_out);
480 case FIO_NET_CMD_ETA:
484 case FIO_NET_CMD_PROBE:
488 case FIO_NET_CMD_START:
489 client->state = Client_started;
492 case FIO_NET_CMD_STOP:
493 client->state = Client_stopped;
497 log_err("fio: unknown client op: %d\n", cmd->opcode);
509 int fio_handle_clients(void)
511 struct fio_client *client;
512 struct flist_head *entry;
516 pfds = malloc(nr_clients * sizeof(struct pollfd));
518 while (!exit_backend && nr_clients) {
520 flist_for_each(entry, &client_list) {
521 client = flist_entry(entry, struct fio_client, list);
523 pfds[i].fd = client->fd;
524 pfds[i].events = POLLIN;
528 assert(i == nr_clients);
531 ret = poll(pfds, nr_clients, 100);
535 log_err("fio: poll clients: %s\n", strerror(errno));
541 for (i = 0; i < nr_clients; i++) {
542 if (!(pfds[i].revents & POLLIN))
545 client = find_client_by_fd(pfds[i].fd);
547 log_err("fio: unknown client\n");
550 handle_client(client, 0);