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;
28 static FLIST_HEAD(client_list);
30 static int handle_client(struct fio_client *client, int one);
32 static struct fio_client *find_client_by_fd(int fd)
34 struct fio_client *client;
35 struct flist_head *entry;
37 flist_for_each(entry, &client_list) {
38 client = flist_entry(entry, struct fio_client, list);
48 static struct fio_client *find_client_by_name(const char *name)
50 struct fio_client *client;
51 struct flist_head *entry;
53 flist_for_each(entry, &client_list) {
54 client = flist_entry(entry, struct fio_client, list);
56 if (!strcmp(name, client->hostname))
64 static void remove_client(struct fio_client *client)
66 dprint(FD_NET, "removed client <%s>\n", client->hostname);
67 flist_del(&client->list);
69 free(client->hostname);
73 void fio_client_add(const char *hostname)
75 struct fio_client *client;
77 dprint(FD_NET, "added client <%s>\n", hostname);
78 client = malloc(sizeof(*client));
79 memset(client, 0, sizeof(*client));
80 client->hostname = strdup(hostname);
82 flist_add(&client->list, &client_list);
86 static int fio_client_connect(struct fio_client *client)
90 dprint(FD_NET, "connect to host %s\n", client->hostname);
92 memset(&client->addr, 0, sizeof(client->addr));
93 client->addr.sin_family = AF_INET;
94 client->addr.sin_port = htons(fio_net_port);
96 if (inet_aton(client->hostname, &client->addr.sin_addr) != 1) {
99 hent = gethostbyname(client->hostname);
101 log_err("fio: gethostbyname: %s\n", strerror(errno));
105 memcpy(&client->addr.sin_addr, hent->h_addr, 4);
108 fd = socket(AF_INET, SOCK_STREAM, 0);
110 log_err("fio: socket: %s\n", strerror(errno));
114 if (connect(fd, (struct sockaddr *) &client->addr, sizeof(client->addr)) < 0) {
115 log_err("fio: connect: %s\n", strerror(errno));
116 log_err("fio: failed to connect to %s\n", client->hostname);
124 void fio_clients_terminate(void)
126 struct flist_head *entry;
127 struct fio_client *client;
129 flist_for_each(entry, &client_list) {
130 client = flist_entry(entry, struct fio_client, list);
132 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_QUIT, 0);
136 static void sig_int(int sig)
138 fio_clients_terminate();
141 static void client_signal_handler(void)
143 struct sigaction act;
145 memset(&act, 0, sizeof(act));
146 act.sa_handler = sig_int;
147 act.sa_flags = SA_RESTART;
148 sigaction(SIGINT, &act, NULL);
150 memset(&act, 0, sizeof(act));
151 act.sa_handler = sig_int;
152 act.sa_flags = SA_RESTART;
153 sigaction(SIGTERM, &act, NULL);
156 static void probe_client(struct fio_client *client)
158 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_PROBE, 0);
159 handle_client(client, 1);
162 int fio_clients_connect(void)
164 struct fio_client *client;
165 struct flist_head *entry, *tmp;
168 client_signal_handler();
170 flist_for_each_safe(entry, tmp, &client_list) {
171 client = flist_entry(entry, struct fio_client, list);
173 ret = fio_client_connect(client);
175 remove_client(client);
179 probe_client(client);
185 static int send_file_buf(struct fio_client *client, char *buf, off_t size)
187 return fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, buf, size);
191 * Send file contents to server backend. We could use sendfile(), but to remain
192 * more portable lets just read/write the darn thing.
194 static int fio_client_send_ini(struct fio_client *client, const char *filename)
201 dprint(FD_NET, "send ini %s to %s\n", filename, client->hostname);
203 fd = open(filename, O_RDONLY);
205 log_err("fio: job file open: %s\n", strerror(errno));
209 if (fstat(fd, &sb) < 0) {
210 log_err("fio: job file stat: %s\n", strerror(errno));
214 buf = malloc(sb.st_size);
219 ret = read(fd, p, len);
228 else if (errno == EAGAIN || errno == EINTR)
233 log_err("fio: failed reading job file %s\n", filename);
237 ret = send_file_buf(client, buf, sb.st_size);
242 int fio_clients_send_ini(const char *filename)
244 struct fio_client *client;
245 struct flist_head *entry, *tmp;
247 flist_for_each_safe(entry, tmp, &client_list) {
248 client = flist_entry(entry, struct fio_client, list);
250 if (fio_client_send_ini(client, filename))
251 remove_client(client);
257 static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
259 dst->max_val = le64_to_cpu(src->max_val);
260 dst->min_val = le64_to_cpu(src->min_val);
261 dst->samples = le64_to_cpu(src->samples);
263 dst->mean = __le64_to_cpu(src->mean);
264 dst->S = __le64_to_cpu(src->S);
267 static void convert_ts(struct thread_stat *dst, struct thread_stat *src)
271 dst->error = le32_to_cpu(src->error);
272 dst->groupid = le32_to_cpu(src->groupid);
273 dst->pid = le32_to_cpu(src->pid);
274 dst->members = le32_to_cpu(src->members);
276 for (i = 0; i < 2; i++) {
277 convert_io_stat(&dst->clat_stat[i], &src->clat_stat[i]);
278 convert_io_stat(&dst->slat_stat[i], &src->slat_stat[i]);
279 convert_io_stat(&dst->lat_stat[i], &src->lat_stat[i]);
280 convert_io_stat(&dst->bw_stat[i], &src->bw_stat[i]);
283 dst->usr_time = le64_to_cpu(src->usr_time);
284 dst->sys_time = le64_to_cpu(src->sys_time);
285 dst->ctx = le64_to_cpu(src->ctx);
286 dst->minf = le64_to_cpu(src->minf);
287 dst->majf = le64_to_cpu(src->majf);
288 dst->clat_percentiles = le64_to_cpu(src->clat_percentiles);
289 dst->percentile_list = NULL;
291 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
292 dst->io_u_map[i] = le32_to_cpu(src->io_u_map[i]);
293 dst->io_u_submit[i] = le32_to_cpu(src->io_u_submit[i]);
294 dst->io_u_complete[i] = le32_to_cpu(src->io_u_complete[i]);
297 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
298 dst->io_u_lat_u[i] = le32_to_cpu(src->io_u_lat_u[i]);
299 dst->io_u_lat_m[i] = le32_to_cpu(src->io_u_lat_m[i]);
302 for (i = 0; i < 2; i++)
303 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
304 dst->io_u_plat[i][j] = le32_to_cpu(src->io_u_plat[i][j]);
306 for (i = 0; i < 3; i++) {
307 dst->total_io_u[i] = le64_to_cpu(src->total_io_u[i]);
308 dst->short_io_u[i] = le64_to_cpu(src->short_io_u[i]);
311 dst->total_submit = le64_to_cpu(src->total_submit);
312 dst->total_complete = le64_to_cpu(src->total_complete);
314 for (i = 0; i < 2; i++) {
315 dst->io_bytes[i] = le64_to_cpu(src->io_bytes[i]);
316 dst->runtime[i] = le64_to_cpu(src->runtime[i]);
319 dst->total_run_time = le64_to_cpu(src->total_run_time);
320 dst->continue_on_error = le16_to_cpu(src->continue_on_error);
321 dst->total_err_count = le64_to_cpu(src->total_err_count);
322 dst->first_error = le32_to_cpu(src->first_error);
323 dst->kb_base = le32_to_cpu(src->kb_base);
326 static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
330 for (i = 0; i < 2; i++) {
331 dst->max_run[i] = le64_to_cpu(src->max_run[i]);
332 dst->min_run[i] = le64_to_cpu(src->min_run[i]);
333 dst->max_bw[i] = le64_to_cpu(src->max_bw[i]);
334 dst->min_bw[i] = le64_to_cpu(src->min_bw[i]);
335 dst->io_kb[i] = le64_to_cpu(src->io_kb[i]);
336 dst->agg[i] = le64_to_cpu(src->agg[i]);
339 dst->kb_base = le32_to_cpu(src->kb_base);
340 dst->groupid = le32_to_cpu(src->groupid);
343 static void handle_ts(struct fio_net_cmd *cmd)
345 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
347 convert_ts(&p->ts, &p->ts);
348 convert_gs(&p->rs, &p->rs);
350 show_thread_status(&p->ts, &p->rs);
353 static void handle_gs(struct fio_net_cmd *cmd)
355 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
358 show_group_stats(gs);
361 static void handle_eta(struct fio_net_cmd *cmd)
363 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
366 je->nr_running = le32_to_cpu(je->nr_running);
367 je->nr_ramp = le32_to_cpu(je->nr_ramp);
368 je->nr_pending = le32_to_cpu(je->nr_pending);
369 je->files_open = le32_to_cpu(je->files_open);
370 je->m_rate = le32_to_cpu(je->m_rate);
371 je->t_rate = le32_to_cpu(je->t_rate);
372 je->m_iops = le32_to_cpu(je->m_iops);
373 je->t_iops = le32_to_cpu(je->t_iops);
375 for (i = 0; i < 2; i++) {
376 je->rate[i] = le32_to_cpu(je->rate[i]);
377 je->iops[i] = le32_to_cpu(je->iops[i]);
380 je->elapsed_sec = le32_to_cpu(je->nr_running);
381 je->eta_sec = le64_to_cpu(je->eta_sec);
383 display_thread_status(je);
386 static void handle_probe(struct fio_net_cmd *cmd)
388 struct cmd_probe_pdu *probe = (struct cmd_probe_pdu *) cmd->payload;
390 log_info("Probe: hostname=%s, fio ver %u.%u.%u\n", probe->hostname,
391 probe->fio_major, probe->fio_minor, probe->fio_patch);
394 static int handle_client(struct fio_client *client, int one)
396 struct fio_net_cmd *cmd;
399 while ((cmd = fio_net_recv_cmd(client->fd, 1)) != NULL) {
400 dprint(FD_NET, "%s: got cmd op %d\n", client->hostname,
403 switch (cmd->opcode) {
404 case FIO_NET_CMD_QUIT:
405 remove_client(client);
409 case FIO_NET_CMD_TEXT:
410 fprintf(f_out, "Client <%s>: ", client->hostname);
411 fwrite(cmd->payload, cmd->pdu_len, 1, f_out);
423 case FIO_NET_CMD_ETA:
427 case FIO_NET_CMD_PROBE:
432 log_err("fio: unknown client op: %d\n", cmd->opcode);
444 int fio_handle_clients(void)
446 struct fio_client *client;
447 struct flist_head *entry;
451 pfds = malloc(nr_clients * sizeof(struct pollfd));
453 while (!exit_backend && nr_clients) {
455 flist_for_each(entry, &client_list) {
456 client = flist_entry(entry, struct fio_client, list);
458 pfds[i].fd = client->fd;
459 pfds[i].events = POLLIN;
463 assert(i == nr_clients);
465 ret = poll(pfds, nr_clients, 100);
469 log_err("fio: poll clients: %s\n", strerror(errno));
474 for (i = 0; i < nr_clients; i++) {
475 if (!(pfds[i].revents & POLLIN))
478 client = find_client_by_fd(pfds[i].fd);
480 log_err("fio: unknown client\n");
483 handle_client(client, 0);