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