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