init: typo, remove -> remote.
[fio.git] / client.c
... / ...
CommitLineData
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/socket.h>
12#include <sys/un.h>
13#include <netinet/in.h>
14#include <arpa/inet.h>
15#include <netdb.h>
16#include <signal.h>
17
18#include "fio.h"
19#include "server.h"
20#include "flist.h"
21#include "hash.h"
22
23struct fio_client {
24 struct flist_head list;
25 struct flist_head hash_list;
26 struct sockaddr_in addr;
27 struct sockaddr_un addr_un;
28 char *hostname;
29 int port;
30 int fd;
31
32 char *name;
33
34 int state;
35
36 int skip_newline;
37 int is_sock;
38 int waiting_eta;
39
40 uint16_t argc;
41 char **argv;
42};
43
44static struct timeval eta_tv;
45
46enum {
47 Client_created = 0,
48 Client_connected = 1,
49 Client_started = 2,
50 Client_stopped = 3,
51 Client_exited = 4,
52};
53
54struct client_eta {
55 struct jobs_eta eta;
56 unsigned int pending;
57};
58
59static FLIST_HEAD(client_list);
60
61#define FIO_CLIENT_HASH_BITS 7
62#define FIO_CLIENT_HASH_SZ (1 << FIO_CLIENT_HASH_BITS)
63#define FIO_CLIENT_HASH_MASK (FIO_CLIENT_HASH_SZ - 1)
64static struct flist_head client_hash[FIO_CLIENT_HASH_SZ];
65
66static int handle_client(struct fio_client *client);
67
68static void fio_client_add_hash(struct fio_client *client)
69{
70 int bucket = hash_long(client->fd, FIO_CLIENT_HASH_BITS);
71
72 bucket &= FIO_CLIENT_HASH_MASK;
73 flist_add(&client->hash_list, &client_hash[bucket]);
74}
75
76static void fio_client_remove_hash(struct fio_client *client)
77{
78 if (!flist_empty(&client->hash_list))
79 flist_del_init(&client->hash_list);
80}
81
82static void fio_init fio_client_hash_init(void)
83{
84 int i;
85
86 for (i = 0; i < FIO_CLIENT_HASH_SZ; i++)
87 INIT_FLIST_HEAD(&client_hash[i]);
88}
89
90static struct fio_client *find_client_by_fd(int fd)
91{
92 int bucket = hash_long(fd, FIO_CLIENT_HASH_BITS) & FIO_CLIENT_HASH_MASK;
93 struct fio_client *client;
94 struct flist_head *entry;
95
96 flist_for_each(entry, &client_hash[bucket]) {
97 client = flist_entry(entry, struct fio_client, hash_list);
98
99 if (client->fd == fd)
100 return client;
101 }
102
103 return NULL;
104}
105
106static void remove_client(struct fio_client *client)
107{
108 dprint(FD_NET, "client: removed <%s>\n", client->hostname);
109 flist_del(&client->list);
110
111 fio_client_remove_hash(client);
112
113 /* FIXME: check ->waiting_eta and handle it */
114
115 free(client->hostname);
116 if (client->argv)
117 free(client->argv);
118 if (client->name)
119 free(client->name);
120
121 free(client);
122 nr_clients--;
123}
124
125static void __fio_client_add_cmd_option(struct fio_client *client,
126 const char *opt)
127{
128 int index;
129
130 index = client->argc++;
131 client->argv = realloc(client->argv, sizeof(char *) * client->argc);
132 client->argv[index] = strdup(opt);
133 dprint(FD_NET, "client: add cmd %d: %s\n", index, opt);
134}
135
136void fio_client_add_cmd_option(void *cookie, const char *opt)
137{
138 struct fio_client *client = cookie;
139
140 if (!client || !opt)
141 return;
142
143 __fio_client_add_cmd_option(client, opt);
144}
145
146int fio_client_add(const char *hostname, void **cookie)
147{
148 struct fio_client *client;
149
150 client = malloc(sizeof(*client));
151 memset(client, 0, sizeof(*client));
152
153 INIT_FLIST_HEAD(&client->list);
154 INIT_FLIST_HEAD(&client->hash_list);
155
156 if (fio_server_parse_string(hostname, &client->hostname,
157 &client->is_sock, &client->port,
158 &client->addr.sin_addr))
159 return -1;
160
161 client->fd = -1;
162
163 __fio_client_add_cmd_option(client, "fio");
164
165 flist_add(&client->list, &client_list);
166 nr_clients++;
167 dprint(FD_NET, "client: added <%s>\n", client->hostname);
168 *cookie = client;
169 return 0;
170}
171
172static int fio_client_connect_ip(struct fio_client *client)
173{
174 int fd;
175
176 client->addr.sin_family = AF_INET;
177 client->addr.sin_port = htons(client->port);
178
179 fd = socket(AF_INET, SOCK_STREAM, 0);
180 if (fd < 0) {
181 log_err("fio: socket: %s\n", strerror(errno));
182 return -1;
183 }
184
185 if (connect(fd, (struct sockaddr *) &client->addr, sizeof(client->addr)) < 0) {
186 log_err("fio: connect: %s\n", strerror(errno));
187 log_err("fio: failed to connect to %s:%u\n", client->hostname,
188 client->port);
189 close(fd);
190 return -1;
191 }
192
193 return fd;
194}
195
196static int fio_client_connect_sock(struct fio_client *client)
197{
198 struct sockaddr_un *addr = &client->addr_un;
199 fio_socklen_t len;
200 int fd;
201
202 memset(addr, 0, sizeof(*addr));
203 addr->sun_family = AF_UNIX;
204 strcpy(addr->sun_path, client->hostname);
205
206 fd = socket(AF_UNIX, SOCK_STREAM, 0);
207 if (fd < 0) {
208 log_err("fio: socket: %s\n", strerror(errno));
209 return -1;
210 }
211
212 len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
213 if (connect(fd, (struct sockaddr *) addr, len) < 0) {
214 log_err("fio: connect; %s\n", strerror(errno));
215 close(fd);
216 return -1;
217 }
218
219 return fd;
220}
221
222static int fio_client_connect(struct fio_client *client)
223{
224 int fd;
225
226 dprint(FD_NET, "client: connect to host %s\n", client->hostname);
227
228 if (client->is_sock)
229 fd = fio_client_connect_sock(client);
230 else
231 fd = fio_client_connect_ip(client);
232
233 if (fd < 0)
234 return 1;
235
236 client->fd = fd;
237 fio_client_add_hash(client);
238 client->state = Client_connected;
239 return 0;
240}
241
242void fio_clients_terminate(void)
243{
244 struct flist_head *entry;
245 struct fio_client *client;
246
247 dprint(FD_NET, "client: terminate clients\n");
248
249 flist_for_each(entry, &client_list) {
250 client = flist_entry(entry, struct fio_client, list);
251
252 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_QUIT, 0);
253 }
254}
255
256static void sig_int(int sig)
257{
258 dprint(FD_NET, "client: got signal %d\n", sig);
259 fio_clients_terminate();
260}
261
262static void client_signal_handler(void)
263{
264 struct sigaction act;
265
266 memset(&act, 0, sizeof(act));
267 act.sa_handler = sig_int;
268 act.sa_flags = SA_RESTART;
269 sigaction(SIGINT, &act, NULL);
270
271 memset(&act, 0, sizeof(act));
272 act.sa_handler = sig_int;
273 act.sa_flags = SA_RESTART;
274 sigaction(SIGTERM, &act, NULL);
275}
276
277static void probe_client(struct fio_client *client)
278{
279 dprint(FD_NET, "client: send probe\n");
280
281 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_PROBE, 0);
282 handle_client(client);
283}
284
285static int send_client_cmd_line(struct fio_client *client)
286{
287 struct cmd_single_line_pdu *cslp;
288 struct cmd_line_pdu *clp;
289 unsigned long offset;
290 void *pdu;
291 size_t mem;
292 int i, ret;
293
294 dprint(FD_NET, "client: send cmdline %d\n", client->argc);
295
296 /*
297 * Find out how much mem we need
298 */
299 for (i = 0, mem = 0; i < client->argc; i++)
300 mem += strlen(client->argv[i]) + 1;
301
302 /*
303 * We need one cmd_line_pdu, and argc number of cmd_single_line_pdu
304 */
305 mem += sizeof(*clp) + (client->argc * sizeof(*cslp));
306
307 pdu = malloc(mem);
308 clp = pdu;
309 offset = sizeof(*clp);
310
311 for (i = 0; i < client->argc; i++) {
312 uint16_t arg_len = strlen(client->argv[i]) + 1;
313
314 cslp = pdu + offset;
315 strcpy((char *) cslp->text, client->argv[i]);
316 cslp->len = cpu_to_le16(arg_len);
317 offset += sizeof(*cslp) + arg_len;
318 }
319
320 clp->lines = cpu_to_le16(client->argc);
321 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOBLINE, pdu, mem, 0);
322 free(pdu);
323 return ret;
324}
325
326int fio_clients_connect(void)
327{
328 struct fio_client *client;
329 struct flist_head *entry, *tmp;
330 int ret;
331
332 dprint(FD_NET, "client: connect all\n");
333
334 client_signal_handler();
335
336 flist_for_each_safe(entry, tmp, &client_list) {
337 client = flist_entry(entry, struct fio_client, list);
338
339 ret = fio_client_connect(client);
340 if (ret) {
341 remove_client(client);
342 continue;
343 }
344
345 probe_client(client);
346
347 if (client->argc > 1)
348 send_client_cmd_line(client);
349 }
350
351 return !nr_clients;
352}
353
354/*
355 * Send file contents to server backend. We could use sendfile(), but to remain
356 * more portable lets just read/write the darn thing.
357 */
358static int fio_client_send_ini(struct fio_client *client, const char *filename)
359{
360 struct stat sb;
361 char *p, *buf;
362 off_t len;
363 int fd, ret;
364
365 dprint(FD_NET, "send ini %s to %s\n", filename, client->hostname);
366
367 fd = open(filename, O_RDONLY);
368 if (fd < 0) {
369 log_err("fio: job file <%s> open: %s\n", filename, strerror(errno));
370 return 1;
371 }
372
373 if (fstat(fd, &sb) < 0) {
374 log_err("fio: job file stat: %s\n", strerror(errno));
375 close(fd);
376 return 1;
377 }
378
379 buf = malloc(sb.st_size);
380
381 len = sb.st_size;
382 p = buf;
383 do {
384 ret = read(fd, p, len);
385 if (ret > 0) {
386 len -= ret;
387 if (!len)
388 break;
389 p += ret;
390 continue;
391 } else if (!ret)
392 break;
393 else if (errno == EAGAIN || errno == EINTR)
394 continue;
395 } while (1);
396
397 if (len) {
398 log_err("fio: failed reading job file %s\n", filename);
399 close(fd);
400 free(buf);
401 return 1;
402 }
403
404 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, buf, sb.st_size, 0);
405 free(buf);
406 close(fd);
407 return ret;
408}
409
410int fio_clients_send_ini(const char *filename)
411{
412 struct fio_client *client;
413 struct flist_head *entry, *tmp;
414
415 flist_for_each_safe(entry, tmp, &client_list) {
416 client = flist_entry(entry, struct fio_client, list);
417
418 if (fio_client_send_ini(client, filename))
419 remove_client(client);
420 }
421
422 return !nr_clients;
423}
424
425static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
426{
427 dst->max_val = le64_to_cpu(src->max_val);
428 dst->min_val = le64_to_cpu(src->min_val);
429 dst->samples = le64_to_cpu(src->samples);
430
431 /*
432 * Floats arrive as IEEE 754 encoded uint64_t, convert back to double
433 */
434 dst->mean.u.f = fio_uint64_to_double(le64_to_cpu(dst->mean.u.i));
435 dst->S.u.f = fio_uint64_to_double(le64_to_cpu(dst->S.u.i));
436}
437
438static void convert_ts(struct thread_stat *dst, struct thread_stat *src)
439{
440 int i, j;
441
442 dst->error = le32_to_cpu(src->error);
443 dst->groupid = le32_to_cpu(src->groupid);
444 dst->pid = le32_to_cpu(src->pid);
445 dst->members = le32_to_cpu(src->members);
446
447 for (i = 0; i < 2; i++) {
448 convert_io_stat(&dst->clat_stat[i], &src->clat_stat[i]);
449 convert_io_stat(&dst->slat_stat[i], &src->slat_stat[i]);
450 convert_io_stat(&dst->lat_stat[i], &src->lat_stat[i]);
451 convert_io_stat(&dst->bw_stat[i], &src->bw_stat[i]);
452 }
453
454 dst->usr_time = le64_to_cpu(src->usr_time);
455 dst->sys_time = le64_to_cpu(src->sys_time);
456 dst->ctx = le64_to_cpu(src->ctx);
457 dst->minf = le64_to_cpu(src->minf);
458 dst->majf = le64_to_cpu(src->majf);
459 dst->clat_percentiles = le64_to_cpu(src->clat_percentiles);
460
461 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
462 fio_fp64_t *fps = &src->percentile_list[i];
463 fio_fp64_t *fpd = &dst->percentile_list[i];
464
465 fpd->u.f = fio_uint64_to_double(le64_to_cpu(fps->u.i));
466 }
467
468 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
469 dst->io_u_map[i] = le32_to_cpu(src->io_u_map[i]);
470 dst->io_u_submit[i] = le32_to_cpu(src->io_u_submit[i]);
471 dst->io_u_complete[i] = le32_to_cpu(src->io_u_complete[i]);
472 }
473
474 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
475 dst->io_u_lat_u[i] = le32_to_cpu(src->io_u_lat_u[i]);
476 dst->io_u_lat_m[i] = le32_to_cpu(src->io_u_lat_m[i]);
477 }
478
479 for (i = 0; i < 2; i++)
480 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
481 dst->io_u_plat[i][j] = le32_to_cpu(src->io_u_plat[i][j]);
482
483 for (i = 0; i < 3; i++) {
484 dst->total_io_u[i] = le64_to_cpu(src->total_io_u[i]);
485 dst->short_io_u[i] = le64_to_cpu(src->short_io_u[i]);
486 }
487
488 dst->total_submit = le64_to_cpu(src->total_submit);
489 dst->total_complete = le64_to_cpu(src->total_complete);
490
491 for (i = 0; i < 2; i++) {
492 dst->io_bytes[i] = le64_to_cpu(src->io_bytes[i]);
493 dst->runtime[i] = le64_to_cpu(src->runtime[i]);
494 }
495
496 dst->total_run_time = le64_to_cpu(src->total_run_time);
497 dst->continue_on_error = le16_to_cpu(src->continue_on_error);
498 dst->total_err_count = le64_to_cpu(src->total_err_count);
499 dst->first_error = le32_to_cpu(src->first_error);
500 dst->kb_base = le32_to_cpu(src->kb_base);
501}
502
503static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
504{
505 int i;
506
507 for (i = 0; i < 2; i++) {
508 dst->max_run[i] = le64_to_cpu(src->max_run[i]);
509 dst->min_run[i] = le64_to_cpu(src->min_run[i]);
510 dst->max_bw[i] = le64_to_cpu(src->max_bw[i]);
511 dst->min_bw[i] = le64_to_cpu(src->min_bw[i]);
512 dst->io_kb[i] = le64_to_cpu(src->io_kb[i]);
513 dst->agg[i] = le64_to_cpu(src->agg[i]);
514 }
515
516 dst->kb_base = le32_to_cpu(src->kb_base);
517 dst->groupid = le32_to_cpu(src->groupid);
518}
519
520static void handle_ts(struct fio_net_cmd *cmd)
521{
522 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
523
524 convert_ts(&p->ts, &p->ts);
525 convert_gs(&p->rs, &p->rs);
526
527 show_thread_status(&p->ts, &p->rs);
528}
529
530static void handle_gs(struct fio_net_cmd *cmd)
531{
532 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
533
534 convert_gs(gs, gs);
535 show_group_stats(gs);
536}
537
538static void convert_jobs_eta(struct jobs_eta *je)
539{
540 int i;
541
542 je->nr_running = le32_to_cpu(je->nr_running);
543 je->nr_ramp = le32_to_cpu(je->nr_ramp);
544 je->nr_pending = le32_to_cpu(je->nr_pending);
545 je->files_open = le32_to_cpu(je->files_open);
546 je->m_rate = le32_to_cpu(je->m_rate);
547 je->t_rate = le32_to_cpu(je->t_rate);
548 je->m_iops = le32_to_cpu(je->m_iops);
549 je->t_iops = le32_to_cpu(je->t_iops);
550
551 for (i = 0; i < 2; i++) {
552 je->rate[i] = le32_to_cpu(je->rate[i]);
553 je->iops[i] = le32_to_cpu(je->iops[i]);
554 }
555
556 je->elapsed_sec = le64_to_cpu(je->elapsed_sec);
557 je->eta_sec = le64_to_cpu(je->eta_sec);
558}
559
560static void sum_jobs_eta(struct jobs_eta *dst, struct jobs_eta *je)
561{
562 int i;
563
564 dst->nr_running += je->nr_running;
565 dst->nr_ramp += je->nr_ramp;
566 dst->nr_pending += je->nr_pending;
567 dst->files_open += je->files_open;
568 dst->m_rate += je->m_rate;
569 dst->t_rate += je->t_rate;
570 dst->m_iops += je->m_iops;
571 dst->t_iops += je->t_iops;
572
573 for (i = 0; i < 2; i++) {
574 dst->rate[i] += je->rate[i];
575 dst->iops[i] += je->iops[i];
576 }
577
578 dst->elapsed_sec += je->elapsed_sec;
579
580 if (je->eta_sec > dst->eta_sec)
581 dst->eta_sec = je->eta_sec;
582}
583
584static void handle_eta(struct fio_net_cmd *cmd)
585{
586 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
587 struct client_eta *eta = (struct client_eta *) cmd->tag;
588
589 dprint(FD_NET, "client: got eta tag %p, %d\n", eta, eta->pending);
590
591 convert_jobs_eta(je);
592 sum_jobs_eta(&eta->eta, je);
593
594 if (!--eta->pending) {
595 display_thread_status(&eta->eta);
596 free(eta);
597 }
598}
599
600static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd)
601{
602 struct cmd_probe_pdu *probe = (struct cmd_probe_pdu *) cmd->payload;
603 const char *os, *arch;
604
605 os = fio_get_os_string(probe->os);
606 if (!os)
607 os = "unknown";
608
609 arch = fio_get_arch_string(probe->arch);
610 if (!arch)
611 os = "unknown";
612
613 log_info("hostname=%s, be=%u, os=%s, arch=%s, fio=%u.%u.%u\n",
614 probe->hostname, probe->bigendian, os, arch, probe->fio_major,
615 probe->fio_minor, probe->fio_patch);
616
617 if (!client->name)
618 client->name = strdup((char *) probe->hostname);
619}
620
621static int handle_client(struct fio_client *client)
622{
623 struct fio_net_cmd *cmd;
624
625 dprint(FD_NET, "client: handle %s\n", client->hostname);
626
627 cmd = fio_net_recv_cmd(client->fd);
628 if (!cmd)
629 return 0;
630
631 dprint(FD_NET, "client: got cmd op %d from %s\n",
632 cmd->opcode, client->hostname);
633
634 switch (cmd->opcode) {
635 case FIO_NET_CMD_QUIT:
636 remove_client(client);
637 free(cmd);
638 break;
639 case FIO_NET_CMD_TEXT: {
640 const char *buf = (const char *) cmd->payload;
641 const char *name;
642 int fio_unused ret;
643
644 name = client->name ? client->name : client->hostname;
645
646 if (!client->skip_newline)
647 fprintf(f_out, "<%s> ", name);
648 ret = fwrite(buf, cmd->pdu_len, 1, f_out);
649 fflush(f_out);
650 client->skip_newline = strchr(buf, '\n') == NULL;
651 free(cmd);
652 break;
653 }
654 case FIO_NET_CMD_TS:
655 handle_ts(cmd);
656 free(cmd);
657 break;
658 case FIO_NET_CMD_GS:
659 handle_gs(cmd);
660 free(cmd);
661 break;
662 case FIO_NET_CMD_ETA:
663 handle_eta(cmd);
664 free(cmd);
665 break;
666 case FIO_NET_CMD_PROBE:
667 handle_probe(client, cmd);
668 free(cmd);
669 break;
670 case FIO_NET_CMD_START:
671 client->state = Client_started;
672 free(cmd);
673 break;
674 case FIO_NET_CMD_STOP:
675 client->state = Client_stopped;
676 free(cmd);
677 break;
678 default:
679 log_err("fio: unknown client op: %d\n", cmd->opcode);
680 free(cmd);
681 break;
682 }
683
684 return 1;
685}
686
687static void request_client_etas(void)
688{
689 struct fio_client *client;
690 struct flist_head *entry;
691 struct client_eta *eta;
692
693 dprint(FD_NET, "client: request eta (%d)\n", nr_clients);
694
695 /*
696 * We need to do something more clever about checking status
697 * of command being send, client haven't sent previous ETA
698 * already, etc.
699 */
700
701 eta = malloc(sizeof(*eta));
702 memset(&eta->eta, 0, sizeof(eta->eta));
703 eta->pending = nr_clients;
704
705 flist_for_each(entry, &client_list) {
706 client = flist_entry(entry, struct fio_client, list);
707
708 client->waiting_eta = 1;
709 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_SEND_ETA,
710 (uint64_t) eta);
711 }
712
713 dprint(FD_NET, "client: requested eta tag %p\n", eta);
714}
715
716int fio_handle_clients(void)
717{
718 struct fio_client *client;
719 struct flist_head *entry;
720 struct pollfd *pfds;
721 int i, ret = 0;
722
723 gettimeofday(&eta_tv, NULL);
724
725 pfds = malloc(nr_clients * sizeof(struct pollfd));
726
727 while (!exit_backend && nr_clients) {
728 i = 0;
729 flist_for_each(entry, &client_list) {
730 client = flist_entry(entry, struct fio_client, list);
731
732 pfds[i].fd = client->fd;
733 pfds[i].events = POLLIN;
734 i++;
735 }
736
737 assert(i == nr_clients);
738
739 do {
740 struct timeval tv;
741
742 gettimeofday(&tv, NULL);
743 if (mtime_since(&eta_tv, &tv) >= 900) {
744 request_client_etas();
745 memcpy(&eta_tv, &tv, sizeof(tv));
746 }
747
748 ret = poll(pfds, nr_clients, 100);
749 if (ret < 0) {
750 if (errno == EINTR)
751 continue;
752 log_err("fio: poll clients: %s\n", strerror(errno));
753 break;
754 } else if (!ret)
755 continue;
756 } while (ret <= 0);
757
758 for (i = 0; i < nr_clients; i++) {
759 if (!(pfds[i].revents & POLLIN))
760 continue;
761
762 client = find_client_by_fd(pfds[i].fd);
763 if (!client) {
764 log_err("fio: unknown client fd %d\n", pfds[i].fd);
765 continue;
766 }
767 if (!handle_client(client)) {
768 log_info("client: host=%s disconnected\n",
769 client->hostname);
770 remove_client(client);
771 }
772 }
773 }
774
775 free(pfds);
776 return 0;
777}