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