blob: 66ef38ca1e93dd761975b5773adc215cae4efa57 [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;
Jens Axboe7f868312011-10-10 09:55:21 +0200298 unsigned int *lens;
Jens Axboefa2ea802011-10-08 21:07:29 +0200299 void *pdu;
300 size_t mem;
Jens Axboe81179ee2011-10-04 12:42:06 +0200301 int i, ret;
302
Jens Axboe39e8e012011-10-04 13:46:08 +0200303 dprint(FD_NET, "client: send cmdline %d\n", client->argc);
Jens Axboe60efd142011-10-04 13:30:11 +0200304
Jens Axboe7f868312011-10-10 09:55:21 +0200305 lens = malloc(client->argc * sizeof(unsigned int));
306
Jens Axboefa2ea802011-10-08 21:07:29 +0200307 /*
308 * Find out how much mem we need
309 */
Jens Axboe7f868312011-10-10 09:55:21 +0200310 for (i = 0, mem = 0; i < client->argc; i++) {
311 lens[i] = strlen(client->argv[i]) + 1;
312 mem += lens[i];
313 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200314
Jens Axboefa2ea802011-10-08 21:07:29 +0200315 /*
316 * We need one cmd_line_pdu, and argc number of cmd_single_line_pdu
317 */
318 mem += sizeof(*clp) + (client->argc * sizeof(*cslp));
319
320 pdu = malloc(mem);
321 clp = pdu;
322 offset = sizeof(*clp);
323
324 for (i = 0; i < client->argc; i++) {
Jens Axboe7f868312011-10-10 09:55:21 +0200325 uint16_t arg_len = lens[i];
Jens Axboefa2ea802011-10-08 21:07:29 +0200326
327 cslp = pdu + offset;
328 strcpy((char *) cslp->text, client->argv[i]);
329 cslp->len = cpu_to_le16(arg_len);
330 offset += sizeof(*cslp) + arg_len;
331 }
332
Jens Axboe7f868312011-10-10 09:55:21 +0200333 free(lens);
Jens Axboefa2ea802011-10-08 21:07:29 +0200334 clp->lines = cpu_to_le16(client->argc);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200335 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOBLINE, pdu, mem, 0);
Jens Axboe81179ee2011-10-04 12:42:06 +0200336 free(pdu);
337 return ret;
338}
339
Jens Axboea37f69b2011-10-01 12:24:21 -0600340int fio_clients_connect(void)
341{
342 struct fio_client *client;
343 struct flist_head *entry, *tmp;
344 int ret;
345
Jens Axboe60efd142011-10-04 13:30:11 +0200346 dprint(FD_NET, "client: connect all\n");
347
Jens Axboecc0df002011-10-03 20:53:32 +0200348 client_signal_handler();
349
Jens Axboea37f69b2011-10-01 12:24:21 -0600350 flist_for_each_safe(entry, tmp, &client_list) {
351 client = flist_entry(entry, struct fio_client, list);
352
353 ret = fio_client_connect(client);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200354 if (ret) {
Jens Axboea37f69b2011-10-01 12:24:21 -0600355 remove_client(client);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200356 continue;
357 }
358
359 probe_client(client);
Jens Axboe81179ee2011-10-04 12:42:06 +0200360
361 if (client->argc > 1)
362 send_client_cmd_line(client);
Jens Axboea37f69b2011-10-01 12:24:21 -0600363 }
364
365 return !nr_clients;
366}
367
Jens Axboe132159a2011-09-30 15:01:32 -0600368/*
369 * Send file contents to server backend. We could use sendfile(), but to remain
370 * more portable lets just read/write the darn thing.
371 */
Jens Axboea37f69b2011-10-01 12:24:21 -0600372static int fio_client_send_ini(struct fio_client *client, const char *filename)
Jens Axboe132159a2011-09-30 15:01:32 -0600373{
374 struct stat sb;
375 char *p, *buf;
376 off_t len;
377 int fd, ret;
378
Jens Axboe46c48f12011-10-01 12:50:51 -0600379 dprint(FD_NET, "send ini %s to %s\n", filename, client->hostname);
380
Jens Axboe132159a2011-09-30 15:01:32 -0600381 fd = open(filename, O_RDONLY);
382 if (fd < 0) {
Jens Axboee951bdc2011-10-05 21:58:45 +0200383 log_err("fio: job file <%s> open: %s\n", filename, strerror(errno));
Jens Axboe132159a2011-09-30 15:01:32 -0600384 return 1;
385 }
386
387 if (fstat(fd, &sb) < 0) {
388 log_err("fio: job file stat: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200389 close(fd);
Jens Axboe132159a2011-09-30 15:01:32 -0600390 return 1;
391 }
392
393 buf = malloc(sb.st_size);
394
395 len = sb.st_size;
396 p = buf;
397 do {
398 ret = read(fd, p, len);
399 if (ret > 0) {
400 len -= ret;
401 if (!len)
402 break;
403 p += ret;
404 continue;
405 } else if (!ret)
406 break;
407 else if (errno == EAGAIN || errno == EINTR)
408 continue;
409 } while (1);
410
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200411 if (len) {
412 log_err("fio: failed reading job file %s\n", filename);
Jens Axboeb94cba42011-10-06 21:27:10 +0200413 close(fd);
Jens Axboec524ef72011-10-07 12:23:34 +0200414 free(buf);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200415 return 1;
416 }
417
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200418 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, buf, sb.st_size, 0);
Jens Axboe132159a2011-09-30 15:01:32 -0600419 free(buf);
Jens Axboeb94cba42011-10-06 21:27:10 +0200420 close(fd);
Jens Axboe132159a2011-09-30 15:01:32 -0600421 return ret;
422}
Jens Axboe37db14f2011-09-30 17:00:42 -0600423
Jens Axboea37f69b2011-10-01 12:24:21 -0600424int fio_clients_send_ini(const char *filename)
425{
426 struct fio_client *client;
427 struct flist_head *entry, *tmp;
428
429 flist_for_each_safe(entry, tmp, &client_list) {
430 client = flist_entry(entry, struct fio_client, list);
431
432 if (fio_client_send_ini(client, filename))
433 remove_client(client);
434 }
435
436 return !nr_clients;
437}
438
Jens Axboea64e88d2011-10-03 14:20:01 +0200439static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
440{
441 dst->max_val = le64_to_cpu(src->max_val);
442 dst->min_val = le64_to_cpu(src->min_val);
443 dst->samples = le64_to_cpu(src->samples);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200444
445 /*
446 * Floats arrive as IEEE 754 encoded uint64_t, convert back to double
447 */
448 dst->mean.u.f = fio_uint64_to_double(le64_to_cpu(dst->mean.u.i));
449 dst->S.u.f = fio_uint64_to_double(le64_to_cpu(dst->S.u.i));
Jens Axboea64e88d2011-10-03 14:20:01 +0200450}
451
452static void convert_ts(struct thread_stat *dst, struct thread_stat *src)
453{
454 int i, j;
455
456 dst->error = le32_to_cpu(src->error);
457 dst->groupid = le32_to_cpu(src->groupid);
458 dst->pid = le32_to_cpu(src->pid);
459 dst->members = le32_to_cpu(src->members);
460
461 for (i = 0; i < 2; i++) {
462 convert_io_stat(&dst->clat_stat[i], &src->clat_stat[i]);
463 convert_io_stat(&dst->slat_stat[i], &src->slat_stat[i]);
464 convert_io_stat(&dst->lat_stat[i], &src->lat_stat[i]);
465 convert_io_stat(&dst->bw_stat[i], &src->bw_stat[i]);
466 }
467
468 dst->usr_time = le64_to_cpu(src->usr_time);
469 dst->sys_time = le64_to_cpu(src->sys_time);
470 dst->ctx = le64_to_cpu(src->ctx);
471 dst->minf = le64_to_cpu(src->minf);
472 dst->majf = le64_to_cpu(src->majf);
473 dst->clat_percentiles = le64_to_cpu(src->clat_percentiles);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200474
475 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
476 fio_fp64_t *fps = &src->percentile_list[i];
477 fio_fp64_t *fpd = &dst->percentile_list[i];
478
479 fpd->u.f = fio_uint64_to_double(le64_to_cpu(fps->u.i));
480 }
Jens Axboea64e88d2011-10-03 14:20:01 +0200481
482 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
483 dst->io_u_map[i] = le32_to_cpu(src->io_u_map[i]);
484 dst->io_u_submit[i] = le32_to_cpu(src->io_u_submit[i]);
485 dst->io_u_complete[i] = le32_to_cpu(src->io_u_complete[i]);
486 }
487
488 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
489 dst->io_u_lat_u[i] = le32_to_cpu(src->io_u_lat_u[i]);
490 dst->io_u_lat_m[i] = le32_to_cpu(src->io_u_lat_m[i]);
491 }
492
493 for (i = 0; i < 2; i++)
494 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
495 dst->io_u_plat[i][j] = le32_to_cpu(src->io_u_plat[i][j]);
496
497 for (i = 0; i < 3; i++) {
498 dst->total_io_u[i] = le64_to_cpu(src->total_io_u[i]);
Jens Axboe93eee042011-10-03 21:08:48 +0200499 dst->short_io_u[i] = le64_to_cpu(src->short_io_u[i]);
Jens Axboea64e88d2011-10-03 14:20:01 +0200500 }
501
502 dst->total_submit = le64_to_cpu(src->total_submit);
503 dst->total_complete = le64_to_cpu(src->total_complete);
504
505 for (i = 0; i < 2; i++) {
506 dst->io_bytes[i] = le64_to_cpu(src->io_bytes[i]);
507 dst->runtime[i] = le64_to_cpu(src->runtime[i]);
508 }
509
510 dst->total_run_time = le64_to_cpu(src->total_run_time);
511 dst->continue_on_error = le16_to_cpu(src->continue_on_error);
512 dst->total_err_count = le64_to_cpu(src->total_err_count);
Jens Axboeddcc0b62011-10-03 14:45:27 +0200513 dst->first_error = le32_to_cpu(src->first_error);
514 dst->kb_base = le32_to_cpu(src->kb_base);
Jens Axboea64e88d2011-10-03 14:20:01 +0200515}
516
517static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
518{
519 int i;
520
521 for (i = 0; i < 2; i++) {
522 dst->max_run[i] = le64_to_cpu(src->max_run[i]);
523 dst->min_run[i] = le64_to_cpu(src->min_run[i]);
524 dst->max_bw[i] = le64_to_cpu(src->max_bw[i]);
525 dst->min_bw[i] = le64_to_cpu(src->min_bw[i]);
526 dst->io_kb[i] = le64_to_cpu(src->io_kb[i]);
527 dst->agg[i] = le64_to_cpu(src->agg[i]);
528 }
529
530 dst->kb_base = le32_to_cpu(src->kb_base);
531 dst->groupid = le32_to_cpu(src->groupid);
532}
533
534static void handle_ts(struct fio_net_cmd *cmd)
535{
536 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
537
538 convert_ts(&p->ts, &p->ts);
539 convert_gs(&p->rs, &p->rs);
540
541 show_thread_status(&p->ts, &p->rs);
542}
543
544static void handle_gs(struct fio_net_cmd *cmd)
545{
546 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
547
548 convert_gs(gs, gs);
549 show_group_stats(gs);
550}
551
Jens Axboe48fbb462011-10-09 12:19:08 +0200552static void convert_jobs_eta(struct jobs_eta *je)
Jens Axboecf451d12011-10-03 16:48:30 +0200553{
Jens Axboecf451d12011-10-03 16:48:30 +0200554 int i;
555
556 je->nr_running = le32_to_cpu(je->nr_running);
557 je->nr_ramp = le32_to_cpu(je->nr_ramp);
558 je->nr_pending = le32_to_cpu(je->nr_pending);
559 je->files_open = le32_to_cpu(je->files_open);
560 je->m_rate = le32_to_cpu(je->m_rate);
561 je->t_rate = le32_to_cpu(je->t_rate);
562 je->m_iops = le32_to_cpu(je->m_iops);
563 je->t_iops = le32_to_cpu(je->t_iops);
564
565 for (i = 0; i < 2; i++) {
566 je->rate[i] = le32_to_cpu(je->rate[i]);
567 je->iops[i] = le32_to_cpu(je->iops[i]);
568 }
569
Jens Axboeb51eedb2011-10-09 12:13:39 +0200570 je->elapsed_sec = le64_to_cpu(je->elapsed_sec);
Jens Axboecf451d12011-10-03 16:48:30 +0200571 je->eta_sec = le64_to_cpu(je->eta_sec);
Jens Axboe48fbb462011-10-09 12:19:08 +0200572}
Jens Axboecf451d12011-10-03 16:48:30 +0200573
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200574static void sum_jobs_eta(struct jobs_eta *dst, struct jobs_eta *je)
Jens Axboe48fbb462011-10-09 12:19:08 +0200575{
Jens Axboe48fbb462011-10-09 12:19:08 +0200576 int i;
577
578 dst->nr_running += je->nr_running;
579 dst->nr_ramp += je->nr_ramp;
580 dst->nr_pending += je->nr_pending;
581 dst->files_open += je->files_open;
582 dst->m_rate += je->m_rate;
583 dst->t_rate += je->t_rate;
584 dst->m_iops += je->m_iops;
585 dst->t_iops += je->t_iops;
586
587 for (i = 0; i < 2; i++) {
588 dst->rate[i] += je->rate[i];
589 dst->iops[i] += je->iops[i];
590 }
591
592 dst->elapsed_sec += je->elapsed_sec;
593
594 if (je->eta_sec > dst->eta_sec)
595 dst->eta_sec = je->eta_sec;
596}
597
Jens Axboe82c1ed32011-10-10 08:56:18 +0200598static void dec_jobs_eta(struct client_eta *eta)
599{
600 if (!--eta->pending) {
601 display_thread_status(&eta->eta);
602 free(eta);
603 }
604}
605
606static void handle_eta(struct fio_client *client, struct fio_net_cmd *cmd)
Jens Axboe48fbb462011-10-09 12:19:08 +0200607{
608 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200609 struct client_eta *eta = (struct client_eta *) cmd->tag;
610
611 dprint(FD_NET, "client: got eta tag %p, %d\n", eta, eta->pending);
Jens Axboe48fbb462011-10-09 12:19:08 +0200612
Jens Axboe82c1ed32011-10-10 08:56:18 +0200613 flist_del_init(&client->eta_list);
614
Jens Axboe48fbb462011-10-09 12:19:08 +0200615 convert_jobs_eta(je);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200616 sum_jobs_eta(&eta->eta, je);
Jens Axboe82c1ed32011-10-10 08:56:18 +0200617 dec_jobs_eta(eta);
Jens Axboecf451d12011-10-03 16:48:30 +0200618}
619
Jens Axboeb5296dd2011-10-07 16:35:56 +0200620static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd)
Jens Axboe2e03b4b2011-10-04 09:00:40 +0200621{
622 struct cmd_probe_pdu *probe = (struct cmd_probe_pdu *) cmd->payload;
Jens Axboecca84642011-10-07 12:47:57 +0200623 const char *os, *arch;
Jens Axboe2e03b4b2011-10-04 09:00:40 +0200624
Jens Axboecca84642011-10-07 12:47:57 +0200625 os = fio_get_os_string(probe->os);
626 if (!os)
627 os = "unknown";
628
629 arch = fio_get_arch_string(probe->arch);
630 if (!arch)
631 os = "unknown";
632
Jens Axboec71233e2011-10-07 13:11:14 +0200633 log_info("hostname=%s, be=%u, os=%s, arch=%s, fio=%u.%u.%u\n",
Jens Axboecca84642011-10-07 12:47:57 +0200634 probe->hostname, probe->bigendian, os, arch, probe->fio_major,
Jens Axboe6eb24792011-10-04 15:00:30 +0200635 probe->fio_minor, probe->fio_patch);
Jens Axboeb5296dd2011-10-07 16:35:56 +0200636
637 if (!client->name)
638 client->name = strdup((char *) probe->hostname);
Jens Axboe2e03b4b2011-10-04 09:00:40 +0200639}
640
Jens Axboee951bdc2011-10-05 21:58:45 +0200641static int handle_client(struct fio_client *client)
Jens Axboe37db14f2011-09-30 17:00:42 -0600642{
643 struct fio_net_cmd *cmd;
644
Jens Axboe60efd142011-10-04 13:30:11 +0200645 dprint(FD_NET, "client: handle %s\n", client->hostname);
646
Jens Axboee951bdc2011-10-05 21:58:45 +0200647 cmd = fio_net_recv_cmd(client->fd);
648 if (!cmd)
649 return 0;
Jens Axboec2c94582011-10-05 20:31:30 +0200650
Jens Axboee951bdc2011-10-05 21:58:45 +0200651 dprint(FD_NET, "client: got cmd op %d from %s\n",
Jens Axboec2c94582011-10-05 20:31:30 +0200652 cmd->opcode, client->hostname);
Jens Axboe46c48f12011-10-01 12:50:51 -0600653
Jens Axboee951bdc2011-10-05 21:58:45 +0200654 switch (cmd->opcode) {
655 case FIO_NET_CMD_QUIT:
656 remove_client(client);
657 free(cmd);
658 break;
659 case FIO_NET_CMD_TEXT: {
660 const char *buf = (const char *) cmd->payload;
Jens Axboeb5296dd2011-10-07 16:35:56 +0200661 const char *name;
Jens Axboee951bdc2011-10-05 21:58:45 +0200662 int fio_unused ret;
Jens Axboe17dd1762011-10-04 13:27:34 +0200663
Jens Axboeb5296dd2011-10-07 16:35:56 +0200664 name = client->name ? client->name : client->hostname;
665
Jens Axboee951bdc2011-10-05 21:58:45 +0200666 if (!client->skip_newline)
Jens Axboeb5296dd2011-10-07 16:35:56 +0200667 fprintf(f_out, "<%s> ", name);
Jens Axboee951bdc2011-10-05 21:58:45 +0200668 ret = fwrite(buf, cmd->pdu_len, 1, f_out);
669 fflush(f_out);
670 client->skip_newline = strchr(buf, '\n') == NULL;
671 free(cmd);
672 break;
Jens Axboe37db14f2011-09-30 17:00:42 -0600673 }
Jens Axboee951bdc2011-10-05 21:58:45 +0200674 case FIO_NET_CMD_TS:
675 handle_ts(cmd);
676 free(cmd);
677 break;
678 case FIO_NET_CMD_GS:
679 handle_gs(cmd);
680 free(cmd);
681 break;
682 case FIO_NET_CMD_ETA:
Jens Axboe82c1ed32011-10-10 08:56:18 +0200683 handle_eta(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +0200684 free(cmd);
685 break;
686 case FIO_NET_CMD_PROBE:
Jens Axboeb5296dd2011-10-07 16:35:56 +0200687 handle_probe(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +0200688 free(cmd);
689 break;
690 case FIO_NET_CMD_START:
691 client->state = Client_started;
692 free(cmd);
693 break;
694 case FIO_NET_CMD_STOP:
695 client->state = Client_stopped;
696 free(cmd);
697 break;
698 default:
699 log_err("fio: unknown client op: %d\n", cmd->opcode);
700 free(cmd);
701 break;
Jens Axboe37db14f2011-09-30 17:00:42 -0600702 }
703
Jens Axboee951bdc2011-10-05 21:58:45 +0200704 return 1;
Jens Axboe37db14f2011-09-30 17:00:42 -0600705}
Jens Axboeb66570d2011-10-01 11:11:35 -0600706
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200707static void request_client_etas(void)
708{
709 struct fio_client *client;
710 struct flist_head *entry;
711 struct client_eta *eta;
Jens Axboe82c1ed32011-10-10 08:56:18 +0200712 int skipped = 0;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200713
714 dprint(FD_NET, "client: request eta (%d)\n", nr_clients);
715
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200716 eta = malloc(sizeof(*eta));
717 memset(&eta->eta, 0, sizeof(eta->eta));
718 eta->pending = nr_clients;
719
720 flist_for_each(entry, &client_list) {
721 client = flist_entry(entry, struct fio_client, list);
722
Jens Axboe82c1ed32011-10-10 08:56:18 +0200723 if (!flist_empty(&client->eta_list)) {
724 skipped++;
725 continue;
726 }
727
728 flist_add_tail(&client->eta_list, &eta_list);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200729 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_SEND_ETA,
730 (uint64_t) eta);
731 }
732
Jens Axboe82c1ed32011-10-10 08:56:18 +0200733 while (skipped--)
734 dec_jobs_eta(eta);
735
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200736 dprint(FD_NET, "client: requested eta tag %p\n", eta);
737}
738
Jens Axboeb66570d2011-10-01 11:11:35 -0600739int fio_handle_clients(void)
740{
741 struct fio_client *client;
742 struct flist_head *entry;
743 struct pollfd *pfds;
Jens Axboe82a4be12011-10-01 12:40:32 -0600744 int i, ret = 0;
Jens Axboeb66570d2011-10-01 11:11:35 -0600745
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200746 gettimeofday(&eta_tv, NULL);
747
Jens Axboeb66570d2011-10-01 11:11:35 -0600748 pfds = malloc(nr_clients * sizeof(struct pollfd));
749
Jens Axboeb66570d2011-10-01 11:11:35 -0600750 while (!exit_backend && nr_clients) {
Jens Axboe82a4be12011-10-01 12:40:32 -0600751 i = 0;
752 flist_for_each(entry, &client_list) {
753 client = flist_entry(entry, struct fio_client, list);
754
755 pfds[i].fd = client->fd;
756 pfds[i].events = POLLIN;
757 i++;
758 }
759
760 assert(i == nr_clients);
761
Jens Axboe5c2857f2011-10-04 13:14:32 +0200762 do {
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200763 struct timeval tv;
764
765 gettimeofday(&tv, NULL);
766 if (mtime_since(&eta_tv, &tv) >= 900) {
767 request_client_etas();
768 memcpy(&eta_tv, &tv, sizeof(tv));
769 }
770
Jens Axboe5c2857f2011-10-04 13:14:32 +0200771 ret = poll(pfds, nr_clients, 100);
772 if (ret < 0) {
773 if (errno == EINTR)
774 continue;
775 log_err("fio: poll clients: %s\n", strerror(errno));
776 break;
777 } else if (!ret)
Jens Axboeb66570d2011-10-01 11:11:35 -0600778 continue;
Jens Axboe5c2857f2011-10-04 13:14:32 +0200779 } while (ret <= 0);
Jens Axboeb66570d2011-10-01 11:11:35 -0600780
781 for (i = 0; i < nr_clients; i++) {
782 if (!(pfds[i].revents & POLLIN))
783 continue;
784
785 client = find_client_by_fd(pfds[i].fd);
786 if (!client) {
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200787 log_err("fio: unknown client fd %d\n", pfds[i].fd);
Jens Axboeb66570d2011-10-01 11:11:35 -0600788 continue;
789 }
Jens Axboee951bdc2011-10-05 21:58:45 +0200790 if (!handle_client(client)) {
Jens Axboe28d3ab02011-10-05 20:45:37 +0200791 log_info("client: host=%s disconnected\n",
792 client->hostname);
793 remove_client(client);
794 }
Jens Axboeb66570d2011-10-01 11:11:35 -0600795 }
796 }
797
798 free(pfds);
Jens Axboeb66570d2011-10-01 11:11:35 -0600799 return 0;
800}