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