blob: 5545a8a90a9982f87af5ca73515daaf848a5c1b1 [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,
Jens Axboe6433ee02012-03-09 20:10:51 +010039 .eta_msec = FIO_CLIENT_DEF_ETA_MSEC,
Stephen M. Camerondd366722012-02-24 08:17:30 +010040};
41
Jens Axboeaf9c9fb2011-10-09 21:54:10 +020042static struct timeval eta_tv;
Jens Axboe48fbb462011-10-09 12:19:08 +020043
Jens Axboeb66570d2011-10-01 11:11:35 -060044static FLIST_HEAD(client_list);
Jens Axboe82c1ed32011-10-10 08:56:18 +020045static FLIST_HEAD(eta_list);
Jens Axboeb66570d2011-10-01 11:11:35 -060046
Jens Axboe3f3a4542011-10-10 21:11:09 +020047static FLIST_HEAD(arg_list);
48
Jens Axboe3650a3c2012-03-05 14:09:03 +010049struct thread_stat client_ts;
50struct group_run_stats client_gs;
51int sum_stat_clients;
52
Jens Axboe37f0c1a2011-10-11 14:08:33 +020053static int sum_stat_nr;
54
Jens Axboe3c5f57e2011-10-06 12:37:50 +020055#define FIO_CLIENT_HASH_BITS 7
56#define FIO_CLIENT_HASH_SZ (1 << FIO_CLIENT_HASH_BITS)
57#define FIO_CLIENT_HASH_MASK (FIO_CLIENT_HASH_SZ - 1)
Jens Axboebebe6392011-10-07 10:00:51 +020058static struct flist_head client_hash[FIO_CLIENT_HASH_SZ];
Jens Axboe3c5f57e2011-10-06 12:37:50 +020059
Jens Axboebebe6392011-10-07 10:00:51 +020060static void fio_client_add_hash(struct fio_client *client)
Jens Axboe3c5f57e2011-10-06 12:37:50 +020061{
62 int bucket = hash_long(client->fd, FIO_CLIENT_HASH_BITS);
63
64 bucket &= FIO_CLIENT_HASH_MASK;
Jens Axboebebe6392011-10-07 10:00:51 +020065 flist_add(&client->hash_list, &client_hash[bucket]);
Jens Axboe3c5f57e2011-10-06 12:37:50 +020066}
67
Jens Axboebebe6392011-10-07 10:00:51 +020068static void fio_client_remove_hash(struct fio_client *client)
Jens Axboe3c5f57e2011-10-06 12:37:50 +020069{
Jens Axboebebe6392011-10-07 10:00:51 +020070 if (!flist_empty(&client->hash_list))
71 flist_del_init(&client->hash_list);
Jens Axboe3c5f57e2011-10-06 12:37:50 +020072}
73
74static void fio_init fio_client_hash_init(void)
75{
76 int i;
77
Jens Axboebebe6392011-10-07 10:00:51 +020078 for (i = 0; i < FIO_CLIENT_HASH_SZ; i++)
79 INIT_FLIST_HEAD(&client_hash[i]);
Jens Axboe3c5f57e2011-10-06 12:37:50 +020080}
81
Jens Axboeb66570d2011-10-01 11:11:35 -060082static struct fio_client *find_client_by_fd(int fd)
83{
Jens Axboe3c5f57e2011-10-06 12:37:50 +020084 int bucket = hash_long(fd, FIO_CLIENT_HASH_BITS) & FIO_CLIENT_HASH_MASK;
Jens Axboeb66570d2011-10-01 11:11:35 -060085 struct fio_client *client;
86 struct flist_head *entry;
87
Jens Axboebebe6392011-10-07 10:00:51 +020088 flist_for_each(entry, &client_hash[bucket]) {
89 client = flist_entry(entry, struct fio_client, hash_list);
Jens Axboeb66570d2011-10-01 11:11:35 -060090
Jens Axboe343cb4a2012-03-09 17:16:51 +010091 if (client->fd == fd)
92 return fio_get_client(client);
Jens Axboeb66570d2011-10-01 11:11:35 -060093 }
94
95 return NULL;
96}
97
Jens Axboeb66570d2011-10-01 11:11:35 -060098static void remove_client(struct fio_client *client)
99{
Jens Axboe5121a9a2012-03-05 13:32:47 +0100100 assert(client->refs);
101
102 if (--client->refs)
103 return;
104
Jens Axboe39e8e012011-10-04 13:46:08 +0200105 dprint(FD_NET, "client: removed <%s>\n", client->hostname);
Jens Axboeb66570d2011-10-01 11:11:35 -0600106 flist_del(&client->list);
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200107
Jens Axboebebe6392011-10-07 10:00:51 +0200108 fio_client_remove_hash(client);
Jens Axboe81179ee2011-10-04 12:42:06 +0200109
Jens Axboe82c1ed32011-10-10 08:56:18 +0200110 if (!flist_empty(&client->eta_list)) {
111 flist_del_init(&client->eta_list);
Jens Axboea5276612012-03-04 15:15:08 +0100112 fio_client_dec_jobs_eta(client->eta_in_flight, client->ops->eta);
Jens Axboe82c1ed32011-10-10 08:56:18 +0200113 }
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200114
Jens Axboeb66570d2011-10-01 11:11:35 -0600115 free(client->hostname);
Jens Axboe81179ee2011-10-04 12:42:06 +0200116 if (client->argv)
117 free(client->argv);
Jens Axboeb5296dd2011-10-07 16:35:56 +0200118 if (client->name)
119 free(client->name);
Jens Axboe81179ee2011-10-04 12:42:06 +0200120
Jens Axboeb66570d2011-10-01 11:11:35 -0600121 free(client);
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200122 nr_clients--;
Jens Axboe5fd0acb2011-10-11 14:20:22 +0200123 sum_stat_clients--;
Jens Axboeb66570d2011-10-01 11:11:35 -0600124}
Jens Axboe132159a2011-09-30 15:01:32 -0600125
Jens Axboe343cb4a2012-03-09 17:16:51 +0100126void fio_put_client(struct fio_client *client)
Jens Axboe5121a9a2012-03-05 13:32:47 +0100127{
128 remove_client(client);
129}
130
Jens Axboe343cb4a2012-03-09 17:16:51 +0100131struct fio_client *fio_get_client(struct fio_client *client)
132{
133 client->refs++;
134 return client;
135}
136
Jens Axboefa2ea802011-10-08 21:07:29 +0200137static void __fio_client_add_cmd_option(struct fio_client *client,
138 const char *opt)
Jens Axboe81179ee2011-10-04 12:42:06 +0200139{
Jens Axboe39e8e012011-10-04 13:46:08 +0200140 int index;
141
142 index = client->argc++;
Jens Axboe81179ee2011-10-04 12:42:06 +0200143 client->argv = realloc(client->argv, sizeof(char *) * client->argc);
Jens Axboe39e8e012011-10-04 13:46:08 +0200144 client->argv[index] = strdup(opt);
145 dprint(FD_NET, "client: add cmd %d: %s\n", index, opt);
Jens Axboe81179ee2011-10-04 12:42:06 +0200146}
147
Jens Axboefa2ea802011-10-08 21:07:29 +0200148void fio_client_add_cmd_option(void *cookie, const char *opt)
Jens Axboe81179ee2011-10-04 12:42:06 +0200149{
Jens Axboebebe6392011-10-07 10:00:51 +0200150 struct fio_client *client = cookie;
Jens Axboe3f3a4542011-10-10 21:11:09 +0200151 struct flist_head *entry;
Jens Axboe81179ee2011-10-04 12:42:06 +0200152
Jens Axboebebe6392011-10-07 10:00:51 +0200153 if (!client || !opt)
Jens Axboefa2ea802011-10-08 21:07:29 +0200154 return;
Jens Axboe81179ee2011-10-04 12:42:06 +0200155
Jens Axboefa2ea802011-10-08 21:07:29 +0200156 __fio_client_add_cmd_option(client, opt);
Jens Axboe3f3a4542011-10-10 21:11:09 +0200157
158 /*
159 * Duplicate arguments to shared client group
160 */
161 flist_for_each(entry, &arg_list) {
162 client = flist_entry(entry, struct fio_client, arg_list);
163
164 __fio_client_add_cmd_option(client, opt);
165 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200166}
167
Jens Axboea5276612012-03-04 15:15:08 +0100168struct fio_client *fio_client_add_explicit(struct client_ops *ops,
169 const char *hostname, int type,
Jens Axboe3ec62ec2012-03-01 12:01:29 +0100170 int port)
171{
172 struct fio_client *client;
173
174 client = malloc(sizeof(*client));
175 memset(client, 0, sizeof(*client));
176
177 INIT_FLIST_HEAD(&client->list);
178 INIT_FLIST_HEAD(&client->hash_list);
179 INIT_FLIST_HEAD(&client->arg_list);
180 INIT_FLIST_HEAD(&client->eta_list);
181 INIT_FLIST_HEAD(&client->cmd_list);
182
183 client->hostname = strdup(hostname);
184
185 if (type == Fio_client_socket)
186 client->is_sock = 1;
187 else {
188 int ipv6;
189
190 ipv6 = type == Fio_client_ipv6;
191 if (fio_server_parse_host(hostname, &ipv6,
192 &client->addr.sin_addr,
193 &client->addr6.sin6_addr))
194 goto err;
195
196 client->port = port;
197 }
198
199 client->fd = -1;
Jens Axboea5276612012-03-04 15:15:08 +0100200 client->ops = ops;
Jens Axboe5121a9a2012-03-05 13:32:47 +0100201 client->refs = 1;
Jens Axboe3ec62ec2012-03-01 12:01:29 +0100202
203 __fio_client_add_cmd_option(client, "fio");
204
205 flist_add(&client->list, &client_list);
206 nr_clients++;
207 dprint(FD_NET, "client: added <%s>\n", client->hostname);
208 return client;
209err:
210 free(client);
211 return NULL;
212}
213
Jens Axboea5276612012-03-04 15:15:08 +0100214int fio_client_add(struct client_ops *ops, const char *hostname, void **cookie)
Jens Axboe132159a2011-09-30 15:01:32 -0600215{
Jens Axboe3f3a4542011-10-10 21:11:09 +0200216 struct fio_client *existing = *cookie;
Jens Axboeb66570d2011-10-01 11:11:35 -0600217 struct fio_client *client;
Jens Axboe132159a2011-09-30 15:01:32 -0600218
Jens Axboe3f3a4542011-10-10 21:11:09 +0200219 if (existing) {
220 /*
221 * We always add our "exec" name as the option, hence 1
222 * means empty.
223 */
224 if (existing->argc == 1)
225 flist_add_tail(&existing->arg_list, &arg_list);
226 else {
227 while (!flist_empty(&arg_list))
228 flist_del_init(arg_list.next);
229 }
230 }
231
Jens Axboeb66570d2011-10-01 11:11:35 -0600232 client = malloc(sizeof(*client));
Jens Axboea37f69b2011-10-01 12:24:21 -0600233 memset(client, 0, sizeof(*client));
Jens Axboe81179ee2011-10-04 12:42:06 +0200234
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200235 INIT_FLIST_HEAD(&client->list);
Jens Axboebebe6392011-10-07 10:00:51 +0200236 INIT_FLIST_HEAD(&client->hash_list);
Jens Axboe3f3a4542011-10-10 21:11:09 +0200237 INIT_FLIST_HEAD(&client->arg_list);
Jens Axboe82c1ed32011-10-10 08:56:18 +0200238 INIT_FLIST_HEAD(&client->eta_list);
Jens Axboe89c17072011-10-11 10:15:51 +0200239 INIT_FLIST_HEAD(&client->cmd_list);
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200240
Jens Axboebebe6392011-10-07 10:00:51 +0200241 if (fio_server_parse_string(hostname, &client->hostname,
242 &client->is_sock, &client->port,
Jens Axboe811826b2011-10-24 09:11:50 +0200243 &client->addr.sin_addr,
244 &client->addr6.sin6_addr,
245 &client->ipv6))
Jens Axboebebe6392011-10-07 10:00:51 +0200246 return -1;
247
Jens Axboea37f69b2011-10-01 12:24:21 -0600248 client->fd = -1;
Jens Axboea5276612012-03-04 15:15:08 +0100249 client->ops = ops;
Jens Axboe5121a9a2012-03-05 13:32:47 +0100250 client->refs = 1;
Jens Axboe81179ee2011-10-04 12:42:06 +0200251
252 __fio_client_add_cmd_option(client, "fio");
253
Jens Axboea37f69b2011-10-01 12:24:21 -0600254 flist_add(&client->list, &client_list);
255 nr_clients++;
Jens Axboebebe6392011-10-07 10:00:51 +0200256 dprint(FD_NET, "client: added <%s>\n", client->hostname);
257 *cookie = client;
258 return 0;
Jens Axboea37f69b2011-10-01 12:24:21 -0600259}
260
Jens Axboed824c3b2012-03-09 17:45:43 +0100261static void probe_client(struct fio_client *client)
262{
263 dprint(FD_NET, "client: send probe\n");
264
265 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_PROBE, 0, &client->cmd_list);
266}
267
Jens Axboe87aa8f12011-10-06 21:24:13 +0200268static int fio_client_connect_ip(struct fio_client *client)
Jens Axboea37f69b2011-10-01 12:24:21 -0600269{
Jens Axboe811826b2011-10-24 09:11:50 +0200270 struct sockaddr *addr;
271 fio_socklen_t socklen;
272 int fd, domain;
Jens Axboe132159a2011-09-30 15:01:32 -0600273
Jens Axboe811826b2011-10-24 09:11:50 +0200274 if (client->ipv6) {
275 client->addr6.sin6_family = AF_INET6;
276 client->addr6.sin6_port = htons(client->port);
277 domain = AF_INET6;
278 addr = (struct sockaddr *) &client->addr6;
279 socklen = sizeof(client->addr6);
280 } else {
281 client->addr.sin_family = AF_INET;
282 client->addr.sin_port = htons(client->port);
283 domain = AF_INET;
284 addr = (struct sockaddr *) &client->addr;
285 socklen = sizeof(client->addr);
286 }
Jens Axboe132159a2011-09-30 15:01:32 -0600287
Jens Axboe811826b2011-10-24 09:11:50 +0200288 fd = socket(domain, SOCK_STREAM, 0);
Jens Axboe132159a2011-09-30 15:01:32 -0600289 if (fd < 0) {
Jens Axboe25095e12012-03-09 17:09:55 +0100290 int ret = -errno;
291
Jens Axboe132159a2011-09-30 15:01:32 -0600292 log_err("fio: socket: %s\n", strerror(errno));
Jens Axboe25095e12012-03-09 17:09:55 +0100293 return ret;
Jens Axboe132159a2011-09-30 15:01:32 -0600294 }
295
Jens Axboe811826b2011-10-24 09:11:50 +0200296 if (connect(fd, addr, socklen) < 0) {
Jens Axboe25095e12012-03-09 17:09:55 +0100297 int ret = -errno;
298
Jens Axboe132159a2011-09-30 15:01:32 -0600299 log_err("fio: connect: %s\n", strerror(errno));
Jens Axboea7de0a12011-10-07 12:55:14 +0200300 log_err("fio: failed to connect to %s:%u\n", client->hostname,
301 client->port);
Jens Axboeb94cba42011-10-06 21:27:10 +0200302 close(fd);
Jens Axboe25095e12012-03-09 17:09:55 +0100303 return ret;
Jens Axboe132159a2011-09-30 15:01:32 -0600304 }
305
Jens Axboe87aa8f12011-10-06 21:24:13 +0200306 return fd;
307}
308
309static int fio_client_connect_sock(struct fio_client *client)
310{
311 struct sockaddr_un *addr = &client->addr_un;
312 fio_socklen_t len;
313 int fd;
314
315 memset(addr, 0, sizeof(*addr));
316 addr->sun_family = AF_UNIX;
317 strcpy(addr->sun_path, client->hostname);
318
319 fd = socket(AF_UNIX, SOCK_STREAM, 0);
320 if (fd < 0) {
Jens Axboe25095e12012-03-09 17:09:55 +0100321 int ret = -errno;
322
Jens Axboe87aa8f12011-10-06 21:24:13 +0200323 log_err("fio: socket: %s\n", strerror(errno));
Jens Axboe25095e12012-03-09 17:09:55 +0100324 return ret;
Jens Axboe87aa8f12011-10-06 21:24:13 +0200325 }
326
327 len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
328 if (connect(fd, (struct sockaddr *) addr, len) < 0) {
Jens Axboe25095e12012-03-09 17:09:55 +0100329 int ret = -errno;
330
Jens Axboe87aa8f12011-10-06 21:24:13 +0200331 log_err("fio: connect; %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200332 close(fd);
Jens Axboe25095e12012-03-09 17:09:55 +0100333 return ret;
Jens Axboe87aa8f12011-10-06 21:24:13 +0200334 }
335
336 return fd;
337}
338
Jens Axboe2f99deb2012-03-09 14:37:29 +0100339int fio_client_connect(struct fio_client *client)
Jens Axboe87aa8f12011-10-06 21:24:13 +0200340{
341 int fd;
342
343 dprint(FD_NET, "client: connect to host %s\n", client->hostname);
344
Jens Axboe87aa8f12011-10-06 21:24:13 +0200345 if (client->is_sock)
346 fd = fio_client_connect_sock(client);
347 else
348 fd = fio_client_connect_ip(client);
349
Jens Axboe89c17072011-10-11 10:15:51 +0200350 dprint(FD_NET, "client: %s connected %d\n", client->hostname, fd);
351
Jens Axboe87aa8f12011-10-06 21:24:13 +0200352 if (fd < 0)
Jens Axboea1a3ed52012-03-09 17:00:01 +0100353 return fd;
Jens Axboe87aa8f12011-10-06 21:24:13 +0200354
Jens Axboeb66570d2011-10-01 11:11:35 -0600355 client->fd = fd;
Jens Axboebebe6392011-10-07 10:00:51 +0200356 fio_client_add_hash(client);
Jens Axboe81179ee2011-10-04 12:42:06 +0200357 client->state = Client_connected;
Jens Axboed824c3b2012-03-09 17:45:43 +0100358
359 probe_client(client);
Jens Axboe132159a2011-09-30 15:01:32 -0600360 return 0;
361}
362
Jens Axboe2f99deb2012-03-09 14:37:29 +0100363void fio_client_terminate(struct fio_client *client)
364{
365 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_QUIT, 0, NULL);
366}
367
Jens Axboecc0df002011-10-03 20:53:32 +0200368void fio_clients_terminate(void)
369{
370 struct flist_head *entry;
371 struct fio_client *client;
372
Jens Axboe60efd142011-10-04 13:30:11 +0200373 dprint(FD_NET, "client: terminate clients\n");
374
Jens Axboecc0df002011-10-03 20:53:32 +0200375 flist_for_each(entry, &client_list) {
376 client = flist_entry(entry, struct fio_client, list);
Jens Axboe2f99deb2012-03-09 14:37:29 +0100377 fio_client_terminate(client);
Jens Axboecc0df002011-10-03 20:53:32 +0200378 }
379}
380
381static void sig_int(int sig)
382{
Jens Axboebebe6392011-10-07 10:00:51 +0200383 dprint(FD_NET, "client: got signal %d\n", sig);
Jens Axboecc0df002011-10-03 20:53:32 +0200384 fio_clients_terminate();
385}
386
387static void client_signal_handler(void)
388{
389 struct sigaction act;
390
391 memset(&act, 0, sizeof(act));
392 act.sa_handler = sig_int;
393 act.sa_flags = SA_RESTART;
394 sigaction(SIGINT, &act, NULL);
395
396 memset(&act, 0, sizeof(act));
397 act.sa_handler = sig_int;
398 act.sa_flags = SA_RESTART;
399 sigaction(SIGTERM, &act, NULL);
400}
401
Jens Axboe81179ee2011-10-04 12:42:06 +0200402static int send_client_cmd_line(struct fio_client *client)
403{
Jens Axboefa2ea802011-10-08 21:07:29 +0200404 struct cmd_single_line_pdu *cslp;
405 struct cmd_line_pdu *clp;
406 unsigned long offset;
Jens Axboe7f868312011-10-10 09:55:21 +0200407 unsigned int *lens;
Jens Axboefa2ea802011-10-08 21:07:29 +0200408 void *pdu;
409 size_t mem;
Jens Axboe81179ee2011-10-04 12:42:06 +0200410 int i, ret;
411
Jens Axboe39e8e012011-10-04 13:46:08 +0200412 dprint(FD_NET, "client: send cmdline %d\n", client->argc);
Jens Axboe60efd142011-10-04 13:30:11 +0200413
Jens Axboe7f868312011-10-10 09:55:21 +0200414 lens = malloc(client->argc * sizeof(unsigned int));
415
Jens Axboefa2ea802011-10-08 21:07:29 +0200416 /*
417 * Find out how much mem we need
418 */
Jens Axboe7f868312011-10-10 09:55:21 +0200419 for (i = 0, mem = 0; i < client->argc; i++) {
420 lens[i] = strlen(client->argv[i]) + 1;
421 mem += lens[i];
422 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200423
Jens Axboefa2ea802011-10-08 21:07:29 +0200424 /*
425 * We need one cmd_line_pdu, and argc number of cmd_single_line_pdu
426 */
427 mem += sizeof(*clp) + (client->argc * sizeof(*cslp));
428
429 pdu = malloc(mem);
430 clp = pdu;
431 offset = sizeof(*clp);
432
433 for (i = 0; i < client->argc; i++) {
Jens Axboe7f868312011-10-10 09:55:21 +0200434 uint16_t arg_len = lens[i];
Jens Axboefa2ea802011-10-08 21:07:29 +0200435
436 cslp = pdu + offset;
437 strcpy((char *) cslp->text, client->argv[i]);
438 cslp->len = cpu_to_le16(arg_len);
439 offset += sizeof(*cslp) + arg_len;
440 }
441
Jens Axboe7f868312011-10-10 09:55:21 +0200442 free(lens);
Jens Axboefa2ea802011-10-08 21:07:29 +0200443 clp->lines = cpu_to_le16(client->argc);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200444 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOBLINE, pdu, mem, 0);
Jens Axboe81179ee2011-10-04 12:42:06 +0200445 free(pdu);
446 return ret;
447}
448
Jens Axboea37f69b2011-10-01 12:24:21 -0600449int fio_clients_connect(void)
450{
451 struct fio_client *client;
452 struct flist_head *entry, *tmp;
453 int ret;
454
Bruce Cran93bcfd22012-02-20 20:18:19 +0100455#ifdef WIN32
456 WSADATA wsd;
457 WSAStartup(MAKEWORD(2,2), &wsd);
458#endif
459
Jens Axboe60efd142011-10-04 13:30:11 +0200460 dprint(FD_NET, "client: connect all\n");
461
Jens Axboecc0df002011-10-03 20:53:32 +0200462 client_signal_handler();
463
Jens Axboea37f69b2011-10-01 12:24:21 -0600464 flist_for_each_safe(entry, tmp, &client_list) {
465 client = flist_entry(entry, struct fio_client, list);
466
467 ret = fio_client_connect(client);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200468 if (ret) {
Jens Axboea37f69b2011-10-01 12:24:21 -0600469 remove_client(client);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200470 continue;
471 }
472
Jens Axboe81179ee2011-10-04 12:42:06 +0200473 if (client->argc > 1)
474 send_client_cmd_line(client);
Jens Axboea37f69b2011-10-01 12:24:21 -0600475 }
476
477 return !nr_clients;
478}
479
Jens Axboeb9d2f302012-03-08 20:36:28 +0100480int fio_start_client(struct fio_client *client)
481{
482 dprint(FD_NET, "client: start %s\n", client->hostname);
483 return fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_RUN, 0, NULL);
484}
485
486int fio_start_all_clients(void)
487{
488 struct fio_client *client;
489 struct flist_head *entry, *tmp;
490 int ret;
491
492 dprint(FD_NET, "client: start all\n");
493
494 flist_for_each_safe(entry, tmp, &client_list) {
495 client = flist_entry(entry, struct fio_client, list);
496
497 ret = fio_start_client(client);
498 if (ret) {
499 remove_client(client);
500 continue;
501 }
502 }
503
504 return flist_empty(&client_list);
505}
506
Jens Axboe132159a2011-09-30 15:01:32 -0600507/*
508 * Send file contents to server backend. We could use sendfile(), but to remain
509 * more portable lets just read/write the darn thing.
510 */
Jens Axboe9988ca72012-03-09 15:14:06 +0100511static int __fio_client_send_ini(struct fio_client *client, const char *filename)
Jens Axboe132159a2011-09-30 15:01:32 -0600512{
513 struct stat sb;
514 char *p, *buf;
515 off_t len;
516 int fd, ret;
517
Jens Axboe46c48f12011-10-01 12:50:51 -0600518 dprint(FD_NET, "send ini %s to %s\n", filename, client->hostname);
519
Jens Axboe132159a2011-09-30 15:01:32 -0600520 fd = open(filename, O_RDONLY);
521 if (fd < 0) {
Jens Axboe25095e12012-03-09 17:09:55 +0100522 int ret = -errno;
523
Jens Axboee951bdc2011-10-05 21:58:45 +0200524 log_err("fio: job file <%s> open: %s\n", filename, strerror(errno));
Jens Axboe25095e12012-03-09 17:09:55 +0100525 return ret;
Jens Axboe132159a2011-09-30 15:01:32 -0600526 }
527
528 if (fstat(fd, &sb) < 0) {
Jens Axboe25095e12012-03-09 17:09:55 +0100529 int ret = -errno;
530
Jens Axboe132159a2011-09-30 15:01:32 -0600531 log_err("fio: job file stat: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200532 close(fd);
Jens Axboe25095e12012-03-09 17:09:55 +0100533 return ret;
Jens Axboe132159a2011-09-30 15:01:32 -0600534 }
535
536 buf = malloc(sb.st_size);
537
538 len = sb.st_size;
539 p = buf;
540 do {
541 ret = read(fd, p, len);
542 if (ret > 0) {
543 len -= ret;
544 if (!len)
545 break;
546 p += ret;
547 continue;
548 } else if (!ret)
549 break;
550 else if (errno == EAGAIN || errno == EINTR)
551 continue;
552 } while (1);
553
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200554 if (len) {
555 log_err("fio: failed reading job file %s\n", filename);
Jens Axboeb94cba42011-10-06 21:27:10 +0200556 close(fd);
Jens Axboec524ef72011-10-07 12:23:34 +0200557 free(buf);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200558 return 1;
559 }
560
Jens Axboec2cb6862012-02-23 20:56:12 +0100561 client->sent_job = 1;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200562 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, buf, sb.st_size, 0);
Jens Axboe132159a2011-09-30 15:01:32 -0600563 free(buf);
Jens Axboeb94cba42011-10-06 21:27:10 +0200564 close(fd);
Jens Axboe132159a2011-09-30 15:01:32 -0600565 return ret;
566}
Jens Axboe37db14f2011-09-30 17:00:42 -0600567
Jens Axboe9988ca72012-03-09 15:14:06 +0100568int fio_client_send_ini(struct fio_client *client, const char *filename)
569{
Jens Axboe25095e12012-03-09 17:09:55 +0100570 int ret;
571
572 ret = __fio_client_send_ini(client, filename);
573 if (ret) {
Jens Axboe9988ca72012-03-09 15:14:06 +0100574 remove_client(client);
Jens Axboe25095e12012-03-09 17:09:55 +0100575 return ret;
Jens Axboe9988ca72012-03-09 15:14:06 +0100576 }
577
578 client->sent_job = 1;
Jens Axboe25095e12012-03-09 17:09:55 +0100579 return ret;
Jens Axboe9988ca72012-03-09 15:14:06 +0100580}
581
Jens Axboea37f69b2011-10-01 12:24:21 -0600582int fio_clients_send_ini(const char *filename)
583{
584 struct fio_client *client;
585 struct flist_head *entry, *tmp;
586
587 flist_for_each_safe(entry, tmp, &client_list) {
588 client = flist_entry(entry, struct fio_client, list);
589
Jens Axboe9988ca72012-03-09 15:14:06 +0100590 fio_client_send_ini(client, filename);
Jens Axboea37f69b2011-10-01 12:24:21 -0600591 }
592
593 return !nr_clients;
594}
595
Jens Axboea64e88d2011-10-03 14:20:01 +0200596static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
597{
598 dst->max_val = le64_to_cpu(src->max_val);
599 dst->min_val = le64_to_cpu(src->min_val);
600 dst->samples = le64_to_cpu(src->samples);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200601
602 /*
603 * Floats arrive as IEEE 754 encoded uint64_t, convert back to double
604 */
605 dst->mean.u.f = fio_uint64_to_double(le64_to_cpu(dst->mean.u.i));
606 dst->S.u.f = fio_uint64_to_double(le64_to_cpu(dst->S.u.i));
Jens Axboea64e88d2011-10-03 14:20:01 +0200607}
608
609static void convert_ts(struct thread_stat *dst, struct thread_stat *src)
610{
611 int i, j;
612
613 dst->error = le32_to_cpu(src->error);
614 dst->groupid = le32_to_cpu(src->groupid);
615 dst->pid = le32_to_cpu(src->pid);
616 dst->members = le32_to_cpu(src->members);
617
618 for (i = 0; i < 2; i++) {
619 convert_io_stat(&dst->clat_stat[i], &src->clat_stat[i]);
620 convert_io_stat(&dst->slat_stat[i], &src->slat_stat[i]);
621 convert_io_stat(&dst->lat_stat[i], &src->lat_stat[i]);
622 convert_io_stat(&dst->bw_stat[i], &src->bw_stat[i]);
623 }
624
625 dst->usr_time = le64_to_cpu(src->usr_time);
626 dst->sys_time = le64_to_cpu(src->sys_time);
627 dst->ctx = le64_to_cpu(src->ctx);
628 dst->minf = le64_to_cpu(src->minf);
629 dst->majf = le64_to_cpu(src->majf);
630 dst->clat_percentiles = le64_to_cpu(src->clat_percentiles);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200631
632 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
633 fio_fp64_t *fps = &src->percentile_list[i];
634 fio_fp64_t *fpd = &dst->percentile_list[i];
635
636 fpd->u.f = fio_uint64_to_double(le64_to_cpu(fps->u.i));
637 }
Jens Axboea64e88d2011-10-03 14:20:01 +0200638
639 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
640 dst->io_u_map[i] = le32_to_cpu(src->io_u_map[i]);
641 dst->io_u_submit[i] = le32_to_cpu(src->io_u_submit[i]);
642 dst->io_u_complete[i] = le32_to_cpu(src->io_u_complete[i]);
643 }
644
645 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
646 dst->io_u_lat_u[i] = le32_to_cpu(src->io_u_lat_u[i]);
647 dst->io_u_lat_m[i] = le32_to_cpu(src->io_u_lat_m[i]);
648 }
649
650 for (i = 0; i < 2; i++)
651 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
652 dst->io_u_plat[i][j] = le32_to_cpu(src->io_u_plat[i][j]);
653
654 for (i = 0; i < 3; i++) {
655 dst->total_io_u[i] = le64_to_cpu(src->total_io_u[i]);
Jens Axboe93eee042011-10-03 21:08:48 +0200656 dst->short_io_u[i] = le64_to_cpu(src->short_io_u[i]);
Jens Axboea64e88d2011-10-03 14:20:01 +0200657 }
658
659 dst->total_submit = le64_to_cpu(src->total_submit);
660 dst->total_complete = le64_to_cpu(src->total_complete);
661
662 for (i = 0; i < 2; i++) {
663 dst->io_bytes[i] = le64_to_cpu(src->io_bytes[i]);
664 dst->runtime[i] = le64_to_cpu(src->runtime[i]);
665 }
666
667 dst->total_run_time = le64_to_cpu(src->total_run_time);
668 dst->continue_on_error = le16_to_cpu(src->continue_on_error);
669 dst->total_err_count = le64_to_cpu(src->total_err_count);
Jens Axboeddcc0b62011-10-03 14:45:27 +0200670 dst->first_error = le32_to_cpu(src->first_error);
671 dst->kb_base = le32_to_cpu(src->kb_base);
Jens Axboea64e88d2011-10-03 14:20:01 +0200672}
673
674static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
675{
676 int i;
677
678 for (i = 0; i < 2; i++) {
679 dst->max_run[i] = le64_to_cpu(src->max_run[i]);
680 dst->min_run[i] = le64_to_cpu(src->min_run[i]);
681 dst->max_bw[i] = le64_to_cpu(src->max_bw[i]);
682 dst->min_bw[i] = le64_to_cpu(src->min_bw[i]);
683 dst->io_kb[i] = le64_to_cpu(src->io_kb[i]);
684 dst->agg[i] = le64_to_cpu(src->agg[i]);
685 }
686
687 dst->kb_base = le32_to_cpu(src->kb_base);
688 dst->groupid = le32_to_cpu(src->groupid);
689}
690
Jens Axboe89e5fad2012-03-05 09:21:12 +0100691static void handle_ts(struct fio_client *client, struct fio_net_cmd *cmd)
Jens Axboea64e88d2011-10-03 14:20:01 +0200692{
693 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
694
Jens Axboea64e88d2011-10-03 14:20:01 +0200695 show_thread_status(&p->ts, &p->rs);
Jens Axboe37f0c1a2011-10-11 14:08:33 +0200696
697 if (sum_stat_clients == 1)
698 return;
699
700 sum_thread_stats(&client_ts, &p->ts, sum_stat_nr);
701 sum_group_stats(&client_gs, &p->rs);
702
703 client_ts.members++;
704 client_ts.groupid = p->ts.groupid;
705
706 if (++sum_stat_nr == sum_stat_clients) {
707 strcpy(client_ts.name, "All clients");
708 show_thread_status(&client_ts, &client_gs);
709 }
Jens Axboea64e88d2011-10-03 14:20:01 +0200710}
711
Jens Axboe89e5fad2012-03-05 09:21:12 +0100712static void handle_gs(struct fio_client *client, struct fio_net_cmd *cmd)
Jens Axboea64e88d2011-10-03 14:20:01 +0200713{
714 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
715
Jens Axboea64e88d2011-10-03 14:20:01 +0200716 show_group_stats(gs);
717}
718
Jens Axboe3bf236c2012-03-03 20:35:50 +0100719static void handle_text(struct fio_client *client, struct fio_net_cmd *cmd)
720{
721 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
722 const char *buf = (const char *) pdu->buf;
723 const char *name;
724 int fio_unused ret;
725
726 name = client->name ? client->name : client->hostname;
727
728 if (!client->skip_newline)
729 fprintf(f_out, "<%s> ", name);
730 ret = fwrite(buf, pdu->buf_len, 1, f_out);
731 fflush(f_out);
732 client->skip_newline = strchr(buf, '\n') == NULL;
733}
734
Jens Axboed09a64a2011-10-13 11:38:56 +0200735static void convert_agg(struct disk_util_agg *agg)
736{
737 int i;
738
739 for (i = 0; i < 2; i++) {
740 agg->ios[i] = le32_to_cpu(agg->ios[i]);
741 agg->merges[i] = le32_to_cpu(agg->merges[i]);
742 agg->sectors[i] = le64_to_cpu(agg->sectors[i]);
743 agg->ticks[i] = le32_to_cpu(agg->ticks[i]);
744 }
745
746 agg->io_ticks = le32_to_cpu(agg->io_ticks);
747 agg->time_in_queue = le32_to_cpu(agg->time_in_queue);
748 agg->slavecount = le32_to_cpu(agg->slavecount);
Anton Blanchard823ba542011-11-07 14:16:26 +0100749 agg->max_util.u.f = fio_uint64_to_double(__le64_to_cpu(agg->max_util.u.i));
Jens Axboed09a64a2011-10-13 11:38:56 +0200750}
751
752static void convert_dus(struct disk_util_stat *dus)
753{
754 int i;
755
756 for (i = 0; i < 2; i++) {
757 dus->ios[i] = le32_to_cpu(dus->ios[i]);
758 dus->merges[i] = le32_to_cpu(dus->merges[i]);
759 dus->sectors[i] = le64_to_cpu(dus->sectors[i]);
760 dus->ticks[i] = le32_to_cpu(dus->ticks[i]);
761 }
762
763 dus->io_ticks = le32_to_cpu(dus->io_ticks);
764 dus->time_in_queue = le32_to_cpu(dus->time_in_queue);
765 dus->msec = le64_to_cpu(dus->msec);
766}
767
768static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd)
769{
770 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
771
Jens Axboed09a64a2011-10-13 11:38:56 +0200772 if (!client->disk_stats_shown) {
773 client->disk_stats_shown = 1;
774 log_info("\nDisk stats (read/write):\n");
775 }
776
Jens Axboef2f788d2011-10-13 14:03:52 +0200777 print_disk_util(&du->dus, &du->agg, terse_output);
Jens Axboed09a64a2011-10-13 11:38:56 +0200778}
779
Jens Axboe3bf236c2012-03-03 20:35:50 +0100780static void convert_jobs_eta(struct jobs_eta *je)
Jens Axboecf451d12011-10-03 16:48:30 +0200781{
Jens Axboecf451d12011-10-03 16:48:30 +0200782 int i;
783
784 je->nr_running = le32_to_cpu(je->nr_running);
785 je->nr_ramp = le32_to_cpu(je->nr_ramp);
786 je->nr_pending = le32_to_cpu(je->nr_pending);
787 je->files_open = le32_to_cpu(je->files_open);
Jens Axboecf451d12011-10-03 16:48:30 +0200788
789 for (i = 0; i < 2; i++) {
Jens Axboe3e47bd22012-02-29 13:45:02 +0100790 je->m_rate[i] = le32_to_cpu(je->m_rate[i]);
791 je->t_rate[i] = le32_to_cpu(je->t_rate[i]);
792 je->m_iops[i] = le32_to_cpu(je->m_iops[i]);
793 je->t_iops[i] = le32_to_cpu(je->t_iops[i]);
Jens Axboecf451d12011-10-03 16:48:30 +0200794 je->rate[i] = le32_to_cpu(je->rate[i]);
795 je->iops[i] = le32_to_cpu(je->iops[i]);
796 }
797
Jens Axboeb51eedb2011-10-09 12:13:39 +0200798 je->elapsed_sec = le64_to_cpu(je->elapsed_sec);
Jens Axboecf451d12011-10-03 16:48:30 +0200799 je->eta_sec = le64_to_cpu(je->eta_sec);
Jens Axboe8c621fb2012-03-08 12:30:48 +0100800 je->nr_threads = le32_to_cpu(je->nr_threads);
Jens Axboe48fbb462011-10-09 12:19:08 +0200801}
Jens Axboecf451d12011-10-03 16:48:30 +0200802
Jens Axboe3e47bd22012-02-29 13:45:02 +0100803void fio_client_sum_jobs_eta(struct jobs_eta *dst, struct jobs_eta *je)
Jens Axboe48fbb462011-10-09 12:19:08 +0200804{
Jens Axboe48fbb462011-10-09 12:19:08 +0200805 int i;
806
807 dst->nr_running += je->nr_running;
808 dst->nr_ramp += je->nr_ramp;
809 dst->nr_pending += je->nr_pending;
810 dst->files_open += je->files_open;
Jens Axboe48fbb462011-10-09 12:19:08 +0200811
812 for (i = 0; i < 2; i++) {
Jens Axboe3e47bd22012-02-29 13:45:02 +0100813 dst->m_rate[i] += je->m_rate[i];
814 dst->t_rate[i] += je->t_rate[i];
815 dst->m_iops[i] += je->m_iops[i];
816 dst->t_iops[i] += je->t_iops[i];
Jens Axboe48fbb462011-10-09 12:19:08 +0200817 dst->rate[i] += je->rate[i];
818 dst->iops[i] += je->iops[i];
819 }
820
821 dst->elapsed_sec += je->elapsed_sec;
822
823 if (je->eta_sec > dst->eta_sec)
824 dst->eta_sec = je->eta_sec;
Jens Axboe8c621fb2012-03-08 12:30:48 +0100825
826 dst->nr_threads += je->nr_threads;
827 /* we need to handle je->run_str too ... */
Jens Axboe48fbb462011-10-09 12:19:08 +0200828}
829
Jens Axboea5276612012-03-04 15:15:08 +0100830void fio_client_dec_jobs_eta(struct client_eta *eta, client_eta_op eta_fn)
Jens Axboe82c1ed32011-10-10 08:56:18 +0200831{
832 if (!--eta->pending) {
Jens Axboea5276612012-03-04 15:15:08 +0100833 eta_fn(&eta->eta);
Jens Axboe82c1ed32011-10-10 08:56:18 +0200834 free(eta);
835 }
836}
837
Jens Axboe89c17072011-10-11 10:15:51 +0200838static void remove_reply_cmd(struct fio_client *client, struct fio_net_cmd *cmd)
839{
840 struct fio_net_int_cmd *icmd = NULL;
841 struct flist_head *entry;
842
843 flist_for_each(entry, &client->cmd_list) {
844 icmd = flist_entry(entry, struct fio_net_int_cmd, list);
845
Jens Axboedf380932011-10-11 14:25:08 +0200846 if (cmd->tag == (uintptr_t) icmd)
Jens Axboe89c17072011-10-11 10:15:51 +0200847 break;
848
849 icmd = NULL;
850 }
851
852 if (!icmd) {
853 log_err("fio: client: unable to find matching tag\n");
854 return;
855 }
856
857 flist_del(&icmd->list);
858 cmd->tag = icmd->saved_tag;
859 free(icmd);
860}
861
Jens Axboe82c1ed32011-10-10 08:56:18 +0200862static void handle_eta(struct fio_client *client, struct fio_net_cmd *cmd)
Jens Axboe48fbb462011-10-09 12:19:08 +0200863{
864 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
Jens Axboedf380932011-10-11 14:25:08 +0200865 struct client_eta *eta = (struct client_eta *) (uintptr_t) cmd->tag;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200866
867 dprint(FD_NET, "client: got eta tag %p, %d\n", eta, eta->pending);
Jens Axboe48fbb462011-10-09 12:19:08 +0200868
Jens Axboef77d2672011-10-10 14:36:07 +0200869 assert(client->eta_in_flight == eta);
870
871 client->eta_in_flight = NULL;
Jens Axboe82c1ed32011-10-10 08:56:18 +0200872 flist_del_init(&client->eta_list);
873
Jens Axboe2f99deb2012-03-09 14:37:29 +0100874 if (client->ops->jobs_eta)
875 client->ops->jobs_eta(client, je);
876
Jens Axboe3e47bd22012-02-29 13:45:02 +0100877 fio_client_sum_jobs_eta(&eta->eta, je);
Jens Axboea5276612012-03-04 15:15:08 +0100878 fio_client_dec_jobs_eta(eta, client->ops->eta);
Jens Axboecf451d12011-10-03 16:48:30 +0200879}
880
Jens Axboeb5296dd2011-10-07 16:35:56 +0200881static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd)
Jens Axboe2e03b4b2011-10-04 09:00:40 +0200882{
883 struct cmd_probe_pdu *probe = (struct cmd_probe_pdu *) cmd->payload;
Jens Axboed2333352011-10-11 15:07:23 +0200884 const char *os, *arch;
885 char bit[16];
Jens Axboe2e03b4b2011-10-04 09:00:40 +0200886
Jens Axboecca84642011-10-07 12:47:57 +0200887 os = fio_get_os_string(probe->os);
888 if (!os)
889 os = "unknown";
890
891 arch = fio_get_arch_string(probe->arch);
892 if (!arch)
893 os = "unknown";
894
Jens Axboed2333352011-10-11 15:07:23 +0200895 sprintf(bit, "%d-bit", probe->bpp * 8);
Jens Axboe38fdef22011-10-11 14:30:06 +0200896
897 log_info("hostname=%s, be=%u, %s, os=%s, arch=%s, fio=%u.%u.%u\n",
898 probe->hostname, probe->bigendian, bit, os, arch,
899 probe->fio_major, probe->fio_minor, probe->fio_patch);
Jens Axboeb5296dd2011-10-07 16:35:56 +0200900
901 if (!client->name)
902 client->name = strdup((char *) probe->hostname);
Jens Axboe2e03b4b2011-10-04 09:00:40 +0200903}
904
Jens Axboe11e950b2011-10-16 21:34:14 +0200905static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd)
906{
907 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
908
909 client->state = Client_started;
910 client->jobs = le32_to_cpu(pdu->jobs);
911}
912
913static void handle_stop(struct fio_client *client, struct fio_net_cmd *cmd)
914{
Jens Axboe498c92c2011-10-17 09:14:42 +0200915 if (client->error)
916 log_info("client <%s>: exited with error %d\n", client->hostname, client->error);
Jens Axboe11e950b2011-10-16 21:34:14 +0200917}
918
Jens Axboe6b79c802012-03-08 10:51:36 +0100919static void convert_stop(struct fio_net_cmd *cmd)
920{
921 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
922
923 pdu->error = le32_to_cpu(pdu->error);
924}
925
Jens Axboe084d1c62012-03-03 20:28:07 +0100926static void convert_text(struct fio_net_cmd *cmd)
927{
928 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
929
930 pdu->level = le32_to_cpu(pdu->level);
931 pdu->buf_len = le32_to_cpu(pdu->buf_len);
932 pdu->log_sec = le64_to_cpu(pdu->log_sec);
933 pdu->log_usec = le64_to_cpu(pdu->log_usec);
934}
935
Jens Axboea5276612012-03-04 15:15:08 +0100936int fio_handle_client(struct fio_client *client)
Jens Axboe37db14f2011-09-30 17:00:42 -0600937{
Jens Axboea5276612012-03-04 15:15:08 +0100938 struct client_ops *ops = client->ops;
Jens Axboe37db14f2011-09-30 17:00:42 -0600939 struct fio_net_cmd *cmd;
940
Jens Axboe60efd142011-10-04 13:30:11 +0200941 dprint(FD_NET, "client: handle %s\n", client->hostname);
942
Jens Axboee951bdc2011-10-05 21:58:45 +0200943 cmd = fio_net_recv_cmd(client->fd);
944 if (!cmd)
945 return 0;
Jens Axboec2c94582011-10-05 20:31:30 +0200946
Jens Axboeb9d2f302012-03-08 20:36:28 +0100947 dprint(FD_NET, "client: got cmd op %s from %s (pdu=%u)\n",
948 fio_server_op(cmd->opcode), client->hostname, cmd->pdu_len);
Jens Axboe46c48f12011-10-01 12:50:51 -0600949
Jens Axboee951bdc2011-10-05 21:58:45 +0200950 switch (cmd->opcode) {
951 case FIO_NET_CMD_QUIT:
Jens Axboe3ec62ec2012-03-01 12:01:29 +0100952 if (ops->quit)
953 ops->quit(client);
Jens Axboee951bdc2011-10-05 21:58:45 +0200954 remove_client(client);
955 free(cmd);
956 break;
Jens Axboe084d1c62012-03-03 20:28:07 +0100957 case FIO_NET_CMD_TEXT:
958 convert_text(cmd);
959 ops->text_op(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +0200960 free(cmd);
961 break;
Jens Axboe3bf236c2012-03-03 20:35:50 +0100962 case FIO_NET_CMD_DU: {
963 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
964
965 convert_dus(&du->dus);
966 convert_agg(&du->agg);
967
Stephen M. Camerondd366722012-02-24 08:17:30 +0100968 ops->disk_util(client, cmd);
Jens Axboed09a64a2011-10-13 11:38:56 +0200969 free(cmd);
970 break;
Jens Axboe3bf236c2012-03-03 20:35:50 +0100971 }
972 case FIO_NET_CMD_TS: {
973 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
974
975 convert_ts(&p->ts, &p->ts);
976 convert_gs(&p->rs, &p->rs);
977
Jens Axboe89e5fad2012-03-05 09:21:12 +0100978 ops->thread_status(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +0200979 free(cmd);
980 break;
Jens Axboe3bf236c2012-03-03 20:35:50 +0100981 }
982 case FIO_NET_CMD_GS: {
983 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
984
985 convert_gs(gs, gs);
986
Jens Axboe89e5fad2012-03-05 09:21:12 +0100987 ops->group_stats(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +0200988 free(cmd);
989 break;
Jens Axboe3bf236c2012-03-03 20:35:50 +0100990 }
991 case FIO_NET_CMD_ETA: {
992 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
993
Jens Axboe89c17072011-10-11 10:15:51 +0200994 remove_reply_cmd(client, cmd);
Jens Axboe3bf236c2012-03-03 20:35:50 +0100995 convert_jobs_eta(je);
Jens Axboea5276612012-03-04 15:15:08 +0100996 handle_eta(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +0200997 free(cmd);
998 break;
Jens Axboe3bf236c2012-03-03 20:35:50 +0100999 }
Jens Axboee951bdc2011-10-05 21:58:45 +02001000 case FIO_NET_CMD_PROBE:
Jens Axboe89c17072011-10-11 10:15:51 +02001001 remove_reply_cmd(client, cmd);
Stephen M. Camerondd366722012-02-24 08:17:30 +01001002 ops->probe(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +02001003 free(cmd);
1004 break;
Jens Axboe5d7793a2012-03-08 19:59:16 +01001005 case FIO_NET_CMD_SERVER_START:
Jens Axboe01be0382011-10-15 14:43:41 +02001006 client->state = Client_running;
1007 free(cmd);
1008 break;
Jens Axboee951bdc2011-10-05 21:58:45 +02001009 case FIO_NET_CMD_START:
Jens Axboe11e950b2011-10-16 21:34:14 +02001010 handle_start(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +02001011 free(cmd);
1012 break;
Jens Axboe6b79c802012-03-08 10:51:36 +01001013 case FIO_NET_CMD_STOP: {
1014 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
1015
1016 convert_stop(cmd);
1017 client->state = Client_stopped;
1018 client->error = pdu->error;
1019 ops->stop(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +02001020 free(cmd);
1021 break;
Jens Axboe6b79c802012-03-08 10:51:36 +01001022 }
Jens Axboe807f9972012-03-02 10:25:24 +01001023 case FIO_NET_CMD_ADD_JOB:
1024 if (ops->add_job)
1025 ops->add_job(client, cmd);
1026 free(cmd);
1027 break;
Jens Axboee951bdc2011-10-05 21:58:45 +02001028 default:
Jens Axboe89c17072011-10-11 10:15:51 +02001029 log_err("fio: unknown client op: %s\n", fio_server_op(cmd->opcode));
Jens Axboee951bdc2011-10-05 21:58:45 +02001030 free(cmd);
1031 break;
Jens Axboe37db14f2011-09-30 17:00:42 -06001032 }
1033
Jens Axboee951bdc2011-10-05 21:58:45 +02001034 return 1;
Jens Axboe37db14f2011-09-30 17:00:42 -06001035}
Jens Axboeb66570d2011-10-01 11:11:35 -06001036
Jens Axboea5276612012-03-04 15:15:08 +01001037static void request_client_etas(struct client_ops *ops)
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001038{
1039 struct fio_client *client;
1040 struct flist_head *entry;
1041 struct client_eta *eta;
Jens Axboe82c1ed32011-10-10 08:56:18 +02001042 int skipped = 0;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001043
1044 dprint(FD_NET, "client: request eta (%d)\n", nr_clients);
1045
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001046 eta = malloc(sizeof(*eta));
1047 memset(&eta->eta, 0, sizeof(eta->eta));
1048 eta->pending = nr_clients;
1049
1050 flist_for_each(entry, &client_list) {
1051 client = flist_entry(entry, struct fio_client, list);
1052
Jens Axboe82c1ed32011-10-10 08:56:18 +02001053 if (!flist_empty(&client->eta_list)) {
1054 skipped++;
1055 continue;
1056 }
Jens Axboe01be0382011-10-15 14:43:41 +02001057 if (client->state != Client_running)
1058 continue;
Jens Axboe82c1ed32011-10-10 08:56:18 +02001059
Jens Axboef77d2672011-10-10 14:36:07 +02001060 assert(!client->eta_in_flight);
Jens Axboe82c1ed32011-10-10 08:56:18 +02001061 flist_add_tail(&client->eta_list, &eta_list);
Jens Axboef77d2672011-10-10 14:36:07 +02001062 client->eta_in_flight = eta;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001063 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_SEND_ETA,
Jens Axboedf380932011-10-11 14:25:08 +02001064 (uintptr_t) eta, &client->cmd_list);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001065 }
1066
Jens Axboe82c1ed32011-10-10 08:56:18 +02001067 while (skipped--)
Jens Axboea5276612012-03-04 15:15:08 +01001068 fio_client_dec_jobs_eta(eta, ops->eta);
Jens Axboe82c1ed32011-10-10 08:56:18 +02001069
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001070 dprint(FD_NET, "client: requested eta tag %p\n", eta);
1071}
1072
Jens Axboe89c17072011-10-11 10:15:51 +02001073static int client_check_cmd_timeout(struct fio_client *client,
1074 struct timeval *now)
1075{
1076 struct fio_net_int_cmd *cmd;
1077 struct flist_head *entry, *tmp;
1078 int ret = 0;
1079
1080 flist_for_each_safe(entry, tmp, &client->cmd_list) {
1081 cmd = flist_entry(entry, struct fio_net_int_cmd, list);
1082
1083 if (mtime_since(&cmd->tv, now) < FIO_NET_CLIENT_TIMEOUT)
1084 continue;
1085
1086 log_err("fio: client %s, timeout on cmd %s\n", client->hostname,
1087 fio_server_op(cmd->cmd.opcode));
1088 flist_del(&cmd->list);
1089 free(cmd);
1090 ret = 1;
1091 }
1092
1093 return flist_empty(&client->cmd_list) && ret;
1094}
1095
Jens Axboea5276612012-03-04 15:15:08 +01001096static int fio_check_clients_timed_out(void)
Jens Axboe89c17072011-10-11 10:15:51 +02001097{
1098 struct fio_client *client;
1099 struct flist_head *entry, *tmp;
1100 struct timeval tv;
1101 int ret = 0;
1102
1103 gettimeofday(&tv, NULL);
1104
1105 flist_for_each_safe(entry, tmp, &client_list) {
1106 client = flist_entry(entry, struct fio_client, list);
1107
1108 if (flist_empty(&client->cmd_list))
1109 continue;
1110
1111 if (!client_check_cmd_timeout(client, &tv))
1112 continue;
1113
Jens Axboea5276612012-03-04 15:15:08 +01001114 if (client->ops->timed_out)
1115 client->ops->timed_out(client);
Jens Axboeed727a42012-03-02 12:14:40 +01001116 else
1117 log_err("fio: client %s timed out\n", client->hostname);
1118
Jens Axboe89c17072011-10-11 10:15:51 +02001119 remove_client(client);
1120 ret = 1;
1121 }
1122
1123 return ret;
1124}
1125
Stephen M. Camerondd366722012-02-24 08:17:30 +01001126int fio_handle_clients(struct client_ops *ops)
Jens Axboeb66570d2011-10-01 11:11:35 -06001127{
Jens Axboeb66570d2011-10-01 11:11:35 -06001128 struct pollfd *pfds;
Jens Axboe498c92c2011-10-17 09:14:42 +02001129 int i, ret = 0, retval = 0;
Jens Axboeb66570d2011-10-01 11:11:35 -06001130
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001131 gettimeofday(&eta_tv, NULL);
1132
Jens Axboeb66570d2011-10-01 11:11:35 -06001133 pfds = malloc(nr_clients * sizeof(struct pollfd));
1134
Jens Axboe37f0c1a2011-10-11 14:08:33 +02001135 sum_stat_clients = nr_clients;
1136 init_thread_stat(&client_ts);
1137 init_group_run_stat(&client_gs);
1138
Jens Axboeb66570d2011-10-01 11:11:35 -06001139 while (!exit_backend && nr_clients) {
Jens Axboec2cb6862012-02-23 20:56:12 +01001140 struct flist_head *entry, *tmp;
1141 struct fio_client *client;
1142
Jens Axboe82a4be12011-10-01 12:40:32 -06001143 i = 0;
Jens Axboec2cb6862012-02-23 20:56:12 +01001144 flist_for_each_safe(entry, tmp, &client_list) {
Jens Axboe82a4be12011-10-01 12:40:32 -06001145 client = flist_entry(entry, struct fio_client, list);
1146
Jens Axboea5276612012-03-04 15:15:08 +01001147 if (!client->sent_job && !client->ops->stay_connected &&
Jens Axboec2cb6862012-02-23 20:56:12 +01001148 flist_empty(&client->cmd_list)) {
1149 remove_client(client);
1150 continue;
1151 }
1152
Jens Axboe82a4be12011-10-01 12:40:32 -06001153 pfds[i].fd = client->fd;
1154 pfds[i].events = POLLIN;
1155 i++;
1156 }
1157
Jens Axboec2cb6862012-02-23 20:56:12 +01001158 if (!nr_clients)
1159 break;
1160
Jens Axboe82a4be12011-10-01 12:40:32 -06001161 assert(i == nr_clients);
1162
Jens Axboe5c2857f2011-10-04 13:14:32 +02001163 do {
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001164 struct timeval tv;
1165
1166 gettimeofday(&tv, NULL);
Jens Axboe6433ee02012-03-09 20:10:51 +01001167 if (mtime_since(&eta_tv, &tv) >= ops->eta_msec) {
Jens Axboea5276612012-03-04 15:15:08 +01001168 request_client_etas(ops);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001169 memcpy(&eta_tv, &tv, sizeof(tv));
Jens Axboe89c17072011-10-11 10:15:51 +02001170
Jens Axboea5276612012-03-04 15:15:08 +01001171 if (fio_check_clients_timed_out())
Jens Axboe89c17072011-10-11 10:15:51 +02001172 break;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001173 }
1174
Jens Axboe5c2857f2011-10-04 13:14:32 +02001175 ret = poll(pfds, nr_clients, 100);
1176 if (ret < 0) {
1177 if (errno == EINTR)
1178 continue;
1179 log_err("fio: poll clients: %s\n", strerror(errno));
1180 break;
1181 } else if (!ret)
Jens Axboeb66570d2011-10-01 11:11:35 -06001182 continue;
Jens Axboe5c2857f2011-10-04 13:14:32 +02001183 } while (ret <= 0);
Jens Axboeb66570d2011-10-01 11:11:35 -06001184
1185 for (i = 0; i < nr_clients; i++) {
1186 if (!(pfds[i].revents & POLLIN))
1187 continue;
1188
1189 client = find_client_by_fd(pfds[i].fd);
1190 if (!client) {
Jens Axboe3c5f57e2011-10-06 12:37:50 +02001191 log_err("fio: unknown client fd %d\n", pfds[i].fd);
Jens Axboeb66570d2011-10-01 11:11:35 -06001192 continue;
1193 }
Jens Axboea5276612012-03-04 15:15:08 +01001194 if (!fio_handle_client(client)) {
Jens Axboe28d3ab02011-10-05 20:45:37 +02001195 log_info("client: host=%s disconnected\n",
1196 client->hostname);
1197 remove_client(client);
Jens Axboe498c92c2011-10-17 09:14:42 +02001198 retval = 1;
Jens Axboe38990762011-10-17 13:31:33 +02001199 } else if (client->error)
Jens Axboe498c92c2011-10-17 09:14:42 +02001200 retval = 1;
Jens Axboe343cb4a2012-03-09 17:16:51 +01001201 fio_put_client(client);
Jens Axboeb66570d2011-10-01 11:11:35 -06001202 }
1203 }
1204
1205 free(pfds);
Jens Axboe498c92c2011-10-17 09:14:42 +02001206 return retval;
Jens Axboeb66570d2011-10-01 11:11:35 -06001207}