blob: 8c85d2b5c6a673f433f2930fbb41c6a70e8b354b [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 Axboe811826b2011-10-24 09:11:50 +020032 union {
33 struct sockaddr_in addr;
34 struct sockaddr_in6 addr6;
35 struct sockaddr_un addr_un;
36 };
Jens Axboeb66570d2011-10-01 11:11:35 -060037 char *hostname;
Jens Axboebebe6392011-10-07 10:00:51 +020038 int port;
Jens Axboeb66570d2011-10-01 11:11:35 -060039 int fd;
Jens Axboe81179ee2011-10-04 12:42:06 +020040
Jens Axboeb5296dd2011-10-07 16:35:56 +020041 char *name;
42
Jens Axboe81179ee2011-10-04 12:42:06 +020043 int state;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +020044
Jens Axboe17dd1762011-10-04 13:27:34 +020045 int skip_newline;
Jens Axboe87aa8f12011-10-06 21:24:13 +020046 int is_sock;
Jens Axboed09a64a2011-10-13 11:38:56 +020047 int disk_stats_shown;
Jens Axboe11e950b2011-10-16 21:34:14 +020048 unsigned int jobs;
49 int error;
Jens Axboe811826b2011-10-24 09:11:50 +020050 int ipv6;
Jens Axboec2cb6862012-02-23 20:56:12 +010051 int sent_job;
Jens Axboe82c1ed32011-10-10 08:56:18 +020052
53 struct flist_head eta_list;
54 struct client_eta *eta_in_flight;
Jens Axboe81179ee2011-10-04 12:42:06 +020055
Jens Axboe89c17072011-10-11 10:15:51 +020056 struct flist_head cmd_list;
57
Jens Axboe81179ee2011-10-04 12:42:06 +020058 uint16_t argc;
59 char **argv;
60};
61
Jens Axboeaf9c9fb2011-10-09 21:54:10 +020062static struct timeval eta_tv;
Jens Axboe48fbb462011-10-09 12:19:08 +020063
Jens Axboe81179ee2011-10-04 12:42:06 +020064enum {
Jens Axboe5c2857f2011-10-04 13:14:32 +020065 Client_created = 0,
Jens Axboe81179ee2011-10-04 12:42:06 +020066 Client_connected = 1,
67 Client_started = 2,
Jens Axboe01be0382011-10-15 14:43:41 +020068 Client_running = 3,
69 Client_stopped = 4,
70 Client_exited = 5,
Jens Axboeb66570d2011-10-01 11:11:35 -060071};
72
73static FLIST_HEAD(client_list);
Jens Axboe82c1ed32011-10-10 08:56:18 +020074static FLIST_HEAD(eta_list);
Jens Axboeb66570d2011-10-01 11:11:35 -060075
Jens Axboe3f3a4542011-10-10 21:11:09 +020076static FLIST_HEAD(arg_list);
77
Jens Axboe37f0c1a2011-10-11 14:08:33 +020078static struct thread_stat client_ts;
79static struct group_run_stats client_gs;
80static int sum_stat_clients;
81static int sum_stat_nr;
82
Jens Axboe3c5f57e2011-10-06 12:37:50 +020083#define FIO_CLIENT_HASH_BITS 7
84#define FIO_CLIENT_HASH_SZ (1 << FIO_CLIENT_HASH_BITS)
85#define FIO_CLIENT_HASH_MASK (FIO_CLIENT_HASH_SZ - 1)
Jens Axboebebe6392011-10-07 10:00:51 +020086static struct flist_head client_hash[FIO_CLIENT_HASH_SZ];
Jens Axboe3c5f57e2011-10-06 12:37:50 +020087
Jens Axboee951bdc2011-10-05 21:58:45 +020088static int handle_client(struct fio_client *client);
Jens Axboe82c1ed32011-10-10 08:56:18 +020089static void dec_jobs_eta(struct client_eta *eta);
Jens Axboe0b8f30a2011-10-04 10:31:53 +020090
Jens Axboebebe6392011-10-07 10:00:51 +020091static void fio_client_add_hash(struct fio_client *client)
Jens Axboe3c5f57e2011-10-06 12:37:50 +020092{
93 int bucket = hash_long(client->fd, FIO_CLIENT_HASH_BITS);
94
95 bucket &= FIO_CLIENT_HASH_MASK;
Jens Axboebebe6392011-10-07 10:00:51 +020096 flist_add(&client->hash_list, &client_hash[bucket]);
Jens Axboe3c5f57e2011-10-06 12:37:50 +020097}
98
Jens Axboebebe6392011-10-07 10:00:51 +020099static void fio_client_remove_hash(struct fio_client *client)
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200100{
Jens Axboebebe6392011-10-07 10:00:51 +0200101 if (!flist_empty(&client->hash_list))
102 flist_del_init(&client->hash_list);
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200103}
104
105static void fio_init fio_client_hash_init(void)
106{
107 int i;
108
Jens Axboebebe6392011-10-07 10:00:51 +0200109 for (i = 0; i < FIO_CLIENT_HASH_SZ; i++)
110 INIT_FLIST_HEAD(&client_hash[i]);
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200111}
112
Jens Axboeb66570d2011-10-01 11:11:35 -0600113static struct fio_client *find_client_by_fd(int fd)
114{
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200115 int bucket = hash_long(fd, FIO_CLIENT_HASH_BITS) & FIO_CLIENT_HASH_MASK;
Jens Axboeb66570d2011-10-01 11:11:35 -0600116 struct fio_client *client;
117 struct flist_head *entry;
118
Jens Axboebebe6392011-10-07 10:00:51 +0200119 flist_for_each(entry, &client_hash[bucket]) {
120 client = flist_entry(entry, struct fio_client, hash_list);
Jens Axboeb66570d2011-10-01 11:11:35 -0600121
122 if (client->fd == fd)
123 return client;
124 }
125
126 return NULL;
127}
128
Jens Axboeb66570d2011-10-01 11:11:35 -0600129static void remove_client(struct fio_client *client)
130{
Jens Axboe39e8e012011-10-04 13:46:08 +0200131 dprint(FD_NET, "client: removed <%s>\n", client->hostname);
Jens Axboeb66570d2011-10-01 11:11:35 -0600132 flist_del(&client->list);
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200133
Jens Axboebebe6392011-10-07 10:00:51 +0200134 fio_client_remove_hash(client);
Jens Axboe81179ee2011-10-04 12:42:06 +0200135
Jens Axboe82c1ed32011-10-10 08:56:18 +0200136 if (!flist_empty(&client->eta_list)) {
137 flist_del_init(&client->eta_list);
138 dec_jobs_eta(client->eta_in_flight);
139 }
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200140
Jens Axboeb66570d2011-10-01 11:11:35 -0600141 free(client->hostname);
Jens Axboe81179ee2011-10-04 12:42:06 +0200142 if (client->argv)
143 free(client->argv);
Jens Axboeb5296dd2011-10-07 16:35:56 +0200144 if (client->name)
145 free(client->name);
Jens Axboe81179ee2011-10-04 12:42:06 +0200146
Jens Axboeb66570d2011-10-01 11:11:35 -0600147 free(client);
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200148 nr_clients--;
Jens Axboe5fd0acb2011-10-11 14:20:22 +0200149 sum_stat_clients--;
Jens Axboeb66570d2011-10-01 11:11:35 -0600150}
Jens Axboe132159a2011-09-30 15:01:32 -0600151
Jens Axboefa2ea802011-10-08 21:07:29 +0200152static void __fio_client_add_cmd_option(struct fio_client *client,
153 const char *opt)
Jens Axboe81179ee2011-10-04 12:42:06 +0200154{
Jens Axboe39e8e012011-10-04 13:46:08 +0200155 int index;
156
157 index = client->argc++;
Jens Axboe81179ee2011-10-04 12:42:06 +0200158 client->argv = realloc(client->argv, sizeof(char *) * client->argc);
Jens Axboe39e8e012011-10-04 13:46:08 +0200159 client->argv[index] = strdup(opt);
160 dprint(FD_NET, "client: add cmd %d: %s\n", index, opt);
Jens Axboe81179ee2011-10-04 12:42:06 +0200161}
162
Jens Axboefa2ea802011-10-08 21:07:29 +0200163void fio_client_add_cmd_option(void *cookie, const char *opt)
Jens Axboe81179ee2011-10-04 12:42:06 +0200164{
Jens Axboebebe6392011-10-07 10:00:51 +0200165 struct fio_client *client = cookie;
Jens Axboe3f3a4542011-10-10 21:11:09 +0200166 struct flist_head *entry;
Jens Axboe81179ee2011-10-04 12:42:06 +0200167
Jens Axboebebe6392011-10-07 10:00:51 +0200168 if (!client || !opt)
Jens Axboefa2ea802011-10-08 21:07:29 +0200169 return;
Jens Axboe81179ee2011-10-04 12:42:06 +0200170
Jens Axboefa2ea802011-10-08 21:07:29 +0200171 __fio_client_add_cmd_option(client, opt);
Jens Axboe3f3a4542011-10-10 21:11:09 +0200172
173 /*
174 * Duplicate arguments to shared client group
175 */
176 flist_for_each(entry, &arg_list) {
177 client = flist_entry(entry, struct fio_client, arg_list);
178
179 __fio_client_add_cmd_option(client, opt);
180 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200181}
182
Jens Axboebebe6392011-10-07 10:00:51 +0200183int fio_client_add(const char *hostname, void **cookie)
Jens Axboe132159a2011-09-30 15:01:32 -0600184{
Jens Axboe3f3a4542011-10-10 21:11:09 +0200185 struct fio_client *existing = *cookie;
Jens Axboeb66570d2011-10-01 11:11:35 -0600186 struct fio_client *client;
Jens Axboe132159a2011-09-30 15:01:32 -0600187
Jens Axboe3f3a4542011-10-10 21:11:09 +0200188 if (existing) {
189 /*
190 * We always add our "exec" name as the option, hence 1
191 * means empty.
192 */
193 if (existing->argc == 1)
194 flist_add_tail(&existing->arg_list, &arg_list);
195 else {
196 while (!flist_empty(&arg_list))
197 flist_del_init(arg_list.next);
198 }
199 }
200
Jens Axboeb66570d2011-10-01 11:11:35 -0600201 client = malloc(sizeof(*client));
Jens Axboea37f69b2011-10-01 12:24:21 -0600202 memset(client, 0, sizeof(*client));
Jens Axboe81179ee2011-10-04 12:42:06 +0200203
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200204 INIT_FLIST_HEAD(&client->list);
Jens Axboebebe6392011-10-07 10:00:51 +0200205 INIT_FLIST_HEAD(&client->hash_list);
Jens Axboe3f3a4542011-10-10 21:11:09 +0200206 INIT_FLIST_HEAD(&client->arg_list);
Jens Axboe82c1ed32011-10-10 08:56:18 +0200207 INIT_FLIST_HEAD(&client->eta_list);
Jens Axboe89c17072011-10-11 10:15:51 +0200208 INIT_FLIST_HEAD(&client->cmd_list);
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200209
Jens Axboebebe6392011-10-07 10:00:51 +0200210 if (fio_server_parse_string(hostname, &client->hostname,
211 &client->is_sock, &client->port,
Jens Axboe811826b2011-10-24 09:11:50 +0200212 &client->addr.sin_addr,
213 &client->addr6.sin6_addr,
214 &client->ipv6))
Jens Axboebebe6392011-10-07 10:00:51 +0200215 return -1;
216
Jens Axboea37f69b2011-10-01 12:24:21 -0600217 client->fd = -1;
Jens Axboe81179ee2011-10-04 12:42:06 +0200218
219 __fio_client_add_cmd_option(client, "fio");
220
Jens Axboea37f69b2011-10-01 12:24:21 -0600221 flist_add(&client->list, &client_list);
222 nr_clients++;
Jens Axboebebe6392011-10-07 10:00:51 +0200223 dprint(FD_NET, "client: added <%s>\n", client->hostname);
224 *cookie = client;
225 return 0;
Jens Axboea37f69b2011-10-01 12:24:21 -0600226}
227
Jens Axboe87aa8f12011-10-06 21:24:13 +0200228static int fio_client_connect_ip(struct fio_client *client)
Jens Axboea37f69b2011-10-01 12:24:21 -0600229{
Jens Axboe811826b2011-10-24 09:11:50 +0200230 struct sockaddr *addr;
231 fio_socklen_t socklen;
232 int fd, domain;
Jens Axboe132159a2011-09-30 15:01:32 -0600233
Jens Axboe811826b2011-10-24 09:11:50 +0200234 if (client->ipv6) {
235 client->addr6.sin6_family = AF_INET6;
236 client->addr6.sin6_port = htons(client->port);
237 domain = AF_INET6;
238 addr = (struct sockaddr *) &client->addr6;
239 socklen = sizeof(client->addr6);
240 } else {
241 client->addr.sin_family = AF_INET;
242 client->addr.sin_port = htons(client->port);
243 domain = AF_INET;
244 addr = (struct sockaddr *) &client->addr;
245 socklen = sizeof(client->addr);
246 }
Jens Axboe132159a2011-09-30 15:01:32 -0600247
Jens Axboe811826b2011-10-24 09:11:50 +0200248 fd = socket(domain, SOCK_STREAM, 0);
Jens Axboe132159a2011-09-30 15:01:32 -0600249 if (fd < 0) {
250 log_err("fio: socket: %s\n", strerror(errno));
Jens Axboe87aa8f12011-10-06 21:24:13 +0200251 return -1;
Jens Axboe132159a2011-09-30 15:01:32 -0600252 }
253
Jens Axboe811826b2011-10-24 09:11:50 +0200254 if (connect(fd, addr, socklen) < 0) {
Jens Axboe132159a2011-09-30 15:01:32 -0600255 log_err("fio: connect: %s\n", strerror(errno));
Jens Axboea7de0a12011-10-07 12:55:14 +0200256 log_err("fio: failed to connect to %s:%u\n", client->hostname,
257 client->port);
Jens Axboeb94cba42011-10-06 21:27:10 +0200258 close(fd);
Jens Axboe87aa8f12011-10-06 21:24:13 +0200259 return -1;
Jens Axboe132159a2011-09-30 15:01:32 -0600260 }
261
Jens Axboe87aa8f12011-10-06 21:24:13 +0200262 return fd;
263}
264
265static int fio_client_connect_sock(struct fio_client *client)
266{
267 struct sockaddr_un *addr = &client->addr_un;
268 fio_socklen_t len;
269 int fd;
270
271 memset(addr, 0, sizeof(*addr));
272 addr->sun_family = AF_UNIX;
273 strcpy(addr->sun_path, client->hostname);
274
275 fd = socket(AF_UNIX, SOCK_STREAM, 0);
276 if (fd < 0) {
277 log_err("fio: socket: %s\n", strerror(errno));
278 return -1;
279 }
280
281 len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
282 if (connect(fd, (struct sockaddr *) addr, len) < 0) {
283 log_err("fio: connect; %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200284 close(fd);
Jens Axboe87aa8f12011-10-06 21:24:13 +0200285 return -1;
286 }
287
288 return fd;
289}
290
291static int fio_client_connect(struct fio_client *client)
292{
293 int fd;
294
295 dprint(FD_NET, "client: connect to host %s\n", client->hostname);
296
Jens Axboe87aa8f12011-10-06 21:24:13 +0200297 if (client->is_sock)
298 fd = fio_client_connect_sock(client);
299 else
300 fd = fio_client_connect_ip(client);
301
Jens Axboe89c17072011-10-11 10:15:51 +0200302 dprint(FD_NET, "client: %s connected %d\n", client->hostname, fd);
303
Jens Axboe87aa8f12011-10-06 21:24:13 +0200304 if (fd < 0)
305 return 1;
306
Jens Axboeb66570d2011-10-01 11:11:35 -0600307 client->fd = fd;
Jens Axboebebe6392011-10-07 10:00:51 +0200308 fio_client_add_hash(client);
Jens Axboe81179ee2011-10-04 12:42:06 +0200309 client->state = Client_connected;
Jens Axboe132159a2011-09-30 15:01:32 -0600310 return 0;
311}
312
Jens Axboecc0df002011-10-03 20:53:32 +0200313void fio_clients_terminate(void)
314{
315 struct flist_head *entry;
316 struct fio_client *client;
317
Jens Axboe60efd142011-10-04 13:30:11 +0200318 dprint(FD_NET, "client: terminate clients\n");
319
Jens Axboecc0df002011-10-03 20:53:32 +0200320 flist_for_each(entry, &client_list) {
321 client = flist_entry(entry, struct fio_client, list);
322
Jens Axboe89c17072011-10-11 10:15:51 +0200323 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_QUIT, 0, NULL);
Jens Axboecc0df002011-10-03 20:53:32 +0200324 }
325}
326
327static void sig_int(int sig)
328{
Jens Axboebebe6392011-10-07 10:00:51 +0200329 dprint(FD_NET, "client: got signal %d\n", sig);
Jens Axboecc0df002011-10-03 20:53:32 +0200330 fio_clients_terminate();
331}
332
333static void client_signal_handler(void)
334{
335 struct sigaction act;
336
337 memset(&act, 0, sizeof(act));
338 act.sa_handler = sig_int;
339 act.sa_flags = SA_RESTART;
340 sigaction(SIGINT, &act, NULL);
341
342 memset(&act, 0, sizeof(act));
343 act.sa_handler = sig_int;
344 act.sa_flags = SA_RESTART;
345 sigaction(SIGTERM, &act, NULL);
346}
347
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200348static void probe_client(struct fio_client *client)
349{
Jens Axboe60efd142011-10-04 13:30:11 +0200350 dprint(FD_NET, "client: send probe\n");
351
Jens Axboe89c17072011-10-11 10:15:51 +0200352 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_PROBE, 0, &client->cmd_list);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200353}
354
Jens Axboe81179ee2011-10-04 12:42:06 +0200355static int send_client_cmd_line(struct fio_client *client)
356{
Jens Axboefa2ea802011-10-08 21:07:29 +0200357 struct cmd_single_line_pdu *cslp;
358 struct cmd_line_pdu *clp;
359 unsigned long offset;
Jens Axboe7f868312011-10-10 09:55:21 +0200360 unsigned int *lens;
Jens Axboefa2ea802011-10-08 21:07:29 +0200361 void *pdu;
362 size_t mem;
Jens Axboe81179ee2011-10-04 12:42:06 +0200363 int i, ret;
364
Jens Axboe39e8e012011-10-04 13:46:08 +0200365 dprint(FD_NET, "client: send cmdline %d\n", client->argc);
Jens Axboe60efd142011-10-04 13:30:11 +0200366
Jens Axboe7f868312011-10-10 09:55:21 +0200367 lens = malloc(client->argc * sizeof(unsigned int));
368
Jens Axboefa2ea802011-10-08 21:07:29 +0200369 /*
370 * Find out how much mem we need
371 */
Jens Axboe7f868312011-10-10 09:55:21 +0200372 for (i = 0, mem = 0; i < client->argc; i++) {
373 lens[i] = strlen(client->argv[i]) + 1;
374 mem += lens[i];
375 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200376
Jens Axboefa2ea802011-10-08 21:07:29 +0200377 /*
378 * We need one cmd_line_pdu, and argc number of cmd_single_line_pdu
379 */
380 mem += sizeof(*clp) + (client->argc * sizeof(*cslp));
381
382 pdu = malloc(mem);
383 clp = pdu;
384 offset = sizeof(*clp);
385
386 for (i = 0; i < client->argc; i++) {
Jens Axboe7f868312011-10-10 09:55:21 +0200387 uint16_t arg_len = lens[i];
Jens Axboefa2ea802011-10-08 21:07:29 +0200388
389 cslp = pdu + offset;
390 strcpy((char *) cslp->text, client->argv[i]);
391 cslp->len = cpu_to_le16(arg_len);
392 offset += sizeof(*cslp) + arg_len;
393 }
394
Jens Axboe7f868312011-10-10 09:55:21 +0200395 free(lens);
Jens Axboefa2ea802011-10-08 21:07:29 +0200396 clp->lines = cpu_to_le16(client->argc);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200397 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOBLINE, pdu, mem, 0);
Jens Axboe81179ee2011-10-04 12:42:06 +0200398 free(pdu);
399 return ret;
400}
401
Jens Axboea37f69b2011-10-01 12:24:21 -0600402int fio_clients_connect(void)
403{
404 struct fio_client *client;
405 struct flist_head *entry, *tmp;
406 int ret;
407
Bruce Cran93bcfd22012-02-20 20:18:19 +0100408#ifdef WIN32
409 WSADATA wsd;
410 WSAStartup(MAKEWORD(2,2), &wsd);
411#endif
412
Jens Axboe60efd142011-10-04 13:30:11 +0200413 dprint(FD_NET, "client: connect all\n");
414
Jens Axboecc0df002011-10-03 20:53:32 +0200415 client_signal_handler();
416
Jens Axboea37f69b2011-10-01 12:24:21 -0600417 flist_for_each_safe(entry, tmp, &client_list) {
418 client = flist_entry(entry, struct fio_client, list);
419
420 ret = fio_client_connect(client);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200421 if (ret) {
Jens Axboea37f69b2011-10-01 12:24:21 -0600422 remove_client(client);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200423 continue;
424 }
425
426 probe_client(client);
Jens Axboe81179ee2011-10-04 12:42:06 +0200427
428 if (client->argc > 1)
429 send_client_cmd_line(client);
Jens Axboea37f69b2011-10-01 12:24:21 -0600430 }
431
432 return !nr_clients;
433}
434
Jens Axboe132159a2011-09-30 15:01:32 -0600435/*
436 * Send file contents to server backend. We could use sendfile(), but to remain
437 * more portable lets just read/write the darn thing.
438 */
Jens Axboea37f69b2011-10-01 12:24:21 -0600439static int fio_client_send_ini(struct fio_client *client, const char *filename)
Jens Axboe132159a2011-09-30 15:01:32 -0600440{
441 struct stat sb;
442 char *p, *buf;
443 off_t len;
444 int fd, ret;
445
Jens Axboe46c48f12011-10-01 12:50:51 -0600446 dprint(FD_NET, "send ini %s to %s\n", filename, client->hostname);
447
Jens Axboe132159a2011-09-30 15:01:32 -0600448 fd = open(filename, O_RDONLY);
449 if (fd < 0) {
Jens Axboee951bdc2011-10-05 21:58:45 +0200450 log_err("fio: job file <%s> open: %s\n", filename, strerror(errno));
Jens Axboe132159a2011-09-30 15:01:32 -0600451 return 1;
452 }
453
454 if (fstat(fd, &sb) < 0) {
455 log_err("fio: job file stat: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200456 close(fd);
Jens Axboe132159a2011-09-30 15:01:32 -0600457 return 1;
458 }
459
460 buf = malloc(sb.st_size);
461
462 len = sb.st_size;
463 p = buf;
464 do {
465 ret = read(fd, p, len);
466 if (ret > 0) {
467 len -= ret;
468 if (!len)
469 break;
470 p += ret;
471 continue;
472 } else if (!ret)
473 break;
474 else if (errno == EAGAIN || errno == EINTR)
475 continue;
476 } while (1);
477
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200478 if (len) {
479 log_err("fio: failed reading job file %s\n", filename);
Jens Axboeb94cba42011-10-06 21:27:10 +0200480 close(fd);
Jens Axboec524ef72011-10-07 12:23:34 +0200481 free(buf);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200482 return 1;
483 }
484
Jens Axboec2cb6862012-02-23 20:56:12 +0100485 client->sent_job = 1;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200486 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, buf, sb.st_size, 0);
Jens Axboe132159a2011-09-30 15:01:32 -0600487 free(buf);
Jens Axboeb94cba42011-10-06 21:27:10 +0200488 close(fd);
Jens Axboe132159a2011-09-30 15:01:32 -0600489 return ret;
490}
Jens Axboe37db14f2011-09-30 17:00:42 -0600491
Jens Axboea37f69b2011-10-01 12:24:21 -0600492int fio_clients_send_ini(const char *filename)
493{
494 struct fio_client *client;
495 struct flist_head *entry, *tmp;
496
497 flist_for_each_safe(entry, tmp, &client_list) {
498 client = flist_entry(entry, struct fio_client, list);
499
500 if (fio_client_send_ini(client, filename))
501 remove_client(client);
Jens Axboec2cb6862012-02-23 20:56:12 +0100502
503 client->sent_job = 1;
Jens Axboea37f69b2011-10-01 12:24:21 -0600504 }
505
506 return !nr_clients;
507}
508
Jens Axboea64e88d2011-10-03 14:20:01 +0200509static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
510{
511 dst->max_val = le64_to_cpu(src->max_val);
512 dst->min_val = le64_to_cpu(src->min_val);
513 dst->samples = le64_to_cpu(src->samples);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200514
515 /*
516 * Floats arrive as IEEE 754 encoded uint64_t, convert back to double
517 */
518 dst->mean.u.f = fio_uint64_to_double(le64_to_cpu(dst->mean.u.i));
519 dst->S.u.f = fio_uint64_to_double(le64_to_cpu(dst->S.u.i));
Jens Axboea64e88d2011-10-03 14:20:01 +0200520}
521
522static void convert_ts(struct thread_stat *dst, struct thread_stat *src)
523{
524 int i, j;
525
526 dst->error = le32_to_cpu(src->error);
527 dst->groupid = le32_to_cpu(src->groupid);
528 dst->pid = le32_to_cpu(src->pid);
529 dst->members = le32_to_cpu(src->members);
530
531 for (i = 0; i < 2; i++) {
532 convert_io_stat(&dst->clat_stat[i], &src->clat_stat[i]);
533 convert_io_stat(&dst->slat_stat[i], &src->slat_stat[i]);
534 convert_io_stat(&dst->lat_stat[i], &src->lat_stat[i]);
535 convert_io_stat(&dst->bw_stat[i], &src->bw_stat[i]);
536 }
537
538 dst->usr_time = le64_to_cpu(src->usr_time);
539 dst->sys_time = le64_to_cpu(src->sys_time);
540 dst->ctx = le64_to_cpu(src->ctx);
541 dst->minf = le64_to_cpu(src->minf);
542 dst->majf = le64_to_cpu(src->majf);
543 dst->clat_percentiles = le64_to_cpu(src->clat_percentiles);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200544
545 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
546 fio_fp64_t *fps = &src->percentile_list[i];
547 fio_fp64_t *fpd = &dst->percentile_list[i];
548
549 fpd->u.f = fio_uint64_to_double(le64_to_cpu(fps->u.i));
550 }
Jens Axboea64e88d2011-10-03 14:20:01 +0200551
552 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
553 dst->io_u_map[i] = le32_to_cpu(src->io_u_map[i]);
554 dst->io_u_submit[i] = le32_to_cpu(src->io_u_submit[i]);
555 dst->io_u_complete[i] = le32_to_cpu(src->io_u_complete[i]);
556 }
557
558 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
559 dst->io_u_lat_u[i] = le32_to_cpu(src->io_u_lat_u[i]);
560 dst->io_u_lat_m[i] = le32_to_cpu(src->io_u_lat_m[i]);
561 }
562
563 for (i = 0; i < 2; i++)
564 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
565 dst->io_u_plat[i][j] = le32_to_cpu(src->io_u_plat[i][j]);
566
567 for (i = 0; i < 3; i++) {
568 dst->total_io_u[i] = le64_to_cpu(src->total_io_u[i]);
Jens Axboe93eee042011-10-03 21:08:48 +0200569 dst->short_io_u[i] = le64_to_cpu(src->short_io_u[i]);
Jens Axboea64e88d2011-10-03 14:20:01 +0200570 }
571
572 dst->total_submit = le64_to_cpu(src->total_submit);
573 dst->total_complete = le64_to_cpu(src->total_complete);
574
575 for (i = 0; i < 2; i++) {
576 dst->io_bytes[i] = le64_to_cpu(src->io_bytes[i]);
577 dst->runtime[i] = le64_to_cpu(src->runtime[i]);
578 }
579
580 dst->total_run_time = le64_to_cpu(src->total_run_time);
581 dst->continue_on_error = le16_to_cpu(src->continue_on_error);
582 dst->total_err_count = le64_to_cpu(src->total_err_count);
Jens Axboeddcc0b62011-10-03 14:45:27 +0200583 dst->first_error = le32_to_cpu(src->first_error);
584 dst->kb_base = le32_to_cpu(src->kb_base);
Jens Axboea64e88d2011-10-03 14:20:01 +0200585}
586
587static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
588{
589 int i;
590
591 for (i = 0; i < 2; i++) {
592 dst->max_run[i] = le64_to_cpu(src->max_run[i]);
593 dst->min_run[i] = le64_to_cpu(src->min_run[i]);
594 dst->max_bw[i] = le64_to_cpu(src->max_bw[i]);
595 dst->min_bw[i] = le64_to_cpu(src->min_bw[i]);
596 dst->io_kb[i] = le64_to_cpu(src->io_kb[i]);
597 dst->agg[i] = le64_to_cpu(src->agg[i]);
598 }
599
600 dst->kb_base = le32_to_cpu(src->kb_base);
601 dst->groupid = le32_to_cpu(src->groupid);
602}
603
604static void handle_ts(struct fio_net_cmd *cmd)
605{
606 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
607
608 convert_ts(&p->ts, &p->ts);
609 convert_gs(&p->rs, &p->rs);
610
611 show_thread_status(&p->ts, &p->rs);
Jens Axboe37f0c1a2011-10-11 14:08:33 +0200612
613 if (sum_stat_clients == 1)
614 return;
615
616 sum_thread_stats(&client_ts, &p->ts, sum_stat_nr);
617 sum_group_stats(&client_gs, &p->rs);
618
619 client_ts.members++;
620 client_ts.groupid = p->ts.groupid;
621
622 if (++sum_stat_nr == sum_stat_clients) {
623 strcpy(client_ts.name, "All clients");
624 show_thread_status(&client_ts, &client_gs);
625 }
Jens Axboea64e88d2011-10-03 14:20:01 +0200626}
627
628static void handle_gs(struct fio_net_cmd *cmd)
629{
630 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
631
632 convert_gs(gs, gs);
633 show_group_stats(gs);
634}
635
Jens Axboed09a64a2011-10-13 11:38:56 +0200636static void convert_agg(struct disk_util_agg *agg)
637{
638 int i;
639
640 for (i = 0; i < 2; i++) {
641 agg->ios[i] = le32_to_cpu(agg->ios[i]);
642 agg->merges[i] = le32_to_cpu(agg->merges[i]);
643 agg->sectors[i] = le64_to_cpu(agg->sectors[i]);
644 agg->ticks[i] = le32_to_cpu(agg->ticks[i]);
645 }
646
647 agg->io_ticks = le32_to_cpu(agg->io_ticks);
648 agg->time_in_queue = le32_to_cpu(agg->time_in_queue);
649 agg->slavecount = le32_to_cpu(agg->slavecount);
Anton Blanchard823ba542011-11-07 14:16:26 +0100650 agg->max_util.u.f = fio_uint64_to_double(__le64_to_cpu(agg->max_util.u.i));
Jens Axboed09a64a2011-10-13 11:38:56 +0200651}
652
653static void convert_dus(struct disk_util_stat *dus)
654{
655 int i;
656
657 for (i = 0; i < 2; i++) {
658 dus->ios[i] = le32_to_cpu(dus->ios[i]);
659 dus->merges[i] = le32_to_cpu(dus->merges[i]);
660 dus->sectors[i] = le64_to_cpu(dus->sectors[i]);
661 dus->ticks[i] = le32_to_cpu(dus->ticks[i]);
662 }
663
664 dus->io_ticks = le32_to_cpu(dus->io_ticks);
665 dus->time_in_queue = le32_to_cpu(dus->time_in_queue);
666 dus->msec = le64_to_cpu(dus->msec);
667}
668
669static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd)
670{
671 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
672
673 convert_dus(&du->dus);
674 convert_agg(&du->agg);
675
676 if (!client->disk_stats_shown) {
677 client->disk_stats_shown = 1;
678 log_info("\nDisk stats (read/write):\n");
679 }
680
Jens Axboef2f788d2011-10-13 14:03:52 +0200681 print_disk_util(&du->dus, &du->agg, terse_output);
Jens Axboed09a64a2011-10-13 11:38:56 +0200682}
683
Jens Axboe48fbb462011-10-09 12:19:08 +0200684static void convert_jobs_eta(struct jobs_eta *je)
Jens Axboecf451d12011-10-03 16:48:30 +0200685{
Jens Axboecf451d12011-10-03 16:48:30 +0200686 int i;
687
688 je->nr_running = le32_to_cpu(je->nr_running);
689 je->nr_ramp = le32_to_cpu(je->nr_ramp);
690 je->nr_pending = le32_to_cpu(je->nr_pending);
691 je->files_open = le32_to_cpu(je->files_open);
692 je->m_rate = le32_to_cpu(je->m_rate);
693 je->t_rate = le32_to_cpu(je->t_rate);
694 je->m_iops = le32_to_cpu(je->m_iops);
695 je->t_iops = le32_to_cpu(je->t_iops);
696
697 for (i = 0; i < 2; i++) {
698 je->rate[i] = le32_to_cpu(je->rate[i]);
699 je->iops[i] = le32_to_cpu(je->iops[i]);
700 }
701
Jens Axboeb51eedb2011-10-09 12:13:39 +0200702 je->elapsed_sec = le64_to_cpu(je->elapsed_sec);
Jens Axboecf451d12011-10-03 16:48:30 +0200703 je->eta_sec = le64_to_cpu(je->eta_sec);
Jens Axboe48fbb462011-10-09 12:19:08 +0200704}
Jens Axboecf451d12011-10-03 16:48:30 +0200705
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200706static void sum_jobs_eta(struct jobs_eta *dst, struct jobs_eta *je)
Jens Axboe48fbb462011-10-09 12:19:08 +0200707{
Jens Axboe48fbb462011-10-09 12:19:08 +0200708 int i;
709
710 dst->nr_running += je->nr_running;
711 dst->nr_ramp += je->nr_ramp;
712 dst->nr_pending += je->nr_pending;
713 dst->files_open += je->files_open;
714 dst->m_rate += je->m_rate;
715 dst->t_rate += je->t_rate;
716 dst->m_iops += je->m_iops;
717 dst->t_iops += je->t_iops;
718
719 for (i = 0; i < 2; i++) {
720 dst->rate[i] += je->rate[i];
721 dst->iops[i] += je->iops[i];
722 }
723
724 dst->elapsed_sec += je->elapsed_sec;
725
726 if (je->eta_sec > dst->eta_sec)
727 dst->eta_sec = je->eta_sec;
728}
729
Jens Axboe82c1ed32011-10-10 08:56:18 +0200730static void dec_jobs_eta(struct client_eta *eta)
731{
732 if (!--eta->pending) {
733 display_thread_status(&eta->eta);
734 free(eta);
735 }
736}
737
Jens Axboe89c17072011-10-11 10:15:51 +0200738static void remove_reply_cmd(struct fio_client *client, struct fio_net_cmd *cmd)
739{
740 struct fio_net_int_cmd *icmd = NULL;
741 struct flist_head *entry;
742
743 flist_for_each(entry, &client->cmd_list) {
744 icmd = flist_entry(entry, struct fio_net_int_cmd, list);
745
Jens Axboedf380932011-10-11 14:25:08 +0200746 if (cmd->tag == (uintptr_t) icmd)
Jens Axboe89c17072011-10-11 10:15:51 +0200747 break;
748
749 icmd = NULL;
750 }
751
752 if (!icmd) {
753 log_err("fio: client: unable to find matching tag\n");
754 return;
755 }
756
757 flist_del(&icmd->list);
758 cmd->tag = icmd->saved_tag;
759 free(icmd);
760}
761
Jens Axboe82c1ed32011-10-10 08:56:18 +0200762static void handle_eta(struct fio_client *client, struct fio_net_cmd *cmd)
Jens Axboe48fbb462011-10-09 12:19:08 +0200763{
764 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
Jens Axboedf380932011-10-11 14:25:08 +0200765 struct client_eta *eta = (struct client_eta *) (uintptr_t) cmd->tag;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200766
767 dprint(FD_NET, "client: got eta tag %p, %d\n", eta, eta->pending);
Jens Axboe48fbb462011-10-09 12:19:08 +0200768
Jens Axboef77d2672011-10-10 14:36:07 +0200769 assert(client->eta_in_flight == eta);
770
771 client->eta_in_flight = NULL;
Jens Axboe82c1ed32011-10-10 08:56:18 +0200772 flist_del_init(&client->eta_list);
773
Jens Axboe48fbb462011-10-09 12:19:08 +0200774 convert_jobs_eta(je);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200775 sum_jobs_eta(&eta->eta, je);
Jens Axboe82c1ed32011-10-10 08:56:18 +0200776 dec_jobs_eta(eta);
Jens Axboecf451d12011-10-03 16:48:30 +0200777}
778
Jens Axboeb5296dd2011-10-07 16:35:56 +0200779static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd)
Jens Axboe2e03b4b2011-10-04 09:00:40 +0200780{
781 struct cmd_probe_pdu *probe = (struct cmd_probe_pdu *) cmd->payload;
Jens Axboed2333352011-10-11 15:07:23 +0200782 const char *os, *arch;
783 char bit[16];
Jens Axboe2e03b4b2011-10-04 09:00:40 +0200784
Jens Axboecca84642011-10-07 12:47:57 +0200785 os = fio_get_os_string(probe->os);
786 if (!os)
787 os = "unknown";
788
789 arch = fio_get_arch_string(probe->arch);
790 if (!arch)
791 os = "unknown";
792
Jens Axboed2333352011-10-11 15:07:23 +0200793 sprintf(bit, "%d-bit", probe->bpp * 8);
Jens Axboe38fdef22011-10-11 14:30:06 +0200794
795 log_info("hostname=%s, be=%u, %s, os=%s, arch=%s, fio=%u.%u.%u\n",
796 probe->hostname, probe->bigendian, bit, os, arch,
797 probe->fio_major, probe->fio_minor, probe->fio_patch);
Jens Axboeb5296dd2011-10-07 16:35:56 +0200798
799 if (!client->name)
800 client->name = strdup((char *) probe->hostname);
Jens Axboe2e03b4b2011-10-04 09:00:40 +0200801}
802
Jens Axboe11e950b2011-10-16 21:34:14 +0200803static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd)
804{
805 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
806
807 client->state = Client_started;
808 client->jobs = le32_to_cpu(pdu->jobs);
809}
810
811static void handle_stop(struct fio_client *client, struct fio_net_cmd *cmd)
812{
813 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
814
815 client->state = Client_stopped;
816 client->error = le32_to_cpu(pdu->error);
Jens Axboe498c92c2011-10-17 09:14:42 +0200817
818 if (client->error)
819 log_info("client <%s>: exited with error %d\n", client->hostname, client->error);
Jens Axboe11e950b2011-10-16 21:34:14 +0200820}
821
Jens Axboee951bdc2011-10-05 21:58:45 +0200822static int handle_client(struct fio_client *client)
Jens Axboe37db14f2011-09-30 17:00:42 -0600823{
824 struct fio_net_cmd *cmd;
825
Jens Axboe60efd142011-10-04 13:30:11 +0200826 dprint(FD_NET, "client: handle %s\n", client->hostname);
827
Jens Axboee951bdc2011-10-05 21:58:45 +0200828 cmd = fio_net_recv_cmd(client->fd);
829 if (!cmd)
830 return 0;
Jens Axboec2c94582011-10-05 20:31:30 +0200831
Jens Axboe89c17072011-10-11 10:15:51 +0200832 dprint(FD_NET, "client: got cmd op %s from %s\n",
833 fio_server_op(cmd->opcode), client->hostname);
Jens Axboe46c48f12011-10-01 12:50:51 -0600834
Jens Axboee951bdc2011-10-05 21:58:45 +0200835 switch (cmd->opcode) {
836 case FIO_NET_CMD_QUIT:
837 remove_client(client);
838 free(cmd);
839 break;
840 case FIO_NET_CMD_TEXT: {
841 const char *buf = (const char *) cmd->payload;
Jens Axboeb5296dd2011-10-07 16:35:56 +0200842 const char *name;
Jens Axboee951bdc2011-10-05 21:58:45 +0200843 int fio_unused ret;
Jens Axboe17dd1762011-10-04 13:27:34 +0200844
Jens Axboeb5296dd2011-10-07 16:35:56 +0200845 name = client->name ? client->name : client->hostname;
846
Jens Axboee951bdc2011-10-05 21:58:45 +0200847 if (!client->skip_newline)
Jens Axboeb5296dd2011-10-07 16:35:56 +0200848 fprintf(f_out, "<%s> ", name);
Jens Axboee951bdc2011-10-05 21:58:45 +0200849 ret = fwrite(buf, cmd->pdu_len, 1, f_out);
850 fflush(f_out);
851 client->skip_newline = strchr(buf, '\n') == NULL;
852 free(cmd);
853 break;
Jens Axboe37db14f2011-09-30 17:00:42 -0600854 }
Jens Axboed09a64a2011-10-13 11:38:56 +0200855 case FIO_NET_CMD_DU:
856 handle_du(client, cmd);
857 free(cmd);
858 break;
Jens Axboee951bdc2011-10-05 21:58:45 +0200859 case FIO_NET_CMD_TS:
860 handle_ts(cmd);
861 free(cmd);
862 break;
863 case FIO_NET_CMD_GS:
864 handle_gs(cmd);
865 free(cmd);
866 break;
867 case FIO_NET_CMD_ETA:
Jens Axboe89c17072011-10-11 10:15:51 +0200868 remove_reply_cmd(client, cmd);
Jens Axboe82c1ed32011-10-10 08:56:18 +0200869 handle_eta(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +0200870 free(cmd);
871 break;
872 case FIO_NET_CMD_PROBE:
Jens Axboe89c17072011-10-11 10:15:51 +0200873 remove_reply_cmd(client, cmd);
Jens Axboeb5296dd2011-10-07 16:35:56 +0200874 handle_probe(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +0200875 free(cmd);
876 break;
Jens Axboe01be0382011-10-15 14:43:41 +0200877 case FIO_NET_CMD_RUN:
878 client->state = Client_running;
879 free(cmd);
880 break;
Jens Axboee951bdc2011-10-05 21:58:45 +0200881 case FIO_NET_CMD_START:
Jens Axboe11e950b2011-10-16 21:34:14 +0200882 handle_start(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +0200883 free(cmd);
884 break;
885 case FIO_NET_CMD_STOP:
Jens Axboe11e950b2011-10-16 21:34:14 +0200886 handle_stop(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +0200887 free(cmd);
888 break;
889 default:
Jens Axboe89c17072011-10-11 10:15:51 +0200890 log_err("fio: unknown client op: %s\n", fio_server_op(cmd->opcode));
Jens Axboee951bdc2011-10-05 21:58:45 +0200891 free(cmd);
892 break;
Jens Axboe37db14f2011-09-30 17:00:42 -0600893 }
894
Jens Axboee951bdc2011-10-05 21:58:45 +0200895 return 1;
Jens Axboe37db14f2011-09-30 17:00:42 -0600896}
Jens Axboeb66570d2011-10-01 11:11:35 -0600897
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200898static void request_client_etas(void)
899{
900 struct fio_client *client;
901 struct flist_head *entry;
902 struct client_eta *eta;
Jens Axboe82c1ed32011-10-10 08:56:18 +0200903 int skipped = 0;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200904
905 dprint(FD_NET, "client: request eta (%d)\n", nr_clients);
906
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200907 eta = malloc(sizeof(*eta));
908 memset(&eta->eta, 0, sizeof(eta->eta));
909 eta->pending = nr_clients;
910
911 flist_for_each(entry, &client_list) {
912 client = flist_entry(entry, struct fio_client, list);
913
Jens Axboe82c1ed32011-10-10 08:56:18 +0200914 if (!flist_empty(&client->eta_list)) {
915 skipped++;
916 continue;
917 }
Jens Axboe01be0382011-10-15 14:43:41 +0200918 if (client->state != Client_running)
919 continue;
Jens Axboe82c1ed32011-10-10 08:56:18 +0200920
Jens Axboef77d2672011-10-10 14:36:07 +0200921 assert(!client->eta_in_flight);
Jens Axboe82c1ed32011-10-10 08:56:18 +0200922 flist_add_tail(&client->eta_list, &eta_list);
Jens Axboef77d2672011-10-10 14:36:07 +0200923 client->eta_in_flight = eta;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200924 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_SEND_ETA,
Jens Axboedf380932011-10-11 14:25:08 +0200925 (uintptr_t) eta, &client->cmd_list);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200926 }
927
Jens Axboe82c1ed32011-10-10 08:56:18 +0200928 while (skipped--)
929 dec_jobs_eta(eta);
930
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200931 dprint(FD_NET, "client: requested eta tag %p\n", eta);
932}
933
Jens Axboe89c17072011-10-11 10:15:51 +0200934static int client_check_cmd_timeout(struct fio_client *client,
935 struct timeval *now)
936{
937 struct fio_net_int_cmd *cmd;
938 struct flist_head *entry, *tmp;
939 int ret = 0;
940
941 flist_for_each_safe(entry, tmp, &client->cmd_list) {
942 cmd = flist_entry(entry, struct fio_net_int_cmd, list);
943
944 if (mtime_since(&cmd->tv, now) < FIO_NET_CLIENT_TIMEOUT)
945 continue;
946
947 log_err("fio: client %s, timeout on cmd %s\n", client->hostname,
948 fio_server_op(cmd->cmd.opcode));
949 flist_del(&cmd->list);
950 free(cmd);
951 ret = 1;
952 }
953
954 return flist_empty(&client->cmd_list) && ret;
955}
956
957static int fio_client_timed_out(void)
958{
959 struct fio_client *client;
960 struct flist_head *entry, *tmp;
961 struct timeval tv;
962 int ret = 0;
963
964 gettimeofday(&tv, NULL);
965
966 flist_for_each_safe(entry, tmp, &client_list) {
967 client = flist_entry(entry, struct fio_client, list);
968
969 if (flist_empty(&client->cmd_list))
970 continue;
971
972 if (!client_check_cmd_timeout(client, &tv))
973 continue;
974
975 log_err("fio: client %s timed out\n", client->hostname);
976 remove_client(client);
977 ret = 1;
978 }
979
980 return ret;
981}
982
Jens Axboeb66570d2011-10-01 11:11:35 -0600983int fio_handle_clients(void)
984{
Jens Axboeb66570d2011-10-01 11:11:35 -0600985 struct pollfd *pfds;
Jens Axboe498c92c2011-10-17 09:14:42 +0200986 int i, ret = 0, retval = 0;
Jens Axboeb66570d2011-10-01 11:11:35 -0600987
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200988 gettimeofday(&eta_tv, NULL);
989
Jens Axboeb66570d2011-10-01 11:11:35 -0600990 pfds = malloc(nr_clients * sizeof(struct pollfd));
991
Jens Axboe37f0c1a2011-10-11 14:08:33 +0200992 sum_stat_clients = nr_clients;
993 init_thread_stat(&client_ts);
994 init_group_run_stat(&client_gs);
995
Jens Axboeb66570d2011-10-01 11:11:35 -0600996 while (!exit_backend && nr_clients) {
Jens Axboec2cb6862012-02-23 20:56:12 +0100997 struct flist_head *entry, *tmp;
998 struct fio_client *client;
999
Jens Axboe82a4be12011-10-01 12:40:32 -06001000 i = 0;
Jens Axboec2cb6862012-02-23 20:56:12 +01001001 flist_for_each_safe(entry, tmp, &client_list) {
Jens Axboe82a4be12011-10-01 12:40:32 -06001002 client = flist_entry(entry, struct fio_client, list);
1003
Jens Axboec2cb6862012-02-23 20:56:12 +01001004 if (!client->sent_job &&
1005 flist_empty(&client->cmd_list)) {
1006 remove_client(client);
1007 continue;
1008 }
1009
Jens Axboe82a4be12011-10-01 12:40:32 -06001010 pfds[i].fd = client->fd;
1011 pfds[i].events = POLLIN;
1012 i++;
1013 }
1014
Jens Axboec2cb6862012-02-23 20:56:12 +01001015 if (!nr_clients)
1016 break;
1017
Jens Axboe82a4be12011-10-01 12:40:32 -06001018 assert(i == nr_clients);
1019
Jens Axboe5c2857f2011-10-04 13:14:32 +02001020 do {
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001021 struct timeval tv;
1022
1023 gettimeofday(&tv, NULL);
1024 if (mtime_since(&eta_tv, &tv) >= 900) {
1025 request_client_etas();
1026 memcpy(&eta_tv, &tv, sizeof(tv));
Jens Axboe89c17072011-10-11 10:15:51 +02001027
1028 if (fio_client_timed_out())
1029 break;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001030 }
1031
Jens Axboe5c2857f2011-10-04 13:14:32 +02001032 ret = poll(pfds, nr_clients, 100);
1033 if (ret < 0) {
1034 if (errno == EINTR)
1035 continue;
1036 log_err("fio: poll clients: %s\n", strerror(errno));
1037 break;
1038 } else if (!ret)
Jens Axboeb66570d2011-10-01 11:11:35 -06001039 continue;
Jens Axboe5c2857f2011-10-04 13:14:32 +02001040 } while (ret <= 0);
Jens Axboeb66570d2011-10-01 11:11:35 -06001041
1042 for (i = 0; i < nr_clients; i++) {
1043 if (!(pfds[i].revents & POLLIN))
1044 continue;
1045
1046 client = find_client_by_fd(pfds[i].fd);
1047 if (!client) {
Jens Axboe3c5f57e2011-10-06 12:37:50 +02001048 log_err("fio: unknown client fd %d\n", pfds[i].fd);
Jens Axboeb66570d2011-10-01 11:11:35 -06001049 continue;
1050 }
Jens Axboee951bdc2011-10-05 21:58:45 +02001051 if (!handle_client(client)) {
Jens Axboe28d3ab02011-10-05 20:45:37 +02001052 log_info("client: host=%s disconnected\n",
1053 client->hostname);
1054 remove_client(client);
Jens Axboe498c92c2011-10-17 09:14:42 +02001055 retval = 1;
Jens Axboe38990762011-10-17 13:31:33 +02001056 } else if (client->error)
Jens Axboe498c92c2011-10-17 09:14:42 +02001057 retval = 1;
Jens Axboeb66570d2011-10-01 11:11:35 -06001058 }
1059 }
1060
1061 free(pfds);
Jens Axboe498c92c2011-10-17 09:14:42 +02001062 return retval;
Jens Axboeb66570d2011-10-01 11:11:35 -06001063}