blob: 3c6778565882b969df59874081e0da2217ff17b7 [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"
Stephen M. Camerondd366722012-02-24 08:17:30 +010019#include "client.h"
Jens Axboe132159a2011-09-30 15:01:32 -060020#include "server.h"
Jens Axboeb66570d2011-10-01 11:11:35 -060021#include "flist.h"
Jens Axboe3c5f57e2011-10-06 12:37:50 +020022#include "hash.h"
Jens Axboe132159a2011-09-30 15:01:32 -060023
Stephen M. Cameron04cc6b72012-02-24 08:17:31 +010024extern void (*update_thread_status)(char *status_message, double perc);
Stephen M. Cameronba936c42012-02-24 08:17:31 +010025
Jens Axboe82c1ed32011-10-10 08:56:18 +020026struct client_eta {
27 struct jobs_eta eta;
28 unsigned int pending;
29};
30
Jens Axboeb66570d2011-10-01 11:11:35 -060031struct fio_client {
32 struct flist_head list;
Jens Axboebebe6392011-10-07 10:00:51 +020033 struct flist_head hash_list;
Jens Axboe3f3a4542011-10-10 21:11:09 +020034 struct flist_head arg_list;
Jens Axboe811826b2011-10-24 09:11:50 +020035 union {
36 struct sockaddr_in addr;
37 struct sockaddr_in6 addr6;
38 struct sockaddr_un addr_un;
39 };
Jens Axboeb66570d2011-10-01 11:11:35 -060040 char *hostname;
Jens Axboebebe6392011-10-07 10:00:51 +020041 int port;
Jens Axboeb66570d2011-10-01 11:11:35 -060042 int fd;
Jens Axboe81179ee2011-10-04 12:42:06 +020043
Jens Axboeb5296dd2011-10-07 16:35:56 +020044 char *name;
45
Jens Axboe81179ee2011-10-04 12:42:06 +020046 int state;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +020047
Jens Axboe17dd1762011-10-04 13:27:34 +020048 int skip_newline;
Jens Axboe87aa8f12011-10-06 21:24:13 +020049 int is_sock;
Jens Axboed09a64a2011-10-13 11:38:56 +020050 int disk_stats_shown;
Jens Axboe11e950b2011-10-16 21:34:14 +020051 unsigned int jobs;
52 int error;
Jens Axboe811826b2011-10-24 09:11:50 +020053 int ipv6;
Jens Axboec2cb6862012-02-23 20:56:12 +010054 int sent_job;
Jens Axboe82c1ed32011-10-10 08:56:18 +020055
56 struct flist_head eta_list;
57 struct client_eta *eta_in_flight;
Jens Axboe81179ee2011-10-04 12:42:06 +020058
Jens Axboe89c17072011-10-11 10:15:51 +020059 struct flist_head cmd_list;
60
Jens Axboe81179ee2011-10-04 12:42:06 +020061 uint16_t argc;
62 char **argv;
63};
64
Stephen M. Camerondd366722012-02-24 08:17:30 +010065static void fio_client_text_op(struct fio_client *client,
66 FILE *f, __u16 pdu_len, const char *buf)
67{
68 const char *name;
69 int fio_unused ret;
70
71 name = client->name ? client->name : client->hostname;
72
73 if (!client->skip_newline)
74 fprintf(f, "<%s> ", name);
75 ret = fwrite(buf, pdu_len, 1, f);
76 fflush(f);
77 client->skip_newline = strchr(buf, '\n') == NULL;
78}
79
80static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd);
81static void handle_ts(struct fio_net_cmd *cmd);
82static void handle_gs(struct fio_net_cmd *cmd);
83static void handle_eta(struct fio_client *client, struct fio_net_cmd *cmd);
84static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd);
85
86struct client_ops fio_client_ops = {
Jens Axboe0420ba62012-02-29 11:16:52 +010087 .text_op = fio_client_text_op,
88 .disk_util = handle_du,
89 .thread_status = handle_ts,
90 .group_stats = handle_gs,
91 .eta = handle_eta,
92 .probe = handle_probe,
93 /* status display, if NULL, printf is used */
Stephen M. Camerondd366722012-02-24 08:17:30 +010094};
95
Jens Axboeaf9c9fb2011-10-09 21:54:10 +020096static struct timeval eta_tv;
Jens Axboe48fbb462011-10-09 12:19:08 +020097
Jens Axboe81179ee2011-10-04 12:42:06 +020098enum {
Jens Axboe5c2857f2011-10-04 13:14:32 +020099 Client_created = 0,
Jens Axboe81179ee2011-10-04 12:42:06 +0200100 Client_connected = 1,
101 Client_started = 2,
Jens Axboe01be0382011-10-15 14:43:41 +0200102 Client_running = 3,
103 Client_stopped = 4,
104 Client_exited = 5,
Jens Axboeb66570d2011-10-01 11:11:35 -0600105};
106
107static FLIST_HEAD(client_list);
Jens Axboe82c1ed32011-10-10 08:56:18 +0200108static FLIST_HEAD(eta_list);
Jens Axboeb66570d2011-10-01 11:11:35 -0600109
Jens Axboe3f3a4542011-10-10 21:11:09 +0200110static FLIST_HEAD(arg_list);
111
Jens Axboe37f0c1a2011-10-11 14:08:33 +0200112static struct thread_stat client_ts;
113static struct group_run_stats client_gs;
114static int sum_stat_clients;
115static int sum_stat_nr;
116
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200117#define FIO_CLIENT_HASH_BITS 7
118#define FIO_CLIENT_HASH_SZ (1 << FIO_CLIENT_HASH_BITS)
119#define FIO_CLIENT_HASH_MASK (FIO_CLIENT_HASH_SZ - 1)
Jens Axboebebe6392011-10-07 10:00:51 +0200120static struct flist_head client_hash[FIO_CLIENT_HASH_SZ];
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200121
Stephen M. Camerondd366722012-02-24 08:17:30 +0100122static int handle_client(struct fio_client *client, struct client_ops *ops);
Jens Axboe82c1ed32011-10-10 08:56:18 +0200123static void dec_jobs_eta(struct client_eta *eta);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200124
Jens Axboebebe6392011-10-07 10:00:51 +0200125static void fio_client_add_hash(struct fio_client *client)
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200126{
127 int bucket = hash_long(client->fd, FIO_CLIENT_HASH_BITS);
128
129 bucket &= FIO_CLIENT_HASH_MASK;
Jens Axboebebe6392011-10-07 10:00:51 +0200130 flist_add(&client->hash_list, &client_hash[bucket]);
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200131}
132
Jens Axboebebe6392011-10-07 10:00:51 +0200133static void fio_client_remove_hash(struct fio_client *client)
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200134{
Jens Axboebebe6392011-10-07 10:00:51 +0200135 if (!flist_empty(&client->hash_list))
136 flist_del_init(&client->hash_list);
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200137}
138
139static void fio_init fio_client_hash_init(void)
140{
141 int i;
142
Jens Axboebebe6392011-10-07 10:00:51 +0200143 for (i = 0; i < FIO_CLIENT_HASH_SZ; i++)
144 INIT_FLIST_HEAD(&client_hash[i]);
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200145}
146
Jens Axboeb66570d2011-10-01 11:11:35 -0600147static struct fio_client *find_client_by_fd(int fd)
148{
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200149 int bucket = hash_long(fd, FIO_CLIENT_HASH_BITS) & FIO_CLIENT_HASH_MASK;
Jens Axboeb66570d2011-10-01 11:11:35 -0600150 struct fio_client *client;
151 struct flist_head *entry;
152
Jens Axboebebe6392011-10-07 10:00:51 +0200153 flist_for_each(entry, &client_hash[bucket]) {
154 client = flist_entry(entry, struct fio_client, hash_list);
Jens Axboeb66570d2011-10-01 11:11:35 -0600155
156 if (client->fd == fd)
157 return client;
158 }
159
160 return NULL;
161}
162
Jens Axboeb66570d2011-10-01 11:11:35 -0600163static void remove_client(struct fio_client *client)
164{
Jens Axboe39e8e012011-10-04 13:46:08 +0200165 dprint(FD_NET, "client: removed <%s>\n", client->hostname);
Jens Axboeb66570d2011-10-01 11:11:35 -0600166 flist_del(&client->list);
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200167
Jens Axboebebe6392011-10-07 10:00:51 +0200168 fio_client_remove_hash(client);
Jens Axboe81179ee2011-10-04 12:42:06 +0200169
Jens Axboe82c1ed32011-10-10 08:56:18 +0200170 if (!flist_empty(&client->eta_list)) {
171 flist_del_init(&client->eta_list);
172 dec_jobs_eta(client->eta_in_flight);
173 }
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200174
Jens Axboeb66570d2011-10-01 11:11:35 -0600175 free(client->hostname);
Jens Axboe81179ee2011-10-04 12:42:06 +0200176 if (client->argv)
177 free(client->argv);
Jens Axboeb5296dd2011-10-07 16:35:56 +0200178 if (client->name)
179 free(client->name);
Jens Axboe81179ee2011-10-04 12:42:06 +0200180
Jens Axboeb66570d2011-10-01 11:11:35 -0600181 free(client);
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200182 nr_clients--;
Jens Axboe5fd0acb2011-10-11 14:20:22 +0200183 sum_stat_clients--;
Jens Axboeb66570d2011-10-01 11:11:35 -0600184}
Jens Axboe132159a2011-09-30 15:01:32 -0600185
Jens Axboefa2ea802011-10-08 21:07:29 +0200186static void __fio_client_add_cmd_option(struct fio_client *client,
187 const char *opt)
Jens Axboe81179ee2011-10-04 12:42:06 +0200188{
Jens Axboe39e8e012011-10-04 13:46:08 +0200189 int index;
190
191 index = client->argc++;
Jens Axboe81179ee2011-10-04 12:42:06 +0200192 client->argv = realloc(client->argv, sizeof(char *) * client->argc);
Jens Axboe39e8e012011-10-04 13:46:08 +0200193 client->argv[index] = strdup(opt);
194 dprint(FD_NET, "client: add cmd %d: %s\n", index, opt);
Jens Axboe81179ee2011-10-04 12:42:06 +0200195}
196
Jens Axboefa2ea802011-10-08 21:07:29 +0200197void fio_client_add_cmd_option(void *cookie, const char *opt)
Jens Axboe81179ee2011-10-04 12:42:06 +0200198{
Jens Axboebebe6392011-10-07 10:00:51 +0200199 struct fio_client *client = cookie;
Jens Axboe3f3a4542011-10-10 21:11:09 +0200200 struct flist_head *entry;
Jens Axboe81179ee2011-10-04 12:42:06 +0200201
Jens Axboebebe6392011-10-07 10:00:51 +0200202 if (!client || !opt)
Jens Axboefa2ea802011-10-08 21:07:29 +0200203 return;
Jens Axboe81179ee2011-10-04 12:42:06 +0200204
Jens Axboefa2ea802011-10-08 21:07:29 +0200205 __fio_client_add_cmd_option(client, opt);
Jens Axboe3f3a4542011-10-10 21:11:09 +0200206
207 /*
208 * Duplicate arguments to shared client group
209 */
210 flist_for_each(entry, &arg_list) {
211 client = flist_entry(entry, struct fio_client, arg_list);
212
213 __fio_client_add_cmd_option(client, opt);
214 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200215}
216
Jens Axboebebe6392011-10-07 10:00:51 +0200217int fio_client_add(const char *hostname, void **cookie)
Jens Axboe132159a2011-09-30 15:01:32 -0600218{
Jens Axboe3f3a4542011-10-10 21:11:09 +0200219 struct fio_client *existing = *cookie;
Jens Axboeb66570d2011-10-01 11:11:35 -0600220 struct fio_client *client;
Jens Axboe132159a2011-09-30 15:01:32 -0600221
Jens Axboe3f3a4542011-10-10 21:11:09 +0200222 if (existing) {
223 /*
224 * We always add our "exec" name as the option, hence 1
225 * means empty.
226 */
227 if (existing->argc == 1)
228 flist_add_tail(&existing->arg_list, &arg_list);
229 else {
230 while (!flist_empty(&arg_list))
231 flist_del_init(arg_list.next);
232 }
233 }
234
Jens Axboeb66570d2011-10-01 11:11:35 -0600235 client = malloc(sizeof(*client));
Jens Axboea37f69b2011-10-01 12:24:21 -0600236 memset(client, 0, sizeof(*client));
Jens Axboe81179ee2011-10-04 12:42:06 +0200237
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200238 INIT_FLIST_HEAD(&client->list);
Jens Axboebebe6392011-10-07 10:00:51 +0200239 INIT_FLIST_HEAD(&client->hash_list);
Jens Axboe3f3a4542011-10-10 21:11:09 +0200240 INIT_FLIST_HEAD(&client->arg_list);
Jens Axboe82c1ed32011-10-10 08:56:18 +0200241 INIT_FLIST_HEAD(&client->eta_list);
Jens Axboe89c17072011-10-11 10:15:51 +0200242 INIT_FLIST_HEAD(&client->cmd_list);
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200243
Jens Axboebebe6392011-10-07 10:00:51 +0200244 if (fio_server_parse_string(hostname, &client->hostname,
245 &client->is_sock, &client->port,
Jens Axboe811826b2011-10-24 09:11:50 +0200246 &client->addr.sin_addr,
247 &client->addr6.sin6_addr,
248 &client->ipv6))
Jens Axboebebe6392011-10-07 10:00:51 +0200249 return -1;
250
Jens Axboea37f69b2011-10-01 12:24:21 -0600251 client->fd = -1;
Jens Axboe81179ee2011-10-04 12:42:06 +0200252
253 __fio_client_add_cmd_option(client, "fio");
254
Jens Axboea37f69b2011-10-01 12:24:21 -0600255 flist_add(&client->list, &client_list);
256 nr_clients++;
Jens Axboebebe6392011-10-07 10:00:51 +0200257 dprint(FD_NET, "client: added <%s>\n", client->hostname);
258 *cookie = client;
259 return 0;
Jens Axboea37f69b2011-10-01 12:24:21 -0600260}
261
Jens Axboe87aa8f12011-10-06 21:24:13 +0200262static int fio_client_connect_ip(struct fio_client *client)
Jens Axboea37f69b2011-10-01 12:24:21 -0600263{
Jens Axboe811826b2011-10-24 09:11:50 +0200264 struct sockaddr *addr;
265 fio_socklen_t socklen;
266 int fd, domain;
Jens Axboe132159a2011-09-30 15:01:32 -0600267
Jens Axboe811826b2011-10-24 09:11:50 +0200268 if (client->ipv6) {
269 client->addr6.sin6_family = AF_INET6;
270 client->addr6.sin6_port = htons(client->port);
271 domain = AF_INET6;
272 addr = (struct sockaddr *) &client->addr6;
273 socklen = sizeof(client->addr6);
274 } else {
275 client->addr.sin_family = AF_INET;
276 client->addr.sin_port = htons(client->port);
277 domain = AF_INET;
278 addr = (struct sockaddr *) &client->addr;
279 socklen = sizeof(client->addr);
280 }
Jens Axboe132159a2011-09-30 15:01:32 -0600281
Jens Axboe811826b2011-10-24 09:11:50 +0200282 fd = socket(domain, SOCK_STREAM, 0);
Jens Axboe132159a2011-09-30 15:01:32 -0600283 if (fd < 0) {
284 log_err("fio: socket: %s\n", strerror(errno));
Jens Axboe87aa8f12011-10-06 21:24:13 +0200285 return -1;
Jens Axboe132159a2011-09-30 15:01:32 -0600286 }
287
Jens Axboe811826b2011-10-24 09:11:50 +0200288 if (connect(fd, addr, socklen) < 0) {
Jens Axboe132159a2011-09-30 15:01:32 -0600289 log_err("fio: connect: %s\n", strerror(errno));
Jens Axboea7de0a12011-10-07 12:55:14 +0200290 log_err("fio: failed to connect to %s:%u\n", client->hostname,
291 client->port);
Jens Axboeb94cba42011-10-06 21:27:10 +0200292 close(fd);
Jens Axboe87aa8f12011-10-06 21:24:13 +0200293 return -1;
Jens Axboe132159a2011-09-30 15:01:32 -0600294 }
295
Jens Axboe87aa8f12011-10-06 21:24:13 +0200296 return fd;
297}
298
299static int fio_client_connect_sock(struct fio_client *client)
300{
301 struct sockaddr_un *addr = &client->addr_un;
302 fio_socklen_t len;
303 int fd;
304
305 memset(addr, 0, sizeof(*addr));
306 addr->sun_family = AF_UNIX;
307 strcpy(addr->sun_path, client->hostname);
308
309 fd = socket(AF_UNIX, SOCK_STREAM, 0);
310 if (fd < 0) {
311 log_err("fio: socket: %s\n", strerror(errno));
312 return -1;
313 }
314
315 len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
316 if (connect(fd, (struct sockaddr *) addr, len) < 0) {
317 log_err("fio: connect; %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200318 close(fd);
Jens Axboe87aa8f12011-10-06 21:24:13 +0200319 return -1;
320 }
321
322 return fd;
323}
324
325static int fio_client_connect(struct fio_client *client)
326{
327 int fd;
328
329 dprint(FD_NET, "client: connect to host %s\n", client->hostname);
330
Jens Axboe87aa8f12011-10-06 21:24:13 +0200331 if (client->is_sock)
332 fd = fio_client_connect_sock(client);
333 else
334 fd = fio_client_connect_ip(client);
335
Jens Axboe89c17072011-10-11 10:15:51 +0200336 dprint(FD_NET, "client: %s connected %d\n", client->hostname, fd);
337
Jens Axboe87aa8f12011-10-06 21:24:13 +0200338 if (fd < 0)
339 return 1;
340
Jens Axboeb66570d2011-10-01 11:11:35 -0600341 client->fd = fd;
Jens Axboebebe6392011-10-07 10:00:51 +0200342 fio_client_add_hash(client);
Jens Axboe81179ee2011-10-04 12:42:06 +0200343 client->state = Client_connected;
Jens Axboe132159a2011-09-30 15:01:32 -0600344 return 0;
345}
346
Jens Axboecc0df002011-10-03 20:53:32 +0200347void fio_clients_terminate(void)
348{
349 struct flist_head *entry;
350 struct fio_client *client;
351
Jens Axboe60efd142011-10-04 13:30:11 +0200352 dprint(FD_NET, "client: terminate clients\n");
353
Jens Axboecc0df002011-10-03 20:53:32 +0200354 flist_for_each(entry, &client_list) {
355 client = flist_entry(entry, struct fio_client, list);
356
Jens Axboe89c17072011-10-11 10:15:51 +0200357 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_QUIT, 0, NULL);
Jens Axboecc0df002011-10-03 20:53:32 +0200358 }
359}
360
361static void sig_int(int sig)
362{
Jens Axboebebe6392011-10-07 10:00:51 +0200363 dprint(FD_NET, "client: got signal %d\n", sig);
Jens Axboecc0df002011-10-03 20:53:32 +0200364 fio_clients_terminate();
365}
366
367static void client_signal_handler(void)
368{
369 struct sigaction act;
370
371 memset(&act, 0, sizeof(act));
372 act.sa_handler = sig_int;
373 act.sa_flags = SA_RESTART;
374 sigaction(SIGINT, &act, NULL);
375
376 memset(&act, 0, sizeof(act));
377 act.sa_handler = sig_int;
378 act.sa_flags = SA_RESTART;
379 sigaction(SIGTERM, &act, NULL);
380}
381
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200382static void probe_client(struct fio_client *client)
383{
Jens Axboe60efd142011-10-04 13:30:11 +0200384 dprint(FD_NET, "client: send probe\n");
385
Jens Axboe89c17072011-10-11 10:15:51 +0200386 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_PROBE, 0, &client->cmd_list);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200387}
388
Jens Axboe81179ee2011-10-04 12:42:06 +0200389static int send_client_cmd_line(struct fio_client *client)
390{
Jens Axboefa2ea802011-10-08 21:07:29 +0200391 struct cmd_single_line_pdu *cslp;
392 struct cmd_line_pdu *clp;
393 unsigned long offset;
Jens Axboe7f868312011-10-10 09:55:21 +0200394 unsigned int *lens;
Jens Axboefa2ea802011-10-08 21:07:29 +0200395 void *pdu;
396 size_t mem;
Jens Axboe81179ee2011-10-04 12:42:06 +0200397 int i, ret;
398
Jens Axboe39e8e012011-10-04 13:46:08 +0200399 dprint(FD_NET, "client: send cmdline %d\n", client->argc);
Jens Axboe60efd142011-10-04 13:30:11 +0200400
Jens Axboe7f868312011-10-10 09:55:21 +0200401 lens = malloc(client->argc * sizeof(unsigned int));
402
Jens Axboefa2ea802011-10-08 21:07:29 +0200403 /*
404 * Find out how much mem we need
405 */
Jens Axboe7f868312011-10-10 09:55:21 +0200406 for (i = 0, mem = 0; i < client->argc; i++) {
407 lens[i] = strlen(client->argv[i]) + 1;
408 mem += lens[i];
409 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200410
Jens Axboefa2ea802011-10-08 21:07:29 +0200411 /*
412 * We need one cmd_line_pdu, and argc number of cmd_single_line_pdu
413 */
414 mem += sizeof(*clp) + (client->argc * sizeof(*cslp));
415
416 pdu = malloc(mem);
417 clp = pdu;
418 offset = sizeof(*clp);
419
420 for (i = 0; i < client->argc; i++) {
Jens Axboe7f868312011-10-10 09:55:21 +0200421 uint16_t arg_len = lens[i];
Jens Axboefa2ea802011-10-08 21:07:29 +0200422
423 cslp = pdu + offset;
424 strcpy((char *) cslp->text, client->argv[i]);
425 cslp->len = cpu_to_le16(arg_len);
426 offset += sizeof(*cslp) + arg_len;
427 }
428
Jens Axboe7f868312011-10-10 09:55:21 +0200429 free(lens);
Jens Axboefa2ea802011-10-08 21:07:29 +0200430 clp->lines = cpu_to_le16(client->argc);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200431 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOBLINE, pdu, mem, 0);
Jens Axboe81179ee2011-10-04 12:42:06 +0200432 free(pdu);
433 return ret;
434}
435
Jens Axboea37f69b2011-10-01 12:24:21 -0600436int fio_clients_connect(void)
437{
438 struct fio_client *client;
439 struct flist_head *entry, *tmp;
440 int ret;
441
Bruce Cran93bcfd22012-02-20 20:18:19 +0100442#ifdef WIN32
443 WSADATA wsd;
444 WSAStartup(MAKEWORD(2,2), &wsd);
445#endif
446
Jens Axboe60efd142011-10-04 13:30:11 +0200447 dprint(FD_NET, "client: connect all\n");
448
Jens Axboecc0df002011-10-03 20:53:32 +0200449 client_signal_handler();
450
Jens Axboea37f69b2011-10-01 12:24:21 -0600451 flist_for_each_safe(entry, tmp, &client_list) {
452 client = flist_entry(entry, struct fio_client, list);
453
454 ret = fio_client_connect(client);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200455 if (ret) {
Jens Axboea37f69b2011-10-01 12:24:21 -0600456 remove_client(client);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200457 continue;
458 }
459
460 probe_client(client);
Jens Axboe81179ee2011-10-04 12:42:06 +0200461
462 if (client->argc > 1)
463 send_client_cmd_line(client);
Jens Axboea37f69b2011-10-01 12:24:21 -0600464 }
465
466 return !nr_clients;
467}
468
Jens Axboe132159a2011-09-30 15:01:32 -0600469/*
470 * Send file contents to server backend. We could use sendfile(), but to remain
471 * more portable lets just read/write the darn thing.
472 */
Jens Axboea37f69b2011-10-01 12:24:21 -0600473static int fio_client_send_ini(struct fio_client *client, const char *filename)
Jens Axboe132159a2011-09-30 15:01:32 -0600474{
475 struct stat sb;
476 char *p, *buf;
477 off_t len;
478 int fd, ret;
479
Jens Axboe46c48f12011-10-01 12:50:51 -0600480 dprint(FD_NET, "send ini %s to %s\n", filename, client->hostname);
481
Jens Axboe132159a2011-09-30 15:01:32 -0600482 fd = open(filename, O_RDONLY);
483 if (fd < 0) {
Jens Axboee951bdc2011-10-05 21:58:45 +0200484 log_err("fio: job file <%s> open: %s\n", filename, strerror(errno));
Jens Axboe132159a2011-09-30 15:01:32 -0600485 return 1;
486 }
487
488 if (fstat(fd, &sb) < 0) {
489 log_err("fio: job file stat: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200490 close(fd);
Jens Axboe132159a2011-09-30 15:01:32 -0600491 return 1;
492 }
493
494 buf = malloc(sb.st_size);
495
496 len = sb.st_size;
497 p = buf;
498 do {
499 ret = read(fd, p, len);
500 if (ret > 0) {
501 len -= ret;
502 if (!len)
503 break;
504 p += ret;
505 continue;
506 } else if (!ret)
507 break;
508 else if (errno == EAGAIN || errno == EINTR)
509 continue;
510 } while (1);
511
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200512 if (len) {
513 log_err("fio: failed reading job file %s\n", filename);
Jens Axboeb94cba42011-10-06 21:27:10 +0200514 close(fd);
Jens Axboec524ef72011-10-07 12:23:34 +0200515 free(buf);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200516 return 1;
517 }
518
Jens Axboec2cb6862012-02-23 20:56:12 +0100519 client->sent_job = 1;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200520 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, buf, sb.st_size, 0);
Jens Axboe132159a2011-09-30 15:01:32 -0600521 free(buf);
Jens Axboeb94cba42011-10-06 21:27:10 +0200522 close(fd);
Jens Axboe132159a2011-09-30 15:01:32 -0600523 return ret;
524}
Jens Axboe37db14f2011-09-30 17:00:42 -0600525
Jens Axboea37f69b2011-10-01 12:24:21 -0600526int fio_clients_send_ini(const char *filename)
527{
528 struct fio_client *client;
529 struct flist_head *entry, *tmp;
530
531 flist_for_each_safe(entry, tmp, &client_list) {
532 client = flist_entry(entry, struct fio_client, list);
533
534 if (fio_client_send_ini(client, filename))
535 remove_client(client);
Jens Axboec2cb6862012-02-23 20:56:12 +0100536
537 client->sent_job = 1;
Jens Axboea37f69b2011-10-01 12:24:21 -0600538 }
539
540 return !nr_clients;
541}
542
Jens Axboea64e88d2011-10-03 14:20:01 +0200543static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
544{
545 dst->max_val = le64_to_cpu(src->max_val);
546 dst->min_val = le64_to_cpu(src->min_val);
547 dst->samples = le64_to_cpu(src->samples);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200548
549 /*
550 * Floats arrive as IEEE 754 encoded uint64_t, convert back to double
551 */
552 dst->mean.u.f = fio_uint64_to_double(le64_to_cpu(dst->mean.u.i));
553 dst->S.u.f = fio_uint64_to_double(le64_to_cpu(dst->S.u.i));
Jens Axboea64e88d2011-10-03 14:20:01 +0200554}
555
556static void convert_ts(struct thread_stat *dst, struct thread_stat *src)
557{
558 int i, j;
559
560 dst->error = le32_to_cpu(src->error);
561 dst->groupid = le32_to_cpu(src->groupid);
562 dst->pid = le32_to_cpu(src->pid);
563 dst->members = le32_to_cpu(src->members);
564
565 for (i = 0; i < 2; i++) {
566 convert_io_stat(&dst->clat_stat[i], &src->clat_stat[i]);
567 convert_io_stat(&dst->slat_stat[i], &src->slat_stat[i]);
568 convert_io_stat(&dst->lat_stat[i], &src->lat_stat[i]);
569 convert_io_stat(&dst->bw_stat[i], &src->bw_stat[i]);
570 }
571
572 dst->usr_time = le64_to_cpu(src->usr_time);
573 dst->sys_time = le64_to_cpu(src->sys_time);
574 dst->ctx = le64_to_cpu(src->ctx);
575 dst->minf = le64_to_cpu(src->minf);
576 dst->majf = le64_to_cpu(src->majf);
577 dst->clat_percentiles = le64_to_cpu(src->clat_percentiles);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200578
579 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
580 fio_fp64_t *fps = &src->percentile_list[i];
581 fio_fp64_t *fpd = &dst->percentile_list[i];
582
583 fpd->u.f = fio_uint64_to_double(le64_to_cpu(fps->u.i));
584 }
Jens Axboea64e88d2011-10-03 14:20:01 +0200585
586 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
587 dst->io_u_map[i] = le32_to_cpu(src->io_u_map[i]);
588 dst->io_u_submit[i] = le32_to_cpu(src->io_u_submit[i]);
589 dst->io_u_complete[i] = le32_to_cpu(src->io_u_complete[i]);
590 }
591
592 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
593 dst->io_u_lat_u[i] = le32_to_cpu(src->io_u_lat_u[i]);
594 dst->io_u_lat_m[i] = le32_to_cpu(src->io_u_lat_m[i]);
595 }
596
597 for (i = 0; i < 2; i++)
598 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
599 dst->io_u_plat[i][j] = le32_to_cpu(src->io_u_plat[i][j]);
600
601 for (i = 0; i < 3; i++) {
602 dst->total_io_u[i] = le64_to_cpu(src->total_io_u[i]);
Jens Axboe93eee042011-10-03 21:08:48 +0200603 dst->short_io_u[i] = le64_to_cpu(src->short_io_u[i]);
Jens Axboea64e88d2011-10-03 14:20:01 +0200604 }
605
606 dst->total_submit = le64_to_cpu(src->total_submit);
607 dst->total_complete = le64_to_cpu(src->total_complete);
608
609 for (i = 0; i < 2; i++) {
610 dst->io_bytes[i] = le64_to_cpu(src->io_bytes[i]);
611 dst->runtime[i] = le64_to_cpu(src->runtime[i]);
612 }
613
614 dst->total_run_time = le64_to_cpu(src->total_run_time);
615 dst->continue_on_error = le16_to_cpu(src->continue_on_error);
616 dst->total_err_count = le64_to_cpu(src->total_err_count);
Jens Axboeddcc0b62011-10-03 14:45:27 +0200617 dst->first_error = le32_to_cpu(src->first_error);
618 dst->kb_base = le32_to_cpu(src->kb_base);
Jens Axboea64e88d2011-10-03 14:20:01 +0200619}
620
621static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
622{
623 int i;
624
625 for (i = 0; i < 2; i++) {
626 dst->max_run[i] = le64_to_cpu(src->max_run[i]);
627 dst->min_run[i] = le64_to_cpu(src->min_run[i]);
628 dst->max_bw[i] = le64_to_cpu(src->max_bw[i]);
629 dst->min_bw[i] = le64_to_cpu(src->min_bw[i]);
630 dst->io_kb[i] = le64_to_cpu(src->io_kb[i]);
631 dst->agg[i] = le64_to_cpu(src->agg[i]);
632 }
633
634 dst->kb_base = le32_to_cpu(src->kb_base);
635 dst->groupid = le32_to_cpu(src->groupid);
636}
637
638static void handle_ts(struct fio_net_cmd *cmd)
639{
640 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
641
642 convert_ts(&p->ts, &p->ts);
643 convert_gs(&p->rs, &p->rs);
644
645 show_thread_status(&p->ts, &p->rs);
Jens Axboe37f0c1a2011-10-11 14:08:33 +0200646
647 if (sum_stat_clients == 1)
648 return;
649
650 sum_thread_stats(&client_ts, &p->ts, sum_stat_nr);
651 sum_group_stats(&client_gs, &p->rs);
652
653 client_ts.members++;
654 client_ts.groupid = p->ts.groupid;
655
656 if (++sum_stat_nr == sum_stat_clients) {
657 strcpy(client_ts.name, "All clients");
658 show_thread_status(&client_ts, &client_gs);
659 }
Jens Axboea64e88d2011-10-03 14:20:01 +0200660}
661
662static void handle_gs(struct fio_net_cmd *cmd)
663{
664 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
665
666 convert_gs(gs, gs);
667 show_group_stats(gs);
668}
669
Jens Axboed09a64a2011-10-13 11:38:56 +0200670static void convert_agg(struct disk_util_agg *agg)
671{
672 int i;
673
674 for (i = 0; i < 2; i++) {
675 agg->ios[i] = le32_to_cpu(agg->ios[i]);
676 agg->merges[i] = le32_to_cpu(agg->merges[i]);
677 agg->sectors[i] = le64_to_cpu(agg->sectors[i]);
678 agg->ticks[i] = le32_to_cpu(agg->ticks[i]);
679 }
680
681 agg->io_ticks = le32_to_cpu(agg->io_ticks);
682 agg->time_in_queue = le32_to_cpu(agg->time_in_queue);
683 agg->slavecount = le32_to_cpu(agg->slavecount);
Anton Blanchard823ba542011-11-07 14:16:26 +0100684 agg->max_util.u.f = fio_uint64_to_double(__le64_to_cpu(agg->max_util.u.i));
Jens Axboed09a64a2011-10-13 11:38:56 +0200685}
686
687static void convert_dus(struct disk_util_stat *dus)
688{
689 int i;
690
691 for (i = 0; i < 2; i++) {
692 dus->ios[i] = le32_to_cpu(dus->ios[i]);
693 dus->merges[i] = le32_to_cpu(dus->merges[i]);
694 dus->sectors[i] = le64_to_cpu(dus->sectors[i]);
695 dus->ticks[i] = le32_to_cpu(dus->ticks[i]);
696 }
697
698 dus->io_ticks = le32_to_cpu(dus->io_ticks);
699 dus->time_in_queue = le32_to_cpu(dus->time_in_queue);
700 dus->msec = le64_to_cpu(dus->msec);
701}
702
703static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd)
704{
705 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
706
707 convert_dus(&du->dus);
708 convert_agg(&du->agg);
709
710 if (!client->disk_stats_shown) {
711 client->disk_stats_shown = 1;
712 log_info("\nDisk stats (read/write):\n");
713 }
714
Jens Axboef2f788d2011-10-13 14:03:52 +0200715 print_disk_util(&du->dus, &du->agg, terse_output);
Jens Axboed09a64a2011-10-13 11:38:56 +0200716}
717
Jens Axboe48fbb462011-10-09 12:19:08 +0200718static void convert_jobs_eta(struct jobs_eta *je)
Jens Axboecf451d12011-10-03 16:48:30 +0200719{
Jens Axboecf451d12011-10-03 16:48:30 +0200720 int i;
721
722 je->nr_running = le32_to_cpu(je->nr_running);
723 je->nr_ramp = le32_to_cpu(je->nr_ramp);
724 je->nr_pending = le32_to_cpu(je->nr_pending);
725 je->files_open = le32_to_cpu(je->files_open);
726 je->m_rate = le32_to_cpu(je->m_rate);
727 je->t_rate = le32_to_cpu(je->t_rate);
728 je->m_iops = le32_to_cpu(je->m_iops);
729 je->t_iops = le32_to_cpu(je->t_iops);
730
731 for (i = 0; i < 2; i++) {
732 je->rate[i] = le32_to_cpu(je->rate[i]);
733 je->iops[i] = le32_to_cpu(je->iops[i]);
734 }
735
Jens Axboeb51eedb2011-10-09 12:13:39 +0200736 je->elapsed_sec = le64_to_cpu(je->elapsed_sec);
Jens Axboecf451d12011-10-03 16:48:30 +0200737 je->eta_sec = le64_to_cpu(je->eta_sec);
Jens Axboe48fbb462011-10-09 12:19:08 +0200738}
Jens Axboecf451d12011-10-03 16:48:30 +0200739
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200740static void sum_jobs_eta(struct jobs_eta *dst, struct jobs_eta *je)
Jens Axboe48fbb462011-10-09 12:19:08 +0200741{
Jens Axboe48fbb462011-10-09 12:19:08 +0200742 int i;
743
744 dst->nr_running += je->nr_running;
745 dst->nr_ramp += je->nr_ramp;
746 dst->nr_pending += je->nr_pending;
747 dst->files_open += je->files_open;
748 dst->m_rate += je->m_rate;
749 dst->t_rate += je->t_rate;
750 dst->m_iops += je->m_iops;
751 dst->t_iops += je->t_iops;
752
753 for (i = 0; i < 2; i++) {
754 dst->rate[i] += je->rate[i];
755 dst->iops[i] += je->iops[i];
756 }
757
758 dst->elapsed_sec += je->elapsed_sec;
759
760 if (je->eta_sec > dst->eta_sec)
761 dst->eta_sec = je->eta_sec;
762}
763
Jens Axboe82c1ed32011-10-10 08:56:18 +0200764static void dec_jobs_eta(struct client_eta *eta)
765{
766 if (!--eta->pending) {
767 display_thread_status(&eta->eta);
768 free(eta);
769 }
770}
771
Jens Axboe89c17072011-10-11 10:15:51 +0200772static void remove_reply_cmd(struct fio_client *client, struct fio_net_cmd *cmd)
773{
774 struct fio_net_int_cmd *icmd = NULL;
775 struct flist_head *entry;
776
777 flist_for_each(entry, &client->cmd_list) {
778 icmd = flist_entry(entry, struct fio_net_int_cmd, list);
779
Jens Axboedf380932011-10-11 14:25:08 +0200780 if (cmd->tag == (uintptr_t) icmd)
Jens Axboe89c17072011-10-11 10:15:51 +0200781 break;
782
783 icmd = NULL;
784 }
785
786 if (!icmd) {
787 log_err("fio: client: unable to find matching tag\n");
788 return;
789 }
790
791 flist_del(&icmd->list);
792 cmd->tag = icmd->saved_tag;
793 free(icmd);
794}
795
Jens Axboe82c1ed32011-10-10 08:56:18 +0200796static void handle_eta(struct fio_client *client, struct fio_net_cmd *cmd)
Jens Axboe48fbb462011-10-09 12:19:08 +0200797{
798 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
Jens Axboedf380932011-10-11 14:25:08 +0200799 struct client_eta *eta = (struct client_eta *) (uintptr_t) cmd->tag;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200800
801 dprint(FD_NET, "client: got eta tag %p, %d\n", eta, eta->pending);
Jens Axboe48fbb462011-10-09 12:19:08 +0200802
Jens Axboef77d2672011-10-10 14:36:07 +0200803 assert(client->eta_in_flight == eta);
804
805 client->eta_in_flight = NULL;
Jens Axboe82c1ed32011-10-10 08:56:18 +0200806 flist_del_init(&client->eta_list);
807
Jens Axboe48fbb462011-10-09 12:19:08 +0200808 convert_jobs_eta(je);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200809 sum_jobs_eta(&eta->eta, je);
Jens Axboe82c1ed32011-10-10 08:56:18 +0200810 dec_jobs_eta(eta);
Jens Axboecf451d12011-10-03 16:48:30 +0200811}
812
Jens Axboeb5296dd2011-10-07 16:35:56 +0200813static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd)
Jens Axboe2e03b4b2011-10-04 09:00:40 +0200814{
815 struct cmd_probe_pdu *probe = (struct cmd_probe_pdu *) cmd->payload;
Jens Axboed2333352011-10-11 15:07:23 +0200816 const char *os, *arch;
817 char bit[16];
Jens Axboe2e03b4b2011-10-04 09:00:40 +0200818
Jens Axboecca84642011-10-07 12:47:57 +0200819 os = fio_get_os_string(probe->os);
820 if (!os)
821 os = "unknown";
822
823 arch = fio_get_arch_string(probe->arch);
824 if (!arch)
825 os = "unknown";
826
Jens Axboed2333352011-10-11 15:07:23 +0200827 sprintf(bit, "%d-bit", probe->bpp * 8);
Jens Axboe38fdef22011-10-11 14:30:06 +0200828
829 log_info("hostname=%s, be=%u, %s, os=%s, arch=%s, fio=%u.%u.%u\n",
830 probe->hostname, probe->bigendian, bit, os, arch,
831 probe->fio_major, probe->fio_minor, probe->fio_patch);
Jens Axboeb5296dd2011-10-07 16:35:56 +0200832
833 if (!client->name)
834 client->name = strdup((char *) probe->hostname);
Jens Axboe2e03b4b2011-10-04 09:00:40 +0200835}
836
Jens Axboe11e950b2011-10-16 21:34:14 +0200837static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd)
838{
839 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
840
841 client->state = Client_started;
842 client->jobs = le32_to_cpu(pdu->jobs);
843}
844
845static void handle_stop(struct fio_client *client, struct fio_net_cmd *cmd)
846{
847 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
848
849 client->state = Client_stopped;
850 client->error = le32_to_cpu(pdu->error);
Jens Axboe498c92c2011-10-17 09:14:42 +0200851
852 if (client->error)
853 log_info("client <%s>: exited with error %d\n", client->hostname, client->error);
Jens Axboe11e950b2011-10-16 21:34:14 +0200854}
855
Stephen M. Camerondd366722012-02-24 08:17:30 +0100856static int handle_client(struct fio_client *client, struct client_ops *ops)
Jens Axboe37db14f2011-09-30 17:00:42 -0600857{
858 struct fio_net_cmd *cmd;
859
Jens Axboe60efd142011-10-04 13:30:11 +0200860 dprint(FD_NET, "client: handle %s\n", client->hostname);
861
Jens Axboee951bdc2011-10-05 21:58:45 +0200862 cmd = fio_net_recv_cmd(client->fd);
863 if (!cmd)
864 return 0;
Jens Axboec2c94582011-10-05 20:31:30 +0200865
Jens Axboe89c17072011-10-11 10:15:51 +0200866 dprint(FD_NET, "client: got cmd op %s from %s\n",
867 fio_server_op(cmd->opcode), client->hostname);
Jens Axboe46c48f12011-10-01 12:50:51 -0600868
Jens Axboee951bdc2011-10-05 21:58:45 +0200869 switch (cmd->opcode) {
870 case FIO_NET_CMD_QUIT:
871 remove_client(client);
872 free(cmd);
873 break;
874 case FIO_NET_CMD_TEXT: {
875 const char *buf = (const char *) cmd->payload;
Stephen M. Camerondd366722012-02-24 08:17:30 +0100876 ops->text_op(client, f_out, cmd->pdu_len, buf);
Jens Axboee951bdc2011-10-05 21:58:45 +0200877 free(cmd);
878 break;
Jens Axboe37db14f2011-09-30 17:00:42 -0600879 }
Jens Axboed09a64a2011-10-13 11:38:56 +0200880 case FIO_NET_CMD_DU:
Stephen M. Camerondd366722012-02-24 08:17:30 +0100881 ops->disk_util(client, cmd);
Jens Axboed09a64a2011-10-13 11:38:56 +0200882 free(cmd);
883 break;
Jens Axboee951bdc2011-10-05 21:58:45 +0200884 case FIO_NET_CMD_TS:
Stephen M. Camerondd366722012-02-24 08:17:30 +0100885 ops->thread_status(cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +0200886 free(cmd);
887 break;
888 case FIO_NET_CMD_GS:
Stephen M. Camerondd366722012-02-24 08:17:30 +0100889 ops->group_stats(cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +0200890 free(cmd);
891 break;
892 case FIO_NET_CMD_ETA:
Jens Axboe89c17072011-10-11 10:15:51 +0200893 remove_reply_cmd(client, cmd);
Stephen M. Camerondd366722012-02-24 08:17:30 +0100894 ops->eta(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +0200895 free(cmd);
896 break;
897 case FIO_NET_CMD_PROBE:
Jens Axboe89c17072011-10-11 10:15:51 +0200898 remove_reply_cmd(client, cmd);
Stephen M. Camerondd366722012-02-24 08:17:30 +0100899 ops->probe(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +0200900 free(cmd);
901 break;
Jens Axboe01be0382011-10-15 14:43:41 +0200902 case FIO_NET_CMD_RUN:
903 client->state = Client_running;
904 free(cmd);
905 break;
Jens Axboee951bdc2011-10-05 21:58:45 +0200906 case FIO_NET_CMD_START:
Jens Axboe11e950b2011-10-16 21:34:14 +0200907 handle_start(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +0200908 free(cmd);
909 break;
910 case FIO_NET_CMD_STOP:
Jens Axboe11e950b2011-10-16 21:34:14 +0200911 handle_stop(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +0200912 free(cmd);
913 break;
914 default:
Jens Axboe89c17072011-10-11 10:15:51 +0200915 log_err("fio: unknown client op: %s\n", fio_server_op(cmd->opcode));
Jens Axboee951bdc2011-10-05 21:58:45 +0200916 free(cmd);
917 break;
Jens Axboe37db14f2011-09-30 17:00:42 -0600918 }
919
Jens Axboee951bdc2011-10-05 21:58:45 +0200920 return 1;
Jens Axboe37db14f2011-09-30 17:00:42 -0600921}
Jens Axboeb66570d2011-10-01 11:11:35 -0600922
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200923static void request_client_etas(void)
924{
925 struct fio_client *client;
926 struct flist_head *entry;
927 struct client_eta *eta;
Jens Axboe82c1ed32011-10-10 08:56:18 +0200928 int skipped = 0;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200929
930 dprint(FD_NET, "client: request eta (%d)\n", nr_clients);
931
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200932 eta = malloc(sizeof(*eta));
933 memset(&eta->eta, 0, sizeof(eta->eta));
934 eta->pending = nr_clients;
935
936 flist_for_each(entry, &client_list) {
937 client = flist_entry(entry, struct fio_client, list);
938
Jens Axboe82c1ed32011-10-10 08:56:18 +0200939 if (!flist_empty(&client->eta_list)) {
940 skipped++;
941 continue;
942 }
Jens Axboe01be0382011-10-15 14:43:41 +0200943 if (client->state != Client_running)
944 continue;
Jens Axboe82c1ed32011-10-10 08:56:18 +0200945
Jens Axboef77d2672011-10-10 14:36:07 +0200946 assert(!client->eta_in_flight);
Jens Axboe82c1ed32011-10-10 08:56:18 +0200947 flist_add_tail(&client->eta_list, &eta_list);
Jens Axboef77d2672011-10-10 14:36:07 +0200948 client->eta_in_flight = eta;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200949 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_SEND_ETA,
Jens Axboedf380932011-10-11 14:25:08 +0200950 (uintptr_t) eta, &client->cmd_list);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200951 }
952
Jens Axboe82c1ed32011-10-10 08:56:18 +0200953 while (skipped--)
954 dec_jobs_eta(eta);
955
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200956 dprint(FD_NET, "client: requested eta tag %p\n", eta);
957}
958
Jens Axboe89c17072011-10-11 10:15:51 +0200959static int client_check_cmd_timeout(struct fio_client *client,
960 struct timeval *now)
961{
962 struct fio_net_int_cmd *cmd;
963 struct flist_head *entry, *tmp;
964 int ret = 0;
965
966 flist_for_each_safe(entry, tmp, &client->cmd_list) {
967 cmd = flist_entry(entry, struct fio_net_int_cmd, list);
968
969 if (mtime_since(&cmd->tv, now) < FIO_NET_CLIENT_TIMEOUT)
970 continue;
971
972 log_err("fio: client %s, timeout on cmd %s\n", client->hostname,
973 fio_server_op(cmd->cmd.opcode));
974 flist_del(&cmd->list);
975 free(cmd);
976 ret = 1;
977 }
978
979 return flist_empty(&client->cmd_list) && ret;
980}
981
982static int fio_client_timed_out(void)
983{
984 struct fio_client *client;
985 struct flist_head *entry, *tmp;
986 struct timeval tv;
987 int ret = 0;
988
989 gettimeofday(&tv, NULL);
990
991 flist_for_each_safe(entry, tmp, &client_list) {
992 client = flist_entry(entry, struct fio_client, list);
993
994 if (flist_empty(&client->cmd_list))
995 continue;
996
997 if (!client_check_cmd_timeout(client, &tv))
998 continue;
999
1000 log_err("fio: client %s timed out\n", client->hostname);
1001 remove_client(client);
1002 ret = 1;
1003 }
1004
1005 return ret;
1006}
1007
Stephen M. Camerondd366722012-02-24 08:17:30 +01001008int fio_handle_clients(struct client_ops *ops)
Jens Axboeb66570d2011-10-01 11:11:35 -06001009{
Jens Axboeb66570d2011-10-01 11:11:35 -06001010 struct pollfd *pfds;
Jens Axboe498c92c2011-10-17 09:14:42 +02001011 int i, ret = 0, retval = 0;
Jens Axboeb66570d2011-10-01 11:11:35 -06001012
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001013 gettimeofday(&eta_tv, NULL);
1014
Jens Axboeb66570d2011-10-01 11:11:35 -06001015 pfds = malloc(nr_clients * sizeof(struct pollfd));
1016
Jens Axboe37f0c1a2011-10-11 14:08:33 +02001017 sum_stat_clients = nr_clients;
1018 init_thread_stat(&client_ts);
1019 init_group_run_stat(&client_gs);
1020
Stephen M. Cameronba936c42012-02-24 08:17:31 +01001021 /* Used by eta.c:display_thread_status() */
1022 update_thread_status = ops->thread_status_display;
1023
Jens Axboeb66570d2011-10-01 11:11:35 -06001024 while (!exit_backend && nr_clients) {
Jens Axboec2cb6862012-02-23 20:56:12 +01001025 struct flist_head *entry, *tmp;
1026 struct fio_client *client;
1027
Jens Axboe82a4be12011-10-01 12:40:32 -06001028 i = 0;
Jens Axboec2cb6862012-02-23 20:56:12 +01001029 flist_for_each_safe(entry, tmp, &client_list) {
Jens Axboe82a4be12011-10-01 12:40:32 -06001030 client = flist_entry(entry, struct fio_client, list);
1031
Jens Axboec2cb6862012-02-23 20:56:12 +01001032 if (!client->sent_job &&
1033 flist_empty(&client->cmd_list)) {
1034 remove_client(client);
1035 continue;
1036 }
1037
Jens Axboe82a4be12011-10-01 12:40:32 -06001038 pfds[i].fd = client->fd;
1039 pfds[i].events = POLLIN;
1040 i++;
1041 }
1042
Jens Axboec2cb6862012-02-23 20:56:12 +01001043 if (!nr_clients)
1044 break;
1045
Jens Axboe82a4be12011-10-01 12:40:32 -06001046 assert(i == nr_clients);
1047
Jens Axboe5c2857f2011-10-04 13:14:32 +02001048 do {
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001049 struct timeval tv;
1050
1051 gettimeofday(&tv, NULL);
1052 if (mtime_since(&eta_tv, &tv) >= 900) {
1053 request_client_etas();
1054 memcpy(&eta_tv, &tv, sizeof(tv));
Jens Axboe89c17072011-10-11 10:15:51 +02001055
1056 if (fio_client_timed_out())
1057 break;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001058 }
1059
Jens Axboe5c2857f2011-10-04 13:14:32 +02001060 ret = poll(pfds, nr_clients, 100);
1061 if (ret < 0) {
1062 if (errno == EINTR)
1063 continue;
1064 log_err("fio: poll clients: %s\n", strerror(errno));
1065 break;
1066 } else if (!ret)
Jens Axboeb66570d2011-10-01 11:11:35 -06001067 continue;
Jens Axboe5c2857f2011-10-04 13:14:32 +02001068 } while (ret <= 0);
Jens Axboeb66570d2011-10-01 11:11:35 -06001069
1070 for (i = 0; i < nr_clients; i++) {
1071 if (!(pfds[i].revents & POLLIN))
1072 continue;
1073
1074 client = find_client_by_fd(pfds[i].fd);
1075 if (!client) {
Jens Axboe3c5f57e2011-10-06 12:37:50 +02001076 log_err("fio: unknown client fd %d\n", pfds[i].fd);
Jens Axboeb66570d2011-10-01 11:11:35 -06001077 continue;
1078 }
Stephen M. Camerondd366722012-02-24 08:17:30 +01001079 if (!handle_client(client, ops)) {
Jens Axboe28d3ab02011-10-05 20:45:37 +02001080 log_info("client: host=%s disconnected\n",
1081 client->hostname);
1082 remove_client(client);
Jens Axboe498c92c2011-10-17 09:14:42 +02001083 retval = 1;
Jens Axboe38990762011-10-17 13:31:33 +02001084 } else if (client->error)
Jens Axboe498c92c2011-10-17 09:14:42 +02001085 retval = 1;
Jens Axboeb66570d2011-10-01 11:11:35 -06001086 }
1087 }
1088
1089 free(pfds);
Jens Axboe498c92c2011-10-17 09:14:42 +02001090 return retval;
Jens Axboeb66570d2011-10-01 11:11:35 -06001091}