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