client: add hashes for fd/name lookups
[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>
132159a5
JA
12#include <netinet/in.h>
13#include <arpa/inet.h>
14#include <netdb.h>
9e22ecb0 15#include <signal.h>
132159a5
JA
16
17#include "fio.h"
18#include "server.h"
b66570dc 19#include "flist.h"
3c5f57e3 20#include "hash.h"
132159a5 21
b66570dc
JA
22struct fio_client {
23 struct flist_head list;
3c5f57e3
JA
24 struct flist_head fd_hash_list;
25 struct flist_head name_hash_list;
b66570dc
JA
26 struct sockaddr_in addr;
27 char *hostname;
28 int fd;
81179eec
JA
29
30 int state;
17dd1764 31 int skip_newline;
81179eec
JA
32
33 uint16_t argc;
34 char **argv;
35};
36
37enum {
5c2857f9 38 Client_created = 0,
81179eec
JA
39 Client_connected = 1,
40 Client_started = 2,
41 Client_stopped = 3,
5c2857f9 42 Client_exited = 4,
b66570dc
JA
43};
44
45static FLIST_HEAD(client_list);
b66570dc 46
3c5f57e3
JA
47#define FIO_CLIENT_HASH_BITS 7
48#define FIO_CLIENT_HASH_SZ (1 << FIO_CLIENT_HASH_BITS)
49#define FIO_CLIENT_HASH_MASK (FIO_CLIENT_HASH_SZ - 1)
50static struct flist_head client_fd_hash[FIO_CLIENT_HASH_SZ];
51static struct flist_head client_name_hash[FIO_CLIENT_HASH_SZ];
52
e951bdc4 53static int handle_client(struct fio_client *client);
0b8f30a5 54
3c5f57e3
JA
55static void fio_client_add_fd_hash(struct fio_client *client)
56{
57 int bucket = hash_long(client->fd, FIO_CLIENT_HASH_BITS);
58
59 bucket &= FIO_CLIENT_HASH_MASK;
60 flist_add(&client->fd_hash_list, &client_fd_hash[bucket]);
61}
62
63static void fio_client_remove_fd_hash(struct fio_client *client)
64{
65 if (!flist_empty(&client->fd_hash_list))
66 flist_del_init(&client->fd_hash_list);
67}
68
69static void fio_client_add_name_hash(struct fio_client *client)
70{
71 int bucket = jhash(client->hostname, strlen(client->hostname), 0);
72
73 bucket &= FIO_CLIENT_HASH_MASK;
74 flist_add(&client->name_hash_list, &client_name_hash[bucket]);
75}
76
77static void fio_client_remove_name_hash(struct fio_client *client)
78{
79 if (!flist_empty(&client->name_hash_list))
80 flist_del_init(&client->name_hash_list);
81}
82
83static void fio_init fio_client_hash_init(void)
84{
85 int i;
86
87 for (i = 0; i < FIO_CLIENT_HASH_SZ; i++) {
88 INIT_FLIST_HEAD(&client_fd_hash[i]);
89 INIT_FLIST_HEAD(&client_name_hash[i]);
90 }
91}
92
b66570dc
JA
93static struct fio_client *find_client_by_fd(int fd)
94{
3c5f57e3 95 int bucket = hash_long(fd, FIO_CLIENT_HASH_BITS) & FIO_CLIENT_HASH_MASK;
b66570dc
JA
96 struct fio_client *client;
97 struct flist_head *entry;
98
3c5f57e3
JA
99 flist_for_each(entry, &client_fd_hash[bucket]) {
100 client = flist_entry(entry, struct fio_client, fd_hash_list);
b66570dc
JA
101
102 if (client->fd == fd)
103 return client;
104 }
105
106 return NULL;
107}
108
109static struct fio_client *find_client_by_name(const char *name)
110{
3c5f57e3 111 int bucket = jhash(name, strlen(name), 0) & FIO_CLIENT_HASH_BITS;
b66570dc
JA
112 struct fio_client *client;
113 struct flist_head *entry;
114
3c5f57e3
JA
115 flist_for_each(entry, &client_name_hash[bucket]) {
116 client = flist_entry(entry, struct fio_client, name_hash_list);
b66570dc
JA
117
118 if (!strcmp(name, client->hostname))
119 return client;
120 }
121
122 return NULL;
123}
124
125static void remove_client(struct fio_client *client)
126{
39e8e016 127 dprint(FD_NET, "client: removed <%s>\n", client->hostname);
b66570dc 128 flist_del(&client->list);
3c5f57e3
JA
129
130 fio_client_remove_fd_hash(client);
131 fio_client_remove_name_hash(client);
81179eec 132
b66570dc 133 free(client->hostname);
81179eec
JA
134 if (client->argv)
135 free(client->argv);
136
b66570dc 137 free(client);
3c5f57e3 138 nr_clients--;
b66570dc 139}
132159a5 140
7a4b8240
JA
141static int __fio_client_add_cmd_option(struct fio_client *client,
142 const char *opt)
81179eec 143{
39e8e016
JA
144 int index;
145
7a4b8240
JA
146 if (client->argc == FIO_NET_CMD_JOBLINE_ARGV) {
147 log_err("fio: max cmd line number reached.\n");
148 log_err("fio: cmd line <%s> has been ignored.\n", opt);
149 return 1;
150 }
151
39e8e016 152 index = client->argc++;
81179eec 153 client->argv = realloc(client->argv, sizeof(char *) * client->argc);
39e8e016
JA
154 client->argv[index] = strdup(opt);
155 dprint(FD_NET, "client: add cmd %d: %s\n", index, opt);
7a4b8240 156 return 0;
81179eec
JA
157}
158
7a4b8240 159int fio_client_add_cmd_option(const char *hostname, const char *opt)
81179eec
JA
160{
161 struct fio_client *client;
162
163 if (!hostname || !opt)
7a4b8240 164 return 0;
81179eec
JA
165
166 client = find_client_by_name(hostname);
167 if (!client) {
168 log_err("fio: unknown client %s\n", hostname);
7a4b8240 169 return 1;
81179eec
JA
170 }
171
7a4b8240 172 return __fio_client_add_cmd_option(client, opt);
81179eec
JA
173}
174
a37f69b7 175void fio_client_add(const char *hostname)
132159a5 176{
b66570dc 177 struct fio_client *client;
132159a5 178
39e8e016 179 dprint(FD_NET, "client: added <%s>\n", hostname);
b66570dc 180 client = malloc(sizeof(*client));
a37f69b7 181 memset(client, 0, sizeof(*client));
81179eec 182
3c5f57e3
JA
183 INIT_FLIST_HEAD(&client->list);
184 INIT_FLIST_HEAD(&client->fd_hash_list);
185 INIT_FLIST_HEAD(&client->name_hash_list);
186
a37f69b7
JA
187 client->hostname = strdup(hostname);
188 client->fd = -1;
81179eec 189
3c5f57e3
JA
190 fio_client_add_name_hash(client);
191
81179eec
JA
192 __fio_client_add_cmd_option(client, "fio");
193
a37f69b7
JA
194 flist_add(&client->list, &client_list);
195 nr_clients++;
196}
197
198static int fio_client_connect(struct fio_client *client)
199{
200 int fd;
132159a5 201
39e8e016 202 dprint(FD_NET, "client: connect to host %s\n", client->hostname);
46c48f1f 203
b66570dc
JA
204 memset(&client->addr, 0, sizeof(client->addr));
205 client->addr.sin_family = AF_INET;
206 client->addr.sin_port = htons(fio_net_port);
207
a37f69b7 208 if (inet_aton(client->hostname, &client->addr.sin_addr) != 1) {
132159a5
JA
209 struct hostent *hent;
210
a37f69b7 211 hent = gethostbyname(client->hostname);
132159a5
JA
212 if (!hent) {
213 log_err("fio: gethostbyname: %s\n", strerror(errno));
e951bdc4
JA
214 log_err("fio: failed looking up hostname %s\n",
215 client->hostname);
132159a5
JA
216 return 1;
217 }
218
b66570dc 219 memcpy(&client->addr.sin_addr, hent->h_addr, 4);
132159a5
JA
220 }
221
222 fd = socket(AF_INET, SOCK_STREAM, 0);
223 if (fd < 0) {
224 log_err("fio: socket: %s\n", strerror(errno));
225 return 1;
226 }
227
b66570dc 228 if (connect(fd, (struct sockaddr *) &client->addr, sizeof(client->addr)) < 0) {
132159a5 229 log_err("fio: connect: %s\n", strerror(errno));
cdf54d85 230 log_err("fio: failed to connect to %s\n", client->hostname);
132159a5
JA
231 return 1;
232 }
233
b66570dc 234 client->fd = fd;
3c5f57e3 235 fio_client_add_fd_hash(client);
81179eec 236 client->state = Client_connected;
132159a5
JA
237 return 0;
238}
239
cc0df00a
JA
240void fio_clients_terminate(void)
241{
242 struct flist_head *entry;
243 struct fio_client *client;
244
60efd14e
JA
245 dprint(FD_NET, "client: terminate clients\n");
246
cc0df00a
JA
247 flist_for_each(entry, &client_list) {
248 client = flist_entry(entry, struct fio_client, list);
249
250 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_QUIT, 0);
251 }
252}
253
254static void sig_int(int sig)
255{
60efd14e 256 dprint(FD_NET, "client: got sign %d\n", sig);
cc0df00a
JA
257 fio_clients_terminate();
258}
259
260static void client_signal_handler(void)
261{
262 struct sigaction act;
263
264 memset(&act, 0, sizeof(act));
265 act.sa_handler = sig_int;
266 act.sa_flags = SA_RESTART;
267 sigaction(SIGINT, &act, NULL);
268
269 memset(&act, 0, sizeof(act));
270 act.sa_handler = sig_int;
271 act.sa_flags = SA_RESTART;
272 sigaction(SIGTERM, &act, NULL);
273}
274
0b8f30a5
JA
275static void probe_client(struct fio_client *client)
276{
60efd14e
JA
277 dprint(FD_NET, "client: send probe\n");
278
0b8f30a5 279 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_PROBE, 0);
e951bdc4 280 handle_client(client);
0b8f30a5
JA
281}
282
81179eec
JA
283static int send_client_cmd_line(struct fio_client *client)
284{
285 struct cmd_line_pdu *pdu;
286 int i, ret;
287
39e8e016 288 dprint(FD_NET, "client: send cmdline %d\n", client->argc);
60efd14e 289
81179eec
JA
290 pdu = malloc(sizeof(*pdu));
291 for (i = 0; i < client->argc; i++)
292 strcpy((char *) pdu->argv[i], client->argv[i]);
293
294 pdu->argc = cpu_to_le16(client->argc);
295 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOBLINE, pdu, sizeof(*pdu));
296 free(pdu);
297 return ret;
298}
299
a37f69b7
JA
300int fio_clients_connect(void)
301{
302 struct fio_client *client;
303 struct flist_head *entry, *tmp;
304 int ret;
305
60efd14e
JA
306 dprint(FD_NET, "client: connect all\n");
307
cc0df00a
JA
308 client_signal_handler();
309
a37f69b7
JA
310 flist_for_each_safe(entry, tmp, &client_list) {
311 client = flist_entry(entry, struct fio_client, list);
312
313 ret = fio_client_connect(client);
0b8f30a5 314 if (ret) {
a37f69b7 315 remove_client(client);
0b8f30a5
JA
316 continue;
317 }
318
319 probe_client(client);
81179eec
JA
320
321 if (client->argc > 1)
322 send_client_cmd_line(client);
a37f69b7
JA
323 }
324
325 return !nr_clients;
326}
327
132159a5
JA
328/*
329 * Send file contents to server backend. We could use sendfile(), but to remain
330 * more portable lets just read/write the darn thing.
331 */
a37f69b7 332static int fio_client_send_ini(struct fio_client *client, const char *filename)
132159a5
JA
333{
334 struct stat sb;
335 char *p, *buf;
336 off_t len;
337 int fd, ret;
338
46c48f1f
JA
339 dprint(FD_NET, "send ini %s to %s\n", filename, client->hostname);
340
132159a5
JA
341 fd = open(filename, O_RDONLY);
342 if (fd < 0) {
e951bdc4 343 log_err("fio: job file <%s> open: %s\n", filename, strerror(errno));
132159a5
JA
344 return 1;
345 }
346
347 if (fstat(fd, &sb) < 0) {
348 log_err("fio: job file stat: %s\n", strerror(errno));
349 return 1;
350 }
351
352 buf = malloc(sb.st_size);
353
354 len = sb.st_size;
355 p = buf;
356 do {
357 ret = read(fd, p, len);
358 if (ret > 0) {
359 len -= ret;
360 if (!len)
361 break;
362 p += ret;
363 continue;
364 } else if (!ret)
365 break;
366 else if (errno == EAGAIN || errno == EINTR)
367 continue;
368 } while (1);
369
0b8f30a5
JA
370 if (len) {
371 log_err("fio: failed reading job file %s\n", filename);
372 return 1;
373 }
374
81179eec 375 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, buf, sb.st_size);
132159a5
JA
376 free(buf);
377 return ret;
378}
37db14fe 379
a37f69b7
JA
380int fio_clients_send_ini(const char *filename)
381{
382 struct fio_client *client;
383 struct flist_head *entry, *tmp;
384
385 flist_for_each_safe(entry, tmp, &client_list) {
386 client = flist_entry(entry, struct fio_client, list);
387
388 if (fio_client_send_ini(client, filename))
389 remove_client(client);
390 }
391
392 return !nr_clients;
393}
394
a64e88da
JA
395static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
396{
397 dst->max_val = le64_to_cpu(src->max_val);
398 dst->min_val = le64_to_cpu(src->min_val);
399 dst->samples = le64_to_cpu(src->samples);
802ad4a8
JA
400
401 /*
402 * Floats arrive as IEEE 754 encoded uint64_t, convert back to double
403 */
404 dst->mean.u.f = fio_uint64_to_double(le64_to_cpu(dst->mean.u.i));
405 dst->S.u.f = fio_uint64_to_double(le64_to_cpu(dst->S.u.i));
a64e88da
JA
406}
407
408static void convert_ts(struct thread_stat *dst, struct thread_stat *src)
409{
410 int i, j;
411
412 dst->error = le32_to_cpu(src->error);
413 dst->groupid = le32_to_cpu(src->groupid);
414 dst->pid = le32_to_cpu(src->pid);
415 dst->members = le32_to_cpu(src->members);
416
417 for (i = 0; i < 2; i++) {
418 convert_io_stat(&dst->clat_stat[i], &src->clat_stat[i]);
419 convert_io_stat(&dst->slat_stat[i], &src->slat_stat[i]);
420 convert_io_stat(&dst->lat_stat[i], &src->lat_stat[i]);
421 convert_io_stat(&dst->bw_stat[i], &src->bw_stat[i]);
422 }
423
424 dst->usr_time = le64_to_cpu(src->usr_time);
425 dst->sys_time = le64_to_cpu(src->sys_time);
426 dst->ctx = le64_to_cpu(src->ctx);
427 dst->minf = le64_to_cpu(src->minf);
428 dst->majf = le64_to_cpu(src->majf);
429 dst->clat_percentiles = le64_to_cpu(src->clat_percentiles);
802ad4a8
JA
430
431 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
432 fio_fp64_t *fps = &src->percentile_list[i];
433 fio_fp64_t *fpd = &dst->percentile_list[i];
434
435 fpd->u.f = fio_uint64_to_double(le64_to_cpu(fps->u.i));
436 }
a64e88da
JA
437
438 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
439 dst->io_u_map[i] = le32_to_cpu(src->io_u_map[i]);
440 dst->io_u_submit[i] = le32_to_cpu(src->io_u_submit[i]);
441 dst->io_u_complete[i] = le32_to_cpu(src->io_u_complete[i]);
442 }
443
444 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
445 dst->io_u_lat_u[i] = le32_to_cpu(src->io_u_lat_u[i]);
446 dst->io_u_lat_m[i] = le32_to_cpu(src->io_u_lat_m[i]);
447 }
448
449 for (i = 0; i < 2; i++)
450 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
451 dst->io_u_plat[i][j] = le32_to_cpu(src->io_u_plat[i][j]);
452
453 for (i = 0; i < 3; i++) {
454 dst->total_io_u[i] = le64_to_cpu(src->total_io_u[i]);
93eee04a 455 dst->short_io_u[i] = le64_to_cpu(src->short_io_u[i]);
a64e88da
JA
456 }
457
458 dst->total_submit = le64_to_cpu(src->total_submit);
459 dst->total_complete = le64_to_cpu(src->total_complete);
460
461 for (i = 0; i < 2; i++) {
462 dst->io_bytes[i] = le64_to_cpu(src->io_bytes[i]);
463 dst->runtime[i] = le64_to_cpu(src->runtime[i]);
464 }
465
466 dst->total_run_time = le64_to_cpu(src->total_run_time);
467 dst->continue_on_error = le16_to_cpu(src->continue_on_error);
468 dst->total_err_count = le64_to_cpu(src->total_err_count);
ddcc0b69
JA
469 dst->first_error = le32_to_cpu(src->first_error);
470 dst->kb_base = le32_to_cpu(src->kb_base);
a64e88da
JA
471}
472
473static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
474{
475 int i;
476
477 for (i = 0; i < 2; i++) {
478 dst->max_run[i] = le64_to_cpu(src->max_run[i]);
479 dst->min_run[i] = le64_to_cpu(src->min_run[i]);
480 dst->max_bw[i] = le64_to_cpu(src->max_bw[i]);
481 dst->min_bw[i] = le64_to_cpu(src->min_bw[i]);
482 dst->io_kb[i] = le64_to_cpu(src->io_kb[i]);
483 dst->agg[i] = le64_to_cpu(src->agg[i]);
484 }
485
486 dst->kb_base = le32_to_cpu(src->kb_base);
487 dst->groupid = le32_to_cpu(src->groupid);
488}
489
490static void handle_ts(struct fio_net_cmd *cmd)
491{
492 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
493
494 convert_ts(&p->ts, &p->ts);
495 convert_gs(&p->rs, &p->rs);
496
497 show_thread_status(&p->ts, &p->rs);
498}
499
500static void handle_gs(struct fio_net_cmd *cmd)
501{
502 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
503
504 convert_gs(gs, gs);
505 show_group_stats(gs);
506}
507
cf451d1e
JA
508static void handle_eta(struct fio_net_cmd *cmd)
509{
510 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
511 int i;
512
513 je->nr_running = le32_to_cpu(je->nr_running);
514 je->nr_ramp = le32_to_cpu(je->nr_ramp);
515 je->nr_pending = le32_to_cpu(je->nr_pending);
516 je->files_open = le32_to_cpu(je->files_open);
517 je->m_rate = le32_to_cpu(je->m_rate);
518 je->t_rate = le32_to_cpu(je->t_rate);
519 je->m_iops = le32_to_cpu(je->m_iops);
520 je->t_iops = le32_to_cpu(je->t_iops);
521
522 for (i = 0; i < 2; i++) {
523 je->rate[i] = le32_to_cpu(je->rate[i]);
524 je->iops[i] = le32_to_cpu(je->iops[i]);
525 }
526
527 je->elapsed_sec = le32_to_cpu(je->nr_running);
528 je->eta_sec = le64_to_cpu(je->eta_sec);
529
530 display_thread_status(je);
531}
532
2e03b4b2
JA
533static void handle_probe(struct fio_net_cmd *cmd)
534{
535 struct cmd_probe_pdu *probe = (struct cmd_probe_pdu *) cmd->payload;
536
6eb24791
JA
537 log_info("Probe: hostname=%s, be=%u, fio ver %u.%u.%u\n",
538 probe->hostname, probe->bigendian, probe->fio_major,
539 probe->fio_minor, probe->fio_patch);
2e03b4b2
JA
540}
541
e951bdc4 542static int handle_client(struct fio_client *client)
37db14fe
JA
543{
544 struct fio_net_cmd *cmd;
545
60efd14e
JA
546 dprint(FD_NET, "client: handle %s\n", client->hostname);
547
e951bdc4
JA
548 cmd = fio_net_recv_cmd(client->fd);
549 if (!cmd)
550 return 0;
c2c94585 551
e951bdc4 552 dprint(FD_NET, "client: got cmd op %d from %s\n",
c2c94585 553 cmd->opcode, client->hostname);
46c48f1f 554
e951bdc4
JA
555 switch (cmd->opcode) {
556 case FIO_NET_CMD_QUIT:
557 remove_client(client);
558 free(cmd);
559 break;
560 case FIO_NET_CMD_TEXT: {
561 const char *buf = (const char *) cmd->payload;
562 int fio_unused ret;
563
564 if (!client->skip_newline)
565 fprintf(f_out, "<%s> ", client->hostname);
566 ret = fwrite(buf, cmd->pdu_len, 1, f_out);
567 fflush(f_out);
568 client->skip_newline = strchr(buf, '\n') == NULL;
569 free(cmd);
570 break;
37db14fe 571 }
e951bdc4
JA
572 case FIO_NET_CMD_TS:
573 handle_ts(cmd);
574 free(cmd);
575 break;
576 case FIO_NET_CMD_GS:
577 handle_gs(cmd);
578 free(cmd);
579 break;
580 case FIO_NET_CMD_ETA:
581 handle_eta(cmd);
582 free(cmd);
583 break;
584 case FIO_NET_CMD_PROBE:
585 handle_probe(cmd);
586 free(cmd);
587 break;
588 case FIO_NET_CMD_START:
589 client->state = Client_started;
590 free(cmd);
591 break;
592 case FIO_NET_CMD_STOP:
593 client->state = Client_stopped;
594 free(cmd);
595 break;
596 default:
597 log_err("fio: unknown client op: %d\n", cmd->opcode);
598 free(cmd);
599 break;
37db14fe
JA
600 }
601
e951bdc4 602 return 1;
37db14fe 603}
b66570dc
JA
604
605int fio_handle_clients(void)
606{
607 struct fio_client *client;
608 struct flist_head *entry;
609 struct pollfd *pfds;
82a4be1b 610 int i, ret = 0;
b66570dc
JA
611
612 pfds = malloc(nr_clients * sizeof(struct pollfd));
613
82a4be1b
JA
614 while (!exit_backend && nr_clients) {
615 i = 0;
616 flist_for_each(entry, &client_list) {
617 client = flist_entry(entry, struct fio_client, list);
b66570dc 618
82a4be1b
JA
619 pfds[i].fd = client->fd;
620 pfds[i].events = POLLIN;
621 i++;
622 }
623
624 assert(i == nr_clients);
b66570dc 625
5c2857f9
JA
626 do {
627 ret = poll(pfds, nr_clients, 100);
628 if (ret < 0) {
629 if (errno == EINTR)
630 continue;
631 log_err("fio: poll clients: %s\n", strerror(errno));
632 break;
633 } else if (!ret)
b66570dc 634 continue;
5c2857f9 635 } while (ret <= 0);
b66570dc
JA
636
637 for (i = 0; i < nr_clients; i++) {
638 if (!(pfds[i].revents & POLLIN))
639 continue;
640
641 client = find_client_by_fd(pfds[i].fd);
642 if (!client) {
3c5f57e3 643 log_err("fio: unknown client fd %d\n", pfds[i].fd);
b66570dc
JA
644 continue;
645 }
e951bdc4 646 if (!handle_client(client)) {
28d3ab07
JA
647 log_info("client: host=%s disconnected\n",
648 client->hostname);
649 remove_client(client);
650 }
b66570dc
JA
651 }
652 }
653
654 free(pfds);
b66570dc
JA
655 return 0;
656}