axmap: random maps are private, don't get them from smalloc
[fio.git] / client.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <limits.h>
5 #include <errno.h>
6 #include <fcntl.h>
7 #include <sys/poll.h>
8 #include <sys/types.h>
9 #include <sys/stat.h>
10 #include <sys/wait.h>
11 #include <sys/socket.h>
12 #include <sys/un.h>
13 #include <netinet/in.h>
14 #include <arpa/inet.h>
15 #include <netdb.h>
16 #include <signal.h>
17 #ifdef CONFIG_ZLIB
18 #include <zlib.h>
19 #endif
20
21 #include "fio.h"
22 #include "client.h"
23 #include "server.h"
24 #include "flist.h"
25 #include "hash.h"
26 #include "verify.h"
27
28 static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd);
29 static void handle_ts(struct fio_client *client, struct fio_net_cmd *cmd);
30 static void handle_gs(struct fio_client *client, struct fio_net_cmd *cmd);
31 static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd);
32 static void handle_text(struct fio_client *client, struct fio_net_cmd *cmd);
33 static void handle_stop(struct fio_client *client, struct fio_net_cmd *cmd);
34 static void handle_start(struct fio_client *client, 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 timeval eta_tv;
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_array *clients_array = NULL;
63 static struct json_array *du_array = NULL;
64
65 #define FIO_CLIENT_HASH_BITS    7
66 #define FIO_CLIENT_HASH_SZ      (1 << FIO_CLIENT_HASH_BITS)
67 #define FIO_CLIENT_HASH_MASK    (FIO_CLIENT_HASH_SZ - 1)
68 static struct flist_head client_hash[FIO_CLIENT_HASH_SZ];
69
70 static void fio_client_add_hash(struct fio_client *client)
71 {
72         int bucket = hash_long(client->fd, FIO_CLIENT_HASH_BITS);
73
74         bucket &= FIO_CLIENT_HASH_MASK;
75         flist_add(&client->hash_list, &client_hash[bucket]);
76 }
77
78 static void fio_client_remove_hash(struct fio_client *client)
79 {
80         if (!flist_empty(&client->hash_list))
81                 flist_del_init(&client->hash_list);
82 }
83
84 static void fio_init fio_client_hash_init(void)
85 {
86         int i;
87
88         for (i = 0; i < FIO_CLIENT_HASH_SZ; i++)
89                 INIT_FLIST_HEAD(&client_hash[i]);
90 }
91
92 static int read_data(int fd, void *data, size_t size)
93 {
94         ssize_t ret;
95
96         while (size) {
97                 ret = read(fd, data, size);
98                 if (ret < 0) {
99                         if (errno == EAGAIN || errno == EINTR)
100                                 continue;
101                         break;
102                 } else if (!ret)
103                         break;
104                 else {
105                         data += ret;
106                         size -= ret;
107                 }
108         }
109
110         if (size)
111                 return EAGAIN;
112
113         return 0;
114 }
115
116 static void fio_client_json_init(void)
117 {
118         if (output_format != FIO_OUTPUT_JSON)
119                 return;
120         root = json_create_object();
121         json_object_add_value_string(root, "fio version", fio_version_string);
122         clients_array = json_create_array();
123         json_object_add_value_array(root, "client_stats", clients_array);
124         du_array = json_create_array();
125         json_object_add_value_array(root, "disk_util", du_array);
126 }
127
128 static void fio_client_json_fini(void)
129 {
130         if (output_format != FIO_OUTPUT_JSON)
131                 return;
132         json_print_object(root);
133         log_info("\n");
134         json_free_object(root);
135         root = NULL;
136         clients_array = NULL;
137         du_array = NULL;
138 }
139
140 static struct fio_client *find_client_by_fd(int fd)
141 {
142         int bucket = hash_long(fd, FIO_CLIENT_HASH_BITS) & FIO_CLIENT_HASH_MASK;
143         struct fio_client *client;
144         struct flist_head *entry;
145
146         flist_for_each(entry, &client_hash[bucket]) {
147                 client = flist_entry(entry, struct fio_client, hash_list);
148
149                 if (client->fd == fd) {
150                         client->refs++;
151                         return client;
152                 }
153         }
154
155         return NULL;
156 }
157
158 void fio_put_client(struct fio_client *client)
159 {
160         if (--client->refs)
161                 return;
162
163         free(client->hostname);
164         if (client->argv)
165                 free(client->argv);
166         if (client->name)
167                 free(client->name);
168         while (client->nr_files) {
169                 struct client_file *cf = &client->files[--client->nr_files];
170
171                 free(cf->file);
172         }
173         if (client->files)
174                 free(client->files);
175
176         if (!client->did_stat)
177                 sum_stat_clients--;
178
179         free(client);
180 }
181
182 static void remove_client(struct fio_client *client)
183 {
184         assert(client->refs);
185
186         dprint(FD_NET, "client: removed <%s>\n", client->hostname);
187
188         if (!flist_empty(&client->list))
189                 flist_del_init(&client->list);
190
191         fio_client_remove_hash(client);
192
193         if (!flist_empty(&client->eta_list)) {
194                 flist_del_init(&client->eta_list);
195                 fio_client_dec_jobs_eta(client->eta_in_flight, client->ops->eta);
196         }
197
198         close(client->fd);
199         client->fd = -1;
200
201         if (client->ops->removed)
202                 client->ops->removed(client);
203
204         nr_clients--;
205         fio_put_client(client);
206 }
207
208 struct fio_client *fio_get_client(struct fio_client *client)
209 {
210         client->refs++;
211         return client;
212 }
213
214 static void __fio_client_add_cmd_option(struct fio_client *client,
215                                         const char *opt)
216 {
217         int index;
218
219         index = client->argc++;
220         client->argv = realloc(client->argv, sizeof(char *) * client->argc);
221         client->argv[index] = strdup(opt);
222         dprint(FD_NET, "client: add cmd %d: %s\n", index, opt);
223 }
224
225 void fio_client_add_cmd_option(void *cookie, const char *opt)
226 {
227         struct fio_client *client = cookie;
228         struct flist_head *entry;
229
230         if (!client || !opt)
231                 return;
232
233         __fio_client_add_cmd_option(client, opt);
234
235         /*
236          * Duplicate arguments to shared client group
237          */
238         flist_for_each(entry, &arg_list) {
239                 client = flist_entry(entry, struct fio_client, arg_list);
240
241                 __fio_client_add_cmd_option(client, opt);
242         }
243 }
244
245 struct fio_client *fio_client_add_explicit(struct client_ops *ops,
246                                            const char *hostname, int type,
247                                            int port)
248 {
249         struct fio_client *client;
250
251         client = malloc(sizeof(*client));
252         memset(client, 0, sizeof(*client));
253
254         INIT_FLIST_HEAD(&client->list);
255         INIT_FLIST_HEAD(&client->hash_list);
256         INIT_FLIST_HEAD(&client->arg_list);
257         INIT_FLIST_HEAD(&client->eta_list);
258         INIT_FLIST_HEAD(&client->cmd_list);
259
260         client->hostname = strdup(hostname);
261
262         if (type == Fio_client_socket)
263                 client->is_sock = 1;
264         else {
265                 int ipv6;
266
267                 ipv6 = type == Fio_client_ipv6;
268                 if (fio_server_parse_host(hostname, ipv6,
269                                                 &client->addr.sin_addr,
270                                                 &client->addr6.sin6_addr))
271                         goto err;
272
273                 client->port = port;
274         }
275
276         client->fd = -1;
277         client->ops = ops;
278         client->refs = 1;
279         client->type = ops->client_type;
280
281         __fio_client_add_cmd_option(client, "fio");
282
283         flist_add(&client->list, &client_list);
284         nr_clients++;
285         dprint(FD_NET, "client: added <%s>\n", client->hostname);
286         return client;
287 err:
288         free(client);
289         return NULL;
290 }
291
292 int fio_client_add_ini_file(void *cookie, const char *ini_file, int remote)
293 {
294         struct fio_client *client = cookie;
295         struct client_file *cf;
296         size_t new_size;
297         void *new_files;
298
299         if (!client)
300                 return 1;
301
302         dprint(FD_NET, "client <%s>: add ini %s\n", client->hostname, ini_file);
303
304         new_size = (client->nr_files + 1) * sizeof(struct client_file);
305         new_files = realloc(client->files, new_size);
306         if (!new_files)
307                 return 1;
308
309         client->files = new_files;
310         cf = &client->files[client->nr_files];
311         cf->file = strdup(ini_file);
312         cf->remote = remote;
313         client->nr_files++;
314         return 0;
315 }
316
317 int fio_client_add(struct client_ops *ops, const char *hostname, void **cookie)
318 {
319         struct fio_client *existing = *cookie;
320         struct fio_client *client;
321
322         if (existing) {
323                 /*
324                  * We always add our "exec" name as the option, hence 1
325                  * means empty.
326                  */
327                 if (existing->argc == 1)
328                         flist_add_tail(&existing->arg_list, &arg_list);
329                 else {
330                         while (!flist_empty(&arg_list))
331                                 flist_del_init(arg_list.next);
332                 }
333         }
334
335         client = malloc(sizeof(*client));
336         memset(client, 0, sizeof(*client));
337
338         INIT_FLIST_HEAD(&client->list);
339         INIT_FLIST_HEAD(&client->hash_list);
340         INIT_FLIST_HEAD(&client->arg_list);
341         INIT_FLIST_HEAD(&client->eta_list);
342         INIT_FLIST_HEAD(&client->cmd_list);
343
344         if (fio_server_parse_string(hostname, &client->hostname,
345                                         &client->is_sock, &client->port,
346                                         &client->addr.sin_addr,
347                                         &client->addr6.sin6_addr,
348                                         &client->ipv6))
349                 return -1;
350
351         client->fd = -1;
352         client->ops = ops;
353         client->refs = 1;
354         client->type = ops->client_type;
355
356         __fio_client_add_cmd_option(client, "fio");
357
358         flist_add(&client->list, &client_list);
359         nr_clients++;
360         dprint(FD_NET, "client: added <%s>\n", client->hostname);
361         *cookie = client;
362         return 0;
363 }
364
365 static const char *server_name(struct fio_client *client, char *buf,
366                                size_t bufsize)
367 {
368         const char *from;
369
370         if (client->ipv6)
371                 from = inet_ntop(AF_INET6, (struct sockaddr *) &client->addr6.sin6_addr, buf, bufsize);
372         else if (client->is_sock)
373                 from = "sock";
374         else
375                 from = inet_ntop(AF_INET, (struct sockaddr *) &client->addr.sin_addr, buf, bufsize);
376
377         return from;
378 }
379
380 static void probe_client(struct fio_client *client)
381 {
382         struct cmd_client_probe_pdu pdu;
383         const char *sname;
384         uint64_t tag;
385         char buf[64];
386
387         dprint(FD_NET, "client: send probe\n");
388
389 #ifdef CONFIG_ZLIB
390         pdu.flags = __le64_to_cpu(FIO_PROBE_FLAG_ZLIB);
391 #else
392         pdu.flags = 0;
393 #endif
394
395         sname = server_name(client, buf, sizeof(buf));
396         memset(pdu.server, 0, sizeof(pdu.server));
397         strncpy((char *) pdu.server, sname, sizeof(pdu.server) - 1);
398
399         fio_net_send_cmd(client->fd, FIO_NET_CMD_PROBE, &pdu, sizeof(pdu), &tag, &client->cmd_list);
400 }
401
402 static int fio_client_connect_ip(struct fio_client *client)
403 {
404         struct sockaddr *addr;
405         socklen_t socklen;
406         int fd, domain;
407
408         if (client->ipv6) {
409                 client->addr6.sin6_family = AF_INET6;
410                 client->addr6.sin6_port = htons(client->port);
411                 domain = AF_INET6;
412                 addr = (struct sockaddr *) &client->addr6;
413                 socklen = sizeof(client->addr6);
414         } else {
415                 client->addr.sin_family = AF_INET;
416                 client->addr.sin_port = htons(client->port);
417                 domain = AF_INET;
418                 addr = (struct sockaddr *) &client->addr;
419                 socklen = sizeof(client->addr);
420         }
421
422         fd = socket(domain, SOCK_STREAM, 0);
423         if (fd < 0) {
424                 int ret = -errno;
425
426                 log_err("fio: socket: %s\n", strerror(errno));
427                 return ret;
428         }
429
430         if (connect(fd, addr, socklen) < 0) {
431                 int ret = -errno;
432
433                 log_err("fio: connect: %s\n", strerror(errno));
434                 log_err("fio: failed to connect to %s:%u\n", client->hostname,
435                                                                 client->port);
436                 close(fd);
437                 return ret;
438         }
439
440         return fd;
441 }
442
443 static int fio_client_connect_sock(struct fio_client *client)
444 {
445         struct sockaddr_un *addr = &client->addr_un;
446         socklen_t len;
447         int fd;
448
449         memset(addr, 0, sizeof(*addr));
450         addr->sun_family = AF_UNIX;
451         strncpy(addr->sun_path, client->hostname, sizeof(addr->sun_path) - 1);
452
453         fd = socket(AF_UNIX, SOCK_STREAM, 0);
454         if (fd < 0) {
455                 int ret = -errno;
456
457                 log_err("fio: socket: %s\n", strerror(errno));
458                 return ret;
459         }
460
461         len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
462         if (connect(fd, (struct sockaddr *) addr, len) < 0) {
463                 int ret = -errno;
464
465                 log_err("fio: connect; %s\n", strerror(errno));
466                 close(fd);
467                 return ret;
468         }
469
470         return fd;
471 }
472
473 int fio_client_connect(struct fio_client *client)
474 {
475         int fd;
476
477         dprint(FD_NET, "client: connect to host %s\n", client->hostname);
478
479         if (client->is_sock)
480                 fd = fio_client_connect_sock(client);
481         else
482                 fd = fio_client_connect_ip(client);
483
484         dprint(FD_NET, "client: %s connected %d\n", client->hostname, fd);
485
486         if (fd < 0)
487                 return fd;
488
489         client->fd = fd;
490         fio_client_add_hash(client);
491         client->state = Client_connected;
492
493         probe_client(client);
494         return 0;
495 }
496
497 int fio_client_terminate(struct fio_client *client)
498 {
499         return fio_net_send_quit(client->fd);
500 }
501
502 void fio_clients_terminate(void)
503 {
504         struct flist_head *entry;
505         struct fio_client *client;
506
507         dprint(FD_NET, "client: terminate clients\n");
508
509         flist_for_each(entry, &client_list) {
510                 client = flist_entry(entry, struct fio_client, list);
511                 fio_client_terminate(client);
512         }
513 }
514
515 static void sig_int(int sig)
516 {
517         dprint(FD_NET, "client: got signal %d\n", sig);
518         fio_clients_terminate();
519 }
520
521 static void sig_show_status(int sig)
522 {
523         show_running_run_stats();
524 }
525
526 static void client_signal_handler(void)
527 {
528         struct sigaction act;
529
530         memset(&act, 0, sizeof(act));
531         act.sa_handler = sig_int;
532         act.sa_flags = SA_RESTART;
533         sigaction(SIGINT, &act, NULL);
534
535         memset(&act, 0, sizeof(act));
536         act.sa_handler = sig_int;
537         act.sa_flags = SA_RESTART;
538         sigaction(SIGTERM, &act, NULL);
539
540 /* Windows uses SIGBREAK as a quit signal from other applications */
541 #ifdef WIN32
542         memset(&act, 0, sizeof(act));
543         act.sa_handler = sig_int;
544         act.sa_flags = SA_RESTART;
545         sigaction(SIGBREAK, &act, NULL);
546 #endif
547
548         memset(&act, 0, sizeof(act));
549         act.sa_handler = sig_show_status;
550         act.sa_flags = SA_RESTART;
551         sigaction(SIGUSR1, &act, NULL);
552 }
553
554 static int send_client_cmd_line(struct fio_client *client)
555 {
556         struct cmd_single_line_pdu *cslp;
557         struct cmd_line_pdu *clp;
558         unsigned long offset;
559         unsigned int *lens;
560         void *pdu;
561         size_t mem;
562         int i, ret;
563
564         dprint(FD_NET, "client: send cmdline %d\n", client->argc);
565
566         lens = malloc(client->argc * sizeof(unsigned int));
567
568         /*
569          * Find out how much mem we need
570          */
571         for (i = 0, mem = 0; i < client->argc; i++) {
572                 lens[i] = strlen(client->argv[i]) + 1;
573                 mem += lens[i];
574         }
575
576         /*
577          * We need one cmd_line_pdu, and argc number of cmd_single_line_pdu
578          */
579         mem += sizeof(*clp) + (client->argc * sizeof(*cslp));
580
581         pdu = malloc(mem);
582         clp = pdu;
583         offset = sizeof(*clp);
584
585         for (i = 0; i < client->argc; i++) {
586                 uint16_t arg_len = lens[i];
587
588                 cslp = pdu + offset;
589                 strcpy((char *) cslp->text, client->argv[i]);
590                 cslp->len = cpu_to_le16(arg_len);
591                 offset += sizeof(*cslp) + arg_len;
592         }
593
594         free(lens);
595         clp->lines = cpu_to_le16(client->argc);
596         clp->client_type = __cpu_to_le16(client->type);
597         ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOBLINE, pdu, mem, NULL, NULL);
598         free(pdu);
599         return ret;
600 }
601
602 int fio_clients_connect(void)
603 {
604         struct fio_client *client;
605         struct flist_head *entry, *tmp;
606         int ret;
607
608 #ifdef WIN32
609         WSADATA wsd;
610         WSAStartup(MAKEWORD(2, 2), &wsd);
611 #endif
612
613         dprint(FD_NET, "client: connect all\n");
614
615         client_signal_handler();
616
617         flist_for_each_safe(entry, tmp, &client_list) {
618                 client = flist_entry(entry, struct fio_client, list);
619
620                 ret = fio_client_connect(client);
621                 if (ret) {
622                         remove_client(client);
623                         continue;
624                 }
625
626                 if (client->argc > 1)
627                         send_client_cmd_line(client);
628         }
629
630         return !nr_clients;
631 }
632
633 int fio_start_client(struct fio_client *client)
634 {
635         dprint(FD_NET, "client: start %s\n", client->hostname);
636         return fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_RUN, 0, NULL);
637 }
638
639 int fio_start_all_clients(void)
640 {
641         struct fio_client *client;
642         struct flist_head *entry, *tmp;
643         int ret;
644
645         dprint(FD_NET, "client: start all\n");
646
647         fio_client_json_init();
648
649         flist_for_each_safe(entry, tmp, &client_list) {
650                 client = flist_entry(entry, struct fio_client, list);
651
652                 ret = fio_start_client(client);
653                 if (ret) {
654                         remove_client(client);
655                         continue;
656                 }
657         }
658
659         return flist_empty(&client_list);
660 }
661
662 static int __fio_client_send_remote_ini(struct fio_client *client,
663                                         const char *filename)
664 {
665         struct cmd_load_file_pdu *pdu;
666         size_t p_size;
667         int ret;
668
669         dprint(FD_NET, "send remote ini %s to %s\n", filename, client->hostname);
670
671         p_size = sizeof(*pdu) + strlen(filename) + 1;
672         pdu = malloc(p_size);
673         memset(pdu, 0, p_size);
674         pdu->name_len = strlen(filename);
675         strcpy((char *) pdu->file, filename);
676         pdu->client_type = cpu_to_le16((uint16_t) client->type);
677
678         client->sent_job = 1;
679         ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_LOAD_FILE, pdu, p_size,NULL, NULL);
680         free(pdu);
681         return ret;
682 }
683
684 /*
685  * Send file contents to server backend. We could use sendfile(), but to remain
686  * more portable lets just read/write the darn thing.
687  */
688 static int __fio_client_send_local_ini(struct fio_client *client,
689                                        const char *filename)
690 {
691         struct cmd_job_pdu *pdu;
692         size_t p_size;
693         struct stat sb;
694         char *p;
695         void *buf;
696         off_t len;
697         int fd, ret;
698
699         dprint(FD_NET, "send ini %s to %s\n", filename, client->hostname);
700
701         fd = open(filename, O_RDONLY);
702         if (fd < 0) {
703                 ret = -errno;
704                 log_err("fio: job file <%s> open: %s\n", filename, strerror(errno));
705                 return ret;
706         }
707
708         if (fstat(fd, &sb) < 0) {
709                 ret = -errno;
710                 log_err("fio: job file stat: %s\n", strerror(errno));
711                 close(fd);
712                 return ret;
713         }
714
715         p_size = sb.st_size + sizeof(*pdu);
716         pdu = malloc(p_size);
717         buf = pdu->buf;
718
719         len = sb.st_size;
720         p = buf;
721         if (read_data(fd, p, len)) {
722                 log_err("fio: failed reading job file %s\n", filename);
723                 close(fd);
724                 free(pdu);
725                 return 1;
726         }
727
728         pdu->buf_len = __cpu_to_le32(sb.st_size);
729         pdu->client_type = cpu_to_le32(client->type);
730
731         client->sent_job = 1;
732         ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, pdu, p_size, NULL, NULL);
733         free(pdu);
734         close(fd);
735         return ret;
736 }
737
738 int fio_client_send_ini(struct fio_client *client, const char *filename,
739                         int remote)
740 {
741         int ret;
742
743         if (!remote)
744                 ret = __fio_client_send_local_ini(client, filename);
745         else
746                 ret = __fio_client_send_remote_ini(client, filename);
747
748         if (!ret)
749                 client->sent_job = 1;
750
751         return ret;
752 }
753
754 static int fio_client_send_cf(struct fio_client *client,
755                               struct client_file *cf)
756 {
757         return fio_client_send_ini(client, cf->file, cf->remote);
758 }
759
760 int fio_clients_send_ini(const char *filename)
761 {
762         struct fio_client *client;
763         struct flist_head *entry, *tmp;
764
765         flist_for_each_safe(entry, tmp, &client_list) {
766                 client = flist_entry(entry, struct fio_client, list);
767
768                 if (client->nr_files) {
769                         int i;
770
771                         for (i = 0; i < client->nr_files; i++) {
772                                 struct client_file *cf;
773
774                                 cf = &client->files[i];
775
776                                 if (fio_client_send_cf(client, cf)) {
777                                         remove_client(client);
778                                         break;
779                                 }
780                         }
781                 }
782                 if (client->sent_job)
783                         continue;
784                 if (!filename || fio_client_send_ini(client, filename, 0))
785                         remove_client(client);
786         }
787
788         return !nr_clients;
789 }
790
791 int fio_client_update_options(struct fio_client *client,
792                               struct thread_options *o, uint64_t *tag)
793 {
794         struct cmd_add_job_pdu pdu;
795
796         pdu.thread_number = cpu_to_le32(client->thread_number);
797         pdu.groupid = cpu_to_le32(client->groupid);
798         convert_thread_options_to_net(&pdu.top, o);
799
800         return fio_net_send_cmd(client->fd, FIO_NET_CMD_UPDATE_JOB, &pdu, sizeof(pdu), tag, &client->cmd_list);
801 }
802
803 static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
804 {
805         dst->max_val    = le64_to_cpu(src->max_val);
806         dst->min_val    = le64_to_cpu(src->min_val);
807         dst->samples    = le64_to_cpu(src->samples);
808
809         /*
810          * Floats arrive as IEEE 754 encoded uint64_t, convert back to double
811          */
812         dst->mean.u.f   = fio_uint64_to_double(le64_to_cpu(dst->mean.u.i));
813         dst->S.u.f      = fio_uint64_to_double(le64_to_cpu(dst->S.u.i));
814 }
815
816 static void convert_ts(struct thread_stat *dst, struct thread_stat *src)
817 {
818         int i, j;
819
820         dst->error              = le32_to_cpu(src->error);
821         dst->thread_number      = le32_to_cpu(src->thread_number);
822         dst->groupid            = le32_to_cpu(src->groupid);
823         dst->pid                = le32_to_cpu(src->pid);
824         dst->members            = le32_to_cpu(src->members);
825         dst->unified_rw_rep     = le32_to_cpu(src->unified_rw_rep);
826
827         for (i = 0; i < DDIR_RWDIR_CNT; i++) {
828                 convert_io_stat(&dst->clat_stat[i], &src->clat_stat[i]);
829                 convert_io_stat(&dst->slat_stat[i], &src->slat_stat[i]);
830                 convert_io_stat(&dst->lat_stat[i], &src->lat_stat[i]);
831                 convert_io_stat(&dst->bw_stat[i], &src->bw_stat[i]);
832         }
833
834         dst->usr_time           = le64_to_cpu(src->usr_time);
835         dst->sys_time           = le64_to_cpu(src->sys_time);
836         dst->ctx                = le64_to_cpu(src->ctx);
837         dst->minf               = le64_to_cpu(src->minf);
838         dst->majf               = le64_to_cpu(src->majf);
839         dst->clat_percentiles   = le64_to_cpu(src->clat_percentiles);
840         dst->percentile_precision = le64_to_cpu(src->percentile_precision);
841
842         for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
843                 fio_fp64_t *fps = &src->percentile_list[i];
844                 fio_fp64_t *fpd = &dst->percentile_list[i];
845
846                 fpd->u.f = fio_uint64_to_double(le64_to_cpu(fps->u.i));
847         }
848
849         for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
850                 dst->io_u_map[i]        = le32_to_cpu(src->io_u_map[i]);
851                 dst->io_u_submit[i]     = le32_to_cpu(src->io_u_submit[i]);
852                 dst->io_u_complete[i]   = le32_to_cpu(src->io_u_complete[i]);
853         }
854
855         for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
856                 dst->io_u_lat_u[i]      = le32_to_cpu(src->io_u_lat_u[i]);
857                 dst->io_u_lat_m[i]      = le32_to_cpu(src->io_u_lat_m[i]);
858         }
859
860         for (i = 0; i < DDIR_RWDIR_CNT; i++)
861                 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
862                         dst->io_u_plat[i][j] = le32_to_cpu(src->io_u_plat[i][j]);
863
864         for (i = 0; i < DDIR_RWDIR_CNT; i++) {
865                 dst->total_io_u[i]      = le64_to_cpu(src->total_io_u[i]);
866                 dst->short_io_u[i]      = le64_to_cpu(src->short_io_u[i]);
867                 dst->drop_io_u[i]       = le64_to_cpu(src->drop_io_u[i]);
868         }
869
870         dst->total_submit       = le64_to_cpu(src->total_submit);
871         dst->total_complete     = le64_to_cpu(src->total_complete);
872
873         for (i = 0; i < DDIR_RWDIR_CNT; i++) {
874                 dst->io_bytes[i]        = le64_to_cpu(src->io_bytes[i]);
875                 dst->runtime[i]         = le64_to_cpu(src->runtime[i]);
876         }
877
878         dst->total_run_time     = le64_to_cpu(src->total_run_time);
879         dst->continue_on_error  = le16_to_cpu(src->continue_on_error);
880         dst->total_err_count    = le64_to_cpu(src->total_err_count);
881         dst->first_error        = le32_to_cpu(src->first_error);
882         dst->kb_base            = le32_to_cpu(src->kb_base);
883         dst->unit_base          = le32_to_cpu(src->unit_base);
884
885         dst->latency_depth      = le32_to_cpu(src->latency_depth);
886         dst->latency_target     = le64_to_cpu(src->latency_target);
887         dst->latency_window     = le64_to_cpu(src->latency_window);
888         dst->latency_percentile.u.f = fio_uint64_to_double(le64_to_cpu(src->latency_percentile.u.i));
889 }
890
891 static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
892 {
893         int i;
894
895         for (i = 0; i < DDIR_RWDIR_CNT; i++) {
896                 dst->max_run[i]         = le64_to_cpu(src->max_run[i]);
897                 dst->min_run[i]         = le64_to_cpu(src->min_run[i]);
898                 dst->max_bw[i]          = le64_to_cpu(src->max_bw[i]);
899                 dst->min_bw[i]          = le64_to_cpu(src->min_bw[i]);
900                 dst->io_kb[i]           = le64_to_cpu(src->io_kb[i]);
901                 dst->agg[i]             = le64_to_cpu(src->agg[i]);
902         }
903
904         dst->kb_base    = le32_to_cpu(src->kb_base);
905         dst->unit_base  = le32_to_cpu(src->unit_base);
906         dst->groupid    = le32_to_cpu(src->groupid);
907         dst->unified_rw_rep     = le32_to_cpu(src->unified_rw_rep);
908 }
909
910 static void json_object_add_client_info(struct json_object *obj,
911                                         struct fio_client *client)
912 {
913         const char *hostname = client->hostname ? client->hostname : "";
914
915         json_object_add_value_string(obj, "hostname", hostname);
916         json_object_add_value_int(obj, "port", client->port);
917 }
918
919 static void handle_ts(struct fio_client *client, struct fio_net_cmd *cmd)
920 {
921         struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
922         struct json_object *tsobj;
923
924         tsobj = show_thread_status(&p->ts, &p->rs);
925         client->did_stat = 1;
926         if (tsobj) {
927                 json_object_add_client_info(tsobj, client);
928                 json_array_add_value_object(clients_array, tsobj);
929         }
930
931         if (sum_stat_clients <= 1)
932                 return;
933
934         sum_thread_stats(&client_ts, &p->ts, sum_stat_nr);
935         sum_group_stats(&client_gs, &p->rs);
936
937         client_ts.members++;
938         client_ts.thread_number = p->ts.thread_number;
939         client_ts.groupid = p->ts.groupid;
940         client_ts.unified_rw_rep = p->ts.unified_rw_rep;
941
942         if (++sum_stat_nr == sum_stat_clients) {
943                 strcpy(client_ts.name, "All clients");
944                 tsobj = show_thread_status(&client_ts, &client_gs);
945                 if (tsobj) {
946                         json_object_add_client_info(tsobj, client);
947                         json_array_add_value_object(clients_array, tsobj);
948                 }
949         }
950 }
951
952 static void handle_gs(struct fio_client *client, struct fio_net_cmd *cmd)
953 {
954         struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
955
956         show_group_stats(gs);
957 }
958
959 static void handle_text(struct fio_client *client, struct fio_net_cmd *cmd)
960 {
961         struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
962         const char *buf = (const char *) pdu->buf;
963         const char *name;
964         int fio_unused ret;
965
966         name = client->name ? client->name : client->hostname;
967
968         if (!client->skip_newline)
969                 fprintf(f_out, "<%s> ", name);
970         ret = fwrite(buf, pdu->buf_len, 1, f_out);
971         fflush(f_out);
972         client->skip_newline = strchr(buf, '\n') == NULL;
973 }
974
975 static void convert_agg(struct disk_util_agg *agg)
976 {
977         int i;
978
979         for (i = 0; i < 2; i++) {
980                 agg->ios[i]     = le64_to_cpu(agg->ios[i]);
981                 agg->merges[i]  = le64_to_cpu(agg->merges[i]);
982                 agg->sectors[i] = le64_to_cpu(agg->sectors[i]);
983                 agg->ticks[i]   = le64_to_cpu(agg->ticks[i]);
984         }
985
986         agg->io_ticks           = le64_to_cpu(agg->io_ticks);
987         agg->time_in_queue      = le64_to_cpu(agg->time_in_queue);
988         agg->slavecount         = le32_to_cpu(agg->slavecount);
989         agg->max_util.u.f       = fio_uint64_to_double(le64_to_cpu(agg->max_util.u.i));
990 }
991
992 static void convert_dus(struct disk_util_stat *dus)
993 {
994         int i;
995
996         for (i = 0; i < 2; i++) {
997                 dus->s.ios[i]           = le64_to_cpu(dus->s.ios[i]);
998                 dus->s.merges[i]        = le64_to_cpu(dus->s.merges[i]);
999                 dus->s.sectors[i]       = le64_to_cpu(dus->s.sectors[i]);
1000                 dus->s.ticks[i]         = le64_to_cpu(dus->s.ticks[i]);
1001         }
1002
1003         dus->s.io_ticks         = le64_to_cpu(dus->s.io_ticks);
1004         dus->s.time_in_queue    = le64_to_cpu(dus->s.time_in_queue);
1005         dus->s.msec             = le64_to_cpu(dus->s.msec);
1006 }
1007
1008 static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd)
1009 {
1010         struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
1011
1012         if (!client->disk_stats_shown) {
1013                 client->disk_stats_shown = 1;
1014                 log_info("\nDisk stats (read/write):\n");
1015         }
1016
1017         if (output_format == FIO_OUTPUT_JSON) {
1018                 struct json_object *duobj;
1019                 json_array_add_disk_util(&du->dus, &du->agg, du_array);
1020                 duobj = json_array_last_value_object(du_array);
1021                 json_object_add_client_info(duobj, client);
1022         } else
1023                 print_disk_util(&du->dus, &du->agg, output_format == FIO_OUTPUT_TERSE);
1024 }
1025
1026 static void convert_jobs_eta(struct jobs_eta *je)
1027 {
1028         int i;
1029
1030         je->nr_running          = le32_to_cpu(je->nr_running);
1031         je->nr_ramp             = le32_to_cpu(je->nr_ramp);
1032         je->nr_pending          = le32_to_cpu(je->nr_pending);
1033         je->nr_setting_up       = le32_to_cpu(je->nr_setting_up);
1034         je->files_open          = le32_to_cpu(je->files_open);
1035
1036         for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1037                 je->m_rate[i]   = le32_to_cpu(je->m_rate[i]);
1038                 je->t_rate[i]   = le32_to_cpu(je->t_rate[i]);
1039                 je->m_iops[i]   = le32_to_cpu(je->m_iops[i]);
1040                 je->t_iops[i]   = le32_to_cpu(je->t_iops[i]);
1041                 je->rate[i]     = le32_to_cpu(je->rate[i]);
1042                 je->iops[i]     = le32_to_cpu(je->iops[i]);
1043         }
1044
1045         je->elapsed_sec         = le64_to_cpu(je->elapsed_sec);
1046         je->eta_sec             = le64_to_cpu(je->eta_sec);
1047         je->nr_threads          = le32_to_cpu(je->nr_threads);
1048         je->is_pow2             = le32_to_cpu(je->is_pow2);
1049         je->unit_base           = le32_to_cpu(je->unit_base);
1050 }
1051
1052 void fio_client_sum_jobs_eta(struct jobs_eta *dst, struct jobs_eta *je)
1053 {
1054         int i;
1055
1056         dst->nr_running         += je->nr_running;
1057         dst->nr_ramp            += je->nr_ramp;
1058         dst->nr_pending         += je->nr_pending;
1059         dst->nr_setting_up      += je->nr_setting_up;
1060         dst->files_open         += je->files_open;
1061
1062         for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1063                 dst->m_rate[i]  += je->m_rate[i];
1064                 dst->t_rate[i]  += je->t_rate[i];
1065                 dst->m_iops[i]  += je->m_iops[i];
1066                 dst->t_iops[i]  += je->t_iops[i];
1067                 dst->rate[i]    += je->rate[i];
1068                 dst->iops[i]    += je->iops[i];
1069         }
1070
1071         dst->elapsed_sec        += je->elapsed_sec;
1072
1073         if (je->eta_sec > dst->eta_sec)
1074                 dst->eta_sec = je->eta_sec;
1075
1076         dst->nr_threads         += je->nr_threads;
1077
1078         /*
1079          * This wont be correct for multiple strings, but at least it
1080          * works for the basic cases.
1081          */
1082         strcpy((char *) dst->run_str, (char *) je->run_str);
1083 }
1084
1085 void fio_client_dec_jobs_eta(struct client_eta *eta, client_eta_op eta_fn)
1086 {
1087         if (!--eta->pending) {
1088                 eta_fn(&eta->eta);
1089                 free(eta);
1090         }
1091 }
1092
1093 static void remove_reply_cmd(struct fio_client *client, struct fio_net_cmd *cmd)
1094 {
1095         struct fio_net_cmd_reply *reply = NULL;
1096         struct flist_head *entry;
1097
1098         flist_for_each(entry, &client->cmd_list) {
1099                 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
1100
1101                 if (cmd->tag == (uintptr_t) reply)
1102                         break;
1103
1104                 reply = NULL;
1105         }
1106
1107         if (!reply) {
1108                 log_err("fio: client: unable to find matching tag (%llx)\n", (unsigned long long) cmd->tag);
1109                 return;
1110         }
1111
1112         flist_del(&reply->list);
1113         cmd->tag = reply->saved_tag;
1114         free(reply);
1115 }
1116
1117 int fio_client_wait_for_reply(struct fio_client *client, uint64_t tag)
1118 {
1119         do {
1120                 struct fio_net_cmd_reply *reply = NULL;
1121                 struct flist_head *entry;
1122
1123                 flist_for_each(entry, &client->cmd_list) {
1124                         reply = flist_entry(entry, struct fio_net_cmd_reply, list);
1125
1126                         if (tag == (uintptr_t) reply)
1127                                 break;
1128
1129                         reply = NULL;
1130                 }
1131
1132                 if (!reply)
1133                         break;
1134
1135                 usleep(1000);
1136         } while (1);
1137
1138         return 0;
1139 }
1140
1141 static void handle_eta(struct fio_client *client, struct fio_net_cmd *cmd)
1142 {
1143         struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
1144         struct client_eta *eta = (struct client_eta *) (uintptr_t) cmd->tag;
1145
1146         dprint(FD_NET, "client: got eta tag %p, %d\n", eta, eta->pending);
1147
1148         assert(client->eta_in_flight == eta);
1149
1150         client->eta_in_flight = NULL;
1151         flist_del_init(&client->eta_list);
1152
1153         if (client->ops->jobs_eta)
1154                 client->ops->jobs_eta(client, je);
1155
1156         fio_client_sum_jobs_eta(&eta->eta, je);
1157         fio_client_dec_jobs_eta(eta, client->ops->eta);
1158 }
1159
1160 static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd)
1161 {
1162         struct cmd_probe_reply_pdu *probe = (struct cmd_probe_reply_pdu *) cmd->payload;
1163         const char *os, *arch;
1164         char bit[16];
1165
1166         os = fio_get_os_string(probe->os);
1167         if (!os)
1168                 os = "unknown";
1169
1170         arch = fio_get_arch_string(probe->arch);
1171         if (!arch)
1172                 os = "unknown";
1173
1174         sprintf(bit, "%d-bit", probe->bpp * 8);
1175         probe->flags = le64_to_cpu(probe->flags);
1176
1177         log_info("hostname=%s, be=%u, %s, os=%s, arch=%s, fio=%s, flags=%lx\n",
1178                 probe->hostname, probe->bigendian, bit, os, arch,
1179                 probe->fio_version, (unsigned long) probe->flags);
1180
1181         if (!client->name)
1182                 client->name = strdup((char *) probe->hostname);
1183 }
1184
1185 static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd)
1186 {
1187         struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
1188
1189         client->state = Client_started;
1190         client->jobs = le32_to_cpu(pdu->jobs);
1191         client->nr_stat = le32_to_cpu(pdu->stat_outputs);
1192
1193         sum_stat_clients += client->nr_stat;
1194 }
1195
1196 static void handle_stop(struct fio_client *client, struct fio_net_cmd *cmd)
1197 {
1198         if (client->error)
1199                 log_info("client <%s>: exited with error %d\n", client->hostname, client->error);
1200 }
1201
1202 static void convert_stop(struct fio_net_cmd *cmd)
1203 {
1204         struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
1205
1206         pdu->error = le32_to_cpu(pdu->error);
1207 }
1208
1209 static void convert_text(struct fio_net_cmd *cmd)
1210 {
1211         struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
1212
1213         pdu->level      = le32_to_cpu(pdu->level);
1214         pdu->buf_len    = le32_to_cpu(pdu->buf_len);
1215         pdu->log_sec    = le64_to_cpu(pdu->log_sec);
1216         pdu->log_usec   = le64_to_cpu(pdu->log_usec);
1217 }
1218
1219 static struct cmd_iolog_pdu *convert_iolog_gz(struct fio_net_cmd *cmd,
1220                                               struct cmd_iolog_pdu *pdu)
1221 {
1222 #ifdef CONFIG_ZLIB
1223         struct cmd_iolog_pdu *ret;
1224         z_stream stream;
1225         uint32_t nr_samples;
1226         size_t total;
1227         void *p;
1228
1229         stream.zalloc = Z_NULL;
1230         stream.zfree = Z_NULL;
1231         stream.opaque = Z_NULL;
1232         stream.avail_in = 0;
1233         stream.next_in = Z_NULL;
1234
1235         if (inflateInit(&stream) != Z_OK)
1236                 return NULL;
1237
1238         /*
1239          * Get header first, it's not compressed
1240          */
1241         nr_samples = le64_to_cpu(pdu->nr_samples);
1242
1243         total = nr_samples * __log_entry_sz(le32_to_cpu(pdu->log_offset));
1244         ret = malloc(total + sizeof(*pdu));
1245         ret->nr_samples = nr_samples;
1246
1247         memcpy(ret, pdu, sizeof(*pdu));
1248
1249         p = (void *) ret + sizeof(*pdu);
1250
1251         stream.avail_in = cmd->pdu_len - sizeof(*pdu);
1252         stream.next_in = (void *) pdu + sizeof(*pdu);
1253         while (stream.avail_in) {
1254                 unsigned int this_chunk = 65536;
1255                 unsigned int this_len;
1256                 int err;
1257
1258                 if (this_chunk > total)
1259                         this_chunk = total;
1260
1261                 stream.avail_out = this_chunk;
1262                 stream.next_out = p;
1263                 err = inflate(&stream, Z_NO_FLUSH);
1264                 /* may be Z_OK, or Z_STREAM_END */
1265                 if (err < 0) {
1266                         log_err("fio: inflate error %d\n", err);
1267                         free(ret);
1268                         ret = NULL;
1269                         goto err;
1270                 }
1271
1272                 this_len = this_chunk - stream.avail_out;
1273                 p += this_len;
1274                 total -= this_len;
1275         }
1276
1277 err:
1278         inflateEnd(&stream);
1279         return ret;
1280 #else
1281         return NULL;
1282 #endif
1283 }
1284
1285 /*
1286  * This has been compressed on the server side, since it can be big.
1287  * Uncompress here.
1288  */
1289 static struct cmd_iolog_pdu *convert_iolog(struct fio_net_cmd *cmd)
1290 {
1291         struct cmd_iolog_pdu *pdu = (struct cmd_iolog_pdu *) cmd->payload;
1292         struct cmd_iolog_pdu *ret;
1293         uint64_t i;
1294         void *samples;
1295
1296         /*
1297          * Convert if compressed and we support it. If it's not
1298          * compressed, we need not do anything.
1299          */
1300         if (le32_to_cpu(pdu->compressed)) {
1301 #ifndef CONFIG_ZLIB
1302                 log_err("fio: server sent compressed data by mistake\n");
1303                 return NULL;
1304 #endif
1305                 ret = convert_iolog_gz(cmd, pdu);
1306                 if (!ret) {
1307                         log_err("fio: failed decompressing log\n");
1308                         return NULL;
1309                 }
1310         } else
1311                 ret = pdu;
1312
1313         ret->nr_samples         = le64_to_cpu(ret->nr_samples);
1314         ret->thread_number      = le32_to_cpu(ret->thread_number);
1315         ret->log_type           = le32_to_cpu(ret->log_type);
1316         ret->compressed         = le32_to_cpu(ret->compressed);
1317         ret->log_offset         = le32_to_cpu(ret->log_offset);
1318
1319         samples = &ret->samples[0];
1320         for (i = 0; i < ret->nr_samples; i++) {
1321                 struct io_sample *s;
1322
1323                 s = __get_sample(samples, ret->log_offset, i);
1324                 s->time         = le64_to_cpu(s->time);
1325                 s->val          = le64_to_cpu(s->val);
1326                 s->__ddir       = le32_to_cpu(s->__ddir);
1327                 s->bs           = le32_to_cpu(s->bs);
1328
1329                 if (ret->log_offset) {
1330                         struct io_sample_offset *so = (void *) s;
1331
1332                         so->offset = le64_to_cpu(so->offset);
1333                 }
1334         }
1335
1336         return ret;
1337 }
1338
1339 static void sendfile_reply(int fd, struct cmd_sendfile_reply *rep,
1340                            size_t size, uint64_t tag)
1341 {
1342         rep->error = cpu_to_le32(rep->error);
1343         fio_net_send_cmd(fd, FIO_NET_CMD_SENDFILE, rep, size, &tag, NULL);
1344 }
1345
1346 static int send_file(struct fio_client *client, struct cmd_sendfile *pdu,
1347                      uint64_t tag)
1348 {
1349         struct cmd_sendfile_reply *rep;
1350         struct stat sb;
1351         size_t size;
1352         int fd;
1353
1354         size = sizeof(*rep);
1355         rep = malloc(size);
1356
1357         if (stat((char *)pdu->path, &sb) < 0) {
1358 fail:
1359                 rep->error = errno;
1360                 sendfile_reply(client->fd, rep, size, tag);
1361                 free(rep);
1362                 return 1;
1363         }
1364
1365         size += sb.st_size;
1366         rep = realloc(rep, size);
1367         rep->size = cpu_to_le32((uint32_t) sb.st_size);
1368
1369         fd = open((char *)pdu->path, O_RDONLY);
1370         if (fd == -1 )
1371                 goto fail;
1372
1373         rep->error = read_data(fd, &rep->data, sb.st_size);
1374         sendfile_reply(client->fd, rep, size, tag);
1375         free(rep);
1376         close(fd);
1377         return 0;
1378 }
1379
1380 int fio_handle_client(struct fio_client *client)
1381 {
1382         struct client_ops *ops = client->ops;
1383         struct fio_net_cmd *cmd;
1384
1385         dprint(FD_NET, "client: handle %s\n", client->hostname);
1386
1387         cmd = fio_net_recv_cmd(client->fd);
1388         if (!cmd)
1389                 return 0;
1390
1391         dprint(FD_NET, "client: got cmd op %s from %s (pdu=%u)\n",
1392                 fio_server_op(cmd->opcode), client->hostname, cmd->pdu_len);
1393
1394         switch (cmd->opcode) {
1395         case FIO_NET_CMD_QUIT:
1396                 if (ops->quit)
1397                         ops->quit(client, cmd);
1398                 remove_client(client);
1399                 break;
1400         case FIO_NET_CMD_TEXT:
1401                 convert_text(cmd);
1402                 ops->text(client, cmd);
1403                 break;
1404         case FIO_NET_CMD_DU: {
1405                 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
1406
1407                 convert_dus(&du->dus);
1408                 convert_agg(&du->agg);
1409
1410                 ops->disk_util(client, cmd);
1411                 break;
1412                 }
1413         case FIO_NET_CMD_TS: {
1414                 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
1415
1416                 convert_ts(&p->ts, &p->ts);
1417                 convert_gs(&p->rs, &p->rs);
1418
1419                 ops->thread_status(client, cmd);
1420                 break;
1421                 }
1422         case FIO_NET_CMD_GS: {
1423                 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
1424
1425                 convert_gs(gs, gs);
1426
1427                 ops->group_stats(client, cmd);
1428                 break;
1429                 }
1430         case FIO_NET_CMD_ETA: {
1431                 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
1432
1433                 remove_reply_cmd(client, cmd);
1434                 convert_jobs_eta(je);
1435                 handle_eta(client, cmd);
1436                 break;
1437                 }
1438         case FIO_NET_CMD_PROBE:
1439                 remove_reply_cmd(client, cmd);
1440                 ops->probe(client, cmd);
1441                 break;
1442         case FIO_NET_CMD_SERVER_START:
1443                 client->state = Client_running;
1444                 if (ops->job_start)
1445                         ops->job_start(client, cmd);
1446                 break;
1447         case FIO_NET_CMD_START: {
1448                 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
1449
1450                 pdu->jobs = le32_to_cpu(pdu->jobs);
1451                 ops->start(client, cmd);
1452                 break;
1453                 }
1454         case FIO_NET_CMD_STOP: {
1455                 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
1456
1457                 convert_stop(cmd);
1458                 client->state = Client_stopped;
1459                 client->error = le32_to_cpu(pdu->error);
1460                 client->signal = le32_to_cpu(pdu->signal);
1461                 ops->stop(client, cmd);
1462                 break;
1463                 }
1464         case FIO_NET_CMD_ADD_JOB: {
1465                 struct cmd_add_job_pdu *pdu = (struct cmd_add_job_pdu *) cmd->payload;
1466
1467                 client->thread_number = le32_to_cpu(pdu->thread_number);
1468                 client->groupid = le32_to_cpu(pdu->groupid);
1469
1470                 if (ops->add_job)
1471                         ops->add_job(client, cmd);
1472                 break;
1473                 }
1474         case FIO_NET_CMD_IOLOG:
1475                 if (ops->iolog) {
1476                         struct cmd_iolog_pdu *pdu;
1477
1478                         pdu = convert_iolog(cmd);
1479                         ops->iolog(client, pdu);
1480                 }
1481                 break;
1482         case FIO_NET_CMD_UPDATE_JOB:
1483                 ops->update_job(client, cmd);
1484                 remove_reply_cmd(client, cmd);
1485                 break;
1486         case FIO_NET_CMD_VTRIGGER: {
1487                 struct all_io_list *pdu = (struct all_io_list *) cmd->payload;
1488                 char buf[64];
1489
1490                 __verify_save_state(pdu, server_name(client, buf, sizeof(buf)));
1491                 exec_trigger(trigger_cmd);
1492                 break;
1493                 }
1494         case FIO_NET_CMD_SENDFILE: {
1495                 struct cmd_sendfile *pdu = (struct cmd_sendfile *) cmd->payload;
1496                 send_file(client, pdu, cmd->tag);
1497                 break;
1498                 }
1499         default:
1500                 log_err("fio: unknown client op: %s\n", fio_server_op(cmd->opcode));
1501                 break;
1502         }
1503
1504         free(cmd);
1505         return 1;
1506 }
1507
1508 int fio_clients_send_trigger(const char *cmd)
1509 {
1510         struct flist_head *entry;
1511         struct fio_client *client;
1512         size_t slen;
1513
1514         dprint(FD_NET, "client: send vtrigger: %s\n", cmd);
1515
1516         if (!cmd)
1517                 slen = 0;
1518         else
1519                 slen = strlen(cmd);
1520
1521         flist_for_each(entry, &client_list) {
1522                 struct cmd_vtrigger_pdu *pdu;
1523
1524                 client = flist_entry(entry, struct fio_client, list);
1525
1526                 pdu = malloc(sizeof(*pdu) + slen);
1527                 pdu->len = cpu_to_le16((uint16_t) slen);
1528                 if (slen)
1529                         memcpy(pdu->cmd, cmd, slen);
1530                 fio_net_send_cmd(client->fd, FIO_NET_CMD_VTRIGGER, pdu,
1531                                         sizeof(*pdu) + slen, NULL, NULL);
1532                 free(pdu);
1533         }
1534
1535         return 0;
1536 }
1537
1538 static void request_client_etas(struct client_ops *ops)
1539 {
1540         struct fio_client *client;
1541         struct flist_head *entry;
1542         struct client_eta *eta;
1543         int skipped = 0;
1544
1545         dprint(FD_NET, "client: request eta (%d)\n", nr_clients);
1546
1547         eta = calloc(1, sizeof(*eta) + __THREAD_RUNSTR_SZ(REAL_MAX_JOBS));
1548         eta->pending = nr_clients;
1549
1550         flist_for_each(entry, &client_list) {
1551                 client = flist_entry(entry, struct fio_client, list);
1552
1553                 if (!flist_empty(&client->eta_list)) {
1554                         skipped++;
1555                         continue;
1556                 }
1557                 if (client->state != Client_running)
1558                         continue;
1559
1560                 assert(!client->eta_in_flight);
1561                 flist_add_tail(&client->eta_list, &eta_list);
1562                 client->eta_in_flight = eta;
1563                 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_SEND_ETA,
1564                                         (uintptr_t) eta, &client->cmd_list);
1565         }
1566
1567         while (skipped--)
1568                 fio_client_dec_jobs_eta(eta, ops->eta);
1569
1570         dprint(FD_NET, "client: requested eta tag %p\n", eta);
1571 }
1572
1573 static int client_check_cmd_timeout(struct fio_client *client,
1574                                     struct timeval *now)
1575 {
1576         struct fio_net_cmd_reply *reply;
1577         struct flist_head *entry, *tmp;
1578         int ret = 0;
1579
1580         flist_for_each_safe(entry, tmp, &client->cmd_list) {
1581                 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
1582
1583                 if (mtime_since(&reply->tv, now) < FIO_NET_CLIENT_TIMEOUT)
1584                         continue;
1585
1586                 log_err("fio: client %s, timeout on cmd %s\n", client->hostname,
1587                                                 fio_server_op(reply->opcode));
1588                 flist_del(&reply->list);
1589                 free(reply);
1590                 ret = 1;
1591         }
1592
1593         return flist_empty(&client->cmd_list) && ret;
1594 }
1595
1596 static int fio_check_clients_timed_out(void)
1597 {
1598         struct fio_client *client;
1599         struct flist_head *entry, *tmp;
1600         struct timeval tv;
1601         int ret = 0;
1602
1603         fio_gettime(&tv, NULL);
1604
1605         flist_for_each_safe(entry, tmp, &client_list) {
1606                 client = flist_entry(entry, struct fio_client, list);
1607
1608                 if (flist_empty(&client->cmd_list))
1609                         continue;
1610
1611                 if (!client_check_cmd_timeout(client, &tv))
1612                         continue;
1613
1614                 if (client->ops->timed_out)
1615                         client->ops->timed_out(client);
1616                 else
1617                         log_err("fio: client %s timed out\n", client->hostname);
1618
1619                 remove_client(client);
1620                 ret = 1;
1621         }
1622
1623         return ret;
1624 }
1625
1626 int fio_handle_clients(struct client_ops *ops)
1627 {
1628         struct pollfd *pfds;
1629         int i, ret = 0, retval = 0;
1630
1631         fio_gettime(&eta_tv, NULL);
1632
1633         pfds = malloc(nr_clients * sizeof(struct pollfd));
1634
1635         init_thread_stat(&client_ts);
1636         init_group_run_stat(&client_gs);
1637
1638         while (!exit_backend && nr_clients) {
1639                 struct flist_head *entry, *tmp;
1640                 struct fio_client *client;
1641
1642                 i = 0;
1643                 flist_for_each_safe(entry, tmp, &client_list) {
1644                         client = flist_entry(entry, struct fio_client, list);
1645
1646                         if (!client->sent_job && !client->ops->stay_connected &&
1647                             flist_empty(&client->cmd_list)) {
1648                                 remove_client(client);
1649                                 continue;
1650                         }
1651
1652                         pfds[i].fd = client->fd;
1653                         pfds[i].events = POLLIN;
1654                         i++;
1655                 }
1656
1657                 if (!nr_clients)
1658                         break;
1659
1660                 assert(i == nr_clients);
1661
1662                 do {
1663                         struct timeval tv;
1664                         int timeout;
1665
1666                         fio_gettime(&tv, NULL);
1667                         if (mtime_since(&eta_tv, &tv) >= 900) {
1668                                 request_client_etas(ops);
1669                                 memcpy(&eta_tv, &tv, sizeof(tv));
1670
1671                                 if (fio_check_clients_timed_out())
1672                                         break;
1673                         }
1674
1675                         check_trigger_file();
1676
1677                         timeout = min(100u, ops->eta_msec);
1678
1679                         ret = poll(pfds, nr_clients, timeout);
1680                         if (ret < 0) {
1681                                 if (errno == EINTR)
1682                                         continue;
1683                                 log_err("fio: poll clients: %s\n", strerror(errno));
1684                                 break;
1685                         } else if (!ret)
1686                                 continue;
1687                 } while (ret <= 0);
1688
1689                 for (i = 0; i < nr_clients; i++) {
1690                         if (!(pfds[i].revents & POLLIN))
1691                                 continue;
1692
1693                         client = find_client_by_fd(pfds[i].fd);
1694                         if (!client) {
1695                                 log_err("fio: unknown client fd %ld\n", (long) pfds[i].fd);
1696                                 continue;
1697                         }
1698                         if (!fio_handle_client(client)) {
1699                                 log_info("client: host=%s disconnected\n",
1700                                                 client->hostname);
1701                                 remove_client(client);
1702                                 retval = 1;
1703                         } else if (client->error)
1704                                 retval = 1;
1705                         fio_put_client(client);
1706                 }
1707         }
1708
1709         fio_client_json_fini();
1710
1711         free(pfds);
1712         return retval;
1713 }