blob: f9a160261a955118f75f60f6c701f58c748a8b63 [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 Axboeb66570d2011-10-01 11:11:35 -060043static FLIST_HEAD(client_list);
Jens Axboe82c1ed32011-10-10 08:56:18 +020044static FLIST_HEAD(eta_list);
Jens Axboeb66570d2011-10-01 11:11:35 -060045
Jens Axboe3f3a4542011-10-10 21:11:09 +020046static FLIST_HEAD(arg_list);
47
Jens Axboe3650a3c2012-03-05 14:09:03 +010048struct thread_stat client_ts;
49struct group_run_stats client_gs;
50int sum_stat_clients;
51
Jens Axboe37f0c1a2011-10-11 14:08:33 +020052static int sum_stat_nr;
53
Jens Axboe3c5f57e2011-10-06 12:37:50 +020054#define FIO_CLIENT_HASH_BITS 7
55#define FIO_CLIENT_HASH_SZ (1 << FIO_CLIENT_HASH_BITS)
56#define FIO_CLIENT_HASH_MASK (FIO_CLIENT_HASH_SZ - 1)
Jens Axboebebe6392011-10-07 10:00:51 +020057static struct flist_head client_hash[FIO_CLIENT_HASH_SZ];
Jens Axboe3c5f57e2011-10-06 12:37:50 +020058
Jens Axboebebe6392011-10-07 10:00:51 +020059static void fio_client_add_hash(struct fio_client *client)
Jens Axboe3c5f57e2011-10-06 12:37:50 +020060{
61 int bucket = hash_long(client->fd, FIO_CLIENT_HASH_BITS);
62
63 bucket &= FIO_CLIENT_HASH_MASK;
Jens Axboebebe6392011-10-07 10:00:51 +020064 flist_add(&client->hash_list, &client_hash[bucket]);
Jens Axboe3c5f57e2011-10-06 12:37:50 +020065}
66
Jens Axboebebe6392011-10-07 10:00:51 +020067static void fio_client_remove_hash(struct fio_client *client)
Jens Axboe3c5f57e2011-10-06 12:37:50 +020068{
Jens Axboebebe6392011-10-07 10:00:51 +020069 if (!flist_empty(&client->hash_list))
70 flist_del_init(&client->hash_list);
Jens Axboe3c5f57e2011-10-06 12:37:50 +020071}
72
73static void fio_init fio_client_hash_init(void)
74{
75 int i;
76
Jens Axboebebe6392011-10-07 10:00:51 +020077 for (i = 0; i < FIO_CLIENT_HASH_SZ; i++)
78 INIT_FLIST_HEAD(&client_hash[i]);
Jens Axboe3c5f57e2011-10-06 12:37:50 +020079}
80
Jens Axboeb66570d2011-10-01 11:11:35 -060081static struct fio_client *find_client_by_fd(int fd)
82{
Jens Axboe3c5f57e2011-10-06 12:37:50 +020083 int bucket = hash_long(fd, FIO_CLIENT_HASH_BITS) & FIO_CLIENT_HASH_MASK;
Jens Axboeb66570d2011-10-01 11:11:35 -060084 struct fio_client *client;
85 struct flist_head *entry;
86
Jens Axboebebe6392011-10-07 10:00:51 +020087 flist_for_each(entry, &client_hash[bucket]) {
88 client = flist_entry(entry, struct fio_client, hash_list);
Jens Axboeb66570d2011-10-01 11:11:35 -060089
Jens Axboe343cb4a2012-03-09 17:16:51 +010090 if (client->fd == fd)
91 return fio_get_client(client);
Jens Axboeb66570d2011-10-01 11:11:35 -060092 }
93
94 return NULL;
95}
96
Jens Axboeb66570d2011-10-01 11:11:35 -060097static void remove_client(struct fio_client *client)
98{
Jens Axboe5121a9a2012-03-05 13:32:47 +010099 assert(client->refs);
100
101 if (--client->refs)
102 return;
103
Jens Axboe39e8e012011-10-04 13:46:08 +0200104 dprint(FD_NET, "client: removed <%s>\n", client->hostname);
Jens Axboeb66570d2011-10-01 11:11:35 -0600105 flist_del(&client->list);
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200106
Jens Axboebebe6392011-10-07 10:00:51 +0200107 fio_client_remove_hash(client);
Jens Axboe81179ee2011-10-04 12:42:06 +0200108
Jens Axboe82c1ed32011-10-10 08:56:18 +0200109 if (!flist_empty(&client->eta_list)) {
110 flist_del_init(&client->eta_list);
Jens Axboea5276612012-03-04 15:15:08 +0100111 fio_client_dec_jobs_eta(client->eta_in_flight, client->ops->eta);
Jens Axboe82c1ed32011-10-10 08:56:18 +0200112 }
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200113
Jens Axboeb66570d2011-10-01 11:11:35 -0600114 free(client->hostname);
Jens Axboe81179ee2011-10-04 12:42:06 +0200115 if (client->argv)
116 free(client->argv);
Jens Axboeb5296dd2011-10-07 16:35:56 +0200117 if (client->name)
118 free(client->name);
Jens Axboe81179ee2011-10-04 12:42:06 +0200119
Jens Axboeb66570d2011-10-01 11:11:35 -0600120 free(client);
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200121 nr_clients--;
Jens Axboe5fd0acb2011-10-11 14:20:22 +0200122 sum_stat_clients--;
Jens Axboeb66570d2011-10-01 11:11:35 -0600123}
Jens Axboe132159a2011-09-30 15:01:32 -0600124
Jens Axboe343cb4a2012-03-09 17:16:51 +0100125void fio_put_client(struct fio_client *client)
Jens Axboe5121a9a2012-03-05 13:32:47 +0100126{
127 remove_client(client);
128}
129
Jens Axboe343cb4a2012-03-09 17:16:51 +0100130struct fio_client *fio_get_client(struct fio_client *client)
131{
132 client->refs++;
133 return client;
134}
135
Jens Axboefa2ea802011-10-08 21:07:29 +0200136static void __fio_client_add_cmd_option(struct fio_client *client,
137 const char *opt)
Jens Axboe81179ee2011-10-04 12:42:06 +0200138{
Jens Axboe39e8e012011-10-04 13:46:08 +0200139 int index;
140
141 index = client->argc++;
Jens Axboe81179ee2011-10-04 12:42:06 +0200142 client->argv = realloc(client->argv, sizeof(char *) * client->argc);
Jens Axboe39e8e012011-10-04 13:46:08 +0200143 client->argv[index] = strdup(opt);
144 dprint(FD_NET, "client: add cmd %d: %s\n", index, opt);
Jens Axboe81179ee2011-10-04 12:42:06 +0200145}
146
Jens Axboefa2ea802011-10-08 21:07:29 +0200147void fio_client_add_cmd_option(void *cookie, const char *opt)
Jens Axboe81179ee2011-10-04 12:42:06 +0200148{
Jens Axboebebe6392011-10-07 10:00:51 +0200149 struct fio_client *client = cookie;
Jens Axboe3f3a4542011-10-10 21:11:09 +0200150 struct flist_head *entry;
Jens Axboe81179ee2011-10-04 12:42:06 +0200151
Jens Axboebebe6392011-10-07 10:00:51 +0200152 if (!client || !opt)
Jens Axboefa2ea802011-10-08 21:07:29 +0200153 return;
Jens Axboe81179ee2011-10-04 12:42:06 +0200154
Jens Axboefa2ea802011-10-08 21:07:29 +0200155 __fio_client_add_cmd_option(client, opt);
Jens Axboe3f3a4542011-10-10 21:11:09 +0200156
157 /*
158 * Duplicate arguments to shared client group
159 */
160 flist_for_each(entry, &arg_list) {
161 client = flist_entry(entry, struct fio_client, arg_list);
162
163 __fio_client_add_cmd_option(client, opt);
164 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200165}
166
Jens Axboea5276612012-03-04 15:15:08 +0100167struct fio_client *fio_client_add_explicit(struct client_ops *ops,
168 const char *hostname, int type,
Jens Axboe3ec62ec2012-03-01 12:01:29 +0100169 int port)
170{
171 struct fio_client *client;
172
173 client = malloc(sizeof(*client));
174 memset(client, 0, sizeof(*client));
175
176 INIT_FLIST_HEAD(&client->list);
177 INIT_FLIST_HEAD(&client->hash_list);
178 INIT_FLIST_HEAD(&client->arg_list);
179 INIT_FLIST_HEAD(&client->eta_list);
180 INIT_FLIST_HEAD(&client->cmd_list);
181
182 client->hostname = strdup(hostname);
183
184 if (type == Fio_client_socket)
185 client->is_sock = 1;
186 else {
187 int ipv6;
188
189 ipv6 = type == Fio_client_ipv6;
190 if (fio_server_parse_host(hostname, &ipv6,
191 &client->addr.sin_addr,
192 &client->addr6.sin6_addr))
193 goto err;
194
195 client->port = port;
196 }
197
198 client->fd = -1;
Jens Axboea5276612012-03-04 15:15:08 +0100199 client->ops = ops;
Jens Axboe5121a9a2012-03-05 13:32:47 +0100200 client->refs = 1;
Jens Axboe3ec62ec2012-03-01 12:01:29 +0100201
202 __fio_client_add_cmd_option(client, "fio");
203
204 flist_add(&client->list, &client_list);
205 nr_clients++;
206 dprint(FD_NET, "client: added <%s>\n", client->hostname);
207 return client;
208err:
209 free(client);
210 return NULL;
211}
212
Jens Axboea5276612012-03-04 15:15:08 +0100213int fio_client_add(struct client_ops *ops, const char *hostname, void **cookie)
Jens Axboe132159a2011-09-30 15:01:32 -0600214{
Jens Axboe3f3a4542011-10-10 21:11:09 +0200215 struct fio_client *existing = *cookie;
Jens Axboeb66570d2011-10-01 11:11:35 -0600216 struct fio_client *client;
Jens Axboe132159a2011-09-30 15:01:32 -0600217
Jens Axboe3f3a4542011-10-10 21:11:09 +0200218 if (existing) {
219 /*
220 * We always add our "exec" name as the option, hence 1
221 * means empty.
222 */
223 if (existing->argc == 1)
224 flist_add_tail(&existing->arg_list, &arg_list);
225 else {
226 while (!flist_empty(&arg_list))
227 flist_del_init(arg_list.next);
228 }
229 }
230
Jens Axboeb66570d2011-10-01 11:11:35 -0600231 client = malloc(sizeof(*client));
Jens Axboea37f69b2011-10-01 12:24:21 -0600232 memset(client, 0, sizeof(*client));
Jens Axboe81179ee2011-10-04 12:42:06 +0200233
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200234 INIT_FLIST_HEAD(&client->list);
Jens Axboebebe6392011-10-07 10:00:51 +0200235 INIT_FLIST_HEAD(&client->hash_list);
Jens Axboe3f3a4542011-10-10 21:11:09 +0200236 INIT_FLIST_HEAD(&client->arg_list);
Jens Axboe82c1ed32011-10-10 08:56:18 +0200237 INIT_FLIST_HEAD(&client->eta_list);
Jens Axboe89c17072011-10-11 10:15:51 +0200238 INIT_FLIST_HEAD(&client->cmd_list);
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200239
Jens Axboebebe6392011-10-07 10:00:51 +0200240 if (fio_server_parse_string(hostname, &client->hostname,
241 &client->is_sock, &client->port,
Jens Axboe811826b2011-10-24 09:11:50 +0200242 &client->addr.sin_addr,
243 &client->addr6.sin6_addr,
244 &client->ipv6))
Jens Axboebebe6392011-10-07 10:00:51 +0200245 return -1;
246
Jens Axboea37f69b2011-10-01 12:24:21 -0600247 client->fd = -1;
Jens Axboea5276612012-03-04 15:15:08 +0100248 client->ops = ops;
Jens Axboe5121a9a2012-03-05 13:32:47 +0100249 client->refs = 1;
Jens Axboe81179ee2011-10-04 12:42:06 +0200250
251 __fio_client_add_cmd_option(client, "fio");
252
Jens Axboea37f69b2011-10-01 12:24:21 -0600253 flist_add(&client->list, &client_list);
254 nr_clients++;
Jens Axboebebe6392011-10-07 10:00:51 +0200255 dprint(FD_NET, "client: added <%s>\n", client->hostname);
256 *cookie = client;
257 return 0;
Jens Axboea37f69b2011-10-01 12:24:21 -0600258}
259
Jens Axboe87aa8f12011-10-06 21:24:13 +0200260static int fio_client_connect_ip(struct fio_client *client)
Jens Axboea37f69b2011-10-01 12:24:21 -0600261{
Jens Axboe811826b2011-10-24 09:11:50 +0200262 struct sockaddr *addr;
263 fio_socklen_t socklen;
264 int fd, domain;
Jens Axboe132159a2011-09-30 15:01:32 -0600265
Jens Axboe811826b2011-10-24 09:11:50 +0200266 if (client->ipv6) {
267 client->addr6.sin6_family = AF_INET6;
268 client->addr6.sin6_port = htons(client->port);
269 domain = AF_INET6;
270 addr = (struct sockaddr *) &client->addr6;
271 socklen = sizeof(client->addr6);
272 } else {
273 client->addr.sin_family = AF_INET;
274 client->addr.sin_port = htons(client->port);
275 domain = AF_INET;
276 addr = (struct sockaddr *) &client->addr;
277 socklen = sizeof(client->addr);
278 }
Jens Axboe132159a2011-09-30 15:01:32 -0600279
Jens Axboe811826b2011-10-24 09:11:50 +0200280 fd = socket(domain, SOCK_STREAM, 0);
Jens Axboe132159a2011-09-30 15:01:32 -0600281 if (fd < 0) {
Jens Axboe25095e12012-03-09 17:09:55 +0100282 int ret = -errno;
283
Jens Axboe132159a2011-09-30 15:01:32 -0600284 log_err("fio: socket: %s\n", strerror(errno));
Jens Axboe25095e12012-03-09 17:09:55 +0100285 return ret;
Jens Axboe132159a2011-09-30 15:01:32 -0600286 }
287
Jens Axboe811826b2011-10-24 09:11:50 +0200288 if (connect(fd, addr, socklen) < 0) {
Jens Axboe25095e12012-03-09 17:09:55 +0100289 int ret = -errno;
290
Jens Axboe132159a2011-09-30 15:01:32 -0600291 log_err("fio: connect: %s\n", strerror(errno));
Jens Axboea7de0a12011-10-07 12:55:14 +0200292 log_err("fio: failed to connect to %s:%u\n", client->hostname,
293 client->port);
Jens Axboeb94cba42011-10-06 21:27:10 +0200294 close(fd);
Jens Axboe25095e12012-03-09 17:09:55 +0100295 return ret;
Jens Axboe132159a2011-09-30 15:01:32 -0600296 }
297
Jens Axboe87aa8f12011-10-06 21:24:13 +0200298 return fd;
299}
300
301static int fio_client_connect_sock(struct fio_client *client)
302{
303 struct sockaddr_un *addr = &client->addr_un;
304 fio_socklen_t len;
305 int fd;
306
307 memset(addr, 0, sizeof(*addr));
308 addr->sun_family = AF_UNIX;
309 strcpy(addr->sun_path, client->hostname);
310
311 fd = socket(AF_UNIX, SOCK_STREAM, 0);
312 if (fd < 0) {
Jens Axboe25095e12012-03-09 17:09:55 +0100313 int ret = -errno;
314
Jens Axboe87aa8f12011-10-06 21:24:13 +0200315 log_err("fio: socket: %s\n", strerror(errno));
Jens Axboe25095e12012-03-09 17:09:55 +0100316 return ret;
Jens Axboe87aa8f12011-10-06 21:24:13 +0200317 }
318
319 len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
320 if (connect(fd, (struct sockaddr *) addr, len) < 0) {
Jens Axboe25095e12012-03-09 17:09:55 +0100321 int ret = -errno;
322
Jens Axboe87aa8f12011-10-06 21:24:13 +0200323 log_err("fio: connect; %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200324 close(fd);
Jens Axboe25095e12012-03-09 17:09:55 +0100325 return ret;
Jens Axboe87aa8f12011-10-06 21:24:13 +0200326 }
327
328 return fd;
329}
330
Jens Axboe2f99deb2012-03-09 14:37:29 +0100331int fio_client_connect(struct fio_client *client)
Jens Axboe87aa8f12011-10-06 21:24:13 +0200332{
333 int fd;
334
335 dprint(FD_NET, "client: connect to host %s\n", client->hostname);
336
Jens Axboe87aa8f12011-10-06 21:24:13 +0200337 if (client->is_sock)
338 fd = fio_client_connect_sock(client);
339 else
340 fd = fio_client_connect_ip(client);
341
Jens Axboe89c17072011-10-11 10:15:51 +0200342 dprint(FD_NET, "client: %s connected %d\n", client->hostname, fd);
343
Jens Axboe87aa8f12011-10-06 21:24:13 +0200344 if (fd < 0)
Jens Axboea1a3ed52012-03-09 17:00:01 +0100345 return fd;
Jens Axboe87aa8f12011-10-06 21:24:13 +0200346
Jens Axboeb66570d2011-10-01 11:11:35 -0600347 client->fd = fd;
Jens Axboebebe6392011-10-07 10:00:51 +0200348 fio_client_add_hash(client);
Jens Axboe81179ee2011-10-04 12:42:06 +0200349 client->state = Client_connected;
Jens Axboe132159a2011-09-30 15:01:32 -0600350 return 0;
351}
352
Jens Axboe2f99deb2012-03-09 14:37:29 +0100353void fio_client_terminate(struct fio_client *client)
354{
355 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_QUIT, 0, NULL);
356}
357
Jens Axboecc0df002011-10-03 20:53:32 +0200358void fio_clients_terminate(void)
359{
360 struct flist_head *entry;
361 struct fio_client *client;
362
Jens Axboe60efd142011-10-04 13:30:11 +0200363 dprint(FD_NET, "client: terminate clients\n");
364
Jens Axboecc0df002011-10-03 20:53:32 +0200365 flist_for_each(entry, &client_list) {
366 client = flist_entry(entry, struct fio_client, list);
Jens Axboe2f99deb2012-03-09 14:37:29 +0100367 fio_client_terminate(client);
Jens Axboecc0df002011-10-03 20:53:32 +0200368 }
369}
370
371static void sig_int(int sig)
372{
Jens Axboebebe6392011-10-07 10:00:51 +0200373 dprint(FD_NET, "client: got signal %d\n", sig);
Jens Axboecc0df002011-10-03 20:53:32 +0200374 fio_clients_terminate();
375}
376
377static void client_signal_handler(void)
378{
379 struct sigaction act;
380
381 memset(&act, 0, sizeof(act));
382 act.sa_handler = sig_int;
383 act.sa_flags = SA_RESTART;
384 sigaction(SIGINT, &act, NULL);
385
386 memset(&act, 0, sizeof(act));
387 act.sa_handler = sig_int;
388 act.sa_flags = SA_RESTART;
389 sigaction(SIGTERM, &act, NULL);
390}
391
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200392static void probe_client(struct fio_client *client)
393{
Jens Axboe60efd142011-10-04 13:30:11 +0200394 dprint(FD_NET, "client: send probe\n");
395
Jens Axboe89c17072011-10-11 10:15:51 +0200396 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_PROBE, 0, &client->cmd_list);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200397}
398
Jens Axboe81179ee2011-10-04 12:42:06 +0200399static int send_client_cmd_line(struct fio_client *client)
400{
Jens Axboefa2ea802011-10-08 21:07:29 +0200401 struct cmd_single_line_pdu *cslp;
402 struct cmd_line_pdu *clp;
403 unsigned long offset;
Jens Axboe7f868312011-10-10 09:55:21 +0200404 unsigned int *lens;
Jens Axboefa2ea802011-10-08 21:07:29 +0200405 void *pdu;
406 size_t mem;
Jens Axboe81179ee2011-10-04 12:42:06 +0200407 int i, ret;
408
Jens Axboe39e8e012011-10-04 13:46:08 +0200409 dprint(FD_NET, "client: send cmdline %d\n", client->argc);
Jens Axboe60efd142011-10-04 13:30:11 +0200410
Jens Axboe7f868312011-10-10 09:55:21 +0200411 lens = malloc(client->argc * sizeof(unsigned int));
412
Jens Axboefa2ea802011-10-08 21:07:29 +0200413 /*
414 * Find out how much mem we need
415 */
Jens Axboe7f868312011-10-10 09:55:21 +0200416 for (i = 0, mem = 0; i < client->argc; i++) {
417 lens[i] = strlen(client->argv[i]) + 1;
418 mem += lens[i];
419 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200420
Jens Axboefa2ea802011-10-08 21:07:29 +0200421 /*
422 * We need one cmd_line_pdu, and argc number of cmd_single_line_pdu
423 */
424 mem += sizeof(*clp) + (client->argc * sizeof(*cslp));
425
426 pdu = malloc(mem);
427 clp = pdu;
428 offset = sizeof(*clp);
429
430 for (i = 0; i < client->argc; i++) {
Jens Axboe7f868312011-10-10 09:55:21 +0200431 uint16_t arg_len = lens[i];
Jens Axboefa2ea802011-10-08 21:07:29 +0200432
433 cslp = pdu + offset;
434 strcpy((char *) cslp->text, client->argv[i]);
435 cslp->len = cpu_to_le16(arg_len);
436 offset += sizeof(*cslp) + arg_len;
437 }
438
Jens Axboe7f868312011-10-10 09:55:21 +0200439 free(lens);
Jens Axboefa2ea802011-10-08 21:07:29 +0200440 clp->lines = cpu_to_le16(client->argc);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200441 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOBLINE, pdu, mem, 0);
Jens Axboe81179ee2011-10-04 12:42:06 +0200442 free(pdu);
443 return ret;
444}
445
Jens Axboea37f69b2011-10-01 12:24:21 -0600446int fio_clients_connect(void)
447{
448 struct fio_client *client;
449 struct flist_head *entry, *tmp;
450 int ret;
451
Bruce Cran93bcfd22012-02-20 20:18:19 +0100452#ifdef WIN32
453 WSADATA wsd;
454 WSAStartup(MAKEWORD(2,2), &wsd);
455#endif
456
Jens Axboe60efd142011-10-04 13:30:11 +0200457 dprint(FD_NET, "client: connect all\n");
458
Jens Axboecc0df002011-10-03 20:53:32 +0200459 client_signal_handler();
460
Jens Axboea37f69b2011-10-01 12:24:21 -0600461 flist_for_each_safe(entry, tmp, &client_list) {
462 client = flist_entry(entry, struct fio_client, list);
463
464 ret = fio_client_connect(client);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200465 if (ret) {
Jens Axboea37f69b2011-10-01 12:24:21 -0600466 remove_client(client);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200467 continue;
468 }
469
470 probe_client(client);
Jens Axboe81179ee2011-10-04 12:42:06 +0200471
472 if (client->argc > 1)
473 send_client_cmd_line(client);
Jens Axboea37f69b2011-10-01 12:24:21 -0600474 }
475
476 return !nr_clients;
477}
478
Jens Axboeb9d2f302012-03-08 20:36:28 +0100479int fio_start_client(struct fio_client *client)
480{
481 dprint(FD_NET, "client: start %s\n", client->hostname);
482 return fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_RUN, 0, NULL);
483}
484
485int fio_start_all_clients(void)
486{
487 struct fio_client *client;
488 struct flist_head *entry, *tmp;
489 int ret;
490
491 dprint(FD_NET, "client: start all\n");
492
493 flist_for_each_safe(entry, tmp, &client_list) {
494 client = flist_entry(entry, struct fio_client, list);
495
496 ret = fio_start_client(client);
497 if (ret) {
498 remove_client(client);
499 continue;
500 }
501 }
502
503 return flist_empty(&client_list);
504}
505
Jens Axboe132159a2011-09-30 15:01:32 -0600506/*
507 * Send file contents to server backend. We could use sendfile(), but to remain
508 * more portable lets just read/write the darn thing.
509 */
Jens Axboe9988ca72012-03-09 15:14:06 +0100510static int __fio_client_send_ini(struct fio_client *client, const char *filename)
Jens Axboe132159a2011-09-30 15:01:32 -0600511{
512 struct stat sb;
513 char *p, *buf;
514 off_t len;
515 int fd, ret;
516
Jens Axboe46c48f12011-10-01 12:50:51 -0600517 dprint(FD_NET, "send ini %s to %s\n", filename, client->hostname);
518
Jens Axboe132159a2011-09-30 15:01:32 -0600519 fd = open(filename, O_RDONLY);
520 if (fd < 0) {
Jens Axboe25095e12012-03-09 17:09:55 +0100521 int ret = -errno;
522
Jens Axboee951bdc2011-10-05 21:58:45 +0200523 log_err("fio: job file <%s> open: %s\n", filename, strerror(errno));
Jens Axboe25095e12012-03-09 17:09:55 +0100524 return ret;
Jens Axboe132159a2011-09-30 15:01:32 -0600525 }
526
527 if (fstat(fd, &sb) < 0) {
Jens Axboe25095e12012-03-09 17:09:55 +0100528 int ret = -errno;
529
Jens Axboe132159a2011-09-30 15:01:32 -0600530 log_err("fio: job file stat: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200531 close(fd);
Jens Axboe25095e12012-03-09 17:09:55 +0100532 return ret;
Jens Axboe132159a2011-09-30 15:01:32 -0600533 }
534
535 buf = malloc(sb.st_size);
536
537 len = sb.st_size;
538 p = buf;
539 do {
540 ret = read(fd, p, len);
541 if (ret > 0) {
542 len -= ret;
543 if (!len)
544 break;
545 p += ret;
546 continue;
547 } else if (!ret)
548 break;
549 else if (errno == EAGAIN || errno == EINTR)
550 continue;
551 } while (1);
552
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200553 if (len) {
554 log_err("fio: failed reading job file %s\n", filename);
Jens Axboeb94cba42011-10-06 21:27:10 +0200555 close(fd);
Jens Axboec524ef72011-10-07 12:23:34 +0200556 free(buf);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200557 return 1;
558 }
559
Jens Axboec2cb6862012-02-23 20:56:12 +0100560 client->sent_job = 1;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200561 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, buf, sb.st_size, 0);
Jens Axboe132159a2011-09-30 15:01:32 -0600562 free(buf);
Jens Axboeb94cba42011-10-06 21:27:10 +0200563 close(fd);
Jens Axboe132159a2011-09-30 15:01:32 -0600564 return ret;
565}
Jens Axboe37db14f2011-09-30 17:00:42 -0600566
Jens Axboe9988ca72012-03-09 15:14:06 +0100567int fio_client_send_ini(struct fio_client *client, const char *filename)
568{
Jens Axboe25095e12012-03-09 17:09:55 +0100569 int ret;
570
571 ret = __fio_client_send_ini(client, filename);
572 if (ret) {
Jens Axboe9988ca72012-03-09 15:14:06 +0100573 remove_client(client);
Jens Axboe25095e12012-03-09 17:09:55 +0100574 return ret;
Jens Axboe9988ca72012-03-09 15:14:06 +0100575 }
576
577 client->sent_job = 1;
Jens Axboe25095e12012-03-09 17:09:55 +0100578 return ret;
Jens Axboe9988ca72012-03-09 15:14:06 +0100579}
580
Jens Axboea37f69b2011-10-01 12:24:21 -0600581int fio_clients_send_ini(const char *filename)
582{
583 struct fio_client *client;
584 struct flist_head *entry, *tmp;
585
586 flist_for_each_safe(entry, tmp, &client_list) {
587 client = flist_entry(entry, struct fio_client, list);
588
Jens Axboe9988ca72012-03-09 15:14:06 +0100589 fio_client_send_ini(client, filename);
Jens Axboea37f69b2011-10-01 12:24:21 -0600590 }
591
592 return !nr_clients;
593}
594
Jens Axboea64e88d2011-10-03 14:20:01 +0200595static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
596{
597 dst->max_val = le64_to_cpu(src->max_val);
598 dst->min_val = le64_to_cpu(src->min_val);
599 dst->samples = le64_to_cpu(src->samples);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200600
601 /*
602 * Floats arrive as IEEE 754 encoded uint64_t, convert back to double
603 */
604 dst->mean.u.f = fio_uint64_to_double(le64_to_cpu(dst->mean.u.i));
605 dst->S.u.f = fio_uint64_to_double(le64_to_cpu(dst->S.u.i));
Jens Axboea64e88d2011-10-03 14:20:01 +0200606}
607
608static void convert_ts(struct thread_stat *dst, struct thread_stat *src)
609{
610 int i, j;
611
612 dst->error = le32_to_cpu(src->error);
613 dst->groupid = le32_to_cpu(src->groupid);
614 dst->pid = le32_to_cpu(src->pid);
615 dst->members = le32_to_cpu(src->members);
616
617 for (i = 0; i < 2; i++) {
618 convert_io_stat(&dst->clat_stat[i], &src->clat_stat[i]);
619 convert_io_stat(&dst->slat_stat[i], &src->slat_stat[i]);
620 convert_io_stat(&dst->lat_stat[i], &src->lat_stat[i]);
621 convert_io_stat(&dst->bw_stat[i], &src->bw_stat[i]);
622 }
623
624 dst->usr_time = le64_to_cpu(src->usr_time);
625 dst->sys_time = le64_to_cpu(src->sys_time);
626 dst->ctx = le64_to_cpu(src->ctx);
627 dst->minf = le64_to_cpu(src->minf);
628 dst->majf = le64_to_cpu(src->majf);
629 dst->clat_percentiles = le64_to_cpu(src->clat_percentiles);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200630
631 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
632 fio_fp64_t *fps = &src->percentile_list[i];
633 fio_fp64_t *fpd = &dst->percentile_list[i];
634
635 fpd->u.f = fio_uint64_to_double(le64_to_cpu(fps->u.i));
636 }
Jens Axboea64e88d2011-10-03 14:20:01 +0200637
638 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
639 dst->io_u_map[i] = le32_to_cpu(src->io_u_map[i]);
640 dst->io_u_submit[i] = le32_to_cpu(src->io_u_submit[i]);
641 dst->io_u_complete[i] = le32_to_cpu(src->io_u_complete[i]);
642 }
643
644 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
645 dst->io_u_lat_u[i] = le32_to_cpu(src->io_u_lat_u[i]);
646 dst->io_u_lat_m[i] = le32_to_cpu(src->io_u_lat_m[i]);
647 }
648
649 for (i = 0; i < 2; i++)
650 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
651 dst->io_u_plat[i][j] = le32_to_cpu(src->io_u_plat[i][j]);
652
653 for (i = 0; i < 3; i++) {
654 dst->total_io_u[i] = le64_to_cpu(src->total_io_u[i]);
Jens Axboe93eee042011-10-03 21:08:48 +0200655 dst->short_io_u[i] = le64_to_cpu(src->short_io_u[i]);
Jens Axboea64e88d2011-10-03 14:20:01 +0200656 }
657
658 dst->total_submit = le64_to_cpu(src->total_submit);
659 dst->total_complete = le64_to_cpu(src->total_complete);
660
661 for (i = 0; i < 2; i++) {
662 dst->io_bytes[i] = le64_to_cpu(src->io_bytes[i]);
663 dst->runtime[i] = le64_to_cpu(src->runtime[i]);
664 }
665
666 dst->total_run_time = le64_to_cpu(src->total_run_time);
667 dst->continue_on_error = le16_to_cpu(src->continue_on_error);
668 dst->total_err_count = le64_to_cpu(src->total_err_count);
Jens Axboeddcc0b62011-10-03 14:45:27 +0200669 dst->first_error = le32_to_cpu(src->first_error);
670 dst->kb_base = le32_to_cpu(src->kb_base);
Jens Axboea64e88d2011-10-03 14:20:01 +0200671}
672
673static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
674{
675 int i;
676
677 for (i = 0; i < 2; i++) {
678 dst->max_run[i] = le64_to_cpu(src->max_run[i]);
679 dst->min_run[i] = le64_to_cpu(src->min_run[i]);
680 dst->max_bw[i] = le64_to_cpu(src->max_bw[i]);
681 dst->min_bw[i] = le64_to_cpu(src->min_bw[i]);
682 dst->io_kb[i] = le64_to_cpu(src->io_kb[i]);
683 dst->agg[i] = le64_to_cpu(src->agg[i]);
684 }
685
686 dst->kb_base = le32_to_cpu(src->kb_base);
687 dst->groupid = le32_to_cpu(src->groupid);
688}
689
Jens Axboe89e5fad2012-03-05 09:21:12 +0100690static void handle_ts(struct fio_client *client, struct fio_net_cmd *cmd)
Jens Axboea64e88d2011-10-03 14:20:01 +0200691{
692 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
693
Jens Axboea64e88d2011-10-03 14:20:01 +0200694 show_thread_status(&p->ts, &p->rs);
Jens Axboe37f0c1a2011-10-11 14:08:33 +0200695
696 if (sum_stat_clients == 1)
697 return;
698
699 sum_thread_stats(&client_ts, &p->ts, sum_stat_nr);
700 sum_group_stats(&client_gs, &p->rs);
701
702 client_ts.members++;
703 client_ts.groupid = p->ts.groupid;
704
705 if (++sum_stat_nr == sum_stat_clients) {
706 strcpy(client_ts.name, "All clients");
707 show_thread_status(&client_ts, &client_gs);
708 }
Jens Axboea64e88d2011-10-03 14:20:01 +0200709}
710
Jens Axboe89e5fad2012-03-05 09:21:12 +0100711static void handle_gs(struct fio_client *client, struct fio_net_cmd *cmd)
Jens Axboea64e88d2011-10-03 14:20:01 +0200712{
713 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
714
Jens Axboea64e88d2011-10-03 14:20:01 +0200715 show_group_stats(gs);
716}
717
Jens Axboe3bf236c2012-03-03 20:35:50 +0100718static void handle_text(struct fio_client *client, struct fio_net_cmd *cmd)
719{
720 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
721 const char *buf = (const char *) pdu->buf;
722 const char *name;
723 int fio_unused ret;
724
725 name = client->name ? client->name : client->hostname;
726
727 if (!client->skip_newline)
728 fprintf(f_out, "<%s> ", name);
729 ret = fwrite(buf, pdu->buf_len, 1, f_out);
730 fflush(f_out);
731 client->skip_newline = strchr(buf, '\n') == NULL;
732}
733
Jens Axboed09a64a2011-10-13 11:38:56 +0200734static void convert_agg(struct disk_util_agg *agg)
735{
736 int i;
737
738 for (i = 0; i < 2; i++) {
739 agg->ios[i] = le32_to_cpu(agg->ios[i]);
740 agg->merges[i] = le32_to_cpu(agg->merges[i]);
741 agg->sectors[i] = le64_to_cpu(agg->sectors[i]);
742 agg->ticks[i] = le32_to_cpu(agg->ticks[i]);
743 }
744
745 agg->io_ticks = le32_to_cpu(agg->io_ticks);
746 agg->time_in_queue = le32_to_cpu(agg->time_in_queue);
747 agg->slavecount = le32_to_cpu(agg->slavecount);
Anton Blanchard823ba542011-11-07 14:16:26 +0100748 agg->max_util.u.f = fio_uint64_to_double(__le64_to_cpu(agg->max_util.u.i));
Jens Axboed09a64a2011-10-13 11:38:56 +0200749}
750
751static void convert_dus(struct disk_util_stat *dus)
752{
753 int i;
754
755 for (i = 0; i < 2; i++) {
756 dus->ios[i] = le32_to_cpu(dus->ios[i]);
757 dus->merges[i] = le32_to_cpu(dus->merges[i]);
758 dus->sectors[i] = le64_to_cpu(dus->sectors[i]);
759 dus->ticks[i] = le32_to_cpu(dus->ticks[i]);
760 }
761
762 dus->io_ticks = le32_to_cpu(dus->io_ticks);
763 dus->time_in_queue = le32_to_cpu(dus->time_in_queue);
764 dus->msec = le64_to_cpu(dus->msec);
765}
766
767static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd)
768{
769 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
770
Jens Axboed09a64a2011-10-13 11:38:56 +0200771 if (!client->disk_stats_shown) {
772 client->disk_stats_shown = 1;
773 log_info("\nDisk stats (read/write):\n");
774 }
775
Jens Axboef2f788d2011-10-13 14:03:52 +0200776 print_disk_util(&du->dus, &du->agg, terse_output);
Jens Axboed09a64a2011-10-13 11:38:56 +0200777}
778
Jens Axboe3bf236c2012-03-03 20:35:50 +0100779static void convert_jobs_eta(struct jobs_eta *je)
Jens Axboecf451d12011-10-03 16:48:30 +0200780{
Jens Axboecf451d12011-10-03 16:48:30 +0200781 int i;
782
783 je->nr_running = le32_to_cpu(je->nr_running);
784 je->nr_ramp = le32_to_cpu(je->nr_ramp);
785 je->nr_pending = le32_to_cpu(je->nr_pending);
786 je->files_open = le32_to_cpu(je->files_open);
Jens Axboecf451d12011-10-03 16:48:30 +0200787
788 for (i = 0; i < 2; i++) {
Jens Axboe3e47bd22012-02-29 13:45:02 +0100789 je->m_rate[i] = le32_to_cpu(je->m_rate[i]);
790 je->t_rate[i] = le32_to_cpu(je->t_rate[i]);
791 je->m_iops[i] = le32_to_cpu(je->m_iops[i]);
792 je->t_iops[i] = le32_to_cpu(je->t_iops[i]);
Jens Axboecf451d12011-10-03 16:48:30 +0200793 je->rate[i] = le32_to_cpu(je->rate[i]);
794 je->iops[i] = le32_to_cpu(je->iops[i]);
795 }
796
Jens Axboeb51eedb2011-10-09 12:13:39 +0200797 je->elapsed_sec = le64_to_cpu(je->elapsed_sec);
Jens Axboecf451d12011-10-03 16:48:30 +0200798 je->eta_sec = le64_to_cpu(je->eta_sec);
Jens Axboe8c621fb2012-03-08 12:30:48 +0100799 je->nr_threads = le32_to_cpu(je->nr_threads);
Jens Axboe48fbb462011-10-09 12:19:08 +0200800}
Jens Axboecf451d12011-10-03 16:48:30 +0200801
Jens Axboe3e47bd22012-02-29 13:45:02 +0100802void fio_client_sum_jobs_eta(struct jobs_eta *dst, struct jobs_eta *je)
Jens Axboe48fbb462011-10-09 12:19:08 +0200803{
Jens Axboe48fbb462011-10-09 12:19:08 +0200804 int i;
805
806 dst->nr_running += je->nr_running;
807 dst->nr_ramp += je->nr_ramp;
808 dst->nr_pending += je->nr_pending;
809 dst->files_open += je->files_open;
Jens Axboe48fbb462011-10-09 12:19:08 +0200810
811 for (i = 0; i < 2; i++) {
Jens Axboe3e47bd22012-02-29 13:45:02 +0100812 dst->m_rate[i] += je->m_rate[i];
813 dst->t_rate[i] += je->t_rate[i];
814 dst->m_iops[i] += je->m_iops[i];
815 dst->t_iops[i] += je->t_iops[i];
Jens Axboe48fbb462011-10-09 12:19:08 +0200816 dst->rate[i] += je->rate[i];
817 dst->iops[i] += je->iops[i];
818 }
819
820 dst->elapsed_sec += je->elapsed_sec;
821
822 if (je->eta_sec > dst->eta_sec)
823 dst->eta_sec = je->eta_sec;
Jens Axboe8c621fb2012-03-08 12:30:48 +0100824
825 dst->nr_threads += je->nr_threads;
826 /* we need to handle je->run_str too ... */
Jens Axboe48fbb462011-10-09 12:19:08 +0200827}
828
Jens Axboea5276612012-03-04 15:15:08 +0100829void fio_client_dec_jobs_eta(struct client_eta *eta, client_eta_op eta_fn)
Jens Axboe82c1ed32011-10-10 08:56:18 +0200830{
831 if (!--eta->pending) {
Jens Axboea5276612012-03-04 15:15:08 +0100832 eta_fn(&eta->eta);
Jens Axboe82c1ed32011-10-10 08:56:18 +0200833 free(eta);
834 }
835}
836
Jens Axboe89c17072011-10-11 10:15:51 +0200837static void remove_reply_cmd(struct fio_client *client, struct fio_net_cmd *cmd)
838{
839 struct fio_net_int_cmd *icmd = NULL;
840 struct flist_head *entry;
841
842 flist_for_each(entry, &client->cmd_list) {
843 icmd = flist_entry(entry, struct fio_net_int_cmd, list);
844
Jens Axboedf380932011-10-11 14:25:08 +0200845 if (cmd->tag == (uintptr_t) icmd)
Jens Axboe89c17072011-10-11 10:15:51 +0200846 break;
847
848 icmd = NULL;
849 }
850
851 if (!icmd) {
852 log_err("fio: client: unable to find matching tag\n");
853 return;
854 }
855
856 flist_del(&icmd->list);
857 cmd->tag = icmd->saved_tag;
858 free(icmd);
859}
860
Jens Axboe82c1ed32011-10-10 08:56:18 +0200861static void handle_eta(struct fio_client *client, struct fio_net_cmd *cmd)
Jens Axboe48fbb462011-10-09 12:19:08 +0200862{
863 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
Jens Axboedf380932011-10-11 14:25:08 +0200864 struct client_eta *eta = (struct client_eta *) (uintptr_t) cmd->tag;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200865
866 dprint(FD_NET, "client: got eta tag %p, %d\n", eta, eta->pending);
Jens Axboe48fbb462011-10-09 12:19:08 +0200867
Jens Axboef77d2672011-10-10 14:36:07 +0200868 assert(client->eta_in_flight == eta);
869
870 client->eta_in_flight = NULL;
Jens Axboe82c1ed32011-10-10 08:56:18 +0200871 flist_del_init(&client->eta_list);
872
Jens Axboe2f99deb2012-03-09 14:37:29 +0100873 if (client->ops->jobs_eta)
874 client->ops->jobs_eta(client, je);
875
Jens Axboe3e47bd22012-02-29 13:45:02 +0100876 fio_client_sum_jobs_eta(&eta->eta, je);
Jens Axboea5276612012-03-04 15:15:08 +0100877 fio_client_dec_jobs_eta(eta, client->ops->eta);
Jens Axboecf451d12011-10-03 16:48:30 +0200878}
879
Jens Axboeb5296dd2011-10-07 16:35:56 +0200880static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd)
Jens Axboe2e03b4b2011-10-04 09:00:40 +0200881{
882 struct cmd_probe_pdu *probe = (struct cmd_probe_pdu *) cmd->payload;
Jens Axboed2333352011-10-11 15:07:23 +0200883 const char *os, *arch;
884 char bit[16];
Jens Axboe2e03b4b2011-10-04 09:00:40 +0200885
Jens Axboecca84642011-10-07 12:47:57 +0200886 os = fio_get_os_string(probe->os);
887 if (!os)
888 os = "unknown";
889
890 arch = fio_get_arch_string(probe->arch);
891 if (!arch)
892 os = "unknown";
893
Jens Axboed2333352011-10-11 15:07:23 +0200894 sprintf(bit, "%d-bit", probe->bpp * 8);
Jens Axboe38fdef22011-10-11 14:30:06 +0200895
896 log_info("hostname=%s, be=%u, %s, os=%s, arch=%s, fio=%u.%u.%u\n",
897 probe->hostname, probe->bigendian, bit, os, arch,
898 probe->fio_major, probe->fio_minor, probe->fio_patch);
Jens Axboeb5296dd2011-10-07 16:35:56 +0200899
900 if (!client->name)
901 client->name = strdup((char *) probe->hostname);
Jens Axboe2e03b4b2011-10-04 09:00:40 +0200902}
903
Jens Axboe11e950b2011-10-16 21:34:14 +0200904static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd)
905{
906 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
907
908 client->state = Client_started;
909 client->jobs = le32_to_cpu(pdu->jobs);
910}
911
912static void handle_stop(struct fio_client *client, struct fio_net_cmd *cmd)
913{
Jens Axboe498c92c2011-10-17 09:14:42 +0200914 if (client->error)
915 log_info("client <%s>: exited with error %d\n", client->hostname, client->error);
Jens Axboe11e950b2011-10-16 21:34:14 +0200916}
917
Jens Axboe6b79c802012-03-08 10:51:36 +0100918static void convert_stop(struct fio_net_cmd *cmd)
919{
920 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
921
922 pdu->error = le32_to_cpu(pdu->error);
923}
924
Jens Axboe084d1c62012-03-03 20:28:07 +0100925static void convert_text(struct fio_net_cmd *cmd)
926{
927 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
928
929 pdu->level = le32_to_cpu(pdu->level);
930 pdu->buf_len = le32_to_cpu(pdu->buf_len);
931 pdu->log_sec = le64_to_cpu(pdu->log_sec);
932 pdu->log_usec = le64_to_cpu(pdu->log_usec);
933}
934
Jens Axboea5276612012-03-04 15:15:08 +0100935int fio_handle_client(struct fio_client *client)
Jens Axboe37db14f2011-09-30 17:00:42 -0600936{
Jens Axboea5276612012-03-04 15:15:08 +0100937 struct client_ops *ops = client->ops;
Jens Axboe37db14f2011-09-30 17:00:42 -0600938 struct fio_net_cmd *cmd;
939
Jens Axboe60efd142011-10-04 13:30:11 +0200940 dprint(FD_NET, "client: handle %s\n", client->hostname);
941
Jens Axboee951bdc2011-10-05 21:58:45 +0200942 cmd = fio_net_recv_cmd(client->fd);
943 if (!cmd)
944 return 0;
Jens Axboec2c94582011-10-05 20:31:30 +0200945
Jens Axboeb9d2f302012-03-08 20:36:28 +0100946 dprint(FD_NET, "client: got cmd op %s from %s (pdu=%u)\n",
947 fio_server_op(cmd->opcode), client->hostname, cmd->pdu_len);
Jens Axboe46c48f12011-10-01 12:50:51 -0600948
Jens Axboee951bdc2011-10-05 21:58:45 +0200949 switch (cmd->opcode) {
950 case FIO_NET_CMD_QUIT:
Jens Axboe3ec62ec2012-03-01 12:01:29 +0100951 if (ops->quit)
952 ops->quit(client);
Jens Axboee951bdc2011-10-05 21:58:45 +0200953 remove_client(client);
954 free(cmd);
955 break;
Jens Axboe084d1c62012-03-03 20:28:07 +0100956 case FIO_NET_CMD_TEXT:
957 convert_text(cmd);
958 ops->text_op(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +0200959 free(cmd);
960 break;
Jens Axboe3bf236c2012-03-03 20:35:50 +0100961 case FIO_NET_CMD_DU: {
962 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
963
964 convert_dus(&du->dus);
965 convert_agg(&du->agg);
966
Stephen M. Camerondd366722012-02-24 08:17:30 +0100967 ops->disk_util(client, cmd);
Jens Axboed09a64a2011-10-13 11:38:56 +0200968 free(cmd);
969 break;
Jens Axboe3bf236c2012-03-03 20:35:50 +0100970 }
971 case FIO_NET_CMD_TS: {
972 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
973
974 convert_ts(&p->ts, &p->ts);
975 convert_gs(&p->rs, &p->rs);
976
Jens Axboe89e5fad2012-03-05 09:21:12 +0100977 ops->thread_status(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +0200978 free(cmd);
979 break;
Jens Axboe3bf236c2012-03-03 20:35:50 +0100980 }
981 case FIO_NET_CMD_GS: {
982 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
983
984 convert_gs(gs, gs);
985
Jens Axboe89e5fad2012-03-05 09:21:12 +0100986 ops->group_stats(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +0200987 free(cmd);
988 break;
Jens Axboe3bf236c2012-03-03 20:35:50 +0100989 }
990 case FIO_NET_CMD_ETA: {
991 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
992
Jens Axboe89c17072011-10-11 10:15:51 +0200993 remove_reply_cmd(client, cmd);
Jens Axboe3bf236c2012-03-03 20:35:50 +0100994 convert_jobs_eta(je);
Jens Axboea5276612012-03-04 15:15:08 +0100995 handle_eta(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +0200996 free(cmd);
997 break;
Jens Axboe3bf236c2012-03-03 20:35:50 +0100998 }
Jens Axboee951bdc2011-10-05 21:58:45 +0200999 case FIO_NET_CMD_PROBE:
Jens Axboe89c17072011-10-11 10:15:51 +02001000 remove_reply_cmd(client, cmd);
Stephen M. Camerondd366722012-02-24 08:17:30 +01001001 ops->probe(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +02001002 free(cmd);
1003 break;
Jens Axboe5d7793a2012-03-08 19:59:16 +01001004 case FIO_NET_CMD_SERVER_START:
Jens Axboe01be0382011-10-15 14:43:41 +02001005 client->state = Client_running;
1006 free(cmd);
1007 break;
Jens Axboee951bdc2011-10-05 21:58:45 +02001008 case FIO_NET_CMD_START:
Jens Axboe11e950b2011-10-16 21:34:14 +02001009 handle_start(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +02001010 free(cmd);
1011 break;
Jens Axboe6b79c802012-03-08 10:51:36 +01001012 case FIO_NET_CMD_STOP: {
1013 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
1014
1015 convert_stop(cmd);
1016 client->state = Client_stopped;
1017 client->error = pdu->error;
1018 ops->stop(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +02001019 free(cmd);
1020 break;
Jens Axboe6b79c802012-03-08 10:51:36 +01001021 }
Jens Axboe807f9972012-03-02 10:25:24 +01001022 case FIO_NET_CMD_ADD_JOB:
1023 if (ops->add_job)
1024 ops->add_job(client, cmd);
1025 free(cmd);
1026 break;
Jens Axboee951bdc2011-10-05 21:58:45 +02001027 default:
Jens Axboe89c17072011-10-11 10:15:51 +02001028 log_err("fio: unknown client op: %s\n", fio_server_op(cmd->opcode));
Jens Axboee951bdc2011-10-05 21:58:45 +02001029 free(cmd);
1030 break;
Jens Axboe37db14f2011-09-30 17:00:42 -06001031 }
1032
Jens Axboee951bdc2011-10-05 21:58:45 +02001033 return 1;
Jens Axboe37db14f2011-09-30 17:00:42 -06001034}
Jens Axboeb66570d2011-10-01 11:11:35 -06001035
Jens Axboea5276612012-03-04 15:15:08 +01001036static void request_client_etas(struct client_ops *ops)
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001037{
1038 struct fio_client *client;
1039 struct flist_head *entry;
1040 struct client_eta *eta;
Jens Axboe82c1ed32011-10-10 08:56:18 +02001041 int skipped = 0;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001042
1043 dprint(FD_NET, "client: request eta (%d)\n", nr_clients);
1044
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001045 eta = malloc(sizeof(*eta));
1046 memset(&eta->eta, 0, sizeof(eta->eta));
1047 eta->pending = nr_clients;
1048
1049 flist_for_each(entry, &client_list) {
1050 client = flist_entry(entry, struct fio_client, list);
1051
Jens Axboe82c1ed32011-10-10 08:56:18 +02001052 if (!flist_empty(&client->eta_list)) {
1053 skipped++;
1054 continue;
1055 }
Jens Axboe01be0382011-10-15 14:43:41 +02001056 if (client->state != Client_running)
1057 continue;
Jens Axboe82c1ed32011-10-10 08:56:18 +02001058
Jens Axboef77d2672011-10-10 14:36:07 +02001059 assert(!client->eta_in_flight);
Jens Axboe82c1ed32011-10-10 08:56:18 +02001060 flist_add_tail(&client->eta_list, &eta_list);
Jens Axboef77d2672011-10-10 14:36:07 +02001061 client->eta_in_flight = eta;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001062 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_SEND_ETA,
Jens Axboedf380932011-10-11 14:25:08 +02001063 (uintptr_t) eta, &client->cmd_list);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001064 }
1065
Jens Axboe82c1ed32011-10-10 08:56:18 +02001066 while (skipped--)
Jens Axboea5276612012-03-04 15:15:08 +01001067 fio_client_dec_jobs_eta(eta, ops->eta);
Jens Axboe82c1ed32011-10-10 08:56:18 +02001068
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001069 dprint(FD_NET, "client: requested eta tag %p\n", eta);
1070}
1071
Jens Axboe89c17072011-10-11 10:15:51 +02001072static int client_check_cmd_timeout(struct fio_client *client,
1073 struct timeval *now)
1074{
1075 struct fio_net_int_cmd *cmd;
1076 struct flist_head *entry, *tmp;
1077 int ret = 0;
1078
1079 flist_for_each_safe(entry, tmp, &client->cmd_list) {
1080 cmd = flist_entry(entry, struct fio_net_int_cmd, list);
1081
1082 if (mtime_since(&cmd->tv, now) < FIO_NET_CLIENT_TIMEOUT)
1083 continue;
1084
1085 log_err("fio: client %s, timeout on cmd %s\n", client->hostname,
1086 fio_server_op(cmd->cmd.opcode));
1087 flist_del(&cmd->list);
1088 free(cmd);
1089 ret = 1;
1090 }
1091
1092 return flist_empty(&client->cmd_list) && ret;
1093}
1094
Jens Axboea5276612012-03-04 15:15:08 +01001095static int fio_check_clients_timed_out(void)
Jens Axboe89c17072011-10-11 10:15:51 +02001096{
1097 struct fio_client *client;
1098 struct flist_head *entry, *tmp;
1099 struct timeval tv;
1100 int ret = 0;
1101
1102 gettimeofday(&tv, NULL);
1103
1104 flist_for_each_safe(entry, tmp, &client_list) {
1105 client = flist_entry(entry, struct fio_client, list);
1106
1107 if (flist_empty(&client->cmd_list))
1108 continue;
1109
1110 if (!client_check_cmd_timeout(client, &tv))
1111 continue;
1112
Jens Axboea5276612012-03-04 15:15:08 +01001113 if (client->ops->timed_out)
1114 client->ops->timed_out(client);
Jens Axboeed727a42012-03-02 12:14:40 +01001115 else
1116 log_err("fio: client %s timed out\n", client->hostname);
1117
Jens Axboe89c17072011-10-11 10:15:51 +02001118 remove_client(client);
1119 ret = 1;
1120 }
1121
1122 return ret;
1123}
1124
Stephen M. Camerondd366722012-02-24 08:17:30 +01001125int fio_handle_clients(struct client_ops *ops)
Jens Axboeb66570d2011-10-01 11:11:35 -06001126{
Jens Axboeb66570d2011-10-01 11:11:35 -06001127 struct pollfd *pfds;
Jens Axboe498c92c2011-10-17 09:14:42 +02001128 int i, ret = 0, retval = 0;
Jens Axboeb66570d2011-10-01 11:11:35 -06001129
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001130 gettimeofday(&eta_tv, NULL);
1131
Jens Axboeb66570d2011-10-01 11:11:35 -06001132 pfds = malloc(nr_clients * sizeof(struct pollfd));
1133
Jens Axboe37f0c1a2011-10-11 14:08:33 +02001134 sum_stat_clients = nr_clients;
1135 init_thread_stat(&client_ts);
1136 init_group_run_stat(&client_gs);
1137
Jens Axboeb66570d2011-10-01 11:11:35 -06001138 while (!exit_backend && nr_clients) {
Jens Axboec2cb6862012-02-23 20:56:12 +01001139 struct flist_head *entry, *tmp;
1140 struct fio_client *client;
1141
Jens Axboe82a4be12011-10-01 12:40:32 -06001142 i = 0;
Jens Axboec2cb6862012-02-23 20:56:12 +01001143 flist_for_each_safe(entry, tmp, &client_list) {
Jens Axboe82a4be12011-10-01 12:40:32 -06001144 client = flist_entry(entry, struct fio_client, list);
1145
Jens Axboea5276612012-03-04 15:15:08 +01001146 if (!client->sent_job && !client->ops->stay_connected &&
Jens Axboec2cb6862012-02-23 20:56:12 +01001147 flist_empty(&client->cmd_list)) {
1148 remove_client(client);
1149 continue;
1150 }
1151
Jens Axboe82a4be12011-10-01 12:40:32 -06001152 pfds[i].fd = client->fd;
1153 pfds[i].events = POLLIN;
1154 i++;
1155 }
1156
Jens Axboec2cb6862012-02-23 20:56:12 +01001157 if (!nr_clients)
1158 break;
1159
Jens Axboe82a4be12011-10-01 12:40:32 -06001160 assert(i == nr_clients);
1161
Jens Axboe5c2857f2011-10-04 13:14:32 +02001162 do {
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001163 struct timeval tv;
1164
1165 gettimeofday(&tv, NULL);
1166 if (mtime_since(&eta_tv, &tv) >= 900) {
Jens Axboea5276612012-03-04 15:15:08 +01001167 request_client_etas(ops);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001168 memcpy(&eta_tv, &tv, sizeof(tv));
Jens Axboe89c17072011-10-11 10:15:51 +02001169
Jens Axboea5276612012-03-04 15:15:08 +01001170 if (fio_check_clients_timed_out())
Jens Axboe89c17072011-10-11 10:15:51 +02001171 break;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001172 }
1173
Jens Axboe5c2857f2011-10-04 13:14:32 +02001174 ret = poll(pfds, nr_clients, 100);
1175 if (ret < 0) {
1176 if (errno == EINTR)
1177 continue;
1178 log_err("fio: poll clients: %s\n", strerror(errno));
1179 break;
1180 } else if (!ret)
Jens Axboeb66570d2011-10-01 11:11:35 -06001181 continue;
Jens Axboe5c2857f2011-10-04 13:14:32 +02001182 } while (ret <= 0);
Jens Axboeb66570d2011-10-01 11:11:35 -06001183
1184 for (i = 0; i < nr_clients; i++) {
1185 if (!(pfds[i].revents & POLLIN))
1186 continue;
1187
1188 client = find_client_by_fd(pfds[i].fd);
1189 if (!client) {
Jens Axboe3c5f57e2011-10-06 12:37:50 +02001190 log_err("fio: unknown client fd %d\n", pfds[i].fd);
Jens Axboeb66570d2011-10-01 11:11:35 -06001191 continue;
1192 }
Jens Axboea5276612012-03-04 15:15:08 +01001193 if (!fio_handle_client(client)) {
Jens Axboe28d3ab02011-10-05 20:45:37 +02001194 log_info("client: host=%s disconnected\n",
1195 client->hostname);
1196 remove_client(client);
Jens Axboe498c92c2011-10-17 09:14:42 +02001197 retval = 1;
Jens Axboe38990762011-10-17 13:31:33 +02001198 } else if (client->error)
Jens Axboe498c92c2011-10-17 09:14:42 +02001199 retval = 1;
Jens Axboe343cb4a2012-03-09 17:16:51 +01001200 fio_put_client(client);
Jens Axboeb66570d2011-10-01 11:11:35 -06001201 }
1202 }
1203
1204 free(pfds);
Jens Axboe498c92c2011-10-17 09:14:42 +02001205 return retval;
Jens Axboeb66570d2011-10-01 11:11:35 -06001206}