gfio: debug dump on iolog receive
[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 #include <zlib.h>
18
19 #include "fio.h"
20 #include "client.h"
21 #include "server.h"
22 #include "flist.h"
23 #include "hash.h"
24
25 static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd);
26 static void handle_ts(struct fio_client *client, struct fio_net_cmd *cmd);
27 static void handle_gs(struct fio_client *client, struct fio_net_cmd *cmd);
28 static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd);
29 static void handle_text(struct fio_client *client, struct fio_net_cmd *cmd);
30 static void handle_stop(struct fio_client *client, struct fio_net_cmd *cmd);
31 static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd);
32
33 struct client_ops fio_client_ops = {
34         .text           = handle_text,
35         .disk_util      = handle_du,
36         .thread_status  = handle_ts,
37         .group_stats    = handle_gs,
38         .stop           = handle_stop,
39         .start          = handle_start,
40         .eta            = display_thread_status,
41         .probe          = handle_probe,
42         .eta_msec       = FIO_CLIENT_DEF_ETA_MSEC,
43         .client_type    = FIO_CLIENT_TYPE_CLI,
44 };
45
46 static struct timeval eta_tv;
47
48 static FLIST_HEAD(client_list);
49 static FLIST_HEAD(eta_list);
50
51 static FLIST_HEAD(arg_list);
52
53 struct thread_stat client_ts;
54 struct group_run_stats client_gs;
55 int sum_stat_clients;
56
57 static int sum_stat_nr;
58
59 #define FIO_CLIENT_HASH_BITS    7
60 #define FIO_CLIENT_HASH_SZ      (1 << FIO_CLIENT_HASH_BITS)
61 #define FIO_CLIENT_HASH_MASK    (FIO_CLIENT_HASH_SZ - 1)
62 static struct flist_head client_hash[FIO_CLIENT_HASH_SZ];
63
64 static void fio_client_add_hash(struct fio_client *client)
65 {
66         int bucket = hash_long(client->fd, FIO_CLIENT_HASH_BITS);
67
68         bucket &= FIO_CLIENT_HASH_MASK;
69         flist_add(&client->hash_list, &client_hash[bucket]);
70 }
71
72 static void fio_client_remove_hash(struct fio_client *client)
73 {
74         if (!flist_empty(&client->hash_list))
75                 flist_del_init(&client->hash_list);
76 }
77
78 static void fio_init fio_client_hash_init(void)
79 {
80         int i;
81
82         for (i = 0; i < FIO_CLIENT_HASH_SZ; i++)
83                 INIT_FLIST_HEAD(&client_hash[i]);
84 }
85
86 static struct fio_client *find_client_by_fd(int fd)
87 {
88         int bucket = hash_long(fd, FIO_CLIENT_HASH_BITS) & FIO_CLIENT_HASH_MASK;
89         struct fio_client *client;
90         struct flist_head *entry;
91
92         flist_for_each(entry, &client_hash[bucket]) {
93                 client = flist_entry(entry, struct fio_client, hash_list);
94
95                 if (client->fd == fd) {
96                         client->refs++;
97                         return client;
98                 }
99         }
100
101         return NULL;
102 }
103
104 void fio_put_client(struct fio_client *client)
105 {
106         if (--client->refs)
107                 return;
108
109         free(client->hostname);
110         if (client->argv)
111                 free(client->argv);
112         if (client->name)
113                 free(client->name);
114
115         free(client);
116 }
117
118 static void remove_client(struct fio_client *client)
119 {
120         assert(client->refs);
121
122         dprint(FD_NET, "client: removed <%s>\n", client->hostname);
123
124         if (!flist_empty(&client->list))
125                 flist_del_init(&client->list);
126
127         fio_client_remove_hash(client);
128
129         if (!flist_empty(&client->eta_list)) {
130                 flist_del_init(&client->eta_list);
131                 fio_client_dec_jobs_eta(client->eta_in_flight, client->ops->eta);
132         }
133
134         nr_clients--;
135         sum_stat_clients--;
136
137         fio_put_client(client);
138 }
139
140 struct fio_client *fio_get_client(struct fio_client *client)
141 {
142         client->refs++;
143         return client;
144 }
145
146 static void __fio_client_add_cmd_option(struct fio_client *client,
147                                         const char *opt)
148 {
149         int index;
150
151         index = client->argc++;
152         client->argv = realloc(client->argv, sizeof(char *) * client->argc);
153         client->argv[index] = strdup(opt);
154         dprint(FD_NET, "client: add cmd %d: %s\n", index, opt);
155 }
156
157 void fio_client_add_cmd_option(void *cookie, const char *opt)
158 {
159         struct fio_client *client = cookie;
160         struct flist_head *entry;
161
162         if (!client || !opt)
163                 return;
164
165         __fio_client_add_cmd_option(client, opt);
166
167         /*
168          * Duplicate arguments to shared client group
169          */
170         flist_for_each(entry, &arg_list) {
171                 client = flist_entry(entry, struct fio_client, arg_list);
172
173                 __fio_client_add_cmd_option(client, opt);
174         }
175 }
176
177 struct fio_client *fio_client_add_explicit(struct client_ops *ops,
178                                            const char *hostname, int type,
179                                            int port)
180 {
181         struct fio_client *client;
182
183         client = malloc(sizeof(*client));
184         memset(client, 0, sizeof(*client));
185
186         INIT_FLIST_HEAD(&client->list);
187         INIT_FLIST_HEAD(&client->hash_list);
188         INIT_FLIST_HEAD(&client->arg_list);
189         INIT_FLIST_HEAD(&client->eta_list);
190         INIT_FLIST_HEAD(&client->cmd_list);
191
192         client->hostname = strdup(hostname);
193
194         if (type == Fio_client_socket)
195                 client->is_sock = 1;
196         else {
197                 int ipv6;
198
199                 ipv6 = type == Fio_client_ipv6;
200                 if (fio_server_parse_host(hostname, &ipv6,
201                                                 &client->addr.sin_addr,
202                                                 &client->addr6.sin6_addr))
203                         goto err;
204
205                 client->port = port;
206         }
207
208         client->fd = -1;
209         client->ops = ops;
210         client->refs = 1;
211         client->type = ops->client_type;
212
213         __fio_client_add_cmd_option(client, "fio");
214
215         flist_add(&client->list, &client_list);
216         nr_clients++;
217         dprint(FD_NET, "client: added <%s>\n", client->hostname);
218         return client;
219 err:
220         free(client);
221         return NULL;
222 }
223
224 int fio_client_add(struct client_ops *ops, const char *hostname, void **cookie)
225 {
226         struct fio_client *existing = *cookie;
227         struct fio_client *client;
228
229         if (existing) {
230                 /*
231                  * We always add our "exec" name as the option, hence 1
232                  * means empty.
233                  */
234                 if (existing->argc == 1)
235                         flist_add_tail(&existing->arg_list, &arg_list);
236                 else {
237                         while (!flist_empty(&arg_list))
238                                 flist_del_init(arg_list.next);
239                 }
240         }
241
242         client = malloc(sizeof(*client));
243         memset(client, 0, sizeof(*client));
244
245         INIT_FLIST_HEAD(&client->list);
246         INIT_FLIST_HEAD(&client->hash_list);
247         INIT_FLIST_HEAD(&client->arg_list);
248         INIT_FLIST_HEAD(&client->eta_list);
249         INIT_FLIST_HEAD(&client->cmd_list);
250
251         if (fio_server_parse_string(hostname, &client->hostname,
252                                         &client->is_sock, &client->port,
253                                         &client->addr.sin_addr,
254                                         &client->addr6.sin6_addr,
255                                         &client->ipv6))
256                 return -1;
257
258         client->fd = -1;
259         client->ops = ops;
260         client->refs = 1;
261         client->type = ops->client_type;
262
263         __fio_client_add_cmd_option(client, "fio");
264
265         flist_add(&client->list, &client_list);
266         nr_clients++;
267         dprint(FD_NET, "client: added <%s>\n", client->hostname);
268         *cookie = client;
269         return 0;
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, &client->cmd_list);
277 }
278
279 static int fio_client_connect_ip(struct fio_client *client)
280 {
281         struct sockaddr *addr;
282         fio_socklen_t socklen;
283         int fd, domain;
284
285         if (client->ipv6) {
286                 client->addr6.sin6_family = AF_INET6;
287                 client->addr6.sin6_port = htons(client->port);
288                 domain = AF_INET6;
289                 addr = (struct sockaddr *) &client->addr6;
290                 socklen = sizeof(client->addr6);
291         } else {
292                 client->addr.sin_family = AF_INET;
293                 client->addr.sin_port = htons(client->port);
294                 domain = AF_INET;
295                 addr = (struct sockaddr *) &client->addr;
296                 socklen = sizeof(client->addr);
297         }
298
299         fd = socket(domain, SOCK_STREAM, 0);
300         if (fd < 0) {
301                 int ret = -errno;
302
303                 log_err("fio: socket: %s\n", strerror(errno));
304                 return ret;
305         }
306
307         if (connect(fd, addr, socklen) < 0) {
308                 int ret = -errno;
309
310                 log_err("fio: connect: %s\n", strerror(errno));
311                 log_err("fio: failed to connect to %s:%u\n", client->hostname,
312                                                                 client->port);
313                 close(fd);
314                 return ret;
315         }
316
317         return fd;
318 }
319
320 static int fio_client_connect_sock(struct fio_client *client)
321 {
322         struct sockaddr_un *addr = &client->addr_un;
323         fio_socklen_t len;
324         int fd;
325
326         memset(addr, 0, sizeof(*addr));
327         addr->sun_family = AF_UNIX;
328         strcpy(addr->sun_path, client->hostname);
329
330         fd = socket(AF_UNIX, SOCK_STREAM, 0);
331         if (fd < 0) {
332                 int ret = -errno;
333
334                 log_err("fio: socket: %s\n", strerror(errno));
335                 return ret;
336         }
337
338         len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
339         if (connect(fd, (struct sockaddr *) addr, len) < 0) {
340                 int ret = -errno;
341
342                 log_err("fio: connect; %s\n", strerror(errno));
343                 close(fd);
344                 return ret;
345         }
346
347         return fd;
348 }
349
350 int fio_client_connect(struct fio_client *client)
351 {
352         int fd;
353
354         dprint(FD_NET, "client: connect to host %s\n", client->hostname);
355
356         if (client->is_sock)
357                 fd = fio_client_connect_sock(client);
358         else
359                 fd = fio_client_connect_ip(client);
360
361         dprint(FD_NET, "client: %s connected %d\n", client->hostname, fd);
362
363         if (fd < 0)
364                 return fd;
365
366         client->fd = fd;
367         fio_client_add_hash(client);
368         client->state = Client_connected;
369
370         probe_client(client);
371         return 0;
372 }
373
374 void fio_client_terminate(struct fio_client *client)
375 {
376         fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_QUIT, 0, NULL);
377 }
378
379 void fio_clients_terminate(void)
380 {
381         struct flist_head *entry;
382         struct fio_client *client;
383
384         dprint(FD_NET, "client: terminate clients\n");
385
386         flist_for_each(entry, &client_list) {
387                 client = flist_entry(entry, struct fio_client, list);
388                 fio_client_terminate(client);
389         }
390 }
391
392 static void sig_int(int sig)
393 {
394         dprint(FD_NET, "client: got signal %d\n", sig);
395         fio_clients_terminate();
396 }
397
398 static void client_signal_handler(void)
399 {
400         struct sigaction act;
401
402         memset(&act, 0, sizeof(act));
403         act.sa_handler = sig_int;
404         act.sa_flags = SA_RESTART;
405         sigaction(SIGINT, &act, NULL);
406
407         memset(&act, 0, sizeof(act));
408         act.sa_handler = sig_int;
409         act.sa_flags = SA_RESTART;
410         sigaction(SIGTERM, &act, NULL);
411 }
412
413 static int send_client_cmd_line(struct fio_client *client)
414 {
415         struct cmd_single_line_pdu *cslp;
416         struct cmd_line_pdu *clp;
417         unsigned long offset;
418         unsigned int *lens;
419         void *pdu;
420         size_t mem;
421         int i, ret;
422
423         dprint(FD_NET, "client: send cmdline %d\n", client->argc);
424
425         lens = malloc(client->argc * sizeof(unsigned int));
426
427         /*
428          * Find out how much mem we need
429          */
430         for (i = 0, mem = 0; i < client->argc; i++) {
431                 lens[i] = strlen(client->argv[i]) + 1;
432                 mem += lens[i];
433         }
434
435         /*
436          * We need one cmd_line_pdu, and argc number of cmd_single_line_pdu
437          */
438         mem += sizeof(*clp) + (client->argc * sizeof(*cslp));
439
440         pdu = malloc(mem);
441         clp = pdu;
442         offset = sizeof(*clp);
443
444         for (i = 0; i < client->argc; i++) {
445                 uint16_t arg_len = lens[i];
446
447                 cslp = pdu + offset;
448                 strcpy((char *) cslp->text, client->argv[i]);
449                 cslp->len = cpu_to_le16(arg_len);
450                 offset += sizeof(*cslp) + arg_len;
451         }
452
453         free(lens);
454         clp->lines = cpu_to_le16(client->argc);
455         clp->client_type = __cpu_to_le16(client->type);
456         ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOBLINE, pdu, mem, 0);
457         free(pdu);
458         return ret;
459 }
460
461 int fio_clients_connect(void)
462 {
463         struct fio_client *client;
464         struct flist_head *entry, *tmp;
465         int ret;
466
467 #ifdef WIN32
468         WSADATA wsd;
469         WSAStartup(MAKEWORD(2,2), &wsd);
470 #endif
471
472         dprint(FD_NET, "client: connect all\n");
473
474         client_signal_handler();
475
476         flist_for_each_safe(entry, tmp, &client_list) {
477                 client = flist_entry(entry, struct fio_client, list);
478
479                 ret = fio_client_connect(client);
480                 if (ret) {
481                         remove_client(client);
482                         continue;
483                 }
484
485                 if (client->argc > 1)
486                         send_client_cmd_line(client);
487         }
488
489         return !nr_clients;
490 }
491
492 int fio_start_client(struct fio_client *client)
493 {
494         dprint(FD_NET, "client: start %s\n", client->hostname);
495         return fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_RUN, 0, NULL);
496 }
497
498 int fio_start_all_clients(void)
499 {
500         struct fio_client *client;
501         struct flist_head *entry, *tmp;
502         int ret;
503
504         dprint(FD_NET, "client: start all\n");
505
506         flist_for_each_safe(entry, tmp, &client_list) {
507                 client = flist_entry(entry, struct fio_client, list);
508
509                 ret = fio_start_client(client);
510                 if (ret) {
511                         remove_client(client);
512                         continue;
513                 }
514         }
515
516         return flist_empty(&client_list);
517 }
518
519 /*
520  * Send file contents to server backend. We could use sendfile(), but to remain
521  * more portable lets just read/write the darn thing.
522  */
523 static int __fio_client_send_ini(struct fio_client *client, const char *filename)
524 {
525         struct cmd_job_pdu *pdu;
526         size_t p_size;
527         struct stat sb;
528         char *p;
529         void *buf;
530         off_t len;
531         int fd, ret;
532
533         dprint(FD_NET, "send ini %s to %s\n", filename, client->hostname);
534
535         fd = open(filename, O_RDONLY);
536         if (fd < 0) {
537                 int ret = -errno;
538
539                 log_err("fio: job file <%s> open: %s\n", filename, strerror(errno));
540                 return ret;
541         }
542
543         if (fstat(fd, &sb) < 0) {
544                 int ret = -errno;
545
546                 log_err("fio: job file stat: %s\n", strerror(errno));
547                 close(fd);
548                 return ret;
549         }
550
551         p_size = sb.st_size + sizeof(*pdu);
552         pdu = malloc(p_size);
553         buf = pdu->buf;
554
555         len = sb.st_size;
556         p = buf;
557         do {
558                 ret = read(fd, p, len);
559                 if (ret > 0) {
560                         len -= ret;
561                         if (!len)
562                                 break;
563                         p += ret;
564                         continue;
565                 } else if (!ret)
566                         break;
567                 else if (errno == EAGAIN || errno == EINTR)
568                         continue;
569         } while (1);
570
571         if (len) {
572                 log_err("fio: failed reading job file %s\n", filename);
573                 close(fd);
574                 free(buf);
575                 return 1;
576         }
577
578         pdu->buf_len = __cpu_to_le32(sb.st_size);
579         pdu->client_type = cpu_to_le32(client->type);
580
581         client->sent_job = 1;
582         ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, pdu, p_size, 0);
583         free(pdu);
584         close(fd);
585         return ret;
586 }
587
588 int fio_client_send_ini(struct fio_client *client, const char *filename)
589 {
590         int ret;
591
592         ret = __fio_client_send_ini(client, filename);
593         if (!ret)
594                 client->sent_job = 1;
595
596         return ret;
597 }
598
599 int fio_clients_send_ini(const char *filename)
600 {
601         struct fio_client *client;
602         struct flist_head *entry, *tmp;
603
604         flist_for_each_safe(entry, tmp, &client_list) {
605                 client = flist_entry(entry, struct fio_client, list);
606
607                 if (fio_client_send_ini(client, filename))
608                         remove_client(client);
609         }
610
611         return !nr_clients;
612 }
613
614 static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
615 {
616         dst->max_val    = le64_to_cpu(src->max_val);
617         dst->min_val    = le64_to_cpu(src->min_val);
618         dst->samples    = le64_to_cpu(src->samples);
619
620         /*
621          * Floats arrive as IEEE 754 encoded uint64_t, convert back to double
622          */
623         dst->mean.u.f   = fio_uint64_to_double(le64_to_cpu(dst->mean.u.i));
624         dst->S.u.f      = fio_uint64_to_double(le64_to_cpu(dst->S.u.i));
625 }
626
627 static void convert_ts(struct thread_stat *dst, struct thread_stat *src)
628 {
629         int i, j;
630
631         dst->error      = le32_to_cpu(src->error);
632         dst->groupid    = le32_to_cpu(src->groupid);
633         dst->pid        = le32_to_cpu(src->pid);
634         dst->members    = le32_to_cpu(src->members);
635
636         for (i = 0; i < 2; i++) {
637                 convert_io_stat(&dst->clat_stat[i], &src->clat_stat[i]);
638                 convert_io_stat(&dst->slat_stat[i], &src->slat_stat[i]);
639                 convert_io_stat(&dst->lat_stat[i], &src->lat_stat[i]);
640                 convert_io_stat(&dst->bw_stat[i], &src->bw_stat[i]);
641         }
642
643         dst->usr_time           = le64_to_cpu(src->usr_time);
644         dst->sys_time           = le64_to_cpu(src->sys_time);
645         dst->ctx                = le64_to_cpu(src->ctx);
646         dst->minf               = le64_to_cpu(src->minf);
647         dst->majf               = le64_to_cpu(src->majf);
648         dst->clat_percentiles   = le64_to_cpu(src->clat_percentiles);
649
650         for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
651                 fio_fp64_t *fps = &src->percentile_list[i];
652                 fio_fp64_t *fpd = &dst->percentile_list[i];
653
654                 fpd->u.f = fio_uint64_to_double(le64_to_cpu(fps->u.i));
655         }
656
657         for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
658                 dst->io_u_map[i]        = le32_to_cpu(src->io_u_map[i]);
659                 dst->io_u_submit[i]     = le32_to_cpu(src->io_u_submit[i]);
660                 dst->io_u_complete[i]   = le32_to_cpu(src->io_u_complete[i]);
661         }
662
663         for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
664                 dst->io_u_lat_u[i]      = le32_to_cpu(src->io_u_lat_u[i]);
665                 dst->io_u_lat_m[i]      = le32_to_cpu(src->io_u_lat_m[i]);
666         }
667
668         for (i = 0; i < 2; i++)
669                 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
670                         dst->io_u_plat[i][j] = le32_to_cpu(src->io_u_plat[i][j]);
671
672         for (i = 0; i < 3; i++) {
673                 dst->total_io_u[i]      = le64_to_cpu(src->total_io_u[i]);
674                 dst->short_io_u[i]      = le64_to_cpu(src->short_io_u[i]);
675         }
676
677         dst->total_submit       = le64_to_cpu(src->total_submit);
678         dst->total_complete     = le64_to_cpu(src->total_complete);
679
680         for (i = 0; i < 2; i++) {
681                 dst->io_bytes[i]        = le64_to_cpu(src->io_bytes[i]);
682                 dst->runtime[i]         = le64_to_cpu(src->runtime[i]);
683         }
684
685         dst->total_run_time     = le64_to_cpu(src->total_run_time);
686         dst->continue_on_error  = le16_to_cpu(src->continue_on_error);
687         dst->total_err_count    = le64_to_cpu(src->total_err_count);
688         dst->first_error        = le32_to_cpu(src->first_error);
689         dst->kb_base            = le32_to_cpu(src->kb_base);
690 }
691
692 static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
693 {
694         int i;
695
696         for (i = 0; i < 2; i++) {
697                 dst->max_run[i]         = le64_to_cpu(src->max_run[i]);
698                 dst->min_run[i]         = le64_to_cpu(src->min_run[i]);
699                 dst->max_bw[i]          = le64_to_cpu(src->max_bw[i]);
700                 dst->min_bw[i]          = le64_to_cpu(src->min_bw[i]);
701                 dst->io_kb[i]           = le64_to_cpu(src->io_kb[i]);
702                 dst->agg[i]             = le64_to_cpu(src->agg[i]);
703         }
704
705         dst->kb_base    = le32_to_cpu(src->kb_base);
706         dst->groupid    = le32_to_cpu(src->groupid);
707 }
708
709 static void handle_ts(struct fio_client *client, struct fio_net_cmd *cmd)
710 {
711         struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
712
713         show_thread_status(&p->ts, &p->rs);
714
715         if (sum_stat_clients == 1)
716                 return;
717
718         sum_thread_stats(&client_ts, &p->ts, sum_stat_nr);
719         sum_group_stats(&client_gs, &p->rs);
720
721         client_ts.members++;
722         client_ts.groupid = p->ts.groupid;
723
724         if (++sum_stat_nr == sum_stat_clients) {
725                 strcpy(client_ts.name, "All clients");
726                 show_thread_status(&client_ts, &client_gs);
727         }
728 }
729
730 static void handle_gs(struct fio_client *client, struct fio_net_cmd *cmd)
731 {
732         struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
733
734         show_group_stats(gs);
735 }
736
737 static void handle_text(struct fio_client *client, struct fio_net_cmd *cmd)
738 {
739         struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
740         const char *buf = (const char *) pdu->buf;
741         const char *name;
742         int fio_unused ret;
743
744         name = client->name ? client->name : client->hostname;
745
746         if (!client->skip_newline)
747                 fprintf(f_out, "<%s> ", name);
748         ret = fwrite(buf, pdu->buf_len, 1, f_out);
749         fflush(f_out);
750         client->skip_newline = strchr(buf, '\n') == NULL;
751 }
752
753 static void convert_agg(struct disk_util_agg *agg)
754 {
755         int i;
756
757         for (i = 0; i < 2; i++) {
758                 agg->ios[i]     = le32_to_cpu(agg->ios[i]);
759                 agg->merges[i]  = le32_to_cpu(agg->merges[i]);
760                 agg->sectors[i] = le64_to_cpu(agg->sectors[i]);
761                 agg->ticks[i]   = le32_to_cpu(agg->ticks[i]);
762         }
763
764         agg->io_ticks           = le32_to_cpu(agg->io_ticks);
765         agg->time_in_queue      = le32_to_cpu(agg->time_in_queue);
766         agg->slavecount         = le32_to_cpu(agg->slavecount);
767         agg->max_util.u.f       = fio_uint64_to_double(__le64_to_cpu(agg->max_util.u.i));
768 }
769
770 static void convert_dus(struct disk_util_stat *dus)
771 {
772         int i;
773
774         for (i = 0; i < 2; i++) {
775                 dus->ios[i]     = le32_to_cpu(dus->ios[i]);
776                 dus->merges[i]  = le32_to_cpu(dus->merges[i]);
777                 dus->sectors[i] = le64_to_cpu(dus->sectors[i]);
778                 dus->ticks[i]   = le32_to_cpu(dus->ticks[i]);
779         }
780
781         dus->io_ticks           = le32_to_cpu(dus->io_ticks);
782         dus->time_in_queue      = le32_to_cpu(dus->time_in_queue);
783         dus->msec               = le64_to_cpu(dus->msec);
784 }
785
786 static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd)
787 {
788         struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
789
790         if (!client->disk_stats_shown) {
791                 client->disk_stats_shown = 1;
792                 log_info("\nDisk stats (read/write):\n");
793         }
794
795         print_disk_util(&du->dus, &du->agg, terse_output);
796 }
797
798 static void convert_jobs_eta(struct jobs_eta *je)
799 {
800         int i;
801
802         je->nr_running          = le32_to_cpu(je->nr_running);
803         je->nr_ramp             = le32_to_cpu(je->nr_ramp);
804         je->nr_pending          = le32_to_cpu(je->nr_pending);
805         je->files_open          = le32_to_cpu(je->files_open);
806
807         for (i = 0; i < 2; i++) {
808                 je->m_rate[i]           = le32_to_cpu(je->m_rate[i]);
809                 je->t_rate[i]           = le32_to_cpu(je->t_rate[i]);
810                 je->m_iops[i]           = le32_to_cpu(je->m_iops[i]);
811                 je->t_iops[i]           = le32_to_cpu(je->t_iops[i]);
812                 je->rate[i]     = le32_to_cpu(je->rate[i]);
813                 je->iops[i]     = le32_to_cpu(je->iops[i]);
814         }
815
816         je->elapsed_sec         = le64_to_cpu(je->elapsed_sec);
817         je->eta_sec             = le64_to_cpu(je->eta_sec);
818         je->nr_threads          = le32_to_cpu(je->nr_threads);
819 }
820
821 void fio_client_sum_jobs_eta(struct jobs_eta *dst, struct jobs_eta *je)
822 {
823         int i;
824
825         dst->nr_running         += je->nr_running;
826         dst->nr_ramp            += je->nr_ramp;
827         dst->nr_pending         += je->nr_pending;
828         dst->files_open         += je->files_open;
829
830         for (i = 0; i < 2; i++) {
831                 dst->m_rate[i]  += je->m_rate[i];
832                 dst->t_rate[i]  += je->t_rate[i];
833                 dst->m_iops[i]  += je->m_iops[i];
834                 dst->t_iops[i]  += je->t_iops[i];
835                 dst->rate[i]    += je->rate[i];
836                 dst->iops[i]    += je->iops[i];
837         }
838
839         dst->elapsed_sec        += je->elapsed_sec;
840
841         if (je->eta_sec > dst->eta_sec)
842                 dst->eta_sec = je->eta_sec;
843
844         dst->nr_threads         += je->nr_threads;
845         /* we need to handle je->run_str too ... */
846 }
847
848 void fio_client_dec_jobs_eta(struct client_eta *eta, client_eta_op eta_fn)
849 {
850         if (!--eta->pending) {
851                 eta_fn(&eta->eta);
852                 free(eta);
853         }
854 }
855
856 static void remove_reply_cmd(struct fio_client *client, struct fio_net_cmd *cmd)
857 {
858         struct fio_net_int_cmd *icmd = NULL;
859         struct flist_head *entry;
860
861         flist_for_each(entry, &client->cmd_list) {
862                 icmd = flist_entry(entry, struct fio_net_int_cmd, list);
863
864                 if (cmd->tag == (uintptr_t) icmd)
865                         break;
866
867                 icmd = NULL;
868         }
869
870         if (!icmd) {
871                 log_err("fio: client: unable to find matching tag\n");
872                 return;
873         }
874
875         flist_del(&icmd->list);
876         cmd->tag = icmd->saved_tag;
877         free(icmd);
878 }
879
880 static void handle_eta(struct fio_client *client, struct fio_net_cmd *cmd)
881 {
882         struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
883         struct client_eta *eta = (struct client_eta *) (uintptr_t) cmd->tag;
884
885         dprint(FD_NET, "client: got eta tag %p, %d\n", eta, eta->pending);
886
887         assert(client->eta_in_flight == eta);
888
889         client->eta_in_flight = NULL;
890         flist_del_init(&client->eta_list);
891
892         if (client->ops->jobs_eta)
893                 client->ops->jobs_eta(client, je);
894
895         fio_client_sum_jobs_eta(&eta->eta, je);
896         fio_client_dec_jobs_eta(eta, client->ops->eta);
897 }
898
899 static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd)
900 {
901         struct cmd_probe_pdu *probe = (struct cmd_probe_pdu *) cmd->payload;
902         const char *os, *arch;
903         char bit[16];
904
905         os = fio_get_os_string(probe->os);
906         if (!os)
907                 os = "unknown";
908
909         arch = fio_get_arch_string(probe->arch);
910         if (!arch)
911                 os = "unknown";
912
913         sprintf(bit, "%d-bit", probe->bpp * 8);
914
915         log_info("hostname=%s, be=%u, %s, os=%s, arch=%s, fio=%u.%u.%u\n",
916                 probe->hostname, probe->bigendian, bit, os, arch,
917                 probe->fio_major, probe->fio_minor, probe->fio_patch);
918
919         if (!client->name)
920                 client->name = strdup((char *) probe->hostname);
921 }
922
923 static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd)
924 {
925         struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
926
927         client->state = Client_started;
928         client->jobs = pdu->jobs;
929 }
930
931 static void handle_stop(struct fio_client *client, struct fio_net_cmd *cmd)
932 {
933         if (client->error)
934                 log_info("client <%s>: exited with error %d\n", client->hostname, client->error);
935 }
936
937 static void convert_stop(struct fio_net_cmd *cmd)
938 {
939         struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
940
941         pdu->error = le32_to_cpu(pdu->error);
942 }
943
944 static void convert_text(struct fio_net_cmd *cmd)
945 {
946         struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
947
948         pdu->level      = le32_to_cpu(pdu->level);
949         pdu->buf_len    = le32_to_cpu(pdu->buf_len);
950         pdu->log_sec    = le64_to_cpu(pdu->log_sec);
951         pdu->log_usec   = le64_to_cpu(pdu->log_usec);
952 }
953
954 /*
955  * This has been compressed on the server side, since it can be big.
956  * Uncompress here.
957  */
958 static struct cmd_iolog_pdu *convert_iolog(struct fio_net_cmd *cmd)
959 {
960         struct cmd_iolog_pdu *pdu = (struct cmd_iolog_pdu *) cmd->payload;
961         struct cmd_iolog_pdu *ret;
962         uint32_t nr_samples;
963         unsigned long total;
964         z_stream stream;
965         void *p;
966         int i;
967
968         stream.zalloc = Z_NULL;
969         stream.zfree = Z_NULL;
970         stream.opaque = Z_NULL;
971         stream.avail_in = 0;
972         stream.next_in = Z_NULL;
973
974         if (inflateInit(&stream) != Z_OK)
975                 return NULL;
976
977         /*
978          * Get header first, it's not compressed
979          */
980         nr_samples = le32_to_cpu(pdu->nr_samples);
981
982         total = nr_samples * sizeof(struct io_sample);
983         ret = malloc(total + sizeof(*pdu));
984         ret->nr_samples = nr_samples;
985         ret->log_type = le32_to_cpu(pdu->log_type);
986         strcpy((char *) ret->name, (char *) pdu->name);
987
988         p = (void *) ret + sizeof(*pdu);
989
990         stream.avail_in = cmd->pdu_len - sizeof(*pdu);
991         stream.next_in = (void *) pdu + sizeof(*pdu);
992         while (stream.avail_in) {
993                 unsigned int this_chunk = 65536;
994                 unsigned int this_len;
995                 int err;
996
997                 if (this_chunk > total)
998                         this_chunk = total;
999
1000                 stream.avail_out = this_chunk;
1001                 stream.next_out = p;
1002                 err = inflate(&stream, Z_NO_FLUSH);
1003                 if (err != Z_OK) {
1004                         log_err("fio: inflate error %d\n", err);
1005                         free(ret);
1006                         ret = NULL;
1007                         goto out;
1008                 }
1009
1010                 this_len = this_chunk - stream.avail_out;
1011                 p += this_len;
1012                 total -= this_len;
1013         }
1014
1015         for (i = 0; i < ret->nr_samples; i++) {
1016                 struct io_sample *s = &ret->samples[i];
1017
1018                 s->time = le64_to_cpu(s->time);
1019                 s->val  = le64_to_cpu(s->val);
1020                 s->ddir = le32_to_cpu(s->ddir);
1021                 s->bs   = le32_to_cpu(s->bs);
1022         }
1023
1024 out:
1025         inflateEnd(&stream);
1026         return ret;
1027 }
1028
1029 int fio_handle_client(struct fio_client *client)
1030 {
1031         struct client_ops *ops = client->ops;
1032         struct fio_net_cmd *cmd;
1033
1034         dprint(FD_NET, "client: handle %s\n", client->hostname);
1035
1036         cmd = fio_net_recv_cmd(client->fd);
1037         if (!cmd)
1038                 return 0;
1039
1040         dprint(FD_NET, "client: got cmd op %s from %s (pdu=%u)\n",
1041                 fio_server_op(cmd->opcode), client->hostname, cmd->pdu_len);
1042
1043         switch (cmd->opcode) {
1044         case FIO_NET_CMD_QUIT:
1045                 if (ops->quit)
1046                         ops->quit(client, cmd);
1047                 remove_client(client);
1048                 free(cmd);
1049                 break;
1050         case FIO_NET_CMD_TEXT:
1051                 convert_text(cmd);
1052                 ops->text(client, cmd);
1053                 free(cmd);
1054                 break;
1055         case FIO_NET_CMD_DU: {
1056                 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
1057
1058                 convert_dus(&du->dus);
1059                 convert_agg(&du->agg);
1060
1061                 ops->disk_util(client, cmd);
1062                 free(cmd);
1063                 break;
1064                 }
1065         case FIO_NET_CMD_TS: {
1066                 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
1067
1068                 convert_ts(&p->ts, &p->ts);
1069                 convert_gs(&p->rs, &p->rs);
1070
1071                 ops->thread_status(client, cmd);
1072                 free(cmd);
1073                 break;
1074                 }
1075         case FIO_NET_CMD_GS: {
1076                 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
1077
1078                 convert_gs(gs, gs);
1079
1080                 ops->group_stats(client, cmd);
1081                 free(cmd);
1082                 break;
1083                 }
1084         case FIO_NET_CMD_ETA: {
1085                 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
1086
1087                 remove_reply_cmd(client, cmd);
1088                 convert_jobs_eta(je);
1089                 handle_eta(client, cmd);
1090                 free(cmd);
1091                 break;
1092                 }
1093         case FIO_NET_CMD_PROBE:
1094                 remove_reply_cmd(client, cmd);
1095                 ops->probe(client, cmd);
1096                 free(cmd);
1097                 break;
1098         case FIO_NET_CMD_SERVER_START:
1099                 client->state = Client_running;
1100                 if (ops->job_start)
1101                         ops->job_start(client, cmd);
1102                 free(cmd);
1103                 break;
1104         case FIO_NET_CMD_START: {
1105                 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
1106
1107                 pdu->jobs = le32_to_cpu(pdu->jobs);
1108                 ops->start(client, cmd);
1109                 free(cmd);
1110                 break;
1111                 }
1112         case FIO_NET_CMD_STOP: {
1113                 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
1114
1115                 convert_stop(cmd);
1116                 client->state = Client_stopped;
1117                 client->error = pdu->error;
1118                 ops->stop(client, cmd);
1119                 free(cmd);
1120                 break;
1121                 }
1122         case FIO_NET_CMD_ADD_JOB:
1123                 if (ops->add_job)
1124                         ops->add_job(client, cmd);
1125                 free(cmd);
1126                 break;
1127         case FIO_NET_CMD_IOLOG:
1128                 if (ops->iolog) {
1129                         struct cmd_iolog_pdu *pdu;
1130
1131                         pdu = convert_iolog(cmd);
1132                         ops->iolog(client, pdu);
1133                 }
1134                 free(cmd);
1135                 break;
1136         default:
1137                 log_err("fio: unknown client op: %s\n", fio_server_op(cmd->opcode));
1138                 free(cmd);
1139                 break;
1140         }
1141
1142         return 1;
1143 }
1144
1145 static void request_client_etas(struct client_ops *ops)
1146 {
1147         struct fio_client *client;
1148         struct flist_head *entry;
1149         struct client_eta *eta;
1150         int skipped = 0;
1151
1152         dprint(FD_NET, "client: request eta (%d)\n", nr_clients);
1153
1154         eta = malloc(sizeof(*eta));
1155         memset(&eta->eta, 0, sizeof(eta->eta));
1156         eta->pending = nr_clients;
1157
1158         flist_for_each(entry, &client_list) {
1159                 client = flist_entry(entry, struct fio_client, list);
1160
1161                 if (!flist_empty(&client->eta_list)) {
1162                         skipped++;
1163                         continue;
1164                 }
1165                 if (client->state != Client_running)
1166                         continue;
1167
1168                 assert(!client->eta_in_flight);
1169                 flist_add_tail(&client->eta_list, &eta_list);
1170                 client->eta_in_flight = eta;
1171                 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_SEND_ETA,
1172                                         (uintptr_t) eta, &client->cmd_list);
1173         }
1174
1175         while (skipped--)
1176                 fio_client_dec_jobs_eta(eta, ops->eta);
1177
1178         dprint(FD_NET, "client: requested eta tag %p\n", eta);
1179 }
1180
1181 static int client_check_cmd_timeout(struct fio_client *client,
1182                                     struct timeval *now)
1183 {
1184         struct fio_net_int_cmd *cmd;
1185         struct flist_head *entry, *tmp;
1186         int ret = 0;
1187
1188         flist_for_each_safe(entry, tmp, &client->cmd_list) {
1189                 cmd = flist_entry(entry, struct fio_net_int_cmd, list);
1190
1191                 if (mtime_since(&cmd->tv, now) < FIO_NET_CLIENT_TIMEOUT)
1192                         continue;
1193
1194                 log_err("fio: client %s, timeout on cmd %s\n", client->hostname,
1195                                                 fio_server_op(cmd->cmd.opcode));
1196                 flist_del(&cmd->list);
1197                 free(cmd);
1198                 ret = 1;
1199         }
1200
1201         return flist_empty(&client->cmd_list) && ret;
1202 }
1203
1204 static int fio_check_clients_timed_out(void)
1205 {
1206         struct fio_client *client;
1207         struct flist_head *entry, *tmp;
1208         struct timeval tv;
1209         int ret = 0;
1210
1211         gettimeofday(&tv, NULL);
1212
1213         flist_for_each_safe(entry, tmp, &client_list) {
1214                 client = flist_entry(entry, struct fio_client, list);
1215
1216                 if (flist_empty(&client->cmd_list))
1217                         continue;
1218
1219                 if (!client_check_cmd_timeout(client, &tv))
1220                         continue;
1221
1222                 if (client->ops->timed_out)
1223                         client->ops->timed_out(client);
1224                 else
1225                         log_err("fio: client %s timed out\n", client->hostname);
1226
1227                 remove_client(client);
1228                 ret = 1;
1229         }
1230
1231         return ret;
1232 }
1233
1234 int fio_handle_clients(struct client_ops *ops)
1235 {
1236         struct pollfd *pfds;
1237         int i, ret = 0, retval = 0;
1238
1239         gettimeofday(&eta_tv, NULL);
1240
1241         pfds = malloc(nr_clients * sizeof(struct pollfd));
1242
1243         sum_stat_clients = nr_clients;
1244         init_thread_stat(&client_ts);
1245         init_group_run_stat(&client_gs);
1246
1247         while (!exit_backend && nr_clients) {
1248                 struct flist_head *entry, *tmp;
1249                 struct fio_client *client;
1250
1251                 i = 0;
1252                 flist_for_each_safe(entry, tmp, &client_list) {
1253                         client = flist_entry(entry, struct fio_client, list);
1254
1255                         if (!client->sent_job && !client->ops->stay_connected &&
1256                             flist_empty(&client->cmd_list)) {
1257                                 remove_client(client);
1258                                 continue;
1259                         }
1260
1261                         pfds[i].fd = client->fd;
1262                         pfds[i].events = POLLIN;
1263                         i++;
1264                 }
1265
1266                 if (!nr_clients)
1267                         break;
1268
1269                 assert(i == nr_clients);
1270
1271                 do {
1272                         struct timeval tv;
1273
1274                         gettimeofday(&tv, NULL);
1275                         if (mtime_since(&eta_tv, &tv) >= ops->eta_msec) {
1276                                 request_client_etas(ops);
1277                                 memcpy(&eta_tv, &tv, sizeof(tv));
1278
1279                                 if (fio_check_clients_timed_out())
1280                                         break;
1281                         }
1282
1283                         ret = poll(pfds, nr_clients, 100);
1284                         if (ret < 0) {
1285                                 if (errno == EINTR)
1286                                         continue;
1287                                 log_err("fio: poll clients: %s\n", strerror(errno));
1288                                 break;
1289                         } else if (!ret)
1290                                 continue;
1291                 } while (ret <= 0);
1292
1293                 for (i = 0; i < nr_clients; i++) {
1294                         if (!(pfds[i].revents & POLLIN))
1295                                 continue;
1296
1297                         client = find_client_by_fd(pfds[i].fd);
1298                         if (!client) {
1299                                 log_err("fio: unknown client fd %d\n", pfds[i].fd);
1300                                 continue;
1301                         }
1302                         if (!fio_handle_client(client)) {
1303                                 log_info("client: host=%s disconnected\n",
1304                                                 client->hostname);
1305                                 remove_client(client);
1306                                 retval = 1;
1307                         } else if (client->error)
1308                                 retval = 1;
1309                         fio_put_client(client);
1310                 }
1311         }
1312
1313         free(pfds);
1314         return retval;
1315 }