blob: 155eb72070038347e2857c26f71437aaa7f741b1 [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 Axboe3989b142013-04-12 13:13:24 +020017#ifdef CONFIG_ZLIB
Jens Axboe1b427252012-03-14 15:03:03 +010018#include <zlib.h>
Jens Axboe3989b142013-04-12 13:13:24 +020019#endif
Jens Axboe132159a2011-09-30 15:01:32 -060020
21#include "fio.h"
Stephen M. Camerondd366722012-02-24 08:17:30 +010022#include "client.h"
Jens Axboe132159a2011-09-30 15:01:32 -060023#include "server.h"
Jens Axboeb66570d2011-10-01 11:11:35 -060024#include "flist.h"
Jens Axboe3c5f57e2011-10-06 12:37:50 +020025#include "hash.h"
Jens Axboe132159a2011-09-30 15:01:32 -060026
Stephen M. Camerondd366722012-02-24 08:17:30 +010027static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd);
Jens Axboe89e5fad2012-03-05 09:21:12 +010028static void handle_ts(struct fio_client *client, struct fio_net_cmd *cmd);
29static void handle_gs(struct fio_client *client, struct fio_net_cmd *cmd);
Stephen M. Camerondd366722012-02-24 08:17:30 +010030static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd);
Jens Axboe084d1c62012-03-03 20:28:07 +010031static void handle_text(struct fio_client *client, struct fio_net_cmd *cmd);
Jens Axboe6b79c802012-03-08 10:51:36 +010032static void handle_stop(struct fio_client *client, struct fio_net_cmd *cmd);
Jens Axboe85dd01e2012-03-12 14:33:16 +010033static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd);
Stephen M. Camerondd366722012-02-24 08:17:30 +010034
35struct client_ops fio_client_ops = {
Jens Axboe35c0ba72012-03-14 10:56:40 +010036 .text = handle_text,
Jens Axboe0420ba62012-02-29 11:16:52 +010037 .disk_util = handle_du,
38 .thread_status = handle_ts,
39 .group_stats = handle_gs,
Jens Axboe6b79c802012-03-08 10:51:36 +010040 .stop = handle_stop,
Jens Axboe85dd01e2012-03-12 14:33:16 +010041 .start = handle_start,
Jens Axboea5276612012-03-04 15:15:08 +010042 .eta = display_thread_status,
Jens Axboe0420ba62012-02-29 11:16:52 +010043 .probe = handle_probe,
Jens Axboe6433ee02012-03-09 20:10:51 +010044 .eta_msec = FIO_CLIENT_DEF_ETA_MSEC,
Jens Axboe46bcd492012-03-14 11:31:21 +010045 .client_type = FIO_CLIENT_TYPE_CLI,
Stephen M. Camerondd366722012-02-24 08:17:30 +010046};
47
Jens Axboeaf9c9fb2011-10-09 21:54:10 +020048static struct timeval eta_tv;
Jens Axboe48fbb462011-10-09 12:19:08 +020049
Jens Axboeb66570d2011-10-01 11:11:35 -060050static FLIST_HEAD(client_list);
Jens Axboe82c1ed32011-10-10 08:56:18 +020051static FLIST_HEAD(eta_list);
Jens Axboeb66570d2011-10-01 11:11:35 -060052
Jens Axboe3f3a4542011-10-10 21:11:09 +020053static FLIST_HEAD(arg_list);
54
Jens Axboe3650a3c2012-03-05 14:09:03 +010055struct thread_stat client_ts;
56struct group_run_stats client_gs;
57int sum_stat_clients;
58
Jens Axboe37f0c1a2011-10-11 14:08:33 +020059static int sum_stat_nr;
Jens Axboe108fea72012-11-14 13:09:45 -070060static int do_output_all_clients;
Jens Axboe37f0c1a2011-10-11 14:08:33 +020061
Jens Axboe3c5f57e2011-10-06 12:37:50 +020062#define FIO_CLIENT_HASH_BITS 7
63#define FIO_CLIENT_HASH_SZ (1 << FIO_CLIENT_HASH_BITS)
64#define FIO_CLIENT_HASH_MASK (FIO_CLIENT_HASH_SZ - 1)
Jens Axboebebe6392011-10-07 10:00:51 +020065static struct flist_head client_hash[FIO_CLIENT_HASH_SZ];
Jens Axboe3c5f57e2011-10-06 12:37:50 +020066
Jens Axboebebe6392011-10-07 10:00:51 +020067static void fio_client_add_hash(struct fio_client *client)
Jens Axboe3c5f57e2011-10-06 12:37:50 +020068{
69 int bucket = hash_long(client->fd, FIO_CLIENT_HASH_BITS);
70
71 bucket &= FIO_CLIENT_HASH_MASK;
Jens Axboebebe6392011-10-07 10:00:51 +020072 flist_add(&client->hash_list, &client_hash[bucket]);
Jens Axboe3c5f57e2011-10-06 12:37:50 +020073}
74
Jens Axboebebe6392011-10-07 10:00:51 +020075static void fio_client_remove_hash(struct fio_client *client)
Jens Axboe3c5f57e2011-10-06 12:37:50 +020076{
Jens Axboebebe6392011-10-07 10:00:51 +020077 if (!flist_empty(&client->hash_list))
78 flist_del_init(&client->hash_list);
Jens Axboe3c5f57e2011-10-06 12:37:50 +020079}
80
81static void fio_init fio_client_hash_init(void)
82{
83 int i;
84
Jens Axboebebe6392011-10-07 10:00:51 +020085 for (i = 0; i < FIO_CLIENT_HASH_SZ; i++)
86 INIT_FLIST_HEAD(&client_hash[i]);
Jens Axboe3c5f57e2011-10-06 12:37:50 +020087}
88
Jens Axboeb66570d2011-10-01 11:11:35 -060089static struct fio_client *find_client_by_fd(int fd)
90{
Jens Axboe3c5f57e2011-10-06 12:37:50 +020091 int bucket = hash_long(fd, FIO_CLIENT_HASH_BITS) & FIO_CLIENT_HASH_MASK;
Jens Axboeb66570d2011-10-01 11:11:35 -060092 struct fio_client *client;
93 struct flist_head *entry;
94
Jens Axboebebe6392011-10-07 10:00:51 +020095 flist_for_each(entry, &client_hash[bucket]) {
96 client = flist_entry(entry, struct fio_client, hash_list);
Jens Axboeb66570d2011-10-01 11:11:35 -060097
Jens Axboee55f8f32012-03-06 15:42:31 +010098 if (client->fd == fd) {
99 client->refs++;
Jens Axboeb66570d2011-10-01 11:11:35 -0600100 return client;
Jens Axboee55f8f32012-03-06 15:42:31 +0100101 }
Jens Axboeb66570d2011-10-01 11:11:35 -0600102 }
103
104 return NULL;
105}
106
Jens Axboe3af45202012-03-13 09:59:53 +0100107void fio_put_client(struct fio_client *client)
Jens Axboeb66570d2011-10-01 11:11:35 -0600108{
Jens Axboe5121a9a2012-03-05 13:32:47 +0100109 if (--client->refs)
110 return;
111
Jens Axboeb66570d2011-10-01 11:11:35 -0600112 free(client->hostname);
Jens Axboe81179ee2011-10-04 12:42:06 +0200113 if (client->argv)
114 free(client->argv);
Jens Axboeb5296dd2011-10-07 16:35:56 +0200115 if (client->name)
116 free(client->name);
Jens Axboe14ea90e2012-08-26 17:33:34 +0200117 while (client->nr_ini_file)
118 free(client->ini_file[--client->nr_ini_file]);
119 if (client->ini_file)
120 free(client->ini_file);
Jens Axboe81179ee2011-10-04 12:42:06 +0200121
Jens Axboe108fea72012-11-14 13:09:45 -0700122 if (!client->did_stat)
123 sum_stat_clients -= client->nr_stat;
124
Jens Axboeb66570d2011-10-01 11:11:35 -0600125 free(client);
126}
Jens Axboe132159a2011-09-30 15:01:32 -0600127
Jens Axboe3af45202012-03-13 09:59:53 +0100128static void remove_client(struct fio_client *client)
Jens Axboe5121a9a2012-03-05 13:32:47 +0100129{
Jens Axboe3af45202012-03-13 09:59:53 +0100130 assert(client->refs);
131
132 dprint(FD_NET, "client: removed <%s>\n", client->hostname);
133
134 if (!flist_empty(&client->list))
135 flist_del_init(&client->list);
136
137 fio_client_remove_hash(client);
138
139 if (!flist_empty(&client->eta_list)) {
140 flist_del_init(&client->eta_list);
141 fio_client_dec_jobs_eta(client->eta_in_flight, client->ops->eta);
142 }
143
Jens Axboe1e0c1842012-03-20 15:15:00 +0100144 close(client->fd);
145 client->fd = -1;
146
Jens Axboe0cf3ece2012-03-21 10:15:20 +0100147 if (client->ops->removed)
148 client->ops->removed(client);
149
Jens Axboe3af45202012-03-13 09:59:53 +0100150 nr_clients--;
Jens Axboe3af45202012-03-13 09:59:53 +0100151 fio_put_client(client);
Jens Axboe5121a9a2012-03-05 13:32:47 +0100152}
153
Jens Axboe343cb4a2012-03-09 17:16:51 +0100154struct fio_client *fio_get_client(struct fio_client *client)
155{
156 client->refs++;
157 return client;
158}
159
Jens Axboefa2ea802011-10-08 21:07:29 +0200160static void __fio_client_add_cmd_option(struct fio_client *client,
161 const char *opt)
Jens Axboe81179ee2011-10-04 12:42:06 +0200162{
Jens Axboe39e8e012011-10-04 13:46:08 +0200163 int index;
164
165 index = client->argc++;
Jens Axboe81179ee2011-10-04 12:42:06 +0200166 client->argv = realloc(client->argv, sizeof(char *) * client->argc);
Jens Axboe39e8e012011-10-04 13:46:08 +0200167 client->argv[index] = strdup(opt);
168 dprint(FD_NET, "client: add cmd %d: %s\n", index, opt);
Jens Axboe81179ee2011-10-04 12:42:06 +0200169}
170
Jens Axboefa2ea802011-10-08 21:07:29 +0200171void fio_client_add_cmd_option(void *cookie, const char *opt)
Jens Axboe81179ee2011-10-04 12:42:06 +0200172{
Jens Axboebebe6392011-10-07 10:00:51 +0200173 struct fio_client *client = cookie;
Jens Axboe3f3a4542011-10-10 21:11:09 +0200174 struct flist_head *entry;
Jens Axboe81179ee2011-10-04 12:42:06 +0200175
Jens Axboebebe6392011-10-07 10:00:51 +0200176 if (!client || !opt)
Jens Axboefa2ea802011-10-08 21:07:29 +0200177 return;
Jens Axboe81179ee2011-10-04 12:42:06 +0200178
Jens Axboefa2ea802011-10-08 21:07:29 +0200179 __fio_client_add_cmd_option(client, opt);
Jens Axboe3f3a4542011-10-10 21:11:09 +0200180
181 /*
182 * Duplicate arguments to shared client group
183 */
184 flist_for_each(entry, &arg_list) {
185 client = flist_entry(entry, struct fio_client, arg_list);
186
187 __fio_client_add_cmd_option(client, opt);
188 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200189}
190
Jens Axboea5276612012-03-04 15:15:08 +0100191struct fio_client *fio_client_add_explicit(struct client_ops *ops,
192 const char *hostname, int type,
Jens Axboe3ec62ec2012-03-01 12:01:29 +0100193 int port)
194{
195 struct fio_client *client;
196
197 client = malloc(sizeof(*client));
198 memset(client, 0, sizeof(*client));
199
200 INIT_FLIST_HEAD(&client->list);
201 INIT_FLIST_HEAD(&client->hash_list);
202 INIT_FLIST_HEAD(&client->arg_list);
203 INIT_FLIST_HEAD(&client->eta_list);
204 INIT_FLIST_HEAD(&client->cmd_list);
205
206 client->hostname = strdup(hostname);
207
208 if (type == Fio_client_socket)
209 client->is_sock = 1;
210 else {
211 int ipv6;
212
213 ipv6 = type == Fio_client_ipv6;
214 if (fio_server_parse_host(hostname, &ipv6,
215 &client->addr.sin_addr,
216 &client->addr6.sin6_addr))
217 goto err;
218
219 client->port = port;
220 }
221
222 client->fd = -1;
Jens Axboea5276612012-03-04 15:15:08 +0100223 client->ops = ops;
Jens Axboe5121a9a2012-03-05 13:32:47 +0100224 client->refs = 1;
Jens Axboe46bcd492012-03-14 11:31:21 +0100225 client->type = ops->client_type;
Jens Axboe3ec62ec2012-03-01 12:01:29 +0100226
227 __fio_client_add_cmd_option(client, "fio");
228
229 flist_add(&client->list, &client_list);
230 nr_clients++;
231 dprint(FD_NET, "client: added <%s>\n", client->hostname);
232 return client;
233err:
234 free(client);
235 return NULL;
236}
237
Jens Axboe14ea90e2012-08-26 17:33:34 +0200238void fio_client_add_ini_file(void *cookie, const char *ini_file)
239{
240 struct fio_client *client = cookie;
241 size_t new_size;
242
243 dprint(FD_NET, "client <%s>: add ini %s\n", client->hostname, ini_file);
244
245 new_size = (client->nr_ini_file + 1) * sizeof(char *);
246 client->ini_file = realloc(client->ini_file, new_size);
247 client->ini_file[client->nr_ini_file] = strdup(ini_file);
248 client->nr_ini_file++;
249}
250
Jens Axboea5276612012-03-04 15:15:08 +0100251int fio_client_add(struct client_ops *ops, const char *hostname, void **cookie)
Jens Axboe132159a2011-09-30 15:01:32 -0600252{
Jens Axboe3f3a4542011-10-10 21:11:09 +0200253 struct fio_client *existing = *cookie;
Jens Axboeb66570d2011-10-01 11:11:35 -0600254 struct fio_client *client;
Jens Axboe132159a2011-09-30 15:01:32 -0600255
Jens Axboe3f3a4542011-10-10 21:11:09 +0200256 if (existing) {
257 /*
258 * We always add our "exec" name as the option, hence 1
259 * means empty.
260 */
261 if (existing->argc == 1)
262 flist_add_tail(&existing->arg_list, &arg_list);
263 else {
264 while (!flist_empty(&arg_list))
265 flist_del_init(arg_list.next);
266 }
267 }
268
Jens Axboeb66570d2011-10-01 11:11:35 -0600269 client = malloc(sizeof(*client));
Jens Axboea37f69b2011-10-01 12:24:21 -0600270 memset(client, 0, sizeof(*client));
Jens Axboe81179ee2011-10-04 12:42:06 +0200271
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200272 INIT_FLIST_HEAD(&client->list);
Jens Axboebebe6392011-10-07 10:00:51 +0200273 INIT_FLIST_HEAD(&client->hash_list);
Jens Axboe3f3a4542011-10-10 21:11:09 +0200274 INIT_FLIST_HEAD(&client->arg_list);
Jens Axboe82c1ed32011-10-10 08:56:18 +0200275 INIT_FLIST_HEAD(&client->eta_list);
Jens Axboe89c17072011-10-11 10:15:51 +0200276 INIT_FLIST_HEAD(&client->cmd_list);
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200277
Jens Axboebebe6392011-10-07 10:00:51 +0200278 if (fio_server_parse_string(hostname, &client->hostname,
279 &client->is_sock, &client->port,
Jens Axboe811826b2011-10-24 09:11:50 +0200280 &client->addr.sin_addr,
281 &client->addr6.sin6_addr,
282 &client->ipv6))
Jens Axboebebe6392011-10-07 10:00:51 +0200283 return -1;
284
Jens Axboea37f69b2011-10-01 12:24:21 -0600285 client->fd = -1;
Jens Axboea5276612012-03-04 15:15:08 +0100286 client->ops = ops;
Jens Axboe5121a9a2012-03-05 13:32:47 +0100287 client->refs = 1;
Jens Axboe46bcd492012-03-14 11:31:21 +0100288 client->type = ops->client_type;
Jens Axboe81179ee2011-10-04 12:42:06 +0200289
290 __fio_client_add_cmd_option(client, "fio");
291
Jens Axboea37f69b2011-10-01 12:24:21 -0600292 flist_add(&client->list, &client_list);
293 nr_clients++;
Jens Axboebebe6392011-10-07 10:00:51 +0200294 dprint(FD_NET, "client: added <%s>\n", client->hostname);
295 *cookie = client;
296 return 0;
Jens Axboea37f69b2011-10-01 12:24:21 -0600297}
298
Jens Axboed824c3b2012-03-09 17:45:43 +0100299static void probe_client(struct fio_client *client)
300{
Jens Axboe3989b142013-04-12 13:13:24 +0200301 struct cmd_client_probe_pdu pdu;
302 uint64_t tag;
303
Jens Axboed824c3b2012-03-09 17:45:43 +0100304 dprint(FD_NET, "client: send probe\n");
305
Jens Axboe3989b142013-04-12 13:13:24 +0200306#ifdef CONFIG_ZLIB
307 pdu.flags = __le64_to_cpu(FIO_PROBE_FLAG_ZLIB);
308#else
309 pdu.flags = 0;
310#endif
311
312 fio_net_send_cmd(client->fd, FIO_NET_CMD_PROBE, &pdu, sizeof(pdu), &tag, &client->cmd_list);
Jens Axboed824c3b2012-03-09 17:45:43 +0100313}
314
Jens Axboe87aa8f12011-10-06 21:24:13 +0200315static int fio_client_connect_ip(struct fio_client *client)
Jens Axboea37f69b2011-10-01 12:24:21 -0600316{
Jens Axboe811826b2011-10-24 09:11:50 +0200317 struct sockaddr *addr;
Jens Axboe67bf9822013-01-10 11:23:19 +0100318 socklen_t socklen;
Jens Axboe811826b2011-10-24 09:11:50 +0200319 int fd, domain;
Jens Axboe132159a2011-09-30 15:01:32 -0600320
Jens Axboe811826b2011-10-24 09:11:50 +0200321 if (client->ipv6) {
322 client->addr6.sin6_family = AF_INET6;
323 client->addr6.sin6_port = htons(client->port);
324 domain = AF_INET6;
325 addr = (struct sockaddr *) &client->addr6;
326 socklen = sizeof(client->addr6);
327 } else {
328 client->addr.sin_family = AF_INET;
329 client->addr.sin_port = htons(client->port);
330 domain = AF_INET;
331 addr = (struct sockaddr *) &client->addr;
332 socklen = sizeof(client->addr);
333 }
Jens Axboe132159a2011-09-30 15:01:32 -0600334
Jens Axboe811826b2011-10-24 09:11:50 +0200335 fd = socket(domain, SOCK_STREAM, 0);
Jens Axboe132159a2011-09-30 15:01:32 -0600336 if (fd < 0) {
Jens Axboe25095e12012-03-09 17:09:55 +0100337 int ret = -errno;
338
Jens Axboe132159a2011-09-30 15:01:32 -0600339 log_err("fio: socket: %s\n", strerror(errno));
Jens Axboe25095e12012-03-09 17:09:55 +0100340 return ret;
Jens Axboe132159a2011-09-30 15:01:32 -0600341 }
342
Jens Axboe811826b2011-10-24 09:11:50 +0200343 if (connect(fd, addr, socklen) < 0) {
Jens Axboe25095e12012-03-09 17:09:55 +0100344 int ret = -errno;
345
Jens Axboe132159a2011-09-30 15:01:32 -0600346 log_err("fio: connect: %s\n", strerror(errno));
Jens Axboea7de0a12011-10-07 12:55:14 +0200347 log_err("fio: failed to connect to %s:%u\n", client->hostname,
348 client->port);
Jens Axboeb94cba42011-10-06 21:27:10 +0200349 close(fd);
Jens Axboe25095e12012-03-09 17:09:55 +0100350 return ret;
Jens Axboe132159a2011-09-30 15:01:32 -0600351 }
352
Jens Axboe87aa8f12011-10-06 21:24:13 +0200353 return fd;
354}
355
356static int fio_client_connect_sock(struct fio_client *client)
357{
358 struct sockaddr_un *addr = &client->addr_un;
Jens Axboe67bf9822013-01-10 11:23:19 +0100359 socklen_t len;
Jens Axboe87aa8f12011-10-06 21:24:13 +0200360 int fd;
361
362 memset(addr, 0, sizeof(*addr));
363 addr->sun_family = AF_UNIX;
364 strcpy(addr->sun_path, client->hostname);
365
366 fd = socket(AF_UNIX, SOCK_STREAM, 0);
367 if (fd < 0) {
Jens Axboe25095e12012-03-09 17:09:55 +0100368 int ret = -errno;
369
Jens Axboe87aa8f12011-10-06 21:24:13 +0200370 log_err("fio: socket: %s\n", strerror(errno));
Jens Axboe25095e12012-03-09 17:09:55 +0100371 return ret;
Jens Axboe87aa8f12011-10-06 21:24:13 +0200372 }
373
374 len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
375 if (connect(fd, (struct sockaddr *) addr, len) < 0) {
Jens Axboe25095e12012-03-09 17:09:55 +0100376 int ret = -errno;
377
Jens Axboe87aa8f12011-10-06 21:24:13 +0200378 log_err("fio: connect; %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200379 close(fd);
Jens Axboe25095e12012-03-09 17:09:55 +0100380 return ret;
Jens Axboe87aa8f12011-10-06 21:24:13 +0200381 }
382
383 return fd;
384}
385
Jens Axboe2f99deb2012-03-09 14:37:29 +0100386int fio_client_connect(struct fio_client *client)
Jens Axboe87aa8f12011-10-06 21:24:13 +0200387{
388 int fd;
389
390 dprint(FD_NET, "client: connect to host %s\n", client->hostname);
391
Jens Axboe87aa8f12011-10-06 21:24:13 +0200392 if (client->is_sock)
393 fd = fio_client_connect_sock(client);
394 else
395 fd = fio_client_connect_ip(client);
396
Jens Axboe89c17072011-10-11 10:15:51 +0200397 dprint(FD_NET, "client: %s connected %d\n", client->hostname, fd);
398
Jens Axboe87aa8f12011-10-06 21:24:13 +0200399 if (fd < 0)
Jens Axboea1a3ed52012-03-09 17:00:01 +0100400 return fd;
Jens Axboe87aa8f12011-10-06 21:24:13 +0200401
Jens Axboeb66570d2011-10-01 11:11:35 -0600402 client->fd = fd;
Jens Axboebebe6392011-10-07 10:00:51 +0200403 fio_client_add_hash(client);
Jens Axboe81179ee2011-10-04 12:42:06 +0200404 client->state = Client_connected;
Jens Axboed824c3b2012-03-09 17:45:43 +0100405
406 probe_client(client);
Jens Axboe132159a2011-09-30 15:01:32 -0600407 return 0;
408}
409
Jens Axboe0cf3ece2012-03-21 10:15:20 +0100410int fio_client_terminate(struct fio_client *client)
Jens Axboe2f99deb2012-03-09 14:37:29 +0100411{
Jens Axboe0cf3ece2012-03-21 10:15:20 +0100412 return fio_net_send_quit(client->fd);
Jens Axboe2f99deb2012-03-09 14:37:29 +0100413}
414
Jens Axboecc0df002011-10-03 20:53:32 +0200415void fio_clients_terminate(void)
416{
417 struct flist_head *entry;
418 struct fio_client *client;
419
Jens Axboe60efd142011-10-04 13:30:11 +0200420 dprint(FD_NET, "client: terminate clients\n");
421
Jens Axboecc0df002011-10-03 20:53:32 +0200422 flist_for_each(entry, &client_list) {
423 client = flist_entry(entry, struct fio_client, list);
Jens Axboe2f99deb2012-03-09 14:37:29 +0100424 fio_client_terminate(client);
Jens Axboecc0df002011-10-03 20:53:32 +0200425 }
426}
427
428static void sig_int(int sig)
429{
Jens Axboebebe6392011-10-07 10:00:51 +0200430 dprint(FD_NET, "client: got signal %d\n", sig);
Jens Axboecc0df002011-10-03 20:53:32 +0200431 fio_clients_terminate();
432}
433
Jens Axboeb852e7c2012-03-30 10:30:35 +0200434static void sig_show_status(int sig)
435{
436 show_running_run_stats();
437}
438
Jens Axboecc0df002011-10-03 20:53:32 +0200439static void client_signal_handler(void)
440{
441 struct sigaction act;
442
443 memset(&act, 0, sizeof(act));
444 act.sa_handler = sig_int;
445 act.sa_flags = SA_RESTART;
446 sigaction(SIGINT, &act, NULL);
447
448 memset(&act, 0, sizeof(act));
449 act.sa_handler = sig_int;
450 act.sa_flags = SA_RESTART;
451 sigaction(SIGTERM, &act, NULL);
Jens Axboeb852e7c2012-03-30 10:30:35 +0200452
Bruce Cran2f694502012-10-10 16:34:13 +0100453/* Windows uses SIGBREAK as a quit signal from other applications */
454#ifdef WIN32
455 memset(&act, 0, sizeof(act));
456 act.sa_handler = sig_int;
457 act.sa_flags = SA_RESTART;
458 sigaction(SIGBREAK, &act, NULL);
459#endif
460
Jens Axboeb852e7c2012-03-30 10:30:35 +0200461 memset(&act, 0, sizeof(act));
462 act.sa_handler = sig_show_status;
463 act.sa_flags = SA_RESTART;
464 sigaction(SIGUSR1, &act, NULL);
Jens Axboecc0df002011-10-03 20:53:32 +0200465}
466
Jens Axboe81179ee2011-10-04 12:42:06 +0200467static int send_client_cmd_line(struct fio_client *client)
468{
Jens Axboefa2ea802011-10-08 21:07:29 +0200469 struct cmd_single_line_pdu *cslp;
470 struct cmd_line_pdu *clp;
471 unsigned long offset;
Jens Axboe7f868312011-10-10 09:55:21 +0200472 unsigned int *lens;
Jens Axboefa2ea802011-10-08 21:07:29 +0200473 void *pdu;
474 size_t mem;
Jens Axboe81179ee2011-10-04 12:42:06 +0200475 int i, ret;
476
Jens Axboe39e8e012011-10-04 13:46:08 +0200477 dprint(FD_NET, "client: send cmdline %d\n", client->argc);
Jens Axboe60efd142011-10-04 13:30:11 +0200478
Jens Axboe7f868312011-10-10 09:55:21 +0200479 lens = malloc(client->argc * sizeof(unsigned int));
480
Jens Axboefa2ea802011-10-08 21:07:29 +0200481 /*
482 * Find out how much mem we need
483 */
Jens Axboe7f868312011-10-10 09:55:21 +0200484 for (i = 0, mem = 0; i < client->argc; i++) {
485 lens[i] = strlen(client->argv[i]) + 1;
486 mem += lens[i];
487 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200488
Jens Axboefa2ea802011-10-08 21:07:29 +0200489 /*
490 * We need one cmd_line_pdu, and argc number of cmd_single_line_pdu
491 */
492 mem += sizeof(*clp) + (client->argc * sizeof(*cslp));
493
494 pdu = malloc(mem);
495 clp = pdu;
496 offset = sizeof(*clp);
497
498 for (i = 0; i < client->argc; i++) {
Jens Axboe7f868312011-10-10 09:55:21 +0200499 uint16_t arg_len = lens[i];
Jens Axboefa2ea802011-10-08 21:07:29 +0200500
501 cslp = pdu + offset;
502 strcpy((char *) cslp->text, client->argv[i]);
503 cslp->len = cpu_to_le16(arg_len);
504 offset += sizeof(*cslp) + arg_len;
505 }
506
Jens Axboe7f868312011-10-10 09:55:21 +0200507 free(lens);
Jens Axboefa2ea802011-10-08 21:07:29 +0200508 clp->lines = cpu_to_le16(client->argc);
Jens Axboe46bcd492012-03-14 11:31:21 +0100509 clp->client_type = __cpu_to_le16(client->type);
Jens Axboe40c60512012-03-27 16:03:04 +0200510 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOBLINE, pdu, mem, NULL, NULL);
Jens Axboe81179ee2011-10-04 12:42:06 +0200511 free(pdu);
512 return ret;
513}
514
Jens Axboea37f69b2011-10-01 12:24:21 -0600515int fio_clients_connect(void)
516{
517 struct fio_client *client;
518 struct flist_head *entry, *tmp;
519 int ret;
520
Bruce Cran93bcfd22012-02-20 20:18:19 +0100521#ifdef WIN32
522 WSADATA wsd;
Jens Axboe3c3ed072012-03-27 09:12:39 +0200523 WSAStartup(MAKEWORD(2, 2), &wsd);
Bruce Cran93bcfd22012-02-20 20:18:19 +0100524#endif
525
Jens Axboe60efd142011-10-04 13:30:11 +0200526 dprint(FD_NET, "client: connect all\n");
527
Jens Axboecc0df002011-10-03 20:53:32 +0200528 client_signal_handler();
529
Jens Axboea37f69b2011-10-01 12:24:21 -0600530 flist_for_each_safe(entry, tmp, &client_list) {
531 client = flist_entry(entry, struct fio_client, list);
532
533 ret = fio_client_connect(client);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200534 if (ret) {
Jens Axboea37f69b2011-10-01 12:24:21 -0600535 remove_client(client);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200536 continue;
537 }
538
Jens Axboe81179ee2011-10-04 12:42:06 +0200539 if (client->argc > 1)
540 send_client_cmd_line(client);
Jens Axboea37f69b2011-10-01 12:24:21 -0600541 }
542
543 return !nr_clients;
544}
545
Jens Axboeb9d2f302012-03-08 20:36:28 +0100546int fio_start_client(struct fio_client *client)
547{
548 dprint(FD_NET, "client: start %s\n", client->hostname);
549 return fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_RUN, 0, NULL);
550}
551
552int fio_start_all_clients(void)
553{
554 struct fio_client *client;
555 struct flist_head *entry, *tmp;
556 int ret;
557
558 dprint(FD_NET, "client: start all\n");
559
560 flist_for_each_safe(entry, tmp, &client_list) {
561 client = flist_entry(entry, struct fio_client, list);
562
563 ret = fio_start_client(client);
564 if (ret) {
565 remove_client(client);
566 continue;
567 }
568 }
569
570 return flist_empty(&client_list);
571}
572
Jens Axboe132159a2011-09-30 15:01:32 -0600573/*
574 * Send file contents to server backend. We could use sendfile(), but to remain
575 * more portable lets just read/write the darn thing.
576 */
Jens Axboe9988ca72012-03-09 15:14:06 +0100577static int __fio_client_send_ini(struct fio_client *client, const char *filename)
Jens Axboe132159a2011-09-30 15:01:32 -0600578{
Jens Axboe46bcd492012-03-14 11:31:21 +0100579 struct cmd_job_pdu *pdu;
580 size_t p_size;
Jens Axboe132159a2011-09-30 15:01:32 -0600581 struct stat sb;
Jens Axboe46bcd492012-03-14 11:31:21 +0100582 char *p;
583 void *buf;
Jens Axboe132159a2011-09-30 15:01:32 -0600584 off_t len;
585 int fd, ret;
586
Jens Axboe46c48f12011-10-01 12:50:51 -0600587 dprint(FD_NET, "send ini %s to %s\n", filename, client->hostname);
588
Jens Axboe132159a2011-09-30 15:01:32 -0600589 fd = open(filename, O_RDONLY);
590 if (fd < 0) {
Jens Axboe25095e12012-03-09 17:09:55 +0100591 int ret = -errno;
592
Jens Axboee951bdc2011-10-05 21:58:45 +0200593 log_err("fio: job file <%s> open: %s\n", filename, strerror(errno));
Jens Axboe25095e12012-03-09 17:09:55 +0100594 return ret;
Jens Axboe132159a2011-09-30 15:01:32 -0600595 }
596
597 if (fstat(fd, &sb) < 0) {
Jens Axboe25095e12012-03-09 17:09:55 +0100598 int ret = -errno;
599
Jens Axboe132159a2011-09-30 15:01:32 -0600600 log_err("fio: job file stat: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200601 close(fd);
Jens Axboe25095e12012-03-09 17:09:55 +0100602 return ret;
Jens Axboe132159a2011-09-30 15:01:32 -0600603 }
604
Jens Axboe46bcd492012-03-14 11:31:21 +0100605 p_size = sb.st_size + sizeof(*pdu);
606 pdu = malloc(p_size);
607 buf = pdu->buf;
Jens Axboe132159a2011-09-30 15:01:32 -0600608
609 len = sb.st_size;
610 p = buf;
611 do {
612 ret = read(fd, p, len);
613 if (ret > 0) {
614 len -= ret;
615 if (!len)
616 break;
617 p += ret;
618 continue;
619 } else if (!ret)
620 break;
621 else if (errno == EAGAIN || errno == EINTR)
622 continue;
623 } while (1);
624
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200625 if (len) {
626 log_err("fio: failed reading job file %s\n", filename);
Jens Axboeb94cba42011-10-06 21:27:10 +0200627 close(fd);
Jens Axboec524ef72011-10-07 12:23:34 +0200628 free(buf);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200629 return 1;
630 }
631
Jens Axboe46bcd492012-03-14 11:31:21 +0100632 pdu->buf_len = __cpu_to_le32(sb.st_size);
633 pdu->client_type = cpu_to_le32(client->type);
634
Jens Axboec2cb6862012-02-23 20:56:12 +0100635 client->sent_job = 1;
Jens Axboe40c60512012-03-27 16:03:04 +0200636 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, pdu, p_size, NULL, NULL);
Jens Axboe46bcd492012-03-14 11:31:21 +0100637 free(pdu);
Jens Axboeb94cba42011-10-06 21:27:10 +0200638 close(fd);
Jens Axboe132159a2011-09-30 15:01:32 -0600639 return ret;
640}
Jens Axboe37db14f2011-09-30 17:00:42 -0600641
Jens Axboe9988ca72012-03-09 15:14:06 +0100642int fio_client_send_ini(struct fio_client *client, const char *filename)
643{
Jens Axboe25095e12012-03-09 17:09:55 +0100644 int ret;
645
646 ret = __fio_client_send_ini(client, filename);
Jens Axboe3af45202012-03-13 09:59:53 +0100647 if (!ret)
648 client->sent_job = 1;
Jens Axboe9988ca72012-03-09 15:14:06 +0100649
Jens Axboe25095e12012-03-09 17:09:55 +0100650 return ret;
Jens Axboe9988ca72012-03-09 15:14:06 +0100651}
652
Jens Axboea37f69b2011-10-01 12:24:21 -0600653int fio_clients_send_ini(const char *filename)
654{
655 struct fio_client *client;
656 struct flist_head *entry, *tmp;
657
658 flist_for_each_safe(entry, tmp, &client_list) {
659 client = flist_entry(entry, struct fio_client, list);
660
Jens Axboe14ea90e2012-08-26 17:33:34 +0200661 if (client->nr_ini_file) {
662 int i;
663
664 for (i = 0; i < client->nr_ini_file; i++) {
665 const char *ini = client->ini_file[i];
666
667 if (fio_client_send_ini(client, ini)) {
668 remove_client(client);
669 break;
670 }
671 }
672 } else if (!filename || fio_client_send_ini(client, filename))
Jens Axboe3af45202012-03-13 09:59:53 +0100673 remove_client(client);
Jens Axboea37f69b2011-10-01 12:24:21 -0600674 }
675
676 return !nr_clients;
677}
678
Jens Axboe40c60512012-03-27 16:03:04 +0200679int fio_client_update_options(struct fio_client *client,
680 struct thread_options *o, uint64_t *tag)
681{
682 struct cmd_add_job_pdu pdu;
683
684 pdu.thread_number = cpu_to_le32(client->thread_number);
685 pdu.groupid = cpu_to_le32(client->groupid);
686 convert_thread_options_to_net(&pdu.top, o);
687
688 return fio_net_send_cmd(client->fd, FIO_NET_CMD_UPDATE_JOB, &pdu, sizeof(pdu), tag, &client->cmd_list);
689}
690
Jens Axboea64e88d2011-10-03 14:20:01 +0200691static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
692{
693 dst->max_val = le64_to_cpu(src->max_val);
694 dst->min_val = le64_to_cpu(src->min_val);
695 dst->samples = le64_to_cpu(src->samples);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200696
697 /*
698 * Floats arrive as IEEE 754 encoded uint64_t, convert back to double
699 */
700 dst->mean.u.f = fio_uint64_to_double(le64_to_cpu(dst->mean.u.i));
701 dst->S.u.f = fio_uint64_to_double(le64_to_cpu(dst->S.u.i));
Jens Axboea64e88d2011-10-03 14:20:01 +0200702}
703
704static void convert_ts(struct thread_stat *dst, struct thread_stat *src)
705{
706 int i, j;
707
Jens Axboe2f122b12012-03-15 13:10:19 +0100708 dst->error = le32_to_cpu(src->error);
709 dst->thread_number = le32_to_cpu(src->thread_number);
710 dst->groupid = le32_to_cpu(src->groupid);
711 dst->pid = le32_to_cpu(src->pid);
712 dst->members = le32_to_cpu(src->members);
Jens Axboe771e58b2013-01-30 12:56:23 +0100713 dst->unified_rw_rep = le32_to_cpu(src->unified_rw_rep);
Jens Axboea64e88d2011-10-03 14:20:01 +0200714
Jens Axboe298921d2012-09-24 18:47:01 +0200715 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
Jens Axboea64e88d2011-10-03 14:20:01 +0200716 convert_io_stat(&dst->clat_stat[i], &src->clat_stat[i]);
717 convert_io_stat(&dst->slat_stat[i], &src->slat_stat[i]);
718 convert_io_stat(&dst->lat_stat[i], &src->lat_stat[i]);
719 convert_io_stat(&dst->bw_stat[i], &src->bw_stat[i]);
720 }
721
722 dst->usr_time = le64_to_cpu(src->usr_time);
723 dst->sys_time = le64_to_cpu(src->sys_time);
724 dst->ctx = le64_to_cpu(src->ctx);
725 dst->minf = le64_to_cpu(src->minf);
726 dst->majf = le64_to_cpu(src->majf);
727 dst->clat_percentiles = le64_to_cpu(src->clat_percentiles);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200728
729 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
730 fio_fp64_t *fps = &src->percentile_list[i];
731 fio_fp64_t *fpd = &dst->percentile_list[i];
732
733 fpd->u.f = fio_uint64_to_double(le64_to_cpu(fps->u.i));
734 }
Jens Axboea64e88d2011-10-03 14:20:01 +0200735
736 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
737 dst->io_u_map[i] = le32_to_cpu(src->io_u_map[i]);
738 dst->io_u_submit[i] = le32_to_cpu(src->io_u_submit[i]);
739 dst->io_u_complete[i] = le32_to_cpu(src->io_u_complete[i]);
740 }
741
742 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
743 dst->io_u_lat_u[i] = le32_to_cpu(src->io_u_lat_u[i]);
744 dst->io_u_lat_m[i] = le32_to_cpu(src->io_u_lat_m[i]);
745 }
746
Jens Axboe298921d2012-09-24 18:47:01 +0200747 for (i = 0; i < DDIR_RWDIR_CNT; i++)
Jens Axboea64e88d2011-10-03 14:20:01 +0200748 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
749 dst->io_u_plat[i][j] = le32_to_cpu(src->io_u_plat[i][j]);
750
Jens Axboe78799de2013-01-29 20:53:52 +0100751 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
Jens Axboea64e88d2011-10-03 14:20:01 +0200752 dst->total_io_u[i] = le64_to_cpu(src->total_io_u[i]);
Jens Axboe93eee042011-10-03 21:08:48 +0200753 dst->short_io_u[i] = le64_to_cpu(src->short_io_u[i]);
Jens Axboea64e88d2011-10-03 14:20:01 +0200754 }
755
756 dst->total_submit = le64_to_cpu(src->total_submit);
757 dst->total_complete = le64_to_cpu(src->total_complete);
758
Jens Axboe298921d2012-09-24 18:47:01 +0200759 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
Jens Axboea64e88d2011-10-03 14:20:01 +0200760 dst->io_bytes[i] = le64_to_cpu(src->io_bytes[i]);
761 dst->runtime[i] = le64_to_cpu(src->runtime[i]);
762 }
763
764 dst->total_run_time = le64_to_cpu(src->total_run_time);
765 dst->continue_on_error = le16_to_cpu(src->continue_on_error);
766 dst->total_err_count = le64_to_cpu(src->total_err_count);
Jens Axboeddcc0b62011-10-03 14:45:27 +0200767 dst->first_error = le32_to_cpu(src->first_error);
768 dst->kb_base = le32_to_cpu(src->kb_base);
Steven Noonanad705bc2013-04-08 15:05:25 -0700769 dst->unit_base = le32_to_cpu(src->unit_base);
Jens Axboea64e88d2011-10-03 14:20:01 +0200770}
771
772static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
773{
774 int i;
775
Jens Axboe298921d2012-09-24 18:47:01 +0200776 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
Jens Axboea64e88d2011-10-03 14:20:01 +0200777 dst->max_run[i] = le64_to_cpu(src->max_run[i]);
778 dst->min_run[i] = le64_to_cpu(src->min_run[i]);
779 dst->max_bw[i] = le64_to_cpu(src->max_bw[i]);
780 dst->min_bw[i] = le64_to_cpu(src->min_bw[i]);
781 dst->io_kb[i] = le64_to_cpu(src->io_kb[i]);
782 dst->agg[i] = le64_to_cpu(src->agg[i]);
783 }
784
785 dst->kb_base = le32_to_cpu(src->kb_base);
Steven Noonanad705bc2013-04-08 15:05:25 -0700786 dst->unit_base = le32_to_cpu(src->unit_base);
Jens Axboea64e88d2011-10-03 14:20:01 +0200787 dst->groupid = le32_to_cpu(src->groupid);
Jens Axboe771e58b2013-01-30 12:56:23 +0100788 dst->unified_rw_rep = le32_to_cpu(src->unified_rw_rep);
Jens Axboea64e88d2011-10-03 14:20:01 +0200789}
790
Jens Axboe89e5fad2012-03-05 09:21:12 +0100791static void handle_ts(struct fio_client *client, struct fio_net_cmd *cmd)
Jens Axboea64e88d2011-10-03 14:20:01 +0200792{
793 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
794
Jens Axboea64e88d2011-10-03 14:20:01 +0200795 show_thread_status(&p->ts, &p->rs);
Jens Axboe108fea72012-11-14 13:09:45 -0700796 client->did_stat = 1;
Jens Axboe37f0c1a2011-10-11 14:08:33 +0200797
Jens Axboe108fea72012-11-14 13:09:45 -0700798 if (!do_output_all_clients)
Jens Axboe37f0c1a2011-10-11 14:08:33 +0200799 return;
800
801 sum_thread_stats(&client_ts, &p->ts, sum_stat_nr);
802 sum_group_stats(&client_gs, &p->rs);
803
804 client_ts.members++;
Jens Axboe2f122b12012-03-15 13:10:19 +0100805 client_ts.thread_number = p->ts.thread_number;
Jens Axboe37f0c1a2011-10-11 14:08:33 +0200806 client_ts.groupid = p->ts.groupid;
Jens Axboe771e58b2013-01-30 12:56:23 +0100807 client_ts.unified_rw_rep = p->ts.unified_rw_rep;
Jens Axboe37f0c1a2011-10-11 14:08:33 +0200808
809 if (++sum_stat_nr == sum_stat_clients) {
810 strcpy(client_ts.name, "All clients");
811 show_thread_status(&client_ts, &client_gs);
812 }
Jens Axboea64e88d2011-10-03 14:20:01 +0200813}
814
Jens Axboe89e5fad2012-03-05 09:21:12 +0100815static void handle_gs(struct fio_client *client, struct fio_net_cmd *cmd)
Jens Axboea64e88d2011-10-03 14:20:01 +0200816{
817 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
818
Jens Axboea64e88d2011-10-03 14:20:01 +0200819 show_group_stats(gs);
820}
821
Jens Axboe3bf236c2012-03-03 20:35:50 +0100822static void handle_text(struct fio_client *client, struct fio_net_cmd *cmd)
823{
824 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
825 const char *buf = (const char *) pdu->buf;
826 const char *name;
827 int fio_unused ret;
828
829 name = client->name ? client->name : client->hostname;
830
831 if (!client->skip_newline)
832 fprintf(f_out, "<%s> ", name);
833 ret = fwrite(buf, pdu->buf_len, 1, f_out);
834 fflush(f_out);
835 client->skip_newline = strchr(buf, '\n') == NULL;
836}
837
Jens Axboed09a64a2011-10-13 11:38:56 +0200838static void convert_agg(struct disk_util_agg *agg)
839{
840 int i;
841
842 for (i = 0; i < 2; i++) {
843 agg->ios[i] = le32_to_cpu(agg->ios[i]);
844 agg->merges[i] = le32_to_cpu(agg->merges[i]);
845 agg->sectors[i] = le64_to_cpu(agg->sectors[i]);
846 agg->ticks[i] = le32_to_cpu(agg->ticks[i]);
847 }
848
849 agg->io_ticks = le32_to_cpu(agg->io_ticks);
850 agg->time_in_queue = le32_to_cpu(agg->time_in_queue);
851 agg->slavecount = le32_to_cpu(agg->slavecount);
Anton Blanchard823ba542011-11-07 14:16:26 +0100852 agg->max_util.u.f = fio_uint64_to_double(__le64_to_cpu(agg->max_util.u.i));
Jens Axboed09a64a2011-10-13 11:38:56 +0200853}
854
855static void convert_dus(struct disk_util_stat *dus)
856{
857 int i;
858
859 for (i = 0; i < 2; i++) {
860 dus->ios[i] = le32_to_cpu(dus->ios[i]);
861 dus->merges[i] = le32_to_cpu(dus->merges[i]);
862 dus->sectors[i] = le64_to_cpu(dus->sectors[i]);
863 dus->ticks[i] = le32_to_cpu(dus->ticks[i]);
864 }
865
866 dus->io_ticks = le32_to_cpu(dus->io_ticks);
867 dus->time_in_queue = le32_to_cpu(dus->time_in_queue);
868 dus->msec = le64_to_cpu(dus->msec);
869}
870
871static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd)
872{
873 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
874
Jens Axboed09a64a2011-10-13 11:38:56 +0200875 if (!client->disk_stats_shown) {
876 client->disk_stats_shown = 1;
877 log_info("\nDisk stats (read/write):\n");
878 }
879
Jens Axboef3afa572012-09-17 13:34:16 +0200880 print_disk_util(&du->dus, &du->agg, output_format == FIO_OUTPUT_TERSE);
Jens Axboed09a64a2011-10-13 11:38:56 +0200881}
882
Jens Axboe3bf236c2012-03-03 20:35:50 +0100883static void convert_jobs_eta(struct jobs_eta *je)
Jens Axboecf451d12011-10-03 16:48:30 +0200884{
Jens Axboecf451d12011-10-03 16:48:30 +0200885 int i;
886
887 je->nr_running = le32_to_cpu(je->nr_running);
888 je->nr_ramp = le32_to_cpu(je->nr_ramp);
889 je->nr_pending = le32_to_cpu(je->nr_pending);
Jens Axboe714e85f2013-04-15 10:21:56 +0200890 je->nr_setting_up = le32_to_cpu(je->nr_setting_up);
Jens Axboecf451d12011-10-03 16:48:30 +0200891 je->files_open = le32_to_cpu(je->files_open);
Jens Axboecf451d12011-10-03 16:48:30 +0200892
Jens Axboe298921d2012-09-24 18:47:01 +0200893 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
894 je->m_rate[i] = le32_to_cpu(je->m_rate[i]);
895 je->t_rate[i] = le32_to_cpu(je->t_rate[i]);
896 je->m_iops[i] = le32_to_cpu(je->m_iops[i]);
897 je->t_iops[i] = le32_to_cpu(je->t_iops[i]);
Jens Axboe16725bb2013-04-11 12:43:05 +0200898 je->rate[i] = le32_to_cpu(je->rate[i]);
899 je->iops[i] = le32_to_cpu(je->iops[i]);
Jens Axboecf451d12011-10-03 16:48:30 +0200900 }
901
Jens Axboeb51eedb2011-10-09 12:13:39 +0200902 je->elapsed_sec = le64_to_cpu(je->elapsed_sec);
Jens Axboecf451d12011-10-03 16:48:30 +0200903 je->eta_sec = le64_to_cpu(je->eta_sec);
Jens Axboe8c621fb2012-03-08 12:30:48 +0100904 je->nr_threads = le32_to_cpu(je->nr_threads);
Jens Axboeb7f05eb2012-05-11 20:33:02 +0200905 je->is_pow2 = le32_to_cpu(je->is_pow2);
Jens Axboeed2c0a12013-04-11 12:55:16 +0200906 je->unit_base = le32_to_cpu(je->unit_base);
Jens Axboe48fbb462011-10-09 12:19:08 +0200907}
Jens Axboecf451d12011-10-03 16:48:30 +0200908
Jens Axboe3e47bd22012-02-29 13:45:02 +0100909void fio_client_sum_jobs_eta(struct jobs_eta *dst, struct jobs_eta *je)
Jens Axboe48fbb462011-10-09 12:19:08 +0200910{
Jens Axboe48fbb462011-10-09 12:19:08 +0200911 int i;
912
913 dst->nr_running += je->nr_running;
914 dst->nr_ramp += je->nr_ramp;
915 dst->nr_pending += je->nr_pending;
Jens Axboe714e85f2013-04-15 10:21:56 +0200916 dst->nr_setting_up += je->nr_setting_up;
Jens Axboe48fbb462011-10-09 12:19:08 +0200917 dst->files_open += je->files_open;
Jens Axboe48fbb462011-10-09 12:19:08 +0200918
Jens Axboe298921d2012-09-24 18:47:01 +0200919 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
Jens Axboe3e47bd22012-02-29 13:45:02 +0100920 dst->m_rate[i] += je->m_rate[i];
921 dst->t_rate[i] += je->t_rate[i];
922 dst->m_iops[i] += je->m_iops[i];
923 dst->t_iops[i] += je->t_iops[i];
Jens Axboe16725bb2013-04-11 12:43:05 +0200924 dst->rate[i] += je->rate[i];
925 dst->iops[i] += je->iops[i];
Jens Axboe48fbb462011-10-09 12:19:08 +0200926 }
927
928 dst->elapsed_sec += je->elapsed_sec;
929
930 if (je->eta_sec > dst->eta_sec)
931 dst->eta_sec = je->eta_sec;
Jens Axboe8c621fb2012-03-08 12:30:48 +0100932
933 dst->nr_threads += je->nr_threads;
934 /* we need to handle je->run_str too ... */
Jens Axboe48fbb462011-10-09 12:19:08 +0200935}
936
Jens Axboea5276612012-03-04 15:15:08 +0100937void fio_client_dec_jobs_eta(struct client_eta *eta, client_eta_op eta_fn)
Jens Axboe82c1ed32011-10-10 08:56:18 +0200938{
939 if (!--eta->pending) {
Jens Axboea5276612012-03-04 15:15:08 +0100940 eta_fn(&eta->eta);
Jens Axboe82c1ed32011-10-10 08:56:18 +0200941 free(eta);
942 }
943}
944
Jens Axboe89c17072011-10-11 10:15:51 +0200945static void remove_reply_cmd(struct fio_client *client, struct fio_net_cmd *cmd)
946{
Jens Axboe40c60512012-03-27 16:03:04 +0200947 struct fio_net_cmd_reply *reply = NULL;
Jens Axboe89c17072011-10-11 10:15:51 +0200948 struct flist_head *entry;
949
950 flist_for_each(entry, &client->cmd_list) {
Jens Axboe40c60512012-03-27 16:03:04 +0200951 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
Jens Axboe89c17072011-10-11 10:15:51 +0200952
Jens Axboe40c60512012-03-27 16:03:04 +0200953 if (cmd->tag == (uintptr_t) reply)
Jens Axboe89c17072011-10-11 10:15:51 +0200954 break;
955
Jens Axboe40c60512012-03-27 16:03:04 +0200956 reply = NULL;
Jens Axboe89c17072011-10-11 10:15:51 +0200957 }
958
Jens Axboe40c60512012-03-27 16:03:04 +0200959 if (!reply) {
Jens Axboe4e0a8fa2013-04-15 11:40:57 +0200960 log_err("fio: client: unable to find matching tag (%llx)\n", (unsigned long long) cmd->tag);
Jens Axboe89c17072011-10-11 10:15:51 +0200961 return;
962 }
963
Jens Axboe40c60512012-03-27 16:03:04 +0200964 flist_del(&reply->list);
965 cmd->tag = reply->saved_tag;
966 free(reply);
967}
968
969int fio_client_wait_for_reply(struct fio_client *client, uint64_t tag)
970{
971 do {
972 struct fio_net_cmd_reply *reply = NULL;
973 struct flist_head *entry;
974
975 flist_for_each(entry, &client->cmd_list) {
976 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
977
978 if (tag == (uintptr_t) reply)
979 break;
980
981 reply = NULL;
982 }
983
984 if (!reply)
985 break;
986
987 usleep(1000);
988 } while (1);
989
990 return 0;
Jens Axboe89c17072011-10-11 10:15:51 +0200991}
992
Jens Axboe82c1ed32011-10-10 08:56:18 +0200993static void handle_eta(struct fio_client *client, struct fio_net_cmd *cmd)
Jens Axboe48fbb462011-10-09 12:19:08 +0200994{
995 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
Jens Axboedf380932011-10-11 14:25:08 +0200996 struct client_eta *eta = (struct client_eta *) (uintptr_t) cmd->tag;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200997
998 dprint(FD_NET, "client: got eta tag %p, %d\n", eta, eta->pending);
Jens Axboe48fbb462011-10-09 12:19:08 +0200999
Jens Axboef77d2672011-10-10 14:36:07 +02001000 assert(client->eta_in_flight == eta);
1001
1002 client->eta_in_flight = NULL;
Jens Axboe82c1ed32011-10-10 08:56:18 +02001003 flist_del_init(&client->eta_list);
1004
Jens Axboe2f99deb2012-03-09 14:37:29 +01001005 if (client->ops->jobs_eta)
1006 client->ops->jobs_eta(client, je);
1007
Jens Axboe3e47bd22012-02-29 13:45:02 +01001008 fio_client_sum_jobs_eta(&eta->eta, je);
Jens Axboea5276612012-03-04 15:15:08 +01001009 fio_client_dec_jobs_eta(eta, client->ops->eta);
Jens Axboecf451d12011-10-03 16:48:30 +02001010}
1011
Jens Axboeb5296dd2011-10-07 16:35:56 +02001012static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd)
Jens Axboe2e03b4b2011-10-04 09:00:40 +02001013{
Jens Axboe3989b142013-04-12 13:13:24 +02001014 struct cmd_probe_reply_pdu *probe = (struct cmd_probe_reply_pdu *) cmd->payload;
Jens Axboed2333352011-10-11 15:07:23 +02001015 const char *os, *arch;
1016 char bit[16];
Jens Axboe2e03b4b2011-10-04 09:00:40 +02001017
Jens Axboecca84642011-10-07 12:47:57 +02001018 os = fio_get_os_string(probe->os);
1019 if (!os)
1020 os = "unknown";
1021
1022 arch = fio_get_arch_string(probe->arch);
1023 if (!arch)
1024 os = "unknown";
1025
Jens Axboed2333352011-10-11 15:07:23 +02001026 sprintf(bit, "%d-bit", probe->bpp * 8);
Jens Axboe3989b142013-04-12 13:13:24 +02001027 probe->flags = le64_to_cpu(probe->flags);
Jens Axboe38fdef22011-10-11 14:30:06 +02001028
Jens Axboe3989b142013-04-12 13:13:24 +02001029 log_info("hostname=%s, be=%u, %s, os=%s, arch=%s, fio=%s, flags=%lx\n",
Jens Axboe38fdef22011-10-11 14:30:06 +02001030 probe->hostname, probe->bigendian, bit, os, arch,
Jens Axboe3989b142013-04-12 13:13:24 +02001031 probe->fio_version, (unsigned long) probe->flags);
Jens Axboeb5296dd2011-10-07 16:35:56 +02001032
1033 if (!client->name)
1034 client->name = strdup((char *) probe->hostname);
Jens Axboe2e03b4b2011-10-04 09:00:40 +02001035}
1036
Jens Axboe11e950b2011-10-16 21:34:14 +02001037static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd)
1038{
1039 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
1040
1041 client->state = Client_started;
1042 client->jobs = le32_to_cpu(pdu->jobs);
Jens Axboe108fea72012-11-14 13:09:45 -07001043 client->nr_stat = le32_to_cpu(pdu->stat_outputs);
1044
1045 if (sum_stat_clients > 1)
1046 do_output_all_clients = 1;
1047
1048 sum_stat_clients += client->nr_stat;
Jens Axboe11e950b2011-10-16 21:34:14 +02001049}
1050
1051static void handle_stop(struct fio_client *client, struct fio_net_cmd *cmd)
1052{
Jens Axboe498c92c2011-10-17 09:14:42 +02001053 if (client->error)
1054 log_info("client <%s>: exited with error %d\n", client->hostname, client->error);
Jens Axboe11e950b2011-10-16 21:34:14 +02001055}
1056
Jens Axboe6b79c802012-03-08 10:51:36 +01001057static void convert_stop(struct fio_net_cmd *cmd)
1058{
1059 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
1060
1061 pdu->error = le32_to_cpu(pdu->error);
1062}
1063
Jens Axboe084d1c62012-03-03 20:28:07 +01001064static void convert_text(struct fio_net_cmd *cmd)
1065{
1066 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
1067
1068 pdu->level = le32_to_cpu(pdu->level);
1069 pdu->buf_len = le32_to_cpu(pdu->buf_len);
1070 pdu->log_sec = le64_to_cpu(pdu->log_sec);
1071 pdu->log_usec = le64_to_cpu(pdu->log_usec);
1072}
1073
Jens Axboe3989b142013-04-12 13:13:24 +02001074static struct cmd_iolog_pdu *convert_iolog_gz(struct fio_net_cmd *cmd,
1075 struct cmd_iolog_pdu *pdu)
Jens Axboe1b427252012-03-14 15:03:03 +01001076{
Jens Axboe3989b142013-04-12 13:13:24 +02001077#ifdef CONFIG_ZLIB
Jens Axboe1b427252012-03-14 15:03:03 +01001078 struct cmd_iolog_pdu *ret;
Jens Axboe1b427252012-03-14 15:03:03 +01001079 z_stream stream;
Jens Axboe3989b142013-04-12 13:13:24 +02001080 uint32_t nr_samples;
1081 size_t total;
Jens Axboe1b427252012-03-14 15:03:03 +01001082 void *p;
1083
1084 stream.zalloc = Z_NULL;
1085 stream.zfree = Z_NULL;
1086 stream.opaque = Z_NULL;
1087 stream.avail_in = 0;
1088 stream.next_in = Z_NULL;
1089
1090 if (inflateInit(&stream) != Z_OK)
1091 return NULL;
1092
1093 /*
Jens Axboef5ed7652012-03-14 16:28:07 +01001094 * Get header first, it's not compressed
Jens Axboe1b427252012-03-14 15:03:03 +01001095 */
1096 nr_samples = le32_to_cpu(pdu->nr_samples);
1097
Jens Axboef5ed7652012-03-14 16:28:07 +01001098 total = nr_samples * sizeof(struct io_sample);
1099 ret = malloc(total + sizeof(*pdu));
Jens Axboe1b427252012-03-14 15:03:03 +01001100 ret->nr_samples = nr_samples;
Jens Axboe3989b142013-04-12 13:13:24 +02001101
1102 memcpy(ret, pdu, sizeof(*pdu));
Jens Axboe1b427252012-03-14 15:03:03 +01001103
Jens Axboef5ed7652012-03-14 16:28:07 +01001104 p = (void *) ret + sizeof(*pdu);
1105
1106 stream.avail_in = cmd->pdu_len - sizeof(*pdu);
1107 stream.next_in = (void *) pdu + sizeof(*pdu);
Jens Axboe1b427252012-03-14 15:03:03 +01001108 while (stream.avail_in) {
1109 unsigned int this_chunk = 65536;
1110 unsigned int this_len;
1111 int err;
1112
1113 if (this_chunk > total)
1114 this_chunk = total;
1115
1116 stream.avail_out = this_chunk;
1117 stream.next_out = p;
1118 err = inflate(&stream, Z_NO_FLUSH);
Jens Axboe3c547fe2012-03-14 21:53:00 +01001119 /* may be Z_OK, or Z_STREAM_END */
1120 if (err < 0) {
Jens Axboe1b427252012-03-14 15:03:03 +01001121 log_err("fio: inflate error %d\n", err);
Jens Axboef5ed7652012-03-14 16:28:07 +01001122 free(ret);
1123 ret = NULL;
Jens Axboe3989b142013-04-12 13:13:24 +02001124 goto err;
Jens Axboe1b427252012-03-14 15:03:03 +01001125 }
1126
1127 this_len = this_chunk - stream.avail_out;
1128 p += this_len;
1129 total -= this_len;
1130 }
1131
Jens Axboe3989b142013-04-12 13:13:24 +02001132err:
1133 inflateEnd(&stream);
1134 return ret;
1135#else
1136 return NULL;
1137#endif
1138}
1139
1140/*
1141 * This has been compressed on the server side, since it can be big.
1142 * Uncompress here.
1143 */
1144static struct cmd_iolog_pdu *convert_iolog(struct fio_net_cmd *cmd)
1145{
1146 struct cmd_iolog_pdu *pdu = (struct cmd_iolog_pdu *) cmd->payload;
1147 struct cmd_iolog_pdu *ret;
1148 int i;
1149
1150 /*
1151 * Convert if compressed and we support it. If it's not
1152 * compressed, we need not do anything.
1153 */
1154 if (le32_to_cpu(pdu->compressed)) {
1155#ifndef CONFIG_ZLIB
1156 log_err("fio: server sent compressed data by mistake\n");
1157 return NULL;
1158#endif
1159 ret = convert_iolog_gz(cmd, pdu);
1160 if (!ret) {
1161 log_err("fio: failed decompressing log\n");
1162 return NULL;
1163 }
1164 } else
1165 ret = pdu;
1166
1167 ret->thread_number = le32_to_cpu(ret->thread_number);
1168 ret->nr_samples = le32_to_cpu(ret->nr_samples);
1169 ret->log_type = le32_to_cpu(ret->log_type);
1170 ret->compressed = le32_to_cpu(ret->compressed);
1171
Jens Axboef5ed7652012-03-14 16:28:07 +01001172 for (i = 0; i < ret->nr_samples; i++) {
1173 struct io_sample *s = &ret->samples[i];
1174
1175 s->time = le64_to_cpu(s->time);
1176 s->val = le64_to_cpu(s->val);
1177 s->ddir = le32_to_cpu(s->ddir);
1178 s->bs = le32_to_cpu(s->bs);
1179 }
1180
Jens Axboe1b427252012-03-14 15:03:03 +01001181 return ret;
1182}
1183
Jens Axboea5276612012-03-04 15:15:08 +01001184int fio_handle_client(struct fio_client *client)
Jens Axboe37db14f2011-09-30 17:00:42 -06001185{
Jens Axboea5276612012-03-04 15:15:08 +01001186 struct client_ops *ops = client->ops;
Jens Axboe37db14f2011-09-30 17:00:42 -06001187 struct fio_net_cmd *cmd;
1188
Jens Axboe60efd142011-10-04 13:30:11 +02001189 dprint(FD_NET, "client: handle %s\n", client->hostname);
1190
Jens Axboee951bdc2011-10-05 21:58:45 +02001191 cmd = fio_net_recv_cmd(client->fd);
1192 if (!cmd)
1193 return 0;
Jens Axboec2c94582011-10-05 20:31:30 +02001194
Jens Axboeb9d2f302012-03-08 20:36:28 +01001195 dprint(FD_NET, "client: got cmd op %s from %s (pdu=%u)\n",
1196 fio_server_op(cmd->opcode), client->hostname, cmd->pdu_len);
Jens Axboe46c48f12011-10-01 12:50:51 -06001197
Jens Axboee951bdc2011-10-05 21:58:45 +02001198 switch (cmd->opcode) {
1199 case FIO_NET_CMD_QUIT:
Jens Axboe3ec62ec2012-03-01 12:01:29 +01001200 if (ops->quit)
Jens Axboe35c0ba72012-03-14 10:56:40 +01001201 ops->quit(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +02001202 remove_client(client);
1203 free(cmd);
1204 break;
Jens Axboe084d1c62012-03-03 20:28:07 +01001205 case FIO_NET_CMD_TEXT:
1206 convert_text(cmd);
Jens Axboe35c0ba72012-03-14 10:56:40 +01001207 ops->text(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +02001208 free(cmd);
1209 break;
Jens Axboe3bf236c2012-03-03 20:35:50 +01001210 case FIO_NET_CMD_DU: {
1211 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
1212
1213 convert_dus(&du->dus);
1214 convert_agg(&du->agg);
1215
Stephen M. Camerondd366722012-02-24 08:17:30 +01001216 ops->disk_util(client, cmd);
Jens Axboed09a64a2011-10-13 11:38:56 +02001217 free(cmd);
1218 break;
Jens Axboe3bf236c2012-03-03 20:35:50 +01001219 }
1220 case FIO_NET_CMD_TS: {
1221 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
1222
1223 convert_ts(&p->ts, &p->ts);
1224 convert_gs(&p->rs, &p->rs);
1225
Jens Axboe89e5fad2012-03-05 09:21:12 +01001226 ops->thread_status(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +02001227 free(cmd);
1228 break;
Jens Axboe3bf236c2012-03-03 20:35:50 +01001229 }
1230 case FIO_NET_CMD_GS: {
1231 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
1232
1233 convert_gs(gs, gs);
1234
Jens Axboe89e5fad2012-03-05 09:21:12 +01001235 ops->group_stats(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +02001236 free(cmd);
1237 break;
Jens Axboe3bf236c2012-03-03 20:35:50 +01001238 }
1239 case FIO_NET_CMD_ETA: {
1240 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
1241
Jens Axboe89c17072011-10-11 10:15:51 +02001242 remove_reply_cmd(client, cmd);
Jens Axboe3bf236c2012-03-03 20:35:50 +01001243 convert_jobs_eta(je);
Jens Axboea5276612012-03-04 15:15:08 +01001244 handle_eta(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +02001245 free(cmd);
1246 break;
Jens Axboe3bf236c2012-03-03 20:35:50 +01001247 }
Jens Axboee951bdc2011-10-05 21:58:45 +02001248 case FIO_NET_CMD_PROBE:
Jens Axboe89c17072011-10-11 10:15:51 +02001249 remove_reply_cmd(client, cmd);
Stephen M. Camerondd366722012-02-24 08:17:30 +01001250 ops->probe(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +02001251 free(cmd);
1252 break;
Jens Axboe5d7793a2012-03-08 19:59:16 +01001253 case FIO_NET_CMD_SERVER_START:
Jens Axboe01be0382011-10-15 14:43:41 +02001254 client->state = Client_running;
Jens Axboe85dd01e2012-03-12 14:33:16 +01001255 if (ops->job_start)
1256 ops->job_start(client, cmd);
Jens Axboe01be0382011-10-15 14:43:41 +02001257 free(cmd);
1258 break;
Jens Axboe85dd01e2012-03-12 14:33:16 +01001259 case FIO_NET_CMD_START: {
1260 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
1261
1262 pdu->jobs = le32_to_cpu(pdu->jobs);
1263 ops->start(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +02001264 free(cmd);
1265 break;
Jens Axboe85dd01e2012-03-12 14:33:16 +01001266 }
Jens Axboe6b79c802012-03-08 10:51:36 +01001267 case FIO_NET_CMD_STOP: {
1268 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
1269
1270 convert_stop(cmd);
1271 client->state = Client_stopped;
Jens Axboe122c7722012-03-19 09:13:15 +01001272 client->error = le32_to_cpu(pdu->error);
1273 client->signal = le32_to_cpu(pdu->signal);
Jens Axboe6b79c802012-03-08 10:51:36 +01001274 ops->stop(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +02001275 free(cmd);
1276 break;
Jens Axboe6b79c802012-03-08 10:51:36 +01001277 }
Jens Axboe40c60512012-03-27 16:03:04 +02001278 case FIO_NET_CMD_ADD_JOB: {
1279 struct cmd_add_job_pdu *pdu = (struct cmd_add_job_pdu *) cmd->payload;
1280
1281 client->thread_number = le32_to_cpu(pdu->thread_number);
1282 client->groupid = le32_to_cpu(pdu->groupid);
1283
Jens Axboe807f9972012-03-02 10:25:24 +01001284 if (ops->add_job)
1285 ops->add_job(client, cmd);
1286 free(cmd);
1287 break;
Jens Axboe40c60512012-03-27 16:03:04 +02001288 }
Jens Axboe1b427252012-03-14 15:03:03 +01001289 case FIO_NET_CMD_IOLOG:
1290 if (ops->iolog) {
1291 struct cmd_iolog_pdu *pdu;
1292
1293 pdu = convert_iolog(cmd);
1294 ops->iolog(client, pdu);
1295 }
1296 free(cmd);
1297 break;
Jens Axboe40c60512012-03-27 16:03:04 +02001298 case FIO_NET_CMD_UPDATE_JOB:
Jens Axboe40c60512012-03-27 16:03:04 +02001299 ops->update_job(client, cmd);
Jens Axboe649cee92012-03-28 09:15:32 +02001300 remove_reply_cmd(client, cmd);
Jens Axboe40c60512012-03-27 16:03:04 +02001301 free(cmd);
1302 break;
Jens Axboee951bdc2011-10-05 21:58:45 +02001303 default:
Jens Axboe89c17072011-10-11 10:15:51 +02001304 log_err("fio: unknown client op: %s\n", fio_server_op(cmd->opcode));
Jens Axboee951bdc2011-10-05 21:58:45 +02001305 free(cmd);
1306 break;
Jens Axboe37db14f2011-09-30 17:00:42 -06001307 }
1308
Jens Axboee951bdc2011-10-05 21:58:45 +02001309 return 1;
Jens Axboe37db14f2011-09-30 17:00:42 -06001310}
Jens Axboeb66570d2011-10-01 11:11:35 -06001311
Jens Axboea5276612012-03-04 15:15:08 +01001312static void request_client_etas(struct client_ops *ops)
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001313{
1314 struct fio_client *client;
1315 struct flist_head *entry;
1316 struct client_eta *eta;
Jens Axboe82c1ed32011-10-10 08:56:18 +02001317 int skipped = 0;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001318
1319 dprint(FD_NET, "client: request eta (%d)\n", nr_clients);
1320
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001321 eta = malloc(sizeof(*eta));
1322 memset(&eta->eta, 0, sizeof(eta->eta));
1323 eta->pending = nr_clients;
1324
1325 flist_for_each(entry, &client_list) {
1326 client = flist_entry(entry, struct fio_client, list);
1327
Jens Axboe82c1ed32011-10-10 08:56:18 +02001328 if (!flist_empty(&client->eta_list)) {
1329 skipped++;
1330 continue;
1331 }
Jens Axboe01be0382011-10-15 14:43:41 +02001332 if (client->state != Client_running)
1333 continue;
Jens Axboe82c1ed32011-10-10 08:56:18 +02001334
Jens Axboef77d2672011-10-10 14:36:07 +02001335 assert(!client->eta_in_flight);
Jens Axboe82c1ed32011-10-10 08:56:18 +02001336 flist_add_tail(&client->eta_list, &eta_list);
Jens Axboef77d2672011-10-10 14:36:07 +02001337 client->eta_in_flight = eta;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001338 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_SEND_ETA,
Jens Axboedf380932011-10-11 14:25:08 +02001339 (uintptr_t) eta, &client->cmd_list);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001340 }
1341
Jens Axboe82c1ed32011-10-10 08:56:18 +02001342 while (skipped--)
Jens Axboea5276612012-03-04 15:15:08 +01001343 fio_client_dec_jobs_eta(eta, ops->eta);
Jens Axboe82c1ed32011-10-10 08:56:18 +02001344
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001345 dprint(FD_NET, "client: requested eta tag %p\n", eta);
1346}
1347
Jens Axboe89c17072011-10-11 10:15:51 +02001348static int client_check_cmd_timeout(struct fio_client *client,
1349 struct timeval *now)
1350{
Jens Axboe40c60512012-03-27 16:03:04 +02001351 struct fio_net_cmd_reply *reply;
Jens Axboe89c17072011-10-11 10:15:51 +02001352 struct flist_head *entry, *tmp;
1353 int ret = 0;
1354
1355 flist_for_each_safe(entry, tmp, &client->cmd_list) {
Jens Axboe40c60512012-03-27 16:03:04 +02001356 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
Jens Axboe89c17072011-10-11 10:15:51 +02001357
Jens Axboe40c60512012-03-27 16:03:04 +02001358 if (mtime_since(&reply->tv, now) < FIO_NET_CLIENT_TIMEOUT)
Jens Axboe89c17072011-10-11 10:15:51 +02001359 continue;
1360
1361 log_err("fio: client %s, timeout on cmd %s\n", client->hostname,
Jens Axboe40c60512012-03-27 16:03:04 +02001362 fio_server_op(reply->opcode));
1363 flist_del(&reply->list);
1364 free(reply);
Jens Axboe89c17072011-10-11 10:15:51 +02001365 ret = 1;
1366 }
1367
1368 return flist_empty(&client->cmd_list) && ret;
1369}
1370
Jens Axboea5276612012-03-04 15:15:08 +01001371static int fio_check_clients_timed_out(void)
Jens Axboe89c17072011-10-11 10:15:51 +02001372{
1373 struct fio_client *client;
1374 struct flist_head *entry, *tmp;
1375 struct timeval tv;
1376 int ret = 0;
1377
Jens Axboe67bf9822013-01-10 11:23:19 +01001378 fio_gettime(&tv, NULL);
Jens Axboe89c17072011-10-11 10:15:51 +02001379
1380 flist_for_each_safe(entry, tmp, &client_list) {
1381 client = flist_entry(entry, struct fio_client, list);
1382
1383 if (flist_empty(&client->cmd_list))
1384 continue;
1385
1386 if (!client_check_cmd_timeout(client, &tv))
1387 continue;
1388
Jens Axboea5276612012-03-04 15:15:08 +01001389 if (client->ops->timed_out)
1390 client->ops->timed_out(client);
Jens Axboeed727a42012-03-02 12:14:40 +01001391 else
1392 log_err("fio: client %s timed out\n", client->hostname);
1393
Jens Axboe89c17072011-10-11 10:15:51 +02001394 remove_client(client);
1395 ret = 1;
1396 }
1397
1398 return ret;
1399}
1400
Stephen M. Camerondd366722012-02-24 08:17:30 +01001401int fio_handle_clients(struct client_ops *ops)
Jens Axboeb66570d2011-10-01 11:11:35 -06001402{
Jens Axboeb66570d2011-10-01 11:11:35 -06001403 struct pollfd *pfds;
Jens Axboe498c92c2011-10-17 09:14:42 +02001404 int i, ret = 0, retval = 0;
Jens Axboeb66570d2011-10-01 11:11:35 -06001405
Jens Axboe67bf9822013-01-10 11:23:19 +01001406 fio_gettime(&eta_tv, NULL);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001407
Jens Axboeb66570d2011-10-01 11:11:35 -06001408 pfds = malloc(nr_clients * sizeof(struct pollfd));
1409
Jens Axboe37f0c1a2011-10-11 14:08:33 +02001410 init_thread_stat(&client_ts);
1411 init_group_run_stat(&client_gs);
1412
Jens Axboeb66570d2011-10-01 11:11:35 -06001413 while (!exit_backend && nr_clients) {
Jens Axboec2cb6862012-02-23 20:56:12 +01001414 struct flist_head *entry, *tmp;
1415 struct fio_client *client;
1416
Jens Axboe82a4be12011-10-01 12:40:32 -06001417 i = 0;
Jens Axboec2cb6862012-02-23 20:56:12 +01001418 flist_for_each_safe(entry, tmp, &client_list) {
Jens Axboe82a4be12011-10-01 12:40:32 -06001419 client = flist_entry(entry, struct fio_client, list);
1420
Jens Axboea5276612012-03-04 15:15:08 +01001421 if (!client->sent_job && !client->ops->stay_connected &&
Jens Axboec2cb6862012-02-23 20:56:12 +01001422 flist_empty(&client->cmd_list)) {
1423 remove_client(client);
1424 continue;
1425 }
1426
Jens Axboe82a4be12011-10-01 12:40:32 -06001427 pfds[i].fd = client->fd;
1428 pfds[i].events = POLLIN;
1429 i++;
1430 }
1431
Jens Axboec2cb6862012-02-23 20:56:12 +01001432 if (!nr_clients)
1433 break;
1434
Jens Axboe82a4be12011-10-01 12:40:32 -06001435 assert(i == nr_clients);
1436
Jens Axboe5c2857f2011-10-04 13:14:32 +02001437 do {
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001438 struct timeval tv;
1439
Jens Axboe67bf9822013-01-10 11:23:19 +01001440 fio_gettime(&tv, NULL);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001441 if (mtime_since(&eta_tv, &tv) >= 900) {
Jens Axboea5276612012-03-04 15:15:08 +01001442 request_client_etas(ops);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001443 memcpy(&eta_tv, &tv, sizeof(tv));
Jens Axboe89c17072011-10-11 10:15:51 +02001444
Jens Axboea5276612012-03-04 15:15:08 +01001445 if (fio_check_clients_timed_out())
Jens Axboe89c17072011-10-11 10:15:51 +02001446 break;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001447 }
1448
Jens Axboe80102c82012-03-23 13:19:31 +01001449 ret = poll(pfds, nr_clients, ops->eta_msec);
Jens Axboe5c2857f2011-10-04 13:14:32 +02001450 if (ret < 0) {
1451 if (errno == EINTR)
1452 continue;
1453 log_err("fio: poll clients: %s\n", strerror(errno));
1454 break;
1455 } else if (!ret)
Jens Axboeb66570d2011-10-01 11:11:35 -06001456 continue;
Jens Axboe5c2857f2011-10-04 13:14:32 +02001457 } while (ret <= 0);
Jens Axboeb66570d2011-10-01 11:11:35 -06001458
1459 for (i = 0; i < nr_clients; i++) {
1460 if (!(pfds[i].revents & POLLIN))
1461 continue;
1462
1463 client = find_client_by_fd(pfds[i].fd);
1464 if (!client) {
Jens Axboe3c5f57e2011-10-06 12:37:50 +02001465 log_err("fio: unknown client fd %d\n", pfds[i].fd);
Jens Axboeb66570d2011-10-01 11:11:35 -06001466 continue;
1467 }
Jens Axboea5276612012-03-04 15:15:08 +01001468 if (!fio_handle_client(client)) {
Jens Axboe28d3ab02011-10-05 20:45:37 +02001469 log_info("client: host=%s disconnected\n",
1470 client->hostname);
1471 remove_client(client);
Jens Axboe498c92c2011-10-17 09:14:42 +02001472 retval = 1;
Jens Axboe38990762011-10-17 13:31:33 +02001473 } else if (client->error)
Jens Axboe498c92c2011-10-17 09:14:42 +02001474 retval = 1;
Jens Axboe343cb4a2012-03-09 17:16:51 +01001475 fio_put_client(client);
Jens Axboeb66570d2011-10-01 11:11:35 -06001476 }
1477 }
1478
1479 free(pfds);
Jens Axboe498c92c2011-10-17 09:14:42 +02001480 return retval;
Jens Axboeb66570d2011-10-01 11:11:35 -06001481}