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