client/server: convert nr_zone_resets on the wire
[fio.git] / client.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <errno.h>
5 #include <fcntl.h>
6 #include <poll.h>
7 #include <sys/types.h>
8 #include <sys/stat.h>
9 #include <sys/socket.h>
10 #include <sys/un.h>
11 #include <netinet/in.h>
12 #include <arpa/inet.h>
13 #include <netdb.h>
14 #include <signal.h>
15 #ifdef CONFIG_ZLIB
16 #include <zlib.h>
17 #endif
18
19 #include "fio.h"
20 #include "client.h"
21 #include "server.h"
22 #include "flist.h"
23 #include "hash.h"
24 #include "verify-state.h"
25
26 static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd);
27 static void handle_ts(struct fio_client *client, struct fio_net_cmd *cmd);
28 static void handle_gs(struct fio_client *client, struct fio_net_cmd *cmd);
29 static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd);
30 static void handle_text(struct fio_client *client, struct fio_net_cmd *cmd);
31 static void handle_stop(struct fio_client *client);
32 static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd);
33
34 static void convert_text(struct fio_net_cmd *cmd);
35 static void client_display_thread_status(struct jobs_eta *je);
36
37 struct client_ops fio_client_ops = {
38         .text           = handle_text,
39         .disk_util      = handle_du,
40         .thread_status  = handle_ts,
41         .group_stats    = handle_gs,
42         .stop           = handle_stop,
43         .start          = handle_start,
44         .eta            = client_display_thread_status,
45         .probe          = handle_probe,
46         .eta_msec       = FIO_CLIENT_DEF_ETA_MSEC,
47         .client_type    = FIO_CLIENT_TYPE_CLI,
48 };
49
50 static struct timespec eta_ts;
51
52 static FLIST_HEAD(client_list);
53 static FLIST_HEAD(eta_list);
54
55 static FLIST_HEAD(arg_list);
56
57 struct thread_stat client_ts;
58 struct group_run_stats client_gs;
59 int sum_stat_clients;
60
61 static int sum_stat_nr;
62 static struct json_object *root = NULL;
63 static struct json_object *job_opt_object = NULL;
64 static struct json_array *clients_array = NULL;
65 static struct json_array *du_array = NULL;
66
67 static int error_clients;
68
69 #define FIO_CLIENT_HASH_BITS    7
70 #define FIO_CLIENT_HASH_SZ      (1 << FIO_CLIENT_HASH_BITS)
71 #define FIO_CLIENT_HASH_MASK    (FIO_CLIENT_HASH_SZ - 1)
72 static struct flist_head client_hash[FIO_CLIENT_HASH_SZ];
73
74 static struct cmd_iolog_pdu *convert_iolog(struct fio_net_cmd *, bool *);
75
76 static void fio_client_add_hash(struct fio_client *client)
77 {
78         int bucket = hash_long(client->fd, FIO_CLIENT_HASH_BITS);
79
80         bucket &= FIO_CLIENT_HASH_MASK;
81         flist_add(&client->hash_list, &client_hash[bucket]);
82 }
83
84 static void fio_client_remove_hash(struct fio_client *client)
85 {
86         if (!flist_empty(&client->hash_list))
87                 flist_del_init(&client->hash_list);
88 }
89
90 static void fio_init fio_client_hash_init(void)
91 {
92         int i;
93
94         for (i = 0; i < FIO_CLIENT_HASH_SZ; i++)
95                 INIT_FLIST_HEAD(&client_hash[i]);
96 }
97
98 static int read_data(int fd, void *data, size_t size)
99 {
100         ssize_t ret;
101
102         while (size) {
103                 ret = read(fd, data, size);
104                 if (ret < 0) {
105                         if (errno == EAGAIN || errno == EINTR)
106                                 continue;
107                         break;
108                 } else if (!ret)
109                         break;
110                 else {
111                         data += ret;
112                         size -= ret;
113                 }
114         }
115
116         if (size)
117                 return EAGAIN;
118
119         return 0;
120 }
121
122 static int read_ini_data(int fd, void *data, size_t size)
123 {
124         char *p = data;
125         int ret = 0;
126         FILE *fp;
127         int dupfd;
128
129         dupfd = dup(fd);
130         if (dupfd < 0)
131                 return errno;
132
133         fp = fdopen(dupfd, "r");
134         if (!fp) {
135                 ret = errno;
136                 close(dupfd);
137                 goto out;
138         }
139
140         while (1) {
141                 ssize_t len;
142                 char buf[OPT_LEN_MAX+1], *sub;
143
144                 if (!fgets(buf, sizeof(buf), fp)) {
145                         if (ferror(fp)) {
146                                 if (errno == EAGAIN || errno == EINTR)
147                                         continue;
148                                 ret = errno;
149                         }
150                         break;
151                 }
152
153                 sub = fio_option_dup_subs(buf);
154                 len = strlen(sub);
155                 if (len + 1 > size) {
156                         log_err("fio: no space left to read data\n");
157                         free(sub);
158                         ret = ENOSPC;
159                         break;
160                 }
161
162                 memcpy(p, sub, len);
163                 free(sub);
164                 p += len;
165                 *p = '\0';
166                 size -= len;
167         }
168
169         fclose(fp);
170 out:
171         return ret;
172 }
173
174 static void fio_client_json_init(void)
175 {
176         char time_buf[32];
177         time_t time_p;
178
179         if (!(output_format & FIO_OUTPUT_JSON))
180                 return;
181
182         time(&time_p);
183         os_ctime_r((const time_t *) &time_p, time_buf, sizeof(time_buf));
184         time_buf[strlen(time_buf) - 1] = '\0';
185
186         root = json_create_object();
187         json_object_add_value_string(root, "fio version", fio_version_string);
188         json_object_add_value_int(root, "timestamp", time_p);
189         json_object_add_value_string(root, "time", time_buf);
190
191         job_opt_object = json_create_object();
192         json_object_add_value_object(root, "global options", job_opt_object);
193         clients_array = json_create_array();
194         json_object_add_value_array(root, "client_stats", clients_array);
195         du_array = json_create_array();
196         json_object_add_value_array(root, "disk_util", du_array);
197 }
198
199 static void fio_client_json_fini(void)
200 {
201         struct buf_output out;
202
203         if (!root)
204                 return;
205
206         buf_output_init(&out);
207
208         __log_buf(&out, "\n");
209         json_print_object(root, &out);
210         __log_buf(&out, "\n");
211         log_info_buf(out.buf, out.buflen);
212
213         buf_output_free(&out);
214
215         json_free_object(root);
216         root = NULL;
217         job_opt_object = NULL;
218         clients_array = NULL;
219         du_array = NULL;
220 }
221
222 static struct fio_client *find_client_by_fd(int fd)
223 {
224         int bucket = hash_long(fd, FIO_CLIENT_HASH_BITS) & FIO_CLIENT_HASH_MASK;
225         struct fio_client *client;
226         struct flist_head *entry;
227
228         flist_for_each(entry, &client_hash[bucket]) {
229                 client = flist_entry(entry, struct fio_client, hash_list);
230
231                 if (client->fd == fd) {
232                         client->refs++;
233                         return client;
234                 }
235         }
236
237         return NULL;
238 }
239
240 void fio_put_client(struct fio_client *client)
241 {
242         if (--client->refs)
243                 return;
244
245         log_info_buf(client->buf.buf, client->buf.buflen);
246         buf_output_free(&client->buf);
247
248         free(client->hostname);
249         if (client->argv)
250                 free(client->argv);
251         if (client->name)
252                 free(client->name);
253         while (client->nr_files) {
254                 struct client_file *cf = &client->files[--client->nr_files];
255
256                 free(cf->file);
257         }
258         if (client->files)
259                 free(client->files);
260         if (client->opt_lists)
261                 free(client->opt_lists);
262
263         if (!client->did_stat)
264                 sum_stat_clients--;
265
266         if (client->error)
267                 error_clients++;
268
269         free(client);
270 }
271
272 static int fio_client_dec_jobs_eta(struct client_eta *eta, client_eta_op eta_fn)
273 {
274         if (!--eta->pending) {
275                 eta_fn(&eta->eta);
276                 free(eta);
277                 return 0;
278         }
279
280         return 1;
281 }
282
283 static void fio_drain_client_text(struct fio_client *client)
284 {
285         do {
286                 struct fio_net_cmd *cmd;
287
288                 cmd = fio_net_recv_cmd(client->fd, false);
289                 if (!cmd)
290                         break;
291
292                 if (cmd->opcode == FIO_NET_CMD_TEXT) {
293                         convert_text(cmd);
294                         client->ops->text(client, cmd);
295                 }
296
297                 free(cmd);
298         } while (1);
299 }
300
301 static void remove_client(struct fio_client *client)
302 {
303         assert(client->refs);
304
305         dprint(FD_NET, "client: removed <%s>\n", client->hostname);
306
307         fio_drain_client_text(client);
308
309         if (!flist_empty(&client->list))
310                 flist_del_init(&client->list);
311
312         fio_client_remove_hash(client);
313
314         if (!flist_empty(&client->eta_list)) {
315                 flist_del_init(&client->eta_list);
316                 fio_client_dec_jobs_eta(client->eta_in_flight, client->ops->eta);
317         }
318
319         close(client->fd);
320         client->fd = -1;
321
322         if (client->ops->removed)
323                 client->ops->removed(client);
324
325         nr_clients--;
326         fio_put_client(client);
327 }
328
329 struct fio_client *fio_get_client(struct fio_client *client)
330 {
331         client->refs++;
332         return client;
333 }
334
335 static void __fio_client_add_cmd_option(struct fio_client *client,
336                                         const char *opt)
337 {
338         int index;
339
340         index = client->argc++;
341         client->argv = realloc(client->argv, sizeof(char *) * client->argc);
342         client->argv[index] = strdup(opt);
343         dprint(FD_NET, "client: add cmd %d: %s\n", index, opt);
344 }
345
346 void fio_client_add_cmd_option(void *cookie, const char *opt)
347 {
348         struct fio_client *client = cookie;
349         struct flist_head *entry;
350
351         if (!client || !opt)
352                 return;
353
354         __fio_client_add_cmd_option(client, opt);
355
356         /*
357          * Duplicate arguments to shared client group
358          */
359         flist_for_each(entry, &arg_list) {
360                 client = flist_entry(entry, struct fio_client, arg_list);
361
362                 __fio_client_add_cmd_option(client, opt);
363         }
364 }
365
366 static struct fio_client *get_new_client(void)
367 {
368         struct fio_client *client;
369
370         client = malloc(sizeof(*client));
371         memset(client, 0, sizeof(*client));
372
373         INIT_FLIST_HEAD(&client->list);
374         INIT_FLIST_HEAD(&client->hash_list);
375         INIT_FLIST_HEAD(&client->arg_list);
376         INIT_FLIST_HEAD(&client->eta_list);
377         INIT_FLIST_HEAD(&client->cmd_list);
378
379         buf_output_init(&client->buf);
380
381         return client;
382 }
383
384 struct fio_client *fio_client_add_explicit(struct client_ops *ops,
385                                            const char *hostname, int type,
386                                            int port)
387 {
388         struct fio_client *client;
389
390         client = get_new_client();
391
392         client->hostname = strdup(hostname);
393
394         if (type == Fio_client_socket)
395                 client->is_sock = true;
396         else {
397                 int ipv6;
398
399                 ipv6 = type == Fio_client_ipv6;
400                 if (fio_server_parse_host(hostname, ipv6,
401                                                 &client->addr.sin_addr,
402                                                 &client->addr6.sin6_addr))
403                         goto err;
404
405                 client->port = port;
406         }
407
408         client->fd = -1;
409         client->ops = ops;
410         client->refs = 1;
411         client->type = ops->client_type;
412
413         __fio_client_add_cmd_option(client, "fio");
414
415         flist_add(&client->list, &client_list);
416         nr_clients++;
417         dprint(FD_NET, "client: added <%s>\n", client->hostname);
418         return client;
419 err:
420         free(client);
421         return NULL;
422 }
423
424 int fio_client_add_ini_file(void *cookie, const char *ini_file, bool remote)
425 {
426         struct fio_client *client = cookie;
427         struct client_file *cf;
428         size_t new_size;
429         void *new_files;
430
431         if (!client)
432                 return 1;
433
434         dprint(FD_NET, "client <%s>: add ini %s\n", client->hostname, ini_file);
435
436         new_size = (client->nr_files + 1) * sizeof(struct client_file);
437         new_files = realloc(client->files, new_size);
438         if (!new_files)
439                 return 1;
440
441         client->files = new_files;
442         cf = &client->files[client->nr_files];
443         cf->file = strdup(ini_file);
444         cf->remote = remote;
445         client->nr_files++;
446         return 0;
447 }
448
449 int fio_client_add(struct client_ops *ops, const char *hostname, void **cookie)
450 {
451         struct fio_client *existing = *cookie;
452         struct fio_client *client;
453
454         if (existing) {
455                 /*
456                  * We always add our "exec" name as the option, hence 1
457                  * means empty.
458                  */
459                 if (existing->argc == 1)
460                         flist_add_tail(&existing->arg_list, &arg_list);
461                 else {
462                         while (!flist_empty(&arg_list))
463                                 flist_del_init(arg_list.next);
464                 }
465         }
466
467         client = get_new_client();
468
469         if (fio_server_parse_string(hostname, &client->hostname,
470                                         &client->is_sock, &client->port,
471                                         &client->addr.sin_addr,
472                                         &client->addr6.sin6_addr,
473                                         &client->ipv6))
474                 return -1;
475
476         client->fd = -1;
477         client->ops = ops;
478         client->refs = 1;
479         client->type = ops->client_type;
480
481         __fio_client_add_cmd_option(client, "fio");
482
483         flist_add(&client->list, &client_list);
484         nr_clients++;
485         dprint(FD_NET, "client: added <%s>\n", client->hostname);
486         *cookie = client;
487         return 0;
488 }
489
490 static const char *server_name(struct fio_client *client, char *buf,
491                                size_t bufsize)
492 {
493         const char *from;
494
495         if (client->ipv6)
496                 from = inet_ntop(AF_INET6, (struct sockaddr *) &client->addr6.sin6_addr, buf, bufsize);
497         else if (client->is_sock)
498                 from = "sock";
499         else
500                 from = inet_ntop(AF_INET, (struct sockaddr *) &client->addr.sin_addr, buf, bufsize);
501
502         return from;
503 }
504
505 static void probe_client(struct fio_client *client)
506 {
507         struct cmd_client_probe_pdu pdu;
508         const char *sname;
509         uint64_t tag;
510         char buf[64];
511
512         dprint(FD_NET, "client: send probe\n");
513
514 #ifdef CONFIG_ZLIB
515         pdu.flags = __le64_to_cpu(FIO_PROBE_FLAG_ZLIB);
516 #else
517         pdu.flags = 0;
518 #endif
519
520         sname = server_name(client, buf, sizeof(buf));
521         memset(pdu.server, 0, sizeof(pdu.server));
522         strncpy((char *) pdu.server, sname, sizeof(pdu.server) - 1);
523
524         fio_net_send_cmd(client->fd, FIO_NET_CMD_PROBE, &pdu, sizeof(pdu), &tag, &client->cmd_list);
525 }
526
527 static int fio_client_connect_ip(struct fio_client *client)
528 {
529         struct sockaddr *addr;
530         socklen_t socklen;
531         int fd, domain;
532
533         if (client->ipv6) {
534                 client->addr6.sin6_family = AF_INET6;
535                 client->addr6.sin6_port = htons(client->port);
536                 domain = AF_INET6;
537                 addr = (struct sockaddr *) &client->addr6;
538                 socklen = sizeof(client->addr6);
539         } else {
540                 client->addr.sin_family = AF_INET;
541                 client->addr.sin_port = htons(client->port);
542                 domain = AF_INET;
543                 addr = (struct sockaddr *) &client->addr;
544                 socklen = sizeof(client->addr);
545         }
546
547         fd = socket(domain, SOCK_STREAM, 0);
548         if (fd < 0) {
549                 int ret = -errno;
550
551                 log_err("fio: socket: %s\n", strerror(errno));
552                 return ret;
553         }
554
555         if (connect(fd, addr, socklen) < 0) {
556                 int ret = -errno;
557
558                 log_err("fio: connect: %s\n", strerror(errno));
559                 log_err("fio: failed to connect to %s:%u\n", client->hostname,
560                                                                 client->port);
561                 close(fd);
562                 return ret;
563         }
564
565         return fd;
566 }
567
568 static int fio_client_connect_sock(struct fio_client *client)
569 {
570         struct sockaddr_un *addr = &client->addr_un;
571         socklen_t len;
572         int fd;
573
574         memset(addr, 0, sizeof(*addr));
575         addr->sun_family = AF_UNIX;
576         strncpy(addr->sun_path, client->hostname, sizeof(addr->sun_path) - 1);
577
578         fd = socket(AF_UNIX, SOCK_STREAM, 0);
579         if (fd < 0) {
580                 int ret = -errno;
581
582                 log_err("fio: socket: %s\n", strerror(errno));
583                 return ret;
584         }
585
586         len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
587         if (connect(fd, (struct sockaddr *) addr, len) < 0) {
588                 int ret = -errno;
589
590                 log_err("fio: connect; %s\n", strerror(errno));
591                 close(fd);
592                 return ret;
593         }
594
595         return fd;
596 }
597
598 int fio_client_connect(struct fio_client *client)
599 {
600         int fd;
601
602         dprint(FD_NET, "client: connect to host %s\n", client->hostname);
603
604         if (client->is_sock)
605                 fd = fio_client_connect_sock(client);
606         else
607                 fd = fio_client_connect_ip(client);
608
609         dprint(FD_NET, "client: %s connected %d\n", client->hostname, fd);
610
611         if (fd < 0)
612                 return fd;
613
614         client->fd = fd;
615         fio_client_add_hash(client);
616         client->state = Client_connected;
617
618         probe_client(client);
619         return 0;
620 }
621
622 int fio_client_terminate(struct fio_client *client)
623 {
624         return fio_net_send_quit(client->fd);
625 }
626
627 static void fio_clients_terminate(void)
628 {
629         struct flist_head *entry;
630         struct fio_client *client;
631
632         dprint(FD_NET, "client: terminate clients\n");
633
634         flist_for_each(entry, &client_list) {
635                 client = flist_entry(entry, struct fio_client, list);
636                 fio_client_terminate(client);
637         }
638 }
639
640 static void sig_int(int sig)
641 {
642         dprint(FD_NET, "client: got signal %d\n", sig);
643         fio_clients_terminate();
644 }
645
646 static void client_signal_handler(void)
647 {
648         struct sigaction act;
649
650         memset(&act, 0, sizeof(act));
651         act.sa_handler = sig_int;
652         act.sa_flags = SA_RESTART;
653         sigaction(SIGINT, &act, NULL);
654
655         memset(&act, 0, sizeof(act));
656         act.sa_handler = sig_int;
657         act.sa_flags = SA_RESTART;
658         sigaction(SIGTERM, &act, NULL);
659
660 /* Windows uses SIGBREAK as a quit signal from other applications */
661 #ifdef WIN32
662         memset(&act, 0, sizeof(act));
663         act.sa_handler = sig_int;
664         act.sa_flags = SA_RESTART;
665         sigaction(SIGBREAK, &act, NULL);
666 #endif
667
668         memset(&act, 0, sizeof(act));
669         act.sa_handler = sig_show_status;
670         act.sa_flags = SA_RESTART;
671         sigaction(SIGUSR1, &act, NULL);
672 }
673
674 static int send_client_cmd_line(struct fio_client *client)
675 {
676         struct cmd_single_line_pdu *cslp;
677         struct cmd_line_pdu *clp;
678         unsigned long offset;
679         unsigned int *lens;
680         void *pdu;
681         size_t mem;
682         int i, ret;
683
684         dprint(FD_NET, "client: send cmdline %d\n", client->argc);
685
686         lens = malloc(client->argc * sizeof(unsigned int));
687
688         /*
689          * Find out how much mem we need
690          */
691         for (i = 0, mem = 0; i < client->argc; i++) {
692                 lens[i] = strlen(client->argv[i]) + 1;
693                 mem += lens[i];
694         }
695
696         /*
697          * We need one cmd_line_pdu, and argc number of cmd_single_line_pdu
698          */
699         mem += sizeof(*clp) + (client->argc * sizeof(*cslp));
700
701         pdu = malloc(mem);
702         clp = pdu;
703         offset = sizeof(*clp);
704
705         for (i = 0; i < client->argc; i++) {
706                 uint16_t arg_len = lens[i];
707
708                 cslp = pdu + offset;
709                 strcpy((char *) cslp->text, client->argv[i]);
710                 cslp->len = cpu_to_le16(arg_len);
711                 offset += sizeof(*cslp) + arg_len;
712         }
713
714         free(lens);
715         clp->lines = cpu_to_le16(client->argc);
716         clp->client_type = __cpu_to_le16(client->type);
717         ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOBLINE, pdu, mem, NULL, NULL);
718         free(pdu);
719         return ret;
720 }
721
722 int fio_clients_connect(void)
723 {
724         struct fio_client *client;
725         struct flist_head *entry, *tmp;
726         int ret;
727
728 #ifdef WIN32
729         WSADATA wsd;
730         WSAStartup(MAKEWORD(2, 2), &wsd);
731 #endif
732
733         dprint(FD_NET, "client: connect all\n");
734
735         client_signal_handler();
736
737         flist_for_each_safe(entry, tmp, &client_list) {
738                 client = flist_entry(entry, struct fio_client, list);
739
740                 ret = fio_client_connect(client);
741                 if (ret) {
742                         remove_client(client);
743                         continue;
744                 }
745
746                 if (client->argc > 1)
747                         send_client_cmd_line(client);
748         }
749
750         return !nr_clients;
751 }
752
753 int fio_start_client(struct fio_client *client)
754 {
755         dprint(FD_NET, "client: start %s\n", client->hostname);
756         return fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_RUN, 0, NULL);
757 }
758
759 int fio_start_all_clients(void)
760 {
761         struct fio_client *client;
762         struct flist_head *entry, *tmp;
763         int ret;
764
765         dprint(FD_NET, "client: start all\n");
766
767         fio_client_json_init();
768
769         flist_for_each_safe(entry, tmp, &client_list) {
770                 client = flist_entry(entry, struct fio_client, list);
771
772                 ret = fio_start_client(client);
773                 if (ret) {
774                         remove_client(client);
775                         continue;
776                 }
777         }
778
779         return flist_empty(&client_list);
780 }
781
782 static int __fio_client_send_remote_ini(struct fio_client *client,
783                                         const char *filename)
784 {
785         struct cmd_load_file_pdu *pdu;
786         size_t p_size;
787         int ret;
788
789         dprint(FD_NET, "send remote ini %s to %s\n", filename, client->hostname);
790
791         p_size = sizeof(*pdu) + strlen(filename) + 1;
792         pdu = malloc(p_size);
793         memset(pdu, 0, p_size);
794         pdu->name_len = strlen(filename);
795         strcpy((char *) pdu->file, filename);
796         pdu->client_type = cpu_to_le16((uint16_t) client->type);
797
798         client->sent_job = true;
799         ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_LOAD_FILE, pdu, p_size,NULL, NULL);
800         free(pdu);
801         return ret;
802 }
803
804 /*
805  * Send file contents to server backend. We could use sendfile(), but to remain
806  * more portable lets just read/write the darn thing.
807  */
808 static int __fio_client_send_local_ini(struct fio_client *client,
809                                        const char *filename)
810 {
811         struct cmd_job_pdu *pdu;
812         size_t p_size;
813         struct stat sb;
814         char *p;
815         void *buf;
816         off_t len;
817         int fd, ret;
818
819         dprint(FD_NET, "send ini %s to %s\n", filename, client->hostname);
820
821         fd = open(filename, O_RDONLY);
822         if (fd < 0) {
823                 ret = -errno;
824                 log_err("fio: job file <%s> open: %s\n", filename, strerror(errno));
825                 return ret;
826         }
827
828         if (fstat(fd, &sb) < 0) {
829                 ret = -errno;
830                 log_err("fio: job file stat: %s\n", strerror(errno));
831                 close(fd);
832                 return ret;
833         }
834
835         /*
836          * Add extra space for variable expansion, but doesn't guarantee.
837          */
838         sb.st_size += OPT_LEN_MAX;
839         p_size = sb.st_size + sizeof(*pdu);
840         pdu = malloc(p_size);
841         buf = pdu->buf;
842
843         len = sb.st_size;
844         p = buf;
845         if (read_ini_data(fd, p, len)) {
846                 log_err("fio: failed reading job file %s\n", filename);
847                 close(fd);
848                 free(pdu);
849                 return 1;
850         }
851
852         pdu->buf_len = __cpu_to_le32(sb.st_size);
853         pdu->client_type = cpu_to_le32(client->type);
854
855         client->sent_job = true;
856         ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, pdu, p_size, NULL, NULL);
857         free(pdu);
858         close(fd);
859         return ret;
860 }
861
862 int fio_client_send_ini(struct fio_client *client, const char *filename,
863                         bool remote)
864 {
865         int ret;
866
867         if (!remote)
868                 ret = __fio_client_send_local_ini(client, filename);
869         else
870                 ret = __fio_client_send_remote_ini(client, filename);
871
872         if (!ret)
873                 client->sent_job = true;
874
875         return ret;
876 }
877
878 static int fio_client_send_cf(struct fio_client *client,
879                               struct client_file *cf)
880 {
881         return fio_client_send_ini(client, cf->file, cf->remote);
882 }
883
884 int fio_clients_send_ini(const char *filename)
885 {
886         struct fio_client *client;
887         struct flist_head *entry, *tmp;
888
889         flist_for_each_safe(entry, tmp, &client_list) {
890                 bool failed = false;
891
892                 client = flist_entry(entry, struct fio_client, list);
893
894                 if (client->nr_files) {
895                         int i;
896
897                         for (i = 0; i < client->nr_files; i++) {
898                                 struct client_file *cf;
899
900                                 cf = &client->files[i];
901
902                                 if (fio_client_send_cf(client, cf)) {
903                                         failed = true;
904                                         remove_client(client);
905                                         break;
906                                 }
907                         }
908                 }
909                 if (client->sent_job || failed)
910                         continue;
911                 if (!filename || fio_client_send_ini(client, filename, 0))
912                         remove_client(client);
913         }
914
915         return !nr_clients;
916 }
917
918 int fio_client_update_options(struct fio_client *client,
919                               struct thread_options *o, uint64_t *tag)
920 {
921         struct cmd_add_job_pdu pdu;
922
923         pdu.thread_number = cpu_to_le32(client->thread_number);
924         pdu.groupid = cpu_to_le32(client->groupid);
925         convert_thread_options_to_net(&pdu.top, o);
926
927         return fio_net_send_cmd(client->fd, FIO_NET_CMD_UPDATE_JOB, &pdu, sizeof(pdu), tag, &client->cmd_list);
928 }
929
930 static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
931 {
932         dst->max_val    = le64_to_cpu(src->max_val);
933         dst->min_val    = le64_to_cpu(src->min_val);
934         dst->samples    = le64_to_cpu(src->samples);
935
936         /*
937          * Floats arrive as IEEE 754 encoded uint64_t, convert back to double
938          */
939         dst->mean.u.f   = fio_uint64_to_double(le64_to_cpu(dst->mean.u.i));
940         dst->S.u.f      = fio_uint64_to_double(le64_to_cpu(dst->S.u.i));
941 }
942
943 static void convert_ts(struct thread_stat *dst, struct thread_stat *src)
944 {
945         int i, j;
946
947         dst->error              = le32_to_cpu(src->error);
948         dst->thread_number      = le32_to_cpu(src->thread_number);
949         dst->groupid            = le32_to_cpu(src->groupid);
950         dst->pid                = le32_to_cpu(src->pid);
951         dst->members            = le32_to_cpu(src->members);
952         dst->unified_rw_rep     = le32_to_cpu(src->unified_rw_rep);
953
954         for (i = 0; i < DDIR_RWDIR_CNT; i++) {
955                 convert_io_stat(&dst->clat_stat[i], &src->clat_stat[i]);
956                 convert_io_stat(&dst->slat_stat[i], &src->slat_stat[i]);
957                 convert_io_stat(&dst->lat_stat[i], &src->lat_stat[i]);
958                 convert_io_stat(&dst->bw_stat[i], &src->bw_stat[i]);
959                 convert_io_stat(&dst->iops_stat[i], &src->iops_stat[i]);
960         }
961
962         dst->usr_time           = le64_to_cpu(src->usr_time);
963         dst->sys_time           = le64_to_cpu(src->sys_time);
964         dst->ctx                = le64_to_cpu(src->ctx);
965         dst->minf               = le64_to_cpu(src->minf);
966         dst->majf               = le64_to_cpu(src->majf);
967         dst->clat_percentiles   = le32_to_cpu(src->clat_percentiles);
968         dst->lat_percentiles    = le32_to_cpu(src->lat_percentiles);
969         dst->percentile_precision = le64_to_cpu(src->percentile_precision);
970
971         for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
972                 fio_fp64_t *fps = &src->percentile_list[i];
973                 fio_fp64_t *fpd = &dst->percentile_list[i];
974
975                 fpd->u.f = fio_uint64_to_double(le64_to_cpu(fps->u.i));
976         }
977
978         for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
979                 dst->io_u_map[i]        = le64_to_cpu(src->io_u_map[i]);
980                 dst->io_u_submit[i]     = le64_to_cpu(src->io_u_submit[i]);
981                 dst->io_u_complete[i]   = le64_to_cpu(src->io_u_complete[i]);
982         }
983
984         for (i = 0; i < FIO_IO_U_LAT_N_NR; i++)
985                 dst->io_u_lat_n[i]      = le64_to_cpu(src->io_u_lat_n[i]);
986         for (i = 0; i < FIO_IO_U_LAT_U_NR; i++)
987                 dst->io_u_lat_u[i]      = le64_to_cpu(src->io_u_lat_u[i]);
988         for (i = 0; i < FIO_IO_U_LAT_M_NR; i++)
989                 dst->io_u_lat_m[i]      = le64_to_cpu(src->io_u_lat_m[i]);
990
991         for (i = 0; i < DDIR_RWDIR_CNT; i++)
992                 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
993                         dst->io_u_plat[i][j] = le64_to_cpu(src->io_u_plat[i][j]);
994
995         for (i = 0; i < DDIR_RWDIR_CNT; i++) {
996                 dst->total_io_u[i]      = le64_to_cpu(src->total_io_u[i]);
997                 dst->short_io_u[i]      = le64_to_cpu(src->short_io_u[i]);
998                 dst->drop_io_u[i]       = le64_to_cpu(src->drop_io_u[i]);
999         }
1000
1001         dst->total_submit       = le64_to_cpu(src->total_submit);
1002         dst->total_complete     = le64_to_cpu(src->total_complete);
1003         dst->nr_zone_resets     = le64_to_cpu(src->nr_zone_resets);
1004
1005         for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1006                 dst->io_bytes[i]        = le64_to_cpu(src->io_bytes[i]);
1007                 dst->runtime[i]         = le64_to_cpu(src->runtime[i]);
1008         }
1009
1010         dst->total_run_time     = le64_to_cpu(src->total_run_time);
1011         dst->continue_on_error  = le16_to_cpu(src->continue_on_error);
1012         dst->total_err_count    = le64_to_cpu(src->total_err_count);
1013         dst->first_error        = le32_to_cpu(src->first_error);
1014         dst->kb_base            = le32_to_cpu(src->kb_base);
1015         dst->unit_base          = le32_to_cpu(src->unit_base);
1016
1017         dst->sig_figs           = le32_to_cpu(src->sig_figs);
1018
1019         dst->latency_depth      = le32_to_cpu(src->latency_depth);
1020         dst->latency_target     = le64_to_cpu(src->latency_target);
1021         dst->latency_window     = le64_to_cpu(src->latency_window);
1022         dst->latency_percentile.u.f = fio_uint64_to_double(le64_to_cpu(src->latency_percentile.u.i));
1023
1024         dst->nr_block_infos     = le64_to_cpu(src->nr_block_infos);
1025         for (i = 0; i < dst->nr_block_infos; i++)
1026                 dst->block_infos[i] = le32_to_cpu(src->block_infos[i]);
1027
1028         dst->ss_dur             = le64_to_cpu(src->ss_dur);
1029         dst->ss_state           = le32_to_cpu(src->ss_state);
1030         dst->ss_head            = le32_to_cpu(src->ss_head);
1031         dst->ss_limit.u.f       = fio_uint64_to_double(le64_to_cpu(src->ss_limit.u.i));
1032         dst->ss_slope.u.f       = fio_uint64_to_double(le64_to_cpu(src->ss_slope.u.i));
1033         dst->ss_deviation.u.f   = fio_uint64_to_double(le64_to_cpu(src->ss_deviation.u.i));
1034         dst->ss_criterion.u.f   = fio_uint64_to_double(le64_to_cpu(src->ss_criterion.u.i));
1035
1036         if (dst->ss_state & FIO_SS_DATA) {
1037                 for (i = 0; i < dst->ss_dur; i++ ) {
1038                         dst->ss_iops_data[i] = le64_to_cpu(src->ss_iops_data[i]);
1039                         dst->ss_bw_data[i] = le64_to_cpu(src->ss_bw_data[i]);
1040                 }
1041         }
1042 }
1043
1044 static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
1045 {
1046         int i;
1047
1048         for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1049                 dst->max_run[i]         = le64_to_cpu(src->max_run[i]);
1050                 dst->min_run[i]         = le64_to_cpu(src->min_run[i]);
1051                 dst->max_bw[i]          = le64_to_cpu(src->max_bw[i]);
1052                 dst->min_bw[i]          = le64_to_cpu(src->min_bw[i]);
1053                 dst->iobytes[i]         = le64_to_cpu(src->iobytes[i]);
1054                 dst->agg[i]             = le64_to_cpu(src->agg[i]);
1055         }
1056
1057         dst->kb_base    = le32_to_cpu(src->kb_base);
1058         dst->unit_base  = le32_to_cpu(src->unit_base);
1059         dst->sig_figs   = le32_to_cpu(src->sig_figs);
1060         dst->groupid    = le32_to_cpu(src->groupid);
1061         dst->unified_rw_rep     = le32_to_cpu(src->unified_rw_rep);
1062 }
1063
1064 static void json_object_add_client_info(struct json_object *obj,
1065                                         struct fio_client *client)
1066 {
1067         const char *hostname = client->hostname ? client->hostname : "";
1068
1069         json_object_add_value_string(obj, "hostname", hostname);
1070         json_object_add_value_int(obj, "port", client->port);
1071 }
1072
1073 static void handle_ts(struct fio_client *client, struct fio_net_cmd *cmd)
1074 {
1075         struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
1076         struct flist_head *opt_list = NULL;
1077         struct json_object *tsobj;
1078
1079         if (client->opt_lists && p->ts.thread_number <= client->jobs)
1080                 opt_list = &client->opt_lists[p->ts.thread_number - 1];
1081
1082         tsobj = show_thread_status(&p->ts, &p->rs, opt_list, &client->buf);
1083         client->did_stat = true;
1084         if (tsobj) {
1085                 json_object_add_client_info(tsobj, client);
1086                 json_array_add_value_object(clients_array, tsobj);
1087         }
1088
1089         if (sum_stat_clients <= 1)
1090                 return;
1091
1092         sum_thread_stats(&client_ts, &p->ts, sum_stat_nr == 1);
1093         sum_group_stats(&client_gs, &p->rs);
1094
1095         client_ts.members++;
1096         client_ts.thread_number = p->ts.thread_number;
1097         client_ts.groupid = p->ts.groupid;
1098         client_ts.unified_rw_rep = p->ts.unified_rw_rep;
1099         client_ts.sig_figs = p->ts.sig_figs;
1100
1101         if (++sum_stat_nr == sum_stat_clients) {
1102                 strcpy(client_ts.name, "All clients");
1103                 tsobj = show_thread_status(&client_ts, &client_gs, NULL, &client->buf);
1104                 if (tsobj) {
1105                         json_object_add_client_info(tsobj, client);
1106                         json_array_add_value_object(clients_array, tsobj);
1107                 }
1108         }
1109 }
1110
1111 static void handle_gs(struct fio_client *client, struct fio_net_cmd *cmd)
1112 {
1113         struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
1114
1115         if (output_format & FIO_OUTPUT_NORMAL)
1116                 show_group_stats(gs, &client->buf);
1117 }
1118
1119 static void handle_job_opt(struct fio_client *client, struct fio_net_cmd *cmd)
1120 {
1121         struct cmd_job_option *pdu = (struct cmd_job_option *) cmd->payload;
1122         struct print_option *p;
1123
1124         if (!job_opt_object)
1125                 return;
1126
1127         pdu->global = le16_to_cpu(pdu->global);
1128         pdu->truncated = le16_to_cpu(pdu->truncated);
1129         pdu->groupid = le32_to_cpu(pdu->groupid);
1130
1131         p = malloc(sizeof(*p));
1132         p->name = strdup((char *) pdu->name);
1133         if (pdu->value[0] != '\0')
1134                 p->value = strdup((char *) pdu->value);
1135         else
1136                 p->value = NULL;
1137
1138         if (pdu->global) {
1139                 const char *pos = "";
1140
1141                 if (p->value)
1142                         pos = p->value;
1143
1144                 json_object_add_value_string(job_opt_object, p->name, pos);
1145         } else if (client->opt_lists) {
1146                 struct flist_head *opt_list = &client->opt_lists[pdu->groupid];
1147
1148                 flist_add_tail(&p->list, opt_list);
1149         }
1150 }
1151
1152 static void handle_text(struct fio_client *client, struct fio_net_cmd *cmd)
1153 {
1154         struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
1155         const char *buf = (const char *) pdu->buf;
1156         const char *name;
1157         int fio_unused ret;
1158         struct buf_output out;
1159
1160         buf_output_init(&out);
1161
1162         name = client->name ? client->name : client->hostname;
1163
1164         if (!client->skip_newline && !(output_format & FIO_OUTPUT_TERSE))
1165                 __log_buf(&out, "<%s> ", name);
1166         __log_buf(&out, "%s", buf);
1167         log_info_buf(out.buf, out.buflen);
1168         buf_output_free(&out);
1169         client->skip_newline = strchr(buf, '\n') == NULL;
1170 }
1171
1172 static void convert_agg(struct disk_util_agg *agg)
1173 {
1174         int i;
1175
1176         for (i = 0; i < 2; i++) {
1177                 agg->ios[i]     = le64_to_cpu(agg->ios[i]);
1178                 agg->merges[i]  = le64_to_cpu(agg->merges[i]);
1179                 agg->sectors[i] = le64_to_cpu(agg->sectors[i]);
1180                 agg->ticks[i]   = le64_to_cpu(agg->ticks[i]);
1181         }
1182
1183         agg->io_ticks           = le64_to_cpu(agg->io_ticks);
1184         agg->time_in_queue      = le64_to_cpu(agg->time_in_queue);
1185         agg->slavecount         = le32_to_cpu(agg->slavecount);
1186         agg->max_util.u.f       = fio_uint64_to_double(le64_to_cpu(agg->max_util.u.i));
1187 }
1188
1189 static void convert_dus(struct disk_util_stat *dus)
1190 {
1191         int i;
1192
1193         for (i = 0; i < 2; i++) {
1194                 dus->s.ios[i]           = le64_to_cpu(dus->s.ios[i]);
1195                 dus->s.merges[i]        = le64_to_cpu(dus->s.merges[i]);
1196                 dus->s.sectors[i]       = le64_to_cpu(dus->s.sectors[i]);
1197                 dus->s.ticks[i]         = le64_to_cpu(dus->s.ticks[i]);
1198         }
1199
1200         dus->s.io_ticks         = le64_to_cpu(dus->s.io_ticks);
1201         dus->s.time_in_queue    = le64_to_cpu(dus->s.time_in_queue);
1202         dus->s.msec             = le64_to_cpu(dus->s.msec);
1203 }
1204
1205 static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd)
1206 {
1207         struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
1208
1209         if (!client->disk_stats_shown)
1210                 client->disk_stats_shown = true;
1211
1212         if (output_format & FIO_OUTPUT_JSON) {
1213                 struct json_object *duobj;
1214
1215                 json_array_add_disk_util(&du->dus, &du->agg, du_array);
1216                 duobj = json_array_last_value_object(du_array);
1217                 json_object_add_client_info(duobj, client);
1218         } else if (output_format & FIO_OUTPUT_TERSE)
1219                 print_disk_util(&du->dus, &du->agg, 1, &client->buf);
1220         else if (output_format & FIO_OUTPUT_NORMAL) {
1221                 __log_buf(&client->buf, "\nDisk stats (read/write):\n");
1222                 print_disk_util(&du->dus, &du->agg, 0, &client->buf);
1223         }
1224 }
1225
1226 static void convert_jobs_eta(struct jobs_eta *je)
1227 {
1228         int i;
1229
1230         je->nr_running          = le32_to_cpu(je->nr_running);
1231         je->nr_ramp             = le32_to_cpu(je->nr_ramp);
1232         je->nr_pending          = le32_to_cpu(je->nr_pending);
1233         je->nr_setting_up       = le32_to_cpu(je->nr_setting_up);
1234         je->files_open          = le32_to_cpu(je->files_open);
1235
1236         for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1237                 je->m_rate[i]   = le64_to_cpu(je->m_rate[i]);
1238                 je->t_rate[i]   = le64_to_cpu(je->t_rate[i]);
1239                 je->m_iops[i]   = le32_to_cpu(je->m_iops[i]);
1240                 je->t_iops[i]   = le32_to_cpu(je->t_iops[i]);
1241                 je->rate[i]     = le64_to_cpu(je->rate[i]);
1242                 je->iops[i]     = le32_to_cpu(je->iops[i]);
1243         }
1244
1245         je->elapsed_sec         = le64_to_cpu(je->elapsed_sec);
1246         je->eta_sec             = le64_to_cpu(je->eta_sec);
1247         je->nr_threads          = le32_to_cpu(je->nr_threads);
1248         je->is_pow2             = le32_to_cpu(je->is_pow2);
1249         je->unit_base           = le32_to_cpu(je->unit_base);
1250         je->sig_figs            = le32_to_cpu(je->sig_figs);
1251 }
1252
1253 void fio_client_sum_jobs_eta(struct jobs_eta *dst, struct jobs_eta *je)
1254 {
1255         int i;
1256
1257         dst->nr_running         += je->nr_running;
1258         dst->nr_ramp            += je->nr_ramp;
1259         dst->nr_pending         += je->nr_pending;
1260         dst->nr_setting_up      += je->nr_setting_up;
1261         dst->files_open         += je->files_open;
1262
1263         for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1264                 dst->m_rate[i]  += je->m_rate[i];
1265                 dst->t_rate[i]  += je->t_rate[i];
1266                 dst->m_iops[i]  += je->m_iops[i];
1267                 dst->t_iops[i]  += je->t_iops[i];
1268                 dst->rate[i]    += je->rate[i];
1269                 dst->iops[i]    += je->iops[i];
1270         }
1271
1272         dst->elapsed_sec        += je->elapsed_sec;
1273
1274         if (je->eta_sec > dst->eta_sec)
1275                 dst->eta_sec = je->eta_sec;
1276
1277         dst->nr_threads         += je->nr_threads;
1278
1279         /*
1280          * This wont be correct for multiple strings, but at least it
1281          * works for the basic cases.
1282          */
1283         strcpy((char *) dst->run_str, (char *) je->run_str);
1284 }
1285
1286 static bool remove_reply_cmd(struct fio_client *client, struct fio_net_cmd *cmd)
1287 {
1288         struct fio_net_cmd_reply *reply = NULL;
1289         struct flist_head *entry;
1290
1291         flist_for_each(entry, &client->cmd_list) {
1292                 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
1293
1294                 if (cmd->tag == (uintptr_t) reply)
1295                         break;
1296
1297                 reply = NULL;
1298         }
1299
1300         if (!reply) {
1301                 log_err("fio: client: unable to find matching tag (%llx)\n", (unsigned long long) cmd->tag);
1302                 return false;
1303         }
1304
1305         flist_del(&reply->list);
1306         cmd->tag = reply->saved_tag;
1307         free(reply);
1308         return true;
1309 }
1310
1311 int fio_client_wait_for_reply(struct fio_client *client, uint64_t tag)
1312 {
1313         do {
1314                 struct fio_net_cmd_reply *reply = NULL;
1315                 struct flist_head *entry;
1316
1317                 flist_for_each(entry, &client->cmd_list) {
1318                         reply = flist_entry(entry, struct fio_net_cmd_reply, list);
1319
1320                         if (tag == (uintptr_t) reply)
1321                                 break;
1322
1323                         reply = NULL;
1324                 }
1325
1326                 if (!reply)
1327                         break;
1328
1329                 usleep(1000);
1330         } while (1);
1331
1332         return 0;
1333 }
1334
1335 static void handle_eta(struct fio_client *client, struct fio_net_cmd *cmd)
1336 {
1337         struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
1338         struct client_eta *eta = (struct client_eta *) (uintptr_t) cmd->tag;
1339
1340         dprint(FD_NET, "client: got eta tag %p, %d\n", eta, eta->pending);
1341
1342         assert(client->eta_in_flight == eta);
1343
1344         client->eta_in_flight = NULL;
1345         flist_del_init(&client->eta_list);
1346         client->eta_timeouts = 0;
1347
1348         if (client->ops->jobs_eta)
1349                 client->ops->jobs_eta(client, je);
1350
1351         fio_client_sum_jobs_eta(&eta->eta, je);
1352         fio_client_dec_jobs_eta(eta, client->ops->eta);
1353 }
1354
1355 static void client_flush_hist_samples(FILE *f, int hist_coarseness, void *samples,
1356                                       uint64_t sample_size)
1357 {
1358         struct io_sample *s;
1359         int log_offset;
1360         uint64_t i, j, nr_samples;
1361         struct io_u_plat_entry *entry;
1362         uint64_t *io_u_plat;
1363
1364         int stride = 1 << hist_coarseness;
1365
1366         if (!sample_size)
1367                 return;
1368
1369         s = __get_sample(samples, 0, 0);
1370         log_offset = (s->__ddir & LOG_OFFSET_SAMPLE_BIT) != 0;
1371
1372         nr_samples = sample_size / __log_entry_sz(log_offset);
1373
1374         for (i = 0; i < nr_samples; i++) {
1375
1376                 s = (struct io_sample *)((char *)__get_sample(samples, log_offset, i) +
1377                         i * sizeof(struct io_u_plat_entry));
1378
1379                 entry = s->data.plat_entry;
1380                 io_u_plat = entry->io_u_plat;
1381
1382                 fprintf(f, "%lu, %u, %llu, ", (unsigned long) s->time,
1383                                                 io_sample_ddir(s), (unsigned long long) s->bs);
1384                 for (j = 0; j < FIO_IO_U_PLAT_NR - stride; j += stride) {
1385                         fprintf(f, "%llu, ", (unsigned long long)hist_sum(j, stride, io_u_plat, NULL));
1386                 }
1387                 fprintf(f, "%llu\n", (unsigned long long)
1388                         hist_sum(FIO_IO_U_PLAT_NR - stride, stride, io_u_plat, NULL));
1389
1390         }
1391 }
1392
1393 static int fio_client_handle_iolog(struct fio_client *client,
1394                                    struct fio_net_cmd *cmd)
1395 {
1396         struct cmd_iolog_pdu *pdu = NULL;
1397         bool store_direct;
1398         char *log_pathname = NULL;
1399         int ret = 0;
1400
1401         pdu = convert_iolog(cmd, &store_direct);
1402         if (!pdu) {
1403                 log_err("fio: failed converting IO log\n");
1404                 ret = 1;
1405                 goto out;
1406         }
1407
1408         /* allocate buffer big enough for next sprintf() call */
1409         log_pathname = malloc(10 + strlen((char *)pdu->name) +
1410                         strlen(client->hostname));
1411         if (!log_pathname) {
1412                 log_err("fio: memory allocation of unique pathname failed\n");
1413                 ret = -1;
1414                 goto out;
1415         }
1416         /* generate a unique pathname for the log file using hostname */
1417         sprintf(log_pathname, "%s.%s", pdu->name, client->hostname);
1418
1419         if (store_direct) {
1420                 ssize_t wrote;
1421                 size_t sz;
1422                 int fd;
1423
1424                 fd = open((const char *) log_pathname,
1425                                 O_WRONLY | O_CREAT | O_TRUNC, 0644);
1426                 if (fd < 0) {
1427                         log_err("fio: open log %s: %s\n",
1428                                 log_pathname, strerror(errno));
1429                         ret = 1;
1430                         goto out;
1431                 }
1432
1433                 sz = cmd->pdu_len - sizeof(*pdu);
1434                 wrote = write(fd, pdu->samples, sz);
1435                 close(fd);
1436
1437                 if (wrote != sz) {
1438                         log_err("fio: short write on compressed log\n");
1439                         ret = 1;
1440                         goto out;
1441                 }
1442
1443                 ret = 0;
1444         } else {
1445                 FILE *f;
1446                 f = fopen((const char *) log_pathname, "w");
1447                 if (!f) {
1448                         log_err("fio: fopen log %s : %s\n",
1449                                 log_pathname, strerror(errno));
1450                         ret = 1;
1451                         goto out;
1452                 }
1453
1454                 if (pdu->log_type == IO_LOG_TYPE_HIST) {
1455                         client_flush_hist_samples(f, pdu->log_hist_coarseness, pdu->samples,
1456                                            pdu->nr_samples * sizeof(struct io_sample));
1457                 } else {
1458                         flush_samples(f, pdu->samples,
1459                                         pdu->nr_samples * sizeof(struct io_sample));
1460                 }
1461                 fclose(f);
1462                 ret = 0;
1463         }
1464
1465 out:
1466         if (pdu && pdu != (void *) cmd->payload)
1467                 free(pdu);
1468
1469         if (log_pathname)
1470                 free(log_pathname);
1471
1472         return ret;
1473 }
1474
1475 static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd)
1476 {
1477         struct cmd_probe_reply_pdu *probe = (struct cmd_probe_reply_pdu *) cmd->payload;
1478         const char *os, *arch;
1479         char bit[16];
1480
1481         os = fio_get_os_string(probe->os);
1482         if (!os)
1483                 os = "unknown";
1484
1485         arch = fio_get_arch_string(probe->arch);
1486         if (!arch)
1487                 os = "unknown";
1488
1489         sprintf(bit, "%d-bit", probe->bpp * 8);
1490         probe->flags = le64_to_cpu(probe->flags);
1491
1492         if (output_format & FIO_OUTPUT_NORMAL) {
1493                 log_info("hostname=%s, be=%u, %s, os=%s, arch=%s, fio=%s, flags=%lx\n",
1494                         probe->hostname, probe->bigendian, bit, os, arch,
1495                         probe->fio_version, (unsigned long) probe->flags);
1496         }
1497
1498         if (!client->name)
1499                 client->name = strdup((char *) probe->hostname);
1500 }
1501
1502 static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd)
1503 {
1504         struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
1505
1506         client->state = Client_started;
1507         client->jobs = le32_to_cpu(pdu->jobs);
1508         client->nr_stat = le32_to_cpu(pdu->stat_outputs);
1509
1510         if (client->jobs) {
1511                 int i;
1512
1513                 if (client->opt_lists)
1514                         free(client->opt_lists);
1515
1516                 client->opt_lists = malloc(client->jobs * sizeof(struct flist_head));
1517                 for (i = 0; i < client->jobs; i++)
1518                         INIT_FLIST_HEAD(&client->opt_lists[i]);
1519         }
1520
1521         sum_stat_clients += client->nr_stat;
1522 }
1523
1524 static void handle_stop(struct fio_client *client)
1525 {
1526         if (client->error)
1527                 log_info("client <%s>: exited with error %d\n", client->hostname, client->error);
1528 }
1529
1530 static void convert_stop(struct fio_net_cmd *cmd)
1531 {
1532         struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
1533
1534         pdu->error = le32_to_cpu(pdu->error);
1535 }
1536
1537 static void convert_text(struct fio_net_cmd *cmd)
1538 {
1539         struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
1540
1541         pdu->level      = le32_to_cpu(pdu->level);
1542         pdu->buf_len    = le32_to_cpu(pdu->buf_len);
1543         pdu->log_sec    = le64_to_cpu(pdu->log_sec);
1544         pdu->log_usec   = le64_to_cpu(pdu->log_usec);
1545 }
1546
1547 static struct cmd_iolog_pdu *convert_iolog_gz(struct fio_net_cmd *cmd,
1548                                               struct cmd_iolog_pdu *pdu)
1549 {
1550 #ifdef CONFIG_ZLIB
1551         struct cmd_iolog_pdu *ret;
1552         z_stream stream;
1553         uint64_t nr_samples;
1554         size_t total;
1555         char *p;
1556
1557         stream.zalloc = Z_NULL;
1558         stream.zfree = Z_NULL;
1559         stream.opaque = Z_NULL;
1560         stream.avail_in = 0;
1561         stream.next_in = Z_NULL;
1562
1563         if (inflateInit(&stream) != Z_OK)
1564                 return NULL;
1565
1566         /*
1567          * Get header first, it's not compressed
1568          */
1569         nr_samples = le64_to_cpu(pdu->nr_samples);
1570
1571         if (pdu->log_type == IO_LOG_TYPE_HIST)
1572                 total = nr_samples * (__log_entry_sz(le32_to_cpu(pdu->log_offset)) +
1573                                         sizeof(struct io_u_plat_entry));
1574         else
1575                 total = nr_samples * __log_entry_sz(le32_to_cpu(pdu->log_offset));
1576         ret = malloc(total + sizeof(*pdu));
1577         ret->nr_samples = nr_samples;
1578
1579         memcpy(ret, pdu, sizeof(*pdu));
1580
1581         p = (char *) ret + sizeof(*pdu);
1582
1583         stream.avail_in = cmd->pdu_len - sizeof(*pdu);
1584         stream.next_in = (void *)((char *) pdu + sizeof(*pdu));
1585         while (stream.avail_in) {
1586                 unsigned int this_chunk = 65536;
1587                 unsigned int this_len;
1588                 int err;
1589
1590                 if (this_chunk > total)
1591                         this_chunk = total;
1592
1593                 stream.avail_out = this_chunk;
1594                 stream.next_out = (void *)p;
1595                 err = inflate(&stream, Z_NO_FLUSH);
1596                 /* may be Z_OK, or Z_STREAM_END */
1597                 if (err < 0) {
1598                         log_err("fio: inflate error %d\n", err);
1599                         free(ret);
1600                         ret = NULL;
1601                         goto err;
1602                 }
1603
1604                 this_len = this_chunk - stream.avail_out;
1605                 p += this_len;
1606                 total -= this_len;
1607         }
1608
1609 err:
1610         inflateEnd(&stream);
1611         return ret;
1612 #else
1613         return NULL;
1614 #endif
1615 }
1616
1617 /*
1618  * This has been compressed on the server side, since it can be big.
1619  * Uncompress here.
1620  */
1621 static struct cmd_iolog_pdu *convert_iolog(struct fio_net_cmd *cmd,
1622                                            bool *store_direct)
1623 {
1624         struct cmd_iolog_pdu *pdu = (struct cmd_iolog_pdu *) cmd->payload;
1625         struct cmd_iolog_pdu *ret;
1626         uint64_t i;
1627         int compressed;
1628         void *samples;
1629
1630         *store_direct = false;
1631
1632         /*
1633          * Convert if compressed and we support it. If it's not
1634          * compressed, we need not do anything.
1635          */
1636         compressed = le32_to_cpu(pdu->compressed);
1637         if (compressed == XMIT_COMPRESSED) {
1638 #ifndef CONFIG_ZLIB
1639                 log_err("fio: server sent compressed data by mistake\n");
1640                 return NULL;
1641 #endif
1642                 ret = convert_iolog_gz(cmd, pdu);
1643                 if (!ret) {
1644                         log_err("fio: failed decompressing log\n");
1645                         return NULL;
1646                 }
1647         } else if (compressed == STORE_COMPRESSED) {
1648                 *store_direct = true;
1649                 ret = pdu;
1650         } else
1651                 ret = pdu;
1652
1653         ret->nr_samples         = le64_to_cpu(ret->nr_samples);
1654         ret->thread_number      = le32_to_cpu(ret->thread_number);
1655         ret->log_type           = le32_to_cpu(ret->log_type);
1656         ret->compressed         = le32_to_cpu(ret->compressed);
1657         ret->log_offset         = le32_to_cpu(ret->log_offset);
1658         ret->log_hist_coarseness = le32_to_cpu(ret->log_hist_coarseness);
1659
1660         if (*store_direct)
1661                 return ret;
1662
1663         samples = &ret->samples[0];
1664         for (i = 0; i < ret->nr_samples; i++) {
1665                 struct io_sample *s;
1666
1667                 s = __get_sample(samples, ret->log_offset, i);
1668                 if (ret->log_type == IO_LOG_TYPE_HIST)
1669                         s = (struct io_sample *)((char *)s + sizeof(struct io_u_plat_entry) * i);
1670
1671                 s->time         = le64_to_cpu(s->time);
1672                 s->data.val     = le64_to_cpu(s->data.val);
1673                 s->__ddir       = le32_to_cpu(s->__ddir);
1674                 s->bs           = le64_to_cpu(s->bs);
1675
1676                 if (ret->log_offset) {
1677                         struct io_sample_offset *so = (void *) s;
1678
1679                         so->offset = le64_to_cpu(so->offset);
1680                 }
1681
1682                 if (ret->log_type == IO_LOG_TYPE_HIST) {
1683                         s->data.plat_entry = (struct io_u_plat_entry *)(((char *)s) + sizeof(*s));
1684                         s->data.plat_entry->list.next = NULL;
1685                         s->data.plat_entry->list.prev = NULL;
1686                 }
1687         }
1688
1689         return ret;
1690 }
1691
1692 static void sendfile_reply(int fd, struct cmd_sendfile_reply *rep,
1693                            size_t size, uint64_t tag)
1694 {
1695         rep->error = cpu_to_le32(rep->error);
1696         fio_net_send_cmd(fd, FIO_NET_CMD_SENDFILE, rep, size, &tag, NULL);
1697 }
1698
1699 static int fio_send_file(struct fio_client *client, struct cmd_sendfile *pdu,
1700                          uint64_t tag)
1701 {
1702         struct cmd_sendfile_reply *rep;
1703         struct stat sb;
1704         size_t size;
1705         int fd;
1706
1707         size = sizeof(*rep);
1708         rep = malloc(size);
1709
1710         if (stat((char *)pdu->path, &sb) < 0) {
1711 fail:
1712                 rep->error = errno;
1713                 sendfile_reply(client->fd, rep, size, tag);
1714                 free(rep);
1715                 return 1;
1716         }
1717
1718         size += sb.st_size;
1719         rep = realloc(rep, size);
1720         rep->size = cpu_to_le32((uint32_t) sb.st_size);
1721
1722         fd = open((char *)pdu->path, O_RDONLY);
1723         if (fd == -1 )
1724                 goto fail;
1725
1726         rep->error = read_data(fd, &rep->data, sb.st_size);
1727         sendfile_reply(client->fd, rep, size, tag);
1728         free(rep);
1729         close(fd);
1730         return 0;
1731 }
1732
1733 int fio_handle_client(struct fio_client *client)
1734 {
1735         struct client_ops *ops = client->ops;
1736         struct fio_net_cmd *cmd;
1737         int size;
1738
1739         dprint(FD_NET, "client: handle %s\n", client->hostname);
1740
1741         cmd = fio_net_recv_cmd(client->fd, true);
1742         if (!cmd)
1743                 return 0;
1744
1745         dprint(FD_NET, "client: got cmd op %s from %s (pdu=%u)\n",
1746                 fio_server_op(cmd->opcode), client->hostname, cmd->pdu_len);
1747
1748         client->last_cmd = cmd->opcode;
1749
1750         switch (cmd->opcode) {
1751         case FIO_NET_CMD_QUIT:
1752                 if (ops->quit)
1753                         ops->quit(client, cmd);
1754                 remove_client(client);
1755                 break;
1756         case FIO_NET_CMD_TEXT:
1757                 convert_text(cmd);
1758                 ops->text(client, cmd);
1759                 break;
1760         case FIO_NET_CMD_DU: {
1761                 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
1762
1763                 convert_dus(&du->dus);
1764                 convert_agg(&du->agg);
1765
1766                 ops->disk_util(client, cmd);
1767                 break;
1768                 }
1769         case FIO_NET_CMD_TS: {
1770                 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
1771
1772                 dprint(FD_NET, "client: ts->ss_state = %u\n", (unsigned int) le32_to_cpu(p->ts.ss_state));
1773                 if (le32_to_cpu(p->ts.ss_state) & FIO_SS_DATA) {
1774                         dprint(FD_NET, "client: received steadystate ring buffers\n");
1775
1776                         size = le64_to_cpu(p->ts.ss_dur);
1777                         p->ts.ss_iops_data = (uint64_t *) ((struct cmd_ts_pdu *)cmd->payload + 1);
1778                         p->ts.ss_bw_data = p->ts.ss_iops_data + size;
1779                 }
1780
1781                 convert_ts(&p->ts, &p->ts);
1782                 convert_gs(&p->rs, &p->rs);
1783
1784                 ops->thread_status(client, cmd);
1785                 break;
1786                 }
1787         case FIO_NET_CMD_GS: {
1788                 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
1789
1790                 convert_gs(gs, gs);
1791
1792                 ops->group_stats(client, cmd);
1793                 break;
1794                 }
1795         case FIO_NET_CMD_ETA: {
1796                 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
1797
1798                 if (!remove_reply_cmd(client, cmd))
1799                         break;
1800                 convert_jobs_eta(je);
1801                 handle_eta(client, cmd);
1802                 break;
1803                 }
1804         case FIO_NET_CMD_PROBE:
1805                 remove_reply_cmd(client, cmd);
1806                 ops->probe(client, cmd);
1807                 break;
1808         case FIO_NET_CMD_SERVER_START:
1809                 client->state = Client_running;
1810                 if (ops->job_start)
1811                         ops->job_start(client, cmd);
1812                 break;
1813         case FIO_NET_CMD_START: {
1814                 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
1815
1816                 pdu->jobs = le32_to_cpu(pdu->jobs);
1817                 ops->start(client, cmd);
1818                 break;
1819                 }
1820         case FIO_NET_CMD_STOP: {
1821                 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
1822
1823                 convert_stop(cmd);
1824                 client->state = Client_stopped;
1825                 client->error = le32_to_cpu(pdu->error);
1826                 client->signal = le32_to_cpu(pdu->signal);
1827                 ops->stop(client);
1828                 break;
1829                 }
1830         case FIO_NET_CMD_ADD_JOB: {
1831                 struct cmd_add_job_pdu *pdu = (struct cmd_add_job_pdu *) cmd->payload;
1832
1833                 client->thread_number = le32_to_cpu(pdu->thread_number);
1834                 client->groupid = le32_to_cpu(pdu->groupid);
1835
1836                 if (ops->add_job)
1837                         ops->add_job(client, cmd);
1838                 break;
1839                 }
1840         case FIO_NET_CMD_IOLOG:
1841                 fio_client_handle_iolog(client, cmd);
1842                 break;
1843         case FIO_NET_CMD_UPDATE_JOB:
1844                 ops->update_job(client, cmd);
1845                 remove_reply_cmd(client, cmd);
1846                 break;
1847         case FIO_NET_CMD_VTRIGGER: {
1848                 struct all_io_list *pdu = (struct all_io_list *) cmd->payload;
1849                 char buf[128];
1850                 int off = 0;
1851
1852                 if (aux_path) {
1853                         strcpy(buf, aux_path);
1854                         off = strlen(buf);
1855                 }
1856
1857                 __verify_save_state(pdu, server_name(client, &buf[off], sizeof(buf) - off));
1858                 exec_trigger(trigger_cmd);
1859                 break;
1860                 }
1861         case FIO_NET_CMD_SENDFILE: {
1862                 struct cmd_sendfile *pdu = (struct cmd_sendfile *) cmd->payload;
1863                 fio_send_file(client, pdu, cmd->tag);
1864                 break;
1865                 }
1866         case FIO_NET_CMD_JOB_OPT: {
1867                 handle_job_opt(client, cmd);
1868                 break;
1869         }
1870         default:
1871                 log_err("fio: unknown client op: %s\n", fio_server_op(cmd->opcode));
1872                 break;
1873         }
1874
1875         free(cmd);
1876         return 1;
1877 }
1878
1879 int fio_clients_send_trigger(const char *cmd)
1880 {
1881         struct flist_head *entry;
1882         struct fio_client *client;
1883         size_t slen;
1884
1885         dprint(FD_NET, "client: send vtrigger: %s\n", cmd);
1886
1887         if (!cmd)
1888                 slen = 0;
1889         else
1890                 slen = strlen(cmd);
1891
1892         flist_for_each(entry, &client_list) {
1893                 struct cmd_vtrigger_pdu *pdu;
1894
1895                 client = flist_entry(entry, struct fio_client, list);
1896
1897                 pdu = malloc(sizeof(*pdu) + slen);
1898                 pdu->len = cpu_to_le16((uint16_t) slen);
1899                 if (slen)
1900                         memcpy(pdu->cmd, cmd, slen);
1901                 fio_net_send_cmd(client->fd, FIO_NET_CMD_VTRIGGER, pdu,
1902                                         sizeof(*pdu) + slen, NULL, NULL);
1903                 free(pdu);
1904         }
1905
1906         return 0;
1907 }
1908
1909 static void request_client_etas(struct client_ops *ops)
1910 {
1911         struct fio_client *client;
1912         struct flist_head *entry;
1913         struct client_eta *eta;
1914         int skipped = 0;
1915
1916         if (eta_print == FIO_ETA_NEVER)
1917                 return;
1918
1919         dprint(FD_NET, "client: request eta (%d)\n", nr_clients);
1920
1921         eta = calloc(1, sizeof(*eta) + __THREAD_RUNSTR_SZ(REAL_MAX_JOBS));
1922         eta->pending = nr_clients;
1923
1924         flist_for_each(entry, &client_list) {
1925                 client = flist_entry(entry, struct fio_client, list);
1926
1927                 if (!flist_empty(&client->eta_list)) {
1928                         skipped++;
1929                         continue;
1930                 }
1931                 if (client->state != Client_running)
1932                         continue;
1933
1934                 assert(!client->eta_in_flight);
1935                 flist_add_tail(&client->eta_list, &eta_list);
1936                 client->eta_in_flight = eta;
1937                 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_SEND_ETA,
1938                                         (uintptr_t) eta, &client->cmd_list);
1939         }
1940
1941         while (skipped--) {
1942                 if (!fio_client_dec_jobs_eta(eta, ops->eta))
1943                         break;
1944         }
1945
1946         dprint(FD_NET, "client: requested eta tag %p\n", eta);
1947 }
1948
1949 /*
1950  * A single SEND_ETA timeout isn't fatal. Attempt to recover.
1951  */
1952 static int handle_cmd_timeout(struct fio_client *client,
1953                               struct fio_net_cmd_reply *reply)
1954 {
1955         uint16_t reply_opcode = reply->opcode;
1956
1957         flist_del(&reply->list);
1958         free(reply);
1959
1960         if (reply_opcode != FIO_NET_CMD_SEND_ETA)
1961                 return 1;
1962
1963         log_info("client <%s>: timeout on SEND_ETA\n", client->hostname);
1964
1965         flist_del_init(&client->eta_list);
1966         if (client->eta_in_flight) {
1967                 fio_client_dec_jobs_eta(client->eta_in_flight, client->ops->eta);
1968                 client->eta_in_flight = NULL;
1969         }
1970
1971         /*
1972          * If we fail 5 in a row, give up...
1973          */
1974         if (client->eta_timeouts++ > 5)
1975                 return 1;
1976
1977         return 0;
1978 }
1979
1980 static int client_check_cmd_timeout(struct fio_client *client,
1981                                     struct timespec *now)
1982 {
1983         struct fio_net_cmd_reply *reply;
1984         struct flist_head *entry, *tmp;
1985         int ret = 0;
1986
1987         flist_for_each_safe(entry, tmp, &client->cmd_list) {
1988                 unsigned int op;
1989
1990                 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
1991
1992                 if (mtime_since(&reply->ts, now) < FIO_NET_CLIENT_TIMEOUT)
1993                         continue;
1994
1995                 op = reply->opcode;
1996                 if (!handle_cmd_timeout(client, reply))
1997                         continue;
1998
1999                 log_err("fio: client %s, timeout on cmd %s\n", client->hostname,
2000                                                 fio_server_op(op));
2001                 ret = 1;
2002         }
2003
2004         return flist_empty(&client->cmd_list) && ret;
2005 }
2006
2007 static int fio_check_clients_timed_out(void)
2008 {
2009         struct fio_client *client;
2010         struct flist_head *entry, *tmp;
2011         struct timespec ts;
2012         int ret = 0;
2013
2014         fio_gettime(&ts, NULL);
2015
2016         flist_for_each_safe(entry, tmp, &client_list) {
2017                 client = flist_entry(entry, struct fio_client, list);
2018
2019                 if (flist_empty(&client->cmd_list))
2020                         continue;
2021
2022                 if (!client_check_cmd_timeout(client, &ts))
2023                         continue;
2024
2025                 if (client->ops->timed_out)
2026                         client->ops->timed_out(client);
2027                 else
2028                         log_err("fio: client %s timed out\n", client->hostname);
2029
2030                 if (client->last_cmd != FIO_NET_CMD_VTRIGGER)
2031                         client->error = ETIMEDOUT;
2032                 else
2033                         log_info("fio: ignoring timeout due to vtrigger\n");
2034                 remove_client(client);
2035                 ret = 1;
2036         }
2037
2038         return ret;
2039 }
2040
2041 int fio_handle_clients(struct client_ops *ops)
2042 {
2043         struct pollfd *pfds;
2044         int i, ret = 0, retval = 0;
2045
2046         fio_gettime(&eta_ts, NULL);
2047
2048         pfds = malloc(nr_clients * sizeof(struct pollfd));
2049
2050         init_thread_stat(&client_ts);
2051         init_group_run_stat(&client_gs);
2052
2053         while (!exit_backend && nr_clients) {
2054                 struct flist_head *entry, *tmp;
2055                 struct fio_client *client;
2056
2057                 i = 0;
2058                 flist_for_each_safe(entry, tmp, &client_list) {
2059                         client = flist_entry(entry, struct fio_client, list);
2060
2061                         if (!client->sent_job && !client->ops->stay_connected &&
2062                             flist_empty(&client->cmd_list)) {
2063                                 remove_client(client);
2064                                 continue;
2065                         }
2066
2067                         pfds[i].fd = client->fd;
2068                         pfds[i].events = POLLIN;
2069                         i++;
2070                 }
2071
2072                 if (!nr_clients)
2073                         break;
2074
2075                 assert(i == nr_clients);
2076
2077                 do {
2078                         struct timespec ts;
2079                         int timeout;
2080
2081                         fio_gettime(&ts, NULL);
2082                         if (eta_time_within_slack(mtime_since(&eta_ts, &ts))) {
2083                                 request_client_etas(ops);
2084                                 memcpy(&eta_ts, &ts, sizeof(ts));
2085
2086                                 if (fio_check_clients_timed_out())
2087                                         break;
2088                         }
2089
2090                         check_trigger_file();
2091
2092                         timeout = min(100u, ops->eta_msec);
2093
2094                         ret = poll(pfds, nr_clients, timeout);
2095                         if (ret < 0) {
2096                                 if (errno == EINTR)
2097                                         continue;
2098                                 log_err("fio: poll clients: %s\n", strerror(errno));
2099                                 break;
2100                         } else if (!ret)
2101                                 continue;
2102                 } while (ret <= 0);
2103
2104                 for (i = 0; i < nr_clients; i++) {
2105                         if (!(pfds[i].revents & POLLIN))
2106                                 continue;
2107
2108                         client = find_client_by_fd(pfds[i].fd);
2109                         if (!client) {
2110                                 log_err("fio: unknown client fd %ld\n", (long) pfds[i].fd);
2111                                 continue;
2112                         }
2113                         if (!fio_handle_client(client)) {
2114                                 log_info("client: host=%s disconnected\n",
2115                                                 client->hostname);
2116                                 remove_client(client);
2117                                 retval = 1;
2118                         } else if (client->error)
2119                                 retval = 1;
2120                         fio_put_client(client);
2121                 }
2122         }
2123
2124         fio_client_json_fini();
2125
2126         free(pfds);
2127         return retval || error_clients;
2128 }
2129
2130 static void client_display_thread_status(struct jobs_eta *je)
2131 {
2132         if (!(output_format & FIO_OUTPUT_JSON))
2133                 display_thread_status(je);
2134 }