gfio: handle clients individually
[fio.git] / client.c
CommitLineData
132159a5
JA
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>
d05c4a03 11#include <sys/socket.h>
87aa8f19 12#include <sys/un.h>
132159a5
JA
13#include <netinet/in.h>
14#include <arpa/inet.h>
15#include <netdb.h>
9e22ecb0 16#include <signal.h>
132159a5
JA
17
18#include "fio.h"
dd366728 19#include "client.h"
132159a5 20#include "server.h"
b66570dc 21#include "flist.h"
3c5f57e3 22#include "hash.h"
132159a5 23
dd366728 24static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd);
89e5fad9
JA
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);
dd366728 27static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd);
084d1c6f 28static void handle_text(struct fio_client *client, struct fio_net_cmd *cmd);
6b79c80c 29static void handle_stop(struct fio_client *client, struct fio_net_cmd *cmd);
dd366728
SC
30
31struct client_ops fio_client_ops = {
084d1c6f 32 .text_op = handle_text,
0420ba6a
JA
33 .disk_util = handle_du,
34 .thread_status = handle_ts,
35 .group_stats = handle_gs,
6b79c80c 36 .stop = handle_stop,
a5276616 37 .eta = display_thread_status,
0420ba6a 38 .probe = handle_probe,
dd366728
SC
39};
40
af9c9fb3 41static struct timeval eta_tv;
48fbb46e 42
b66570dc 43static FLIST_HEAD(client_list);
82c1ed38 44static FLIST_HEAD(eta_list);
b66570dc 45
3f3a4542
JA
46static FLIST_HEAD(arg_list);
47
3650a3ca
JA
48struct thread_stat client_ts;
49struct group_run_stats client_gs;
50int sum_stat_clients;
51
37f0c1ae
JA
52static int sum_stat_nr;
53
3c5f57e3
JA
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)
bebe6398 57static struct flist_head client_hash[FIO_CLIENT_HASH_SZ];
3c5f57e3 58
bebe6398 59static void fio_client_add_hash(struct fio_client *client)
3c5f57e3
JA
60{
61 int bucket = hash_long(client->fd, FIO_CLIENT_HASH_BITS);
62
63 bucket &= FIO_CLIENT_HASH_MASK;
bebe6398 64 flist_add(&client->hash_list, &client_hash[bucket]);
3c5f57e3
JA
65}
66
bebe6398 67static void fio_client_remove_hash(struct fio_client *client)
3c5f57e3 68{
bebe6398
JA
69 if (!flist_empty(&client->hash_list))
70 flist_del_init(&client->hash_list);
3c5f57e3
JA
71}
72
73static void fio_init fio_client_hash_init(void)
74{
75 int i;
76
bebe6398
JA
77 for (i = 0; i < FIO_CLIENT_HASH_SZ; i++)
78 INIT_FLIST_HEAD(&client_hash[i]);
3c5f57e3
JA
79}
80
b66570dc
JA
81static struct fio_client *find_client_by_fd(int fd)
82{
3c5f57e3 83 int bucket = hash_long(fd, FIO_CLIENT_HASH_BITS) & FIO_CLIENT_HASH_MASK;
b66570dc
JA
84 struct fio_client *client;
85 struct flist_head *entry;
86
bebe6398
JA
87 flist_for_each(entry, &client_hash[bucket]) {
88 client = flist_entry(entry, struct fio_client, hash_list);
b66570dc 89
5121a9aa
JA
90 if (client->fd == fd) {
91 client->refs++;
b66570dc 92 return client;
5121a9aa 93 }
b66570dc
JA
94 }
95
96 return NULL;
97}
98
b66570dc
JA
99static void remove_client(struct fio_client *client)
100{
5121a9aa
JA
101 assert(client->refs);
102
103 if (--client->refs)
104 return;
105
39e8e016 106 dprint(FD_NET, "client: removed <%s>\n", client->hostname);
b66570dc 107 flist_del(&client->list);
3c5f57e3 108
bebe6398 109 fio_client_remove_hash(client);
81179eec 110
82c1ed38
JA
111 if (!flist_empty(&client->eta_list)) {
112 flist_del_init(&client->eta_list);
a5276616 113 fio_client_dec_jobs_eta(client->eta_in_flight, client->ops->eta);
82c1ed38 114 }
af9c9fb3 115
b66570dc 116 free(client->hostname);
81179eec
JA
117 if (client->argv)
118 free(client->argv);
b5296ddb
JA
119 if (client->name)
120 free(client->name);
81179eec 121
b66570dc 122 free(client);
3c5f57e3 123 nr_clients--;
5fd0acbd 124 sum_stat_clients--;
b66570dc 125}
132159a5 126
5121a9aa
JA
127static void put_client(struct fio_client *client)
128{
129 remove_client(client);
130}
131
fa2ea806
JA
132static void __fio_client_add_cmd_option(struct fio_client *client,
133 const char *opt)
81179eec 134{
39e8e016
JA
135 int index;
136
137 index = client->argc++;
81179eec 138 client->argv = realloc(client->argv, sizeof(char *) * client->argc);
39e8e016
JA
139 client->argv[index] = strdup(opt);
140 dprint(FD_NET, "client: add cmd %d: %s\n", index, opt);
81179eec
JA
141}
142
fa2ea806 143void fio_client_add_cmd_option(void *cookie, const char *opt)
81179eec 144{
bebe6398 145 struct fio_client *client = cookie;
3f3a4542 146 struct flist_head *entry;
81179eec 147
bebe6398 148 if (!client || !opt)
fa2ea806 149 return;
81179eec 150
fa2ea806 151 __fio_client_add_cmd_option(client, opt);
3f3a4542
JA
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 }
81179eec
JA
161}
162
a5276616
JA
163struct fio_client *fio_client_add_explicit(struct client_ops *ops,
164 const char *hostname, int type,
3ec62ec4
JA
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;
a5276616 195 client->ops = ops;
5121a9aa 196 client->refs = 1;
3ec62ec4
JA
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
a5276616 209int fio_client_add(struct client_ops *ops, const char *hostname, void **cookie)
132159a5 210{
3f3a4542 211 struct fio_client *existing = *cookie;
b66570dc 212 struct fio_client *client;
132159a5 213
3f3a4542
JA
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
b66570dc 227 client = malloc(sizeof(*client));
a37f69b7 228 memset(client, 0, sizeof(*client));
81179eec 229
3c5f57e3 230 INIT_FLIST_HEAD(&client->list);
bebe6398 231 INIT_FLIST_HEAD(&client->hash_list);
3f3a4542 232 INIT_FLIST_HEAD(&client->arg_list);
82c1ed38 233 INIT_FLIST_HEAD(&client->eta_list);
89c1707c 234 INIT_FLIST_HEAD(&client->cmd_list);
3c5f57e3 235
bebe6398
JA
236 if (fio_server_parse_string(hostname, &client->hostname,
237 &client->is_sock, &client->port,
811826be
JA
238 &client->addr.sin_addr,
239 &client->addr6.sin6_addr,
240 &client->ipv6))
bebe6398 241 return -1;
87aa8f19 242
bebe6398 243 client->fd = -1;
a5276616 244 client->ops = ops;
5121a9aa 245 client->refs = 1;
3c5f57e3 246
81179eec
JA
247 __fio_client_add_cmd_option(client, "fio");
248
a37f69b7
JA
249 flist_add(&client->list, &client_list);
250 nr_clients++;
bebe6398
JA
251 dprint(FD_NET, "client: added <%s>\n", client->hostname);
252 *cookie = client;
253 return 0;
a37f69b7
JA
254}
255
87aa8f19 256static int fio_client_connect_ip(struct fio_client *client)
a37f69b7 257{
811826be
JA
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 }
132159a5 275
811826be 276 fd = socket(domain, SOCK_STREAM, 0);
132159a5
JA
277 if (fd < 0) {
278 log_err("fio: socket: %s\n", strerror(errno));
87aa8f19 279 return -1;
132159a5
JA
280 }
281
811826be 282 if (connect(fd, addr, socklen) < 0) {
132159a5 283 log_err("fio: connect: %s\n", strerror(errno));
a7de0a11
JA
284 log_err("fio: failed to connect to %s:%u\n", client->hostname,
285 client->port);
b94cba47 286 close(fd);
87aa8f19
JA
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));
b94cba47 312 close(fd);
87aa8f19 313 return -1;
132159a5
JA
314 }
315
87aa8f19
JA
316 return fd;
317}
318
2f99deb0 319int fio_client_connect(struct fio_client *client)
87aa8f19
JA
320{
321 int fd;
322
323 dprint(FD_NET, "client: connect to host %s\n", client->hostname);
324
87aa8f19
JA
325 if (client->is_sock)
326 fd = fio_client_connect_sock(client);
327 else
328 fd = fio_client_connect_ip(client);
329
89c1707c
JA
330 dprint(FD_NET, "client: %s connected %d\n", client->hostname, fd);
331
87aa8f19
JA
332 if (fd < 0)
333 return 1;
334
b66570dc 335 client->fd = fd;
bebe6398 336 fio_client_add_hash(client);
81179eec 337 client->state = Client_connected;
132159a5
JA
338 return 0;
339}
340
2f99deb0
JA
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
cc0df00a
JA
346void fio_clients_terminate(void)
347{
348 struct flist_head *entry;
349 struct fio_client *client;
350
60efd14e
JA
351 dprint(FD_NET, "client: terminate clients\n");
352
cc0df00a
JA
353 flist_for_each(entry, &client_list) {
354 client = flist_entry(entry, struct fio_client, list);
2f99deb0 355 fio_client_terminate(client);
cc0df00a
JA
356 }
357}
358
359static void sig_int(int sig)
360{
bebe6398 361 dprint(FD_NET, "client: got signal %d\n", sig);
cc0df00a
JA
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
0b8f30a5
JA
380static void probe_client(struct fio_client *client)
381{
60efd14e
JA
382 dprint(FD_NET, "client: send probe\n");
383
89c1707c 384 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_PROBE, 0, &client->cmd_list);
0b8f30a5
JA
385}
386
81179eec
JA
387static int send_client_cmd_line(struct fio_client *client)
388{
fa2ea806
JA
389 struct cmd_single_line_pdu *cslp;
390 struct cmd_line_pdu *clp;
391 unsigned long offset;
7f868316 392 unsigned int *lens;
fa2ea806
JA
393 void *pdu;
394 size_t mem;
81179eec
JA
395 int i, ret;
396
39e8e016 397 dprint(FD_NET, "client: send cmdline %d\n", client->argc);
60efd14e 398
7f868316
JA
399 lens = malloc(client->argc * sizeof(unsigned int));
400
fa2ea806
JA
401 /*
402 * Find out how much mem we need
403 */
7f868316
JA
404 for (i = 0, mem = 0; i < client->argc; i++) {
405 lens[i] = strlen(client->argv[i]) + 1;
406 mem += lens[i];
407 }
fa2ea806
JA
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++) {
7f868316 419 uint16_t arg_len = lens[i];
fa2ea806
JA
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 }
81179eec 426
7f868316 427 free(lens);
fa2ea806 428 clp->lines = cpu_to_le16(client->argc);
af9c9fb3 429 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOBLINE, pdu, mem, 0);
81179eec
JA
430 free(pdu);
431 return ret;
432}
433
a37f69b7
JA
434int fio_clients_connect(void)
435{
436 struct fio_client *client;
437 struct flist_head *entry, *tmp;
438 int ret;
439
93bcfd20
BC
440#ifdef WIN32
441 WSADATA wsd;
442 WSAStartup(MAKEWORD(2,2), &wsd);
443#endif
444
60efd14e
JA
445 dprint(FD_NET, "client: connect all\n");
446
cc0df00a
JA
447 client_signal_handler();
448
a37f69b7
JA
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);
0b8f30a5 453 if (ret) {
a37f69b7 454 remove_client(client);
0b8f30a5
JA
455 continue;
456 }
457
458 probe_client(client);
81179eec
JA
459
460 if (client->argc > 1)
461 send_client_cmd_line(client);
a37f69b7
JA
462 }
463
464 return !nr_clients;
465}
466
b9d2f30a
JA
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
132159a5
JA
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 */
9988ca70 498static int __fio_client_send_ini(struct fio_client *client, const char *filename)
132159a5
JA
499{
500 struct stat sb;
501 char *p, *buf;
502 off_t len;
503 int fd, ret;
504
46c48f1f
JA
505 dprint(FD_NET, "send ini %s to %s\n", filename, client->hostname);
506
132159a5
JA
507 fd = open(filename, O_RDONLY);
508 if (fd < 0) {
e951bdc4 509 log_err("fio: job file <%s> open: %s\n", filename, strerror(errno));
132159a5
JA
510 return 1;
511 }
512
513 if (fstat(fd, &sb) < 0) {
514 log_err("fio: job file stat: %s\n", strerror(errno));
b94cba47 515 close(fd);
132159a5
JA
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
0b8f30a5
JA
537 if (len) {
538 log_err("fio: failed reading job file %s\n", filename);
b94cba47 539 close(fd);
c524ef72 540 free(buf);
0b8f30a5
JA
541 return 1;
542 }
543
c2cb6869 544 client->sent_job = 1;
af9c9fb3 545 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, buf, sb.st_size, 0);
132159a5 546 free(buf);
b94cba47 547 close(fd);
132159a5
JA
548 return ret;
549}
37db14fe 550
9988ca70
JA
551int fio_client_send_ini(struct fio_client *client, const char *filename)
552{
553 if (__fio_client_send_ini(client, filename)) {
554 remove_client(client);
555 return 1;
556 }
557
558 client->sent_job = 1;
559 return 0;
560}
561
a37f69b7
JA
562int fio_clients_send_ini(const char *filename)
563{
564 struct fio_client *client;
565 struct flist_head *entry, *tmp;
566
567 flist_for_each_safe(entry, tmp, &client_list) {
568 client = flist_entry(entry, struct fio_client, list);
569
9988ca70 570 fio_client_send_ini(client, filename);
a37f69b7
JA
571 }
572
573 return !nr_clients;
574}
575
a64e88da
JA
576static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
577{
578 dst->max_val = le64_to_cpu(src->max_val);
579 dst->min_val = le64_to_cpu(src->min_val);
580 dst->samples = le64_to_cpu(src->samples);
802ad4a8
JA
581
582 /*
583 * Floats arrive as IEEE 754 encoded uint64_t, convert back to double
584 */
585 dst->mean.u.f = fio_uint64_to_double(le64_to_cpu(dst->mean.u.i));
586 dst->S.u.f = fio_uint64_to_double(le64_to_cpu(dst->S.u.i));
a64e88da
JA
587}
588
589static void convert_ts(struct thread_stat *dst, struct thread_stat *src)
590{
591 int i, j;
592
593 dst->error = le32_to_cpu(src->error);
594 dst->groupid = le32_to_cpu(src->groupid);
595 dst->pid = le32_to_cpu(src->pid);
596 dst->members = le32_to_cpu(src->members);
597
598 for (i = 0; i < 2; i++) {
599 convert_io_stat(&dst->clat_stat[i], &src->clat_stat[i]);
600 convert_io_stat(&dst->slat_stat[i], &src->slat_stat[i]);
601 convert_io_stat(&dst->lat_stat[i], &src->lat_stat[i]);
602 convert_io_stat(&dst->bw_stat[i], &src->bw_stat[i]);
603 }
604
605 dst->usr_time = le64_to_cpu(src->usr_time);
606 dst->sys_time = le64_to_cpu(src->sys_time);
607 dst->ctx = le64_to_cpu(src->ctx);
608 dst->minf = le64_to_cpu(src->minf);
609 dst->majf = le64_to_cpu(src->majf);
610 dst->clat_percentiles = le64_to_cpu(src->clat_percentiles);
802ad4a8
JA
611
612 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
613 fio_fp64_t *fps = &src->percentile_list[i];
614 fio_fp64_t *fpd = &dst->percentile_list[i];
615
616 fpd->u.f = fio_uint64_to_double(le64_to_cpu(fps->u.i));
617 }
a64e88da
JA
618
619 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
620 dst->io_u_map[i] = le32_to_cpu(src->io_u_map[i]);
621 dst->io_u_submit[i] = le32_to_cpu(src->io_u_submit[i]);
622 dst->io_u_complete[i] = le32_to_cpu(src->io_u_complete[i]);
623 }
624
625 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
626 dst->io_u_lat_u[i] = le32_to_cpu(src->io_u_lat_u[i]);
627 dst->io_u_lat_m[i] = le32_to_cpu(src->io_u_lat_m[i]);
628 }
629
630 for (i = 0; i < 2; i++)
631 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
632 dst->io_u_plat[i][j] = le32_to_cpu(src->io_u_plat[i][j]);
633
634 for (i = 0; i < 3; i++) {
635 dst->total_io_u[i] = le64_to_cpu(src->total_io_u[i]);
93eee04a 636 dst->short_io_u[i] = le64_to_cpu(src->short_io_u[i]);
a64e88da
JA
637 }
638
639 dst->total_submit = le64_to_cpu(src->total_submit);
640 dst->total_complete = le64_to_cpu(src->total_complete);
641
642 for (i = 0; i < 2; i++) {
643 dst->io_bytes[i] = le64_to_cpu(src->io_bytes[i]);
644 dst->runtime[i] = le64_to_cpu(src->runtime[i]);
645 }
646
647 dst->total_run_time = le64_to_cpu(src->total_run_time);
648 dst->continue_on_error = le16_to_cpu(src->continue_on_error);
649 dst->total_err_count = le64_to_cpu(src->total_err_count);
ddcc0b69
JA
650 dst->first_error = le32_to_cpu(src->first_error);
651 dst->kb_base = le32_to_cpu(src->kb_base);
a64e88da
JA
652}
653
654static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
655{
656 int i;
657
658 for (i = 0; i < 2; i++) {
659 dst->max_run[i] = le64_to_cpu(src->max_run[i]);
660 dst->min_run[i] = le64_to_cpu(src->min_run[i]);
661 dst->max_bw[i] = le64_to_cpu(src->max_bw[i]);
662 dst->min_bw[i] = le64_to_cpu(src->min_bw[i]);
663 dst->io_kb[i] = le64_to_cpu(src->io_kb[i]);
664 dst->agg[i] = le64_to_cpu(src->agg[i]);
665 }
666
667 dst->kb_base = le32_to_cpu(src->kb_base);
668 dst->groupid = le32_to_cpu(src->groupid);
669}
670
89e5fad9 671static void handle_ts(struct fio_client *client, struct fio_net_cmd *cmd)
a64e88da
JA
672{
673 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
674
a64e88da 675 show_thread_status(&p->ts, &p->rs);
37f0c1ae
JA
676
677 if (sum_stat_clients == 1)
678 return;
679
680 sum_thread_stats(&client_ts, &p->ts, sum_stat_nr);
681 sum_group_stats(&client_gs, &p->rs);
682
683 client_ts.members++;
684 client_ts.groupid = p->ts.groupid;
685
686 if (++sum_stat_nr == sum_stat_clients) {
687 strcpy(client_ts.name, "All clients");
688 show_thread_status(&client_ts, &client_gs);
689 }
a64e88da
JA
690}
691
89e5fad9 692static void handle_gs(struct fio_client *client, struct fio_net_cmd *cmd)
a64e88da
JA
693{
694 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
695
a64e88da
JA
696 show_group_stats(gs);
697}
698
3bf236c0
JA
699static void handle_text(struct fio_client *client, struct fio_net_cmd *cmd)
700{
701 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
702 const char *buf = (const char *) pdu->buf;
703 const char *name;
704 int fio_unused ret;
705
706 name = client->name ? client->name : client->hostname;
707
708 if (!client->skip_newline)
709 fprintf(f_out, "<%s> ", name);
710 ret = fwrite(buf, pdu->buf_len, 1, f_out);
711 fflush(f_out);
712 client->skip_newline = strchr(buf, '\n') == NULL;
713}
714
d09a64a0
JA
715static void convert_agg(struct disk_util_agg *agg)
716{
717 int i;
718
719 for (i = 0; i < 2; i++) {
720 agg->ios[i] = le32_to_cpu(agg->ios[i]);
721 agg->merges[i] = le32_to_cpu(agg->merges[i]);
722 agg->sectors[i] = le64_to_cpu(agg->sectors[i]);
723 agg->ticks[i] = le32_to_cpu(agg->ticks[i]);
724 }
725
726 agg->io_ticks = le32_to_cpu(agg->io_ticks);
727 agg->time_in_queue = le32_to_cpu(agg->time_in_queue);
728 agg->slavecount = le32_to_cpu(agg->slavecount);
823ba54b 729 agg->max_util.u.f = fio_uint64_to_double(__le64_to_cpu(agg->max_util.u.i));
d09a64a0
JA
730}
731
732static void convert_dus(struct disk_util_stat *dus)
733{
734 int i;
735
736 for (i = 0; i < 2; i++) {
737 dus->ios[i] = le32_to_cpu(dus->ios[i]);
738 dus->merges[i] = le32_to_cpu(dus->merges[i]);
739 dus->sectors[i] = le64_to_cpu(dus->sectors[i]);
740 dus->ticks[i] = le32_to_cpu(dus->ticks[i]);
741 }
742
743 dus->io_ticks = le32_to_cpu(dus->io_ticks);
744 dus->time_in_queue = le32_to_cpu(dus->time_in_queue);
745 dus->msec = le64_to_cpu(dus->msec);
746}
747
748static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd)
749{
750 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
751
d09a64a0
JA
752 if (!client->disk_stats_shown) {
753 client->disk_stats_shown = 1;
754 log_info("\nDisk stats (read/write):\n");
755 }
756
f2f788dd 757 print_disk_util(&du->dus, &du->agg, terse_output);
d09a64a0
JA
758}
759
3bf236c0 760static void convert_jobs_eta(struct jobs_eta *je)
cf451d1e 761{
cf451d1e
JA
762 int i;
763
764 je->nr_running = le32_to_cpu(je->nr_running);
765 je->nr_ramp = le32_to_cpu(je->nr_ramp);
766 je->nr_pending = le32_to_cpu(je->nr_pending);
767 je->files_open = le32_to_cpu(je->files_open);
cf451d1e
JA
768
769 for (i = 0; i < 2; i++) {
3e47bd25
JA
770 je->m_rate[i] = le32_to_cpu(je->m_rate[i]);
771 je->t_rate[i] = le32_to_cpu(je->t_rate[i]);
772 je->m_iops[i] = le32_to_cpu(je->m_iops[i]);
773 je->t_iops[i] = le32_to_cpu(je->t_iops[i]);
cf451d1e
JA
774 je->rate[i] = le32_to_cpu(je->rate[i]);
775 je->iops[i] = le32_to_cpu(je->iops[i]);
776 }
777
b51eedb7 778 je->elapsed_sec = le64_to_cpu(je->elapsed_sec);
cf451d1e 779 je->eta_sec = le64_to_cpu(je->eta_sec);
8c621fb2 780 je->nr_threads = le32_to_cpu(je->nr_threads);
48fbb46e
JA
781}
782
3e47bd25 783void fio_client_sum_jobs_eta(struct jobs_eta *dst, struct jobs_eta *je)
48fbb46e 784{
48fbb46e
JA
785 int i;
786
787 dst->nr_running += je->nr_running;
788 dst->nr_ramp += je->nr_ramp;
789 dst->nr_pending += je->nr_pending;
790 dst->files_open += je->files_open;
48fbb46e
JA
791
792 for (i = 0; i < 2; i++) {
3e47bd25
JA
793 dst->m_rate[i] += je->m_rate[i];
794 dst->t_rate[i] += je->t_rate[i];
795 dst->m_iops[i] += je->m_iops[i];
796 dst->t_iops[i] += je->t_iops[i];
48fbb46e
JA
797 dst->rate[i] += je->rate[i];
798 dst->iops[i] += je->iops[i];
799 }
800
801 dst->elapsed_sec += je->elapsed_sec;
802
803 if (je->eta_sec > dst->eta_sec)
804 dst->eta_sec = je->eta_sec;
8c621fb2
JA
805
806 dst->nr_threads += je->nr_threads;
807 /* we need to handle je->run_str too ... */
48fbb46e
JA
808}
809
a5276616 810void fio_client_dec_jobs_eta(struct client_eta *eta, client_eta_op eta_fn)
82c1ed38
JA
811{
812 if (!--eta->pending) {
a5276616 813 eta_fn(&eta->eta);
82c1ed38
JA
814 free(eta);
815 }
816}
817
89c1707c
JA
818static void remove_reply_cmd(struct fio_client *client, struct fio_net_cmd *cmd)
819{
820 struct fio_net_int_cmd *icmd = NULL;
821 struct flist_head *entry;
822
823 flist_for_each(entry, &client->cmd_list) {
824 icmd = flist_entry(entry, struct fio_net_int_cmd, list);
825
df380934 826 if (cmd->tag == (uintptr_t) icmd)
89c1707c
JA
827 break;
828
829 icmd = NULL;
830 }
831
832 if (!icmd) {
833 log_err("fio: client: unable to find matching tag\n");
834 return;
835 }
836
837 flist_del(&icmd->list);
838 cmd->tag = icmd->saved_tag;
839 free(icmd);
840}
841
82c1ed38 842static void handle_eta(struct fio_client *client, struct fio_net_cmd *cmd)
48fbb46e
JA
843{
844 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
df380934 845 struct client_eta *eta = (struct client_eta *) (uintptr_t) cmd->tag;
af9c9fb3
JA
846
847 dprint(FD_NET, "client: got eta tag %p, %d\n", eta, eta->pending);
cf451d1e 848
f77d2676
JA
849 assert(client->eta_in_flight == eta);
850
851 client->eta_in_flight = NULL;
82c1ed38
JA
852 flist_del_init(&client->eta_list);
853
2f99deb0
JA
854 if (client->ops->jobs_eta)
855 client->ops->jobs_eta(client, je);
856
3e47bd25 857 fio_client_sum_jobs_eta(&eta->eta, je);
a5276616 858 fio_client_dec_jobs_eta(eta, client->ops->eta);
cf451d1e
JA
859}
860
b5296ddb 861static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd)
2e03b4b2
JA
862{
863 struct cmd_probe_pdu *probe = (struct cmd_probe_pdu *) cmd->payload;
d2333358
JA
864 const char *os, *arch;
865 char bit[16];
2e03b4b2 866
cca84643
JA
867 os = fio_get_os_string(probe->os);
868 if (!os)
869 os = "unknown";
870
871 arch = fio_get_arch_string(probe->arch);
872 if (!arch)
873 os = "unknown";
874
d2333358 875 sprintf(bit, "%d-bit", probe->bpp * 8);
38fdef22
JA
876
877 log_info("hostname=%s, be=%u, %s, os=%s, arch=%s, fio=%u.%u.%u\n",
878 probe->hostname, probe->bigendian, bit, os, arch,
879 probe->fio_major, probe->fio_minor, probe->fio_patch);
b5296ddb
JA
880
881 if (!client->name)
882 client->name = strdup((char *) probe->hostname);
2e03b4b2
JA
883}
884
11e950bd
JA
885static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd)
886{
887 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
888
889 client->state = Client_started;
890 client->jobs = le32_to_cpu(pdu->jobs);
891}
892
893static void handle_stop(struct fio_client *client, struct fio_net_cmd *cmd)
894{
498c92c2
JA
895 if (client->error)
896 log_info("client <%s>: exited with error %d\n", client->hostname, client->error);
11e950bd
JA
897}
898
6b79c80c
JA
899static void convert_stop(struct fio_net_cmd *cmd)
900{
901 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
902
903 pdu->error = le32_to_cpu(pdu->error);
904}
905
084d1c6f
JA
906static void convert_text(struct fio_net_cmd *cmd)
907{
908 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
909
910 pdu->level = le32_to_cpu(pdu->level);
911 pdu->buf_len = le32_to_cpu(pdu->buf_len);
912 pdu->log_sec = le64_to_cpu(pdu->log_sec);
913 pdu->log_usec = le64_to_cpu(pdu->log_usec);
914}
915
a5276616 916int fio_handle_client(struct fio_client *client)
37db14fe 917{
a5276616 918 struct client_ops *ops = client->ops;
37db14fe
JA
919 struct fio_net_cmd *cmd;
920
60efd14e
JA
921 dprint(FD_NET, "client: handle %s\n", client->hostname);
922
e951bdc4
JA
923 cmd = fio_net_recv_cmd(client->fd);
924 if (!cmd)
925 return 0;
c2c94585 926
b9d2f30a
JA
927 dprint(FD_NET, "client: got cmd op %s from %s (pdu=%u)\n",
928 fio_server_op(cmd->opcode), client->hostname, cmd->pdu_len);
46c48f1f 929
e951bdc4
JA
930 switch (cmd->opcode) {
931 case FIO_NET_CMD_QUIT:
3ec62ec4
JA
932 if (ops->quit)
933 ops->quit(client);
e951bdc4
JA
934 remove_client(client);
935 free(cmd);
936 break;
084d1c6f
JA
937 case FIO_NET_CMD_TEXT:
938 convert_text(cmd);
939 ops->text_op(client, cmd);
e951bdc4
JA
940 free(cmd);
941 break;
3bf236c0
JA
942 case FIO_NET_CMD_DU: {
943 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
944
945 convert_dus(&du->dus);
946 convert_agg(&du->agg);
947
dd366728 948 ops->disk_util(client, cmd);
d09a64a0
JA
949 free(cmd);
950 break;
3bf236c0
JA
951 }
952 case FIO_NET_CMD_TS: {
953 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
954
955 convert_ts(&p->ts, &p->ts);
956 convert_gs(&p->rs, &p->rs);
957
89e5fad9 958 ops->thread_status(client, cmd);
e951bdc4
JA
959 free(cmd);
960 break;
3bf236c0
JA
961 }
962 case FIO_NET_CMD_GS: {
963 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
964
965 convert_gs(gs, gs);
966
89e5fad9 967 ops->group_stats(client, cmd);
e951bdc4
JA
968 free(cmd);
969 break;
3bf236c0
JA
970 }
971 case FIO_NET_CMD_ETA: {
972 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
973
89c1707c 974 remove_reply_cmd(client, cmd);
3bf236c0 975 convert_jobs_eta(je);
a5276616 976 handle_eta(client, cmd);
e951bdc4
JA
977 free(cmd);
978 break;
3bf236c0 979 }
e951bdc4 980 case FIO_NET_CMD_PROBE:
89c1707c 981 remove_reply_cmd(client, cmd);
dd366728 982 ops->probe(client, cmd);
e951bdc4
JA
983 free(cmd);
984 break;
5d7793aa 985 case FIO_NET_CMD_SERVER_START:
01be038e
JA
986 client->state = Client_running;
987 free(cmd);
988 break;
e951bdc4 989 case FIO_NET_CMD_START:
11e950bd 990 handle_start(client, cmd);
e951bdc4
JA
991 free(cmd);
992 break;
6b79c80c
JA
993 case FIO_NET_CMD_STOP: {
994 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
995
996 convert_stop(cmd);
997 client->state = Client_stopped;
998 client->error = pdu->error;
999 ops->stop(client, cmd);
e951bdc4
JA
1000 free(cmd);
1001 break;
6b79c80c 1002 }
807f9971
JA
1003 case FIO_NET_CMD_ADD_JOB:
1004 if (ops->add_job)
1005 ops->add_job(client, cmd);
1006 free(cmd);
1007 break;
e951bdc4 1008 default:
89c1707c 1009 log_err("fio: unknown client op: %s\n", fio_server_op(cmd->opcode));
e951bdc4
JA
1010 free(cmd);
1011 break;
37db14fe
JA
1012 }
1013
e951bdc4 1014 return 1;
37db14fe 1015}
b66570dc 1016
a5276616 1017static void request_client_etas(struct client_ops *ops)
af9c9fb3
JA
1018{
1019 struct fio_client *client;
1020 struct flist_head *entry;
1021 struct client_eta *eta;
82c1ed38 1022 int skipped = 0;
af9c9fb3
JA
1023
1024 dprint(FD_NET, "client: request eta (%d)\n", nr_clients);
1025
af9c9fb3
JA
1026 eta = malloc(sizeof(*eta));
1027 memset(&eta->eta, 0, sizeof(eta->eta));
1028 eta->pending = nr_clients;
1029
1030 flist_for_each(entry, &client_list) {
1031 client = flist_entry(entry, struct fio_client, list);
1032
82c1ed38
JA
1033 if (!flist_empty(&client->eta_list)) {
1034 skipped++;
1035 continue;
1036 }
01be038e
JA
1037 if (client->state != Client_running)
1038 continue;
82c1ed38 1039
f77d2676 1040 assert(!client->eta_in_flight);
82c1ed38 1041 flist_add_tail(&client->eta_list, &eta_list);
f77d2676 1042 client->eta_in_flight = eta;
af9c9fb3 1043 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_SEND_ETA,
df380934 1044 (uintptr_t) eta, &client->cmd_list);
af9c9fb3
JA
1045 }
1046
82c1ed38 1047 while (skipped--)
a5276616 1048 fio_client_dec_jobs_eta(eta, ops->eta);
82c1ed38 1049
af9c9fb3
JA
1050 dprint(FD_NET, "client: requested eta tag %p\n", eta);
1051}
1052
89c1707c
JA
1053static int client_check_cmd_timeout(struct fio_client *client,
1054 struct timeval *now)
1055{
1056 struct fio_net_int_cmd *cmd;
1057 struct flist_head *entry, *tmp;
1058 int ret = 0;
1059
1060 flist_for_each_safe(entry, tmp, &client->cmd_list) {
1061 cmd = flist_entry(entry, struct fio_net_int_cmd, list);
1062
1063 if (mtime_since(&cmd->tv, now) < FIO_NET_CLIENT_TIMEOUT)
1064 continue;
1065
1066 log_err("fio: client %s, timeout on cmd %s\n", client->hostname,
1067 fio_server_op(cmd->cmd.opcode));
1068 flist_del(&cmd->list);
1069 free(cmd);
1070 ret = 1;
1071 }
1072
1073 return flist_empty(&client->cmd_list) && ret;
1074}
1075
a5276616 1076static int fio_check_clients_timed_out(void)
89c1707c
JA
1077{
1078 struct fio_client *client;
1079 struct flist_head *entry, *tmp;
1080 struct timeval tv;
1081 int ret = 0;
1082
1083 gettimeofday(&tv, NULL);
1084
1085 flist_for_each_safe(entry, tmp, &client_list) {
1086 client = flist_entry(entry, struct fio_client, list);
1087
1088 if (flist_empty(&client->cmd_list))
1089 continue;
1090
1091 if (!client_check_cmd_timeout(client, &tv))
1092 continue;
1093
a5276616
JA
1094 if (client->ops->timed_out)
1095 client->ops->timed_out(client);
ed727a46
JA
1096 else
1097 log_err("fio: client %s timed out\n", client->hostname);
1098
89c1707c
JA
1099 remove_client(client);
1100 ret = 1;
1101 }
1102
1103 return ret;
1104}
1105
dd366728 1106int fio_handle_clients(struct client_ops *ops)
b66570dc 1107{
b66570dc 1108 struct pollfd *pfds;
498c92c2 1109 int i, ret = 0, retval = 0;
b66570dc 1110
af9c9fb3
JA
1111 gettimeofday(&eta_tv, NULL);
1112
b66570dc
JA
1113 pfds = malloc(nr_clients * sizeof(struct pollfd));
1114
37f0c1ae
JA
1115 sum_stat_clients = nr_clients;
1116 init_thread_stat(&client_ts);
1117 init_group_run_stat(&client_gs);
1118
82a4be1b 1119 while (!exit_backend && nr_clients) {
c2cb6869
JA
1120 struct flist_head *entry, *tmp;
1121 struct fio_client *client;
1122
82a4be1b 1123 i = 0;
c2cb6869 1124 flist_for_each_safe(entry, tmp, &client_list) {
82a4be1b 1125 client = flist_entry(entry, struct fio_client, list);
b66570dc 1126
a5276616 1127 if (!client->sent_job && !client->ops->stay_connected &&
c2cb6869
JA
1128 flist_empty(&client->cmd_list)) {
1129 remove_client(client);
1130 continue;
1131 }
1132
82a4be1b
JA
1133 pfds[i].fd = client->fd;
1134 pfds[i].events = POLLIN;
1135 i++;
1136 }
1137
c2cb6869
JA
1138 if (!nr_clients)
1139 break;
1140
82a4be1b 1141 assert(i == nr_clients);
b66570dc 1142
5c2857f9 1143 do {
af9c9fb3
JA
1144 struct timeval tv;
1145
1146 gettimeofday(&tv, NULL);
1147 if (mtime_since(&eta_tv, &tv) >= 900) {
a5276616 1148 request_client_etas(ops);
af9c9fb3 1149 memcpy(&eta_tv, &tv, sizeof(tv));
89c1707c 1150
a5276616 1151 if (fio_check_clients_timed_out())
89c1707c 1152 break;
af9c9fb3
JA
1153 }
1154
5c2857f9
JA
1155 ret = poll(pfds, nr_clients, 100);
1156 if (ret < 0) {
1157 if (errno == EINTR)
1158 continue;
1159 log_err("fio: poll clients: %s\n", strerror(errno));
1160 break;
1161 } else if (!ret)
b66570dc 1162 continue;
5c2857f9 1163 } while (ret <= 0);
b66570dc
JA
1164
1165 for (i = 0; i < nr_clients; i++) {
1166 if (!(pfds[i].revents & POLLIN))
1167 continue;
1168
1169 client = find_client_by_fd(pfds[i].fd);
1170 if (!client) {
3c5f57e3 1171 log_err("fio: unknown client fd %d\n", pfds[i].fd);
b66570dc
JA
1172 continue;
1173 }
a5276616 1174 if (!fio_handle_client(client)) {
28d3ab07
JA
1175 log_info("client: host=%s disconnected\n",
1176 client->hostname);
1177 remove_client(client);
498c92c2 1178 retval = 1;
38990764 1179 } else if (client->error)
498c92c2 1180 retval = 1;
5121a9aa 1181 put_client(client);
b66570dc
JA
1182 }
1183 }
1184
1185 free(pfds);
498c92c2 1186 return retval;
b66570dc 1187}