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