blob: 0da4f47a5b24f7eea6d9f2f2613c2ef2005c1ccc [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 Axboe3af45202012-03-13 09:59:53 +0100100void fio_put_client(struct fio_client *client)
Jens Axboeb66570d2011-10-01 11:11:35 -0600101{
Jens Axboe5121a9a2012-03-05 13:32:47 +0100102 if (--client->refs)
103 return;
104
Jens Axboeb66570d2011-10-01 11:11:35 -0600105 free(client->hostname);
Jens Axboe81179ee2011-10-04 12:42:06 +0200106 if (client->argv)
107 free(client->argv);
Jens Axboeb5296dd2011-10-07 16:35:56 +0200108 if (client->name)
109 free(client->name);
Jens Axboe81179ee2011-10-04 12:42:06 +0200110
Jens Axboeb66570d2011-10-01 11:11:35 -0600111 free(client);
112}
Jens Axboe132159a2011-09-30 15:01:32 -0600113
Jens Axboe3af45202012-03-13 09:59:53 +0100114static void remove_client(struct fio_client *client)
Jens Axboe5121a9a2012-03-05 13:32:47 +0100115{
Jens Axboe3af45202012-03-13 09:59:53 +0100116 assert(client->refs);
117
118 dprint(FD_NET, "client: removed <%s>\n", client->hostname);
119
120 if (!flist_empty(&client->list))
121 flist_del_init(&client->list);
122
123 fio_client_remove_hash(client);
124
125 if (!flist_empty(&client->eta_list)) {
126 flist_del_init(&client->eta_list);
127 fio_client_dec_jobs_eta(client->eta_in_flight, client->ops->eta);
128 }
129
130 nr_clients--;
131 sum_stat_clients--;
132
133 fio_put_client(client);
Jens Axboe5121a9a2012-03-05 13:32:47 +0100134}
135
Jens Axboe343cb4a2012-03-09 17:16:51 +0100136struct fio_client *fio_get_client(struct fio_client *client)
137{
138 client->refs++;
139 return client;
140}
141
Jens Axboefa2ea802011-10-08 21:07:29 +0200142static void __fio_client_add_cmd_option(struct fio_client *client,
143 const char *opt)
Jens Axboe81179ee2011-10-04 12:42:06 +0200144{
Jens Axboe39e8e012011-10-04 13:46:08 +0200145 int index;
146
147 index = client->argc++;
Jens Axboe81179ee2011-10-04 12:42:06 +0200148 client->argv = realloc(client->argv, sizeof(char *) * client->argc);
Jens Axboe39e8e012011-10-04 13:46:08 +0200149 client->argv[index] = strdup(opt);
150 dprint(FD_NET, "client: add cmd %d: %s\n", index, opt);
Jens Axboe81179ee2011-10-04 12:42:06 +0200151}
152
Jens Axboefa2ea802011-10-08 21:07:29 +0200153void fio_client_add_cmd_option(void *cookie, const char *opt)
Jens Axboe81179ee2011-10-04 12:42:06 +0200154{
Jens Axboebebe6392011-10-07 10:00:51 +0200155 struct fio_client *client = cookie;
Jens Axboe3f3a4542011-10-10 21:11:09 +0200156 struct flist_head *entry;
Jens Axboe81179ee2011-10-04 12:42:06 +0200157
Jens Axboebebe6392011-10-07 10:00:51 +0200158 if (!client || !opt)
Jens Axboefa2ea802011-10-08 21:07:29 +0200159 return;
Jens Axboe81179ee2011-10-04 12:42:06 +0200160
Jens Axboefa2ea802011-10-08 21:07:29 +0200161 __fio_client_add_cmd_option(client, opt);
Jens Axboe3f3a4542011-10-10 21:11:09 +0200162
163 /*
164 * Duplicate arguments to shared client group
165 */
166 flist_for_each(entry, &arg_list) {
167 client = flist_entry(entry, struct fio_client, arg_list);
168
169 __fio_client_add_cmd_option(client, opt);
170 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200171}
172
Jens Axboea5276612012-03-04 15:15:08 +0100173struct fio_client *fio_client_add_explicit(struct client_ops *ops,
174 const char *hostname, int type,
Jens Axboe3ec62ec2012-03-01 12:01:29 +0100175 int port)
176{
177 struct fio_client *client;
178
179 client = malloc(sizeof(*client));
180 memset(client, 0, sizeof(*client));
181
182 INIT_FLIST_HEAD(&client->list);
183 INIT_FLIST_HEAD(&client->hash_list);
184 INIT_FLIST_HEAD(&client->arg_list);
185 INIT_FLIST_HEAD(&client->eta_list);
186 INIT_FLIST_HEAD(&client->cmd_list);
187
188 client->hostname = strdup(hostname);
189
190 if (type == Fio_client_socket)
191 client->is_sock = 1;
192 else {
193 int ipv6;
194
195 ipv6 = type == Fio_client_ipv6;
196 if (fio_server_parse_host(hostname, &ipv6,
197 &client->addr.sin_addr,
198 &client->addr6.sin6_addr))
199 goto err;
200
201 client->port = port;
202 }
203
204 client->fd = -1;
Jens Axboea5276612012-03-04 15:15:08 +0100205 client->ops = ops;
Jens Axboe5121a9a2012-03-05 13:32:47 +0100206 client->refs = 1;
Jens Axboe3ec62ec2012-03-01 12:01:29 +0100207
208 __fio_client_add_cmd_option(client, "fio");
209
210 flist_add(&client->list, &client_list);
211 nr_clients++;
212 dprint(FD_NET, "client: added <%s>\n", client->hostname);
213 return client;
214err:
215 free(client);
216 return NULL;
217}
218
Jens Axboea5276612012-03-04 15:15:08 +0100219int fio_client_add(struct client_ops *ops, const char *hostname, void **cookie)
Jens Axboe132159a2011-09-30 15:01:32 -0600220{
Jens Axboe3f3a4542011-10-10 21:11:09 +0200221 struct fio_client *existing = *cookie;
Jens Axboeb66570d2011-10-01 11:11:35 -0600222 struct fio_client *client;
Jens Axboe132159a2011-09-30 15:01:32 -0600223
Jens Axboe3f3a4542011-10-10 21:11:09 +0200224 if (existing) {
225 /*
226 * We always add our "exec" name as the option, hence 1
227 * means empty.
228 */
229 if (existing->argc == 1)
230 flist_add_tail(&existing->arg_list, &arg_list);
231 else {
232 while (!flist_empty(&arg_list))
233 flist_del_init(arg_list.next);
234 }
235 }
236
Jens Axboeb66570d2011-10-01 11:11:35 -0600237 client = malloc(sizeof(*client));
Jens Axboea37f69b2011-10-01 12:24:21 -0600238 memset(client, 0, sizeof(*client));
Jens Axboe81179ee2011-10-04 12:42:06 +0200239
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200240 INIT_FLIST_HEAD(&client->list);
Jens Axboebebe6392011-10-07 10:00:51 +0200241 INIT_FLIST_HEAD(&client->hash_list);
Jens Axboe3f3a4542011-10-10 21:11:09 +0200242 INIT_FLIST_HEAD(&client->arg_list);
Jens Axboe82c1ed32011-10-10 08:56:18 +0200243 INIT_FLIST_HEAD(&client->eta_list);
Jens Axboe89c17072011-10-11 10:15:51 +0200244 INIT_FLIST_HEAD(&client->cmd_list);
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200245
Jens Axboebebe6392011-10-07 10:00:51 +0200246 if (fio_server_parse_string(hostname, &client->hostname,
247 &client->is_sock, &client->port,
Jens Axboe811826b2011-10-24 09:11:50 +0200248 &client->addr.sin_addr,
249 &client->addr6.sin6_addr,
250 &client->ipv6))
Jens Axboebebe6392011-10-07 10:00:51 +0200251 return -1;
252
Jens Axboea37f69b2011-10-01 12:24:21 -0600253 client->fd = -1;
Jens Axboea5276612012-03-04 15:15:08 +0100254 client->ops = ops;
Jens Axboe5121a9a2012-03-05 13:32:47 +0100255 client->refs = 1;
Jens Axboe81179ee2011-10-04 12:42:06 +0200256
257 __fio_client_add_cmd_option(client, "fio");
258
Jens Axboea37f69b2011-10-01 12:24:21 -0600259 flist_add(&client->list, &client_list);
260 nr_clients++;
Jens Axboebebe6392011-10-07 10:00:51 +0200261 dprint(FD_NET, "client: added <%s>\n", client->hostname);
262 *cookie = client;
263 return 0;
Jens Axboea37f69b2011-10-01 12:24:21 -0600264}
265
Jens Axboed824c3b2012-03-09 17:45:43 +0100266static void probe_client(struct fio_client *client)
267{
268 dprint(FD_NET, "client: send probe\n");
269
270 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_PROBE, 0, &client->cmd_list);
271}
272
Jens Axboe87aa8f12011-10-06 21:24:13 +0200273static int fio_client_connect_ip(struct fio_client *client)
Jens Axboea37f69b2011-10-01 12:24:21 -0600274{
Jens Axboe811826b2011-10-24 09:11:50 +0200275 struct sockaddr *addr;
276 fio_socklen_t socklen;
277 int fd, domain;
Jens Axboe132159a2011-09-30 15:01:32 -0600278
Jens Axboe811826b2011-10-24 09:11:50 +0200279 if (client->ipv6) {
280 client->addr6.sin6_family = AF_INET6;
281 client->addr6.sin6_port = htons(client->port);
282 domain = AF_INET6;
283 addr = (struct sockaddr *) &client->addr6;
284 socklen = sizeof(client->addr6);
285 } else {
286 client->addr.sin_family = AF_INET;
287 client->addr.sin_port = htons(client->port);
288 domain = AF_INET;
289 addr = (struct sockaddr *) &client->addr;
290 socklen = sizeof(client->addr);
291 }
Jens Axboe132159a2011-09-30 15:01:32 -0600292
Jens Axboe811826b2011-10-24 09:11:50 +0200293 fd = socket(domain, SOCK_STREAM, 0);
Jens Axboe132159a2011-09-30 15:01:32 -0600294 if (fd < 0) {
Jens Axboe25095e12012-03-09 17:09:55 +0100295 int ret = -errno;
296
Jens Axboe132159a2011-09-30 15:01:32 -0600297 log_err("fio: socket: %s\n", strerror(errno));
Jens Axboe25095e12012-03-09 17:09:55 +0100298 return ret;
Jens Axboe132159a2011-09-30 15:01:32 -0600299 }
300
Jens Axboe811826b2011-10-24 09:11:50 +0200301 if (connect(fd, addr, socklen) < 0) {
Jens Axboe25095e12012-03-09 17:09:55 +0100302 int ret = -errno;
303
Jens Axboe132159a2011-09-30 15:01:32 -0600304 log_err("fio: connect: %s\n", strerror(errno));
Jens Axboea7de0a12011-10-07 12:55:14 +0200305 log_err("fio: failed to connect to %s:%u\n", client->hostname,
306 client->port);
Jens Axboeb94cba42011-10-06 21:27:10 +0200307 close(fd);
Jens Axboe25095e12012-03-09 17:09:55 +0100308 return ret;
Jens Axboe132159a2011-09-30 15:01:32 -0600309 }
310
Jens Axboe87aa8f12011-10-06 21:24:13 +0200311 return fd;
312}
313
314static int fio_client_connect_sock(struct fio_client *client)
315{
316 struct sockaddr_un *addr = &client->addr_un;
317 fio_socklen_t len;
318 int fd;
319
320 memset(addr, 0, sizeof(*addr));
321 addr->sun_family = AF_UNIX;
322 strcpy(addr->sun_path, client->hostname);
323
324 fd = socket(AF_UNIX, SOCK_STREAM, 0);
325 if (fd < 0) {
Jens Axboe25095e12012-03-09 17:09:55 +0100326 int ret = -errno;
327
Jens Axboe87aa8f12011-10-06 21:24:13 +0200328 log_err("fio: socket: %s\n", strerror(errno));
Jens Axboe25095e12012-03-09 17:09:55 +0100329 return ret;
Jens Axboe87aa8f12011-10-06 21:24:13 +0200330 }
331
332 len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
333 if (connect(fd, (struct sockaddr *) addr, len) < 0) {
Jens Axboe25095e12012-03-09 17:09:55 +0100334 int ret = -errno;
335
Jens Axboe87aa8f12011-10-06 21:24:13 +0200336 log_err("fio: connect; %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200337 close(fd);
Jens Axboe25095e12012-03-09 17:09:55 +0100338 return ret;
Jens Axboe87aa8f12011-10-06 21:24:13 +0200339 }
340
341 return fd;
342}
343
Jens Axboe2f99deb2012-03-09 14:37:29 +0100344int fio_client_connect(struct fio_client *client)
Jens Axboe87aa8f12011-10-06 21:24:13 +0200345{
346 int fd;
347
348 dprint(FD_NET, "client: connect to host %s\n", client->hostname);
349
Jens Axboe87aa8f12011-10-06 21:24:13 +0200350 if (client->is_sock)
351 fd = fio_client_connect_sock(client);
352 else
353 fd = fio_client_connect_ip(client);
354
Jens Axboe89c17072011-10-11 10:15:51 +0200355 dprint(FD_NET, "client: %s connected %d\n", client->hostname, fd);
356
Jens Axboe87aa8f12011-10-06 21:24:13 +0200357 if (fd < 0)
Jens Axboea1a3ed52012-03-09 17:00:01 +0100358 return fd;
Jens Axboe87aa8f12011-10-06 21:24:13 +0200359
Jens Axboeb66570d2011-10-01 11:11:35 -0600360 client->fd = fd;
Jens Axboebebe6392011-10-07 10:00:51 +0200361 fio_client_add_hash(client);
Jens Axboe81179ee2011-10-04 12:42:06 +0200362 client->state = Client_connected;
Jens Axboed824c3b2012-03-09 17:45:43 +0100363
364 probe_client(client);
Jens Axboe132159a2011-09-30 15:01:32 -0600365 return 0;
366}
367
Jens Axboe2f99deb2012-03-09 14:37:29 +0100368void fio_client_terminate(struct fio_client *client)
369{
370 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_QUIT, 0, NULL);
371}
372
Jens Axboecc0df002011-10-03 20:53:32 +0200373void fio_clients_terminate(void)
374{
375 struct flist_head *entry;
376 struct fio_client *client;
377
Jens Axboe60efd142011-10-04 13:30:11 +0200378 dprint(FD_NET, "client: terminate clients\n");
379
Jens Axboecc0df002011-10-03 20:53:32 +0200380 flist_for_each(entry, &client_list) {
381 client = flist_entry(entry, struct fio_client, list);
Jens Axboe2f99deb2012-03-09 14:37:29 +0100382 fio_client_terminate(client);
Jens Axboecc0df002011-10-03 20:53:32 +0200383 }
384}
385
386static void sig_int(int sig)
387{
Jens Axboebebe6392011-10-07 10:00:51 +0200388 dprint(FD_NET, "client: got signal %d\n", sig);
Jens Axboecc0df002011-10-03 20:53:32 +0200389 fio_clients_terminate();
390}
391
392static void client_signal_handler(void)
393{
394 struct sigaction act;
395
396 memset(&act, 0, sizeof(act));
397 act.sa_handler = sig_int;
398 act.sa_flags = SA_RESTART;
399 sigaction(SIGINT, &act, NULL);
400
401 memset(&act, 0, sizeof(act));
402 act.sa_handler = sig_int;
403 act.sa_flags = SA_RESTART;
404 sigaction(SIGTERM, &act, NULL);
405}
406
Jens Axboe81179ee2011-10-04 12:42:06 +0200407static int send_client_cmd_line(struct fio_client *client)
408{
Jens Axboefa2ea802011-10-08 21:07:29 +0200409 struct cmd_single_line_pdu *cslp;
410 struct cmd_line_pdu *clp;
411 unsigned long offset;
Jens Axboe7f868312011-10-10 09:55:21 +0200412 unsigned int *lens;
Jens Axboefa2ea802011-10-08 21:07:29 +0200413 void *pdu;
414 size_t mem;
Jens Axboe81179ee2011-10-04 12:42:06 +0200415 int i, ret;
416
Jens Axboe39e8e012011-10-04 13:46:08 +0200417 dprint(FD_NET, "client: send cmdline %d\n", client->argc);
Jens Axboe60efd142011-10-04 13:30:11 +0200418
Jens Axboe7f868312011-10-10 09:55:21 +0200419 lens = malloc(client->argc * sizeof(unsigned int));
420
Jens Axboefa2ea802011-10-08 21:07:29 +0200421 /*
422 * Find out how much mem we need
423 */
Jens Axboe7f868312011-10-10 09:55:21 +0200424 for (i = 0, mem = 0; i < client->argc; i++) {
425 lens[i] = strlen(client->argv[i]) + 1;
426 mem += lens[i];
427 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200428
Jens Axboefa2ea802011-10-08 21:07:29 +0200429 /*
430 * We need one cmd_line_pdu, and argc number of cmd_single_line_pdu
431 */
432 mem += sizeof(*clp) + (client->argc * sizeof(*cslp));
433
434 pdu = malloc(mem);
435 clp = pdu;
436 offset = sizeof(*clp);
437
438 for (i = 0; i < client->argc; i++) {
Jens Axboe7f868312011-10-10 09:55:21 +0200439 uint16_t arg_len = lens[i];
Jens Axboefa2ea802011-10-08 21:07:29 +0200440
441 cslp = pdu + offset;
442 strcpy((char *) cslp->text, client->argv[i]);
443 cslp->len = cpu_to_le16(arg_len);
444 offset += sizeof(*cslp) + arg_len;
445 }
446
Jens Axboe7f868312011-10-10 09:55:21 +0200447 free(lens);
Jens Axboefa2ea802011-10-08 21:07:29 +0200448 clp->lines = cpu_to_le16(client->argc);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200449 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOBLINE, pdu, mem, 0);
Jens Axboe81179ee2011-10-04 12:42:06 +0200450 free(pdu);
451 return ret;
452}
453
Jens Axboea37f69b2011-10-01 12:24:21 -0600454int fio_clients_connect(void)
455{
456 struct fio_client *client;
457 struct flist_head *entry, *tmp;
458 int ret;
459
Bruce Cran93bcfd22012-02-20 20:18:19 +0100460#ifdef WIN32
461 WSADATA wsd;
462 WSAStartup(MAKEWORD(2,2), &wsd);
463#endif
464
Jens Axboe60efd142011-10-04 13:30:11 +0200465 dprint(FD_NET, "client: connect all\n");
466
Jens Axboecc0df002011-10-03 20:53:32 +0200467 client_signal_handler();
468
Jens Axboea37f69b2011-10-01 12:24:21 -0600469 flist_for_each_safe(entry, tmp, &client_list) {
470 client = flist_entry(entry, struct fio_client, list);
471
472 ret = fio_client_connect(client);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200473 if (ret) {
Jens Axboea37f69b2011-10-01 12:24:21 -0600474 remove_client(client);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200475 continue;
476 }
477
Jens Axboe81179ee2011-10-04 12:42:06 +0200478 if (client->argc > 1)
479 send_client_cmd_line(client);
Jens Axboea37f69b2011-10-01 12:24:21 -0600480 }
481
482 return !nr_clients;
483}
484
Jens Axboeb9d2f302012-03-08 20:36:28 +0100485int fio_start_client(struct fio_client *client)
486{
487 dprint(FD_NET, "client: start %s\n", client->hostname);
488 return fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_RUN, 0, NULL);
489}
490
491int fio_start_all_clients(void)
492{
493 struct fio_client *client;
494 struct flist_head *entry, *tmp;
495 int ret;
496
497 dprint(FD_NET, "client: start all\n");
498
499 flist_for_each_safe(entry, tmp, &client_list) {
500 client = flist_entry(entry, struct fio_client, list);
501
502 ret = fio_start_client(client);
503 if (ret) {
504 remove_client(client);
505 continue;
506 }
507 }
508
509 return flist_empty(&client_list);
510}
511
Jens Axboe132159a2011-09-30 15:01:32 -0600512/*
513 * Send file contents to server backend. We could use sendfile(), but to remain
514 * more portable lets just read/write the darn thing.
515 */
Jens Axboe9988ca72012-03-09 15:14:06 +0100516static int __fio_client_send_ini(struct fio_client *client, const char *filename)
Jens Axboe132159a2011-09-30 15:01:32 -0600517{
518 struct stat sb;
519 char *p, *buf;
520 off_t len;
521 int fd, ret;
522
Jens Axboe46c48f12011-10-01 12:50:51 -0600523 dprint(FD_NET, "send ini %s to %s\n", filename, client->hostname);
524
Jens Axboe132159a2011-09-30 15:01:32 -0600525 fd = open(filename, O_RDONLY);
526 if (fd < 0) {
Jens Axboe25095e12012-03-09 17:09:55 +0100527 int ret = -errno;
528
Jens Axboee951bdc2011-10-05 21:58:45 +0200529 log_err("fio: job file <%s> open: %s\n", filename, strerror(errno));
Jens Axboe25095e12012-03-09 17:09:55 +0100530 return ret;
Jens Axboe132159a2011-09-30 15:01:32 -0600531 }
532
533 if (fstat(fd, &sb) < 0) {
Jens Axboe25095e12012-03-09 17:09:55 +0100534 int ret = -errno;
535
Jens Axboe132159a2011-09-30 15:01:32 -0600536 log_err("fio: job file stat: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200537 close(fd);
Jens Axboe25095e12012-03-09 17:09:55 +0100538 return ret;
Jens Axboe132159a2011-09-30 15:01:32 -0600539 }
540
541 buf = malloc(sb.st_size);
542
543 len = sb.st_size;
544 p = buf;
545 do {
546 ret = read(fd, p, len);
547 if (ret > 0) {
548 len -= ret;
549 if (!len)
550 break;
551 p += ret;
552 continue;
553 } else if (!ret)
554 break;
555 else if (errno == EAGAIN || errno == EINTR)
556 continue;
557 } while (1);
558
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200559 if (len) {
560 log_err("fio: failed reading job file %s\n", filename);
Jens Axboeb94cba42011-10-06 21:27:10 +0200561 close(fd);
Jens Axboec524ef72011-10-07 12:23:34 +0200562 free(buf);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200563 return 1;
564 }
565
Jens Axboec2cb6862012-02-23 20:56:12 +0100566 client->sent_job = 1;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200567 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, buf, sb.st_size, 0);
Jens Axboe132159a2011-09-30 15:01:32 -0600568 free(buf);
Jens Axboeb94cba42011-10-06 21:27:10 +0200569 close(fd);
Jens Axboe132159a2011-09-30 15:01:32 -0600570 return ret;
571}
Jens Axboe37db14f2011-09-30 17:00:42 -0600572
Jens Axboe9988ca72012-03-09 15:14:06 +0100573int fio_client_send_ini(struct fio_client *client, const char *filename)
574{
Jens Axboe25095e12012-03-09 17:09:55 +0100575 int ret;
576
577 ret = __fio_client_send_ini(client, filename);
Jens Axboe3af45202012-03-13 09:59:53 +0100578 if (!ret)
579 client->sent_job = 1;
Jens Axboe9988ca72012-03-09 15:14:06 +0100580
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 Axboe3af45202012-03-13 09:59:53 +0100592 if (fio_client_send_ini(client, filename))
593 remove_client(client);
Jens Axboea37f69b2011-10-01 12:24:21 -0600594 }
595
596 return !nr_clients;
597}
598
Jens Axboea64e88d2011-10-03 14:20:01 +0200599static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
600{
601 dst->max_val = le64_to_cpu(src->max_val);
602 dst->min_val = le64_to_cpu(src->min_val);
603 dst->samples = le64_to_cpu(src->samples);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200604
605 /*
606 * Floats arrive as IEEE 754 encoded uint64_t, convert back to double
607 */
608 dst->mean.u.f = fio_uint64_to_double(le64_to_cpu(dst->mean.u.i));
609 dst->S.u.f = fio_uint64_to_double(le64_to_cpu(dst->S.u.i));
Jens Axboea64e88d2011-10-03 14:20:01 +0200610}
611
612static void convert_ts(struct thread_stat *dst, struct thread_stat *src)
613{
614 int i, j;
615
616 dst->error = le32_to_cpu(src->error);
617 dst->groupid = le32_to_cpu(src->groupid);
618 dst->pid = le32_to_cpu(src->pid);
619 dst->members = le32_to_cpu(src->members);
620
621 for (i = 0; i < 2; i++) {
622 convert_io_stat(&dst->clat_stat[i], &src->clat_stat[i]);
623 convert_io_stat(&dst->slat_stat[i], &src->slat_stat[i]);
624 convert_io_stat(&dst->lat_stat[i], &src->lat_stat[i]);
625 convert_io_stat(&dst->bw_stat[i], &src->bw_stat[i]);
626 }
627
628 dst->usr_time = le64_to_cpu(src->usr_time);
629 dst->sys_time = le64_to_cpu(src->sys_time);
630 dst->ctx = le64_to_cpu(src->ctx);
631 dst->minf = le64_to_cpu(src->minf);
632 dst->majf = le64_to_cpu(src->majf);
633 dst->clat_percentiles = le64_to_cpu(src->clat_percentiles);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200634
635 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
636 fio_fp64_t *fps = &src->percentile_list[i];
637 fio_fp64_t *fpd = &dst->percentile_list[i];
638
639 fpd->u.f = fio_uint64_to_double(le64_to_cpu(fps->u.i));
640 }
Jens Axboea64e88d2011-10-03 14:20:01 +0200641
642 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
643 dst->io_u_map[i] = le32_to_cpu(src->io_u_map[i]);
644 dst->io_u_submit[i] = le32_to_cpu(src->io_u_submit[i]);
645 dst->io_u_complete[i] = le32_to_cpu(src->io_u_complete[i]);
646 }
647
648 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
649 dst->io_u_lat_u[i] = le32_to_cpu(src->io_u_lat_u[i]);
650 dst->io_u_lat_m[i] = le32_to_cpu(src->io_u_lat_m[i]);
651 }
652
653 for (i = 0; i < 2; i++)
654 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
655 dst->io_u_plat[i][j] = le32_to_cpu(src->io_u_plat[i][j]);
656
657 for (i = 0; i < 3; i++) {
658 dst->total_io_u[i] = le64_to_cpu(src->total_io_u[i]);
Jens Axboe93eee042011-10-03 21:08:48 +0200659 dst->short_io_u[i] = le64_to_cpu(src->short_io_u[i]);
Jens Axboea64e88d2011-10-03 14:20:01 +0200660 }
661
662 dst->total_submit = le64_to_cpu(src->total_submit);
663 dst->total_complete = le64_to_cpu(src->total_complete);
664
665 for (i = 0; i < 2; i++) {
666 dst->io_bytes[i] = le64_to_cpu(src->io_bytes[i]);
667 dst->runtime[i] = le64_to_cpu(src->runtime[i]);
668 }
669
670 dst->total_run_time = le64_to_cpu(src->total_run_time);
671 dst->continue_on_error = le16_to_cpu(src->continue_on_error);
672 dst->total_err_count = le64_to_cpu(src->total_err_count);
Jens Axboeddcc0b62011-10-03 14:45:27 +0200673 dst->first_error = le32_to_cpu(src->first_error);
674 dst->kb_base = le32_to_cpu(src->kb_base);
Jens Axboea64e88d2011-10-03 14:20:01 +0200675}
676
677static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
678{
679 int i;
680
681 for (i = 0; i < 2; i++) {
682 dst->max_run[i] = le64_to_cpu(src->max_run[i]);
683 dst->min_run[i] = le64_to_cpu(src->min_run[i]);
684 dst->max_bw[i] = le64_to_cpu(src->max_bw[i]);
685 dst->min_bw[i] = le64_to_cpu(src->min_bw[i]);
686 dst->io_kb[i] = le64_to_cpu(src->io_kb[i]);
687 dst->agg[i] = le64_to_cpu(src->agg[i]);
688 }
689
690 dst->kb_base = le32_to_cpu(src->kb_base);
691 dst->groupid = le32_to_cpu(src->groupid);
692}
693
Jens Axboe89e5fad2012-03-05 09:21:12 +0100694static void handle_ts(struct fio_client *client, struct fio_net_cmd *cmd)
Jens Axboea64e88d2011-10-03 14:20:01 +0200695{
696 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
697
Jens Axboea64e88d2011-10-03 14:20:01 +0200698 show_thread_status(&p->ts, &p->rs);
Jens Axboe37f0c1a2011-10-11 14:08:33 +0200699
700 if (sum_stat_clients == 1)
701 return;
702
703 sum_thread_stats(&client_ts, &p->ts, sum_stat_nr);
704 sum_group_stats(&client_gs, &p->rs);
705
706 client_ts.members++;
707 client_ts.groupid = p->ts.groupid;
708
709 if (++sum_stat_nr == sum_stat_clients) {
710 strcpy(client_ts.name, "All clients");
711 show_thread_status(&client_ts, &client_gs);
712 }
Jens Axboea64e88d2011-10-03 14:20:01 +0200713}
714
Jens Axboe89e5fad2012-03-05 09:21:12 +0100715static void handle_gs(struct fio_client *client, struct fio_net_cmd *cmd)
Jens Axboea64e88d2011-10-03 14:20:01 +0200716{
717 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
718
Jens Axboea64e88d2011-10-03 14:20:01 +0200719 show_group_stats(gs);
720}
721
Jens Axboe3bf236c2012-03-03 20:35:50 +0100722static void handle_text(struct fio_client *client, struct fio_net_cmd *cmd)
723{
724 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
725 const char *buf = (const char *) pdu->buf;
726 const char *name;
727 int fio_unused ret;
728
729 name = client->name ? client->name : client->hostname;
730
731 if (!client->skip_newline)
732 fprintf(f_out, "<%s> ", name);
733 ret = fwrite(buf, pdu->buf_len, 1, f_out);
734 fflush(f_out);
735 client->skip_newline = strchr(buf, '\n') == NULL;
736}
737
Jens Axboed09a64a2011-10-13 11:38:56 +0200738static void convert_agg(struct disk_util_agg *agg)
739{
740 int i;
741
742 for (i = 0; i < 2; i++) {
743 agg->ios[i] = le32_to_cpu(agg->ios[i]);
744 agg->merges[i] = le32_to_cpu(agg->merges[i]);
745 agg->sectors[i] = le64_to_cpu(agg->sectors[i]);
746 agg->ticks[i] = le32_to_cpu(agg->ticks[i]);
747 }
748
749 agg->io_ticks = le32_to_cpu(agg->io_ticks);
750 agg->time_in_queue = le32_to_cpu(agg->time_in_queue);
751 agg->slavecount = le32_to_cpu(agg->slavecount);
Anton Blanchard823ba542011-11-07 14:16:26 +0100752 agg->max_util.u.f = fio_uint64_to_double(__le64_to_cpu(agg->max_util.u.i));
Jens Axboed09a64a2011-10-13 11:38:56 +0200753}
754
755static void convert_dus(struct disk_util_stat *dus)
756{
757 int i;
758
759 for (i = 0; i < 2; i++) {
760 dus->ios[i] = le32_to_cpu(dus->ios[i]);
761 dus->merges[i] = le32_to_cpu(dus->merges[i]);
762 dus->sectors[i] = le64_to_cpu(dus->sectors[i]);
763 dus->ticks[i] = le32_to_cpu(dus->ticks[i]);
764 }
765
766 dus->io_ticks = le32_to_cpu(dus->io_ticks);
767 dus->time_in_queue = le32_to_cpu(dus->time_in_queue);
768 dus->msec = le64_to_cpu(dus->msec);
769}
770
771static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd)
772{
773 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
774
Jens Axboed09a64a2011-10-13 11:38:56 +0200775 if (!client->disk_stats_shown) {
776 client->disk_stats_shown = 1;
777 log_info("\nDisk stats (read/write):\n");
778 }
779
Jens Axboef2f788d2011-10-13 14:03:52 +0200780 print_disk_util(&du->dus, &du->agg, terse_output);
Jens Axboed09a64a2011-10-13 11:38:56 +0200781}
782
Jens Axboe3bf236c2012-03-03 20:35:50 +0100783static void convert_jobs_eta(struct jobs_eta *je)
Jens Axboecf451d12011-10-03 16:48:30 +0200784{
Jens Axboecf451d12011-10-03 16:48:30 +0200785 int i;
786
787 je->nr_running = le32_to_cpu(je->nr_running);
788 je->nr_ramp = le32_to_cpu(je->nr_ramp);
789 je->nr_pending = le32_to_cpu(je->nr_pending);
790 je->files_open = le32_to_cpu(je->files_open);
Jens Axboecf451d12011-10-03 16:48:30 +0200791
792 for (i = 0; i < 2; i++) {
Jens Axboe3e47bd22012-02-29 13:45:02 +0100793 je->m_rate[i] = le32_to_cpu(je->m_rate[i]);
794 je->t_rate[i] = le32_to_cpu(je->t_rate[i]);
795 je->m_iops[i] = le32_to_cpu(je->m_iops[i]);
796 je->t_iops[i] = le32_to_cpu(je->t_iops[i]);
Jens Axboecf451d12011-10-03 16:48:30 +0200797 je->rate[i] = le32_to_cpu(je->rate[i]);
798 je->iops[i] = le32_to_cpu(je->iops[i]);
799 }
800
Jens Axboeb51eedb2011-10-09 12:13:39 +0200801 je->elapsed_sec = le64_to_cpu(je->elapsed_sec);
Jens Axboecf451d12011-10-03 16:48:30 +0200802 je->eta_sec = le64_to_cpu(je->eta_sec);
Jens Axboe8c621fb2012-03-08 12:30:48 +0100803 je->nr_threads = le32_to_cpu(je->nr_threads);
Jens Axboe48fbb462011-10-09 12:19:08 +0200804}
Jens Axboecf451d12011-10-03 16:48:30 +0200805
Jens Axboe3e47bd22012-02-29 13:45:02 +0100806void fio_client_sum_jobs_eta(struct jobs_eta *dst, struct jobs_eta *je)
Jens Axboe48fbb462011-10-09 12:19:08 +0200807{
Jens Axboe48fbb462011-10-09 12:19:08 +0200808 int i;
809
810 dst->nr_running += je->nr_running;
811 dst->nr_ramp += je->nr_ramp;
812 dst->nr_pending += je->nr_pending;
813 dst->files_open += je->files_open;
Jens Axboe48fbb462011-10-09 12:19:08 +0200814
815 for (i = 0; i < 2; i++) {
Jens Axboe3e47bd22012-02-29 13:45:02 +0100816 dst->m_rate[i] += je->m_rate[i];
817 dst->t_rate[i] += je->t_rate[i];
818 dst->m_iops[i] += je->m_iops[i];
819 dst->t_iops[i] += je->t_iops[i];
Jens Axboe48fbb462011-10-09 12:19:08 +0200820 dst->rate[i] += je->rate[i];
821 dst->iops[i] += je->iops[i];
822 }
823
824 dst->elapsed_sec += je->elapsed_sec;
825
826 if (je->eta_sec > dst->eta_sec)
827 dst->eta_sec = je->eta_sec;
Jens Axboe8c621fb2012-03-08 12:30:48 +0100828
829 dst->nr_threads += je->nr_threads;
830 /* we need to handle je->run_str too ... */
Jens Axboe48fbb462011-10-09 12:19:08 +0200831}
832
Jens Axboea5276612012-03-04 15:15:08 +0100833void fio_client_dec_jobs_eta(struct client_eta *eta, client_eta_op eta_fn)
Jens Axboe82c1ed32011-10-10 08:56:18 +0200834{
835 if (!--eta->pending) {
Jens Axboea5276612012-03-04 15:15:08 +0100836 eta_fn(&eta->eta);
Jens Axboe82c1ed32011-10-10 08:56:18 +0200837 free(eta);
838 }
839}
840
Jens Axboe89c17072011-10-11 10:15:51 +0200841static void remove_reply_cmd(struct fio_client *client, struct fio_net_cmd *cmd)
842{
843 struct fio_net_int_cmd *icmd = NULL;
844 struct flist_head *entry;
845
846 flist_for_each(entry, &client->cmd_list) {
847 icmd = flist_entry(entry, struct fio_net_int_cmd, list);
848
Jens Axboedf380932011-10-11 14:25:08 +0200849 if (cmd->tag == (uintptr_t) icmd)
Jens Axboe89c17072011-10-11 10:15:51 +0200850 break;
851
852 icmd = NULL;
853 }
854
855 if (!icmd) {
856 log_err("fio: client: unable to find matching tag\n");
857 return;
858 }
859
860 flist_del(&icmd->list);
861 cmd->tag = icmd->saved_tag;
862 free(icmd);
863}
864
Jens Axboe82c1ed32011-10-10 08:56:18 +0200865static void handle_eta(struct fio_client *client, struct fio_net_cmd *cmd)
Jens Axboe48fbb462011-10-09 12:19:08 +0200866{
867 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
Jens Axboedf380932011-10-11 14:25:08 +0200868 struct client_eta *eta = (struct client_eta *) (uintptr_t) cmd->tag;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200869
870 dprint(FD_NET, "client: got eta tag %p, %d\n", eta, eta->pending);
Jens Axboe48fbb462011-10-09 12:19:08 +0200871
Jens Axboef77d2672011-10-10 14:36:07 +0200872 assert(client->eta_in_flight == eta);
873
874 client->eta_in_flight = NULL;
Jens Axboe82c1ed32011-10-10 08:56:18 +0200875 flist_del_init(&client->eta_list);
876
Jens Axboe2f99deb2012-03-09 14:37:29 +0100877 if (client->ops->jobs_eta)
878 client->ops->jobs_eta(client, je);
879
Jens Axboe3e47bd22012-02-29 13:45:02 +0100880 fio_client_sum_jobs_eta(&eta->eta, je);
Jens Axboea5276612012-03-04 15:15:08 +0100881 fio_client_dec_jobs_eta(eta, client->ops->eta);
Jens Axboecf451d12011-10-03 16:48:30 +0200882}
883
Jens Axboeb5296dd2011-10-07 16:35:56 +0200884static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd)
Jens Axboe2e03b4b2011-10-04 09:00:40 +0200885{
886 struct cmd_probe_pdu *probe = (struct cmd_probe_pdu *) cmd->payload;
Jens Axboed2333352011-10-11 15:07:23 +0200887 const char *os, *arch;
888 char bit[16];
Jens Axboe2e03b4b2011-10-04 09:00:40 +0200889
Jens Axboecca84642011-10-07 12:47:57 +0200890 os = fio_get_os_string(probe->os);
891 if (!os)
892 os = "unknown";
893
894 arch = fio_get_arch_string(probe->arch);
895 if (!arch)
896 os = "unknown";
897
Jens Axboed2333352011-10-11 15:07:23 +0200898 sprintf(bit, "%d-bit", probe->bpp * 8);
Jens Axboe38fdef22011-10-11 14:30:06 +0200899
900 log_info("hostname=%s, be=%u, %s, os=%s, arch=%s, fio=%u.%u.%u\n",
901 probe->hostname, probe->bigendian, bit, os, arch,
902 probe->fio_major, probe->fio_minor, probe->fio_patch);
Jens Axboeb5296dd2011-10-07 16:35:56 +0200903
904 if (!client->name)
905 client->name = strdup((char *) probe->hostname);
Jens Axboe2e03b4b2011-10-04 09:00:40 +0200906}
907
Jens Axboe11e950b2011-10-16 21:34:14 +0200908static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd)
909{
910 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
911
912 client->state = Client_started;
Jens Axboe85dd01e2012-03-12 14:33:16 +0100913 client->jobs = pdu->jobs;
Jens Axboe11e950b2011-10-16 21:34:14 +0200914}
915
916static void handle_stop(struct fio_client *client, struct fio_net_cmd *cmd)
917{
Jens Axboe498c92c2011-10-17 09:14:42 +0200918 if (client->error)
919 log_info("client <%s>: exited with error %d\n", client->hostname, client->error);
Jens Axboe11e950b2011-10-16 21:34:14 +0200920}
921
Jens Axboe6b79c802012-03-08 10:51:36 +0100922static void convert_stop(struct fio_net_cmd *cmd)
923{
924 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
925
926 pdu->error = le32_to_cpu(pdu->error);
927}
928
Jens Axboe084d1c62012-03-03 20:28:07 +0100929static void convert_text(struct fio_net_cmd *cmd)
930{
931 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
932
933 pdu->level = le32_to_cpu(pdu->level);
934 pdu->buf_len = le32_to_cpu(pdu->buf_len);
935 pdu->log_sec = le64_to_cpu(pdu->log_sec);
936 pdu->log_usec = le64_to_cpu(pdu->log_usec);
937}
938
Jens Axboea5276612012-03-04 15:15:08 +0100939int fio_handle_client(struct fio_client *client)
Jens Axboe37db14f2011-09-30 17:00:42 -0600940{
Jens Axboea5276612012-03-04 15:15:08 +0100941 struct client_ops *ops = client->ops;
Jens Axboe37db14f2011-09-30 17:00:42 -0600942 struct fio_net_cmd *cmd;
943
Jens Axboe60efd142011-10-04 13:30:11 +0200944 dprint(FD_NET, "client: handle %s\n", client->hostname);
945
Jens Axboee951bdc2011-10-05 21:58:45 +0200946 cmd = fio_net_recv_cmd(client->fd);
947 if (!cmd)
948 return 0;
Jens Axboec2c94582011-10-05 20:31:30 +0200949
Jens Axboeb9d2f302012-03-08 20:36:28 +0100950 dprint(FD_NET, "client: got cmd op %s from %s (pdu=%u)\n",
951 fio_server_op(cmd->opcode), client->hostname, cmd->pdu_len);
Jens Axboe46c48f12011-10-01 12:50:51 -0600952
Jens Axboee951bdc2011-10-05 21:58:45 +0200953 switch (cmd->opcode) {
954 case FIO_NET_CMD_QUIT:
Jens Axboe3ec62ec2012-03-01 12:01:29 +0100955 if (ops->quit)
956 ops->quit(client);
Jens Axboee951bdc2011-10-05 21:58:45 +0200957 remove_client(client);
958 free(cmd);
959 break;
Jens Axboe084d1c62012-03-03 20:28:07 +0100960 case FIO_NET_CMD_TEXT:
961 convert_text(cmd);
962 ops->text_op(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +0200963 free(cmd);
964 break;
Jens Axboe3bf236c2012-03-03 20:35:50 +0100965 case FIO_NET_CMD_DU: {
966 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
967
968 convert_dus(&du->dus);
969 convert_agg(&du->agg);
970
Stephen M. Camerondd366722012-02-24 08:17:30 +0100971 ops->disk_util(client, cmd);
Jens Axboed09a64a2011-10-13 11:38:56 +0200972 free(cmd);
973 break;
Jens Axboe3bf236c2012-03-03 20:35:50 +0100974 }
975 case FIO_NET_CMD_TS: {
976 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
977
978 convert_ts(&p->ts, &p->ts);
979 convert_gs(&p->rs, &p->rs);
980
Jens Axboe89e5fad2012-03-05 09:21:12 +0100981 ops->thread_status(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +0200982 free(cmd);
983 break;
Jens Axboe3bf236c2012-03-03 20:35:50 +0100984 }
985 case FIO_NET_CMD_GS: {
986 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
987
988 convert_gs(gs, gs);
989
Jens Axboe89e5fad2012-03-05 09:21:12 +0100990 ops->group_stats(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +0200991 free(cmd);
992 break;
Jens Axboe3bf236c2012-03-03 20:35:50 +0100993 }
994 case FIO_NET_CMD_ETA: {
995 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
996
Jens Axboe89c17072011-10-11 10:15:51 +0200997 remove_reply_cmd(client, cmd);
Jens Axboe3bf236c2012-03-03 20:35:50 +0100998 convert_jobs_eta(je);
Jens Axboea5276612012-03-04 15:15:08 +0100999 handle_eta(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +02001000 free(cmd);
1001 break;
Jens Axboe3bf236c2012-03-03 20:35:50 +01001002 }
Jens Axboee951bdc2011-10-05 21:58:45 +02001003 case FIO_NET_CMD_PROBE:
Jens Axboe89c17072011-10-11 10:15:51 +02001004 remove_reply_cmd(client, cmd);
Stephen M. Camerondd366722012-02-24 08:17:30 +01001005 ops->probe(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +02001006 free(cmd);
1007 break;
Jens Axboe5d7793a2012-03-08 19:59:16 +01001008 case FIO_NET_CMD_SERVER_START:
Jens Axboe01be0382011-10-15 14:43:41 +02001009 client->state = Client_running;
Jens Axboe85dd01e2012-03-12 14:33:16 +01001010 if (ops->job_start)
1011 ops->job_start(client, cmd);
Jens Axboe01be0382011-10-15 14:43:41 +02001012 free(cmd);
1013 break;
Jens Axboe85dd01e2012-03-12 14:33:16 +01001014 case FIO_NET_CMD_START: {
1015 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
1016
1017 pdu->jobs = le32_to_cpu(pdu->jobs);
1018 ops->start(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +02001019 free(cmd);
1020 break;
Jens Axboe85dd01e2012-03-12 14:33:16 +01001021 }
Jens Axboe6b79c802012-03-08 10:51:36 +01001022 case FIO_NET_CMD_STOP: {
1023 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
1024
1025 convert_stop(cmd);
1026 client->state = Client_stopped;
1027 client->error = pdu->error;
1028 ops->stop(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +02001029 free(cmd);
1030 break;
Jens Axboe6b79c802012-03-08 10:51:36 +01001031 }
Jens Axboe807f9972012-03-02 10:25:24 +01001032 case FIO_NET_CMD_ADD_JOB:
1033 if (ops->add_job)
1034 ops->add_job(client, cmd);
1035 free(cmd);
1036 break;
Jens Axboee951bdc2011-10-05 21:58:45 +02001037 default:
Jens Axboe89c17072011-10-11 10:15:51 +02001038 log_err("fio: unknown client op: %s\n", fio_server_op(cmd->opcode));
Jens Axboee951bdc2011-10-05 21:58:45 +02001039 free(cmd);
1040 break;
Jens Axboe37db14f2011-09-30 17:00:42 -06001041 }
1042
Jens Axboee951bdc2011-10-05 21:58:45 +02001043 return 1;
Jens Axboe37db14f2011-09-30 17:00:42 -06001044}
Jens Axboeb66570d2011-10-01 11:11:35 -06001045
Jens Axboea5276612012-03-04 15:15:08 +01001046static void request_client_etas(struct client_ops *ops)
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001047{
1048 struct fio_client *client;
1049 struct flist_head *entry;
1050 struct client_eta *eta;
Jens Axboe82c1ed32011-10-10 08:56:18 +02001051 int skipped = 0;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001052
1053 dprint(FD_NET, "client: request eta (%d)\n", nr_clients);
1054
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001055 eta = malloc(sizeof(*eta));
1056 memset(&eta->eta, 0, sizeof(eta->eta));
1057 eta->pending = nr_clients;
1058
1059 flist_for_each(entry, &client_list) {
1060 client = flist_entry(entry, struct fio_client, list);
1061
Jens Axboe82c1ed32011-10-10 08:56:18 +02001062 if (!flist_empty(&client->eta_list)) {
1063 skipped++;
1064 continue;
1065 }
Jens Axboe01be0382011-10-15 14:43:41 +02001066 if (client->state != Client_running)
1067 continue;
Jens Axboe82c1ed32011-10-10 08:56:18 +02001068
Jens Axboef77d2672011-10-10 14:36:07 +02001069 assert(!client->eta_in_flight);
Jens Axboe82c1ed32011-10-10 08:56:18 +02001070 flist_add_tail(&client->eta_list, &eta_list);
Jens Axboef77d2672011-10-10 14:36:07 +02001071 client->eta_in_flight = eta;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001072 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_SEND_ETA,
Jens Axboedf380932011-10-11 14:25:08 +02001073 (uintptr_t) eta, &client->cmd_list);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001074 }
1075
Jens Axboe82c1ed32011-10-10 08:56:18 +02001076 while (skipped--)
Jens Axboea5276612012-03-04 15:15:08 +01001077 fio_client_dec_jobs_eta(eta, ops->eta);
Jens Axboe82c1ed32011-10-10 08:56:18 +02001078
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001079 dprint(FD_NET, "client: requested eta tag %p\n", eta);
1080}
1081
Jens Axboe89c17072011-10-11 10:15:51 +02001082static int client_check_cmd_timeout(struct fio_client *client,
1083 struct timeval *now)
1084{
1085 struct fio_net_int_cmd *cmd;
1086 struct flist_head *entry, *tmp;
1087 int ret = 0;
1088
1089 flist_for_each_safe(entry, tmp, &client->cmd_list) {
1090 cmd = flist_entry(entry, struct fio_net_int_cmd, list);
1091
1092 if (mtime_since(&cmd->tv, now) < FIO_NET_CLIENT_TIMEOUT)
1093 continue;
1094
1095 log_err("fio: client %s, timeout on cmd %s\n", client->hostname,
1096 fio_server_op(cmd->cmd.opcode));
1097 flist_del(&cmd->list);
1098 free(cmd);
1099 ret = 1;
1100 }
1101
1102 return flist_empty(&client->cmd_list) && ret;
1103}
1104
Jens Axboea5276612012-03-04 15:15:08 +01001105static int fio_check_clients_timed_out(void)
Jens Axboe89c17072011-10-11 10:15:51 +02001106{
1107 struct fio_client *client;
1108 struct flist_head *entry, *tmp;
1109 struct timeval tv;
1110 int ret = 0;
1111
1112 gettimeofday(&tv, NULL);
1113
1114 flist_for_each_safe(entry, tmp, &client_list) {
1115 client = flist_entry(entry, struct fio_client, list);
1116
1117 if (flist_empty(&client->cmd_list))
1118 continue;
1119
1120 if (!client_check_cmd_timeout(client, &tv))
1121 continue;
1122
Jens Axboea5276612012-03-04 15:15:08 +01001123 if (client->ops->timed_out)
1124 client->ops->timed_out(client);
Jens Axboeed727a42012-03-02 12:14:40 +01001125 else
1126 log_err("fio: client %s timed out\n", client->hostname);
1127
Jens Axboe89c17072011-10-11 10:15:51 +02001128 remove_client(client);
1129 ret = 1;
1130 }
1131
1132 return ret;
1133}
1134
Stephen M. Camerondd366722012-02-24 08:17:30 +01001135int fio_handle_clients(struct client_ops *ops)
Jens Axboeb66570d2011-10-01 11:11:35 -06001136{
Jens Axboeb66570d2011-10-01 11:11:35 -06001137 struct pollfd *pfds;
Jens Axboe498c92c2011-10-17 09:14:42 +02001138 int i, ret = 0, retval = 0;
Jens Axboeb66570d2011-10-01 11:11:35 -06001139
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001140 gettimeofday(&eta_tv, NULL);
1141
Jens Axboeb66570d2011-10-01 11:11:35 -06001142 pfds = malloc(nr_clients * sizeof(struct pollfd));
1143
Jens Axboe37f0c1a2011-10-11 14:08:33 +02001144 sum_stat_clients = nr_clients;
1145 init_thread_stat(&client_ts);
1146 init_group_run_stat(&client_gs);
1147
Jens Axboeb66570d2011-10-01 11:11:35 -06001148 while (!exit_backend && nr_clients) {
Jens Axboec2cb6862012-02-23 20:56:12 +01001149 struct flist_head *entry, *tmp;
1150 struct fio_client *client;
1151
Jens Axboe82a4be12011-10-01 12:40:32 -06001152 i = 0;
Jens Axboec2cb6862012-02-23 20:56:12 +01001153 flist_for_each_safe(entry, tmp, &client_list) {
Jens Axboe82a4be12011-10-01 12:40:32 -06001154 client = flist_entry(entry, struct fio_client, list);
1155
Jens Axboea5276612012-03-04 15:15:08 +01001156 if (!client->sent_job && !client->ops->stay_connected &&
Jens Axboec2cb6862012-02-23 20:56:12 +01001157 flist_empty(&client->cmd_list)) {
1158 remove_client(client);
1159 continue;
1160 }
1161
Jens Axboe82a4be12011-10-01 12:40:32 -06001162 pfds[i].fd = client->fd;
1163 pfds[i].events = POLLIN;
1164 i++;
1165 }
1166
Jens Axboec2cb6862012-02-23 20:56:12 +01001167 if (!nr_clients)
1168 break;
1169
Jens Axboe82a4be12011-10-01 12:40:32 -06001170 assert(i == nr_clients);
1171
Jens Axboe5c2857f2011-10-04 13:14:32 +02001172 do {
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001173 struct timeval tv;
1174
1175 gettimeofday(&tv, NULL);
Jens Axboe6433ee02012-03-09 20:10:51 +01001176 if (mtime_since(&eta_tv, &tv) >= ops->eta_msec) {
Jens Axboea5276612012-03-04 15:15:08 +01001177 request_client_etas(ops);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001178 memcpy(&eta_tv, &tv, sizeof(tv));
Jens Axboe89c17072011-10-11 10:15:51 +02001179
Jens Axboea5276612012-03-04 15:15:08 +01001180 if (fio_check_clients_timed_out())
Jens Axboe89c17072011-10-11 10:15:51 +02001181 break;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001182 }
1183
Jens Axboe5c2857f2011-10-04 13:14:32 +02001184 ret = poll(pfds, nr_clients, 100);
1185 if (ret < 0) {
1186 if (errno == EINTR)
1187 continue;
1188 log_err("fio: poll clients: %s\n", strerror(errno));
1189 break;
1190 } else if (!ret)
Jens Axboeb66570d2011-10-01 11:11:35 -06001191 continue;
Jens Axboe5c2857f2011-10-04 13:14:32 +02001192 } while (ret <= 0);
Jens Axboeb66570d2011-10-01 11:11:35 -06001193
1194 for (i = 0; i < nr_clients; i++) {
1195 if (!(pfds[i].revents & POLLIN))
1196 continue;
1197
1198 client = find_client_by_fd(pfds[i].fd);
1199 if (!client) {
Jens Axboe3c5f57e2011-10-06 12:37:50 +02001200 log_err("fio: unknown client fd %d\n", pfds[i].fd);
Jens Axboeb66570d2011-10-01 11:11:35 -06001201 continue;
1202 }
Jens Axboea5276612012-03-04 15:15:08 +01001203 if (!fio_handle_client(client)) {
Jens Axboe28d3ab02011-10-05 20:45:37 +02001204 log_info("client: host=%s disconnected\n",
1205 client->hostname);
1206 remove_client(client);
Jens Axboe498c92c2011-10-17 09:14:42 +02001207 retval = 1;
Jens Axboe38990762011-10-17 13:31:33 +02001208 } else if (client->error)
Jens Axboe498c92c2011-10-17 09:14:42 +02001209 retval = 1;
Jens Axboe343cb4a2012-03-09 17:16:51 +01001210 fio_put_client(client);
Jens Axboeb66570d2011-10-01 11:11:35 -06001211 }
1212 }
1213
1214 free(pfds);
Jens Axboe498c92c2011-10-17 09:14:42 +02001215 return retval;
Jens Axboeb66570d2011-10-01 11:11:35 -06001216}