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