blob: 713194f9284e4c86a06738a7e99e35eefbce6301 [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 Axboe82c1ed32011-10-10 08:56:18 +020023struct client_eta {
24 struct jobs_eta eta;
25 unsigned int pending;
26};
27
Jens Axboeb66570d2011-10-01 11:11:35 -060028struct fio_client {
29 struct flist_head list;
Jens Axboebebe6392011-10-07 10:00:51 +020030 struct flist_head hash_list;
Jens Axboeb66570d2011-10-01 11:11:35 -060031 struct sockaddr_in addr;
Jens Axboe87aa8f12011-10-06 21:24:13 +020032 struct sockaddr_un addr_un;
Jens Axboeb66570d2011-10-01 11:11:35 -060033 char *hostname;
Jens Axboebebe6392011-10-07 10:00:51 +020034 int port;
Jens Axboeb66570d2011-10-01 11:11:35 -060035 int fd;
Jens Axboe81179ee2011-10-04 12:42:06 +020036
Jens Axboeb5296dd2011-10-07 16:35:56 +020037 char *name;
38
Jens Axboe81179ee2011-10-04 12:42:06 +020039 int state;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +020040
Jens Axboe17dd1762011-10-04 13:27:34 +020041 int skip_newline;
Jens Axboe87aa8f12011-10-06 21:24:13 +020042 int is_sock;
Jens Axboe82c1ed32011-10-10 08:56:18 +020043
44 struct flist_head eta_list;
45 struct client_eta *eta_in_flight;
Jens Axboe81179ee2011-10-04 12:42:06 +020046
47 uint16_t argc;
48 char **argv;
49};
50
Jens Axboeaf9c9fb2011-10-09 21:54:10 +020051static struct timeval eta_tv;
Jens Axboe48fbb462011-10-09 12:19:08 +020052
Jens Axboe81179ee2011-10-04 12:42:06 +020053enum {
Jens Axboe5c2857f2011-10-04 13:14:32 +020054 Client_created = 0,
Jens Axboe81179ee2011-10-04 12:42:06 +020055 Client_connected = 1,
56 Client_started = 2,
57 Client_stopped = 3,
Jens Axboe5c2857f2011-10-04 13:14:32 +020058 Client_exited = 4,
Jens Axboeb66570d2011-10-01 11:11:35 -060059};
60
61static FLIST_HEAD(client_list);
Jens Axboe82c1ed32011-10-10 08:56:18 +020062static FLIST_HEAD(eta_list);
Jens Axboeb66570d2011-10-01 11:11:35 -060063
Jens Axboe3c5f57e2011-10-06 12:37:50 +020064#define FIO_CLIENT_HASH_BITS 7
65#define FIO_CLIENT_HASH_SZ (1 << FIO_CLIENT_HASH_BITS)
66#define FIO_CLIENT_HASH_MASK (FIO_CLIENT_HASH_SZ - 1)
Jens Axboebebe6392011-10-07 10:00:51 +020067static struct flist_head client_hash[FIO_CLIENT_HASH_SZ];
Jens Axboe3c5f57e2011-10-06 12:37:50 +020068
Jens Axboee951bdc2011-10-05 21:58:45 +020069static int handle_client(struct fio_client *client);
Jens Axboe82c1ed32011-10-10 08:56:18 +020070static void dec_jobs_eta(struct client_eta *eta);
Jens Axboe0b8f30a2011-10-04 10:31:53 +020071
Jens Axboebebe6392011-10-07 10:00:51 +020072static void fio_client_add_hash(struct fio_client *client)
Jens Axboe3c5f57e2011-10-06 12:37:50 +020073{
74 int bucket = hash_long(client->fd, FIO_CLIENT_HASH_BITS);
75
76 bucket &= FIO_CLIENT_HASH_MASK;
Jens Axboebebe6392011-10-07 10:00:51 +020077 flist_add(&client->hash_list, &client_hash[bucket]);
Jens Axboe3c5f57e2011-10-06 12:37:50 +020078}
79
Jens Axboebebe6392011-10-07 10:00:51 +020080static void fio_client_remove_hash(struct fio_client *client)
Jens Axboe3c5f57e2011-10-06 12:37:50 +020081{
Jens Axboebebe6392011-10-07 10:00:51 +020082 if (!flist_empty(&client->hash_list))
83 flist_del_init(&client->hash_list);
Jens Axboe3c5f57e2011-10-06 12:37:50 +020084}
85
86static void fio_init fio_client_hash_init(void)
87{
88 int i;
89
Jens Axboebebe6392011-10-07 10:00:51 +020090 for (i = 0; i < FIO_CLIENT_HASH_SZ; i++)
91 INIT_FLIST_HEAD(&client_hash[i]);
Jens Axboe3c5f57e2011-10-06 12:37:50 +020092}
93
Jens Axboeb66570d2011-10-01 11:11:35 -060094static struct fio_client *find_client_by_fd(int fd)
95{
Jens Axboe3c5f57e2011-10-06 12:37:50 +020096 int bucket = hash_long(fd, FIO_CLIENT_HASH_BITS) & FIO_CLIENT_HASH_MASK;
Jens Axboeb66570d2011-10-01 11:11:35 -060097 struct fio_client *client;
98 struct flist_head *entry;
99
Jens Axboebebe6392011-10-07 10:00:51 +0200100 flist_for_each(entry, &client_hash[bucket]) {
101 client = flist_entry(entry, struct fio_client, hash_list);
Jens Axboeb66570d2011-10-01 11:11:35 -0600102
103 if (client->fd == fd)
104 return client;
105 }
106
107 return NULL;
108}
109
Jens Axboeb66570d2011-10-01 11:11:35 -0600110static void remove_client(struct fio_client *client)
111{
Jens Axboe39e8e012011-10-04 13:46:08 +0200112 dprint(FD_NET, "client: removed <%s>\n", client->hostname);
Jens Axboeb66570d2011-10-01 11:11:35 -0600113 flist_del(&client->list);
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200114
Jens Axboebebe6392011-10-07 10:00:51 +0200115 fio_client_remove_hash(client);
Jens Axboe81179ee2011-10-04 12:42:06 +0200116
Jens Axboe82c1ed32011-10-10 08:56:18 +0200117 if (!flist_empty(&client->eta_list)) {
118 flist_del_init(&client->eta_list);
119 dec_jobs_eta(client->eta_in_flight);
120 }
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200121
Jens Axboeb66570d2011-10-01 11:11:35 -0600122 free(client->hostname);
Jens Axboe81179ee2011-10-04 12:42:06 +0200123 if (client->argv)
124 free(client->argv);
Jens Axboeb5296dd2011-10-07 16:35:56 +0200125 if (client->name)
126 free(client->name);
Jens Axboe81179ee2011-10-04 12:42:06 +0200127
Jens Axboeb66570d2011-10-01 11:11:35 -0600128 free(client);
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200129 nr_clients--;
Jens Axboeb66570d2011-10-01 11:11:35 -0600130}
Jens Axboe132159a2011-09-30 15:01:32 -0600131
Jens Axboefa2ea802011-10-08 21:07:29 +0200132static void __fio_client_add_cmd_option(struct fio_client *client,
133 const char *opt)
Jens Axboe81179ee2011-10-04 12:42:06 +0200134{
Jens Axboe39e8e012011-10-04 13:46:08 +0200135 int index;
136
137 index = client->argc++;
Jens Axboe81179ee2011-10-04 12:42:06 +0200138 client->argv = realloc(client->argv, sizeof(char *) * client->argc);
Jens Axboe39e8e012011-10-04 13:46:08 +0200139 client->argv[index] = strdup(opt);
140 dprint(FD_NET, "client: add cmd %d: %s\n", index, opt);
Jens Axboe81179ee2011-10-04 12:42:06 +0200141}
142
Jens Axboefa2ea802011-10-08 21:07:29 +0200143void fio_client_add_cmd_option(void *cookie, const char *opt)
Jens Axboe81179ee2011-10-04 12:42:06 +0200144{
Jens Axboebebe6392011-10-07 10:00:51 +0200145 struct fio_client *client = cookie;
Jens Axboe81179ee2011-10-04 12:42:06 +0200146
Jens Axboebebe6392011-10-07 10:00:51 +0200147 if (!client || !opt)
Jens Axboefa2ea802011-10-08 21:07:29 +0200148 return;
Jens Axboe81179ee2011-10-04 12:42:06 +0200149
Jens Axboefa2ea802011-10-08 21:07:29 +0200150 __fio_client_add_cmd_option(client, opt);
Jens Axboe81179ee2011-10-04 12:42:06 +0200151}
152
Jens Axboebebe6392011-10-07 10:00:51 +0200153int fio_client_add(const char *hostname, void **cookie)
Jens Axboe132159a2011-09-30 15:01:32 -0600154{
Jens Axboeb66570d2011-10-01 11:11:35 -0600155 struct fio_client *client;
Jens Axboe132159a2011-09-30 15:01:32 -0600156
Jens Axboeb66570d2011-10-01 11:11:35 -0600157 client = malloc(sizeof(*client));
Jens Axboea37f69b2011-10-01 12:24:21 -0600158 memset(client, 0, sizeof(*client));
Jens Axboe81179ee2011-10-04 12:42:06 +0200159
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200160 INIT_FLIST_HEAD(&client->list);
Jens Axboebebe6392011-10-07 10:00:51 +0200161 INIT_FLIST_HEAD(&client->hash_list);
Jens Axboe82c1ed32011-10-10 08:56:18 +0200162 INIT_FLIST_HEAD(&client->eta_list);
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200163
Jens Axboebebe6392011-10-07 10:00:51 +0200164 if (fio_server_parse_string(hostname, &client->hostname,
165 &client->is_sock, &client->port,
166 &client->addr.sin_addr))
167 return -1;
168
Jens Axboea37f69b2011-10-01 12:24:21 -0600169 client->fd = -1;
Jens Axboe81179ee2011-10-04 12:42:06 +0200170
171 __fio_client_add_cmd_option(client, "fio");
172
Jens Axboea37f69b2011-10-01 12:24:21 -0600173 flist_add(&client->list, &client_list);
174 nr_clients++;
Jens Axboebebe6392011-10-07 10:00:51 +0200175 dprint(FD_NET, "client: added <%s>\n", client->hostname);
176 *cookie = client;
177 return 0;
Jens Axboea37f69b2011-10-01 12:24:21 -0600178}
179
Jens Axboe87aa8f12011-10-06 21:24:13 +0200180static int fio_client_connect_ip(struct fio_client *client)
Jens Axboea37f69b2011-10-01 12:24:21 -0600181{
182 int fd;
Jens Axboe132159a2011-09-30 15:01:32 -0600183
Jens Axboeb66570d2011-10-01 11:11:35 -0600184 client->addr.sin_family = AF_INET;
Jens Axboebebe6392011-10-07 10:00:51 +0200185 client->addr.sin_port = htons(client->port);
Jens Axboe132159a2011-09-30 15:01:32 -0600186
187 fd = socket(AF_INET, SOCK_STREAM, 0);
188 if (fd < 0) {
189 log_err("fio: socket: %s\n", strerror(errno));
Jens Axboe87aa8f12011-10-06 21:24:13 +0200190 return -1;
Jens Axboe132159a2011-09-30 15:01:32 -0600191 }
192
Jens Axboeb66570d2011-10-01 11:11:35 -0600193 if (connect(fd, (struct sockaddr *) &client->addr, sizeof(client->addr)) < 0) {
Jens Axboe132159a2011-09-30 15:01:32 -0600194 log_err("fio: connect: %s\n", strerror(errno));
Jens Axboea7de0a12011-10-07 12:55:14 +0200195 log_err("fio: failed to connect to %s:%u\n", client->hostname,
196 client->port);
Jens Axboeb94cba42011-10-06 21:27:10 +0200197 close(fd);
Jens Axboe87aa8f12011-10-06 21:24:13 +0200198 return -1;
Jens Axboe132159a2011-09-30 15:01:32 -0600199 }
200
Jens Axboe87aa8f12011-10-06 21:24:13 +0200201 return fd;
202}
203
204static int fio_client_connect_sock(struct fio_client *client)
205{
206 struct sockaddr_un *addr = &client->addr_un;
207 fio_socklen_t len;
208 int fd;
209
210 memset(addr, 0, sizeof(*addr));
211 addr->sun_family = AF_UNIX;
212 strcpy(addr->sun_path, client->hostname);
213
214 fd = socket(AF_UNIX, SOCK_STREAM, 0);
215 if (fd < 0) {
216 log_err("fio: socket: %s\n", strerror(errno));
217 return -1;
218 }
219
220 len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
221 if (connect(fd, (struct sockaddr *) addr, len) < 0) {
222 log_err("fio: connect; %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200223 close(fd);
Jens Axboe87aa8f12011-10-06 21:24:13 +0200224 return -1;
225 }
226
227 return fd;
228}
229
230static int fio_client_connect(struct fio_client *client)
231{
232 int fd;
233
234 dprint(FD_NET, "client: connect to host %s\n", client->hostname);
235
Jens Axboe87aa8f12011-10-06 21:24:13 +0200236 if (client->is_sock)
237 fd = fio_client_connect_sock(client);
238 else
239 fd = fio_client_connect_ip(client);
240
241 if (fd < 0)
242 return 1;
243
Jens Axboeb66570d2011-10-01 11:11:35 -0600244 client->fd = fd;
Jens Axboebebe6392011-10-07 10:00:51 +0200245 fio_client_add_hash(client);
Jens Axboe81179ee2011-10-04 12:42:06 +0200246 client->state = Client_connected;
Jens Axboe132159a2011-09-30 15:01:32 -0600247 return 0;
248}
249
Jens Axboecc0df002011-10-03 20:53:32 +0200250void fio_clients_terminate(void)
251{
252 struct flist_head *entry;
253 struct fio_client *client;
254
Jens Axboe60efd142011-10-04 13:30:11 +0200255 dprint(FD_NET, "client: terminate clients\n");
256
Jens Axboecc0df002011-10-03 20:53:32 +0200257 flist_for_each(entry, &client_list) {
258 client = flist_entry(entry, struct fio_client, list);
259
260 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_QUIT, 0);
261 }
262}
263
264static void sig_int(int sig)
265{
Jens Axboebebe6392011-10-07 10:00:51 +0200266 dprint(FD_NET, "client: got signal %d\n", sig);
Jens Axboecc0df002011-10-03 20:53:32 +0200267 fio_clients_terminate();
268}
269
270static void client_signal_handler(void)
271{
272 struct sigaction act;
273
274 memset(&act, 0, sizeof(act));
275 act.sa_handler = sig_int;
276 act.sa_flags = SA_RESTART;
277 sigaction(SIGINT, &act, NULL);
278
279 memset(&act, 0, sizeof(act));
280 act.sa_handler = sig_int;
281 act.sa_flags = SA_RESTART;
282 sigaction(SIGTERM, &act, NULL);
283}
284
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200285static void probe_client(struct fio_client *client)
286{
Jens Axboe60efd142011-10-04 13:30:11 +0200287 dprint(FD_NET, "client: send probe\n");
288
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200289 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_PROBE, 0);
Jens Axboee951bdc2011-10-05 21:58:45 +0200290 handle_client(client);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200291}
292
Jens Axboe81179ee2011-10-04 12:42:06 +0200293static int send_client_cmd_line(struct fio_client *client)
294{
Jens Axboefa2ea802011-10-08 21:07:29 +0200295 struct cmd_single_line_pdu *cslp;
296 struct cmd_line_pdu *clp;
297 unsigned long offset;
298 void *pdu;
299 size_t mem;
Jens Axboe81179ee2011-10-04 12:42:06 +0200300 int i, ret;
301
Jens Axboe39e8e012011-10-04 13:46:08 +0200302 dprint(FD_NET, "client: send cmdline %d\n", client->argc);
Jens Axboe60efd142011-10-04 13:30:11 +0200303
Jens Axboefa2ea802011-10-08 21:07:29 +0200304 /*
305 * Find out how much mem we need
306 */
307 for (i = 0, mem = 0; i < client->argc; i++)
308 mem += strlen(client->argv[i]) + 1;
Jens Axboe81179ee2011-10-04 12:42:06 +0200309
Jens Axboefa2ea802011-10-08 21:07:29 +0200310 /*
311 * We need one cmd_line_pdu, and argc number of cmd_single_line_pdu
312 */
313 mem += sizeof(*clp) + (client->argc * sizeof(*cslp));
314
315 pdu = malloc(mem);
316 clp = pdu;
317 offset = sizeof(*clp);
318
319 for (i = 0; i < client->argc; i++) {
320 uint16_t arg_len = strlen(client->argv[i]) + 1;
321
322 cslp = pdu + offset;
323 strcpy((char *) cslp->text, client->argv[i]);
324 cslp->len = cpu_to_le16(arg_len);
325 offset += sizeof(*cslp) + arg_len;
326 }
327
328 clp->lines = cpu_to_le16(client->argc);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200329 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOBLINE, pdu, mem, 0);
Jens Axboe81179ee2011-10-04 12:42:06 +0200330 free(pdu);
331 return ret;
332}
333
Jens Axboea37f69b2011-10-01 12:24:21 -0600334int fio_clients_connect(void)
335{
336 struct fio_client *client;
337 struct flist_head *entry, *tmp;
338 int ret;
339
Jens Axboe60efd142011-10-04 13:30:11 +0200340 dprint(FD_NET, "client: connect all\n");
341
Jens Axboecc0df002011-10-03 20:53:32 +0200342 client_signal_handler();
343
Jens Axboea37f69b2011-10-01 12:24:21 -0600344 flist_for_each_safe(entry, tmp, &client_list) {
345 client = flist_entry(entry, struct fio_client, list);
346
347 ret = fio_client_connect(client);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200348 if (ret) {
Jens Axboea37f69b2011-10-01 12:24:21 -0600349 remove_client(client);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200350 continue;
351 }
352
353 probe_client(client);
Jens Axboe81179ee2011-10-04 12:42:06 +0200354
355 if (client->argc > 1)
356 send_client_cmd_line(client);
Jens Axboea37f69b2011-10-01 12:24:21 -0600357 }
358
359 return !nr_clients;
360}
361
Jens Axboe132159a2011-09-30 15:01:32 -0600362/*
363 * Send file contents to server backend. We could use sendfile(), but to remain
364 * more portable lets just read/write the darn thing.
365 */
Jens Axboea37f69b2011-10-01 12:24:21 -0600366static int fio_client_send_ini(struct fio_client *client, const char *filename)
Jens Axboe132159a2011-09-30 15:01:32 -0600367{
368 struct stat sb;
369 char *p, *buf;
370 off_t len;
371 int fd, ret;
372
Jens Axboe46c48f12011-10-01 12:50:51 -0600373 dprint(FD_NET, "send ini %s to %s\n", filename, client->hostname);
374
Jens Axboe132159a2011-09-30 15:01:32 -0600375 fd = open(filename, O_RDONLY);
376 if (fd < 0) {
Jens Axboee951bdc2011-10-05 21:58:45 +0200377 log_err("fio: job file <%s> open: %s\n", filename, strerror(errno));
Jens Axboe132159a2011-09-30 15:01:32 -0600378 return 1;
379 }
380
381 if (fstat(fd, &sb) < 0) {
382 log_err("fio: job file stat: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200383 close(fd);
Jens Axboe132159a2011-09-30 15:01:32 -0600384 return 1;
385 }
386
387 buf = malloc(sb.st_size);
388
389 len = sb.st_size;
390 p = buf;
391 do {
392 ret = read(fd, p, len);
393 if (ret > 0) {
394 len -= ret;
395 if (!len)
396 break;
397 p += ret;
398 continue;
399 } else if (!ret)
400 break;
401 else if (errno == EAGAIN || errno == EINTR)
402 continue;
403 } while (1);
404
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200405 if (len) {
406 log_err("fio: failed reading job file %s\n", filename);
Jens Axboeb94cba42011-10-06 21:27:10 +0200407 close(fd);
Jens Axboec524ef72011-10-07 12:23:34 +0200408 free(buf);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200409 return 1;
410 }
411
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200412 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, buf, sb.st_size, 0);
Jens Axboe132159a2011-09-30 15:01:32 -0600413 free(buf);
Jens Axboeb94cba42011-10-06 21:27:10 +0200414 close(fd);
Jens Axboe132159a2011-09-30 15:01:32 -0600415 return ret;
416}
Jens Axboe37db14f2011-09-30 17:00:42 -0600417
Jens Axboea37f69b2011-10-01 12:24:21 -0600418int fio_clients_send_ini(const char *filename)
419{
420 struct fio_client *client;
421 struct flist_head *entry, *tmp;
422
423 flist_for_each_safe(entry, tmp, &client_list) {
424 client = flist_entry(entry, struct fio_client, list);
425
426 if (fio_client_send_ini(client, filename))
427 remove_client(client);
428 }
429
430 return !nr_clients;
431}
432
Jens Axboea64e88d2011-10-03 14:20:01 +0200433static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
434{
435 dst->max_val = le64_to_cpu(src->max_val);
436 dst->min_val = le64_to_cpu(src->min_val);
437 dst->samples = le64_to_cpu(src->samples);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200438
439 /*
440 * Floats arrive as IEEE 754 encoded uint64_t, convert back to double
441 */
442 dst->mean.u.f = fio_uint64_to_double(le64_to_cpu(dst->mean.u.i));
443 dst->S.u.f = fio_uint64_to_double(le64_to_cpu(dst->S.u.i));
Jens Axboea64e88d2011-10-03 14:20:01 +0200444}
445
446static void convert_ts(struct thread_stat *dst, struct thread_stat *src)
447{
448 int i, j;
449
450 dst->error = le32_to_cpu(src->error);
451 dst->groupid = le32_to_cpu(src->groupid);
452 dst->pid = le32_to_cpu(src->pid);
453 dst->members = le32_to_cpu(src->members);
454
455 for (i = 0; i < 2; i++) {
456 convert_io_stat(&dst->clat_stat[i], &src->clat_stat[i]);
457 convert_io_stat(&dst->slat_stat[i], &src->slat_stat[i]);
458 convert_io_stat(&dst->lat_stat[i], &src->lat_stat[i]);
459 convert_io_stat(&dst->bw_stat[i], &src->bw_stat[i]);
460 }
461
462 dst->usr_time = le64_to_cpu(src->usr_time);
463 dst->sys_time = le64_to_cpu(src->sys_time);
464 dst->ctx = le64_to_cpu(src->ctx);
465 dst->minf = le64_to_cpu(src->minf);
466 dst->majf = le64_to_cpu(src->majf);
467 dst->clat_percentiles = le64_to_cpu(src->clat_percentiles);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200468
469 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
470 fio_fp64_t *fps = &src->percentile_list[i];
471 fio_fp64_t *fpd = &dst->percentile_list[i];
472
473 fpd->u.f = fio_uint64_to_double(le64_to_cpu(fps->u.i));
474 }
Jens Axboea64e88d2011-10-03 14:20:01 +0200475
476 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
477 dst->io_u_map[i] = le32_to_cpu(src->io_u_map[i]);
478 dst->io_u_submit[i] = le32_to_cpu(src->io_u_submit[i]);
479 dst->io_u_complete[i] = le32_to_cpu(src->io_u_complete[i]);
480 }
481
482 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
483 dst->io_u_lat_u[i] = le32_to_cpu(src->io_u_lat_u[i]);
484 dst->io_u_lat_m[i] = le32_to_cpu(src->io_u_lat_m[i]);
485 }
486
487 for (i = 0; i < 2; i++)
488 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
489 dst->io_u_plat[i][j] = le32_to_cpu(src->io_u_plat[i][j]);
490
491 for (i = 0; i < 3; i++) {
492 dst->total_io_u[i] = le64_to_cpu(src->total_io_u[i]);
Jens Axboe93eee042011-10-03 21:08:48 +0200493 dst->short_io_u[i] = le64_to_cpu(src->short_io_u[i]);
Jens Axboea64e88d2011-10-03 14:20:01 +0200494 }
495
496 dst->total_submit = le64_to_cpu(src->total_submit);
497 dst->total_complete = le64_to_cpu(src->total_complete);
498
499 for (i = 0; i < 2; i++) {
500 dst->io_bytes[i] = le64_to_cpu(src->io_bytes[i]);
501 dst->runtime[i] = le64_to_cpu(src->runtime[i]);
502 }
503
504 dst->total_run_time = le64_to_cpu(src->total_run_time);
505 dst->continue_on_error = le16_to_cpu(src->continue_on_error);
506 dst->total_err_count = le64_to_cpu(src->total_err_count);
Jens Axboeddcc0b62011-10-03 14:45:27 +0200507 dst->first_error = le32_to_cpu(src->first_error);
508 dst->kb_base = le32_to_cpu(src->kb_base);
Jens Axboea64e88d2011-10-03 14:20:01 +0200509}
510
511static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
512{
513 int i;
514
515 for (i = 0; i < 2; i++) {
516 dst->max_run[i] = le64_to_cpu(src->max_run[i]);
517 dst->min_run[i] = le64_to_cpu(src->min_run[i]);
518 dst->max_bw[i] = le64_to_cpu(src->max_bw[i]);
519 dst->min_bw[i] = le64_to_cpu(src->min_bw[i]);
520 dst->io_kb[i] = le64_to_cpu(src->io_kb[i]);
521 dst->agg[i] = le64_to_cpu(src->agg[i]);
522 }
523
524 dst->kb_base = le32_to_cpu(src->kb_base);
525 dst->groupid = le32_to_cpu(src->groupid);
526}
527
528static void handle_ts(struct fio_net_cmd *cmd)
529{
530 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
531
532 convert_ts(&p->ts, &p->ts);
533 convert_gs(&p->rs, &p->rs);
534
535 show_thread_status(&p->ts, &p->rs);
536}
537
538static void handle_gs(struct fio_net_cmd *cmd)
539{
540 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
541
542 convert_gs(gs, gs);
543 show_group_stats(gs);
544}
545
Jens Axboe48fbb462011-10-09 12:19:08 +0200546static void convert_jobs_eta(struct jobs_eta *je)
Jens Axboecf451d12011-10-03 16:48:30 +0200547{
Jens Axboecf451d12011-10-03 16:48:30 +0200548 int i;
549
550 je->nr_running = le32_to_cpu(je->nr_running);
551 je->nr_ramp = le32_to_cpu(je->nr_ramp);
552 je->nr_pending = le32_to_cpu(je->nr_pending);
553 je->files_open = le32_to_cpu(je->files_open);
554 je->m_rate = le32_to_cpu(je->m_rate);
555 je->t_rate = le32_to_cpu(je->t_rate);
556 je->m_iops = le32_to_cpu(je->m_iops);
557 je->t_iops = le32_to_cpu(je->t_iops);
558
559 for (i = 0; i < 2; i++) {
560 je->rate[i] = le32_to_cpu(je->rate[i]);
561 je->iops[i] = le32_to_cpu(je->iops[i]);
562 }
563
Jens Axboeb51eedb2011-10-09 12:13:39 +0200564 je->elapsed_sec = le64_to_cpu(je->elapsed_sec);
Jens Axboecf451d12011-10-03 16:48:30 +0200565 je->eta_sec = le64_to_cpu(je->eta_sec);
Jens Axboe48fbb462011-10-09 12:19:08 +0200566}
Jens Axboecf451d12011-10-03 16:48:30 +0200567
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200568static void sum_jobs_eta(struct jobs_eta *dst, struct jobs_eta *je)
Jens Axboe48fbb462011-10-09 12:19:08 +0200569{
Jens Axboe48fbb462011-10-09 12:19:08 +0200570 int i;
571
572 dst->nr_running += je->nr_running;
573 dst->nr_ramp += je->nr_ramp;
574 dst->nr_pending += je->nr_pending;
575 dst->files_open += je->files_open;
576 dst->m_rate += je->m_rate;
577 dst->t_rate += je->t_rate;
578 dst->m_iops += je->m_iops;
579 dst->t_iops += je->t_iops;
580
581 for (i = 0; i < 2; i++) {
582 dst->rate[i] += je->rate[i];
583 dst->iops[i] += je->iops[i];
584 }
585
586 dst->elapsed_sec += je->elapsed_sec;
587
588 if (je->eta_sec > dst->eta_sec)
589 dst->eta_sec = je->eta_sec;
590}
591
Jens Axboe82c1ed32011-10-10 08:56:18 +0200592static void dec_jobs_eta(struct client_eta *eta)
593{
594 if (!--eta->pending) {
595 display_thread_status(&eta->eta);
596 free(eta);
597 }
598}
599
600static void handle_eta(struct fio_client *client, struct fio_net_cmd *cmd)
Jens Axboe48fbb462011-10-09 12:19:08 +0200601{
602 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200603 struct client_eta *eta = (struct client_eta *) cmd->tag;
604
605 dprint(FD_NET, "client: got eta tag %p, %d\n", eta, eta->pending);
Jens Axboe48fbb462011-10-09 12:19:08 +0200606
Jens Axboe82c1ed32011-10-10 08:56:18 +0200607 flist_del_init(&client->eta_list);
608
Jens Axboe48fbb462011-10-09 12:19:08 +0200609 convert_jobs_eta(je);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200610 sum_jobs_eta(&eta->eta, je);
Jens Axboe82c1ed32011-10-10 08:56:18 +0200611 dec_jobs_eta(eta);
Jens Axboecf451d12011-10-03 16:48:30 +0200612}
613
Jens Axboeb5296dd2011-10-07 16:35:56 +0200614static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd)
Jens Axboe2e03b4b2011-10-04 09:00:40 +0200615{
616 struct cmd_probe_pdu *probe = (struct cmd_probe_pdu *) cmd->payload;
Jens Axboecca84642011-10-07 12:47:57 +0200617 const char *os, *arch;
Jens Axboe2e03b4b2011-10-04 09:00:40 +0200618
Jens Axboecca84642011-10-07 12:47:57 +0200619 os = fio_get_os_string(probe->os);
620 if (!os)
621 os = "unknown";
622
623 arch = fio_get_arch_string(probe->arch);
624 if (!arch)
625 os = "unknown";
626
Jens Axboec71233e2011-10-07 13:11:14 +0200627 log_info("hostname=%s, be=%u, os=%s, arch=%s, fio=%u.%u.%u\n",
Jens Axboecca84642011-10-07 12:47:57 +0200628 probe->hostname, probe->bigendian, os, arch, probe->fio_major,
Jens Axboe6eb24792011-10-04 15:00:30 +0200629 probe->fio_minor, probe->fio_patch);
Jens Axboeb5296dd2011-10-07 16:35:56 +0200630
631 if (!client->name)
632 client->name = strdup((char *) probe->hostname);
Jens Axboe2e03b4b2011-10-04 09:00:40 +0200633}
634
Jens Axboee951bdc2011-10-05 21:58:45 +0200635static int handle_client(struct fio_client *client)
Jens Axboe37db14f2011-09-30 17:00:42 -0600636{
637 struct fio_net_cmd *cmd;
638
Jens Axboe60efd142011-10-04 13:30:11 +0200639 dprint(FD_NET, "client: handle %s\n", client->hostname);
640
Jens Axboee951bdc2011-10-05 21:58:45 +0200641 cmd = fio_net_recv_cmd(client->fd);
642 if (!cmd)
643 return 0;
Jens Axboec2c94582011-10-05 20:31:30 +0200644
Jens Axboee951bdc2011-10-05 21:58:45 +0200645 dprint(FD_NET, "client: got cmd op %d from %s\n",
Jens Axboec2c94582011-10-05 20:31:30 +0200646 cmd->opcode, client->hostname);
Jens Axboe46c48f12011-10-01 12:50:51 -0600647
Jens Axboee951bdc2011-10-05 21:58:45 +0200648 switch (cmd->opcode) {
649 case FIO_NET_CMD_QUIT:
650 remove_client(client);
651 free(cmd);
652 break;
653 case FIO_NET_CMD_TEXT: {
654 const char *buf = (const char *) cmd->payload;
Jens Axboeb5296dd2011-10-07 16:35:56 +0200655 const char *name;
Jens Axboee951bdc2011-10-05 21:58:45 +0200656 int fio_unused ret;
Jens Axboe17dd1762011-10-04 13:27:34 +0200657
Jens Axboeb5296dd2011-10-07 16:35:56 +0200658 name = client->name ? client->name : client->hostname;
659
Jens Axboee951bdc2011-10-05 21:58:45 +0200660 if (!client->skip_newline)
Jens Axboeb5296dd2011-10-07 16:35:56 +0200661 fprintf(f_out, "<%s> ", name);
Jens Axboee951bdc2011-10-05 21:58:45 +0200662 ret = fwrite(buf, cmd->pdu_len, 1, f_out);
663 fflush(f_out);
664 client->skip_newline = strchr(buf, '\n') == NULL;
665 free(cmd);
666 break;
Jens Axboe37db14f2011-09-30 17:00:42 -0600667 }
Jens Axboee951bdc2011-10-05 21:58:45 +0200668 case FIO_NET_CMD_TS:
669 handle_ts(cmd);
670 free(cmd);
671 break;
672 case FIO_NET_CMD_GS:
673 handle_gs(cmd);
674 free(cmd);
675 break;
676 case FIO_NET_CMD_ETA:
Jens Axboe82c1ed32011-10-10 08:56:18 +0200677 handle_eta(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +0200678 free(cmd);
679 break;
680 case FIO_NET_CMD_PROBE:
Jens Axboeb5296dd2011-10-07 16:35:56 +0200681 handle_probe(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +0200682 free(cmd);
683 break;
684 case FIO_NET_CMD_START:
685 client->state = Client_started;
686 free(cmd);
687 break;
688 case FIO_NET_CMD_STOP:
689 client->state = Client_stopped;
690 free(cmd);
691 break;
692 default:
693 log_err("fio: unknown client op: %d\n", cmd->opcode);
694 free(cmd);
695 break;
Jens Axboe37db14f2011-09-30 17:00:42 -0600696 }
697
Jens Axboee951bdc2011-10-05 21:58:45 +0200698 return 1;
Jens Axboe37db14f2011-09-30 17:00:42 -0600699}
Jens Axboeb66570d2011-10-01 11:11:35 -0600700
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200701static void request_client_etas(void)
702{
703 struct fio_client *client;
704 struct flist_head *entry;
705 struct client_eta *eta;
Jens Axboe82c1ed32011-10-10 08:56:18 +0200706 int skipped = 0;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200707
708 dprint(FD_NET, "client: request eta (%d)\n", nr_clients);
709
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200710 eta = malloc(sizeof(*eta));
711 memset(&eta->eta, 0, sizeof(eta->eta));
712 eta->pending = nr_clients;
713
714 flist_for_each(entry, &client_list) {
715 client = flist_entry(entry, struct fio_client, list);
716
Jens Axboe82c1ed32011-10-10 08:56:18 +0200717 if (!flist_empty(&client->eta_list)) {
718 skipped++;
719 continue;
720 }
721
722 flist_add_tail(&client->eta_list, &eta_list);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200723 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_SEND_ETA,
724 (uint64_t) eta);
725 }
726
Jens Axboe82c1ed32011-10-10 08:56:18 +0200727 while (skipped--)
728 dec_jobs_eta(eta);
729
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200730 dprint(FD_NET, "client: requested eta tag %p\n", eta);
731}
732
Jens Axboeb66570d2011-10-01 11:11:35 -0600733int fio_handle_clients(void)
734{
735 struct fio_client *client;
736 struct flist_head *entry;
737 struct pollfd *pfds;
Jens Axboe82a4be12011-10-01 12:40:32 -0600738 int i, ret = 0;
Jens Axboeb66570d2011-10-01 11:11:35 -0600739
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200740 gettimeofday(&eta_tv, NULL);
741
Jens Axboeb66570d2011-10-01 11:11:35 -0600742 pfds = malloc(nr_clients * sizeof(struct pollfd));
743
Jens Axboeb66570d2011-10-01 11:11:35 -0600744 while (!exit_backend && nr_clients) {
Jens Axboe82a4be12011-10-01 12:40:32 -0600745 i = 0;
746 flist_for_each(entry, &client_list) {
747 client = flist_entry(entry, struct fio_client, list);
748
749 pfds[i].fd = client->fd;
750 pfds[i].events = POLLIN;
751 i++;
752 }
753
754 assert(i == nr_clients);
755
Jens Axboe5c2857f2011-10-04 13:14:32 +0200756 do {
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200757 struct timeval tv;
758
759 gettimeofday(&tv, NULL);
760 if (mtime_since(&eta_tv, &tv) >= 900) {
761 request_client_etas();
762 memcpy(&eta_tv, &tv, sizeof(tv));
763 }
764
Jens Axboe5c2857f2011-10-04 13:14:32 +0200765 ret = poll(pfds, nr_clients, 100);
766 if (ret < 0) {
767 if (errno == EINTR)
768 continue;
769 log_err("fio: poll clients: %s\n", strerror(errno));
770 break;
771 } else if (!ret)
Jens Axboeb66570d2011-10-01 11:11:35 -0600772 continue;
Jens Axboe5c2857f2011-10-04 13:14:32 +0200773 } while (ret <= 0);
Jens Axboeb66570d2011-10-01 11:11:35 -0600774
775 for (i = 0; i < nr_clients; i++) {
776 if (!(pfds[i].revents & POLLIN))
777 continue;
778
779 client = find_client_by_fd(pfds[i].fd);
780 if (!client) {
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200781 log_err("fio: unknown client fd %d\n", pfds[i].fd);
Jens Axboeb66570d2011-10-01 11:11:35 -0600782 continue;
783 }
Jens Axboee951bdc2011-10-05 21:58:45 +0200784 if (!handle_client(client)) {
Jens Axboe28d3ab02011-10-05 20:45:37 +0200785 log_info("client: host=%s disconnected\n",
786 client->hostname);
787 remove_client(client);
788 }
Jens Axboeb66570d2011-10-01 11:11:35 -0600789 }
790 }
791
792 free(pfds);
Jens Axboeb66570d2011-10-01 11:11:35 -0600793 return 0;
794}