blob: 6021e4a5810568015be5c259bd1a78627c02d23e [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 {
Jens Axboe82c1ed32011-10-10 08:56:18 +020024 unsigned int pending;
Jens Axboe78c11112013-01-09 11:46:01 +010025 struct jobs_eta eta;
Jens Axboe82c1ed32011-10-10 08:56:18 +020026};
27
Jens Axboeb66570d2011-10-01 11:11:35 -060028struct fio_client {
29 struct flist_head list;
Jens Axboebebe6392011-10-07 10:00:51 +020030 struct flist_head hash_list;
Jens Axboe3f3a4542011-10-10 21:11:09 +020031 struct flist_head arg_list;
Jens Axboe811826b2011-10-24 09:11:50 +020032 union {
33 struct sockaddr_in addr;
34 struct sockaddr_in6 addr6;
35 struct sockaddr_un addr_un;
36 };
Jens Axboeb66570d2011-10-01 11:11:35 -060037 char *hostname;
Jens Axboebebe6392011-10-07 10:00:51 +020038 int port;
Jens Axboeb66570d2011-10-01 11:11:35 -060039 int fd;
Jens Axboee55f8f32012-03-06 15:42:31 +010040 unsigned int refs;
Jens Axboe81179ee2011-10-04 12:42:06 +020041
Jens Axboeb5296dd2011-10-07 16:35:56 +020042 char *name;
43
Jens Axboe81179ee2011-10-04 12:42:06 +020044 int state;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +020045
Jens Axboe17dd1762011-10-04 13:27:34 +020046 int skip_newline;
Jens Axboe87aa8f12011-10-06 21:24:13 +020047 int is_sock;
Jens Axboed09a64a2011-10-13 11:38:56 +020048 int disk_stats_shown;
Jens Axboe11e950b2011-10-16 21:34:14 +020049 unsigned int jobs;
Jens Axboe108fea72012-11-14 13:09:45 -070050 unsigned int nr_stat;
Jens Axboe11e950b2011-10-16 21:34:14 +020051 int error;
Jens Axboe811826b2011-10-24 09:11:50 +020052 int ipv6;
Jens Axboec2cb6862012-02-23 20:56:12 +010053 int sent_job;
Jens Axboe108fea72012-11-14 13:09:45 -070054 int did_stat;
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;
Jens Axboebd023602012-08-26 17:30:45 +020063
64 char **ini_file;
65 unsigned int nr_ini_file;
Jens Axboe81179ee2011-10-04 12:42:06 +020066};
67
Jens Axboeaf9c9fb2011-10-09 21:54:10 +020068static struct timeval eta_tv;
Jens Axboe48fbb462011-10-09 12:19:08 +020069
Jens Axboe81179ee2011-10-04 12:42:06 +020070enum {
Jens Axboe5c2857f2011-10-04 13:14:32 +020071 Client_created = 0,
Jens Axboe81179ee2011-10-04 12:42:06 +020072 Client_connected = 1,
73 Client_started = 2,
Jens Axboe01be0382011-10-15 14:43:41 +020074 Client_running = 3,
75 Client_stopped = 4,
76 Client_exited = 5,
Jens Axboeb66570d2011-10-01 11:11:35 -060077};
78
79static FLIST_HEAD(client_list);
Jens Axboe82c1ed32011-10-10 08:56:18 +020080static FLIST_HEAD(eta_list);
Jens Axboeb66570d2011-10-01 11:11:35 -060081
Jens Axboe3f3a4542011-10-10 21:11:09 +020082static FLIST_HEAD(arg_list);
83
Jens Axboe37f0c1a2011-10-11 14:08:33 +020084static struct thread_stat client_ts;
85static struct group_run_stats client_gs;
Jens Axboe108fea72012-11-14 13:09:45 -070086static int sum_stat_clients = 0;
Jens Axboe37f0c1a2011-10-11 14:08:33 +020087static int sum_stat_nr;
Jens Axboe108fea72012-11-14 13:09:45 -070088static int do_output_all_clients;
Jens Axboe37f0c1a2011-10-11 14:08:33 +020089
Jens Axboe3c5f57e2011-10-06 12:37:50 +020090#define FIO_CLIENT_HASH_BITS 7
91#define FIO_CLIENT_HASH_SZ (1 << FIO_CLIENT_HASH_BITS)
92#define FIO_CLIENT_HASH_MASK (FIO_CLIENT_HASH_SZ - 1)
Jens Axboebebe6392011-10-07 10:00:51 +020093static struct flist_head client_hash[FIO_CLIENT_HASH_SZ];
Jens Axboe3c5f57e2011-10-06 12:37:50 +020094
Jens Axboee951bdc2011-10-05 21:58:45 +020095static int handle_client(struct fio_client *client);
Jens Axboe82c1ed32011-10-10 08:56:18 +020096static void dec_jobs_eta(struct client_eta *eta);
Jens Axboe0b8f30a2011-10-04 10:31:53 +020097
Jens Axboebebe6392011-10-07 10:00:51 +020098static void fio_client_add_hash(struct fio_client *client)
Jens Axboe3c5f57e2011-10-06 12:37:50 +020099{
100 int bucket = hash_long(client->fd, FIO_CLIENT_HASH_BITS);
101
102 bucket &= FIO_CLIENT_HASH_MASK;
Jens Axboebebe6392011-10-07 10:00:51 +0200103 flist_add(&client->hash_list, &client_hash[bucket]);
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200104}
105
Jens Axboebebe6392011-10-07 10:00:51 +0200106static void fio_client_remove_hash(struct fio_client *client)
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200107{
Jens Axboebebe6392011-10-07 10:00:51 +0200108 if (!flist_empty(&client->hash_list))
109 flist_del_init(&client->hash_list);
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200110}
111
112static void fio_init fio_client_hash_init(void)
113{
114 int i;
115
Jens Axboebebe6392011-10-07 10:00:51 +0200116 for (i = 0; i < FIO_CLIENT_HASH_SZ; i++)
117 INIT_FLIST_HEAD(&client_hash[i]);
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200118}
119
Jens Axboeb66570d2011-10-01 11:11:35 -0600120static struct fio_client *find_client_by_fd(int fd)
121{
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200122 int bucket = hash_long(fd, FIO_CLIENT_HASH_BITS) & FIO_CLIENT_HASH_MASK;
Jens Axboeb66570d2011-10-01 11:11:35 -0600123 struct fio_client *client;
124 struct flist_head *entry;
125
Jens Axboebebe6392011-10-07 10:00:51 +0200126 flist_for_each(entry, &client_hash[bucket]) {
127 client = flist_entry(entry, struct fio_client, hash_list);
Jens Axboeb66570d2011-10-01 11:11:35 -0600128
Jens Axboee55f8f32012-03-06 15:42:31 +0100129 if (client->fd == fd) {
130 client->refs++;
Jens Axboeb66570d2011-10-01 11:11:35 -0600131 return client;
Jens Axboee55f8f32012-03-06 15:42:31 +0100132 }
Jens Axboeb66570d2011-10-01 11:11:35 -0600133 }
134
135 return NULL;
136}
137
Jens Axboeb66570d2011-10-01 11:11:35 -0600138static void remove_client(struct fio_client *client)
139{
Jens Axboee55f8f32012-03-06 15:42:31 +0100140 assert(client->refs);
141
142 if (--client->refs)
143 return;
144
Jens Axboe39e8e012011-10-04 13:46:08 +0200145 dprint(FD_NET, "client: removed <%s>\n", client->hostname);
Jens Axboeb66570d2011-10-01 11:11:35 -0600146 flist_del(&client->list);
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200147
Jens Axboebebe6392011-10-07 10:00:51 +0200148 fio_client_remove_hash(client);
Jens Axboe81179ee2011-10-04 12:42:06 +0200149
Jens Axboe82c1ed32011-10-10 08:56:18 +0200150 if (!flist_empty(&client->eta_list)) {
151 flist_del_init(&client->eta_list);
152 dec_jobs_eta(client->eta_in_flight);
153 }
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200154
Jens Axboeb66570d2011-10-01 11:11:35 -0600155 free(client->hostname);
Jens Axboe81179ee2011-10-04 12:42:06 +0200156 if (client->argv)
157 free(client->argv);
Jens Axboeb5296dd2011-10-07 16:35:56 +0200158 if (client->name)
159 free(client->name);
Jens Axboebd023602012-08-26 17:30:45 +0200160 while (client->nr_ini_file)
161 free(client->ini_file[--client->nr_ini_file]);
162 if (client->ini_file)
163 free(client->ini_file);
Jens Axboe81179ee2011-10-04 12:42:06 +0200164
Jens Axboe108fea72012-11-14 13:09:45 -0700165 if (!client->did_stat)
166 sum_stat_clients -= client->nr_stat;
167
Jens Axboeb66570d2011-10-01 11:11:35 -0600168 free(client);
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200169 nr_clients--;
Jens Axboeb66570d2011-10-01 11:11:35 -0600170}
Jens Axboe132159a2011-09-30 15:01:32 -0600171
Jens Axboee55f8f32012-03-06 15:42:31 +0100172static void put_client(struct fio_client *client)
173{
174 remove_client(client);
175}
176
Jens Axboefa2ea802011-10-08 21:07:29 +0200177static void __fio_client_add_cmd_option(struct fio_client *client,
178 const char *opt)
Jens Axboe81179ee2011-10-04 12:42:06 +0200179{
Jens Axboe39e8e012011-10-04 13:46:08 +0200180 int index;
181
182 index = client->argc++;
Jens Axboe81179ee2011-10-04 12:42:06 +0200183 client->argv = realloc(client->argv, sizeof(char *) * client->argc);
Jens Axboe39e8e012011-10-04 13:46:08 +0200184 client->argv[index] = strdup(opt);
185 dprint(FD_NET, "client: add cmd %d: %s\n", index, opt);
Jens Axboe81179ee2011-10-04 12:42:06 +0200186}
187
Jens Axboefa2ea802011-10-08 21:07:29 +0200188void fio_client_add_cmd_option(void *cookie, const char *opt)
Jens Axboe81179ee2011-10-04 12:42:06 +0200189{
Jens Axboebebe6392011-10-07 10:00:51 +0200190 struct fio_client *client = cookie;
Jens Axboe3f3a4542011-10-10 21:11:09 +0200191 struct flist_head *entry;
Jens Axboe81179ee2011-10-04 12:42:06 +0200192
Jens Axboebebe6392011-10-07 10:00:51 +0200193 if (!client || !opt)
Jens Axboefa2ea802011-10-08 21:07:29 +0200194 return;
Jens Axboe81179ee2011-10-04 12:42:06 +0200195
Jens Axboefa2ea802011-10-08 21:07:29 +0200196 __fio_client_add_cmd_option(client, opt);
Jens Axboe3f3a4542011-10-10 21:11:09 +0200197
198 /*
199 * Duplicate arguments to shared client group
200 */
201 flist_for_each(entry, &arg_list) {
202 client = flist_entry(entry, struct fio_client, arg_list);
203
204 __fio_client_add_cmd_option(client, opt);
205 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200206}
207
Jens Axboebd023602012-08-26 17:30:45 +0200208void fio_client_add_ini_file(void *cookie, const char *ini_file)
209{
210 struct fio_client *client = cookie;
211 size_t new_size;
212
213 dprint(FD_NET, "client <%s>: add ini %s\n", client->hostname, ini_file);
214
215 new_size = (client->nr_ini_file + 1) * sizeof(char *);
216 client->ini_file = realloc(client->ini_file, new_size);
217 client->ini_file[client->nr_ini_file] = strdup(ini_file);
218 client->nr_ini_file++;
219}
220
Jens Axboebebe6392011-10-07 10:00:51 +0200221int fio_client_add(const char *hostname, void **cookie)
Jens Axboe132159a2011-09-30 15:01:32 -0600222{
Jens Axboe3f3a4542011-10-10 21:11:09 +0200223 struct fio_client *existing = *cookie;
Jens Axboeb66570d2011-10-01 11:11:35 -0600224 struct fio_client *client;
Jens Axboe132159a2011-09-30 15:01:32 -0600225
Jens Axboe3f3a4542011-10-10 21:11:09 +0200226 if (existing) {
227 /*
228 * We always add our "exec" name as the option, hence 1
229 * means empty.
230 */
231 if (existing->argc == 1)
232 flist_add_tail(&existing->arg_list, &arg_list);
233 else {
234 while (!flist_empty(&arg_list))
235 flist_del_init(arg_list.next);
236 }
237 }
238
Jens Axboeb66570d2011-10-01 11:11:35 -0600239 client = malloc(sizeof(*client));
Jens Axboea37f69b2011-10-01 12:24:21 -0600240 memset(client, 0, sizeof(*client));
Jens Axboe81179ee2011-10-04 12:42:06 +0200241
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200242 INIT_FLIST_HEAD(&client->list);
Jens Axboebebe6392011-10-07 10:00:51 +0200243 INIT_FLIST_HEAD(&client->hash_list);
Jens Axboe3f3a4542011-10-10 21:11:09 +0200244 INIT_FLIST_HEAD(&client->arg_list);
Jens Axboe82c1ed32011-10-10 08:56:18 +0200245 INIT_FLIST_HEAD(&client->eta_list);
Jens Axboe89c17072011-10-11 10:15:51 +0200246 INIT_FLIST_HEAD(&client->cmd_list);
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200247
Jens Axboebebe6392011-10-07 10:00:51 +0200248 if (fio_server_parse_string(hostname, &client->hostname,
249 &client->is_sock, &client->port,
Jens Axboe811826b2011-10-24 09:11:50 +0200250 &client->addr.sin_addr,
251 &client->addr6.sin6_addr,
252 &client->ipv6))
Jens Axboebebe6392011-10-07 10:00:51 +0200253 return -1;
254
Jens Axboea37f69b2011-10-01 12:24:21 -0600255 client->fd = -1;
Jens Axboee55f8f32012-03-06 15:42:31 +0100256 client->refs = 1;
Jens Axboe81179ee2011-10-04 12:42:06 +0200257
258 __fio_client_add_cmd_option(client, "fio");
259
Jens Axboea37f69b2011-10-01 12:24:21 -0600260 flist_add(&client->list, &client_list);
261 nr_clients++;
Jens Axboebebe6392011-10-07 10:00:51 +0200262 dprint(FD_NET, "client: added <%s>\n", client->hostname);
263 *cookie = client;
264 return 0;
Jens Axboea37f69b2011-10-01 12:24:21 -0600265}
266
Jens Axboe87aa8f12011-10-06 21:24:13 +0200267static int fio_client_connect_ip(struct fio_client *client)
Jens Axboea37f69b2011-10-01 12:24:21 -0600268{
Jens Axboe811826b2011-10-24 09:11:50 +0200269 struct sockaddr *addr;
Jens Axboe67bf9822013-01-10 11:23:19 +0100270 socklen_t socklen;
Jens Axboe811826b2011-10-24 09:11:50 +0200271 int fd, domain;
Jens Axboe132159a2011-09-30 15:01:32 -0600272
Jens Axboe811826b2011-10-24 09:11:50 +0200273 if (client->ipv6) {
274 client->addr6.sin6_family = AF_INET6;
275 client->addr6.sin6_port = htons(client->port);
276 domain = AF_INET6;
277 addr = (struct sockaddr *) &client->addr6;
278 socklen = sizeof(client->addr6);
279 } else {
280 client->addr.sin_family = AF_INET;
281 client->addr.sin_port = htons(client->port);
282 domain = AF_INET;
283 addr = (struct sockaddr *) &client->addr;
284 socklen = sizeof(client->addr);
285 }
Jens Axboe132159a2011-09-30 15:01:32 -0600286
Jens Axboe811826b2011-10-24 09:11:50 +0200287 fd = socket(domain, SOCK_STREAM, 0);
Jens Axboe132159a2011-09-30 15:01:32 -0600288 if (fd < 0) {
289 log_err("fio: socket: %s\n", strerror(errno));
Jens Axboe87aa8f12011-10-06 21:24:13 +0200290 return -1;
Jens Axboe132159a2011-09-30 15:01:32 -0600291 }
292
Jens Axboe811826b2011-10-24 09:11:50 +0200293 if (connect(fd, addr, socklen) < 0) {
Jens Axboe132159a2011-09-30 15:01:32 -0600294 log_err("fio: connect: %s\n", strerror(errno));
Jens Axboea7de0a12011-10-07 12:55:14 +0200295 log_err("fio: failed to connect to %s:%u\n", client->hostname,
296 client->port);
Jens Axboeb94cba42011-10-06 21:27:10 +0200297 close(fd);
Jens Axboe87aa8f12011-10-06 21:24:13 +0200298 return -1;
Jens Axboe132159a2011-09-30 15:01:32 -0600299 }
300
Jens Axboe87aa8f12011-10-06 21:24:13 +0200301 return fd;
302}
303
304static int fio_client_connect_sock(struct fio_client *client)
305{
306 struct sockaddr_un *addr = &client->addr_un;
Jens Axboe67bf9822013-01-10 11:23:19 +0100307 socklen_t len;
Jens Axboe87aa8f12011-10-06 21:24:13 +0200308 int fd;
309
310 memset(addr, 0, sizeof(*addr));
311 addr->sun_family = AF_UNIX;
312 strcpy(addr->sun_path, client->hostname);
313
314 fd = socket(AF_UNIX, SOCK_STREAM, 0);
315 if (fd < 0) {
316 log_err("fio: socket: %s\n", strerror(errno));
317 return -1;
318 }
319
320 len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
321 if (connect(fd, (struct sockaddr *) addr, len) < 0) {
322 log_err("fio: connect; %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200323 close(fd);
Jens Axboe87aa8f12011-10-06 21:24:13 +0200324 return -1;
325 }
326
327 return fd;
328}
329
330static int fio_client_connect(struct fio_client *client)
331{
332 int fd;
333
334 dprint(FD_NET, "client: connect to host %s\n", client->hostname);
335
Jens Axboe87aa8f12011-10-06 21:24:13 +0200336 if (client->is_sock)
337 fd = fio_client_connect_sock(client);
338 else
339 fd = fio_client_connect_ip(client);
340
Jens Axboe89c17072011-10-11 10:15:51 +0200341 dprint(FD_NET, "client: %s connected %d\n", client->hostname, fd);
342
Jens Axboe87aa8f12011-10-06 21:24:13 +0200343 if (fd < 0)
344 return 1;
345
Jens Axboeb66570d2011-10-01 11:11:35 -0600346 client->fd = fd;
Jens Axboebebe6392011-10-07 10:00:51 +0200347 fio_client_add_hash(client);
Jens Axboe81179ee2011-10-04 12:42:06 +0200348 client->state = Client_connected;
Jens Axboe132159a2011-09-30 15:01:32 -0600349 return 0;
350}
351
Jens Axboecc0df002011-10-03 20:53:32 +0200352void fio_clients_terminate(void)
353{
354 struct flist_head *entry;
355 struct fio_client *client;
356
Jens Axboe60efd142011-10-04 13:30:11 +0200357 dprint(FD_NET, "client: terminate clients\n");
358
Jens Axboecc0df002011-10-03 20:53:32 +0200359 flist_for_each(entry, &client_list) {
360 client = flist_entry(entry, struct fio_client, list);
361
Jens Axboe89c17072011-10-11 10:15:51 +0200362 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_QUIT, 0, NULL);
Jens Axboecc0df002011-10-03 20:53:32 +0200363 }
364}
365
366static void sig_int(int sig)
367{
Jens Axboebebe6392011-10-07 10:00:51 +0200368 dprint(FD_NET, "client: got signal %d\n", sig);
Jens Axboecc0df002011-10-03 20:53:32 +0200369 fio_clients_terminate();
370}
371
Jens Axboe4c6d91e2012-03-30 10:30:35 +0200372static void sig_show_status(int sig)
373{
374 show_running_run_stats();
375}
376
Jens Axboecc0df002011-10-03 20:53:32 +0200377static void client_signal_handler(void)
378{
379 struct sigaction act;
380
381 memset(&act, 0, sizeof(act));
382 act.sa_handler = sig_int;
383 act.sa_flags = SA_RESTART;
384 sigaction(SIGINT, &act, NULL);
385
386 memset(&act, 0, sizeof(act));
387 act.sa_handler = sig_int;
388 act.sa_flags = SA_RESTART;
389 sigaction(SIGTERM, &act, NULL);
Jens Axboe4c6d91e2012-03-30 10:30:35 +0200390
Bruce Cran2f694502012-10-10 16:34:13 +0100391/* Windows uses SIGBREAK as a quit signal from other applications */
392#ifdef WIN32
393 memset(&act, 0, sizeof(act));
394 act.sa_handler = sig_int;
395 act.sa_flags = SA_RESTART;
396 sigaction(SIGBREAK, &act, NULL);
397#endif
398
Jens Axboe4c6d91e2012-03-30 10:30:35 +0200399 memset(&act, 0, sizeof(act));
400 act.sa_handler = sig_show_status;
401 act.sa_flags = SA_RESTART;
402 sigaction(SIGUSR1, &act, NULL);
Jens Axboecc0df002011-10-03 20:53:32 +0200403}
404
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200405static void probe_client(struct fio_client *client)
406{
Jens Axboe60efd142011-10-04 13:30:11 +0200407 dprint(FD_NET, "client: send probe\n");
408
Jens Axboe89c17072011-10-11 10:15:51 +0200409 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_PROBE, 0, &client->cmd_list);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200410}
411
Jens Axboe81179ee2011-10-04 12:42:06 +0200412static int send_client_cmd_line(struct fio_client *client)
413{
Jens Axboefa2ea802011-10-08 21:07:29 +0200414 struct cmd_single_line_pdu *cslp;
415 struct cmd_line_pdu *clp;
416 unsigned long offset;
Jens Axboe7f868312011-10-10 09:55:21 +0200417 unsigned int *lens;
Jens Axboefa2ea802011-10-08 21:07:29 +0200418 void *pdu;
419 size_t mem;
Jens Axboe81179ee2011-10-04 12:42:06 +0200420 int i, ret;
421
Jens Axboe39e8e012011-10-04 13:46:08 +0200422 dprint(FD_NET, "client: send cmdline %d\n", client->argc);
Jens Axboe60efd142011-10-04 13:30:11 +0200423
Jens Axboe7f868312011-10-10 09:55:21 +0200424 lens = malloc(client->argc * sizeof(unsigned int));
425
Jens Axboefa2ea802011-10-08 21:07:29 +0200426 /*
427 * Find out how much mem we need
428 */
Jens Axboe7f868312011-10-10 09:55:21 +0200429 for (i = 0, mem = 0; i < client->argc; i++) {
430 lens[i] = strlen(client->argv[i]) + 1;
431 mem += lens[i];
432 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200433
Jens Axboefa2ea802011-10-08 21:07:29 +0200434 /*
435 * We need one cmd_line_pdu, and argc number of cmd_single_line_pdu
436 */
437 mem += sizeof(*clp) + (client->argc * sizeof(*cslp));
438
439 pdu = malloc(mem);
440 clp = pdu;
441 offset = sizeof(*clp);
442
443 for (i = 0; i < client->argc; i++) {
Jens Axboe7f868312011-10-10 09:55:21 +0200444 uint16_t arg_len = lens[i];
Jens Axboefa2ea802011-10-08 21:07:29 +0200445
446 cslp = pdu + offset;
447 strcpy((char *) cslp->text, client->argv[i]);
448 cslp->len = cpu_to_le16(arg_len);
449 offset += sizeof(*cslp) + arg_len;
450 }
451
Jens Axboe7f868312011-10-10 09:55:21 +0200452 free(lens);
Jens Axboefa2ea802011-10-08 21:07:29 +0200453 clp->lines = cpu_to_le16(client->argc);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200454 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOBLINE, pdu, mem, 0);
Jens Axboe81179ee2011-10-04 12:42:06 +0200455 free(pdu);
456 return ret;
457}
458
Jens Axboea37f69b2011-10-01 12:24:21 -0600459int fio_clients_connect(void)
460{
461 struct fio_client *client;
462 struct flist_head *entry, *tmp;
463 int ret;
464
Bruce Cran93bcfd22012-02-20 20:18:19 +0100465#ifdef WIN32
466 WSADATA wsd;
467 WSAStartup(MAKEWORD(2,2), &wsd);
468#endif
469
Jens Axboe60efd142011-10-04 13:30:11 +0200470 dprint(FD_NET, "client: connect all\n");
471
Jens Axboecc0df002011-10-03 20:53:32 +0200472 client_signal_handler();
473
Jens Axboea37f69b2011-10-01 12:24:21 -0600474 flist_for_each_safe(entry, tmp, &client_list) {
475 client = flist_entry(entry, struct fio_client, list);
476
477 ret = fio_client_connect(client);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200478 if (ret) {
Jens Axboea37f69b2011-10-01 12:24:21 -0600479 remove_client(client);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200480 continue;
481 }
482
483 probe_client(client);
Jens Axboe81179ee2011-10-04 12:42:06 +0200484
485 if (client->argc > 1)
486 send_client_cmd_line(client);
Jens Axboea37f69b2011-10-01 12:24:21 -0600487 }
488
489 return !nr_clients;
490}
491
Jens Axboe132159a2011-09-30 15:01:32 -0600492/*
493 * Send file contents to server backend. We could use sendfile(), but to remain
494 * more portable lets just read/write the darn thing.
495 */
Jens Axboea37f69b2011-10-01 12:24:21 -0600496static int fio_client_send_ini(struct fio_client *client, const char *filename)
Jens Axboe132159a2011-09-30 15:01:32 -0600497{
498 struct stat sb;
499 char *p, *buf;
500 off_t len;
501 int fd, ret;
502
Jens Axboe46c48f12011-10-01 12:50:51 -0600503 dprint(FD_NET, "send ini %s to %s\n", filename, client->hostname);
504
Jens Axboe132159a2011-09-30 15:01:32 -0600505 fd = open(filename, O_RDONLY);
506 if (fd < 0) {
Jens Axboee951bdc2011-10-05 21:58:45 +0200507 log_err("fio: job file <%s> open: %s\n", filename, strerror(errno));
Jens Axboe132159a2011-09-30 15:01:32 -0600508 return 1;
509 }
510
511 if (fstat(fd, &sb) < 0) {
512 log_err("fio: job file stat: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200513 close(fd);
Jens Axboe132159a2011-09-30 15:01:32 -0600514 return 1;
515 }
516
517 buf = malloc(sb.st_size);
518
519 len = sb.st_size;
520 p = buf;
521 do {
522 ret = read(fd, p, len);
523 if (ret > 0) {
524 len -= ret;
525 if (!len)
526 break;
527 p += ret;
528 continue;
529 } else if (!ret)
530 break;
531 else if (errno == EAGAIN || errno == EINTR)
532 continue;
533 } while (1);
534
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200535 if (len) {
536 log_err("fio: failed reading job file %s\n", filename);
Jens Axboeb94cba42011-10-06 21:27:10 +0200537 close(fd);
Jens Axboec524ef72011-10-07 12:23:34 +0200538 free(buf);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200539 return 1;
540 }
541
Jens Axboec2cb6862012-02-23 20:56:12 +0100542 client->sent_job = 1;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200543 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, buf, sb.st_size, 0);
Jens Axboe132159a2011-09-30 15:01:32 -0600544 free(buf);
Jens Axboeb94cba42011-10-06 21:27:10 +0200545 close(fd);
Jens Axboe132159a2011-09-30 15:01:32 -0600546 return ret;
547}
Jens Axboe37db14f2011-09-30 17:00:42 -0600548
Jens Axboea37f69b2011-10-01 12:24:21 -0600549int fio_clients_send_ini(const char *filename)
550{
551 struct fio_client *client;
552 struct flist_head *entry, *tmp;
553
554 flist_for_each_safe(entry, tmp, &client_list) {
555 client = flist_entry(entry, struct fio_client, list);
556
Jens Axboebd023602012-08-26 17:30:45 +0200557 if (client->nr_ini_file) {
558 int i;
559
560 for (i = 0; i < client->nr_ini_file; i++) {
561 const char *ini = client->ini_file[i];
562
563 if (fio_client_send_ini(client, ini)) {
564 remove_client(client);
565 break;
566 }
567 }
568 } else if (!filename || fio_client_send_ini(client, filename))
Jens Axboea37f69b2011-10-01 12:24:21 -0600569 remove_client(client);
Jens Axboec2cb6862012-02-23 20:56:12 +0100570
571 client->sent_job = 1;
Jens Axboea37f69b2011-10-01 12:24:21 -0600572 }
573
574 return !nr_clients;
575}
576
Jens Axboea64e88d2011-10-03 14:20:01 +0200577static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
578{
579 dst->max_val = le64_to_cpu(src->max_val);
580 dst->min_val = le64_to_cpu(src->min_val);
581 dst->samples = le64_to_cpu(src->samples);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200582
583 /*
584 * Floats arrive as IEEE 754 encoded uint64_t, convert back to double
585 */
586 dst->mean.u.f = fio_uint64_to_double(le64_to_cpu(dst->mean.u.i));
587 dst->S.u.f = fio_uint64_to_double(le64_to_cpu(dst->S.u.i));
Jens Axboea64e88d2011-10-03 14:20:01 +0200588}
589
590static void convert_ts(struct thread_stat *dst, struct thread_stat *src)
591{
592 int i, j;
593
594 dst->error = le32_to_cpu(src->error);
595 dst->groupid = le32_to_cpu(src->groupid);
596 dst->pid = le32_to_cpu(src->pid);
597 dst->members = le32_to_cpu(src->members);
598
Jens Axboe78799de2013-01-29 20:53:52 +0100599 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
Jens Axboea64e88d2011-10-03 14:20:01 +0200600 convert_io_stat(&dst->clat_stat[i], &src->clat_stat[i]);
601 convert_io_stat(&dst->slat_stat[i], &src->slat_stat[i]);
602 convert_io_stat(&dst->lat_stat[i], &src->lat_stat[i]);
603 convert_io_stat(&dst->bw_stat[i], &src->bw_stat[i]);
604 }
605
606 dst->usr_time = le64_to_cpu(src->usr_time);
607 dst->sys_time = le64_to_cpu(src->sys_time);
608 dst->ctx = le64_to_cpu(src->ctx);
609 dst->minf = le64_to_cpu(src->minf);
610 dst->majf = le64_to_cpu(src->majf);
611 dst->clat_percentiles = le64_to_cpu(src->clat_percentiles);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200612
613 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
614 fio_fp64_t *fps = &src->percentile_list[i];
615 fio_fp64_t *fpd = &dst->percentile_list[i];
616
617 fpd->u.f = fio_uint64_to_double(le64_to_cpu(fps->u.i));
618 }
Jens Axboea64e88d2011-10-03 14:20:01 +0200619
620 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
621 dst->io_u_map[i] = le32_to_cpu(src->io_u_map[i]);
622 dst->io_u_submit[i] = le32_to_cpu(src->io_u_submit[i]);
623 dst->io_u_complete[i] = le32_to_cpu(src->io_u_complete[i]);
624 }
625
626 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
627 dst->io_u_lat_u[i] = le32_to_cpu(src->io_u_lat_u[i]);
628 dst->io_u_lat_m[i] = le32_to_cpu(src->io_u_lat_m[i]);
629 }
630
Jens Axboe78799de2013-01-29 20:53:52 +0100631 for (i = 0; i < DDIR_RWDIR_CNT; i++)
Jens Axboea64e88d2011-10-03 14:20:01 +0200632 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
633 dst->io_u_plat[i][j] = le32_to_cpu(src->io_u_plat[i][j]);
634
Jens Axboe78799de2013-01-29 20:53:52 +0100635 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
Jens Axboea64e88d2011-10-03 14:20:01 +0200636 dst->total_io_u[i] = le64_to_cpu(src->total_io_u[i]);
Jens Axboe93eee042011-10-03 21:08:48 +0200637 dst->short_io_u[i] = le64_to_cpu(src->short_io_u[i]);
Jens Axboea64e88d2011-10-03 14:20:01 +0200638 }
639
640 dst->total_submit = le64_to_cpu(src->total_submit);
641 dst->total_complete = le64_to_cpu(src->total_complete);
642
Jens Axboe78799de2013-01-29 20:53:52 +0100643 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
Jens Axboea64e88d2011-10-03 14:20:01 +0200644 dst->io_bytes[i] = le64_to_cpu(src->io_bytes[i]);
645 dst->runtime[i] = le64_to_cpu(src->runtime[i]);
646 }
647
648 dst->total_run_time = le64_to_cpu(src->total_run_time);
649 dst->continue_on_error = le16_to_cpu(src->continue_on_error);
650 dst->total_err_count = le64_to_cpu(src->total_err_count);
Jens Axboeddcc0b62011-10-03 14:45:27 +0200651 dst->first_error = le32_to_cpu(src->first_error);
652 dst->kb_base = le32_to_cpu(src->kb_base);
Jens Axboea64e88d2011-10-03 14:20:01 +0200653}
654
655static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
656{
657 int i;
658
Jens Axboe78799de2013-01-29 20:53:52 +0100659 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
Jens Axboea64e88d2011-10-03 14:20:01 +0200660 dst->max_run[i] = le64_to_cpu(src->max_run[i]);
661 dst->min_run[i] = le64_to_cpu(src->min_run[i]);
662 dst->max_bw[i] = le64_to_cpu(src->max_bw[i]);
663 dst->min_bw[i] = le64_to_cpu(src->min_bw[i]);
664 dst->io_kb[i] = le64_to_cpu(src->io_kb[i]);
665 dst->agg[i] = le64_to_cpu(src->agg[i]);
666 }
667
668 dst->kb_base = le32_to_cpu(src->kb_base);
669 dst->groupid = le32_to_cpu(src->groupid);
670}
671
Jens Axboe108fea72012-11-14 13:09:45 -0700672static void handle_ts(struct fio_client *client, struct fio_net_cmd *cmd)
Jens Axboea64e88d2011-10-03 14:20:01 +0200673{
674 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
675
676 convert_ts(&p->ts, &p->ts);
677 convert_gs(&p->rs, &p->rs);
678
679 show_thread_status(&p->ts, &p->rs);
Jens Axboe108fea72012-11-14 13:09:45 -0700680 client->did_stat = 1;
Jens Axboe37f0c1a2011-10-11 14:08:33 +0200681
Jens Axboe108fea72012-11-14 13:09:45 -0700682 if (!do_output_all_clients)
Jens Axboe37f0c1a2011-10-11 14:08:33 +0200683 return;
684
685 sum_thread_stats(&client_ts, &p->ts, sum_stat_nr);
686 sum_group_stats(&client_gs, &p->rs);
687
688 client_ts.members++;
689 client_ts.groupid = p->ts.groupid;
690
691 if (++sum_stat_nr == sum_stat_clients) {
692 strcpy(client_ts.name, "All clients");
693 show_thread_status(&client_ts, &client_gs);
694 }
Jens Axboea64e88d2011-10-03 14:20:01 +0200695}
696
697static void handle_gs(struct fio_net_cmd *cmd)
698{
699 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
700
701 convert_gs(gs, gs);
702 show_group_stats(gs);
703}
704
Jens Axboed09a64a2011-10-13 11:38:56 +0200705static void convert_agg(struct disk_util_agg *agg)
706{
707 int i;
708
709 for (i = 0; i < 2; i++) {
710 agg->ios[i] = le32_to_cpu(agg->ios[i]);
711 agg->merges[i] = le32_to_cpu(agg->merges[i]);
712 agg->sectors[i] = le64_to_cpu(agg->sectors[i]);
713 agg->ticks[i] = le32_to_cpu(agg->ticks[i]);
714 }
715
716 agg->io_ticks = le32_to_cpu(agg->io_ticks);
717 agg->time_in_queue = le32_to_cpu(agg->time_in_queue);
718 agg->slavecount = le32_to_cpu(agg->slavecount);
Anton Blanchard823ba542011-11-07 14:16:26 +0100719 agg->max_util.u.f = fio_uint64_to_double(__le64_to_cpu(agg->max_util.u.i));
Jens Axboed09a64a2011-10-13 11:38:56 +0200720}
721
722static void convert_dus(struct disk_util_stat *dus)
723{
724 int i;
725
726 for (i = 0; i < 2; i++) {
727 dus->ios[i] = le32_to_cpu(dus->ios[i]);
728 dus->merges[i] = le32_to_cpu(dus->merges[i]);
729 dus->sectors[i] = le64_to_cpu(dus->sectors[i]);
730 dus->ticks[i] = le32_to_cpu(dus->ticks[i]);
731 }
732
733 dus->io_ticks = le32_to_cpu(dus->io_ticks);
734 dus->time_in_queue = le32_to_cpu(dus->time_in_queue);
735 dus->msec = le64_to_cpu(dus->msec);
736}
737
738static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd)
739{
740 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
741
742 convert_dus(&du->dus);
743 convert_agg(&du->agg);
744
745 if (!client->disk_stats_shown) {
746 client->disk_stats_shown = 1;
747 log_info("\nDisk stats (read/write):\n");
748 }
749
Jens Axboef3afa572012-09-17 13:34:16 +0200750 print_disk_util(&du->dus, &du->agg, output_format == FIO_OUTPUT_TERSE);
Jens Axboed09a64a2011-10-13 11:38:56 +0200751}
752
Jens Axboe48fbb462011-10-09 12:19:08 +0200753static void convert_jobs_eta(struct jobs_eta *je)
Jens Axboecf451d12011-10-03 16:48:30 +0200754{
Jens Axboecf451d12011-10-03 16:48:30 +0200755 int i;
756
757 je->nr_running = le32_to_cpu(je->nr_running);
758 je->nr_ramp = le32_to_cpu(je->nr_ramp);
759 je->nr_pending = le32_to_cpu(je->nr_pending);
760 je->files_open = le32_to_cpu(je->files_open);
761 je->m_rate = le32_to_cpu(je->m_rate);
762 je->t_rate = le32_to_cpu(je->t_rate);
763 je->m_iops = le32_to_cpu(je->m_iops);
764 je->t_iops = le32_to_cpu(je->t_iops);
765
Jens Axboe78799de2013-01-29 20:53:52 +0100766 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
Jens Axboecf451d12011-10-03 16:48:30 +0200767 je->rate[i] = le32_to_cpu(je->rate[i]);
768 je->iops[i] = le32_to_cpu(je->iops[i]);
769 }
770
Jens Axboeb51eedb2011-10-09 12:13:39 +0200771 je->elapsed_sec = le64_to_cpu(je->elapsed_sec);
Jens Axboecf451d12011-10-03 16:48:30 +0200772 je->eta_sec = le64_to_cpu(je->eta_sec);
Jens Axboeb7f05eb2012-05-11 20:33:02 +0200773 je->is_pow2 = le32_to_cpu(je->is_pow2);
Jens Axboe48fbb462011-10-09 12:19:08 +0200774}
Jens Axboecf451d12011-10-03 16:48:30 +0200775
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200776static void sum_jobs_eta(struct jobs_eta *dst, struct jobs_eta *je)
Jens Axboe48fbb462011-10-09 12:19:08 +0200777{
Jens Axboe48fbb462011-10-09 12:19:08 +0200778 int i;
779
780 dst->nr_running += je->nr_running;
781 dst->nr_ramp += je->nr_ramp;
782 dst->nr_pending += je->nr_pending;
783 dst->files_open += je->files_open;
784 dst->m_rate += je->m_rate;
785 dst->t_rate += je->t_rate;
786 dst->m_iops += je->m_iops;
787 dst->t_iops += je->t_iops;
788
Jens Axboe78799de2013-01-29 20:53:52 +0100789 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
Jens Axboe48fbb462011-10-09 12:19:08 +0200790 dst->rate[i] += je->rate[i];
791 dst->iops[i] += je->iops[i];
792 }
793
794 dst->elapsed_sec += je->elapsed_sec;
795
796 if (je->eta_sec > dst->eta_sec)
797 dst->eta_sec = je->eta_sec;
798}
799
Jens Axboe82c1ed32011-10-10 08:56:18 +0200800static void dec_jobs_eta(struct client_eta *eta)
801{
802 if (!--eta->pending) {
803 display_thread_status(&eta->eta);
804 free(eta);
805 }
806}
807
Jens Axboe89c17072011-10-11 10:15:51 +0200808static void remove_reply_cmd(struct fio_client *client, struct fio_net_cmd *cmd)
809{
810 struct fio_net_int_cmd *icmd = NULL;
811 struct flist_head *entry;
812
813 flist_for_each(entry, &client->cmd_list) {
814 icmd = flist_entry(entry, struct fio_net_int_cmd, list);
815
Jens Axboedf380932011-10-11 14:25:08 +0200816 if (cmd->tag == (uintptr_t) icmd)
Jens Axboe89c17072011-10-11 10:15:51 +0200817 break;
818
819 icmd = NULL;
820 }
821
822 if (!icmd) {
823 log_err("fio: client: unable to find matching tag\n");
824 return;
825 }
826
827 flist_del(&icmd->list);
828 cmd->tag = icmd->saved_tag;
829 free(icmd);
830}
831
Jens Axboe82c1ed32011-10-10 08:56:18 +0200832static void handle_eta(struct fio_client *client, struct fio_net_cmd *cmd)
Jens Axboe48fbb462011-10-09 12:19:08 +0200833{
834 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
Jens Axboedf380932011-10-11 14:25:08 +0200835 struct client_eta *eta = (struct client_eta *) (uintptr_t) cmd->tag;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200836
837 dprint(FD_NET, "client: got eta tag %p, %d\n", eta, eta->pending);
Jens Axboe48fbb462011-10-09 12:19:08 +0200838
Jens Axboef77d2672011-10-10 14:36:07 +0200839 assert(client->eta_in_flight == eta);
840
841 client->eta_in_flight = NULL;
Jens Axboe82c1ed32011-10-10 08:56:18 +0200842 flist_del_init(&client->eta_list);
843
Jens Axboe48fbb462011-10-09 12:19:08 +0200844 convert_jobs_eta(je);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200845 sum_jobs_eta(&eta->eta, je);
Jens Axboe82c1ed32011-10-10 08:56:18 +0200846 dec_jobs_eta(eta);
Jens Axboecf451d12011-10-03 16:48:30 +0200847}
848
Jens Axboeb5296dd2011-10-07 16:35:56 +0200849static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd)
Jens Axboe2e03b4b2011-10-04 09:00:40 +0200850{
851 struct cmd_probe_pdu *probe = (struct cmd_probe_pdu *) cmd->payload;
Jens Axboed2333352011-10-11 15:07:23 +0200852 const char *os, *arch;
853 char bit[16];
Jens Axboe2e03b4b2011-10-04 09:00:40 +0200854
Jens Axboecca84642011-10-07 12:47:57 +0200855 os = fio_get_os_string(probe->os);
856 if (!os)
857 os = "unknown";
858
859 arch = fio_get_arch_string(probe->arch);
860 if (!arch)
861 os = "unknown";
862
Jens Axboed2333352011-10-11 15:07:23 +0200863 sprintf(bit, "%d-bit", probe->bpp * 8);
Jens Axboe38fdef22011-10-11 14:30:06 +0200864
Jens Axboe0922d612012-04-16 09:52:22 +0200865 log_info("hostname=%s, be=%u, %s, os=%s, arch=%s, fio=%s\n",
Jens Axboe38fdef22011-10-11 14:30:06 +0200866 probe->hostname, probe->bigendian, bit, os, arch,
Jens Axboe0922d612012-04-16 09:52:22 +0200867 probe->fio_version);
Jens Axboeb5296dd2011-10-07 16:35:56 +0200868
869 if (!client->name)
870 client->name = strdup((char *) probe->hostname);
Jens Axboe2e03b4b2011-10-04 09:00:40 +0200871}
872
Jens Axboe11e950b2011-10-16 21:34:14 +0200873static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd)
874{
875 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
876
877 client->state = Client_started;
878 client->jobs = le32_to_cpu(pdu->jobs);
Jens Axboe108fea72012-11-14 13:09:45 -0700879 client->nr_stat = le32_to_cpu(pdu->stat_outputs);
880
881 if (sum_stat_clients > 1)
882 do_output_all_clients = 1;
883
884 sum_stat_clients += client->nr_stat;
Jens Axboe11e950b2011-10-16 21:34:14 +0200885}
886
887static void handle_stop(struct fio_client *client, struct fio_net_cmd *cmd)
888{
889 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
890
891 client->state = Client_stopped;
892 client->error = le32_to_cpu(pdu->error);
Jens Axboe498c92c2011-10-17 09:14:42 +0200893
894 if (client->error)
895 log_info("client <%s>: exited with error %d\n", client->hostname, client->error);
Jens Axboe11e950b2011-10-16 21:34:14 +0200896}
897
Jens Axboee951bdc2011-10-05 21:58:45 +0200898static int handle_client(struct fio_client *client)
Jens Axboe37db14f2011-09-30 17:00:42 -0600899{
900 struct fio_net_cmd *cmd;
901
Jens Axboe60efd142011-10-04 13:30:11 +0200902 dprint(FD_NET, "client: handle %s\n", client->hostname);
903
Jens Axboee951bdc2011-10-05 21:58:45 +0200904 cmd = fio_net_recv_cmd(client->fd);
905 if (!cmd)
906 return 0;
Jens Axboec2c94582011-10-05 20:31:30 +0200907
Jens Axboe89c17072011-10-11 10:15:51 +0200908 dprint(FD_NET, "client: got cmd op %s from %s\n",
909 fio_server_op(cmd->opcode), client->hostname);
Jens Axboe46c48f12011-10-01 12:50:51 -0600910
Jens Axboee951bdc2011-10-05 21:58:45 +0200911 switch (cmd->opcode) {
912 case FIO_NET_CMD_QUIT:
913 remove_client(client);
914 free(cmd);
915 break;
916 case FIO_NET_CMD_TEXT: {
917 const char *buf = (const char *) cmd->payload;
Jens Axboeb5296dd2011-10-07 16:35:56 +0200918 const char *name;
Jens Axboee951bdc2011-10-05 21:58:45 +0200919 int fio_unused ret;
Jens Axboe17dd1762011-10-04 13:27:34 +0200920
Jens Axboeb5296dd2011-10-07 16:35:56 +0200921 name = client->name ? client->name : client->hostname;
922
Jens Axboee951bdc2011-10-05 21:58:45 +0200923 if (!client->skip_newline)
Jens Axboeb5296dd2011-10-07 16:35:56 +0200924 fprintf(f_out, "<%s> ", name);
Jens Axboee951bdc2011-10-05 21:58:45 +0200925 ret = fwrite(buf, cmd->pdu_len, 1, f_out);
926 fflush(f_out);
927 client->skip_newline = strchr(buf, '\n') == NULL;
928 free(cmd);
929 break;
Jens Axboe37db14f2011-09-30 17:00:42 -0600930 }
Jens Axboed09a64a2011-10-13 11:38:56 +0200931 case FIO_NET_CMD_DU:
932 handle_du(client, cmd);
933 free(cmd);
934 break;
Jens Axboee951bdc2011-10-05 21:58:45 +0200935 case FIO_NET_CMD_TS:
Jens Axboe108fea72012-11-14 13:09:45 -0700936 handle_ts(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +0200937 free(cmd);
938 break;
939 case FIO_NET_CMD_GS:
940 handle_gs(cmd);
941 free(cmd);
942 break;
943 case FIO_NET_CMD_ETA:
Jens Axboe89c17072011-10-11 10:15:51 +0200944 remove_reply_cmd(client, cmd);
Jens Axboe82c1ed32011-10-10 08:56:18 +0200945 handle_eta(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +0200946 free(cmd);
947 break;
948 case FIO_NET_CMD_PROBE:
Jens Axboe89c17072011-10-11 10:15:51 +0200949 remove_reply_cmd(client, cmd);
Jens Axboeb5296dd2011-10-07 16:35:56 +0200950 handle_probe(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +0200951 free(cmd);
952 break;
Jens Axboe01be0382011-10-15 14:43:41 +0200953 case FIO_NET_CMD_RUN:
954 client->state = Client_running;
955 free(cmd);
956 break;
Jens Axboee951bdc2011-10-05 21:58:45 +0200957 case FIO_NET_CMD_START:
Jens Axboe11e950b2011-10-16 21:34:14 +0200958 handle_start(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +0200959 free(cmd);
960 break;
961 case FIO_NET_CMD_STOP:
Jens Axboe11e950b2011-10-16 21:34:14 +0200962 handle_stop(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +0200963 free(cmd);
964 break;
965 default:
Jens Axboe89c17072011-10-11 10:15:51 +0200966 log_err("fio: unknown client op: %s\n", fio_server_op(cmd->opcode));
Jens Axboee951bdc2011-10-05 21:58:45 +0200967 free(cmd);
968 break;
Jens Axboe37db14f2011-09-30 17:00:42 -0600969 }
970
Jens Axboee951bdc2011-10-05 21:58:45 +0200971 return 1;
Jens Axboe37db14f2011-09-30 17:00:42 -0600972}
Jens Axboeb66570d2011-10-01 11:11:35 -0600973
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200974static void request_client_etas(void)
975{
976 struct fio_client *client;
977 struct flist_head *entry;
978 struct client_eta *eta;
Jens Axboe82c1ed32011-10-10 08:56:18 +0200979 int skipped = 0;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200980
981 dprint(FD_NET, "client: request eta (%d)\n", nr_clients);
982
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200983 eta = malloc(sizeof(*eta));
984 memset(&eta->eta, 0, sizeof(eta->eta));
985 eta->pending = nr_clients;
986
987 flist_for_each(entry, &client_list) {
988 client = flist_entry(entry, struct fio_client, list);
989
Jens Axboe82c1ed32011-10-10 08:56:18 +0200990 if (!flist_empty(&client->eta_list)) {
991 skipped++;
992 continue;
993 }
Jens Axboe01be0382011-10-15 14:43:41 +0200994 if (client->state != Client_running)
995 continue;
Jens Axboe82c1ed32011-10-10 08:56:18 +0200996
Jens Axboef77d2672011-10-10 14:36:07 +0200997 assert(!client->eta_in_flight);
Jens Axboe82c1ed32011-10-10 08:56:18 +0200998 flist_add_tail(&client->eta_list, &eta_list);
Jens Axboef77d2672011-10-10 14:36:07 +0200999 client->eta_in_flight = eta;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001000 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_SEND_ETA,
Jens Axboedf380932011-10-11 14:25:08 +02001001 (uintptr_t) eta, &client->cmd_list);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001002 }
1003
Jens Axboe82c1ed32011-10-10 08:56:18 +02001004 while (skipped--)
1005 dec_jobs_eta(eta);
1006
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001007 dprint(FD_NET, "client: requested eta tag %p\n", eta);
1008}
1009
Jens Axboe89c17072011-10-11 10:15:51 +02001010static int client_check_cmd_timeout(struct fio_client *client,
1011 struct timeval *now)
1012{
1013 struct fio_net_int_cmd *cmd;
1014 struct flist_head *entry, *tmp;
1015 int ret = 0;
1016
1017 flist_for_each_safe(entry, tmp, &client->cmd_list) {
1018 cmd = flist_entry(entry, struct fio_net_int_cmd, list);
1019
1020 if (mtime_since(&cmd->tv, now) < FIO_NET_CLIENT_TIMEOUT)
1021 continue;
1022
1023 log_err("fio: client %s, timeout on cmd %s\n", client->hostname,
1024 fio_server_op(cmd->cmd.opcode));
1025 flist_del(&cmd->list);
1026 free(cmd);
1027 ret = 1;
1028 }
1029
1030 return flist_empty(&client->cmd_list) && ret;
1031}
1032
1033static int fio_client_timed_out(void)
1034{
1035 struct fio_client *client;
1036 struct flist_head *entry, *tmp;
1037 struct timeval tv;
1038 int ret = 0;
1039
Jens Axboe67bf9822013-01-10 11:23:19 +01001040 fio_gettime(&tv, NULL);
Jens Axboe89c17072011-10-11 10:15:51 +02001041
1042 flist_for_each_safe(entry, tmp, &client_list) {
1043 client = flist_entry(entry, struct fio_client, list);
1044
1045 if (flist_empty(&client->cmd_list))
1046 continue;
1047
1048 if (!client_check_cmd_timeout(client, &tv))
1049 continue;
1050
1051 log_err("fio: client %s timed out\n", client->hostname);
1052 remove_client(client);
1053 ret = 1;
1054 }
1055
1056 return ret;
1057}
1058
Jens Axboeb66570d2011-10-01 11:11:35 -06001059int fio_handle_clients(void)
1060{
Jens Axboeb66570d2011-10-01 11:11:35 -06001061 struct pollfd *pfds;
Jens Axboe498c92c2011-10-17 09:14:42 +02001062 int i, ret = 0, retval = 0;
Jens Axboeb66570d2011-10-01 11:11:35 -06001063
Jens Axboe67bf9822013-01-10 11:23:19 +01001064 fio_gettime(&eta_tv, NULL);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001065
Jens Axboeb66570d2011-10-01 11:11:35 -06001066 pfds = malloc(nr_clients * sizeof(struct pollfd));
1067
Jens Axboe37f0c1a2011-10-11 14:08:33 +02001068 init_thread_stat(&client_ts);
1069 init_group_run_stat(&client_gs);
1070
Jens Axboeb66570d2011-10-01 11:11:35 -06001071 while (!exit_backend && nr_clients) {
Jens Axboec2cb6862012-02-23 20:56:12 +01001072 struct flist_head *entry, *tmp;
1073 struct fio_client *client;
1074
Jens Axboe82a4be12011-10-01 12:40:32 -06001075 i = 0;
Jens Axboec2cb6862012-02-23 20:56:12 +01001076 flist_for_each_safe(entry, tmp, &client_list) {
Jens Axboe82a4be12011-10-01 12:40:32 -06001077 client = flist_entry(entry, struct fio_client, list);
1078
Jens Axboec2cb6862012-02-23 20:56:12 +01001079 if (!client->sent_job &&
1080 flist_empty(&client->cmd_list)) {
1081 remove_client(client);
1082 continue;
1083 }
1084
Jens Axboe82a4be12011-10-01 12:40:32 -06001085 pfds[i].fd = client->fd;
1086 pfds[i].events = POLLIN;
1087 i++;
1088 }
1089
Jens Axboec2cb6862012-02-23 20:56:12 +01001090 if (!nr_clients)
1091 break;
1092
Jens Axboe82a4be12011-10-01 12:40:32 -06001093 assert(i == nr_clients);
1094
Jens Axboe5c2857f2011-10-04 13:14:32 +02001095 do {
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001096 struct timeval tv;
1097
Jens Axboe67bf9822013-01-10 11:23:19 +01001098 fio_gettime(&tv, NULL);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001099 if (mtime_since(&eta_tv, &tv) >= 900) {
1100 request_client_etas();
1101 memcpy(&eta_tv, &tv, sizeof(tv));
Jens Axboe89c17072011-10-11 10:15:51 +02001102
1103 if (fio_client_timed_out())
1104 break;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001105 }
1106
Jens Axboe5c2857f2011-10-04 13:14:32 +02001107 ret = poll(pfds, nr_clients, 100);
1108 if (ret < 0) {
1109 if (errno == EINTR)
1110 continue;
1111 log_err("fio: poll clients: %s\n", strerror(errno));
1112 break;
1113 } else if (!ret)
Jens Axboeb66570d2011-10-01 11:11:35 -06001114 continue;
Jens Axboe5c2857f2011-10-04 13:14:32 +02001115 } while (ret <= 0);
Jens Axboeb66570d2011-10-01 11:11:35 -06001116
1117 for (i = 0; i < nr_clients; i++) {
1118 if (!(pfds[i].revents & POLLIN))
1119 continue;
1120
1121 client = find_client_by_fd(pfds[i].fd);
1122 if (!client) {
Jens Axboe3c5f57e2011-10-06 12:37:50 +02001123 log_err("fio: unknown client fd %d\n", pfds[i].fd);
Jens Axboeb66570d2011-10-01 11:11:35 -06001124 continue;
1125 }
Jens Axboee951bdc2011-10-05 21:58:45 +02001126 if (!handle_client(client)) {
Jens Axboe28d3ab02011-10-05 20:45:37 +02001127 log_info("client: host=%s disconnected\n",
1128 client->hostname);
1129 remove_client(client);
Jens Axboe498c92c2011-10-17 09:14:42 +02001130 retval = 1;
Jens Axboe38990762011-10-17 13:31:33 +02001131 } else if (client->error)
Jens Axboe498c92c2011-10-17 09:14:42 +02001132 retval = 1;
Jens Axboee55f8f32012-03-06 15:42:31 +01001133 put_client(client);
Jens Axboeb66570d2011-10-01 11:11:35 -06001134 }
1135 }
1136
1137 free(pfds);
Jens Axboe498c92c2011-10-17 09:14:42 +02001138 return retval;
Jens Axboeb66570d2011-10-01 11:11:35 -06001139}