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