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