Add new net command for text logging
[fio.git] / client.c
CommitLineData
132159a5
JA
1#include <stdio.h>
2#include <stdlib.h>
3#include <unistd.h>
4#include <limits.h>
5#include <errno.h>
6#include <fcntl.h>
7#include <sys/poll.h>
8#include <sys/types.h>
9#include <sys/stat.h>
10#include <sys/wait.h>
d05c4a03 11#include <sys/socket.h>
87aa8f19 12#include <sys/un.h>
132159a5
JA
13#include <netinet/in.h>
14#include <arpa/inet.h>
15#include <netdb.h>
9e22ecb0 16#include <signal.h>
132159a5
JA
17
18#include "fio.h"
dd366728 19#include "client.h"
132159a5 20#include "server.h"
b66570dc 21#include "flist.h"
3c5f57e3 22#include "hash.h"
132159a5 23
dd366728
SC
24static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd);
25static void handle_ts(struct fio_net_cmd *cmd);
26static void handle_gs(struct fio_net_cmd *cmd);
27static void handle_eta(struct fio_client *client, struct fio_net_cmd *cmd);
28static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd);
084d1c6f 29static void handle_text(struct fio_client *client, struct fio_net_cmd *cmd);
dd366728
SC
30
31struct client_ops fio_client_ops = {
084d1c6f 32 .text_op = handle_text,
0420ba6a
JA
33 .disk_util = handle_du,
34 .thread_status = handle_ts,
35 .group_stats = handle_gs,
36 .eta = handle_eta,
37 .probe = handle_probe,
dd366728
SC
38};
39
af9c9fb3 40static struct timeval eta_tv;
48fbb46e 41
81179eec 42enum {
5c2857f9 43 Client_created = 0,
81179eec
JA
44 Client_connected = 1,
45 Client_started = 2,
01be038e
JA
46 Client_running = 3,
47 Client_stopped = 4,
48 Client_exited = 5,
b66570dc
JA
49};
50
51static FLIST_HEAD(client_list);
82c1ed38 52static FLIST_HEAD(eta_list);
b66570dc 53
3f3a4542
JA
54static FLIST_HEAD(arg_list);
55
37f0c1ae
JA
56static struct thread_stat client_ts;
57static struct group_run_stats client_gs;
58static int sum_stat_clients;
59static int sum_stat_nr;
60
3c5f57e3
JA
61#define FIO_CLIENT_HASH_BITS 7
62#define FIO_CLIENT_HASH_SZ (1 << FIO_CLIENT_HASH_BITS)
63#define FIO_CLIENT_HASH_MASK (FIO_CLIENT_HASH_SZ - 1)
bebe6398 64static struct flist_head client_hash[FIO_CLIENT_HASH_SZ];
3c5f57e3 65
bebe6398 66static void fio_client_add_hash(struct fio_client *client)
3c5f57e3
JA
67{
68 int bucket = hash_long(client->fd, FIO_CLIENT_HASH_BITS);
69
70 bucket &= FIO_CLIENT_HASH_MASK;
bebe6398 71 flist_add(&client->hash_list, &client_hash[bucket]);
3c5f57e3
JA
72}
73
bebe6398 74static void fio_client_remove_hash(struct fio_client *client)
3c5f57e3 75{
bebe6398
JA
76 if (!flist_empty(&client->hash_list))
77 flist_del_init(&client->hash_list);
3c5f57e3
JA
78}
79
80static void fio_init fio_client_hash_init(void)
81{
82 int i;
83
bebe6398
JA
84 for (i = 0; i < FIO_CLIENT_HASH_SZ; i++)
85 INIT_FLIST_HEAD(&client_hash[i]);
3c5f57e3
JA
86}
87
b66570dc
JA
88static struct fio_client *find_client_by_fd(int fd)
89{
3c5f57e3 90 int bucket = hash_long(fd, FIO_CLIENT_HASH_BITS) & FIO_CLIENT_HASH_MASK;
b66570dc
JA
91 struct fio_client *client;
92 struct flist_head *entry;
93
bebe6398
JA
94 flist_for_each(entry, &client_hash[bucket]) {
95 client = flist_entry(entry, struct fio_client, hash_list);
b66570dc
JA
96
97 if (client->fd == fd)
98 return client;
99 }
100
101 return NULL;
102}
103
b66570dc
JA
104static void remove_client(struct fio_client *client)
105{
39e8e016 106 dprint(FD_NET, "client: removed <%s>\n", client->hostname);
b66570dc 107 flist_del(&client->list);
3c5f57e3 108
bebe6398 109 fio_client_remove_hash(client);
81179eec 110
82c1ed38
JA
111 if (!flist_empty(&client->eta_list)) {
112 flist_del_init(&client->eta_list);
3e47bd25 113 fio_client_dec_jobs_eta(client->eta_in_flight, display_thread_status);
82c1ed38 114 }
af9c9fb3 115
b66570dc 116 free(client->hostname);
81179eec
JA
117 if (client->argv)
118 free(client->argv);
b5296ddb
JA
119 if (client->name)
120 free(client->name);
81179eec 121
b66570dc 122 free(client);
3c5f57e3 123 nr_clients--;
5fd0acbd 124 sum_stat_clients--;
b66570dc 125}
132159a5 126
fa2ea806
JA
127static void __fio_client_add_cmd_option(struct fio_client *client,
128 const char *opt)
81179eec 129{
39e8e016
JA
130 int index;
131
132 index = client->argc++;
81179eec 133 client->argv = realloc(client->argv, sizeof(char *) * client->argc);
39e8e016
JA
134 client->argv[index] = strdup(opt);
135 dprint(FD_NET, "client: add cmd %d: %s\n", index, opt);
81179eec
JA
136}
137
fa2ea806 138void fio_client_add_cmd_option(void *cookie, const char *opt)
81179eec 139{
bebe6398 140 struct fio_client *client = cookie;
3f3a4542 141 struct flist_head *entry;
81179eec 142
bebe6398 143 if (!client || !opt)
fa2ea806 144 return;
81179eec 145
fa2ea806 146 __fio_client_add_cmd_option(client, opt);
3f3a4542
JA
147
148 /*
149 * Duplicate arguments to shared client group
150 */
151 flist_for_each(entry, &arg_list) {
152 client = flist_entry(entry, struct fio_client, arg_list);
153
154 __fio_client_add_cmd_option(client, opt);
155 }
81179eec
JA
156}
157
3ec62ec4
JA
158struct fio_client *fio_client_add_explicit(const char *hostname, int type,
159 int port)
160{
161 struct fio_client *client;
162
163 client = malloc(sizeof(*client));
164 memset(client, 0, sizeof(*client));
165
166 INIT_FLIST_HEAD(&client->list);
167 INIT_FLIST_HEAD(&client->hash_list);
168 INIT_FLIST_HEAD(&client->arg_list);
169 INIT_FLIST_HEAD(&client->eta_list);
170 INIT_FLIST_HEAD(&client->cmd_list);
171
172 client->hostname = strdup(hostname);
173
174 if (type == Fio_client_socket)
175 client->is_sock = 1;
176 else {
177 int ipv6;
178
179 ipv6 = type == Fio_client_ipv6;
180 if (fio_server_parse_host(hostname, &ipv6,
181 &client->addr.sin_addr,
182 &client->addr6.sin6_addr))
183 goto err;
184
185 client->port = port;
186 }
187
188 client->fd = -1;
189
190 __fio_client_add_cmd_option(client, "fio");
191
192 flist_add(&client->list, &client_list);
193 nr_clients++;
194 dprint(FD_NET, "client: added <%s>\n", client->hostname);
195 return client;
196err:
197 free(client);
198 return NULL;
199}
200
bebe6398 201int fio_client_add(const char *hostname, void **cookie)
132159a5 202{
3f3a4542 203 struct fio_client *existing = *cookie;
b66570dc 204 struct fio_client *client;
132159a5 205
3f3a4542
JA
206 if (existing) {
207 /*
208 * We always add our "exec" name as the option, hence 1
209 * means empty.
210 */
211 if (existing->argc == 1)
212 flist_add_tail(&existing->arg_list, &arg_list);
213 else {
214 while (!flist_empty(&arg_list))
215 flist_del_init(arg_list.next);
216 }
217 }
218
b66570dc 219 client = malloc(sizeof(*client));
a37f69b7 220 memset(client, 0, sizeof(*client));
81179eec 221
3c5f57e3 222 INIT_FLIST_HEAD(&client->list);
bebe6398 223 INIT_FLIST_HEAD(&client->hash_list);
3f3a4542 224 INIT_FLIST_HEAD(&client->arg_list);
82c1ed38 225 INIT_FLIST_HEAD(&client->eta_list);
89c1707c 226 INIT_FLIST_HEAD(&client->cmd_list);
3c5f57e3 227
bebe6398
JA
228 if (fio_server_parse_string(hostname, &client->hostname,
229 &client->is_sock, &client->port,
811826be
JA
230 &client->addr.sin_addr,
231 &client->addr6.sin6_addr,
232 &client->ipv6))
bebe6398 233 return -1;
87aa8f19 234
bebe6398 235 client->fd = -1;
3c5f57e3 236
81179eec
JA
237 __fio_client_add_cmd_option(client, "fio");
238
a37f69b7
JA
239 flist_add(&client->list, &client_list);
240 nr_clients++;
bebe6398
JA
241 dprint(FD_NET, "client: added <%s>\n", client->hostname);
242 *cookie = client;
243 return 0;
a37f69b7
JA
244}
245
87aa8f19 246static int fio_client_connect_ip(struct fio_client *client)
a37f69b7 247{
811826be
JA
248 struct sockaddr *addr;
249 fio_socklen_t socklen;
250 int fd, domain;
251
252 if (client->ipv6) {
253 client->addr6.sin6_family = AF_INET6;
254 client->addr6.sin6_port = htons(client->port);
255 domain = AF_INET6;
256 addr = (struct sockaddr *) &client->addr6;
257 socklen = sizeof(client->addr6);
258 } else {
259 client->addr.sin_family = AF_INET;
260 client->addr.sin_port = htons(client->port);
261 domain = AF_INET;
262 addr = (struct sockaddr *) &client->addr;
263 socklen = sizeof(client->addr);
264 }
132159a5 265
811826be 266 fd = socket(domain, SOCK_STREAM, 0);
132159a5
JA
267 if (fd < 0) {
268 log_err("fio: socket: %s\n", strerror(errno));
87aa8f19 269 return -1;
132159a5
JA
270 }
271
811826be 272 if (connect(fd, addr, socklen) < 0) {
132159a5 273 log_err("fio: connect: %s\n", strerror(errno));
a7de0a11
JA
274 log_err("fio: failed to connect to %s:%u\n", client->hostname,
275 client->port);
b94cba47 276 close(fd);
87aa8f19
JA
277 return -1;
278 }
279
280 return fd;
281}
282
283static int fio_client_connect_sock(struct fio_client *client)
284{
285 struct sockaddr_un *addr = &client->addr_un;
286 fio_socklen_t len;
287 int fd;
288
289 memset(addr, 0, sizeof(*addr));
290 addr->sun_family = AF_UNIX;
291 strcpy(addr->sun_path, client->hostname);
292
293 fd = socket(AF_UNIX, SOCK_STREAM, 0);
294 if (fd < 0) {
295 log_err("fio: socket: %s\n", strerror(errno));
296 return -1;
297 }
298
299 len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
300 if (connect(fd, (struct sockaddr *) addr, len) < 0) {
301 log_err("fio: connect; %s\n", strerror(errno));
b94cba47 302 close(fd);
87aa8f19 303 return -1;
132159a5
JA
304 }
305
87aa8f19
JA
306 return fd;
307}
308
309static int fio_client_connect(struct fio_client *client)
310{
311 int fd;
312
313 dprint(FD_NET, "client: connect to host %s\n", client->hostname);
314
87aa8f19
JA
315 if (client->is_sock)
316 fd = fio_client_connect_sock(client);
317 else
318 fd = fio_client_connect_ip(client);
319
89c1707c
JA
320 dprint(FD_NET, "client: %s connected %d\n", client->hostname, fd);
321
87aa8f19
JA
322 if (fd < 0)
323 return 1;
324
b66570dc 325 client->fd = fd;
bebe6398 326 fio_client_add_hash(client);
81179eec 327 client->state = Client_connected;
132159a5
JA
328 return 0;
329}
330
cc0df00a
JA
331void fio_clients_terminate(void)
332{
333 struct flist_head *entry;
334 struct fio_client *client;
335
60efd14e
JA
336 dprint(FD_NET, "client: terminate clients\n");
337
cc0df00a
JA
338 flist_for_each(entry, &client_list) {
339 client = flist_entry(entry, struct fio_client, list);
340
89c1707c 341 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_QUIT, 0, NULL);
cc0df00a
JA
342 }
343}
344
345static void sig_int(int sig)
346{
bebe6398 347 dprint(FD_NET, "client: got signal %d\n", sig);
cc0df00a
JA
348 fio_clients_terminate();
349}
350
351static void client_signal_handler(void)
352{
353 struct sigaction act;
354
355 memset(&act, 0, sizeof(act));
356 act.sa_handler = sig_int;
357 act.sa_flags = SA_RESTART;
358 sigaction(SIGINT, &act, NULL);
359
360 memset(&act, 0, sizeof(act));
361 act.sa_handler = sig_int;
362 act.sa_flags = SA_RESTART;
363 sigaction(SIGTERM, &act, NULL);
364}
365
0b8f30a5
JA
366static void probe_client(struct fio_client *client)
367{
60efd14e
JA
368 dprint(FD_NET, "client: send probe\n");
369
89c1707c 370 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_PROBE, 0, &client->cmd_list);
0b8f30a5
JA
371}
372
81179eec
JA
373static int send_client_cmd_line(struct fio_client *client)
374{
fa2ea806
JA
375 struct cmd_single_line_pdu *cslp;
376 struct cmd_line_pdu *clp;
377 unsigned long offset;
7f868316 378 unsigned int *lens;
fa2ea806
JA
379 void *pdu;
380 size_t mem;
81179eec
JA
381 int i, ret;
382
39e8e016 383 dprint(FD_NET, "client: send cmdline %d\n", client->argc);
60efd14e 384
7f868316
JA
385 lens = malloc(client->argc * sizeof(unsigned int));
386
fa2ea806
JA
387 /*
388 * Find out how much mem we need
389 */
7f868316
JA
390 for (i = 0, mem = 0; i < client->argc; i++) {
391 lens[i] = strlen(client->argv[i]) + 1;
392 mem += lens[i];
393 }
fa2ea806
JA
394
395 /*
396 * We need one cmd_line_pdu, and argc number of cmd_single_line_pdu
397 */
398 mem += sizeof(*clp) + (client->argc * sizeof(*cslp));
399
400 pdu = malloc(mem);
401 clp = pdu;
402 offset = sizeof(*clp);
403
404 for (i = 0; i < client->argc; i++) {
7f868316 405 uint16_t arg_len = lens[i];
fa2ea806
JA
406
407 cslp = pdu + offset;
408 strcpy((char *) cslp->text, client->argv[i]);
409 cslp->len = cpu_to_le16(arg_len);
410 offset += sizeof(*cslp) + arg_len;
411 }
81179eec 412
7f868316 413 free(lens);
fa2ea806 414 clp->lines = cpu_to_le16(client->argc);
af9c9fb3 415 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOBLINE, pdu, mem, 0);
81179eec
JA
416 free(pdu);
417 return ret;
418}
419
a37f69b7
JA
420int fio_clients_connect(void)
421{
422 struct fio_client *client;
423 struct flist_head *entry, *tmp;
424 int ret;
425
93bcfd20
BC
426#ifdef WIN32
427 WSADATA wsd;
428 WSAStartup(MAKEWORD(2,2), &wsd);
429#endif
430
60efd14e
JA
431 dprint(FD_NET, "client: connect all\n");
432
cc0df00a
JA
433 client_signal_handler();
434
a37f69b7
JA
435 flist_for_each_safe(entry, tmp, &client_list) {
436 client = flist_entry(entry, struct fio_client, list);
437
438 ret = fio_client_connect(client);
0b8f30a5 439 if (ret) {
a37f69b7 440 remove_client(client);
0b8f30a5
JA
441 continue;
442 }
443
444 probe_client(client);
81179eec
JA
445
446 if (client->argc > 1)
447 send_client_cmd_line(client);
a37f69b7
JA
448 }
449
450 return !nr_clients;
451}
452
132159a5
JA
453/*
454 * Send file contents to server backend. We could use sendfile(), but to remain
455 * more portable lets just read/write the darn thing.
456 */
a37f69b7 457static int fio_client_send_ini(struct fio_client *client, const char *filename)
132159a5
JA
458{
459 struct stat sb;
460 char *p, *buf;
461 off_t len;
462 int fd, ret;
463
46c48f1f
JA
464 dprint(FD_NET, "send ini %s to %s\n", filename, client->hostname);
465
132159a5
JA
466 fd = open(filename, O_RDONLY);
467 if (fd < 0) {
e951bdc4 468 log_err("fio: job file <%s> open: %s\n", filename, strerror(errno));
132159a5
JA
469 return 1;
470 }
471
472 if (fstat(fd, &sb) < 0) {
473 log_err("fio: job file stat: %s\n", strerror(errno));
b94cba47 474 close(fd);
132159a5
JA
475 return 1;
476 }
477
478 buf = malloc(sb.st_size);
479
480 len = sb.st_size;
481 p = buf;
482 do {
483 ret = read(fd, p, len);
484 if (ret > 0) {
485 len -= ret;
486 if (!len)
487 break;
488 p += ret;
489 continue;
490 } else if (!ret)
491 break;
492 else if (errno == EAGAIN || errno == EINTR)
493 continue;
494 } while (1);
495
0b8f30a5
JA
496 if (len) {
497 log_err("fio: failed reading job file %s\n", filename);
b94cba47 498 close(fd);
c524ef72 499 free(buf);
0b8f30a5
JA
500 return 1;
501 }
502
c2cb6869 503 client->sent_job = 1;
af9c9fb3 504 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, buf, sb.st_size, 0);
132159a5 505 free(buf);
b94cba47 506 close(fd);
132159a5
JA
507 return ret;
508}
37db14fe 509
a37f69b7
JA
510int fio_clients_send_ini(const char *filename)
511{
512 struct fio_client *client;
513 struct flist_head *entry, *tmp;
514
515 flist_for_each_safe(entry, tmp, &client_list) {
516 client = flist_entry(entry, struct fio_client, list);
517
518 if (fio_client_send_ini(client, filename))
519 remove_client(client);
c2cb6869
JA
520
521 client->sent_job = 1;
a37f69b7
JA
522 }
523
524 return !nr_clients;
525}
526
a64e88da
JA
527static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
528{
529 dst->max_val = le64_to_cpu(src->max_val);
530 dst->min_val = le64_to_cpu(src->min_val);
531 dst->samples = le64_to_cpu(src->samples);
802ad4a8
JA
532
533 /*
534 * Floats arrive as IEEE 754 encoded uint64_t, convert back to double
535 */
536 dst->mean.u.f = fio_uint64_to_double(le64_to_cpu(dst->mean.u.i));
537 dst->S.u.f = fio_uint64_to_double(le64_to_cpu(dst->S.u.i));
a64e88da
JA
538}
539
540static void convert_ts(struct thread_stat *dst, struct thread_stat *src)
541{
542 int i, j;
543
544 dst->error = le32_to_cpu(src->error);
545 dst->groupid = le32_to_cpu(src->groupid);
546 dst->pid = le32_to_cpu(src->pid);
547 dst->members = le32_to_cpu(src->members);
548
549 for (i = 0; i < 2; i++) {
550 convert_io_stat(&dst->clat_stat[i], &src->clat_stat[i]);
551 convert_io_stat(&dst->slat_stat[i], &src->slat_stat[i]);
552 convert_io_stat(&dst->lat_stat[i], &src->lat_stat[i]);
553 convert_io_stat(&dst->bw_stat[i], &src->bw_stat[i]);
554 }
555
556 dst->usr_time = le64_to_cpu(src->usr_time);
557 dst->sys_time = le64_to_cpu(src->sys_time);
558 dst->ctx = le64_to_cpu(src->ctx);
559 dst->minf = le64_to_cpu(src->minf);
560 dst->majf = le64_to_cpu(src->majf);
561 dst->clat_percentiles = le64_to_cpu(src->clat_percentiles);
802ad4a8
JA
562
563 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
564 fio_fp64_t *fps = &src->percentile_list[i];
565 fio_fp64_t *fpd = &dst->percentile_list[i];
566
567 fpd->u.f = fio_uint64_to_double(le64_to_cpu(fps->u.i));
568 }
a64e88da
JA
569
570 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
571 dst->io_u_map[i] = le32_to_cpu(src->io_u_map[i]);
572 dst->io_u_submit[i] = le32_to_cpu(src->io_u_submit[i]);
573 dst->io_u_complete[i] = le32_to_cpu(src->io_u_complete[i]);
574 }
575
576 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
577 dst->io_u_lat_u[i] = le32_to_cpu(src->io_u_lat_u[i]);
578 dst->io_u_lat_m[i] = le32_to_cpu(src->io_u_lat_m[i]);
579 }
580
581 for (i = 0; i < 2; i++)
582 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
583 dst->io_u_plat[i][j] = le32_to_cpu(src->io_u_plat[i][j]);
584
585 for (i = 0; i < 3; i++) {
586 dst->total_io_u[i] = le64_to_cpu(src->total_io_u[i]);
93eee04a 587 dst->short_io_u[i] = le64_to_cpu(src->short_io_u[i]);
a64e88da
JA
588 }
589
590 dst->total_submit = le64_to_cpu(src->total_submit);
591 dst->total_complete = le64_to_cpu(src->total_complete);
592
593 for (i = 0; i < 2; i++) {
594 dst->io_bytes[i] = le64_to_cpu(src->io_bytes[i]);
595 dst->runtime[i] = le64_to_cpu(src->runtime[i]);
596 }
597
598 dst->total_run_time = le64_to_cpu(src->total_run_time);
599 dst->continue_on_error = le16_to_cpu(src->continue_on_error);
600 dst->total_err_count = le64_to_cpu(src->total_err_count);
ddcc0b69
JA
601 dst->first_error = le32_to_cpu(src->first_error);
602 dst->kb_base = le32_to_cpu(src->kb_base);
a64e88da
JA
603}
604
605static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
606{
607 int i;
608
609 for (i = 0; i < 2; i++) {
610 dst->max_run[i] = le64_to_cpu(src->max_run[i]);
611 dst->min_run[i] = le64_to_cpu(src->min_run[i]);
612 dst->max_bw[i] = le64_to_cpu(src->max_bw[i]);
613 dst->min_bw[i] = le64_to_cpu(src->min_bw[i]);
614 dst->io_kb[i] = le64_to_cpu(src->io_kb[i]);
615 dst->agg[i] = le64_to_cpu(src->agg[i]);
616 }
617
618 dst->kb_base = le32_to_cpu(src->kb_base);
619 dst->groupid = le32_to_cpu(src->groupid);
620}
621
622static void handle_ts(struct fio_net_cmd *cmd)
623{
624 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
625
626 convert_ts(&p->ts, &p->ts);
627 convert_gs(&p->rs, &p->rs);
628
629 show_thread_status(&p->ts, &p->rs);
37f0c1ae
JA
630
631 if (sum_stat_clients == 1)
632 return;
633
634 sum_thread_stats(&client_ts, &p->ts, sum_stat_nr);
635 sum_group_stats(&client_gs, &p->rs);
636
637 client_ts.members++;
638 client_ts.groupid = p->ts.groupid;
639
640 if (++sum_stat_nr == sum_stat_clients) {
641 strcpy(client_ts.name, "All clients");
642 show_thread_status(&client_ts, &client_gs);
643 }
a64e88da
JA
644}
645
646static void handle_gs(struct fio_net_cmd *cmd)
647{
648 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
649
650 convert_gs(gs, gs);
651 show_group_stats(gs);
652}
653
d09a64a0
JA
654static void convert_agg(struct disk_util_agg *agg)
655{
656 int i;
657
658 for (i = 0; i < 2; i++) {
659 agg->ios[i] = le32_to_cpu(agg->ios[i]);
660 agg->merges[i] = le32_to_cpu(agg->merges[i]);
661 agg->sectors[i] = le64_to_cpu(agg->sectors[i]);
662 agg->ticks[i] = le32_to_cpu(agg->ticks[i]);
663 }
664
665 agg->io_ticks = le32_to_cpu(agg->io_ticks);
666 agg->time_in_queue = le32_to_cpu(agg->time_in_queue);
667 agg->slavecount = le32_to_cpu(agg->slavecount);
823ba54b 668 agg->max_util.u.f = fio_uint64_to_double(__le64_to_cpu(agg->max_util.u.i));
d09a64a0
JA
669}
670
671static void convert_dus(struct disk_util_stat *dus)
672{
673 int i;
674
675 for (i = 0; i < 2; i++) {
676 dus->ios[i] = le32_to_cpu(dus->ios[i]);
677 dus->merges[i] = le32_to_cpu(dus->merges[i]);
678 dus->sectors[i] = le64_to_cpu(dus->sectors[i]);
679 dus->ticks[i] = le32_to_cpu(dus->ticks[i]);
680 }
681
682 dus->io_ticks = le32_to_cpu(dus->io_ticks);
683 dus->time_in_queue = le32_to_cpu(dus->time_in_queue);
684 dus->msec = le64_to_cpu(dus->msec);
685}
686
084d1c6f
JA
687static void handle_text(struct fio_client *client, struct fio_net_cmd *cmd)
688{
689 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
690 const char *buf = (const char *) pdu->buf;
691 const char *name;
692 int fio_unused ret;
693
694 name = client->name ? client->name : client->hostname;
695
696 if (!client->skip_newline)
697 fprintf(f_out, "<%s> ", name);
698 ret = fwrite(buf, pdu->buf_len, 1, f_out);
699 fflush(f_out);
700 client->skip_newline = strchr(buf, '\n') == NULL;
701}
702
703
d09a64a0
JA
704static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd)
705{
706 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
707
708 convert_dus(&du->dus);
709 convert_agg(&du->agg);
710
711 if (!client->disk_stats_shown) {
712 client->disk_stats_shown = 1;
713 log_info("\nDisk stats (read/write):\n");
714 }
715
f2f788dd 716 print_disk_util(&du->dus, &du->agg, terse_output);
d09a64a0
JA
717}
718
3e47bd25 719void fio_client_convert_jobs_eta(struct jobs_eta *je)
cf451d1e 720{
cf451d1e
JA
721 int i;
722
723 je->nr_running = le32_to_cpu(je->nr_running);
724 je->nr_ramp = le32_to_cpu(je->nr_ramp);
725 je->nr_pending = le32_to_cpu(je->nr_pending);
726 je->files_open = le32_to_cpu(je->files_open);
cf451d1e
JA
727
728 for (i = 0; i < 2; i++) {
3e47bd25
JA
729 je->m_rate[i] = le32_to_cpu(je->m_rate[i]);
730 je->t_rate[i] = le32_to_cpu(je->t_rate[i]);
731 je->m_iops[i] = le32_to_cpu(je->m_iops[i]);
732 je->t_iops[i] = le32_to_cpu(je->t_iops[i]);
cf451d1e
JA
733 je->rate[i] = le32_to_cpu(je->rate[i]);
734 je->iops[i] = le32_to_cpu(je->iops[i]);
735 }
736
b51eedb7 737 je->elapsed_sec = le64_to_cpu(je->elapsed_sec);
cf451d1e 738 je->eta_sec = le64_to_cpu(je->eta_sec);
48fbb46e
JA
739}
740
3e47bd25 741void fio_client_sum_jobs_eta(struct jobs_eta *dst, struct jobs_eta *je)
48fbb46e 742{
48fbb46e
JA
743 int i;
744
745 dst->nr_running += je->nr_running;
746 dst->nr_ramp += je->nr_ramp;
747 dst->nr_pending += je->nr_pending;
748 dst->files_open += je->files_open;
48fbb46e
JA
749
750 for (i = 0; i < 2; i++) {
3e47bd25
JA
751 dst->m_rate[i] += je->m_rate[i];
752 dst->t_rate[i] += je->t_rate[i];
753 dst->m_iops[i] += je->m_iops[i];
754 dst->t_iops[i] += je->t_iops[i];
48fbb46e
JA
755 dst->rate[i] += je->rate[i];
756 dst->iops[i] += je->iops[i];
757 }
758
759 dst->elapsed_sec += je->elapsed_sec;
760
761 if (je->eta_sec > dst->eta_sec)
762 dst->eta_sec = je->eta_sec;
763}
764
3e47bd25 765void fio_client_dec_jobs_eta(struct client_eta *eta, void (*fn)(struct jobs_eta *))
82c1ed38
JA
766{
767 if (!--eta->pending) {
3e47bd25 768 fn(&eta->eta);
82c1ed38
JA
769 free(eta);
770 }
771}
772
89c1707c
JA
773static void remove_reply_cmd(struct fio_client *client, struct fio_net_cmd *cmd)
774{
775 struct fio_net_int_cmd *icmd = NULL;
776 struct flist_head *entry;
777
778 flist_for_each(entry, &client->cmd_list) {
779 icmd = flist_entry(entry, struct fio_net_int_cmd, list);
780
df380934 781 if (cmd->tag == (uintptr_t) icmd)
89c1707c
JA
782 break;
783
784 icmd = NULL;
785 }
786
787 if (!icmd) {
788 log_err("fio: client: unable to find matching tag\n");
789 return;
790 }
791
792 flist_del(&icmd->list);
793 cmd->tag = icmd->saved_tag;
794 free(icmd);
795}
796
82c1ed38 797static void handle_eta(struct fio_client *client, struct fio_net_cmd *cmd)
48fbb46e
JA
798{
799 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
df380934 800 struct client_eta *eta = (struct client_eta *) (uintptr_t) cmd->tag;
af9c9fb3
JA
801
802 dprint(FD_NET, "client: got eta tag %p, %d\n", eta, eta->pending);
cf451d1e 803
f77d2676
JA
804 assert(client->eta_in_flight == eta);
805
806 client->eta_in_flight = NULL;
82c1ed38
JA
807 flist_del_init(&client->eta_list);
808
3e47bd25
JA
809 fio_client_convert_jobs_eta(je);
810 fio_client_sum_jobs_eta(&eta->eta, je);
811 fio_client_dec_jobs_eta(eta, display_thread_status);
cf451d1e
JA
812}
813
b5296ddb 814static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd)
2e03b4b2
JA
815{
816 struct cmd_probe_pdu *probe = (struct cmd_probe_pdu *) cmd->payload;
d2333358
JA
817 const char *os, *arch;
818 char bit[16];
2e03b4b2 819
cca84643
JA
820 os = fio_get_os_string(probe->os);
821 if (!os)
822 os = "unknown";
823
824 arch = fio_get_arch_string(probe->arch);
825 if (!arch)
826 os = "unknown";
827
d2333358 828 sprintf(bit, "%d-bit", probe->bpp * 8);
38fdef22
JA
829
830 log_info("hostname=%s, be=%u, %s, os=%s, arch=%s, fio=%u.%u.%u\n",
831 probe->hostname, probe->bigendian, bit, os, arch,
832 probe->fio_major, probe->fio_minor, probe->fio_patch);
b5296ddb
JA
833
834 if (!client->name)
835 client->name = strdup((char *) probe->hostname);
2e03b4b2
JA
836}
837
11e950bd
JA
838static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd)
839{
840 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
841
842 client->state = Client_started;
843 client->jobs = le32_to_cpu(pdu->jobs);
844}
845
846static void handle_stop(struct fio_client *client, struct fio_net_cmd *cmd)
847{
848 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
849
850 client->state = Client_stopped;
851 client->error = le32_to_cpu(pdu->error);
498c92c2
JA
852
853 if (client->error)
854 log_info("client <%s>: exited with error %d\n", client->hostname, client->error);
11e950bd
JA
855}
856
084d1c6f
JA
857static void convert_text(struct fio_net_cmd *cmd)
858{
859 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
860
861 pdu->level = le32_to_cpu(pdu->level);
862 pdu->buf_len = le32_to_cpu(pdu->buf_len);
863 pdu->log_sec = le64_to_cpu(pdu->log_sec);
864 pdu->log_usec = le64_to_cpu(pdu->log_usec);
865}
866
3e47bd25 867int fio_handle_client(struct fio_client *client, struct client_ops *ops)
37db14fe
JA
868{
869 struct fio_net_cmd *cmd;
870
60efd14e
JA
871 dprint(FD_NET, "client: handle %s\n", client->hostname);
872
e951bdc4
JA
873 cmd = fio_net_recv_cmd(client->fd);
874 if (!cmd)
875 return 0;
c2c94585 876
89c1707c
JA
877 dprint(FD_NET, "client: got cmd op %s from %s\n",
878 fio_server_op(cmd->opcode), client->hostname);
46c48f1f 879
e951bdc4
JA
880 switch (cmd->opcode) {
881 case FIO_NET_CMD_QUIT:
3ec62ec4
JA
882 if (ops->quit)
883 ops->quit(client);
e951bdc4
JA
884 remove_client(client);
885 free(cmd);
886 break;
084d1c6f
JA
887 case FIO_NET_CMD_TEXT:
888 convert_text(cmd);
889 ops->text_op(client, cmd);
e951bdc4
JA
890 free(cmd);
891 break;
d09a64a0 892 case FIO_NET_CMD_DU:
dd366728 893 ops->disk_util(client, cmd);
d09a64a0
JA
894 free(cmd);
895 break;
e951bdc4 896 case FIO_NET_CMD_TS:
dd366728 897 ops->thread_status(cmd);
e951bdc4
JA
898 free(cmd);
899 break;
900 case FIO_NET_CMD_GS:
dd366728 901 ops->group_stats(cmd);
e951bdc4
JA
902 free(cmd);
903 break;
904 case FIO_NET_CMD_ETA:
89c1707c 905 remove_reply_cmd(client, cmd);
dd366728 906 ops->eta(client, cmd);
e951bdc4
JA
907 free(cmd);
908 break;
909 case FIO_NET_CMD_PROBE:
89c1707c 910 remove_reply_cmd(client, cmd);
dd366728 911 ops->probe(client, cmd);
e951bdc4
JA
912 free(cmd);
913 break;
01be038e
JA
914 case FIO_NET_CMD_RUN:
915 client->state = Client_running;
916 free(cmd);
917 break;
e951bdc4 918 case FIO_NET_CMD_START:
11e950bd 919 handle_start(client, cmd);
e951bdc4
JA
920 free(cmd);
921 break;
922 case FIO_NET_CMD_STOP:
11e950bd 923 handle_stop(client, cmd);
e951bdc4
JA
924 free(cmd);
925 break;
807f9971
JA
926 case FIO_NET_CMD_ADD_JOB:
927 if (ops->add_job)
928 ops->add_job(client, cmd);
929 free(cmd);
930 break;
e951bdc4 931 default:
89c1707c 932 log_err("fio: unknown client op: %s\n", fio_server_op(cmd->opcode));
e951bdc4
JA
933 free(cmd);
934 break;
37db14fe
JA
935 }
936
e951bdc4 937 return 1;
37db14fe 938}
b66570dc 939
af9c9fb3
JA
940static void request_client_etas(void)
941{
942 struct fio_client *client;
943 struct flist_head *entry;
944 struct client_eta *eta;
82c1ed38 945 int skipped = 0;
af9c9fb3
JA
946
947 dprint(FD_NET, "client: request eta (%d)\n", nr_clients);
948
af9c9fb3
JA
949 eta = malloc(sizeof(*eta));
950 memset(&eta->eta, 0, sizeof(eta->eta));
951 eta->pending = nr_clients;
952
953 flist_for_each(entry, &client_list) {
954 client = flist_entry(entry, struct fio_client, list);
955
82c1ed38
JA
956 if (!flist_empty(&client->eta_list)) {
957 skipped++;
958 continue;
959 }
01be038e
JA
960 if (client->state != Client_running)
961 continue;
82c1ed38 962
f77d2676 963 assert(!client->eta_in_flight);
82c1ed38 964 flist_add_tail(&client->eta_list, &eta_list);
f77d2676 965 client->eta_in_flight = eta;
af9c9fb3 966 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_SEND_ETA,
df380934 967 (uintptr_t) eta, &client->cmd_list);
af9c9fb3
JA
968 }
969
82c1ed38 970 while (skipped--)
3e47bd25 971 fio_client_dec_jobs_eta(eta, display_thread_status);
82c1ed38 972
af9c9fb3
JA
973 dprint(FD_NET, "client: requested eta tag %p\n", eta);
974}
975
89c1707c
JA
976static int client_check_cmd_timeout(struct fio_client *client,
977 struct timeval *now)
978{
979 struct fio_net_int_cmd *cmd;
980 struct flist_head *entry, *tmp;
981 int ret = 0;
982
983 flist_for_each_safe(entry, tmp, &client->cmd_list) {
984 cmd = flist_entry(entry, struct fio_net_int_cmd, list);
985
986 if (mtime_since(&cmd->tv, now) < FIO_NET_CLIENT_TIMEOUT)
987 continue;
988
989 log_err("fio: client %s, timeout on cmd %s\n", client->hostname,
990 fio_server_op(cmd->cmd.opcode));
991 flist_del(&cmd->list);
992 free(cmd);
993 ret = 1;
994 }
995
996 return flist_empty(&client->cmd_list) && ret;
997}
998
ed727a46 999static int fio_client_timed_out(struct client_ops *ops)
89c1707c
JA
1000{
1001 struct fio_client *client;
1002 struct flist_head *entry, *tmp;
1003 struct timeval tv;
1004 int ret = 0;
1005
1006 gettimeofday(&tv, NULL);
1007
1008 flist_for_each_safe(entry, tmp, &client_list) {
1009 client = flist_entry(entry, struct fio_client, list);
1010
1011 if (flist_empty(&client->cmd_list))
1012 continue;
1013
1014 if (!client_check_cmd_timeout(client, &tv))
1015 continue;
1016
ed727a46
JA
1017 if (ops->timed_out)
1018 ops->timed_out(client);
1019 else
1020 log_err("fio: client %s timed out\n", client->hostname);
1021
89c1707c
JA
1022 remove_client(client);
1023 ret = 1;
1024 }
1025
1026 return ret;
1027}
1028
dd366728 1029int fio_handle_clients(struct client_ops *ops)
b66570dc 1030{
b66570dc 1031 struct pollfd *pfds;
498c92c2 1032 int i, ret = 0, retval = 0;
b66570dc 1033
af9c9fb3
JA
1034 gettimeofday(&eta_tv, NULL);
1035
b66570dc
JA
1036 pfds = malloc(nr_clients * sizeof(struct pollfd));
1037
37f0c1ae
JA
1038 sum_stat_clients = nr_clients;
1039 init_thread_stat(&client_ts);
1040 init_group_run_stat(&client_gs);
1041
82a4be1b 1042 while (!exit_backend && nr_clients) {
c2cb6869
JA
1043 struct flist_head *entry, *tmp;
1044 struct fio_client *client;
1045
82a4be1b 1046 i = 0;
c2cb6869 1047 flist_for_each_safe(entry, tmp, &client_list) {
82a4be1b 1048 client = flist_entry(entry, struct fio_client, list);
b66570dc 1049
3ec62ec4 1050 if (!client->sent_job && !ops->stay_connected &&
c2cb6869
JA
1051 flist_empty(&client->cmd_list)) {
1052 remove_client(client);
1053 continue;
1054 }
1055
82a4be1b
JA
1056 pfds[i].fd = client->fd;
1057 pfds[i].events = POLLIN;
1058 i++;
1059 }
1060
c2cb6869
JA
1061 if (!nr_clients)
1062 break;
1063
82a4be1b 1064 assert(i == nr_clients);
b66570dc 1065
5c2857f9 1066 do {
af9c9fb3
JA
1067 struct timeval tv;
1068
1069 gettimeofday(&tv, NULL);
1070 if (mtime_since(&eta_tv, &tv) >= 900) {
1071 request_client_etas();
1072 memcpy(&eta_tv, &tv, sizeof(tv));
89c1707c 1073
ed727a46 1074 if (fio_client_timed_out(ops))
89c1707c 1075 break;
af9c9fb3
JA
1076 }
1077
5c2857f9
JA
1078 ret = poll(pfds, nr_clients, 100);
1079 if (ret < 0) {
1080 if (errno == EINTR)
1081 continue;
1082 log_err("fio: poll clients: %s\n", strerror(errno));
1083 break;
1084 } else if (!ret)
b66570dc 1085 continue;
5c2857f9 1086 } while (ret <= 0);
b66570dc
JA
1087
1088 for (i = 0; i < nr_clients; i++) {
1089 if (!(pfds[i].revents & POLLIN))
1090 continue;
1091
1092 client = find_client_by_fd(pfds[i].fd);
1093 if (!client) {
3c5f57e3 1094 log_err("fio: unknown client fd %d\n", pfds[i].fd);
b66570dc
JA
1095 continue;
1096 }
3e47bd25 1097 if (!fio_handle_client(client, ops)) {
28d3ab07
JA
1098 log_info("client: host=%s disconnected\n",
1099 client->hostname);
1100 remove_client(client);
498c92c2 1101 retval = 1;
38990764 1102 } else if (client->error)
498c92c2 1103 retval = 1;
b66570dc
JA
1104 }
1105 }
1106
1107 free(pfds);
498c92c2 1108 return retval;
b66570dc 1109}