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