blob: 76935ca597b2fd6b17ec2aaa304ccaaa596771a6 [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
Jens Axboeb5296dd2011-10-07 16:35:56 +020032 char *name;
33
Jens Axboe81179ee2011-10-04 12:42:06 +020034 int state;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +020035
Jens Axboe17dd1762011-10-04 13:27:34 +020036 int skip_newline;
Jens Axboe87aa8f12011-10-06 21:24:13 +020037 int is_sock;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +020038 int waiting_eta;
Jens Axboe81179ee2011-10-04 12:42:06 +020039
40 uint16_t argc;
41 char **argv;
42};
43
Jens Axboeaf9c9fb2011-10-09 21:54:10 +020044static struct timeval eta_tv;
Jens Axboe48fbb462011-10-09 12:19:08 +020045
Jens Axboe81179ee2011-10-04 12:42:06 +020046enum {
Jens Axboe5c2857f2011-10-04 13:14:32 +020047 Client_created = 0,
Jens Axboe81179ee2011-10-04 12:42:06 +020048 Client_connected = 1,
49 Client_started = 2,
50 Client_stopped = 3,
Jens Axboe5c2857f2011-10-04 13:14:32 +020051 Client_exited = 4,
Jens Axboeb66570d2011-10-01 11:11:35 -060052};
53
Jens Axboeaf9c9fb2011-10-09 21:54:10 +020054struct client_eta {
55 struct jobs_eta eta;
56 unsigned int pending;
57};
58
Jens Axboeb66570d2011-10-01 11:11:35 -060059static FLIST_HEAD(client_list);
Jens Axboeb66570d2011-10-01 11:11:35 -060060
Jens Axboe3c5f57e2011-10-06 12:37:50 +020061#define FIO_CLIENT_HASH_BITS 7
62#define FIO_CLIENT_HASH_SZ (1 << FIO_CLIENT_HASH_BITS)
63#define FIO_CLIENT_HASH_MASK (FIO_CLIENT_HASH_SZ - 1)
Jens Axboebebe6392011-10-07 10:00:51 +020064static struct flist_head client_hash[FIO_CLIENT_HASH_SZ];
Jens Axboe3c5f57e2011-10-06 12:37:50 +020065
Jens Axboee951bdc2011-10-05 21:58:45 +020066static int handle_client(struct fio_client *client);
Jens Axboe0b8f30a2011-10-04 10:31:53 +020067
Jens Axboebebe6392011-10-07 10:00:51 +020068static void fio_client_add_hash(struct fio_client *client)
Jens Axboe3c5f57e2011-10-06 12:37:50 +020069{
70 int bucket = hash_long(client->fd, FIO_CLIENT_HASH_BITS);
71
72 bucket &= FIO_CLIENT_HASH_MASK;
Jens Axboebebe6392011-10-07 10:00:51 +020073 flist_add(&client->hash_list, &client_hash[bucket]);
Jens Axboe3c5f57e2011-10-06 12:37:50 +020074}
75
Jens Axboebebe6392011-10-07 10:00:51 +020076static void fio_client_remove_hash(struct fio_client *client)
Jens Axboe3c5f57e2011-10-06 12:37:50 +020077{
Jens Axboebebe6392011-10-07 10:00:51 +020078 if (!flist_empty(&client->hash_list))
79 flist_del_init(&client->hash_list);
Jens Axboe3c5f57e2011-10-06 12:37:50 +020080}
81
82static void fio_init fio_client_hash_init(void)
83{
84 int i;
85
Jens Axboebebe6392011-10-07 10:00:51 +020086 for (i = 0; i < FIO_CLIENT_HASH_SZ; i++)
87 INIT_FLIST_HEAD(&client_hash[i]);
Jens Axboe3c5f57e2011-10-06 12:37:50 +020088}
89
Jens Axboeb66570d2011-10-01 11:11:35 -060090static struct fio_client *find_client_by_fd(int fd)
91{
Jens Axboe3c5f57e2011-10-06 12:37:50 +020092 int bucket = hash_long(fd, FIO_CLIENT_HASH_BITS) & FIO_CLIENT_HASH_MASK;
Jens Axboeb66570d2011-10-01 11:11:35 -060093 struct fio_client *client;
94 struct flist_head *entry;
95
Jens Axboebebe6392011-10-07 10:00:51 +020096 flist_for_each(entry, &client_hash[bucket]) {
97 client = flist_entry(entry, struct fio_client, hash_list);
Jens Axboeb66570d2011-10-01 11:11:35 -060098
99 if (client->fd == fd)
100 return client;
101 }
102
103 return NULL;
104}
105
Jens Axboeb66570d2011-10-01 11:11:35 -0600106static void remove_client(struct fio_client *client)
107{
Jens Axboe39e8e012011-10-04 13:46:08 +0200108 dprint(FD_NET, "client: removed <%s>\n", client->hostname);
Jens Axboeb66570d2011-10-01 11:11:35 -0600109 flist_del(&client->list);
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200110
Jens Axboebebe6392011-10-07 10:00:51 +0200111 fio_client_remove_hash(client);
Jens Axboe81179ee2011-10-04 12:42:06 +0200112
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200113 /* FIXME: check ->waiting_eta and handle it */
114
Jens Axboeb66570d2011-10-01 11:11:35 -0600115 free(client->hostname);
Jens Axboe81179ee2011-10-04 12:42:06 +0200116 if (client->argv)
117 free(client->argv);
Jens Axboeb5296dd2011-10-07 16:35:56 +0200118 if (client->name)
119 free(client->name);
Jens Axboe81179ee2011-10-04 12:42:06 +0200120
Jens Axboeb66570d2011-10-01 11:11:35 -0600121 free(client);
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200122 nr_clients--;
Jens Axboeb66570d2011-10-01 11:11:35 -0600123}
Jens Axboe132159a2011-09-30 15:01:32 -0600124
Jens Axboefa2ea802011-10-08 21:07:29 +0200125static void __fio_client_add_cmd_option(struct fio_client *client,
126 const char *opt)
Jens Axboe81179ee2011-10-04 12:42:06 +0200127{
Jens Axboe39e8e012011-10-04 13:46:08 +0200128 int index;
129
130 index = client->argc++;
Jens Axboe81179ee2011-10-04 12:42:06 +0200131 client->argv = realloc(client->argv, sizeof(char *) * client->argc);
Jens Axboe39e8e012011-10-04 13:46:08 +0200132 client->argv[index] = strdup(opt);
133 dprint(FD_NET, "client: add cmd %d: %s\n", index, opt);
Jens Axboe81179ee2011-10-04 12:42:06 +0200134}
135
Jens Axboefa2ea802011-10-08 21:07:29 +0200136void fio_client_add_cmd_option(void *cookie, const char *opt)
Jens Axboe81179ee2011-10-04 12:42:06 +0200137{
Jens Axboebebe6392011-10-07 10:00:51 +0200138 struct fio_client *client = cookie;
Jens Axboe81179ee2011-10-04 12:42:06 +0200139
Jens Axboebebe6392011-10-07 10:00:51 +0200140 if (!client || !opt)
Jens Axboefa2ea802011-10-08 21:07:29 +0200141 return;
Jens Axboe81179ee2011-10-04 12:42:06 +0200142
Jens Axboefa2ea802011-10-08 21:07:29 +0200143 __fio_client_add_cmd_option(client, opt);
Jens Axboe81179ee2011-10-04 12:42:06 +0200144}
145
Jens Axboebebe6392011-10-07 10:00:51 +0200146int fio_client_add(const char *hostname, void **cookie)
Jens Axboe132159a2011-09-30 15:01:32 -0600147{
Jens Axboeb66570d2011-10-01 11:11:35 -0600148 struct fio_client *client;
Jens Axboe132159a2011-09-30 15:01:32 -0600149
Jens Axboeb66570d2011-10-01 11:11:35 -0600150 client = malloc(sizeof(*client));
Jens Axboea37f69b2011-10-01 12:24:21 -0600151 memset(client, 0, sizeof(*client));
Jens Axboe81179ee2011-10-04 12:42:06 +0200152
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200153 INIT_FLIST_HEAD(&client->list);
Jens Axboebebe6392011-10-07 10:00:51 +0200154 INIT_FLIST_HEAD(&client->hash_list);
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200155
Jens Axboebebe6392011-10-07 10:00:51 +0200156 if (fio_server_parse_string(hostname, &client->hostname,
157 &client->is_sock, &client->port,
158 &client->addr.sin_addr))
159 return -1;
160
Jens Axboea37f69b2011-10-01 12:24:21 -0600161 client->fd = -1;
Jens Axboe81179ee2011-10-04 12:42:06 +0200162
163 __fio_client_add_cmd_option(client, "fio");
164
Jens Axboea37f69b2011-10-01 12:24:21 -0600165 flist_add(&client->list, &client_list);
166 nr_clients++;
Jens Axboebebe6392011-10-07 10:00:51 +0200167 dprint(FD_NET, "client: added <%s>\n", client->hostname);
168 *cookie = client;
169 return 0;
Jens Axboea37f69b2011-10-01 12:24:21 -0600170}
171
Jens Axboe87aa8f12011-10-06 21:24:13 +0200172static int fio_client_connect_ip(struct fio_client *client)
Jens Axboea37f69b2011-10-01 12:24:21 -0600173{
174 int fd;
Jens Axboe132159a2011-09-30 15:01:32 -0600175
Jens Axboeb66570d2011-10-01 11:11:35 -0600176 client->addr.sin_family = AF_INET;
Jens Axboebebe6392011-10-07 10:00:51 +0200177 client->addr.sin_port = htons(client->port);
Jens Axboe132159a2011-09-30 15:01:32 -0600178
179 fd = socket(AF_INET, SOCK_STREAM, 0);
180 if (fd < 0) {
181 log_err("fio: socket: %s\n", strerror(errno));
Jens Axboe87aa8f12011-10-06 21:24:13 +0200182 return -1;
Jens Axboe132159a2011-09-30 15:01:32 -0600183 }
184
Jens Axboeb66570d2011-10-01 11:11:35 -0600185 if (connect(fd, (struct sockaddr *) &client->addr, sizeof(client->addr)) < 0) {
Jens Axboe132159a2011-09-30 15:01:32 -0600186 log_err("fio: connect: %s\n", strerror(errno));
Jens Axboea7de0a12011-10-07 12:55:14 +0200187 log_err("fio: failed to connect to %s:%u\n", client->hostname,
188 client->port);
Jens Axboeb94cba42011-10-06 21:27:10 +0200189 close(fd);
Jens Axboe87aa8f12011-10-06 21:24:13 +0200190 return -1;
Jens Axboe132159a2011-09-30 15:01:32 -0600191 }
192
Jens Axboe87aa8f12011-10-06 21:24:13 +0200193 return fd;
194}
195
196static int fio_client_connect_sock(struct fio_client *client)
197{
198 struct sockaddr_un *addr = &client->addr_un;
199 fio_socklen_t len;
200 int fd;
201
202 memset(addr, 0, sizeof(*addr));
203 addr->sun_family = AF_UNIX;
204 strcpy(addr->sun_path, client->hostname);
205
206 fd = socket(AF_UNIX, SOCK_STREAM, 0);
207 if (fd < 0) {
208 log_err("fio: socket: %s\n", strerror(errno));
209 return -1;
210 }
211
212 len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
213 if (connect(fd, (struct sockaddr *) addr, len) < 0) {
214 log_err("fio: connect; %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200215 close(fd);
Jens Axboe87aa8f12011-10-06 21:24:13 +0200216 return -1;
217 }
218
219 return fd;
220}
221
222static int fio_client_connect(struct fio_client *client)
223{
224 int fd;
225
226 dprint(FD_NET, "client: connect to host %s\n", client->hostname);
227
Jens Axboe87aa8f12011-10-06 21:24:13 +0200228 if (client->is_sock)
229 fd = fio_client_connect_sock(client);
230 else
231 fd = fio_client_connect_ip(client);
232
233 if (fd < 0)
234 return 1;
235
Jens Axboeb66570d2011-10-01 11:11:35 -0600236 client->fd = fd;
Jens Axboebebe6392011-10-07 10:00:51 +0200237 fio_client_add_hash(client);
Jens Axboe81179ee2011-10-04 12:42:06 +0200238 client->state = Client_connected;
Jens Axboe132159a2011-09-30 15:01:32 -0600239 return 0;
240}
241
Jens Axboecc0df002011-10-03 20:53:32 +0200242void fio_clients_terminate(void)
243{
244 struct flist_head *entry;
245 struct fio_client *client;
246
Jens Axboe60efd142011-10-04 13:30:11 +0200247 dprint(FD_NET, "client: terminate clients\n");
248
Jens Axboecc0df002011-10-03 20:53:32 +0200249 flist_for_each(entry, &client_list) {
250 client = flist_entry(entry, struct fio_client, list);
251
252 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_QUIT, 0);
253 }
254}
255
256static void sig_int(int sig)
257{
Jens Axboebebe6392011-10-07 10:00:51 +0200258 dprint(FD_NET, "client: got signal %d\n", sig);
Jens Axboecc0df002011-10-03 20:53:32 +0200259 fio_clients_terminate();
260}
261
262static void client_signal_handler(void)
263{
264 struct sigaction act;
265
266 memset(&act, 0, sizeof(act));
267 act.sa_handler = sig_int;
268 act.sa_flags = SA_RESTART;
269 sigaction(SIGINT, &act, NULL);
270
271 memset(&act, 0, sizeof(act));
272 act.sa_handler = sig_int;
273 act.sa_flags = SA_RESTART;
274 sigaction(SIGTERM, &act, NULL);
275}
276
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200277static void probe_client(struct fio_client *client)
278{
Jens Axboe60efd142011-10-04 13:30:11 +0200279 dprint(FD_NET, "client: send probe\n");
280
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200281 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_PROBE, 0);
Jens Axboee951bdc2011-10-05 21:58:45 +0200282 handle_client(client);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200283}
284
Jens Axboe81179ee2011-10-04 12:42:06 +0200285static int send_client_cmd_line(struct fio_client *client)
286{
Jens Axboefa2ea802011-10-08 21:07:29 +0200287 struct cmd_single_line_pdu *cslp;
288 struct cmd_line_pdu *clp;
289 unsigned long offset;
290 void *pdu;
291 size_t mem;
Jens Axboe81179ee2011-10-04 12:42:06 +0200292 int i, ret;
293
Jens Axboe39e8e012011-10-04 13:46:08 +0200294 dprint(FD_NET, "client: send cmdline %d\n", client->argc);
Jens Axboe60efd142011-10-04 13:30:11 +0200295
Jens Axboefa2ea802011-10-08 21:07:29 +0200296 /*
297 * Find out how much mem we need
298 */
299 for (i = 0, mem = 0; i < client->argc; i++)
300 mem += strlen(client->argv[i]) + 1;
Jens Axboe81179ee2011-10-04 12:42:06 +0200301
Jens Axboefa2ea802011-10-08 21:07:29 +0200302 /*
303 * We need one cmd_line_pdu, and argc number of cmd_single_line_pdu
304 */
305 mem += sizeof(*clp) + (client->argc * sizeof(*cslp));
306
307 pdu = malloc(mem);
308 clp = pdu;
309 offset = sizeof(*clp);
310
311 for (i = 0; i < client->argc; i++) {
312 uint16_t arg_len = strlen(client->argv[i]) + 1;
313
314 cslp = pdu + offset;
315 strcpy((char *) cslp->text, client->argv[i]);
316 cslp->len = cpu_to_le16(arg_len);
317 offset += sizeof(*cslp) + arg_len;
318 }
319
320 clp->lines = cpu_to_le16(client->argc);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200321 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOBLINE, pdu, mem, 0);
Jens Axboe81179ee2011-10-04 12:42:06 +0200322 free(pdu);
323 return ret;
324}
325
Jens Axboea37f69b2011-10-01 12:24:21 -0600326int fio_clients_connect(void)
327{
328 struct fio_client *client;
329 struct flist_head *entry, *tmp;
330 int ret;
331
Jens Axboe60efd142011-10-04 13:30:11 +0200332 dprint(FD_NET, "client: connect all\n");
333
Jens Axboecc0df002011-10-03 20:53:32 +0200334 client_signal_handler();
335
Jens Axboea37f69b2011-10-01 12:24:21 -0600336 flist_for_each_safe(entry, tmp, &client_list) {
337 client = flist_entry(entry, struct fio_client, list);
338
339 ret = fio_client_connect(client);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200340 if (ret) {
Jens Axboea37f69b2011-10-01 12:24:21 -0600341 remove_client(client);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200342 continue;
343 }
344
345 probe_client(client);
Jens Axboe81179ee2011-10-04 12:42:06 +0200346
347 if (client->argc > 1)
348 send_client_cmd_line(client);
Jens Axboea37f69b2011-10-01 12:24:21 -0600349 }
350
351 return !nr_clients;
352}
353
Jens Axboe132159a2011-09-30 15:01:32 -0600354/*
355 * Send file contents to server backend. We could use sendfile(), but to remain
356 * more portable lets just read/write the darn thing.
357 */
Jens Axboea37f69b2011-10-01 12:24:21 -0600358static int fio_client_send_ini(struct fio_client *client, const char *filename)
Jens Axboe132159a2011-09-30 15:01:32 -0600359{
360 struct stat sb;
361 char *p, *buf;
362 off_t len;
363 int fd, ret;
364
Jens Axboe46c48f12011-10-01 12:50:51 -0600365 dprint(FD_NET, "send ini %s to %s\n", filename, client->hostname);
366
Jens Axboe132159a2011-09-30 15:01:32 -0600367 fd = open(filename, O_RDONLY);
368 if (fd < 0) {
Jens Axboee951bdc2011-10-05 21:58:45 +0200369 log_err("fio: job file <%s> open: %s\n", filename, strerror(errno));
Jens Axboe132159a2011-09-30 15:01:32 -0600370 return 1;
371 }
372
373 if (fstat(fd, &sb) < 0) {
374 log_err("fio: job file stat: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200375 close(fd);
Jens Axboe132159a2011-09-30 15:01:32 -0600376 return 1;
377 }
378
379 buf = malloc(sb.st_size);
380
381 len = sb.st_size;
382 p = buf;
383 do {
384 ret = read(fd, p, len);
385 if (ret > 0) {
386 len -= ret;
387 if (!len)
388 break;
389 p += ret;
390 continue;
391 } else if (!ret)
392 break;
393 else if (errno == EAGAIN || errno == EINTR)
394 continue;
395 } while (1);
396
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200397 if (len) {
398 log_err("fio: failed reading job file %s\n", filename);
Jens Axboeb94cba42011-10-06 21:27:10 +0200399 close(fd);
Jens Axboec524ef72011-10-07 12:23:34 +0200400 free(buf);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200401 return 1;
402 }
403
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200404 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, buf, sb.st_size, 0);
Jens Axboe132159a2011-09-30 15:01:32 -0600405 free(buf);
Jens Axboeb94cba42011-10-06 21:27:10 +0200406 close(fd);
Jens Axboe132159a2011-09-30 15:01:32 -0600407 return ret;
408}
Jens Axboe37db14f2011-09-30 17:00:42 -0600409
Jens Axboea37f69b2011-10-01 12:24:21 -0600410int fio_clients_send_ini(const char *filename)
411{
412 struct fio_client *client;
413 struct flist_head *entry, *tmp;
414
415 flist_for_each_safe(entry, tmp, &client_list) {
416 client = flist_entry(entry, struct fio_client, list);
417
418 if (fio_client_send_ini(client, filename))
419 remove_client(client);
420 }
421
422 return !nr_clients;
423}
424
Jens Axboea64e88d2011-10-03 14:20:01 +0200425static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
426{
427 dst->max_val = le64_to_cpu(src->max_val);
428 dst->min_val = le64_to_cpu(src->min_val);
429 dst->samples = le64_to_cpu(src->samples);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200430
431 /*
432 * Floats arrive as IEEE 754 encoded uint64_t, convert back to double
433 */
434 dst->mean.u.f = fio_uint64_to_double(le64_to_cpu(dst->mean.u.i));
435 dst->S.u.f = fio_uint64_to_double(le64_to_cpu(dst->S.u.i));
Jens Axboea64e88d2011-10-03 14:20:01 +0200436}
437
438static void convert_ts(struct thread_stat *dst, struct thread_stat *src)
439{
440 int i, j;
441
442 dst->error = le32_to_cpu(src->error);
443 dst->groupid = le32_to_cpu(src->groupid);
444 dst->pid = le32_to_cpu(src->pid);
445 dst->members = le32_to_cpu(src->members);
446
447 for (i = 0; i < 2; i++) {
448 convert_io_stat(&dst->clat_stat[i], &src->clat_stat[i]);
449 convert_io_stat(&dst->slat_stat[i], &src->slat_stat[i]);
450 convert_io_stat(&dst->lat_stat[i], &src->lat_stat[i]);
451 convert_io_stat(&dst->bw_stat[i], &src->bw_stat[i]);
452 }
453
454 dst->usr_time = le64_to_cpu(src->usr_time);
455 dst->sys_time = le64_to_cpu(src->sys_time);
456 dst->ctx = le64_to_cpu(src->ctx);
457 dst->minf = le64_to_cpu(src->minf);
458 dst->majf = le64_to_cpu(src->majf);
459 dst->clat_percentiles = le64_to_cpu(src->clat_percentiles);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200460
461 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
462 fio_fp64_t *fps = &src->percentile_list[i];
463 fio_fp64_t *fpd = &dst->percentile_list[i];
464
465 fpd->u.f = fio_uint64_to_double(le64_to_cpu(fps->u.i));
466 }
Jens Axboea64e88d2011-10-03 14:20:01 +0200467
468 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
469 dst->io_u_map[i] = le32_to_cpu(src->io_u_map[i]);
470 dst->io_u_submit[i] = le32_to_cpu(src->io_u_submit[i]);
471 dst->io_u_complete[i] = le32_to_cpu(src->io_u_complete[i]);
472 }
473
474 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
475 dst->io_u_lat_u[i] = le32_to_cpu(src->io_u_lat_u[i]);
476 dst->io_u_lat_m[i] = le32_to_cpu(src->io_u_lat_m[i]);
477 }
478
479 for (i = 0; i < 2; i++)
480 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
481 dst->io_u_plat[i][j] = le32_to_cpu(src->io_u_plat[i][j]);
482
483 for (i = 0; i < 3; i++) {
484 dst->total_io_u[i] = le64_to_cpu(src->total_io_u[i]);
Jens Axboe93eee042011-10-03 21:08:48 +0200485 dst->short_io_u[i] = le64_to_cpu(src->short_io_u[i]);
Jens Axboea64e88d2011-10-03 14:20:01 +0200486 }
487
488 dst->total_submit = le64_to_cpu(src->total_submit);
489 dst->total_complete = le64_to_cpu(src->total_complete);
490
491 for (i = 0; i < 2; i++) {
492 dst->io_bytes[i] = le64_to_cpu(src->io_bytes[i]);
493 dst->runtime[i] = le64_to_cpu(src->runtime[i]);
494 }
495
496 dst->total_run_time = le64_to_cpu(src->total_run_time);
497 dst->continue_on_error = le16_to_cpu(src->continue_on_error);
498 dst->total_err_count = le64_to_cpu(src->total_err_count);
Jens Axboeddcc0b62011-10-03 14:45:27 +0200499 dst->first_error = le32_to_cpu(src->first_error);
500 dst->kb_base = le32_to_cpu(src->kb_base);
Jens Axboea64e88d2011-10-03 14:20:01 +0200501}
502
503static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
504{
505 int i;
506
507 for (i = 0; i < 2; i++) {
508 dst->max_run[i] = le64_to_cpu(src->max_run[i]);
509 dst->min_run[i] = le64_to_cpu(src->min_run[i]);
510 dst->max_bw[i] = le64_to_cpu(src->max_bw[i]);
511 dst->min_bw[i] = le64_to_cpu(src->min_bw[i]);
512 dst->io_kb[i] = le64_to_cpu(src->io_kb[i]);
513 dst->agg[i] = le64_to_cpu(src->agg[i]);
514 }
515
516 dst->kb_base = le32_to_cpu(src->kb_base);
517 dst->groupid = le32_to_cpu(src->groupid);
518}
519
520static void handle_ts(struct fio_net_cmd *cmd)
521{
522 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
523
524 convert_ts(&p->ts, &p->ts);
525 convert_gs(&p->rs, &p->rs);
526
527 show_thread_status(&p->ts, &p->rs);
528}
529
530static void handle_gs(struct fio_net_cmd *cmd)
531{
532 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
533
534 convert_gs(gs, gs);
535 show_group_stats(gs);
536}
537
Jens Axboe48fbb462011-10-09 12:19:08 +0200538static void convert_jobs_eta(struct jobs_eta *je)
Jens Axboecf451d12011-10-03 16:48:30 +0200539{
Jens Axboecf451d12011-10-03 16:48:30 +0200540 int i;
541
542 je->nr_running = le32_to_cpu(je->nr_running);
543 je->nr_ramp = le32_to_cpu(je->nr_ramp);
544 je->nr_pending = le32_to_cpu(je->nr_pending);
545 je->files_open = le32_to_cpu(je->files_open);
546 je->m_rate = le32_to_cpu(je->m_rate);
547 je->t_rate = le32_to_cpu(je->t_rate);
548 je->m_iops = le32_to_cpu(je->m_iops);
549 je->t_iops = le32_to_cpu(je->t_iops);
550
551 for (i = 0; i < 2; i++) {
552 je->rate[i] = le32_to_cpu(je->rate[i]);
553 je->iops[i] = le32_to_cpu(je->iops[i]);
554 }
555
Jens Axboeb51eedb2011-10-09 12:13:39 +0200556 je->elapsed_sec = le64_to_cpu(je->elapsed_sec);
Jens Axboecf451d12011-10-03 16:48:30 +0200557 je->eta_sec = le64_to_cpu(je->eta_sec);
Jens Axboe48fbb462011-10-09 12:19:08 +0200558}
Jens Axboecf451d12011-10-03 16:48:30 +0200559
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200560static void sum_jobs_eta(struct jobs_eta *dst, struct jobs_eta *je)
Jens Axboe48fbb462011-10-09 12:19:08 +0200561{
Jens Axboe48fbb462011-10-09 12:19:08 +0200562 int i;
563
564 dst->nr_running += je->nr_running;
565 dst->nr_ramp += je->nr_ramp;
566 dst->nr_pending += je->nr_pending;
567 dst->files_open += je->files_open;
568 dst->m_rate += je->m_rate;
569 dst->t_rate += je->t_rate;
570 dst->m_iops += je->m_iops;
571 dst->t_iops += je->t_iops;
572
573 for (i = 0; i < 2; i++) {
574 dst->rate[i] += je->rate[i];
575 dst->iops[i] += je->iops[i];
576 }
577
578 dst->elapsed_sec += je->elapsed_sec;
579
580 if (je->eta_sec > dst->eta_sec)
581 dst->eta_sec = je->eta_sec;
582}
583
584static void handle_eta(struct fio_net_cmd *cmd)
585{
586 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200587 struct client_eta *eta = (struct client_eta *) cmd->tag;
588
589 dprint(FD_NET, "client: got eta tag %p, %d\n", eta, eta->pending);
Jens Axboe48fbb462011-10-09 12:19:08 +0200590
591 convert_jobs_eta(je);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200592 sum_jobs_eta(&eta->eta, je);
Jens Axboe48fbb462011-10-09 12:19:08 +0200593
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200594 if (!--eta->pending) {
595 display_thread_status(&eta->eta);
596 free(eta);
597 }
Jens Axboecf451d12011-10-03 16:48:30 +0200598}
599
Jens Axboeb5296dd2011-10-07 16:35:56 +0200600static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd)
Jens Axboe2e03b4b2011-10-04 09:00:40 +0200601{
602 struct cmd_probe_pdu *probe = (struct cmd_probe_pdu *) cmd->payload;
Jens Axboecca84642011-10-07 12:47:57 +0200603 const char *os, *arch;
Jens Axboe2e03b4b2011-10-04 09:00:40 +0200604
Jens Axboecca84642011-10-07 12:47:57 +0200605 os = fio_get_os_string(probe->os);
606 if (!os)
607 os = "unknown";
608
609 arch = fio_get_arch_string(probe->arch);
610 if (!arch)
611 os = "unknown";
612
Jens Axboec71233e2011-10-07 13:11:14 +0200613 log_info("hostname=%s, be=%u, os=%s, arch=%s, fio=%u.%u.%u\n",
Jens Axboecca84642011-10-07 12:47:57 +0200614 probe->hostname, probe->bigendian, os, arch, probe->fio_major,
Jens Axboe6eb24792011-10-04 15:00:30 +0200615 probe->fio_minor, probe->fio_patch);
Jens Axboeb5296dd2011-10-07 16:35:56 +0200616
617 if (!client->name)
618 client->name = strdup((char *) probe->hostname);
Jens Axboe2e03b4b2011-10-04 09:00:40 +0200619}
620
Jens Axboee951bdc2011-10-05 21:58:45 +0200621static int handle_client(struct fio_client *client)
Jens Axboe37db14f2011-09-30 17:00:42 -0600622{
623 struct fio_net_cmd *cmd;
624
Jens Axboe60efd142011-10-04 13:30:11 +0200625 dprint(FD_NET, "client: handle %s\n", client->hostname);
626
Jens Axboee951bdc2011-10-05 21:58:45 +0200627 cmd = fio_net_recv_cmd(client->fd);
628 if (!cmd)
629 return 0;
Jens Axboec2c94582011-10-05 20:31:30 +0200630
Jens Axboee951bdc2011-10-05 21:58:45 +0200631 dprint(FD_NET, "client: got cmd op %d from %s\n",
Jens Axboec2c94582011-10-05 20:31:30 +0200632 cmd->opcode, client->hostname);
Jens Axboe46c48f12011-10-01 12:50:51 -0600633
Jens Axboee951bdc2011-10-05 21:58:45 +0200634 switch (cmd->opcode) {
635 case FIO_NET_CMD_QUIT:
636 remove_client(client);
637 free(cmd);
638 break;
639 case FIO_NET_CMD_TEXT: {
640 const char *buf = (const char *) cmd->payload;
Jens Axboeb5296dd2011-10-07 16:35:56 +0200641 const char *name;
Jens Axboee951bdc2011-10-05 21:58:45 +0200642 int fio_unused ret;
Jens Axboe17dd1762011-10-04 13:27:34 +0200643
Jens Axboeb5296dd2011-10-07 16:35:56 +0200644 name = client->name ? client->name : client->hostname;
645
Jens Axboee951bdc2011-10-05 21:58:45 +0200646 if (!client->skip_newline)
Jens Axboeb5296dd2011-10-07 16:35:56 +0200647 fprintf(f_out, "<%s> ", name);
Jens Axboee951bdc2011-10-05 21:58:45 +0200648 ret = fwrite(buf, cmd->pdu_len, 1, f_out);
649 fflush(f_out);
650 client->skip_newline = strchr(buf, '\n') == NULL;
651 free(cmd);
652 break;
Jens Axboe37db14f2011-09-30 17:00:42 -0600653 }
Jens Axboee951bdc2011-10-05 21:58:45 +0200654 case FIO_NET_CMD_TS:
655 handle_ts(cmd);
656 free(cmd);
657 break;
658 case FIO_NET_CMD_GS:
659 handle_gs(cmd);
660 free(cmd);
661 break;
662 case FIO_NET_CMD_ETA:
663 handle_eta(cmd);
664 free(cmd);
665 break;
666 case FIO_NET_CMD_PROBE:
Jens Axboeb5296dd2011-10-07 16:35:56 +0200667 handle_probe(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +0200668 free(cmd);
669 break;
670 case FIO_NET_CMD_START:
671 client->state = Client_started;
672 free(cmd);
673 break;
674 case FIO_NET_CMD_STOP:
675 client->state = Client_stopped;
676 free(cmd);
677 break;
678 default:
679 log_err("fio: unknown client op: %d\n", cmd->opcode);
680 free(cmd);
681 break;
Jens Axboe37db14f2011-09-30 17:00:42 -0600682 }
683
Jens Axboee951bdc2011-10-05 21:58:45 +0200684 return 1;
Jens Axboe37db14f2011-09-30 17:00:42 -0600685}
Jens Axboeb66570d2011-10-01 11:11:35 -0600686
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200687static void request_client_etas(void)
688{
689 struct fio_client *client;
690 struct flist_head *entry;
691 struct client_eta *eta;
692
693 dprint(FD_NET, "client: request eta (%d)\n", nr_clients);
694
695 /*
696 * We need to do something more clever about checking status
697 * of command being send, client haven't sent previous ETA
698 * already, etc.
699 */
700
701 eta = malloc(sizeof(*eta));
702 memset(&eta->eta, 0, sizeof(eta->eta));
703 eta->pending = nr_clients;
704
705 flist_for_each(entry, &client_list) {
706 client = flist_entry(entry, struct fio_client, list);
707
708 client->waiting_eta = 1;
709 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_SEND_ETA,
710 (uint64_t) eta);
711 }
712
713 dprint(FD_NET, "client: requested eta tag %p\n", eta);
714}
715
Jens Axboeb66570d2011-10-01 11:11:35 -0600716int fio_handle_clients(void)
717{
718 struct fio_client *client;
719 struct flist_head *entry;
720 struct pollfd *pfds;
Jens Axboe82a4be12011-10-01 12:40:32 -0600721 int i, ret = 0;
Jens Axboeb66570d2011-10-01 11:11:35 -0600722
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200723 gettimeofday(&eta_tv, NULL);
724
Jens Axboeb66570d2011-10-01 11:11:35 -0600725 pfds = malloc(nr_clients * sizeof(struct pollfd));
726
Jens Axboeb66570d2011-10-01 11:11:35 -0600727 while (!exit_backend && nr_clients) {
Jens Axboe82a4be12011-10-01 12:40:32 -0600728 i = 0;
729 flist_for_each(entry, &client_list) {
730 client = flist_entry(entry, struct fio_client, list);
731
732 pfds[i].fd = client->fd;
733 pfds[i].events = POLLIN;
734 i++;
735 }
736
737 assert(i == nr_clients);
738
Jens Axboe5c2857f2011-10-04 13:14:32 +0200739 do {
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200740 struct timeval tv;
741
742 gettimeofday(&tv, NULL);
743 if (mtime_since(&eta_tv, &tv) >= 900) {
744 request_client_etas();
745 memcpy(&eta_tv, &tv, sizeof(tv));
746 }
747
Jens Axboe5c2857f2011-10-04 13:14:32 +0200748 ret = poll(pfds, nr_clients, 100);
749 if (ret < 0) {
750 if (errno == EINTR)
751 continue;
752 log_err("fio: poll clients: %s\n", strerror(errno));
753 break;
754 } else if (!ret)
Jens Axboeb66570d2011-10-01 11:11:35 -0600755 continue;
Jens Axboe5c2857f2011-10-04 13:14:32 +0200756 } while (ret <= 0);
Jens Axboeb66570d2011-10-01 11:11:35 -0600757
758 for (i = 0; i < nr_clients; i++) {
759 if (!(pfds[i].revents & POLLIN))
760 continue;
761
762 client = find_client_by_fd(pfds[i].fd);
763 if (!client) {
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200764 log_err("fio: unknown client fd %d\n", pfds[i].fd);
Jens Axboeb66570d2011-10-01 11:11:35 -0600765 continue;
766 }
Jens Axboee951bdc2011-10-05 21:58:45 +0200767 if (!handle_client(client)) {
Jens Axboe28d3ab02011-10-05 20:45:37 +0200768 log_info("client: host=%s disconnected\n",
769 client->hostname);
770 remove_client(client);
771 }
Jens Axboeb66570d2011-10-01 11:11:35 -0600772 }
773 }
774
775 free(pfds);
Jens Axboeb66570d2011-10-01 11:11:35 -0600776 return 0;
777}