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