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