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