Make use of zlib optional
[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>
3989b143 17#ifdef CONFIG_ZLIB
1b42725f 18#include <zlib.h>
3989b143 19#endif
132159a5
JA
20
21#include "fio.h"
dd366728 22#include "client.h"
132159a5 23#include "server.h"
b66570dc 24#include "flist.h"
3c5f57e3 25#include "hash.h"
132159a5 26
dd366728 27static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd);
89e5fad9
JA
28static void handle_ts(struct fio_client *client, struct fio_net_cmd *cmd);
29static void handle_gs(struct fio_client *client, struct fio_net_cmd *cmd);
dd366728 30static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd);
084d1c6f 31static void handle_text(struct fio_client *client, struct fio_net_cmd *cmd);
6b79c80c 32static void handle_stop(struct fio_client *client, struct fio_net_cmd *cmd);
85dd01e7 33static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd);
dd366728
SC
34
35struct client_ops fio_client_ops = {
35c0ba7f 36 .text = handle_text,
0420ba6a
JA
37 .disk_util = handle_du,
38 .thread_status = handle_ts,
39 .group_stats = handle_gs,
6b79c80c 40 .stop = handle_stop,
85dd01e7 41 .start = handle_start,
a5276616 42 .eta = display_thread_status,
0420ba6a 43 .probe = handle_probe,
6433ee05 44 .eta_msec = FIO_CLIENT_DEF_ETA_MSEC,
46bcd498 45 .client_type = FIO_CLIENT_TYPE_CLI,
dd366728
SC
46};
47
af9c9fb3 48static struct timeval eta_tv;
48fbb46e 49
b66570dc 50static FLIST_HEAD(client_list);
82c1ed38 51static FLIST_HEAD(eta_list);
b66570dc 52
3f3a4542
JA
53static FLIST_HEAD(arg_list);
54
3650a3ca
JA
55struct thread_stat client_ts;
56struct group_run_stats client_gs;
57int sum_stat_clients;
58
37f0c1ae 59static int sum_stat_nr;
108fea77 60static int do_output_all_clients;
37f0c1ae 61
3c5f57e3
JA
62#define FIO_CLIENT_HASH_BITS 7
63#define FIO_CLIENT_HASH_SZ (1 << FIO_CLIENT_HASH_BITS)
64#define FIO_CLIENT_HASH_MASK (FIO_CLIENT_HASH_SZ - 1)
bebe6398 65static struct flist_head client_hash[FIO_CLIENT_HASH_SZ];
3c5f57e3 66
bebe6398 67static void fio_client_add_hash(struct fio_client *client)
3c5f57e3
JA
68{
69 int bucket = hash_long(client->fd, FIO_CLIENT_HASH_BITS);
70
71 bucket &= FIO_CLIENT_HASH_MASK;
bebe6398 72 flist_add(&client->hash_list, &client_hash[bucket]);
3c5f57e3
JA
73}
74
bebe6398 75static void fio_client_remove_hash(struct fio_client *client)
3c5f57e3 76{
bebe6398
JA
77 if (!flist_empty(&client->hash_list))
78 flist_del_init(&client->hash_list);
3c5f57e3
JA
79}
80
81static void fio_init fio_client_hash_init(void)
82{
83 int i;
84
bebe6398
JA
85 for (i = 0; i < FIO_CLIENT_HASH_SZ; i++)
86 INIT_FLIST_HEAD(&client_hash[i]);
3c5f57e3
JA
87}
88
b66570dc
JA
89static struct fio_client *find_client_by_fd(int fd)
90{
3c5f57e3 91 int bucket = hash_long(fd, FIO_CLIENT_HASH_BITS) & FIO_CLIENT_HASH_MASK;
b66570dc
JA
92 struct fio_client *client;
93 struct flist_head *entry;
94
bebe6398
JA
95 flist_for_each(entry, &client_hash[bucket]) {
96 client = flist_entry(entry, struct fio_client, hash_list);
b66570dc 97
e55f8f30
JA
98 if (client->fd == fd) {
99 client->refs++;
b66570dc 100 return client;
e55f8f30 101 }
b66570dc
JA
102 }
103
104 return NULL;
105}
106
3af45201 107void fio_put_client(struct fio_client *client)
b66570dc 108{
5121a9aa
JA
109 if (--client->refs)
110 return;
111
3af45201
JA
112 free(client->hostname);
113 if (client->argv)
114 free(client->argv);
115 if (client->name)
116 free(client->name);
14ea90ed
JA
117 while (client->nr_ini_file)
118 free(client->ini_file[--client->nr_ini_file]);
119 if (client->ini_file)
120 free(client->ini_file);
3af45201 121
108fea77
JA
122 if (!client->did_stat)
123 sum_stat_clients -= client->nr_stat;
124
3af45201
JA
125 free(client);
126}
127
128static void remove_client(struct fio_client *client)
129{
130 assert(client->refs);
131
39e8e016 132 dprint(FD_NET, "client: removed <%s>\n", client->hostname);
3af45201
JA
133
134 if (!flist_empty(&client->list))
135 flist_del_init(&client->list);
3c5f57e3 136
bebe6398 137 fio_client_remove_hash(client);
81179eec 138
82c1ed38
JA
139 if (!flist_empty(&client->eta_list)) {
140 flist_del_init(&client->eta_list);
a5276616 141 fio_client_dec_jobs_eta(client->eta_in_flight, client->ops->eta);
82c1ed38 142 }
af9c9fb3 143
1e0c184e
JA
144 close(client->fd);
145 client->fd = -1;
146
0cf3ece0
JA
147 if (client->ops->removed)
148 client->ops->removed(client);
149
3c5f57e3 150 nr_clients--;
3af45201 151 fio_put_client(client);
5121a9aa
JA
152}
153
343cb4a9
JA
154struct fio_client *fio_get_client(struct fio_client *client)
155{
156 client->refs++;
157 return client;
158}
159
fa2ea806
JA
160static void __fio_client_add_cmd_option(struct fio_client *client,
161 const char *opt)
81179eec 162{
39e8e016
JA
163 int index;
164
165 index = client->argc++;
81179eec 166 client->argv = realloc(client->argv, sizeof(char *) * client->argc);
39e8e016
JA
167 client->argv[index] = strdup(opt);
168 dprint(FD_NET, "client: add cmd %d: %s\n", index, opt);
81179eec
JA
169}
170
fa2ea806 171void fio_client_add_cmd_option(void *cookie, const char *opt)
81179eec 172{
bebe6398 173 struct fio_client *client = cookie;
3f3a4542 174 struct flist_head *entry;
81179eec 175
bebe6398 176 if (!client || !opt)
fa2ea806 177 return;
81179eec 178
fa2ea806 179 __fio_client_add_cmd_option(client, opt);
3f3a4542
JA
180
181 /*
182 * Duplicate arguments to shared client group
183 */
184 flist_for_each(entry, &arg_list) {
185 client = flist_entry(entry, struct fio_client, arg_list);
186
187 __fio_client_add_cmd_option(client, opt);
188 }
81179eec
JA
189}
190
a5276616
JA
191struct fio_client *fio_client_add_explicit(struct client_ops *ops,
192 const char *hostname, int type,
3ec62ec4
JA
193 int port)
194{
195 struct fio_client *client;
196
197 client = malloc(sizeof(*client));
198 memset(client, 0, sizeof(*client));
199
200 INIT_FLIST_HEAD(&client->list);
201 INIT_FLIST_HEAD(&client->hash_list);
202 INIT_FLIST_HEAD(&client->arg_list);
203 INIT_FLIST_HEAD(&client->eta_list);
204 INIT_FLIST_HEAD(&client->cmd_list);
205
206 client->hostname = strdup(hostname);
207
208 if (type == Fio_client_socket)
209 client->is_sock = 1;
210 else {
211 int ipv6;
212
213 ipv6 = type == Fio_client_ipv6;
214 if (fio_server_parse_host(hostname, &ipv6,
215 &client->addr.sin_addr,
216 &client->addr6.sin6_addr))
217 goto err;
218
219 client->port = port;
220 }
221
222 client->fd = -1;
a5276616 223 client->ops = ops;
5121a9aa 224 client->refs = 1;
46bcd498 225 client->type = ops->client_type;
3ec62ec4
JA
226
227 __fio_client_add_cmd_option(client, "fio");
228
229 flist_add(&client->list, &client_list);
230 nr_clients++;
231 dprint(FD_NET, "client: added <%s>\n", client->hostname);
232 return client;
233err:
234 free(client);
235 return NULL;
236}
237
14ea90ed
JA
238void fio_client_add_ini_file(void *cookie, const char *ini_file)
239{
240 struct fio_client *client = cookie;
241 size_t new_size;
242
243 dprint(FD_NET, "client <%s>: add ini %s\n", client->hostname, ini_file);
244
245 new_size = (client->nr_ini_file + 1) * sizeof(char *);
246 client->ini_file = realloc(client->ini_file, new_size);
247 client->ini_file[client->nr_ini_file] = strdup(ini_file);
248 client->nr_ini_file++;
249}
250
a5276616 251int fio_client_add(struct client_ops *ops, const char *hostname, void **cookie)
132159a5 252{
3f3a4542 253 struct fio_client *existing = *cookie;
b66570dc 254 struct fio_client *client;
132159a5 255
3f3a4542
JA
256 if (existing) {
257 /*
258 * We always add our "exec" name as the option, hence 1
259 * means empty.
260 */
261 if (existing->argc == 1)
262 flist_add_tail(&existing->arg_list, &arg_list);
263 else {
264 while (!flist_empty(&arg_list))
265 flist_del_init(arg_list.next);
266 }
267 }
268
b66570dc 269 client = malloc(sizeof(*client));
a37f69b7 270 memset(client, 0, sizeof(*client));
81179eec 271
3c5f57e3 272 INIT_FLIST_HEAD(&client->list);
bebe6398 273 INIT_FLIST_HEAD(&client->hash_list);
3f3a4542 274 INIT_FLIST_HEAD(&client->arg_list);
82c1ed38 275 INIT_FLIST_HEAD(&client->eta_list);
89c1707c 276 INIT_FLIST_HEAD(&client->cmd_list);
3c5f57e3 277
bebe6398
JA
278 if (fio_server_parse_string(hostname, &client->hostname,
279 &client->is_sock, &client->port,
811826be
JA
280 &client->addr.sin_addr,
281 &client->addr6.sin6_addr,
282 &client->ipv6))
bebe6398 283 return -1;
87aa8f19 284
bebe6398 285 client->fd = -1;
a5276616 286 client->ops = ops;
5121a9aa 287 client->refs = 1;
46bcd498 288 client->type = ops->client_type;
3c5f57e3 289
81179eec
JA
290 __fio_client_add_cmd_option(client, "fio");
291
a37f69b7
JA
292 flist_add(&client->list, &client_list);
293 nr_clients++;
bebe6398
JA
294 dprint(FD_NET, "client: added <%s>\n", client->hostname);
295 *cookie = client;
296 return 0;
a37f69b7
JA
297}
298
d824c3b8
JA
299static void probe_client(struct fio_client *client)
300{
3989b143
JA
301 struct cmd_client_probe_pdu pdu;
302 uint64_t tag;
303
d824c3b8
JA
304 dprint(FD_NET, "client: send probe\n");
305
3989b143
JA
306#ifdef CONFIG_ZLIB
307 pdu.flags = __le64_to_cpu(FIO_PROBE_FLAG_ZLIB);
308#else
309 pdu.flags = 0;
310#endif
311
312 fio_net_send_cmd(client->fd, FIO_NET_CMD_PROBE, &pdu, sizeof(pdu), &tag, &client->cmd_list);
d824c3b8
JA
313}
314
87aa8f19 315static int fio_client_connect_ip(struct fio_client *client)
a37f69b7 316{
811826be 317 struct sockaddr *addr;
67bf9823 318 socklen_t socklen;
811826be
JA
319 int fd, domain;
320
321 if (client->ipv6) {
322 client->addr6.sin6_family = AF_INET6;
323 client->addr6.sin6_port = htons(client->port);
324 domain = AF_INET6;
325 addr = (struct sockaddr *) &client->addr6;
326 socklen = sizeof(client->addr6);
327 } else {
328 client->addr.sin_family = AF_INET;
329 client->addr.sin_port = htons(client->port);
330 domain = AF_INET;
331 addr = (struct sockaddr *) &client->addr;
332 socklen = sizeof(client->addr);
333 }
132159a5 334
811826be 335 fd = socket(domain, SOCK_STREAM, 0);
132159a5 336 if (fd < 0) {
25095e1b
JA
337 int ret = -errno;
338
132159a5 339 log_err("fio: socket: %s\n", strerror(errno));
25095e1b 340 return ret;
132159a5
JA
341 }
342
811826be 343 if (connect(fd, addr, socklen) < 0) {
25095e1b
JA
344 int ret = -errno;
345
132159a5 346 log_err("fio: connect: %s\n", strerror(errno));
a7de0a11
JA
347 log_err("fio: failed to connect to %s:%u\n", client->hostname,
348 client->port);
b94cba47 349 close(fd);
25095e1b 350 return ret;
87aa8f19
JA
351 }
352
353 return fd;
354}
355
356static int fio_client_connect_sock(struct fio_client *client)
357{
358 struct sockaddr_un *addr = &client->addr_un;
67bf9823 359 socklen_t len;
87aa8f19
JA
360 int fd;
361
362 memset(addr, 0, sizeof(*addr));
363 addr->sun_family = AF_UNIX;
364 strcpy(addr->sun_path, client->hostname);
365
366 fd = socket(AF_UNIX, SOCK_STREAM, 0);
367 if (fd < 0) {
25095e1b
JA
368 int ret = -errno;
369
87aa8f19 370 log_err("fio: socket: %s\n", strerror(errno));
25095e1b 371 return ret;
87aa8f19
JA
372 }
373
374 len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
375 if (connect(fd, (struct sockaddr *) addr, len) < 0) {
25095e1b
JA
376 int ret = -errno;
377
87aa8f19 378 log_err("fio: connect; %s\n", strerror(errno));
b94cba47 379 close(fd);
25095e1b 380 return ret;
132159a5
JA
381 }
382
87aa8f19
JA
383 return fd;
384}
385
2f99deb0 386int fio_client_connect(struct fio_client *client)
87aa8f19
JA
387{
388 int fd;
389
390 dprint(FD_NET, "client: connect to host %s\n", client->hostname);
391
87aa8f19
JA
392 if (client->is_sock)
393 fd = fio_client_connect_sock(client);
394 else
395 fd = fio_client_connect_ip(client);
396
89c1707c
JA
397 dprint(FD_NET, "client: %s connected %d\n", client->hostname, fd);
398
87aa8f19 399 if (fd < 0)
a1a3ed51 400 return fd;
87aa8f19 401
b66570dc 402 client->fd = fd;
bebe6398 403 fio_client_add_hash(client);
81179eec 404 client->state = Client_connected;
d824c3b8
JA
405
406 probe_client(client);
132159a5
JA
407 return 0;
408}
409
0cf3ece0 410int fio_client_terminate(struct fio_client *client)
2f99deb0 411{
0cf3ece0 412 return fio_net_send_quit(client->fd);
2f99deb0
JA
413}
414
cc0df00a
JA
415void fio_clients_terminate(void)
416{
417 struct flist_head *entry;
418 struct fio_client *client;
419
60efd14e
JA
420 dprint(FD_NET, "client: terminate clients\n");
421
cc0df00a
JA
422 flist_for_each(entry, &client_list) {
423 client = flist_entry(entry, struct fio_client, list);
2f99deb0 424 fio_client_terminate(client);
cc0df00a
JA
425 }
426}
427
428static void sig_int(int sig)
429{
bebe6398 430 dprint(FD_NET, "client: got signal %d\n", sig);
cc0df00a
JA
431 fio_clients_terminate();
432}
433
b852e7cf
JA
434static void sig_show_status(int sig)
435{
436 show_running_run_stats();
437}
438
cc0df00a
JA
439static void client_signal_handler(void)
440{
441 struct sigaction act;
442
443 memset(&act, 0, sizeof(act));
444 act.sa_handler = sig_int;
445 act.sa_flags = SA_RESTART;
446 sigaction(SIGINT, &act, NULL);
447
448 memset(&act, 0, sizeof(act));
449 act.sa_handler = sig_int;
450 act.sa_flags = SA_RESTART;
451 sigaction(SIGTERM, &act, NULL);
b852e7cf 452
2f694507
BC
453/* Windows uses SIGBREAK as a quit signal from other applications */
454#ifdef WIN32
455 memset(&act, 0, sizeof(act));
456 act.sa_handler = sig_int;
457 act.sa_flags = SA_RESTART;
458 sigaction(SIGBREAK, &act, NULL);
459#endif
460
b852e7cf
JA
461 memset(&act, 0, sizeof(act));
462 act.sa_handler = sig_show_status;
463 act.sa_flags = SA_RESTART;
464 sigaction(SIGUSR1, &act, NULL);
cc0df00a
JA
465}
466
81179eec
JA
467static int send_client_cmd_line(struct fio_client *client)
468{
fa2ea806
JA
469 struct cmd_single_line_pdu *cslp;
470 struct cmd_line_pdu *clp;
471 unsigned long offset;
7f868316 472 unsigned int *lens;
fa2ea806
JA
473 void *pdu;
474 size_t mem;
81179eec
JA
475 int i, ret;
476
39e8e016 477 dprint(FD_NET, "client: send cmdline %d\n", client->argc);
60efd14e 478
7f868316
JA
479 lens = malloc(client->argc * sizeof(unsigned int));
480
fa2ea806
JA
481 /*
482 * Find out how much mem we need
483 */
7f868316
JA
484 for (i = 0, mem = 0; i < client->argc; i++) {
485 lens[i] = strlen(client->argv[i]) + 1;
486 mem += lens[i];
487 }
fa2ea806
JA
488
489 /*
490 * We need one cmd_line_pdu, and argc number of cmd_single_line_pdu
491 */
492 mem += sizeof(*clp) + (client->argc * sizeof(*cslp));
493
494 pdu = malloc(mem);
495 clp = pdu;
496 offset = sizeof(*clp);
497
498 for (i = 0; i < client->argc; i++) {
7f868316 499 uint16_t arg_len = lens[i];
fa2ea806
JA
500
501 cslp = pdu + offset;
502 strcpy((char *) cslp->text, client->argv[i]);
503 cslp->len = cpu_to_le16(arg_len);
504 offset += sizeof(*cslp) + arg_len;
505 }
81179eec 506
7f868316 507 free(lens);
fa2ea806 508 clp->lines = cpu_to_le16(client->argc);
46bcd498 509 clp->client_type = __cpu_to_le16(client->type);
40c60516 510 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOBLINE, pdu, mem, NULL, NULL);
81179eec
JA
511 free(pdu);
512 return ret;
513}
514
a37f69b7
JA
515int fio_clients_connect(void)
516{
517 struct fio_client *client;
518 struct flist_head *entry, *tmp;
519 int ret;
520
93bcfd20
BC
521#ifdef WIN32
522 WSADATA wsd;
3c3ed070 523 WSAStartup(MAKEWORD(2, 2), &wsd);
93bcfd20
BC
524#endif
525
60efd14e
JA
526 dprint(FD_NET, "client: connect all\n");
527
cc0df00a
JA
528 client_signal_handler();
529
a37f69b7
JA
530 flist_for_each_safe(entry, tmp, &client_list) {
531 client = flist_entry(entry, struct fio_client, list);
532
533 ret = fio_client_connect(client);
0b8f30a5 534 if (ret) {
a37f69b7 535 remove_client(client);
0b8f30a5
JA
536 continue;
537 }
538
81179eec
JA
539 if (client->argc > 1)
540 send_client_cmd_line(client);
a37f69b7
JA
541 }
542
543 return !nr_clients;
544}
545
b9d2f30a
JA
546int fio_start_client(struct fio_client *client)
547{
548 dprint(FD_NET, "client: start %s\n", client->hostname);
549 return fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_RUN, 0, NULL);
550}
551
552int fio_start_all_clients(void)
553{
554 struct fio_client *client;
555 struct flist_head *entry, *tmp;
556 int ret;
557
558 dprint(FD_NET, "client: start all\n");
559
560 flist_for_each_safe(entry, tmp, &client_list) {
561 client = flist_entry(entry, struct fio_client, list);
562
563 ret = fio_start_client(client);
564 if (ret) {
565 remove_client(client);
566 continue;
567 }
568 }
569
570 return flist_empty(&client_list);
571}
572
132159a5
JA
573/*
574 * Send file contents to server backend. We could use sendfile(), but to remain
575 * more portable lets just read/write the darn thing.
576 */
9988ca70 577static int __fio_client_send_ini(struct fio_client *client, const char *filename)
132159a5 578{
46bcd498
JA
579 struct cmd_job_pdu *pdu;
580 size_t p_size;
132159a5 581 struct stat sb;
46bcd498
JA
582 char *p;
583 void *buf;
132159a5
JA
584 off_t len;
585 int fd, ret;
586
46c48f1f
JA
587 dprint(FD_NET, "send ini %s to %s\n", filename, client->hostname);
588
132159a5
JA
589 fd = open(filename, O_RDONLY);
590 if (fd < 0) {
25095e1b
JA
591 int ret = -errno;
592
e951bdc4 593 log_err("fio: job file <%s> open: %s\n", filename, strerror(errno));
25095e1b 594 return ret;
132159a5
JA
595 }
596
597 if (fstat(fd, &sb) < 0) {
25095e1b
JA
598 int ret = -errno;
599
132159a5 600 log_err("fio: job file stat: %s\n", strerror(errno));
b94cba47 601 close(fd);
25095e1b 602 return ret;
132159a5
JA
603 }
604
46bcd498
JA
605 p_size = sb.st_size + sizeof(*pdu);
606 pdu = malloc(p_size);
607 buf = pdu->buf;
132159a5
JA
608
609 len = sb.st_size;
610 p = buf;
611 do {
612 ret = read(fd, p, len);
613 if (ret > 0) {
614 len -= ret;
615 if (!len)
616 break;
617 p += ret;
618 continue;
619 } else if (!ret)
620 break;
621 else if (errno == EAGAIN || errno == EINTR)
622 continue;
623 } while (1);
624
0b8f30a5
JA
625 if (len) {
626 log_err("fio: failed reading job file %s\n", filename);
b94cba47 627 close(fd);
c524ef72 628 free(buf);
0b8f30a5
JA
629 return 1;
630 }
631
46bcd498
JA
632 pdu->buf_len = __cpu_to_le32(sb.st_size);
633 pdu->client_type = cpu_to_le32(client->type);
634
c2cb6869 635 client->sent_job = 1;
40c60516 636 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, pdu, p_size, NULL, NULL);
46bcd498 637 free(pdu);
b94cba47 638 close(fd);
132159a5
JA
639 return ret;
640}
37db14fe 641
9988ca70
JA
642int fio_client_send_ini(struct fio_client *client, const char *filename)
643{
25095e1b
JA
644 int ret;
645
646 ret = __fio_client_send_ini(client, filename);
3af45201
JA
647 if (!ret)
648 client->sent_job = 1;
9988ca70 649
25095e1b 650 return ret;
9988ca70
JA
651}
652
a37f69b7
JA
653int fio_clients_send_ini(const char *filename)
654{
655 struct fio_client *client;
656 struct flist_head *entry, *tmp;
657
658 flist_for_each_safe(entry, tmp, &client_list) {
659 client = flist_entry(entry, struct fio_client, list);
660
14ea90ed
JA
661 if (client->nr_ini_file) {
662 int i;
663
664 for (i = 0; i < client->nr_ini_file; i++) {
665 const char *ini = client->ini_file[i];
666
667 if (fio_client_send_ini(client, ini)) {
668 remove_client(client);
669 break;
670 }
671 }
672 } else if (!filename || fio_client_send_ini(client, filename))
3af45201 673 remove_client(client);
a37f69b7
JA
674 }
675
676 return !nr_clients;
677}
678
40c60516
JA
679int fio_client_update_options(struct fio_client *client,
680 struct thread_options *o, uint64_t *tag)
681{
682 struct cmd_add_job_pdu pdu;
683
684 pdu.thread_number = cpu_to_le32(client->thread_number);
685 pdu.groupid = cpu_to_le32(client->groupid);
686 convert_thread_options_to_net(&pdu.top, o);
687
688 return fio_net_send_cmd(client->fd, FIO_NET_CMD_UPDATE_JOB, &pdu, sizeof(pdu), tag, &client->cmd_list);
689}
690
a64e88da
JA
691static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
692{
693 dst->max_val = le64_to_cpu(src->max_val);
694 dst->min_val = le64_to_cpu(src->min_val);
695 dst->samples = le64_to_cpu(src->samples);
802ad4a8
JA
696
697 /*
698 * Floats arrive as IEEE 754 encoded uint64_t, convert back to double
699 */
700 dst->mean.u.f = fio_uint64_to_double(le64_to_cpu(dst->mean.u.i));
701 dst->S.u.f = fio_uint64_to_double(le64_to_cpu(dst->S.u.i));
a64e88da
JA
702}
703
704static void convert_ts(struct thread_stat *dst, struct thread_stat *src)
705{
706 int i, j;
707
2f122b13
JA
708 dst->error = le32_to_cpu(src->error);
709 dst->thread_number = le32_to_cpu(src->thread_number);
710 dst->groupid = le32_to_cpu(src->groupid);
711 dst->pid = le32_to_cpu(src->pid);
712 dst->members = le32_to_cpu(src->members);
771e58be 713 dst->unified_rw_rep = le32_to_cpu(src->unified_rw_rep);
a64e88da 714
298921d6 715 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
a64e88da
JA
716 convert_io_stat(&dst->clat_stat[i], &src->clat_stat[i]);
717 convert_io_stat(&dst->slat_stat[i], &src->slat_stat[i]);
718 convert_io_stat(&dst->lat_stat[i], &src->lat_stat[i]);
719 convert_io_stat(&dst->bw_stat[i], &src->bw_stat[i]);
720 }
721
722 dst->usr_time = le64_to_cpu(src->usr_time);
723 dst->sys_time = le64_to_cpu(src->sys_time);
724 dst->ctx = le64_to_cpu(src->ctx);
725 dst->minf = le64_to_cpu(src->minf);
726 dst->majf = le64_to_cpu(src->majf);
727 dst->clat_percentiles = le64_to_cpu(src->clat_percentiles);
802ad4a8
JA
728
729 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
730 fio_fp64_t *fps = &src->percentile_list[i];
731 fio_fp64_t *fpd = &dst->percentile_list[i];
732
733 fpd->u.f = fio_uint64_to_double(le64_to_cpu(fps->u.i));
734 }
a64e88da
JA
735
736 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
737 dst->io_u_map[i] = le32_to_cpu(src->io_u_map[i]);
738 dst->io_u_submit[i] = le32_to_cpu(src->io_u_submit[i]);
739 dst->io_u_complete[i] = le32_to_cpu(src->io_u_complete[i]);
740 }
741
742 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
743 dst->io_u_lat_u[i] = le32_to_cpu(src->io_u_lat_u[i]);
744 dst->io_u_lat_m[i] = le32_to_cpu(src->io_u_lat_m[i]);
745 }
746
298921d6 747 for (i = 0; i < DDIR_RWDIR_CNT; i++)
a64e88da
JA
748 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
749 dst->io_u_plat[i][j] = le32_to_cpu(src->io_u_plat[i][j]);
750
78799deb 751 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
a64e88da 752 dst->total_io_u[i] = le64_to_cpu(src->total_io_u[i]);
93eee04a 753 dst->short_io_u[i] = le64_to_cpu(src->short_io_u[i]);
a64e88da
JA
754 }
755
756 dst->total_submit = le64_to_cpu(src->total_submit);
757 dst->total_complete = le64_to_cpu(src->total_complete);
758
298921d6 759 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
a64e88da
JA
760 dst->io_bytes[i] = le64_to_cpu(src->io_bytes[i]);
761 dst->runtime[i] = le64_to_cpu(src->runtime[i]);
762 }
763
764 dst->total_run_time = le64_to_cpu(src->total_run_time);
765 dst->continue_on_error = le16_to_cpu(src->continue_on_error);
766 dst->total_err_count = le64_to_cpu(src->total_err_count);
ddcc0b69
JA
767 dst->first_error = le32_to_cpu(src->first_error);
768 dst->kb_base = le32_to_cpu(src->kb_base);
ad705bcb 769 dst->unit_base = le32_to_cpu(src->unit_base);
a64e88da
JA
770}
771
772static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
773{
774 int i;
775
298921d6 776 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
a64e88da
JA
777 dst->max_run[i] = le64_to_cpu(src->max_run[i]);
778 dst->min_run[i] = le64_to_cpu(src->min_run[i]);
779 dst->max_bw[i] = le64_to_cpu(src->max_bw[i]);
780 dst->min_bw[i] = le64_to_cpu(src->min_bw[i]);
781 dst->io_kb[i] = le64_to_cpu(src->io_kb[i]);
782 dst->agg[i] = le64_to_cpu(src->agg[i]);
783 }
784
785 dst->kb_base = le32_to_cpu(src->kb_base);
ad705bcb 786 dst->unit_base = le32_to_cpu(src->unit_base);
a64e88da 787 dst->groupid = le32_to_cpu(src->groupid);
771e58be 788 dst->unified_rw_rep = le32_to_cpu(src->unified_rw_rep);
a64e88da
JA
789}
790
89e5fad9 791static void handle_ts(struct fio_client *client, struct fio_net_cmd *cmd)
a64e88da
JA
792{
793 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
794
a64e88da 795 show_thread_status(&p->ts, &p->rs);
108fea77 796 client->did_stat = 1;
37f0c1ae 797
108fea77 798 if (!do_output_all_clients)
37f0c1ae
JA
799 return;
800
801 sum_thread_stats(&client_ts, &p->ts, sum_stat_nr);
802 sum_group_stats(&client_gs, &p->rs);
803
804 client_ts.members++;
2f122b13 805 client_ts.thread_number = p->ts.thread_number;
37f0c1ae 806 client_ts.groupid = p->ts.groupid;
771e58be 807 client_ts.unified_rw_rep = p->ts.unified_rw_rep;
37f0c1ae
JA
808
809 if (++sum_stat_nr == sum_stat_clients) {
810 strcpy(client_ts.name, "All clients");
811 show_thread_status(&client_ts, &client_gs);
812 }
a64e88da
JA
813}
814
89e5fad9 815static void handle_gs(struct fio_client *client, struct fio_net_cmd *cmd)
a64e88da
JA
816{
817 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
818
a64e88da
JA
819 show_group_stats(gs);
820}
821
3bf236c0
JA
822static void handle_text(struct fio_client *client, struct fio_net_cmd *cmd)
823{
824 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
825 const char *buf = (const char *) pdu->buf;
826 const char *name;
827 int fio_unused ret;
828
829 name = client->name ? client->name : client->hostname;
830
831 if (!client->skip_newline)
832 fprintf(f_out, "<%s> ", name);
833 ret = fwrite(buf, pdu->buf_len, 1, f_out);
834 fflush(f_out);
835 client->skip_newline = strchr(buf, '\n') == NULL;
836}
837
d09a64a0
JA
838static void convert_agg(struct disk_util_agg *agg)
839{
840 int i;
841
842 for (i = 0; i < 2; i++) {
843 agg->ios[i] = le32_to_cpu(agg->ios[i]);
844 agg->merges[i] = le32_to_cpu(agg->merges[i]);
845 agg->sectors[i] = le64_to_cpu(agg->sectors[i]);
846 agg->ticks[i] = le32_to_cpu(agg->ticks[i]);
847 }
848
849 agg->io_ticks = le32_to_cpu(agg->io_ticks);
850 agg->time_in_queue = le32_to_cpu(agg->time_in_queue);
851 agg->slavecount = le32_to_cpu(agg->slavecount);
823ba54b 852 agg->max_util.u.f = fio_uint64_to_double(__le64_to_cpu(agg->max_util.u.i));
d09a64a0
JA
853}
854
855static void convert_dus(struct disk_util_stat *dus)
856{
857 int i;
858
859 for (i = 0; i < 2; i++) {
860 dus->ios[i] = le32_to_cpu(dus->ios[i]);
861 dus->merges[i] = le32_to_cpu(dus->merges[i]);
862 dus->sectors[i] = le64_to_cpu(dus->sectors[i]);
863 dus->ticks[i] = le32_to_cpu(dus->ticks[i]);
864 }
865
866 dus->io_ticks = le32_to_cpu(dus->io_ticks);
867 dus->time_in_queue = le32_to_cpu(dus->time_in_queue);
868 dus->msec = le64_to_cpu(dus->msec);
869}
870
871static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd)
872{
873 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
874
d09a64a0
JA
875 if (!client->disk_stats_shown) {
876 client->disk_stats_shown = 1;
877 log_info("\nDisk stats (read/write):\n");
878 }
879
f3afa57e 880 print_disk_util(&du->dus, &du->agg, output_format == FIO_OUTPUT_TERSE);
d09a64a0
JA
881}
882
3bf236c0 883static void convert_jobs_eta(struct jobs_eta *je)
cf451d1e 884{
cf451d1e
JA
885 int i;
886
887 je->nr_running = le32_to_cpu(je->nr_running);
888 je->nr_ramp = le32_to_cpu(je->nr_ramp);
889 je->nr_pending = le32_to_cpu(je->nr_pending);
890 je->files_open = le32_to_cpu(je->files_open);
cf451d1e 891
298921d6
JA
892 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
893 je->m_rate[i] = le32_to_cpu(je->m_rate[i]);
894 je->t_rate[i] = le32_to_cpu(je->t_rate[i]);
895 je->m_iops[i] = le32_to_cpu(je->m_iops[i]);
896 je->t_iops[i] = le32_to_cpu(je->t_iops[i]);
16725bb0
JA
897 je->rate[i] = le32_to_cpu(je->rate[i]);
898 je->iops[i] = le32_to_cpu(je->iops[i]);
cf451d1e
JA
899 }
900
b51eedb7 901 je->elapsed_sec = le64_to_cpu(je->elapsed_sec);
cf451d1e 902 je->eta_sec = le64_to_cpu(je->eta_sec);
8c621fb2 903 je->nr_threads = le32_to_cpu(je->nr_threads);
b7f05eb0 904 je->is_pow2 = le32_to_cpu(je->is_pow2);
ed2c0a12 905 je->unit_base = le32_to_cpu(je->unit_base);
48fbb46e
JA
906}
907
3e47bd25 908void fio_client_sum_jobs_eta(struct jobs_eta *dst, struct jobs_eta *je)
48fbb46e 909{
48fbb46e
JA
910 int i;
911
912 dst->nr_running += je->nr_running;
913 dst->nr_ramp += je->nr_ramp;
914 dst->nr_pending += je->nr_pending;
915 dst->files_open += je->files_open;
48fbb46e 916
298921d6 917 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
3e47bd25
JA
918 dst->m_rate[i] += je->m_rate[i];
919 dst->t_rate[i] += je->t_rate[i];
920 dst->m_iops[i] += je->m_iops[i];
921 dst->t_iops[i] += je->t_iops[i];
16725bb0
JA
922 dst->rate[i] += je->rate[i];
923 dst->iops[i] += je->iops[i];
48fbb46e
JA
924 }
925
926 dst->elapsed_sec += je->elapsed_sec;
927
928 if (je->eta_sec > dst->eta_sec)
929 dst->eta_sec = je->eta_sec;
8c621fb2
JA
930
931 dst->nr_threads += je->nr_threads;
932 /* we need to handle je->run_str too ... */
48fbb46e
JA
933}
934
a5276616 935void fio_client_dec_jobs_eta(struct client_eta *eta, client_eta_op eta_fn)
82c1ed38
JA
936{
937 if (!--eta->pending) {
a5276616 938 eta_fn(&eta->eta);
82c1ed38
JA
939 free(eta);
940 }
941}
942
89c1707c
JA
943static void remove_reply_cmd(struct fio_client *client, struct fio_net_cmd *cmd)
944{
40c60516 945 struct fio_net_cmd_reply *reply = NULL;
89c1707c
JA
946 struct flist_head *entry;
947
948 flist_for_each(entry, &client->cmd_list) {
40c60516 949 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
89c1707c 950
40c60516 951 if (cmd->tag == (uintptr_t) reply)
89c1707c
JA
952 break;
953
40c60516 954 reply = NULL;
89c1707c
JA
955 }
956
40c60516
JA
957 if (!reply) {
958 log_err("fio: client: unable to find matching tag (%lx)\n", cmd->tag);
89c1707c
JA
959 return;
960 }
961
40c60516
JA
962 flist_del(&reply->list);
963 cmd->tag = reply->saved_tag;
964 free(reply);
965}
966
967int fio_client_wait_for_reply(struct fio_client *client, uint64_t tag)
968{
969 do {
970 struct fio_net_cmd_reply *reply = NULL;
971 struct flist_head *entry;
972
973 flist_for_each(entry, &client->cmd_list) {
974 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
975
976 if (tag == (uintptr_t) reply)
977 break;
978
979 reply = NULL;
980 }
981
982 if (!reply)
983 break;
984
985 usleep(1000);
986 } while (1);
987
988 return 0;
89c1707c
JA
989}
990
82c1ed38 991static void handle_eta(struct fio_client *client, struct fio_net_cmd *cmd)
48fbb46e
JA
992{
993 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
df380934 994 struct client_eta *eta = (struct client_eta *) (uintptr_t) cmd->tag;
af9c9fb3
JA
995
996 dprint(FD_NET, "client: got eta tag %p, %d\n", eta, eta->pending);
cf451d1e 997
f77d2676
JA
998 assert(client->eta_in_flight == eta);
999
1000 client->eta_in_flight = NULL;
82c1ed38
JA
1001 flist_del_init(&client->eta_list);
1002
2f99deb0
JA
1003 if (client->ops->jobs_eta)
1004 client->ops->jobs_eta(client, je);
1005
3e47bd25 1006 fio_client_sum_jobs_eta(&eta->eta, je);
a5276616 1007 fio_client_dec_jobs_eta(eta, client->ops->eta);
cf451d1e
JA
1008}
1009
b5296ddb 1010static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd)
2e03b4b2 1011{
3989b143 1012 struct cmd_probe_reply_pdu *probe = (struct cmd_probe_reply_pdu *) cmd->payload;
d2333358
JA
1013 const char *os, *arch;
1014 char bit[16];
2e03b4b2 1015
cca84643
JA
1016 os = fio_get_os_string(probe->os);
1017 if (!os)
1018 os = "unknown";
1019
1020 arch = fio_get_arch_string(probe->arch);
1021 if (!arch)
1022 os = "unknown";
1023
d2333358 1024 sprintf(bit, "%d-bit", probe->bpp * 8);
3989b143 1025 probe->flags = le64_to_cpu(probe->flags);
38fdef22 1026
3989b143 1027 log_info("hostname=%s, be=%u, %s, os=%s, arch=%s, fio=%s, flags=%lx\n",
38fdef22 1028 probe->hostname, probe->bigendian, bit, os, arch,
3989b143 1029 probe->fio_version, (unsigned long) probe->flags);
b5296ddb
JA
1030
1031 if (!client->name)
1032 client->name = strdup((char *) probe->hostname);
2e03b4b2
JA
1033}
1034
11e950bd
JA
1035static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd)
1036{
1037 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
1038
1039 client->state = Client_started;
1040 client->jobs = le32_to_cpu(pdu->jobs);
108fea77
JA
1041 client->nr_stat = le32_to_cpu(pdu->stat_outputs);
1042
1043 if (sum_stat_clients > 1)
1044 do_output_all_clients = 1;
1045
1046 sum_stat_clients += client->nr_stat;
11e950bd
JA
1047}
1048
1049static void handle_stop(struct fio_client *client, struct fio_net_cmd *cmd)
1050{
498c92c2
JA
1051 if (client->error)
1052 log_info("client <%s>: exited with error %d\n", client->hostname, client->error);
11e950bd
JA
1053}
1054
6b79c80c
JA
1055static void convert_stop(struct fio_net_cmd *cmd)
1056{
1057 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
1058
1059 pdu->error = le32_to_cpu(pdu->error);
1060}
1061
084d1c6f
JA
1062static void convert_text(struct fio_net_cmd *cmd)
1063{
1064 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
1065
1066 pdu->level = le32_to_cpu(pdu->level);
1067 pdu->buf_len = le32_to_cpu(pdu->buf_len);
1068 pdu->log_sec = le64_to_cpu(pdu->log_sec);
1069 pdu->log_usec = le64_to_cpu(pdu->log_usec);
1070}
1071
3989b143
JA
1072static struct cmd_iolog_pdu *convert_iolog_gz(struct fio_net_cmd *cmd,
1073 struct cmd_iolog_pdu *pdu)
1b42725f 1074{
3989b143 1075#ifdef CONFIG_ZLIB
1b42725f 1076 struct cmd_iolog_pdu *ret;
1b42725f 1077 z_stream stream;
3989b143
JA
1078 uint32_t nr_samples;
1079 size_t total;
1b42725f
JA
1080 void *p;
1081
1082 stream.zalloc = Z_NULL;
1083 stream.zfree = Z_NULL;
1084 stream.opaque = Z_NULL;
1085 stream.avail_in = 0;
1086 stream.next_in = Z_NULL;
1087
1088 if (inflateInit(&stream) != Z_OK)
1089 return NULL;
1090
1091 /*
f5ed765a 1092 * Get header first, it's not compressed
1b42725f
JA
1093 */
1094 nr_samples = le32_to_cpu(pdu->nr_samples);
1095
f5ed765a
JA
1096 total = nr_samples * sizeof(struct io_sample);
1097 ret = malloc(total + sizeof(*pdu));
1b42725f 1098 ret->nr_samples = nr_samples;
3989b143
JA
1099
1100 memcpy(ret, pdu, sizeof(*pdu));
f5ed765a
JA
1101
1102 p = (void *) ret + sizeof(*pdu);
1b42725f 1103
f5ed765a
JA
1104 stream.avail_in = cmd->pdu_len - sizeof(*pdu);
1105 stream.next_in = (void *) pdu + sizeof(*pdu);
1b42725f
JA
1106 while (stream.avail_in) {
1107 unsigned int this_chunk = 65536;
1108 unsigned int this_len;
1109 int err;
1110
1111 if (this_chunk > total)
1112 this_chunk = total;
1113
1114 stream.avail_out = this_chunk;
1115 stream.next_out = p;
1116 err = inflate(&stream, Z_NO_FLUSH);
3c547fe0
JA
1117 /* may be Z_OK, or Z_STREAM_END */
1118 if (err < 0) {
1b42725f 1119 log_err("fio: inflate error %d\n", err);
f5ed765a
JA
1120 free(ret);
1121 ret = NULL;
3989b143 1122 goto err;
1b42725f
JA
1123 }
1124
1125 this_len = this_chunk - stream.avail_out;
1126 p += this_len;
1127 total -= this_len;
1128 }
1129
3989b143
JA
1130err:
1131 inflateEnd(&stream);
1132 return ret;
1133#else
1134 return NULL;
1135#endif
1136}
1137
1138/*
1139 * This has been compressed on the server side, since it can be big.
1140 * Uncompress here.
1141 */
1142static struct cmd_iolog_pdu *convert_iolog(struct fio_net_cmd *cmd)
1143{
1144 struct cmd_iolog_pdu *pdu = (struct cmd_iolog_pdu *) cmd->payload;
1145 struct cmd_iolog_pdu *ret;
1146 int i;
1147
1148 /*
1149 * Convert if compressed and we support it. If it's not
1150 * compressed, we need not do anything.
1151 */
1152 if (le32_to_cpu(pdu->compressed)) {
1153#ifndef CONFIG_ZLIB
1154 log_err("fio: server sent compressed data by mistake\n");
1155 return NULL;
1156#endif
1157 ret = convert_iolog_gz(cmd, pdu);
1158 if (!ret) {
1159 log_err("fio: failed decompressing log\n");
1160 return NULL;
1161 }
1162 } else
1163 ret = pdu;
1164
1165 ret->thread_number = le32_to_cpu(ret->thread_number);
1166 ret->nr_samples = le32_to_cpu(ret->nr_samples);
1167 ret->log_type = le32_to_cpu(ret->log_type);
1168 ret->compressed = le32_to_cpu(ret->compressed);
1169
f5ed765a
JA
1170 for (i = 0; i < ret->nr_samples; i++) {
1171 struct io_sample *s = &ret->samples[i];
1172
1173 s->time = le64_to_cpu(s->time);
1174 s->val = le64_to_cpu(s->val);
1175 s->ddir = le32_to_cpu(s->ddir);
1176 s->bs = le32_to_cpu(s->bs);
1177 }
1178
1b42725f
JA
1179 return ret;
1180}
1181
a5276616 1182int fio_handle_client(struct fio_client *client)
37db14fe 1183{
a5276616 1184 struct client_ops *ops = client->ops;
37db14fe
JA
1185 struct fio_net_cmd *cmd;
1186
60efd14e
JA
1187 dprint(FD_NET, "client: handle %s\n", client->hostname);
1188
e951bdc4
JA
1189 cmd = fio_net_recv_cmd(client->fd);
1190 if (!cmd)
1191 return 0;
c2c94585 1192
b9d2f30a
JA
1193 dprint(FD_NET, "client: got cmd op %s from %s (pdu=%u)\n",
1194 fio_server_op(cmd->opcode), client->hostname, cmd->pdu_len);
46c48f1f 1195
e951bdc4
JA
1196 switch (cmd->opcode) {
1197 case FIO_NET_CMD_QUIT:
3ec62ec4 1198 if (ops->quit)
35c0ba7f 1199 ops->quit(client, cmd);
e951bdc4
JA
1200 remove_client(client);
1201 free(cmd);
1202 break;
084d1c6f
JA
1203 case FIO_NET_CMD_TEXT:
1204 convert_text(cmd);
35c0ba7f 1205 ops->text(client, cmd);
e951bdc4
JA
1206 free(cmd);
1207 break;
3bf236c0
JA
1208 case FIO_NET_CMD_DU: {
1209 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
1210
1211 convert_dus(&du->dus);
1212 convert_agg(&du->agg);
1213
dd366728 1214 ops->disk_util(client, cmd);
d09a64a0
JA
1215 free(cmd);
1216 break;
3bf236c0
JA
1217 }
1218 case FIO_NET_CMD_TS: {
1219 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
1220
1221 convert_ts(&p->ts, &p->ts);
1222 convert_gs(&p->rs, &p->rs);
1223
89e5fad9 1224 ops->thread_status(client, cmd);
e951bdc4
JA
1225 free(cmd);
1226 break;
3bf236c0
JA
1227 }
1228 case FIO_NET_CMD_GS: {
1229 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
1230
1231 convert_gs(gs, gs);
1232
89e5fad9 1233 ops->group_stats(client, cmd);
e951bdc4
JA
1234 free(cmd);
1235 break;
3bf236c0
JA
1236 }
1237 case FIO_NET_CMD_ETA: {
1238 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
1239
89c1707c 1240 remove_reply_cmd(client, cmd);
3bf236c0 1241 convert_jobs_eta(je);
a5276616 1242 handle_eta(client, cmd);
e951bdc4
JA
1243 free(cmd);
1244 break;
3bf236c0 1245 }
e951bdc4 1246 case FIO_NET_CMD_PROBE:
89c1707c 1247 remove_reply_cmd(client, cmd);
dd366728 1248 ops->probe(client, cmd);
e951bdc4
JA
1249 free(cmd);
1250 break;
5d7793aa 1251 case FIO_NET_CMD_SERVER_START:
01be038e 1252 client->state = Client_running;
85dd01e7
JA
1253 if (ops->job_start)
1254 ops->job_start(client, cmd);
01be038e
JA
1255 free(cmd);
1256 break;
85dd01e7
JA
1257 case FIO_NET_CMD_START: {
1258 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
1259
1260 pdu->jobs = le32_to_cpu(pdu->jobs);
1261 ops->start(client, cmd);
e951bdc4
JA
1262 free(cmd);
1263 break;
85dd01e7 1264 }
6b79c80c
JA
1265 case FIO_NET_CMD_STOP: {
1266 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
1267
1268 convert_stop(cmd);
1269 client->state = Client_stopped;
122c7725
JA
1270 client->error = le32_to_cpu(pdu->error);
1271 client->signal = le32_to_cpu(pdu->signal);
6b79c80c 1272 ops->stop(client, cmd);
e951bdc4
JA
1273 free(cmd);
1274 break;
6b79c80c 1275 }
40c60516
JA
1276 case FIO_NET_CMD_ADD_JOB: {
1277 struct cmd_add_job_pdu *pdu = (struct cmd_add_job_pdu *) cmd->payload;
1278
1279 client->thread_number = le32_to_cpu(pdu->thread_number);
1280 client->groupid = le32_to_cpu(pdu->groupid);
1281
807f9971
JA
1282 if (ops->add_job)
1283 ops->add_job(client, cmd);
1284 free(cmd);
1285 break;
40c60516 1286 }
1b42725f
JA
1287 case FIO_NET_CMD_IOLOG:
1288 if (ops->iolog) {
1289 struct cmd_iolog_pdu *pdu;
1290
1291 pdu = convert_iolog(cmd);
1292 ops->iolog(client, pdu);
1293 }
1294 free(cmd);
1295 break;
40c60516 1296 case FIO_NET_CMD_UPDATE_JOB:
40c60516 1297 ops->update_job(client, cmd);
649cee91 1298 remove_reply_cmd(client, cmd);
40c60516
JA
1299 free(cmd);
1300 break;
e951bdc4 1301 default:
89c1707c 1302 log_err("fio: unknown client op: %s\n", fio_server_op(cmd->opcode));
e951bdc4
JA
1303 free(cmd);
1304 break;
37db14fe
JA
1305 }
1306
e951bdc4 1307 return 1;
37db14fe 1308}
b66570dc 1309
a5276616 1310static void request_client_etas(struct client_ops *ops)
af9c9fb3
JA
1311{
1312 struct fio_client *client;
1313 struct flist_head *entry;
1314 struct client_eta *eta;
82c1ed38 1315 int skipped = 0;
af9c9fb3
JA
1316
1317 dprint(FD_NET, "client: request eta (%d)\n", nr_clients);
1318
af9c9fb3
JA
1319 eta = malloc(sizeof(*eta));
1320 memset(&eta->eta, 0, sizeof(eta->eta));
1321 eta->pending = nr_clients;
1322
1323 flist_for_each(entry, &client_list) {
1324 client = flist_entry(entry, struct fio_client, list);
1325
82c1ed38
JA
1326 if (!flist_empty(&client->eta_list)) {
1327 skipped++;
1328 continue;
1329 }
01be038e
JA
1330 if (client->state != Client_running)
1331 continue;
82c1ed38 1332
f77d2676 1333 assert(!client->eta_in_flight);
82c1ed38 1334 flist_add_tail(&client->eta_list, &eta_list);
f77d2676 1335 client->eta_in_flight = eta;
af9c9fb3 1336 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_SEND_ETA,
df380934 1337 (uintptr_t) eta, &client->cmd_list);
af9c9fb3
JA
1338 }
1339
82c1ed38 1340 while (skipped--)
a5276616 1341 fio_client_dec_jobs_eta(eta, ops->eta);
82c1ed38 1342
af9c9fb3
JA
1343 dprint(FD_NET, "client: requested eta tag %p\n", eta);
1344}
1345
89c1707c
JA
1346static int client_check_cmd_timeout(struct fio_client *client,
1347 struct timeval *now)
1348{
40c60516 1349 struct fio_net_cmd_reply *reply;
89c1707c
JA
1350 struct flist_head *entry, *tmp;
1351 int ret = 0;
1352
1353 flist_for_each_safe(entry, tmp, &client->cmd_list) {
40c60516 1354 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
89c1707c 1355
40c60516 1356 if (mtime_since(&reply->tv, now) < FIO_NET_CLIENT_TIMEOUT)
89c1707c
JA
1357 continue;
1358
1359 log_err("fio: client %s, timeout on cmd %s\n", client->hostname,
40c60516
JA
1360 fio_server_op(reply->opcode));
1361 flist_del(&reply->list);
1362 free(reply);
89c1707c
JA
1363 ret = 1;
1364 }
1365
1366 return flist_empty(&client->cmd_list) && ret;
1367}
1368
a5276616 1369static int fio_check_clients_timed_out(void)
89c1707c
JA
1370{
1371 struct fio_client *client;
1372 struct flist_head *entry, *tmp;
1373 struct timeval tv;
1374 int ret = 0;
1375
67bf9823 1376 fio_gettime(&tv, NULL);
89c1707c
JA
1377
1378 flist_for_each_safe(entry, tmp, &client_list) {
1379 client = flist_entry(entry, struct fio_client, list);
1380
1381 if (flist_empty(&client->cmd_list))
1382 continue;
1383
1384 if (!client_check_cmd_timeout(client, &tv))
1385 continue;
1386
a5276616
JA
1387 if (client->ops->timed_out)
1388 client->ops->timed_out(client);
ed727a46
JA
1389 else
1390 log_err("fio: client %s timed out\n", client->hostname);
1391
89c1707c
JA
1392 remove_client(client);
1393 ret = 1;
1394 }
1395
1396 return ret;
1397}
1398
dd366728 1399int fio_handle_clients(struct client_ops *ops)
b66570dc 1400{
b66570dc 1401 struct pollfd *pfds;
498c92c2 1402 int i, ret = 0, retval = 0;
b66570dc 1403
67bf9823 1404 fio_gettime(&eta_tv, NULL);
af9c9fb3 1405
b66570dc
JA
1406 pfds = malloc(nr_clients * sizeof(struct pollfd));
1407
37f0c1ae
JA
1408 init_thread_stat(&client_ts);
1409 init_group_run_stat(&client_gs);
1410
82a4be1b 1411 while (!exit_backend && nr_clients) {
c2cb6869
JA
1412 struct flist_head *entry, *tmp;
1413 struct fio_client *client;
1414
82a4be1b 1415 i = 0;
c2cb6869 1416 flist_for_each_safe(entry, tmp, &client_list) {
82a4be1b 1417 client = flist_entry(entry, struct fio_client, list);
b66570dc 1418
a5276616 1419 if (!client->sent_job && !client->ops->stay_connected &&
c2cb6869
JA
1420 flist_empty(&client->cmd_list)) {
1421 remove_client(client);
1422 continue;
1423 }
1424
82a4be1b
JA
1425 pfds[i].fd = client->fd;
1426 pfds[i].events = POLLIN;
1427 i++;
1428 }
1429
c2cb6869
JA
1430 if (!nr_clients)
1431 break;
1432
82a4be1b 1433 assert(i == nr_clients);
b66570dc 1434
5c2857f9 1435 do {
af9c9fb3
JA
1436 struct timeval tv;
1437
67bf9823 1438 fio_gettime(&tv, NULL);
af9c9fb3 1439 if (mtime_since(&eta_tv, &tv) >= 900) {
a5276616 1440 request_client_etas(ops);
af9c9fb3 1441 memcpy(&eta_tv, &tv, sizeof(tv));
89c1707c 1442
a5276616 1443 if (fio_check_clients_timed_out())
89c1707c 1444 break;
af9c9fb3
JA
1445 }
1446
80102c86 1447 ret = poll(pfds, nr_clients, ops->eta_msec);
5c2857f9
JA
1448 if (ret < 0) {
1449 if (errno == EINTR)
1450 continue;
1451 log_err("fio: poll clients: %s\n", strerror(errno));
1452 break;
1453 } else if (!ret)
b66570dc 1454 continue;
5c2857f9 1455 } while (ret <= 0);
b66570dc
JA
1456
1457 for (i = 0; i < nr_clients; i++) {
1458 if (!(pfds[i].revents & POLLIN))
1459 continue;
1460
1461 client = find_client_by_fd(pfds[i].fd);
1462 if (!client) {
3c5f57e3 1463 log_err("fio: unknown client fd %d\n", pfds[i].fd);
b66570dc
JA
1464 continue;
1465 }
a5276616 1466 if (!fio_handle_client(client)) {
28d3ab07
JA
1467 log_info("client: host=%s disconnected\n",
1468 client->hostname);
1469 remove_client(client);
498c92c2 1470 retval = 1;
38990764 1471 } else if (client->error)
498c92c2 1472 retval = 1;
343cb4a9 1473 fio_put_client(client);
b66570dc
JA
1474 }
1475 }
1476
1477 free(pfds);
498c92c2 1478 return retval;
b66570dc 1479}