blob: 358903bb1f29bff18df48b67c189e38c7d05149f [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 Axboe3f3a4542011-10-10 21:11:09 +020031 struct flist_head arg_list;
Jens Axboeb66570d2011-10-01 11:11:35 -060032 struct sockaddr_in addr;
Jens Axboe87aa8f12011-10-06 21:24:13 +020033 struct sockaddr_un addr_un;
Jens Axboeb66570d2011-10-01 11:11:35 -060034 char *hostname;
Jens Axboebebe6392011-10-07 10:00:51 +020035 int port;
Jens Axboeb66570d2011-10-01 11:11:35 -060036 int fd;
Jens Axboe81179ee2011-10-04 12:42:06 +020037
Jens Axboeb5296dd2011-10-07 16:35:56 +020038 char *name;
39
Jens Axboe81179ee2011-10-04 12:42:06 +020040 int state;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +020041
Jens Axboe17dd1762011-10-04 13:27:34 +020042 int skip_newline;
Jens Axboe87aa8f12011-10-06 21:24:13 +020043 int is_sock;
Jens Axboe82c1ed32011-10-10 08:56:18 +020044
45 struct flist_head eta_list;
46 struct client_eta *eta_in_flight;
Jens Axboe81179ee2011-10-04 12:42:06 +020047
48 uint16_t argc;
49 char **argv;
50};
51
Jens Axboeaf9c9fb2011-10-09 21:54:10 +020052static struct timeval eta_tv;
Jens Axboe48fbb462011-10-09 12:19:08 +020053
Jens Axboe81179ee2011-10-04 12:42:06 +020054enum {
Jens Axboe5c2857f2011-10-04 13:14:32 +020055 Client_created = 0,
Jens Axboe81179ee2011-10-04 12:42:06 +020056 Client_connected = 1,
57 Client_started = 2,
58 Client_stopped = 3,
Jens Axboe5c2857f2011-10-04 13:14:32 +020059 Client_exited = 4,
Jens Axboeb66570d2011-10-01 11:11:35 -060060};
61
62static FLIST_HEAD(client_list);
Jens Axboe82c1ed32011-10-10 08:56:18 +020063static FLIST_HEAD(eta_list);
Jens Axboeb66570d2011-10-01 11:11:35 -060064
Jens Axboe3f3a4542011-10-10 21:11:09 +020065static FLIST_HEAD(arg_list);
66
Jens Axboe3c5f57e2011-10-06 12:37:50 +020067#define FIO_CLIENT_HASH_BITS 7
68#define FIO_CLIENT_HASH_SZ (1 << FIO_CLIENT_HASH_BITS)
69#define FIO_CLIENT_HASH_MASK (FIO_CLIENT_HASH_SZ - 1)
Jens Axboebebe6392011-10-07 10:00:51 +020070static struct flist_head client_hash[FIO_CLIENT_HASH_SZ];
Jens Axboe3c5f57e2011-10-06 12:37:50 +020071
Jens Axboee951bdc2011-10-05 21:58:45 +020072static int handle_client(struct fio_client *client);
Jens Axboe82c1ed32011-10-10 08:56:18 +020073static void dec_jobs_eta(struct client_eta *eta);
Jens Axboe0b8f30a2011-10-04 10:31:53 +020074
Jens Axboebebe6392011-10-07 10:00:51 +020075static void fio_client_add_hash(struct fio_client *client)
Jens Axboe3c5f57e2011-10-06 12:37:50 +020076{
77 int bucket = hash_long(client->fd, FIO_CLIENT_HASH_BITS);
78
79 bucket &= FIO_CLIENT_HASH_MASK;
Jens Axboebebe6392011-10-07 10:00:51 +020080 flist_add(&client->hash_list, &client_hash[bucket]);
Jens Axboe3c5f57e2011-10-06 12:37:50 +020081}
82
Jens Axboebebe6392011-10-07 10:00:51 +020083static void fio_client_remove_hash(struct fio_client *client)
Jens Axboe3c5f57e2011-10-06 12:37:50 +020084{
Jens Axboebebe6392011-10-07 10:00:51 +020085 if (!flist_empty(&client->hash_list))
86 flist_del_init(&client->hash_list);
Jens Axboe3c5f57e2011-10-06 12:37:50 +020087}
88
89static void fio_init fio_client_hash_init(void)
90{
91 int i;
92
Jens Axboebebe6392011-10-07 10:00:51 +020093 for (i = 0; i < FIO_CLIENT_HASH_SZ; i++)
94 INIT_FLIST_HEAD(&client_hash[i]);
Jens Axboe3c5f57e2011-10-06 12:37:50 +020095}
96
Jens Axboeb66570d2011-10-01 11:11:35 -060097static struct fio_client *find_client_by_fd(int fd)
98{
Jens Axboe3c5f57e2011-10-06 12:37:50 +020099 int bucket = hash_long(fd, FIO_CLIENT_HASH_BITS) & FIO_CLIENT_HASH_MASK;
Jens Axboeb66570d2011-10-01 11:11:35 -0600100 struct fio_client *client;
101 struct flist_head *entry;
102
Jens Axboebebe6392011-10-07 10:00:51 +0200103 flist_for_each(entry, &client_hash[bucket]) {
104 client = flist_entry(entry, struct fio_client, hash_list);
Jens Axboeb66570d2011-10-01 11:11:35 -0600105
106 if (client->fd == fd)
107 return client;
108 }
109
110 return NULL;
111}
112
Jens Axboeb66570d2011-10-01 11:11:35 -0600113static void remove_client(struct fio_client *client)
114{
Jens Axboe39e8e012011-10-04 13:46:08 +0200115 dprint(FD_NET, "client: removed <%s>\n", client->hostname);
Jens Axboeb66570d2011-10-01 11:11:35 -0600116 flist_del(&client->list);
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200117
Jens Axboebebe6392011-10-07 10:00:51 +0200118 fio_client_remove_hash(client);
Jens Axboe81179ee2011-10-04 12:42:06 +0200119
Jens Axboe82c1ed32011-10-10 08:56:18 +0200120 if (!flist_empty(&client->eta_list)) {
121 flist_del_init(&client->eta_list);
122 dec_jobs_eta(client->eta_in_flight);
123 }
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200124
Jens Axboeb66570d2011-10-01 11:11:35 -0600125 free(client->hostname);
Jens Axboe81179ee2011-10-04 12:42:06 +0200126 if (client->argv)
127 free(client->argv);
Jens Axboeb5296dd2011-10-07 16:35:56 +0200128 if (client->name)
129 free(client->name);
Jens Axboe81179ee2011-10-04 12:42:06 +0200130
Jens Axboeb66570d2011-10-01 11:11:35 -0600131 free(client);
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200132 nr_clients--;
Jens Axboeb66570d2011-10-01 11:11:35 -0600133}
Jens Axboe132159a2011-09-30 15:01:32 -0600134
Jens Axboefa2ea802011-10-08 21:07:29 +0200135static void __fio_client_add_cmd_option(struct fio_client *client,
136 const char *opt)
Jens Axboe81179ee2011-10-04 12:42:06 +0200137{
Jens Axboe39e8e012011-10-04 13:46:08 +0200138 int index;
139
140 index = client->argc++;
Jens Axboe81179ee2011-10-04 12:42:06 +0200141 client->argv = realloc(client->argv, sizeof(char *) * client->argc);
Jens Axboe39e8e012011-10-04 13:46:08 +0200142 client->argv[index] = strdup(opt);
143 dprint(FD_NET, "client: add cmd %d: %s\n", index, opt);
Jens Axboe81179ee2011-10-04 12:42:06 +0200144}
145
Jens Axboefa2ea802011-10-08 21:07:29 +0200146void fio_client_add_cmd_option(void *cookie, const char *opt)
Jens Axboe81179ee2011-10-04 12:42:06 +0200147{
Jens Axboebebe6392011-10-07 10:00:51 +0200148 struct fio_client *client = cookie;
Jens Axboe3f3a4542011-10-10 21:11:09 +0200149 struct flist_head *entry;
Jens Axboe81179ee2011-10-04 12:42:06 +0200150
Jens Axboebebe6392011-10-07 10:00:51 +0200151 if (!client || !opt)
Jens Axboefa2ea802011-10-08 21:07:29 +0200152 return;
Jens Axboe81179ee2011-10-04 12:42:06 +0200153
Jens Axboefa2ea802011-10-08 21:07:29 +0200154 __fio_client_add_cmd_option(client, opt);
Jens Axboe3f3a4542011-10-10 21:11:09 +0200155
156 /*
157 * Duplicate arguments to shared client group
158 */
159 flist_for_each(entry, &arg_list) {
160 client = flist_entry(entry, struct fio_client, arg_list);
161
162 __fio_client_add_cmd_option(client, opt);
163 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200164}
165
Jens Axboebebe6392011-10-07 10:00:51 +0200166int fio_client_add(const char *hostname, void **cookie)
Jens Axboe132159a2011-09-30 15:01:32 -0600167{
Jens Axboe3f3a4542011-10-10 21:11:09 +0200168 struct fio_client *existing = *cookie;
Jens Axboeb66570d2011-10-01 11:11:35 -0600169 struct fio_client *client;
Jens Axboe132159a2011-09-30 15:01:32 -0600170
Jens Axboe3f3a4542011-10-10 21:11:09 +0200171 if (existing) {
172 /*
173 * We always add our "exec" name as the option, hence 1
174 * means empty.
175 */
176 if (existing->argc == 1)
177 flist_add_tail(&existing->arg_list, &arg_list);
178 else {
179 while (!flist_empty(&arg_list))
180 flist_del_init(arg_list.next);
181 }
182 }
183
Jens Axboeb66570d2011-10-01 11:11:35 -0600184 client = malloc(sizeof(*client));
Jens Axboea37f69b2011-10-01 12:24:21 -0600185 memset(client, 0, sizeof(*client));
Jens Axboe81179ee2011-10-04 12:42:06 +0200186
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200187 INIT_FLIST_HEAD(&client->list);
Jens Axboebebe6392011-10-07 10:00:51 +0200188 INIT_FLIST_HEAD(&client->hash_list);
Jens Axboe3f3a4542011-10-10 21:11:09 +0200189 INIT_FLIST_HEAD(&client->arg_list);
Jens Axboe82c1ed32011-10-10 08:56:18 +0200190 INIT_FLIST_HEAD(&client->eta_list);
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200191
Jens Axboebebe6392011-10-07 10:00:51 +0200192 if (fio_server_parse_string(hostname, &client->hostname,
193 &client->is_sock, &client->port,
194 &client->addr.sin_addr))
195 return -1;
196
Jens Axboea37f69b2011-10-01 12:24:21 -0600197 client->fd = -1;
Jens Axboe81179ee2011-10-04 12:42:06 +0200198
199 __fio_client_add_cmd_option(client, "fio");
200
Jens Axboea37f69b2011-10-01 12:24:21 -0600201 flist_add(&client->list, &client_list);
202 nr_clients++;
Jens Axboebebe6392011-10-07 10:00:51 +0200203 dprint(FD_NET, "client: added <%s>\n", client->hostname);
204 *cookie = client;
205 return 0;
Jens Axboea37f69b2011-10-01 12:24:21 -0600206}
207
Jens Axboe87aa8f12011-10-06 21:24:13 +0200208static int fio_client_connect_ip(struct fio_client *client)
Jens Axboea37f69b2011-10-01 12:24:21 -0600209{
210 int fd;
Jens Axboe132159a2011-09-30 15:01:32 -0600211
Jens Axboeb66570d2011-10-01 11:11:35 -0600212 client->addr.sin_family = AF_INET;
Jens Axboebebe6392011-10-07 10:00:51 +0200213 client->addr.sin_port = htons(client->port);
Jens Axboe132159a2011-09-30 15:01:32 -0600214
215 fd = socket(AF_INET, SOCK_STREAM, 0);
216 if (fd < 0) {
217 log_err("fio: socket: %s\n", strerror(errno));
Jens Axboe87aa8f12011-10-06 21:24:13 +0200218 return -1;
Jens Axboe132159a2011-09-30 15:01:32 -0600219 }
220
Jens Axboeb66570d2011-10-01 11:11:35 -0600221 if (connect(fd, (struct sockaddr *) &client->addr, sizeof(client->addr)) < 0) {
Jens Axboe132159a2011-09-30 15:01:32 -0600222 log_err("fio: connect: %s\n", strerror(errno));
Jens Axboea7de0a12011-10-07 12:55:14 +0200223 log_err("fio: failed to connect to %s:%u\n", client->hostname,
224 client->port);
Jens Axboeb94cba42011-10-06 21:27:10 +0200225 close(fd);
Jens Axboe87aa8f12011-10-06 21:24:13 +0200226 return -1;
Jens Axboe132159a2011-09-30 15:01:32 -0600227 }
228
Jens Axboe87aa8f12011-10-06 21:24:13 +0200229 return fd;
230}
231
232static int fio_client_connect_sock(struct fio_client *client)
233{
234 struct sockaddr_un *addr = &client->addr_un;
235 fio_socklen_t len;
236 int fd;
237
238 memset(addr, 0, sizeof(*addr));
239 addr->sun_family = AF_UNIX;
240 strcpy(addr->sun_path, client->hostname);
241
242 fd = socket(AF_UNIX, SOCK_STREAM, 0);
243 if (fd < 0) {
244 log_err("fio: socket: %s\n", strerror(errno));
245 return -1;
246 }
247
248 len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
249 if (connect(fd, (struct sockaddr *) addr, len) < 0) {
250 log_err("fio: connect; %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200251 close(fd);
Jens Axboe87aa8f12011-10-06 21:24:13 +0200252 return -1;
253 }
254
255 return fd;
256}
257
258static int fio_client_connect(struct fio_client *client)
259{
260 int fd;
261
262 dprint(FD_NET, "client: connect to host %s\n", client->hostname);
263
Jens Axboe87aa8f12011-10-06 21:24:13 +0200264 if (client->is_sock)
265 fd = fio_client_connect_sock(client);
266 else
267 fd = fio_client_connect_ip(client);
268
269 if (fd < 0)
270 return 1;
271
Jens Axboeb66570d2011-10-01 11:11:35 -0600272 client->fd = fd;
Jens Axboebebe6392011-10-07 10:00:51 +0200273 fio_client_add_hash(client);
Jens Axboe81179ee2011-10-04 12:42:06 +0200274 client->state = Client_connected;
Jens Axboe132159a2011-09-30 15:01:32 -0600275 return 0;
276}
277
Jens Axboecc0df002011-10-03 20:53:32 +0200278void fio_clients_terminate(void)
279{
280 struct flist_head *entry;
281 struct fio_client *client;
282
Jens Axboe60efd142011-10-04 13:30:11 +0200283 dprint(FD_NET, "client: terminate clients\n");
284
Jens Axboecc0df002011-10-03 20:53:32 +0200285 flist_for_each(entry, &client_list) {
286 client = flist_entry(entry, struct fio_client, list);
287
288 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_QUIT, 0);
289 }
290}
291
292static void sig_int(int sig)
293{
Jens Axboebebe6392011-10-07 10:00:51 +0200294 dprint(FD_NET, "client: got signal %d\n", sig);
Jens Axboecc0df002011-10-03 20:53:32 +0200295 fio_clients_terminate();
296}
297
298static void client_signal_handler(void)
299{
300 struct sigaction act;
301
302 memset(&act, 0, sizeof(act));
303 act.sa_handler = sig_int;
304 act.sa_flags = SA_RESTART;
305 sigaction(SIGINT, &act, NULL);
306
307 memset(&act, 0, sizeof(act));
308 act.sa_handler = sig_int;
309 act.sa_flags = SA_RESTART;
310 sigaction(SIGTERM, &act, NULL);
311}
312
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200313static void probe_client(struct fio_client *client)
314{
Jens Axboe60efd142011-10-04 13:30:11 +0200315 dprint(FD_NET, "client: send probe\n");
316
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200317 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_PROBE, 0);
Jens Axboee951bdc2011-10-05 21:58:45 +0200318 handle_client(client);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200319}
320
Jens Axboe81179ee2011-10-04 12:42:06 +0200321static int send_client_cmd_line(struct fio_client *client)
322{
Jens Axboefa2ea802011-10-08 21:07:29 +0200323 struct cmd_single_line_pdu *cslp;
324 struct cmd_line_pdu *clp;
325 unsigned long offset;
Jens Axboe7f868312011-10-10 09:55:21 +0200326 unsigned int *lens;
Jens Axboefa2ea802011-10-08 21:07:29 +0200327 void *pdu;
328 size_t mem;
Jens Axboe81179ee2011-10-04 12:42:06 +0200329 int i, ret;
330
Jens Axboe39e8e012011-10-04 13:46:08 +0200331 dprint(FD_NET, "client: send cmdline %d\n", client->argc);
Jens Axboe60efd142011-10-04 13:30:11 +0200332
Jens Axboe7f868312011-10-10 09:55:21 +0200333 lens = malloc(client->argc * sizeof(unsigned int));
334
Jens Axboefa2ea802011-10-08 21:07:29 +0200335 /*
336 * Find out how much mem we need
337 */
Jens Axboe7f868312011-10-10 09:55:21 +0200338 for (i = 0, mem = 0; i < client->argc; i++) {
339 lens[i] = strlen(client->argv[i]) + 1;
340 mem += lens[i];
341 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200342
Jens Axboefa2ea802011-10-08 21:07:29 +0200343 /*
344 * We need one cmd_line_pdu, and argc number of cmd_single_line_pdu
345 */
346 mem += sizeof(*clp) + (client->argc * sizeof(*cslp));
347
348 pdu = malloc(mem);
349 clp = pdu;
350 offset = sizeof(*clp);
351
352 for (i = 0; i < client->argc; i++) {
Jens Axboe7f868312011-10-10 09:55:21 +0200353 uint16_t arg_len = lens[i];
Jens Axboefa2ea802011-10-08 21:07:29 +0200354
355 cslp = pdu + offset;
356 strcpy((char *) cslp->text, client->argv[i]);
357 cslp->len = cpu_to_le16(arg_len);
358 offset += sizeof(*cslp) + arg_len;
359 }
360
Jens Axboe7f868312011-10-10 09:55:21 +0200361 free(lens);
Jens Axboefa2ea802011-10-08 21:07:29 +0200362 clp->lines = cpu_to_le16(client->argc);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200363 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOBLINE, pdu, mem, 0);
Jens Axboe81179ee2011-10-04 12:42:06 +0200364 free(pdu);
365 return ret;
366}
367
Jens Axboea37f69b2011-10-01 12:24:21 -0600368int fio_clients_connect(void)
369{
370 struct fio_client *client;
371 struct flist_head *entry, *tmp;
372 int ret;
373
Jens Axboe60efd142011-10-04 13:30:11 +0200374 dprint(FD_NET, "client: connect all\n");
375
Jens Axboecc0df002011-10-03 20:53:32 +0200376 client_signal_handler();
377
Jens Axboea37f69b2011-10-01 12:24:21 -0600378 flist_for_each_safe(entry, tmp, &client_list) {
379 client = flist_entry(entry, struct fio_client, list);
380
381 ret = fio_client_connect(client);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200382 if (ret) {
Jens Axboea37f69b2011-10-01 12:24:21 -0600383 remove_client(client);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200384 continue;
385 }
386
387 probe_client(client);
Jens Axboe81179ee2011-10-04 12:42:06 +0200388
389 if (client->argc > 1)
390 send_client_cmd_line(client);
Jens Axboea37f69b2011-10-01 12:24:21 -0600391 }
392
393 return !nr_clients;
394}
395
Jens Axboe132159a2011-09-30 15:01:32 -0600396/*
397 * Send file contents to server backend. We could use sendfile(), but to remain
398 * more portable lets just read/write the darn thing.
399 */
Jens Axboea37f69b2011-10-01 12:24:21 -0600400static int fio_client_send_ini(struct fio_client *client, const char *filename)
Jens Axboe132159a2011-09-30 15:01:32 -0600401{
402 struct stat sb;
403 char *p, *buf;
404 off_t len;
405 int fd, ret;
406
Jens Axboe46c48f12011-10-01 12:50:51 -0600407 dprint(FD_NET, "send ini %s to %s\n", filename, client->hostname);
408
Jens Axboe132159a2011-09-30 15:01:32 -0600409 fd = open(filename, O_RDONLY);
410 if (fd < 0) {
Jens Axboee951bdc2011-10-05 21:58:45 +0200411 log_err("fio: job file <%s> open: %s\n", filename, strerror(errno));
Jens Axboe132159a2011-09-30 15:01:32 -0600412 return 1;
413 }
414
415 if (fstat(fd, &sb) < 0) {
416 log_err("fio: job file stat: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200417 close(fd);
Jens Axboe132159a2011-09-30 15:01:32 -0600418 return 1;
419 }
420
421 buf = malloc(sb.st_size);
422
423 len = sb.st_size;
424 p = buf;
425 do {
426 ret = read(fd, p, len);
427 if (ret > 0) {
428 len -= ret;
429 if (!len)
430 break;
431 p += ret;
432 continue;
433 } else if (!ret)
434 break;
435 else if (errno == EAGAIN || errno == EINTR)
436 continue;
437 } while (1);
438
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200439 if (len) {
440 log_err("fio: failed reading job file %s\n", filename);
Jens Axboeb94cba42011-10-06 21:27:10 +0200441 close(fd);
Jens Axboec524ef72011-10-07 12:23:34 +0200442 free(buf);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200443 return 1;
444 }
445
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200446 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, buf, sb.st_size, 0);
Jens Axboe132159a2011-09-30 15:01:32 -0600447 free(buf);
Jens Axboeb94cba42011-10-06 21:27:10 +0200448 close(fd);
Jens Axboe132159a2011-09-30 15:01:32 -0600449 return ret;
450}
Jens Axboe37db14f2011-09-30 17:00:42 -0600451
Jens Axboea37f69b2011-10-01 12:24:21 -0600452int fio_clients_send_ini(const char *filename)
453{
454 struct fio_client *client;
455 struct flist_head *entry, *tmp;
456
457 flist_for_each_safe(entry, tmp, &client_list) {
458 client = flist_entry(entry, struct fio_client, list);
459
460 if (fio_client_send_ini(client, filename))
461 remove_client(client);
462 }
463
464 return !nr_clients;
465}
466
Jens Axboea64e88d2011-10-03 14:20:01 +0200467static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
468{
469 dst->max_val = le64_to_cpu(src->max_val);
470 dst->min_val = le64_to_cpu(src->min_val);
471 dst->samples = le64_to_cpu(src->samples);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200472
473 /*
474 * Floats arrive as IEEE 754 encoded uint64_t, convert back to double
475 */
476 dst->mean.u.f = fio_uint64_to_double(le64_to_cpu(dst->mean.u.i));
477 dst->S.u.f = fio_uint64_to_double(le64_to_cpu(dst->S.u.i));
Jens Axboea64e88d2011-10-03 14:20:01 +0200478}
479
480static void convert_ts(struct thread_stat *dst, struct thread_stat *src)
481{
482 int i, j;
483
484 dst->error = le32_to_cpu(src->error);
485 dst->groupid = le32_to_cpu(src->groupid);
486 dst->pid = le32_to_cpu(src->pid);
487 dst->members = le32_to_cpu(src->members);
488
489 for (i = 0; i < 2; i++) {
490 convert_io_stat(&dst->clat_stat[i], &src->clat_stat[i]);
491 convert_io_stat(&dst->slat_stat[i], &src->slat_stat[i]);
492 convert_io_stat(&dst->lat_stat[i], &src->lat_stat[i]);
493 convert_io_stat(&dst->bw_stat[i], &src->bw_stat[i]);
494 }
495
496 dst->usr_time = le64_to_cpu(src->usr_time);
497 dst->sys_time = le64_to_cpu(src->sys_time);
498 dst->ctx = le64_to_cpu(src->ctx);
499 dst->minf = le64_to_cpu(src->minf);
500 dst->majf = le64_to_cpu(src->majf);
501 dst->clat_percentiles = le64_to_cpu(src->clat_percentiles);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200502
503 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
504 fio_fp64_t *fps = &src->percentile_list[i];
505 fio_fp64_t *fpd = &dst->percentile_list[i];
506
507 fpd->u.f = fio_uint64_to_double(le64_to_cpu(fps->u.i));
508 }
Jens Axboea64e88d2011-10-03 14:20:01 +0200509
510 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
511 dst->io_u_map[i] = le32_to_cpu(src->io_u_map[i]);
512 dst->io_u_submit[i] = le32_to_cpu(src->io_u_submit[i]);
513 dst->io_u_complete[i] = le32_to_cpu(src->io_u_complete[i]);
514 }
515
516 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
517 dst->io_u_lat_u[i] = le32_to_cpu(src->io_u_lat_u[i]);
518 dst->io_u_lat_m[i] = le32_to_cpu(src->io_u_lat_m[i]);
519 }
520
521 for (i = 0; i < 2; i++)
522 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
523 dst->io_u_plat[i][j] = le32_to_cpu(src->io_u_plat[i][j]);
524
525 for (i = 0; i < 3; i++) {
526 dst->total_io_u[i] = le64_to_cpu(src->total_io_u[i]);
Jens Axboe93eee042011-10-03 21:08:48 +0200527 dst->short_io_u[i] = le64_to_cpu(src->short_io_u[i]);
Jens Axboea64e88d2011-10-03 14:20:01 +0200528 }
529
530 dst->total_submit = le64_to_cpu(src->total_submit);
531 dst->total_complete = le64_to_cpu(src->total_complete);
532
533 for (i = 0; i < 2; i++) {
534 dst->io_bytes[i] = le64_to_cpu(src->io_bytes[i]);
535 dst->runtime[i] = le64_to_cpu(src->runtime[i]);
536 }
537
538 dst->total_run_time = le64_to_cpu(src->total_run_time);
539 dst->continue_on_error = le16_to_cpu(src->continue_on_error);
540 dst->total_err_count = le64_to_cpu(src->total_err_count);
Jens Axboeddcc0b62011-10-03 14:45:27 +0200541 dst->first_error = le32_to_cpu(src->first_error);
542 dst->kb_base = le32_to_cpu(src->kb_base);
Jens Axboea64e88d2011-10-03 14:20:01 +0200543}
544
545static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
546{
547 int i;
548
549 for (i = 0; i < 2; i++) {
550 dst->max_run[i] = le64_to_cpu(src->max_run[i]);
551 dst->min_run[i] = le64_to_cpu(src->min_run[i]);
552 dst->max_bw[i] = le64_to_cpu(src->max_bw[i]);
553 dst->min_bw[i] = le64_to_cpu(src->min_bw[i]);
554 dst->io_kb[i] = le64_to_cpu(src->io_kb[i]);
555 dst->agg[i] = le64_to_cpu(src->agg[i]);
556 }
557
558 dst->kb_base = le32_to_cpu(src->kb_base);
559 dst->groupid = le32_to_cpu(src->groupid);
560}
561
562static void handle_ts(struct fio_net_cmd *cmd)
563{
564 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
565
566 convert_ts(&p->ts, &p->ts);
567 convert_gs(&p->rs, &p->rs);
568
569 show_thread_status(&p->ts, &p->rs);
570}
571
572static void handle_gs(struct fio_net_cmd *cmd)
573{
574 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
575
576 convert_gs(gs, gs);
577 show_group_stats(gs);
578}
579
Jens Axboe48fbb462011-10-09 12:19:08 +0200580static void convert_jobs_eta(struct jobs_eta *je)
Jens Axboecf451d12011-10-03 16:48:30 +0200581{
Jens Axboecf451d12011-10-03 16:48:30 +0200582 int i;
583
584 je->nr_running = le32_to_cpu(je->nr_running);
585 je->nr_ramp = le32_to_cpu(je->nr_ramp);
586 je->nr_pending = le32_to_cpu(je->nr_pending);
587 je->files_open = le32_to_cpu(je->files_open);
588 je->m_rate = le32_to_cpu(je->m_rate);
589 je->t_rate = le32_to_cpu(je->t_rate);
590 je->m_iops = le32_to_cpu(je->m_iops);
591 je->t_iops = le32_to_cpu(je->t_iops);
592
593 for (i = 0; i < 2; i++) {
594 je->rate[i] = le32_to_cpu(je->rate[i]);
595 je->iops[i] = le32_to_cpu(je->iops[i]);
596 }
597
Jens Axboeb51eedb2011-10-09 12:13:39 +0200598 je->elapsed_sec = le64_to_cpu(je->elapsed_sec);
Jens Axboecf451d12011-10-03 16:48:30 +0200599 je->eta_sec = le64_to_cpu(je->eta_sec);
Jens Axboe48fbb462011-10-09 12:19:08 +0200600}
Jens Axboecf451d12011-10-03 16:48:30 +0200601
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200602static void sum_jobs_eta(struct jobs_eta *dst, struct jobs_eta *je)
Jens Axboe48fbb462011-10-09 12:19:08 +0200603{
Jens Axboe48fbb462011-10-09 12:19:08 +0200604 int i;
605
606 dst->nr_running += je->nr_running;
607 dst->nr_ramp += je->nr_ramp;
608 dst->nr_pending += je->nr_pending;
609 dst->files_open += je->files_open;
610 dst->m_rate += je->m_rate;
611 dst->t_rate += je->t_rate;
612 dst->m_iops += je->m_iops;
613 dst->t_iops += je->t_iops;
614
615 for (i = 0; i < 2; i++) {
616 dst->rate[i] += je->rate[i];
617 dst->iops[i] += je->iops[i];
618 }
619
620 dst->elapsed_sec += je->elapsed_sec;
621
622 if (je->eta_sec > dst->eta_sec)
623 dst->eta_sec = je->eta_sec;
624}
625
Jens Axboe82c1ed32011-10-10 08:56:18 +0200626static void dec_jobs_eta(struct client_eta *eta)
627{
628 if (!--eta->pending) {
629 display_thread_status(&eta->eta);
630 free(eta);
631 }
632}
633
634static void handle_eta(struct fio_client *client, struct fio_net_cmd *cmd)
Jens Axboe48fbb462011-10-09 12:19:08 +0200635{
636 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200637 struct client_eta *eta = (struct client_eta *) cmd->tag;
638
639 dprint(FD_NET, "client: got eta tag %p, %d\n", eta, eta->pending);
Jens Axboe48fbb462011-10-09 12:19:08 +0200640
Jens Axboef77d2672011-10-10 14:36:07 +0200641 assert(client->eta_in_flight == eta);
642
643 client->eta_in_flight = NULL;
Jens Axboe82c1ed32011-10-10 08:56:18 +0200644 flist_del_init(&client->eta_list);
645
Jens Axboe48fbb462011-10-09 12:19:08 +0200646 convert_jobs_eta(je);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200647 sum_jobs_eta(&eta->eta, je);
Jens Axboe82c1ed32011-10-10 08:56:18 +0200648 dec_jobs_eta(eta);
Jens Axboecf451d12011-10-03 16:48:30 +0200649}
650
Jens Axboeb5296dd2011-10-07 16:35:56 +0200651static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd)
Jens Axboe2e03b4b2011-10-04 09:00:40 +0200652{
653 struct cmd_probe_pdu *probe = (struct cmd_probe_pdu *) cmd->payload;
Jens Axboecca84642011-10-07 12:47:57 +0200654 const char *os, *arch;
Jens Axboe2e03b4b2011-10-04 09:00:40 +0200655
Jens Axboecca84642011-10-07 12:47:57 +0200656 os = fio_get_os_string(probe->os);
657 if (!os)
658 os = "unknown";
659
660 arch = fio_get_arch_string(probe->arch);
661 if (!arch)
662 os = "unknown";
663
Jens Axboec71233e2011-10-07 13:11:14 +0200664 log_info("hostname=%s, be=%u, os=%s, arch=%s, fio=%u.%u.%u\n",
Jens Axboecca84642011-10-07 12:47:57 +0200665 probe->hostname, probe->bigendian, os, arch, probe->fio_major,
Jens Axboe6eb24792011-10-04 15:00:30 +0200666 probe->fio_minor, probe->fio_patch);
Jens Axboeb5296dd2011-10-07 16:35:56 +0200667
668 if (!client->name)
669 client->name = strdup((char *) probe->hostname);
Jens Axboe2e03b4b2011-10-04 09:00:40 +0200670}
671
Jens Axboee951bdc2011-10-05 21:58:45 +0200672static int handle_client(struct fio_client *client)
Jens Axboe37db14f2011-09-30 17:00:42 -0600673{
674 struct fio_net_cmd *cmd;
675
Jens Axboe60efd142011-10-04 13:30:11 +0200676 dprint(FD_NET, "client: handle %s\n", client->hostname);
677
Jens Axboee951bdc2011-10-05 21:58:45 +0200678 cmd = fio_net_recv_cmd(client->fd);
679 if (!cmd)
680 return 0;
Jens Axboec2c94582011-10-05 20:31:30 +0200681
Jens Axboee951bdc2011-10-05 21:58:45 +0200682 dprint(FD_NET, "client: got cmd op %d from %s\n",
Jens Axboec2c94582011-10-05 20:31:30 +0200683 cmd->opcode, client->hostname);
Jens Axboe46c48f12011-10-01 12:50:51 -0600684
Jens Axboee951bdc2011-10-05 21:58:45 +0200685 switch (cmd->opcode) {
686 case FIO_NET_CMD_QUIT:
687 remove_client(client);
688 free(cmd);
689 break;
690 case FIO_NET_CMD_TEXT: {
691 const char *buf = (const char *) cmd->payload;
Jens Axboeb5296dd2011-10-07 16:35:56 +0200692 const char *name;
Jens Axboee951bdc2011-10-05 21:58:45 +0200693 int fio_unused ret;
Jens Axboe17dd1762011-10-04 13:27:34 +0200694
Jens Axboeb5296dd2011-10-07 16:35:56 +0200695 name = client->name ? client->name : client->hostname;
696
Jens Axboee951bdc2011-10-05 21:58:45 +0200697 if (!client->skip_newline)
Jens Axboeb5296dd2011-10-07 16:35:56 +0200698 fprintf(f_out, "<%s> ", name);
Jens Axboee951bdc2011-10-05 21:58:45 +0200699 ret = fwrite(buf, cmd->pdu_len, 1, f_out);
700 fflush(f_out);
701 client->skip_newline = strchr(buf, '\n') == NULL;
702 free(cmd);
703 break;
Jens Axboe37db14f2011-09-30 17:00:42 -0600704 }
Jens Axboee951bdc2011-10-05 21:58:45 +0200705 case FIO_NET_CMD_TS:
706 handle_ts(cmd);
707 free(cmd);
708 break;
709 case FIO_NET_CMD_GS:
710 handle_gs(cmd);
711 free(cmd);
712 break;
713 case FIO_NET_CMD_ETA:
Jens Axboe82c1ed32011-10-10 08:56:18 +0200714 handle_eta(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +0200715 free(cmd);
716 break;
717 case FIO_NET_CMD_PROBE:
Jens Axboeb5296dd2011-10-07 16:35:56 +0200718 handle_probe(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +0200719 free(cmd);
720 break;
721 case FIO_NET_CMD_START:
722 client->state = Client_started;
723 free(cmd);
724 break;
725 case FIO_NET_CMD_STOP:
726 client->state = Client_stopped;
727 free(cmd);
728 break;
729 default:
730 log_err("fio: unknown client op: %d\n", cmd->opcode);
731 free(cmd);
732 break;
Jens Axboe37db14f2011-09-30 17:00:42 -0600733 }
734
Jens Axboee951bdc2011-10-05 21:58:45 +0200735 return 1;
Jens Axboe37db14f2011-09-30 17:00:42 -0600736}
Jens Axboeb66570d2011-10-01 11:11:35 -0600737
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200738static void request_client_etas(void)
739{
740 struct fio_client *client;
741 struct flist_head *entry;
742 struct client_eta *eta;
Jens Axboe82c1ed32011-10-10 08:56:18 +0200743 int skipped = 0;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200744
745 dprint(FD_NET, "client: request eta (%d)\n", nr_clients);
746
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200747 eta = malloc(sizeof(*eta));
748 memset(&eta->eta, 0, sizeof(eta->eta));
749 eta->pending = nr_clients;
750
751 flist_for_each(entry, &client_list) {
752 client = flist_entry(entry, struct fio_client, list);
753
Jens Axboe82c1ed32011-10-10 08:56:18 +0200754 if (!flist_empty(&client->eta_list)) {
755 skipped++;
756 continue;
757 }
758
Jens Axboef77d2672011-10-10 14:36:07 +0200759 assert(!client->eta_in_flight);
Jens Axboe82c1ed32011-10-10 08:56:18 +0200760 flist_add_tail(&client->eta_list, &eta_list);
Jens Axboef77d2672011-10-10 14:36:07 +0200761 client->eta_in_flight = eta;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200762 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_SEND_ETA,
763 (uint64_t) eta);
764 }
765
Jens Axboe82c1ed32011-10-10 08:56:18 +0200766 while (skipped--)
767 dec_jobs_eta(eta);
768
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200769 dprint(FD_NET, "client: requested eta tag %p\n", eta);
770}
771
Jens Axboeb66570d2011-10-01 11:11:35 -0600772int fio_handle_clients(void)
773{
774 struct fio_client *client;
775 struct flist_head *entry;
776 struct pollfd *pfds;
Jens Axboe82a4be12011-10-01 12:40:32 -0600777 int i, ret = 0;
Jens Axboeb66570d2011-10-01 11:11:35 -0600778
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200779 gettimeofday(&eta_tv, NULL);
780
Jens Axboeb66570d2011-10-01 11:11:35 -0600781 pfds = malloc(nr_clients * sizeof(struct pollfd));
782
Jens Axboeb66570d2011-10-01 11:11:35 -0600783 while (!exit_backend && nr_clients) {
Jens Axboe82a4be12011-10-01 12:40:32 -0600784 i = 0;
785 flist_for_each(entry, &client_list) {
786 client = flist_entry(entry, struct fio_client, list);
787
788 pfds[i].fd = client->fd;
789 pfds[i].events = POLLIN;
790 i++;
791 }
792
793 assert(i == nr_clients);
794
Jens Axboe5c2857f2011-10-04 13:14:32 +0200795 do {
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200796 struct timeval tv;
797
798 gettimeofday(&tv, NULL);
799 if (mtime_since(&eta_tv, &tv) >= 900) {
800 request_client_etas();
801 memcpy(&eta_tv, &tv, sizeof(tv));
802 }
803
Jens Axboe5c2857f2011-10-04 13:14:32 +0200804 ret = poll(pfds, nr_clients, 100);
805 if (ret < 0) {
806 if (errno == EINTR)
807 continue;
808 log_err("fio: poll clients: %s\n", strerror(errno));
809 break;
810 } else if (!ret)
Jens Axboeb66570d2011-10-01 11:11:35 -0600811 continue;
Jens Axboe5c2857f2011-10-04 13:14:32 +0200812 } while (ret <= 0);
Jens Axboeb66570d2011-10-01 11:11:35 -0600813
814 for (i = 0; i < nr_clients; i++) {
815 if (!(pfds[i].revents & POLLIN))
816 continue;
817
818 client = find_client_by_fd(pfds[i].fd);
819 if (!client) {
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200820 log_err("fio: unknown client fd %d\n", pfds[i].fd);
Jens Axboeb66570d2011-10-01 11:11:35 -0600821 continue;
822 }
Jens Axboee951bdc2011-10-05 21:58:45 +0200823 if (!handle_client(client)) {
Jens Axboe28d3ab02011-10-05 20:45:37 +0200824 log_info("client: host=%s disconnected\n",
825 client->hostname);
826 remove_client(client);
827 }
Jens Axboeb66570d2011-10-01 11:11:35 -0600828 }
829 }
830
831 free(pfds);
Jens Axboeb66570d2011-10-01 11:11:35 -0600832 return 0;
833}