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