Fix 'min' latency times being 0 with ramp_time
[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 dst->nr_zone_resets = le64_to_cpu(src->nr_zone_resets);
1004
1005 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1006 dst->io_bytes[i] = le64_to_cpu(src->io_bytes[i]);
1007 dst->runtime[i] = le64_to_cpu(src->runtime[i]);
1008 }
1009
1010 dst->total_run_time = le64_to_cpu(src->total_run_time);
1011 dst->continue_on_error = le16_to_cpu(src->continue_on_error);
1012 dst->total_err_count = le64_to_cpu(src->total_err_count);
1013 dst->first_error = le32_to_cpu(src->first_error);
1014 dst->kb_base = le32_to_cpu(src->kb_base);
1015 dst->unit_base = le32_to_cpu(src->unit_base);
1016
1017 dst->sig_figs = le32_to_cpu(src->sig_figs);
1018
1019 dst->latency_depth = le32_to_cpu(src->latency_depth);
1020 dst->latency_target = le64_to_cpu(src->latency_target);
1021 dst->latency_window = le64_to_cpu(src->latency_window);
1022 dst->latency_percentile.u.f = fio_uint64_to_double(le64_to_cpu(src->latency_percentile.u.i));
1023
1024 dst->nr_block_infos = le64_to_cpu(src->nr_block_infos);
1025 for (i = 0; i < dst->nr_block_infos; i++)
1026 dst->block_infos[i] = le32_to_cpu(src->block_infos[i]);
1027
1028 dst->ss_dur = le64_to_cpu(src->ss_dur);
1029 dst->ss_state = le32_to_cpu(src->ss_state);
1030 dst->ss_head = le32_to_cpu(src->ss_head);
1031 dst->ss_limit.u.f = fio_uint64_to_double(le64_to_cpu(src->ss_limit.u.i));
1032 dst->ss_slope.u.f = fio_uint64_to_double(le64_to_cpu(src->ss_slope.u.i));
1033 dst->ss_deviation.u.f = fio_uint64_to_double(le64_to_cpu(src->ss_deviation.u.i));
1034 dst->ss_criterion.u.f = fio_uint64_to_double(le64_to_cpu(src->ss_criterion.u.i));
1035
1036 if (dst->ss_state & FIO_SS_DATA) {
1037 for (i = 0; i < dst->ss_dur; i++ ) {
1038 dst->ss_iops_data[i] = le64_to_cpu(src->ss_iops_data[i]);
1039 dst->ss_bw_data[i] = le64_to_cpu(src->ss_bw_data[i]);
1040 }
1041 }
1042
1043 dst->cachehit = le64_to_cpu(src->cachehit);
1044 dst->cachemiss = le64_to_cpu(src->cachemiss);
1045}
1046
1047static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
1048{
1049 int i;
1050
1051 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1052 dst->max_run[i] = le64_to_cpu(src->max_run[i]);
1053 dst->min_run[i] = le64_to_cpu(src->min_run[i]);
1054 dst->max_bw[i] = le64_to_cpu(src->max_bw[i]);
1055 dst->min_bw[i] = le64_to_cpu(src->min_bw[i]);
1056 dst->iobytes[i] = le64_to_cpu(src->iobytes[i]);
1057 dst->agg[i] = le64_to_cpu(src->agg[i]);
1058 }
1059
1060 dst->kb_base = le32_to_cpu(src->kb_base);
1061 dst->unit_base = le32_to_cpu(src->unit_base);
1062 dst->sig_figs = le32_to_cpu(src->sig_figs);
1063 dst->groupid = le32_to_cpu(src->groupid);
1064 dst->unified_rw_rep = le32_to_cpu(src->unified_rw_rep);
1065}
1066
1067static void json_object_add_client_info(struct json_object *obj,
1068 struct fio_client *client)
1069{
1070 const char *hostname = client->hostname ? client->hostname : "";
1071
1072 json_object_add_value_string(obj, "hostname", hostname);
1073 json_object_add_value_int(obj, "port", client->port);
1074}
1075
1076static void handle_ts(struct fio_client *client, struct fio_net_cmd *cmd)
1077{
1078 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
1079 struct flist_head *opt_list = NULL;
1080 struct json_object *tsobj;
1081
1082 if (client->opt_lists && p->ts.thread_number <= client->jobs)
1083 opt_list = &client->opt_lists[p->ts.thread_number - 1];
1084
1085 tsobj = show_thread_status(&p->ts, &p->rs, opt_list, &client->buf);
1086 client->did_stat = true;
1087 if (tsobj) {
1088 json_object_add_client_info(tsobj, client);
1089 json_array_add_value_object(clients_array, tsobj);
1090 }
1091
1092 if (sum_stat_clients <= 1)
1093 return;
1094
1095 sum_thread_stats(&client_ts, &p->ts, sum_stat_nr == 1);
1096 sum_group_stats(&client_gs, &p->rs);
1097
1098 client_ts.members++;
1099 client_ts.thread_number = p->ts.thread_number;
1100 client_ts.groupid = p->ts.groupid;
1101 client_ts.unified_rw_rep = p->ts.unified_rw_rep;
1102 client_ts.sig_figs = p->ts.sig_figs;
1103
1104 if (++sum_stat_nr == sum_stat_clients) {
1105 strcpy(client_ts.name, "All clients");
1106 tsobj = show_thread_status(&client_ts, &client_gs, NULL, &client->buf);
1107 if (tsobj) {
1108 json_object_add_client_info(tsobj, client);
1109 json_array_add_value_object(clients_array, tsobj);
1110 }
1111 }
1112}
1113
1114static void handle_gs(struct fio_client *client, struct fio_net_cmd *cmd)
1115{
1116 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
1117
1118 if (output_format & FIO_OUTPUT_NORMAL)
1119 show_group_stats(gs, &client->buf);
1120}
1121
1122static void handle_job_opt(struct fio_client *client, struct fio_net_cmd *cmd)
1123{
1124 struct cmd_job_option *pdu = (struct cmd_job_option *) cmd->payload;
1125 struct print_option *p;
1126
1127 if (!job_opt_object)
1128 return;
1129
1130 pdu->global = le16_to_cpu(pdu->global);
1131 pdu->truncated = le16_to_cpu(pdu->truncated);
1132 pdu->groupid = le32_to_cpu(pdu->groupid);
1133
1134 p = malloc(sizeof(*p));
1135 p->name = strdup((char *) pdu->name);
1136 if (pdu->value[0] != '\0')
1137 p->value = strdup((char *) pdu->value);
1138 else
1139 p->value = NULL;
1140
1141 if (pdu->global) {
1142 const char *pos = "";
1143
1144 if (p->value)
1145 pos = p->value;
1146
1147 json_object_add_value_string(job_opt_object, p->name, pos);
1148 } else if (client->opt_lists) {
1149 struct flist_head *opt_list = &client->opt_lists[pdu->groupid];
1150
1151 flist_add_tail(&p->list, opt_list);
1152 }
1153}
1154
1155static void handle_text(struct fio_client *client, struct fio_net_cmd *cmd)
1156{
1157 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
1158 const char *buf = (const char *) pdu->buf;
1159 const char *name;
1160 int fio_unused ret;
1161 struct buf_output out;
1162
1163 buf_output_init(&out);
1164
1165 name = client->name ? client->name : client->hostname;
1166
1167 if (!client->skip_newline && !(output_format & FIO_OUTPUT_TERSE))
1168 __log_buf(&out, "<%s> ", name);
1169 __log_buf(&out, "%s", buf);
1170 log_info_buf(out.buf, out.buflen);
1171 buf_output_free(&out);
1172 client->skip_newline = strchr(buf, '\n') == NULL;
1173}
1174
1175static void convert_agg(struct disk_util_agg *agg)
1176{
1177 int i;
1178
1179 for (i = 0; i < 2; i++) {
1180 agg->ios[i] = le64_to_cpu(agg->ios[i]);
1181 agg->merges[i] = le64_to_cpu(agg->merges[i]);
1182 agg->sectors[i] = le64_to_cpu(agg->sectors[i]);
1183 agg->ticks[i] = le64_to_cpu(agg->ticks[i]);
1184 }
1185
1186 agg->io_ticks = le64_to_cpu(agg->io_ticks);
1187 agg->time_in_queue = le64_to_cpu(agg->time_in_queue);
1188 agg->slavecount = le32_to_cpu(agg->slavecount);
1189 agg->max_util.u.f = fio_uint64_to_double(le64_to_cpu(agg->max_util.u.i));
1190}
1191
1192static void convert_dus(struct disk_util_stat *dus)
1193{
1194 int i;
1195
1196 for (i = 0; i < 2; i++) {
1197 dus->s.ios[i] = le64_to_cpu(dus->s.ios[i]);
1198 dus->s.merges[i] = le64_to_cpu(dus->s.merges[i]);
1199 dus->s.sectors[i] = le64_to_cpu(dus->s.sectors[i]);
1200 dus->s.ticks[i] = le64_to_cpu(dus->s.ticks[i]);
1201 }
1202
1203 dus->s.io_ticks = le64_to_cpu(dus->s.io_ticks);
1204 dus->s.time_in_queue = le64_to_cpu(dus->s.time_in_queue);
1205 dus->s.msec = le64_to_cpu(dus->s.msec);
1206}
1207
1208static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd)
1209{
1210 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
1211
1212 if (!client->disk_stats_shown)
1213 client->disk_stats_shown = true;
1214
1215 if (output_format & FIO_OUTPUT_JSON) {
1216 struct json_object *duobj;
1217
1218 json_array_add_disk_util(&du->dus, &du->agg, du_array);
1219 duobj = json_array_last_value_object(du_array);
1220 json_object_add_client_info(duobj, client);
1221 } else if (output_format & FIO_OUTPUT_TERSE)
1222 print_disk_util(&du->dus, &du->agg, 1, &client->buf);
1223 else if (output_format & FIO_OUTPUT_NORMAL) {
1224 __log_buf(&client->buf, "\nDisk stats (read/write):\n");
1225 print_disk_util(&du->dus, &du->agg, 0, &client->buf);
1226 }
1227}
1228
1229static void convert_jobs_eta(struct jobs_eta *je)
1230{
1231 int i;
1232
1233 je->nr_running = le32_to_cpu(je->nr_running);
1234 je->nr_ramp = le32_to_cpu(je->nr_ramp);
1235 je->nr_pending = le32_to_cpu(je->nr_pending);
1236 je->nr_setting_up = le32_to_cpu(je->nr_setting_up);
1237 je->files_open = le32_to_cpu(je->files_open);
1238
1239 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1240 je->m_rate[i] = le64_to_cpu(je->m_rate[i]);
1241 je->t_rate[i] = le64_to_cpu(je->t_rate[i]);
1242 je->m_iops[i] = le32_to_cpu(je->m_iops[i]);
1243 je->t_iops[i] = le32_to_cpu(je->t_iops[i]);
1244 je->rate[i] = le64_to_cpu(je->rate[i]);
1245 je->iops[i] = le32_to_cpu(je->iops[i]);
1246 }
1247
1248 je->elapsed_sec = le64_to_cpu(je->elapsed_sec);
1249 je->eta_sec = le64_to_cpu(je->eta_sec);
1250 je->nr_threads = le32_to_cpu(je->nr_threads);
1251 je->is_pow2 = le32_to_cpu(je->is_pow2);
1252 je->unit_base = le32_to_cpu(je->unit_base);
1253 je->sig_figs = le32_to_cpu(je->sig_figs);
1254}
1255
1256void fio_client_sum_jobs_eta(struct jobs_eta *dst, struct jobs_eta *je)
1257{
1258 int i;
1259
1260 dst->nr_running += je->nr_running;
1261 dst->nr_ramp += je->nr_ramp;
1262 dst->nr_pending += je->nr_pending;
1263 dst->nr_setting_up += je->nr_setting_up;
1264 dst->files_open += je->files_open;
1265
1266 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1267 dst->m_rate[i] += je->m_rate[i];
1268 dst->t_rate[i] += je->t_rate[i];
1269 dst->m_iops[i] += je->m_iops[i];
1270 dst->t_iops[i] += je->t_iops[i];
1271 dst->rate[i] += je->rate[i];
1272 dst->iops[i] += je->iops[i];
1273 }
1274
1275 dst->elapsed_sec += je->elapsed_sec;
1276
1277 if (je->eta_sec > dst->eta_sec)
1278 dst->eta_sec = je->eta_sec;
1279
1280 dst->nr_threads += je->nr_threads;
1281
1282 /*
1283 * This wont be correct for multiple strings, but at least it
1284 * works for the basic cases.
1285 */
1286 strcpy((char *) dst->run_str, (char *) je->run_str);
1287}
1288
1289static bool remove_reply_cmd(struct fio_client *client, struct fio_net_cmd *cmd)
1290{
1291 struct fio_net_cmd_reply *reply = NULL;
1292 struct flist_head *entry;
1293
1294 flist_for_each(entry, &client->cmd_list) {
1295 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
1296
1297 if (cmd->tag == (uintptr_t) reply)
1298 break;
1299
1300 reply = NULL;
1301 }
1302
1303 if (!reply) {
1304 log_err("fio: client: unable to find matching tag (%llx)\n", (unsigned long long) cmd->tag);
1305 return false;
1306 }
1307
1308 flist_del(&reply->list);
1309 cmd->tag = reply->saved_tag;
1310 free(reply);
1311 return true;
1312}
1313
1314int fio_client_wait_for_reply(struct fio_client *client, uint64_t tag)
1315{
1316 do {
1317 struct fio_net_cmd_reply *reply = NULL;
1318 struct flist_head *entry;
1319
1320 flist_for_each(entry, &client->cmd_list) {
1321 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
1322
1323 if (tag == (uintptr_t) reply)
1324 break;
1325
1326 reply = NULL;
1327 }
1328
1329 if (!reply)
1330 break;
1331
1332 usleep(1000);
1333 } while (1);
1334
1335 return 0;
1336}
1337
1338static void handle_eta(struct fio_client *client, struct fio_net_cmd *cmd)
1339{
1340 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
1341 struct client_eta *eta = (struct client_eta *) (uintptr_t) cmd->tag;
1342
1343 dprint(FD_NET, "client: got eta tag %p, %d\n", eta, eta->pending);
1344
1345 assert(client->eta_in_flight == eta);
1346
1347 client->eta_in_flight = NULL;
1348 flist_del_init(&client->eta_list);
1349 client->eta_timeouts = 0;
1350
1351 if (client->ops->jobs_eta)
1352 client->ops->jobs_eta(client, je);
1353
1354 fio_client_sum_jobs_eta(&eta->eta, je);
1355 fio_client_dec_jobs_eta(eta, client->ops->eta);
1356}
1357
1358static void client_flush_hist_samples(FILE *f, int hist_coarseness, void *samples,
1359 uint64_t sample_size)
1360{
1361 struct io_sample *s;
1362 int log_offset;
1363 uint64_t i, j, nr_samples;
1364 struct io_u_plat_entry *entry;
1365 uint64_t *io_u_plat;
1366
1367 int stride = 1 << hist_coarseness;
1368
1369 if (!sample_size)
1370 return;
1371
1372 s = __get_sample(samples, 0, 0);
1373 log_offset = (s->__ddir & LOG_OFFSET_SAMPLE_BIT) != 0;
1374
1375 nr_samples = sample_size / __log_entry_sz(log_offset);
1376
1377 for (i = 0; i < nr_samples; i++) {
1378
1379 s = (struct io_sample *)((char *)__get_sample(samples, log_offset, i) +
1380 i * sizeof(struct io_u_plat_entry));
1381
1382 entry = s->data.plat_entry;
1383 io_u_plat = entry->io_u_plat;
1384
1385 fprintf(f, "%lu, %u, %llu, ", (unsigned long) s->time,
1386 io_sample_ddir(s), (unsigned long long) s->bs);
1387 for (j = 0; j < FIO_IO_U_PLAT_NR - stride; j += stride) {
1388 fprintf(f, "%llu, ", (unsigned long long)hist_sum(j, stride, io_u_plat, NULL));
1389 }
1390 fprintf(f, "%llu\n", (unsigned long long)
1391 hist_sum(FIO_IO_U_PLAT_NR - stride, stride, io_u_plat, NULL));
1392
1393 }
1394}
1395
1396static int fio_client_handle_iolog(struct fio_client *client,
1397 struct fio_net_cmd *cmd)
1398{
1399 struct cmd_iolog_pdu *pdu = NULL;
1400 bool store_direct;
1401 char *log_pathname = NULL;
1402 int ret = 0;
1403
1404 pdu = convert_iolog(cmd, &store_direct);
1405 if (!pdu) {
1406 log_err("fio: failed converting IO log\n");
1407 ret = 1;
1408 goto out;
1409 }
1410
1411 /* allocate buffer big enough for next sprintf() call */
1412 log_pathname = malloc(10 + strlen((char *)pdu->name) +
1413 strlen(client->hostname));
1414 if (!log_pathname) {
1415 log_err("fio: memory allocation of unique pathname failed\n");
1416 ret = -1;
1417 goto out;
1418 }
1419 /* generate a unique pathname for the log file using hostname */
1420 sprintf(log_pathname, "%s.%s", pdu->name, client->hostname);
1421
1422 if (store_direct) {
1423 ssize_t wrote;
1424 size_t sz;
1425 int fd;
1426
1427 fd = open((const char *) log_pathname,
1428 O_WRONLY | O_CREAT | O_TRUNC, 0644);
1429 if (fd < 0) {
1430 log_err("fio: open log %s: %s\n",
1431 log_pathname, strerror(errno));
1432 ret = 1;
1433 goto out;
1434 }
1435
1436 sz = cmd->pdu_len - sizeof(*pdu);
1437 wrote = write(fd, pdu->samples, sz);
1438 close(fd);
1439
1440 if (wrote != sz) {
1441 log_err("fio: short write on compressed log\n");
1442 ret = 1;
1443 goto out;
1444 }
1445
1446 ret = 0;
1447 } else {
1448 FILE *f;
1449 f = fopen((const char *) log_pathname, "w");
1450 if (!f) {
1451 log_err("fio: fopen log %s : %s\n",
1452 log_pathname, strerror(errno));
1453 ret = 1;
1454 goto out;
1455 }
1456
1457 if (pdu->log_type == IO_LOG_TYPE_HIST) {
1458 client_flush_hist_samples(f, pdu->log_hist_coarseness, pdu->samples,
1459 pdu->nr_samples * sizeof(struct io_sample));
1460 } else {
1461 flush_samples(f, pdu->samples,
1462 pdu->nr_samples * sizeof(struct io_sample));
1463 }
1464 fclose(f);
1465 ret = 0;
1466 }
1467
1468out:
1469 if (pdu && pdu != (void *) cmd->payload)
1470 free(pdu);
1471
1472 if (log_pathname)
1473 free(log_pathname);
1474
1475 return ret;
1476}
1477
1478static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd)
1479{
1480 struct cmd_probe_reply_pdu *probe = (struct cmd_probe_reply_pdu *) cmd->payload;
1481 const char *os, *arch;
1482 char bit[16];
1483
1484 os = fio_get_os_string(probe->os);
1485 if (!os)
1486 os = "unknown";
1487
1488 arch = fio_get_arch_string(probe->arch);
1489 if (!arch)
1490 os = "unknown";
1491
1492 sprintf(bit, "%d-bit", probe->bpp * 8);
1493 probe->flags = le64_to_cpu(probe->flags);
1494
1495 if (output_format & FIO_OUTPUT_NORMAL) {
1496 log_info("hostname=%s, be=%u, %s, os=%s, arch=%s, fio=%s, flags=%lx\n",
1497 probe->hostname, probe->bigendian, bit, os, arch,
1498 probe->fio_version, (unsigned long) probe->flags);
1499 }
1500
1501 if (!client->name)
1502 client->name = strdup((char *) probe->hostname);
1503}
1504
1505static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd)
1506{
1507 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
1508
1509 client->state = Client_started;
1510 client->jobs = le32_to_cpu(pdu->jobs);
1511 client->nr_stat = le32_to_cpu(pdu->stat_outputs);
1512
1513 if (client->jobs) {
1514 int i;
1515
1516 if (client->opt_lists)
1517 free(client->opt_lists);
1518
1519 client->opt_lists = malloc(client->jobs * sizeof(struct flist_head));
1520 for (i = 0; i < client->jobs; i++)
1521 INIT_FLIST_HEAD(&client->opt_lists[i]);
1522 }
1523
1524 sum_stat_clients += client->nr_stat;
1525}
1526
1527static void handle_stop(struct fio_client *client)
1528{
1529 if (client->error)
1530 log_info("client <%s>: exited with error %d\n", client->hostname, client->error);
1531}
1532
1533static void convert_stop(struct fio_net_cmd *cmd)
1534{
1535 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
1536
1537 pdu->error = le32_to_cpu(pdu->error);
1538}
1539
1540static void convert_text(struct fio_net_cmd *cmd)
1541{
1542 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
1543
1544 pdu->level = le32_to_cpu(pdu->level);
1545 pdu->buf_len = le32_to_cpu(pdu->buf_len);
1546 pdu->log_sec = le64_to_cpu(pdu->log_sec);
1547 pdu->log_usec = le64_to_cpu(pdu->log_usec);
1548}
1549
1550static struct cmd_iolog_pdu *convert_iolog_gz(struct fio_net_cmd *cmd,
1551 struct cmd_iolog_pdu *pdu)
1552{
1553#ifdef CONFIG_ZLIB
1554 struct cmd_iolog_pdu *ret;
1555 z_stream stream;
1556 uint64_t nr_samples;
1557 size_t total;
1558 char *p;
1559
1560 stream.zalloc = Z_NULL;
1561 stream.zfree = Z_NULL;
1562 stream.opaque = Z_NULL;
1563 stream.avail_in = 0;
1564 stream.next_in = Z_NULL;
1565
1566 if (inflateInit(&stream) != Z_OK)
1567 return NULL;
1568
1569 /*
1570 * Get header first, it's not compressed
1571 */
1572 nr_samples = le64_to_cpu(pdu->nr_samples);
1573
1574 if (pdu->log_type == IO_LOG_TYPE_HIST)
1575 total = nr_samples * (__log_entry_sz(le32_to_cpu(pdu->log_offset)) +
1576 sizeof(struct io_u_plat_entry));
1577 else
1578 total = nr_samples * __log_entry_sz(le32_to_cpu(pdu->log_offset));
1579 ret = malloc(total + sizeof(*pdu));
1580 ret->nr_samples = nr_samples;
1581
1582 memcpy(ret, pdu, sizeof(*pdu));
1583
1584 p = (char *) ret + sizeof(*pdu);
1585
1586 stream.avail_in = cmd->pdu_len - sizeof(*pdu);
1587 stream.next_in = (void *)((char *) pdu + sizeof(*pdu));
1588 while (stream.avail_in) {
1589 unsigned int this_chunk = 65536;
1590 unsigned int this_len;
1591 int err;
1592
1593 if (this_chunk > total)
1594 this_chunk = total;
1595
1596 stream.avail_out = this_chunk;
1597 stream.next_out = (void *)p;
1598 err = inflate(&stream, Z_NO_FLUSH);
1599 /* may be Z_OK, or Z_STREAM_END */
1600 if (err < 0) {
1601 log_err("fio: inflate error %d\n", err);
1602 free(ret);
1603 ret = NULL;
1604 goto err;
1605 }
1606
1607 this_len = this_chunk - stream.avail_out;
1608 p += this_len;
1609 total -= this_len;
1610 }
1611
1612err:
1613 inflateEnd(&stream);
1614 return ret;
1615#else
1616 return NULL;
1617#endif
1618}
1619
1620/*
1621 * This has been compressed on the server side, since it can be big.
1622 * Uncompress here.
1623 */
1624static struct cmd_iolog_pdu *convert_iolog(struct fio_net_cmd *cmd,
1625 bool *store_direct)
1626{
1627 struct cmd_iolog_pdu *pdu = (struct cmd_iolog_pdu *) cmd->payload;
1628 struct cmd_iolog_pdu *ret;
1629 uint64_t i;
1630 int compressed;
1631 void *samples;
1632
1633 *store_direct = false;
1634
1635 /*
1636 * Convert if compressed and we support it. If it's not
1637 * compressed, we need not do anything.
1638 */
1639 compressed = le32_to_cpu(pdu->compressed);
1640 if (compressed == XMIT_COMPRESSED) {
1641#ifndef CONFIG_ZLIB
1642 log_err("fio: server sent compressed data by mistake\n");
1643 return NULL;
1644#endif
1645 ret = convert_iolog_gz(cmd, pdu);
1646 if (!ret) {
1647 log_err("fio: failed decompressing log\n");
1648 return NULL;
1649 }
1650 } else if (compressed == STORE_COMPRESSED) {
1651 *store_direct = true;
1652 ret = pdu;
1653 } else
1654 ret = pdu;
1655
1656 ret->nr_samples = le64_to_cpu(ret->nr_samples);
1657 ret->thread_number = le32_to_cpu(ret->thread_number);
1658 ret->log_type = le32_to_cpu(ret->log_type);
1659 ret->compressed = le32_to_cpu(ret->compressed);
1660 ret->log_offset = le32_to_cpu(ret->log_offset);
1661 ret->log_hist_coarseness = le32_to_cpu(ret->log_hist_coarseness);
1662
1663 if (*store_direct)
1664 return ret;
1665
1666 samples = &ret->samples[0];
1667 for (i = 0; i < ret->nr_samples; i++) {
1668 struct io_sample *s;
1669
1670 s = __get_sample(samples, ret->log_offset, i);
1671 if (ret->log_type == IO_LOG_TYPE_HIST)
1672 s = (struct io_sample *)((char *)s + sizeof(struct io_u_plat_entry) * i);
1673
1674 s->time = le64_to_cpu(s->time);
1675 s->data.val = le64_to_cpu(s->data.val);
1676 s->__ddir = le32_to_cpu(s->__ddir);
1677 s->bs = le64_to_cpu(s->bs);
1678
1679 if (ret->log_offset) {
1680 struct io_sample_offset *so = (void *) s;
1681
1682 so->offset = le64_to_cpu(so->offset);
1683 }
1684
1685 if (ret->log_type == IO_LOG_TYPE_HIST) {
1686 s->data.plat_entry = (struct io_u_plat_entry *)(((char *)s) + sizeof(*s));
1687 s->data.plat_entry->list.next = NULL;
1688 s->data.plat_entry->list.prev = NULL;
1689 }
1690 }
1691
1692 return ret;
1693}
1694
1695static void sendfile_reply(int fd, struct cmd_sendfile_reply *rep,
1696 size_t size, uint64_t tag)
1697{
1698 rep->error = cpu_to_le32(rep->error);
1699 fio_net_send_cmd(fd, FIO_NET_CMD_SENDFILE, rep, size, &tag, NULL);
1700}
1701
1702static int fio_send_file(struct fio_client *client, struct cmd_sendfile *pdu,
1703 uint64_t tag)
1704{
1705 struct cmd_sendfile_reply *rep;
1706 struct stat sb;
1707 size_t size;
1708 int fd;
1709
1710 size = sizeof(*rep);
1711 rep = malloc(size);
1712
1713 if (stat((char *)pdu->path, &sb) < 0) {
1714fail:
1715 rep->error = errno;
1716 sendfile_reply(client->fd, rep, size, tag);
1717 free(rep);
1718 return 1;
1719 }
1720
1721 size += sb.st_size;
1722 rep = realloc(rep, size);
1723 rep->size = cpu_to_le32((uint32_t) sb.st_size);
1724
1725 fd = open((char *)pdu->path, O_RDONLY);
1726 if (fd == -1 )
1727 goto fail;
1728
1729 rep->error = read_data(fd, &rep->data, sb.st_size);
1730 sendfile_reply(client->fd, rep, size, tag);
1731 free(rep);
1732 close(fd);
1733 return 0;
1734}
1735
1736int fio_handle_client(struct fio_client *client)
1737{
1738 struct client_ops *ops = client->ops;
1739 struct fio_net_cmd *cmd;
1740 int size;
1741
1742 dprint(FD_NET, "client: handle %s\n", client->hostname);
1743
1744 cmd = fio_net_recv_cmd(client->fd, true);
1745 if (!cmd)
1746 return 0;
1747
1748 dprint(FD_NET, "client: got cmd op %s from %s (pdu=%u)\n",
1749 fio_server_op(cmd->opcode), client->hostname, cmd->pdu_len);
1750
1751 client->last_cmd = cmd->opcode;
1752
1753 switch (cmd->opcode) {
1754 case FIO_NET_CMD_QUIT:
1755 if (ops->quit)
1756 ops->quit(client, cmd);
1757 remove_client(client);
1758 break;
1759 case FIO_NET_CMD_TEXT:
1760 convert_text(cmd);
1761 ops->text(client, cmd);
1762 break;
1763 case FIO_NET_CMD_DU: {
1764 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
1765
1766 convert_dus(&du->dus);
1767 convert_agg(&du->agg);
1768
1769 ops->disk_util(client, cmd);
1770 break;
1771 }
1772 case FIO_NET_CMD_TS: {
1773 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
1774
1775 dprint(FD_NET, "client: ts->ss_state = %u\n", (unsigned int) le32_to_cpu(p->ts.ss_state));
1776 if (le32_to_cpu(p->ts.ss_state) & FIO_SS_DATA) {
1777 dprint(FD_NET, "client: received steadystate ring buffers\n");
1778
1779 size = le64_to_cpu(p->ts.ss_dur);
1780 p->ts.ss_iops_data = (uint64_t *) ((struct cmd_ts_pdu *)cmd->payload + 1);
1781 p->ts.ss_bw_data = p->ts.ss_iops_data + size;
1782 }
1783
1784 convert_ts(&p->ts, &p->ts);
1785 convert_gs(&p->rs, &p->rs);
1786
1787 ops->thread_status(client, cmd);
1788 break;
1789 }
1790 case FIO_NET_CMD_GS: {
1791 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
1792
1793 convert_gs(gs, gs);
1794
1795 ops->group_stats(client, cmd);
1796 break;
1797 }
1798 case FIO_NET_CMD_ETA: {
1799 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
1800
1801 if (!remove_reply_cmd(client, cmd))
1802 break;
1803 convert_jobs_eta(je);
1804 handle_eta(client, cmd);
1805 break;
1806 }
1807 case FIO_NET_CMD_PROBE:
1808 remove_reply_cmd(client, cmd);
1809 ops->probe(client, cmd);
1810 break;
1811 case FIO_NET_CMD_SERVER_START:
1812 client->state = Client_running;
1813 if (ops->job_start)
1814 ops->job_start(client, cmd);
1815 break;
1816 case FIO_NET_CMD_START: {
1817 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
1818
1819 pdu->jobs = le32_to_cpu(pdu->jobs);
1820 ops->start(client, cmd);
1821 break;
1822 }
1823 case FIO_NET_CMD_STOP: {
1824 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
1825
1826 convert_stop(cmd);
1827 client->state = Client_stopped;
1828 client->error = le32_to_cpu(pdu->error);
1829 client->signal = le32_to_cpu(pdu->signal);
1830 ops->stop(client);
1831 break;
1832 }
1833 case FIO_NET_CMD_ADD_JOB: {
1834 struct cmd_add_job_pdu *pdu = (struct cmd_add_job_pdu *) cmd->payload;
1835
1836 client->thread_number = le32_to_cpu(pdu->thread_number);
1837 client->groupid = le32_to_cpu(pdu->groupid);
1838
1839 if (ops->add_job)
1840 ops->add_job(client, cmd);
1841 break;
1842 }
1843 case FIO_NET_CMD_IOLOG:
1844 fio_client_handle_iolog(client, cmd);
1845 break;
1846 case FIO_NET_CMD_UPDATE_JOB:
1847 ops->update_job(client, cmd);
1848 remove_reply_cmd(client, cmd);
1849 break;
1850 case FIO_NET_CMD_VTRIGGER: {
1851 struct all_io_list *pdu = (struct all_io_list *) cmd->payload;
1852 char buf[128];
1853 int off = 0;
1854
1855 if (aux_path) {
1856 strcpy(buf, aux_path);
1857 off = strlen(buf);
1858 }
1859
1860 __verify_save_state(pdu, server_name(client, &buf[off], sizeof(buf) - off));
1861 exec_trigger(trigger_cmd);
1862 break;
1863 }
1864 case FIO_NET_CMD_SENDFILE: {
1865 struct cmd_sendfile *pdu = (struct cmd_sendfile *) cmd->payload;
1866 fio_send_file(client, pdu, cmd->tag);
1867 break;
1868 }
1869 case FIO_NET_CMD_JOB_OPT: {
1870 handle_job_opt(client, cmd);
1871 break;
1872 }
1873 default:
1874 log_err("fio: unknown client op: %s\n", fio_server_op(cmd->opcode));
1875 break;
1876 }
1877
1878 free(cmd);
1879 return 1;
1880}
1881
1882int fio_clients_send_trigger(const char *cmd)
1883{
1884 struct flist_head *entry;
1885 struct fio_client *client;
1886 size_t slen;
1887
1888 dprint(FD_NET, "client: send vtrigger: %s\n", cmd);
1889
1890 if (!cmd)
1891 slen = 0;
1892 else
1893 slen = strlen(cmd);
1894
1895 flist_for_each(entry, &client_list) {
1896 struct cmd_vtrigger_pdu *pdu;
1897
1898 client = flist_entry(entry, struct fio_client, list);
1899
1900 pdu = malloc(sizeof(*pdu) + slen);
1901 pdu->len = cpu_to_le16((uint16_t) slen);
1902 if (slen)
1903 memcpy(pdu->cmd, cmd, slen);
1904 fio_net_send_cmd(client->fd, FIO_NET_CMD_VTRIGGER, pdu,
1905 sizeof(*pdu) + slen, NULL, NULL);
1906 free(pdu);
1907 }
1908
1909 return 0;
1910}
1911
1912static void request_client_etas(struct client_ops *ops)
1913{
1914 struct fio_client *client;
1915 struct flist_head *entry;
1916 struct client_eta *eta;
1917 int skipped = 0;
1918
1919 if (eta_print == FIO_ETA_NEVER)
1920 return;
1921
1922 dprint(FD_NET, "client: request eta (%d)\n", nr_clients);
1923
1924 eta = calloc(1, sizeof(*eta) + __THREAD_RUNSTR_SZ(REAL_MAX_JOBS));
1925 eta->pending = nr_clients;
1926
1927 flist_for_each(entry, &client_list) {
1928 client = flist_entry(entry, struct fio_client, list);
1929
1930 if (!flist_empty(&client->eta_list)) {
1931 skipped++;
1932 continue;
1933 }
1934 if (client->state != Client_running)
1935 continue;
1936
1937 assert(!client->eta_in_flight);
1938 flist_add_tail(&client->eta_list, &eta_list);
1939 client->eta_in_flight = eta;
1940 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_SEND_ETA,
1941 (uintptr_t) eta, &client->cmd_list);
1942 }
1943
1944 while (skipped--) {
1945 if (!fio_client_dec_jobs_eta(eta, ops->eta))
1946 break;
1947 }
1948
1949 dprint(FD_NET, "client: requested eta tag %p\n", eta);
1950}
1951
1952/*
1953 * A single SEND_ETA timeout isn't fatal. Attempt to recover.
1954 */
1955static int handle_cmd_timeout(struct fio_client *client,
1956 struct fio_net_cmd_reply *reply)
1957{
1958 uint16_t reply_opcode = reply->opcode;
1959
1960 flist_del(&reply->list);
1961 free(reply);
1962
1963 if (reply_opcode != FIO_NET_CMD_SEND_ETA)
1964 return 1;
1965
1966 log_info("client <%s>: timeout on SEND_ETA\n", client->hostname);
1967
1968 flist_del_init(&client->eta_list);
1969 if (client->eta_in_flight) {
1970 fio_client_dec_jobs_eta(client->eta_in_flight, client->ops->eta);
1971 client->eta_in_flight = NULL;
1972 }
1973
1974 /*
1975 * If we fail 5 in a row, give up...
1976 */
1977 if (client->eta_timeouts++ > 5)
1978 return 1;
1979
1980 return 0;
1981}
1982
1983static int client_check_cmd_timeout(struct fio_client *client,
1984 struct timespec *now)
1985{
1986 struct fio_net_cmd_reply *reply;
1987 struct flist_head *entry, *tmp;
1988 int ret = 0;
1989
1990 flist_for_each_safe(entry, tmp, &client->cmd_list) {
1991 unsigned int op;
1992
1993 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
1994
1995 if (mtime_since(&reply->ts, now) < FIO_NET_CLIENT_TIMEOUT)
1996 continue;
1997
1998 op = reply->opcode;
1999 if (!handle_cmd_timeout(client, reply))
2000 continue;
2001
2002 log_err("fio: client %s, timeout on cmd %s\n", client->hostname,
2003 fio_server_op(op));
2004 ret = 1;
2005 }
2006
2007 return flist_empty(&client->cmd_list) && ret;
2008}
2009
2010static int fio_check_clients_timed_out(void)
2011{
2012 struct fio_client *client;
2013 struct flist_head *entry, *tmp;
2014 struct timespec ts;
2015 int ret = 0;
2016
2017 fio_gettime(&ts, NULL);
2018
2019 flist_for_each_safe(entry, tmp, &client_list) {
2020 client = flist_entry(entry, struct fio_client, list);
2021
2022 if (flist_empty(&client->cmd_list))
2023 continue;
2024
2025 if (!client_check_cmd_timeout(client, &ts))
2026 continue;
2027
2028 if (client->ops->timed_out)
2029 client->ops->timed_out(client);
2030 else
2031 log_err("fio: client %s timed out\n", client->hostname);
2032
2033 if (client->last_cmd != FIO_NET_CMD_VTRIGGER)
2034 client->error = ETIMEDOUT;
2035 else
2036 log_info("fio: ignoring timeout due to vtrigger\n");
2037 remove_client(client);
2038 ret = 1;
2039 }
2040
2041 return ret;
2042}
2043
2044int fio_handle_clients(struct client_ops *ops)
2045{
2046 struct pollfd *pfds;
2047 int i, ret = 0, retval = 0;
2048
2049 fio_gettime(&eta_ts, NULL);
2050
2051 pfds = malloc(nr_clients * sizeof(struct pollfd));
2052
2053 init_thread_stat(&client_ts);
2054 init_group_run_stat(&client_gs);
2055
2056 while (!exit_backend && nr_clients) {
2057 struct flist_head *entry, *tmp;
2058 struct fio_client *client;
2059
2060 i = 0;
2061 flist_for_each_safe(entry, tmp, &client_list) {
2062 client = flist_entry(entry, struct fio_client, list);
2063
2064 if (!client->sent_job && !client->ops->stay_connected &&
2065 flist_empty(&client->cmd_list)) {
2066 remove_client(client);
2067 continue;
2068 }
2069
2070 pfds[i].fd = client->fd;
2071 pfds[i].events = POLLIN;
2072 i++;
2073 }
2074
2075 if (!nr_clients)
2076 break;
2077
2078 assert(i == nr_clients);
2079
2080 do {
2081 struct timespec ts;
2082 int timeout;
2083
2084 fio_gettime(&ts, NULL);
2085 if (eta_time_within_slack(mtime_since(&eta_ts, &ts))) {
2086 request_client_etas(ops);
2087 memcpy(&eta_ts, &ts, sizeof(ts));
2088
2089 if (fio_check_clients_timed_out())
2090 break;
2091 }
2092
2093 check_trigger_file();
2094
2095 timeout = min(100u, ops->eta_msec);
2096
2097 ret = poll(pfds, nr_clients, timeout);
2098 if (ret < 0) {
2099 if (errno == EINTR)
2100 continue;
2101 log_err("fio: poll clients: %s\n", strerror(errno));
2102 break;
2103 } else if (!ret)
2104 continue;
2105 } while (ret <= 0);
2106
2107 for (i = 0; i < nr_clients; i++) {
2108 if (!(pfds[i].revents & POLLIN))
2109 continue;
2110
2111 client = find_client_by_fd(pfds[i].fd);
2112 if (!client) {
2113 log_err("fio: unknown client fd %ld\n", (long) pfds[i].fd);
2114 continue;
2115 }
2116 if (!fio_handle_client(client)) {
2117 log_info("client: host=%s disconnected\n",
2118 client->hostname);
2119 remove_client(client);
2120 retval = 1;
2121 } else if (client->error)
2122 retval = 1;
2123 fio_put_client(client);
2124 }
2125 }
2126
2127 fio_client_json_fini();
2128
2129 free(pfds);
2130 return retval || error_clients;
2131}
2132
2133static void client_display_thread_status(struct jobs_eta *je)
2134{
2135 if (!(output_format & FIO_OUTPUT_JSON))
2136 display_thread_status(je);
2137}