blob: f2c157ae7525a030f6b23c7e9ad15b4032ad5c8d [file] [log] [blame]
Jens Axboe132159a2011-09-30 15:01:32 -06001#include <stdio.h>
2#include <stdlib.h>
3#include <unistd.h>
4#include <limits.h>
5#include <errno.h>
6#include <fcntl.h>
7#include <sys/poll.h>
8#include <sys/types.h>
9#include <sys/stat.h>
10#include <sys/wait.h>
Jens Axboed05c4a02011-10-05 00:16:11 +020011#include <sys/socket.h>
Jens Axboe87aa8f12011-10-06 21:24:13 +020012#include <sys/un.h>
Jens Axboe132159a2011-09-30 15:01:32 -060013#include <netinet/in.h>
14#include <arpa/inet.h>
15#include <netdb.h>
Jens Axboe9e22ecb2011-10-04 14:50:21 +020016#include <signal.h>
Jens Axboe132159a2011-09-30 15:01:32 -060017
18#include "fio.h"
Stephen M. Camerondd366722012-02-24 08:17:30 +010019#include "client.h"
Jens Axboe132159a2011-09-30 15:01:32 -060020#include "server.h"
Jens Axboeb66570d2011-10-01 11:11:35 -060021#include "flist.h"
Jens Axboe3c5f57e2011-10-06 12:37:50 +020022#include "hash.h"
Jens Axboe132159a2011-09-30 15:01:32 -060023
Stephen M. Camerondd366722012-02-24 08:17:30 +010024static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd);
Jens Axboe89e5fad2012-03-05 09:21:12 +010025static void handle_ts(struct fio_client *client, struct fio_net_cmd *cmd);
26static void handle_gs(struct fio_client *client, struct fio_net_cmd *cmd);
Stephen M. Camerondd366722012-02-24 08:17:30 +010027static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd);
Jens Axboe084d1c62012-03-03 20:28:07 +010028static void handle_text(struct fio_client *client, struct fio_net_cmd *cmd);
Jens Axboe6b79c802012-03-08 10:51:36 +010029static void handle_stop(struct fio_client *client, struct fio_net_cmd *cmd);
Jens Axboe85dd01e2012-03-12 14:33:16 +010030static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd);
Stephen M. Camerondd366722012-02-24 08:17:30 +010031
32struct client_ops fio_client_ops = {
Jens Axboe084d1c62012-03-03 20:28:07 +010033 .text_op = handle_text,
Jens Axboe0420ba62012-02-29 11:16:52 +010034 .disk_util = handle_du,
35 .thread_status = handle_ts,
36 .group_stats = handle_gs,
Jens Axboe6b79c802012-03-08 10:51:36 +010037 .stop = handle_stop,
Jens Axboe85dd01e2012-03-12 14:33:16 +010038 .start = handle_start,
Jens Axboea5276612012-03-04 15:15:08 +010039 .eta = display_thread_status,
Jens Axboe0420ba62012-02-29 11:16:52 +010040 .probe = handle_probe,
Jens Axboe6433ee02012-03-09 20:10:51 +010041 .eta_msec = FIO_CLIENT_DEF_ETA_MSEC,
Stephen M. Camerondd366722012-02-24 08:17:30 +010042};
43
Jens Axboeaf9c9fb2011-10-09 21:54:10 +020044static struct timeval eta_tv;
Jens Axboe48fbb462011-10-09 12:19:08 +020045
Jens Axboeb66570d2011-10-01 11:11:35 -060046static FLIST_HEAD(client_list);
Jens Axboe82c1ed32011-10-10 08:56:18 +020047static FLIST_HEAD(eta_list);
Jens Axboeb66570d2011-10-01 11:11:35 -060048
Jens Axboe3f3a4542011-10-10 21:11:09 +020049static FLIST_HEAD(arg_list);
50
Jens Axboe3650a3c2012-03-05 14:09:03 +010051struct thread_stat client_ts;
52struct group_run_stats client_gs;
53int sum_stat_clients;
54
Jens Axboe37f0c1a2011-10-11 14:08:33 +020055static int sum_stat_nr;
56
Jens Axboe3c5f57e2011-10-06 12:37:50 +020057#define FIO_CLIENT_HASH_BITS 7
58#define FIO_CLIENT_HASH_SZ (1 << FIO_CLIENT_HASH_BITS)
59#define FIO_CLIENT_HASH_MASK (FIO_CLIENT_HASH_SZ - 1)
Jens Axboebebe6392011-10-07 10:00:51 +020060static struct flist_head client_hash[FIO_CLIENT_HASH_SZ];
Jens Axboe3c5f57e2011-10-06 12:37:50 +020061
Jens Axboebebe6392011-10-07 10:00:51 +020062static void fio_client_add_hash(struct fio_client *client)
Jens Axboe3c5f57e2011-10-06 12:37:50 +020063{
64 int bucket = hash_long(client->fd, FIO_CLIENT_HASH_BITS);
65
66 bucket &= FIO_CLIENT_HASH_MASK;
Jens Axboebebe6392011-10-07 10:00:51 +020067 flist_add(&client->hash_list, &client_hash[bucket]);
Jens Axboe3c5f57e2011-10-06 12:37:50 +020068}
69
Jens Axboebebe6392011-10-07 10:00:51 +020070static void fio_client_remove_hash(struct fio_client *client)
Jens Axboe3c5f57e2011-10-06 12:37:50 +020071{
Jens Axboebebe6392011-10-07 10:00:51 +020072 if (!flist_empty(&client->hash_list))
73 flist_del_init(&client->hash_list);
Jens Axboe3c5f57e2011-10-06 12:37:50 +020074}
75
76static void fio_init fio_client_hash_init(void)
77{
78 int i;
79
Jens Axboebebe6392011-10-07 10:00:51 +020080 for (i = 0; i < FIO_CLIENT_HASH_SZ; i++)
81 INIT_FLIST_HEAD(&client_hash[i]);
Jens Axboe3c5f57e2011-10-06 12:37:50 +020082}
83
Jens Axboeb66570d2011-10-01 11:11:35 -060084static struct fio_client *find_client_by_fd(int fd)
85{
Jens Axboe3c5f57e2011-10-06 12:37:50 +020086 int bucket = hash_long(fd, FIO_CLIENT_HASH_BITS) & FIO_CLIENT_HASH_MASK;
Jens Axboeb66570d2011-10-01 11:11:35 -060087 struct fio_client *client;
88 struct flist_head *entry;
89
Jens Axboebebe6392011-10-07 10:00:51 +020090 flist_for_each(entry, &client_hash[bucket]) {
91 client = flist_entry(entry, struct fio_client, hash_list);
Jens Axboeb66570d2011-10-01 11:11:35 -060092
Jens Axboe343cb4a2012-03-09 17:16:51 +010093 if (client->fd == fd)
94 return fio_get_client(client);
Jens Axboeb66570d2011-10-01 11:11:35 -060095 }
96
97 return NULL;
98}
99
Jens Axboeb66570d2011-10-01 11:11:35 -0600100static void remove_client(struct fio_client *client)
101{
Jens Axboe5121a9a2012-03-05 13:32:47 +0100102 assert(client->refs);
103
104 if (--client->refs)
105 return;
106
Jens Axboe39e8e012011-10-04 13:46:08 +0200107 dprint(FD_NET, "client: removed <%s>\n", client->hostname);
Jens Axboeb66570d2011-10-01 11:11:35 -0600108 flist_del(&client->list);
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200109
Jens Axboebebe6392011-10-07 10:00:51 +0200110 fio_client_remove_hash(client);
Jens Axboe81179ee2011-10-04 12:42:06 +0200111
Jens Axboe82c1ed32011-10-10 08:56:18 +0200112 if (!flist_empty(&client->eta_list)) {
113 flist_del_init(&client->eta_list);
Jens Axboea5276612012-03-04 15:15:08 +0100114 fio_client_dec_jobs_eta(client->eta_in_flight, client->ops->eta);
Jens Axboe82c1ed32011-10-10 08:56:18 +0200115 }
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200116
Jens Axboeb66570d2011-10-01 11:11:35 -0600117 free(client->hostname);
Jens Axboe81179ee2011-10-04 12:42:06 +0200118 if (client->argv)
119 free(client->argv);
Jens Axboeb5296dd2011-10-07 16:35:56 +0200120 if (client->name)
121 free(client->name);
Jens Axboe81179ee2011-10-04 12:42:06 +0200122
Jens Axboeb66570d2011-10-01 11:11:35 -0600123 free(client);
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200124 nr_clients--;
Jens Axboe5fd0acb2011-10-11 14:20:22 +0200125 sum_stat_clients--;
Jens Axboeb66570d2011-10-01 11:11:35 -0600126}
Jens Axboe132159a2011-09-30 15:01:32 -0600127
Jens Axboe343cb4a2012-03-09 17:16:51 +0100128void fio_put_client(struct fio_client *client)
Jens Axboe5121a9a2012-03-05 13:32:47 +0100129{
130 remove_client(client);
131}
132
Jens Axboe343cb4a2012-03-09 17:16:51 +0100133struct fio_client *fio_get_client(struct fio_client *client)
134{
135 client->refs++;
136 return client;
137}
138
Jens Axboefa2ea802011-10-08 21:07:29 +0200139static void __fio_client_add_cmd_option(struct fio_client *client,
140 const char *opt)
Jens Axboe81179ee2011-10-04 12:42:06 +0200141{
Jens Axboe39e8e012011-10-04 13:46:08 +0200142 int index;
143
144 index = client->argc++;
Jens Axboe81179ee2011-10-04 12:42:06 +0200145 client->argv = realloc(client->argv, sizeof(char *) * client->argc);
Jens Axboe39e8e012011-10-04 13:46:08 +0200146 client->argv[index] = strdup(opt);
147 dprint(FD_NET, "client: add cmd %d: %s\n", index, opt);
Jens Axboe81179ee2011-10-04 12:42:06 +0200148}
149
Jens Axboefa2ea802011-10-08 21:07:29 +0200150void fio_client_add_cmd_option(void *cookie, const char *opt)
Jens Axboe81179ee2011-10-04 12:42:06 +0200151{
Jens Axboebebe6392011-10-07 10:00:51 +0200152 struct fio_client *client = cookie;
Jens Axboe3f3a4542011-10-10 21:11:09 +0200153 struct flist_head *entry;
Jens Axboe81179ee2011-10-04 12:42:06 +0200154
Jens Axboebebe6392011-10-07 10:00:51 +0200155 if (!client || !opt)
Jens Axboefa2ea802011-10-08 21:07:29 +0200156 return;
Jens Axboe81179ee2011-10-04 12:42:06 +0200157
Jens Axboefa2ea802011-10-08 21:07:29 +0200158 __fio_client_add_cmd_option(client, opt);
Jens Axboe3f3a4542011-10-10 21:11:09 +0200159
160 /*
161 * Duplicate arguments to shared client group
162 */
163 flist_for_each(entry, &arg_list) {
164 client = flist_entry(entry, struct fio_client, arg_list);
165
166 __fio_client_add_cmd_option(client, opt);
167 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200168}
169
Jens Axboea5276612012-03-04 15:15:08 +0100170struct fio_client *fio_client_add_explicit(struct client_ops *ops,
171 const char *hostname, int type,
Jens Axboe3ec62ec2012-03-01 12:01:29 +0100172 int port)
173{
174 struct fio_client *client;
175
176 client = malloc(sizeof(*client));
177 memset(client, 0, sizeof(*client));
178
179 INIT_FLIST_HEAD(&client->list);
180 INIT_FLIST_HEAD(&client->hash_list);
181 INIT_FLIST_HEAD(&client->arg_list);
182 INIT_FLIST_HEAD(&client->eta_list);
183 INIT_FLIST_HEAD(&client->cmd_list);
184
185 client->hostname = strdup(hostname);
186
187 if (type == Fio_client_socket)
188 client->is_sock = 1;
189 else {
190 int ipv6;
191
192 ipv6 = type == Fio_client_ipv6;
193 if (fio_server_parse_host(hostname, &ipv6,
194 &client->addr.sin_addr,
195 &client->addr6.sin6_addr))
196 goto err;
197
198 client->port = port;
199 }
200
201 client->fd = -1;
Jens Axboea5276612012-03-04 15:15:08 +0100202 client->ops = ops;
Jens Axboe5121a9a2012-03-05 13:32:47 +0100203 client->refs = 1;
Jens Axboe3ec62ec2012-03-01 12:01:29 +0100204
205 __fio_client_add_cmd_option(client, "fio");
206
207 flist_add(&client->list, &client_list);
208 nr_clients++;
209 dprint(FD_NET, "client: added <%s>\n", client->hostname);
210 return client;
211err:
212 free(client);
213 return NULL;
214}
215
Jens Axboea5276612012-03-04 15:15:08 +0100216int fio_client_add(struct client_ops *ops, const char *hostname, void **cookie)
Jens Axboe132159a2011-09-30 15:01:32 -0600217{
Jens Axboe3f3a4542011-10-10 21:11:09 +0200218 struct fio_client *existing = *cookie;
Jens Axboeb66570d2011-10-01 11:11:35 -0600219 struct fio_client *client;
Jens Axboe132159a2011-09-30 15:01:32 -0600220
Jens Axboe3f3a4542011-10-10 21:11:09 +0200221 if (existing) {
222 /*
223 * We always add our "exec" name as the option, hence 1
224 * means empty.
225 */
226 if (existing->argc == 1)
227 flist_add_tail(&existing->arg_list, &arg_list);
228 else {
229 while (!flist_empty(&arg_list))
230 flist_del_init(arg_list.next);
231 }
232 }
233
Jens Axboeb66570d2011-10-01 11:11:35 -0600234 client = malloc(sizeof(*client));
Jens Axboea37f69b2011-10-01 12:24:21 -0600235 memset(client, 0, sizeof(*client));
Jens Axboe81179ee2011-10-04 12:42:06 +0200236
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200237 INIT_FLIST_HEAD(&client->list);
Jens Axboebebe6392011-10-07 10:00:51 +0200238 INIT_FLIST_HEAD(&client->hash_list);
Jens Axboe3f3a4542011-10-10 21:11:09 +0200239 INIT_FLIST_HEAD(&client->arg_list);
Jens Axboe82c1ed32011-10-10 08:56:18 +0200240 INIT_FLIST_HEAD(&client->eta_list);
Jens Axboe89c17072011-10-11 10:15:51 +0200241 INIT_FLIST_HEAD(&client->cmd_list);
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200242
Jens Axboebebe6392011-10-07 10:00:51 +0200243 if (fio_server_parse_string(hostname, &client->hostname,
244 &client->is_sock, &client->port,
Jens Axboe811826b2011-10-24 09:11:50 +0200245 &client->addr.sin_addr,
246 &client->addr6.sin6_addr,
247 &client->ipv6))
Jens Axboebebe6392011-10-07 10:00:51 +0200248 return -1;
249
Jens Axboea37f69b2011-10-01 12:24:21 -0600250 client->fd = -1;
Jens Axboea5276612012-03-04 15:15:08 +0100251 client->ops = ops;
Jens Axboe5121a9a2012-03-05 13:32:47 +0100252 client->refs = 1;
Jens Axboe81179ee2011-10-04 12:42:06 +0200253
254 __fio_client_add_cmd_option(client, "fio");
255
Jens Axboea37f69b2011-10-01 12:24:21 -0600256 flist_add(&client->list, &client_list);
257 nr_clients++;
Jens Axboebebe6392011-10-07 10:00:51 +0200258 dprint(FD_NET, "client: added <%s>\n", client->hostname);
259 *cookie = client;
260 return 0;
Jens Axboea37f69b2011-10-01 12:24:21 -0600261}
262
Jens Axboed824c3b2012-03-09 17:45:43 +0100263static void probe_client(struct fio_client *client)
264{
265 dprint(FD_NET, "client: send probe\n");
266
267 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_PROBE, 0, &client->cmd_list);
268}
269
Jens Axboe87aa8f12011-10-06 21:24:13 +0200270static int fio_client_connect_ip(struct fio_client *client)
Jens Axboea37f69b2011-10-01 12:24:21 -0600271{
Jens Axboe811826b2011-10-24 09:11:50 +0200272 struct sockaddr *addr;
273 fio_socklen_t socklen;
274 int fd, domain;
Jens Axboe132159a2011-09-30 15:01:32 -0600275
Jens Axboe811826b2011-10-24 09:11:50 +0200276 if (client->ipv6) {
277 client->addr6.sin6_family = AF_INET6;
278 client->addr6.sin6_port = htons(client->port);
279 domain = AF_INET6;
280 addr = (struct sockaddr *) &client->addr6;
281 socklen = sizeof(client->addr6);
282 } else {
283 client->addr.sin_family = AF_INET;
284 client->addr.sin_port = htons(client->port);
285 domain = AF_INET;
286 addr = (struct sockaddr *) &client->addr;
287 socklen = sizeof(client->addr);
288 }
Jens Axboe132159a2011-09-30 15:01:32 -0600289
Jens Axboe811826b2011-10-24 09:11:50 +0200290 fd = socket(domain, SOCK_STREAM, 0);
Jens Axboe132159a2011-09-30 15:01:32 -0600291 if (fd < 0) {
Jens Axboe25095e12012-03-09 17:09:55 +0100292 int ret = -errno;
293
Jens Axboe132159a2011-09-30 15:01:32 -0600294 log_err("fio: socket: %s\n", strerror(errno));
Jens Axboe25095e12012-03-09 17:09:55 +0100295 return ret;
Jens Axboe132159a2011-09-30 15:01:32 -0600296 }
297
Jens Axboe811826b2011-10-24 09:11:50 +0200298 if (connect(fd, addr, socklen) < 0) {
Jens Axboe25095e12012-03-09 17:09:55 +0100299 int ret = -errno;
300
Jens Axboe132159a2011-09-30 15:01:32 -0600301 log_err("fio: connect: %s\n", strerror(errno));
Jens Axboea7de0a12011-10-07 12:55:14 +0200302 log_err("fio: failed to connect to %s:%u\n", client->hostname,
303 client->port);
Jens Axboeb94cba42011-10-06 21:27:10 +0200304 close(fd);
Jens Axboe25095e12012-03-09 17:09:55 +0100305 return ret;
Jens Axboe132159a2011-09-30 15:01:32 -0600306 }
307
Jens Axboe87aa8f12011-10-06 21:24:13 +0200308 return fd;
309}
310
311static int fio_client_connect_sock(struct fio_client *client)
312{
313 struct sockaddr_un *addr = &client->addr_un;
314 fio_socklen_t len;
315 int fd;
316
317 memset(addr, 0, sizeof(*addr));
318 addr->sun_family = AF_UNIX;
319 strcpy(addr->sun_path, client->hostname);
320
321 fd = socket(AF_UNIX, SOCK_STREAM, 0);
322 if (fd < 0) {
Jens Axboe25095e12012-03-09 17:09:55 +0100323 int ret = -errno;
324
Jens Axboe87aa8f12011-10-06 21:24:13 +0200325 log_err("fio: socket: %s\n", strerror(errno));
Jens Axboe25095e12012-03-09 17:09:55 +0100326 return ret;
Jens Axboe87aa8f12011-10-06 21:24:13 +0200327 }
328
329 len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
330 if (connect(fd, (struct sockaddr *) addr, len) < 0) {
Jens Axboe25095e12012-03-09 17:09:55 +0100331 int ret = -errno;
332
Jens Axboe87aa8f12011-10-06 21:24:13 +0200333 log_err("fio: connect; %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200334 close(fd);
Jens Axboe25095e12012-03-09 17:09:55 +0100335 return ret;
Jens Axboe87aa8f12011-10-06 21:24:13 +0200336 }
337
338 return fd;
339}
340
Jens Axboe2f99deb2012-03-09 14:37:29 +0100341int fio_client_connect(struct fio_client *client)
Jens Axboe87aa8f12011-10-06 21:24:13 +0200342{
343 int fd;
344
345 dprint(FD_NET, "client: connect to host %s\n", client->hostname);
346
Jens Axboe87aa8f12011-10-06 21:24:13 +0200347 if (client->is_sock)
348 fd = fio_client_connect_sock(client);
349 else
350 fd = fio_client_connect_ip(client);
351
Jens Axboe89c17072011-10-11 10:15:51 +0200352 dprint(FD_NET, "client: %s connected %d\n", client->hostname, fd);
353
Jens Axboe87aa8f12011-10-06 21:24:13 +0200354 if (fd < 0)
Jens Axboea1a3ed52012-03-09 17:00:01 +0100355 return fd;
Jens Axboe87aa8f12011-10-06 21:24:13 +0200356
Jens Axboeb66570d2011-10-01 11:11:35 -0600357 client->fd = fd;
Jens Axboebebe6392011-10-07 10:00:51 +0200358 fio_client_add_hash(client);
Jens Axboe81179ee2011-10-04 12:42:06 +0200359 client->state = Client_connected;
Jens Axboed824c3b2012-03-09 17:45:43 +0100360
361 probe_client(client);
Jens Axboe132159a2011-09-30 15:01:32 -0600362 return 0;
363}
364
Jens Axboe2f99deb2012-03-09 14:37:29 +0100365void fio_client_terminate(struct fio_client *client)
366{
367 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_QUIT, 0, NULL);
368}
369
Jens Axboecc0df002011-10-03 20:53:32 +0200370void fio_clients_terminate(void)
371{
372 struct flist_head *entry;
373 struct fio_client *client;
374
Jens Axboe60efd142011-10-04 13:30:11 +0200375 dprint(FD_NET, "client: terminate clients\n");
376
Jens Axboecc0df002011-10-03 20:53:32 +0200377 flist_for_each(entry, &client_list) {
378 client = flist_entry(entry, struct fio_client, list);
Jens Axboe2f99deb2012-03-09 14:37:29 +0100379 fio_client_terminate(client);
Jens Axboecc0df002011-10-03 20:53:32 +0200380 }
381}
382
383static void sig_int(int sig)
384{
Jens Axboebebe6392011-10-07 10:00:51 +0200385 dprint(FD_NET, "client: got signal %d\n", sig);
Jens Axboecc0df002011-10-03 20:53:32 +0200386 fio_clients_terminate();
387}
388
389static void client_signal_handler(void)
390{
391 struct sigaction act;
392
393 memset(&act, 0, sizeof(act));
394 act.sa_handler = sig_int;
395 act.sa_flags = SA_RESTART;
396 sigaction(SIGINT, &act, NULL);
397
398 memset(&act, 0, sizeof(act));
399 act.sa_handler = sig_int;
400 act.sa_flags = SA_RESTART;
401 sigaction(SIGTERM, &act, NULL);
402}
403
Jens Axboe81179ee2011-10-04 12:42:06 +0200404static int send_client_cmd_line(struct fio_client *client)
405{
Jens Axboefa2ea802011-10-08 21:07:29 +0200406 struct cmd_single_line_pdu *cslp;
407 struct cmd_line_pdu *clp;
408 unsigned long offset;
Jens Axboe7f868312011-10-10 09:55:21 +0200409 unsigned int *lens;
Jens Axboefa2ea802011-10-08 21:07:29 +0200410 void *pdu;
411 size_t mem;
Jens Axboe81179ee2011-10-04 12:42:06 +0200412 int i, ret;
413
Jens Axboe39e8e012011-10-04 13:46:08 +0200414 dprint(FD_NET, "client: send cmdline %d\n", client->argc);
Jens Axboe60efd142011-10-04 13:30:11 +0200415
Jens Axboe7f868312011-10-10 09:55:21 +0200416 lens = malloc(client->argc * sizeof(unsigned int));
417
Jens Axboefa2ea802011-10-08 21:07:29 +0200418 /*
419 * Find out how much mem we need
420 */
Jens Axboe7f868312011-10-10 09:55:21 +0200421 for (i = 0, mem = 0; i < client->argc; i++) {
422 lens[i] = strlen(client->argv[i]) + 1;
423 mem += lens[i];
424 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200425
Jens Axboefa2ea802011-10-08 21:07:29 +0200426 /*
427 * We need one cmd_line_pdu, and argc number of cmd_single_line_pdu
428 */
429 mem += sizeof(*clp) + (client->argc * sizeof(*cslp));
430
431 pdu = malloc(mem);
432 clp = pdu;
433 offset = sizeof(*clp);
434
435 for (i = 0; i < client->argc; i++) {
Jens Axboe7f868312011-10-10 09:55:21 +0200436 uint16_t arg_len = lens[i];
Jens Axboefa2ea802011-10-08 21:07:29 +0200437
438 cslp = pdu + offset;
439 strcpy((char *) cslp->text, client->argv[i]);
440 cslp->len = cpu_to_le16(arg_len);
441 offset += sizeof(*cslp) + arg_len;
442 }
443
Jens Axboe7f868312011-10-10 09:55:21 +0200444 free(lens);
Jens Axboefa2ea802011-10-08 21:07:29 +0200445 clp->lines = cpu_to_le16(client->argc);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200446 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOBLINE, pdu, mem, 0);
Jens Axboe81179ee2011-10-04 12:42:06 +0200447 free(pdu);
448 return ret;
449}
450
Jens Axboea37f69b2011-10-01 12:24:21 -0600451int fio_clients_connect(void)
452{
453 struct fio_client *client;
454 struct flist_head *entry, *tmp;
455 int ret;
456
Bruce Cran93bcfd22012-02-20 20:18:19 +0100457#ifdef WIN32
458 WSADATA wsd;
459 WSAStartup(MAKEWORD(2,2), &wsd);
460#endif
461
Jens Axboe60efd142011-10-04 13:30:11 +0200462 dprint(FD_NET, "client: connect all\n");
463
Jens Axboecc0df002011-10-03 20:53:32 +0200464 client_signal_handler();
465
Jens Axboea37f69b2011-10-01 12:24:21 -0600466 flist_for_each_safe(entry, tmp, &client_list) {
467 client = flist_entry(entry, struct fio_client, list);
468
469 ret = fio_client_connect(client);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200470 if (ret) {
Jens Axboea37f69b2011-10-01 12:24:21 -0600471 remove_client(client);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200472 continue;
473 }
474
Jens Axboe81179ee2011-10-04 12:42:06 +0200475 if (client->argc > 1)
476 send_client_cmd_line(client);
Jens Axboea37f69b2011-10-01 12:24:21 -0600477 }
478
479 return !nr_clients;
480}
481
Jens Axboeb9d2f302012-03-08 20:36:28 +0100482int fio_start_client(struct fio_client *client)
483{
484 dprint(FD_NET, "client: start %s\n", client->hostname);
485 return fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_RUN, 0, NULL);
486}
487
488int fio_start_all_clients(void)
489{
490 struct fio_client *client;
491 struct flist_head *entry, *tmp;
492 int ret;
493
494 dprint(FD_NET, "client: start all\n");
495
496 flist_for_each_safe(entry, tmp, &client_list) {
497 client = flist_entry(entry, struct fio_client, list);
498
499 ret = fio_start_client(client);
500 if (ret) {
501 remove_client(client);
502 continue;
503 }
504 }
505
506 return flist_empty(&client_list);
507}
508
Jens Axboe132159a2011-09-30 15:01:32 -0600509/*
510 * Send file contents to server backend. We could use sendfile(), but to remain
511 * more portable lets just read/write the darn thing.
512 */
Jens Axboe9988ca72012-03-09 15:14:06 +0100513static int __fio_client_send_ini(struct fio_client *client, const char *filename)
Jens Axboe132159a2011-09-30 15:01:32 -0600514{
515 struct stat sb;
516 char *p, *buf;
517 off_t len;
518 int fd, ret;
519
Jens Axboe46c48f12011-10-01 12:50:51 -0600520 dprint(FD_NET, "send ini %s to %s\n", filename, client->hostname);
521
Jens Axboe132159a2011-09-30 15:01:32 -0600522 fd = open(filename, O_RDONLY);
523 if (fd < 0) {
Jens Axboe25095e12012-03-09 17:09:55 +0100524 int ret = -errno;
525
Jens Axboee951bdc2011-10-05 21:58:45 +0200526 log_err("fio: job file <%s> open: %s\n", filename, strerror(errno));
Jens Axboe25095e12012-03-09 17:09:55 +0100527 return ret;
Jens Axboe132159a2011-09-30 15:01:32 -0600528 }
529
530 if (fstat(fd, &sb) < 0) {
Jens Axboe25095e12012-03-09 17:09:55 +0100531 int ret = -errno;
532
Jens Axboe132159a2011-09-30 15:01:32 -0600533 log_err("fio: job file stat: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200534 close(fd);
Jens Axboe25095e12012-03-09 17:09:55 +0100535 return ret;
Jens Axboe132159a2011-09-30 15:01:32 -0600536 }
537
538 buf = malloc(sb.st_size);
539
540 len = sb.st_size;
541 p = buf;
542 do {
543 ret = read(fd, p, len);
544 if (ret > 0) {
545 len -= ret;
546 if (!len)
547 break;
548 p += ret;
549 continue;
550 } else if (!ret)
551 break;
552 else if (errno == EAGAIN || errno == EINTR)
553 continue;
554 } while (1);
555
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200556 if (len) {
557 log_err("fio: failed reading job file %s\n", filename);
Jens Axboeb94cba42011-10-06 21:27:10 +0200558 close(fd);
Jens Axboec524ef72011-10-07 12:23:34 +0200559 free(buf);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200560 return 1;
561 }
562
Jens Axboec2cb6862012-02-23 20:56:12 +0100563 client->sent_job = 1;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200564 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, buf, sb.st_size, 0);
Jens Axboe132159a2011-09-30 15:01:32 -0600565 free(buf);
Jens Axboeb94cba42011-10-06 21:27:10 +0200566 close(fd);
Jens Axboe132159a2011-09-30 15:01:32 -0600567 return ret;
568}
Jens Axboe37db14f2011-09-30 17:00:42 -0600569
Jens Axboe9988ca72012-03-09 15:14:06 +0100570int fio_client_send_ini(struct fio_client *client, const char *filename)
571{
Jens Axboe25095e12012-03-09 17:09:55 +0100572 int ret;
573
574 ret = __fio_client_send_ini(client, filename);
575 if (ret) {
Jens Axboe9988ca72012-03-09 15:14:06 +0100576 remove_client(client);
Jens Axboe25095e12012-03-09 17:09:55 +0100577 return ret;
Jens Axboe9988ca72012-03-09 15:14:06 +0100578 }
579
580 client->sent_job = 1;
Jens Axboe25095e12012-03-09 17:09:55 +0100581 return ret;
Jens Axboe9988ca72012-03-09 15:14:06 +0100582}
583
Jens Axboea37f69b2011-10-01 12:24:21 -0600584int fio_clients_send_ini(const char *filename)
585{
586 struct fio_client *client;
587 struct flist_head *entry, *tmp;
588
589 flist_for_each_safe(entry, tmp, &client_list) {
590 client = flist_entry(entry, struct fio_client, list);
591
Jens Axboe9988ca72012-03-09 15:14:06 +0100592 fio_client_send_ini(client, filename);
Jens Axboea37f69b2011-10-01 12:24:21 -0600593 }
594
595 return !nr_clients;
596}
597
Jens Axboea64e88d2011-10-03 14:20:01 +0200598static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
599{
600 dst->max_val = le64_to_cpu(src->max_val);
601 dst->min_val = le64_to_cpu(src->min_val);
602 dst->samples = le64_to_cpu(src->samples);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200603
604 /*
605 * Floats arrive as IEEE 754 encoded uint64_t, convert back to double
606 */
607 dst->mean.u.f = fio_uint64_to_double(le64_to_cpu(dst->mean.u.i));
608 dst->S.u.f = fio_uint64_to_double(le64_to_cpu(dst->S.u.i));
Jens Axboea64e88d2011-10-03 14:20:01 +0200609}
610
611static void convert_ts(struct thread_stat *dst, struct thread_stat *src)
612{
613 int i, j;
614
615 dst->error = le32_to_cpu(src->error);
616 dst->groupid = le32_to_cpu(src->groupid);
617 dst->pid = le32_to_cpu(src->pid);
618 dst->members = le32_to_cpu(src->members);
619
620 for (i = 0; i < 2; i++) {
621 convert_io_stat(&dst->clat_stat[i], &src->clat_stat[i]);
622 convert_io_stat(&dst->slat_stat[i], &src->slat_stat[i]);
623 convert_io_stat(&dst->lat_stat[i], &src->lat_stat[i]);
624 convert_io_stat(&dst->bw_stat[i], &src->bw_stat[i]);
625 }
626
627 dst->usr_time = le64_to_cpu(src->usr_time);
628 dst->sys_time = le64_to_cpu(src->sys_time);
629 dst->ctx = le64_to_cpu(src->ctx);
630 dst->minf = le64_to_cpu(src->minf);
631 dst->majf = le64_to_cpu(src->majf);
632 dst->clat_percentiles = le64_to_cpu(src->clat_percentiles);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200633
634 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
635 fio_fp64_t *fps = &src->percentile_list[i];
636 fio_fp64_t *fpd = &dst->percentile_list[i];
637
638 fpd->u.f = fio_uint64_to_double(le64_to_cpu(fps->u.i));
639 }
Jens Axboea64e88d2011-10-03 14:20:01 +0200640
641 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
642 dst->io_u_map[i] = le32_to_cpu(src->io_u_map[i]);
643 dst->io_u_submit[i] = le32_to_cpu(src->io_u_submit[i]);
644 dst->io_u_complete[i] = le32_to_cpu(src->io_u_complete[i]);
645 }
646
647 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
648 dst->io_u_lat_u[i] = le32_to_cpu(src->io_u_lat_u[i]);
649 dst->io_u_lat_m[i] = le32_to_cpu(src->io_u_lat_m[i]);
650 }
651
652 for (i = 0; i < 2; i++)
653 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
654 dst->io_u_plat[i][j] = le32_to_cpu(src->io_u_plat[i][j]);
655
656 for (i = 0; i < 3; i++) {
657 dst->total_io_u[i] = le64_to_cpu(src->total_io_u[i]);
Jens Axboe93eee042011-10-03 21:08:48 +0200658 dst->short_io_u[i] = le64_to_cpu(src->short_io_u[i]);
Jens Axboea64e88d2011-10-03 14:20:01 +0200659 }
660
661 dst->total_submit = le64_to_cpu(src->total_submit);
662 dst->total_complete = le64_to_cpu(src->total_complete);
663
664 for (i = 0; i < 2; i++) {
665 dst->io_bytes[i] = le64_to_cpu(src->io_bytes[i]);
666 dst->runtime[i] = le64_to_cpu(src->runtime[i]);
667 }
668
669 dst->total_run_time = le64_to_cpu(src->total_run_time);
670 dst->continue_on_error = le16_to_cpu(src->continue_on_error);
671 dst->total_err_count = le64_to_cpu(src->total_err_count);
Jens Axboeddcc0b62011-10-03 14:45:27 +0200672 dst->first_error = le32_to_cpu(src->first_error);
673 dst->kb_base = le32_to_cpu(src->kb_base);
Jens Axboea64e88d2011-10-03 14:20:01 +0200674}
675
676static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
677{
678 int i;
679
680 for (i = 0; i < 2; i++) {
681 dst->max_run[i] = le64_to_cpu(src->max_run[i]);
682 dst->min_run[i] = le64_to_cpu(src->min_run[i]);
683 dst->max_bw[i] = le64_to_cpu(src->max_bw[i]);
684 dst->min_bw[i] = le64_to_cpu(src->min_bw[i]);
685 dst->io_kb[i] = le64_to_cpu(src->io_kb[i]);
686 dst->agg[i] = le64_to_cpu(src->agg[i]);
687 }
688
689 dst->kb_base = le32_to_cpu(src->kb_base);
690 dst->groupid = le32_to_cpu(src->groupid);
691}
692
Jens Axboe89e5fad2012-03-05 09:21:12 +0100693static void handle_ts(struct fio_client *client, struct fio_net_cmd *cmd)
Jens Axboea64e88d2011-10-03 14:20:01 +0200694{
695 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
696
Jens Axboea64e88d2011-10-03 14:20:01 +0200697 show_thread_status(&p->ts, &p->rs);
Jens Axboe37f0c1a2011-10-11 14:08:33 +0200698
699 if (sum_stat_clients == 1)
700 return;
701
702 sum_thread_stats(&client_ts, &p->ts, sum_stat_nr);
703 sum_group_stats(&client_gs, &p->rs);
704
705 client_ts.members++;
706 client_ts.groupid = p->ts.groupid;
707
708 if (++sum_stat_nr == sum_stat_clients) {
709 strcpy(client_ts.name, "All clients");
710 show_thread_status(&client_ts, &client_gs);
711 }
Jens Axboea64e88d2011-10-03 14:20:01 +0200712}
713
Jens Axboe89e5fad2012-03-05 09:21:12 +0100714static void handle_gs(struct fio_client *client, struct fio_net_cmd *cmd)
Jens Axboea64e88d2011-10-03 14:20:01 +0200715{
716 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
717
Jens Axboea64e88d2011-10-03 14:20:01 +0200718 show_group_stats(gs);
719}
720
Jens Axboe3bf236c2012-03-03 20:35:50 +0100721static void handle_text(struct fio_client *client, struct fio_net_cmd *cmd)
722{
723 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
724 const char *buf = (const char *) pdu->buf;
725 const char *name;
726 int fio_unused ret;
727
728 name = client->name ? client->name : client->hostname;
729
730 if (!client->skip_newline)
731 fprintf(f_out, "<%s> ", name);
732 ret = fwrite(buf, pdu->buf_len, 1, f_out);
733 fflush(f_out);
734 client->skip_newline = strchr(buf, '\n') == NULL;
735}
736
Jens Axboed09a64a2011-10-13 11:38:56 +0200737static void convert_agg(struct disk_util_agg *agg)
738{
739 int i;
740
741 for (i = 0; i < 2; i++) {
742 agg->ios[i] = le32_to_cpu(agg->ios[i]);
743 agg->merges[i] = le32_to_cpu(agg->merges[i]);
744 agg->sectors[i] = le64_to_cpu(agg->sectors[i]);
745 agg->ticks[i] = le32_to_cpu(agg->ticks[i]);
746 }
747
748 agg->io_ticks = le32_to_cpu(agg->io_ticks);
749 agg->time_in_queue = le32_to_cpu(agg->time_in_queue);
750 agg->slavecount = le32_to_cpu(agg->slavecount);
Anton Blanchard823ba542011-11-07 14:16:26 +0100751 agg->max_util.u.f = fio_uint64_to_double(__le64_to_cpu(agg->max_util.u.i));
Jens Axboed09a64a2011-10-13 11:38:56 +0200752}
753
754static void convert_dus(struct disk_util_stat *dus)
755{
756 int i;
757
758 for (i = 0; i < 2; i++) {
759 dus->ios[i] = le32_to_cpu(dus->ios[i]);
760 dus->merges[i] = le32_to_cpu(dus->merges[i]);
761 dus->sectors[i] = le64_to_cpu(dus->sectors[i]);
762 dus->ticks[i] = le32_to_cpu(dus->ticks[i]);
763 }
764
765 dus->io_ticks = le32_to_cpu(dus->io_ticks);
766 dus->time_in_queue = le32_to_cpu(dus->time_in_queue);
767 dus->msec = le64_to_cpu(dus->msec);
768}
769
770static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd)
771{
772 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
773
Jens Axboed09a64a2011-10-13 11:38:56 +0200774 if (!client->disk_stats_shown) {
775 client->disk_stats_shown = 1;
776 log_info("\nDisk stats (read/write):\n");
777 }
778
Jens Axboef2f788d2011-10-13 14:03:52 +0200779 print_disk_util(&du->dus, &du->agg, terse_output);
Jens Axboed09a64a2011-10-13 11:38:56 +0200780}
781
Jens Axboe3bf236c2012-03-03 20:35:50 +0100782static void convert_jobs_eta(struct jobs_eta *je)
Jens Axboecf451d12011-10-03 16:48:30 +0200783{
Jens Axboecf451d12011-10-03 16:48:30 +0200784 int i;
785
786 je->nr_running = le32_to_cpu(je->nr_running);
787 je->nr_ramp = le32_to_cpu(je->nr_ramp);
788 je->nr_pending = le32_to_cpu(je->nr_pending);
789 je->files_open = le32_to_cpu(je->files_open);
Jens Axboecf451d12011-10-03 16:48:30 +0200790
791 for (i = 0; i < 2; i++) {
Jens Axboe3e47bd22012-02-29 13:45:02 +0100792 je->m_rate[i] = le32_to_cpu(je->m_rate[i]);
793 je->t_rate[i] = le32_to_cpu(je->t_rate[i]);
794 je->m_iops[i] = le32_to_cpu(je->m_iops[i]);
795 je->t_iops[i] = le32_to_cpu(je->t_iops[i]);
Jens Axboecf451d12011-10-03 16:48:30 +0200796 je->rate[i] = le32_to_cpu(je->rate[i]);
797 je->iops[i] = le32_to_cpu(je->iops[i]);
798 }
799
Jens Axboeb51eedb2011-10-09 12:13:39 +0200800 je->elapsed_sec = le64_to_cpu(je->elapsed_sec);
Jens Axboecf451d12011-10-03 16:48:30 +0200801 je->eta_sec = le64_to_cpu(je->eta_sec);
Jens Axboe8c621fb2012-03-08 12:30:48 +0100802 je->nr_threads = le32_to_cpu(je->nr_threads);
Jens Axboe48fbb462011-10-09 12:19:08 +0200803}
Jens Axboecf451d12011-10-03 16:48:30 +0200804
Jens Axboe3e47bd22012-02-29 13:45:02 +0100805void fio_client_sum_jobs_eta(struct jobs_eta *dst, struct jobs_eta *je)
Jens Axboe48fbb462011-10-09 12:19:08 +0200806{
Jens Axboe48fbb462011-10-09 12:19:08 +0200807 int i;
808
809 dst->nr_running += je->nr_running;
810 dst->nr_ramp += je->nr_ramp;
811 dst->nr_pending += je->nr_pending;
812 dst->files_open += je->files_open;
Jens Axboe48fbb462011-10-09 12:19:08 +0200813
814 for (i = 0; i < 2; i++) {
Jens Axboe3e47bd22012-02-29 13:45:02 +0100815 dst->m_rate[i] += je->m_rate[i];
816 dst->t_rate[i] += je->t_rate[i];
817 dst->m_iops[i] += je->m_iops[i];
818 dst->t_iops[i] += je->t_iops[i];
Jens Axboe48fbb462011-10-09 12:19:08 +0200819 dst->rate[i] += je->rate[i];
820 dst->iops[i] += je->iops[i];
821 }
822
823 dst->elapsed_sec += je->elapsed_sec;
824
825 if (je->eta_sec > dst->eta_sec)
826 dst->eta_sec = je->eta_sec;
Jens Axboe8c621fb2012-03-08 12:30:48 +0100827
828 dst->nr_threads += je->nr_threads;
829 /* we need to handle je->run_str too ... */
Jens Axboe48fbb462011-10-09 12:19:08 +0200830}
831
Jens Axboea5276612012-03-04 15:15:08 +0100832void fio_client_dec_jobs_eta(struct client_eta *eta, client_eta_op eta_fn)
Jens Axboe82c1ed32011-10-10 08:56:18 +0200833{
834 if (!--eta->pending) {
Jens Axboea5276612012-03-04 15:15:08 +0100835 eta_fn(&eta->eta);
Jens Axboe82c1ed32011-10-10 08:56:18 +0200836 free(eta);
837 }
838}
839
Jens Axboe89c17072011-10-11 10:15:51 +0200840static void remove_reply_cmd(struct fio_client *client, struct fio_net_cmd *cmd)
841{
842 struct fio_net_int_cmd *icmd = NULL;
843 struct flist_head *entry;
844
845 flist_for_each(entry, &client->cmd_list) {
846 icmd = flist_entry(entry, struct fio_net_int_cmd, list);
847
Jens Axboedf380932011-10-11 14:25:08 +0200848 if (cmd->tag == (uintptr_t) icmd)
Jens Axboe89c17072011-10-11 10:15:51 +0200849 break;
850
851 icmd = NULL;
852 }
853
854 if (!icmd) {
855 log_err("fio: client: unable to find matching tag\n");
856 return;
857 }
858
859 flist_del(&icmd->list);
860 cmd->tag = icmd->saved_tag;
861 free(icmd);
862}
863
Jens Axboe82c1ed32011-10-10 08:56:18 +0200864static void handle_eta(struct fio_client *client, struct fio_net_cmd *cmd)
Jens Axboe48fbb462011-10-09 12:19:08 +0200865{
866 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
Jens Axboedf380932011-10-11 14:25:08 +0200867 struct client_eta *eta = (struct client_eta *) (uintptr_t) cmd->tag;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200868
869 dprint(FD_NET, "client: got eta tag %p, %d\n", eta, eta->pending);
Jens Axboe48fbb462011-10-09 12:19:08 +0200870
Jens Axboef77d2672011-10-10 14:36:07 +0200871 assert(client->eta_in_flight == eta);
872
873 client->eta_in_flight = NULL;
Jens Axboe82c1ed32011-10-10 08:56:18 +0200874 flist_del_init(&client->eta_list);
875
Jens Axboe2f99deb2012-03-09 14:37:29 +0100876 if (client->ops->jobs_eta)
877 client->ops->jobs_eta(client, je);
878
Jens Axboe3e47bd22012-02-29 13:45:02 +0100879 fio_client_sum_jobs_eta(&eta->eta, je);
Jens Axboea5276612012-03-04 15:15:08 +0100880 fio_client_dec_jobs_eta(eta, client->ops->eta);
Jens Axboecf451d12011-10-03 16:48:30 +0200881}
882
Jens Axboeb5296dd2011-10-07 16:35:56 +0200883static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd)
Jens Axboe2e03b4b2011-10-04 09:00:40 +0200884{
885 struct cmd_probe_pdu *probe = (struct cmd_probe_pdu *) cmd->payload;
Jens Axboed2333352011-10-11 15:07:23 +0200886 const char *os, *arch;
887 char bit[16];
Jens Axboe2e03b4b2011-10-04 09:00:40 +0200888
Jens Axboecca84642011-10-07 12:47:57 +0200889 os = fio_get_os_string(probe->os);
890 if (!os)
891 os = "unknown";
892
893 arch = fio_get_arch_string(probe->arch);
894 if (!arch)
895 os = "unknown";
896
Jens Axboed2333352011-10-11 15:07:23 +0200897 sprintf(bit, "%d-bit", probe->bpp * 8);
Jens Axboe38fdef22011-10-11 14:30:06 +0200898
899 log_info("hostname=%s, be=%u, %s, os=%s, arch=%s, fio=%u.%u.%u\n",
900 probe->hostname, probe->bigendian, bit, os, arch,
901 probe->fio_major, probe->fio_minor, probe->fio_patch);
Jens Axboeb5296dd2011-10-07 16:35:56 +0200902
903 if (!client->name)
904 client->name = strdup((char *) probe->hostname);
Jens Axboe2e03b4b2011-10-04 09:00:40 +0200905}
906
Jens Axboe11e950b2011-10-16 21:34:14 +0200907static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd)
908{
909 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
910
911 client->state = Client_started;
Jens Axboe85dd01e2012-03-12 14:33:16 +0100912 client->jobs = pdu->jobs;
Jens Axboe11e950b2011-10-16 21:34:14 +0200913}
914
915static void handle_stop(struct fio_client *client, struct fio_net_cmd *cmd)
916{
Jens Axboe498c92c2011-10-17 09:14:42 +0200917 if (client->error)
918 log_info("client <%s>: exited with error %d\n", client->hostname, client->error);
Jens Axboe11e950b2011-10-16 21:34:14 +0200919}
920
Jens Axboe6b79c802012-03-08 10:51:36 +0100921static void convert_stop(struct fio_net_cmd *cmd)
922{
923 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
924
925 pdu->error = le32_to_cpu(pdu->error);
926}
927
Jens Axboe084d1c62012-03-03 20:28:07 +0100928static void convert_text(struct fio_net_cmd *cmd)
929{
930 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
931
932 pdu->level = le32_to_cpu(pdu->level);
933 pdu->buf_len = le32_to_cpu(pdu->buf_len);
934 pdu->log_sec = le64_to_cpu(pdu->log_sec);
935 pdu->log_usec = le64_to_cpu(pdu->log_usec);
936}
937
Jens Axboea5276612012-03-04 15:15:08 +0100938int fio_handle_client(struct fio_client *client)
Jens Axboe37db14f2011-09-30 17:00:42 -0600939{
Jens Axboea5276612012-03-04 15:15:08 +0100940 struct client_ops *ops = client->ops;
Jens Axboe37db14f2011-09-30 17:00:42 -0600941 struct fio_net_cmd *cmd;
942
Jens Axboe60efd142011-10-04 13:30:11 +0200943 dprint(FD_NET, "client: handle %s\n", client->hostname);
944
Jens Axboee951bdc2011-10-05 21:58:45 +0200945 cmd = fio_net_recv_cmd(client->fd);
946 if (!cmd)
947 return 0;
Jens Axboec2c94582011-10-05 20:31:30 +0200948
Jens Axboeb9d2f302012-03-08 20:36:28 +0100949 dprint(FD_NET, "client: got cmd op %s from %s (pdu=%u)\n",
950 fio_server_op(cmd->opcode), client->hostname, cmd->pdu_len);
Jens Axboe46c48f12011-10-01 12:50:51 -0600951
Jens Axboee951bdc2011-10-05 21:58:45 +0200952 switch (cmd->opcode) {
953 case FIO_NET_CMD_QUIT:
Jens Axboe3ec62ec2012-03-01 12:01:29 +0100954 if (ops->quit)
955 ops->quit(client);
Jens Axboee951bdc2011-10-05 21:58:45 +0200956 remove_client(client);
957 free(cmd);
958 break;
Jens Axboe084d1c62012-03-03 20:28:07 +0100959 case FIO_NET_CMD_TEXT:
960 convert_text(cmd);
961 ops->text_op(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +0200962 free(cmd);
963 break;
Jens Axboe3bf236c2012-03-03 20:35:50 +0100964 case FIO_NET_CMD_DU: {
965 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
966
967 convert_dus(&du->dus);
968 convert_agg(&du->agg);
969
Stephen M. Camerondd366722012-02-24 08:17:30 +0100970 ops->disk_util(client, cmd);
Jens Axboed09a64a2011-10-13 11:38:56 +0200971 free(cmd);
972 break;
Jens Axboe3bf236c2012-03-03 20:35:50 +0100973 }
974 case FIO_NET_CMD_TS: {
975 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
976
977 convert_ts(&p->ts, &p->ts);
978 convert_gs(&p->rs, &p->rs);
979
Jens Axboe89e5fad2012-03-05 09:21:12 +0100980 ops->thread_status(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +0200981 free(cmd);
982 break;
Jens Axboe3bf236c2012-03-03 20:35:50 +0100983 }
984 case FIO_NET_CMD_GS: {
985 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
986
987 convert_gs(gs, gs);
988
Jens Axboe89e5fad2012-03-05 09:21:12 +0100989 ops->group_stats(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +0200990 free(cmd);
991 break;
Jens Axboe3bf236c2012-03-03 20:35:50 +0100992 }
993 case FIO_NET_CMD_ETA: {
994 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
995
Jens Axboe89c17072011-10-11 10:15:51 +0200996 remove_reply_cmd(client, cmd);
Jens Axboe3bf236c2012-03-03 20:35:50 +0100997 convert_jobs_eta(je);
Jens Axboea5276612012-03-04 15:15:08 +0100998 handle_eta(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +0200999 free(cmd);
1000 break;
Jens Axboe3bf236c2012-03-03 20:35:50 +01001001 }
Jens Axboee951bdc2011-10-05 21:58:45 +02001002 case FIO_NET_CMD_PROBE:
Jens Axboe89c17072011-10-11 10:15:51 +02001003 remove_reply_cmd(client, cmd);
Stephen M. Camerondd366722012-02-24 08:17:30 +01001004 ops->probe(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +02001005 free(cmd);
1006 break;
Jens Axboe5d7793a2012-03-08 19:59:16 +01001007 case FIO_NET_CMD_SERVER_START:
Jens Axboe01be0382011-10-15 14:43:41 +02001008 client->state = Client_running;
Jens Axboe85dd01e2012-03-12 14:33:16 +01001009 if (ops->job_start)
1010 ops->job_start(client, cmd);
Jens Axboe01be0382011-10-15 14:43:41 +02001011 free(cmd);
1012 break;
Jens Axboe85dd01e2012-03-12 14:33:16 +01001013 case FIO_NET_CMD_START: {
1014 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
1015
1016 pdu->jobs = le32_to_cpu(pdu->jobs);
1017 ops->start(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +02001018 free(cmd);
1019 break;
Jens Axboe85dd01e2012-03-12 14:33:16 +01001020 }
Jens Axboe6b79c802012-03-08 10:51:36 +01001021 case FIO_NET_CMD_STOP: {
1022 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
1023
1024 convert_stop(cmd);
1025 client->state = Client_stopped;
1026 client->error = pdu->error;
1027 ops->stop(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +02001028 free(cmd);
1029 break;
Jens Axboe6b79c802012-03-08 10:51:36 +01001030 }
Jens Axboe807f9972012-03-02 10:25:24 +01001031 case FIO_NET_CMD_ADD_JOB:
1032 if (ops->add_job)
1033 ops->add_job(client, cmd);
1034 free(cmd);
1035 break;
Jens Axboee951bdc2011-10-05 21:58:45 +02001036 default:
Jens Axboe89c17072011-10-11 10:15:51 +02001037 log_err("fio: unknown client op: %s\n", fio_server_op(cmd->opcode));
Jens Axboee951bdc2011-10-05 21:58:45 +02001038 free(cmd);
1039 break;
Jens Axboe37db14f2011-09-30 17:00:42 -06001040 }
1041
Jens Axboee951bdc2011-10-05 21:58:45 +02001042 return 1;
Jens Axboe37db14f2011-09-30 17:00:42 -06001043}
Jens Axboeb66570d2011-10-01 11:11:35 -06001044
Jens Axboea5276612012-03-04 15:15:08 +01001045static void request_client_etas(struct client_ops *ops)
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001046{
1047 struct fio_client *client;
1048 struct flist_head *entry;
1049 struct client_eta *eta;
Jens Axboe82c1ed32011-10-10 08:56:18 +02001050 int skipped = 0;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001051
1052 dprint(FD_NET, "client: request eta (%d)\n", nr_clients);
1053
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001054 eta = malloc(sizeof(*eta));
1055 memset(&eta->eta, 0, sizeof(eta->eta));
1056 eta->pending = nr_clients;
1057
1058 flist_for_each(entry, &client_list) {
1059 client = flist_entry(entry, struct fio_client, list);
1060
Jens Axboe82c1ed32011-10-10 08:56:18 +02001061 if (!flist_empty(&client->eta_list)) {
1062 skipped++;
1063 continue;
1064 }
Jens Axboe01be0382011-10-15 14:43:41 +02001065 if (client->state != Client_running)
1066 continue;
Jens Axboe82c1ed32011-10-10 08:56:18 +02001067
Jens Axboef77d2672011-10-10 14:36:07 +02001068 assert(!client->eta_in_flight);
Jens Axboe82c1ed32011-10-10 08:56:18 +02001069 flist_add_tail(&client->eta_list, &eta_list);
Jens Axboef77d2672011-10-10 14:36:07 +02001070 client->eta_in_flight = eta;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001071 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_SEND_ETA,
Jens Axboedf380932011-10-11 14:25:08 +02001072 (uintptr_t) eta, &client->cmd_list);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001073 }
1074
Jens Axboe82c1ed32011-10-10 08:56:18 +02001075 while (skipped--)
Jens Axboea5276612012-03-04 15:15:08 +01001076 fio_client_dec_jobs_eta(eta, ops->eta);
Jens Axboe82c1ed32011-10-10 08:56:18 +02001077
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001078 dprint(FD_NET, "client: requested eta tag %p\n", eta);
1079}
1080
Jens Axboe89c17072011-10-11 10:15:51 +02001081static int client_check_cmd_timeout(struct fio_client *client,
1082 struct timeval *now)
1083{
1084 struct fio_net_int_cmd *cmd;
1085 struct flist_head *entry, *tmp;
1086 int ret = 0;
1087
1088 flist_for_each_safe(entry, tmp, &client->cmd_list) {
1089 cmd = flist_entry(entry, struct fio_net_int_cmd, list);
1090
1091 if (mtime_since(&cmd->tv, now) < FIO_NET_CLIENT_TIMEOUT)
1092 continue;
1093
1094 log_err("fio: client %s, timeout on cmd %s\n", client->hostname,
1095 fio_server_op(cmd->cmd.opcode));
1096 flist_del(&cmd->list);
1097 free(cmd);
1098 ret = 1;
1099 }
1100
1101 return flist_empty(&client->cmd_list) && ret;
1102}
1103
Jens Axboea5276612012-03-04 15:15:08 +01001104static int fio_check_clients_timed_out(void)
Jens Axboe89c17072011-10-11 10:15:51 +02001105{
1106 struct fio_client *client;
1107 struct flist_head *entry, *tmp;
1108 struct timeval tv;
1109 int ret = 0;
1110
1111 gettimeofday(&tv, NULL);
1112
1113 flist_for_each_safe(entry, tmp, &client_list) {
1114 client = flist_entry(entry, struct fio_client, list);
1115
1116 if (flist_empty(&client->cmd_list))
1117 continue;
1118
1119 if (!client_check_cmd_timeout(client, &tv))
1120 continue;
1121
Jens Axboea5276612012-03-04 15:15:08 +01001122 if (client->ops->timed_out)
1123 client->ops->timed_out(client);
Jens Axboeed727a42012-03-02 12:14:40 +01001124 else
1125 log_err("fio: client %s timed out\n", client->hostname);
1126
Jens Axboe89c17072011-10-11 10:15:51 +02001127 remove_client(client);
1128 ret = 1;
1129 }
1130
1131 return ret;
1132}
1133
Stephen M. Camerondd366722012-02-24 08:17:30 +01001134int fio_handle_clients(struct client_ops *ops)
Jens Axboeb66570d2011-10-01 11:11:35 -06001135{
Jens Axboeb66570d2011-10-01 11:11:35 -06001136 struct pollfd *pfds;
Jens Axboe498c92c2011-10-17 09:14:42 +02001137 int i, ret = 0, retval = 0;
Jens Axboeb66570d2011-10-01 11:11:35 -06001138
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001139 gettimeofday(&eta_tv, NULL);
1140
Jens Axboeb66570d2011-10-01 11:11:35 -06001141 pfds = malloc(nr_clients * sizeof(struct pollfd));
1142
Jens Axboe37f0c1a2011-10-11 14:08:33 +02001143 sum_stat_clients = nr_clients;
1144 init_thread_stat(&client_ts);
1145 init_group_run_stat(&client_gs);
1146
Jens Axboeb66570d2011-10-01 11:11:35 -06001147 while (!exit_backend && nr_clients) {
Jens Axboec2cb6862012-02-23 20:56:12 +01001148 struct flist_head *entry, *tmp;
1149 struct fio_client *client;
1150
Jens Axboe82a4be12011-10-01 12:40:32 -06001151 i = 0;
Jens Axboec2cb6862012-02-23 20:56:12 +01001152 flist_for_each_safe(entry, tmp, &client_list) {
Jens Axboe82a4be12011-10-01 12:40:32 -06001153 client = flist_entry(entry, struct fio_client, list);
1154
Jens Axboea5276612012-03-04 15:15:08 +01001155 if (!client->sent_job && !client->ops->stay_connected &&
Jens Axboec2cb6862012-02-23 20:56:12 +01001156 flist_empty(&client->cmd_list)) {
1157 remove_client(client);
1158 continue;
1159 }
1160
Jens Axboe82a4be12011-10-01 12:40:32 -06001161 pfds[i].fd = client->fd;
1162 pfds[i].events = POLLIN;
1163 i++;
1164 }
1165
Jens Axboec2cb6862012-02-23 20:56:12 +01001166 if (!nr_clients)
1167 break;
1168
Jens Axboe82a4be12011-10-01 12:40:32 -06001169 assert(i == nr_clients);
1170
Jens Axboe5c2857f2011-10-04 13:14:32 +02001171 do {
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001172 struct timeval tv;
1173
1174 gettimeofday(&tv, NULL);
Jens Axboe6433ee02012-03-09 20:10:51 +01001175 if (mtime_since(&eta_tv, &tv) >= ops->eta_msec) {
Jens Axboea5276612012-03-04 15:15:08 +01001176 request_client_etas(ops);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001177 memcpy(&eta_tv, &tv, sizeof(tv));
Jens Axboe89c17072011-10-11 10:15:51 +02001178
Jens Axboea5276612012-03-04 15:15:08 +01001179 if (fio_check_clients_timed_out())
Jens Axboe89c17072011-10-11 10:15:51 +02001180 break;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001181 }
1182
Jens Axboe5c2857f2011-10-04 13:14:32 +02001183 ret = poll(pfds, nr_clients, 100);
1184 if (ret < 0) {
1185 if (errno == EINTR)
1186 continue;
1187 log_err("fio: poll clients: %s\n", strerror(errno));
1188 break;
1189 } else if (!ret)
Jens Axboeb66570d2011-10-01 11:11:35 -06001190 continue;
Jens Axboe5c2857f2011-10-04 13:14:32 +02001191 } while (ret <= 0);
Jens Axboeb66570d2011-10-01 11:11:35 -06001192
1193 for (i = 0; i < nr_clients; i++) {
1194 if (!(pfds[i].revents & POLLIN))
1195 continue;
1196
1197 client = find_client_by_fd(pfds[i].fd);
1198 if (!client) {
Jens Axboe3c5f57e2011-10-06 12:37:50 +02001199 log_err("fio: unknown client fd %d\n", pfds[i].fd);
Jens Axboeb66570d2011-10-01 11:11:35 -06001200 continue;
1201 }
Jens Axboea5276612012-03-04 15:15:08 +01001202 if (!fio_handle_client(client)) {
Jens Axboe28d3ab02011-10-05 20:45:37 +02001203 log_info("client: host=%s disconnected\n",
1204 client->hostname);
1205 remove_client(client);
Jens Axboe498c92c2011-10-17 09:14:42 +02001206 retval = 1;
Jens Axboe38990762011-10-17 13:31:33 +02001207 } else if (client->error)
Jens Axboe498c92c2011-10-17 09:14:42 +02001208 retval = 1;
Jens Axboe343cb4a2012-03-09 17:16:51 +01001209 fio_put_client(client);
Jens Axboeb66570d2011-10-01 11:11:35 -06001210 }
1211 }
1212
1213 free(pfds);
Jens Axboe498c92c2011-10-17 09:14:42 +02001214 return retval;
Jens Axboeb66570d2011-10-01 11:11:35 -06001215}