blob: a61bc803b4267708a7f1f8affe160f9154256ac5 [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 Axboe82c1ed32011-10-10 08:56:18 +020051
52 struct flist_head eta_list;
53 struct client_eta *eta_in_flight;
Jens Axboe81179ee2011-10-04 12:42:06 +020054
Jens Axboe89c17072011-10-11 10:15:51 +020055 struct flist_head cmd_list;
56
Jens Axboe81179ee2011-10-04 12:42:06 +020057 uint16_t argc;
58 char **argv;
59};
60
Jens Axboeaf9c9fb2011-10-09 21:54:10 +020061static struct timeval eta_tv;
Jens Axboe48fbb462011-10-09 12:19:08 +020062
Jens Axboe81179ee2011-10-04 12:42:06 +020063enum {
Jens Axboe5c2857f2011-10-04 13:14:32 +020064 Client_created = 0,
Jens Axboe81179ee2011-10-04 12:42:06 +020065 Client_connected = 1,
66 Client_started = 2,
Jens Axboe01be0382011-10-15 14:43:41 +020067 Client_running = 3,
68 Client_stopped = 4,
69 Client_exited = 5,
Jens Axboeb66570d2011-10-01 11:11:35 -060070};
71
72static FLIST_HEAD(client_list);
Jens Axboe82c1ed32011-10-10 08:56:18 +020073static FLIST_HEAD(eta_list);
Jens Axboeb66570d2011-10-01 11:11:35 -060074
Jens Axboe3f3a4542011-10-10 21:11:09 +020075static FLIST_HEAD(arg_list);
76
Jens Axboe37f0c1a2011-10-11 14:08:33 +020077static struct thread_stat client_ts;
78static struct group_run_stats client_gs;
79static int sum_stat_clients;
80static int sum_stat_nr;
81
Jens Axboe3c5f57e2011-10-06 12:37:50 +020082#define FIO_CLIENT_HASH_BITS 7
83#define FIO_CLIENT_HASH_SZ (1 << FIO_CLIENT_HASH_BITS)
84#define FIO_CLIENT_HASH_MASK (FIO_CLIENT_HASH_SZ - 1)
Jens Axboebebe6392011-10-07 10:00:51 +020085static struct flist_head client_hash[FIO_CLIENT_HASH_SZ];
Jens Axboe3c5f57e2011-10-06 12:37:50 +020086
Jens Axboee951bdc2011-10-05 21:58:45 +020087static int handle_client(struct fio_client *client);
Jens Axboe82c1ed32011-10-10 08:56:18 +020088static void dec_jobs_eta(struct client_eta *eta);
Jens Axboe0b8f30a2011-10-04 10:31:53 +020089
Jens Axboebebe6392011-10-07 10:00:51 +020090static void fio_client_add_hash(struct fio_client *client)
Jens Axboe3c5f57e2011-10-06 12:37:50 +020091{
92 int bucket = hash_long(client->fd, FIO_CLIENT_HASH_BITS);
93
94 bucket &= FIO_CLIENT_HASH_MASK;
Jens Axboebebe6392011-10-07 10:00:51 +020095 flist_add(&client->hash_list, &client_hash[bucket]);
Jens Axboe3c5f57e2011-10-06 12:37:50 +020096}
97
Jens Axboebebe6392011-10-07 10:00:51 +020098static void fio_client_remove_hash(struct fio_client *client)
Jens Axboe3c5f57e2011-10-06 12:37:50 +020099{
Jens Axboebebe6392011-10-07 10:00:51 +0200100 if (!flist_empty(&client->hash_list))
101 flist_del_init(&client->hash_list);
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200102}
103
104static void fio_init fio_client_hash_init(void)
105{
106 int i;
107
Jens Axboebebe6392011-10-07 10:00:51 +0200108 for (i = 0; i < FIO_CLIENT_HASH_SZ; i++)
109 INIT_FLIST_HEAD(&client_hash[i]);
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200110}
111
Jens Axboeb66570d2011-10-01 11:11:35 -0600112static struct fio_client *find_client_by_fd(int fd)
113{
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200114 int bucket = hash_long(fd, FIO_CLIENT_HASH_BITS) & FIO_CLIENT_HASH_MASK;
Jens Axboeb66570d2011-10-01 11:11:35 -0600115 struct fio_client *client;
116 struct flist_head *entry;
117
Jens Axboebebe6392011-10-07 10:00:51 +0200118 flist_for_each(entry, &client_hash[bucket]) {
119 client = flist_entry(entry, struct fio_client, hash_list);
Jens Axboeb66570d2011-10-01 11:11:35 -0600120
121 if (client->fd == fd)
122 return client;
123 }
124
125 return NULL;
126}
127
Jens Axboeb66570d2011-10-01 11:11:35 -0600128static void remove_client(struct fio_client *client)
129{
Jens Axboe39e8e012011-10-04 13:46:08 +0200130 dprint(FD_NET, "client: removed <%s>\n", client->hostname);
Jens Axboeb66570d2011-10-01 11:11:35 -0600131 flist_del(&client->list);
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200132
Jens Axboebebe6392011-10-07 10:00:51 +0200133 fio_client_remove_hash(client);
Jens Axboe81179ee2011-10-04 12:42:06 +0200134
Jens Axboe82c1ed32011-10-10 08:56:18 +0200135 if (!flist_empty(&client->eta_list)) {
136 flist_del_init(&client->eta_list);
137 dec_jobs_eta(client->eta_in_flight);
138 }
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200139
Jens Axboeb66570d2011-10-01 11:11:35 -0600140 free(client->hostname);
Jens Axboe81179ee2011-10-04 12:42:06 +0200141 if (client->argv)
142 free(client->argv);
Jens Axboeb5296dd2011-10-07 16:35:56 +0200143 if (client->name)
144 free(client->name);
Jens Axboe81179ee2011-10-04 12:42:06 +0200145
Jens Axboeb66570d2011-10-01 11:11:35 -0600146 free(client);
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200147 nr_clients--;
Jens Axboe5fd0acb2011-10-11 14:20:22 +0200148 sum_stat_clients--;
Jens Axboeb66570d2011-10-01 11:11:35 -0600149}
Jens Axboe132159a2011-09-30 15:01:32 -0600150
Jens Axboefa2ea802011-10-08 21:07:29 +0200151static void __fio_client_add_cmd_option(struct fio_client *client,
152 const char *opt)
Jens Axboe81179ee2011-10-04 12:42:06 +0200153{
Jens Axboe39e8e012011-10-04 13:46:08 +0200154 int index;
155
156 index = client->argc++;
Jens Axboe81179ee2011-10-04 12:42:06 +0200157 client->argv = realloc(client->argv, sizeof(char *) * client->argc);
Jens Axboe39e8e012011-10-04 13:46:08 +0200158 client->argv[index] = strdup(opt);
159 dprint(FD_NET, "client: add cmd %d: %s\n", index, opt);
Jens Axboe81179ee2011-10-04 12:42:06 +0200160}
161
Jens Axboefa2ea802011-10-08 21:07:29 +0200162void fio_client_add_cmd_option(void *cookie, const char *opt)
Jens Axboe81179ee2011-10-04 12:42:06 +0200163{
Jens Axboebebe6392011-10-07 10:00:51 +0200164 struct fio_client *client = cookie;
Jens Axboe3f3a4542011-10-10 21:11:09 +0200165 struct flist_head *entry;
Jens Axboe81179ee2011-10-04 12:42:06 +0200166
Jens Axboebebe6392011-10-07 10:00:51 +0200167 if (!client || !opt)
Jens Axboefa2ea802011-10-08 21:07:29 +0200168 return;
Jens Axboe81179ee2011-10-04 12:42:06 +0200169
Jens Axboefa2ea802011-10-08 21:07:29 +0200170 __fio_client_add_cmd_option(client, opt);
Jens Axboe3f3a4542011-10-10 21:11:09 +0200171
172 /*
173 * Duplicate arguments to shared client group
174 */
175 flist_for_each(entry, &arg_list) {
176 client = flist_entry(entry, struct fio_client, arg_list);
177
178 __fio_client_add_cmd_option(client, opt);
179 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200180}
181
Jens Axboebebe6392011-10-07 10:00:51 +0200182int fio_client_add(const char *hostname, void **cookie)
Jens Axboe132159a2011-09-30 15:01:32 -0600183{
Jens Axboe3f3a4542011-10-10 21:11:09 +0200184 struct fio_client *existing = *cookie;
Jens Axboeb66570d2011-10-01 11:11:35 -0600185 struct fio_client *client;
Jens Axboe132159a2011-09-30 15:01:32 -0600186
Jens Axboe3f3a4542011-10-10 21:11:09 +0200187 if (existing) {
188 /*
189 * We always add our "exec" name as the option, hence 1
190 * means empty.
191 */
192 if (existing->argc == 1)
193 flist_add_tail(&existing->arg_list, &arg_list);
194 else {
195 while (!flist_empty(&arg_list))
196 flist_del_init(arg_list.next);
197 }
198 }
199
Jens Axboeb66570d2011-10-01 11:11:35 -0600200 client = malloc(sizeof(*client));
Jens Axboea37f69b2011-10-01 12:24:21 -0600201 memset(client, 0, sizeof(*client));
Jens Axboe81179ee2011-10-04 12:42:06 +0200202
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200203 INIT_FLIST_HEAD(&client->list);
Jens Axboebebe6392011-10-07 10:00:51 +0200204 INIT_FLIST_HEAD(&client->hash_list);
Jens Axboe3f3a4542011-10-10 21:11:09 +0200205 INIT_FLIST_HEAD(&client->arg_list);
Jens Axboe82c1ed32011-10-10 08:56:18 +0200206 INIT_FLIST_HEAD(&client->eta_list);
Jens Axboe89c17072011-10-11 10:15:51 +0200207 INIT_FLIST_HEAD(&client->cmd_list);
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200208
Jens Axboebebe6392011-10-07 10:00:51 +0200209 if (fio_server_parse_string(hostname, &client->hostname,
210 &client->is_sock, &client->port,
Jens Axboe811826b2011-10-24 09:11:50 +0200211 &client->addr.sin_addr,
212 &client->addr6.sin6_addr,
213 &client->ipv6))
Jens Axboebebe6392011-10-07 10:00:51 +0200214 return -1;
215
Jens Axboea37f69b2011-10-01 12:24:21 -0600216 client->fd = -1;
Jens Axboe81179ee2011-10-04 12:42:06 +0200217
218 __fio_client_add_cmd_option(client, "fio");
219
Jens Axboea37f69b2011-10-01 12:24:21 -0600220 flist_add(&client->list, &client_list);
221 nr_clients++;
Jens Axboebebe6392011-10-07 10:00:51 +0200222 dprint(FD_NET, "client: added <%s>\n", client->hostname);
223 *cookie = client;
224 return 0;
Jens Axboea37f69b2011-10-01 12:24:21 -0600225}
226
Jens Axboe87aa8f12011-10-06 21:24:13 +0200227static int fio_client_connect_ip(struct fio_client *client)
Jens Axboea37f69b2011-10-01 12:24:21 -0600228{
Jens Axboe811826b2011-10-24 09:11:50 +0200229 struct sockaddr *addr;
230 fio_socklen_t socklen;
231 int fd, domain;
Jens Axboe132159a2011-09-30 15:01:32 -0600232
Jens Axboe811826b2011-10-24 09:11:50 +0200233 if (client->ipv6) {
234 client->addr6.sin6_family = AF_INET6;
235 client->addr6.sin6_port = htons(client->port);
236 domain = AF_INET6;
237 addr = (struct sockaddr *) &client->addr6;
238 socklen = sizeof(client->addr6);
239 } else {
240 client->addr.sin_family = AF_INET;
241 client->addr.sin_port = htons(client->port);
242 domain = AF_INET;
243 addr = (struct sockaddr *) &client->addr;
244 socklen = sizeof(client->addr);
245 }
Jens Axboe132159a2011-09-30 15:01:32 -0600246
Jens Axboe811826b2011-10-24 09:11:50 +0200247 fd = socket(domain, SOCK_STREAM, 0);
Jens Axboe132159a2011-09-30 15:01:32 -0600248 if (fd < 0) {
249 log_err("fio: socket: %s\n", strerror(errno));
Jens Axboe87aa8f12011-10-06 21:24:13 +0200250 return -1;
Jens Axboe132159a2011-09-30 15:01:32 -0600251 }
252
Jens Axboe811826b2011-10-24 09:11:50 +0200253 if (connect(fd, addr, socklen) < 0) {
Jens Axboe132159a2011-09-30 15:01:32 -0600254 log_err("fio: connect: %s\n", strerror(errno));
Jens Axboea7de0a12011-10-07 12:55:14 +0200255 log_err("fio: failed to connect to %s:%u\n", client->hostname,
256 client->port);
Jens Axboeb94cba42011-10-06 21:27:10 +0200257 close(fd);
Jens Axboe87aa8f12011-10-06 21:24:13 +0200258 return -1;
Jens Axboe132159a2011-09-30 15:01:32 -0600259 }
260
Jens Axboe87aa8f12011-10-06 21:24:13 +0200261 return fd;
262}
263
264static int fio_client_connect_sock(struct fio_client *client)
265{
266 struct sockaddr_un *addr = &client->addr_un;
267 fio_socklen_t len;
268 int fd;
269
270 memset(addr, 0, sizeof(*addr));
271 addr->sun_family = AF_UNIX;
272 strcpy(addr->sun_path, client->hostname);
273
274 fd = socket(AF_UNIX, SOCK_STREAM, 0);
275 if (fd < 0) {
276 log_err("fio: socket: %s\n", strerror(errno));
277 return -1;
278 }
279
280 len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
281 if (connect(fd, (struct sockaddr *) addr, len) < 0) {
282 log_err("fio: connect; %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200283 close(fd);
Jens Axboe87aa8f12011-10-06 21:24:13 +0200284 return -1;
285 }
286
287 return fd;
288}
289
290static int fio_client_connect(struct fio_client *client)
291{
292 int fd;
293
294 dprint(FD_NET, "client: connect to host %s\n", client->hostname);
295
Jens Axboe87aa8f12011-10-06 21:24:13 +0200296 if (client->is_sock)
297 fd = fio_client_connect_sock(client);
298 else
299 fd = fio_client_connect_ip(client);
300
Jens Axboe89c17072011-10-11 10:15:51 +0200301 dprint(FD_NET, "client: %s connected %d\n", client->hostname, fd);
302
Jens Axboe87aa8f12011-10-06 21:24:13 +0200303 if (fd < 0)
304 return 1;
305
Jens Axboeb66570d2011-10-01 11:11:35 -0600306 client->fd = fd;
Jens Axboebebe6392011-10-07 10:00:51 +0200307 fio_client_add_hash(client);
Jens Axboe81179ee2011-10-04 12:42:06 +0200308 client->state = Client_connected;
Jens Axboe132159a2011-09-30 15:01:32 -0600309 return 0;
310}
311
Jens Axboecc0df002011-10-03 20:53:32 +0200312void fio_clients_terminate(void)
313{
314 struct flist_head *entry;
315 struct fio_client *client;
316
Jens Axboe60efd142011-10-04 13:30:11 +0200317 dprint(FD_NET, "client: terminate clients\n");
318
Jens Axboecc0df002011-10-03 20:53:32 +0200319 flist_for_each(entry, &client_list) {
320 client = flist_entry(entry, struct fio_client, list);
321
Jens Axboe89c17072011-10-11 10:15:51 +0200322 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_QUIT, 0, NULL);
Jens Axboecc0df002011-10-03 20:53:32 +0200323 }
324}
325
326static void sig_int(int sig)
327{
Jens Axboebebe6392011-10-07 10:00:51 +0200328 dprint(FD_NET, "client: got signal %d\n", sig);
Jens Axboecc0df002011-10-03 20:53:32 +0200329 fio_clients_terminate();
330}
331
332static void client_signal_handler(void)
333{
334 struct sigaction act;
335
336 memset(&act, 0, sizeof(act));
337 act.sa_handler = sig_int;
338 act.sa_flags = SA_RESTART;
339 sigaction(SIGINT, &act, NULL);
340
341 memset(&act, 0, sizeof(act));
342 act.sa_handler = sig_int;
343 act.sa_flags = SA_RESTART;
344 sigaction(SIGTERM, &act, NULL);
345}
346
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200347static void probe_client(struct fio_client *client)
348{
Jens Axboe60efd142011-10-04 13:30:11 +0200349 dprint(FD_NET, "client: send probe\n");
350
Jens Axboe89c17072011-10-11 10:15:51 +0200351 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_PROBE, 0, &client->cmd_list);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200352}
353
Jens Axboe81179ee2011-10-04 12:42:06 +0200354static int send_client_cmd_line(struct fio_client *client)
355{
Jens Axboefa2ea802011-10-08 21:07:29 +0200356 struct cmd_single_line_pdu *cslp;
357 struct cmd_line_pdu *clp;
358 unsigned long offset;
Jens Axboe7f868312011-10-10 09:55:21 +0200359 unsigned int *lens;
Jens Axboefa2ea802011-10-08 21:07:29 +0200360 void *pdu;
361 size_t mem;
Jens Axboe81179ee2011-10-04 12:42:06 +0200362 int i, ret;
363
Jens Axboe39e8e012011-10-04 13:46:08 +0200364 dprint(FD_NET, "client: send cmdline %d\n", client->argc);
Jens Axboe60efd142011-10-04 13:30:11 +0200365
Jens Axboe7f868312011-10-10 09:55:21 +0200366 lens = malloc(client->argc * sizeof(unsigned int));
367
Jens Axboefa2ea802011-10-08 21:07:29 +0200368 /*
369 * Find out how much mem we need
370 */
Jens Axboe7f868312011-10-10 09:55:21 +0200371 for (i = 0, mem = 0; i < client->argc; i++) {
372 lens[i] = strlen(client->argv[i]) + 1;
373 mem += lens[i];
374 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200375
Jens Axboefa2ea802011-10-08 21:07:29 +0200376 /*
377 * We need one cmd_line_pdu, and argc number of cmd_single_line_pdu
378 */
379 mem += sizeof(*clp) + (client->argc * sizeof(*cslp));
380
381 pdu = malloc(mem);
382 clp = pdu;
383 offset = sizeof(*clp);
384
385 for (i = 0; i < client->argc; i++) {
Jens Axboe7f868312011-10-10 09:55:21 +0200386 uint16_t arg_len = lens[i];
Jens Axboefa2ea802011-10-08 21:07:29 +0200387
388 cslp = pdu + offset;
389 strcpy((char *) cslp->text, client->argv[i]);
390 cslp->len = cpu_to_le16(arg_len);
391 offset += sizeof(*cslp) + arg_len;
392 }
393
Jens Axboe7f868312011-10-10 09:55:21 +0200394 free(lens);
Jens Axboefa2ea802011-10-08 21:07:29 +0200395 clp->lines = cpu_to_le16(client->argc);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200396 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOBLINE, pdu, mem, 0);
Jens Axboe81179ee2011-10-04 12:42:06 +0200397 free(pdu);
398 return ret;
399}
400
Jens Axboea37f69b2011-10-01 12:24:21 -0600401int fio_clients_connect(void)
402{
403 struct fio_client *client;
404 struct flist_head *entry, *tmp;
405 int ret;
406
Jens Axboe60efd142011-10-04 13:30:11 +0200407 dprint(FD_NET, "client: connect all\n");
408
Jens Axboecc0df002011-10-03 20:53:32 +0200409 client_signal_handler();
410
Jens Axboea37f69b2011-10-01 12:24:21 -0600411 flist_for_each_safe(entry, tmp, &client_list) {
412 client = flist_entry(entry, struct fio_client, list);
413
414 ret = fio_client_connect(client);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200415 if (ret) {
Jens Axboea37f69b2011-10-01 12:24:21 -0600416 remove_client(client);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200417 continue;
418 }
419
420 probe_client(client);
Jens Axboe81179ee2011-10-04 12:42:06 +0200421
422 if (client->argc > 1)
423 send_client_cmd_line(client);
Jens Axboea37f69b2011-10-01 12:24:21 -0600424 }
425
426 return !nr_clients;
427}
428
Jens Axboe132159a2011-09-30 15:01:32 -0600429/*
430 * Send file contents to server backend. We could use sendfile(), but to remain
431 * more portable lets just read/write the darn thing.
432 */
Jens Axboea37f69b2011-10-01 12:24:21 -0600433static int fio_client_send_ini(struct fio_client *client, const char *filename)
Jens Axboe132159a2011-09-30 15:01:32 -0600434{
435 struct stat sb;
436 char *p, *buf;
437 off_t len;
438 int fd, ret;
439
Jens Axboe46c48f12011-10-01 12:50:51 -0600440 dprint(FD_NET, "send ini %s to %s\n", filename, client->hostname);
441
Jens Axboe132159a2011-09-30 15:01:32 -0600442 fd = open(filename, O_RDONLY);
443 if (fd < 0) {
Jens Axboee951bdc2011-10-05 21:58:45 +0200444 log_err("fio: job file <%s> open: %s\n", filename, strerror(errno));
Jens Axboe132159a2011-09-30 15:01:32 -0600445 return 1;
446 }
447
448 if (fstat(fd, &sb) < 0) {
449 log_err("fio: job file stat: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200450 close(fd);
Jens Axboe132159a2011-09-30 15:01:32 -0600451 return 1;
452 }
453
454 buf = malloc(sb.st_size);
455
456 len = sb.st_size;
457 p = buf;
458 do {
459 ret = read(fd, p, len);
460 if (ret > 0) {
461 len -= ret;
462 if (!len)
463 break;
464 p += ret;
465 continue;
466 } else if (!ret)
467 break;
468 else if (errno == EAGAIN || errno == EINTR)
469 continue;
470 } while (1);
471
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200472 if (len) {
473 log_err("fio: failed reading job file %s\n", filename);
Jens Axboeb94cba42011-10-06 21:27:10 +0200474 close(fd);
Jens Axboec524ef72011-10-07 12:23:34 +0200475 free(buf);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200476 return 1;
477 }
478
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200479 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, buf, sb.st_size, 0);
Jens Axboe132159a2011-09-30 15:01:32 -0600480 free(buf);
Jens Axboeb94cba42011-10-06 21:27:10 +0200481 close(fd);
Jens Axboe132159a2011-09-30 15:01:32 -0600482 return ret;
483}
Jens Axboe37db14f2011-09-30 17:00:42 -0600484
Jens Axboea37f69b2011-10-01 12:24:21 -0600485int fio_clients_send_ini(const char *filename)
486{
487 struct fio_client *client;
488 struct flist_head *entry, *tmp;
489
490 flist_for_each_safe(entry, tmp, &client_list) {
491 client = flist_entry(entry, struct fio_client, list);
492
493 if (fio_client_send_ini(client, filename))
494 remove_client(client);
495 }
496
497 return !nr_clients;
498}
499
Jens Axboea64e88d2011-10-03 14:20:01 +0200500static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
501{
502 dst->max_val = le64_to_cpu(src->max_val);
503 dst->min_val = le64_to_cpu(src->min_val);
504 dst->samples = le64_to_cpu(src->samples);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200505
506 /*
507 * Floats arrive as IEEE 754 encoded uint64_t, convert back to double
508 */
509 dst->mean.u.f = fio_uint64_to_double(le64_to_cpu(dst->mean.u.i));
510 dst->S.u.f = fio_uint64_to_double(le64_to_cpu(dst->S.u.i));
Jens Axboea64e88d2011-10-03 14:20:01 +0200511}
512
513static void convert_ts(struct thread_stat *dst, struct thread_stat *src)
514{
515 int i, j;
516
517 dst->error = le32_to_cpu(src->error);
518 dst->groupid = le32_to_cpu(src->groupid);
519 dst->pid = le32_to_cpu(src->pid);
520 dst->members = le32_to_cpu(src->members);
521
522 for (i = 0; i < 2; i++) {
523 convert_io_stat(&dst->clat_stat[i], &src->clat_stat[i]);
524 convert_io_stat(&dst->slat_stat[i], &src->slat_stat[i]);
525 convert_io_stat(&dst->lat_stat[i], &src->lat_stat[i]);
526 convert_io_stat(&dst->bw_stat[i], &src->bw_stat[i]);
527 }
528
529 dst->usr_time = le64_to_cpu(src->usr_time);
530 dst->sys_time = le64_to_cpu(src->sys_time);
531 dst->ctx = le64_to_cpu(src->ctx);
532 dst->minf = le64_to_cpu(src->minf);
533 dst->majf = le64_to_cpu(src->majf);
534 dst->clat_percentiles = le64_to_cpu(src->clat_percentiles);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200535
536 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
537 fio_fp64_t *fps = &src->percentile_list[i];
538 fio_fp64_t *fpd = &dst->percentile_list[i];
539
540 fpd->u.f = fio_uint64_to_double(le64_to_cpu(fps->u.i));
541 }
Jens Axboea64e88d2011-10-03 14:20:01 +0200542
543 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
544 dst->io_u_map[i] = le32_to_cpu(src->io_u_map[i]);
545 dst->io_u_submit[i] = le32_to_cpu(src->io_u_submit[i]);
546 dst->io_u_complete[i] = le32_to_cpu(src->io_u_complete[i]);
547 }
548
549 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
550 dst->io_u_lat_u[i] = le32_to_cpu(src->io_u_lat_u[i]);
551 dst->io_u_lat_m[i] = le32_to_cpu(src->io_u_lat_m[i]);
552 }
553
554 for (i = 0; i < 2; i++)
555 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
556 dst->io_u_plat[i][j] = le32_to_cpu(src->io_u_plat[i][j]);
557
558 for (i = 0; i < 3; i++) {
559 dst->total_io_u[i] = le64_to_cpu(src->total_io_u[i]);
Jens Axboe93eee042011-10-03 21:08:48 +0200560 dst->short_io_u[i] = le64_to_cpu(src->short_io_u[i]);
Jens Axboea64e88d2011-10-03 14:20:01 +0200561 }
562
563 dst->total_submit = le64_to_cpu(src->total_submit);
564 dst->total_complete = le64_to_cpu(src->total_complete);
565
566 for (i = 0; i < 2; i++) {
567 dst->io_bytes[i] = le64_to_cpu(src->io_bytes[i]);
568 dst->runtime[i] = le64_to_cpu(src->runtime[i]);
569 }
570
571 dst->total_run_time = le64_to_cpu(src->total_run_time);
572 dst->continue_on_error = le16_to_cpu(src->continue_on_error);
573 dst->total_err_count = le64_to_cpu(src->total_err_count);
Jens Axboeddcc0b62011-10-03 14:45:27 +0200574 dst->first_error = le32_to_cpu(src->first_error);
575 dst->kb_base = le32_to_cpu(src->kb_base);
Jens Axboea64e88d2011-10-03 14:20:01 +0200576}
577
578static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
579{
580 int i;
581
582 for (i = 0; i < 2; i++) {
583 dst->max_run[i] = le64_to_cpu(src->max_run[i]);
584 dst->min_run[i] = le64_to_cpu(src->min_run[i]);
585 dst->max_bw[i] = le64_to_cpu(src->max_bw[i]);
586 dst->min_bw[i] = le64_to_cpu(src->min_bw[i]);
587 dst->io_kb[i] = le64_to_cpu(src->io_kb[i]);
588 dst->agg[i] = le64_to_cpu(src->agg[i]);
589 }
590
591 dst->kb_base = le32_to_cpu(src->kb_base);
592 dst->groupid = le32_to_cpu(src->groupid);
593}
594
595static void handle_ts(struct fio_net_cmd *cmd)
596{
597 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
598
599 convert_ts(&p->ts, &p->ts);
600 convert_gs(&p->rs, &p->rs);
601
602 show_thread_status(&p->ts, &p->rs);
Jens Axboe37f0c1a2011-10-11 14:08:33 +0200603
604 if (sum_stat_clients == 1)
605 return;
606
607 sum_thread_stats(&client_ts, &p->ts, sum_stat_nr);
608 sum_group_stats(&client_gs, &p->rs);
609
610 client_ts.members++;
611 client_ts.groupid = p->ts.groupid;
612
613 if (++sum_stat_nr == sum_stat_clients) {
614 strcpy(client_ts.name, "All clients");
615 show_thread_status(&client_ts, &client_gs);
616 }
Jens Axboea64e88d2011-10-03 14:20:01 +0200617}
618
619static void handle_gs(struct fio_net_cmd *cmd)
620{
621 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
622
623 convert_gs(gs, gs);
624 show_group_stats(gs);
625}
626
Jens Axboed09a64a2011-10-13 11:38:56 +0200627static void convert_agg(struct disk_util_agg *agg)
628{
629 int i;
630
631 for (i = 0; i < 2; i++) {
632 agg->ios[i] = le32_to_cpu(agg->ios[i]);
633 agg->merges[i] = le32_to_cpu(agg->merges[i]);
634 agg->sectors[i] = le64_to_cpu(agg->sectors[i]);
635 agg->ticks[i] = le32_to_cpu(agg->ticks[i]);
636 }
637
638 agg->io_ticks = le32_to_cpu(agg->io_ticks);
639 agg->time_in_queue = le32_to_cpu(agg->time_in_queue);
640 agg->slavecount = le32_to_cpu(agg->slavecount);
641 agg->max_util.u.f = __le64_to_cpu(fio_uint64_to_double(agg->max_util.u.i));
642}
643
644static void convert_dus(struct disk_util_stat *dus)
645{
646 int i;
647
648 for (i = 0; i < 2; i++) {
649 dus->ios[i] = le32_to_cpu(dus->ios[i]);
650 dus->merges[i] = le32_to_cpu(dus->merges[i]);
651 dus->sectors[i] = le64_to_cpu(dus->sectors[i]);
652 dus->ticks[i] = le32_to_cpu(dus->ticks[i]);
653 }
654
655 dus->io_ticks = le32_to_cpu(dus->io_ticks);
656 dus->time_in_queue = le32_to_cpu(dus->time_in_queue);
657 dus->msec = le64_to_cpu(dus->msec);
658}
659
660static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd)
661{
662 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
663
664 convert_dus(&du->dus);
665 convert_agg(&du->agg);
666
667 if (!client->disk_stats_shown) {
668 client->disk_stats_shown = 1;
669 log_info("\nDisk stats (read/write):\n");
670 }
671
Jens Axboef2f788d2011-10-13 14:03:52 +0200672 print_disk_util(&du->dus, &du->agg, terse_output);
Jens Axboed09a64a2011-10-13 11:38:56 +0200673}
674
Jens Axboe48fbb462011-10-09 12:19:08 +0200675static void convert_jobs_eta(struct jobs_eta *je)
Jens Axboecf451d12011-10-03 16:48:30 +0200676{
Jens Axboecf451d12011-10-03 16:48:30 +0200677 int i;
678
679 je->nr_running = le32_to_cpu(je->nr_running);
680 je->nr_ramp = le32_to_cpu(je->nr_ramp);
681 je->nr_pending = le32_to_cpu(je->nr_pending);
682 je->files_open = le32_to_cpu(je->files_open);
683 je->m_rate = le32_to_cpu(je->m_rate);
684 je->t_rate = le32_to_cpu(je->t_rate);
685 je->m_iops = le32_to_cpu(je->m_iops);
686 je->t_iops = le32_to_cpu(je->t_iops);
687
688 for (i = 0; i < 2; i++) {
689 je->rate[i] = le32_to_cpu(je->rate[i]);
690 je->iops[i] = le32_to_cpu(je->iops[i]);
691 }
692
Jens Axboeb51eedb2011-10-09 12:13:39 +0200693 je->elapsed_sec = le64_to_cpu(je->elapsed_sec);
Jens Axboecf451d12011-10-03 16:48:30 +0200694 je->eta_sec = le64_to_cpu(je->eta_sec);
Jens Axboe48fbb462011-10-09 12:19:08 +0200695}
Jens Axboecf451d12011-10-03 16:48:30 +0200696
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200697static void sum_jobs_eta(struct jobs_eta *dst, struct jobs_eta *je)
Jens Axboe48fbb462011-10-09 12:19:08 +0200698{
Jens Axboe48fbb462011-10-09 12:19:08 +0200699 int i;
700
701 dst->nr_running += je->nr_running;
702 dst->nr_ramp += je->nr_ramp;
703 dst->nr_pending += je->nr_pending;
704 dst->files_open += je->files_open;
705 dst->m_rate += je->m_rate;
706 dst->t_rate += je->t_rate;
707 dst->m_iops += je->m_iops;
708 dst->t_iops += je->t_iops;
709
710 for (i = 0; i < 2; i++) {
711 dst->rate[i] += je->rate[i];
712 dst->iops[i] += je->iops[i];
713 }
714
715 dst->elapsed_sec += je->elapsed_sec;
716
717 if (je->eta_sec > dst->eta_sec)
718 dst->eta_sec = je->eta_sec;
719}
720
Jens Axboe82c1ed32011-10-10 08:56:18 +0200721static void dec_jobs_eta(struct client_eta *eta)
722{
723 if (!--eta->pending) {
724 display_thread_status(&eta->eta);
725 free(eta);
726 }
727}
728
Jens Axboe89c17072011-10-11 10:15:51 +0200729static void remove_reply_cmd(struct fio_client *client, struct fio_net_cmd *cmd)
730{
731 struct fio_net_int_cmd *icmd = NULL;
732 struct flist_head *entry;
733
734 flist_for_each(entry, &client->cmd_list) {
735 icmd = flist_entry(entry, struct fio_net_int_cmd, list);
736
Jens Axboedf380932011-10-11 14:25:08 +0200737 if (cmd->tag == (uintptr_t) icmd)
Jens Axboe89c17072011-10-11 10:15:51 +0200738 break;
739
740 icmd = NULL;
741 }
742
743 if (!icmd) {
744 log_err("fio: client: unable to find matching tag\n");
745 return;
746 }
747
748 flist_del(&icmd->list);
749 cmd->tag = icmd->saved_tag;
750 free(icmd);
751}
752
Jens Axboe82c1ed32011-10-10 08:56:18 +0200753static void handle_eta(struct fio_client *client, struct fio_net_cmd *cmd)
Jens Axboe48fbb462011-10-09 12:19:08 +0200754{
755 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
Jens Axboedf380932011-10-11 14:25:08 +0200756 struct client_eta *eta = (struct client_eta *) (uintptr_t) cmd->tag;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200757
758 dprint(FD_NET, "client: got eta tag %p, %d\n", eta, eta->pending);
Jens Axboe48fbb462011-10-09 12:19:08 +0200759
Jens Axboef77d2672011-10-10 14:36:07 +0200760 assert(client->eta_in_flight == eta);
761
762 client->eta_in_flight = NULL;
Jens Axboe82c1ed32011-10-10 08:56:18 +0200763 flist_del_init(&client->eta_list);
764
Jens Axboe48fbb462011-10-09 12:19:08 +0200765 convert_jobs_eta(je);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200766 sum_jobs_eta(&eta->eta, je);
Jens Axboe82c1ed32011-10-10 08:56:18 +0200767 dec_jobs_eta(eta);
Jens Axboecf451d12011-10-03 16:48:30 +0200768}
769
Jens Axboeb5296dd2011-10-07 16:35:56 +0200770static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd)
Jens Axboe2e03b4b2011-10-04 09:00:40 +0200771{
772 struct cmd_probe_pdu *probe = (struct cmd_probe_pdu *) cmd->payload;
Jens Axboed2333352011-10-11 15:07:23 +0200773 const char *os, *arch;
774 char bit[16];
Jens Axboe2e03b4b2011-10-04 09:00:40 +0200775
Jens Axboecca84642011-10-07 12:47:57 +0200776 os = fio_get_os_string(probe->os);
777 if (!os)
778 os = "unknown";
779
780 arch = fio_get_arch_string(probe->arch);
781 if (!arch)
782 os = "unknown";
783
Jens Axboed2333352011-10-11 15:07:23 +0200784 sprintf(bit, "%d-bit", probe->bpp * 8);
Jens Axboe38fdef22011-10-11 14:30:06 +0200785
786 log_info("hostname=%s, be=%u, %s, os=%s, arch=%s, fio=%u.%u.%u\n",
787 probe->hostname, probe->bigendian, bit, os, arch,
788 probe->fio_major, probe->fio_minor, probe->fio_patch);
Jens Axboeb5296dd2011-10-07 16:35:56 +0200789
790 if (!client->name)
791 client->name = strdup((char *) probe->hostname);
Jens Axboe2e03b4b2011-10-04 09:00:40 +0200792}
793
Jens Axboe11e950b2011-10-16 21:34:14 +0200794static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd)
795{
796 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
797
798 client->state = Client_started;
799 client->jobs = le32_to_cpu(pdu->jobs);
800}
801
802static void handle_stop(struct fio_client *client, struct fio_net_cmd *cmd)
803{
804 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
805
806 client->state = Client_stopped;
807 client->error = le32_to_cpu(pdu->error);
Jens Axboe498c92c2011-10-17 09:14:42 +0200808
809 if (client->error)
810 log_info("client <%s>: exited with error %d\n", client->hostname, client->error);
Jens Axboe11e950b2011-10-16 21:34:14 +0200811}
812
Jens Axboee951bdc2011-10-05 21:58:45 +0200813static int handle_client(struct fio_client *client)
Jens Axboe37db14f2011-09-30 17:00:42 -0600814{
815 struct fio_net_cmd *cmd;
816
Jens Axboe60efd142011-10-04 13:30:11 +0200817 dprint(FD_NET, "client: handle %s\n", client->hostname);
818
Jens Axboee951bdc2011-10-05 21:58:45 +0200819 cmd = fio_net_recv_cmd(client->fd);
820 if (!cmd)
821 return 0;
Jens Axboec2c94582011-10-05 20:31:30 +0200822
Jens Axboe89c17072011-10-11 10:15:51 +0200823 dprint(FD_NET, "client: got cmd op %s from %s\n",
824 fio_server_op(cmd->opcode), client->hostname);
Jens Axboe46c48f12011-10-01 12:50:51 -0600825
Jens Axboee951bdc2011-10-05 21:58:45 +0200826 switch (cmd->opcode) {
827 case FIO_NET_CMD_QUIT:
828 remove_client(client);
829 free(cmd);
830 break;
831 case FIO_NET_CMD_TEXT: {
832 const char *buf = (const char *) cmd->payload;
Jens Axboeb5296dd2011-10-07 16:35:56 +0200833 const char *name;
Jens Axboee951bdc2011-10-05 21:58:45 +0200834 int fio_unused ret;
Jens Axboe17dd1762011-10-04 13:27:34 +0200835
Jens Axboeb5296dd2011-10-07 16:35:56 +0200836 name = client->name ? client->name : client->hostname;
837
Jens Axboee951bdc2011-10-05 21:58:45 +0200838 if (!client->skip_newline)
Jens Axboeb5296dd2011-10-07 16:35:56 +0200839 fprintf(f_out, "<%s> ", name);
Jens Axboee951bdc2011-10-05 21:58:45 +0200840 ret = fwrite(buf, cmd->pdu_len, 1, f_out);
841 fflush(f_out);
842 client->skip_newline = strchr(buf, '\n') == NULL;
843 free(cmd);
844 break;
Jens Axboe37db14f2011-09-30 17:00:42 -0600845 }
Jens Axboed09a64a2011-10-13 11:38:56 +0200846 case FIO_NET_CMD_DU:
847 handle_du(client, cmd);
848 free(cmd);
849 break;
Jens Axboee951bdc2011-10-05 21:58:45 +0200850 case FIO_NET_CMD_TS:
851 handle_ts(cmd);
852 free(cmd);
853 break;
854 case FIO_NET_CMD_GS:
855 handle_gs(cmd);
856 free(cmd);
857 break;
858 case FIO_NET_CMD_ETA:
Jens Axboe89c17072011-10-11 10:15:51 +0200859 remove_reply_cmd(client, cmd);
Jens Axboe82c1ed32011-10-10 08:56:18 +0200860 handle_eta(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +0200861 free(cmd);
862 break;
863 case FIO_NET_CMD_PROBE:
Jens Axboe89c17072011-10-11 10:15:51 +0200864 remove_reply_cmd(client, cmd);
Jens Axboeb5296dd2011-10-07 16:35:56 +0200865 handle_probe(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +0200866 free(cmd);
867 break;
Jens Axboe01be0382011-10-15 14:43:41 +0200868 case FIO_NET_CMD_RUN:
869 client->state = Client_running;
870 free(cmd);
871 break;
Jens Axboee951bdc2011-10-05 21:58:45 +0200872 case FIO_NET_CMD_START:
Jens Axboe11e950b2011-10-16 21:34:14 +0200873 handle_start(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +0200874 free(cmd);
875 break;
876 case FIO_NET_CMD_STOP:
Jens Axboe11e950b2011-10-16 21:34:14 +0200877 handle_stop(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +0200878 free(cmd);
879 break;
880 default:
Jens Axboe89c17072011-10-11 10:15:51 +0200881 log_err("fio: unknown client op: %s\n", fio_server_op(cmd->opcode));
Jens Axboee951bdc2011-10-05 21:58:45 +0200882 free(cmd);
883 break;
Jens Axboe37db14f2011-09-30 17:00:42 -0600884 }
885
Jens Axboee951bdc2011-10-05 21:58:45 +0200886 return 1;
Jens Axboe37db14f2011-09-30 17:00:42 -0600887}
Jens Axboeb66570d2011-10-01 11:11:35 -0600888
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200889static void request_client_etas(void)
890{
891 struct fio_client *client;
892 struct flist_head *entry;
893 struct client_eta *eta;
Jens Axboe82c1ed32011-10-10 08:56:18 +0200894 int skipped = 0;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200895
896 dprint(FD_NET, "client: request eta (%d)\n", nr_clients);
897
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200898 eta = malloc(sizeof(*eta));
899 memset(&eta->eta, 0, sizeof(eta->eta));
900 eta->pending = nr_clients;
901
902 flist_for_each(entry, &client_list) {
903 client = flist_entry(entry, struct fio_client, list);
904
Jens Axboe82c1ed32011-10-10 08:56:18 +0200905 if (!flist_empty(&client->eta_list)) {
906 skipped++;
907 continue;
908 }
Jens Axboe01be0382011-10-15 14:43:41 +0200909 if (client->state != Client_running)
910 continue;
Jens Axboe82c1ed32011-10-10 08:56:18 +0200911
Jens Axboef77d2672011-10-10 14:36:07 +0200912 assert(!client->eta_in_flight);
Jens Axboe82c1ed32011-10-10 08:56:18 +0200913 flist_add_tail(&client->eta_list, &eta_list);
Jens Axboef77d2672011-10-10 14:36:07 +0200914 client->eta_in_flight = eta;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200915 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_SEND_ETA,
Jens Axboedf380932011-10-11 14:25:08 +0200916 (uintptr_t) eta, &client->cmd_list);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200917 }
918
Jens Axboe82c1ed32011-10-10 08:56:18 +0200919 while (skipped--)
920 dec_jobs_eta(eta);
921
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200922 dprint(FD_NET, "client: requested eta tag %p\n", eta);
923}
924
Jens Axboe89c17072011-10-11 10:15:51 +0200925static int client_check_cmd_timeout(struct fio_client *client,
926 struct timeval *now)
927{
928 struct fio_net_int_cmd *cmd;
929 struct flist_head *entry, *tmp;
930 int ret = 0;
931
932 flist_for_each_safe(entry, tmp, &client->cmd_list) {
933 cmd = flist_entry(entry, struct fio_net_int_cmd, list);
934
935 if (mtime_since(&cmd->tv, now) < FIO_NET_CLIENT_TIMEOUT)
936 continue;
937
938 log_err("fio: client %s, timeout on cmd %s\n", client->hostname,
939 fio_server_op(cmd->cmd.opcode));
940 flist_del(&cmd->list);
941 free(cmd);
942 ret = 1;
943 }
944
945 return flist_empty(&client->cmd_list) && ret;
946}
947
948static int fio_client_timed_out(void)
949{
950 struct fio_client *client;
951 struct flist_head *entry, *tmp;
952 struct timeval tv;
953 int ret = 0;
954
955 gettimeofday(&tv, NULL);
956
957 flist_for_each_safe(entry, tmp, &client_list) {
958 client = flist_entry(entry, struct fio_client, list);
959
960 if (flist_empty(&client->cmd_list))
961 continue;
962
963 if (!client_check_cmd_timeout(client, &tv))
964 continue;
965
966 log_err("fio: client %s timed out\n", client->hostname);
967 remove_client(client);
968 ret = 1;
969 }
970
971 return ret;
972}
973
Jens Axboeb66570d2011-10-01 11:11:35 -0600974int fio_handle_clients(void)
975{
976 struct fio_client *client;
977 struct flist_head *entry;
978 struct pollfd *pfds;
Jens Axboe498c92c2011-10-17 09:14:42 +0200979 int i, ret = 0, retval = 0;
Jens Axboeb66570d2011-10-01 11:11:35 -0600980
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200981 gettimeofday(&eta_tv, NULL);
982
Jens Axboeb66570d2011-10-01 11:11:35 -0600983 pfds = malloc(nr_clients * sizeof(struct pollfd));
984
Jens Axboe37f0c1a2011-10-11 14:08:33 +0200985 sum_stat_clients = nr_clients;
986 init_thread_stat(&client_ts);
987 init_group_run_stat(&client_gs);
988
Jens Axboeb66570d2011-10-01 11:11:35 -0600989 while (!exit_backend && nr_clients) {
Jens Axboe82a4be12011-10-01 12:40:32 -0600990 i = 0;
991 flist_for_each(entry, &client_list) {
992 client = flist_entry(entry, struct fio_client, list);
993
994 pfds[i].fd = client->fd;
995 pfds[i].events = POLLIN;
996 i++;
997 }
998
999 assert(i == nr_clients);
1000
Jens Axboe5c2857f2011-10-04 13:14:32 +02001001 do {
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001002 struct timeval tv;
1003
1004 gettimeofday(&tv, NULL);
1005 if (mtime_since(&eta_tv, &tv) >= 900) {
1006 request_client_etas();
1007 memcpy(&eta_tv, &tv, sizeof(tv));
Jens Axboe89c17072011-10-11 10:15:51 +02001008
1009 if (fio_client_timed_out())
1010 break;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001011 }
1012
Jens Axboe5c2857f2011-10-04 13:14:32 +02001013 ret = poll(pfds, nr_clients, 100);
1014 if (ret < 0) {
1015 if (errno == EINTR)
1016 continue;
1017 log_err("fio: poll clients: %s\n", strerror(errno));
1018 break;
1019 } else if (!ret)
Jens Axboeb66570d2011-10-01 11:11:35 -06001020 continue;
Jens Axboe5c2857f2011-10-04 13:14:32 +02001021 } while (ret <= 0);
Jens Axboeb66570d2011-10-01 11:11:35 -06001022
1023 for (i = 0; i < nr_clients; i++) {
1024 if (!(pfds[i].revents & POLLIN))
1025 continue;
1026
1027 client = find_client_by_fd(pfds[i].fd);
1028 if (!client) {
Jens Axboe3c5f57e2011-10-06 12:37:50 +02001029 log_err("fio: unknown client fd %d\n", pfds[i].fd);
Jens Axboeb66570d2011-10-01 11:11:35 -06001030 continue;
1031 }
Jens Axboee951bdc2011-10-05 21:58:45 +02001032 if (!handle_client(client)) {
Jens Axboe28d3ab02011-10-05 20:45:37 +02001033 log_info("client: host=%s disconnected\n",
1034 client->hostname);
1035 remove_client(client);
Jens Axboe498c92c2011-10-17 09:14:42 +02001036 retval = 1;
Jens Axboe38990762011-10-17 13:31:33 +02001037 } else if (client->error)
Jens Axboe498c92c2011-10-17 09:14:42 +02001038 retval = 1;
Jens Axboeb66570d2011-10-01 11:11:35 -06001039 }
1040 }
1041
1042 free(pfds);
Jens Axboe498c92c2011-10-17 09:14:42 +02001043 return retval;
Jens Axboeb66570d2011-10-01 11:11:35 -06001044}