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