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