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