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