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