blob: 5725fd12d87c38b8bef91daafe1afaf96265753d [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;
Castor Fu952b05e2013-10-31 11:00:34 -060060static struct json_object *root = NULL;
61static struct json_array *clients_array = NULL;
62static struct json_array *du_array = NULL;
Jens Axboe108fea72012-11-14 13:09:45 -070063static int do_output_all_clients;
Jens Axboe37f0c1a2011-10-11 14:08:33 +020064
Jens Axboe3c5f57e2011-10-06 12:37:50 +020065#define FIO_CLIENT_HASH_BITS 7
66#define FIO_CLIENT_HASH_SZ (1 << FIO_CLIENT_HASH_BITS)
67#define FIO_CLIENT_HASH_MASK (FIO_CLIENT_HASH_SZ - 1)
Jens Axboebebe6392011-10-07 10:00:51 +020068static struct flist_head client_hash[FIO_CLIENT_HASH_SZ];
Jens Axboe3c5f57e2011-10-06 12:37:50 +020069
Jens Axboebebe6392011-10-07 10:00:51 +020070static void fio_client_add_hash(struct fio_client *client)
Jens Axboe3c5f57e2011-10-06 12:37:50 +020071{
72 int bucket = hash_long(client->fd, FIO_CLIENT_HASH_BITS);
73
74 bucket &= FIO_CLIENT_HASH_MASK;
Jens Axboebebe6392011-10-07 10:00:51 +020075 flist_add(&client->hash_list, &client_hash[bucket]);
Jens Axboe3c5f57e2011-10-06 12:37:50 +020076}
77
Jens Axboebebe6392011-10-07 10:00:51 +020078static void fio_client_remove_hash(struct fio_client *client)
Jens Axboe3c5f57e2011-10-06 12:37:50 +020079{
Jens Axboebebe6392011-10-07 10:00:51 +020080 if (!flist_empty(&client->hash_list))
81 flist_del_init(&client->hash_list);
Jens Axboe3c5f57e2011-10-06 12:37:50 +020082}
83
84static void fio_init fio_client_hash_init(void)
85{
86 int i;
87
Jens Axboebebe6392011-10-07 10:00:51 +020088 for (i = 0; i < FIO_CLIENT_HASH_SZ; i++)
89 INIT_FLIST_HEAD(&client_hash[i]);
Jens Axboe3c5f57e2011-10-06 12:37:50 +020090}
91
Castor Fu952b05e2013-10-31 11:00:34 -060092static void fio_client_json_init(void)
93{
94 if (output_format != FIO_OUTPUT_JSON)
95 return;
96 root = json_create_object();
97 json_object_add_value_string(root, "fio version", fio_version_string);
98 clients_array = json_create_array();
99 json_object_add_value_array(root, "client_stats", clients_array);
100 du_array = json_create_array();
101 json_object_add_value_array(root, "disk_util", du_array);
102}
103
104static void fio_client_json_fini(void)
105{
106 if (output_format != FIO_OUTPUT_JSON)
107 return;
108 json_print_object(root);
109 log_info("\n");
110 json_free_object(root);
111 root = NULL;
112 clients_array = NULL;
113 du_array = NULL;
114}
115
Jens Axboeb66570d2011-10-01 11:11:35 -0600116static struct fio_client *find_client_by_fd(int fd)
117{
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200118 int bucket = hash_long(fd, FIO_CLIENT_HASH_BITS) & FIO_CLIENT_HASH_MASK;
Jens Axboeb66570d2011-10-01 11:11:35 -0600119 struct fio_client *client;
120 struct flist_head *entry;
121
Jens Axboebebe6392011-10-07 10:00:51 +0200122 flist_for_each(entry, &client_hash[bucket]) {
123 client = flist_entry(entry, struct fio_client, hash_list);
Jens Axboeb66570d2011-10-01 11:11:35 -0600124
Jens Axboee55f8f32012-03-06 15:42:31 +0100125 if (client->fd == fd) {
126 client->refs++;
Jens Axboeb66570d2011-10-01 11:11:35 -0600127 return client;
Jens Axboee55f8f32012-03-06 15:42:31 +0100128 }
Jens Axboeb66570d2011-10-01 11:11:35 -0600129 }
130
131 return NULL;
132}
133
Jens Axboe3af45202012-03-13 09:59:53 +0100134void fio_put_client(struct fio_client *client)
Jens Axboeb66570d2011-10-01 11:11:35 -0600135{
Jens Axboe5121a9a2012-03-05 13:32:47 +0100136 if (--client->refs)
137 return;
138
Jens Axboeb66570d2011-10-01 11:11:35 -0600139 free(client->hostname);
Jens Axboe81179ee2011-10-04 12:42:06 +0200140 if (client->argv)
141 free(client->argv);
Jens Axboeb5296dd2011-10-07 16:35:56 +0200142 if (client->name)
143 free(client->name);
Jens Axboe35899182014-10-07 20:56:28 -0600144 while (client->nr_files) {
145 struct client_file *cf = &client->files[--client->nr_files];
146
147 free(cf->file);
148 }
149 if (client->files)
150 free(client->files);
Jens Axboe81179ee2011-10-04 12:42:06 +0200151
Jens Axboe108fea72012-11-14 13:09:45 -0700152 if (!client->did_stat)
153 sum_stat_clients -= client->nr_stat;
154
Jens Axboeb66570d2011-10-01 11:11:35 -0600155 free(client);
156}
Jens Axboe132159a2011-09-30 15:01:32 -0600157
Jens Axboe3af45202012-03-13 09:59:53 +0100158static void remove_client(struct fio_client *client)
Jens Axboe5121a9a2012-03-05 13:32:47 +0100159{
Jens Axboe3af45202012-03-13 09:59:53 +0100160 assert(client->refs);
161
162 dprint(FD_NET, "client: removed <%s>\n", client->hostname);
163
164 if (!flist_empty(&client->list))
165 flist_del_init(&client->list);
166
167 fio_client_remove_hash(client);
168
169 if (!flist_empty(&client->eta_list)) {
170 flist_del_init(&client->eta_list);
171 fio_client_dec_jobs_eta(client->eta_in_flight, client->ops->eta);
172 }
173
Jens Axboe1e0c1842012-03-20 15:15:00 +0100174 close(client->fd);
175 client->fd = -1;
176
Jens Axboe0cf3ece2012-03-21 10:15:20 +0100177 if (client->ops->removed)
178 client->ops->removed(client);
179
Jens Axboe3af45202012-03-13 09:59:53 +0100180 nr_clients--;
Jens Axboe3af45202012-03-13 09:59:53 +0100181 fio_put_client(client);
Jens Axboe5121a9a2012-03-05 13:32:47 +0100182}
183
Jens Axboe343cb4a2012-03-09 17:16:51 +0100184struct fio_client *fio_get_client(struct fio_client *client)
185{
186 client->refs++;
187 return client;
188}
189
Jens Axboefa2ea802011-10-08 21:07:29 +0200190static void __fio_client_add_cmd_option(struct fio_client *client,
191 const char *opt)
Jens Axboe81179ee2011-10-04 12:42:06 +0200192{
Jens Axboe39e8e012011-10-04 13:46:08 +0200193 int index;
194
195 index = client->argc++;
Jens Axboe81179ee2011-10-04 12:42:06 +0200196 client->argv = realloc(client->argv, sizeof(char *) * client->argc);
Jens Axboe39e8e012011-10-04 13:46:08 +0200197 client->argv[index] = strdup(opt);
198 dprint(FD_NET, "client: add cmd %d: %s\n", index, opt);
Jens Axboe81179ee2011-10-04 12:42:06 +0200199}
200
Jens Axboefa2ea802011-10-08 21:07:29 +0200201void fio_client_add_cmd_option(void *cookie, const char *opt)
Jens Axboe81179ee2011-10-04 12:42:06 +0200202{
Jens Axboebebe6392011-10-07 10:00:51 +0200203 struct fio_client *client = cookie;
Jens Axboe3f3a4542011-10-10 21:11:09 +0200204 struct flist_head *entry;
Jens Axboe81179ee2011-10-04 12:42:06 +0200205
Jens Axboebebe6392011-10-07 10:00:51 +0200206 if (!client || !opt)
Jens Axboefa2ea802011-10-08 21:07:29 +0200207 return;
Jens Axboe81179ee2011-10-04 12:42:06 +0200208
Jens Axboefa2ea802011-10-08 21:07:29 +0200209 __fio_client_add_cmd_option(client, opt);
Jens Axboe3f3a4542011-10-10 21:11:09 +0200210
211 /*
212 * Duplicate arguments to shared client group
213 */
214 flist_for_each(entry, &arg_list) {
215 client = flist_entry(entry, struct fio_client, arg_list);
216
217 __fio_client_add_cmd_option(client, opt);
218 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200219}
220
Jens Axboea5276612012-03-04 15:15:08 +0100221struct fio_client *fio_client_add_explicit(struct client_ops *ops,
222 const char *hostname, int type,
Jens Axboe3ec62ec2012-03-01 12:01:29 +0100223 int port)
224{
225 struct fio_client *client;
226
227 client = malloc(sizeof(*client));
228 memset(client, 0, sizeof(*client));
229
230 INIT_FLIST_HEAD(&client->list);
231 INIT_FLIST_HEAD(&client->hash_list);
232 INIT_FLIST_HEAD(&client->arg_list);
233 INIT_FLIST_HEAD(&client->eta_list);
234 INIT_FLIST_HEAD(&client->cmd_list);
235
236 client->hostname = strdup(hostname);
237
238 if (type == Fio_client_socket)
239 client->is_sock = 1;
240 else {
241 int ipv6;
242
243 ipv6 = type == Fio_client_ipv6;
Jens Axboe3aa3cee2014-01-24 18:56:56 -0800244 if (fio_server_parse_host(hostname, ipv6,
Jens Axboe3ec62ec2012-03-01 12:01:29 +0100245 &client->addr.sin_addr,
246 &client->addr6.sin6_addr))
247 goto err;
248
249 client->port = port;
250 }
251
252 client->fd = -1;
Jens Axboea5276612012-03-04 15:15:08 +0100253 client->ops = ops;
Jens Axboe5121a9a2012-03-05 13:32:47 +0100254 client->refs = 1;
Jens Axboe46bcd492012-03-14 11:31:21 +0100255 client->type = ops->client_type;
Jens Axboe3ec62ec2012-03-01 12:01:29 +0100256
257 __fio_client_add_cmd_option(client, "fio");
258
259 flist_add(&client->list, &client_list);
260 nr_clients++;
261 dprint(FD_NET, "client: added <%s>\n", client->hostname);
262 return client;
263err:
264 free(client);
265 return NULL;
266}
267
Jens Axboe35899182014-10-07 20:56:28 -0600268int fio_client_add_ini_file(void *cookie, const char *ini_file, int remote)
Jens Axboe14ea90e2012-08-26 17:33:34 +0200269{
270 struct fio_client *client = cookie;
Jens Axboe35899182014-10-07 20:56:28 -0600271 struct client_file *cf;
Jens Axboe14ea90e2012-08-26 17:33:34 +0200272 size_t new_size;
Jens Axboe35899182014-10-07 20:56:28 -0600273 void *new_files;
Jens Axboe14ea90e2012-08-26 17:33:34 +0200274
275 dprint(FD_NET, "client <%s>: add ini %s\n", client->hostname, ini_file);
276
Jens Axboe35899182014-10-07 20:56:28 -0600277 new_size = (client->nr_files + 1) * sizeof(struct client_file);
278 new_files = realloc(client->files, new_size);
279 if (!new_files)
280 return 1;
281
282 client->files = new_files;
283 cf = &client->files[client->nr_files];
284 cf->file = strdup(ini_file);
285 cf->remote = remote;
286 client->nr_files++;
287 return 0;
Jens Axboe14ea90e2012-08-26 17:33:34 +0200288}
289
Jens Axboea5276612012-03-04 15:15:08 +0100290int fio_client_add(struct client_ops *ops, const char *hostname, void **cookie)
Jens Axboe132159a2011-09-30 15:01:32 -0600291{
Jens Axboe3f3a4542011-10-10 21:11:09 +0200292 struct fio_client *existing = *cookie;
Jens Axboeb66570d2011-10-01 11:11:35 -0600293 struct fio_client *client;
Jens Axboe132159a2011-09-30 15:01:32 -0600294
Jens Axboe3f3a4542011-10-10 21:11:09 +0200295 if (existing) {
296 /*
297 * We always add our "exec" name as the option, hence 1
298 * means empty.
299 */
300 if (existing->argc == 1)
301 flist_add_tail(&existing->arg_list, &arg_list);
302 else {
303 while (!flist_empty(&arg_list))
304 flist_del_init(arg_list.next);
305 }
306 }
307
Jens Axboeb66570d2011-10-01 11:11:35 -0600308 client = malloc(sizeof(*client));
Jens Axboea37f69b2011-10-01 12:24:21 -0600309 memset(client, 0, sizeof(*client));
Jens Axboe81179ee2011-10-04 12:42:06 +0200310
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200311 INIT_FLIST_HEAD(&client->list);
Jens Axboebebe6392011-10-07 10:00:51 +0200312 INIT_FLIST_HEAD(&client->hash_list);
Jens Axboe3f3a4542011-10-10 21:11:09 +0200313 INIT_FLIST_HEAD(&client->arg_list);
Jens Axboe82c1ed32011-10-10 08:56:18 +0200314 INIT_FLIST_HEAD(&client->eta_list);
Jens Axboe89c17072011-10-11 10:15:51 +0200315 INIT_FLIST_HEAD(&client->cmd_list);
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200316
Jens Axboebebe6392011-10-07 10:00:51 +0200317 if (fio_server_parse_string(hostname, &client->hostname,
318 &client->is_sock, &client->port,
Jens Axboe811826b2011-10-24 09:11:50 +0200319 &client->addr.sin_addr,
320 &client->addr6.sin6_addr,
321 &client->ipv6))
Jens Axboebebe6392011-10-07 10:00:51 +0200322 return -1;
323
Jens Axboea37f69b2011-10-01 12:24:21 -0600324 client->fd = -1;
Jens Axboea5276612012-03-04 15:15:08 +0100325 client->ops = ops;
Jens Axboe5121a9a2012-03-05 13:32:47 +0100326 client->refs = 1;
Jens Axboe46bcd492012-03-14 11:31:21 +0100327 client->type = ops->client_type;
Jens Axboe81179ee2011-10-04 12:42:06 +0200328
329 __fio_client_add_cmd_option(client, "fio");
330
Jens Axboea37f69b2011-10-01 12:24:21 -0600331 flist_add(&client->list, &client_list);
332 nr_clients++;
Jens Axboebebe6392011-10-07 10:00:51 +0200333 dprint(FD_NET, "client: added <%s>\n", client->hostname);
334 *cookie = client;
335 return 0;
Jens Axboea37f69b2011-10-01 12:24:21 -0600336}
337
Jens Axboed824c3b2012-03-09 17:45:43 +0100338static void probe_client(struct fio_client *client)
339{
Jens Axboe3989b142013-04-12 13:13:24 +0200340 struct cmd_client_probe_pdu pdu;
341 uint64_t tag;
342
Jens Axboed824c3b2012-03-09 17:45:43 +0100343 dprint(FD_NET, "client: send probe\n");
344
Jens Axboe3989b142013-04-12 13:13:24 +0200345#ifdef CONFIG_ZLIB
346 pdu.flags = __le64_to_cpu(FIO_PROBE_FLAG_ZLIB);
347#else
348 pdu.flags = 0;
349#endif
350
351 fio_net_send_cmd(client->fd, FIO_NET_CMD_PROBE, &pdu, sizeof(pdu), &tag, &client->cmd_list);
Jens Axboed824c3b2012-03-09 17:45:43 +0100352}
353
Jens Axboe87aa8f12011-10-06 21:24:13 +0200354static int fio_client_connect_ip(struct fio_client *client)
Jens Axboea37f69b2011-10-01 12:24:21 -0600355{
Jens Axboe811826b2011-10-24 09:11:50 +0200356 struct sockaddr *addr;
Jens Axboe67bf9822013-01-10 11:23:19 +0100357 socklen_t socklen;
Jens Axboe811826b2011-10-24 09:11:50 +0200358 int fd, domain;
Jens Axboe132159a2011-09-30 15:01:32 -0600359
Jens Axboe811826b2011-10-24 09:11:50 +0200360 if (client->ipv6) {
361 client->addr6.sin6_family = AF_INET6;
362 client->addr6.sin6_port = htons(client->port);
363 domain = AF_INET6;
364 addr = (struct sockaddr *) &client->addr6;
365 socklen = sizeof(client->addr6);
366 } else {
367 client->addr.sin_family = AF_INET;
368 client->addr.sin_port = htons(client->port);
369 domain = AF_INET;
370 addr = (struct sockaddr *) &client->addr;
371 socklen = sizeof(client->addr);
372 }
Jens Axboe132159a2011-09-30 15:01:32 -0600373
Jens Axboe811826b2011-10-24 09:11:50 +0200374 fd = socket(domain, SOCK_STREAM, 0);
Jens Axboe132159a2011-09-30 15:01:32 -0600375 if (fd < 0) {
Jens Axboe25095e12012-03-09 17:09:55 +0100376 int ret = -errno;
377
Jens Axboe132159a2011-09-30 15:01:32 -0600378 log_err("fio: socket: %s\n", strerror(errno));
Jens Axboe25095e12012-03-09 17:09:55 +0100379 return ret;
Jens Axboe132159a2011-09-30 15:01:32 -0600380 }
381
Jens Axboe811826b2011-10-24 09:11:50 +0200382 if (connect(fd, addr, socklen) < 0) {
Jens Axboe25095e12012-03-09 17:09:55 +0100383 int ret = -errno;
384
Jens Axboe132159a2011-09-30 15:01:32 -0600385 log_err("fio: connect: %s\n", strerror(errno));
Jens Axboea7de0a12011-10-07 12:55:14 +0200386 log_err("fio: failed to connect to %s:%u\n", client->hostname,
387 client->port);
Jens Axboeb94cba42011-10-06 21:27:10 +0200388 close(fd);
Jens Axboe25095e12012-03-09 17:09:55 +0100389 return ret;
Jens Axboe132159a2011-09-30 15:01:32 -0600390 }
391
Jens Axboe87aa8f12011-10-06 21:24:13 +0200392 return fd;
393}
394
395static int fio_client_connect_sock(struct fio_client *client)
396{
397 struct sockaddr_un *addr = &client->addr_un;
Jens Axboe67bf9822013-01-10 11:23:19 +0100398 socklen_t len;
Jens Axboe87aa8f12011-10-06 21:24:13 +0200399 int fd;
400
401 memset(addr, 0, sizeof(*addr));
402 addr->sun_family = AF_UNIX;
Jens Axboe1cb96412014-04-14 08:50:33 -0600403 strncpy(addr->sun_path, client->hostname, sizeof(addr->sun_path) - 1);
Jens Axboe87aa8f12011-10-06 21:24:13 +0200404
405 fd = socket(AF_UNIX, SOCK_STREAM, 0);
406 if (fd < 0) {
Jens Axboe25095e12012-03-09 17:09:55 +0100407 int ret = -errno;
408
Jens Axboe87aa8f12011-10-06 21:24:13 +0200409 log_err("fio: socket: %s\n", strerror(errno));
Jens Axboe25095e12012-03-09 17:09:55 +0100410 return ret;
Jens Axboe87aa8f12011-10-06 21:24:13 +0200411 }
412
413 len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
414 if (connect(fd, (struct sockaddr *) addr, len) < 0) {
Jens Axboe25095e12012-03-09 17:09:55 +0100415 int ret = -errno;
416
Jens Axboe87aa8f12011-10-06 21:24:13 +0200417 log_err("fio: connect; %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200418 close(fd);
Jens Axboe25095e12012-03-09 17:09:55 +0100419 return ret;
Jens Axboe87aa8f12011-10-06 21:24:13 +0200420 }
421
422 return fd;
423}
424
Jens Axboe2f99deb2012-03-09 14:37:29 +0100425int fio_client_connect(struct fio_client *client)
Jens Axboe87aa8f12011-10-06 21:24:13 +0200426{
427 int fd;
428
429 dprint(FD_NET, "client: connect to host %s\n", client->hostname);
430
Jens Axboe87aa8f12011-10-06 21:24:13 +0200431 if (client->is_sock)
432 fd = fio_client_connect_sock(client);
433 else
434 fd = fio_client_connect_ip(client);
435
Jens Axboe89c17072011-10-11 10:15:51 +0200436 dprint(FD_NET, "client: %s connected %d\n", client->hostname, fd);
437
Jens Axboe87aa8f12011-10-06 21:24:13 +0200438 if (fd < 0)
Jens Axboea1a3ed52012-03-09 17:00:01 +0100439 return fd;
Jens Axboe87aa8f12011-10-06 21:24:13 +0200440
Jens Axboeb66570d2011-10-01 11:11:35 -0600441 client->fd = fd;
Jens Axboebebe6392011-10-07 10:00:51 +0200442 fio_client_add_hash(client);
Jens Axboe81179ee2011-10-04 12:42:06 +0200443 client->state = Client_connected;
Jens Axboed824c3b2012-03-09 17:45:43 +0100444
445 probe_client(client);
Jens Axboe132159a2011-09-30 15:01:32 -0600446 return 0;
447}
448
Jens Axboe0cf3ece2012-03-21 10:15:20 +0100449int fio_client_terminate(struct fio_client *client)
Jens Axboe2f99deb2012-03-09 14:37:29 +0100450{
Jens Axboe0cf3ece2012-03-21 10:15:20 +0100451 return fio_net_send_quit(client->fd);
Jens Axboe2f99deb2012-03-09 14:37:29 +0100452}
453
Jens Axboecc0df002011-10-03 20:53:32 +0200454void fio_clients_terminate(void)
455{
456 struct flist_head *entry;
457 struct fio_client *client;
458
Jens Axboe60efd142011-10-04 13:30:11 +0200459 dprint(FD_NET, "client: terminate clients\n");
460
Jens Axboecc0df002011-10-03 20:53:32 +0200461 flist_for_each(entry, &client_list) {
462 client = flist_entry(entry, struct fio_client, list);
Jens Axboe2f99deb2012-03-09 14:37:29 +0100463 fio_client_terminate(client);
Jens Axboecc0df002011-10-03 20:53:32 +0200464 }
465}
466
467static void sig_int(int sig)
468{
Jens Axboebebe6392011-10-07 10:00:51 +0200469 dprint(FD_NET, "client: got signal %d\n", sig);
Jens Axboecc0df002011-10-03 20:53:32 +0200470 fio_clients_terminate();
471}
472
Jens Axboeb852e7c2012-03-30 10:30:35 +0200473static void sig_show_status(int sig)
474{
475 show_running_run_stats();
476}
477
Jens Axboecc0df002011-10-03 20:53:32 +0200478static void client_signal_handler(void)
479{
480 struct sigaction act;
481
482 memset(&act, 0, sizeof(act));
483 act.sa_handler = sig_int;
484 act.sa_flags = SA_RESTART;
485 sigaction(SIGINT, &act, NULL);
486
487 memset(&act, 0, sizeof(act));
488 act.sa_handler = sig_int;
489 act.sa_flags = SA_RESTART;
490 sigaction(SIGTERM, &act, NULL);
Jens Axboeb852e7c2012-03-30 10:30:35 +0200491
Bruce Cran2f694502012-10-10 16:34:13 +0100492/* Windows uses SIGBREAK as a quit signal from other applications */
493#ifdef WIN32
494 memset(&act, 0, sizeof(act));
495 act.sa_handler = sig_int;
496 act.sa_flags = SA_RESTART;
497 sigaction(SIGBREAK, &act, NULL);
498#endif
499
Jens Axboeb852e7c2012-03-30 10:30:35 +0200500 memset(&act, 0, sizeof(act));
501 act.sa_handler = sig_show_status;
502 act.sa_flags = SA_RESTART;
503 sigaction(SIGUSR1, &act, NULL);
Jens Axboecc0df002011-10-03 20:53:32 +0200504}
505
Jens Axboe81179ee2011-10-04 12:42:06 +0200506static int send_client_cmd_line(struct fio_client *client)
507{
Jens Axboefa2ea802011-10-08 21:07:29 +0200508 struct cmd_single_line_pdu *cslp;
509 struct cmd_line_pdu *clp;
510 unsigned long offset;
Jens Axboe7f868312011-10-10 09:55:21 +0200511 unsigned int *lens;
Jens Axboefa2ea802011-10-08 21:07:29 +0200512 void *pdu;
513 size_t mem;
Jens Axboe81179ee2011-10-04 12:42:06 +0200514 int i, ret;
515
Jens Axboe39e8e012011-10-04 13:46:08 +0200516 dprint(FD_NET, "client: send cmdline %d\n", client->argc);
Jens Axboe60efd142011-10-04 13:30:11 +0200517
Jens Axboe7f868312011-10-10 09:55:21 +0200518 lens = malloc(client->argc * sizeof(unsigned int));
519
Jens Axboefa2ea802011-10-08 21:07:29 +0200520 /*
521 * Find out how much mem we need
522 */
Jens Axboe7f868312011-10-10 09:55:21 +0200523 for (i = 0, mem = 0; i < client->argc; i++) {
524 lens[i] = strlen(client->argv[i]) + 1;
525 mem += lens[i];
526 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200527
Jens Axboefa2ea802011-10-08 21:07:29 +0200528 /*
529 * We need one cmd_line_pdu, and argc number of cmd_single_line_pdu
530 */
531 mem += sizeof(*clp) + (client->argc * sizeof(*cslp));
532
533 pdu = malloc(mem);
534 clp = pdu;
535 offset = sizeof(*clp);
536
537 for (i = 0; i < client->argc; i++) {
Jens Axboe7f868312011-10-10 09:55:21 +0200538 uint16_t arg_len = lens[i];
Jens Axboefa2ea802011-10-08 21:07:29 +0200539
540 cslp = pdu + offset;
541 strcpy((char *) cslp->text, client->argv[i]);
542 cslp->len = cpu_to_le16(arg_len);
543 offset += sizeof(*cslp) + arg_len;
544 }
545
Jens Axboe7f868312011-10-10 09:55:21 +0200546 free(lens);
Jens Axboefa2ea802011-10-08 21:07:29 +0200547 clp->lines = cpu_to_le16(client->argc);
Jens Axboe46bcd492012-03-14 11:31:21 +0100548 clp->client_type = __cpu_to_le16(client->type);
Jens Axboe40c60512012-03-27 16:03:04 +0200549 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOBLINE, pdu, mem, NULL, NULL);
Jens Axboe81179ee2011-10-04 12:42:06 +0200550 free(pdu);
551 return ret;
552}
553
Jens Axboea37f69b2011-10-01 12:24:21 -0600554int fio_clients_connect(void)
555{
556 struct fio_client *client;
557 struct flist_head *entry, *tmp;
558 int ret;
559
Bruce Cran93bcfd22012-02-20 20:18:19 +0100560#ifdef WIN32
561 WSADATA wsd;
Jens Axboe3c3ed072012-03-27 09:12:39 +0200562 WSAStartup(MAKEWORD(2, 2), &wsd);
Bruce Cran93bcfd22012-02-20 20:18:19 +0100563#endif
564
Jens Axboe60efd142011-10-04 13:30:11 +0200565 dprint(FD_NET, "client: connect all\n");
566
Jens Axboecc0df002011-10-03 20:53:32 +0200567 client_signal_handler();
568
Jens Axboea37f69b2011-10-01 12:24:21 -0600569 flist_for_each_safe(entry, tmp, &client_list) {
570 client = flist_entry(entry, struct fio_client, list);
571
572 ret = fio_client_connect(client);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200573 if (ret) {
Jens Axboea37f69b2011-10-01 12:24:21 -0600574 remove_client(client);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200575 continue;
576 }
577
Jens Axboe81179ee2011-10-04 12:42:06 +0200578 if (client->argc > 1)
579 send_client_cmd_line(client);
Jens Axboea37f69b2011-10-01 12:24:21 -0600580 }
581
582 return !nr_clients;
583}
584
Jens Axboeb9d2f302012-03-08 20:36:28 +0100585int fio_start_client(struct fio_client *client)
586{
587 dprint(FD_NET, "client: start %s\n", client->hostname);
588 return fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_RUN, 0, NULL);
589}
590
591int fio_start_all_clients(void)
592{
593 struct fio_client *client;
594 struct flist_head *entry, *tmp;
595 int ret;
596
597 dprint(FD_NET, "client: start all\n");
598
Castor Fu952b05e2013-10-31 11:00:34 -0600599 fio_client_json_init();
600
Jens Axboeb9d2f302012-03-08 20:36:28 +0100601 flist_for_each_safe(entry, tmp, &client_list) {
602 client = flist_entry(entry, struct fio_client, list);
603
604 ret = fio_start_client(client);
605 if (ret) {
606 remove_client(client);
607 continue;
608 }
609 }
610
611 return flist_empty(&client_list);
612}
613
Jens Axboe35899182014-10-07 20:56:28 -0600614static int __fio_client_send_remote_ini(struct fio_client *client,
615 const char *filename)
616{
617 struct cmd_load_file_pdu *pdu;
618 size_t p_size;
619 int ret;
620
621 dprint(FD_NET, "send remote ini %s to %s\n", filename, client->hostname);
622
623 p_size = sizeof(*pdu) + strlen(filename);
624 pdu = malloc(p_size);
625 pdu->name_len = strlen(filename);
626 strcpy((char *) pdu->file, filename);
627 pdu->client_type = cpu_to_le16((uint16_t) client->type);
628
629 client->sent_job = 1;
630 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_LOAD_FILE, pdu, p_size,NULL, NULL);
631 free(pdu);
632 return ret;
633}
634
Jens Axboe132159a2011-09-30 15:01:32 -0600635/*
636 * Send file contents to server backend. We could use sendfile(), but to remain
637 * more portable lets just read/write the darn thing.
638 */
Jens Axboe35899182014-10-07 20:56:28 -0600639static int __fio_client_send_local_ini(struct fio_client *client,
640 const char *filename)
Jens Axboe132159a2011-09-30 15:01:32 -0600641{
Jens Axboe46bcd492012-03-14 11:31:21 +0100642 struct cmd_job_pdu *pdu;
643 size_t p_size;
Jens Axboe132159a2011-09-30 15:01:32 -0600644 struct stat sb;
Jens Axboe46bcd492012-03-14 11:31:21 +0100645 char *p;
646 void *buf;
Jens Axboe132159a2011-09-30 15:01:32 -0600647 off_t len;
648 int fd, ret;
649
Jens Axboe46c48f12011-10-01 12:50:51 -0600650 dprint(FD_NET, "send ini %s to %s\n", filename, client->hostname);
651
Jens Axboe132159a2011-09-30 15:01:32 -0600652 fd = open(filename, O_RDONLY);
653 if (fd < 0) {
Jens Axboe25095e12012-03-09 17:09:55 +0100654 int ret = -errno;
655
Jens Axboee951bdc2011-10-05 21:58:45 +0200656 log_err("fio: job file <%s> open: %s\n", filename, strerror(errno));
Jens Axboe25095e12012-03-09 17:09:55 +0100657 return ret;
Jens Axboe132159a2011-09-30 15:01:32 -0600658 }
659
660 if (fstat(fd, &sb) < 0) {
Jens Axboe25095e12012-03-09 17:09:55 +0100661 int ret = -errno;
662
Jens Axboe132159a2011-09-30 15:01:32 -0600663 log_err("fio: job file stat: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200664 close(fd);
Jens Axboe25095e12012-03-09 17:09:55 +0100665 return ret;
Jens Axboe132159a2011-09-30 15:01:32 -0600666 }
667
Jens Axboe46bcd492012-03-14 11:31:21 +0100668 p_size = sb.st_size + sizeof(*pdu);
669 pdu = malloc(p_size);
670 buf = pdu->buf;
Jens Axboe132159a2011-09-30 15:01:32 -0600671
672 len = sb.st_size;
673 p = buf;
674 do {
675 ret = read(fd, p, len);
676 if (ret > 0) {
677 len -= ret;
678 if (!len)
679 break;
680 p += ret;
681 continue;
682 } else if (!ret)
683 break;
684 else if (errno == EAGAIN || errno == EINTR)
685 continue;
686 } while (1);
687
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200688 if (len) {
689 log_err("fio: failed reading job file %s\n", filename);
Jens Axboeb94cba42011-10-06 21:27:10 +0200690 close(fd);
Hong Zhiguo03a22d92013-10-16 08:35:12 -0600691 free(pdu);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200692 return 1;
693 }
694
Jens Axboe46bcd492012-03-14 11:31:21 +0100695 pdu->buf_len = __cpu_to_le32(sb.st_size);
696 pdu->client_type = cpu_to_le32(client->type);
697
Jens Axboec2cb6862012-02-23 20:56:12 +0100698 client->sent_job = 1;
Jens Axboe40c60512012-03-27 16:03:04 +0200699 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, pdu, p_size, NULL, NULL);
Jens Axboe46bcd492012-03-14 11:31:21 +0100700 free(pdu);
Jens Axboeb94cba42011-10-06 21:27:10 +0200701 close(fd);
Jens Axboe132159a2011-09-30 15:01:32 -0600702 return ret;
703}
Jens Axboe37db14f2011-09-30 17:00:42 -0600704
Jens Axboe35899182014-10-07 20:56:28 -0600705int fio_client_send_ini(struct fio_client *client, const char *filename,
706 int remote)
Jens Axboe9988ca72012-03-09 15:14:06 +0100707{
Jens Axboe25095e12012-03-09 17:09:55 +0100708 int ret;
709
Jens Axboe35899182014-10-07 20:56:28 -0600710 if (!remote)
711 ret = __fio_client_send_local_ini(client, filename);
712 else
713 ret = __fio_client_send_remote_ini(client, filename);
714
Jens Axboe3af45202012-03-13 09:59:53 +0100715 if (!ret)
716 client->sent_job = 1;
Jens Axboe9988ca72012-03-09 15:14:06 +0100717
Jens Axboe25095e12012-03-09 17:09:55 +0100718 return ret;
Jens Axboe9988ca72012-03-09 15:14:06 +0100719}
720
Jens Axboe35899182014-10-07 20:56:28 -0600721static int fio_client_send_cf(struct fio_client *client,
722 struct client_file *cf)
723{
724 return fio_client_send_ini(client, cf->file, cf->remote);
725}
726
Jens Axboea37f69b2011-10-01 12:24:21 -0600727int fio_clients_send_ini(const char *filename)
728{
729 struct fio_client *client;
730 struct flist_head *entry, *tmp;
731
732 flist_for_each_safe(entry, tmp, &client_list) {
733 client = flist_entry(entry, struct fio_client, list);
734
Jens Axboe35899182014-10-07 20:56:28 -0600735 if (client->nr_files) {
Jens Axboe14ea90e2012-08-26 17:33:34 +0200736 int i;
737
Jens Axboe35899182014-10-07 20:56:28 -0600738 for (i = 0; i < client->nr_files; i++) {
739 struct client_file *cf;
Jens Axboe14ea90e2012-08-26 17:33:34 +0200740
Jens Axboe35899182014-10-07 20:56:28 -0600741 cf = &client->files[i];
742
743 if (fio_client_send_cf(client, cf)) {
Jens Axboe14ea90e2012-08-26 17:33:34 +0200744 remove_client(client);
745 break;
746 }
747 }
Jens Axboe35899182014-10-07 20:56:28 -0600748 }
749 if (client->sent_job)
750 continue;
751 if (!filename || fio_client_send_ini(client, filename, 0))
Jens Axboe3af45202012-03-13 09:59:53 +0100752 remove_client(client);
Jens Axboea37f69b2011-10-01 12:24:21 -0600753 }
754
755 return !nr_clients;
756}
757
Jens Axboe40c60512012-03-27 16:03:04 +0200758int fio_client_update_options(struct fio_client *client,
759 struct thread_options *o, uint64_t *tag)
760{
761 struct cmd_add_job_pdu pdu;
762
763 pdu.thread_number = cpu_to_le32(client->thread_number);
764 pdu.groupid = cpu_to_le32(client->groupid);
765 convert_thread_options_to_net(&pdu.top, o);
Castor Fu952b05e2013-10-31 11:00:34 -0600766
Jens Axboe40c60512012-03-27 16:03:04 +0200767 return fio_net_send_cmd(client->fd, FIO_NET_CMD_UPDATE_JOB, &pdu, sizeof(pdu), tag, &client->cmd_list);
768}
769
Jens Axboea64e88d2011-10-03 14:20:01 +0200770static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
771{
772 dst->max_val = le64_to_cpu(src->max_val);
773 dst->min_val = le64_to_cpu(src->min_val);
774 dst->samples = le64_to_cpu(src->samples);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200775
776 /*
777 * Floats arrive as IEEE 754 encoded uint64_t, convert back to double
778 */
779 dst->mean.u.f = fio_uint64_to_double(le64_to_cpu(dst->mean.u.i));
780 dst->S.u.f = fio_uint64_to_double(le64_to_cpu(dst->S.u.i));
Jens Axboea64e88d2011-10-03 14:20:01 +0200781}
782
783static void convert_ts(struct thread_stat *dst, struct thread_stat *src)
784{
785 int i, j;
786
Jens Axboe2f122b12012-03-15 13:10:19 +0100787 dst->error = le32_to_cpu(src->error);
788 dst->thread_number = le32_to_cpu(src->thread_number);
789 dst->groupid = le32_to_cpu(src->groupid);
790 dst->pid = le32_to_cpu(src->pid);
791 dst->members = le32_to_cpu(src->members);
Jens Axboe771e58b2013-01-30 12:56:23 +0100792 dst->unified_rw_rep = le32_to_cpu(src->unified_rw_rep);
Jens Axboea64e88d2011-10-03 14:20:01 +0200793
Jens Axboe298921d2012-09-24 18:47:01 +0200794 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
Jens Axboea64e88d2011-10-03 14:20:01 +0200795 convert_io_stat(&dst->clat_stat[i], &src->clat_stat[i]);
796 convert_io_stat(&dst->slat_stat[i], &src->slat_stat[i]);
797 convert_io_stat(&dst->lat_stat[i], &src->lat_stat[i]);
798 convert_io_stat(&dst->bw_stat[i], &src->bw_stat[i]);
799 }
800
801 dst->usr_time = le64_to_cpu(src->usr_time);
802 dst->sys_time = le64_to_cpu(src->sys_time);
803 dst->ctx = le64_to_cpu(src->ctx);
804 dst->minf = le64_to_cpu(src->minf);
805 dst->majf = le64_to_cpu(src->majf);
806 dst->clat_percentiles = le64_to_cpu(src->clat_percentiles);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200807
808 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
809 fio_fp64_t *fps = &src->percentile_list[i];
810 fio_fp64_t *fpd = &dst->percentile_list[i];
811
812 fpd->u.f = fio_uint64_to_double(le64_to_cpu(fps->u.i));
813 }
Jens Axboea64e88d2011-10-03 14:20:01 +0200814
815 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
816 dst->io_u_map[i] = le32_to_cpu(src->io_u_map[i]);
817 dst->io_u_submit[i] = le32_to_cpu(src->io_u_submit[i]);
818 dst->io_u_complete[i] = le32_to_cpu(src->io_u_complete[i]);
819 }
820
821 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
822 dst->io_u_lat_u[i] = le32_to_cpu(src->io_u_lat_u[i]);
823 dst->io_u_lat_m[i] = le32_to_cpu(src->io_u_lat_m[i]);
824 }
825
Jens Axboe298921d2012-09-24 18:47:01 +0200826 for (i = 0; i < DDIR_RWDIR_CNT; i++)
Jens Axboea64e88d2011-10-03 14:20:01 +0200827 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
828 dst->io_u_plat[i][j] = le32_to_cpu(src->io_u_plat[i][j]);
829
Jens Axboe78799de2013-01-29 20:53:52 +0100830 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
Jens Axboea64e88d2011-10-03 14:20:01 +0200831 dst->total_io_u[i] = le64_to_cpu(src->total_io_u[i]);
Jens Axboe93eee042011-10-03 21:08:48 +0200832 dst->short_io_u[i] = le64_to_cpu(src->short_io_u[i]);
Jens Axboea64e88d2011-10-03 14:20:01 +0200833 }
834
835 dst->total_submit = le64_to_cpu(src->total_submit);
836 dst->total_complete = le64_to_cpu(src->total_complete);
837
Jens Axboe298921d2012-09-24 18:47:01 +0200838 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
Jens Axboea64e88d2011-10-03 14:20:01 +0200839 dst->io_bytes[i] = le64_to_cpu(src->io_bytes[i]);
840 dst->runtime[i] = le64_to_cpu(src->runtime[i]);
841 }
842
843 dst->total_run_time = le64_to_cpu(src->total_run_time);
844 dst->continue_on_error = le16_to_cpu(src->continue_on_error);
845 dst->total_err_count = le64_to_cpu(src->total_err_count);
Jens Axboeddcc0b62011-10-03 14:45:27 +0200846 dst->first_error = le32_to_cpu(src->first_error);
847 dst->kb_base = le32_to_cpu(src->kb_base);
Steven Noonanad705bc2013-04-08 15:05:25 -0700848 dst->unit_base = le32_to_cpu(src->unit_base);
Jens Axboe3e260a42013-12-09 12:38:53 -0700849
850 dst->latency_depth = le32_to_cpu(src->latency_depth);
851 dst->latency_target = le64_to_cpu(src->latency_target);
852 dst->latency_window = le64_to_cpu(src->latency_window);
853 dst->latency_percentile.u.f = fio_uint64_to_double(le64_to_cpu(src->latency_percentile.u.i));
Jens Axboea64e88d2011-10-03 14:20:01 +0200854}
855
856static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
857{
858 int i;
859
Jens Axboe298921d2012-09-24 18:47:01 +0200860 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
Jens Axboea64e88d2011-10-03 14:20:01 +0200861 dst->max_run[i] = le64_to_cpu(src->max_run[i]);
862 dst->min_run[i] = le64_to_cpu(src->min_run[i]);
863 dst->max_bw[i] = le64_to_cpu(src->max_bw[i]);
864 dst->min_bw[i] = le64_to_cpu(src->min_bw[i]);
865 dst->io_kb[i] = le64_to_cpu(src->io_kb[i]);
866 dst->agg[i] = le64_to_cpu(src->agg[i]);
867 }
868
869 dst->kb_base = le32_to_cpu(src->kb_base);
Steven Noonanad705bc2013-04-08 15:05:25 -0700870 dst->unit_base = le32_to_cpu(src->unit_base);
Jens Axboea64e88d2011-10-03 14:20:01 +0200871 dst->groupid = le32_to_cpu(src->groupid);
Jens Axboe771e58b2013-01-30 12:56:23 +0100872 dst->unified_rw_rep = le32_to_cpu(src->unified_rw_rep);
Jens Axboea64e88d2011-10-03 14:20:01 +0200873}
874
Castor Fu952b05e2013-10-31 11:00:34 -0600875static void json_object_add_client_info(struct json_object *obj,
Castor Fu54bcda52014-08-22 17:16:44 -0500876 struct fio_client *client)
Castor Fu952b05e2013-10-31 11:00:34 -0600877{
Castor Fu54bcda52014-08-22 17:16:44 -0500878 const char *hostname = client->hostname ? client->hostname : "";
879
880 json_object_add_value_string(obj, "hostname", hostname);
Castor Fu952b05e2013-10-31 11:00:34 -0600881 json_object_add_value_int(obj, "port", client->port);
882}
883
Jens Axboe89e5fad2012-03-05 09:21:12 +0100884static void handle_ts(struct fio_client *client, struct fio_net_cmd *cmd)
Jens Axboea64e88d2011-10-03 14:20:01 +0200885{
886 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
Castor Fu952b05e2013-10-31 11:00:34 -0600887 struct json_object *tsobj;
Jens Axboea64e88d2011-10-03 14:20:01 +0200888
Castor Fu952b05e2013-10-31 11:00:34 -0600889 tsobj = show_thread_status(&p->ts, &p->rs);
Jens Axboe108fea72012-11-14 13:09:45 -0700890 client->did_stat = 1;
Castor Fu952b05e2013-10-31 11:00:34 -0600891 if (tsobj) {
892 json_object_add_client_info(tsobj, client);
893 json_array_add_value_object(clients_array, tsobj);
894 }
Jens Axboe37f0c1a2011-10-11 14:08:33 +0200895
Jens Axboe108fea72012-11-14 13:09:45 -0700896 if (!do_output_all_clients)
Jens Axboe37f0c1a2011-10-11 14:08:33 +0200897 return;
898
899 sum_thread_stats(&client_ts, &p->ts, sum_stat_nr);
900 sum_group_stats(&client_gs, &p->rs);
901
902 client_ts.members++;
Jens Axboe2f122b12012-03-15 13:10:19 +0100903 client_ts.thread_number = p->ts.thread_number;
Jens Axboe37f0c1a2011-10-11 14:08:33 +0200904 client_ts.groupid = p->ts.groupid;
Jens Axboe771e58b2013-01-30 12:56:23 +0100905 client_ts.unified_rw_rep = p->ts.unified_rw_rep;
Jens Axboe37f0c1a2011-10-11 14:08:33 +0200906
907 if (++sum_stat_nr == sum_stat_clients) {
908 strcpy(client_ts.name, "All clients");
Castor Fu952b05e2013-10-31 11:00:34 -0600909 tsobj = show_thread_status(&client_ts, &client_gs);
910 if (tsobj) {
911 json_object_add_client_info(tsobj, client);
912 json_array_add_value_object(clients_array, tsobj);
913 }
Jens Axboe37f0c1a2011-10-11 14:08:33 +0200914 }
Jens Axboea64e88d2011-10-03 14:20:01 +0200915}
916
Jens Axboe89e5fad2012-03-05 09:21:12 +0100917static void handle_gs(struct fio_client *client, struct fio_net_cmd *cmd)
Jens Axboea64e88d2011-10-03 14:20:01 +0200918{
919 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
920
Jens Axboea64e88d2011-10-03 14:20:01 +0200921 show_group_stats(gs);
922}
923
Jens Axboe3bf236c2012-03-03 20:35:50 +0100924static void handle_text(struct fio_client *client, struct fio_net_cmd *cmd)
925{
926 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
927 const char *buf = (const char *) pdu->buf;
928 const char *name;
929 int fio_unused ret;
930
931 name = client->name ? client->name : client->hostname;
932
933 if (!client->skip_newline)
934 fprintf(f_out, "<%s> ", name);
935 ret = fwrite(buf, pdu->buf_len, 1, f_out);
936 fflush(f_out);
937 client->skip_newline = strchr(buf, '\n') == NULL;
938}
939
Jens Axboed09a64a2011-10-13 11:38:56 +0200940static void convert_agg(struct disk_util_agg *agg)
941{
942 int i;
943
944 for (i = 0; i < 2; i++) {
945 agg->ios[i] = le32_to_cpu(agg->ios[i]);
946 agg->merges[i] = le32_to_cpu(agg->merges[i]);
947 agg->sectors[i] = le64_to_cpu(agg->sectors[i]);
948 agg->ticks[i] = le32_to_cpu(agg->ticks[i]);
949 }
950
951 agg->io_ticks = le32_to_cpu(agg->io_ticks);
952 agg->time_in_queue = le32_to_cpu(agg->time_in_queue);
953 agg->slavecount = le32_to_cpu(agg->slavecount);
Anton Blanchard823ba542011-11-07 14:16:26 +0100954 agg->max_util.u.f = fio_uint64_to_double(__le64_to_cpu(agg->max_util.u.i));
Jens Axboed09a64a2011-10-13 11:38:56 +0200955}
956
957static void convert_dus(struct disk_util_stat *dus)
958{
959 int i;
960
961 for (i = 0; i < 2; i++) {
Jens Axboea3b4cf72014-04-11 12:17:53 -0600962 dus->s.ios[i] = le32_to_cpu(dus->s.ios[i]);
963 dus->s.merges[i] = le32_to_cpu(dus->s.merges[i]);
964 dus->s.sectors[i] = le64_to_cpu(dus->s.sectors[i]);
965 dus->s.ticks[i] = le32_to_cpu(dus->s.ticks[i]);
Jens Axboed09a64a2011-10-13 11:38:56 +0200966 }
967
Jens Axboea3b4cf72014-04-11 12:17:53 -0600968 dus->s.io_ticks = le32_to_cpu(dus->s.io_ticks);
969 dus->s.time_in_queue = le32_to_cpu(dus->s.time_in_queue);
970 dus->s.msec = le64_to_cpu(dus->s.msec);
Jens Axboed09a64a2011-10-13 11:38:56 +0200971}
972
973static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd)
974{
975 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
976
Jens Axboed09a64a2011-10-13 11:38:56 +0200977 if (!client->disk_stats_shown) {
978 client->disk_stats_shown = 1;
979 log_info("\nDisk stats (read/write):\n");
980 }
981
Castor Fu952b05e2013-10-31 11:00:34 -0600982 if (output_format == FIO_OUTPUT_JSON) {
983 struct json_object *duobj;
984 json_array_add_disk_util(&du->dus, &du->agg, du_array);
985 duobj = json_array_last_value_object(du_array);
986 json_object_add_client_info(duobj, client);
987 } else
988 print_disk_util(&du->dus, &du->agg, output_format == FIO_OUTPUT_TERSE);
Jens Axboed09a64a2011-10-13 11:38:56 +0200989}
990
Jens Axboe3bf236c2012-03-03 20:35:50 +0100991static void convert_jobs_eta(struct jobs_eta *je)
Jens Axboecf451d12011-10-03 16:48:30 +0200992{
Jens Axboecf451d12011-10-03 16:48:30 +0200993 int i;
994
995 je->nr_running = le32_to_cpu(je->nr_running);
996 je->nr_ramp = le32_to_cpu(je->nr_ramp);
997 je->nr_pending = le32_to_cpu(je->nr_pending);
Jens Axboe714e85f2013-04-15 10:21:56 +0200998 je->nr_setting_up = le32_to_cpu(je->nr_setting_up);
Jens Axboecf451d12011-10-03 16:48:30 +0200999 je->files_open = le32_to_cpu(je->files_open);
Jens Axboecf451d12011-10-03 16:48:30 +02001000
Jens Axboe298921d2012-09-24 18:47:01 +02001001 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1002 je->m_rate[i] = le32_to_cpu(je->m_rate[i]);
1003 je->t_rate[i] = le32_to_cpu(je->t_rate[i]);
1004 je->m_iops[i] = le32_to_cpu(je->m_iops[i]);
1005 je->t_iops[i] = le32_to_cpu(je->t_iops[i]);
Jens Axboe16725bb2013-04-11 12:43:05 +02001006 je->rate[i] = le32_to_cpu(je->rate[i]);
1007 je->iops[i] = le32_to_cpu(je->iops[i]);
Jens Axboecf451d12011-10-03 16:48:30 +02001008 }
1009
Jens Axboeb51eedb2011-10-09 12:13:39 +02001010 je->elapsed_sec = le64_to_cpu(je->elapsed_sec);
Jens Axboecf451d12011-10-03 16:48:30 +02001011 je->eta_sec = le64_to_cpu(je->eta_sec);
Jens Axboe8c621fb2012-03-08 12:30:48 +01001012 je->nr_threads = le32_to_cpu(je->nr_threads);
Jens Axboeb7f05eb2012-05-11 20:33:02 +02001013 je->is_pow2 = le32_to_cpu(je->is_pow2);
Jens Axboeed2c0a12013-04-11 12:55:16 +02001014 je->unit_base = le32_to_cpu(je->unit_base);
Jens Axboe48fbb462011-10-09 12:19:08 +02001015}
Jens Axboecf451d12011-10-03 16:48:30 +02001016
Jens Axboe3e47bd22012-02-29 13:45:02 +01001017void fio_client_sum_jobs_eta(struct jobs_eta *dst, struct jobs_eta *je)
Jens Axboe48fbb462011-10-09 12:19:08 +02001018{
Jens Axboe48fbb462011-10-09 12:19:08 +02001019 int i;
1020
1021 dst->nr_running += je->nr_running;
1022 dst->nr_ramp += je->nr_ramp;
1023 dst->nr_pending += je->nr_pending;
Jens Axboe714e85f2013-04-15 10:21:56 +02001024 dst->nr_setting_up += je->nr_setting_up;
Jens Axboe48fbb462011-10-09 12:19:08 +02001025 dst->files_open += je->files_open;
Jens Axboe48fbb462011-10-09 12:19:08 +02001026
Jens Axboe298921d2012-09-24 18:47:01 +02001027 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
Jens Axboe3e47bd22012-02-29 13:45:02 +01001028 dst->m_rate[i] += je->m_rate[i];
1029 dst->t_rate[i] += je->t_rate[i];
1030 dst->m_iops[i] += je->m_iops[i];
1031 dst->t_iops[i] += je->t_iops[i];
Jens Axboe16725bb2013-04-11 12:43:05 +02001032 dst->rate[i] += je->rate[i];
1033 dst->iops[i] += je->iops[i];
Jens Axboe48fbb462011-10-09 12:19:08 +02001034 }
1035
1036 dst->elapsed_sec += je->elapsed_sec;
1037
1038 if (je->eta_sec > dst->eta_sec)
1039 dst->eta_sec = je->eta_sec;
Jens Axboe8c621fb2012-03-08 12:30:48 +01001040
1041 dst->nr_threads += je->nr_threads;
Jens Axboe27b91552014-06-27 15:30:06 -06001042
1043 /*
1044 * This wont be correct for multiple strings, but at least it
1045 * works for the basic cases.
1046 */
1047 strcpy((char *) dst->run_str, (char *) je->run_str);
Jens Axboe48fbb462011-10-09 12:19:08 +02001048}
1049
Jens Axboea5276612012-03-04 15:15:08 +01001050void fio_client_dec_jobs_eta(struct client_eta *eta, client_eta_op eta_fn)
Jens Axboe82c1ed32011-10-10 08:56:18 +02001051{
1052 if (!--eta->pending) {
Jens Axboea5276612012-03-04 15:15:08 +01001053 eta_fn(&eta->eta);
Jens Axboe82c1ed32011-10-10 08:56:18 +02001054 free(eta);
1055 }
1056}
1057
Jens Axboe89c17072011-10-11 10:15:51 +02001058static void remove_reply_cmd(struct fio_client *client, struct fio_net_cmd *cmd)
1059{
Jens Axboe40c60512012-03-27 16:03:04 +02001060 struct fio_net_cmd_reply *reply = NULL;
Jens Axboe89c17072011-10-11 10:15:51 +02001061 struct flist_head *entry;
1062
1063 flist_for_each(entry, &client->cmd_list) {
Jens Axboe40c60512012-03-27 16:03:04 +02001064 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
Jens Axboe89c17072011-10-11 10:15:51 +02001065
Jens Axboe40c60512012-03-27 16:03:04 +02001066 if (cmd->tag == (uintptr_t) reply)
Jens Axboe89c17072011-10-11 10:15:51 +02001067 break;
1068
Jens Axboe40c60512012-03-27 16:03:04 +02001069 reply = NULL;
Jens Axboe89c17072011-10-11 10:15:51 +02001070 }
1071
Jens Axboe40c60512012-03-27 16:03:04 +02001072 if (!reply) {
Jens Axboe4e0a8fa2013-04-15 11:40:57 +02001073 log_err("fio: client: unable to find matching tag (%llx)\n", (unsigned long long) cmd->tag);
Jens Axboe89c17072011-10-11 10:15:51 +02001074 return;
1075 }
1076
Jens Axboe40c60512012-03-27 16:03:04 +02001077 flist_del(&reply->list);
1078 cmd->tag = reply->saved_tag;
1079 free(reply);
1080}
1081
1082int fio_client_wait_for_reply(struct fio_client *client, uint64_t tag)
1083{
1084 do {
1085 struct fio_net_cmd_reply *reply = NULL;
1086 struct flist_head *entry;
1087
1088 flist_for_each(entry, &client->cmd_list) {
1089 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
1090
1091 if (tag == (uintptr_t) reply)
1092 break;
1093
1094 reply = NULL;
1095 }
1096
1097 if (!reply)
1098 break;
1099
1100 usleep(1000);
1101 } while (1);
1102
1103 return 0;
Jens Axboe89c17072011-10-11 10:15:51 +02001104}
1105
Jens Axboe82c1ed32011-10-10 08:56:18 +02001106static void handle_eta(struct fio_client *client, struct fio_net_cmd *cmd)
Jens Axboe48fbb462011-10-09 12:19:08 +02001107{
1108 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
Jens Axboedf380932011-10-11 14:25:08 +02001109 struct client_eta *eta = (struct client_eta *) (uintptr_t) cmd->tag;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001110
1111 dprint(FD_NET, "client: got eta tag %p, %d\n", eta, eta->pending);
Jens Axboe48fbb462011-10-09 12:19:08 +02001112
Jens Axboef77d2672011-10-10 14:36:07 +02001113 assert(client->eta_in_flight == eta);
1114
1115 client->eta_in_flight = NULL;
Jens Axboe82c1ed32011-10-10 08:56:18 +02001116 flist_del_init(&client->eta_list);
1117
Jens Axboe2f99deb2012-03-09 14:37:29 +01001118 if (client->ops->jobs_eta)
1119 client->ops->jobs_eta(client, je);
1120
Jens Axboe3e47bd22012-02-29 13:45:02 +01001121 fio_client_sum_jobs_eta(&eta->eta, je);
Jens Axboea5276612012-03-04 15:15:08 +01001122 fio_client_dec_jobs_eta(eta, client->ops->eta);
Jens Axboecf451d12011-10-03 16:48:30 +02001123}
1124
Jens Axboeb5296dd2011-10-07 16:35:56 +02001125static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd)
Jens Axboe2e03b4b2011-10-04 09:00:40 +02001126{
Jens Axboe3989b142013-04-12 13:13:24 +02001127 struct cmd_probe_reply_pdu *probe = (struct cmd_probe_reply_pdu *) cmd->payload;
Jens Axboed2333352011-10-11 15:07:23 +02001128 const char *os, *arch;
1129 char bit[16];
Jens Axboe2e03b4b2011-10-04 09:00:40 +02001130
Jens Axboecca84642011-10-07 12:47:57 +02001131 os = fio_get_os_string(probe->os);
1132 if (!os)
1133 os = "unknown";
1134
1135 arch = fio_get_arch_string(probe->arch);
1136 if (!arch)
1137 os = "unknown";
1138
Jens Axboed2333352011-10-11 15:07:23 +02001139 sprintf(bit, "%d-bit", probe->bpp * 8);
Jens Axboe3989b142013-04-12 13:13:24 +02001140 probe->flags = le64_to_cpu(probe->flags);
Jens Axboe38fdef22011-10-11 14:30:06 +02001141
Jens Axboe3989b142013-04-12 13:13:24 +02001142 log_info("hostname=%s, be=%u, %s, os=%s, arch=%s, fio=%s, flags=%lx\n",
Jens Axboe38fdef22011-10-11 14:30:06 +02001143 probe->hostname, probe->bigendian, bit, os, arch,
Jens Axboe3989b142013-04-12 13:13:24 +02001144 probe->fio_version, (unsigned long) probe->flags);
Jens Axboeb5296dd2011-10-07 16:35:56 +02001145
1146 if (!client->name)
1147 client->name = strdup((char *) probe->hostname);
Jens Axboe2e03b4b2011-10-04 09:00:40 +02001148}
1149
Jens Axboe11e950b2011-10-16 21:34:14 +02001150static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd)
1151{
1152 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
1153
1154 client->state = Client_started;
1155 client->jobs = le32_to_cpu(pdu->jobs);
Jens Axboe108fea72012-11-14 13:09:45 -07001156 client->nr_stat = le32_to_cpu(pdu->stat_outputs);
1157
1158 if (sum_stat_clients > 1)
1159 do_output_all_clients = 1;
1160
1161 sum_stat_clients += client->nr_stat;
Jens Axboe11e950b2011-10-16 21:34:14 +02001162}
1163
1164static void handle_stop(struct fio_client *client, struct fio_net_cmd *cmd)
1165{
Jens Axboe498c92c2011-10-17 09:14:42 +02001166 if (client->error)
1167 log_info("client <%s>: exited with error %d\n", client->hostname, client->error);
Jens Axboe11e950b2011-10-16 21:34:14 +02001168}
1169
Jens Axboe6b79c802012-03-08 10:51:36 +01001170static void convert_stop(struct fio_net_cmd *cmd)
1171{
1172 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
1173
1174 pdu->error = le32_to_cpu(pdu->error);
1175}
1176
Jens Axboe084d1c62012-03-03 20:28:07 +01001177static void convert_text(struct fio_net_cmd *cmd)
1178{
1179 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
1180
1181 pdu->level = le32_to_cpu(pdu->level);
1182 pdu->buf_len = le32_to_cpu(pdu->buf_len);
1183 pdu->log_sec = le64_to_cpu(pdu->log_sec);
1184 pdu->log_usec = le64_to_cpu(pdu->log_usec);
1185}
1186
Jens Axboe3989b142013-04-12 13:13:24 +02001187static struct cmd_iolog_pdu *convert_iolog_gz(struct fio_net_cmd *cmd,
1188 struct cmd_iolog_pdu *pdu)
Jens Axboe1b427252012-03-14 15:03:03 +01001189{
Jens Axboe3989b142013-04-12 13:13:24 +02001190#ifdef CONFIG_ZLIB
Jens Axboe1b427252012-03-14 15:03:03 +01001191 struct cmd_iolog_pdu *ret;
Jens Axboe1b427252012-03-14 15:03:03 +01001192 z_stream stream;
Jens Axboe3989b142013-04-12 13:13:24 +02001193 uint32_t nr_samples;
1194 size_t total;
Jens Axboe1b427252012-03-14 15:03:03 +01001195 void *p;
1196
1197 stream.zalloc = Z_NULL;
1198 stream.zfree = Z_NULL;
1199 stream.opaque = Z_NULL;
1200 stream.avail_in = 0;
1201 stream.next_in = Z_NULL;
1202
1203 if (inflateInit(&stream) != Z_OK)
1204 return NULL;
1205
1206 /*
Jens Axboef5ed7652012-03-14 16:28:07 +01001207 * Get header first, it's not compressed
Jens Axboe1b427252012-03-14 15:03:03 +01001208 */
Jens Axboef2b9a672014-07-01 08:47:11 -06001209 nr_samples = le64_to_cpu(pdu->nr_samples);
Jens Axboe1b427252012-03-14 15:03:03 +01001210
Jens Axboef2b9a672014-07-01 08:47:11 -06001211 total = nr_samples * __log_entry_sz(le32_to_cpu(pdu->log_offset));
Jens Axboef5ed7652012-03-14 16:28:07 +01001212 ret = malloc(total + sizeof(*pdu));
Jens Axboe1b427252012-03-14 15:03:03 +01001213 ret->nr_samples = nr_samples;
Jens Axboe3989b142013-04-12 13:13:24 +02001214
1215 memcpy(ret, pdu, sizeof(*pdu));
Jens Axboe1b427252012-03-14 15:03:03 +01001216
Jens Axboef5ed7652012-03-14 16:28:07 +01001217 p = (void *) ret + sizeof(*pdu);
1218
1219 stream.avail_in = cmd->pdu_len - sizeof(*pdu);
1220 stream.next_in = (void *) pdu + sizeof(*pdu);
Jens Axboe1b427252012-03-14 15:03:03 +01001221 while (stream.avail_in) {
1222 unsigned int this_chunk = 65536;
1223 unsigned int this_len;
1224 int err;
1225
1226 if (this_chunk > total)
1227 this_chunk = total;
1228
1229 stream.avail_out = this_chunk;
1230 stream.next_out = p;
1231 err = inflate(&stream, Z_NO_FLUSH);
Jens Axboe3c547fe2012-03-14 21:53:00 +01001232 /* may be Z_OK, or Z_STREAM_END */
1233 if (err < 0) {
Jens Axboe1b427252012-03-14 15:03:03 +01001234 log_err("fio: inflate error %d\n", err);
Jens Axboef5ed7652012-03-14 16:28:07 +01001235 free(ret);
1236 ret = NULL;
Jens Axboe3989b142013-04-12 13:13:24 +02001237 goto err;
Jens Axboe1b427252012-03-14 15:03:03 +01001238 }
1239
1240 this_len = this_chunk - stream.avail_out;
1241 p += this_len;
1242 total -= this_len;
1243 }
1244
Jens Axboe3989b142013-04-12 13:13:24 +02001245err:
1246 inflateEnd(&stream);
1247 return ret;
1248#else
1249 return NULL;
1250#endif
1251}
1252
1253/*
1254 * This has been compressed on the server side, since it can be big.
1255 * Uncompress here.
1256 */
1257static struct cmd_iolog_pdu *convert_iolog(struct fio_net_cmd *cmd)
1258{
1259 struct cmd_iolog_pdu *pdu = (struct cmd_iolog_pdu *) cmd->payload;
1260 struct cmd_iolog_pdu *ret;
Jens Axboeccefd5f2014-06-30 20:59:03 -06001261 uint64_t i;
1262 void *samples;
Jens Axboe3989b142013-04-12 13:13:24 +02001263
1264 /*
1265 * Convert if compressed and we support it. If it's not
1266 * compressed, we need not do anything.
1267 */
1268 if (le32_to_cpu(pdu->compressed)) {
1269#ifndef CONFIG_ZLIB
1270 log_err("fio: server sent compressed data by mistake\n");
1271 return NULL;
1272#endif
1273 ret = convert_iolog_gz(cmd, pdu);
1274 if (!ret) {
1275 log_err("fio: failed decompressing log\n");
1276 return NULL;
1277 }
1278 } else
1279 ret = pdu;
1280
Jens Axboeccefd5f2014-06-30 20:59:03 -06001281 ret->nr_samples = le64_to_cpu(ret->nr_samples);
Jens Axboe3989b142013-04-12 13:13:24 +02001282 ret->thread_number = le32_to_cpu(ret->thread_number);
Jens Axboe3989b142013-04-12 13:13:24 +02001283 ret->log_type = le32_to_cpu(ret->log_type);
1284 ret->compressed = le32_to_cpu(ret->compressed);
Jens Axboeccefd5f2014-06-30 20:59:03 -06001285 ret->log_offset = le32_to_cpu(ret->log_offset);
Jens Axboe3989b142013-04-12 13:13:24 +02001286
Jens Axboee8c4fb02014-07-01 16:07:59 -06001287 samples = &ret->samples[0];
Jens Axboef5ed7652012-03-14 16:28:07 +01001288 for (i = 0; i < ret->nr_samples; i++) {
Jens Axboeccefd5f2014-06-30 20:59:03 -06001289 struct io_sample *s;
Jens Axboef5ed7652012-03-14 16:28:07 +01001290
Jens Axboeccefd5f2014-06-30 20:59:03 -06001291 s = __get_sample(samples, ret->log_offset, i);
Jens Axboebac4af12014-07-03 13:42:28 -06001292 s->time = le64_to_cpu(s->time);
1293 s->val = le64_to_cpu(s->val);
1294 s->__ddir = le32_to_cpu(s->__ddir);
1295 s->bs = le32_to_cpu(s->bs);
Jens Axboeccefd5f2014-06-30 20:59:03 -06001296
1297 if (ret->log_offset) {
1298 struct io_sample_offset *so = (void *) s;
1299
1300 so->offset = le64_to_cpu(so->offset);
1301 }
Jens Axboef5ed7652012-03-14 16:28:07 +01001302 }
1303
Jens Axboe1b427252012-03-14 15:03:03 +01001304 return ret;
1305}
1306
Jens Axboea5276612012-03-04 15:15:08 +01001307int fio_handle_client(struct fio_client *client)
Jens Axboe37db14f2011-09-30 17:00:42 -06001308{
Jens Axboea5276612012-03-04 15:15:08 +01001309 struct client_ops *ops = client->ops;
Jens Axboe37db14f2011-09-30 17:00:42 -06001310 struct fio_net_cmd *cmd;
1311
Jens Axboe60efd142011-10-04 13:30:11 +02001312 dprint(FD_NET, "client: handle %s\n", client->hostname);
1313
Jens Axboee951bdc2011-10-05 21:58:45 +02001314 cmd = fio_net_recv_cmd(client->fd);
1315 if (!cmd)
1316 return 0;
Jens Axboec2c94582011-10-05 20:31:30 +02001317
Jens Axboeb9d2f302012-03-08 20:36:28 +01001318 dprint(FD_NET, "client: got cmd op %s from %s (pdu=%u)\n",
1319 fio_server_op(cmd->opcode), client->hostname, cmd->pdu_len);
Jens Axboe46c48f12011-10-01 12:50:51 -06001320
Jens Axboee951bdc2011-10-05 21:58:45 +02001321 switch (cmd->opcode) {
1322 case FIO_NET_CMD_QUIT:
Jens Axboe3ec62ec2012-03-01 12:01:29 +01001323 if (ops->quit)
Jens Axboe35c0ba72012-03-14 10:56:40 +01001324 ops->quit(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +02001325 remove_client(client);
1326 free(cmd);
1327 break;
Jens Axboe084d1c62012-03-03 20:28:07 +01001328 case FIO_NET_CMD_TEXT:
1329 convert_text(cmd);
Jens Axboe35c0ba72012-03-14 10:56:40 +01001330 ops->text(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +02001331 free(cmd);
1332 break;
Jens Axboe3bf236c2012-03-03 20:35:50 +01001333 case FIO_NET_CMD_DU: {
1334 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
1335
1336 convert_dus(&du->dus);
1337 convert_agg(&du->agg);
1338
Stephen M. Camerondd366722012-02-24 08:17:30 +01001339 ops->disk_util(client, cmd);
Jens Axboed09a64a2011-10-13 11:38:56 +02001340 free(cmd);
1341 break;
Jens Axboe3bf236c2012-03-03 20:35:50 +01001342 }
1343 case FIO_NET_CMD_TS: {
1344 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
1345
1346 convert_ts(&p->ts, &p->ts);
1347 convert_gs(&p->rs, &p->rs);
1348
Jens Axboe89e5fad2012-03-05 09:21:12 +01001349 ops->thread_status(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +02001350 free(cmd);
1351 break;
Jens Axboe3bf236c2012-03-03 20:35:50 +01001352 }
1353 case FIO_NET_CMD_GS: {
1354 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
1355
1356 convert_gs(gs, gs);
1357
Jens Axboe89e5fad2012-03-05 09:21:12 +01001358 ops->group_stats(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +02001359 free(cmd);
1360 break;
Jens Axboe3bf236c2012-03-03 20:35:50 +01001361 }
1362 case FIO_NET_CMD_ETA: {
1363 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
1364
Jens Axboe89c17072011-10-11 10:15:51 +02001365 remove_reply_cmd(client, cmd);
Jens Axboe3bf236c2012-03-03 20:35:50 +01001366 convert_jobs_eta(je);
Jens Axboea5276612012-03-04 15:15:08 +01001367 handle_eta(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +02001368 free(cmd);
1369 break;
Jens Axboe3bf236c2012-03-03 20:35:50 +01001370 }
Jens Axboee951bdc2011-10-05 21:58:45 +02001371 case FIO_NET_CMD_PROBE:
Jens Axboe89c17072011-10-11 10:15:51 +02001372 remove_reply_cmd(client, cmd);
Stephen M. Camerondd366722012-02-24 08:17:30 +01001373 ops->probe(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +02001374 free(cmd);
1375 break;
Jens Axboe5d7793a2012-03-08 19:59:16 +01001376 case FIO_NET_CMD_SERVER_START:
Jens Axboe01be0382011-10-15 14:43:41 +02001377 client->state = Client_running;
Jens Axboe85dd01e2012-03-12 14:33:16 +01001378 if (ops->job_start)
1379 ops->job_start(client, cmd);
Jens Axboe01be0382011-10-15 14:43:41 +02001380 free(cmd);
1381 break;
Jens Axboe85dd01e2012-03-12 14:33:16 +01001382 case FIO_NET_CMD_START: {
1383 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
1384
1385 pdu->jobs = le32_to_cpu(pdu->jobs);
1386 ops->start(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +02001387 free(cmd);
1388 break;
Jens Axboe85dd01e2012-03-12 14:33:16 +01001389 }
Jens Axboe6b79c802012-03-08 10:51:36 +01001390 case FIO_NET_CMD_STOP: {
1391 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
1392
1393 convert_stop(cmd);
1394 client->state = Client_stopped;
Jens Axboe122c7722012-03-19 09:13:15 +01001395 client->error = le32_to_cpu(pdu->error);
1396 client->signal = le32_to_cpu(pdu->signal);
Jens Axboe6b79c802012-03-08 10:51:36 +01001397 ops->stop(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +02001398 free(cmd);
1399 break;
Jens Axboe6b79c802012-03-08 10:51:36 +01001400 }
Jens Axboe40c60512012-03-27 16:03:04 +02001401 case FIO_NET_CMD_ADD_JOB: {
1402 struct cmd_add_job_pdu *pdu = (struct cmd_add_job_pdu *) cmd->payload;
1403
1404 client->thread_number = le32_to_cpu(pdu->thread_number);
1405 client->groupid = le32_to_cpu(pdu->groupid);
1406
Jens Axboe807f9972012-03-02 10:25:24 +01001407 if (ops->add_job)
1408 ops->add_job(client, cmd);
1409 free(cmd);
1410 break;
Jens Axboe40c60512012-03-27 16:03:04 +02001411 }
Jens Axboe1b427252012-03-14 15:03:03 +01001412 case FIO_NET_CMD_IOLOG:
1413 if (ops->iolog) {
1414 struct cmd_iolog_pdu *pdu;
1415
1416 pdu = convert_iolog(cmd);
1417 ops->iolog(client, pdu);
1418 }
1419 free(cmd);
1420 break;
Jens Axboe40c60512012-03-27 16:03:04 +02001421 case FIO_NET_CMD_UPDATE_JOB:
Jens Axboe40c60512012-03-27 16:03:04 +02001422 ops->update_job(client, cmd);
Jens Axboe649cee92012-03-28 09:15:32 +02001423 remove_reply_cmd(client, cmd);
Jens Axboe40c60512012-03-27 16:03:04 +02001424 free(cmd);
1425 break;
Jens Axboee951bdc2011-10-05 21:58:45 +02001426 default:
Jens Axboe89c17072011-10-11 10:15:51 +02001427 log_err("fio: unknown client op: %s\n", fio_server_op(cmd->opcode));
Jens Axboee951bdc2011-10-05 21:58:45 +02001428 free(cmd);
1429 break;
Jens Axboe37db14f2011-09-30 17:00:42 -06001430 }
1431
Jens Axboee951bdc2011-10-05 21:58:45 +02001432 return 1;
Jens Axboe37db14f2011-09-30 17:00:42 -06001433}
Jens Axboeb66570d2011-10-01 11:11:35 -06001434
Jens Axboea5276612012-03-04 15:15:08 +01001435static void request_client_etas(struct client_ops *ops)
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001436{
1437 struct fio_client *client;
1438 struct flist_head *entry;
1439 struct client_eta *eta;
Jens Axboe82c1ed32011-10-10 08:56:18 +02001440 int skipped = 0;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001441
1442 dprint(FD_NET, "client: request eta (%d)\n", nr_clients);
1443
Jens Axboe27b91552014-06-27 15:30:06 -06001444 eta = calloc(1, sizeof(*eta) + __THREAD_RUNSTR_SZ(REAL_MAX_JOBS));
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001445 eta->pending = nr_clients;
1446
1447 flist_for_each(entry, &client_list) {
1448 client = flist_entry(entry, struct fio_client, list);
1449
Jens Axboe82c1ed32011-10-10 08:56:18 +02001450 if (!flist_empty(&client->eta_list)) {
1451 skipped++;
1452 continue;
1453 }
Jens Axboe01be0382011-10-15 14:43:41 +02001454 if (client->state != Client_running)
1455 continue;
Jens Axboe82c1ed32011-10-10 08:56:18 +02001456
Jens Axboef77d2672011-10-10 14:36:07 +02001457 assert(!client->eta_in_flight);
Jens Axboe82c1ed32011-10-10 08:56:18 +02001458 flist_add_tail(&client->eta_list, &eta_list);
Jens Axboef77d2672011-10-10 14:36:07 +02001459 client->eta_in_flight = eta;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001460 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_SEND_ETA,
Jens Axboedf380932011-10-11 14:25:08 +02001461 (uintptr_t) eta, &client->cmd_list);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001462 }
1463
Jens Axboe82c1ed32011-10-10 08:56:18 +02001464 while (skipped--)
Jens Axboea5276612012-03-04 15:15:08 +01001465 fio_client_dec_jobs_eta(eta, ops->eta);
Jens Axboe82c1ed32011-10-10 08:56:18 +02001466
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001467 dprint(FD_NET, "client: requested eta tag %p\n", eta);
1468}
1469
Jens Axboe89c17072011-10-11 10:15:51 +02001470static int client_check_cmd_timeout(struct fio_client *client,
1471 struct timeval *now)
1472{
Jens Axboe40c60512012-03-27 16:03:04 +02001473 struct fio_net_cmd_reply *reply;
Jens Axboe89c17072011-10-11 10:15:51 +02001474 struct flist_head *entry, *tmp;
1475 int ret = 0;
1476
1477 flist_for_each_safe(entry, tmp, &client->cmd_list) {
Jens Axboe40c60512012-03-27 16:03:04 +02001478 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
Jens Axboe89c17072011-10-11 10:15:51 +02001479
Jens Axboe40c60512012-03-27 16:03:04 +02001480 if (mtime_since(&reply->tv, now) < FIO_NET_CLIENT_TIMEOUT)
Jens Axboe89c17072011-10-11 10:15:51 +02001481 continue;
1482
1483 log_err("fio: client %s, timeout on cmd %s\n", client->hostname,
Jens Axboe40c60512012-03-27 16:03:04 +02001484 fio_server_op(reply->opcode));
1485 flist_del(&reply->list);
1486 free(reply);
Jens Axboe89c17072011-10-11 10:15:51 +02001487 ret = 1;
1488 }
1489
1490 return flist_empty(&client->cmd_list) && ret;
1491}
1492
Jens Axboea5276612012-03-04 15:15:08 +01001493static int fio_check_clients_timed_out(void)
Jens Axboe89c17072011-10-11 10:15:51 +02001494{
1495 struct fio_client *client;
1496 struct flist_head *entry, *tmp;
1497 struct timeval tv;
1498 int ret = 0;
1499
Jens Axboe67bf9822013-01-10 11:23:19 +01001500 fio_gettime(&tv, NULL);
Jens Axboe89c17072011-10-11 10:15:51 +02001501
1502 flist_for_each_safe(entry, tmp, &client_list) {
1503 client = flist_entry(entry, struct fio_client, list);
1504
1505 if (flist_empty(&client->cmd_list))
1506 continue;
1507
1508 if (!client_check_cmd_timeout(client, &tv))
1509 continue;
1510
Jens Axboea5276612012-03-04 15:15:08 +01001511 if (client->ops->timed_out)
1512 client->ops->timed_out(client);
Jens Axboeed727a42012-03-02 12:14:40 +01001513 else
1514 log_err("fio: client %s timed out\n", client->hostname);
1515
Jens Axboe89c17072011-10-11 10:15:51 +02001516 remove_client(client);
1517 ret = 1;
1518 }
1519
1520 return ret;
1521}
1522
Stephen M. Camerondd366722012-02-24 08:17:30 +01001523int fio_handle_clients(struct client_ops *ops)
Jens Axboeb66570d2011-10-01 11:11:35 -06001524{
Jens Axboeb66570d2011-10-01 11:11:35 -06001525 struct pollfd *pfds;
Jens Axboe498c92c2011-10-17 09:14:42 +02001526 int i, ret = 0, retval = 0;
Jens Axboeb66570d2011-10-01 11:11:35 -06001527
Jens Axboe67bf9822013-01-10 11:23:19 +01001528 fio_gettime(&eta_tv, NULL);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001529
Jens Axboeb66570d2011-10-01 11:11:35 -06001530 pfds = malloc(nr_clients * sizeof(struct pollfd));
1531
Jens Axboe37f0c1a2011-10-11 14:08:33 +02001532 init_thread_stat(&client_ts);
1533 init_group_run_stat(&client_gs);
1534
Jens Axboeb66570d2011-10-01 11:11:35 -06001535 while (!exit_backend && nr_clients) {
Jens Axboec2cb6862012-02-23 20:56:12 +01001536 struct flist_head *entry, *tmp;
1537 struct fio_client *client;
1538
Jens Axboe82a4be12011-10-01 12:40:32 -06001539 i = 0;
Jens Axboec2cb6862012-02-23 20:56:12 +01001540 flist_for_each_safe(entry, tmp, &client_list) {
Jens Axboe82a4be12011-10-01 12:40:32 -06001541 client = flist_entry(entry, struct fio_client, list);
1542
Jens Axboea5276612012-03-04 15:15:08 +01001543 if (!client->sent_job && !client->ops->stay_connected &&
Jens Axboec2cb6862012-02-23 20:56:12 +01001544 flist_empty(&client->cmd_list)) {
1545 remove_client(client);
1546 continue;
1547 }
1548
Jens Axboe82a4be12011-10-01 12:40:32 -06001549 pfds[i].fd = client->fd;
1550 pfds[i].events = POLLIN;
1551 i++;
1552 }
1553
Jens Axboec2cb6862012-02-23 20:56:12 +01001554 if (!nr_clients)
1555 break;
1556
Jens Axboe82a4be12011-10-01 12:40:32 -06001557 assert(i == nr_clients);
1558
Jens Axboe5c2857f2011-10-04 13:14:32 +02001559 do {
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001560 struct timeval tv;
1561
Jens Axboe67bf9822013-01-10 11:23:19 +01001562 fio_gettime(&tv, NULL);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001563 if (mtime_since(&eta_tv, &tv) >= 900) {
Jens Axboea5276612012-03-04 15:15:08 +01001564 request_client_etas(ops);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001565 memcpy(&eta_tv, &tv, sizeof(tv));
Jens Axboe89c17072011-10-11 10:15:51 +02001566
Jens Axboea5276612012-03-04 15:15:08 +01001567 if (fio_check_clients_timed_out())
Jens Axboe89c17072011-10-11 10:15:51 +02001568 break;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001569 }
1570
Jens Axboe80102c82012-03-23 13:19:31 +01001571 ret = poll(pfds, nr_clients, ops->eta_msec);
Jens Axboe5c2857f2011-10-04 13:14:32 +02001572 if (ret < 0) {
1573 if (errno == EINTR)
1574 continue;
1575 log_err("fio: poll clients: %s\n", strerror(errno));
1576 break;
1577 } else if (!ret)
Jens Axboeb66570d2011-10-01 11:11:35 -06001578 continue;
Jens Axboe5c2857f2011-10-04 13:14:32 +02001579 } while (ret <= 0);
Jens Axboeb66570d2011-10-01 11:11:35 -06001580
1581 for (i = 0; i < nr_clients; i++) {
1582 if (!(pfds[i].revents & POLLIN))
1583 continue;
1584
1585 client = find_client_by_fd(pfds[i].fd);
1586 if (!client) {
Jens Axboe65e1a122013-08-30 10:13:36 -06001587 log_err("fio: unknown client fd %ld\n", (long) pfds[i].fd);
Jens Axboeb66570d2011-10-01 11:11:35 -06001588 continue;
1589 }
Jens Axboea5276612012-03-04 15:15:08 +01001590 if (!fio_handle_client(client)) {
Jens Axboe28d3ab02011-10-05 20:45:37 +02001591 log_info("client: host=%s disconnected\n",
1592 client->hostname);
1593 remove_client(client);
Jens Axboe498c92c2011-10-17 09:14:42 +02001594 retval = 1;
Jens Axboe38990762011-10-17 13:31:33 +02001595 } else if (client->error)
Jens Axboe498c92c2011-10-17 09:14:42 +02001596 retval = 1;
Jens Axboe343cb4a2012-03-09 17:16:51 +01001597 fio_put_client(client);
Jens Axboeb66570d2011-10-01 11:11:35 -06001598 }
1599 }
1600
Castor Fu952b05e2013-10-31 11:00:34 -06001601 fio_client_json_fini();
1602
Jens Axboeb66570d2011-10-01 11:11:35 -06001603 free(pfds);
Jens Axboe498c92c2011-10-17 09:14:42 +02001604 return retval;
Jens Axboeb66570d2011-10-01 11:11:35 -06001605}