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