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