engines/http: work-around crash in certain libssl versions
[fio.git] / client.c
... / ...
CommitLineData
1#include <stdio.h>
2#include <stdlib.h>
3#include <unistd.h>
4#include <errno.h>
5#include <fcntl.h>
6#include <poll.h>
7#include <sys/types.h>
8#include <sys/stat.h>
9#include <sys/socket.h>
10#include <sys/un.h>
11#include <netinet/in.h>
12#include <arpa/inet.h>
13#include <netdb.h>
14#include <signal.h>
15#ifdef CONFIG_ZLIB
16#include <zlib.h>
17#endif
18
19#include "fio.h"
20#include "client.h"
21#include "server.h"
22#include "flist.h"
23#include "hash.h"
24#include "verify-state.h"
25
26static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd);
27static void handle_ts(struct fio_client *client, struct fio_net_cmd *cmd);
28static void handle_gs(struct fio_client *client, struct fio_net_cmd *cmd);
29static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd);
30static void handle_text(struct fio_client *client, struct fio_net_cmd *cmd);
31static void handle_stop(struct fio_client *client);
32static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd);
33
34static void convert_text(struct fio_net_cmd *cmd);
35static void client_display_thread_status(struct jobs_eta *je);
36
37struct client_ops fio_client_ops = {
38 .text = handle_text,
39 .disk_util = handle_du,
40 .thread_status = handle_ts,
41 .group_stats = handle_gs,
42 .stop = handle_stop,
43 .start = handle_start,
44 .eta = client_display_thread_status,
45 .probe = handle_probe,
46 .eta_msec = FIO_CLIENT_DEF_ETA_MSEC,
47 .client_type = FIO_CLIENT_TYPE_CLI,
48};
49
50static struct timespec eta_ts;
51
52static FLIST_HEAD(client_list);
53static FLIST_HEAD(eta_list);
54
55static FLIST_HEAD(arg_list);
56
57struct thread_stat client_ts;
58struct group_run_stats client_gs;
59int sum_stat_clients;
60
61static int sum_stat_nr;
62static struct json_object *root = NULL;
63static struct json_object *job_opt_object = NULL;
64static struct json_array *clients_array = NULL;
65static struct json_array *du_array = NULL;
66
67static int error_clients;
68
69#define FIO_CLIENT_HASH_BITS 7
70#define FIO_CLIENT_HASH_SZ (1 << FIO_CLIENT_HASH_BITS)
71#define FIO_CLIENT_HASH_MASK (FIO_CLIENT_HASH_SZ - 1)
72static struct flist_head client_hash[FIO_CLIENT_HASH_SZ];
73
74static struct cmd_iolog_pdu *convert_iolog(struct fio_net_cmd *, bool *);
75
76static void fio_client_add_hash(struct fio_client *client)
77{
78 int bucket = hash_long(client->fd, FIO_CLIENT_HASH_BITS);
79
80 bucket &= FIO_CLIENT_HASH_MASK;
81 flist_add(&client->hash_list, &client_hash[bucket]);
82}
83
84static void fio_client_remove_hash(struct fio_client *client)
85{
86 if (!flist_empty(&client->hash_list))
87 flist_del_init(&client->hash_list);
88}
89
90static void fio_init fio_client_hash_init(void)
91{
92 int i;
93
94 for (i = 0; i < FIO_CLIENT_HASH_SZ; i++)
95 INIT_FLIST_HEAD(&client_hash[i]);
96}
97
98static int read_data(int fd, void *data, size_t size)
99{
100 ssize_t ret;
101
102 while (size) {
103 ret = read(fd, data, size);
104 if (ret < 0) {
105 if (errno == EAGAIN || errno == EINTR)
106 continue;
107 break;
108 } else if (!ret)
109 break;
110 else {
111 data += ret;
112 size -= ret;
113 }
114 }
115
116 if (size)
117 return EAGAIN;
118
119 return 0;
120}
121
122static int read_ini_data(int fd, void *data, size_t size)
123{
124 char *p = data;
125 int ret = 0;
126 FILE *fp;
127 int dupfd;
128
129 dupfd = dup(fd);
130 if (dupfd < 0)
131 return errno;
132
133 fp = fdopen(dupfd, "r");
134 if (!fp) {
135 ret = errno;
136 close(dupfd);
137 goto out;
138 }
139
140 while (1) {
141 ssize_t len;
142 char buf[OPT_LEN_MAX+1], *sub;
143
144 if (!fgets(buf, sizeof(buf), fp)) {
145 if (ferror(fp)) {
146 if (errno == EAGAIN || errno == EINTR)
147 continue;
148 ret = errno;
149 }
150 break;
151 }
152
153 sub = fio_option_dup_subs(buf);
154 len = strlen(sub);
155 if (len + 1 > size) {
156 log_err("fio: no space left to read data\n");
157 free(sub);
158 ret = ENOSPC;
159 break;
160 }
161
162 memcpy(p, sub, len);
163 free(sub);
164 p += len;
165 *p = '\0';
166 size -= len;
167 }
168
169 fclose(fp);
170out:
171 return ret;
172}
173
174static void fio_client_json_init(void)
175{
176 char time_buf[32];
177 time_t time_p;
178
179 if (!(output_format & FIO_OUTPUT_JSON))
180 return;
181
182 time(&time_p);
183 os_ctime_r((const time_t *) &time_p, time_buf, sizeof(time_buf));
184 time_buf[strlen(time_buf) - 1] = '\0';
185
186 root = json_create_object();
187 json_object_add_value_string(root, "fio version", fio_version_string);
188 json_object_add_value_int(root, "timestamp", time_p);
189 json_object_add_value_string(root, "time", time_buf);
190
191 job_opt_object = json_create_object();
192 json_object_add_value_object(root, "global options", job_opt_object);
193 clients_array = json_create_array();
194 json_object_add_value_array(root, "client_stats", clients_array);
195 du_array = json_create_array();
196 json_object_add_value_array(root, "disk_util", du_array);
197}
198
199static void fio_client_json_fini(void)
200{
201 struct buf_output out;
202
203 if (!root)
204 return;
205
206 buf_output_init(&out);
207
208 __log_buf(&out, "\n");
209 json_print_object(root, &out);
210 __log_buf(&out, "\n");
211 log_info_buf(out.buf, out.buflen);
212
213 buf_output_free(&out);
214
215 json_free_object(root);
216 root = NULL;
217 job_opt_object = NULL;
218 clients_array = NULL;
219 du_array = NULL;
220}
221
222static struct fio_client *find_client_by_fd(int fd)
223{
224 int bucket = hash_long(fd, FIO_CLIENT_HASH_BITS) & FIO_CLIENT_HASH_MASK;
225 struct fio_client *client;
226 struct flist_head *entry;
227
228 flist_for_each(entry, &client_hash[bucket]) {
229 client = flist_entry(entry, struct fio_client, hash_list);
230
231 if (client->fd == fd) {
232 client->refs++;
233 return client;
234 }
235 }
236
237 return NULL;
238}
239
240void fio_put_client(struct fio_client *client)
241{
242 if (--client->refs)
243 return;
244
245 log_info_buf(client->buf.buf, client->buf.buflen);
246 buf_output_free(&client->buf);
247
248 free(client->hostname);
249 if (client->argv)
250 free(client->argv);
251 if (client->name)
252 free(client->name);
253 while (client->nr_files) {
254 struct client_file *cf = &client->files[--client->nr_files];
255
256 free(cf->file);
257 }
258 if (client->files)
259 free(client->files);
260 if (client->opt_lists)
261 free(client->opt_lists);
262
263 if (!client->did_stat)
264 sum_stat_clients--;
265
266 if (client->error)
267 error_clients++;
268
269 free(client);
270}
271
272static int fio_client_dec_jobs_eta(struct client_eta *eta, client_eta_op eta_fn)
273{
274 if (!--eta->pending) {
275 eta_fn(&eta->eta);
276 free(eta);
277 return 0;
278 }
279
280 return 1;
281}
282
283static void fio_drain_client_text(struct fio_client *client)
284{
285 do {
286 struct fio_net_cmd *cmd;
287
288 cmd = fio_net_recv_cmd(client->fd, false);
289 if (!cmd)
290 break;
291
292 if (cmd->opcode == FIO_NET_CMD_TEXT) {
293 convert_text(cmd);
294 client->ops->text(client, cmd);
295 }
296
297 free(cmd);
298 } while (1);
299}
300
301static void remove_client(struct fio_client *client)
302{
303 assert(client->refs);
304
305 dprint(FD_NET, "client: removed <%s>\n", client->hostname);
306
307 fio_drain_client_text(client);
308
309 if (!flist_empty(&client->list))
310 flist_del_init(&client->list);
311
312 fio_client_remove_hash(client);
313
314 if (!flist_empty(&client->eta_list)) {
315 flist_del_init(&client->eta_list);
316 fio_client_dec_jobs_eta(client->eta_in_flight, client->ops->eta);
317 }
318
319 close(client->fd);
320 client->fd = -1;
321
322 if (client->ops->removed)
323 client->ops->removed(client);
324
325 nr_clients--;
326 fio_put_client(client);
327}
328
329struct fio_client *fio_get_client(struct fio_client *client)
330{
331 client->refs++;
332 return client;
333}
334
335static void __fio_client_add_cmd_option(struct fio_client *client,
336 const char *opt)
337{
338 int index;
339
340 index = client->argc++;
341 client->argv = realloc(client->argv, sizeof(char *) * client->argc);
342 client->argv[index] = strdup(opt);
343 dprint(FD_NET, "client: add cmd %d: %s\n", index, opt);
344}
345
346void fio_client_add_cmd_option(void *cookie, const char *opt)
347{
348 struct fio_client *client = cookie;
349 struct flist_head *entry;
350
351 if (!client || !opt)
352 return;
353
354 __fio_client_add_cmd_option(client, opt);
355
356 /*
357 * Duplicate arguments to shared client group
358 */
359 flist_for_each(entry, &arg_list) {
360 client = flist_entry(entry, struct fio_client, arg_list);
361
362 __fio_client_add_cmd_option(client, opt);
363 }
364}
365
366static struct fio_client *get_new_client(void)
367{
368 struct fio_client *client;
369
370 client = malloc(sizeof(*client));
371 memset(client, 0, sizeof(*client));
372
373 INIT_FLIST_HEAD(&client->list);
374 INIT_FLIST_HEAD(&client->hash_list);
375 INIT_FLIST_HEAD(&client->arg_list);
376 INIT_FLIST_HEAD(&client->eta_list);
377 INIT_FLIST_HEAD(&client->cmd_list);
378
379 buf_output_init(&client->buf);
380
381 return client;
382}
383
384struct fio_client *fio_client_add_explicit(struct client_ops *ops,
385 const char *hostname, int type,
386 int port)
387{
388 struct fio_client *client;
389
390 client = get_new_client();
391
392 client->hostname = strdup(hostname);
393
394 if (type == Fio_client_socket)
395 client->is_sock = true;
396 else {
397 int ipv6;
398
399 ipv6 = type == Fio_client_ipv6;
400 if (fio_server_parse_host(hostname, ipv6,
401 &client->addr.sin_addr,
402 &client->addr6.sin6_addr))
403 goto err;
404
405 client->port = port;
406 }
407
408 client->fd = -1;
409 client->ops = ops;
410 client->refs = 1;
411 client->type = ops->client_type;
412
413 __fio_client_add_cmd_option(client, "fio");
414
415 flist_add(&client->list, &client_list);
416 nr_clients++;
417 dprint(FD_NET, "client: added <%s>\n", client->hostname);
418 return client;
419err:
420 free(client);
421 return NULL;
422}
423
424int fio_client_add_ini_file(void *cookie, const char *ini_file, bool remote)
425{
426 struct fio_client *client = cookie;
427 struct client_file *cf;
428 size_t new_size;
429 void *new_files;
430
431 if (!client)
432 return 1;
433
434 dprint(FD_NET, "client <%s>: add ini %s\n", client->hostname, ini_file);
435
436 new_size = (client->nr_files + 1) * sizeof(struct client_file);
437 new_files = realloc(client->files, new_size);
438 if (!new_files)
439 return 1;
440
441 client->files = new_files;
442 cf = &client->files[client->nr_files];
443 cf->file = strdup(ini_file);
444 cf->remote = remote;
445 client->nr_files++;
446 return 0;
447}
448
449int fio_client_add(struct client_ops *ops, const char *hostname, void **cookie)
450{
451 struct fio_client *existing = *cookie;
452 struct fio_client *client;
453
454 if (existing) {
455 /*
456 * We always add our "exec" name as the option, hence 1
457 * means empty.
458 */
459 if (existing->argc == 1)
460 flist_add_tail(&existing->arg_list, &arg_list);
461 else {
462 while (!flist_empty(&arg_list))
463 flist_del_init(arg_list.next);
464 }
465 }
466
467 client = get_new_client();
468
469 if (fio_server_parse_string(hostname, &client->hostname,
470 &client->is_sock, &client->port,
471 &client->addr.sin_addr,
472 &client->addr6.sin6_addr,
473 &client->ipv6))
474 return -1;
475
476 client->fd = -1;
477 client->ops = ops;
478 client->refs = 1;
479 client->type = ops->client_type;
480
481 __fio_client_add_cmd_option(client, "fio");
482
483 flist_add(&client->list, &client_list);
484 nr_clients++;
485 dprint(FD_NET, "client: added <%s>\n", client->hostname);
486 *cookie = client;
487 return 0;
488}
489
490static const char *server_name(struct fio_client *client, char *buf,
491 size_t bufsize)
492{
493 const char *from;
494
495 if (client->ipv6)
496 from = inet_ntop(AF_INET6, (struct sockaddr *) &client->addr6.sin6_addr, buf, bufsize);
497 else if (client->is_sock)
498 from = "sock";
499 else
500 from = inet_ntop(AF_INET, (struct sockaddr *) &client->addr.sin_addr, buf, bufsize);
501
502 return from;
503}
504
505static void probe_client(struct fio_client *client)
506{
507 struct cmd_client_probe_pdu pdu;
508 const char *sname;
509 uint64_t tag;
510 char buf[64];
511
512 dprint(FD_NET, "client: send probe\n");
513
514#ifdef CONFIG_ZLIB
515 pdu.flags = __le64_to_cpu(FIO_PROBE_FLAG_ZLIB);
516#else
517 pdu.flags = 0;
518#endif
519
520 sname = server_name(client, buf, sizeof(buf));
521 memset(pdu.server, 0, sizeof(pdu.server));
522 strncpy((char *) pdu.server, sname, sizeof(pdu.server) - 1);
523
524 fio_net_send_cmd(client->fd, FIO_NET_CMD_PROBE, &pdu, sizeof(pdu), &tag, &client->cmd_list);
525}
526
527static int fio_client_connect_ip(struct fio_client *client)
528{
529 struct sockaddr *addr;
530 socklen_t socklen;
531 int fd, domain;
532
533 if (client->ipv6) {
534 client->addr6.sin6_family = AF_INET6;
535 client->addr6.sin6_port = htons(client->port);
536 domain = AF_INET6;
537 addr = (struct sockaddr *) &client->addr6;
538 socklen = sizeof(client->addr6);
539 } else {
540 client->addr.sin_family = AF_INET;
541 client->addr.sin_port = htons(client->port);
542 domain = AF_INET;
543 addr = (struct sockaddr *) &client->addr;
544 socklen = sizeof(client->addr);
545 }
546
547 fd = socket(domain, SOCK_STREAM, 0);
548 if (fd < 0) {
549 int ret = -errno;
550
551 log_err("fio: socket: %s\n", strerror(errno));
552 return ret;
553 }
554
555 if (connect(fd, addr, socklen) < 0) {
556 int ret = -errno;
557
558 log_err("fio: connect: %s\n", strerror(errno));
559 log_err("fio: failed to connect to %s:%u\n", client->hostname,
560 client->port);
561 close(fd);
562 return ret;
563 }
564
565 return fd;
566}
567
568static int fio_client_connect_sock(struct fio_client *client)
569{
570 struct sockaddr_un *addr = &client->addr_un;
571 socklen_t len;
572 int fd;
573
574 memset(addr, 0, sizeof(*addr));
575 addr->sun_family = AF_UNIX;
576 strncpy(addr->sun_path, client->hostname, sizeof(addr->sun_path) - 1);
577
578 fd = socket(AF_UNIX, SOCK_STREAM, 0);
579 if (fd < 0) {
580 int ret = -errno;
581
582 log_err("fio: socket: %s\n", strerror(errno));
583 return ret;
584 }
585
586 len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
587 if (connect(fd, (struct sockaddr *) addr, len) < 0) {
588 int ret = -errno;
589
590 log_err("fio: connect; %s\n", strerror(errno));
591 close(fd);
592 return ret;
593 }
594
595 return fd;
596}
597
598int fio_client_connect(struct fio_client *client)
599{
600 int fd;
601
602 dprint(FD_NET, "client: connect to host %s\n", client->hostname);
603
604 if (client->is_sock)
605 fd = fio_client_connect_sock(client);
606 else
607 fd = fio_client_connect_ip(client);
608
609 dprint(FD_NET, "client: %s connected %d\n", client->hostname, fd);
610
611 if (fd < 0)
612 return fd;
613
614 client->fd = fd;
615 fio_client_add_hash(client);
616 client->state = Client_connected;
617
618 probe_client(client);
619 return 0;
620}
621
622int fio_client_terminate(struct fio_client *client)
623{
624 return fio_net_send_quit(client->fd);
625}
626
627static void fio_clients_terminate(void)
628{
629 struct flist_head *entry;
630 struct fio_client *client;
631
632 dprint(FD_NET, "client: terminate clients\n");
633
634 flist_for_each(entry, &client_list) {
635 client = flist_entry(entry, struct fio_client, list);
636 fio_client_terminate(client);
637 }
638}
639
640static void sig_int(int sig)
641{
642 dprint(FD_NET, "client: got signal %d\n", sig);
643 fio_clients_terminate();
644}
645
646static void client_signal_handler(void)
647{
648 struct sigaction act;
649
650 memset(&act, 0, sizeof(act));
651 act.sa_handler = sig_int;
652 act.sa_flags = SA_RESTART;
653 sigaction(SIGINT, &act, NULL);
654
655 memset(&act, 0, sizeof(act));
656 act.sa_handler = sig_int;
657 act.sa_flags = SA_RESTART;
658 sigaction(SIGTERM, &act, NULL);
659
660/* Windows uses SIGBREAK as a quit signal from other applications */
661#ifdef WIN32
662 memset(&act, 0, sizeof(act));
663 act.sa_handler = sig_int;
664 act.sa_flags = SA_RESTART;
665 sigaction(SIGBREAK, &act, NULL);
666#endif
667
668 memset(&act, 0, sizeof(act));
669 act.sa_handler = sig_show_status;
670 act.sa_flags = SA_RESTART;
671 sigaction(SIGUSR1, &act, NULL);
672}
673
674static int send_client_cmd_line(struct fio_client *client)
675{
676 struct cmd_single_line_pdu *cslp;
677 struct cmd_line_pdu *clp;
678 unsigned long offset;
679 unsigned int *lens;
680 void *pdu;
681 size_t mem;
682 int i, ret;
683
684 dprint(FD_NET, "client: send cmdline %d\n", client->argc);
685
686 lens = malloc(client->argc * sizeof(unsigned int));
687
688 /*
689 * Find out how much mem we need
690 */
691 for (i = 0, mem = 0; i < client->argc; i++) {
692 lens[i] = strlen(client->argv[i]) + 1;
693 mem += lens[i];
694 }
695
696 /*
697 * We need one cmd_line_pdu, and argc number of cmd_single_line_pdu
698 */
699 mem += sizeof(*clp) + (client->argc * sizeof(*cslp));
700
701 pdu = malloc(mem);
702 clp = pdu;
703 offset = sizeof(*clp);
704
705 for (i = 0; i < client->argc; i++) {
706 uint16_t arg_len = lens[i];
707
708 cslp = pdu + offset;
709 strcpy((char *) cslp->text, client->argv[i]);
710 cslp->len = cpu_to_le16(arg_len);
711 offset += sizeof(*cslp) + arg_len;
712 }
713
714 free(lens);
715 clp->lines = cpu_to_le16(client->argc);
716 clp->client_type = __cpu_to_le16(client->type);
717 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOBLINE, pdu, mem, NULL, NULL);
718 free(pdu);
719 return ret;
720}
721
722int fio_clients_connect(void)
723{
724 struct fio_client *client;
725 struct flist_head *entry, *tmp;
726 int ret;
727
728#ifdef WIN32
729 WSADATA wsd;
730 WSAStartup(MAKEWORD(2, 2), &wsd);
731#endif
732
733 dprint(FD_NET, "client: connect all\n");
734
735 client_signal_handler();
736
737 flist_for_each_safe(entry, tmp, &client_list) {
738 client = flist_entry(entry, struct fio_client, list);
739
740 ret = fio_client_connect(client);
741 if (ret) {
742 remove_client(client);
743 continue;
744 }
745
746 if (client->argc > 1)
747 send_client_cmd_line(client);
748 }
749
750 return !nr_clients;
751}
752
753int fio_start_client(struct fio_client *client)
754{
755 dprint(FD_NET, "client: start %s\n", client->hostname);
756 return fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_RUN, 0, NULL);
757}
758
759int fio_start_all_clients(void)
760{
761 struct fio_client *client;
762 struct flist_head *entry, *tmp;
763 int ret;
764
765 dprint(FD_NET, "client: start all\n");
766
767 fio_client_json_init();
768
769 flist_for_each_safe(entry, tmp, &client_list) {
770 client = flist_entry(entry, struct fio_client, list);
771
772 ret = fio_start_client(client);
773 if (ret) {
774 remove_client(client);
775 continue;
776 }
777 }
778
779 return flist_empty(&client_list);
780}
781
782static int __fio_client_send_remote_ini(struct fio_client *client,
783 const char *filename)
784{
785 struct cmd_load_file_pdu *pdu;
786 size_t p_size;
787 int ret;
788
789 dprint(FD_NET, "send remote ini %s to %s\n", filename, client->hostname);
790
791 p_size = sizeof(*pdu) + strlen(filename) + 1;
792 pdu = malloc(p_size);
793 memset(pdu, 0, p_size);
794 pdu->name_len = strlen(filename);
795 strcpy((char *) pdu->file, filename);
796 pdu->client_type = cpu_to_le16((uint16_t) client->type);
797
798 client->sent_job = true;
799 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_LOAD_FILE, pdu, p_size,NULL, NULL);
800 free(pdu);
801 return ret;
802}
803
804/*
805 * Send file contents to server backend. We could use sendfile(), but to remain
806 * more portable lets just read/write the darn thing.
807 */
808static int __fio_client_send_local_ini(struct fio_client *client,
809 const char *filename)
810{
811 struct cmd_job_pdu *pdu;
812 size_t p_size;
813 struct stat sb;
814 char *p;
815 void *buf;
816 off_t len;
817 int fd, ret;
818
819 dprint(FD_NET, "send ini %s to %s\n", filename, client->hostname);
820
821 fd = open(filename, O_RDONLY);
822 if (fd < 0) {
823 ret = -errno;
824 log_err("fio: job file <%s> open: %s\n", filename, strerror(errno));
825 return ret;
826 }
827
828 if (fstat(fd, &sb) < 0) {
829 ret = -errno;
830 log_err("fio: job file stat: %s\n", strerror(errno));
831 close(fd);
832 return ret;
833 }
834
835 /*
836 * Add extra space for variable expansion, but doesn't guarantee.
837 */
838 sb.st_size += OPT_LEN_MAX;
839 p_size = sb.st_size + sizeof(*pdu);
840 pdu = malloc(p_size);
841 buf = pdu->buf;
842
843 len = sb.st_size;
844 p = buf;
845 if (read_ini_data(fd, p, len)) {
846 log_err("fio: failed reading job file %s\n", filename);
847 close(fd);
848 free(pdu);
849 return 1;
850 }
851
852 pdu->buf_len = __cpu_to_le32(sb.st_size);
853 pdu->client_type = cpu_to_le32(client->type);
854
855 client->sent_job = true;
856 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, pdu, p_size, NULL, NULL);
857 free(pdu);
858 close(fd);
859 return ret;
860}
861
862int fio_client_send_ini(struct fio_client *client, const char *filename,
863 bool remote)
864{
865 int ret;
866
867 if (!remote)
868 ret = __fio_client_send_local_ini(client, filename);
869 else
870 ret = __fio_client_send_remote_ini(client, filename);
871
872 if (!ret)
873 client->sent_job = true;
874
875 return ret;
876}
877
878static int fio_client_send_cf(struct fio_client *client,
879 struct client_file *cf)
880{
881 return fio_client_send_ini(client, cf->file, cf->remote);
882}
883
884int fio_clients_send_ini(const char *filename)
885{
886 struct fio_client *client;
887 struct flist_head *entry, *tmp;
888
889 flist_for_each_safe(entry, tmp, &client_list) {
890 bool failed = false;
891
892 client = flist_entry(entry, struct fio_client, list);
893
894 if (client->nr_files) {
895 int i;
896
897 for (i = 0; i < client->nr_files; i++) {
898 struct client_file *cf;
899
900 cf = &client->files[i];
901
902 if (fio_client_send_cf(client, cf)) {
903 failed = true;
904 remove_client(client);
905 break;
906 }
907 }
908 }
909 if (client->sent_job || failed)
910 continue;
911 if (!filename || fio_client_send_ini(client, filename, 0))
912 remove_client(client);
913 }
914
915 return !nr_clients;
916}
917
918int fio_client_update_options(struct fio_client *client,
919 struct thread_options *o, uint64_t *tag)
920{
921 struct cmd_add_job_pdu pdu;
922
923 pdu.thread_number = cpu_to_le32(client->thread_number);
924 pdu.groupid = cpu_to_le32(client->groupid);
925 convert_thread_options_to_net(&pdu.top, o);
926
927 return fio_net_send_cmd(client->fd, FIO_NET_CMD_UPDATE_JOB, &pdu, sizeof(pdu), tag, &client->cmd_list);
928}
929
930static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
931{
932 dst->max_val = le64_to_cpu(src->max_val);
933 dst->min_val = le64_to_cpu(src->min_val);
934 dst->samples = le64_to_cpu(src->samples);
935
936 /*
937 * Floats arrive as IEEE 754 encoded uint64_t, convert back to double
938 */
939 dst->mean.u.f = fio_uint64_to_double(le64_to_cpu(dst->mean.u.i));
940 dst->S.u.f = fio_uint64_to_double(le64_to_cpu(dst->S.u.i));
941}
942
943static void convert_ts(struct thread_stat *dst, struct thread_stat *src)
944{
945 int i, j;
946
947 dst->error = le32_to_cpu(src->error);
948 dst->thread_number = le32_to_cpu(src->thread_number);
949 dst->groupid = le32_to_cpu(src->groupid);
950 dst->pid = le32_to_cpu(src->pid);
951 dst->members = le32_to_cpu(src->members);
952 dst->unified_rw_rep = le32_to_cpu(src->unified_rw_rep);
953
954 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
955 convert_io_stat(&dst->clat_stat[i], &src->clat_stat[i]);
956 convert_io_stat(&dst->slat_stat[i], &src->slat_stat[i]);
957 convert_io_stat(&dst->lat_stat[i], &src->lat_stat[i]);
958 convert_io_stat(&dst->bw_stat[i], &src->bw_stat[i]);
959 convert_io_stat(&dst->iops_stat[i], &src->iops_stat[i]);
960 }
961
962 dst->usr_time = le64_to_cpu(src->usr_time);
963 dst->sys_time = le64_to_cpu(src->sys_time);
964 dst->ctx = le64_to_cpu(src->ctx);
965 dst->minf = le64_to_cpu(src->minf);
966 dst->majf = le64_to_cpu(src->majf);
967 dst->clat_percentiles = le32_to_cpu(src->clat_percentiles);
968 dst->lat_percentiles = le32_to_cpu(src->lat_percentiles);
969 dst->percentile_precision = le64_to_cpu(src->percentile_precision);
970
971 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
972 fio_fp64_t *fps = &src->percentile_list[i];
973 fio_fp64_t *fpd = &dst->percentile_list[i];
974
975 fpd->u.f = fio_uint64_to_double(le64_to_cpu(fps->u.i));
976 }
977
978 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
979 dst->io_u_map[i] = le64_to_cpu(src->io_u_map[i]);
980 dst->io_u_submit[i] = le64_to_cpu(src->io_u_submit[i]);
981 dst->io_u_complete[i] = le64_to_cpu(src->io_u_complete[i]);
982 }
983
984 for (i = 0; i < FIO_IO_U_LAT_N_NR; i++)
985 dst->io_u_lat_n[i] = le64_to_cpu(src->io_u_lat_n[i]);
986 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++)
987 dst->io_u_lat_u[i] = le64_to_cpu(src->io_u_lat_u[i]);
988 for (i = 0; i < FIO_IO_U_LAT_M_NR; i++)
989 dst->io_u_lat_m[i] = le64_to_cpu(src->io_u_lat_m[i]);
990
991 for (i = 0; i < DDIR_RWDIR_CNT; i++)
992 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
993 dst->io_u_plat[i][j] = le64_to_cpu(src->io_u_plat[i][j]);
994
995 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
996 dst->total_io_u[i] = le64_to_cpu(src->total_io_u[i]);
997 dst->short_io_u[i] = le64_to_cpu(src->short_io_u[i]);
998 dst->drop_io_u[i] = le64_to_cpu(src->drop_io_u[i]);
999 }
1000
1001 dst->total_submit = le64_to_cpu(src->total_submit);
1002 dst->total_complete = le64_to_cpu(src->total_complete);
1003
1004 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1005 dst->io_bytes[i] = le64_to_cpu(src->io_bytes[i]);
1006 dst->runtime[i] = le64_to_cpu(src->runtime[i]);
1007 }
1008
1009 dst->total_run_time = le64_to_cpu(src->total_run_time);
1010 dst->continue_on_error = le16_to_cpu(src->continue_on_error);
1011 dst->total_err_count = le64_to_cpu(src->total_err_count);
1012 dst->first_error = le32_to_cpu(src->first_error);
1013 dst->kb_base = le32_to_cpu(src->kb_base);
1014 dst->unit_base = le32_to_cpu(src->unit_base);
1015
1016 dst->sig_figs = le32_to_cpu(src->sig_figs);
1017
1018 dst->latency_depth = le32_to_cpu(src->latency_depth);
1019 dst->latency_target = le64_to_cpu(src->latency_target);
1020 dst->latency_window = le64_to_cpu(src->latency_window);
1021 dst->latency_percentile.u.f = fio_uint64_to_double(le64_to_cpu(src->latency_percentile.u.i));
1022
1023 dst->nr_block_infos = le64_to_cpu(src->nr_block_infos);
1024 for (i = 0; i < dst->nr_block_infos; i++)
1025 dst->block_infos[i] = le32_to_cpu(src->block_infos[i]);
1026
1027 dst->ss_dur = le64_to_cpu(src->ss_dur);
1028 dst->ss_state = le32_to_cpu(src->ss_state);
1029 dst->ss_head = le32_to_cpu(src->ss_head);
1030 dst->ss_limit.u.f = fio_uint64_to_double(le64_to_cpu(src->ss_limit.u.i));
1031 dst->ss_slope.u.f = fio_uint64_to_double(le64_to_cpu(src->ss_slope.u.i));
1032 dst->ss_deviation.u.f = fio_uint64_to_double(le64_to_cpu(src->ss_deviation.u.i));
1033 dst->ss_criterion.u.f = fio_uint64_to_double(le64_to_cpu(src->ss_criterion.u.i));
1034
1035 if (dst->ss_state & FIO_SS_DATA) {
1036 for (i = 0; i < dst->ss_dur; i++ ) {
1037 dst->ss_iops_data[i] = le64_to_cpu(src->ss_iops_data[i]);
1038 dst->ss_bw_data[i] = le64_to_cpu(src->ss_bw_data[i]);
1039 }
1040 }
1041}
1042
1043static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
1044{
1045 int i;
1046
1047 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1048 dst->max_run[i] = le64_to_cpu(src->max_run[i]);
1049 dst->min_run[i] = le64_to_cpu(src->min_run[i]);
1050 dst->max_bw[i] = le64_to_cpu(src->max_bw[i]);
1051 dst->min_bw[i] = le64_to_cpu(src->min_bw[i]);
1052 dst->iobytes[i] = le64_to_cpu(src->iobytes[i]);
1053 dst->agg[i] = le64_to_cpu(src->agg[i]);
1054 }
1055
1056 dst->kb_base = le32_to_cpu(src->kb_base);
1057 dst->unit_base = le32_to_cpu(src->unit_base);
1058 dst->sig_figs = le32_to_cpu(src->sig_figs);
1059 dst->groupid = le32_to_cpu(src->groupid);
1060 dst->unified_rw_rep = le32_to_cpu(src->unified_rw_rep);
1061}
1062
1063static void json_object_add_client_info(struct json_object *obj,
1064 struct fio_client *client)
1065{
1066 const char *hostname = client->hostname ? client->hostname : "";
1067
1068 json_object_add_value_string(obj, "hostname", hostname);
1069 json_object_add_value_int(obj, "port", client->port);
1070}
1071
1072static void handle_ts(struct fio_client *client, struct fio_net_cmd *cmd)
1073{
1074 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
1075 struct flist_head *opt_list = NULL;
1076 struct json_object *tsobj;
1077
1078 if (client->opt_lists && p->ts.thread_number <= client->jobs)
1079 opt_list = &client->opt_lists[p->ts.thread_number - 1];
1080
1081 tsobj = show_thread_status(&p->ts, &p->rs, opt_list, &client->buf);
1082 client->did_stat = true;
1083 if (tsobj) {
1084 json_object_add_client_info(tsobj, client);
1085 json_array_add_value_object(clients_array, tsobj);
1086 }
1087
1088 if (sum_stat_clients <= 1)
1089 return;
1090
1091 sum_thread_stats(&client_ts, &p->ts, sum_stat_nr == 1);
1092 sum_group_stats(&client_gs, &p->rs);
1093
1094 client_ts.members++;
1095 client_ts.thread_number = p->ts.thread_number;
1096 client_ts.groupid = p->ts.groupid;
1097 client_ts.unified_rw_rep = p->ts.unified_rw_rep;
1098 client_ts.sig_figs = p->ts.sig_figs;
1099
1100 if (++sum_stat_nr == sum_stat_clients) {
1101 strcpy(client_ts.name, "All clients");
1102 tsobj = show_thread_status(&client_ts, &client_gs, NULL, &client->buf);
1103 if (tsobj) {
1104 json_object_add_client_info(tsobj, client);
1105 json_array_add_value_object(clients_array, tsobj);
1106 }
1107 }
1108}
1109
1110static void handle_gs(struct fio_client *client, struct fio_net_cmd *cmd)
1111{
1112 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
1113
1114 if (output_format & FIO_OUTPUT_NORMAL)
1115 show_group_stats(gs, &client->buf);
1116}
1117
1118static void handle_job_opt(struct fio_client *client, struct fio_net_cmd *cmd)
1119{
1120 struct cmd_job_option *pdu = (struct cmd_job_option *) cmd->payload;
1121 struct print_option *p;
1122
1123 if (!job_opt_object)
1124 return;
1125
1126 pdu->global = le16_to_cpu(pdu->global);
1127 pdu->truncated = le16_to_cpu(pdu->truncated);
1128 pdu->groupid = le32_to_cpu(pdu->groupid);
1129
1130 p = malloc(sizeof(*p));
1131 p->name = strdup((char *) pdu->name);
1132 if (pdu->value[0] != '\0')
1133 p->value = strdup((char *) pdu->value);
1134 else
1135 p->value = NULL;
1136
1137 if (pdu->global) {
1138 const char *pos = "";
1139
1140 if (p->value)
1141 pos = p->value;
1142
1143 json_object_add_value_string(job_opt_object, p->name, pos);
1144 } else if (client->opt_lists) {
1145 struct flist_head *opt_list = &client->opt_lists[pdu->groupid];
1146
1147 flist_add_tail(&p->list, opt_list);
1148 }
1149}
1150
1151static void handle_text(struct fio_client *client, struct fio_net_cmd *cmd)
1152{
1153 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
1154 const char *buf = (const char *) pdu->buf;
1155 const char *name;
1156 int fio_unused ret;
1157 struct buf_output out;
1158
1159 buf_output_init(&out);
1160
1161 name = client->name ? client->name : client->hostname;
1162
1163 if (!client->skip_newline && !(output_format & FIO_OUTPUT_TERSE))
1164 __log_buf(&out, "<%s> ", name);
1165 __log_buf(&out, "%s", buf);
1166 log_info_buf(out.buf, out.buflen);
1167 buf_output_free(&out);
1168 client->skip_newline = strchr(buf, '\n') == NULL;
1169}
1170
1171static void convert_agg(struct disk_util_agg *agg)
1172{
1173 int i;
1174
1175 for (i = 0; i < 2; i++) {
1176 agg->ios[i] = le64_to_cpu(agg->ios[i]);
1177 agg->merges[i] = le64_to_cpu(agg->merges[i]);
1178 agg->sectors[i] = le64_to_cpu(agg->sectors[i]);
1179 agg->ticks[i] = le64_to_cpu(agg->ticks[i]);
1180 }
1181
1182 agg->io_ticks = le64_to_cpu(agg->io_ticks);
1183 agg->time_in_queue = le64_to_cpu(agg->time_in_queue);
1184 agg->slavecount = le32_to_cpu(agg->slavecount);
1185 agg->max_util.u.f = fio_uint64_to_double(le64_to_cpu(agg->max_util.u.i));
1186}
1187
1188static void convert_dus(struct disk_util_stat *dus)
1189{
1190 int i;
1191
1192 for (i = 0; i < 2; i++) {
1193 dus->s.ios[i] = le64_to_cpu(dus->s.ios[i]);
1194 dus->s.merges[i] = le64_to_cpu(dus->s.merges[i]);
1195 dus->s.sectors[i] = le64_to_cpu(dus->s.sectors[i]);
1196 dus->s.ticks[i] = le64_to_cpu(dus->s.ticks[i]);
1197 }
1198
1199 dus->s.io_ticks = le64_to_cpu(dus->s.io_ticks);
1200 dus->s.time_in_queue = le64_to_cpu(dus->s.time_in_queue);
1201 dus->s.msec = le64_to_cpu(dus->s.msec);
1202}
1203
1204static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd)
1205{
1206 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
1207
1208 if (!client->disk_stats_shown)
1209 client->disk_stats_shown = true;
1210
1211 if (output_format & FIO_OUTPUT_JSON) {
1212 struct json_object *duobj;
1213
1214 json_array_add_disk_util(&du->dus, &du->agg, du_array);
1215 duobj = json_array_last_value_object(du_array);
1216 json_object_add_client_info(duobj, client);
1217 } else if (output_format & FIO_OUTPUT_TERSE)
1218 print_disk_util(&du->dus, &du->agg, 1, &client->buf);
1219 else if (output_format & FIO_OUTPUT_NORMAL) {
1220 __log_buf(&client->buf, "\nDisk stats (read/write):\n");
1221 print_disk_util(&du->dus, &du->agg, 0, &client->buf);
1222 }
1223}
1224
1225static void convert_jobs_eta(struct jobs_eta *je)
1226{
1227 int i;
1228
1229 je->nr_running = le32_to_cpu(je->nr_running);
1230 je->nr_ramp = le32_to_cpu(je->nr_ramp);
1231 je->nr_pending = le32_to_cpu(je->nr_pending);
1232 je->nr_setting_up = le32_to_cpu(je->nr_setting_up);
1233 je->files_open = le32_to_cpu(je->files_open);
1234
1235 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1236 je->m_rate[i] = le64_to_cpu(je->m_rate[i]);
1237 je->t_rate[i] = le64_to_cpu(je->t_rate[i]);
1238 je->m_iops[i] = le32_to_cpu(je->m_iops[i]);
1239 je->t_iops[i] = le32_to_cpu(je->t_iops[i]);
1240 je->rate[i] = le64_to_cpu(je->rate[i]);
1241 je->iops[i] = le32_to_cpu(je->iops[i]);
1242 }
1243
1244 je->elapsed_sec = le64_to_cpu(je->elapsed_sec);
1245 je->eta_sec = le64_to_cpu(je->eta_sec);
1246 je->nr_threads = le32_to_cpu(je->nr_threads);
1247 je->is_pow2 = le32_to_cpu(je->is_pow2);
1248 je->unit_base = le32_to_cpu(je->unit_base);
1249 je->sig_figs = le32_to_cpu(je->sig_figs);
1250}
1251
1252void fio_client_sum_jobs_eta(struct jobs_eta *dst, struct jobs_eta *je)
1253{
1254 int i;
1255
1256 dst->nr_running += je->nr_running;
1257 dst->nr_ramp += je->nr_ramp;
1258 dst->nr_pending += je->nr_pending;
1259 dst->nr_setting_up += je->nr_setting_up;
1260 dst->files_open += je->files_open;
1261
1262 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1263 dst->m_rate[i] += je->m_rate[i];
1264 dst->t_rate[i] += je->t_rate[i];
1265 dst->m_iops[i] += je->m_iops[i];
1266 dst->t_iops[i] += je->t_iops[i];
1267 dst->rate[i] += je->rate[i];
1268 dst->iops[i] += je->iops[i];
1269 }
1270
1271 dst->elapsed_sec += je->elapsed_sec;
1272
1273 if (je->eta_sec > dst->eta_sec)
1274 dst->eta_sec = je->eta_sec;
1275
1276 dst->nr_threads += je->nr_threads;
1277
1278 /*
1279 * This wont be correct for multiple strings, but at least it
1280 * works for the basic cases.
1281 */
1282 strcpy((char *) dst->run_str, (char *) je->run_str);
1283}
1284
1285static bool remove_reply_cmd(struct fio_client *client, struct fio_net_cmd *cmd)
1286{
1287 struct fio_net_cmd_reply *reply = NULL;
1288 struct flist_head *entry;
1289
1290 flist_for_each(entry, &client->cmd_list) {
1291 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
1292
1293 if (cmd->tag == (uintptr_t) reply)
1294 break;
1295
1296 reply = NULL;
1297 }
1298
1299 if (!reply) {
1300 log_err("fio: client: unable to find matching tag (%llx)\n", (unsigned long long) cmd->tag);
1301 return false;
1302 }
1303
1304 flist_del(&reply->list);
1305 cmd->tag = reply->saved_tag;
1306 free(reply);
1307 return true;
1308}
1309
1310int fio_client_wait_for_reply(struct fio_client *client, uint64_t tag)
1311{
1312 do {
1313 struct fio_net_cmd_reply *reply = NULL;
1314 struct flist_head *entry;
1315
1316 flist_for_each(entry, &client->cmd_list) {
1317 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
1318
1319 if (tag == (uintptr_t) reply)
1320 break;
1321
1322 reply = NULL;
1323 }
1324
1325 if (!reply)
1326 break;
1327
1328 usleep(1000);
1329 } while (1);
1330
1331 return 0;
1332}
1333
1334static void handle_eta(struct fio_client *client, struct fio_net_cmd *cmd)
1335{
1336 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
1337 struct client_eta *eta = (struct client_eta *) (uintptr_t) cmd->tag;
1338
1339 dprint(FD_NET, "client: got eta tag %p, %d\n", eta, eta->pending);
1340
1341 assert(client->eta_in_flight == eta);
1342
1343 client->eta_in_flight = NULL;
1344 flist_del_init(&client->eta_list);
1345 client->eta_timeouts = 0;
1346
1347 if (client->ops->jobs_eta)
1348 client->ops->jobs_eta(client, je);
1349
1350 fio_client_sum_jobs_eta(&eta->eta, je);
1351 fio_client_dec_jobs_eta(eta, client->ops->eta);
1352}
1353
1354static void client_flush_hist_samples(FILE *f, int hist_coarseness, void *samples,
1355 uint64_t sample_size)
1356{
1357 struct io_sample *s;
1358 int log_offset;
1359 uint64_t i, j, nr_samples;
1360 struct io_u_plat_entry *entry;
1361 uint64_t *io_u_plat;
1362
1363 int stride = 1 << hist_coarseness;
1364
1365 if (!sample_size)
1366 return;
1367
1368 s = __get_sample(samples, 0, 0);
1369 log_offset = (s->__ddir & LOG_OFFSET_SAMPLE_BIT) != 0;
1370
1371 nr_samples = sample_size / __log_entry_sz(log_offset);
1372
1373 for (i = 0; i < nr_samples; i++) {
1374
1375 s = (struct io_sample *)((char *)__get_sample(samples, log_offset, i) +
1376 i * sizeof(struct io_u_plat_entry));
1377
1378 entry = s->data.plat_entry;
1379 io_u_plat = entry->io_u_plat;
1380
1381 fprintf(f, "%lu, %u, %llu, ", (unsigned long) s->time,
1382 io_sample_ddir(s), (unsigned long long) s->bs);
1383 for (j = 0; j < FIO_IO_U_PLAT_NR - stride; j += stride) {
1384 fprintf(f, "%llu, ", (unsigned long long)hist_sum(j, stride, io_u_plat, NULL));
1385 }
1386 fprintf(f, "%llu\n", (unsigned long long)
1387 hist_sum(FIO_IO_U_PLAT_NR - stride, stride, io_u_plat, NULL));
1388
1389 }
1390}
1391
1392static int fio_client_handle_iolog(struct fio_client *client,
1393 struct fio_net_cmd *cmd)
1394{
1395 struct cmd_iolog_pdu *pdu = NULL;
1396 bool store_direct;
1397 char *log_pathname = NULL;
1398 int ret = 0;
1399
1400 pdu = convert_iolog(cmd, &store_direct);
1401 if (!pdu) {
1402 log_err("fio: failed converting IO log\n");
1403 ret = 1;
1404 goto out;
1405 }
1406
1407 /* allocate buffer big enough for next sprintf() call */
1408 log_pathname = malloc(10 + strlen((char *)pdu->name) +
1409 strlen(client->hostname));
1410 if (!log_pathname) {
1411 log_err("fio: memory allocation of unique pathname failed\n");
1412 ret = -1;
1413 goto out;
1414 }
1415 /* generate a unique pathname for the log file using hostname */
1416 sprintf(log_pathname, "%s.%s", pdu->name, client->hostname);
1417
1418 if (store_direct) {
1419 ssize_t wrote;
1420 size_t sz;
1421 int fd;
1422
1423 fd = open((const char *) log_pathname,
1424 O_WRONLY | O_CREAT | O_TRUNC, 0644);
1425 if (fd < 0) {
1426 log_err("fio: open log %s: %s\n",
1427 log_pathname, strerror(errno));
1428 ret = 1;
1429 goto out;
1430 }
1431
1432 sz = cmd->pdu_len - sizeof(*pdu);
1433 wrote = write(fd, pdu->samples, sz);
1434 close(fd);
1435
1436 if (wrote != sz) {
1437 log_err("fio: short write on compressed log\n");
1438 ret = 1;
1439 goto out;
1440 }
1441
1442 ret = 0;
1443 } else {
1444 FILE *f;
1445 f = fopen((const char *) log_pathname, "w");
1446 if (!f) {
1447 log_err("fio: fopen log %s : %s\n",
1448 log_pathname, strerror(errno));
1449 ret = 1;
1450 goto out;
1451 }
1452
1453 if (pdu->log_type == IO_LOG_TYPE_HIST) {
1454 client_flush_hist_samples(f, pdu->log_hist_coarseness, pdu->samples,
1455 pdu->nr_samples * sizeof(struct io_sample));
1456 } else {
1457 flush_samples(f, pdu->samples,
1458 pdu->nr_samples * sizeof(struct io_sample));
1459 }
1460 fclose(f);
1461 ret = 0;
1462 }
1463
1464out:
1465 if (pdu && pdu != (void *) cmd->payload)
1466 free(pdu);
1467
1468 if (log_pathname)
1469 free(log_pathname);
1470
1471 return ret;
1472}
1473
1474static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd)
1475{
1476 struct cmd_probe_reply_pdu *probe = (struct cmd_probe_reply_pdu *) cmd->payload;
1477 const char *os, *arch;
1478 char bit[16];
1479
1480 os = fio_get_os_string(probe->os);
1481 if (!os)
1482 os = "unknown";
1483
1484 arch = fio_get_arch_string(probe->arch);
1485 if (!arch)
1486 os = "unknown";
1487
1488 sprintf(bit, "%d-bit", probe->bpp * 8);
1489 probe->flags = le64_to_cpu(probe->flags);
1490
1491 if (output_format & FIO_OUTPUT_NORMAL) {
1492 log_info("hostname=%s, be=%u, %s, os=%s, arch=%s, fio=%s, flags=%lx\n",
1493 probe->hostname, probe->bigendian, bit, os, arch,
1494 probe->fio_version, (unsigned long) probe->flags);
1495 }
1496
1497 if (!client->name)
1498 client->name = strdup((char *) probe->hostname);
1499}
1500
1501static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd)
1502{
1503 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
1504
1505 client->state = Client_started;
1506 client->jobs = le32_to_cpu(pdu->jobs);
1507 client->nr_stat = le32_to_cpu(pdu->stat_outputs);
1508
1509 if (client->jobs) {
1510 int i;
1511
1512 if (client->opt_lists)
1513 free(client->opt_lists);
1514
1515 client->opt_lists = malloc(client->jobs * sizeof(struct flist_head));
1516 for (i = 0; i < client->jobs; i++)
1517 INIT_FLIST_HEAD(&client->opt_lists[i]);
1518 }
1519
1520 sum_stat_clients += client->nr_stat;
1521}
1522
1523static void handle_stop(struct fio_client *client)
1524{
1525 if (client->error)
1526 log_info("client <%s>: exited with error %d\n", client->hostname, client->error);
1527}
1528
1529static void convert_stop(struct fio_net_cmd *cmd)
1530{
1531 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
1532
1533 pdu->error = le32_to_cpu(pdu->error);
1534}
1535
1536static void convert_text(struct fio_net_cmd *cmd)
1537{
1538 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
1539
1540 pdu->level = le32_to_cpu(pdu->level);
1541 pdu->buf_len = le32_to_cpu(pdu->buf_len);
1542 pdu->log_sec = le64_to_cpu(pdu->log_sec);
1543 pdu->log_usec = le64_to_cpu(pdu->log_usec);
1544}
1545
1546static struct cmd_iolog_pdu *convert_iolog_gz(struct fio_net_cmd *cmd,
1547 struct cmd_iolog_pdu *pdu)
1548{
1549#ifdef CONFIG_ZLIB
1550 struct cmd_iolog_pdu *ret;
1551 z_stream stream;
1552 uint64_t nr_samples;
1553 size_t total;
1554 char *p;
1555
1556 stream.zalloc = Z_NULL;
1557 stream.zfree = Z_NULL;
1558 stream.opaque = Z_NULL;
1559 stream.avail_in = 0;
1560 stream.next_in = Z_NULL;
1561
1562 if (inflateInit(&stream) != Z_OK)
1563 return NULL;
1564
1565 /*
1566 * Get header first, it's not compressed
1567 */
1568 nr_samples = le64_to_cpu(pdu->nr_samples);
1569
1570 if (pdu->log_type == IO_LOG_TYPE_HIST)
1571 total = nr_samples * (__log_entry_sz(le32_to_cpu(pdu->log_offset)) +
1572 sizeof(struct io_u_plat_entry));
1573 else
1574 total = nr_samples * __log_entry_sz(le32_to_cpu(pdu->log_offset));
1575 ret = malloc(total + sizeof(*pdu));
1576 ret->nr_samples = nr_samples;
1577
1578 memcpy(ret, pdu, sizeof(*pdu));
1579
1580 p = (char *) ret + sizeof(*pdu);
1581
1582 stream.avail_in = cmd->pdu_len - sizeof(*pdu);
1583 stream.next_in = (void *)((char *) pdu + sizeof(*pdu));
1584 while (stream.avail_in) {
1585 unsigned int this_chunk = 65536;
1586 unsigned int this_len;
1587 int err;
1588
1589 if (this_chunk > total)
1590 this_chunk = total;
1591
1592 stream.avail_out = this_chunk;
1593 stream.next_out = (void *)p;
1594 err = inflate(&stream, Z_NO_FLUSH);
1595 /* may be Z_OK, or Z_STREAM_END */
1596 if (err < 0) {
1597 log_err("fio: inflate error %d\n", err);
1598 free(ret);
1599 ret = NULL;
1600 goto err;
1601 }
1602
1603 this_len = this_chunk - stream.avail_out;
1604 p += this_len;
1605 total -= this_len;
1606 }
1607
1608err:
1609 inflateEnd(&stream);
1610 return ret;
1611#else
1612 return NULL;
1613#endif
1614}
1615
1616/*
1617 * This has been compressed on the server side, since it can be big.
1618 * Uncompress here.
1619 */
1620static struct cmd_iolog_pdu *convert_iolog(struct fio_net_cmd *cmd,
1621 bool *store_direct)
1622{
1623 struct cmd_iolog_pdu *pdu = (struct cmd_iolog_pdu *) cmd->payload;
1624 struct cmd_iolog_pdu *ret;
1625 uint64_t i;
1626 int compressed;
1627 void *samples;
1628
1629 *store_direct = false;
1630
1631 /*
1632 * Convert if compressed and we support it. If it's not
1633 * compressed, we need not do anything.
1634 */
1635 compressed = le32_to_cpu(pdu->compressed);
1636 if (compressed == XMIT_COMPRESSED) {
1637#ifndef CONFIG_ZLIB
1638 log_err("fio: server sent compressed data by mistake\n");
1639 return NULL;
1640#endif
1641 ret = convert_iolog_gz(cmd, pdu);
1642 if (!ret) {
1643 log_err("fio: failed decompressing log\n");
1644 return NULL;
1645 }
1646 } else if (compressed == STORE_COMPRESSED) {
1647 *store_direct = true;
1648 ret = pdu;
1649 } else
1650 ret = pdu;
1651
1652 ret->nr_samples = le64_to_cpu(ret->nr_samples);
1653 ret->thread_number = le32_to_cpu(ret->thread_number);
1654 ret->log_type = le32_to_cpu(ret->log_type);
1655 ret->compressed = le32_to_cpu(ret->compressed);
1656 ret->log_offset = le32_to_cpu(ret->log_offset);
1657 ret->log_hist_coarseness = le32_to_cpu(ret->log_hist_coarseness);
1658
1659 if (*store_direct)
1660 return ret;
1661
1662 samples = &ret->samples[0];
1663 for (i = 0; i < ret->nr_samples; i++) {
1664 struct io_sample *s;
1665
1666 s = __get_sample(samples, ret->log_offset, i);
1667 if (ret->log_type == IO_LOG_TYPE_HIST)
1668 s = (struct io_sample *)((char *)s + sizeof(struct io_u_plat_entry) * i);
1669
1670 s->time = le64_to_cpu(s->time);
1671 s->data.val = le64_to_cpu(s->data.val);
1672 s->__ddir = le32_to_cpu(s->__ddir);
1673 s->bs = le64_to_cpu(s->bs);
1674
1675 if (ret->log_offset) {
1676 struct io_sample_offset *so = (void *) s;
1677
1678 so->offset = le64_to_cpu(so->offset);
1679 }
1680
1681 if (ret->log_type == IO_LOG_TYPE_HIST) {
1682 s->data.plat_entry = (struct io_u_plat_entry *)(((char *)s) + sizeof(*s));
1683 s->data.plat_entry->list.next = NULL;
1684 s->data.plat_entry->list.prev = NULL;
1685 }
1686 }
1687
1688 return ret;
1689}
1690
1691static void sendfile_reply(int fd, struct cmd_sendfile_reply *rep,
1692 size_t size, uint64_t tag)
1693{
1694 rep->error = cpu_to_le32(rep->error);
1695 fio_net_send_cmd(fd, FIO_NET_CMD_SENDFILE, rep, size, &tag, NULL);
1696}
1697
1698static int fio_send_file(struct fio_client *client, struct cmd_sendfile *pdu,
1699 uint64_t tag)
1700{
1701 struct cmd_sendfile_reply *rep;
1702 struct stat sb;
1703 size_t size;
1704 int fd;
1705
1706 size = sizeof(*rep);
1707 rep = malloc(size);
1708
1709 if (stat((char *)pdu->path, &sb) < 0) {
1710fail:
1711 rep->error = errno;
1712 sendfile_reply(client->fd, rep, size, tag);
1713 free(rep);
1714 return 1;
1715 }
1716
1717 size += sb.st_size;
1718 rep = realloc(rep, size);
1719 rep->size = cpu_to_le32((uint32_t) sb.st_size);
1720
1721 fd = open((char *)pdu->path, O_RDONLY);
1722 if (fd == -1 )
1723 goto fail;
1724
1725 rep->error = read_data(fd, &rep->data, sb.st_size);
1726 sendfile_reply(client->fd, rep, size, tag);
1727 free(rep);
1728 close(fd);
1729 return 0;
1730}
1731
1732int fio_handle_client(struct fio_client *client)
1733{
1734 struct client_ops *ops = client->ops;
1735 struct fio_net_cmd *cmd;
1736 int size;
1737
1738 dprint(FD_NET, "client: handle %s\n", client->hostname);
1739
1740 cmd = fio_net_recv_cmd(client->fd, true);
1741 if (!cmd)
1742 return 0;
1743
1744 dprint(FD_NET, "client: got cmd op %s from %s (pdu=%u)\n",
1745 fio_server_op(cmd->opcode), client->hostname, cmd->pdu_len);
1746
1747 client->last_cmd = cmd->opcode;
1748
1749 switch (cmd->opcode) {
1750 case FIO_NET_CMD_QUIT:
1751 if (ops->quit)
1752 ops->quit(client, cmd);
1753 remove_client(client);
1754 break;
1755 case FIO_NET_CMD_TEXT:
1756 convert_text(cmd);
1757 ops->text(client, cmd);
1758 break;
1759 case FIO_NET_CMD_DU: {
1760 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
1761
1762 convert_dus(&du->dus);
1763 convert_agg(&du->agg);
1764
1765 ops->disk_util(client, cmd);
1766 break;
1767 }
1768 case FIO_NET_CMD_TS: {
1769 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
1770
1771 dprint(FD_NET, "client: ts->ss_state = %u\n", (unsigned int) le32_to_cpu(p->ts.ss_state));
1772 if (le32_to_cpu(p->ts.ss_state) & FIO_SS_DATA) {
1773 dprint(FD_NET, "client: received steadystate ring buffers\n");
1774
1775 size = le64_to_cpu(p->ts.ss_dur);
1776 p->ts.ss_iops_data = (uint64_t *) ((struct cmd_ts_pdu *)cmd->payload + 1);
1777 p->ts.ss_bw_data = p->ts.ss_iops_data + size;
1778 }
1779
1780 convert_ts(&p->ts, &p->ts);
1781 convert_gs(&p->rs, &p->rs);
1782
1783 ops->thread_status(client, cmd);
1784 break;
1785 }
1786 case FIO_NET_CMD_GS: {
1787 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
1788
1789 convert_gs(gs, gs);
1790
1791 ops->group_stats(client, cmd);
1792 break;
1793 }
1794 case FIO_NET_CMD_ETA: {
1795 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
1796
1797 if (!remove_reply_cmd(client, cmd))
1798 break;
1799 convert_jobs_eta(je);
1800 handle_eta(client, cmd);
1801 break;
1802 }
1803 case FIO_NET_CMD_PROBE:
1804 remove_reply_cmd(client, cmd);
1805 ops->probe(client, cmd);
1806 break;
1807 case FIO_NET_CMD_SERVER_START:
1808 client->state = Client_running;
1809 if (ops->job_start)
1810 ops->job_start(client, cmd);
1811 break;
1812 case FIO_NET_CMD_START: {
1813 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
1814
1815 pdu->jobs = le32_to_cpu(pdu->jobs);
1816 ops->start(client, cmd);
1817 break;
1818 }
1819 case FIO_NET_CMD_STOP: {
1820 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
1821
1822 convert_stop(cmd);
1823 client->state = Client_stopped;
1824 client->error = le32_to_cpu(pdu->error);
1825 client->signal = le32_to_cpu(pdu->signal);
1826 ops->stop(client);
1827 break;
1828 }
1829 case FIO_NET_CMD_ADD_JOB: {
1830 struct cmd_add_job_pdu *pdu = (struct cmd_add_job_pdu *) cmd->payload;
1831
1832 client->thread_number = le32_to_cpu(pdu->thread_number);
1833 client->groupid = le32_to_cpu(pdu->groupid);
1834
1835 if (ops->add_job)
1836 ops->add_job(client, cmd);
1837 break;
1838 }
1839 case FIO_NET_CMD_IOLOG:
1840 fio_client_handle_iolog(client, cmd);
1841 break;
1842 case FIO_NET_CMD_UPDATE_JOB:
1843 ops->update_job(client, cmd);
1844 remove_reply_cmd(client, cmd);
1845 break;
1846 case FIO_NET_CMD_VTRIGGER: {
1847 struct all_io_list *pdu = (struct all_io_list *) cmd->payload;
1848 char buf[128];
1849 int off = 0;
1850
1851 if (aux_path) {
1852 strcpy(buf, aux_path);
1853 off = strlen(buf);
1854 }
1855
1856 __verify_save_state(pdu, server_name(client, &buf[off], sizeof(buf) - off));
1857 exec_trigger(trigger_cmd);
1858 break;
1859 }
1860 case FIO_NET_CMD_SENDFILE: {
1861 struct cmd_sendfile *pdu = (struct cmd_sendfile *) cmd->payload;
1862 fio_send_file(client, pdu, cmd->tag);
1863 break;
1864 }
1865 case FIO_NET_CMD_JOB_OPT: {
1866 handle_job_opt(client, cmd);
1867 break;
1868 }
1869 default:
1870 log_err("fio: unknown client op: %s\n", fio_server_op(cmd->opcode));
1871 break;
1872 }
1873
1874 free(cmd);
1875 return 1;
1876}
1877
1878int fio_clients_send_trigger(const char *cmd)
1879{
1880 struct flist_head *entry;
1881 struct fio_client *client;
1882 size_t slen;
1883
1884 dprint(FD_NET, "client: send vtrigger: %s\n", cmd);
1885
1886 if (!cmd)
1887 slen = 0;
1888 else
1889 slen = strlen(cmd);
1890
1891 flist_for_each(entry, &client_list) {
1892 struct cmd_vtrigger_pdu *pdu;
1893
1894 client = flist_entry(entry, struct fio_client, list);
1895
1896 pdu = malloc(sizeof(*pdu) + slen);
1897 pdu->len = cpu_to_le16((uint16_t) slen);
1898 if (slen)
1899 memcpy(pdu->cmd, cmd, slen);
1900 fio_net_send_cmd(client->fd, FIO_NET_CMD_VTRIGGER, pdu,
1901 sizeof(*pdu) + slen, NULL, NULL);
1902 free(pdu);
1903 }
1904
1905 return 0;
1906}
1907
1908static void request_client_etas(struct client_ops *ops)
1909{
1910 struct fio_client *client;
1911 struct flist_head *entry;
1912 struct client_eta *eta;
1913 int skipped = 0;
1914
1915 if (eta_print == FIO_ETA_NEVER)
1916 return;
1917
1918 dprint(FD_NET, "client: request eta (%d)\n", nr_clients);
1919
1920 eta = calloc(1, sizeof(*eta) + __THREAD_RUNSTR_SZ(REAL_MAX_JOBS));
1921 eta->pending = nr_clients;
1922
1923 flist_for_each(entry, &client_list) {
1924 client = flist_entry(entry, struct fio_client, list);
1925
1926 if (!flist_empty(&client->eta_list)) {
1927 skipped++;
1928 continue;
1929 }
1930 if (client->state != Client_running)
1931 continue;
1932
1933 assert(!client->eta_in_flight);
1934 flist_add_tail(&client->eta_list, &eta_list);
1935 client->eta_in_flight = eta;
1936 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_SEND_ETA,
1937 (uintptr_t) eta, &client->cmd_list);
1938 }
1939
1940 while (skipped--) {
1941 if (!fio_client_dec_jobs_eta(eta, ops->eta))
1942 break;
1943 }
1944
1945 dprint(FD_NET, "client: requested eta tag %p\n", eta);
1946}
1947
1948/*
1949 * A single SEND_ETA timeout isn't fatal. Attempt to recover.
1950 */
1951static int handle_cmd_timeout(struct fio_client *client,
1952 struct fio_net_cmd_reply *reply)
1953{
1954 uint16_t reply_opcode = reply->opcode;
1955
1956 flist_del(&reply->list);
1957 free(reply);
1958
1959 if (reply_opcode != FIO_NET_CMD_SEND_ETA)
1960 return 1;
1961
1962 log_info("client <%s>: timeout on SEND_ETA\n", client->hostname);
1963
1964 flist_del_init(&client->eta_list);
1965 if (client->eta_in_flight) {
1966 fio_client_dec_jobs_eta(client->eta_in_flight, client->ops->eta);
1967 client->eta_in_flight = NULL;
1968 }
1969
1970 /*
1971 * If we fail 5 in a row, give up...
1972 */
1973 if (client->eta_timeouts++ > 5)
1974 return 1;
1975
1976 return 0;
1977}
1978
1979static int client_check_cmd_timeout(struct fio_client *client,
1980 struct timespec *now)
1981{
1982 struct fio_net_cmd_reply *reply;
1983 struct flist_head *entry, *tmp;
1984 int ret = 0;
1985
1986 flist_for_each_safe(entry, tmp, &client->cmd_list) {
1987 unsigned int op;
1988
1989 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
1990
1991 if (mtime_since(&reply->ts, now) < FIO_NET_CLIENT_TIMEOUT)
1992 continue;
1993
1994 op = reply->opcode;
1995 if (!handle_cmd_timeout(client, reply))
1996 continue;
1997
1998 log_err("fio: client %s, timeout on cmd %s\n", client->hostname,
1999 fio_server_op(op));
2000 ret = 1;
2001 }
2002
2003 return flist_empty(&client->cmd_list) && ret;
2004}
2005
2006static int fio_check_clients_timed_out(void)
2007{
2008 struct fio_client *client;
2009 struct flist_head *entry, *tmp;
2010 struct timespec ts;
2011 int ret = 0;
2012
2013 fio_gettime(&ts, NULL);
2014
2015 flist_for_each_safe(entry, tmp, &client_list) {
2016 client = flist_entry(entry, struct fio_client, list);
2017
2018 if (flist_empty(&client->cmd_list))
2019 continue;
2020
2021 if (!client_check_cmd_timeout(client, &ts))
2022 continue;
2023
2024 if (client->ops->timed_out)
2025 client->ops->timed_out(client);
2026 else
2027 log_err("fio: client %s timed out\n", client->hostname);
2028
2029 if (client->last_cmd != FIO_NET_CMD_VTRIGGER)
2030 client->error = ETIMEDOUT;
2031 else
2032 log_info("fio: ignoring timeout due to vtrigger\n");
2033 remove_client(client);
2034 ret = 1;
2035 }
2036
2037 return ret;
2038}
2039
2040int fio_handle_clients(struct client_ops *ops)
2041{
2042 struct pollfd *pfds;
2043 int i, ret = 0, retval = 0;
2044
2045 fio_gettime(&eta_ts, NULL);
2046
2047 pfds = malloc(nr_clients * sizeof(struct pollfd));
2048
2049 init_thread_stat(&client_ts);
2050 init_group_run_stat(&client_gs);
2051
2052 while (!exit_backend && nr_clients) {
2053 struct flist_head *entry, *tmp;
2054 struct fio_client *client;
2055
2056 i = 0;
2057 flist_for_each_safe(entry, tmp, &client_list) {
2058 client = flist_entry(entry, struct fio_client, list);
2059
2060 if (!client->sent_job && !client->ops->stay_connected &&
2061 flist_empty(&client->cmd_list)) {
2062 remove_client(client);
2063 continue;
2064 }
2065
2066 pfds[i].fd = client->fd;
2067 pfds[i].events = POLLIN;
2068 i++;
2069 }
2070
2071 if (!nr_clients)
2072 break;
2073
2074 assert(i == nr_clients);
2075
2076 do {
2077 struct timespec ts;
2078 int timeout;
2079
2080 fio_gettime(&ts, NULL);
2081 if (eta_time_within_slack(mtime_since(&eta_ts, &ts))) {
2082 request_client_etas(ops);
2083 memcpy(&eta_ts, &ts, sizeof(ts));
2084
2085 if (fio_check_clients_timed_out())
2086 break;
2087 }
2088
2089 check_trigger_file();
2090
2091 timeout = min(100u, ops->eta_msec);
2092
2093 ret = poll(pfds, nr_clients, timeout);
2094 if (ret < 0) {
2095 if (errno == EINTR)
2096 continue;
2097 log_err("fio: poll clients: %s\n", strerror(errno));
2098 break;
2099 } else if (!ret)
2100 continue;
2101 } while (ret <= 0);
2102
2103 for (i = 0; i < nr_clients; i++) {
2104 if (!(pfds[i].revents & POLLIN))
2105 continue;
2106
2107 client = find_client_by_fd(pfds[i].fd);
2108 if (!client) {
2109 log_err("fio: unknown client fd %ld\n", (long) pfds[i].fd);
2110 continue;
2111 }
2112 if (!fio_handle_client(client)) {
2113 log_info("client: host=%s disconnected\n",
2114 client->hostname);
2115 remove_client(client);
2116 retval = 1;
2117 } else if (client->error)
2118 retval = 1;
2119 fio_put_client(client);
2120 }
2121 }
2122
2123 fio_client_json_fini();
2124
2125 free(pfds);
2126 return retval || error_clients;
2127}
2128
2129static void client_display_thread_status(struct jobs_eta *je)
2130{
2131 if (!(output_format & FIO_OUTPUT_JSON))
2132 display_thread_status(je);
2133}