blob: c6588664ecd3613501154b21810a2c0f98a5c69a [file] [log] [blame]
Jens Axboe132159a2011-09-30 15:01:32 -06001#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"
Jens Axboeb66570d2011-10-01 11:11:35 -060019#include "flist.h"
Jens Axboe132159a2011-09-30 15:01:32 -060020
Jens Axboeb66570d2011-10-01 11:11:35 -060021struct fio_client {
22 struct flist_head list;
23 struct sockaddr_in addr;
24 char *hostname;
25 int fd;
26};
27
28static FLIST_HEAD(client_list);
Jens Axboeb66570d2011-10-01 11:11:35 -060029
30static 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
Jens Axboea37f69b2011-10-01 12:24:21 -060045#if 0
Jens Axboeb66570d2011-10-01 11:11:35 -060046static 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}
Jens Axboea37f69b2011-10-01 12:24:21 -060060#endif
Jens Axboeb66570d2011-10-01 11:11:35 -060061
62static void remove_client(struct fio_client *client)
63{
Jens Axboe46c48f12011-10-01 12:50:51 -060064 dprint(FD_NET, "removed client <%s>\n", client->hostname);
Jens Axboeb66570d2011-10-01 11:11:35 -060065 flist_del(&client->list);
66 nr_clients--;
67 free(client->hostname);
68 free(client);
69}
Jens Axboe132159a2011-09-30 15:01:32 -060070
Jens Axboea37f69b2011-10-01 12:24:21 -060071void fio_client_add(const char *hostname)
Jens Axboe132159a2011-09-30 15:01:32 -060072{
Jens Axboeb66570d2011-10-01 11:11:35 -060073 struct fio_client *client;
Jens Axboe132159a2011-09-30 15:01:32 -060074
Jens Axboe46c48f12011-10-01 12:50:51 -060075 dprint(FD_NET, "added client <%s>\n", hostname);
Jens Axboeb66570d2011-10-01 11:11:35 -060076 client = malloc(sizeof(*client));
Jens Axboea37f69b2011-10-01 12:24:21 -060077 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
84static int fio_client_connect(struct fio_client *client)
85{
86 int fd;
Jens Axboe132159a2011-09-30 15:01:32 -060087
Jens Axboe46c48f12011-10-01 12:50:51 -060088 dprint(FD_NET, "connect to host %s\n", client->hostname);
89
Jens Axboeb66570d2011-10-01 11:11:35 -060090 memset(&client->addr, 0, sizeof(client->addr));
91 client->addr.sin_family = AF_INET;
92 client->addr.sin_port = htons(fio_net_port);
93
Jens Axboea37f69b2011-10-01 12:24:21 -060094 if (inet_aton(client->hostname, &client->addr.sin_addr) != 1) {
Jens Axboe132159a2011-09-30 15:01:32 -060095 struct hostent *hent;
96
Jens Axboea37f69b2011-10-01 12:24:21 -060097 hent = gethostbyname(client->hostname);
Jens Axboe132159a2011-09-30 15:01:32 -060098 if (!hent) {
99 log_err("fio: gethostbyname: %s\n", strerror(errno));
100 return 1;
101 }
102
Jens Axboeb66570d2011-10-01 11:11:35 -0600103 memcpy(&client->addr.sin_addr, hent->h_addr, 4);
Jens Axboe132159a2011-09-30 15:01:32 -0600104 }
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
Jens Axboeb66570d2011-10-01 11:11:35 -0600112 if (connect(fd, (struct sockaddr *) &client->addr, sizeof(client->addr)) < 0) {
Jens Axboe132159a2011-09-30 15:01:32 -0600113 log_err("fio: connect: %s\n", strerror(errno));
114 return 1;
115 }
116
Jens Axboeb66570d2011-10-01 11:11:35 -0600117 client->fd = fd;
Jens Axboe132159a2011-09-30 15:01:32 -0600118 return 0;
119}
120
Jens Axboea37f69b2011-10-01 12:24:21 -0600121int fio_clients_connect(void)
122{
123 struct fio_client *client;
124 struct flist_head *entry, *tmp;
125 int ret;
126
127 flist_for_each_safe(entry, tmp, &client_list) {
128 client = flist_entry(entry, struct fio_client, list);
129
130 ret = fio_client_connect(client);
131 if (ret)
132 remove_client(client);
133 }
134
135 return !nr_clients;
136}
137
Jens Axboeb66570d2011-10-01 11:11:35 -0600138static int send_file_buf(struct fio_client *client, char *buf, off_t size)
Jens Axboe132159a2011-09-30 15:01:32 -0600139{
Jens Axboeb66570d2011-10-01 11:11:35 -0600140 return fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, buf, size);
Jens Axboe132159a2011-09-30 15:01:32 -0600141}
142
143/*
144 * Send file contents to server backend. We could use sendfile(), but to remain
145 * more portable lets just read/write the darn thing.
146 */
Jens Axboea37f69b2011-10-01 12:24:21 -0600147static int fio_client_send_ini(struct fio_client *client, const char *filename)
Jens Axboe132159a2011-09-30 15:01:32 -0600148{
149 struct stat sb;
150 char *p, *buf;
151 off_t len;
152 int fd, ret;
153
Jens Axboe46c48f12011-10-01 12:50:51 -0600154 dprint(FD_NET, "send ini %s to %s\n", filename, client->hostname);
155
Jens Axboe132159a2011-09-30 15:01:32 -0600156 fd = open(filename, O_RDONLY);
157 if (fd < 0) {
158 log_err("fio: job file open: %s\n", strerror(errno));
159 return 1;
160 }
161
162 if (fstat(fd, &sb) < 0) {
163 log_err("fio: job file stat: %s\n", strerror(errno));
164 return 1;
165 }
166
167 buf = malloc(sb.st_size);
168
169 len = sb.st_size;
170 p = buf;
171 do {
172 ret = read(fd, p, len);
173 if (ret > 0) {
174 len -= ret;
175 if (!len)
176 break;
177 p += ret;
178 continue;
179 } else if (!ret)
180 break;
181 else if (errno == EAGAIN || errno == EINTR)
182 continue;
183 } while (1);
184
Jens Axboeb66570d2011-10-01 11:11:35 -0600185 ret = send_file_buf(client, buf, sb.st_size);
Jens Axboe132159a2011-09-30 15:01:32 -0600186 free(buf);
187 return ret;
188}
Jens Axboe37db14f2011-09-30 17:00:42 -0600189
Jens Axboea37f69b2011-10-01 12:24:21 -0600190int fio_clients_send_ini(const char *filename)
191{
192 struct fio_client *client;
193 struct flist_head *entry, *tmp;
194
195 flist_for_each_safe(entry, tmp, &client_list) {
196 client = flist_entry(entry, struct fio_client, list);
197
198 if (fio_client_send_ini(client, filename))
199 remove_client(client);
200 }
201
202 return !nr_clients;
203}
204
Jens Axboea64e88d2011-10-03 14:20:01 +0200205static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
206{
207 dst->max_val = le64_to_cpu(src->max_val);
208 dst->min_val = le64_to_cpu(src->min_val);
209 dst->samples = le64_to_cpu(src->samples);
210 /* FIXME */
Jens Axboeddcc0b62011-10-03 14:45:27 +0200211 dst->mean = __le64_to_cpu(src->mean);
212 dst->S = __le64_to_cpu(src->S);
Jens Axboea64e88d2011-10-03 14:20:01 +0200213}
214
215static void convert_ts(struct thread_stat *dst, struct thread_stat *src)
216{
217 int i, j;
218
219 dst->error = le32_to_cpu(src->error);
220 dst->groupid = le32_to_cpu(src->groupid);
221 dst->pid = le32_to_cpu(src->pid);
222 dst->members = le32_to_cpu(src->members);
223
224 for (i = 0; i < 2; i++) {
225 convert_io_stat(&dst->clat_stat[i], &src->clat_stat[i]);
226 convert_io_stat(&dst->slat_stat[i], &src->slat_stat[i]);
227 convert_io_stat(&dst->lat_stat[i], &src->lat_stat[i]);
228 convert_io_stat(&dst->bw_stat[i], &src->bw_stat[i]);
229 }
230
231 dst->usr_time = le64_to_cpu(src->usr_time);
232 dst->sys_time = le64_to_cpu(src->sys_time);
233 dst->ctx = le64_to_cpu(src->ctx);
234 dst->minf = le64_to_cpu(src->minf);
235 dst->majf = le64_to_cpu(src->majf);
236 dst->clat_percentiles = le64_to_cpu(src->clat_percentiles);
237 dst->percentile_list = NULL;
238
239 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
240 dst->io_u_map[i] = le32_to_cpu(src->io_u_map[i]);
241 dst->io_u_submit[i] = le32_to_cpu(src->io_u_submit[i]);
242 dst->io_u_complete[i] = le32_to_cpu(src->io_u_complete[i]);
243 }
244
245 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
246 dst->io_u_lat_u[i] = le32_to_cpu(src->io_u_lat_u[i]);
247 dst->io_u_lat_m[i] = le32_to_cpu(src->io_u_lat_m[i]);
248 }
249
250 for (i = 0; i < 2; i++)
251 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
252 dst->io_u_plat[i][j] = le32_to_cpu(src->io_u_plat[i][j]);
253
254 for (i = 0; i < 3; i++) {
255 dst->total_io_u[i] = le64_to_cpu(src->total_io_u[i]);
256 dst->short_io_u[i] = le64_to_cpu(src->total_io_u[i]);
257 }
258
259 dst->total_submit = le64_to_cpu(src->total_submit);
260 dst->total_complete = le64_to_cpu(src->total_complete);
261
262 for (i = 0; i < 2; i++) {
263 dst->io_bytes[i] = le64_to_cpu(src->io_bytes[i]);
264 dst->runtime[i] = le64_to_cpu(src->runtime[i]);
265 }
266
267 dst->total_run_time = le64_to_cpu(src->total_run_time);
268 dst->continue_on_error = le16_to_cpu(src->continue_on_error);
269 dst->total_err_count = le64_to_cpu(src->total_err_count);
Jens Axboeddcc0b62011-10-03 14:45:27 +0200270 dst->first_error = le32_to_cpu(src->first_error);
271 dst->kb_base = le32_to_cpu(src->kb_base);
Jens Axboea64e88d2011-10-03 14:20:01 +0200272}
273
274static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
275{
276 int i;
277
278 for (i = 0; i < 2; i++) {
279 dst->max_run[i] = le64_to_cpu(src->max_run[i]);
280 dst->min_run[i] = le64_to_cpu(src->min_run[i]);
281 dst->max_bw[i] = le64_to_cpu(src->max_bw[i]);
282 dst->min_bw[i] = le64_to_cpu(src->min_bw[i]);
283 dst->io_kb[i] = le64_to_cpu(src->io_kb[i]);
284 dst->agg[i] = le64_to_cpu(src->agg[i]);
285 }
286
287 dst->kb_base = le32_to_cpu(src->kb_base);
288 dst->groupid = le32_to_cpu(src->groupid);
289}
290
291static void handle_ts(struct fio_net_cmd *cmd)
292{
293 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
294
295 convert_ts(&p->ts, &p->ts);
296 convert_gs(&p->rs, &p->rs);
297
298 show_thread_status(&p->ts, &p->rs);
299}
300
301static void handle_gs(struct fio_net_cmd *cmd)
302{
303 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
304
305 convert_gs(gs, gs);
306 show_group_stats(gs);
307}
308
Jens Axboeb66570d2011-10-01 11:11:35 -0600309static int handle_client(struct fio_client *client)
Jens Axboe37db14f2011-09-30 17:00:42 -0600310{
311 struct fio_net_cmd *cmd;
Jens Axboea450e492011-10-03 14:24:03 +0200312 int done = 0;
Jens Axboe37db14f2011-09-30 17:00:42 -0600313
Jens Axboea64e88d2011-10-03 14:20:01 +0200314 while ((cmd = fio_net_recv_cmd(client->fd)) != NULL) {
Jens Axboe46c48f12011-10-01 12:50:51 -0600315 dprint(FD_NET, "%s: got cmd op %d\n", client->hostname,
316 cmd->opcode);
317
Jens Axboea64e88d2011-10-03 14:20:01 +0200318 switch (cmd->opcode) {
319 case FIO_NET_CMD_ACK:
Jens Axboe37db14f2011-09-30 17:00:42 -0600320 free(cmd);
Jens Axboea64e88d2011-10-03 14:20:01 +0200321 break;
322 case FIO_NET_CMD_QUIT:
Jens Axboeb66570d2011-10-01 11:11:35 -0600323 remove_client(client);
Jens Axboe437377e2011-10-01 08:31:30 -0600324 free(cmd);
Jens Axboea450e492011-10-03 14:24:03 +0200325 done = 1;
Jens Axboe437377e2011-10-01 08:31:30 -0600326 break;
Jens Axboea64e88d2011-10-03 14:20:01 +0200327 case FIO_NET_CMD_TEXT:
328 fwrite(cmd->payload, cmd->pdu_len, 1, stdout);
329 fflush(stdout);
Jens Axboe37db14f2011-09-30 17:00:42 -0600330 free(cmd);
Jens Axboea64e88d2011-10-03 14:20:01 +0200331 break;
332 case FIO_NET_CMD_TS:
333 handle_ts(cmd);
334 free(cmd);
335 break;
336 case FIO_NET_CMD_GS:
337 handle_gs(cmd);
338 free(cmd);
339 break;
340 default:
341 log_err("fio: unknown client op: %d\n", cmd->opcode);
342 free(cmd);
343 break;
Jens Axboe37db14f2011-09-30 17:00:42 -0600344 }
Jens Axboea450e492011-10-03 14:24:03 +0200345
346 if (done)
347 break;
Jens Axboe37db14f2011-09-30 17:00:42 -0600348 }
349
350 return 0;
351}
Jens Axboeb66570d2011-10-01 11:11:35 -0600352
353int fio_handle_clients(void)
354{
355 struct fio_client *client;
356 struct flist_head *entry;
357 struct pollfd *pfds;
Jens Axboe82a4be12011-10-01 12:40:32 -0600358 int i, ret = 0;
Jens Axboeb66570d2011-10-01 11:11:35 -0600359
360 pfds = malloc(nr_clients * sizeof(struct pollfd));
361
Jens Axboeb66570d2011-10-01 11:11:35 -0600362 while (!exit_backend && nr_clients) {
Jens Axboe82a4be12011-10-01 12:40:32 -0600363 i = 0;
364 flist_for_each(entry, &client_list) {
365 client = flist_entry(entry, struct fio_client, list);
366
367 pfds[i].fd = client->fd;
368 pfds[i].events = POLLIN;
369 i++;
370 }
371
372 assert(i == nr_clients);
373
Jens Axboeb66570d2011-10-01 11:11:35 -0600374 ret = poll(pfds, nr_clients, 100);
375 if (ret < 0) {
376 if (errno == EINTR)
377 continue;
378 log_err("fio: poll clients: %s\n", strerror(errno));
379 break;
380 } else if (!ret)
381 continue;
382
383 for (i = 0; i < nr_clients; i++) {
384 if (!(pfds[i].revents & POLLIN))
385 continue;
386
387 client = find_client_by_fd(pfds[i].fd);
388 if (!client) {
389 log_err("fio: unknown client\n");
390 continue;
391 }
392 handle_client(client);
393 }
394 }
395
396 free(pfds);
Jens Axboeb66570d2011-10-01 11:11:35 -0600397 return 0;
398}