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