blob: 84f13aa181d5d109dcb3f28f21c571f97684961a [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);
Jens Axboe85dd01e2012-03-12 14:33:16 +010030static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd);
Stephen M. Camerondd366722012-02-24 08:17:30 +010031
32struct client_ops fio_client_ops = {
Jens Axboe35c0ba72012-03-14 10:56:40 +010033 .text = handle_text,
Jens Axboe0420ba62012-02-29 11:16:52 +010034 .disk_util = handle_du,
35 .thread_status = handle_ts,
36 .group_stats = handle_gs,
Jens Axboe6b79c802012-03-08 10:51:36 +010037 .stop = handle_stop,
Jens Axboe85dd01e2012-03-12 14:33:16 +010038 .start = handle_start,
Jens Axboea5276612012-03-04 15:15:08 +010039 .eta = display_thread_status,
Jens Axboe0420ba62012-02-29 11:16:52 +010040 .probe = handle_probe,
Jens Axboe6433ee02012-03-09 20:10:51 +010041 .eta_msec = FIO_CLIENT_DEF_ETA_MSEC,
Stephen M. Camerondd366722012-02-24 08:17:30 +010042};
43
Jens Axboeaf9c9fb2011-10-09 21:54:10 +020044static struct timeval eta_tv;
Jens Axboe48fbb462011-10-09 12:19:08 +020045
Jens Axboeb66570d2011-10-01 11:11:35 -060046static FLIST_HEAD(client_list);
Jens Axboe82c1ed32011-10-10 08:56:18 +020047static FLIST_HEAD(eta_list);
Jens Axboeb66570d2011-10-01 11:11:35 -060048
Jens Axboe3f3a4542011-10-10 21:11:09 +020049static FLIST_HEAD(arg_list);
50
Jens Axboe3650a3c2012-03-05 14:09:03 +010051struct thread_stat client_ts;
52struct group_run_stats client_gs;
53int sum_stat_clients;
54
Jens Axboe37f0c1a2011-10-11 14:08:33 +020055static int sum_stat_nr;
56
Jens Axboe3c5f57e2011-10-06 12:37:50 +020057#define FIO_CLIENT_HASH_BITS 7
58#define FIO_CLIENT_HASH_SZ (1 << FIO_CLIENT_HASH_BITS)
59#define FIO_CLIENT_HASH_MASK (FIO_CLIENT_HASH_SZ - 1)
Jens Axboebebe6392011-10-07 10:00:51 +020060static struct flist_head client_hash[FIO_CLIENT_HASH_SZ];
Jens Axboe3c5f57e2011-10-06 12:37:50 +020061
Jens Axboebebe6392011-10-07 10:00:51 +020062static void fio_client_add_hash(struct fio_client *client)
Jens Axboe3c5f57e2011-10-06 12:37:50 +020063{
64 int bucket = hash_long(client->fd, FIO_CLIENT_HASH_BITS);
65
66 bucket &= FIO_CLIENT_HASH_MASK;
Jens Axboebebe6392011-10-07 10:00:51 +020067 flist_add(&client->hash_list, &client_hash[bucket]);
Jens Axboe3c5f57e2011-10-06 12:37:50 +020068}
69
Jens Axboebebe6392011-10-07 10:00:51 +020070static void fio_client_remove_hash(struct fio_client *client)
Jens Axboe3c5f57e2011-10-06 12:37:50 +020071{
Jens Axboebebe6392011-10-07 10:00:51 +020072 if (!flist_empty(&client->hash_list))
73 flist_del_init(&client->hash_list);
Jens Axboe3c5f57e2011-10-06 12:37:50 +020074}
75
76static void fio_init fio_client_hash_init(void)
77{
78 int i;
79
Jens Axboebebe6392011-10-07 10:00:51 +020080 for (i = 0; i < FIO_CLIENT_HASH_SZ; i++)
81 INIT_FLIST_HEAD(&client_hash[i]);
Jens Axboe3c5f57e2011-10-06 12:37:50 +020082}
83
Jens Axboeb66570d2011-10-01 11:11:35 -060084static struct fio_client *find_client_by_fd(int fd)
85{
Jens Axboe3c5f57e2011-10-06 12:37:50 +020086 int bucket = hash_long(fd, FIO_CLIENT_HASH_BITS) & FIO_CLIENT_HASH_MASK;
Jens Axboeb66570d2011-10-01 11:11:35 -060087 struct fio_client *client;
88 struct flist_head *entry;
89
Jens Axboebebe6392011-10-07 10:00:51 +020090 flist_for_each(entry, &client_hash[bucket]) {
91 client = flist_entry(entry, struct fio_client, hash_list);
Jens Axboeb66570d2011-10-01 11:11:35 -060092
Jens Axboee55f8f32012-03-06 15:42:31 +010093 if (client->fd == fd) {
94 client->refs++;
Jens Axboeb66570d2011-10-01 11:11:35 -060095 return client;
Jens Axboee55f8f32012-03-06 15:42:31 +010096 }
Jens Axboeb66570d2011-10-01 11:11:35 -060097 }
98
99 return NULL;
100}
101
Jens Axboe3af45202012-03-13 09:59:53 +0100102void fio_put_client(struct fio_client *client)
Jens Axboeb66570d2011-10-01 11:11:35 -0600103{
Jens Axboe5121a9a2012-03-05 13:32:47 +0100104 if (--client->refs)
105 return;
106
Jens Axboeb66570d2011-10-01 11:11:35 -0600107 free(client->hostname);
Jens Axboe81179ee2011-10-04 12:42:06 +0200108 if (client->argv)
109 free(client->argv);
Jens Axboeb5296dd2011-10-07 16:35:56 +0200110 if (client->name)
111 free(client->name);
Jens Axboe81179ee2011-10-04 12:42:06 +0200112
Jens Axboeb66570d2011-10-01 11:11:35 -0600113 free(client);
114}
Jens Axboe132159a2011-09-30 15:01:32 -0600115
Jens Axboe3af45202012-03-13 09:59:53 +0100116static void remove_client(struct fio_client *client)
Jens Axboe5121a9a2012-03-05 13:32:47 +0100117{
Jens Axboe3af45202012-03-13 09:59:53 +0100118 assert(client->refs);
119
120 dprint(FD_NET, "client: removed <%s>\n", client->hostname);
121
122 if (!flist_empty(&client->list))
123 flist_del_init(&client->list);
124
125 fio_client_remove_hash(client);
126
127 if (!flist_empty(&client->eta_list)) {
128 flist_del_init(&client->eta_list);
129 fio_client_dec_jobs_eta(client->eta_in_flight, client->ops->eta);
130 }
131
132 nr_clients--;
133 sum_stat_clients--;
134
135 fio_put_client(client);
Jens Axboe5121a9a2012-03-05 13:32:47 +0100136}
137
Jens Axboe343cb4a2012-03-09 17:16:51 +0100138struct fio_client *fio_get_client(struct fio_client *client)
139{
140 client->refs++;
141 return client;
142}
143
Jens Axboefa2ea802011-10-08 21:07:29 +0200144static void __fio_client_add_cmd_option(struct fio_client *client,
145 const char *opt)
Jens Axboe81179ee2011-10-04 12:42:06 +0200146{
Jens Axboe39e8e012011-10-04 13:46:08 +0200147 int index;
148
149 index = client->argc++;
Jens Axboe81179ee2011-10-04 12:42:06 +0200150 client->argv = realloc(client->argv, sizeof(char *) * client->argc);
Jens Axboe39e8e012011-10-04 13:46:08 +0200151 client->argv[index] = strdup(opt);
152 dprint(FD_NET, "client: add cmd %d: %s\n", index, opt);
Jens Axboe81179ee2011-10-04 12:42:06 +0200153}
154
Jens Axboefa2ea802011-10-08 21:07:29 +0200155void fio_client_add_cmd_option(void *cookie, const char *opt)
Jens Axboe81179ee2011-10-04 12:42:06 +0200156{
Jens Axboebebe6392011-10-07 10:00:51 +0200157 struct fio_client *client = cookie;
Jens Axboe3f3a4542011-10-10 21:11:09 +0200158 struct flist_head *entry;
Jens Axboe81179ee2011-10-04 12:42:06 +0200159
Jens Axboebebe6392011-10-07 10:00:51 +0200160 if (!client || !opt)
Jens Axboefa2ea802011-10-08 21:07:29 +0200161 return;
Jens Axboe81179ee2011-10-04 12:42:06 +0200162
Jens Axboefa2ea802011-10-08 21:07:29 +0200163 __fio_client_add_cmd_option(client, opt);
Jens Axboe3f3a4542011-10-10 21:11:09 +0200164
165 /*
166 * Duplicate arguments to shared client group
167 */
168 flist_for_each(entry, &arg_list) {
169 client = flist_entry(entry, struct fio_client, arg_list);
170
171 __fio_client_add_cmd_option(client, opt);
172 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200173}
174
Jens Axboea5276612012-03-04 15:15:08 +0100175struct fio_client *fio_client_add_explicit(struct client_ops *ops,
176 const char *hostname, int type,
Jens Axboe3ec62ec2012-03-01 12:01:29 +0100177 int port)
178{
179 struct fio_client *client;
180
181 client = malloc(sizeof(*client));
182 memset(client, 0, sizeof(*client));
183
184 INIT_FLIST_HEAD(&client->list);
185 INIT_FLIST_HEAD(&client->hash_list);
186 INIT_FLIST_HEAD(&client->arg_list);
187 INIT_FLIST_HEAD(&client->eta_list);
188 INIT_FLIST_HEAD(&client->cmd_list);
189
190 client->hostname = strdup(hostname);
191
192 if (type == Fio_client_socket)
193 client->is_sock = 1;
194 else {
195 int ipv6;
196
197 ipv6 = type == Fio_client_ipv6;
198 if (fio_server_parse_host(hostname, &ipv6,
199 &client->addr.sin_addr,
200 &client->addr6.sin6_addr))
201 goto err;
202
203 client->port = port;
204 }
205
206 client->fd = -1;
Jens Axboea5276612012-03-04 15:15:08 +0100207 client->ops = ops;
Jens Axboe5121a9a2012-03-05 13:32:47 +0100208 client->refs = 1;
Jens Axboe3ec62ec2012-03-01 12:01:29 +0100209
210 __fio_client_add_cmd_option(client, "fio");
211
212 flist_add(&client->list, &client_list);
213 nr_clients++;
214 dprint(FD_NET, "client: added <%s>\n", client->hostname);
215 return client;
216err:
217 free(client);
218 return NULL;
219}
220
Jens Axboea5276612012-03-04 15:15:08 +0100221int fio_client_add(struct client_ops *ops, 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 Axboea5276612012-03-04 15:15:08 +0100256 client->ops = ops;
Jens Axboe5121a9a2012-03-05 13:32:47 +0100257 client->refs = 1;
Jens Axboe81179ee2011-10-04 12:42:06 +0200258
259 __fio_client_add_cmd_option(client, "fio");
260
Jens Axboea37f69b2011-10-01 12:24:21 -0600261 flist_add(&client->list, &client_list);
262 nr_clients++;
Jens Axboebebe6392011-10-07 10:00:51 +0200263 dprint(FD_NET, "client: added <%s>\n", client->hostname);
264 *cookie = client;
265 return 0;
Jens Axboea37f69b2011-10-01 12:24:21 -0600266}
267
Jens Axboed824c3b2012-03-09 17:45:43 +0100268static void probe_client(struct fio_client *client)
269{
270 dprint(FD_NET, "client: send probe\n");
271
272 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_PROBE, 0, &client->cmd_list);
273}
274
Jens Axboe87aa8f12011-10-06 21:24:13 +0200275static int fio_client_connect_ip(struct fio_client *client)
Jens Axboea37f69b2011-10-01 12:24:21 -0600276{
Jens Axboe811826b2011-10-24 09:11:50 +0200277 struct sockaddr *addr;
278 fio_socklen_t socklen;
279 int fd, domain;
Jens Axboe132159a2011-09-30 15:01:32 -0600280
Jens Axboe811826b2011-10-24 09:11:50 +0200281 if (client->ipv6) {
282 client->addr6.sin6_family = AF_INET6;
283 client->addr6.sin6_port = htons(client->port);
284 domain = AF_INET6;
285 addr = (struct sockaddr *) &client->addr6;
286 socklen = sizeof(client->addr6);
287 } else {
288 client->addr.sin_family = AF_INET;
289 client->addr.sin_port = htons(client->port);
290 domain = AF_INET;
291 addr = (struct sockaddr *) &client->addr;
292 socklen = sizeof(client->addr);
293 }
Jens Axboe132159a2011-09-30 15:01:32 -0600294
Jens Axboe811826b2011-10-24 09:11:50 +0200295 fd = socket(domain, SOCK_STREAM, 0);
Jens Axboe132159a2011-09-30 15:01:32 -0600296 if (fd < 0) {
Jens Axboe25095e12012-03-09 17:09:55 +0100297 int ret = -errno;
298
Jens Axboe132159a2011-09-30 15:01:32 -0600299 log_err("fio: socket: %s\n", strerror(errno));
Jens Axboe25095e12012-03-09 17:09:55 +0100300 return ret;
Jens Axboe132159a2011-09-30 15:01:32 -0600301 }
302
Jens Axboe811826b2011-10-24 09:11:50 +0200303 if (connect(fd, addr, socklen) < 0) {
Jens Axboe25095e12012-03-09 17:09:55 +0100304 int ret = -errno;
305
Jens Axboe132159a2011-09-30 15:01:32 -0600306 log_err("fio: connect: %s\n", strerror(errno));
Jens Axboea7de0a12011-10-07 12:55:14 +0200307 log_err("fio: failed to connect to %s:%u\n", client->hostname,
308 client->port);
Jens Axboeb94cba42011-10-06 21:27:10 +0200309 close(fd);
Jens Axboe25095e12012-03-09 17:09:55 +0100310 return ret;
Jens Axboe132159a2011-09-30 15:01:32 -0600311 }
312
Jens Axboe87aa8f12011-10-06 21:24:13 +0200313 return fd;
314}
315
316static int fio_client_connect_sock(struct fio_client *client)
317{
318 struct sockaddr_un *addr = &client->addr_un;
319 fio_socklen_t len;
320 int fd;
321
322 memset(addr, 0, sizeof(*addr));
323 addr->sun_family = AF_UNIX;
324 strcpy(addr->sun_path, client->hostname);
325
326 fd = socket(AF_UNIX, SOCK_STREAM, 0);
327 if (fd < 0) {
Jens Axboe25095e12012-03-09 17:09:55 +0100328 int ret = -errno;
329
Jens Axboe87aa8f12011-10-06 21:24:13 +0200330 log_err("fio: socket: %s\n", strerror(errno));
Jens Axboe25095e12012-03-09 17:09:55 +0100331 return ret;
Jens Axboe87aa8f12011-10-06 21:24:13 +0200332 }
333
334 len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
335 if (connect(fd, (struct sockaddr *) addr, len) < 0) {
Jens Axboe25095e12012-03-09 17:09:55 +0100336 int ret = -errno;
337
Jens Axboe87aa8f12011-10-06 21:24:13 +0200338 log_err("fio: connect; %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200339 close(fd);
Jens Axboe25095e12012-03-09 17:09:55 +0100340 return ret;
Jens Axboe87aa8f12011-10-06 21:24:13 +0200341 }
342
343 return fd;
344}
345
Jens Axboe2f99deb2012-03-09 14:37:29 +0100346int fio_client_connect(struct fio_client *client)
Jens Axboe87aa8f12011-10-06 21:24:13 +0200347{
348 int fd;
349
350 dprint(FD_NET, "client: connect to host %s\n", client->hostname);
351
Jens Axboe87aa8f12011-10-06 21:24:13 +0200352 if (client->is_sock)
353 fd = fio_client_connect_sock(client);
354 else
355 fd = fio_client_connect_ip(client);
356
Jens Axboe89c17072011-10-11 10:15:51 +0200357 dprint(FD_NET, "client: %s connected %d\n", client->hostname, fd);
358
Jens Axboe87aa8f12011-10-06 21:24:13 +0200359 if (fd < 0)
Jens Axboea1a3ed52012-03-09 17:00:01 +0100360 return fd;
Jens Axboe87aa8f12011-10-06 21:24:13 +0200361
Jens Axboeb66570d2011-10-01 11:11:35 -0600362 client->fd = fd;
Jens Axboebebe6392011-10-07 10:00:51 +0200363 fio_client_add_hash(client);
Jens Axboe81179ee2011-10-04 12:42:06 +0200364 client->state = Client_connected;
Jens Axboed824c3b2012-03-09 17:45:43 +0100365
366 probe_client(client);
Jens Axboe132159a2011-09-30 15:01:32 -0600367 return 0;
368}
369
Jens Axboe2f99deb2012-03-09 14:37:29 +0100370void fio_client_terminate(struct fio_client *client)
371{
372 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_QUIT, 0, NULL);
373}
374
Jens Axboecc0df002011-10-03 20:53:32 +0200375void fio_clients_terminate(void)
376{
377 struct flist_head *entry;
378 struct fio_client *client;
379
Jens Axboe60efd142011-10-04 13:30:11 +0200380 dprint(FD_NET, "client: terminate clients\n");
381
Jens Axboecc0df002011-10-03 20:53:32 +0200382 flist_for_each(entry, &client_list) {
383 client = flist_entry(entry, struct fio_client, list);
Jens Axboe2f99deb2012-03-09 14:37:29 +0100384 fio_client_terminate(client);
Jens Axboecc0df002011-10-03 20:53:32 +0200385 }
386}
387
388static void sig_int(int sig)
389{
Jens Axboebebe6392011-10-07 10:00:51 +0200390 dprint(FD_NET, "client: got signal %d\n", sig);
Jens Axboecc0df002011-10-03 20:53:32 +0200391 fio_clients_terminate();
392}
393
394static void client_signal_handler(void)
395{
396 struct sigaction act;
397
398 memset(&act, 0, sizeof(act));
399 act.sa_handler = sig_int;
400 act.sa_flags = SA_RESTART;
401 sigaction(SIGINT, &act, NULL);
402
403 memset(&act, 0, sizeof(act));
404 act.sa_handler = sig_int;
405 act.sa_flags = SA_RESTART;
406 sigaction(SIGTERM, &act, NULL);
407}
408
Jens Axboe81179ee2011-10-04 12:42:06 +0200409static int send_client_cmd_line(struct fio_client *client)
410{
Jens Axboefa2ea802011-10-08 21:07:29 +0200411 struct cmd_single_line_pdu *cslp;
412 struct cmd_line_pdu *clp;
413 unsigned long offset;
Jens Axboe7f868312011-10-10 09:55:21 +0200414 unsigned int *lens;
Jens Axboefa2ea802011-10-08 21:07:29 +0200415 void *pdu;
416 size_t mem;
Jens Axboe81179ee2011-10-04 12:42:06 +0200417 int i, ret;
418
Jens Axboe39e8e012011-10-04 13:46:08 +0200419 dprint(FD_NET, "client: send cmdline %d\n", client->argc);
Jens Axboe60efd142011-10-04 13:30:11 +0200420
Jens Axboe7f868312011-10-10 09:55:21 +0200421 lens = malloc(client->argc * sizeof(unsigned int));
422
Jens Axboefa2ea802011-10-08 21:07:29 +0200423 /*
424 * Find out how much mem we need
425 */
Jens Axboe7f868312011-10-10 09:55:21 +0200426 for (i = 0, mem = 0; i < client->argc; i++) {
427 lens[i] = strlen(client->argv[i]) + 1;
428 mem += lens[i];
429 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200430
Jens Axboefa2ea802011-10-08 21:07:29 +0200431 /*
432 * We need one cmd_line_pdu, and argc number of cmd_single_line_pdu
433 */
434 mem += sizeof(*clp) + (client->argc * sizeof(*cslp));
435
436 pdu = malloc(mem);
437 clp = pdu;
438 offset = sizeof(*clp);
439
440 for (i = 0; i < client->argc; i++) {
Jens Axboe7f868312011-10-10 09:55:21 +0200441 uint16_t arg_len = lens[i];
Jens Axboefa2ea802011-10-08 21:07:29 +0200442
443 cslp = pdu + offset;
444 strcpy((char *) cslp->text, client->argv[i]);
445 cslp->len = cpu_to_le16(arg_len);
446 offset += sizeof(*cslp) + arg_len;
447 }
448
Jens Axboe7f868312011-10-10 09:55:21 +0200449 free(lens);
Jens Axboefa2ea802011-10-08 21:07:29 +0200450 clp->lines = cpu_to_le16(client->argc);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200451 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOBLINE, pdu, mem, 0);
Jens Axboe81179ee2011-10-04 12:42:06 +0200452 free(pdu);
453 return ret;
454}
455
Jens Axboea37f69b2011-10-01 12:24:21 -0600456int fio_clients_connect(void)
457{
458 struct fio_client *client;
459 struct flist_head *entry, *tmp;
460 int ret;
461
Bruce Cran93bcfd22012-02-20 20:18:19 +0100462#ifdef WIN32
463 WSADATA wsd;
464 WSAStartup(MAKEWORD(2,2), &wsd);
465#endif
466
Jens Axboe60efd142011-10-04 13:30:11 +0200467 dprint(FD_NET, "client: connect all\n");
468
Jens Axboecc0df002011-10-03 20:53:32 +0200469 client_signal_handler();
470
Jens Axboea37f69b2011-10-01 12:24:21 -0600471 flist_for_each_safe(entry, tmp, &client_list) {
472 client = flist_entry(entry, struct fio_client, list);
473
474 ret = fio_client_connect(client);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200475 if (ret) {
Jens Axboea37f69b2011-10-01 12:24:21 -0600476 remove_client(client);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200477 continue;
478 }
479
Jens Axboe81179ee2011-10-04 12:42:06 +0200480 if (client->argc > 1)
481 send_client_cmd_line(client);
Jens Axboea37f69b2011-10-01 12:24:21 -0600482 }
483
484 return !nr_clients;
485}
486
Jens Axboeb9d2f302012-03-08 20:36:28 +0100487int fio_start_client(struct fio_client *client)
488{
489 dprint(FD_NET, "client: start %s\n", client->hostname);
490 return fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_RUN, 0, NULL);
491}
492
493int fio_start_all_clients(void)
494{
495 struct fio_client *client;
496 struct flist_head *entry, *tmp;
497 int ret;
498
499 dprint(FD_NET, "client: start all\n");
500
501 flist_for_each_safe(entry, tmp, &client_list) {
502 client = flist_entry(entry, struct fio_client, list);
503
504 ret = fio_start_client(client);
505 if (ret) {
506 remove_client(client);
507 continue;
508 }
509 }
510
511 return flist_empty(&client_list);
512}
513
Jens Axboe132159a2011-09-30 15:01:32 -0600514/*
515 * Send file contents to server backend. We could use sendfile(), but to remain
516 * more portable lets just read/write the darn thing.
517 */
Jens Axboe9988ca72012-03-09 15:14:06 +0100518static int __fio_client_send_ini(struct fio_client *client, const char *filename)
Jens Axboe132159a2011-09-30 15:01:32 -0600519{
520 struct stat sb;
521 char *p, *buf;
522 off_t len;
523 int fd, ret;
524
Jens Axboe46c48f12011-10-01 12:50:51 -0600525 dprint(FD_NET, "send ini %s to %s\n", filename, client->hostname);
526
Jens Axboe132159a2011-09-30 15:01:32 -0600527 fd = open(filename, O_RDONLY);
528 if (fd < 0) {
Jens Axboe25095e12012-03-09 17:09:55 +0100529 int ret = -errno;
530
Jens Axboee951bdc2011-10-05 21:58:45 +0200531 log_err("fio: job file <%s> open: %s\n", filename, strerror(errno));
Jens Axboe25095e12012-03-09 17:09:55 +0100532 return ret;
Jens Axboe132159a2011-09-30 15:01:32 -0600533 }
534
535 if (fstat(fd, &sb) < 0) {
Jens Axboe25095e12012-03-09 17:09:55 +0100536 int ret = -errno;
537
Jens Axboe132159a2011-09-30 15:01:32 -0600538 log_err("fio: job file stat: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200539 close(fd);
Jens Axboe25095e12012-03-09 17:09:55 +0100540 return ret;
Jens Axboe132159a2011-09-30 15:01:32 -0600541 }
542
543 buf = malloc(sb.st_size);
544
545 len = sb.st_size;
546 p = buf;
547 do {
548 ret = read(fd, p, len);
549 if (ret > 0) {
550 len -= ret;
551 if (!len)
552 break;
553 p += ret;
554 continue;
555 } else if (!ret)
556 break;
557 else if (errno == EAGAIN || errno == EINTR)
558 continue;
559 } while (1);
560
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200561 if (len) {
562 log_err("fio: failed reading job file %s\n", filename);
Jens Axboeb94cba42011-10-06 21:27:10 +0200563 close(fd);
Jens Axboec524ef72011-10-07 12:23:34 +0200564 free(buf);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200565 return 1;
566 }
567
Jens Axboec2cb6862012-02-23 20:56:12 +0100568 client->sent_job = 1;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200569 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, buf, sb.st_size, 0);
Jens Axboe132159a2011-09-30 15:01:32 -0600570 free(buf);
Jens Axboeb94cba42011-10-06 21:27:10 +0200571 close(fd);
Jens Axboe132159a2011-09-30 15:01:32 -0600572 return ret;
573}
Jens Axboe37db14f2011-09-30 17:00:42 -0600574
Jens Axboe9988ca72012-03-09 15:14:06 +0100575int fio_client_send_ini(struct fio_client *client, const char *filename)
576{
Jens Axboe25095e12012-03-09 17:09:55 +0100577 int ret;
578
579 ret = __fio_client_send_ini(client, filename);
Jens Axboe3af45202012-03-13 09:59:53 +0100580 if (!ret)
581 client->sent_job = 1;
Jens Axboe9988ca72012-03-09 15:14:06 +0100582
Jens Axboe25095e12012-03-09 17:09:55 +0100583 return ret;
Jens Axboe9988ca72012-03-09 15:14:06 +0100584}
585
Jens Axboea37f69b2011-10-01 12:24:21 -0600586int fio_clients_send_ini(const char *filename)
587{
588 struct fio_client *client;
589 struct flist_head *entry, *tmp;
590
591 flist_for_each_safe(entry, tmp, &client_list) {
592 client = flist_entry(entry, struct fio_client, list);
593
Jens Axboe3af45202012-03-13 09:59:53 +0100594 if (fio_client_send_ini(client, filename))
595 remove_client(client);
Jens Axboea37f69b2011-10-01 12:24:21 -0600596 }
597
598 return !nr_clients;
599}
600
Jens Axboea64e88d2011-10-03 14:20:01 +0200601static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
602{
603 dst->max_val = le64_to_cpu(src->max_val);
604 dst->min_val = le64_to_cpu(src->min_val);
605 dst->samples = le64_to_cpu(src->samples);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200606
607 /*
608 * Floats arrive as IEEE 754 encoded uint64_t, convert back to double
609 */
610 dst->mean.u.f = fio_uint64_to_double(le64_to_cpu(dst->mean.u.i));
611 dst->S.u.f = fio_uint64_to_double(le64_to_cpu(dst->S.u.i));
Jens Axboea64e88d2011-10-03 14:20:01 +0200612}
613
614static void convert_ts(struct thread_stat *dst, struct thread_stat *src)
615{
616 int i, j;
617
618 dst->error = le32_to_cpu(src->error);
619 dst->groupid = le32_to_cpu(src->groupid);
620 dst->pid = le32_to_cpu(src->pid);
621 dst->members = le32_to_cpu(src->members);
622
623 for (i = 0; i < 2; i++) {
624 convert_io_stat(&dst->clat_stat[i], &src->clat_stat[i]);
625 convert_io_stat(&dst->slat_stat[i], &src->slat_stat[i]);
626 convert_io_stat(&dst->lat_stat[i], &src->lat_stat[i]);
627 convert_io_stat(&dst->bw_stat[i], &src->bw_stat[i]);
628 }
629
630 dst->usr_time = le64_to_cpu(src->usr_time);
631 dst->sys_time = le64_to_cpu(src->sys_time);
632 dst->ctx = le64_to_cpu(src->ctx);
633 dst->minf = le64_to_cpu(src->minf);
634 dst->majf = le64_to_cpu(src->majf);
635 dst->clat_percentiles = le64_to_cpu(src->clat_percentiles);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200636
637 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
638 fio_fp64_t *fps = &src->percentile_list[i];
639 fio_fp64_t *fpd = &dst->percentile_list[i];
640
641 fpd->u.f = fio_uint64_to_double(le64_to_cpu(fps->u.i));
642 }
Jens Axboea64e88d2011-10-03 14:20:01 +0200643
644 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
645 dst->io_u_map[i] = le32_to_cpu(src->io_u_map[i]);
646 dst->io_u_submit[i] = le32_to_cpu(src->io_u_submit[i]);
647 dst->io_u_complete[i] = le32_to_cpu(src->io_u_complete[i]);
648 }
649
650 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
651 dst->io_u_lat_u[i] = le32_to_cpu(src->io_u_lat_u[i]);
652 dst->io_u_lat_m[i] = le32_to_cpu(src->io_u_lat_m[i]);
653 }
654
655 for (i = 0; i < 2; i++)
656 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
657 dst->io_u_plat[i][j] = le32_to_cpu(src->io_u_plat[i][j]);
658
659 for (i = 0; i < 3; i++) {
660 dst->total_io_u[i] = le64_to_cpu(src->total_io_u[i]);
Jens Axboe93eee042011-10-03 21:08:48 +0200661 dst->short_io_u[i] = le64_to_cpu(src->short_io_u[i]);
Jens Axboea64e88d2011-10-03 14:20:01 +0200662 }
663
664 dst->total_submit = le64_to_cpu(src->total_submit);
665 dst->total_complete = le64_to_cpu(src->total_complete);
666
667 for (i = 0; i < 2; i++) {
668 dst->io_bytes[i] = le64_to_cpu(src->io_bytes[i]);
669 dst->runtime[i] = le64_to_cpu(src->runtime[i]);
670 }
671
672 dst->total_run_time = le64_to_cpu(src->total_run_time);
673 dst->continue_on_error = le16_to_cpu(src->continue_on_error);
674 dst->total_err_count = le64_to_cpu(src->total_err_count);
Jens Axboeddcc0b62011-10-03 14:45:27 +0200675 dst->first_error = le32_to_cpu(src->first_error);
676 dst->kb_base = le32_to_cpu(src->kb_base);
Jens Axboea64e88d2011-10-03 14:20:01 +0200677}
678
679static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
680{
681 int i;
682
683 for (i = 0; i < 2; i++) {
684 dst->max_run[i] = le64_to_cpu(src->max_run[i]);
685 dst->min_run[i] = le64_to_cpu(src->min_run[i]);
686 dst->max_bw[i] = le64_to_cpu(src->max_bw[i]);
687 dst->min_bw[i] = le64_to_cpu(src->min_bw[i]);
688 dst->io_kb[i] = le64_to_cpu(src->io_kb[i]);
689 dst->agg[i] = le64_to_cpu(src->agg[i]);
690 }
691
692 dst->kb_base = le32_to_cpu(src->kb_base);
693 dst->groupid = le32_to_cpu(src->groupid);
694}
695
Jens Axboe89e5fad2012-03-05 09:21:12 +0100696static void handle_ts(struct fio_client *client, struct fio_net_cmd *cmd)
Jens Axboea64e88d2011-10-03 14:20:01 +0200697{
698 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
699
Jens Axboea64e88d2011-10-03 14:20:01 +0200700 show_thread_status(&p->ts, &p->rs);
Jens Axboe37f0c1a2011-10-11 14:08:33 +0200701
702 if (sum_stat_clients == 1)
703 return;
704
705 sum_thread_stats(&client_ts, &p->ts, sum_stat_nr);
706 sum_group_stats(&client_gs, &p->rs);
707
708 client_ts.members++;
709 client_ts.groupid = p->ts.groupid;
710
711 if (++sum_stat_nr == sum_stat_clients) {
712 strcpy(client_ts.name, "All clients");
713 show_thread_status(&client_ts, &client_gs);
714 }
Jens Axboea64e88d2011-10-03 14:20:01 +0200715}
716
Jens Axboe89e5fad2012-03-05 09:21:12 +0100717static void handle_gs(struct fio_client *client, struct fio_net_cmd *cmd)
Jens Axboea64e88d2011-10-03 14:20:01 +0200718{
719 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
720
Jens Axboea64e88d2011-10-03 14:20:01 +0200721 show_group_stats(gs);
722}
723
Jens Axboe3bf236c2012-03-03 20:35:50 +0100724static void handle_text(struct fio_client *client, struct fio_net_cmd *cmd)
725{
726 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
727 const char *buf = (const char *) pdu->buf;
728 const char *name;
729 int fio_unused ret;
730
731 name = client->name ? client->name : client->hostname;
732
733 if (!client->skip_newline)
734 fprintf(f_out, "<%s> ", name);
735 ret = fwrite(buf, pdu->buf_len, 1, f_out);
736 fflush(f_out);
737 client->skip_newline = strchr(buf, '\n') == NULL;
738}
739
Jens Axboed09a64a2011-10-13 11:38:56 +0200740static void convert_agg(struct disk_util_agg *agg)
741{
742 int i;
743
744 for (i = 0; i < 2; i++) {
745 agg->ios[i] = le32_to_cpu(agg->ios[i]);
746 agg->merges[i] = le32_to_cpu(agg->merges[i]);
747 agg->sectors[i] = le64_to_cpu(agg->sectors[i]);
748 agg->ticks[i] = le32_to_cpu(agg->ticks[i]);
749 }
750
751 agg->io_ticks = le32_to_cpu(agg->io_ticks);
752 agg->time_in_queue = le32_to_cpu(agg->time_in_queue);
753 agg->slavecount = le32_to_cpu(agg->slavecount);
Anton Blanchard823ba542011-11-07 14:16:26 +0100754 agg->max_util.u.f = fio_uint64_to_double(__le64_to_cpu(agg->max_util.u.i));
Jens Axboed09a64a2011-10-13 11:38:56 +0200755}
756
757static void convert_dus(struct disk_util_stat *dus)
758{
759 int i;
760
761 for (i = 0; i < 2; i++) {
762 dus->ios[i] = le32_to_cpu(dus->ios[i]);
763 dus->merges[i] = le32_to_cpu(dus->merges[i]);
764 dus->sectors[i] = le64_to_cpu(dus->sectors[i]);
765 dus->ticks[i] = le32_to_cpu(dus->ticks[i]);
766 }
767
768 dus->io_ticks = le32_to_cpu(dus->io_ticks);
769 dus->time_in_queue = le32_to_cpu(dus->time_in_queue);
770 dus->msec = le64_to_cpu(dus->msec);
771}
772
773static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd)
774{
775 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
776
Jens Axboed09a64a2011-10-13 11:38:56 +0200777 if (!client->disk_stats_shown) {
778 client->disk_stats_shown = 1;
779 log_info("\nDisk stats (read/write):\n");
780 }
781
Jens Axboef2f788d2011-10-13 14:03:52 +0200782 print_disk_util(&du->dus, &du->agg, terse_output);
Jens Axboed09a64a2011-10-13 11:38:56 +0200783}
784
Jens Axboe3bf236c2012-03-03 20:35:50 +0100785static void convert_jobs_eta(struct jobs_eta *je)
Jens Axboecf451d12011-10-03 16:48:30 +0200786{
Jens Axboecf451d12011-10-03 16:48:30 +0200787 int i;
788
789 je->nr_running = le32_to_cpu(je->nr_running);
790 je->nr_ramp = le32_to_cpu(je->nr_ramp);
791 je->nr_pending = le32_to_cpu(je->nr_pending);
792 je->files_open = le32_to_cpu(je->files_open);
Jens Axboecf451d12011-10-03 16:48:30 +0200793
794 for (i = 0; i < 2; i++) {
Jens Axboe3e47bd22012-02-29 13:45:02 +0100795 je->m_rate[i] = le32_to_cpu(je->m_rate[i]);
796 je->t_rate[i] = le32_to_cpu(je->t_rate[i]);
797 je->m_iops[i] = le32_to_cpu(je->m_iops[i]);
798 je->t_iops[i] = le32_to_cpu(je->t_iops[i]);
Jens Axboecf451d12011-10-03 16:48:30 +0200799 je->rate[i] = le32_to_cpu(je->rate[i]);
800 je->iops[i] = le32_to_cpu(je->iops[i]);
801 }
802
Jens Axboeb51eedb2011-10-09 12:13:39 +0200803 je->elapsed_sec = le64_to_cpu(je->elapsed_sec);
Jens Axboecf451d12011-10-03 16:48:30 +0200804 je->eta_sec = le64_to_cpu(je->eta_sec);
Jens Axboe8c621fb2012-03-08 12:30:48 +0100805 je->nr_threads = le32_to_cpu(je->nr_threads);
Jens Axboe48fbb462011-10-09 12:19:08 +0200806}
Jens Axboecf451d12011-10-03 16:48:30 +0200807
Jens Axboe3e47bd22012-02-29 13:45:02 +0100808void fio_client_sum_jobs_eta(struct jobs_eta *dst, struct jobs_eta *je)
Jens Axboe48fbb462011-10-09 12:19:08 +0200809{
Jens Axboe48fbb462011-10-09 12:19:08 +0200810 int i;
811
812 dst->nr_running += je->nr_running;
813 dst->nr_ramp += je->nr_ramp;
814 dst->nr_pending += je->nr_pending;
815 dst->files_open += je->files_open;
Jens Axboe48fbb462011-10-09 12:19:08 +0200816
817 for (i = 0; i < 2; i++) {
Jens Axboe3e47bd22012-02-29 13:45:02 +0100818 dst->m_rate[i] += je->m_rate[i];
819 dst->t_rate[i] += je->t_rate[i];
820 dst->m_iops[i] += je->m_iops[i];
821 dst->t_iops[i] += je->t_iops[i];
Jens Axboe48fbb462011-10-09 12:19:08 +0200822 dst->rate[i] += je->rate[i];
823 dst->iops[i] += je->iops[i];
824 }
825
826 dst->elapsed_sec += je->elapsed_sec;
827
828 if (je->eta_sec > dst->eta_sec)
829 dst->eta_sec = je->eta_sec;
Jens Axboe8c621fb2012-03-08 12:30:48 +0100830
831 dst->nr_threads += je->nr_threads;
832 /* we need to handle je->run_str too ... */
Jens Axboe48fbb462011-10-09 12:19:08 +0200833}
834
Jens Axboea5276612012-03-04 15:15:08 +0100835void fio_client_dec_jobs_eta(struct client_eta *eta, client_eta_op eta_fn)
Jens Axboe82c1ed32011-10-10 08:56:18 +0200836{
837 if (!--eta->pending) {
Jens Axboea5276612012-03-04 15:15:08 +0100838 eta_fn(&eta->eta);
Jens Axboe82c1ed32011-10-10 08:56:18 +0200839 free(eta);
840 }
841}
842
Jens Axboe89c17072011-10-11 10:15:51 +0200843static void remove_reply_cmd(struct fio_client *client, struct fio_net_cmd *cmd)
844{
845 struct fio_net_int_cmd *icmd = NULL;
846 struct flist_head *entry;
847
848 flist_for_each(entry, &client->cmd_list) {
849 icmd = flist_entry(entry, struct fio_net_int_cmd, list);
850
Jens Axboedf380932011-10-11 14:25:08 +0200851 if (cmd->tag == (uintptr_t) icmd)
Jens Axboe89c17072011-10-11 10:15:51 +0200852 break;
853
854 icmd = NULL;
855 }
856
857 if (!icmd) {
858 log_err("fio: client: unable to find matching tag\n");
859 return;
860 }
861
862 flist_del(&icmd->list);
863 cmd->tag = icmd->saved_tag;
864 free(icmd);
865}
866
Jens Axboe82c1ed32011-10-10 08:56:18 +0200867static void handle_eta(struct fio_client *client, struct fio_net_cmd *cmd)
Jens Axboe48fbb462011-10-09 12:19:08 +0200868{
869 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
Jens Axboedf380932011-10-11 14:25:08 +0200870 struct client_eta *eta = (struct client_eta *) (uintptr_t) cmd->tag;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200871
872 dprint(FD_NET, "client: got eta tag %p, %d\n", eta, eta->pending);
Jens Axboe48fbb462011-10-09 12:19:08 +0200873
Jens Axboef77d2672011-10-10 14:36:07 +0200874 assert(client->eta_in_flight == eta);
875
876 client->eta_in_flight = NULL;
Jens Axboe82c1ed32011-10-10 08:56:18 +0200877 flist_del_init(&client->eta_list);
878
Jens Axboe2f99deb2012-03-09 14:37:29 +0100879 if (client->ops->jobs_eta)
880 client->ops->jobs_eta(client, je);
881
Jens Axboe3e47bd22012-02-29 13:45:02 +0100882 fio_client_sum_jobs_eta(&eta->eta, je);
Jens Axboea5276612012-03-04 15:15:08 +0100883 fio_client_dec_jobs_eta(eta, client->ops->eta);
Jens Axboecf451d12011-10-03 16:48:30 +0200884}
885
Jens Axboeb5296dd2011-10-07 16:35:56 +0200886static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd)
Jens Axboe2e03b4b2011-10-04 09:00:40 +0200887{
888 struct cmd_probe_pdu *probe = (struct cmd_probe_pdu *) cmd->payload;
Jens Axboed2333352011-10-11 15:07:23 +0200889 const char *os, *arch;
890 char bit[16];
Jens Axboe2e03b4b2011-10-04 09:00:40 +0200891
Jens Axboecca84642011-10-07 12:47:57 +0200892 os = fio_get_os_string(probe->os);
893 if (!os)
894 os = "unknown";
895
896 arch = fio_get_arch_string(probe->arch);
897 if (!arch)
898 os = "unknown";
899
Jens Axboed2333352011-10-11 15:07:23 +0200900 sprintf(bit, "%d-bit", probe->bpp * 8);
Jens Axboe38fdef22011-10-11 14:30:06 +0200901
902 log_info("hostname=%s, be=%u, %s, os=%s, arch=%s, fio=%u.%u.%u\n",
903 probe->hostname, probe->bigendian, bit, os, arch,
904 probe->fio_major, probe->fio_minor, probe->fio_patch);
Jens Axboeb5296dd2011-10-07 16:35:56 +0200905
906 if (!client->name)
907 client->name = strdup((char *) probe->hostname);
Jens Axboe2e03b4b2011-10-04 09:00:40 +0200908}
909
Jens Axboe11e950b2011-10-16 21:34:14 +0200910static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd)
911{
912 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
913
914 client->state = Client_started;
Jens Axboe85dd01e2012-03-12 14:33:16 +0100915 client->jobs = pdu->jobs;
Jens Axboe11e950b2011-10-16 21:34:14 +0200916}
917
918static void handle_stop(struct fio_client *client, struct fio_net_cmd *cmd)
919{
Jens Axboe498c92c2011-10-17 09:14:42 +0200920 if (client->error)
921 log_info("client <%s>: exited with error %d\n", client->hostname, client->error);
Jens Axboe11e950b2011-10-16 21:34:14 +0200922}
923
Jens Axboe6b79c802012-03-08 10:51:36 +0100924static void convert_stop(struct fio_net_cmd *cmd)
925{
926 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
927
928 pdu->error = le32_to_cpu(pdu->error);
929}
930
Jens Axboe084d1c62012-03-03 20:28:07 +0100931static void convert_text(struct fio_net_cmd *cmd)
932{
933 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
934
935 pdu->level = le32_to_cpu(pdu->level);
936 pdu->buf_len = le32_to_cpu(pdu->buf_len);
937 pdu->log_sec = le64_to_cpu(pdu->log_sec);
938 pdu->log_usec = le64_to_cpu(pdu->log_usec);
939}
940
Jens Axboea5276612012-03-04 15:15:08 +0100941int fio_handle_client(struct fio_client *client)
Jens Axboe37db14f2011-09-30 17:00:42 -0600942{
Jens Axboea5276612012-03-04 15:15:08 +0100943 struct client_ops *ops = client->ops;
Jens Axboe37db14f2011-09-30 17:00:42 -0600944 struct fio_net_cmd *cmd;
945
Jens Axboe60efd142011-10-04 13:30:11 +0200946 dprint(FD_NET, "client: handle %s\n", client->hostname);
947
Jens Axboee951bdc2011-10-05 21:58:45 +0200948 cmd = fio_net_recv_cmd(client->fd);
949 if (!cmd)
950 return 0;
Jens Axboec2c94582011-10-05 20:31:30 +0200951
Jens Axboeb9d2f302012-03-08 20:36:28 +0100952 dprint(FD_NET, "client: got cmd op %s from %s (pdu=%u)\n",
953 fio_server_op(cmd->opcode), client->hostname, cmd->pdu_len);
Jens Axboe46c48f12011-10-01 12:50:51 -0600954
Jens Axboee951bdc2011-10-05 21:58:45 +0200955 switch (cmd->opcode) {
956 case FIO_NET_CMD_QUIT:
Jens Axboe3ec62ec2012-03-01 12:01:29 +0100957 if (ops->quit)
Jens Axboe35c0ba72012-03-14 10:56:40 +0100958 ops->quit(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +0200959 remove_client(client);
960 free(cmd);
961 break;
Jens Axboe084d1c62012-03-03 20:28:07 +0100962 case FIO_NET_CMD_TEXT:
963 convert_text(cmd);
Jens Axboe35c0ba72012-03-14 10:56:40 +0100964 ops->text(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +0200965 free(cmd);
966 break;
Jens Axboe3bf236c2012-03-03 20:35:50 +0100967 case FIO_NET_CMD_DU: {
968 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
969
970 convert_dus(&du->dus);
971 convert_agg(&du->agg);
972
Stephen M. Camerondd366722012-02-24 08:17:30 +0100973 ops->disk_util(client, cmd);
Jens Axboed09a64a2011-10-13 11:38:56 +0200974 free(cmd);
975 break;
Jens Axboe3bf236c2012-03-03 20:35:50 +0100976 }
977 case FIO_NET_CMD_TS: {
978 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
979
980 convert_ts(&p->ts, &p->ts);
981 convert_gs(&p->rs, &p->rs);
982
Jens Axboe89e5fad2012-03-05 09:21:12 +0100983 ops->thread_status(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +0200984 free(cmd);
985 break;
Jens Axboe3bf236c2012-03-03 20:35:50 +0100986 }
987 case FIO_NET_CMD_GS: {
988 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
989
990 convert_gs(gs, gs);
991
Jens Axboe89e5fad2012-03-05 09:21:12 +0100992 ops->group_stats(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +0200993 free(cmd);
994 break;
Jens Axboe3bf236c2012-03-03 20:35:50 +0100995 }
996 case FIO_NET_CMD_ETA: {
997 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
998
Jens Axboe89c17072011-10-11 10:15:51 +0200999 remove_reply_cmd(client, cmd);
Jens Axboe3bf236c2012-03-03 20:35:50 +01001000 convert_jobs_eta(je);
Jens Axboea5276612012-03-04 15:15:08 +01001001 handle_eta(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +02001002 free(cmd);
1003 break;
Jens Axboe3bf236c2012-03-03 20:35:50 +01001004 }
Jens Axboee951bdc2011-10-05 21:58:45 +02001005 case FIO_NET_CMD_PROBE:
Jens Axboe89c17072011-10-11 10:15:51 +02001006 remove_reply_cmd(client, cmd);
Stephen M. Camerondd366722012-02-24 08:17:30 +01001007 ops->probe(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +02001008 free(cmd);
1009 break;
Jens Axboe5d7793a2012-03-08 19:59:16 +01001010 case FIO_NET_CMD_SERVER_START:
Jens Axboe01be0382011-10-15 14:43:41 +02001011 client->state = Client_running;
Jens Axboe85dd01e2012-03-12 14:33:16 +01001012 if (ops->job_start)
1013 ops->job_start(client, cmd);
Jens Axboe01be0382011-10-15 14:43:41 +02001014 free(cmd);
1015 break;
Jens Axboe85dd01e2012-03-12 14:33:16 +01001016 case FIO_NET_CMD_START: {
1017 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
1018
1019 pdu->jobs = le32_to_cpu(pdu->jobs);
1020 ops->start(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +02001021 free(cmd);
1022 break;
Jens Axboe85dd01e2012-03-12 14:33:16 +01001023 }
Jens Axboe6b79c802012-03-08 10:51:36 +01001024 case FIO_NET_CMD_STOP: {
1025 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
1026
1027 convert_stop(cmd);
1028 client->state = Client_stopped;
1029 client->error = pdu->error;
1030 ops->stop(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +02001031 free(cmd);
1032 break;
Jens Axboe6b79c802012-03-08 10:51:36 +01001033 }
Jens Axboe807f9972012-03-02 10:25:24 +01001034 case FIO_NET_CMD_ADD_JOB:
1035 if (ops->add_job)
1036 ops->add_job(client, cmd);
1037 free(cmd);
1038 break;
Jens Axboee951bdc2011-10-05 21:58:45 +02001039 default:
Jens Axboe89c17072011-10-11 10:15:51 +02001040 log_err("fio: unknown client op: %s\n", fio_server_op(cmd->opcode));
Jens Axboee951bdc2011-10-05 21:58:45 +02001041 free(cmd);
1042 break;
Jens Axboe37db14f2011-09-30 17:00:42 -06001043 }
1044
Jens Axboee951bdc2011-10-05 21:58:45 +02001045 return 1;
Jens Axboe37db14f2011-09-30 17:00:42 -06001046}
Jens Axboeb66570d2011-10-01 11:11:35 -06001047
Jens Axboea5276612012-03-04 15:15:08 +01001048static void request_client_etas(struct client_ops *ops)
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001049{
1050 struct fio_client *client;
1051 struct flist_head *entry;
1052 struct client_eta *eta;
Jens Axboe82c1ed32011-10-10 08:56:18 +02001053 int skipped = 0;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001054
1055 dprint(FD_NET, "client: request eta (%d)\n", nr_clients);
1056
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001057 eta = malloc(sizeof(*eta));
1058 memset(&eta->eta, 0, sizeof(eta->eta));
1059 eta->pending = nr_clients;
1060
1061 flist_for_each(entry, &client_list) {
1062 client = flist_entry(entry, struct fio_client, list);
1063
Jens Axboe82c1ed32011-10-10 08:56:18 +02001064 if (!flist_empty(&client->eta_list)) {
1065 skipped++;
1066 continue;
1067 }
Jens Axboe01be0382011-10-15 14:43:41 +02001068 if (client->state != Client_running)
1069 continue;
Jens Axboe82c1ed32011-10-10 08:56:18 +02001070
Jens Axboef77d2672011-10-10 14:36:07 +02001071 assert(!client->eta_in_flight);
Jens Axboe82c1ed32011-10-10 08:56:18 +02001072 flist_add_tail(&client->eta_list, &eta_list);
Jens Axboef77d2672011-10-10 14:36:07 +02001073 client->eta_in_flight = eta;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001074 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_SEND_ETA,
Jens Axboedf380932011-10-11 14:25:08 +02001075 (uintptr_t) eta, &client->cmd_list);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001076 }
1077
Jens Axboe82c1ed32011-10-10 08:56:18 +02001078 while (skipped--)
Jens Axboea5276612012-03-04 15:15:08 +01001079 fio_client_dec_jobs_eta(eta, ops->eta);
Jens Axboe82c1ed32011-10-10 08:56:18 +02001080
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001081 dprint(FD_NET, "client: requested eta tag %p\n", eta);
1082}
1083
Jens Axboe89c17072011-10-11 10:15:51 +02001084static int client_check_cmd_timeout(struct fio_client *client,
1085 struct timeval *now)
1086{
1087 struct fio_net_int_cmd *cmd;
1088 struct flist_head *entry, *tmp;
1089 int ret = 0;
1090
1091 flist_for_each_safe(entry, tmp, &client->cmd_list) {
1092 cmd = flist_entry(entry, struct fio_net_int_cmd, list);
1093
1094 if (mtime_since(&cmd->tv, now) < FIO_NET_CLIENT_TIMEOUT)
1095 continue;
1096
1097 log_err("fio: client %s, timeout on cmd %s\n", client->hostname,
1098 fio_server_op(cmd->cmd.opcode));
1099 flist_del(&cmd->list);
1100 free(cmd);
1101 ret = 1;
1102 }
1103
1104 return flist_empty(&client->cmd_list) && ret;
1105}
1106
Jens Axboea5276612012-03-04 15:15:08 +01001107static int fio_check_clients_timed_out(void)
Jens Axboe89c17072011-10-11 10:15:51 +02001108{
1109 struct fio_client *client;
1110 struct flist_head *entry, *tmp;
1111 struct timeval tv;
1112 int ret = 0;
1113
1114 gettimeofday(&tv, NULL);
1115
1116 flist_for_each_safe(entry, tmp, &client_list) {
1117 client = flist_entry(entry, struct fio_client, list);
1118
1119 if (flist_empty(&client->cmd_list))
1120 continue;
1121
1122 if (!client_check_cmd_timeout(client, &tv))
1123 continue;
1124
Jens Axboea5276612012-03-04 15:15:08 +01001125 if (client->ops->timed_out)
1126 client->ops->timed_out(client);
Jens Axboeed727a42012-03-02 12:14:40 +01001127 else
1128 log_err("fio: client %s timed out\n", client->hostname);
1129
Jens Axboe89c17072011-10-11 10:15:51 +02001130 remove_client(client);
1131 ret = 1;
1132 }
1133
1134 return ret;
1135}
1136
Stephen M. Camerondd366722012-02-24 08:17:30 +01001137int fio_handle_clients(struct client_ops *ops)
Jens Axboeb66570d2011-10-01 11:11:35 -06001138{
Jens Axboeb66570d2011-10-01 11:11:35 -06001139 struct pollfd *pfds;
Jens Axboe498c92c2011-10-17 09:14:42 +02001140 int i, ret = 0, retval = 0;
Jens Axboeb66570d2011-10-01 11:11:35 -06001141
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001142 gettimeofday(&eta_tv, NULL);
1143
Jens Axboeb66570d2011-10-01 11:11:35 -06001144 pfds = malloc(nr_clients * sizeof(struct pollfd));
1145
Jens Axboe37f0c1a2011-10-11 14:08:33 +02001146 sum_stat_clients = nr_clients;
1147 init_thread_stat(&client_ts);
1148 init_group_run_stat(&client_gs);
1149
Jens Axboeb66570d2011-10-01 11:11:35 -06001150 while (!exit_backend && nr_clients) {
Jens Axboec2cb6862012-02-23 20:56:12 +01001151 struct flist_head *entry, *tmp;
1152 struct fio_client *client;
1153
Jens Axboe82a4be12011-10-01 12:40:32 -06001154 i = 0;
Jens Axboec2cb6862012-02-23 20:56:12 +01001155 flist_for_each_safe(entry, tmp, &client_list) {
Jens Axboe82a4be12011-10-01 12:40:32 -06001156 client = flist_entry(entry, struct fio_client, list);
1157
Jens Axboea5276612012-03-04 15:15:08 +01001158 if (!client->sent_job && !client->ops->stay_connected &&
Jens Axboec2cb6862012-02-23 20:56:12 +01001159 flist_empty(&client->cmd_list)) {
1160 remove_client(client);
1161 continue;
1162 }
1163
Jens Axboe82a4be12011-10-01 12:40:32 -06001164 pfds[i].fd = client->fd;
1165 pfds[i].events = POLLIN;
1166 i++;
1167 }
1168
Jens Axboec2cb6862012-02-23 20:56:12 +01001169 if (!nr_clients)
1170 break;
1171
Jens Axboe82a4be12011-10-01 12:40:32 -06001172 assert(i == nr_clients);
1173
Jens Axboe5c2857f2011-10-04 13:14:32 +02001174 do {
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001175 struct timeval tv;
1176
1177 gettimeofday(&tv, NULL);
Jens Axboe6433ee02012-03-09 20:10:51 +01001178 if (mtime_since(&eta_tv, &tv) >= ops->eta_msec) {
Jens Axboea5276612012-03-04 15:15:08 +01001179 request_client_etas(ops);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001180 memcpy(&eta_tv, &tv, sizeof(tv));
Jens Axboe89c17072011-10-11 10:15:51 +02001181
Jens Axboea5276612012-03-04 15:15:08 +01001182 if (fio_check_clients_timed_out())
Jens Axboe89c17072011-10-11 10:15:51 +02001183 break;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001184 }
1185
Jens Axboe5c2857f2011-10-04 13:14:32 +02001186 ret = poll(pfds, nr_clients, 100);
1187 if (ret < 0) {
1188 if (errno == EINTR)
1189 continue;
1190 log_err("fio: poll clients: %s\n", strerror(errno));
1191 break;
1192 } else if (!ret)
Jens Axboeb66570d2011-10-01 11:11:35 -06001193 continue;
Jens Axboe5c2857f2011-10-04 13:14:32 +02001194 } while (ret <= 0);
Jens Axboeb66570d2011-10-01 11:11:35 -06001195
1196 for (i = 0; i < nr_clients; i++) {
1197 if (!(pfds[i].revents & POLLIN))
1198 continue;
1199
1200 client = find_client_by_fd(pfds[i].fd);
1201 if (!client) {
Jens Axboe3c5f57e2011-10-06 12:37:50 +02001202 log_err("fio: unknown client fd %d\n", pfds[i].fd);
Jens Axboeb66570d2011-10-01 11:11:35 -06001203 continue;
1204 }
Jens Axboea5276612012-03-04 15:15:08 +01001205 if (!fio_handle_client(client)) {
Jens Axboe28d3ab02011-10-05 20:45:37 +02001206 log_info("client: host=%s disconnected\n",
1207 client->hostname);
1208 remove_client(client);
Jens Axboe498c92c2011-10-17 09:14:42 +02001209 retval = 1;
Jens Axboe38990762011-10-17 13:31:33 +02001210 } else if (client->error)
Jens Axboe498c92c2011-10-17 09:14:42 +02001211 retval = 1;
Jens Axboe343cb4a2012-03-09 17:16:51 +01001212 fio_put_client(client);
Jens Axboeb66570d2011-10-01 11:11:35 -06001213 }
1214 }
1215
1216 free(pfds);
Jens Axboe498c92c2011-10-17 09:14:42 +02001217 return retval;
Jens Axboeb66570d2011-10-01 11:11:35 -06001218}