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