blob: ee6765ed7adf9671c5cc131909e80ebe2e5abdff [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 Axboee55f8f32012-03-06 15:42:31 +010040 unsigned int refs;
Jens Axboe81179ee2011-10-04 12:42:06 +020041
Jens Axboeb5296dd2011-10-07 16:35:56 +020042 char *name;
43
Jens Axboe81179ee2011-10-04 12:42:06 +020044 int state;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +020045
Jens Axboe17dd1762011-10-04 13:27:34 +020046 int skip_newline;
Jens Axboe87aa8f12011-10-06 21:24:13 +020047 int is_sock;
Jens Axboed09a64a2011-10-13 11:38:56 +020048 int disk_stats_shown;
Jens Axboe11e950b2011-10-16 21:34:14 +020049 unsigned int jobs;
50 int error;
Jens Axboe811826b2011-10-24 09:11:50 +020051 int ipv6;
Jens Axboec2cb6862012-02-23 20:56:12 +010052 int sent_job;
Jens Axboe82c1ed32011-10-10 08:56:18 +020053
54 struct flist_head eta_list;
55 struct client_eta *eta_in_flight;
Jens Axboe81179ee2011-10-04 12:42:06 +020056
Jens Axboe89c17072011-10-11 10:15:51 +020057 struct flist_head cmd_list;
58
Jens Axboe81179ee2011-10-04 12:42:06 +020059 uint16_t argc;
60 char **argv;
61};
62
Jens Axboeaf9c9fb2011-10-09 21:54:10 +020063static struct timeval eta_tv;
Jens Axboe48fbb462011-10-09 12:19:08 +020064
Jens Axboe81179ee2011-10-04 12:42:06 +020065enum {
Jens Axboe5c2857f2011-10-04 13:14:32 +020066 Client_created = 0,
Jens Axboe81179ee2011-10-04 12:42:06 +020067 Client_connected = 1,
68 Client_started = 2,
Jens Axboe01be0382011-10-15 14:43:41 +020069 Client_running = 3,
70 Client_stopped = 4,
71 Client_exited = 5,
Jens Axboeb66570d2011-10-01 11:11:35 -060072};
73
74static FLIST_HEAD(client_list);
Jens Axboe82c1ed32011-10-10 08:56:18 +020075static FLIST_HEAD(eta_list);
Jens Axboeb66570d2011-10-01 11:11:35 -060076
Jens Axboe3f3a4542011-10-10 21:11:09 +020077static FLIST_HEAD(arg_list);
78
Jens Axboe37f0c1a2011-10-11 14:08:33 +020079static struct thread_stat client_ts;
80static struct group_run_stats client_gs;
81static int sum_stat_clients;
82static int sum_stat_nr;
83
Jens Axboe3c5f57e2011-10-06 12:37:50 +020084#define FIO_CLIENT_HASH_BITS 7
85#define FIO_CLIENT_HASH_SZ (1 << FIO_CLIENT_HASH_BITS)
86#define FIO_CLIENT_HASH_MASK (FIO_CLIENT_HASH_SZ - 1)
Jens Axboebebe6392011-10-07 10:00:51 +020087static struct flist_head client_hash[FIO_CLIENT_HASH_SZ];
Jens Axboe3c5f57e2011-10-06 12:37:50 +020088
Jens Axboee951bdc2011-10-05 21:58:45 +020089static int handle_client(struct fio_client *client);
Jens Axboe82c1ed32011-10-10 08:56:18 +020090static void dec_jobs_eta(struct client_eta *eta);
Jens Axboe0b8f30a2011-10-04 10:31:53 +020091
Jens Axboebebe6392011-10-07 10:00:51 +020092static void fio_client_add_hash(struct fio_client *client)
Jens Axboe3c5f57e2011-10-06 12:37:50 +020093{
94 int bucket = hash_long(client->fd, FIO_CLIENT_HASH_BITS);
95
96 bucket &= FIO_CLIENT_HASH_MASK;
Jens Axboebebe6392011-10-07 10:00:51 +020097 flist_add(&client->hash_list, &client_hash[bucket]);
Jens Axboe3c5f57e2011-10-06 12:37:50 +020098}
99
Jens Axboebebe6392011-10-07 10:00:51 +0200100static void fio_client_remove_hash(struct fio_client *client)
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200101{
Jens Axboebebe6392011-10-07 10:00:51 +0200102 if (!flist_empty(&client->hash_list))
103 flist_del_init(&client->hash_list);
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200104}
105
106static void fio_init fio_client_hash_init(void)
107{
108 int i;
109
Jens Axboebebe6392011-10-07 10:00:51 +0200110 for (i = 0; i < FIO_CLIENT_HASH_SZ; i++)
111 INIT_FLIST_HEAD(&client_hash[i]);
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200112}
113
Jens Axboeb66570d2011-10-01 11:11:35 -0600114static struct fio_client *find_client_by_fd(int fd)
115{
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200116 int bucket = hash_long(fd, FIO_CLIENT_HASH_BITS) & FIO_CLIENT_HASH_MASK;
Jens Axboeb66570d2011-10-01 11:11:35 -0600117 struct fio_client *client;
118 struct flist_head *entry;
119
Jens Axboebebe6392011-10-07 10:00:51 +0200120 flist_for_each(entry, &client_hash[bucket]) {
121 client = flist_entry(entry, struct fio_client, hash_list);
Jens Axboeb66570d2011-10-01 11:11:35 -0600122
Jens Axboee55f8f32012-03-06 15:42:31 +0100123 if (client->fd == fd) {
124 client->refs++;
Jens Axboeb66570d2011-10-01 11:11:35 -0600125 return client;
Jens Axboee55f8f32012-03-06 15:42:31 +0100126 }
Jens Axboeb66570d2011-10-01 11:11:35 -0600127 }
128
129 return NULL;
130}
131
Jens Axboeb66570d2011-10-01 11:11:35 -0600132static void remove_client(struct fio_client *client)
133{
Jens Axboee55f8f32012-03-06 15:42:31 +0100134 assert(client->refs);
135
136 if (--client->refs)
137 return;
138
Jens Axboe39e8e012011-10-04 13:46:08 +0200139 dprint(FD_NET, "client: removed <%s>\n", client->hostname);
Jens Axboeb66570d2011-10-01 11:11:35 -0600140 flist_del(&client->list);
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200141
Jens Axboebebe6392011-10-07 10:00:51 +0200142 fio_client_remove_hash(client);
Jens Axboe81179ee2011-10-04 12:42:06 +0200143
Jens Axboe82c1ed32011-10-10 08:56:18 +0200144 if (!flist_empty(&client->eta_list)) {
145 flist_del_init(&client->eta_list);
146 dec_jobs_eta(client->eta_in_flight);
147 }
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200148
Jens Axboeb66570d2011-10-01 11:11:35 -0600149 free(client->hostname);
Jens Axboe81179ee2011-10-04 12:42:06 +0200150 if (client->argv)
151 free(client->argv);
Jens Axboeb5296dd2011-10-07 16:35:56 +0200152 if (client->name)
153 free(client->name);
Jens Axboe81179ee2011-10-04 12:42:06 +0200154
Jens Axboeb66570d2011-10-01 11:11:35 -0600155 free(client);
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200156 nr_clients--;
Jens Axboe5fd0acb2011-10-11 14:20:22 +0200157 sum_stat_clients--;
Jens Axboeb66570d2011-10-01 11:11:35 -0600158}
Jens Axboe132159a2011-09-30 15:01:32 -0600159
Jens Axboee55f8f32012-03-06 15:42:31 +0100160static void put_client(struct fio_client *client)
161{
162 remove_client(client);
163}
164
Jens Axboefa2ea802011-10-08 21:07:29 +0200165static void __fio_client_add_cmd_option(struct fio_client *client,
166 const char *opt)
Jens Axboe81179ee2011-10-04 12:42:06 +0200167{
Jens Axboe39e8e012011-10-04 13:46:08 +0200168 int index;
169
170 index = client->argc++;
Jens Axboe81179ee2011-10-04 12:42:06 +0200171 client->argv = realloc(client->argv, sizeof(char *) * client->argc);
Jens Axboe39e8e012011-10-04 13:46:08 +0200172 client->argv[index] = strdup(opt);
173 dprint(FD_NET, "client: add cmd %d: %s\n", index, opt);
Jens Axboe81179ee2011-10-04 12:42:06 +0200174}
175
Jens Axboefa2ea802011-10-08 21:07:29 +0200176void fio_client_add_cmd_option(void *cookie, const char *opt)
Jens Axboe81179ee2011-10-04 12:42:06 +0200177{
Jens Axboebebe6392011-10-07 10:00:51 +0200178 struct fio_client *client = cookie;
Jens Axboe3f3a4542011-10-10 21:11:09 +0200179 struct flist_head *entry;
Jens Axboe81179ee2011-10-04 12:42:06 +0200180
Jens Axboebebe6392011-10-07 10:00:51 +0200181 if (!client || !opt)
Jens Axboefa2ea802011-10-08 21:07:29 +0200182 return;
Jens Axboe81179ee2011-10-04 12:42:06 +0200183
Jens Axboefa2ea802011-10-08 21:07:29 +0200184 __fio_client_add_cmd_option(client, opt);
Jens Axboe3f3a4542011-10-10 21:11:09 +0200185
186 /*
187 * Duplicate arguments to shared client group
188 */
189 flist_for_each(entry, &arg_list) {
190 client = flist_entry(entry, struct fio_client, arg_list);
191
192 __fio_client_add_cmd_option(client, opt);
193 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200194}
195
Jens Axboebebe6392011-10-07 10:00:51 +0200196int fio_client_add(const char *hostname, void **cookie)
Jens Axboe132159a2011-09-30 15:01:32 -0600197{
Jens Axboe3f3a4542011-10-10 21:11:09 +0200198 struct fio_client *existing = *cookie;
Jens Axboeb66570d2011-10-01 11:11:35 -0600199 struct fio_client *client;
Jens Axboe132159a2011-09-30 15:01:32 -0600200
Jens Axboe3f3a4542011-10-10 21:11:09 +0200201 if (existing) {
202 /*
203 * We always add our "exec" name as the option, hence 1
204 * means empty.
205 */
206 if (existing->argc == 1)
207 flist_add_tail(&existing->arg_list, &arg_list);
208 else {
209 while (!flist_empty(&arg_list))
210 flist_del_init(arg_list.next);
211 }
212 }
213
Jens Axboeb66570d2011-10-01 11:11:35 -0600214 client = malloc(sizeof(*client));
Jens Axboea37f69b2011-10-01 12:24:21 -0600215 memset(client, 0, sizeof(*client));
Jens Axboe81179ee2011-10-04 12:42:06 +0200216
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200217 INIT_FLIST_HEAD(&client->list);
Jens Axboebebe6392011-10-07 10:00:51 +0200218 INIT_FLIST_HEAD(&client->hash_list);
Jens Axboe3f3a4542011-10-10 21:11:09 +0200219 INIT_FLIST_HEAD(&client->arg_list);
Jens Axboe82c1ed32011-10-10 08:56:18 +0200220 INIT_FLIST_HEAD(&client->eta_list);
Jens Axboe89c17072011-10-11 10:15:51 +0200221 INIT_FLIST_HEAD(&client->cmd_list);
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200222
Jens Axboebebe6392011-10-07 10:00:51 +0200223 if (fio_server_parse_string(hostname, &client->hostname,
224 &client->is_sock, &client->port,
Jens Axboe811826b2011-10-24 09:11:50 +0200225 &client->addr.sin_addr,
226 &client->addr6.sin6_addr,
227 &client->ipv6))
Jens Axboebebe6392011-10-07 10:00:51 +0200228 return -1;
229
Jens Axboea37f69b2011-10-01 12:24:21 -0600230 client->fd = -1;
Jens Axboee55f8f32012-03-06 15:42:31 +0100231 client->refs = 1;
Jens Axboe81179ee2011-10-04 12:42:06 +0200232
233 __fio_client_add_cmd_option(client, "fio");
234
Jens Axboea37f69b2011-10-01 12:24:21 -0600235 flist_add(&client->list, &client_list);
236 nr_clients++;
Jens Axboebebe6392011-10-07 10:00:51 +0200237 dprint(FD_NET, "client: added <%s>\n", client->hostname);
238 *cookie = client;
239 return 0;
Jens Axboea37f69b2011-10-01 12:24:21 -0600240}
241
Jens Axboe87aa8f12011-10-06 21:24:13 +0200242static int fio_client_connect_ip(struct fio_client *client)
Jens Axboea37f69b2011-10-01 12:24:21 -0600243{
Jens Axboe811826b2011-10-24 09:11:50 +0200244 struct sockaddr *addr;
245 fio_socklen_t socklen;
246 int fd, domain;
Jens Axboe132159a2011-09-30 15:01:32 -0600247
Jens Axboe811826b2011-10-24 09:11:50 +0200248 if (client->ipv6) {
249 client->addr6.sin6_family = AF_INET6;
250 client->addr6.sin6_port = htons(client->port);
251 domain = AF_INET6;
252 addr = (struct sockaddr *) &client->addr6;
253 socklen = sizeof(client->addr6);
254 } else {
255 client->addr.sin_family = AF_INET;
256 client->addr.sin_port = htons(client->port);
257 domain = AF_INET;
258 addr = (struct sockaddr *) &client->addr;
259 socklen = sizeof(client->addr);
260 }
Jens Axboe132159a2011-09-30 15:01:32 -0600261
Jens Axboe811826b2011-10-24 09:11:50 +0200262 fd = socket(domain, SOCK_STREAM, 0);
Jens Axboe132159a2011-09-30 15:01:32 -0600263 if (fd < 0) {
264 log_err("fio: socket: %s\n", strerror(errno));
Jens Axboe87aa8f12011-10-06 21:24:13 +0200265 return -1;
Jens Axboe132159a2011-09-30 15:01:32 -0600266 }
267
Jens Axboe811826b2011-10-24 09:11:50 +0200268 if (connect(fd, addr, socklen) < 0) {
Jens Axboe132159a2011-09-30 15:01:32 -0600269 log_err("fio: connect: %s\n", strerror(errno));
Jens Axboea7de0a12011-10-07 12:55:14 +0200270 log_err("fio: failed to connect to %s:%u\n", client->hostname,
271 client->port);
Jens Axboeb94cba42011-10-06 21:27:10 +0200272 close(fd);
Jens Axboe87aa8f12011-10-06 21:24:13 +0200273 return -1;
Jens Axboe132159a2011-09-30 15:01:32 -0600274 }
275
Jens Axboe87aa8f12011-10-06 21:24:13 +0200276 return fd;
277}
278
279static int fio_client_connect_sock(struct fio_client *client)
280{
281 struct sockaddr_un *addr = &client->addr_un;
282 fio_socklen_t len;
283 int fd;
284
285 memset(addr, 0, sizeof(*addr));
286 addr->sun_family = AF_UNIX;
287 strcpy(addr->sun_path, client->hostname);
288
289 fd = socket(AF_UNIX, SOCK_STREAM, 0);
290 if (fd < 0) {
291 log_err("fio: socket: %s\n", strerror(errno));
292 return -1;
293 }
294
295 len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
296 if (connect(fd, (struct sockaddr *) addr, len) < 0) {
297 log_err("fio: connect; %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200298 close(fd);
Jens Axboe87aa8f12011-10-06 21:24:13 +0200299 return -1;
300 }
301
302 return fd;
303}
304
305static int fio_client_connect(struct fio_client *client)
306{
307 int fd;
308
309 dprint(FD_NET, "client: connect to host %s\n", client->hostname);
310
Jens Axboe87aa8f12011-10-06 21:24:13 +0200311 if (client->is_sock)
312 fd = fio_client_connect_sock(client);
313 else
314 fd = fio_client_connect_ip(client);
315
Jens Axboe89c17072011-10-11 10:15:51 +0200316 dprint(FD_NET, "client: %s connected %d\n", client->hostname, fd);
317
Jens Axboe87aa8f12011-10-06 21:24:13 +0200318 if (fd < 0)
319 return 1;
320
Jens Axboeb66570d2011-10-01 11:11:35 -0600321 client->fd = fd;
Jens Axboebebe6392011-10-07 10:00:51 +0200322 fio_client_add_hash(client);
Jens Axboe81179ee2011-10-04 12:42:06 +0200323 client->state = Client_connected;
Jens Axboe132159a2011-09-30 15:01:32 -0600324 return 0;
325}
326
Jens Axboecc0df002011-10-03 20:53:32 +0200327void fio_clients_terminate(void)
328{
329 struct flist_head *entry;
330 struct fio_client *client;
331
Jens Axboe60efd142011-10-04 13:30:11 +0200332 dprint(FD_NET, "client: terminate clients\n");
333
Jens Axboecc0df002011-10-03 20:53:32 +0200334 flist_for_each(entry, &client_list) {
335 client = flist_entry(entry, struct fio_client, list);
336
Jens Axboe89c17072011-10-11 10:15:51 +0200337 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_QUIT, 0, NULL);
Jens Axboecc0df002011-10-03 20:53:32 +0200338 }
339}
340
341static void sig_int(int sig)
342{
Jens Axboebebe6392011-10-07 10:00:51 +0200343 dprint(FD_NET, "client: got signal %d\n", sig);
Jens Axboecc0df002011-10-03 20:53:32 +0200344 fio_clients_terminate();
345}
346
Jens Axboe4c6d91e2012-03-30 10:30:35 +0200347static void sig_show_status(int sig)
348{
349 show_running_run_stats();
350}
351
Jens Axboecc0df002011-10-03 20:53:32 +0200352static void client_signal_handler(void)
353{
354 struct sigaction act;
355
356 memset(&act, 0, sizeof(act));
357 act.sa_handler = sig_int;
358 act.sa_flags = SA_RESTART;
359 sigaction(SIGINT, &act, NULL);
360
361 memset(&act, 0, sizeof(act));
362 act.sa_handler = sig_int;
363 act.sa_flags = SA_RESTART;
364 sigaction(SIGTERM, &act, NULL);
Jens Axboe4c6d91e2012-03-30 10:30:35 +0200365
366 memset(&act, 0, sizeof(act));
367 act.sa_handler = sig_show_status;
368 act.sa_flags = SA_RESTART;
369 sigaction(SIGUSR1, &act, NULL);
Jens Axboecc0df002011-10-03 20:53:32 +0200370}
371
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200372static void probe_client(struct fio_client *client)
373{
Jens Axboe60efd142011-10-04 13:30:11 +0200374 dprint(FD_NET, "client: send probe\n");
375
Jens Axboe89c17072011-10-11 10:15:51 +0200376 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_PROBE, 0, &client->cmd_list);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200377}
378
Jens Axboe81179ee2011-10-04 12:42:06 +0200379static int send_client_cmd_line(struct fio_client *client)
380{
Jens Axboefa2ea802011-10-08 21:07:29 +0200381 struct cmd_single_line_pdu *cslp;
382 struct cmd_line_pdu *clp;
383 unsigned long offset;
Jens Axboe7f868312011-10-10 09:55:21 +0200384 unsigned int *lens;
Jens Axboefa2ea802011-10-08 21:07:29 +0200385 void *pdu;
386 size_t mem;
Jens Axboe81179ee2011-10-04 12:42:06 +0200387 int i, ret;
388
Jens Axboe39e8e012011-10-04 13:46:08 +0200389 dprint(FD_NET, "client: send cmdline %d\n", client->argc);
Jens Axboe60efd142011-10-04 13:30:11 +0200390
Jens Axboe7f868312011-10-10 09:55:21 +0200391 lens = malloc(client->argc * sizeof(unsigned int));
392
Jens Axboefa2ea802011-10-08 21:07:29 +0200393 /*
394 * Find out how much mem we need
395 */
Jens Axboe7f868312011-10-10 09:55:21 +0200396 for (i = 0, mem = 0; i < client->argc; i++) {
397 lens[i] = strlen(client->argv[i]) + 1;
398 mem += lens[i];
399 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200400
Jens Axboefa2ea802011-10-08 21:07:29 +0200401 /*
402 * We need one cmd_line_pdu, and argc number of cmd_single_line_pdu
403 */
404 mem += sizeof(*clp) + (client->argc * sizeof(*cslp));
405
406 pdu = malloc(mem);
407 clp = pdu;
408 offset = sizeof(*clp);
409
410 for (i = 0; i < client->argc; i++) {
Jens Axboe7f868312011-10-10 09:55:21 +0200411 uint16_t arg_len = lens[i];
Jens Axboefa2ea802011-10-08 21:07:29 +0200412
413 cslp = pdu + offset;
414 strcpy((char *) cslp->text, client->argv[i]);
415 cslp->len = cpu_to_le16(arg_len);
416 offset += sizeof(*cslp) + arg_len;
417 }
418
Jens Axboe7f868312011-10-10 09:55:21 +0200419 free(lens);
Jens Axboefa2ea802011-10-08 21:07:29 +0200420 clp->lines = cpu_to_le16(client->argc);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200421 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOBLINE, pdu, mem, 0);
Jens Axboe81179ee2011-10-04 12:42:06 +0200422 free(pdu);
423 return ret;
424}
425
Jens Axboea37f69b2011-10-01 12:24:21 -0600426int fio_clients_connect(void)
427{
428 struct fio_client *client;
429 struct flist_head *entry, *tmp;
430 int ret;
431
Bruce Cran93bcfd22012-02-20 20:18:19 +0100432#ifdef WIN32
433 WSADATA wsd;
434 WSAStartup(MAKEWORD(2,2), &wsd);
435#endif
436
Jens Axboe60efd142011-10-04 13:30:11 +0200437 dprint(FD_NET, "client: connect all\n");
438
Jens Axboecc0df002011-10-03 20:53:32 +0200439 client_signal_handler();
440
Jens Axboea37f69b2011-10-01 12:24:21 -0600441 flist_for_each_safe(entry, tmp, &client_list) {
442 client = flist_entry(entry, struct fio_client, list);
443
444 ret = fio_client_connect(client);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200445 if (ret) {
Jens Axboea37f69b2011-10-01 12:24:21 -0600446 remove_client(client);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200447 continue;
448 }
449
450 probe_client(client);
Jens Axboe81179ee2011-10-04 12:42:06 +0200451
452 if (client->argc > 1)
453 send_client_cmd_line(client);
Jens Axboea37f69b2011-10-01 12:24:21 -0600454 }
455
456 return !nr_clients;
457}
458
Jens Axboe132159a2011-09-30 15:01:32 -0600459/*
460 * Send file contents to server backend. We could use sendfile(), but to remain
461 * more portable lets just read/write the darn thing.
462 */
Jens Axboea37f69b2011-10-01 12:24:21 -0600463static int fio_client_send_ini(struct fio_client *client, const char *filename)
Jens Axboe132159a2011-09-30 15:01:32 -0600464{
465 struct stat sb;
466 char *p, *buf;
467 off_t len;
468 int fd, ret;
469
Jens Axboe46c48f12011-10-01 12:50:51 -0600470 dprint(FD_NET, "send ini %s to %s\n", filename, client->hostname);
471
Jens Axboe132159a2011-09-30 15:01:32 -0600472 fd = open(filename, O_RDONLY);
473 if (fd < 0) {
Jens Axboee951bdc2011-10-05 21:58:45 +0200474 log_err("fio: job file <%s> open: %s\n", filename, strerror(errno));
Jens Axboe132159a2011-09-30 15:01:32 -0600475 return 1;
476 }
477
478 if (fstat(fd, &sb) < 0) {
479 log_err("fio: job file stat: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200480 close(fd);
Jens Axboe132159a2011-09-30 15:01:32 -0600481 return 1;
482 }
483
484 buf = malloc(sb.st_size);
485
486 len = sb.st_size;
487 p = buf;
488 do {
489 ret = read(fd, p, len);
490 if (ret > 0) {
491 len -= ret;
492 if (!len)
493 break;
494 p += ret;
495 continue;
496 } else if (!ret)
497 break;
498 else if (errno == EAGAIN || errno == EINTR)
499 continue;
500 } while (1);
501
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200502 if (len) {
503 log_err("fio: failed reading job file %s\n", filename);
Jens Axboeb94cba42011-10-06 21:27:10 +0200504 close(fd);
Jens Axboec524ef72011-10-07 12:23:34 +0200505 free(buf);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200506 return 1;
507 }
508
Jens Axboec2cb6862012-02-23 20:56:12 +0100509 client->sent_job = 1;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200510 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, buf, sb.st_size, 0);
Jens Axboe132159a2011-09-30 15:01:32 -0600511 free(buf);
Jens Axboeb94cba42011-10-06 21:27:10 +0200512 close(fd);
Jens Axboe132159a2011-09-30 15:01:32 -0600513 return ret;
514}
Jens Axboe37db14f2011-09-30 17:00:42 -0600515
Jens Axboea37f69b2011-10-01 12:24:21 -0600516int fio_clients_send_ini(const char *filename)
517{
518 struct fio_client *client;
519 struct flist_head *entry, *tmp;
520
521 flist_for_each_safe(entry, tmp, &client_list) {
522 client = flist_entry(entry, struct fio_client, list);
523
524 if (fio_client_send_ini(client, filename))
525 remove_client(client);
Jens Axboec2cb6862012-02-23 20:56:12 +0100526
527 client->sent_job = 1;
Jens Axboea37f69b2011-10-01 12:24:21 -0600528 }
529
530 return !nr_clients;
531}
532
Jens Axboea64e88d2011-10-03 14:20:01 +0200533static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
534{
535 dst->max_val = le64_to_cpu(src->max_val);
536 dst->min_val = le64_to_cpu(src->min_val);
537 dst->samples = le64_to_cpu(src->samples);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200538
539 /*
540 * Floats arrive as IEEE 754 encoded uint64_t, convert back to double
541 */
542 dst->mean.u.f = fio_uint64_to_double(le64_to_cpu(dst->mean.u.i));
543 dst->S.u.f = fio_uint64_to_double(le64_to_cpu(dst->S.u.i));
Jens Axboea64e88d2011-10-03 14:20:01 +0200544}
545
546static void convert_ts(struct thread_stat *dst, struct thread_stat *src)
547{
548 int i, j;
549
550 dst->error = le32_to_cpu(src->error);
551 dst->groupid = le32_to_cpu(src->groupid);
552 dst->pid = le32_to_cpu(src->pid);
553 dst->members = le32_to_cpu(src->members);
554
555 for (i = 0; i < 2; i++) {
556 convert_io_stat(&dst->clat_stat[i], &src->clat_stat[i]);
557 convert_io_stat(&dst->slat_stat[i], &src->slat_stat[i]);
558 convert_io_stat(&dst->lat_stat[i], &src->lat_stat[i]);
559 convert_io_stat(&dst->bw_stat[i], &src->bw_stat[i]);
560 }
561
562 dst->usr_time = le64_to_cpu(src->usr_time);
563 dst->sys_time = le64_to_cpu(src->sys_time);
564 dst->ctx = le64_to_cpu(src->ctx);
565 dst->minf = le64_to_cpu(src->minf);
566 dst->majf = le64_to_cpu(src->majf);
567 dst->clat_percentiles = le64_to_cpu(src->clat_percentiles);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200568
569 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
570 fio_fp64_t *fps = &src->percentile_list[i];
571 fio_fp64_t *fpd = &dst->percentile_list[i];
572
573 fpd->u.f = fio_uint64_to_double(le64_to_cpu(fps->u.i));
574 }
Jens Axboea64e88d2011-10-03 14:20:01 +0200575
576 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
577 dst->io_u_map[i] = le32_to_cpu(src->io_u_map[i]);
578 dst->io_u_submit[i] = le32_to_cpu(src->io_u_submit[i]);
579 dst->io_u_complete[i] = le32_to_cpu(src->io_u_complete[i]);
580 }
581
582 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
583 dst->io_u_lat_u[i] = le32_to_cpu(src->io_u_lat_u[i]);
584 dst->io_u_lat_m[i] = le32_to_cpu(src->io_u_lat_m[i]);
585 }
586
587 for (i = 0; i < 2; i++)
588 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
589 dst->io_u_plat[i][j] = le32_to_cpu(src->io_u_plat[i][j]);
590
591 for (i = 0; i < 3; i++) {
592 dst->total_io_u[i] = le64_to_cpu(src->total_io_u[i]);
Jens Axboe93eee042011-10-03 21:08:48 +0200593 dst->short_io_u[i] = le64_to_cpu(src->short_io_u[i]);
Jens Axboea64e88d2011-10-03 14:20:01 +0200594 }
595
596 dst->total_submit = le64_to_cpu(src->total_submit);
597 dst->total_complete = le64_to_cpu(src->total_complete);
598
599 for (i = 0; i < 2; i++) {
600 dst->io_bytes[i] = le64_to_cpu(src->io_bytes[i]);
601 dst->runtime[i] = le64_to_cpu(src->runtime[i]);
602 }
603
604 dst->total_run_time = le64_to_cpu(src->total_run_time);
605 dst->continue_on_error = le16_to_cpu(src->continue_on_error);
606 dst->total_err_count = le64_to_cpu(src->total_err_count);
Jens Axboeddcc0b62011-10-03 14:45:27 +0200607 dst->first_error = le32_to_cpu(src->first_error);
608 dst->kb_base = le32_to_cpu(src->kb_base);
Jens Axboea64e88d2011-10-03 14:20:01 +0200609}
610
611static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
612{
613 int i;
614
615 for (i = 0; i < 2; i++) {
616 dst->max_run[i] = le64_to_cpu(src->max_run[i]);
617 dst->min_run[i] = le64_to_cpu(src->min_run[i]);
618 dst->max_bw[i] = le64_to_cpu(src->max_bw[i]);
619 dst->min_bw[i] = le64_to_cpu(src->min_bw[i]);
620 dst->io_kb[i] = le64_to_cpu(src->io_kb[i]);
621 dst->agg[i] = le64_to_cpu(src->agg[i]);
622 }
623
624 dst->kb_base = le32_to_cpu(src->kb_base);
625 dst->groupid = le32_to_cpu(src->groupid);
626}
627
628static void handle_ts(struct fio_net_cmd *cmd)
629{
630 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
631
632 convert_ts(&p->ts, &p->ts);
633 convert_gs(&p->rs, &p->rs);
634
635 show_thread_status(&p->ts, &p->rs);
Jens Axboe37f0c1a2011-10-11 14:08:33 +0200636
637 if (sum_stat_clients == 1)
638 return;
639
640 sum_thread_stats(&client_ts, &p->ts, sum_stat_nr);
641 sum_group_stats(&client_gs, &p->rs);
642
643 client_ts.members++;
644 client_ts.groupid = p->ts.groupid;
645
646 if (++sum_stat_nr == sum_stat_clients) {
647 strcpy(client_ts.name, "All clients");
648 show_thread_status(&client_ts, &client_gs);
649 }
Jens Axboea64e88d2011-10-03 14:20:01 +0200650}
651
652static void handle_gs(struct fio_net_cmd *cmd)
653{
654 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
655
656 convert_gs(gs, gs);
657 show_group_stats(gs);
658}
659
Jens Axboed09a64a2011-10-13 11:38:56 +0200660static void convert_agg(struct disk_util_agg *agg)
661{
662 int i;
663
664 for (i = 0; i < 2; i++) {
665 agg->ios[i] = le32_to_cpu(agg->ios[i]);
666 agg->merges[i] = le32_to_cpu(agg->merges[i]);
667 agg->sectors[i] = le64_to_cpu(agg->sectors[i]);
668 agg->ticks[i] = le32_to_cpu(agg->ticks[i]);
669 }
670
671 agg->io_ticks = le32_to_cpu(agg->io_ticks);
672 agg->time_in_queue = le32_to_cpu(agg->time_in_queue);
673 agg->slavecount = le32_to_cpu(agg->slavecount);
Anton Blanchard823ba542011-11-07 14:16:26 +0100674 agg->max_util.u.f = fio_uint64_to_double(__le64_to_cpu(agg->max_util.u.i));
Jens Axboed09a64a2011-10-13 11:38:56 +0200675}
676
677static void convert_dus(struct disk_util_stat *dus)
678{
679 int i;
680
681 for (i = 0; i < 2; i++) {
682 dus->ios[i] = le32_to_cpu(dus->ios[i]);
683 dus->merges[i] = le32_to_cpu(dus->merges[i]);
684 dus->sectors[i] = le64_to_cpu(dus->sectors[i]);
685 dus->ticks[i] = le32_to_cpu(dus->ticks[i]);
686 }
687
688 dus->io_ticks = le32_to_cpu(dus->io_ticks);
689 dus->time_in_queue = le32_to_cpu(dus->time_in_queue);
690 dus->msec = le64_to_cpu(dus->msec);
691}
692
693static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd)
694{
695 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
696
697 convert_dus(&du->dus);
698 convert_agg(&du->agg);
699
700 if (!client->disk_stats_shown) {
701 client->disk_stats_shown = 1;
702 log_info("\nDisk stats (read/write):\n");
703 }
704
Jens Axboef2f788d2011-10-13 14:03:52 +0200705 print_disk_util(&du->dus, &du->agg, terse_output);
Jens Axboed09a64a2011-10-13 11:38:56 +0200706}
707
Jens Axboe48fbb462011-10-09 12:19:08 +0200708static void convert_jobs_eta(struct jobs_eta *je)
Jens Axboecf451d12011-10-03 16:48:30 +0200709{
Jens Axboecf451d12011-10-03 16:48:30 +0200710 int i;
711
712 je->nr_running = le32_to_cpu(je->nr_running);
713 je->nr_ramp = le32_to_cpu(je->nr_ramp);
714 je->nr_pending = le32_to_cpu(je->nr_pending);
715 je->files_open = le32_to_cpu(je->files_open);
716 je->m_rate = le32_to_cpu(je->m_rate);
717 je->t_rate = le32_to_cpu(je->t_rate);
718 je->m_iops = le32_to_cpu(je->m_iops);
719 je->t_iops = le32_to_cpu(je->t_iops);
720
721 for (i = 0; i < 2; i++) {
722 je->rate[i] = le32_to_cpu(je->rate[i]);
723 je->iops[i] = le32_to_cpu(je->iops[i]);
724 }
725
Jens Axboeb51eedb2011-10-09 12:13:39 +0200726 je->elapsed_sec = le64_to_cpu(je->elapsed_sec);
Jens Axboecf451d12011-10-03 16:48:30 +0200727 je->eta_sec = le64_to_cpu(je->eta_sec);
Jens Axboe48fbb462011-10-09 12:19:08 +0200728}
Jens Axboecf451d12011-10-03 16:48:30 +0200729
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200730static void sum_jobs_eta(struct jobs_eta *dst, struct jobs_eta *je)
Jens Axboe48fbb462011-10-09 12:19:08 +0200731{
Jens Axboe48fbb462011-10-09 12:19:08 +0200732 int i;
733
734 dst->nr_running += je->nr_running;
735 dst->nr_ramp += je->nr_ramp;
736 dst->nr_pending += je->nr_pending;
737 dst->files_open += je->files_open;
738 dst->m_rate += je->m_rate;
739 dst->t_rate += je->t_rate;
740 dst->m_iops += je->m_iops;
741 dst->t_iops += je->t_iops;
742
743 for (i = 0; i < 2; i++) {
744 dst->rate[i] += je->rate[i];
745 dst->iops[i] += je->iops[i];
746 }
747
748 dst->elapsed_sec += je->elapsed_sec;
749
750 if (je->eta_sec > dst->eta_sec)
751 dst->eta_sec = je->eta_sec;
752}
753
Jens Axboe82c1ed32011-10-10 08:56:18 +0200754static void dec_jobs_eta(struct client_eta *eta)
755{
756 if (!--eta->pending) {
757 display_thread_status(&eta->eta);
758 free(eta);
759 }
760}
761
Jens Axboe89c17072011-10-11 10:15:51 +0200762static void remove_reply_cmd(struct fio_client *client, struct fio_net_cmd *cmd)
763{
764 struct fio_net_int_cmd *icmd = NULL;
765 struct flist_head *entry;
766
767 flist_for_each(entry, &client->cmd_list) {
768 icmd = flist_entry(entry, struct fio_net_int_cmd, list);
769
Jens Axboedf380932011-10-11 14:25:08 +0200770 if (cmd->tag == (uintptr_t) icmd)
Jens Axboe89c17072011-10-11 10:15:51 +0200771 break;
772
773 icmd = NULL;
774 }
775
776 if (!icmd) {
777 log_err("fio: client: unable to find matching tag\n");
778 return;
779 }
780
781 flist_del(&icmd->list);
782 cmd->tag = icmd->saved_tag;
783 free(icmd);
784}
785
Jens Axboe82c1ed32011-10-10 08:56:18 +0200786static void handle_eta(struct fio_client *client, struct fio_net_cmd *cmd)
Jens Axboe48fbb462011-10-09 12:19:08 +0200787{
788 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
Jens Axboedf380932011-10-11 14:25:08 +0200789 struct client_eta *eta = (struct client_eta *) (uintptr_t) cmd->tag;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200790
791 dprint(FD_NET, "client: got eta tag %p, %d\n", eta, eta->pending);
Jens Axboe48fbb462011-10-09 12:19:08 +0200792
Jens Axboef77d2672011-10-10 14:36:07 +0200793 assert(client->eta_in_flight == eta);
794
795 client->eta_in_flight = NULL;
Jens Axboe82c1ed32011-10-10 08:56:18 +0200796 flist_del_init(&client->eta_list);
797
Jens Axboe48fbb462011-10-09 12:19:08 +0200798 convert_jobs_eta(je);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200799 sum_jobs_eta(&eta->eta, je);
Jens Axboe82c1ed32011-10-10 08:56:18 +0200800 dec_jobs_eta(eta);
Jens Axboecf451d12011-10-03 16:48:30 +0200801}
802
Jens Axboeb5296dd2011-10-07 16:35:56 +0200803static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd)
Jens Axboe2e03b4b2011-10-04 09:00:40 +0200804{
805 struct cmd_probe_pdu *probe = (struct cmd_probe_pdu *) cmd->payload;
Jens Axboed2333352011-10-11 15:07:23 +0200806 const char *os, *arch;
807 char bit[16];
Jens Axboe2e03b4b2011-10-04 09:00:40 +0200808
Jens Axboecca84642011-10-07 12:47:57 +0200809 os = fio_get_os_string(probe->os);
810 if (!os)
811 os = "unknown";
812
813 arch = fio_get_arch_string(probe->arch);
814 if (!arch)
815 os = "unknown";
816
Jens Axboed2333352011-10-11 15:07:23 +0200817 sprintf(bit, "%d-bit", probe->bpp * 8);
Jens Axboe38fdef22011-10-11 14:30:06 +0200818
819 log_info("hostname=%s, be=%u, %s, os=%s, arch=%s, fio=%u.%u.%u\n",
820 probe->hostname, probe->bigendian, bit, os, arch,
821 probe->fio_major, probe->fio_minor, probe->fio_patch);
Jens Axboeb5296dd2011-10-07 16:35:56 +0200822
823 if (!client->name)
824 client->name = strdup((char *) probe->hostname);
Jens Axboe2e03b4b2011-10-04 09:00:40 +0200825}
826
Jens Axboe11e950b2011-10-16 21:34:14 +0200827static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd)
828{
829 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
830
831 client->state = Client_started;
832 client->jobs = le32_to_cpu(pdu->jobs);
833}
834
835static void handle_stop(struct fio_client *client, struct fio_net_cmd *cmd)
836{
837 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
838
839 client->state = Client_stopped;
840 client->error = le32_to_cpu(pdu->error);
Jens Axboe498c92c2011-10-17 09:14:42 +0200841
842 if (client->error)
843 log_info("client <%s>: exited with error %d\n", client->hostname, client->error);
Jens Axboe11e950b2011-10-16 21:34:14 +0200844}
845
Jens Axboee951bdc2011-10-05 21:58:45 +0200846static int handle_client(struct fio_client *client)
Jens Axboe37db14f2011-09-30 17:00:42 -0600847{
848 struct fio_net_cmd *cmd;
849
Jens Axboe60efd142011-10-04 13:30:11 +0200850 dprint(FD_NET, "client: handle %s\n", client->hostname);
851
Jens Axboee951bdc2011-10-05 21:58:45 +0200852 cmd = fio_net_recv_cmd(client->fd);
853 if (!cmd)
854 return 0;
Jens Axboec2c94582011-10-05 20:31:30 +0200855
Jens Axboe89c17072011-10-11 10:15:51 +0200856 dprint(FD_NET, "client: got cmd op %s from %s\n",
857 fio_server_op(cmd->opcode), client->hostname);
Jens Axboe46c48f12011-10-01 12:50:51 -0600858
Jens Axboee951bdc2011-10-05 21:58:45 +0200859 switch (cmd->opcode) {
860 case FIO_NET_CMD_QUIT:
861 remove_client(client);
862 free(cmd);
863 break;
864 case FIO_NET_CMD_TEXT: {
865 const char *buf = (const char *) cmd->payload;
Jens Axboeb5296dd2011-10-07 16:35:56 +0200866 const char *name;
Jens Axboee951bdc2011-10-05 21:58:45 +0200867 int fio_unused ret;
Jens Axboe17dd1762011-10-04 13:27:34 +0200868
Jens Axboeb5296dd2011-10-07 16:35:56 +0200869 name = client->name ? client->name : client->hostname;
870
Jens Axboee951bdc2011-10-05 21:58:45 +0200871 if (!client->skip_newline)
Jens Axboeb5296dd2011-10-07 16:35:56 +0200872 fprintf(f_out, "<%s> ", name);
Jens Axboee951bdc2011-10-05 21:58:45 +0200873 ret = fwrite(buf, cmd->pdu_len, 1, f_out);
874 fflush(f_out);
875 client->skip_newline = strchr(buf, '\n') == NULL;
876 free(cmd);
877 break;
Jens Axboe37db14f2011-09-30 17:00:42 -0600878 }
Jens Axboed09a64a2011-10-13 11:38:56 +0200879 case FIO_NET_CMD_DU:
880 handle_du(client, cmd);
881 free(cmd);
882 break;
Jens Axboee951bdc2011-10-05 21:58:45 +0200883 case FIO_NET_CMD_TS:
884 handle_ts(cmd);
885 free(cmd);
886 break;
887 case FIO_NET_CMD_GS:
888 handle_gs(cmd);
889 free(cmd);
890 break;
891 case FIO_NET_CMD_ETA:
Jens Axboe89c17072011-10-11 10:15:51 +0200892 remove_reply_cmd(client, cmd);
Jens Axboe82c1ed32011-10-10 08:56:18 +0200893 handle_eta(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +0200894 free(cmd);
895 break;
896 case FIO_NET_CMD_PROBE:
Jens Axboe89c17072011-10-11 10:15:51 +0200897 remove_reply_cmd(client, cmd);
Jens Axboeb5296dd2011-10-07 16:35:56 +0200898 handle_probe(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +0200899 free(cmd);
900 break;
Jens Axboe01be0382011-10-15 14:43:41 +0200901 case FIO_NET_CMD_RUN:
902 client->state = Client_running;
903 free(cmd);
904 break;
Jens Axboee951bdc2011-10-05 21:58:45 +0200905 case FIO_NET_CMD_START:
Jens Axboe11e950b2011-10-16 21:34:14 +0200906 handle_start(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +0200907 free(cmd);
908 break;
909 case FIO_NET_CMD_STOP:
Jens Axboe11e950b2011-10-16 21:34:14 +0200910 handle_stop(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +0200911 free(cmd);
912 break;
913 default:
Jens Axboe89c17072011-10-11 10:15:51 +0200914 log_err("fio: unknown client op: %s\n", fio_server_op(cmd->opcode));
Jens Axboee951bdc2011-10-05 21:58:45 +0200915 free(cmd);
916 break;
Jens Axboe37db14f2011-09-30 17:00:42 -0600917 }
918
Jens Axboee951bdc2011-10-05 21:58:45 +0200919 return 1;
Jens Axboe37db14f2011-09-30 17:00:42 -0600920}
Jens Axboeb66570d2011-10-01 11:11:35 -0600921
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200922static void request_client_etas(void)
923{
924 struct fio_client *client;
925 struct flist_head *entry;
926 struct client_eta *eta;
Jens Axboe82c1ed32011-10-10 08:56:18 +0200927 int skipped = 0;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200928
929 dprint(FD_NET, "client: request eta (%d)\n", nr_clients);
930
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200931 eta = malloc(sizeof(*eta));
932 memset(&eta->eta, 0, sizeof(eta->eta));
933 eta->pending = nr_clients;
934
935 flist_for_each(entry, &client_list) {
936 client = flist_entry(entry, struct fio_client, list);
937
Jens Axboe82c1ed32011-10-10 08:56:18 +0200938 if (!flist_empty(&client->eta_list)) {
939 skipped++;
940 continue;
941 }
Jens Axboe01be0382011-10-15 14:43:41 +0200942 if (client->state != Client_running)
943 continue;
Jens Axboe82c1ed32011-10-10 08:56:18 +0200944
Jens Axboef77d2672011-10-10 14:36:07 +0200945 assert(!client->eta_in_flight);
Jens Axboe82c1ed32011-10-10 08:56:18 +0200946 flist_add_tail(&client->eta_list, &eta_list);
Jens Axboef77d2672011-10-10 14:36:07 +0200947 client->eta_in_flight = eta;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200948 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_SEND_ETA,
Jens Axboedf380932011-10-11 14:25:08 +0200949 (uintptr_t) eta, &client->cmd_list);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200950 }
951
Jens Axboe82c1ed32011-10-10 08:56:18 +0200952 while (skipped--)
953 dec_jobs_eta(eta);
954
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200955 dprint(FD_NET, "client: requested eta tag %p\n", eta);
956}
957
Jens Axboe89c17072011-10-11 10:15:51 +0200958static int client_check_cmd_timeout(struct fio_client *client,
959 struct timeval *now)
960{
961 struct fio_net_int_cmd *cmd;
962 struct flist_head *entry, *tmp;
963 int ret = 0;
964
965 flist_for_each_safe(entry, tmp, &client->cmd_list) {
966 cmd = flist_entry(entry, struct fio_net_int_cmd, list);
967
968 if (mtime_since(&cmd->tv, now) < FIO_NET_CLIENT_TIMEOUT)
969 continue;
970
971 log_err("fio: client %s, timeout on cmd %s\n", client->hostname,
972 fio_server_op(cmd->cmd.opcode));
973 flist_del(&cmd->list);
974 free(cmd);
975 ret = 1;
976 }
977
978 return flist_empty(&client->cmd_list) && ret;
979}
980
981static int fio_client_timed_out(void)
982{
983 struct fio_client *client;
984 struct flist_head *entry, *tmp;
985 struct timeval tv;
986 int ret = 0;
987
988 gettimeofday(&tv, NULL);
989
990 flist_for_each_safe(entry, tmp, &client_list) {
991 client = flist_entry(entry, struct fio_client, list);
992
993 if (flist_empty(&client->cmd_list))
994 continue;
995
996 if (!client_check_cmd_timeout(client, &tv))
997 continue;
998
999 log_err("fio: client %s timed out\n", client->hostname);
1000 remove_client(client);
1001 ret = 1;
1002 }
1003
1004 return ret;
1005}
1006
Jens Axboeb66570d2011-10-01 11:11:35 -06001007int fio_handle_clients(void)
1008{
Jens Axboeb66570d2011-10-01 11:11:35 -06001009 struct pollfd *pfds;
Jens Axboe498c92c2011-10-17 09:14:42 +02001010 int i, ret = 0, retval = 0;
Jens Axboeb66570d2011-10-01 11:11:35 -06001011
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001012 gettimeofday(&eta_tv, NULL);
1013
Jens Axboeb66570d2011-10-01 11:11:35 -06001014 pfds = malloc(nr_clients * sizeof(struct pollfd));
1015
Jens Axboe37f0c1a2011-10-11 14:08:33 +02001016 sum_stat_clients = nr_clients;
1017 init_thread_stat(&client_ts);
1018 init_group_run_stat(&client_gs);
1019
Jens Axboeb66570d2011-10-01 11:11:35 -06001020 while (!exit_backend && nr_clients) {
Jens Axboec2cb6862012-02-23 20:56:12 +01001021 struct flist_head *entry, *tmp;
1022 struct fio_client *client;
1023
Jens Axboe82a4be12011-10-01 12:40:32 -06001024 i = 0;
Jens Axboec2cb6862012-02-23 20:56:12 +01001025 flist_for_each_safe(entry, tmp, &client_list) {
Jens Axboe82a4be12011-10-01 12:40:32 -06001026 client = flist_entry(entry, struct fio_client, list);
1027
Jens Axboec2cb6862012-02-23 20:56:12 +01001028 if (!client->sent_job &&
1029 flist_empty(&client->cmd_list)) {
1030 remove_client(client);
1031 continue;
1032 }
1033
Jens Axboe82a4be12011-10-01 12:40:32 -06001034 pfds[i].fd = client->fd;
1035 pfds[i].events = POLLIN;
1036 i++;
1037 }
1038
Jens Axboec2cb6862012-02-23 20:56:12 +01001039 if (!nr_clients)
1040 break;
1041
Jens Axboe82a4be12011-10-01 12:40:32 -06001042 assert(i == nr_clients);
1043
Jens Axboe5c2857f2011-10-04 13:14:32 +02001044 do {
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001045 struct timeval tv;
1046
1047 gettimeofday(&tv, NULL);
1048 if (mtime_since(&eta_tv, &tv) >= 900) {
1049 request_client_etas();
1050 memcpy(&eta_tv, &tv, sizeof(tv));
Jens Axboe89c17072011-10-11 10:15:51 +02001051
1052 if (fio_client_timed_out())
1053 break;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001054 }
1055
Jens Axboe5c2857f2011-10-04 13:14:32 +02001056 ret = poll(pfds, nr_clients, 100);
1057 if (ret < 0) {
1058 if (errno == EINTR)
1059 continue;
1060 log_err("fio: poll clients: %s\n", strerror(errno));
1061 break;
1062 } else if (!ret)
Jens Axboeb66570d2011-10-01 11:11:35 -06001063 continue;
Jens Axboe5c2857f2011-10-04 13:14:32 +02001064 } while (ret <= 0);
Jens Axboeb66570d2011-10-01 11:11:35 -06001065
1066 for (i = 0; i < nr_clients; i++) {
1067 if (!(pfds[i].revents & POLLIN))
1068 continue;
1069
1070 client = find_client_by_fd(pfds[i].fd);
1071 if (!client) {
Jens Axboe3c5f57e2011-10-06 12:37:50 +02001072 log_err("fio: unknown client fd %d\n", pfds[i].fd);
Jens Axboeb66570d2011-10-01 11:11:35 -06001073 continue;
1074 }
Jens Axboee951bdc2011-10-05 21:58:45 +02001075 if (!handle_client(client)) {
Jens Axboe28d3ab02011-10-05 20:45:37 +02001076 log_info("client: host=%s disconnected\n",
1077 client->hostname);
1078 remove_client(client);
Jens Axboe498c92c2011-10-17 09:14:42 +02001079 retval = 1;
Jens Axboe38990762011-10-17 13:31:33 +02001080 } else if (client->error)
Jens Axboe498c92c2011-10-17 09:14:42 +02001081 retval = 1;
Jens Axboee55f8f32012-03-06 15:42:31 +01001082 put_client(client);
Jens Axboeb66570d2011-10-01 11:11:35 -06001083 }
1084 }
1085
1086 free(pfds);
Jens Axboe498c92c2011-10-17 09:14:42 +02001087 return retval;
Jens Axboeb66570d2011-10-01 11:11:35 -06001088}