client: improve poll() loop
[fio.git] / client.c
CommitLineData
132159a5
JA
1#include <stdio.h>
2#include <stdlib.h>
3#include <unistd.h>
4#include <limits.h>
5#include <errno.h>
6#include <fcntl.h>
7#include <sys/poll.h>
8#include <sys/types.h>
9#include <sys/stat.h>
10#include <sys/wait.h>
11#include <sys/mman.h>
12#include <netinet/in.h>
13#include <arpa/inet.h>
14#include <netdb.h>
15
16#include "fio.h"
17#include "server.h"
18#include "crc/crc32.h"
b66570dc 19#include "flist.h"
132159a5 20
b66570dc
JA
21struct fio_client {
22 struct flist_head list;
23 struct sockaddr_in addr;
24 char *hostname;
25 int fd;
81179eec
JA
26
27 int state;
28
29 uint16_t argc;
30 char **argv;
31};
32
33enum {
5c2857f9 34 Client_created = 0,
81179eec
JA
35 Client_connected = 1,
36 Client_started = 2,
37 Client_stopped = 3,
5c2857f9 38 Client_exited = 4,
b66570dc
JA
39};
40
41static FLIST_HEAD(client_list);
b66570dc 42
0b8f30a5
JA
43static int handle_client(struct fio_client *client, int one);
44
b66570dc
JA
45static struct fio_client *find_client_by_fd(int fd)
46{
47 struct fio_client *client;
48 struct flist_head *entry;
49
50 flist_for_each(entry, &client_list) {
51 client = flist_entry(entry, struct fio_client, list);
52
53 if (client->fd == fd)
54 return client;
55 }
56
57 return NULL;
58}
59
60static struct fio_client *find_client_by_name(const char *name)
61{
62 struct fio_client *client;
63 struct flist_head *entry;
64
65 flist_for_each(entry, &client_list) {
66 client = flist_entry(entry, struct fio_client, list);
67
68 if (!strcmp(name, client->hostname))
69 return client;
70 }
71
72 return NULL;
73}
74
75static void remove_client(struct fio_client *client)
76{
46c48f1f 77 dprint(FD_NET, "removed client <%s>\n", client->hostname);
b66570dc
JA
78 flist_del(&client->list);
79 nr_clients--;
81179eec 80
b66570dc 81 free(client->hostname);
81179eec
JA
82 if (client->argv)
83 free(client->argv);
84
b66570dc
JA
85 free(client);
86}
132159a5 87
81179eec
JA
88static void __fio_client_add_cmd_option(struct fio_client *client,
89 const char *opt)
90{
91 client->argc++;
92 client->argv = realloc(client->argv, sizeof(char *) * client->argc);
93 client->argv[client->argc - 1] = strdup(opt);
94}
95
96void fio_client_add_cmd_option(const char *hostname, const char *opt)
97{
98 struct fio_client *client;
99
100 if (!hostname || !opt)
101 return;
102
103 client = find_client_by_name(hostname);
104 if (!client) {
105 log_err("fio: unknown client %s\n", hostname);
106 return;
107 }
108
109 __fio_client_add_cmd_option(client, opt);
110}
111
a37f69b7 112void fio_client_add(const char *hostname)
132159a5 113{
b66570dc 114 struct fio_client *client;
132159a5 115
46c48f1f 116 dprint(FD_NET, "added client <%s>\n", hostname);
b66570dc 117 client = malloc(sizeof(*client));
a37f69b7 118 memset(client, 0, sizeof(*client));
81179eec 119
a37f69b7
JA
120 client->hostname = strdup(hostname);
121 client->fd = -1;
81179eec
JA
122
123 __fio_client_add_cmd_option(client, "fio");
124
a37f69b7
JA
125 flist_add(&client->list, &client_list);
126 nr_clients++;
127}
128
129static int fio_client_connect(struct fio_client *client)
130{
131 int fd;
132159a5 132
46c48f1f
JA
133 dprint(FD_NET, "connect to host %s\n", client->hostname);
134
b66570dc
JA
135 memset(&client->addr, 0, sizeof(client->addr));
136 client->addr.sin_family = AF_INET;
137 client->addr.sin_port = htons(fio_net_port);
138
a37f69b7 139 if (inet_aton(client->hostname, &client->addr.sin_addr) != 1) {
132159a5
JA
140 struct hostent *hent;
141
a37f69b7 142 hent = gethostbyname(client->hostname);
132159a5
JA
143 if (!hent) {
144 log_err("fio: gethostbyname: %s\n", strerror(errno));
145 return 1;
146 }
147
b66570dc 148 memcpy(&client->addr.sin_addr, hent->h_addr, 4);
132159a5
JA
149 }
150
151 fd = socket(AF_INET, SOCK_STREAM, 0);
152 if (fd < 0) {
153 log_err("fio: socket: %s\n", strerror(errno));
154 return 1;
155 }
156
b66570dc 157 if (connect(fd, (struct sockaddr *) &client->addr, sizeof(client->addr)) < 0) {
132159a5 158 log_err("fio: connect: %s\n", strerror(errno));
cdf54d85 159 log_err("fio: failed to connect to %s\n", client->hostname);
132159a5
JA
160 return 1;
161 }
162
b66570dc 163 client->fd = fd;
81179eec 164 client->state = Client_connected;
132159a5
JA
165 return 0;
166}
167
cc0df00a
JA
168void fio_clients_terminate(void)
169{
170 struct flist_head *entry;
171 struct fio_client *client;
172
173 flist_for_each(entry, &client_list) {
174 client = flist_entry(entry, struct fio_client, list);
175
176 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_QUIT, 0);
177 }
178}
179
180static void sig_int(int sig)
181{
182 fio_clients_terminate();
183}
184
185static void client_signal_handler(void)
186{
187 struct sigaction act;
188
189 memset(&act, 0, sizeof(act));
190 act.sa_handler = sig_int;
191 act.sa_flags = SA_RESTART;
192 sigaction(SIGINT, &act, NULL);
193
194 memset(&act, 0, sizeof(act));
195 act.sa_handler = sig_int;
196 act.sa_flags = SA_RESTART;
197 sigaction(SIGTERM, &act, NULL);
198}
199
0b8f30a5
JA
200static void probe_client(struct fio_client *client)
201{
202 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_PROBE, 0);
203 handle_client(client, 1);
204}
205
81179eec
JA
206static int send_client_cmd_line(struct fio_client *client)
207{
208 struct cmd_line_pdu *pdu;
209 int i, ret;
210
211 pdu = malloc(sizeof(*pdu));
212 for (i = 0; i < client->argc; i++)
213 strcpy((char *) pdu->argv[i], client->argv[i]);
214
215 pdu->argc = cpu_to_le16(client->argc);
216 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOBLINE, pdu, sizeof(*pdu));
217 free(pdu);
218 return ret;
219}
220
a37f69b7
JA
221int fio_clients_connect(void)
222{
223 struct fio_client *client;
224 struct flist_head *entry, *tmp;
225 int ret;
226
cc0df00a
JA
227 client_signal_handler();
228
a37f69b7
JA
229 flist_for_each_safe(entry, tmp, &client_list) {
230 client = flist_entry(entry, struct fio_client, list);
231
232 ret = fio_client_connect(client);
0b8f30a5 233 if (ret) {
a37f69b7 234 remove_client(client);
0b8f30a5
JA
235 continue;
236 }
237
238 probe_client(client);
81179eec
JA
239
240 if (client->argc > 1)
241 send_client_cmd_line(client);
a37f69b7
JA
242 }
243
244 return !nr_clients;
245}
246
132159a5
JA
247/*
248 * Send file contents to server backend. We could use sendfile(), but to remain
249 * more portable lets just read/write the darn thing.
250 */
a37f69b7 251static int fio_client_send_ini(struct fio_client *client, const char *filename)
132159a5
JA
252{
253 struct stat sb;
254 char *p, *buf;
255 off_t len;
256 int fd, ret;
257
46c48f1f
JA
258 dprint(FD_NET, "send ini %s to %s\n", filename, client->hostname);
259
132159a5
JA
260 fd = open(filename, O_RDONLY);
261 if (fd < 0) {
262 log_err("fio: job file open: %s\n", strerror(errno));
263 return 1;
264 }
265
266 if (fstat(fd, &sb) < 0) {
267 log_err("fio: job file stat: %s\n", strerror(errno));
268 return 1;
269 }
270
271 buf = malloc(sb.st_size);
272
273 len = sb.st_size;
274 p = buf;
275 do {
276 ret = read(fd, p, len);
277 if (ret > 0) {
278 len -= ret;
279 if (!len)
280 break;
281 p += ret;
282 continue;
283 } else if (!ret)
284 break;
285 else if (errno == EAGAIN || errno == EINTR)
286 continue;
287 } while (1);
288
0b8f30a5
JA
289 if (len) {
290 log_err("fio: failed reading job file %s\n", filename);
291 return 1;
292 }
293
81179eec 294 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, buf, sb.st_size);
132159a5
JA
295 free(buf);
296 return ret;
297}
37db14fe 298
a37f69b7
JA
299int fio_clients_send_ini(const char *filename)
300{
301 struct fio_client *client;
302 struct flist_head *entry, *tmp;
303
304 flist_for_each_safe(entry, tmp, &client_list) {
305 client = flist_entry(entry, struct fio_client, list);
306
307 if (fio_client_send_ini(client, filename))
308 remove_client(client);
309 }
310
311 return !nr_clients;
312}
313
a64e88da
JA
314static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
315{
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);
319 /* FIXME */
ddcc0b69
JA
320 dst->mean = __le64_to_cpu(src->mean);
321 dst->S = __le64_to_cpu(src->S);
a64e88da
JA
322}
323
324static void convert_ts(struct thread_stat *dst, struct thread_stat *src)
325{
326 int i, j;
327
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);
332
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]);
338 }
339
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;
347
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]);
352 }
353
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]);
357 }
358
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]);
362
363 for (i = 0; i < 3; i++) {
364 dst->total_io_u[i] = le64_to_cpu(src->total_io_u[i]);
93eee04a 365 dst->short_io_u[i] = le64_to_cpu(src->short_io_u[i]);
a64e88da
JA
366 }
367
368 dst->total_submit = le64_to_cpu(src->total_submit);
369 dst->total_complete = le64_to_cpu(src->total_complete);
370
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]);
374 }
375
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);
ddcc0b69
JA
379 dst->first_error = le32_to_cpu(src->first_error);
380 dst->kb_base = le32_to_cpu(src->kb_base);
a64e88da
JA
381}
382
383static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
384{
385 int i;
386
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]);
394 }
395
396 dst->kb_base = le32_to_cpu(src->kb_base);
397 dst->groupid = le32_to_cpu(src->groupid);
398}
399
400static void handle_ts(struct fio_net_cmd *cmd)
401{
402 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
403
404 convert_ts(&p->ts, &p->ts);
405 convert_gs(&p->rs, &p->rs);
406
407 show_thread_status(&p->ts, &p->rs);
408}
409
410static void handle_gs(struct fio_net_cmd *cmd)
411{
412 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
413
414 convert_gs(gs, gs);
415 show_group_stats(gs);
416}
417
cf451d1e
JA
418static void handle_eta(struct fio_net_cmd *cmd)
419{
420 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
421 int i;
422
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);
431
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]);
435 }
436
437 je->elapsed_sec = le32_to_cpu(je->nr_running);
438 je->eta_sec = le64_to_cpu(je->eta_sec);
439
440 display_thread_status(je);
441}
442
2e03b4b2
JA
443static void handle_probe(struct fio_net_cmd *cmd)
444{
445 struct cmd_probe_pdu *probe = (struct cmd_probe_pdu *) cmd->payload;
446
0b8f30a5
JA
447 log_info("Probe: hostname=%s, fio ver %u.%u.%u\n", probe->hostname,
448 probe->fio_major, probe->fio_minor, probe->fio_patch);
2e03b4b2
JA
449}
450
0b8f30a5 451static int handle_client(struct fio_client *client, int one)
37db14fe
JA
452{
453 struct fio_net_cmd *cmd;
a450e492 454 int done = 0;
37db14fe 455
70e0c316 456 while ((cmd = fio_net_recv_cmd(client->fd, 1)) != NULL) {
46c48f1f
JA
457 dprint(FD_NET, "%s: got cmd op %d\n", client->hostname,
458 cmd->opcode);
459
a64e88da 460 switch (cmd->opcode) {
a64e88da 461 case FIO_NET_CMD_QUIT:
b66570dc 462 remove_client(client);
437377e1 463 free(cmd);
a450e492 464 done = 1;
437377e1 465 break;
a64e88da 466 case FIO_NET_CMD_TEXT:
0b8f30a5
JA
467 fprintf(f_out, "Client <%s>: ", client->hostname);
468 fwrite(cmd->payload, cmd->pdu_len, 1, f_out);
469 fflush(f_out);
37db14fe 470 free(cmd);
a64e88da
JA
471 break;
472 case FIO_NET_CMD_TS:
473 handle_ts(cmd);
474 free(cmd);
475 break;
476 case FIO_NET_CMD_GS:
477 handle_gs(cmd);
478 free(cmd);
479 break;
cf451d1e
JA
480 case FIO_NET_CMD_ETA:
481 handle_eta(cmd);
482 free(cmd);
483 break;
2e03b4b2
JA
484 case FIO_NET_CMD_PROBE:
485 handle_probe(cmd);
486 free(cmd);
487 break;
81179eec
JA
488 case FIO_NET_CMD_START:
489 client->state = Client_started;
490 free(cmd);
491 break;
492 case FIO_NET_CMD_STOP:
493 client->state = Client_stopped;
494 free(cmd);
495 break;
a64e88da
JA
496 default:
497 log_err("fio: unknown client op: %d\n", cmd->opcode);
498 free(cmd);
499 break;
37db14fe 500 }
a450e492 501
0b8f30a5 502 if (done || one)
a450e492 503 break;
37db14fe
JA
504 }
505
506 return 0;
507}
b66570dc
JA
508
509int fio_handle_clients(void)
510{
511 struct fio_client *client;
512 struct flist_head *entry;
513 struct pollfd *pfds;
82a4be1b 514 int i, ret = 0;
b66570dc
JA
515
516 pfds = malloc(nr_clients * sizeof(struct pollfd));
517
82a4be1b
JA
518 while (!exit_backend && nr_clients) {
519 i = 0;
520 flist_for_each(entry, &client_list) {
521 client = flist_entry(entry, struct fio_client, list);
b66570dc 522
82a4be1b
JA
523 pfds[i].fd = client->fd;
524 pfds[i].events = POLLIN;
525 i++;
526 }
527
528 assert(i == nr_clients);
b66570dc 529
5c2857f9
JA
530 do {
531 ret = poll(pfds, nr_clients, 100);
532 if (ret < 0) {
533 if (errno == EINTR)
534 continue;
535 log_err("fio: poll clients: %s\n", strerror(errno));
536 break;
537 } else if (!ret)
b66570dc 538 continue;
5c2857f9 539 } while (ret <= 0);
b66570dc
JA
540
541 for (i = 0; i < nr_clients; i++) {
542 if (!(pfds[i].revents & POLLIN))
543 continue;
544
545 client = find_client_by_fd(pfds[i].fd);
546 if (!client) {
547 log_err("fio: unknown client\n");
548 continue;
549 }
0b8f30a5 550 handle_client(client, 0);
b66570dc
JA
551 }
552 }
553
554 free(pfds);
b66570dc
JA
555 return 0;
556}