blob: 130aeafc1a493efc8d3c92f63049200b9724a177 [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
Jens Axboe89c17072011-10-11 10:15:51 +020048 struct flist_head cmd_list;
49
Jens Axboe81179ee2011-10-04 12:42:06 +020050 uint16_t argc;
51 char **argv;
52};
53
Jens Axboeaf9c9fb2011-10-09 21:54:10 +020054static struct timeval eta_tv;
Jens Axboe48fbb462011-10-09 12:19:08 +020055
Jens Axboe81179ee2011-10-04 12:42:06 +020056enum {
Jens Axboe5c2857f2011-10-04 13:14:32 +020057 Client_created = 0,
Jens Axboe81179ee2011-10-04 12:42:06 +020058 Client_connected = 1,
59 Client_started = 2,
60 Client_stopped = 3,
Jens Axboe5c2857f2011-10-04 13:14:32 +020061 Client_exited = 4,
Jens Axboeb66570d2011-10-01 11:11:35 -060062};
63
64static FLIST_HEAD(client_list);
Jens Axboe82c1ed32011-10-10 08:56:18 +020065static FLIST_HEAD(eta_list);
Jens Axboeb66570d2011-10-01 11:11:35 -060066
Jens Axboe3f3a4542011-10-10 21:11:09 +020067static FLIST_HEAD(arg_list);
68
Jens Axboe3c5f57e2011-10-06 12:37:50 +020069#define FIO_CLIENT_HASH_BITS 7
70#define FIO_CLIENT_HASH_SZ (1 << FIO_CLIENT_HASH_BITS)
71#define FIO_CLIENT_HASH_MASK (FIO_CLIENT_HASH_SZ - 1)
Jens Axboebebe6392011-10-07 10:00:51 +020072static struct flist_head client_hash[FIO_CLIENT_HASH_SZ];
Jens Axboe3c5f57e2011-10-06 12:37:50 +020073
Jens Axboee951bdc2011-10-05 21:58:45 +020074static int handle_client(struct fio_client *client);
Jens Axboe82c1ed32011-10-10 08:56:18 +020075static void dec_jobs_eta(struct client_eta *eta);
Jens Axboe0b8f30a2011-10-04 10:31:53 +020076
Jens Axboebebe6392011-10-07 10:00:51 +020077static void fio_client_add_hash(struct fio_client *client)
Jens Axboe3c5f57e2011-10-06 12:37:50 +020078{
79 int bucket = hash_long(client->fd, FIO_CLIENT_HASH_BITS);
80
81 bucket &= FIO_CLIENT_HASH_MASK;
Jens Axboebebe6392011-10-07 10:00:51 +020082 flist_add(&client->hash_list, &client_hash[bucket]);
Jens Axboe3c5f57e2011-10-06 12:37:50 +020083}
84
Jens Axboebebe6392011-10-07 10:00:51 +020085static void fio_client_remove_hash(struct fio_client *client)
Jens Axboe3c5f57e2011-10-06 12:37:50 +020086{
Jens Axboebebe6392011-10-07 10:00:51 +020087 if (!flist_empty(&client->hash_list))
88 flist_del_init(&client->hash_list);
Jens Axboe3c5f57e2011-10-06 12:37:50 +020089}
90
91static void fio_init fio_client_hash_init(void)
92{
93 int i;
94
Jens Axboebebe6392011-10-07 10:00:51 +020095 for (i = 0; i < FIO_CLIENT_HASH_SZ; i++)
96 INIT_FLIST_HEAD(&client_hash[i]);
Jens Axboe3c5f57e2011-10-06 12:37:50 +020097}
98
Jens Axboeb66570d2011-10-01 11:11:35 -060099static struct fio_client *find_client_by_fd(int fd)
100{
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200101 int bucket = hash_long(fd, FIO_CLIENT_HASH_BITS) & FIO_CLIENT_HASH_MASK;
Jens Axboeb66570d2011-10-01 11:11:35 -0600102 struct fio_client *client;
103 struct flist_head *entry;
104
Jens Axboebebe6392011-10-07 10:00:51 +0200105 flist_for_each(entry, &client_hash[bucket]) {
106 client = flist_entry(entry, struct fio_client, hash_list);
Jens Axboeb66570d2011-10-01 11:11:35 -0600107
108 if (client->fd == fd)
109 return client;
110 }
111
112 return NULL;
113}
114
Jens Axboeb66570d2011-10-01 11:11:35 -0600115static void remove_client(struct fio_client *client)
116{
Jens Axboe39e8e012011-10-04 13:46:08 +0200117 dprint(FD_NET, "client: removed <%s>\n", client->hostname);
Jens Axboeb66570d2011-10-01 11:11:35 -0600118 flist_del(&client->list);
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200119
Jens Axboebebe6392011-10-07 10:00:51 +0200120 fio_client_remove_hash(client);
Jens Axboe81179ee2011-10-04 12:42:06 +0200121
Jens Axboe82c1ed32011-10-10 08:56:18 +0200122 if (!flist_empty(&client->eta_list)) {
123 flist_del_init(&client->eta_list);
124 dec_jobs_eta(client->eta_in_flight);
125 }
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200126
Jens Axboeb66570d2011-10-01 11:11:35 -0600127 free(client->hostname);
Jens Axboe81179ee2011-10-04 12:42:06 +0200128 if (client->argv)
129 free(client->argv);
Jens Axboeb5296dd2011-10-07 16:35:56 +0200130 if (client->name)
131 free(client->name);
Jens Axboe81179ee2011-10-04 12:42:06 +0200132
Jens Axboeb66570d2011-10-01 11:11:35 -0600133 free(client);
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200134 nr_clients--;
Jens Axboeb66570d2011-10-01 11:11:35 -0600135}
Jens Axboe132159a2011-09-30 15:01:32 -0600136
Jens Axboefa2ea802011-10-08 21:07:29 +0200137static void __fio_client_add_cmd_option(struct fio_client *client,
138 const char *opt)
Jens Axboe81179ee2011-10-04 12:42:06 +0200139{
Jens Axboe39e8e012011-10-04 13:46:08 +0200140 int index;
141
142 index = client->argc++;
Jens Axboe81179ee2011-10-04 12:42:06 +0200143 client->argv = realloc(client->argv, sizeof(char *) * client->argc);
Jens Axboe39e8e012011-10-04 13:46:08 +0200144 client->argv[index] = strdup(opt);
145 dprint(FD_NET, "client: add cmd %d: %s\n", index, opt);
Jens Axboe81179ee2011-10-04 12:42:06 +0200146}
147
Jens Axboefa2ea802011-10-08 21:07:29 +0200148void fio_client_add_cmd_option(void *cookie, const char *opt)
Jens Axboe81179ee2011-10-04 12:42:06 +0200149{
Jens Axboebebe6392011-10-07 10:00:51 +0200150 struct fio_client *client = cookie;
Jens Axboe3f3a4542011-10-10 21:11:09 +0200151 struct flist_head *entry;
Jens Axboe81179ee2011-10-04 12:42:06 +0200152
Jens Axboebebe6392011-10-07 10:00:51 +0200153 if (!client || !opt)
Jens Axboefa2ea802011-10-08 21:07:29 +0200154 return;
Jens Axboe81179ee2011-10-04 12:42:06 +0200155
Jens Axboefa2ea802011-10-08 21:07:29 +0200156 __fio_client_add_cmd_option(client, opt);
Jens Axboe3f3a4542011-10-10 21:11:09 +0200157
158 /*
159 * Duplicate arguments to shared client group
160 */
161 flist_for_each(entry, &arg_list) {
162 client = flist_entry(entry, struct fio_client, arg_list);
163
164 __fio_client_add_cmd_option(client, opt);
165 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200166}
167
Jens Axboebebe6392011-10-07 10:00:51 +0200168int fio_client_add(const char *hostname, void **cookie)
Jens Axboe132159a2011-09-30 15:01:32 -0600169{
Jens Axboe3f3a4542011-10-10 21:11:09 +0200170 struct fio_client *existing = *cookie;
Jens Axboeb66570d2011-10-01 11:11:35 -0600171 struct fio_client *client;
Jens Axboe132159a2011-09-30 15:01:32 -0600172
Jens Axboe3f3a4542011-10-10 21:11:09 +0200173 if (existing) {
174 /*
175 * We always add our "exec" name as the option, hence 1
176 * means empty.
177 */
178 if (existing->argc == 1)
179 flist_add_tail(&existing->arg_list, &arg_list);
180 else {
181 while (!flist_empty(&arg_list))
182 flist_del_init(arg_list.next);
183 }
184 }
185
Jens Axboeb66570d2011-10-01 11:11:35 -0600186 client = malloc(sizeof(*client));
Jens Axboea37f69b2011-10-01 12:24:21 -0600187 memset(client, 0, sizeof(*client));
Jens Axboe81179ee2011-10-04 12:42:06 +0200188
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200189 INIT_FLIST_HEAD(&client->list);
Jens Axboebebe6392011-10-07 10:00:51 +0200190 INIT_FLIST_HEAD(&client->hash_list);
Jens Axboe3f3a4542011-10-10 21:11:09 +0200191 INIT_FLIST_HEAD(&client->arg_list);
Jens Axboe82c1ed32011-10-10 08:56:18 +0200192 INIT_FLIST_HEAD(&client->eta_list);
Jens Axboe89c17072011-10-11 10:15:51 +0200193 INIT_FLIST_HEAD(&client->cmd_list);
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200194
Jens Axboebebe6392011-10-07 10:00:51 +0200195 if (fio_server_parse_string(hostname, &client->hostname,
196 &client->is_sock, &client->port,
197 &client->addr.sin_addr))
198 return -1;
199
Jens Axboea37f69b2011-10-01 12:24:21 -0600200 client->fd = -1;
Jens Axboe81179ee2011-10-04 12:42:06 +0200201
202 __fio_client_add_cmd_option(client, "fio");
203
Jens Axboea37f69b2011-10-01 12:24:21 -0600204 flist_add(&client->list, &client_list);
205 nr_clients++;
Jens Axboebebe6392011-10-07 10:00:51 +0200206 dprint(FD_NET, "client: added <%s>\n", client->hostname);
207 *cookie = client;
208 return 0;
Jens Axboea37f69b2011-10-01 12:24:21 -0600209}
210
Jens Axboe87aa8f12011-10-06 21:24:13 +0200211static int fio_client_connect_ip(struct fio_client *client)
Jens Axboea37f69b2011-10-01 12:24:21 -0600212{
213 int fd;
Jens Axboe132159a2011-09-30 15:01:32 -0600214
Jens Axboeb66570d2011-10-01 11:11:35 -0600215 client->addr.sin_family = AF_INET;
Jens Axboebebe6392011-10-07 10:00:51 +0200216 client->addr.sin_port = htons(client->port);
Jens Axboe132159a2011-09-30 15:01:32 -0600217
218 fd = socket(AF_INET, SOCK_STREAM, 0);
219 if (fd < 0) {
220 log_err("fio: socket: %s\n", strerror(errno));
Jens Axboe87aa8f12011-10-06 21:24:13 +0200221 return -1;
Jens Axboe132159a2011-09-30 15:01:32 -0600222 }
223
Jens Axboeb66570d2011-10-01 11:11:35 -0600224 if (connect(fd, (struct sockaddr *) &client->addr, sizeof(client->addr)) < 0) {
Jens Axboe132159a2011-09-30 15:01:32 -0600225 log_err("fio: connect: %s\n", strerror(errno));
Jens Axboea7de0a12011-10-07 12:55:14 +0200226 log_err("fio: failed to connect to %s:%u\n", client->hostname,
227 client->port);
Jens Axboeb94cba42011-10-06 21:27:10 +0200228 close(fd);
Jens Axboe87aa8f12011-10-06 21:24:13 +0200229 return -1;
Jens Axboe132159a2011-09-30 15:01:32 -0600230 }
231
Jens Axboe87aa8f12011-10-06 21:24:13 +0200232 return fd;
233}
234
235static int fio_client_connect_sock(struct fio_client *client)
236{
237 struct sockaddr_un *addr = &client->addr_un;
238 fio_socklen_t len;
239 int fd;
240
241 memset(addr, 0, sizeof(*addr));
242 addr->sun_family = AF_UNIX;
243 strcpy(addr->sun_path, client->hostname);
244
245 fd = socket(AF_UNIX, SOCK_STREAM, 0);
246 if (fd < 0) {
247 log_err("fio: socket: %s\n", strerror(errno));
248 return -1;
249 }
250
251 len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
252 if (connect(fd, (struct sockaddr *) addr, len) < 0) {
253 log_err("fio: connect; %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200254 close(fd);
Jens Axboe87aa8f12011-10-06 21:24:13 +0200255 return -1;
256 }
257
258 return fd;
259}
260
261static int fio_client_connect(struct fio_client *client)
262{
263 int fd;
264
265 dprint(FD_NET, "client: connect to host %s\n", client->hostname);
266
Jens Axboe87aa8f12011-10-06 21:24:13 +0200267 if (client->is_sock)
268 fd = fio_client_connect_sock(client);
269 else
270 fd = fio_client_connect_ip(client);
271
Jens Axboe89c17072011-10-11 10:15:51 +0200272 dprint(FD_NET, "client: %s connected %d\n", client->hostname, fd);
273
Jens Axboe87aa8f12011-10-06 21:24:13 +0200274 if (fd < 0)
275 return 1;
276
Jens Axboeb66570d2011-10-01 11:11:35 -0600277 client->fd = fd;
Jens Axboebebe6392011-10-07 10:00:51 +0200278 fio_client_add_hash(client);
Jens Axboe81179ee2011-10-04 12:42:06 +0200279 client->state = Client_connected;
Jens Axboe132159a2011-09-30 15:01:32 -0600280 return 0;
281}
282
Jens Axboecc0df002011-10-03 20:53:32 +0200283void fio_clients_terminate(void)
284{
285 struct flist_head *entry;
286 struct fio_client *client;
287
Jens Axboe60efd142011-10-04 13:30:11 +0200288 dprint(FD_NET, "client: terminate clients\n");
289
Jens Axboecc0df002011-10-03 20:53:32 +0200290 flist_for_each(entry, &client_list) {
291 client = flist_entry(entry, struct fio_client, list);
292
Jens Axboe89c17072011-10-11 10:15:51 +0200293 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_QUIT, 0, NULL);
Jens Axboecc0df002011-10-03 20:53:32 +0200294 }
295}
296
297static void sig_int(int sig)
298{
Jens Axboebebe6392011-10-07 10:00:51 +0200299 dprint(FD_NET, "client: got signal %d\n", sig);
Jens Axboecc0df002011-10-03 20:53:32 +0200300 fio_clients_terminate();
301}
302
303static void client_signal_handler(void)
304{
305 struct sigaction act;
306
307 memset(&act, 0, sizeof(act));
308 act.sa_handler = sig_int;
309 act.sa_flags = SA_RESTART;
310 sigaction(SIGINT, &act, NULL);
311
312 memset(&act, 0, sizeof(act));
313 act.sa_handler = sig_int;
314 act.sa_flags = SA_RESTART;
315 sigaction(SIGTERM, &act, NULL);
316}
317
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200318static void probe_client(struct fio_client *client)
319{
Jens Axboe60efd142011-10-04 13:30:11 +0200320 dprint(FD_NET, "client: send probe\n");
321
Jens Axboe89c17072011-10-11 10:15:51 +0200322 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_PROBE, 0, &client->cmd_list);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200323}
324
Jens Axboe81179ee2011-10-04 12:42:06 +0200325static int send_client_cmd_line(struct fio_client *client)
326{
Jens Axboefa2ea802011-10-08 21:07:29 +0200327 struct cmd_single_line_pdu *cslp;
328 struct cmd_line_pdu *clp;
329 unsigned long offset;
Jens Axboe7f868312011-10-10 09:55:21 +0200330 unsigned int *lens;
Jens Axboefa2ea802011-10-08 21:07:29 +0200331 void *pdu;
332 size_t mem;
Jens Axboe81179ee2011-10-04 12:42:06 +0200333 int i, ret;
334
Jens Axboe39e8e012011-10-04 13:46:08 +0200335 dprint(FD_NET, "client: send cmdline %d\n", client->argc);
Jens Axboe60efd142011-10-04 13:30:11 +0200336
Jens Axboe7f868312011-10-10 09:55:21 +0200337 lens = malloc(client->argc * sizeof(unsigned int));
338
Jens Axboefa2ea802011-10-08 21:07:29 +0200339 /*
340 * Find out how much mem we need
341 */
Jens Axboe7f868312011-10-10 09:55:21 +0200342 for (i = 0, mem = 0; i < client->argc; i++) {
343 lens[i] = strlen(client->argv[i]) + 1;
344 mem += lens[i];
345 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200346
Jens Axboefa2ea802011-10-08 21:07:29 +0200347 /*
348 * We need one cmd_line_pdu, and argc number of cmd_single_line_pdu
349 */
350 mem += sizeof(*clp) + (client->argc * sizeof(*cslp));
351
352 pdu = malloc(mem);
353 clp = pdu;
354 offset = sizeof(*clp);
355
356 for (i = 0; i < client->argc; i++) {
Jens Axboe7f868312011-10-10 09:55:21 +0200357 uint16_t arg_len = lens[i];
Jens Axboefa2ea802011-10-08 21:07:29 +0200358
359 cslp = pdu + offset;
360 strcpy((char *) cslp->text, client->argv[i]);
361 cslp->len = cpu_to_le16(arg_len);
362 offset += sizeof(*cslp) + arg_len;
363 }
364
Jens Axboe7f868312011-10-10 09:55:21 +0200365 free(lens);
Jens Axboefa2ea802011-10-08 21:07:29 +0200366 clp->lines = cpu_to_le16(client->argc);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200367 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOBLINE, pdu, mem, 0);
Jens Axboe81179ee2011-10-04 12:42:06 +0200368 free(pdu);
369 return ret;
370}
371
Jens Axboea37f69b2011-10-01 12:24:21 -0600372int fio_clients_connect(void)
373{
374 struct fio_client *client;
375 struct flist_head *entry, *tmp;
376 int ret;
377
Jens Axboe60efd142011-10-04 13:30:11 +0200378 dprint(FD_NET, "client: connect all\n");
379
Jens Axboecc0df002011-10-03 20:53:32 +0200380 client_signal_handler();
381
Jens Axboea37f69b2011-10-01 12:24:21 -0600382 flist_for_each_safe(entry, tmp, &client_list) {
383 client = flist_entry(entry, struct fio_client, list);
384
385 ret = fio_client_connect(client);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200386 if (ret) {
Jens Axboea37f69b2011-10-01 12:24:21 -0600387 remove_client(client);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200388 continue;
389 }
390
391 probe_client(client);
Jens Axboe81179ee2011-10-04 12:42:06 +0200392
393 if (client->argc > 1)
394 send_client_cmd_line(client);
Jens Axboea37f69b2011-10-01 12:24:21 -0600395 }
396
397 return !nr_clients;
398}
399
Jens Axboe132159a2011-09-30 15:01:32 -0600400/*
401 * Send file contents to server backend. We could use sendfile(), but to remain
402 * more portable lets just read/write the darn thing.
403 */
Jens Axboea37f69b2011-10-01 12:24:21 -0600404static int fio_client_send_ini(struct fio_client *client, const char *filename)
Jens Axboe132159a2011-09-30 15:01:32 -0600405{
406 struct stat sb;
407 char *p, *buf;
408 off_t len;
409 int fd, ret;
410
Jens Axboe46c48f12011-10-01 12:50:51 -0600411 dprint(FD_NET, "send ini %s to %s\n", filename, client->hostname);
412
Jens Axboe132159a2011-09-30 15:01:32 -0600413 fd = open(filename, O_RDONLY);
414 if (fd < 0) {
Jens Axboee951bdc2011-10-05 21:58:45 +0200415 log_err("fio: job file <%s> open: %s\n", filename, strerror(errno));
Jens Axboe132159a2011-09-30 15:01:32 -0600416 return 1;
417 }
418
419 if (fstat(fd, &sb) < 0) {
420 log_err("fio: job file stat: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200421 close(fd);
Jens Axboe132159a2011-09-30 15:01:32 -0600422 return 1;
423 }
424
425 buf = malloc(sb.st_size);
426
427 len = sb.st_size;
428 p = buf;
429 do {
430 ret = read(fd, p, len);
431 if (ret > 0) {
432 len -= ret;
433 if (!len)
434 break;
435 p += ret;
436 continue;
437 } else if (!ret)
438 break;
439 else if (errno == EAGAIN || errno == EINTR)
440 continue;
441 } while (1);
442
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200443 if (len) {
444 log_err("fio: failed reading job file %s\n", filename);
Jens Axboeb94cba42011-10-06 21:27:10 +0200445 close(fd);
Jens Axboec524ef72011-10-07 12:23:34 +0200446 free(buf);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200447 return 1;
448 }
449
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200450 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, buf, sb.st_size, 0);
Jens Axboe132159a2011-09-30 15:01:32 -0600451 free(buf);
Jens Axboeb94cba42011-10-06 21:27:10 +0200452 close(fd);
Jens Axboe132159a2011-09-30 15:01:32 -0600453 return ret;
454}
Jens Axboe37db14f2011-09-30 17:00:42 -0600455
Jens Axboea37f69b2011-10-01 12:24:21 -0600456int fio_clients_send_ini(const char *filename)
457{
458 struct fio_client *client;
459 struct flist_head *entry, *tmp;
460
461 flist_for_each_safe(entry, tmp, &client_list) {
462 client = flist_entry(entry, struct fio_client, list);
463
464 if (fio_client_send_ini(client, filename))
465 remove_client(client);
466 }
467
468 return !nr_clients;
469}
470
Jens Axboea64e88d2011-10-03 14:20:01 +0200471static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
472{
473 dst->max_val = le64_to_cpu(src->max_val);
474 dst->min_val = le64_to_cpu(src->min_val);
475 dst->samples = le64_to_cpu(src->samples);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200476
477 /*
478 * Floats arrive as IEEE 754 encoded uint64_t, convert back to double
479 */
480 dst->mean.u.f = fio_uint64_to_double(le64_to_cpu(dst->mean.u.i));
481 dst->S.u.f = fio_uint64_to_double(le64_to_cpu(dst->S.u.i));
Jens Axboea64e88d2011-10-03 14:20:01 +0200482}
483
484static void convert_ts(struct thread_stat *dst, struct thread_stat *src)
485{
486 int i, j;
487
488 dst->error = le32_to_cpu(src->error);
489 dst->groupid = le32_to_cpu(src->groupid);
490 dst->pid = le32_to_cpu(src->pid);
491 dst->members = le32_to_cpu(src->members);
492
493 for (i = 0; i < 2; i++) {
494 convert_io_stat(&dst->clat_stat[i], &src->clat_stat[i]);
495 convert_io_stat(&dst->slat_stat[i], &src->slat_stat[i]);
496 convert_io_stat(&dst->lat_stat[i], &src->lat_stat[i]);
497 convert_io_stat(&dst->bw_stat[i], &src->bw_stat[i]);
498 }
499
500 dst->usr_time = le64_to_cpu(src->usr_time);
501 dst->sys_time = le64_to_cpu(src->sys_time);
502 dst->ctx = le64_to_cpu(src->ctx);
503 dst->minf = le64_to_cpu(src->minf);
504 dst->majf = le64_to_cpu(src->majf);
505 dst->clat_percentiles = le64_to_cpu(src->clat_percentiles);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200506
507 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
508 fio_fp64_t *fps = &src->percentile_list[i];
509 fio_fp64_t *fpd = &dst->percentile_list[i];
510
511 fpd->u.f = fio_uint64_to_double(le64_to_cpu(fps->u.i));
512 }
Jens Axboea64e88d2011-10-03 14:20:01 +0200513
514 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
515 dst->io_u_map[i] = le32_to_cpu(src->io_u_map[i]);
516 dst->io_u_submit[i] = le32_to_cpu(src->io_u_submit[i]);
517 dst->io_u_complete[i] = le32_to_cpu(src->io_u_complete[i]);
518 }
519
520 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
521 dst->io_u_lat_u[i] = le32_to_cpu(src->io_u_lat_u[i]);
522 dst->io_u_lat_m[i] = le32_to_cpu(src->io_u_lat_m[i]);
523 }
524
525 for (i = 0; i < 2; i++)
526 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
527 dst->io_u_plat[i][j] = le32_to_cpu(src->io_u_plat[i][j]);
528
529 for (i = 0; i < 3; i++) {
530 dst->total_io_u[i] = le64_to_cpu(src->total_io_u[i]);
Jens Axboe93eee042011-10-03 21:08:48 +0200531 dst->short_io_u[i] = le64_to_cpu(src->short_io_u[i]);
Jens Axboea64e88d2011-10-03 14:20:01 +0200532 }
533
534 dst->total_submit = le64_to_cpu(src->total_submit);
535 dst->total_complete = le64_to_cpu(src->total_complete);
536
537 for (i = 0; i < 2; i++) {
538 dst->io_bytes[i] = le64_to_cpu(src->io_bytes[i]);
539 dst->runtime[i] = le64_to_cpu(src->runtime[i]);
540 }
541
542 dst->total_run_time = le64_to_cpu(src->total_run_time);
543 dst->continue_on_error = le16_to_cpu(src->continue_on_error);
544 dst->total_err_count = le64_to_cpu(src->total_err_count);
Jens Axboeddcc0b62011-10-03 14:45:27 +0200545 dst->first_error = le32_to_cpu(src->first_error);
546 dst->kb_base = le32_to_cpu(src->kb_base);
Jens Axboea64e88d2011-10-03 14:20:01 +0200547}
548
549static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
550{
551 int i;
552
553 for (i = 0; i < 2; i++) {
554 dst->max_run[i] = le64_to_cpu(src->max_run[i]);
555 dst->min_run[i] = le64_to_cpu(src->min_run[i]);
556 dst->max_bw[i] = le64_to_cpu(src->max_bw[i]);
557 dst->min_bw[i] = le64_to_cpu(src->min_bw[i]);
558 dst->io_kb[i] = le64_to_cpu(src->io_kb[i]);
559 dst->agg[i] = le64_to_cpu(src->agg[i]);
560 }
561
562 dst->kb_base = le32_to_cpu(src->kb_base);
563 dst->groupid = le32_to_cpu(src->groupid);
564}
565
566static void handle_ts(struct fio_net_cmd *cmd)
567{
568 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
569
570 convert_ts(&p->ts, &p->ts);
571 convert_gs(&p->rs, &p->rs);
572
573 show_thread_status(&p->ts, &p->rs);
574}
575
576static void handle_gs(struct fio_net_cmd *cmd)
577{
578 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
579
580 convert_gs(gs, gs);
581 show_group_stats(gs);
582}
583
Jens Axboe48fbb462011-10-09 12:19:08 +0200584static void convert_jobs_eta(struct jobs_eta *je)
Jens Axboecf451d12011-10-03 16:48:30 +0200585{
Jens Axboecf451d12011-10-03 16:48:30 +0200586 int i;
587
588 je->nr_running = le32_to_cpu(je->nr_running);
589 je->nr_ramp = le32_to_cpu(je->nr_ramp);
590 je->nr_pending = le32_to_cpu(je->nr_pending);
591 je->files_open = le32_to_cpu(je->files_open);
592 je->m_rate = le32_to_cpu(je->m_rate);
593 je->t_rate = le32_to_cpu(je->t_rate);
594 je->m_iops = le32_to_cpu(je->m_iops);
595 je->t_iops = le32_to_cpu(je->t_iops);
596
597 for (i = 0; i < 2; i++) {
598 je->rate[i] = le32_to_cpu(je->rate[i]);
599 je->iops[i] = le32_to_cpu(je->iops[i]);
600 }
601
Jens Axboeb51eedb2011-10-09 12:13:39 +0200602 je->elapsed_sec = le64_to_cpu(je->elapsed_sec);
Jens Axboecf451d12011-10-03 16:48:30 +0200603 je->eta_sec = le64_to_cpu(je->eta_sec);
Jens Axboe48fbb462011-10-09 12:19:08 +0200604}
Jens Axboecf451d12011-10-03 16:48:30 +0200605
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200606static void sum_jobs_eta(struct jobs_eta *dst, struct jobs_eta *je)
Jens Axboe48fbb462011-10-09 12:19:08 +0200607{
Jens Axboe48fbb462011-10-09 12:19:08 +0200608 int i;
609
610 dst->nr_running += je->nr_running;
611 dst->nr_ramp += je->nr_ramp;
612 dst->nr_pending += je->nr_pending;
613 dst->files_open += je->files_open;
614 dst->m_rate += je->m_rate;
615 dst->t_rate += je->t_rate;
616 dst->m_iops += je->m_iops;
617 dst->t_iops += je->t_iops;
618
619 for (i = 0; i < 2; i++) {
620 dst->rate[i] += je->rate[i];
621 dst->iops[i] += je->iops[i];
622 }
623
624 dst->elapsed_sec += je->elapsed_sec;
625
626 if (je->eta_sec > dst->eta_sec)
627 dst->eta_sec = je->eta_sec;
628}
629
Jens Axboe82c1ed32011-10-10 08:56:18 +0200630static void dec_jobs_eta(struct client_eta *eta)
631{
632 if (!--eta->pending) {
633 display_thread_status(&eta->eta);
634 free(eta);
635 }
636}
637
Jens Axboe89c17072011-10-11 10:15:51 +0200638static void remove_reply_cmd(struct fio_client *client, struct fio_net_cmd *cmd)
639{
640 struct fio_net_int_cmd *icmd = NULL;
641 struct flist_head *entry;
642
643 flist_for_each(entry, &client->cmd_list) {
644 icmd = flist_entry(entry, struct fio_net_int_cmd, list);
645
646 if (cmd->tag == (uint64_t) icmd)
647 break;
648
649 icmd = NULL;
650 }
651
652 if (!icmd) {
653 log_err("fio: client: unable to find matching tag\n");
654 return;
655 }
656
657 flist_del(&icmd->list);
658 cmd->tag = icmd->saved_tag;
659 free(icmd);
660}
661
Jens Axboe82c1ed32011-10-10 08:56:18 +0200662static void handle_eta(struct fio_client *client, struct fio_net_cmd *cmd)
Jens Axboe48fbb462011-10-09 12:19:08 +0200663{
664 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200665 struct client_eta *eta = (struct client_eta *) cmd->tag;
666
667 dprint(FD_NET, "client: got eta tag %p, %d\n", eta, eta->pending);
Jens Axboe48fbb462011-10-09 12:19:08 +0200668
Jens Axboef77d2672011-10-10 14:36:07 +0200669 assert(client->eta_in_flight == eta);
670
671 client->eta_in_flight = NULL;
Jens Axboe82c1ed32011-10-10 08:56:18 +0200672 flist_del_init(&client->eta_list);
673
Jens Axboe48fbb462011-10-09 12:19:08 +0200674 convert_jobs_eta(je);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200675 sum_jobs_eta(&eta->eta, je);
Jens Axboe82c1ed32011-10-10 08:56:18 +0200676 dec_jobs_eta(eta);
Jens Axboecf451d12011-10-03 16:48:30 +0200677}
678
Jens Axboeb5296dd2011-10-07 16:35:56 +0200679static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd)
Jens Axboe2e03b4b2011-10-04 09:00:40 +0200680{
681 struct cmd_probe_pdu *probe = (struct cmd_probe_pdu *) cmd->payload;
Jens Axboecca84642011-10-07 12:47:57 +0200682 const char *os, *arch;
Jens Axboe2e03b4b2011-10-04 09:00:40 +0200683
Jens Axboecca84642011-10-07 12:47:57 +0200684 os = fio_get_os_string(probe->os);
685 if (!os)
686 os = "unknown";
687
688 arch = fio_get_arch_string(probe->arch);
689 if (!arch)
690 os = "unknown";
691
Jens Axboec71233e2011-10-07 13:11:14 +0200692 log_info("hostname=%s, be=%u, os=%s, arch=%s, fio=%u.%u.%u\n",
Jens Axboecca84642011-10-07 12:47:57 +0200693 probe->hostname, probe->bigendian, os, arch, probe->fio_major,
Jens Axboe6eb24792011-10-04 15:00:30 +0200694 probe->fio_minor, probe->fio_patch);
Jens Axboeb5296dd2011-10-07 16:35:56 +0200695
696 if (!client->name)
697 client->name = strdup((char *) probe->hostname);
Jens Axboe2e03b4b2011-10-04 09:00:40 +0200698}
699
Jens Axboee951bdc2011-10-05 21:58:45 +0200700static int handle_client(struct fio_client *client)
Jens Axboe37db14f2011-09-30 17:00:42 -0600701{
702 struct fio_net_cmd *cmd;
703
Jens Axboe60efd142011-10-04 13:30:11 +0200704 dprint(FD_NET, "client: handle %s\n", client->hostname);
705
Jens Axboee951bdc2011-10-05 21:58:45 +0200706 cmd = fio_net_recv_cmd(client->fd);
707 if (!cmd)
708 return 0;
Jens Axboec2c94582011-10-05 20:31:30 +0200709
Jens Axboe89c17072011-10-11 10:15:51 +0200710 dprint(FD_NET, "client: got cmd op %s from %s\n",
711 fio_server_op(cmd->opcode), client->hostname);
Jens Axboe46c48f12011-10-01 12:50:51 -0600712
Jens Axboee951bdc2011-10-05 21:58:45 +0200713 switch (cmd->opcode) {
714 case FIO_NET_CMD_QUIT:
715 remove_client(client);
716 free(cmd);
717 break;
718 case FIO_NET_CMD_TEXT: {
719 const char *buf = (const char *) cmd->payload;
Jens Axboeb5296dd2011-10-07 16:35:56 +0200720 const char *name;
Jens Axboee951bdc2011-10-05 21:58:45 +0200721 int fio_unused ret;
Jens Axboe17dd1762011-10-04 13:27:34 +0200722
Jens Axboeb5296dd2011-10-07 16:35:56 +0200723 name = client->name ? client->name : client->hostname;
724
Jens Axboee951bdc2011-10-05 21:58:45 +0200725 if (!client->skip_newline)
Jens Axboeb5296dd2011-10-07 16:35:56 +0200726 fprintf(f_out, "<%s> ", name);
Jens Axboee951bdc2011-10-05 21:58:45 +0200727 ret = fwrite(buf, cmd->pdu_len, 1, f_out);
728 fflush(f_out);
729 client->skip_newline = strchr(buf, '\n') == NULL;
730 free(cmd);
731 break;
Jens Axboe37db14f2011-09-30 17:00:42 -0600732 }
Jens Axboee951bdc2011-10-05 21:58:45 +0200733 case FIO_NET_CMD_TS:
734 handle_ts(cmd);
735 free(cmd);
736 break;
737 case FIO_NET_CMD_GS:
738 handle_gs(cmd);
739 free(cmd);
740 break;
741 case FIO_NET_CMD_ETA:
Jens Axboe89c17072011-10-11 10:15:51 +0200742 remove_reply_cmd(client, cmd);
Jens Axboe82c1ed32011-10-10 08:56:18 +0200743 handle_eta(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +0200744 free(cmd);
745 break;
746 case FIO_NET_CMD_PROBE:
Jens Axboe89c17072011-10-11 10:15:51 +0200747 remove_reply_cmd(client, cmd);
Jens Axboeb5296dd2011-10-07 16:35:56 +0200748 handle_probe(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +0200749 free(cmd);
750 break;
751 case FIO_NET_CMD_START:
752 client->state = Client_started;
753 free(cmd);
754 break;
755 case FIO_NET_CMD_STOP:
756 client->state = Client_stopped;
757 free(cmd);
758 break;
759 default:
Jens Axboe89c17072011-10-11 10:15:51 +0200760 log_err("fio: unknown client op: %s\n", fio_server_op(cmd->opcode));
Jens Axboee951bdc2011-10-05 21:58:45 +0200761 free(cmd);
762 break;
Jens Axboe37db14f2011-09-30 17:00:42 -0600763 }
764
Jens Axboee951bdc2011-10-05 21:58:45 +0200765 return 1;
Jens Axboe37db14f2011-09-30 17:00:42 -0600766}
Jens Axboeb66570d2011-10-01 11:11:35 -0600767
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200768static void request_client_etas(void)
769{
770 struct fio_client *client;
771 struct flist_head *entry;
772 struct client_eta *eta;
Jens Axboe82c1ed32011-10-10 08:56:18 +0200773 int skipped = 0;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200774
775 dprint(FD_NET, "client: request eta (%d)\n", nr_clients);
776
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200777 eta = malloc(sizeof(*eta));
778 memset(&eta->eta, 0, sizeof(eta->eta));
779 eta->pending = nr_clients;
780
781 flist_for_each(entry, &client_list) {
782 client = flist_entry(entry, struct fio_client, list);
783
Jens Axboe82c1ed32011-10-10 08:56:18 +0200784 if (!flist_empty(&client->eta_list)) {
785 skipped++;
786 continue;
787 }
788
Jens Axboef77d2672011-10-10 14:36:07 +0200789 assert(!client->eta_in_flight);
Jens Axboe82c1ed32011-10-10 08:56:18 +0200790 flist_add_tail(&client->eta_list, &eta_list);
Jens Axboef77d2672011-10-10 14:36:07 +0200791 client->eta_in_flight = eta;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200792 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_SEND_ETA,
Jens Axboe89c17072011-10-11 10:15:51 +0200793 (uint64_t) eta, &client->cmd_list);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200794 }
795
Jens Axboe82c1ed32011-10-10 08:56:18 +0200796 while (skipped--)
797 dec_jobs_eta(eta);
798
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200799 dprint(FD_NET, "client: requested eta tag %p\n", eta);
800}
801
Jens Axboe89c17072011-10-11 10:15:51 +0200802static int client_check_cmd_timeout(struct fio_client *client,
803 struct timeval *now)
804{
805 struct fio_net_int_cmd *cmd;
806 struct flist_head *entry, *tmp;
807 int ret = 0;
808
809 flist_for_each_safe(entry, tmp, &client->cmd_list) {
810 cmd = flist_entry(entry, struct fio_net_int_cmd, list);
811
812 if (mtime_since(&cmd->tv, now) < FIO_NET_CLIENT_TIMEOUT)
813 continue;
814
815 log_err("fio: client %s, timeout on cmd %s\n", client->hostname,
816 fio_server_op(cmd->cmd.opcode));
817 flist_del(&cmd->list);
818 free(cmd);
819 ret = 1;
820 }
821
822 return flist_empty(&client->cmd_list) && ret;
823}
824
825static int fio_client_timed_out(void)
826{
827 struct fio_client *client;
828 struct flist_head *entry, *tmp;
829 struct timeval tv;
830 int ret = 0;
831
832 gettimeofday(&tv, NULL);
833
834 flist_for_each_safe(entry, tmp, &client_list) {
835 client = flist_entry(entry, struct fio_client, list);
836
837 if (flist_empty(&client->cmd_list))
838 continue;
839
840 if (!client_check_cmd_timeout(client, &tv))
841 continue;
842
843 log_err("fio: client %s timed out\n", client->hostname);
844 remove_client(client);
845 ret = 1;
846 }
847
848 return ret;
849}
850
Jens Axboeb66570d2011-10-01 11:11:35 -0600851int fio_handle_clients(void)
852{
853 struct fio_client *client;
854 struct flist_head *entry;
855 struct pollfd *pfds;
Jens Axboe82a4be12011-10-01 12:40:32 -0600856 int i, ret = 0;
Jens Axboeb66570d2011-10-01 11:11:35 -0600857
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200858 gettimeofday(&eta_tv, NULL);
859
Jens Axboeb66570d2011-10-01 11:11:35 -0600860 pfds = malloc(nr_clients * sizeof(struct pollfd));
861
Jens Axboeb66570d2011-10-01 11:11:35 -0600862 while (!exit_backend && nr_clients) {
Jens Axboe82a4be12011-10-01 12:40:32 -0600863 i = 0;
864 flist_for_each(entry, &client_list) {
865 client = flist_entry(entry, struct fio_client, list);
866
867 pfds[i].fd = client->fd;
868 pfds[i].events = POLLIN;
869 i++;
870 }
871
872 assert(i == nr_clients);
873
Jens Axboe5c2857f2011-10-04 13:14:32 +0200874 do {
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200875 struct timeval tv;
876
877 gettimeofday(&tv, NULL);
878 if (mtime_since(&eta_tv, &tv) >= 900) {
879 request_client_etas();
880 memcpy(&eta_tv, &tv, sizeof(tv));
Jens Axboe89c17072011-10-11 10:15:51 +0200881
882 if (fio_client_timed_out())
883 break;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200884 }
885
Jens Axboe5c2857f2011-10-04 13:14:32 +0200886 ret = poll(pfds, nr_clients, 100);
887 if (ret < 0) {
888 if (errno == EINTR)
889 continue;
890 log_err("fio: poll clients: %s\n", strerror(errno));
891 break;
892 } else if (!ret)
Jens Axboeb66570d2011-10-01 11:11:35 -0600893 continue;
Jens Axboe5c2857f2011-10-04 13:14:32 +0200894 } while (ret <= 0);
Jens Axboeb66570d2011-10-01 11:11:35 -0600895
896 for (i = 0; i < nr_clients; i++) {
897 if (!(pfds[i].revents & POLLIN))
898 continue;
899
900 client = find_client_by_fd(pfds[i].fd);
901 if (!client) {
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200902 log_err("fio: unknown client fd %d\n", pfds[i].fd);
Jens Axboeb66570d2011-10-01 11:11:35 -0600903 continue;
904 }
Jens Axboee951bdc2011-10-05 21:58:45 +0200905 if (!handle_client(client)) {
Jens Axboe28d3ab02011-10-05 20:45:37 +0200906 log_info("client: host=%s disconnected\n",
907 client->hostname);
908 remove_client(client);
909 }
Jens Axboeb66570d2011-10-01 11:11:35 -0600910 }
911 }
912
913 free(pfds);
Jens Axboeb66570d2011-10-01 11:11:35 -0600914 return 0;
915}