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