blob: 10d41e5b33cc738f06b25c20f074af090538cdb1 [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>
Jens Axboed05c4a02011-10-05 00:16:11 +020011#include <sys/socket.h>
Jens Axboe87aa8f12011-10-06 21:24:13 +020012#include <sys/un.h>
Jens Axboe132159a2011-09-30 15:01:32 -060013#include <netinet/in.h>
14#include <arpa/inet.h>
15#include <netdb.h>
Jens Axboe9e22ecb2011-10-04 14:50:21 +020016#include <signal.h>
Jens Axboe132159a2011-09-30 15:01:32 -060017
18#include "fio.h"
19#include "server.h"
Jens Axboeb66570d2011-10-01 11:11:35 -060020#include "flist.h"
Jens Axboe3c5f57e2011-10-06 12:37:50 +020021#include "hash.h"
Jens Axboe132159a2011-09-30 15:01:32 -060022
Jens Axboeb66570d2011-10-01 11:11:35 -060023struct fio_client {
24 struct flist_head list;
Jens Axboe3c5f57e2011-10-06 12:37:50 +020025 struct flist_head fd_hash_list;
26 struct flist_head name_hash_list;
Jens Axboeb66570d2011-10-01 11:11:35 -060027 struct sockaddr_in addr;
Jens Axboe87aa8f12011-10-06 21:24:13 +020028 struct sockaddr_un addr_un;
Jens Axboeb66570d2011-10-01 11:11:35 -060029 char *hostname;
30 int fd;
Jens Axboe81179ee2011-10-04 12:42:06 +020031
32 int state;
Jens Axboe17dd1762011-10-04 13:27:34 +020033 int skip_newline;
Jens Axboe87aa8f12011-10-06 21:24:13 +020034 int is_sock;
Jens Axboe81179ee2011-10-04 12:42:06 +020035
36 uint16_t argc;
37 char **argv;
38};
39
40enum {
Jens Axboe5c2857f2011-10-04 13:14:32 +020041 Client_created = 0,
Jens Axboe81179ee2011-10-04 12:42:06 +020042 Client_connected = 1,
43 Client_started = 2,
44 Client_stopped = 3,
Jens Axboe5c2857f2011-10-04 13:14:32 +020045 Client_exited = 4,
Jens Axboeb66570d2011-10-01 11:11:35 -060046};
47
48static FLIST_HEAD(client_list);
Jens Axboeb66570d2011-10-01 11:11:35 -060049
Jens Axboe3c5f57e2011-10-06 12:37:50 +020050#define FIO_CLIENT_HASH_BITS 7
51#define FIO_CLIENT_HASH_SZ (1 << FIO_CLIENT_HASH_BITS)
52#define FIO_CLIENT_HASH_MASK (FIO_CLIENT_HASH_SZ - 1)
53static struct flist_head client_fd_hash[FIO_CLIENT_HASH_SZ];
54static struct flist_head client_name_hash[FIO_CLIENT_HASH_SZ];
55
Jens Axboee951bdc2011-10-05 21:58:45 +020056static int handle_client(struct fio_client *client);
Jens Axboe0b8f30a2011-10-04 10:31:53 +020057
Jens Axboe3c5f57e2011-10-06 12:37:50 +020058static void fio_client_add_fd_hash(struct fio_client *client)
59{
60 int bucket = hash_long(client->fd, FIO_CLIENT_HASH_BITS);
61
62 bucket &= FIO_CLIENT_HASH_MASK;
63 flist_add(&client->fd_hash_list, &client_fd_hash[bucket]);
64}
65
66static void fio_client_remove_fd_hash(struct fio_client *client)
67{
68 if (!flist_empty(&client->fd_hash_list))
69 flist_del_init(&client->fd_hash_list);
70}
71
72static void fio_client_add_name_hash(struct fio_client *client)
73{
74 int bucket = jhash(client->hostname, strlen(client->hostname), 0);
75
76 bucket &= FIO_CLIENT_HASH_MASK;
77 flist_add(&client->name_hash_list, &client_name_hash[bucket]);
78}
79
80static void fio_client_remove_name_hash(struct fio_client *client)
81{
82 if (!flist_empty(&client->name_hash_list))
83 flist_del_init(&client->name_hash_list);
84}
85
86static void fio_init fio_client_hash_init(void)
87{
88 int i;
89
90 for (i = 0; i < FIO_CLIENT_HASH_SZ; i++) {
91 INIT_FLIST_HEAD(&client_fd_hash[i]);
92 INIT_FLIST_HEAD(&client_name_hash[i]);
93 }
94}
95
Jens Axboeb66570d2011-10-01 11:11:35 -060096static struct fio_client *find_client_by_fd(int fd)
97{
Jens Axboe3c5f57e2011-10-06 12:37:50 +020098 int bucket = hash_long(fd, FIO_CLIENT_HASH_BITS) & FIO_CLIENT_HASH_MASK;
Jens Axboeb66570d2011-10-01 11:11:35 -060099 struct fio_client *client;
100 struct flist_head *entry;
101
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200102 flist_for_each(entry, &client_fd_hash[bucket]) {
103 client = flist_entry(entry, struct fio_client, fd_hash_list);
Jens Axboeb66570d2011-10-01 11:11:35 -0600104
105 if (client->fd == fd)
106 return client;
107 }
108
109 return NULL;
110}
111
112static struct fio_client *find_client_by_name(const char *name)
113{
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200114 int bucket = jhash(name, strlen(name), 0) & FIO_CLIENT_HASH_BITS;
Jens Axboeb66570d2011-10-01 11:11:35 -0600115 struct fio_client *client;
116 struct flist_head *entry;
117
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200118 flist_for_each(entry, &client_name_hash[bucket]) {
119 client = flist_entry(entry, struct fio_client, name_hash_list);
Jens Axboeb66570d2011-10-01 11:11:35 -0600120
121 if (!strcmp(name, client->hostname))
122 return client;
123 }
124
125 return NULL;
126}
127
128static void remove_client(struct fio_client *client)
129{
Jens Axboe39e8e012011-10-04 13:46:08 +0200130 dprint(FD_NET, "client: removed <%s>\n", client->hostname);
Jens Axboeb66570d2011-10-01 11:11:35 -0600131 flist_del(&client->list);
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200132
133 fio_client_remove_fd_hash(client);
134 fio_client_remove_name_hash(client);
Jens Axboe81179ee2011-10-04 12:42:06 +0200135
Jens Axboeb66570d2011-10-01 11:11:35 -0600136 free(client->hostname);
Jens Axboe81179ee2011-10-04 12:42:06 +0200137 if (client->argv)
138 free(client->argv);
139
Jens Axboeb66570d2011-10-01 11:11:35 -0600140 free(client);
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200141 nr_clients--;
Jens Axboeb66570d2011-10-01 11:11:35 -0600142}
Jens Axboe132159a2011-09-30 15:01:32 -0600143
Jens Axboe7a4b8242011-10-05 17:35:15 +0200144static int __fio_client_add_cmd_option(struct fio_client *client,
145 const char *opt)
Jens Axboe81179ee2011-10-04 12:42:06 +0200146{
Jens Axboe39e8e012011-10-04 13:46:08 +0200147 int index;
148
Jens Axboe7a4b8242011-10-05 17:35:15 +0200149 if (client->argc == FIO_NET_CMD_JOBLINE_ARGV) {
150 log_err("fio: max cmd line number reached.\n");
151 log_err("fio: cmd line <%s> has been ignored.\n", opt);
152 return 1;
153 }
154
Jens Axboe39e8e012011-10-04 13:46:08 +0200155 index = client->argc++;
Jens Axboe81179ee2011-10-04 12:42:06 +0200156 client->argv = realloc(client->argv, sizeof(char *) * client->argc);
Jens Axboe39e8e012011-10-04 13:46:08 +0200157 client->argv[index] = strdup(opt);
158 dprint(FD_NET, "client: add cmd %d: %s\n", index, opt);
Jens Axboe7a4b8242011-10-05 17:35:15 +0200159 return 0;
Jens Axboe81179ee2011-10-04 12:42:06 +0200160}
161
Jens Axboe7a4b8242011-10-05 17:35:15 +0200162int fio_client_add_cmd_option(const char *hostname, const char *opt)
Jens Axboe81179ee2011-10-04 12:42:06 +0200163{
164 struct fio_client *client;
165
166 if (!hostname || !opt)
Jens Axboe7a4b8242011-10-05 17:35:15 +0200167 return 0;
Jens Axboe81179ee2011-10-04 12:42:06 +0200168
169 client = find_client_by_name(hostname);
170 if (!client) {
171 log_err("fio: unknown client %s\n", hostname);
Jens Axboe7a4b8242011-10-05 17:35:15 +0200172 return 1;
Jens Axboe81179ee2011-10-04 12:42:06 +0200173 }
174
Jens Axboe7a4b8242011-10-05 17:35:15 +0200175 return __fio_client_add_cmd_option(client, opt);
Jens Axboe81179ee2011-10-04 12:42:06 +0200176}
177
Jens Axboea37f69b2011-10-01 12:24:21 -0600178void fio_client_add(const char *hostname)
Jens Axboe132159a2011-09-30 15:01:32 -0600179{
Jens Axboeb66570d2011-10-01 11:11:35 -0600180 struct fio_client *client;
Jens Axboe132159a2011-09-30 15:01:32 -0600181
Jens Axboe39e8e012011-10-04 13:46:08 +0200182 dprint(FD_NET, "client: added <%s>\n", hostname);
Jens Axboeb66570d2011-10-01 11:11:35 -0600183 client = malloc(sizeof(*client));
Jens Axboea37f69b2011-10-01 12:24:21 -0600184 memset(client, 0, sizeof(*client));
Jens Axboe81179ee2011-10-04 12:42:06 +0200185
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200186 INIT_FLIST_HEAD(&client->list);
187 INIT_FLIST_HEAD(&client->fd_hash_list);
188 INIT_FLIST_HEAD(&client->name_hash_list);
189
Jens Axboe87aa8f12011-10-06 21:24:13 +0200190 if (!strncmp(hostname, "sock:", 5)) {
191 client->hostname = strdup(hostname + 5);
192 client->is_sock = 1;
193 } else
194 client->hostname = strdup(hostname);
195
Jens Axboea37f69b2011-10-01 12:24:21 -0600196 client->fd = -1;
Jens Axboe81179ee2011-10-04 12:42:06 +0200197
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200198 fio_client_add_name_hash(client);
199
Jens Axboe81179ee2011-10-04 12:42:06 +0200200 __fio_client_add_cmd_option(client, "fio");
201
Jens Axboea37f69b2011-10-01 12:24:21 -0600202 flist_add(&client->list, &client_list);
203 nr_clients++;
204}
205
Jens Axboe87aa8f12011-10-06 21:24:13 +0200206static int fio_client_connect_ip(struct fio_client *client)
Jens Axboea37f69b2011-10-01 12:24:21 -0600207{
208 int fd;
Jens Axboe132159a2011-09-30 15:01:32 -0600209
Jens Axboeb66570d2011-10-01 11:11:35 -0600210 client->addr.sin_family = AF_INET;
211 client->addr.sin_port = htons(fio_net_port);
212
Jens Axboea37f69b2011-10-01 12:24:21 -0600213 if (inet_aton(client->hostname, &client->addr.sin_addr) != 1) {
Jens Axboe132159a2011-09-30 15:01:32 -0600214 struct hostent *hent;
215
Jens Axboea37f69b2011-10-01 12:24:21 -0600216 hent = gethostbyname(client->hostname);
Jens Axboe132159a2011-09-30 15:01:32 -0600217 if (!hent) {
218 log_err("fio: gethostbyname: %s\n", strerror(errno));
Jens Axboee951bdc2011-10-05 21:58:45 +0200219 log_err("fio: failed looking up hostname %s\n",
220 client->hostname);
Jens Axboe87aa8f12011-10-06 21:24:13 +0200221 return -1;
Jens Axboe132159a2011-09-30 15:01:32 -0600222 }
223
Jens Axboeb66570d2011-10-01 11:11:35 -0600224 memcpy(&client->addr.sin_addr, hent->h_addr, 4);
Jens Axboe132159a2011-09-30 15:01:32 -0600225 }
226
227 fd = socket(AF_INET, SOCK_STREAM, 0);
228 if (fd < 0) {
229 log_err("fio: socket: %s\n", strerror(errno));
Jens Axboe87aa8f12011-10-06 21:24:13 +0200230 return -1;
Jens Axboe132159a2011-09-30 15:01:32 -0600231 }
232
Jens Axboeb66570d2011-10-01 11:11:35 -0600233 if (connect(fd, (struct sockaddr *) &client->addr, sizeof(client->addr)) < 0) {
Jens Axboe132159a2011-09-30 15:01:32 -0600234 log_err("fio: connect: %s\n", strerror(errno));
Jens Axboecdf54d82011-10-04 08:31:40 +0200235 log_err("fio: failed to connect to %s\n", client->hostname);
Jens Axboeb94cba42011-10-06 21:27:10 +0200236 close(fd);
Jens Axboe87aa8f12011-10-06 21:24:13 +0200237 return -1;
Jens Axboe132159a2011-09-30 15:01:32 -0600238 }
239
Jens Axboe87aa8f12011-10-06 21:24:13 +0200240 return fd;
241}
242
243static int fio_client_connect_sock(struct fio_client *client)
244{
245 struct sockaddr_un *addr = &client->addr_un;
246 fio_socklen_t len;
247 int fd;
248
249 memset(addr, 0, sizeof(*addr));
250 addr->sun_family = AF_UNIX;
251 strcpy(addr->sun_path, client->hostname);
252
253 fd = socket(AF_UNIX, SOCK_STREAM, 0);
254 if (fd < 0) {
255 log_err("fio: socket: %s\n", strerror(errno));
256 return -1;
257 }
258
259 len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
260 if (connect(fd, (struct sockaddr *) addr, len) < 0) {
261 log_err("fio: connect; %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200262 close(fd);
Jens Axboe87aa8f12011-10-06 21:24:13 +0200263 return -1;
264 }
265
266 return fd;
267}
268
269static int fio_client_connect(struct fio_client *client)
270{
271 int fd;
272
273 dprint(FD_NET, "client: connect to host %s\n", client->hostname);
274
275 memset(&client->addr, 0, sizeof(client->addr));
276
277 if (client->is_sock)
278 fd = fio_client_connect_sock(client);
279 else
280 fd = fio_client_connect_ip(client);
281
282 if (fd < 0)
283 return 1;
284
Jens Axboeb66570d2011-10-01 11:11:35 -0600285 client->fd = fd;
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200286 fio_client_add_fd_hash(client);
Jens Axboe81179ee2011-10-04 12:42:06 +0200287 client->state = Client_connected;
Jens Axboe132159a2011-09-30 15:01:32 -0600288 return 0;
289}
290
Jens Axboecc0df002011-10-03 20:53:32 +0200291void fio_clients_terminate(void)
292{
293 struct flist_head *entry;
294 struct fio_client *client;
295
Jens Axboe60efd142011-10-04 13:30:11 +0200296 dprint(FD_NET, "client: terminate clients\n");
297
Jens Axboecc0df002011-10-03 20:53:32 +0200298 flist_for_each(entry, &client_list) {
299 client = flist_entry(entry, struct fio_client, list);
300
301 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_QUIT, 0);
302 }
303}
304
305static void sig_int(int sig)
306{
Jens Axboe60efd142011-10-04 13:30:11 +0200307 dprint(FD_NET, "client: got sign %d\n", sig);
Jens Axboecc0df002011-10-03 20:53:32 +0200308 fio_clients_terminate();
309}
310
311static void client_signal_handler(void)
312{
313 struct sigaction act;
314
315 memset(&act, 0, sizeof(act));
316 act.sa_handler = sig_int;
317 act.sa_flags = SA_RESTART;
318 sigaction(SIGINT, &act, NULL);
319
320 memset(&act, 0, sizeof(act));
321 act.sa_handler = sig_int;
322 act.sa_flags = SA_RESTART;
323 sigaction(SIGTERM, &act, NULL);
324}
325
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200326static void probe_client(struct fio_client *client)
327{
Jens Axboe60efd142011-10-04 13:30:11 +0200328 dprint(FD_NET, "client: send probe\n");
329
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200330 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_PROBE, 0);
Jens Axboee951bdc2011-10-05 21:58:45 +0200331 handle_client(client);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200332}
333
Jens Axboe81179ee2011-10-04 12:42:06 +0200334static int send_client_cmd_line(struct fio_client *client)
335{
336 struct cmd_line_pdu *pdu;
337 int i, ret;
338
Jens Axboe39e8e012011-10-04 13:46:08 +0200339 dprint(FD_NET, "client: send cmdline %d\n", client->argc);
Jens Axboe60efd142011-10-04 13:30:11 +0200340
Jens Axboe81179ee2011-10-04 12:42:06 +0200341 pdu = malloc(sizeof(*pdu));
342 for (i = 0; i < client->argc; i++)
343 strcpy((char *) pdu->argv[i], client->argv[i]);
344
345 pdu->argc = cpu_to_le16(client->argc);
346 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOBLINE, pdu, sizeof(*pdu));
347 free(pdu);
348 return ret;
349}
350
Jens Axboea37f69b2011-10-01 12:24:21 -0600351int fio_clients_connect(void)
352{
353 struct fio_client *client;
354 struct flist_head *entry, *tmp;
355 int ret;
356
Jens Axboe60efd142011-10-04 13:30:11 +0200357 dprint(FD_NET, "client: connect all\n");
358
Jens Axboecc0df002011-10-03 20:53:32 +0200359 client_signal_handler();
360
Jens Axboea37f69b2011-10-01 12:24:21 -0600361 flist_for_each_safe(entry, tmp, &client_list) {
362 client = flist_entry(entry, struct fio_client, list);
363
364 ret = fio_client_connect(client);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200365 if (ret) {
Jens Axboea37f69b2011-10-01 12:24:21 -0600366 remove_client(client);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200367 continue;
368 }
369
370 probe_client(client);
Jens Axboe81179ee2011-10-04 12:42:06 +0200371
372 if (client->argc > 1)
373 send_client_cmd_line(client);
Jens Axboea37f69b2011-10-01 12:24:21 -0600374 }
375
376 return !nr_clients;
377}
378
Jens Axboe132159a2011-09-30 15:01:32 -0600379/*
380 * Send file contents to server backend. We could use sendfile(), but to remain
381 * more portable lets just read/write the darn thing.
382 */
Jens Axboea37f69b2011-10-01 12:24:21 -0600383static int fio_client_send_ini(struct fio_client *client, const char *filename)
Jens Axboe132159a2011-09-30 15:01:32 -0600384{
385 struct stat sb;
386 char *p, *buf;
387 off_t len;
388 int fd, ret;
389
Jens Axboe46c48f12011-10-01 12:50:51 -0600390 dprint(FD_NET, "send ini %s to %s\n", filename, client->hostname);
391
Jens Axboe132159a2011-09-30 15:01:32 -0600392 fd = open(filename, O_RDONLY);
393 if (fd < 0) {
Jens Axboee951bdc2011-10-05 21:58:45 +0200394 log_err("fio: job file <%s> open: %s\n", filename, strerror(errno));
Jens Axboe132159a2011-09-30 15:01:32 -0600395 return 1;
396 }
397
398 if (fstat(fd, &sb) < 0) {
399 log_err("fio: job file stat: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200400 close(fd);
Jens Axboe132159a2011-09-30 15:01:32 -0600401 return 1;
402 }
403
404 buf = malloc(sb.st_size);
405
406 len = sb.st_size;
407 p = buf;
408 do {
409 ret = read(fd, p, len);
410 if (ret > 0) {
411 len -= ret;
412 if (!len)
413 break;
414 p += ret;
415 continue;
416 } else if (!ret)
417 break;
418 else if (errno == EAGAIN || errno == EINTR)
419 continue;
420 } while (1);
421
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200422 if (len) {
423 log_err("fio: failed reading job file %s\n", filename);
Jens Axboeb94cba42011-10-06 21:27:10 +0200424 close(fd);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200425 return 1;
426 }
427
Jens Axboe81179ee2011-10-04 12:42:06 +0200428 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, buf, sb.st_size);
Jens Axboe132159a2011-09-30 15:01:32 -0600429 free(buf);
Jens Axboeb94cba42011-10-06 21:27:10 +0200430 close(fd);
Jens Axboe132159a2011-09-30 15:01:32 -0600431 return ret;
432}
Jens Axboe37db14f2011-09-30 17:00:42 -0600433
Jens Axboea37f69b2011-10-01 12:24:21 -0600434int fio_clients_send_ini(const char *filename)
435{
436 struct fio_client *client;
437 struct flist_head *entry, *tmp;
438
439 flist_for_each_safe(entry, tmp, &client_list) {
440 client = flist_entry(entry, struct fio_client, list);
441
442 if (fio_client_send_ini(client, filename))
443 remove_client(client);
444 }
445
446 return !nr_clients;
447}
448
Jens Axboea64e88d2011-10-03 14:20:01 +0200449static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
450{
451 dst->max_val = le64_to_cpu(src->max_val);
452 dst->min_val = le64_to_cpu(src->min_val);
453 dst->samples = le64_to_cpu(src->samples);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200454
455 /*
456 * Floats arrive as IEEE 754 encoded uint64_t, convert back to double
457 */
458 dst->mean.u.f = fio_uint64_to_double(le64_to_cpu(dst->mean.u.i));
459 dst->S.u.f = fio_uint64_to_double(le64_to_cpu(dst->S.u.i));
Jens Axboea64e88d2011-10-03 14:20:01 +0200460}
461
462static void convert_ts(struct thread_stat *dst, struct thread_stat *src)
463{
464 int i, j;
465
466 dst->error = le32_to_cpu(src->error);
467 dst->groupid = le32_to_cpu(src->groupid);
468 dst->pid = le32_to_cpu(src->pid);
469 dst->members = le32_to_cpu(src->members);
470
471 for (i = 0; i < 2; i++) {
472 convert_io_stat(&dst->clat_stat[i], &src->clat_stat[i]);
473 convert_io_stat(&dst->slat_stat[i], &src->slat_stat[i]);
474 convert_io_stat(&dst->lat_stat[i], &src->lat_stat[i]);
475 convert_io_stat(&dst->bw_stat[i], &src->bw_stat[i]);
476 }
477
478 dst->usr_time = le64_to_cpu(src->usr_time);
479 dst->sys_time = le64_to_cpu(src->sys_time);
480 dst->ctx = le64_to_cpu(src->ctx);
481 dst->minf = le64_to_cpu(src->minf);
482 dst->majf = le64_to_cpu(src->majf);
483 dst->clat_percentiles = le64_to_cpu(src->clat_percentiles);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200484
485 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
486 fio_fp64_t *fps = &src->percentile_list[i];
487 fio_fp64_t *fpd = &dst->percentile_list[i];
488
489 fpd->u.f = fio_uint64_to_double(le64_to_cpu(fps->u.i));
490 }
Jens Axboea64e88d2011-10-03 14:20:01 +0200491
492 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
493 dst->io_u_map[i] = le32_to_cpu(src->io_u_map[i]);
494 dst->io_u_submit[i] = le32_to_cpu(src->io_u_submit[i]);
495 dst->io_u_complete[i] = le32_to_cpu(src->io_u_complete[i]);
496 }
497
498 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
499 dst->io_u_lat_u[i] = le32_to_cpu(src->io_u_lat_u[i]);
500 dst->io_u_lat_m[i] = le32_to_cpu(src->io_u_lat_m[i]);
501 }
502
503 for (i = 0; i < 2; i++)
504 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
505 dst->io_u_plat[i][j] = le32_to_cpu(src->io_u_plat[i][j]);
506
507 for (i = 0; i < 3; i++) {
508 dst->total_io_u[i] = le64_to_cpu(src->total_io_u[i]);
Jens Axboe93eee042011-10-03 21:08:48 +0200509 dst->short_io_u[i] = le64_to_cpu(src->short_io_u[i]);
Jens Axboea64e88d2011-10-03 14:20:01 +0200510 }
511
512 dst->total_submit = le64_to_cpu(src->total_submit);
513 dst->total_complete = le64_to_cpu(src->total_complete);
514
515 for (i = 0; i < 2; i++) {
516 dst->io_bytes[i] = le64_to_cpu(src->io_bytes[i]);
517 dst->runtime[i] = le64_to_cpu(src->runtime[i]);
518 }
519
520 dst->total_run_time = le64_to_cpu(src->total_run_time);
521 dst->continue_on_error = le16_to_cpu(src->continue_on_error);
522 dst->total_err_count = le64_to_cpu(src->total_err_count);
Jens Axboeddcc0b62011-10-03 14:45:27 +0200523 dst->first_error = le32_to_cpu(src->first_error);
524 dst->kb_base = le32_to_cpu(src->kb_base);
Jens Axboea64e88d2011-10-03 14:20:01 +0200525}
526
527static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
528{
529 int i;
530
531 for (i = 0; i < 2; i++) {
532 dst->max_run[i] = le64_to_cpu(src->max_run[i]);
533 dst->min_run[i] = le64_to_cpu(src->min_run[i]);
534 dst->max_bw[i] = le64_to_cpu(src->max_bw[i]);
535 dst->min_bw[i] = le64_to_cpu(src->min_bw[i]);
536 dst->io_kb[i] = le64_to_cpu(src->io_kb[i]);
537 dst->agg[i] = le64_to_cpu(src->agg[i]);
538 }
539
540 dst->kb_base = le32_to_cpu(src->kb_base);
541 dst->groupid = le32_to_cpu(src->groupid);
542}
543
544static void handle_ts(struct fio_net_cmd *cmd)
545{
546 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
547
548 convert_ts(&p->ts, &p->ts);
549 convert_gs(&p->rs, &p->rs);
550
551 show_thread_status(&p->ts, &p->rs);
552}
553
554static void handle_gs(struct fio_net_cmd *cmd)
555{
556 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
557
558 convert_gs(gs, gs);
559 show_group_stats(gs);
560}
561
Jens Axboecf451d12011-10-03 16:48:30 +0200562static void handle_eta(struct fio_net_cmd *cmd)
563{
564 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
565 int i;
566
567 je->nr_running = le32_to_cpu(je->nr_running);
568 je->nr_ramp = le32_to_cpu(je->nr_ramp);
569 je->nr_pending = le32_to_cpu(je->nr_pending);
570 je->files_open = le32_to_cpu(je->files_open);
571 je->m_rate = le32_to_cpu(je->m_rate);
572 je->t_rate = le32_to_cpu(je->t_rate);
573 je->m_iops = le32_to_cpu(je->m_iops);
574 je->t_iops = le32_to_cpu(je->t_iops);
575
576 for (i = 0; i < 2; i++) {
577 je->rate[i] = le32_to_cpu(je->rate[i]);
578 je->iops[i] = le32_to_cpu(je->iops[i]);
579 }
580
581 je->elapsed_sec = le32_to_cpu(je->nr_running);
582 je->eta_sec = le64_to_cpu(je->eta_sec);
583
584 display_thread_status(je);
585}
586
Jens Axboe2e03b4b2011-10-04 09:00:40 +0200587static void handle_probe(struct fio_net_cmd *cmd)
588{
589 struct cmd_probe_pdu *probe = (struct cmd_probe_pdu *) cmd->payload;
590
Jens Axboe6eb24792011-10-04 15:00:30 +0200591 log_info("Probe: hostname=%s, be=%u, fio ver %u.%u.%u\n",
592 probe->hostname, probe->bigendian, probe->fio_major,
593 probe->fio_minor, probe->fio_patch);
Jens Axboe2e03b4b2011-10-04 09:00:40 +0200594}
595
Jens Axboee951bdc2011-10-05 21:58:45 +0200596static int handle_client(struct fio_client *client)
Jens Axboe37db14f2011-09-30 17:00:42 -0600597{
598 struct fio_net_cmd *cmd;
599
Jens Axboe60efd142011-10-04 13:30:11 +0200600 dprint(FD_NET, "client: handle %s\n", client->hostname);
601
Jens Axboee951bdc2011-10-05 21:58:45 +0200602 cmd = fio_net_recv_cmd(client->fd);
603 if (!cmd)
604 return 0;
Jens Axboec2c94582011-10-05 20:31:30 +0200605
Jens Axboee951bdc2011-10-05 21:58:45 +0200606 dprint(FD_NET, "client: got cmd op %d from %s\n",
Jens Axboec2c94582011-10-05 20:31:30 +0200607 cmd->opcode, client->hostname);
Jens Axboe46c48f12011-10-01 12:50:51 -0600608
Jens Axboee951bdc2011-10-05 21:58:45 +0200609 switch (cmd->opcode) {
610 case FIO_NET_CMD_QUIT:
611 remove_client(client);
612 free(cmd);
613 break;
614 case FIO_NET_CMD_TEXT: {
615 const char *buf = (const char *) cmd->payload;
616 int fio_unused ret;
Jens Axboe17dd1762011-10-04 13:27:34 +0200617
Jens Axboee951bdc2011-10-05 21:58:45 +0200618 if (!client->skip_newline)
619 fprintf(f_out, "<%s> ", client->hostname);
620 ret = fwrite(buf, cmd->pdu_len, 1, f_out);
621 fflush(f_out);
622 client->skip_newline = strchr(buf, '\n') == NULL;
623 free(cmd);
624 break;
Jens Axboe37db14f2011-09-30 17:00:42 -0600625 }
Jens Axboee951bdc2011-10-05 21:58:45 +0200626 case FIO_NET_CMD_TS:
627 handle_ts(cmd);
628 free(cmd);
629 break;
630 case FIO_NET_CMD_GS:
631 handle_gs(cmd);
632 free(cmd);
633 break;
634 case FIO_NET_CMD_ETA:
635 handle_eta(cmd);
636 free(cmd);
637 break;
638 case FIO_NET_CMD_PROBE:
639 handle_probe(cmd);
640 free(cmd);
641 break;
642 case FIO_NET_CMD_START:
643 client->state = Client_started;
644 free(cmd);
645 break;
646 case FIO_NET_CMD_STOP:
647 client->state = Client_stopped;
648 free(cmd);
649 break;
650 default:
651 log_err("fio: unknown client op: %d\n", cmd->opcode);
652 free(cmd);
653 break;
Jens Axboe37db14f2011-09-30 17:00:42 -0600654 }
655
Jens Axboee951bdc2011-10-05 21:58:45 +0200656 return 1;
Jens Axboe37db14f2011-09-30 17:00:42 -0600657}
Jens Axboeb66570d2011-10-01 11:11:35 -0600658
659int fio_handle_clients(void)
660{
661 struct fio_client *client;
662 struct flist_head *entry;
663 struct pollfd *pfds;
Jens Axboe82a4be12011-10-01 12:40:32 -0600664 int i, ret = 0;
Jens Axboeb66570d2011-10-01 11:11:35 -0600665
666 pfds = malloc(nr_clients * sizeof(struct pollfd));
667
Jens Axboeb66570d2011-10-01 11:11:35 -0600668 while (!exit_backend && nr_clients) {
Jens Axboe82a4be12011-10-01 12:40:32 -0600669 i = 0;
670 flist_for_each(entry, &client_list) {
671 client = flist_entry(entry, struct fio_client, list);
672
673 pfds[i].fd = client->fd;
674 pfds[i].events = POLLIN;
675 i++;
676 }
677
678 assert(i == nr_clients);
679
Jens Axboe5c2857f2011-10-04 13:14:32 +0200680 do {
681 ret = poll(pfds, nr_clients, 100);
682 if (ret < 0) {
683 if (errno == EINTR)
684 continue;
685 log_err("fio: poll clients: %s\n", strerror(errno));
686 break;
687 } else if (!ret)
Jens Axboeb66570d2011-10-01 11:11:35 -0600688 continue;
Jens Axboe5c2857f2011-10-04 13:14:32 +0200689 } while (ret <= 0);
Jens Axboeb66570d2011-10-01 11:11:35 -0600690
691 for (i = 0; i < nr_clients; i++) {
692 if (!(pfds[i].revents & POLLIN))
693 continue;
694
695 client = find_client_by_fd(pfds[i].fd);
696 if (!client) {
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200697 log_err("fio: unknown client fd %d\n", pfds[i].fd);
Jens Axboeb66570d2011-10-01 11:11:35 -0600698 continue;
699 }
Jens Axboee951bdc2011-10-05 21:58:45 +0200700 if (!handle_client(client)) {
Jens Axboe28d3ab02011-10-05 20:45:37 +0200701 log_info("client: host=%s disconnected\n",
702 client->hostname);
703 remove_client(client);
704 }
Jens Axboeb66570d2011-10-01 11:11:35 -0600705 }
706 }
707
708 free(pfds);
Jens Axboeb66570d2011-10-01 11:11:35 -0600709 return 0;
710}