ff3983bdf84c37e727ce5b481fe0574fddeff7f5
[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
18 #include "fio.h"
19 #include "client.h"
20 #include "server.h"
21 #include "flist.h"
22 #include "hash.h"
23
24 static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd);
25 static void handle_ts(struct fio_net_cmd *cmd);
26 static void handle_gs(struct fio_net_cmd *cmd);
27 static void handle_eta(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
31 struct client_ops fio_client_ops = {
32         .text_op        = handle_text,
33         .disk_util      = handle_du,
34         .thread_status  = handle_ts,
35         .group_stats    = handle_gs,
36         .eta            = handle_eta,
37         .probe          = handle_probe,
38 };
39
40 static struct timeval eta_tv;
41
42 enum {
43         Client_created          = 0,
44         Client_connected        = 1,
45         Client_started          = 2,
46         Client_running          = 3,
47         Client_stopped          = 4,
48         Client_exited           = 5,
49 };
50
51 static FLIST_HEAD(client_list);
52 static FLIST_HEAD(eta_list);
53
54 static FLIST_HEAD(arg_list);
55
56 static struct thread_stat client_ts;
57 static struct group_run_stats client_gs;
58 static int sum_stat_clients;
59 static int sum_stat_nr;
60
61 #define FIO_CLIENT_HASH_BITS    7
62 #define FIO_CLIENT_HASH_SZ      (1 << FIO_CLIENT_HASH_BITS)
63 #define FIO_CLIENT_HASH_MASK    (FIO_CLIENT_HASH_SZ - 1)
64 static struct flist_head client_hash[FIO_CLIENT_HASH_SZ];
65
66 static void fio_client_add_hash(struct fio_client *client)
67 {
68         int bucket = hash_long(client->fd, FIO_CLIENT_HASH_BITS);
69
70         bucket &= FIO_CLIENT_HASH_MASK;
71         flist_add(&client->hash_list, &client_hash[bucket]);
72 }
73
74 static void fio_client_remove_hash(struct fio_client *client)
75 {
76         if (!flist_empty(&client->hash_list))
77                 flist_del_init(&client->hash_list);
78 }
79
80 static void fio_init fio_client_hash_init(void)
81 {
82         int i;
83
84         for (i = 0; i < FIO_CLIENT_HASH_SZ; i++)
85                 INIT_FLIST_HEAD(&client_hash[i]);
86 }
87
88 static struct fio_client *find_client_by_fd(int fd)
89 {
90         int bucket = hash_long(fd, FIO_CLIENT_HASH_BITS) & FIO_CLIENT_HASH_MASK;
91         struct fio_client *client;
92         struct flist_head *entry;
93
94         flist_for_each(entry, &client_hash[bucket]) {
95                 client = flist_entry(entry, struct fio_client, hash_list);
96
97                 if (client->fd == fd)
98                         return client;
99         }
100
101         return NULL;
102 }
103
104 static void remove_client(struct fio_client *client)
105 {
106         dprint(FD_NET, "client: removed <%s>\n", client->hostname);
107         flist_del(&client->list);
108
109         fio_client_remove_hash(client);
110
111         if (!flist_empty(&client->eta_list)) {
112                 flist_del_init(&client->eta_list);
113                 fio_client_dec_jobs_eta(client->eta_in_flight, display_thread_status);
114         }
115
116         free(client->hostname);
117         if (client->argv)
118                 free(client->argv);
119         if (client->name)
120                 free(client->name);
121
122         free(client);
123         nr_clients--;
124         sum_stat_clients--;
125 }
126
127 static void __fio_client_add_cmd_option(struct fio_client *client,
128                                         const char *opt)
129 {
130         int index;
131
132         index = client->argc++;
133         client->argv = realloc(client->argv, sizeof(char *) * client->argc);
134         client->argv[index] = strdup(opt);
135         dprint(FD_NET, "client: add cmd %d: %s\n", index, opt);
136 }
137
138 void fio_client_add_cmd_option(void *cookie, const char *opt)
139 {
140         struct fio_client *client = cookie;
141         struct flist_head *entry;
142
143         if (!client || !opt)
144                 return;
145
146         __fio_client_add_cmd_option(client, opt);
147
148         /*
149          * Duplicate arguments to shared client group
150          */
151         flist_for_each(entry, &arg_list) {
152                 client = flist_entry(entry, struct fio_client, arg_list);
153
154                 __fio_client_add_cmd_option(client, opt);
155         }
156 }
157
158 struct fio_client *fio_client_add_explicit(const char *hostname, int type,
159                                            int port)
160 {
161         struct fio_client *client;
162
163         client = malloc(sizeof(*client));
164         memset(client, 0, sizeof(*client));
165
166         INIT_FLIST_HEAD(&client->list);
167         INIT_FLIST_HEAD(&client->hash_list);
168         INIT_FLIST_HEAD(&client->arg_list);
169         INIT_FLIST_HEAD(&client->eta_list);
170         INIT_FLIST_HEAD(&client->cmd_list);
171
172         client->hostname = strdup(hostname);
173
174         if (type == Fio_client_socket)
175                 client->is_sock = 1;
176         else {
177                 int ipv6;
178
179                 ipv6 = type == Fio_client_ipv6;
180                 if (fio_server_parse_host(hostname, &ipv6,
181                                                 &client->addr.sin_addr,
182                                                 &client->addr6.sin6_addr))
183                         goto err;
184
185                 client->port = port;
186         }
187
188         client->fd = -1;
189
190         __fio_client_add_cmd_option(client, "fio");
191
192         flist_add(&client->list, &client_list);
193         nr_clients++;
194         dprint(FD_NET, "client: added <%s>\n", client->hostname);
195         return client;
196 err:
197         free(client);
198         return NULL;
199 }
200
201 int fio_client_add(const char *hostname, void **cookie)
202 {
203         struct fio_client *existing = *cookie;
204         struct fio_client *client;
205
206         if (existing) {
207                 /*
208                  * We always add our "exec" name as the option, hence 1
209                  * means empty.
210                  */
211                 if (existing->argc == 1)
212                         flist_add_tail(&existing->arg_list, &arg_list);
213                 else {
214                         while (!flist_empty(&arg_list))
215                                 flist_del_init(arg_list.next);
216                 }
217         }
218
219         client = malloc(sizeof(*client));
220         memset(client, 0, sizeof(*client));
221
222         INIT_FLIST_HEAD(&client->list);
223         INIT_FLIST_HEAD(&client->hash_list);
224         INIT_FLIST_HEAD(&client->arg_list);
225         INIT_FLIST_HEAD(&client->eta_list);
226         INIT_FLIST_HEAD(&client->cmd_list);
227
228         if (fio_server_parse_string(hostname, &client->hostname,
229                                         &client->is_sock, &client->port,
230                                         &client->addr.sin_addr,
231                                         &client->addr6.sin6_addr,
232                                         &client->ipv6))
233                 return -1;
234
235         client->fd = -1;
236
237         __fio_client_add_cmd_option(client, "fio");
238
239         flist_add(&client->list, &client_list);
240         nr_clients++;
241         dprint(FD_NET, "client: added <%s>\n", client->hostname);
242         *cookie = client;
243         return 0;
244 }
245
246 static int fio_client_connect_ip(struct fio_client *client)
247 {
248         struct sockaddr *addr;
249         fio_socklen_t socklen;
250         int fd, domain;
251
252         if (client->ipv6) {
253                 client->addr6.sin6_family = AF_INET6;
254                 client->addr6.sin6_port = htons(client->port);
255                 domain = AF_INET6;
256                 addr = (struct sockaddr *) &client->addr6;
257                 socklen = sizeof(client->addr6);
258         } else {
259                 client->addr.sin_family = AF_INET;
260                 client->addr.sin_port = htons(client->port);
261                 domain = AF_INET;
262                 addr = (struct sockaddr *) &client->addr;
263                 socklen = sizeof(client->addr);
264         }
265
266         fd = socket(domain, SOCK_STREAM, 0);
267         if (fd < 0) {
268                 log_err("fio: socket: %s\n", strerror(errno));
269                 return -1;
270         }
271
272         if (connect(fd, addr, socklen) < 0) {
273                 log_err("fio: connect: %s\n", strerror(errno));
274                 log_err("fio: failed to connect to %s:%u\n", client->hostname,
275                                                                 client->port);
276                 close(fd);
277                 return -1;
278         }
279
280         return fd;
281 }
282
283 static int fio_client_connect_sock(struct fio_client *client)
284 {
285         struct sockaddr_un *addr = &client->addr_un;
286         fio_socklen_t len;
287         int fd;
288
289         memset(addr, 0, sizeof(*addr));
290         addr->sun_family = AF_UNIX;
291         strcpy(addr->sun_path, client->hostname);
292
293         fd = socket(AF_UNIX, SOCK_STREAM, 0);
294         if (fd < 0) {
295                 log_err("fio: socket: %s\n", strerror(errno));
296                 return -1;
297         }
298
299         len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
300         if (connect(fd, (struct sockaddr *) addr, len) < 0) {
301                 log_err("fio: connect; %s\n", strerror(errno));
302                 close(fd);
303                 return -1;
304         }
305
306         return fd;
307 }
308
309 static int fio_client_connect(struct fio_client *client)
310 {
311         int fd;
312
313         dprint(FD_NET, "client: connect to host %s\n", client->hostname);
314
315         if (client->is_sock)
316                 fd = fio_client_connect_sock(client);
317         else
318                 fd = fio_client_connect_ip(client);
319
320         dprint(FD_NET, "client: %s connected %d\n", client->hostname, fd);
321
322         if (fd < 0)
323                 return 1;
324
325         client->fd = fd;
326         fio_client_add_hash(client);
327         client->state = Client_connected;
328         return 0;
329 }
330
331 void fio_clients_terminate(void)
332 {
333         struct flist_head *entry;
334         struct fio_client *client;
335
336         dprint(FD_NET, "client: terminate clients\n");
337
338         flist_for_each(entry, &client_list) {
339                 client = flist_entry(entry, struct fio_client, list);
340
341                 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_QUIT, 0, NULL);
342         }
343 }
344
345 static void sig_int(int sig)
346 {
347         dprint(FD_NET, "client: got signal %d\n", sig);
348         fio_clients_terminate();
349 }
350
351 static void client_signal_handler(void)
352 {
353         struct sigaction act;
354
355         memset(&act, 0, sizeof(act));
356         act.sa_handler = sig_int;
357         act.sa_flags = SA_RESTART;
358         sigaction(SIGINT, &act, NULL);
359
360         memset(&act, 0, sizeof(act));
361         act.sa_handler = sig_int;
362         act.sa_flags = SA_RESTART;
363         sigaction(SIGTERM, &act, NULL);
364 }
365
366 static void probe_client(struct fio_client *client)
367 {
368         dprint(FD_NET, "client: send probe\n");
369
370         fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_PROBE, 0, &client->cmd_list);
371 }
372
373 static int send_client_cmd_line(struct fio_client *client)
374 {
375         struct cmd_single_line_pdu *cslp;
376         struct cmd_line_pdu *clp;
377         unsigned long offset;
378         unsigned int *lens;
379         void *pdu;
380         size_t mem;
381         int i, ret;
382
383         dprint(FD_NET, "client: send cmdline %d\n", client->argc);
384
385         lens = malloc(client->argc * sizeof(unsigned int));
386
387         /*
388          * Find out how much mem we need
389          */
390         for (i = 0, mem = 0; i < client->argc; i++) {
391                 lens[i] = strlen(client->argv[i]) + 1;
392                 mem += lens[i];
393         }
394
395         /*
396          * We need one cmd_line_pdu, and argc number of cmd_single_line_pdu
397          */
398         mem += sizeof(*clp) + (client->argc * sizeof(*cslp));
399
400         pdu = malloc(mem);
401         clp = pdu;
402         offset = sizeof(*clp);
403
404         for (i = 0; i < client->argc; i++) {
405                 uint16_t arg_len = lens[i];
406
407                 cslp = pdu + offset;
408                 strcpy((char *) cslp->text, client->argv[i]);
409                 cslp->len = cpu_to_le16(arg_len);
410                 offset += sizeof(*cslp) + arg_len;
411         }
412
413         free(lens);
414         clp->lines = cpu_to_le16(client->argc);
415         ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOBLINE, pdu, mem, 0);
416         free(pdu);
417         return ret;
418 }
419
420 int fio_clients_connect(void)
421 {
422         struct fio_client *client;
423         struct flist_head *entry, *tmp;
424         int ret;
425
426 #ifdef WIN32
427         WSADATA wsd;
428         WSAStartup(MAKEWORD(2,2), &wsd);
429 #endif
430
431         dprint(FD_NET, "client: connect all\n");
432
433         client_signal_handler();
434
435         flist_for_each_safe(entry, tmp, &client_list) {
436                 client = flist_entry(entry, struct fio_client, list);
437
438                 ret = fio_client_connect(client);
439                 if (ret) {
440                         remove_client(client);
441                         continue;
442                 }
443
444                 probe_client(client);
445
446                 if (client->argc > 1)
447                         send_client_cmd_line(client);
448         }
449
450         return !nr_clients;
451 }
452
453 /*
454  * Send file contents to server backend. We could use sendfile(), but to remain
455  * more portable lets just read/write the darn thing.
456  */
457 static int fio_client_send_ini(struct fio_client *client, const char *filename)
458 {
459         struct stat sb;
460         char *p, *buf;
461         off_t len;
462         int fd, ret;
463
464         dprint(FD_NET, "send ini %s to %s\n", filename, client->hostname);
465
466         fd = open(filename, O_RDONLY);
467         if (fd < 0) {
468                 log_err("fio: job file <%s> open: %s\n", filename, strerror(errno));
469                 return 1;
470         }
471
472         if (fstat(fd, &sb) < 0) {
473                 log_err("fio: job file stat: %s\n", strerror(errno));
474                 close(fd);
475                 return 1;
476         }
477
478         buf = malloc(sb.st_size);
479
480         len = sb.st_size;
481         p = buf;
482         do {
483                 ret = read(fd, p, len);
484                 if (ret > 0) {
485                         len -= ret;
486                         if (!len)
487                                 break;
488                         p += ret;
489                         continue;
490                 } else if (!ret)
491                         break;
492                 else if (errno == EAGAIN || errno == EINTR)
493                         continue;
494         } while (1);
495
496         if (len) {
497                 log_err("fio: failed reading job file %s\n", filename);
498                 close(fd);
499                 free(buf);
500                 return 1;
501         }
502
503         client->sent_job = 1;
504         ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, buf, sb.st_size, 0);
505         free(buf);
506         close(fd);
507         return ret;
508 }
509
510 int fio_clients_send_ini(const char *filename)
511 {
512         struct fio_client *client;
513         struct flist_head *entry, *tmp;
514
515         flist_for_each_safe(entry, tmp, &client_list) {
516                 client = flist_entry(entry, struct fio_client, list);
517
518                 if (fio_client_send_ini(client, filename))
519                         remove_client(client);
520
521                 client->sent_job = 1;
522         }
523
524         return !nr_clients;
525 }
526
527 static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
528 {
529         dst->max_val    = le64_to_cpu(src->max_val);
530         dst->min_val    = le64_to_cpu(src->min_val);
531         dst->samples    = le64_to_cpu(src->samples);
532
533         /*
534          * Floats arrive as IEEE 754 encoded uint64_t, convert back to double
535          */
536         dst->mean.u.f   = fio_uint64_to_double(le64_to_cpu(dst->mean.u.i));
537         dst->S.u.f      = fio_uint64_to_double(le64_to_cpu(dst->S.u.i));
538 }
539
540 static void convert_ts(struct thread_stat *dst, struct thread_stat *src)
541 {
542         int i, j;
543
544         dst->error      = le32_to_cpu(src->error);
545         dst->groupid    = le32_to_cpu(src->groupid);
546         dst->pid        = le32_to_cpu(src->pid);
547         dst->members    = le32_to_cpu(src->members);
548
549         for (i = 0; i < 2; i++) {
550                 convert_io_stat(&dst->clat_stat[i], &src->clat_stat[i]);
551                 convert_io_stat(&dst->slat_stat[i], &src->slat_stat[i]);
552                 convert_io_stat(&dst->lat_stat[i], &src->lat_stat[i]);
553                 convert_io_stat(&dst->bw_stat[i], &src->bw_stat[i]);
554         }
555
556         dst->usr_time           = le64_to_cpu(src->usr_time);
557         dst->sys_time           = le64_to_cpu(src->sys_time);
558         dst->ctx                = le64_to_cpu(src->ctx);
559         dst->minf               = le64_to_cpu(src->minf);
560         dst->majf               = le64_to_cpu(src->majf);
561         dst->clat_percentiles   = le64_to_cpu(src->clat_percentiles);
562
563         for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
564                 fio_fp64_t *fps = &src->percentile_list[i];
565                 fio_fp64_t *fpd = &dst->percentile_list[i];
566
567                 fpd->u.f = fio_uint64_to_double(le64_to_cpu(fps->u.i));
568         }
569
570         for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
571                 dst->io_u_map[i]        = le32_to_cpu(src->io_u_map[i]);
572                 dst->io_u_submit[i]     = le32_to_cpu(src->io_u_submit[i]);
573                 dst->io_u_complete[i]   = le32_to_cpu(src->io_u_complete[i]);
574         }
575
576         for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
577                 dst->io_u_lat_u[i]      = le32_to_cpu(src->io_u_lat_u[i]);
578                 dst->io_u_lat_m[i]      = le32_to_cpu(src->io_u_lat_m[i]);
579         }
580
581         for (i = 0; i < 2; i++)
582                 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
583                         dst->io_u_plat[i][j] = le32_to_cpu(src->io_u_plat[i][j]);
584
585         for (i = 0; i < 3; i++) {
586                 dst->total_io_u[i]      = le64_to_cpu(src->total_io_u[i]);
587                 dst->short_io_u[i]      = le64_to_cpu(src->short_io_u[i]);
588         }
589
590         dst->total_submit       = le64_to_cpu(src->total_submit);
591         dst->total_complete     = le64_to_cpu(src->total_complete);
592
593         for (i = 0; i < 2; i++) {
594                 dst->io_bytes[i]        = le64_to_cpu(src->io_bytes[i]);
595                 dst->runtime[i]         = le64_to_cpu(src->runtime[i]);
596         }
597
598         dst->total_run_time     = le64_to_cpu(src->total_run_time);
599         dst->continue_on_error  = le16_to_cpu(src->continue_on_error);
600         dst->total_err_count    = le64_to_cpu(src->total_err_count);
601         dst->first_error        = le32_to_cpu(src->first_error);
602         dst->kb_base            = le32_to_cpu(src->kb_base);
603 }
604
605 static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
606 {
607         int i;
608
609         for (i = 0; i < 2; i++) {
610                 dst->max_run[i]         = le64_to_cpu(src->max_run[i]);
611                 dst->min_run[i]         = le64_to_cpu(src->min_run[i]);
612                 dst->max_bw[i]          = le64_to_cpu(src->max_bw[i]);
613                 dst->min_bw[i]          = le64_to_cpu(src->min_bw[i]);
614                 dst->io_kb[i]           = le64_to_cpu(src->io_kb[i]);
615                 dst->agg[i]             = le64_to_cpu(src->agg[i]);
616         }
617
618         dst->kb_base    = le32_to_cpu(src->kb_base);
619         dst->groupid    = le32_to_cpu(src->groupid);
620 }
621
622 static void handle_ts(struct fio_net_cmd *cmd)
623 {
624         struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
625
626         convert_ts(&p->ts, &p->ts);
627         convert_gs(&p->rs, &p->rs);
628
629         show_thread_status(&p->ts, &p->rs);
630
631         if (sum_stat_clients == 1)
632                 return;
633
634         sum_thread_stats(&client_ts, &p->ts, sum_stat_nr);
635         sum_group_stats(&client_gs, &p->rs);
636
637         client_ts.members++;
638         client_ts.groupid = p->ts.groupid;
639
640         if (++sum_stat_nr == sum_stat_clients) {
641                 strcpy(client_ts.name, "All clients");
642                 show_thread_status(&client_ts, &client_gs);
643         }
644 }
645
646 static void handle_gs(struct fio_net_cmd *cmd)
647 {
648         struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
649
650         convert_gs(gs, gs);
651         show_group_stats(gs);
652 }
653
654 static void convert_agg(struct disk_util_agg *agg)
655 {
656         int i;
657
658         for (i = 0; i < 2; i++) {
659                 agg->ios[i]     = le32_to_cpu(agg->ios[i]);
660                 agg->merges[i]  = le32_to_cpu(agg->merges[i]);
661                 agg->sectors[i] = le64_to_cpu(agg->sectors[i]);
662                 agg->ticks[i]   = le32_to_cpu(agg->ticks[i]);
663         }
664
665         agg->io_ticks           = le32_to_cpu(agg->io_ticks);
666         agg->time_in_queue      = le32_to_cpu(agg->time_in_queue);
667         agg->slavecount         = le32_to_cpu(agg->slavecount);
668         agg->max_util.u.f       = fio_uint64_to_double(__le64_to_cpu(agg->max_util.u.i));
669 }
670
671 static void convert_dus(struct disk_util_stat *dus)
672 {
673         int i;
674
675         for (i = 0; i < 2; i++) {
676                 dus->ios[i]     = le32_to_cpu(dus->ios[i]);
677                 dus->merges[i]  = le32_to_cpu(dus->merges[i]);
678                 dus->sectors[i] = le64_to_cpu(dus->sectors[i]);
679                 dus->ticks[i]   = le32_to_cpu(dus->ticks[i]);
680         }
681
682         dus->io_ticks           = le32_to_cpu(dus->io_ticks);
683         dus->time_in_queue      = le32_to_cpu(dus->time_in_queue);
684         dus->msec               = le64_to_cpu(dus->msec);
685 }
686
687 static void handle_text(struct fio_client *client, struct fio_net_cmd *cmd)
688 {
689         struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
690         const char *buf = (const char *) pdu->buf;
691         const char *name;
692         int fio_unused ret;
693
694         name = client->name ? client->name : client->hostname;
695
696         if (!client->skip_newline)
697                 fprintf(f_out, "<%s> ", name);
698         ret = fwrite(buf, pdu->buf_len, 1, f_out);
699         fflush(f_out);
700         client->skip_newline = strchr(buf, '\n') == NULL;
701 }
702
703
704 static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd)
705 {
706         struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
707
708         convert_dus(&du->dus);
709         convert_agg(&du->agg);
710
711         if (!client->disk_stats_shown) {
712                 client->disk_stats_shown = 1;
713                 log_info("\nDisk stats (read/write):\n");
714         }
715
716         print_disk_util(&du->dus, &du->agg, terse_output);
717 }
718
719 void fio_client_convert_jobs_eta(struct jobs_eta *je)
720 {
721         int i;
722
723         je->nr_running          = le32_to_cpu(je->nr_running);
724         je->nr_ramp             = le32_to_cpu(je->nr_ramp);
725         je->nr_pending          = le32_to_cpu(je->nr_pending);
726         je->files_open          = le32_to_cpu(je->files_open);
727
728         for (i = 0; i < 2; i++) {
729                 je->m_rate[i]           = le32_to_cpu(je->m_rate[i]);
730                 je->t_rate[i]           = le32_to_cpu(je->t_rate[i]);
731                 je->m_iops[i]           = le32_to_cpu(je->m_iops[i]);
732                 je->t_iops[i]           = le32_to_cpu(je->t_iops[i]);
733                 je->rate[i]     = le32_to_cpu(je->rate[i]);
734                 je->iops[i]     = le32_to_cpu(je->iops[i]);
735         }
736
737         je->elapsed_sec         = le64_to_cpu(je->elapsed_sec);
738         je->eta_sec             = le64_to_cpu(je->eta_sec);
739 }
740
741 void fio_client_sum_jobs_eta(struct jobs_eta *dst, struct jobs_eta *je)
742 {
743         int i;
744
745         dst->nr_running         += je->nr_running;
746         dst->nr_ramp            += je->nr_ramp;
747         dst->nr_pending         += je->nr_pending;
748         dst->files_open         += je->files_open;
749
750         for (i = 0; i < 2; i++) {
751                 dst->m_rate[i]  += je->m_rate[i];
752                 dst->t_rate[i]  += je->t_rate[i];
753                 dst->m_iops[i]  += je->m_iops[i];
754                 dst->t_iops[i]  += je->t_iops[i];
755                 dst->rate[i]    += je->rate[i];
756                 dst->iops[i]    += je->iops[i];
757         }
758
759         dst->elapsed_sec        += je->elapsed_sec;
760
761         if (je->eta_sec > dst->eta_sec)
762                 dst->eta_sec = je->eta_sec;
763 }
764
765 void fio_client_dec_jobs_eta(struct client_eta *eta, void (*fn)(struct jobs_eta *))
766 {
767         if (!--eta->pending) {
768                 fn(&eta->eta);
769                 free(eta);
770         }
771 }
772
773 static void remove_reply_cmd(struct fio_client *client, struct fio_net_cmd *cmd)
774 {
775         struct fio_net_int_cmd *icmd = NULL;
776         struct flist_head *entry;
777
778         flist_for_each(entry, &client->cmd_list) {
779                 icmd = flist_entry(entry, struct fio_net_int_cmd, list);
780
781                 if (cmd->tag == (uintptr_t) icmd)
782                         break;
783
784                 icmd = NULL;
785         }
786
787         if (!icmd) {
788                 log_err("fio: client: unable to find matching tag\n");
789                 return;
790         }
791
792         flist_del(&icmd->list);
793         cmd->tag = icmd->saved_tag;
794         free(icmd);
795 }
796
797 static void handle_eta(struct fio_client *client, struct fio_net_cmd *cmd)
798 {
799         struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
800         struct client_eta *eta = (struct client_eta *) (uintptr_t) cmd->tag;
801
802         dprint(FD_NET, "client: got eta tag %p, %d\n", eta, eta->pending);
803
804         assert(client->eta_in_flight == eta);
805
806         client->eta_in_flight = NULL;
807         flist_del_init(&client->eta_list);
808
809         fio_client_convert_jobs_eta(je);
810         fio_client_sum_jobs_eta(&eta->eta, je);
811         fio_client_dec_jobs_eta(eta, display_thread_status);
812 }
813
814 static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd)
815 {
816         struct cmd_probe_pdu *probe = (struct cmd_probe_pdu *) cmd->payload;
817         const char *os, *arch;
818         char bit[16];
819
820         os = fio_get_os_string(probe->os);
821         if (!os)
822                 os = "unknown";
823
824         arch = fio_get_arch_string(probe->arch);
825         if (!arch)
826                 os = "unknown";
827
828         sprintf(bit, "%d-bit", probe->bpp * 8);
829
830         log_info("hostname=%s, be=%u, %s, os=%s, arch=%s, fio=%u.%u.%u\n",
831                 probe->hostname, probe->bigendian, bit, os, arch,
832                 probe->fio_major, probe->fio_minor, probe->fio_patch);
833
834         if (!client->name)
835                 client->name = strdup((char *) probe->hostname);
836 }
837
838 static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd)
839 {
840         struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
841
842         client->state = Client_started;
843         client->jobs = le32_to_cpu(pdu->jobs);
844 }
845
846 static void handle_stop(struct fio_client *client, struct fio_net_cmd *cmd)
847 {
848         struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
849
850         client->state = Client_stopped;
851         client->error = le32_to_cpu(pdu->error);
852
853         if (client->error)
854                 log_info("client <%s>: exited with error %d\n", client->hostname, client->error);
855 }
856
857 static void convert_text(struct fio_net_cmd *cmd)
858 {
859         struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
860
861         pdu->level      = le32_to_cpu(pdu->level);
862         pdu->buf_len    = le32_to_cpu(pdu->buf_len);
863         pdu->log_sec    = le64_to_cpu(pdu->log_sec);
864         pdu->log_usec   = le64_to_cpu(pdu->log_usec);
865 }
866
867 int fio_handle_client(struct fio_client *client, struct client_ops *ops)
868 {
869         struct fio_net_cmd *cmd;
870
871         dprint(FD_NET, "client: handle %s\n", client->hostname);
872
873         cmd = fio_net_recv_cmd(client->fd);
874         if (!cmd)
875                 return 0;
876
877         dprint(FD_NET, "client: got cmd op %s from %s\n",
878                                 fio_server_op(cmd->opcode), client->hostname);
879
880         switch (cmd->opcode) {
881         case FIO_NET_CMD_QUIT:
882                 if (ops->quit)
883                         ops->quit(client);
884                 remove_client(client);
885                 free(cmd);
886                 break;
887         case FIO_NET_CMD_TEXT:
888                 convert_text(cmd);
889                 ops->text_op(client, cmd);
890                 free(cmd);
891                 break;
892         case FIO_NET_CMD_DU:
893                 ops->disk_util(client, cmd);
894                 free(cmd);
895                 break;
896         case FIO_NET_CMD_TS:
897                 ops->thread_status(cmd);
898                 free(cmd);
899                 break;
900         case FIO_NET_CMD_GS:
901                 ops->group_stats(cmd);
902                 free(cmd);
903                 break;
904         case FIO_NET_CMD_ETA:
905                 remove_reply_cmd(client, cmd);
906                 ops->eta(client, cmd);
907                 free(cmd);
908                 break;
909         case FIO_NET_CMD_PROBE:
910                 remove_reply_cmd(client, cmd);
911                 ops->probe(client, cmd);
912                 free(cmd);
913                 break;
914         case FIO_NET_CMD_RUN:
915                 client->state = Client_running;
916                 free(cmd);
917                 break;
918         case FIO_NET_CMD_START:
919                 handle_start(client, cmd);
920                 free(cmd);
921                 break;
922         case FIO_NET_CMD_STOP:
923                 handle_stop(client, cmd);
924                 free(cmd);
925                 break;
926         case FIO_NET_CMD_ADD_JOB:
927                 if (ops->add_job)
928                         ops->add_job(client, cmd);
929                 free(cmd);
930                 break;
931         default:
932                 log_err("fio: unknown client op: %s\n", fio_server_op(cmd->opcode));
933                 free(cmd);
934                 break;
935         }
936
937         return 1;
938 }
939
940 static void request_client_etas(void)
941 {
942         struct fio_client *client;
943         struct flist_head *entry;
944         struct client_eta *eta;
945         int skipped = 0;
946
947         dprint(FD_NET, "client: request eta (%d)\n", nr_clients);
948
949         eta = malloc(sizeof(*eta));
950         memset(&eta->eta, 0, sizeof(eta->eta));
951         eta->pending = nr_clients;
952
953         flist_for_each(entry, &client_list) {
954                 client = flist_entry(entry, struct fio_client, list);
955
956                 if (!flist_empty(&client->eta_list)) {
957                         skipped++;
958                         continue;
959                 }
960                 if (client->state != Client_running)
961                         continue;
962
963                 assert(!client->eta_in_flight);
964                 flist_add_tail(&client->eta_list, &eta_list);
965                 client->eta_in_flight = eta;
966                 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_SEND_ETA,
967                                         (uintptr_t) eta, &client->cmd_list);
968         }
969
970         while (skipped--)
971                 fio_client_dec_jobs_eta(eta, display_thread_status);
972
973         dprint(FD_NET, "client: requested eta tag %p\n", eta);
974 }
975
976 static int client_check_cmd_timeout(struct fio_client *client,
977                                     struct timeval *now)
978 {
979         struct fio_net_int_cmd *cmd;
980         struct flist_head *entry, *tmp;
981         int ret = 0;
982
983         flist_for_each_safe(entry, tmp, &client->cmd_list) {
984                 cmd = flist_entry(entry, struct fio_net_int_cmd, list);
985
986                 if (mtime_since(&cmd->tv, now) < FIO_NET_CLIENT_TIMEOUT)
987                         continue;
988
989                 log_err("fio: client %s, timeout on cmd %s\n", client->hostname,
990                                                 fio_server_op(cmd->cmd.opcode));
991                 flist_del(&cmd->list);
992                 free(cmd);
993                 ret = 1;
994         }
995
996         return flist_empty(&client->cmd_list) && ret;
997 }
998
999 static int fio_client_timed_out(struct client_ops *ops)
1000 {
1001         struct fio_client *client;
1002         struct flist_head *entry, *tmp;
1003         struct timeval tv;
1004         int ret = 0;
1005
1006         gettimeofday(&tv, NULL);
1007
1008         flist_for_each_safe(entry, tmp, &client_list) {
1009                 client = flist_entry(entry, struct fio_client, list);
1010
1011                 if (flist_empty(&client->cmd_list))
1012                         continue;
1013
1014                 if (!client_check_cmd_timeout(client, &tv))
1015                         continue;
1016
1017                 if (ops->timed_out)
1018                         ops->timed_out(client);
1019                 else
1020                         log_err("fio: client %s timed out\n", client->hostname);
1021
1022                 remove_client(client);
1023                 ret = 1;
1024         }
1025
1026         return ret;
1027 }
1028
1029 int fio_handle_clients(struct client_ops *ops)
1030 {
1031         struct pollfd *pfds;
1032         int i, ret = 0, retval = 0;
1033
1034         gettimeofday(&eta_tv, NULL);
1035
1036         pfds = malloc(nr_clients * sizeof(struct pollfd));
1037
1038         sum_stat_clients = nr_clients;
1039         init_thread_stat(&client_ts);
1040         init_group_run_stat(&client_gs);
1041
1042         while (!exit_backend && nr_clients) {
1043                 struct flist_head *entry, *tmp;
1044                 struct fio_client *client;
1045
1046                 i = 0;
1047                 flist_for_each_safe(entry, tmp, &client_list) {
1048                         client = flist_entry(entry, struct fio_client, list);
1049
1050                         if (!client->sent_job && !ops->stay_connected &&
1051                             flist_empty(&client->cmd_list)) {
1052                                 remove_client(client);
1053                                 continue;
1054                         }
1055
1056                         pfds[i].fd = client->fd;
1057                         pfds[i].events = POLLIN;
1058                         i++;
1059                 }
1060
1061                 if (!nr_clients)
1062                         break;
1063
1064                 assert(i == nr_clients);
1065
1066                 do {
1067                         struct timeval tv;
1068
1069                         gettimeofday(&tv, NULL);
1070                         if (mtime_since(&eta_tv, &tv) >= 900) {
1071                                 request_client_etas();
1072                                 memcpy(&eta_tv, &tv, sizeof(tv));
1073
1074                                 if (fio_client_timed_out(ops))
1075                                         break;
1076                         }
1077
1078                         ret = poll(pfds, nr_clients, 100);
1079                         if (ret < 0) {
1080                                 if (errno == EINTR)
1081                                         continue;
1082                                 log_err("fio: poll clients: %s\n", strerror(errno));
1083                                 break;
1084                         } else if (!ret)
1085                                 continue;
1086                 } while (ret <= 0);
1087
1088                 for (i = 0; i < nr_clients; i++) {
1089                         if (!(pfds[i].revents & POLLIN))
1090                                 continue;
1091
1092                         client = find_client_by_fd(pfds[i].fd);
1093                         if (!client) {
1094                                 log_err("fio: unknown client fd %d\n", pfds[i].fd);
1095                                 continue;
1096                         }
1097                         if (!fio_handle_client(client, ops)) {
1098                                 log_info("client: host=%s disconnected\n",
1099                                                 client->hostname);
1100                                 remove_client(client);
1101                                 retval = 1;
1102                         } else if (client->error)
1103                                 retval = 1;
1104                 }
1105         }
1106
1107         free(pfds);
1108         return retval;
1109 }