blob: 8bd39a8bc7886990f835e4f89fa9dae94df46429 [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 Axboebebe6392011-10-07 10:00:51 +020025 struct flist_head hash_list;
Jens Axboeb66570d2011-10-01 11:11:35 -060026 struct sockaddr_in addr;
Jens Axboe87aa8f12011-10-06 21:24:13 +020027 struct sockaddr_un addr_un;
Jens Axboeb66570d2011-10-01 11:11:35 -060028 char *hostname;
Jens Axboebebe6392011-10-07 10:00:51 +020029 int port;
Jens Axboeb66570d2011-10-01 11:11:35 -060030 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)
Jens Axboebebe6392011-10-07 10:00:51 +020053static struct flist_head client_hash[FIO_CLIENT_HASH_SZ];
Jens Axboe3c5f57e2011-10-06 12:37:50 +020054
Jens Axboee951bdc2011-10-05 21:58:45 +020055static int handle_client(struct fio_client *client);
Jens Axboe0b8f30a2011-10-04 10:31:53 +020056
Jens Axboebebe6392011-10-07 10:00:51 +020057static void fio_client_add_hash(struct fio_client *client)
Jens Axboe3c5f57e2011-10-06 12:37:50 +020058{
59 int bucket = hash_long(client->fd, FIO_CLIENT_HASH_BITS);
60
61 bucket &= FIO_CLIENT_HASH_MASK;
Jens Axboebebe6392011-10-07 10:00:51 +020062 flist_add(&client->hash_list, &client_hash[bucket]);
Jens Axboe3c5f57e2011-10-06 12:37:50 +020063}
64
Jens Axboebebe6392011-10-07 10:00:51 +020065static void fio_client_remove_hash(struct fio_client *client)
Jens Axboe3c5f57e2011-10-06 12:37:50 +020066{
Jens Axboebebe6392011-10-07 10:00:51 +020067 if (!flist_empty(&client->hash_list))
68 flist_del_init(&client->hash_list);
Jens Axboe3c5f57e2011-10-06 12:37:50 +020069}
70
71static void fio_init fio_client_hash_init(void)
72{
73 int i;
74
Jens Axboebebe6392011-10-07 10:00:51 +020075 for (i = 0; i < FIO_CLIENT_HASH_SZ; i++)
76 INIT_FLIST_HEAD(&client_hash[i]);
Jens Axboe3c5f57e2011-10-06 12:37:50 +020077}
78
Jens Axboeb66570d2011-10-01 11:11:35 -060079static struct fio_client *find_client_by_fd(int fd)
80{
Jens Axboe3c5f57e2011-10-06 12:37:50 +020081 int bucket = hash_long(fd, FIO_CLIENT_HASH_BITS) & FIO_CLIENT_HASH_MASK;
Jens Axboeb66570d2011-10-01 11:11:35 -060082 struct fio_client *client;
83 struct flist_head *entry;
84
Jens Axboebebe6392011-10-07 10:00:51 +020085 flist_for_each(entry, &client_hash[bucket]) {
86 client = flist_entry(entry, struct fio_client, hash_list);
Jens Axboeb66570d2011-10-01 11:11:35 -060087
88 if (client->fd == fd)
89 return client;
90 }
91
92 return NULL;
93}
94
Jens Axboeb66570d2011-10-01 11:11:35 -060095static void remove_client(struct fio_client *client)
96{
Jens Axboe39e8e012011-10-04 13:46:08 +020097 dprint(FD_NET, "client: removed <%s>\n", client->hostname);
Jens Axboeb66570d2011-10-01 11:11:35 -060098 flist_del(&client->list);
Jens Axboe3c5f57e2011-10-06 12:37:50 +020099
Jens Axboebebe6392011-10-07 10:00:51 +0200100 fio_client_remove_hash(client);
Jens Axboe81179ee2011-10-04 12:42:06 +0200101
Jens Axboeb66570d2011-10-01 11:11:35 -0600102 free(client->hostname);
Jens Axboe81179ee2011-10-04 12:42:06 +0200103 if (client->argv)
104 free(client->argv);
105
Jens Axboeb66570d2011-10-01 11:11:35 -0600106 free(client);
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200107 nr_clients--;
Jens Axboeb66570d2011-10-01 11:11:35 -0600108}
Jens Axboe132159a2011-09-30 15:01:32 -0600109
Jens Axboe7a4b8242011-10-05 17:35:15 +0200110static int __fio_client_add_cmd_option(struct fio_client *client,
111 const char *opt)
Jens Axboe81179ee2011-10-04 12:42:06 +0200112{
Jens Axboe39e8e012011-10-04 13:46:08 +0200113 int index;
114
Jens Axboe7a4b8242011-10-05 17:35:15 +0200115 if (client->argc == FIO_NET_CMD_JOBLINE_ARGV) {
116 log_err("fio: max cmd line number reached.\n");
117 log_err("fio: cmd line <%s> has been ignored.\n", opt);
118 return 1;
119 }
120
Jens Axboe39e8e012011-10-04 13:46:08 +0200121 index = client->argc++;
Jens Axboe81179ee2011-10-04 12:42:06 +0200122 client->argv = realloc(client->argv, sizeof(char *) * client->argc);
Jens Axboe39e8e012011-10-04 13:46:08 +0200123 client->argv[index] = strdup(opt);
124 dprint(FD_NET, "client: add cmd %d: %s\n", index, opt);
Jens Axboe7a4b8242011-10-05 17:35:15 +0200125 return 0;
Jens Axboe81179ee2011-10-04 12:42:06 +0200126}
127
Jens Axboebebe6392011-10-07 10:00:51 +0200128int fio_client_add_cmd_option(void *cookie, const char *opt)
Jens Axboe81179ee2011-10-04 12:42:06 +0200129{
Jens Axboebebe6392011-10-07 10:00:51 +0200130 struct fio_client *client = cookie;
Jens Axboe81179ee2011-10-04 12:42:06 +0200131
Jens Axboebebe6392011-10-07 10:00:51 +0200132 if (!client || !opt)
Jens Axboe7a4b8242011-10-05 17:35:15 +0200133 return 0;
Jens Axboe81179ee2011-10-04 12:42:06 +0200134
Jens Axboe7a4b8242011-10-05 17:35:15 +0200135 return __fio_client_add_cmd_option(client, opt);
Jens Axboe81179ee2011-10-04 12:42:06 +0200136}
137
Jens Axboebebe6392011-10-07 10:00:51 +0200138int fio_client_add(const char *hostname, void **cookie)
Jens Axboe132159a2011-09-30 15:01:32 -0600139{
Jens Axboeb66570d2011-10-01 11:11:35 -0600140 struct fio_client *client;
Jens Axboe132159a2011-09-30 15:01:32 -0600141
Jens Axboeb66570d2011-10-01 11:11:35 -0600142 client = malloc(sizeof(*client));
Jens Axboea37f69b2011-10-01 12:24:21 -0600143 memset(client, 0, sizeof(*client));
Jens Axboe81179ee2011-10-04 12:42:06 +0200144
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200145 INIT_FLIST_HEAD(&client->list);
Jens Axboebebe6392011-10-07 10:00:51 +0200146 INIT_FLIST_HEAD(&client->hash_list);
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200147
Jens Axboebebe6392011-10-07 10:00:51 +0200148 if (fio_server_parse_string(hostname, &client->hostname,
149 &client->is_sock, &client->port,
150 &client->addr.sin_addr))
151 return -1;
152
Jens Axboea37f69b2011-10-01 12:24:21 -0600153 client->fd = -1;
Jens Axboe81179ee2011-10-04 12:42:06 +0200154
155 __fio_client_add_cmd_option(client, "fio");
156
Jens Axboea37f69b2011-10-01 12:24:21 -0600157 flist_add(&client->list, &client_list);
158 nr_clients++;
Jens Axboebebe6392011-10-07 10:00:51 +0200159 dprint(FD_NET, "client: added <%s>\n", client->hostname);
160 *cookie = client;
161 return 0;
Jens Axboea37f69b2011-10-01 12:24:21 -0600162}
163
Jens Axboe87aa8f12011-10-06 21:24:13 +0200164static int fio_client_connect_ip(struct fio_client *client)
Jens Axboea37f69b2011-10-01 12:24:21 -0600165{
166 int fd;
Jens Axboe132159a2011-09-30 15:01:32 -0600167
Jens Axboeb66570d2011-10-01 11:11:35 -0600168 client->addr.sin_family = AF_INET;
Jens Axboebebe6392011-10-07 10:00:51 +0200169 client->addr.sin_port = htons(client->port);
Jens Axboe132159a2011-09-30 15:01:32 -0600170
171 fd = socket(AF_INET, SOCK_STREAM, 0);
172 if (fd < 0) {
173 log_err("fio: socket: %s\n", strerror(errno));
Jens Axboe87aa8f12011-10-06 21:24:13 +0200174 return -1;
Jens Axboe132159a2011-09-30 15:01:32 -0600175 }
176
Jens Axboeb66570d2011-10-01 11:11:35 -0600177 if (connect(fd, (struct sockaddr *) &client->addr, sizeof(client->addr)) < 0) {
Jens Axboe132159a2011-09-30 15:01:32 -0600178 log_err("fio: connect: %s\n", strerror(errno));
Jens Axboecdf54d82011-10-04 08:31:40 +0200179 log_err("fio: failed to connect to %s\n", client->hostname);
Jens Axboeb94cba42011-10-06 21:27:10 +0200180 close(fd);
Jens Axboe87aa8f12011-10-06 21:24:13 +0200181 return -1;
Jens Axboe132159a2011-09-30 15:01:32 -0600182 }
183
Jens Axboe87aa8f12011-10-06 21:24:13 +0200184 return fd;
185}
186
187static int fio_client_connect_sock(struct fio_client *client)
188{
189 struct sockaddr_un *addr = &client->addr_un;
190 fio_socklen_t len;
191 int fd;
192
193 memset(addr, 0, sizeof(*addr));
194 addr->sun_family = AF_UNIX;
195 strcpy(addr->sun_path, client->hostname);
196
197 fd = socket(AF_UNIX, SOCK_STREAM, 0);
198 if (fd < 0) {
199 log_err("fio: socket: %s\n", strerror(errno));
200 return -1;
201 }
202
203 len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
204 if (connect(fd, (struct sockaddr *) addr, len) < 0) {
205 log_err("fio: connect; %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200206 close(fd);
Jens Axboe87aa8f12011-10-06 21:24:13 +0200207 return -1;
208 }
209
210 return fd;
211}
212
213static int fio_client_connect(struct fio_client *client)
214{
215 int fd;
216
217 dprint(FD_NET, "client: connect to host %s\n", client->hostname);
218
219 memset(&client->addr, 0, sizeof(client->addr));
220
221 if (client->is_sock)
222 fd = fio_client_connect_sock(client);
223 else
224 fd = fio_client_connect_ip(client);
225
226 if (fd < 0)
227 return 1;
228
Jens Axboeb66570d2011-10-01 11:11:35 -0600229 client->fd = fd;
Jens Axboebebe6392011-10-07 10:00:51 +0200230 fio_client_add_hash(client);
Jens Axboe81179ee2011-10-04 12:42:06 +0200231 client->state = Client_connected;
Jens Axboe132159a2011-09-30 15:01:32 -0600232 return 0;
233}
234
Jens Axboecc0df002011-10-03 20:53:32 +0200235void fio_clients_terminate(void)
236{
237 struct flist_head *entry;
238 struct fio_client *client;
239
Jens Axboe60efd142011-10-04 13:30:11 +0200240 dprint(FD_NET, "client: terminate clients\n");
241
Jens Axboecc0df002011-10-03 20:53:32 +0200242 flist_for_each(entry, &client_list) {
243 client = flist_entry(entry, struct fio_client, list);
244
245 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_QUIT, 0);
246 }
247}
248
249static void sig_int(int sig)
250{
Jens Axboebebe6392011-10-07 10:00:51 +0200251 dprint(FD_NET, "client: got signal %d\n", sig);
Jens Axboecc0df002011-10-03 20:53:32 +0200252 fio_clients_terminate();
253}
254
255static void client_signal_handler(void)
256{
257 struct sigaction act;
258
259 memset(&act, 0, sizeof(act));
260 act.sa_handler = sig_int;
261 act.sa_flags = SA_RESTART;
262 sigaction(SIGINT, &act, NULL);
263
264 memset(&act, 0, sizeof(act));
265 act.sa_handler = sig_int;
266 act.sa_flags = SA_RESTART;
267 sigaction(SIGTERM, &act, NULL);
268}
269
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200270static void probe_client(struct fio_client *client)
271{
Jens Axboe60efd142011-10-04 13:30:11 +0200272 dprint(FD_NET, "client: send probe\n");
273
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200274 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_PROBE, 0);
Jens Axboee951bdc2011-10-05 21:58:45 +0200275 handle_client(client);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200276}
277
Jens Axboe81179ee2011-10-04 12:42:06 +0200278static int send_client_cmd_line(struct fio_client *client)
279{
280 struct cmd_line_pdu *pdu;
281 int i, ret;
282
Jens Axboe39e8e012011-10-04 13:46:08 +0200283 dprint(FD_NET, "client: send cmdline %d\n", client->argc);
Jens Axboe60efd142011-10-04 13:30:11 +0200284
Jens Axboe81179ee2011-10-04 12:42:06 +0200285 pdu = malloc(sizeof(*pdu));
286 for (i = 0; i < client->argc; i++)
287 strcpy((char *) pdu->argv[i], client->argv[i]);
288
289 pdu->argc = cpu_to_le16(client->argc);
290 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOBLINE, pdu, sizeof(*pdu));
291 free(pdu);
292 return ret;
293}
294
Jens Axboea37f69b2011-10-01 12:24:21 -0600295int fio_clients_connect(void)
296{
297 struct fio_client *client;
298 struct flist_head *entry, *tmp;
299 int ret;
300
Jens Axboe60efd142011-10-04 13:30:11 +0200301 dprint(FD_NET, "client: connect all\n");
302
Jens Axboecc0df002011-10-03 20:53:32 +0200303 client_signal_handler();
304
Jens Axboea37f69b2011-10-01 12:24:21 -0600305 flist_for_each_safe(entry, tmp, &client_list) {
306 client = flist_entry(entry, struct fio_client, list);
307
308 ret = fio_client_connect(client);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200309 if (ret) {
Jens Axboea37f69b2011-10-01 12:24:21 -0600310 remove_client(client);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200311 continue;
312 }
313
314 probe_client(client);
Jens Axboe81179ee2011-10-04 12:42:06 +0200315
316 if (client->argc > 1)
317 send_client_cmd_line(client);
Jens Axboea37f69b2011-10-01 12:24:21 -0600318 }
319
320 return !nr_clients;
321}
322
Jens Axboe132159a2011-09-30 15:01:32 -0600323/*
324 * Send file contents to server backend. We could use sendfile(), but to remain
325 * more portable lets just read/write the darn thing.
326 */
Jens Axboea37f69b2011-10-01 12:24:21 -0600327static int fio_client_send_ini(struct fio_client *client, const char *filename)
Jens Axboe132159a2011-09-30 15:01:32 -0600328{
329 struct stat sb;
330 char *p, *buf;
331 off_t len;
332 int fd, ret;
333
Jens Axboe46c48f12011-10-01 12:50:51 -0600334 dprint(FD_NET, "send ini %s to %s\n", filename, client->hostname);
335
Jens Axboe132159a2011-09-30 15:01:32 -0600336 fd = open(filename, O_RDONLY);
337 if (fd < 0) {
Jens Axboee951bdc2011-10-05 21:58:45 +0200338 log_err("fio: job file <%s> open: %s\n", filename, strerror(errno));
Jens Axboe132159a2011-09-30 15:01:32 -0600339 return 1;
340 }
341
342 if (fstat(fd, &sb) < 0) {
343 log_err("fio: job file stat: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200344 close(fd);
Jens Axboe132159a2011-09-30 15:01:32 -0600345 return 1;
346 }
347
348 buf = malloc(sb.st_size);
349
350 len = sb.st_size;
351 p = buf;
352 do {
353 ret = read(fd, p, len);
354 if (ret > 0) {
355 len -= ret;
356 if (!len)
357 break;
358 p += ret;
359 continue;
360 } else if (!ret)
361 break;
362 else if (errno == EAGAIN || errno == EINTR)
363 continue;
364 } while (1);
365
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200366 if (len) {
367 log_err("fio: failed reading job file %s\n", filename);
Jens Axboeb94cba42011-10-06 21:27:10 +0200368 close(fd);
Jens Axboec524ef72011-10-07 12:23:34 +0200369 free(buf);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200370 return 1;
371 }
372
Jens Axboe81179ee2011-10-04 12:42:06 +0200373 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, buf, sb.st_size);
Jens Axboe132159a2011-09-30 15:01:32 -0600374 free(buf);
Jens Axboeb94cba42011-10-06 21:27:10 +0200375 close(fd);
Jens Axboe132159a2011-09-30 15:01:32 -0600376 return ret;
377}
Jens Axboe37db14f2011-09-30 17:00:42 -0600378
Jens Axboea37f69b2011-10-01 12:24:21 -0600379int fio_clients_send_ini(const char *filename)
380{
381 struct fio_client *client;
382 struct flist_head *entry, *tmp;
383
384 flist_for_each_safe(entry, tmp, &client_list) {
385 client = flist_entry(entry, struct fio_client, list);
386
387 if (fio_client_send_ini(client, filename))
388 remove_client(client);
389 }
390
391 return !nr_clients;
392}
393
Jens Axboea64e88d2011-10-03 14:20:01 +0200394static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
395{
396 dst->max_val = le64_to_cpu(src->max_val);
397 dst->min_val = le64_to_cpu(src->min_val);
398 dst->samples = le64_to_cpu(src->samples);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200399
400 /*
401 * Floats arrive as IEEE 754 encoded uint64_t, convert back to double
402 */
403 dst->mean.u.f = fio_uint64_to_double(le64_to_cpu(dst->mean.u.i));
404 dst->S.u.f = fio_uint64_to_double(le64_to_cpu(dst->S.u.i));
Jens Axboea64e88d2011-10-03 14:20:01 +0200405}
406
407static void convert_ts(struct thread_stat *dst, struct thread_stat *src)
408{
409 int i, j;
410
411 dst->error = le32_to_cpu(src->error);
412 dst->groupid = le32_to_cpu(src->groupid);
413 dst->pid = le32_to_cpu(src->pid);
414 dst->members = le32_to_cpu(src->members);
415
416 for (i = 0; i < 2; i++) {
417 convert_io_stat(&dst->clat_stat[i], &src->clat_stat[i]);
418 convert_io_stat(&dst->slat_stat[i], &src->slat_stat[i]);
419 convert_io_stat(&dst->lat_stat[i], &src->lat_stat[i]);
420 convert_io_stat(&dst->bw_stat[i], &src->bw_stat[i]);
421 }
422
423 dst->usr_time = le64_to_cpu(src->usr_time);
424 dst->sys_time = le64_to_cpu(src->sys_time);
425 dst->ctx = le64_to_cpu(src->ctx);
426 dst->minf = le64_to_cpu(src->minf);
427 dst->majf = le64_to_cpu(src->majf);
428 dst->clat_percentiles = le64_to_cpu(src->clat_percentiles);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200429
430 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
431 fio_fp64_t *fps = &src->percentile_list[i];
432 fio_fp64_t *fpd = &dst->percentile_list[i];
433
434 fpd->u.f = fio_uint64_to_double(le64_to_cpu(fps->u.i));
435 }
Jens Axboea64e88d2011-10-03 14:20:01 +0200436
437 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
438 dst->io_u_map[i] = le32_to_cpu(src->io_u_map[i]);
439 dst->io_u_submit[i] = le32_to_cpu(src->io_u_submit[i]);
440 dst->io_u_complete[i] = le32_to_cpu(src->io_u_complete[i]);
441 }
442
443 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
444 dst->io_u_lat_u[i] = le32_to_cpu(src->io_u_lat_u[i]);
445 dst->io_u_lat_m[i] = le32_to_cpu(src->io_u_lat_m[i]);
446 }
447
448 for (i = 0; i < 2; i++)
449 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
450 dst->io_u_plat[i][j] = le32_to_cpu(src->io_u_plat[i][j]);
451
452 for (i = 0; i < 3; i++) {
453 dst->total_io_u[i] = le64_to_cpu(src->total_io_u[i]);
Jens Axboe93eee042011-10-03 21:08:48 +0200454 dst->short_io_u[i] = le64_to_cpu(src->short_io_u[i]);
Jens Axboea64e88d2011-10-03 14:20:01 +0200455 }
456
457 dst->total_submit = le64_to_cpu(src->total_submit);
458 dst->total_complete = le64_to_cpu(src->total_complete);
459
460 for (i = 0; i < 2; i++) {
461 dst->io_bytes[i] = le64_to_cpu(src->io_bytes[i]);
462 dst->runtime[i] = le64_to_cpu(src->runtime[i]);
463 }
464
465 dst->total_run_time = le64_to_cpu(src->total_run_time);
466 dst->continue_on_error = le16_to_cpu(src->continue_on_error);
467 dst->total_err_count = le64_to_cpu(src->total_err_count);
Jens Axboeddcc0b62011-10-03 14:45:27 +0200468 dst->first_error = le32_to_cpu(src->first_error);
469 dst->kb_base = le32_to_cpu(src->kb_base);
Jens Axboea64e88d2011-10-03 14:20:01 +0200470}
471
472static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
473{
474 int i;
475
476 for (i = 0; i < 2; i++) {
477 dst->max_run[i] = le64_to_cpu(src->max_run[i]);
478 dst->min_run[i] = le64_to_cpu(src->min_run[i]);
479 dst->max_bw[i] = le64_to_cpu(src->max_bw[i]);
480 dst->min_bw[i] = le64_to_cpu(src->min_bw[i]);
481 dst->io_kb[i] = le64_to_cpu(src->io_kb[i]);
482 dst->agg[i] = le64_to_cpu(src->agg[i]);
483 }
484
485 dst->kb_base = le32_to_cpu(src->kb_base);
486 dst->groupid = le32_to_cpu(src->groupid);
487}
488
489static void handle_ts(struct fio_net_cmd *cmd)
490{
491 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
492
493 convert_ts(&p->ts, &p->ts);
494 convert_gs(&p->rs, &p->rs);
495
496 show_thread_status(&p->ts, &p->rs);
497}
498
499static void handle_gs(struct fio_net_cmd *cmd)
500{
501 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
502
503 convert_gs(gs, gs);
504 show_group_stats(gs);
505}
506
Jens Axboecf451d12011-10-03 16:48:30 +0200507static void handle_eta(struct fio_net_cmd *cmd)
508{
509 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
510 int i;
511
512 je->nr_running = le32_to_cpu(je->nr_running);
513 je->nr_ramp = le32_to_cpu(je->nr_ramp);
514 je->nr_pending = le32_to_cpu(je->nr_pending);
515 je->files_open = le32_to_cpu(je->files_open);
516 je->m_rate = le32_to_cpu(je->m_rate);
517 je->t_rate = le32_to_cpu(je->t_rate);
518 je->m_iops = le32_to_cpu(je->m_iops);
519 je->t_iops = le32_to_cpu(je->t_iops);
520
521 for (i = 0; i < 2; i++) {
522 je->rate[i] = le32_to_cpu(je->rate[i]);
523 je->iops[i] = le32_to_cpu(je->iops[i]);
524 }
525
526 je->elapsed_sec = le32_to_cpu(je->nr_running);
527 je->eta_sec = le64_to_cpu(je->eta_sec);
528
529 display_thread_status(je);
530}
531
Jens Axboe2e03b4b2011-10-04 09:00:40 +0200532static void handle_probe(struct fio_net_cmd *cmd)
533{
534 struct cmd_probe_pdu *probe = (struct cmd_probe_pdu *) cmd->payload;
Jens Axboecca84642011-10-07 12:47:57 +0200535 const char *os, *arch;
Jens Axboe2e03b4b2011-10-04 09:00:40 +0200536
Jens Axboecca84642011-10-07 12:47:57 +0200537 os = fio_get_os_string(probe->os);
538 if (!os)
539 os = "unknown";
540
541 arch = fio_get_arch_string(probe->arch);
542 if (!arch)
543 os = "unknown";
544
545 log_info("hostname=%s, be=%u, os=%s, arch=%s, fio ver %u.%u.%u\n",
546 probe->hostname, probe->bigendian, os, arch, probe->fio_major,
Jens Axboe6eb24792011-10-04 15:00:30 +0200547 probe->fio_minor, probe->fio_patch);
Jens Axboe2e03b4b2011-10-04 09:00:40 +0200548}
549
Jens Axboee951bdc2011-10-05 21:58:45 +0200550static int handle_client(struct fio_client *client)
Jens Axboe37db14f2011-09-30 17:00:42 -0600551{
552 struct fio_net_cmd *cmd;
553
Jens Axboe60efd142011-10-04 13:30:11 +0200554 dprint(FD_NET, "client: handle %s\n", client->hostname);
555
Jens Axboee951bdc2011-10-05 21:58:45 +0200556 cmd = fio_net_recv_cmd(client->fd);
557 if (!cmd)
558 return 0;
Jens Axboec2c94582011-10-05 20:31:30 +0200559
Jens Axboee951bdc2011-10-05 21:58:45 +0200560 dprint(FD_NET, "client: got cmd op %d from %s\n",
Jens Axboec2c94582011-10-05 20:31:30 +0200561 cmd->opcode, client->hostname);
Jens Axboe46c48f12011-10-01 12:50:51 -0600562
Jens Axboee951bdc2011-10-05 21:58:45 +0200563 switch (cmd->opcode) {
564 case FIO_NET_CMD_QUIT:
565 remove_client(client);
566 free(cmd);
567 break;
568 case FIO_NET_CMD_TEXT: {
569 const char *buf = (const char *) cmd->payload;
570 int fio_unused ret;
Jens Axboe17dd1762011-10-04 13:27:34 +0200571
Jens Axboee951bdc2011-10-05 21:58:45 +0200572 if (!client->skip_newline)
573 fprintf(f_out, "<%s> ", client->hostname);
574 ret = fwrite(buf, cmd->pdu_len, 1, f_out);
575 fflush(f_out);
576 client->skip_newline = strchr(buf, '\n') == NULL;
577 free(cmd);
578 break;
Jens Axboe37db14f2011-09-30 17:00:42 -0600579 }
Jens Axboee951bdc2011-10-05 21:58:45 +0200580 case FIO_NET_CMD_TS:
581 handle_ts(cmd);
582 free(cmd);
583 break;
584 case FIO_NET_CMD_GS:
585 handle_gs(cmd);
586 free(cmd);
587 break;
588 case FIO_NET_CMD_ETA:
589 handle_eta(cmd);
590 free(cmd);
591 break;
592 case FIO_NET_CMD_PROBE:
593 handle_probe(cmd);
594 free(cmd);
595 break;
596 case FIO_NET_CMD_START:
597 client->state = Client_started;
598 free(cmd);
599 break;
600 case FIO_NET_CMD_STOP:
601 client->state = Client_stopped;
602 free(cmd);
603 break;
604 default:
605 log_err("fio: unknown client op: %d\n", cmd->opcode);
606 free(cmd);
607 break;
Jens Axboe37db14f2011-09-30 17:00:42 -0600608 }
609
Jens Axboee951bdc2011-10-05 21:58:45 +0200610 return 1;
Jens Axboe37db14f2011-09-30 17:00:42 -0600611}
Jens Axboeb66570d2011-10-01 11:11:35 -0600612
613int fio_handle_clients(void)
614{
615 struct fio_client *client;
616 struct flist_head *entry;
617 struct pollfd *pfds;
Jens Axboe82a4be12011-10-01 12:40:32 -0600618 int i, ret = 0;
Jens Axboeb66570d2011-10-01 11:11:35 -0600619
620 pfds = malloc(nr_clients * sizeof(struct pollfd));
621
Jens Axboeb66570d2011-10-01 11:11:35 -0600622 while (!exit_backend && nr_clients) {
Jens Axboe82a4be12011-10-01 12:40:32 -0600623 i = 0;
624 flist_for_each(entry, &client_list) {
625 client = flist_entry(entry, struct fio_client, list);
626
627 pfds[i].fd = client->fd;
628 pfds[i].events = POLLIN;
629 i++;
630 }
631
632 assert(i == nr_clients);
633
Jens Axboe5c2857f2011-10-04 13:14:32 +0200634 do {
635 ret = poll(pfds, nr_clients, 100);
636 if (ret < 0) {
637 if (errno == EINTR)
638 continue;
639 log_err("fio: poll clients: %s\n", strerror(errno));
640 break;
641 } else if (!ret)
Jens Axboeb66570d2011-10-01 11:11:35 -0600642 continue;
Jens Axboe5c2857f2011-10-04 13:14:32 +0200643 } while (ret <= 0);
Jens Axboeb66570d2011-10-01 11:11:35 -0600644
645 for (i = 0; i < nr_clients; i++) {
646 if (!(pfds[i].revents & POLLIN))
647 continue;
648
649 client = find_client_by_fd(pfds[i].fd);
650 if (!client) {
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200651 log_err("fio: unknown client fd %d\n", pfds[i].fd);
Jens Axboeb66570d2011-10-01 11:11:35 -0600652 continue;
653 }
Jens Axboee951bdc2011-10-05 21:58:45 +0200654 if (!handle_client(client)) {
Jens Axboe28d3ab02011-10-05 20:45:37 +0200655 log_info("client: host=%s disconnected\n",
656 client->hostname);
657 remove_client(client);
658 }
Jens Axboeb66570d2011-10-01 11:11:35 -0600659 }
660 }
661
662 free(pfds);
Jens Axboeb66570d2011-10-01 11:11:35 -0600663 return 0;
664}