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