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