blob: d80123648207b13ec13e1896783c895f2c7cda31 [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. Camerondd366722012-02-24 08:17:30 +010024static void fio_client_text_op(struct fio_client *client,
25 FILE *f, __u16 pdu_len, const char *buf)
26{
27 const char *name;
28 int fio_unused ret;
29
30 name = client->name ? client->name : client->hostname;
31
32 if (!client->skip_newline)
33 fprintf(f, "<%s> ", name);
34 ret = fwrite(buf, pdu_len, 1, f);
35 fflush(f);
36 client->skip_newline = strchr(buf, '\n') == NULL;
37}
38
39static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd);
40static void handle_ts(struct fio_net_cmd *cmd);
41static void handle_gs(struct fio_net_cmd *cmd);
42static void handle_eta(struct fio_client *client, struct fio_net_cmd *cmd);
43static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd);
44
45struct client_ops fio_client_ops = {
Jens Axboe0420ba62012-02-29 11:16:52 +010046 .text_op = fio_client_text_op,
47 .disk_util = handle_du,
48 .thread_status = handle_ts,
49 .group_stats = handle_gs,
50 .eta = handle_eta,
51 .probe = handle_probe,
Stephen M. Camerondd366722012-02-24 08:17:30 +010052};
53
Jens Axboeaf9c9fb2011-10-09 21:54:10 +020054static struct timeval eta_tv;
Jens Axboe48fbb462011-10-09 12:19:08 +020055
Jens Axboe81179ee2011-10-04 12:42:06 +020056enum {
Jens Axboe5c2857f2011-10-04 13:14:32 +020057 Client_created = 0,
Jens Axboe81179ee2011-10-04 12:42:06 +020058 Client_connected = 1,
59 Client_started = 2,
Jens Axboe01be0382011-10-15 14:43:41 +020060 Client_running = 3,
61 Client_stopped = 4,
62 Client_exited = 5,
Jens Axboeb66570d2011-10-01 11:11:35 -060063};
64
65static FLIST_HEAD(client_list);
Jens Axboe82c1ed32011-10-10 08:56:18 +020066static FLIST_HEAD(eta_list);
Jens Axboeb66570d2011-10-01 11:11:35 -060067
Jens Axboe3f3a4542011-10-10 21:11:09 +020068static FLIST_HEAD(arg_list);
69
Jens Axboe37f0c1a2011-10-11 14:08:33 +020070static struct thread_stat client_ts;
71static struct group_run_stats client_gs;
72static int sum_stat_clients;
73static int sum_stat_nr;
74
Jens Axboe3c5f57e2011-10-06 12:37:50 +020075#define FIO_CLIENT_HASH_BITS 7
76#define FIO_CLIENT_HASH_SZ (1 << FIO_CLIENT_HASH_BITS)
77#define FIO_CLIENT_HASH_MASK (FIO_CLIENT_HASH_SZ - 1)
Jens Axboebebe6392011-10-07 10:00:51 +020078static struct flist_head client_hash[FIO_CLIENT_HASH_SZ];
Jens Axboe3c5f57e2011-10-06 12:37:50 +020079
Jens Axboebebe6392011-10-07 10:00:51 +020080static void fio_client_add_hash(struct fio_client *client)
Jens Axboe3c5f57e2011-10-06 12:37:50 +020081{
82 int bucket = hash_long(client->fd, FIO_CLIENT_HASH_BITS);
83
84 bucket &= FIO_CLIENT_HASH_MASK;
Jens Axboebebe6392011-10-07 10:00:51 +020085 flist_add(&client->hash_list, &client_hash[bucket]);
Jens Axboe3c5f57e2011-10-06 12:37:50 +020086}
87
Jens Axboebebe6392011-10-07 10:00:51 +020088static void fio_client_remove_hash(struct fio_client *client)
Jens Axboe3c5f57e2011-10-06 12:37:50 +020089{
Jens Axboebebe6392011-10-07 10:00:51 +020090 if (!flist_empty(&client->hash_list))
91 flist_del_init(&client->hash_list);
Jens Axboe3c5f57e2011-10-06 12:37:50 +020092}
93
94static void fio_init fio_client_hash_init(void)
95{
96 int i;
97
Jens Axboebebe6392011-10-07 10:00:51 +020098 for (i = 0; i < FIO_CLIENT_HASH_SZ; i++)
99 INIT_FLIST_HEAD(&client_hash[i]);
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200100}
101
Jens Axboeb66570d2011-10-01 11:11:35 -0600102static struct fio_client *find_client_by_fd(int fd)
103{
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200104 int bucket = hash_long(fd, FIO_CLIENT_HASH_BITS) & FIO_CLIENT_HASH_MASK;
Jens Axboeb66570d2011-10-01 11:11:35 -0600105 struct fio_client *client;
106 struct flist_head *entry;
107
Jens Axboebebe6392011-10-07 10:00:51 +0200108 flist_for_each(entry, &client_hash[bucket]) {
109 client = flist_entry(entry, struct fio_client, hash_list);
Jens Axboeb66570d2011-10-01 11:11:35 -0600110
111 if (client->fd == fd)
112 return client;
113 }
114
115 return NULL;
116}
117
Jens Axboeb66570d2011-10-01 11:11:35 -0600118static void remove_client(struct fio_client *client)
119{
Jens Axboe39e8e012011-10-04 13:46:08 +0200120 dprint(FD_NET, "client: removed <%s>\n", client->hostname);
Jens Axboeb66570d2011-10-01 11:11:35 -0600121 flist_del(&client->list);
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200122
Jens Axboebebe6392011-10-07 10:00:51 +0200123 fio_client_remove_hash(client);
Jens Axboe81179ee2011-10-04 12:42:06 +0200124
Jens Axboe82c1ed32011-10-10 08:56:18 +0200125 if (!flist_empty(&client->eta_list)) {
126 flist_del_init(&client->eta_list);
Jens Axboe3e47bd22012-02-29 13:45:02 +0100127 fio_client_dec_jobs_eta(client->eta_in_flight, display_thread_status);
Jens Axboe82c1ed32011-10-10 08:56:18 +0200128 }
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200129
Jens Axboeb66570d2011-10-01 11:11:35 -0600130 free(client->hostname);
Jens Axboe81179ee2011-10-04 12:42:06 +0200131 if (client->argv)
132 free(client->argv);
Jens Axboeb5296dd2011-10-07 16:35:56 +0200133 if (client->name)
134 free(client->name);
Jens Axboe81179ee2011-10-04 12:42:06 +0200135
Jens Axboeb66570d2011-10-01 11:11:35 -0600136 free(client);
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200137 nr_clients--;
Jens Axboe5fd0acb2011-10-11 14:20:22 +0200138 sum_stat_clients--;
Jens Axboeb66570d2011-10-01 11:11:35 -0600139}
Jens Axboe132159a2011-09-30 15:01:32 -0600140
Jens Axboefa2ea802011-10-08 21:07:29 +0200141static void __fio_client_add_cmd_option(struct fio_client *client,
142 const char *opt)
Jens Axboe81179ee2011-10-04 12:42:06 +0200143{
Jens Axboe39e8e012011-10-04 13:46:08 +0200144 int index;
145
146 index = client->argc++;
Jens Axboe81179ee2011-10-04 12:42:06 +0200147 client->argv = realloc(client->argv, sizeof(char *) * client->argc);
Jens Axboe39e8e012011-10-04 13:46:08 +0200148 client->argv[index] = strdup(opt);
149 dprint(FD_NET, "client: add cmd %d: %s\n", index, opt);
Jens Axboe81179ee2011-10-04 12:42:06 +0200150}
151
Jens Axboefa2ea802011-10-08 21:07:29 +0200152void fio_client_add_cmd_option(void *cookie, const char *opt)
Jens Axboe81179ee2011-10-04 12:42:06 +0200153{
Jens Axboebebe6392011-10-07 10:00:51 +0200154 struct fio_client *client = cookie;
Jens Axboe3f3a4542011-10-10 21:11:09 +0200155 struct flist_head *entry;
Jens Axboe81179ee2011-10-04 12:42:06 +0200156
Jens Axboebebe6392011-10-07 10:00:51 +0200157 if (!client || !opt)
Jens Axboefa2ea802011-10-08 21:07:29 +0200158 return;
Jens Axboe81179ee2011-10-04 12:42:06 +0200159
Jens Axboefa2ea802011-10-08 21:07:29 +0200160 __fio_client_add_cmd_option(client, opt);
Jens Axboe3f3a4542011-10-10 21:11:09 +0200161
162 /*
163 * Duplicate arguments to shared client group
164 */
165 flist_for_each(entry, &arg_list) {
166 client = flist_entry(entry, struct fio_client, arg_list);
167
168 __fio_client_add_cmd_option(client, opt);
169 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200170}
171
Jens Axboe3ec62ec2012-03-01 12:01:29 +0100172struct fio_client *fio_client_add_explicit(const char *hostname, int type,
173 int port)
174{
175 struct fio_client *client;
176
177 client = malloc(sizeof(*client));
178 memset(client, 0, sizeof(*client));
179
180 INIT_FLIST_HEAD(&client->list);
181 INIT_FLIST_HEAD(&client->hash_list);
182 INIT_FLIST_HEAD(&client->arg_list);
183 INIT_FLIST_HEAD(&client->eta_list);
184 INIT_FLIST_HEAD(&client->cmd_list);
185
186 client->hostname = strdup(hostname);
187
188 if (type == Fio_client_socket)
189 client->is_sock = 1;
190 else {
191 int ipv6;
192
193 ipv6 = type == Fio_client_ipv6;
194 if (fio_server_parse_host(hostname, &ipv6,
195 &client->addr.sin_addr,
196 &client->addr6.sin6_addr))
197 goto err;
198
199 client->port = port;
200 }
201
202 client->fd = -1;
203
204 __fio_client_add_cmd_option(client, "fio");
205
206 flist_add(&client->list, &client_list);
207 nr_clients++;
208 dprint(FD_NET, "client: added <%s>\n", client->hostname);
209 return client;
210err:
211 free(client);
212 return NULL;
213}
214
Jens Axboebebe6392011-10-07 10:00:51 +0200215int fio_client_add(const char *hostname, void **cookie)
Jens Axboe132159a2011-09-30 15:01:32 -0600216{
Jens Axboe3f3a4542011-10-10 21:11:09 +0200217 struct fio_client *existing = *cookie;
Jens Axboeb66570d2011-10-01 11:11:35 -0600218 struct fio_client *client;
Jens Axboe132159a2011-09-30 15:01:32 -0600219
Jens Axboe3f3a4542011-10-10 21:11:09 +0200220 if (existing) {
221 /*
222 * We always add our "exec" name as the option, hence 1
223 * means empty.
224 */
225 if (existing->argc == 1)
226 flist_add_tail(&existing->arg_list, &arg_list);
227 else {
228 while (!flist_empty(&arg_list))
229 flist_del_init(arg_list.next);
230 }
231 }
232
Jens Axboeb66570d2011-10-01 11:11:35 -0600233 client = malloc(sizeof(*client));
Jens Axboea37f69b2011-10-01 12:24:21 -0600234 memset(client, 0, sizeof(*client));
Jens Axboe81179ee2011-10-04 12:42:06 +0200235
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200236 INIT_FLIST_HEAD(&client->list);
Jens Axboebebe6392011-10-07 10:00:51 +0200237 INIT_FLIST_HEAD(&client->hash_list);
Jens Axboe3f3a4542011-10-10 21:11:09 +0200238 INIT_FLIST_HEAD(&client->arg_list);
Jens Axboe82c1ed32011-10-10 08:56:18 +0200239 INIT_FLIST_HEAD(&client->eta_list);
Jens Axboe89c17072011-10-11 10:15:51 +0200240 INIT_FLIST_HEAD(&client->cmd_list);
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200241
Jens Axboebebe6392011-10-07 10:00:51 +0200242 if (fio_server_parse_string(hostname, &client->hostname,
243 &client->is_sock, &client->port,
Jens Axboe811826b2011-10-24 09:11:50 +0200244 &client->addr.sin_addr,
245 &client->addr6.sin6_addr,
246 &client->ipv6))
Jens Axboebebe6392011-10-07 10:00:51 +0200247 return -1;
248
Jens Axboea37f69b2011-10-01 12:24:21 -0600249 client->fd = -1;
Jens Axboe81179ee2011-10-04 12:42:06 +0200250
251 __fio_client_add_cmd_option(client, "fio");
252
Jens Axboea37f69b2011-10-01 12:24:21 -0600253 flist_add(&client->list, &client_list);
254 nr_clients++;
Jens Axboebebe6392011-10-07 10:00:51 +0200255 dprint(FD_NET, "client: added <%s>\n", client->hostname);
256 *cookie = client;
257 return 0;
Jens Axboea37f69b2011-10-01 12:24:21 -0600258}
259
Jens Axboe87aa8f12011-10-06 21:24:13 +0200260static int fio_client_connect_ip(struct fio_client *client)
Jens Axboea37f69b2011-10-01 12:24:21 -0600261{
Jens Axboe811826b2011-10-24 09:11:50 +0200262 struct sockaddr *addr;
263 fio_socklen_t socklen;
264 int fd, domain;
Jens Axboe132159a2011-09-30 15:01:32 -0600265
Jens Axboe811826b2011-10-24 09:11:50 +0200266 if (client->ipv6) {
267 client->addr6.sin6_family = AF_INET6;
268 client->addr6.sin6_port = htons(client->port);
269 domain = AF_INET6;
270 addr = (struct sockaddr *) &client->addr6;
271 socklen = sizeof(client->addr6);
272 } else {
273 client->addr.sin_family = AF_INET;
274 client->addr.sin_port = htons(client->port);
275 domain = AF_INET;
276 addr = (struct sockaddr *) &client->addr;
277 socklen = sizeof(client->addr);
278 }
Jens Axboe132159a2011-09-30 15:01:32 -0600279
Jens Axboe811826b2011-10-24 09:11:50 +0200280 fd = socket(domain, SOCK_STREAM, 0);
Jens Axboe132159a2011-09-30 15:01:32 -0600281 if (fd < 0) {
282 log_err("fio: socket: %s\n", strerror(errno));
Jens Axboe87aa8f12011-10-06 21:24:13 +0200283 return -1;
Jens Axboe132159a2011-09-30 15:01:32 -0600284 }
285
Jens Axboe811826b2011-10-24 09:11:50 +0200286 if (connect(fd, addr, socklen) < 0) {
Jens Axboe132159a2011-09-30 15:01:32 -0600287 log_err("fio: connect: %s\n", strerror(errno));
Jens Axboea7de0a12011-10-07 12:55:14 +0200288 log_err("fio: failed to connect to %s:%u\n", client->hostname,
289 client->port);
Jens Axboeb94cba42011-10-06 21:27:10 +0200290 close(fd);
Jens Axboe87aa8f12011-10-06 21:24:13 +0200291 return -1;
Jens Axboe132159a2011-09-30 15:01:32 -0600292 }
293
Jens Axboe87aa8f12011-10-06 21:24:13 +0200294 return fd;
295}
296
297static int fio_client_connect_sock(struct fio_client *client)
298{
299 struct sockaddr_un *addr = &client->addr_un;
300 fio_socklen_t len;
301 int fd;
302
303 memset(addr, 0, sizeof(*addr));
304 addr->sun_family = AF_UNIX;
305 strcpy(addr->sun_path, client->hostname);
306
307 fd = socket(AF_UNIX, SOCK_STREAM, 0);
308 if (fd < 0) {
309 log_err("fio: socket: %s\n", strerror(errno));
310 return -1;
311 }
312
313 len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
314 if (connect(fd, (struct sockaddr *) addr, len) < 0) {
315 log_err("fio: connect; %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200316 close(fd);
Jens Axboe87aa8f12011-10-06 21:24:13 +0200317 return -1;
318 }
319
320 return fd;
321}
322
323static int fio_client_connect(struct fio_client *client)
324{
325 int fd;
326
327 dprint(FD_NET, "client: connect to host %s\n", client->hostname);
328
Jens Axboe87aa8f12011-10-06 21:24:13 +0200329 if (client->is_sock)
330 fd = fio_client_connect_sock(client);
331 else
332 fd = fio_client_connect_ip(client);
333
Jens Axboe89c17072011-10-11 10:15:51 +0200334 dprint(FD_NET, "client: %s connected %d\n", client->hostname, fd);
335
Jens Axboe87aa8f12011-10-06 21:24:13 +0200336 if (fd < 0)
337 return 1;
338
Jens Axboeb66570d2011-10-01 11:11:35 -0600339 client->fd = fd;
Jens Axboebebe6392011-10-07 10:00:51 +0200340 fio_client_add_hash(client);
Jens Axboe81179ee2011-10-04 12:42:06 +0200341 client->state = Client_connected;
Jens Axboe132159a2011-09-30 15:01:32 -0600342 return 0;
343}
344
Jens Axboecc0df002011-10-03 20:53:32 +0200345void fio_clients_terminate(void)
346{
347 struct flist_head *entry;
348 struct fio_client *client;
349
Jens Axboe60efd142011-10-04 13:30:11 +0200350 dprint(FD_NET, "client: terminate clients\n");
351
Jens Axboecc0df002011-10-03 20:53:32 +0200352 flist_for_each(entry, &client_list) {
353 client = flist_entry(entry, struct fio_client, list);
354
Jens Axboe89c17072011-10-11 10:15:51 +0200355 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_QUIT, 0, NULL);
Jens Axboecc0df002011-10-03 20:53:32 +0200356 }
357}
358
359static void sig_int(int sig)
360{
Jens Axboebebe6392011-10-07 10:00:51 +0200361 dprint(FD_NET, "client: got signal %d\n", sig);
Jens Axboecc0df002011-10-03 20:53:32 +0200362 fio_clients_terminate();
363}
364
365static void client_signal_handler(void)
366{
367 struct sigaction act;
368
369 memset(&act, 0, sizeof(act));
370 act.sa_handler = sig_int;
371 act.sa_flags = SA_RESTART;
372 sigaction(SIGINT, &act, NULL);
373
374 memset(&act, 0, sizeof(act));
375 act.sa_handler = sig_int;
376 act.sa_flags = SA_RESTART;
377 sigaction(SIGTERM, &act, NULL);
378}
379
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200380static void probe_client(struct fio_client *client)
381{
Jens Axboe60efd142011-10-04 13:30:11 +0200382 dprint(FD_NET, "client: send probe\n");
383
Jens Axboe89c17072011-10-11 10:15:51 +0200384 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_PROBE, 0, &client->cmd_list);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200385}
386
Jens Axboe81179ee2011-10-04 12:42:06 +0200387static int send_client_cmd_line(struct fio_client *client)
388{
Jens Axboefa2ea802011-10-08 21:07:29 +0200389 struct cmd_single_line_pdu *cslp;
390 struct cmd_line_pdu *clp;
391 unsigned long offset;
Jens Axboe7f868312011-10-10 09:55:21 +0200392 unsigned int *lens;
Jens Axboefa2ea802011-10-08 21:07:29 +0200393 void *pdu;
394 size_t mem;
Jens Axboe81179ee2011-10-04 12:42:06 +0200395 int i, ret;
396
Jens Axboe39e8e012011-10-04 13:46:08 +0200397 dprint(FD_NET, "client: send cmdline %d\n", client->argc);
Jens Axboe60efd142011-10-04 13:30:11 +0200398
Jens Axboe7f868312011-10-10 09:55:21 +0200399 lens = malloc(client->argc * sizeof(unsigned int));
400
Jens Axboefa2ea802011-10-08 21:07:29 +0200401 /*
402 * Find out how much mem we need
403 */
Jens Axboe7f868312011-10-10 09:55:21 +0200404 for (i = 0, mem = 0; i < client->argc; i++) {
405 lens[i] = strlen(client->argv[i]) + 1;
406 mem += lens[i];
407 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200408
Jens Axboefa2ea802011-10-08 21:07:29 +0200409 /*
410 * We need one cmd_line_pdu, and argc number of cmd_single_line_pdu
411 */
412 mem += sizeof(*clp) + (client->argc * sizeof(*cslp));
413
414 pdu = malloc(mem);
415 clp = pdu;
416 offset = sizeof(*clp);
417
418 for (i = 0; i < client->argc; i++) {
Jens Axboe7f868312011-10-10 09:55:21 +0200419 uint16_t arg_len = lens[i];
Jens Axboefa2ea802011-10-08 21:07:29 +0200420
421 cslp = pdu + offset;
422 strcpy((char *) cslp->text, client->argv[i]);
423 cslp->len = cpu_to_le16(arg_len);
424 offset += sizeof(*cslp) + arg_len;
425 }
426
Jens Axboe7f868312011-10-10 09:55:21 +0200427 free(lens);
Jens Axboefa2ea802011-10-08 21:07:29 +0200428 clp->lines = cpu_to_le16(client->argc);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200429 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOBLINE, pdu, mem, 0);
Jens Axboe81179ee2011-10-04 12:42:06 +0200430 free(pdu);
431 return ret;
432}
433
Jens Axboea37f69b2011-10-01 12:24:21 -0600434int fio_clients_connect(void)
435{
436 struct fio_client *client;
437 struct flist_head *entry, *tmp;
438 int ret;
439
Bruce Cran93bcfd22012-02-20 20:18:19 +0100440#ifdef WIN32
441 WSADATA wsd;
442 WSAStartup(MAKEWORD(2,2), &wsd);
443#endif
444
Jens Axboe60efd142011-10-04 13:30:11 +0200445 dprint(FD_NET, "client: connect all\n");
446
Jens Axboecc0df002011-10-03 20:53:32 +0200447 client_signal_handler();
448
Jens Axboea37f69b2011-10-01 12:24:21 -0600449 flist_for_each_safe(entry, tmp, &client_list) {
450 client = flist_entry(entry, struct fio_client, list);
451
452 ret = fio_client_connect(client);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200453 if (ret) {
Jens Axboea37f69b2011-10-01 12:24:21 -0600454 remove_client(client);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200455 continue;
456 }
457
458 probe_client(client);
Jens Axboe81179ee2011-10-04 12:42:06 +0200459
460 if (client->argc > 1)
461 send_client_cmd_line(client);
Jens Axboea37f69b2011-10-01 12:24:21 -0600462 }
463
464 return !nr_clients;
465}
466
Jens Axboe132159a2011-09-30 15:01:32 -0600467/*
468 * Send file contents to server backend. We could use sendfile(), but to remain
469 * more portable lets just read/write the darn thing.
470 */
Jens Axboea37f69b2011-10-01 12:24:21 -0600471static int fio_client_send_ini(struct fio_client *client, const char *filename)
Jens Axboe132159a2011-09-30 15:01:32 -0600472{
473 struct stat sb;
474 char *p, *buf;
475 off_t len;
476 int fd, ret;
477
Jens Axboe46c48f12011-10-01 12:50:51 -0600478 dprint(FD_NET, "send ini %s to %s\n", filename, client->hostname);
479
Jens Axboe132159a2011-09-30 15:01:32 -0600480 fd = open(filename, O_RDONLY);
481 if (fd < 0) {
Jens Axboee951bdc2011-10-05 21:58:45 +0200482 log_err("fio: job file <%s> open: %s\n", filename, strerror(errno));
Jens Axboe132159a2011-09-30 15:01:32 -0600483 return 1;
484 }
485
486 if (fstat(fd, &sb) < 0) {
487 log_err("fio: job file stat: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200488 close(fd);
Jens Axboe132159a2011-09-30 15:01:32 -0600489 return 1;
490 }
491
492 buf = malloc(sb.st_size);
493
494 len = sb.st_size;
495 p = buf;
496 do {
497 ret = read(fd, p, len);
498 if (ret > 0) {
499 len -= ret;
500 if (!len)
501 break;
502 p += ret;
503 continue;
504 } else if (!ret)
505 break;
506 else if (errno == EAGAIN || errno == EINTR)
507 continue;
508 } while (1);
509
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200510 if (len) {
511 log_err("fio: failed reading job file %s\n", filename);
Jens Axboeb94cba42011-10-06 21:27:10 +0200512 close(fd);
Jens Axboec524ef72011-10-07 12:23:34 +0200513 free(buf);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200514 return 1;
515 }
516
Jens Axboec2cb6862012-02-23 20:56:12 +0100517 client->sent_job = 1;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200518 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, buf, sb.st_size, 0);
Jens Axboe132159a2011-09-30 15:01:32 -0600519 free(buf);
Jens Axboeb94cba42011-10-06 21:27:10 +0200520 close(fd);
Jens Axboe132159a2011-09-30 15:01:32 -0600521 return ret;
522}
Jens Axboe37db14f2011-09-30 17:00:42 -0600523
Jens Axboea37f69b2011-10-01 12:24:21 -0600524int fio_clients_send_ini(const char *filename)
525{
526 struct fio_client *client;
527 struct flist_head *entry, *tmp;
528
529 flist_for_each_safe(entry, tmp, &client_list) {
530 client = flist_entry(entry, struct fio_client, list);
531
532 if (fio_client_send_ini(client, filename))
533 remove_client(client);
Jens Axboec2cb6862012-02-23 20:56:12 +0100534
535 client->sent_job = 1;
Jens Axboea37f69b2011-10-01 12:24:21 -0600536 }
537
538 return !nr_clients;
539}
540
Jens Axboea64e88d2011-10-03 14:20:01 +0200541static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
542{
543 dst->max_val = le64_to_cpu(src->max_val);
544 dst->min_val = le64_to_cpu(src->min_val);
545 dst->samples = le64_to_cpu(src->samples);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200546
547 /*
548 * Floats arrive as IEEE 754 encoded uint64_t, convert back to double
549 */
550 dst->mean.u.f = fio_uint64_to_double(le64_to_cpu(dst->mean.u.i));
551 dst->S.u.f = fio_uint64_to_double(le64_to_cpu(dst->S.u.i));
Jens Axboea64e88d2011-10-03 14:20:01 +0200552}
553
554static void convert_ts(struct thread_stat *dst, struct thread_stat *src)
555{
556 int i, j;
557
558 dst->error = le32_to_cpu(src->error);
559 dst->groupid = le32_to_cpu(src->groupid);
560 dst->pid = le32_to_cpu(src->pid);
561 dst->members = le32_to_cpu(src->members);
562
563 for (i = 0; i < 2; i++) {
564 convert_io_stat(&dst->clat_stat[i], &src->clat_stat[i]);
565 convert_io_stat(&dst->slat_stat[i], &src->slat_stat[i]);
566 convert_io_stat(&dst->lat_stat[i], &src->lat_stat[i]);
567 convert_io_stat(&dst->bw_stat[i], &src->bw_stat[i]);
568 }
569
570 dst->usr_time = le64_to_cpu(src->usr_time);
571 dst->sys_time = le64_to_cpu(src->sys_time);
572 dst->ctx = le64_to_cpu(src->ctx);
573 dst->minf = le64_to_cpu(src->minf);
574 dst->majf = le64_to_cpu(src->majf);
575 dst->clat_percentiles = le64_to_cpu(src->clat_percentiles);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200576
577 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
578 fio_fp64_t *fps = &src->percentile_list[i];
579 fio_fp64_t *fpd = &dst->percentile_list[i];
580
581 fpd->u.f = fio_uint64_to_double(le64_to_cpu(fps->u.i));
582 }
Jens Axboea64e88d2011-10-03 14:20:01 +0200583
584 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
585 dst->io_u_map[i] = le32_to_cpu(src->io_u_map[i]);
586 dst->io_u_submit[i] = le32_to_cpu(src->io_u_submit[i]);
587 dst->io_u_complete[i] = le32_to_cpu(src->io_u_complete[i]);
588 }
589
590 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
591 dst->io_u_lat_u[i] = le32_to_cpu(src->io_u_lat_u[i]);
592 dst->io_u_lat_m[i] = le32_to_cpu(src->io_u_lat_m[i]);
593 }
594
595 for (i = 0; i < 2; i++)
596 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
597 dst->io_u_plat[i][j] = le32_to_cpu(src->io_u_plat[i][j]);
598
599 for (i = 0; i < 3; i++) {
600 dst->total_io_u[i] = le64_to_cpu(src->total_io_u[i]);
Jens Axboe93eee042011-10-03 21:08:48 +0200601 dst->short_io_u[i] = le64_to_cpu(src->short_io_u[i]);
Jens Axboea64e88d2011-10-03 14:20:01 +0200602 }
603
604 dst->total_submit = le64_to_cpu(src->total_submit);
605 dst->total_complete = le64_to_cpu(src->total_complete);
606
607 for (i = 0; i < 2; i++) {
608 dst->io_bytes[i] = le64_to_cpu(src->io_bytes[i]);
609 dst->runtime[i] = le64_to_cpu(src->runtime[i]);
610 }
611
612 dst->total_run_time = le64_to_cpu(src->total_run_time);
613 dst->continue_on_error = le16_to_cpu(src->continue_on_error);
614 dst->total_err_count = le64_to_cpu(src->total_err_count);
Jens Axboeddcc0b62011-10-03 14:45:27 +0200615 dst->first_error = le32_to_cpu(src->first_error);
616 dst->kb_base = le32_to_cpu(src->kb_base);
Jens Axboea64e88d2011-10-03 14:20:01 +0200617}
618
619static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
620{
621 int i;
622
623 for (i = 0; i < 2; i++) {
624 dst->max_run[i] = le64_to_cpu(src->max_run[i]);
625 dst->min_run[i] = le64_to_cpu(src->min_run[i]);
626 dst->max_bw[i] = le64_to_cpu(src->max_bw[i]);
627 dst->min_bw[i] = le64_to_cpu(src->min_bw[i]);
628 dst->io_kb[i] = le64_to_cpu(src->io_kb[i]);
629 dst->agg[i] = le64_to_cpu(src->agg[i]);
630 }
631
632 dst->kb_base = le32_to_cpu(src->kb_base);
633 dst->groupid = le32_to_cpu(src->groupid);
634}
635
636static void handle_ts(struct fio_net_cmd *cmd)
637{
638 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
639
640 convert_ts(&p->ts, &p->ts);
641 convert_gs(&p->rs, &p->rs);
642
643 show_thread_status(&p->ts, &p->rs);
Jens Axboe37f0c1a2011-10-11 14:08:33 +0200644
645 if (sum_stat_clients == 1)
646 return;
647
648 sum_thread_stats(&client_ts, &p->ts, sum_stat_nr);
649 sum_group_stats(&client_gs, &p->rs);
650
651 client_ts.members++;
652 client_ts.groupid = p->ts.groupid;
653
654 if (++sum_stat_nr == sum_stat_clients) {
655 strcpy(client_ts.name, "All clients");
656 show_thread_status(&client_ts, &client_gs);
657 }
Jens Axboea64e88d2011-10-03 14:20:01 +0200658}
659
660static void handle_gs(struct fio_net_cmd *cmd)
661{
662 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
663
664 convert_gs(gs, gs);
665 show_group_stats(gs);
666}
667
Jens Axboed09a64a2011-10-13 11:38:56 +0200668static void convert_agg(struct disk_util_agg *agg)
669{
670 int i;
671
672 for (i = 0; i < 2; i++) {
673 agg->ios[i] = le32_to_cpu(agg->ios[i]);
674 agg->merges[i] = le32_to_cpu(agg->merges[i]);
675 agg->sectors[i] = le64_to_cpu(agg->sectors[i]);
676 agg->ticks[i] = le32_to_cpu(agg->ticks[i]);
677 }
678
679 agg->io_ticks = le32_to_cpu(agg->io_ticks);
680 agg->time_in_queue = le32_to_cpu(agg->time_in_queue);
681 agg->slavecount = le32_to_cpu(agg->slavecount);
Anton Blanchard823ba542011-11-07 14:16:26 +0100682 agg->max_util.u.f = fio_uint64_to_double(__le64_to_cpu(agg->max_util.u.i));
Jens Axboed09a64a2011-10-13 11:38:56 +0200683}
684
685static void convert_dus(struct disk_util_stat *dus)
686{
687 int i;
688
689 for (i = 0; i < 2; i++) {
690 dus->ios[i] = le32_to_cpu(dus->ios[i]);
691 dus->merges[i] = le32_to_cpu(dus->merges[i]);
692 dus->sectors[i] = le64_to_cpu(dus->sectors[i]);
693 dus->ticks[i] = le32_to_cpu(dus->ticks[i]);
694 }
695
696 dus->io_ticks = le32_to_cpu(dus->io_ticks);
697 dus->time_in_queue = le32_to_cpu(dus->time_in_queue);
698 dus->msec = le64_to_cpu(dus->msec);
699}
700
701static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd)
702{
703 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
704
705 convert_dus(&du->dus);
706 convert_agg(&du->agg);
707
708 if (!client->disk_stats_shown) {
709 client->disk_stats_shown = 1;
710 log_info("\nDisk stats (read/write):\n");
711 }
712
Jens Axboef2f788d2011-10-13 14:03:52 +0200713 print_disk_util(&du->dus, &du->agg, terse_output);
Jens Axboed09a64a2011-10-13 11:38:56 +0200714}
715
Jens Axboe3e47bd22012-02-29 13:45:02 +0100716void fio_client_convert_jobs_eta(struct jobs_eta *je)
Jens Axboecf451d12011-10-03 16:48:30 +0200717{
Jens Axboecf451d12011-10-03 16:48:30 +0200718 int i;
719
720 je->nr_running = le32_to_cpu(je->nr_running);
721 je->nr_ramp = le32_to_cpu(je->nr_ramp);
722 je->nr_pending = le32_to_cpu(je->nr_pending);
723 je->files_open = le32_to_cpu(je->files_open);
Jens Axboecf451d12011-10-03 16:48:30 +0200724
725 for (i = 0; i < 2; i++) {
Jens Axboe3e47bd22012-02-29 13:45:02 +0100726 je->m_rate[i] = le32_to_cpu(je->m_rate[i]);
727 je->t_rate[i] = le32_to_cpu(je->t_rate[i]);
728 je->m_iops[i] = le32_to_cpu(je->m_iops[i]);
729 je->t_iops[i] = le32_to_cpu(je->t_iops[i]);
Jens Axboecf451d12011-10-03 16:48:30 +0200730 je->rate[i] = le32_to_cpu(je->rate[i]);
731 je->iops[i] = le32_to_cpu(je->iops[i]);
732 }
733
Jens Axboeb51eedb2011-10-09 12:13:39 +0200734 je->elapsed_sec = le64_to_cpu(je->elapsed_sec);
Jens Axboecf451d12011-10-03 16:48:30 +0200735 je->eta_sec = le64_to_cpu(je->eta_sec);
Jens Axboe48fbb462011-10-09 12:19:08 +0200736}
Jens Axboecf451d12011-10-03 16:48:30 +0200737
Jens Axboe3e47bd22012-02-29 13:45:02 +0100738void fio_client_sum_jobs_eta(struct jobs_eta *dst, struct jobs_eta *je)
Jens Axboe48fbb462011-10-09 12:19:08 +0200739{
Jens Axboe48fbb462011-10-09 12:19:08 +0200740 int i;
741
742 dst->nr_running += je->nr_running;
743 dst->nr_ramp += je->nr_ramp;
744 dst->nr_pending += je->nr_pending;
745 dst->files_open += je->files_open;
Jens Axboe48fbb462011-10-09 12:19:08 +0200746
747 for (i = 0; i < 2; i++) {
Jens Axboe3e47bd22012-02-29 13:45:02 +0100748 dst->m_rate[i] += je->m_rate[i];
749 dst->t_rate[i] += je->t_rate[i];
750 dst->m_iops[i] += je->m_iops[i];
751 dst->t_iops[i] += je->t_iops[i];
Jens Axboe48fbb462011-10-09 12:19:08 +0200752 dst->rate[i] += je->rate[i];
753 dst->iops[i] += je->iops[i];
754 }
755
756 dst->elapsed_sec += je->elapsed_sec;
757
758 if (je->eta_sec > dst->eta_sec)
759 dst->eta_sec = je->eta_sec;
760}
761
Jens Axboe3e47bd22012-02-29 13:45:02 +0100762void fio_client_dec_jobs_eta(struct client_eta *eta, void (*fn)(struct jobs_eta *))
Jens Axboe82c1ed32011-10-10 08:56:18 +0200763{
764 if (!--eta->pending) {
Jens Axboe3e47bd22012-02-29 13:45:02 +0100765 fn(&eta->eta);
Jens Axboe82c1ed32011-10-10 08:56:18 +0200766 free(eta);
767 }
768}
769
Jens Axboe89c17072011-10-11 10:15:51 +0200770static void remove_reply_cmd(struct fio_client *client, struct fio_net_cmd *cmd)
771{
772 struct fio_net_int_cmd *icmd = NULL;
773 struct flist_head *entry;
774
775 flist_for_each(entry, &client->cmd_list) {
776 icmd = flist_entry(entry, struct fio_net_int_cmd, list);
777
Jens Axboedf380932011-10-11 14:25:08 +0200778 if (cmd->tag == (uintptr_t) icmd)
Jens Axboe89c17072011-10-11 10:15:51 +0200779 break;
780
781 icmd = NULL;
782 }
783
784 if (!icmd) {
785 log_err("fio: client: unable to find matching tag\n");
786 return;
787 }
788
789 flist_del(&icmd->list);
790 cmd->tag = icmd->saved_tag;
791 free(icmd);
792}
793
Jens Axboe82c1ed32011-10-10 08:56:18 +0200794static void handle_eta(struct fio_client *client, struct fio_net_cmd *cmd)
Jens Axboe48fbb462011-10-09 12:19:08 +0200795{
796 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
Jens Axboedf380932011-10-11 14:25:08 +0200797 struct client_eta *eta = (struct client_eta *) (uintptr_t) cmd->tag;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200798
799 dprint(FD_NET, "client: got eta tag %p, %d\n", eta, eta->pending);
Jens Axboe48fbb462011-10-09 12:19:08 +0200800
Jens Axboef77d2672011-10-10 14:36:07 +0200801 assert(client->eta_in_flight == eta);
802
803 client->eta_in_flight = NULL;
Jens Axboe82c1ed32011-10-10 08:56:18 +0200804 flist_del_init(&client->eta_list);
805
Jens Axboe3e47bd22012-02-29 13:45:02 +0100806 fio_client_convert_jobs_eta(je);
807 fio_client_sum_jobs_eta(&eta->eta, je);
808 fio_client_dec_jobs_eta(eta, display_thread_status);
Jens Axboecf451d12011-10-03 16:48:30 +0200809}
810
Jens Axboeb5296dd2011-10-07 16:35:56 +0200811static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd)
Jens Axboe2e03b4b2011-10-04 09:00:40 +0200812{
813 struct cmd_probe_pdu *probe = (struct cmd_probe_pdu *) cmd->payload;
Jens Axboed2333352011-10-11 15:07:23 +0200814 const char *os, *arch;
815 char bit[16];
Jens Axboe2e03b4b2011-10-04 09:00:40 +0200816
Jens Axboecca84642011-10-07 12:47:57 +0200817 os = fio_get_os_string(probe->os);
818 if (!os)
819 os = "unknown";
820
821 arch = fio_get_arch_string(probe->arch);
822 if (!arch)
823 os = "unknown";
824
Jens Axboed2333352011-10-11 15:07:23 +0200825 sprintf(bit, "%d-bit", probe->bpp * 8);
Jens Axboe38fdef22011-10-11 14:30:06 +0200826
827 log_info("hostname=%s, be=%u, %s, os=%s, arch=%s, fio=%u.%u.%u\n",
828 probe->hostname, probe->bigendian, bit, os, arch,
829 probe->fio_major, probe->fio_minor, probe->fio_patch);
Jens Axboeb5296dd2011-10-07 16:35:56 +0200830
831 if (!client->name)
832 client->name = strdup((char *) probe->hostname);
Jens Axboe2e03b4b2011-10-04 09:00:40 +0200833}
834
Jens Axboe11e950b2011-10-16 21:34:14 +0200835static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd)
836{
837 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
838
839 client->state = Client_started;
840 client->jobs = le32_to_cpu(pdu->jobs);
841}
842
843static void handle_stop(struct fio_client *client, struct fio_net_cmd *cmd)
844{
845 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
846
847 client->state = Client_stopped;
848 client->error = le32_to_cpu(pdu->error);
Jens Axboe498c92c2011-10-17 09:14:42 +0200849
850 if (client->error)
851 log_info("client <%s>: exited with error %d\n", client->hostname, client->error);
Jens Axboe11e950b2011-10-16 21:34:14 +0200852}
853
Jens Axboe3e47bd22012-02-29 13:45:02 +0100854int fio_handle_client(struct fio_client *client, struct client_ops *ops)
Jens Axboe37db14f2011-09-30 17:00:42 -0600855{
856 struct fio_net_cmd *cmd;
857
Jens Axboe60efd142011-10-04 13:30:11 +0200858 dprint(FD_NET, "client: handle %s\n", client->hostname);
859
Jens Axboee951bdc2011-10-05 21:58:45 +0200860 cmd = fio_net_recv_cmd(client->fd);
861 if (!cmd)
862 return 0;
Jens Axboec2c94582011-10-05 20:31:30 +0200863
Jens Axboe89c17072011-10-11 10:15:51 +0200864 dprint(FD_NET, "client: got cmd op %s from %s\n",
865 fio_server_op(cmd->opcode), client->hostname);
Jens Axboe46c48f12011-10-01 12:50:51 -0600866
Jens Axboee951bdc2011-10-05 21:58:45 +0200867 switch (cmd->opcode) {
868 case FIO_NET_CMD_QUIT:
Jens Axboe3ec62ec2012-03-01 12:01:29 +0100869 if (ops->quit)
870 ops->quit(client);
Jens Axboee951bdc2011-10-05 21:58:45 +0200871 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--)
Jens Axboe3e47bd22012-02-29 13:45:02 +0100954 fio_client_dec_jobs_eta(eta, display_thread_status);
Jens Axboe82c1ed32011-10-10 08:56:18 +0200955
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
Jens Axboeb66570d2011-10-01 11:11:35 -06001021 while (!exit_backend && nr_clients) {
Jens Axboec2cb6862012-02-23 20:56:12 +01001022 struct flist_head *entry, *tmp;
1023 struct fio_client *client;
1024
Jens Axboe82a4be12011-10-01 12:40:32 -06001025 i = 0;
Jens Axboec2cb6862012-02-23 20:56:12 +01001026 flist_for_each_safe(entry, tmp, &client_list) {
Jens Axboe82a4be12011-10-01 12:40:32 -06001027 client = flist_entry(entry, struct fio_client, list);
1028
Jens Axboe3ec62ec2012-03-01 12:01:29 +01001029 if (!client->sent_job && !ops->stay_connected &&
Jens Axboec2cb6862012-02-23 20:56:12 +01001030 flist_empty(&client->cmd_list)) {
1031 remove_client(client);
1032 continue;
1033 }
1034
Jens Axboe82a4be12011-10-01 12:40:32 -06001035 pfds[i].fd = client->fd;
1036 pfds[i].events = POLLIN;
1037 i++;
1038 }
1039
Jens Axboec2cb6862012-02-23 20:56:12 +01001040 if (!nr_clients)
1041 break;
1042
Jens Axboe82a4be12011-10-01 12:40:32 -06001043 assert(i == nr_clients);
1044
Jens Axboe5c2857f2011-10-04 13:14:32 +02001045 do {
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001046 struct timeval tv;
1047
1048 gettimeofday(&tv, NULL);
1049 if (mtime_since(&eta_tv, &tv) >= 900) {
1050 request_client_etas();
1051 memcpy(&eta_tv, &tv, sizeof(tv));
Jens Axboe89c17072011-10-11 10:15:51 +02001052
1053 if (fio_client_timed_out())
1054 break;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001055 }
1056
Jens Axboe5c2857f2011-10-04 13:14:32 +02001057 ret = poll(pfds, nr_clients, 100);
1058 if (ret < 0) {
1059 if (errno == EINTR)
1060 continue;
1061 log_err("fio: poll clients: %s\n", strerror(errno));
1062 break;
1063 } else if (!ret)
Jens Axboeb66570d2011-10-01 11:11:35 -06001064 continue;
Jens Axboe5c2857f2011-10-04 13:14:32 +02001065 } while (ret <= 0);
Jens Axboeb66570d2011-10-01 11:11:35 -06001066
1067 for (i = 0; i < nr_clients; i++) {
1068 if (!(pfds[i].revents & POLLIN))
1069 continue;
1070
1071 client = find_client_by_fd(pfds[i].fd);
1072 if (!client) {
Jens Axboe3c5f57e2011-10-06 12:37:50 +02001073 log_err("fio: unknown client fd %d\n", pfds[i].fd);
Jens Axboeb66570d2011-10-01 11:11:35 -06001074 continue;
1075 }
Jens Axboe3e47bd22012-02-29 13:45:02 +01001076 if (!fio_handle_client(client, ops)) {
Jens Axboe28d3ab02011-10-05 20:45:37 +02001077 log_info("client: host=%s disconnected\n",
1078 client->hostname);
1079 remove_client(client);
Jens Axboe498c92c2011-10-17 09:14:42 +02001080 retval = 1;
Jens Axboe38990762011-10-17 13:31:33 +02001081 } else if (client->error)
Jens Axboe498c92c2011-10-17 09:14:42 +02001082 retval = 1;
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}