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