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 struct fio_client *find_client_by_fd(int fd)
32 struct fio_client *client;
33 struct flist_head *entry;
35 flist_for_each(entry, &client_list) {
36 client = flist_entry(entry, struct fio_client, list);
46 static struct fio_client *find_client_by_name(const char *name)
48 struct fio_client *client;
49 struct flist_head *entry;
51 flist_for_each(entry, &client_list) {
52 client = flist_entry(entry, struct fio_client, list);
54 if (!strcmp(name, client->hostname))
62 static void remove_client(struct fio_client *client)
64 dprint(FD_NET, "removed client <%s>\n", client->hostname);
65 flist_del(&client->list);
67 free(client->hostname);
71 void fio_client_add(const char *hostname)
73 struct fio_client *client;
75 dprint(FD_NET, "added client <%s>\n", hostname);
76 client = malloc(sizeof(*client));
77 memset(client, 0, sizeof(*client));
78 client->hostname = strdup(hostname);
80 flist_add(&client->list, &client_list);
84 static int fio_client_connect(struct fio_client *client)
88 dprint(FD_NET, "connect to host %s\n", client->hostname);
90 memset(&client->addr, 0, sizeof(client->addr));
91 client->addr.sin_family = AF_INET;
92 client->addr.sin_port = htons(fio_net_port);
94 if (inet_aton(client->hostname, &client->addr.sin_addr) != 1) {
97 hent = gethostbyname(client->hostname);
99 log_err("fio: gethostbyname: %s\n", strerror(errno));
103 memcpy(&client->addr.sin_addr, hent->h_addr, 4);
106 fd = socket(AF_INET, SOCK_STREAM, 0);
108 log_err("fio: socket: %s\n", strerror(errno));
112 if (connect(fd, (struct sockaddr *) &client->addr, sizeof(client->addr)) < 0) {
113 log_err("fio: connect: %s\n", strerror(errno));
114 log_err("fio: failed to connect to %s\n", client->hostname);
122 void fio_clients_terminate(void)
124 struct flist_head *entry;
125 struct fio_client *client;
127 flist_for_each(entry, &client_list) {
128 client = flist_entry(entry, struct fio_client, list);
130 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_QUIT, 0);
134 static void sig_int(int sig)
136 fio_clients_terminate();
139 static void client_signal_handler(void)
141 struct sigaction act;
143 memset(&act, 0, sizeof(act));
144 act.sa_handler = sig_int;
145 act.sa_flags = SA_RESTART;
146 sigaction(SIGINT, &act, NULL);
148 memset(&act, 0, sizeof(act));
149 act.sa_handler = sig_int;
150 act.sa_flags = SA_RESTART;
151 sigaction(SIGTERM, &act, NULL);
154 int fio_clients_connect(void)
156 struct fio_client *client;
157 struct flist_head *entry, *tmp;
160 client_signal_handler();
162 flist_for_each_safe(entry, tmp, &client_list) {
163 client = flist_entry(entry, struct fio_client, list);
165 ret = fio_client_connect(client);
167 remove_client(client);
173 static int send_file_buf(struct fio_client *client, char *buf, off_t size)
175 return fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, buf, size);
179 * Send file contents to server backend. We could use sendfile(), but to remain
180 * more portable lets just read/write the darn thing.
182 static int fio_client_send_ini(struct fio_client *client, const char *filename)
189 dprint(FD_NET, "send ini %s to %s\n", filename, client->hostname);
191 fd = open(filename, O_RDONLY);
193 log_err("fio: job file open: %s\n", strerror(errno));
197 if (fstat(fd, &sb) < 0) {
198 log_err("fio: job file stat: %s\n", strerror(errno));
202 buf = malloc(sb.st_size);
207 ret = read(fd, p, len);
216 else if (errno == EAGAIN || errno == EINTR)
220 ret = send_file_buf(client, buf, sb.st_size);
225 int fio_clients_send_ini(const char *filename)
227 struct fio_client *client;
228 struct flist_head *entry, *tmp;
230 flist_for_each_safe(entry, tmp, &client_list) {
231 client = flist_entry(entry, struct fio_client, list);
233 if (fio_client_send_ini(client, filename))
234 remove_client(client);
240 static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
242 dst->max_val = le64_to_cpu(src->max_val);
243 dst->min_val = le64_to_cpu(src->min_val);
244 dst->samples = le64_to_cpu(src->samples);
246 dst->mean = __le64_to_cpu(src->mean);
247 dst->S = __le64_to_cpu(src->S);
250 static void convert_ts(struct thread_stat *dst, struct thread_stat *src)
254 dst->error = le32_to_cpu(src->error);
255 dst->groupid = le32_to_cpu(src->groupid);
256 dst->pid = le32_to_cpu(src->pid);
257 dst->members = le32_to_cpu(src->members);
259 for (i = 0; i < 2; i++) {
260 convert_io_stat(&dst->clat_stat[i], &src->clat_stat[i]);
261 convert_io_stat(&dst->slat_stat[i], &src->slat_stat[i]);
262 convert_io_stat(&dst->lat_stat[i], &src->lat_stat[i]);
263 convert_io_stat(&dst->bw_stat[i], &src->bw_stat[i]);
266 dst->usr_time = le64_to_cpu(src->usr_time);
267 dst->sys_time = le64_to_cpu(src->sys_time);
268 dst->ctx = le64_to_cpu(src->ctx);
269 dst->minf = le64_to_cpu(src->minf);
270 dst->majf = le64_to_cpu(src->majf);
271 dst->clat_percentiles = le64_to_cpu(src->clat_percentiles);
272 dst->percentile_list = NULL;
274 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
275 dst->io_u_map[i] = le32_to_cpu(src->io_u_map[i]);
276 dst->io_u_submit[i] = le32_to_cpu(src->io_u_submit[i]);
277 dst->io_u_complete[i] = le32_to_cpu(src->io_u_complete[i]);
280 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
281 dst->io_u_lat_u[i] = le32_to_cpu(src->io_u_lat_u[i]);
282 dst->io_u_lat_m[i] = le32_to_cpu(src->io_u_lat_m[i]);
285 for (i = 0; i < 2; i++)
286 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
287 dst->io_u_plat[i][j] = le32_to_cpu(src->io_u_plat[i][j]);
289 for (i = 0; i < 3; i++) {
290 dst->total_io_u[i] = le64_to_cpu(src->total_io_u[i]);
291 dst->short_io_u[i] = le64_to_cpu(src->short_io_u[i]);
294 dst->total_submit = le64_to_cpu(src->total_submit);
295 dst->total_complete = le64_to_cpu(src->total_complete);
297 for (i = 0; i < 2; i++) {
298 dst->io_bytes[i] = le64_to_cpu(src->io_bytes[i]);
299 dst->runtime[i] = le64_to_cpu(src->runtime[i]);
302 dst->total_run_time = le64_to_cpu(src->total_run_time);
303 dst->continue_on_error = le16_to_cpu(src->continue_on_error);
304 dst->total_err_count = le64_to_cpu(src->total_err_count);
305 dst->first_error = le32_to_cpu(src->first_error);
306 dst->kb_base = le32_to_cpu(src->kb_base);
309 static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
313 for (i = 0; i < 2; i++) {
314 dst->max_run[i] = le64_to_cpu(src->max_run[i]);
315 dst->min_run[i] = le64_to_cpu(src->min_run[i]);
316 dst->max_bw[i] = le64_to_cpu(src->max_bw[i]);
317 dst->min_bw[i] = le64_to_cpu(src->min_bw[i]);
318 dst->io_kb[i] = le64_to_cpu(src->io_kb[i]);
319 dst->agg[i] = le64_to_cpu(src->agg[i]);
322 dst->kb_base = le32_to_cpu(src->kb_base);
323 dst->groupid = le32_to_cpu(src->groupid);
326 static void handle_ts(struct fio_net_cmd *cmd)
328 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
330 convert_ts(&p->ts, &p->ts);
331 convert_gs(&p->rs, &p->rs);
333 show_thread_status(&p->ts, &p->rs);
336 static void handle_gs(struct fio_net_cmd *cmd)
338 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
341 show_group_stats(gs);
344 static void handle_eta(struct fio_net_cmd *cmd)
346 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
349 je->nr_running = le32_to_cpu(je->nr_running);
350 je->nr_ramp = le32_to_cpu(je->nr_ramp);
351 je->nr_pending = le32_to_cpu(je->nr_pending);
352 je->files_open = le32_to_cpu(je->files_open);
353 je->m_rate = le32_to_cpu(je->m_rate);
354 je->t_rate = le32_to_cpu(je->t_rate);
355 je->m_iops = le32_to_cpu(je->m_iops);
356 je->t_iops = le32_to_cpu(je->t_iops);
358 for (i = 0; i < 2; i++) {
359 je->rate[i] = le32_to_cpu(je->rate[i]);
360 je->iops[i] = le32_to_cpu(je->iops[i]);
363 je->elapsed_sec = le32_to_cpu(je->nr_running);
364 je->eta_sec = le64_to_cpu(je->eta_sec);
366 display_thread_status(je);
369 static int handle_client(struct fio_client *client)
371 struct fio_net_cmd *cmd;
374 while ((cmd = fio_net_recv_cmd(client->fd)) != NULL) {
375 dprint(FD_NET, "%s: got cmd op %d\n", client->hostname,
378 switch (cmd->opcode) {
379 case FIO_NET_CMD_ACK:
382 case FIO_NET_CMD_QUIT:
383 remove_client(client);
387 case FIO_NET_CMD_TEXT:
388 fwrite(cmd->payload, cmd->pdu_len, 1, stdout);
400 case FIO_NET_CMD_ETA:
405 log_err("fio: unknown client op: %d\n", cmd->opcode);
417 int fio_handle_clients(void)
419 struct fio_client *client;
420 struct flist_head *entry;
424 pfds = malloc(nr_clients * sizeof(struct pollfd));
426 while (!exit_backend && nr_clients) {
428 flist_for_each(entry, &client_list) {
429 client = flist_entry(entry, struct fio_client, list);
431 pfds[i].fd = client->fd;
432 pfds[i].events = POLLIN;
436 assert(i == nr_clients);
438 ret = poll(pfds, nr_clients, 100);
442 log_err("fio: poll clients: %s\n", strerror(errno));
447 for (i = 0; i < nr_clients; i++) {
448 if (!(pfds[i].revents & POLLIN))
451 client = find_client_by_fd(pfds[i].fd);
453 log_err("fio: unknown client\n");
456 handle_client(client);