Merge branch 'master' into gfio
[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 fio_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 fio_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
703 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
704 convert_io_stat(&dst->clat_stat[i], &src->clat_stat[i]);
705 convert_io_stat(&dst->slat_stat[i], &src->slat_stat[i]);
706 convert_io_stat(&dst->lat_stat[i], &src->lat_stat[i]);
707 convert_io_stat(&dst->bw_stat[i], &src->bw_stat[i]);
708 }
709
710 dst->usr_time = le64_to_cpu(src->usr_time);
711 dst->sys_time = le64_to_cpu(src->sys_time);
712 dst->ctx = le64_to_cpu(src->ctx);
713 dst->minf = le64_to_cpu(src->minf);
714 dst->majf = le64_to_cpu(src->majf);
715 dst->clat_percentiles = le64_to_cpu(src->clat_percentiles);
716
717 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
718 fio_fp64_t *fps = &src->percentile_list[i];
719 fio_fp64_t *fpd = &dst->percentile_list[i];
720
721 fpd->u.f = fio_uint64_to_double(le64_to_cpu(fps->u.i));
722 }
723
724 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
725 dst->io_u_map[i] = le32_to_cpu(src->io_u_map[i]);
726 dst->io_u_submit[i] = le32_to_cpu(src->io_u_submit[i]);
727 dst->io_u_complete[i] = le32_to_cpu(src->io_u_complete[i]);
728 }
729
730 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
731 dst->io_u_lat_u[i] = le32_to_cpu(src->io_u_lat_u[i]);
732 dst->io_u_lat_m[i] = le32_to_cpu(src->io_u_lat_m[i]);
733 }
734
735 for (i = 0; i < DDIR_RWDIR_CNT; i++)
736 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
737 dst->io_u_plat[i][j] = le32_to_cpu(src->io_u_plat[i][j]);
738
739 for (i = 0; i < 3; i++) {
740 dst->total_io_u[i] = le64_to_cpu(src->total_io_u[i]);
741 dst->short_io_u[i] = le64_to_cpu(src->short_io_u[i]);
742 }
743
744 dst->total_submit = le64_to_cpu(src->total_submit);
745 dst->total_complete = le64_to_cpu(src->total_complete);
746
747 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
748 dst->io_bytes[i] = le64_to_cpu(src->io_bytes[i]);
749 dst->runtime[i] = le64_to_cpu(src->runtime[i]);
750 }
751
752 dst->total_run_time = le64_to_cpu(src->total_run_time);
753 dst->continue_on_error = le16_to_cpu(src->continue_on_error);
754 dst->total_err_count = le64_to_cpu(src->total_err_count);
755 dst->first_error = le32_to_cpu(src->first_error);
756 dst->kb_base = le32_to_cpu(src->kb_base);
757}
758
759static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
760{
761 int i;
762
763 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
764 dst->max_run[i] = le64_to_cpu(src->max_run[i]);
765 dst->min_run[i] = le64_to_cpu(src->min_run[i]);
766 dst->max_bw[i] = le64_to_cpu(src->max_bw[i]);
767 dst->min_bw[i] = le64_to_cpu(src->min_bw[i]);
768 dst->io_kb[i] = le64_to_cpu(src->io_kb[i]);
769 dst->agg[i] = le64_to_cpu(src->agg[i]);
770 }
771
772 dst->kb_base = le32_to_cpu(src->kb_base);
773 dst->groupid = le32_to_cpu(src->groupid);
774}
775
776static void handle_ts(struct fio_client *client, struct fio_net_cmd *cmd)
777{
778 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
779
780 show_thread_status(&p->ts, &p->rs);
781 client->did_stat = 1;
782
783 if (!do_output_all_clients)
784 return;
785
786 sum_thread_stats(&client_ts, &p->ts, sum_stat_nr);
787 sum_group_stats(&client_gs, &p->rs);
788
789 client_ts.members++;
790 client_ts.thread_number = p->ts.thread_number;
791 client_ts.groupid = p->ts.groupid;
792
793 if (++sum_stat_nr == sum_stat_clients) {
794 strcpy(client_ts.name, "All clients");
795 show_thread_status(&client_ts, &client_gs);
796 }
797}
798
799static void handle_gs(struct fio_client *client, struct fio_net_cmd *cmd)
800{
801 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
802
803 show_group_stats(gs);
804}
805
806static void handle_text(struct fio_client *client, struct fio_net_cmd *cmd)
807{
808 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
809 const char *buf = (const char *) pdu->buf;
810 const char *name;
811 int fio_unused ret;
812
813 name = client->name ? client->name : client->hostname;
814
815 if (!client->skip_newline)
816 fprintf(f_out, "<%s> ", name);
817 ret = fwrite(buf, pdu->buf_len, 1, f_out);
818 fflush(f_out);
819 client->skip_newline = strchr(buf, '\n') == NULL;
820}
821
822static void convert_agg(struct disk_util_agg *agg)
823{
824 int i;
825
826 for (i = 0; i < 2; i++) {
827 agg->ios[i] = le32_to_cpu(agg->ios[i]);
828 agg->merges[i] = le32_to_cpu(agg->merges[i]);
829 agg->sectors[i] = le64_to_cpu(agg->sectors[i]);
830 agg->ticks[i] = le32_to_cpu(agg->ticks[i]);
831 }
832
833 agg->io_ticks = le32_to_cpu(agg->io_ticks);
834 agg->time_in_queue = le32_to_cpu(agg->time_in_queue);
835 agg->slavecount = le32_to_cpu(agg->slavecount);
836 agg->max_util.u.f = fio_uint64_to_double(__le64_to_cpu(agg->max_util.u.i));
837}
838
839static void convert_dus(struct disk_util_stat *dus)
840{
841 int i;
842
843 for (i = 0; i < 2; i++) {
844 dus->ios[i] = le32_to_cpu(dus->ios[i]);
845 dus->merges[i] = le32_to_cpu(dus->merges[i]);
846 dus->sectors[i] = le64_to_cpu(dus->sectors[i]);
847 dus->ticks[i] = le32_to_cpu(dus->ticks[i]);
848 }
849
850 dus->io_ticks = le32_to_cpu(dus->io_ticks);
851 dus->time_in_queue = le32_to_cpu(dus->time_in_queue);
852 dus->msec = le64_to_cpu(dus->msec);
853}
854
855static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd)
856{
857 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
858
859 if (!client->disk_stats_shown) {
860 client->disk_stats_shown = 1;
861 log_info("\nDisk stats (read/write):\n");
862 }
863
864 print_disk_util(&du->dus, &du->agg, output_format == FIO_OUTPUT_TERSE);
865}
866
867static void convert_jobs_eta(struct jobs_eta *je)
868{
869 int i;
870
871 je->nr_running = le32_to_cpu(je->nr_running);
872 je->nr_ramp = le32_to_cpu(je->nr_ramp);
873 je->nr_pending = le32_to_cpu(je->nr_pending);
874 je->files_open = le32_to_cpu(je->files_open);
875
876 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
877 je->m_rate[i] = le32_to_cpu(je->m_rate[i]);
878 je->t_rate[i] = le32_to_cpu(je->t_rate[i]);
879 je->m_iops[i] = le32_to_cpu(je->m_iops[i]);
880 je->t_iops[i] = le32_to_cpu(je->t_iops[i]);
881 je->rate[i] = le32_to_cpu(je->rate[i]);
882 je->iops[i] = le32_to_cpu(je->iops[i]);
883 }
884
885 je->elapsed_sec = le64_to_cpu(je->elapsed_sec);
886 je->eta_sec = le64_to_cpu(je->eta_sec);
887 je->nr_threads = le32_to_cpu(je->nr_threads);
888 je->is_pow2 = le32_to_cpu(je->is_pow2);
889}
890
891void fio_client_sum_jobs_eta(struct jobs_eta *dst, struct jobs_eta *je)
892{
893 int i;
894
895 dst->nr_running += je->nr_running;
896 dst->nr_ramp += je->nr_ramp;
897 dst->nr_pending += je->nr_pending;
898 dst->files_open += je->files_open;
899
900 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
901 dst->m_rate[i] += je->m_rate[i];
902 dst->t_rate[i] += je->t_rate[i];
903 dst->m_iops[i] += je->m_iops[i];
904 dst->t_iops[i] += je->t_iops[i];
905 dst->rate[i] += je->rate[i];
906 dst->iops[i] += je->iops[i];
907 }
908
909 dst->elapsed_sec += je->elapsed_sec;
910
911 if (je->eta_sec > dst->eta_sec)
912 dst->eta_sec = je->eta_sec;
913
914 dst->nr_threads += je->nr_threads;
915 /* we need to handle je->run_str too ... */
916}
917
918void fio_client_dec_jobs_eta(struct client_eta *eta, client_eta_op eta_fn)
919{
920 if (!--eta->pending) {
921 eta_fn(&eta->eta);
922 free(eta);
923 }
924}
925
926static void remove_reply_cmd(struct fio_client *client, struct fio_net_cmd *cmd)
927{
928 struct fio_net_cmd_reply *reply = NULL;
929 struct flist_head *entry;
930
931 flist_for_each(entry, &client->cmd_list) {
932 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
933
934 if (cmd->tag == (uintptr_t) reply)
935 break;
936
937 reply = NULL;
938 }
939
940 if (!reply) {
941 log_err("fio: client: unable to find matching tag (%lx)\n", cmd->tag);
942 return;
943 }
944
945 flist_del(&reply->list);
946 cmd->tag = reply->saved_tag;
947 free(reply);
948}
949
950int fio_client_wait_for_reply(struct fio_client *client, uint64_t tag)
951{
952 do {
953 struct fio_net_cmd_reply *reply = NULL;
954 struct flist_head *entry;
955
956 flist_for_each(entry, &client->cmd_list) {
957 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
958
959 if (tag == (uintptr_t) reply)
960 break;
961
962 reply = NULL;
963 }
964
965 if (!reply)
966 break;
967
968 usleep(1000);
969 } while (1);
970
971 return 0;
972}
973
974static void handle_eta(struct fio_client *client, struct fio_net_cmd *cmd)
975{
976 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
977 struct client_eta *eta = (struct client_eta *) (uintptr_t) cmd->tag;
978
979 dprint(FD_NET, "client: got eta tag %p, %d\n", eta, eta->pending);
980
981 assert(client->eta_in_flight == eta);
982
983 client->eta_in_flight = NULL;
984 flist_del_init(&client->eta_list);
985
986 if (client->ops->jobs_eta)
987 client->ops->jobs_eta(client, je);
988
989 fio_client_sum_jobs_eta(&eta->eta, je);
990 fio_client_dec_jobs_eta(eta, client->ops->eta);
991}
992
993static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd)
994{
995 struct cmd_probe_pdu *probe = (struct cmd_probe_pdu *) cmd->payload;
996 const char *os, *arch;
997 char bit[16];
998
999 os = fio_get_os_string(probe->os);
1000 if (!os)
1001 os = "unknown";
1002
1003 arch = fio_get_arch_string(probe->arch);
1004 if (!arch)
1005 os = "unknown";
1006
1007 sprintf(bit, "%d-bit", probe->bpp * 8);
1008
1009 log_info("hostname=%s, be=%u, %s, os=%s, arch=%s, fio=%s\n",
1010 probe->hostname, probe->bigendian, bit, os, arch,
1011 probe->fio_version);
1012
1013 if (!client->name)
1014 client->name = strdup((char *) probe->hostname);
1015}
1016
1017static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd)
1018{
1019 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
1020
1021 client->state = Client_started;
1022 client->jobs = le32_to_cpu(pdu->jobs);
1023 client->nr_stat = le32_to_cpu(pdu->stat_outputs);
1024
1025 if (sum_stat_clients > 1)
1026 do_output_all_clients = 1;
1027
1028 sum_stat_clients += client->nr_stat;
1029}
1030
1031static void handle_stop(struct fio_client *client, struct fio_net_cmd *cmd)
1032{
1033 if (client->error)
1034 log_info("client <%s>: exited with error %d\n", client->hostname, client->error);
1035}
1036
1037static void convert_stop(struct fio_net_cmd *cmd)
1038{
1039 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
1040
1041 pdu->error = le32_to_cpu(pdu->error);
1042}
1043
1044static void convert_text(struct fio_net_cmd *cmd)
1045{
1046 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
1047
1048 pdu->level = le32_to_cpu(pdu->level);
1049 pdu->buf_len = le32_to_cpu(pdu->buf_len);
1050 pdu->log_sec = le64_to_cpu(pdu->log_sec);
1051 pdu->log_usec = le64_to_cpu(pdu->log_usec);
1052}
1053
1054/*
1055 * This has been compressed on the server side, since it can be big.
1056 * Uncompress here.
1057 */
1058static struct cmd_iolog_pdu *convert_iolog(struct fio_net_cmd *cmd)
1059{
1060 struct cmd_iolog_pdu *pdu = (struct cmd_iolog_pdu *) cmd->payload;
1061 struct cmd_iolog_pdu *ret;
1062 uint32_t nr_samples;
1063 unsigned long total;
1064 z_stream stream;
1065 void *p;
1066 int i;
1067
1068 stream.zalloc = Z_NULL;
1069 stream.zfree = Z_NULL;
1070 stream.opaque = Z_NULL;
1071 stream.avail_in = 0;
1072 stream.next_in = Z_NULL;
1073
1074 if (inflateInit(&stream) != Z_OK)
1075 return NULL;
1076
1077 /*
1078 * Get header first, it's not compressed
1079 */
1080 nr_samples = le32_to_cpu(pdu->nr_samples);
1081
1082 total = nr_samples * sizeof(struct io_sample);
1083 ret = malloc(total + sizeof(*pdu));
1084 ret->thread_number = le32_to_cpu(pdu->thread_number);
1085 ret->nr_samples = nr_samples;
1086 ret->log_type = le32_to_cpu(pdu->log_type);
1087 strcpy((char *) ret->name, (char *) pdu->name);
1088
1089 p = (void *) ret + sizeof(*pdu);
1090
1091 stream.avail_in = cmd->pdu_len - sizeof(*pdu);
1092 stream.next_in = (void *) pdu + sizeof(*pdu);
1093 while (stream.avail_in) {
1094 unsigned int this_chunk = 65536;
1095 unsigned int this_len;
1096 int err;
1097
1098 if (this_chunk > total)
1099 this_chunk = total;
1100
1101 stream.avail_out = this_chunk;
1102 stream.next_out = p;
1103 err = inflate(&stream, Z_NO_FLUSH);
1104 /* may be Z_OK, or Z_STREAM_END */
1105 if (err < 0) {
1106 log_err("fio: inflate error %d\n", err);
1107 free(ret);
1108 ret = NULL;
1109 goto out;
1110 }
1111
1112 this_len = this_chunk - stream.avail_out;
1113 p += this_len;
1114 total -= this_len;
1115 }
1116
1117 for (i = 0; i < ret->nr_samples; i++) {
1118 struct io_sample *s = &ret->samples[i];
1119
1120 s->time = le64_to_cpu(s->time);
1121 s->val = le64_to_cpu(s->val);
1122 s->ddir = le32_to_cpu(s->ddir);
1123 s->bs = le32_to_cpu(s->bs);
1124 }
1125
1126out:
1127 inflateEnd(&stream);
1128 return ret;
1129}
1130
1131int fio_handle_client(struct fio_client *client)
1132{
1133 struct client_ops *ops = client->ops;
1134 struct fio_net_cmd *cmd;
1135
1136 dprint(FD_NET, "client: handle %s\n", client->hostname);
1137
1138 cmd = fio_net_recv_cmd(client->fd);
1139 if (!cmd)
1140 return 0;
1141
1142 dprint(FD_NET, "client: got cmd op %s from %s (pdu=%u)\n",
1143 fio_server_op(cmd->opcode), client->hostname, cmd->pdu_len);
1144
1145 switch (cmd->opcode) {
1146 case FIO_NET_CMD_QUIT:
1147 if (ops->quit)
1148 ops->quit(client, cmd);
1149 remove_client(client);
1150 free(cmd);
1151 break;
1152 case FIO_NET_CMD_TEXT:
1153 convert_text(cmd);
1154 ops->text(client, cmd);
1155 free(cmd);
1156 break;
1157 case FIO_NET_CMD_DU: {
1158 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
1159
1160 convert_dus(&du->dus);
1161 convert_agg(&du->agg);
1162
1163 ops->disk_util(client, cmd);
1164 free(cmd);
1165 break;
1166 }
1167 case FIO_NET_CMD_TS: {
1168 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
1169
1170 convert_ts(&p->ts, &p->ts);
1171 convert_gs(&p->rs, &p->rs);
1172
1173 ops->thread_status(client, cmd);
1174 free(cmd);
1175 break;
1176 }
1177 case FIO_NET_CMD_GS: {
1178 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
1179
1180 convert_gs(gs, gs);
1181
1182 ops->group_stats(client, cmd);
1183 free(cmd);
1184 break;
1185 }
1186 case FIO_NET_CMD_ETA: {
1187 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
1188
1189 remove_reply_cmd(client, cmd);
1190 convert_jobs_eta(je);
1191 handle_eta(client, cmd);
1192 free(cmd);
1193 break;
1194 }
1195 case FIO_NET_CMD_PROBE:
1196 remove_reply_cmd(client, cmd);
1197 ops->probe(client, cmd);
1198 free(cmd);
1199 break;
1200 case FIO_NET_CMD_SERVER_START:
1201 client->state = Client_running;
1202 if (ops->job_start)
1203 ops->job_start(client, cmd);
1204 free(cmd);
1205 break;
1206 case FIO_NET_CMD_START: {
1207 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
1208
1209 pdu->jobs = le32_to_cpu(pdu->jobs);
1210 ops->start(client, cmd);
1211 free(cmd);
1212 break;
1213 }
1214 case FIO_NET_CMD_STOP: {
1215 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
1216
1217 convert_stop(cmd);
1218 client->state = Client_stopped;
1219 client->error = le32_to_cpu(pdu->error);
1220 client->signal = le32_to_cpu(pdu->signal);
1221 ops->stop(client, cmd);
1222 free(cmd);
1223 break;
1224 }
1225 case FIO_NET_CMD_ADD_JOB: {
1226 struct cmd_add_job_pdu *pdu = (struct cmd_add_job_pdu *) cmd->payload;
1227
1228 client->thread_number = le32_to_cpu(pdu->thread_number);
1229 client->groupid = le32_to_cpu(pdu->groupid);
1230
1231 if (ops->add_job)
1232 ops->add_job(client, cmd);
1233 free(cmd);
1234 break;
1235 }
1236 case FIO_NET_CMD_IOLOG:
1237 if (ops->iolog) {
1238 struct cmd_iolog_pdu *pdu;
1239
1240 pdu = convert_iolog(cmd);
1241 ops->iolog(client, pdu);
1242 }
1243 free(cmd);
1244 break;
1245 case FIO_NET_CMD_UPDATE_JOB:
1246 ops->update_job(client, cmd);
1247 remove_reply_cmd(client, cmd);
1248 free(cmd);
1249 break;
1250 default:
1251 log_err("fio: unknown client op: %s\n", fio_server_op(cmd->opcode));
1252 free(cmd);
1253 break;
1254 }
1255
1256 return 1;
1257}
1258
1259static void request_client_etas(struct client_ops *ops)
1260{
1261 struct fio_client *client;
1262 struct flist_head *entry;
1263 struct client_eta *eta;
1264 int skipped = 0;
1265
1266 dprint(FD_NET, "client: request eta (%d)\n", nr_clients);
1267
1268 eta = malloc(sizeof(*eta));
1269 memset(&eta->eta, 0, sizeof(eta->eta));
1270 eta->pending = nr_clients;
1271
1272 flist_for_each(entry, &client_list) {
1273 client = flist_entry(entry, struct fio_client, list);
1274
1275 if (!flist_empty(&client->eta_list)) {
1276 skipped++;
1277 continue;
1278 }
1279 if (client->state != Client_running)
1280 continue;
1281
1282 assert(!client->eta_in_flight);
1283 flist_add_tail(&client->eta_list, &eta_list);
1284 client->eta_in_flight = eta;
1285 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_SEND_ETA,
1286 (uintptr_t) eta, &client->cmd_list);
1287 }
1288
1289 while (skipped--)
1290 fio_client_dec_jobs_eta(eta, ops->eta);
1291
1292 dprint(FD_NET, "client: requested eta tag %p\n", eta);
1293}
1294
1295static int client_check_cmd_timeout(struct fio_client *client,
1296 struct timeval *now)
1297{
1298 struct fio_net_cmd_reply *reply;
1299 struct flist_head *entry, *tmp;
1300 int ret = 0;
1301
1302 flist_for_each_safe(entry, tmp, &client->cmd_list) {
1303 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
1304
1305 if (mtime_since(&reply->tv, now) < FIO_NET_CLIENT_TIMEOUT)
1306 continue;
1307
1308 log_err("fio: client %s, timeout on cmd %s\n", client->hostname,
1309 fio_server_op(reply->opcode));
1310 flist_del(&reply->list);
1311 free(reply);
1312 ret = 1;
1313 }
1314
1315 return flist_empty(&client->cmd_list) && ret;
1316}
1317
1318static int fio_check_clients_timed_out(void)
1319{
1320 struct fio_client *client;
1321 struct flist_head *entry, *tmp;
1322 struct timeval tv;
1323 int ret = 0;
1324
1325 gettimeofday(&tv, NULL);
1326
1327 flist_for_each_safe(entry, tmp, &client_list) {
1328 client = flist_entry(entry, struct fio_client, list);
1329
1330 if (flist_empty(&client->cmd_list))
1331 continue;
1332
1333 if (!client_check_cmd_timeout(client, &tv))
1334 continue;
1335
1336 if (client->ops->timed_out)
1337 client->ops->timed_out(client);
1338 else
1339 log_err("fio: client %s timed out\n", client->hostname);
1340
1341 remove_client(client);
1342 ret = 1;
1343 }
1344
1345 return ret;
1346}
1347
1348int fio_handle_clients(struct client_ops *ops)
1349{
1350 struct pollfd *pfds;
1351 int i, ret = 0, retval = 0;
1352
1353 gettimeofday(&eta_tv, NULL);
1354
1355 pfds = malloc(nr_clients * sizeof(struct pollfd));
1356
1357 init_thread_stat(&client_ts);
1358 init_group_run_stat(&client_gs);
1359
1360 while (!exit_backend && nr_clients) {
1361 struct flist_head *entry, *tmp;
1362 struct fio_client *client;
1363
1364 i = 0;
1365 flist_for_each_safe(entry, tmp, &client_list) {
1366 client = flist_entry(entry, struct fio_client, list);
1367
1368 if (!client->sent_job && !client->ops->stay_connected &&
1369 flist_empty(&client->cmd_list)) {
1370 remove_client(client);
1371 continue;
1372 }
1373
1374 pfds[i].fd = client->fd;
1375 pfds[i].events = POLLIN;
1376 i++;
1377 }
1378
1379 if (!nr_clients)
1380 break;
1381
1382 assert(i == nr_clients);
1383
1384 do {
1385 struct timeval tv;
1386
1387 gettimeofday(&tv, NULL);
1388 if (mtime_since(&eta_tv, &tv) >= ops->eta_msec) {
1389 request_client_etas(ops);
1390 memcpy(&eta_tv, &tv, sizeof(tv));
1391
1392 if (fio_check_clients_timed_out())
1393 break;
1394 }
1395
1396 ret = poll(pfds, nr_clients, ops->eta_msec);
1397 if (ret < 0) {
1398 if (errno == EINTR)
1399 continue;
1400 log_err("fio: poll clients: %s\n", strerror(errno));
1401 break;
1402 } else if (!ret)
1403 continue;
1404 } while (ret <= 0);
1405
1406 for (i = 0; i < nr_clients; i++) {
1407 if (!(pfds[i].revents & POLLIN))
1408 continue;
1409
1410 client = find_client_by_fd(pfds[i].fd);
1411 if (!client) {
1412 log_err("fio: unknown client fd %d\n", pfds[i].fd);
1413 continue;
1414 }
1415 if (!fio_handle_client(client)) {
1416 log_info("client: host=%s disconnected\n",
1417 client->hostname);
1418 remove_client(client);
1419 retval = 1;
1420 } else if (client->error)
1421 retval = 1;
1422 fio_put_client(client);
1423 }
1424 }
1425
1426 free(pfds);
1427 return retval;
1428}