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