blob: 3adddc02f3777d462662b755b298bc50c871d16c [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 handle_du(struct fio_client *client, struct fio_net_cmd *cmd);
Jens Axboe89e5fad2012-03-05 09:21:12 +010025static void handle_ts(struct fio_client *client, struct fio_net_cmd *cmd);
26static void handle_gs(struct fio_client *client, struct fio_net_cmd *cmd);
Stephen M. Camerondd366722012-02-24 08:17:30 +010027static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd);
Jens Axboe084d1c62012-03-03 20:28:07 +010028static void handle_text(struct fio_client *client, struct fio_net_cmd *cmd);
Jens Axboe6b79c802012-03-08 10:51:36 +010029static void handle_stop(struct fio_client *client, struct fio_net_cmd *cmd);
Stephen M. Camerondd366722012-02-24 08:17:30 +010030
31struct client_ops fio_client_ops = {
Jens Axboe084d1c62012-03-03 20:28:07 +010032 .text_op = handle_text,
Jens Axboe0420ba62012-02-29 11:16:52 +010033 .disk_util = handle_du,
34 .thread_status = handle_ts,
35 .group_stats = handle_gs,
Jens Axboe6b79c802012-03-08 10:51:36 +010036 .stop = handle_stop,
Jens Axboea5276612012-03-04 15:15:08 +010037 .eta = display_thread_status,
Jens Axboe0420ba62012-02-29 11:16:52 +010038 .probe = handle_probe,
Stephen M. Camerondd366722012-02-24 08:17:30 +010039};
40
Jens Axboeaf9c9fb2011-10-09 21:54:10 +020041static struct timeval eta_tv;
Jens Axboe48fbb462011-10-09 12:19:08 +020042
Jens Axboe81179ee2011-10-04 12:42:06 +020043enum {
Jens Axboe5c2857f2011-10-04 13:14:32 +020044 Client_created = 0,
Jens Axboe81179ee2011-10-04 12:42:06 +020045 Client_connected = 1,
46 Client_started = 2,
Jens Axboe01be0382011-10-15 14:43:41 +020047 Client_running = 3,
48 Client_stopped = 4,
49 Client_exited = 5,
Jens Axboeb66570d2011-10-01 11:11:35 -060050};
51
52static FLIST_HEAD(client_list);
Jens Axboe82c1ed32011-10-10 08:56:18 +020053static FLIST_HEAD(eta_list);
Jens Axboeb66570d2011-10-01 11:11:35 -060054
Jens Axboe3f3a4542011-10-10 21:11:09 +020055static FLIST_HEAD(arg_list);
56
Jens Axboe3650a3c2012-03-05 14:09:03 +010057struct thread_stat client_ts;
58struct group_run_stats client_gs;
59int sum_stat_clients;
60
Jens Axboe37f0c1a2011-10-11 14:08:33 +020061static int sum_stat_nr;
62
Jens Axboe3c5f57e2011-10-06 12:37:50 +020063#define FIO_CLIENT_HASH_BITS 7
64#define FIO_CLIENT_HASH_SZ (1 << FIO_CLIENT_HASH_BITS)
65#define FIO_CLIENT_HASH_MASK (FIO_CLIENT_HASH_SZ - 1)
Jens Axboebebe6392011-10-07 10:00:51 +020066static struct flist_head client_hash[FIO_CLIENT_HASH_SZ];
Jens Axboe3c5f57e2011-10-06 12:37:50 +020067
Jens Axboebebe6392011-10-07 10:00:51 +020068static void fio_client_add_hash(struct fio_client *client)
Jens Axboe3c5f57e2011-10-06 12:37:50 +020069{
70 int bucket = hash_long(client->fd, FIO_CLIENT_HASH_BITS);
71
72 bucket &= FIO_CLIENT_HASH_MASK;
Jens Axboebebe6392011-10-07 10:00:51 +020073 flist_add(&client->hash_list, &client_hash[bucket]);
Jens Axboe3c5f57e2011-10-06 12:37:50 +020074}
75
Jens Axboebebe6392011-10-07 10:00:51 +020076static void fio_client_remove_hash(struct fio_client *client)
Jens Axboe3c5f57e2011-10-06 12:37:50 +020077{
Jens Axboebebe6392011-10-07 10:00:51 +020078 if (!flist_empty(&client->hash_list))
79 flist_del_init(&client->hash_list);
Jens Axboe3c5f57e2011-10-06 12:37:50 +020080}
81
82static void fio_init fio_client_hash_init(void)
83{
84 int i;
85
Jens Axboebebe6392011-10-07 10:00:51 +020086 for (i = 0; i < FIO_CLIENT_HASH_SZ; i++)
87 INIT_FLIST_HEAD(&client_hash[i]);
Jens Axboe3c5f57e2011-10-06 12:37:50 +020088}
89
Jens Axboeb66570d2011-10-01 11:11:35 -060090static struct fio_client *find_client_by_fd(int fd)
91{
Jens Axboe3c5f57e2011-10-06 12:37:50 +020092 int bucket = hash_long(fd, FIO_CLIENT_HASH_BITS) & FIO_CLIENT_HASH_MASK;
Jens Axboeb66570d2011-10-01 11:11:35 -060093 struct fio_client *client;
94 struct flist_head *entry;
95
Jens Axboebebe6392011-10-07 10:00:51 +020096 flist_for_each(entry, &client_hash[bucket]) {
97 client = flist_entry(entry, struct fio_client, hash_list);
Jens Axboeb66570d2011-10-01 11:11:35 -060098
Jens Axboe5121a9a2012-03-05 13:32:47 +010099 if (client->fd == fd) {
100 client->refs++;
Jens Axboeb66570d2011-10-01 11:11:35 -0600101 return client;
Jens Axboe5121a9a2012-03-05 13:32:47 +0100102 }
Jens Axboeb66570d2011-10-01 11:11:35 -0600103 }
104
105 return NULL;
106}
107
Jens Axboeb66570d2011-10-01 11:11:35 -0600108static void remove_client(struct fio_client *client)
109{
Jens Axboe5121a9a2012-03-05 13:32:47 +0100110 assert(client->refs);
111
112 if (--client->refs)
113 return;
114
Jens Axboe39e8e012011-10-04 13:46:08 +0200115 dprint(FD_NET, "client: removed <%s>\n", client->hostname);
Jens Axboeb66570d2011-10-01 11:11:35 -0600116 flist_del(&client->list);
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200117
Jens Axboebebe6392011-10-07 10:00:51 +0200118 fio_client_remove_hash(client);
Jens Axboe81179ee2011-10-04 12:42:06 +0200119
Jens Axboe82c1ed32011-10-10 08:56:18 +0200120 if (!flist_empty(&client->eta_list)) {
121 flist_del_init(&client->eta_list);
Jens Axboea5276612012-03-04 15:15:08 +0100122 fio_client_dec_jobs_eta(client->eta_in_flight, client->ops->eta);
Jens Axboe82c1ed32011-10-10 08:56:18 +0200123 }
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200124
Jens Axboeb66570d2011-10-01 11:11:35 -0600125 free(client->hostname);
Jens Axboe81179ee2011-10-04 12:42:06 +0200126 if (client->argv)
127 free(client->argv);
Jens Axboeb5296dd2011-10-07 16:35:56 +0200128 if (client->name)
129 free(client->name);
Jens Axboe81179ee2011-10-04 12:42:06 +0200130
Jens Axboeb66570d2011-10-01 11:11:35 -0600131 free(client);
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200132 nr_clients--;
Jens Axboe5fd0acb2011-10-11 14:20:22 +0200133 sum_stat_clients--;
Jens Axboeb66570d2011-10-01 11:11:35 -0600134}
Jens Axboe132159a2011-09-30 15:01:32 -0600135
Jens Axboe5121a9a2012-03-05 13:32:47 +0100136static void put_client(struct fio_client *client)
137{
138 remove_client(client);
139}
140
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 Axboea5276612012-03-04 15:15:08 +0100172struct fio_client *fio_client_add_explicit(struct client_ops *ops,
173 const char *hostname, int type,
Jens Axboe3ec62ec2012-03-01 12:01:29 +0100174 int port)
175{
176 struct fio_client *client;
177
178 client = malloc(sizeof(*client));
179 memset(client, 0, sizeof(*client));
180
181 INIT_FLIST_HEAD(&client->list);
182 INIT_FLIST_HEAD(&client->hash_list);
183 INIT_FLIST_HEAD(&client->arg_list);
184 INIT_FLIST_HEAD(&client->eta_list);
185 INIT_FLIST_HEAD(&client->cmd_list);
186
187 client->hostname = strdup(hostname);
188
189 if (type == Fio_client_socket)
190 client->is_sock = 1;
191 else {
192 int ipv6;
193
194 ipv6 = type == Fio_client_ipv6;
195 if (fio_server_parse_host(hostname, &ipv6,
196 &client->addr.sin_addr,
197 &client->addr6.sin6_addr))
198 goto err;
199
200 client->port = port;
201 }
202
203 client->fd = -1;
Jens Axboea5276612012-03-04 15:15:08 +0100204 client->ops = ops;
Jens Axboe5121a9a2012-03-05 13:32:47 +0100205 client->refs = 1;
Jens Axboe3ec62ec2012-03-01 12:01:29 +0100206
207 __fio_client_add_cmd_option(client, "fio");
208
209 flist_add(&client->list, &client_list);
210 nr_clients++;
211 dprint(FD_NET, "client: added <%s>\n", client->hostname);
212 return client;
213err:
214 free(client);
215 return NULL;
216}
217
Jens Axboea5276612012-03-04 15:15:08 +0100218int fio_client_add(struct client_ops *ops, const char *hostname, void **cookie)
Jens Axboe132159a2011-09-30 15:01:32 -0600219{
Jens Axboe3f3a4542011-10-10 21:11:09 +0200220 struct fio_client *existing = *cookie;
Jens Axboeb66570d2011-10-01 11:11:35 -0600221 struct fio_client *client;
Jens Axboe132159a2011-09-30 15:01:32 -0600222
Jens Axboe3f3a4542011-10-10 21:11:09 +0200223 if (existing) {
224 /*
225 * We always add our "exec" name as the option, hence 1
226 * means empty.
227 */
228 if (existing->argc == 1)
229 flist_add_tail(&existing->arg_list, &arg_list);
230 else {
231 while (!flist_empty(&arg_list))
232 flist_del_init(arg_list.next);
233 }
234 }
235
Jens Axboeb66570d2011-10-01 11:11:35 -0600236 client = malloc(sizeof(*client));
Jens Axboea37f69b2011-10-01 12:24:21 -0600237 memset(client, 0, sizeof(*client));
Jens Axboe81179ee2011-10-04 12:42:06 +0200238
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200239 INIT_FLIST_HEAD(&client->list);
Jens Axboebebe6392011-10-07 10:00:51 +0200240 INIT_FLIST_HEAD(&client->hash_list);
Jens Axboe3f3a4542011-10-10 21:11:09 +0200241 INIT_FLIST_HEAD(&client->arg_list);
Jens Axboe82c1ed32011-10-10 08:56:18 +0200242 INIT_FLIST_HEAD(&client->eta_list);
Jens Axboe89c17072011-10-11 10:15:51 +0200243 INIT_FLIST_HEAD(&client->cmd_list);
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200244
Jens Axboebebe6392011-10-07 10:00:51 +0200245 if (fio_server_parse_string(hostname, &client->hostname,
246 &client->is_sock, &client->port,
Jens Axboe811826b2011-10-24 09:11:50 +0200247 &client->addr.sin_addr,
248 &client->addr6.sin6_addr,
249 &client->ipv6))
Jens Axboebebe6392011-10-07 10:00:51 +0200250 return -1;
251
Jens Axboea37f69b2011-10-01 12:24:21 -0600252 client->fd = -1;
Jens Axboea5276612012-03-04 15:15:08 +0100253 client->ops = ops;
Jens Axboe5121a9a2012-03-05 13:32:47 +0100254 client->refs = 1;
Jens Axboe81179ee2011-10-04 12:42:06 +0200255
256 __fio_client_add_cmd_option(client, "fio");
257
Jens Axboea37f69b2011-10-01 12:24:21 -0600258 flist_add(&client->list, &client_list);
259 nr_clients++;
Jens Axboebebe6392011-10-07 10:00:51 +0200260 dprint(FD_NET, "client: added <%s>\n", client->hostname);
261 *cookie = client;
262 return 0;
Jens Axboea37f69b2011-10-01 12:24:21 -0600263}
264
Jens Axboe87aa8f12011-10-06 21:24:13 +0200265static int fio_client_connect_ip(struct fio_client *client)
Jens Axboea37f69b2011-10-01 12:24:21 -0600266{
Jens Axboe811826b2011-10-24 09:11:50 +0200267 struct sockaddr *addr;
268 fio_socklen_t socklen;
269 int fd, domain;
Jens Axboe132159a2011-09-30 15:01:32 -0600270
Jens Axboe811826b2011-10-24 09:11:50 +0200271 if (client->ipv6) {
272 client->addr6.sin6_family = AF_INET6;
273 client->addr6.sin6_port = htons(client->port);
274 domain = AF_INET6;
275 addr = (struct sockaddr *) &client->addr6;
276 socklen = sizeof(client->addr6);
277 } else {
278 client->addr.sin_family = AF_INET;
279 client->addr.sin_port = htons(client->port);
280 domain = AF_INET;
281 addr = (struct sockaddr *) &client->addr;
282 socklen = sizeof(client->addr);
283 }
Jens Axboe132159a2011-09-30 15:01:32 -0600284
Jens Axboe811826b2011-10-24 09:11:50 +0200285 fd = socket(domain, SOCK_STREAM, 0);
Jens Axboe132159a2011-09-30 15:01:32 -0600286 if (fd < 0) {
287 log_err("fio: socket: %s\n", strerror(errno));
Jens Axboe87aa8f12011-10-06 21:24:13 +0200288 return -1;
Jens Axboe132159a2011-09-30 15:01:32 -0600289 }
290
Jens Axboe811826b2011-10-24 09:11:50 +0200291 if (connect(fd, addr, socklen) < 0) {
Jens Axboe132159a2011-09-30 15:01:32 -0600292 log_err("fio: connect: %s\n", strerror(errno));
Jens Axboea7de0a12011-10-07 12:55:14 +0200293 log_err("fio: failed to connect to %s:%u\n", client->hostname,
294 client->port);
Jens Axboeb94cba42011-10-06 21:27:10 +0200295 close(fd);
Jens Axboe87aa8f12011-10-06 21:24:13 +0200296 return -1;
Jens Axboe132159a2011-09-30 15:01:32 -0600297 }
298
Jens Axboe87aa8f12011-10-06 21:24:13 +0200299 return fd;
300}
301
302static int fio_client_connect_sock(struct fio_client *client)
303{
304 struct sockaddr_un *addr = &client->addr_un;
305 fio_socklen_t len;
306 int fd;
307
308 memset(addr, 0, sizeof(*addr));
309 addr->sun_family = AF_UNIX;
310 strcpy(addr->sun_path, client->hostname);
311
312 fd = socket(AF_UNIX, SOCK_STREAM, 0);
313 if (fd < 0) {
314 log_err("fio: socket: %s\n", strerror(errno));
315 return -1;
316 }
317
318 len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
319 if (connect(fd, (struct sockaddr *) addr, len) < 0) {
320 log_err("fio: connect; %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200321 close(fd);
Jens Axboe87aa8f12011-10-06 21:24:13 +0200322 return -1;
323 }
324
325 return fd;
326}
327
328static int fio_client_connect(struct fio_client *client)
329{
330 int fd;
331
332 dprint(FD_NET, "client: connect to host %s\n", client->hostname);
333
Jens Axboe87aa8f12011-10-06 21:24:13 +0200334 if (client->is_sock)
335 fd = fio_client_connect_sock(client);
336 else
337 fd = fio_client_connect_ip(client);
338
Jens Axboe89c17072011-10-11 10:15:51 +0200339 dprint(FD_NET, "client: %s connected %d\n", client->hostname, fd);
340
Jens Axboe87aa8f12011-10-06 21:24:13 +0200341 if (fd < 0)
342 return 1;
343
Jens Axboeb66570d2011-10-01 11:11:35 -0600344 client->fd = fd;
Jens Axboebebe6392011-10-07 10:00:51 +0200345 fio_client_add_hash(client);
Jens Axboe81179ee2011-10-04 12:42:06 +0200346 client->state = Client_connected;
Jens Axboe132159a2011-09-30 15:01:32 -0600347 return 0;
348}
349
Jens Axboecc0df002011-10-03 20:53:32 +0200350void fio_clients_terminate(void)
351{
352 struct flist_head *entry;
353 struct fio_client *client;
354
Jens Axboe60efd142011-10-04 13:30:11 +0200355 dprint(FD_NET, "client: terminate clients\n");
356
Jens Axboecc0df002011-10-03 20:53:32 +0200357 flist_for_each(entry, &client_list) {
358 client = flist_entry(entry, struct fio_client, list);
359
Jens Axboe89c17072011-10-11 10:15:51 +0200360 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_QUIT, 0, NULL);
Jens Axboecc0df002011-10-03 20:53:32 +0200361 }
362}
363
364static void sig_int(int sig)
365{
Jens Axboebebe6392011-10-07 10:00:51 +0200366 dprint(FD_NET, "client: got signal %d\n", sig);
Jens Axboecc0df002011-10-03 20:53:32 +0200367 fio_clients_terminate();
368}
369
370static void client_signal_handler(void)
371{
372 struct sigaction act;
373
374 memset(&act, 0, sizeof(act));
375 act.sa_handler = sig_int;
376 act.sa_flags = SA_RESTART;
377 sigaction(SIGINT, &act, NULL);
378
379 memset(&act, 0, sizeof(act));
380 act.sa_handler = sig_int;
381 act.sa_flags = SA_RESTART;
382 sigaction(SIGTERM, &act, NULL);
383}
384
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200385static void probe_client(struct fio_client *client)
386{
Jens Axboe60efd142011-10-04 13:30:11 +0200387 dprint(FD_NET, "client: send probe\n");
388
Jens Axboe89c17072011-10-11 10:15:51 +0200389 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_PROBE, 0, &client->cmd_list);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200390}
391
Jens Axboe81179ee2011-10-04 12:42:06 +0200392static int send_client_cmd_line(struct fio_client *client)
393{
Jens Axboefa2ea802011-10-08 21:07:29 +0200394 struct cmd_single_line_pdu *cslp;
395 struct cmd_line_pdu *clp;
396 unsigned long offset;
Jens Axboe7f868312011-10-10 09:55:21 +0200397 unsigned int *lens;
Jens Axboefa2ea802011-10-08 21:07:29 +0200398 void *pdu;
399 size_t mem;
Jens Axboe81179ee2011-10-04 12:42:06 +0200400 int i, ret;
401
Jens Axboe39e8e012011-10-04 13:46:08 +0200402 dprint(FD_NET, "client: send cmdline %d\n", client->argc);
Jens Axboe60efd142011-10-04 13:30:11 +0200403
Jens Axboe7f868312011-10-10 09:55:21 +0200404 lens = malloc(client->argc * sizeof(unsigned int));
405
Jens Axboefa2ea802011-10-08 21:07:29 +0200406 /*
407 * Find out how much mem we need
408 */
Jens Axboe7f868312011-10-10 09:55:21 +0200409 for (i = 0, mem = 0; i < client->argc; i++) {
410 lens[i] = strlen(client->argv[i]) + 1;
411 mem += lens[i];
412 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200413
Jens Axboefa2ea802011-10-08 21:07:29 +0200414 /*
415 * We need one cmd_line_pdu, and argc number of cmd_single_line_pdu
416 */
417 mem += sizeof(*clp) + (client->argc * sizeof(*cslp));
418
419 pdu = malloc(mem);
420 clp = pdu;
421 offset = sizeof(*clp);
422
423 for (i = 0; i < client->argc; i++) {
Jens Axboe7f868312011-10-10 09:55:21 +0200424 uint16_t arg_len = lens[i];
Jens Axboefa2ea802011-10-08 21:07:29 +0200425
426 cslp = pdu + offset;
427 strcpy((char *) cslp->text, client->argv[i]);
428 cslp->len = cpu_to_le16(arg_len);
429 offset += sizeof(*cslp) + arg_len;
430 }
431
Jens Axboe7f868312011-10-10 09:55:21 +0200432 free(lens);
Jens Axboefa2ea802011-10-08 21:07:29 +0200433 clp->lines = cpu_to_le16(client->argc);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200434 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOBLINE, pdu, mem, 0);
Jens Axboe81179ee2011-10-04 12:42:06 +0200435 free(pdu);
436 return ret;
437}
438
Jens Axboea37f69b2011-10-01 12:24:21 -0600439int fio_clients_connect(void)
440{
441 struct fio_client *client;
442 struct flist_head *entry, *tmp;
443 int ret;
444
Bruce Cran93bcfd22012-02-20 20:18:19 +0100445#ifdef WIN32
446 WSADATA wsd;
447 WSAStartup(MAKEWORD(2,2), &wsd);
448#endif
449
Jens Axboe60efd142011-10-04 13:30:11 +0200450 dprint(FD_NET, "client: connect all\n");
451
Jens Axboecc0df002011-10-03 20:53:32 +0200452 client_signal_handler();
453
Jens Axboea37f69b2011-10-01 12:24:21 -0600454 flist_for_each_safe(entry, tmp, &client_list) {
455 client = flist_entry(entry, struct fio_client, list);
456
457 ret = fio_client_connect(client);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200458 if (ret) {
Jens Axboea37f69b2011-10-01 12:24:21 -0600459 remove_client(client);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200460 continue;
461 }
462
463 probe_client(client);
Jens Axboe81179ee2011-10-04 12:42:06 +0200464
465 if (client->argc > 1)
466 send_client_cmd_line(client);
Jens Axboea37f69b2011-10-01 12:24:21 -0600467 }
468
469 return !nr_clients;
470}
471
Jens Axboe132159a2011-09-30 15:01:32 -0600472/*
473 * Send file contents to server backend. We could use sendfile(), but to remain
474 * more portable lets just read/write the darn thing.
475 */
Jens Axboea37f69b2011-10-01 12:24:21 -0600476static int fio_client_send_ini(struct fio_client *client, const char *filename)
Jens Axboe132159a2011-09-30 15:01:32 -0600477{
478 struct stat sb;
479 char *p, *buf;
480 off_t len;
481 int fd, ret;
482
Jens Axboe46c48f12011-10-01 12:50:51 -0600483 dprint(FD_NET, "send ini %s to %s\n", filename, client->hostname);
484
Jens Axboe132159a2011-09-30 15:01:32 -0600485 fd = open(filename, O_RDONLY);
486 if (fd < 0) {
Jens Axboee951bdc2011-10-05 21:58:45 +0200487 log_err("fio: job file <%s> open: %s\n", filename, strerror(errno));
Jens Axboe132159a2011-09-30 15:01:32 -0600488 return 1;
489 }
490
491 if (fstat(fd, &sb) < 0) {
492 log_err("fio: job file stat: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200493 close(fd);
Jens Axboe132159a2011-09-30 15:01:32 -0600494 return 1;
495 }
496
497 buf = malloc(sb.st_size);
498
499 len = sb.st_size;
500 p = buf;
501 do {
502 ret = read(fd, p, len);
503 if (ret > 0) {
504 len -= ret;
505 if (!len)
506 break;
507 p += ret;
508 continue;
509 } else if (!ret)
510 break;
511 else if (errno == EAGAIN || errno == EINTR)
512 continue;
513 } while (1);
514
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200515 if (len) {
516 log_err("fio: failed reading job file %s\n", filename);
Jens Axboeb94cba42011-10-06 21:27:10 +0200517 close(fd);
Jens Axboec524ef72011-10-07 12:23:34 +0200518 free(buf);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200519 return 1;
520 }
521
Jens Axboec2cb6862012-02-23 20:56:12 +0100522 client->sent_job = 1;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200523 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, buf, sb.st_size, 0);
Jens Axboe132159a2011-09-30 15:01:32 -0600524 free(buf);
Jens Axboeb94cba42011-10-06 21:27:10 +0200525 close(fd);
Jens Axboe132159a2011-09-30 15:01:32 -0600526 return ret;
527}
Jens Axboe37db14f2011-09-30 17:00:42 -0600528
Jens Axboea37f69b2011-10-01 12:24:21 -0600529int fio_clients_send_ini(const char *filename)
530{
531 struct fio_client *client;
532 struct flist_head *entry, *tmp;
533
534 flist_for_each_safe(entry, tmp, &client_list) {
535 client = flist_entry(entry, struct fio_client, list);
536
537 if (fio_client_send_ini(client, filename))
538 remove_client(client);
Jens Axboec2cb6862012-02-23 20:56:12 +0100539
540 client->sent_job = 1;
Jens Axboea37f69b2011-10-01 12:24:21 -0600541 }
542
543 return !nr_clients;
544}
545
Jens Axboea64e88d2011-10-03 14:20:01 +0200546static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
547{
548 dst->max_val = le64_to_cpu(src->max_val);
549 dst->min_val = le64_to_cpu(src->min_val);
550 dst->samples = le64_to_cpu(src->samples);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200551
552 /*
553 * Floats arrive as IEEE 754 encoded uint64_t, convert back to double
554 */
555 dst->mean.u.f = fio_uint64_to_double(le64_to_cpu(dst->mean.u.i));
556 dst->S.u.f = fio_uint64_to_double(le64_to_cpu(dst->S.u.i));
Jens Axboea64e88d2011-10-03 14:20:01 +0200557}
558
559static void convert_ts(struct thread_stat *dst, struct thread_stat *src)
560{
561 int i, j;
562
563 dst->error = le32_to_cpu(src->error);
564 dst->groupid = le32_to_cpu(src->groupid);
565 dst->pid = le32_to_cpu(src->pid);
566 dst->members = le32_to_cpu(src->members);
567
568 for (i = 0; i < 2; i++) {
569 convert_io_stat(&dst->clat_stat[i], &src->clat_stat[i]);
570 convert_io_stat(&dst->slat_stat[i], &src->slat_stat[i]);
571 convert_io_stat(&dst->lat_stat[i], &src->lat_stat[i]);
572 convert_io_stat(&dst->bw_stat[i], &src->bw_stat[i]);
573 }
574
575 dst->usr_time = le64_to_cpu(src->usr_time);
576 dst->sys_time = le64_to_cpu(src->sys_time);
577 dst->ctx = le64_to_cpu(src->ctx);
578 dst->minf = le64_to_cpu(src->minf);
579 dst->majf = le64_to_cpu(src->majf);
580 dst->clat_percentiles = le64_to_cpu(src->clat_percentiles);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200581
582 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
583 fio_fp64_t *fps = &src->percentile_list[i];
584 fio_fp64_t *fpd = &dst->percentile_list[i];
585
586 fpd->u.f = fio_uint64_to_double(le64_to_cpu(fps->u.i));
587 }
Jens Axboea64e88d2011-10-03 14:20:01 +0200588
589 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
590 dst->io_u_map[i] = le32_to_cpu(src->io_u_map[i]);
591 dst->io_u_submit[i] = le32_to_cpu(src->io_u_submit[i]);
592 dst->io_u_complete[i] = le32_to_cpu(src->io_u_complete[i]);
593 }
594
595 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
596 dst->io_u_lat_u[i] = le32_to_cpu(src->io_u_lat_u[i]);
597 dst->io_u_lat_m[i] = le32_to_cpu(src->io_u_lat_m[i]);
598 }
599
600 for (i = 0; i < 2; i++)
601 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
602 dst->io_u_plat[i][j] = le32_to_cpu(src->io_u_plat[i][j]);
603
604 for (i = 0; i < 3; i++) {
605 dst->total_io_u[i] = le64_to_cpu(src->total_io_u[i]);
Jens Axboe93eee042011-10-03 21:08:48 +0200606 dst->short_io_u[i] = le64_to_cpu(src->short_io_u[i]);
Jens Axboea64e88d2011-10-03 14:20:01 +0200607 }
608
609 dst->total_submit = le64_to_cpu(src->total_submit);
610 dst->total_complete = le64_to_cpu(src->total_complete);
611
612 for (i = 0; i < 2; i++) {
613 dst->io_bytes[i] = le64_to_cpu(src->io_bytes[i]);
614 dst->runtime[i] = le64_to_cpu(src->runtime[i]);
615 }
616
617 dst->total_run_time = le64_to_cpu(src->total_run_time);
618 dst->continue_on_error = le16_to_cpu(src->continue_on_error);
619 dst->total_err_count = le64_to_cpu(src->total_err_count);
Jens Axboeddcc0b62011-10-03 14:45:27 +0200620 dst->first_error = le32_to_cpu(src->first_error);
621 dst->kb_base = le32_to_cpu(src->kb_base);
Jens Axboea64e88d2011-10-03 14:20:01 +0200622}
623
624static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
625{
626 int i;
627
628 for (i = 0; i < 2; i++) {
629 dst->max_run[i] = le64_to_cpu(src->max_run[i]);
630 dst->min_run[i] = le64_to_cpu(src->min_run[i]);
631 dst->max_bw[i] = le64_to_cpu(src->max_bw[i]);
632 dst->min_bw[i] = le64_to_cpu(src->min_bw[i]);
633 dst->io_kb[i] = le64_to_cpu(src->io_kb[i]);
634 dst->agg[i] = le64_to_cpu(src->agg[i]);
635 }
636
637 dst->kb_base = le32_to_cpu(src->kb_base);
638 dst->groupid = le32_to_cpu(src->groupid);
639}
640
Jens Axboe89e5fad2012-03-05 09:21:12 +0100641static void handle_ts(struct fio_client *client, struct fio_net_cmd *cmd)
Jens Axboea64e88d2011-10-03 14:20:01 +0200642{
643 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
644
Jens Axboea64e88d2011-10-03 14:20:01 +0200645 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
Jens Axboe89e5fad2012-03-05 09:21:12 +0100662static void handle_gs(struct fio_client *client, struct fio_net_cmd *cmd)
Jens Axboea64e88d2011-10-03 14:20:01 +0200663{
664 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
665
Jens Axboea64e88d2011-10-03 14:20:01 +0200666 show_group_stats(gs);
667}
668
Jens Axboe3bf236c2012-03-03 20:35:50 +0100669static void handle_text(struct fio_client *client, struct fio_net_cmd *cmd)
670{
671 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
672 const char *buf = (const char *) pdu->buf;
673 const char *name;
674 int fio_unused ret;
675
676 name = client->name ? client->name : client->hostname;
677
678 if (!client->skip_newline)
679 fprintf(f_out, "<%s> ", name);
680 ret = fwrite(buf, pdu->buf_len, 1, f_out);
681 fflush(f_out);
682 client->skip_newline = strchr(buf, '\n') == NULL;
683}
684
Jens Axboed09a64a2011-10-13 11:38:56 +0200685static void convert_agg(struct disk_util_agg *agg)
686{
687 int i;
688
689 for (i = 0; i < 2; i++) {
690 agg->ios[i] = le32_to_cpu(agg->ios[i]);
691 agg->merges[i] = le32_to_cpu(agg->merges[i]);
692 agg->sectors[i] = le64_to_cpu(agg->sectors[i]);
693 agg->ticks[i] = le32_to_cpu(agg->ticks[i]);
694 }
695
696 agg->io_ticks = le32_to_cpu(agg->io_ticks);
697 agg->time_in_queue = le32_to_cpu(agg->time_in_queue);
698 agg->slavecount = le32_to_cpu(agg->slavecount);
Anton Blanchard823ba542011-11-07 14:16:26 +0100699 agg->max_util.u.f = fio_uint64_to_double(__le64_to_cpu(agg->max_util.u.i));
Jens Axboed09a64a2011-10-13 11:38:56 +0200700}
701
702static void convert_dus(struct disk_util_stat *dus)
703{
704 int i;
705
706 for (i = 0; i < 2; i++) {
707 dus->ios[i] = le32_to_cpu(dus->ios[i]);
708 dus->merges[i] = le32_to_cpu(dus->merges[i]);
709 dus->sectors[i] = le64_to_cpu(dus->sectors[i]);
710 dus->ticks[i] = le32_to_cpu(dus->ticks[i]);
711 }
712
713 dus->io_ticks = le32_to_cpu(dus->io_ticks);
714 dus->time_in_queue = le32_to_cpu(dus->time_in_queue);
715 dus->msec = le64_to_cpu(dus->msec);
716}
717
718static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd)
719{
720 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
721
Jens Axboed09a64a2011-10-13 11:38:56 +0200722 if (!client->disk_stats_shown) {
723 client->disk_stats_shown = 1;
724 log_info("\nDisk stats (read/write):\n");
725 }
726
Jens Axboef2f788d2011-10-13 14:03:52 +0200727 print_disk_util(&du->dus, &du->agg, terse_output);
Jens Axboed09a64a2011-10-13 11:38:56 +0200728}
729
Jens Axboe3bf236c2012-03-03 20:35:50 +0100730static void convert_jobs_eta(struct jobs_eta *je)
Jens Axboecf451d12011-10-03 16:48:30 +0200731{
Jens Axboecf451d12011-10-03 16:48:30 +0200732 int i;
733
734 je->nr_running = le32_to_cpu(je->nr_running);
735 je->nr_ramp = le32_to_cpu(je->nr_ramp);
736 je->nr_pending = le32_to_cpu(je->nr_pending);
737 je->files_open = le32_to_cpu(je->files_open);
Jens Axboecf451d12011-10-03 16:48:30 +0200738
739 for (i = 0; i < 2; i++) {
Jens Axboe3e47bd22012-02-29 13:45:02 +0100740 je->m_rate[i] = le32_to_cpu(je->m_rate[i]);
741 je->t_rate[i] = le32_to_cpu(je->t_rate[i]);
742 je->m_iops[i] = le32_to_cpu(je->m_iops[i]);
743 je->t_iops[i] = le32_to_cpu(je->t_iops[i]);
Jens Axboecf451d12011-10-03 16:48:30 +0200744 je->rate[i] = le32_to_cpu(je->rate[i]);
745 je->iops[i] = le32_to_cpu(je->iops[i]);
746 }
747
Jens Axboeb51eedb2011-10-09 12:13:39 +0200748 je->elapsed_sec = le64_to_cpu(je->elapsed_sec);
Jens Axboecf451d12011-10-03 16:48:30 +0200749 je->eta_sec = le64_to_cpu(je->eta_sec);
Jens Axboe8c621fb2012-03-08 12:30:48 +0100750 je->nr_threads = le32_to_cpu(je->nr_threads);
Jens Axboe48fbb462011-10-09 12:19:08 +0200751}
Jens Axboecf451d12011-10-03 16:48:30 +0200752
Jens Axboe3e47bd22012-02-29 13:45:02 +0100753void fio_client_sum_jobs_eta(struct jobs_eta *dst, struct jobs_eta *je)
Jens Axboe48fbb462011-10-09 12:19:08 +0200754{
Jens Axboe48fbb462011-10-09 12:19:08 +0200755 int i;
756
757 dst->nr_running += je->nr_running;
758 dst->nr_ramp += je->nr_ramp;
759 dst->nr_pending += je->nr_pending;
760 dst->files_open += je->files_open;
Jens Axboe48fbb462011-10-09 12:19:08 +0200761
762 for (i = 0; i < 2; i++) {
Jens Axboe3e47bd22012-02-29 13:45:02 +0100763 dst->m_rate[i] += je->m_rate[i];
764 dst->t_rate[i] += je->t_rate[i];
765 dst->m_iops[i] += je->m_iops[i];
766 dst->t_iops[i] += je->t_iops[i];
Jens Axboe48fbb462011-10-09 12:19:08 +0200767 dst->rate[i] += je->rate[i];
768 dst->iops[i] += je->iops[i];
769 }
770
771 dst->elapsed_sec += je->elapsed_sec;
772
773 if (je->eta_sec > dst->eta_sec)
774 dst->eta_sec = je->eta_sec;
Jens Axboe8c621fb2012-03-08 12:30:48 +0100775
776 dst->nr_threads += je->nr_threads;
777 /* we need to handle je->run_str too ... */
Jens Axboe48fbb462011-10-09 12:19:08 +0200778}
779
Jens Axboea5276612012-03-04 15:15:08 +0100780void fio_client_dec_jobs_eta(struct client_eta *eta, client_eta_op eta_fn)
Jens Axboe82c1ed32011-10-10 08:56:18 +0200781{
782 if (!--eta->pending) {
Jens Axboea5276612012-03-04 15:15:08 +0100783 eta_fn(&eta->eta);
Jens Axboe82c1ed32011-10-10 08:56:18 +0200784 free(eta);
785 }
786}
787
Jens Axboe89c17072011-10-11 10:15:51 +0200788static void remove_reply_cmd(struct fio_client *client, struct fio_net_cmd *cmd)
789{
790 struct fio_net_int_cmd *icmd = NULL;
791 struct flist_head *entry;
792
793 flist_for_each(entry, &client->cmd_list) {
794 icmd = flist_entry(entry, struct fio_net_int_cmd, list);
795
Jens Axboedf380932011-10-11 14:25:08 +0200796 if (cmd->tag == (uintptr_t) icmd)
Jens Axboe89c17072011-10-11 10:15:51 +0200797 break;
798
799 icmd = NULL;
800 }
801
802 if (!icmd) {
803 log_err("fio: client: unable to find matching tag\n");
804 return;
805 }
806
807 flist_del(&icmd->list);
808 cmd->tag = icmd->saved_tag;
809 free(icmd);
810}
811
Jens Axboe82c1ed32011-10-10 08:56:18 +0200812static void handle_eta(struct fio_client *client, struct fio_net_cmd *cmd)
Jens Axboe48fbb462011-10-09 12:19:08 +0200813{
814 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
Jens Axboedf380932011-10-11 14:25:08 +0200815 struct client_eta *eta = (struct client_eta *) (uintptr_t) cmd->tag;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200816
817 dprint(FD_NET, "client: got eta tag %p, %d\n", eta, eta->pending);
Jens Axboe48fbb462011-10-09 12:19:08 +0200818
Jens Axboef77d2672011-10-10 14:36:07 +0200819 assert(client->eta_in_flight == eta);
820
821 client->eta_in_flight = NULL;
Jens Axboe82c1ed32011-10-10 08:56:18 +0200822 flist_del_init(&client->eta_list);
823
Jens Axboe3e47bd22012-02-29 13:45:02 +0100824 fio_client_sum_jobs_eta(&eta->eta, je);
Jens Axboea5276612012-03-04 15:15:08 +0100825 fio_client_dec_jobs_eta(eta, client->ops->eta);
Jens Axboecf451d12011-10-03 16:48:30 +0200826}
827
Jens Axboeb5296dd2011-10-07 16:35:56 +0200828static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd)
Jens Axboe2e03b4b2011-10-04 09:00:40 +0200829{
830 struct cmd_probe_pdu *probe = (struct cmd_probe_pdu *) cmd->payload;
Jens Axboed2333352011-10-11 15:07:23 +0200831 const char *os, *arch;
832 char bit[16];
Jens Axboe2e03b4b2011-10-04 09:00:40 +0200833
Jens Axboecca84642011-10-07 12:47:57 +0200834 os = fio_get_os_string(probe->os);
835 if (!os)
836 os = "unknown";
837
838 arch = fio_get_arch_string(probe->arch);
839 if (!arch)
840 os = "unknown";
841
Jens Axboed2333352011-10-11 15:07:23 +0200842 sprintf(bit, "%d-bit", probe->bpp * 8);
Jens Axboe38fdef22011-10-11 14:30:06 +0200843
844 log_info("hostname=%s, be=%u, %s, os=%s, arch=%s, fio=%u.%u.%u\n",
845 probe->hostname, probe->bigendian, bit, os, arch,
846 probe->fio_major, probe->fio_minor, probe->fio_patch);
Jens Axboeb5296dd2011-10-07 16:35:56 +0200847
848 if (!client->name)
849 client->name = strdup((char *) probe->hostname);
Jens Axboe2e03b4b2011-10-04 09:00:40 +0200850}
851
Jens Axboe11e950b2011-10-16 21:34:14 +0200852static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd)
853{
854 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
855
856 client->state = Client_started;
857 client->jobs = le32_to_cpu(pdu->jobs);
858}
859
860static void handle_stop(struct fio_client *client, struct fio_net_cmd *cmd)
861{
Jens Axboe498c92c2011-10-17 09:14:42 +0200862 if (client->error)
863 log_info("client <%s>: exited with error %d\n", client->hostname, client->error);
Jens Axboe11e950b2011-10-16 21:34:14 +0200864}
865
Jens Axboe6b79c802012-03-08 10:51:36 +0100866static void convert_stop(struct fio_net_cmd *cmd)
867{
868 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
869
870 pdu->error = le32_to_cpu(pdu->error);
871}
872
Jens Axboe084d1c62012-03-03 20:28:07 +0100873static void convert_text(struct fio_net_cmd *cmd)
874{
875 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
876
877 pdu->level = le32_to_cpu(pdu->level);
878 pdu->buf_len = le32_to_cpu(pdu->buf_len);
879 pdu->log_sec = le64_to_cpu(pdu->log_sec);
880 pdu->log_usec = le64_to_cpu(pdu->log_usec);
881}
882
Jens Axboea5276612012-03-04 15:15:08 +0100883int fio_handle_client(struct fio_client *client)
Jens Axboe37db14f2011-09-30 17:00:42 -0600884{
Jens Axboea5276612012-03-04 15:15:08 +0100885 struct client_ops *ops = client->ops;
Jens Axboe37db14f2011-09-30 17:00:42 -0600886 struct fio_net_cmd *cmd;
887
Jens Axboe60efd142011-10-04 13:30:11 +0200888 dprint(FD_NET, "client: handle %s\n", client->hostname);
889
Jens Axboee951bdc2011-10-05 21:58:45 +0200890 cmd = fio_net_recv_cmd(client->fd);
891 if (!cmd)
892 return 0;
Jens Axboec2c94582011-10-05 20:31:30 +0200893
Jens Axboe89c17072011-10-11 10:15:51 +0200894 dprint(FD_NET, "client: got cmd op %s from %s\n",
895 fio_server_op(cmd->opcode), client->hostname);
Jens Axboe46c48f12011-10-01 12:50:51 -0600896
Jens Axboee951bdc2011-10-05 21:58:45 +0200897 switch (cmd->opcode) {
898 case FIO_NET_CMD_QUIT:
Jens Axboe3ec62ec2012-03-01 12:01:29 +0100899 if (ops->quit)
900 ops->quit(client);
Jens Axboee951bdc2011-10-05 21:58:45 +0200901 remove_client(client);
902 free(cmd);
903 break;
Jens Axboe084d1c62012-03-03 20:28:07 +0100904 case FIO_NET_CMD_TEXT:
905 convert_text(cmd);
906 ops->text_op(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +0200907 free(cmd);
908 break;
Jens Axboe3bf236c2012-03-03 20:35:50 +0100909 case FIO_NET_CMD_DU: {
910 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
911
912 convert_dus(&du->dus);
913 convert_agg(&du->agg);
914
Stephen M. Camerondd366722012-02-24 08:17:30 +0100915 ops->disk_util(client, cmd);
Jens Axboed09a64a2011-10-13 11:38:56 +0200916 free(cmd);
917 break;
Jens Axboe3bf236c2012-03-03 20:35:50 +0100918 }
919 case FIO_NET_CMD_TS: {
920 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
921
922 convert_ts(&p->ts, &p->ts);
923 convert_gs(&p->rs, &p->rs);
924
Jens Axboe89e5fad2012-03-05 09:21:12 +0100925 ops->thread_status(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +0200926 free(cmd);
927 break;
Jens Axboe3bf236c2012-03-03 20:35:50 +0100928 }
929 case FIO_NET_CMD_GS: {
930 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
931
932 convert_gs(gs, gs);
933
Jens Axboe89e5fad2012-03-05 09:21:12 +0100934 ops->group_stats(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +0200935 free(cmd);
936 break;
Jens Axboe3bf236c2012-03-03 20:35:50 +0100937 }
938 case FIO_NET_CMD_ETA: {
939 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
940
Jens Axboe89c17072011-10-11 10:15:51 +0200941 remove_reply_cmd(client, cmd);
Jens Axboe3bf236c2012-03-03 20:35:50 +0100942 convert_jobs_eta(je);
Jens Axboea5276612012-03-04 15:15:08 +0100943 handle_eta(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +0200944 free(cmd);
945 break;
Jens Axboe3bf236c2012-03-03 20:35:50 +0100946 }
Jens Axboee951bdc2011-10-05 21:58:45 +0200947 case FIO_NET_CMD_PROBE:
Jens Axboe89c17072011-10-11 10:15:51 +0200948 remove_reply_cmd(client, cmd);
Stephen M. Camerondd366722012-02-24 08:17:30 +0100949 ops->probe(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +0200950 free(cmd);
951 break;
Jens Axboe01be0382011-10-15 14:43:41 +0200952 case FIO_NET_CMD_RUN:
953 client->state = Client_running;
954 free(cmd);
955 break;
Jens Axboee951bdc2011-10-05 21:58:45 +0200956 case FIO_NET_CMD_START:
Jens Axboe11e950b2011-10-16 21:34:14 +0200957 handle_start(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +0200958 free(cmd);
959 break;
Jens Axboe6b79c802012-03-08 10:51:36 +0100960 case FIO_NET_CMD_STOP: {
961 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
962
963 convert_stop(cmd);
964 client->state = Client_stopped;
965 client->error = pdu->error;
966 ops->stop(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +0200967 free(cmd);
968 break;
Jens Axboe6b79c802012-03-08 10:51:36 +0100969 }
Jens Axboe807f9972012-03-02 10:25:24 +0100970 case FIO_NET_CMD_ADD_JOB:
971 if (ops->add_job)
972 ops->add_job(client, cmd);
973 free(cmd);
974 break;
Jens Axboee951bdc2011-10-05 21:58:45 +0200975 default:
Jens Axboe89c17072011-10-11 10:15:51 +0200976 log_err("fio: unknown client op: %s\n", fio_server_op(cmd->opcode));
Jens Axboee951bdc2011-10-05 21:58:45 +0200977 free(cmd);
978 break;
Jens Axboe37db14f2011-09-30 17:00:42 -0600979 }
980
Jens Axboee951bdc2011-10-05 21:58:45 +0200981 return 1;
Jens Axboe37db14f2011-09-30 17:00:42 -0600982}
Jens Axboeb66570d2011-10-01 11:11:35 -0600983
Jens Axboea5276612012-03-04 15:15:08 +0100984static void request_client_etas(struct client_ops *ops)
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200985{
986 struct fio_client *client;
987 struct flist_head *entry;
988 struct client_eta *eta;
Jens Axboe82c1ed32011-10-10 08:56:18 +0200989 int skipped = 0;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200990
991 dprint(FD_NET, "client: request eta (%d)\n", nr_clients);
992
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200993 eta = malloc(sizeof(*eta));
994 memset(&eta->eta, 0, sizeof(eta->eta));
995 eta->pending = nr_clients;
996
997 flist_for_each(entry, &client_list) {
998 client = flist_entry(entry, struct fio_client, list);
999
Jens Axboe82c1ed32011-10-10 08:56:18 +02001000 if (!flist_empty(&client->eta_list)) {
1001 skipped++;
1002 continue;
1003 }
Jens Axboe01be0382011-10-15 14:43:41 +02001004 if (client->state != Client_running)
1005 continue;
Jens Axboe82c1ed32011-10-10 08:56:18 +02001006
Jens Axboef77d2672011-10-10 14:36:07 +02001007 assert(!client->eta_in_flight);
Jens Axboe82c1ed32011-10-10 08:56:18 +02001008 flist_add_tail(&client->eta_list, &eta_list);
Jens Axboef77d2672011-10-10 14:36:07 +02001009 client->eta_in_flight = eta;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001010 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_SEND_ETA,
Jens Axboedf380932011-10-11 14:25:08 +02001011 (uintptr_t) eta, &client->cmd_list);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001012 }
1013
Jens Axboe82c1ed32011-10-10 08:56:18 +02001014 while (skipped--)
Jens Axboea5276612012-03-04 15:15:08 +01001015 fio_client_dec_jobs_eta(eta, ops->eta);
Jens Axboe82c1ed32011-10-10 08:56:18 +02001016
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001017 dprint(FD_NET, "client: requested eta tag %p\n", eta);
1018}
1019
Jens Axboe89c17072011-10-11 10:15:51 +02001020static int client_check_cmd_timeout(struct fio_client *client,
1021 struct timeval *now)
1022{
1023 struct fio_net_int_cmd *cmd;
1024 struct flist_head *entry, *tmp;
1025 int ret = 0;
1026
1027 flist_for_each_safe(entry, tmp, &client->cmd_list) {
1028 cmd = flist_entry(entry, struct fio_net_int_cmd, list);
1029
1030 if (mtime_since(&cmd->tv, now) < FIO_NET_CLIENT_TIMEOUT)
1031 continue;
1032
1033 log_err("fio: client %s, timeout on cmd %s\n", client->hostname,
1034 fio_server_op(cmd->cmd.opcode));
1035 flist_del(&cmd->list);
1036 free(cmd);
1037 ret = 1;
1038 }
1039
1040 return flist_empty(&client->cmd_list) && ret;
1041}
1042
Jens Axboea5276612012-03-04 15:15:08 +01001043static int fio_check_clients_timed_out(void)
Jens Axboe89c17072011-10-11 10:15:51 +02001044{
1045 struct fio_client *client;
1046 struct flist_head *entry, *tmp;
1047 struct timeval tv;
1048 int ret = 0;
1049
1050 gettimeofday(&tv, NULL);
1051
1052 flist_for_each_safe(entry, tmp, &client_list) {
1053 client = flist_entry(entry, struct fio_client, list);
1054
1055 if (flist_empty(&client->cmd_list))
1056 continue;
1057
1058 if (!client_check_cmd_timeout(client, &tv))
1059 continue;
1060
Jens Axboea5276612012-03-04 15:15:08 +01001061 if (client->ops->timed_out)
1062 client->ops->timed_out(client);
Jens Axboeed727a42012-03-02 12:14:40 +01001063 else
1064 log_err("fio: client %s timed out\n", client->hostname);
1065
Jens Axboe89c17072011-10-11 10:15:51 +02001066 remove_client(client);
1067 ret = 1;
1068 }
1069
1070 return ret;
1071}
1072
Stephen M. Camerondd366722012-02-24 08:17:30 +01001073int fio_handle_clients(struct client_ops *ops)
Jens Axboeb66570d2011-10-01 11:11:35 -06001074{
Jens Axboeb66570d2011-10-01 11:11:35 -06001075 struct pollfd *pfds;
Jens Axboe498c92c2011-10-17 09:14:42 +02001076 int i, ret = 0, retval = 0;
Jens Axboeb66570d2011-10-01 11:11:35 -06001077
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001078 gettimeofday(&eta_tv, NULL);
1079
Jens Axboeb66570d2011-10-01 11:11:35 -06001080 pfds = malloc(nr_clients * sizeof(struct pollfd));
1081
Jens Axboe37f0c1a2011-10-11 14:08:33 +02001082 sum_stat_clients = nr_clients;
1083 init_thread_stat(&client_ts);
1084 init_group_run_stat(&client_gs);
1085
Jens Axboeb66570d2011-10-01 11:11:35 -06001086 while (!exit_backend && nr_clients) {
Jens Axboec2cb6862012-02-23 20:56:12 +01001087 struct flist_head *entry, *tmp;
1088 struct fio_client *client;
1089
Jens Axboe82a4be12011-10-01 12:40:32 -06001090 i = 0;
Jens Axboec2cb6862012-02-23 20:56:12 +01001091 flist_for_each_safe(entry, tmp, &client_list) {
Jens Axboe82a4be12011-10-01 12:40:32 -06001092 client = flist_entry(entry, struct fio_client, list);
1093
Jens Axboea5276612012-03-04 15:15:08 +01001094 if (!client->sent_job && !client->ops->stay_connected &&
Jens Axboec2cb6862012-02-23 20:56:12 +01001095 flist_empty(&client->cmd_list)) {
1096 remove_client(client);
1097 continue;
1098 }
1099
Jens Axboe82a4be12011-10-01 12:40:32 -06001100 pfds[i].fd = client->fd;
1101 pfds[i].events = POLLIN;
1102 i++;
1103 }
1104
Jens Axboec2cb6862012-02-23 20:56:12 +01001105 if (!nr_clients)
1106 break;
1107
Jens Axboe82a4be12011-10-01 12:40:32 -06001108 assert(i == nr_clients);
1109
Jens Axboe5c2857f2011-10-04 13:14:32 +02001110 do {
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001111 struct timeval tv;
1112
1113 gettimeofday(&tv, NULL);
1114 if (mtime_since(&eta_tv, &tv) >= 900) {
Jens Axboea5276612012-03-04 15:15:08 +01001115 request_client_etas(ops);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001116 memcpy(&eta_tv, &tv, sizeof(tv));
Jens Axboe89c17072011-10-11 10:15:51 +02001117
Jens Axboea5276612012-03-04 15:15:08 +01001118 if (fio_check_clients_timed_out())
Jens Axboe89c17072011-10-11 10:15:51 +02001119 break;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001120 }
1121
Jens Axboe5c2857f2011-10-04 13:14:32 +02001122 ret = poll(pfds, nr_clients, 100);
1123 if (ret < 0) {
1124 if (errno == EINTR)
1125 continue;
1126 log_err("fio: poll clients: %s\n", strerror(errno));
1127 break;
1128 } else if (!ret)
Jens Axboeb66570d2011-10-01 11:11:35 -06001129 continue;
Jens Axboe5c2857f2011-10-04 13:14:32 +02001130 } while (ret <= 0);
Jens Axboeb66570d2011-10-01 11:11:35 -06001131
1132 for (i = 0; i < nr_clients; i++) {
1133 if (!(pfds[i].revents & POLLIN))
1134 continue;
1135
1136 client = find_client_by_fd(pfds[i].fd);
1137 if (!client) {
Jens Axboe3c5f57e2011-10-06 12:37:50 +02001138 log_err("fio: unknown client fd %d\n", pfds[i].fd);
Jens Axboeb66570d2011-10-01 11:11:35 -06001139 continue;
1140 }
Jens Axboea5276612012-03-04 15:15:08 +01001141 if (!fio_handle_client(client)) {
Jens Axboe28d3ab02011-10-05 20:45:37 +02001142 log_info("client: host=%s disconnected\n",
1143 client->hostname);
1144 remove_client(client);
Jens Axboe498c92c2011-10-17 09:14:42 +02001145 retval = 1;
Jens Axboe38990762011-10-17 13:31:33 +02001146 } else if (client->error)
Jens Axboe498c92c2011-10-17 09:14:42 +02001147 retval = 1;
Jens Axboe5121a9a2012-03-05 13:32:47 +01001148 put_client(client);
Jens Axboeb66570d2011-10-01 11:11:35 -06001149 }
1150 }
1151
1152 free(pfds);
Jens Axboe498c92c2011-10-17 09:14:42 +02001153 return retval;
Jens Axboeb66570d2011-10-01 11:11:35 -06001154}