blob: edc02a0a13416c04fbb546bf2c05799a440bbf84 [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 Axboe37f0c1a2011-10-11 14:08:33 +020063
Jens Axboe3c5f57e2011-10-06 12:37:50 +020064#define FIO_CLIENT_HASH_BITS 7
65#define FIO_CLIENT_HASH_SZ (1 << FIO_CLIENT_HASH_BITS)
66#define FIO_CLIENT_HASH_MASK (FIO_CLIENT_HASH_SZ - 1)
Jens Axboebebe6392011-10-07 10:00:51 +020067static struct flist_head client_hash[FIO_CLIENT_HASH_SZ];
Jens Axboe3c5f57e2011-10-06 12:37:50 +020068
Jens Axboebebe6392011-10-07 10:00:51 +020069static void fio_client_add_hash(struct fio_client *client)
Jens Axboe3c5f57e2011-10-06 12:37:50 +020070{
71 int bucket = hash_long(client->fd, FIO_CLIENT_HASH_BITS);
72
73 bucket &= FIO_CLIENT_HASH_MASK;
Jens Axboebebe6392011-10-07 10:00:51 +020074 flist_add(&client->hash_list, &client_hash[bucket]);
Jens Axboe3c5f57e2011-10-06 12:37:50 +020075}
76
Jens Axboebebe6392011-10-07 10:00:51 +020077static void fio_client_remove_hash(struct fio_client *client)
Jens Axboe3c5f57e2011-10-06 12:37:50 +020078{
Jens Axboebebe6392011-10-07 10:00:51 +020079 if (!flist_empty(&client->hash_list))
80 flist_del_init(&client->hash_list);
Jens Axboe3c5f57e2011-10-06 12:37:50 +020081}
82
83static void fio_init fio_client_hash_init(void)
84{
85 int i;
86
Jens Axboebebe6392011-10-07 10:00:51 +020087 for (i = 0; i < FIO_CLIENT_HASH_SZ; i++)
88 INIT_FLIST_HEAD(&client_hash[i]);
Jens Axboe3c5f57e2011-10-06 12:37:50 +020089}
90
Castor Fu952b05e2013-10-31 11:00:34 -060091static void fio_client_json_init(void)
92{
93 if (output_format != FIO_OUTPUT_JSON)
94 return;
95 root = json_create_object();
96 json_object_add_value_string(root, "fio version", fio_version_string);
97 clients_array = json_create_array();
98 json_object_add_value_array(root, "client_stats", clients_array);
99 du_array = json_create_array();
100 json_object_add_value_array(root, "disk_util", du_array);
101}
102
103static void fio_client_json_fini(void)
104{
105 if (output_format != FIO_OUTPUT_JSON)
106 return;
107 json_print_object(root);
108 log_info("\n");
109 json_free_object(root);
110 root = NULL;
111 clients_array = NULL;
112 du_array = NULL;
113}
114
Jens Axboeb66570d2011-10-01 11:11:35 -0600115static struct fio_client *find_client_by_fd(int fd)
116{
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200117 int bucket = hash_long(fd, FIO_CLIENT_HASH_BITS) & FIO_CLIENT_HASH_MASK;
Jens Axboeb66570d2011-10-01 11:11:35 -0600118 struct fio_client *client;
119 struct flist_head *entry;
120
Jens Axboebebe6392011-10-07 10:00:51 +0200121 flist_for_each(entry, &client_hash[bucket]) {
122 client = flist_entry(entry, struct fio_client, hash_list);
Jens Axboeb66570d2011-10-01 11:11:35 -0600123
Jens Axboee55f8f32012-03-06 15:42:31 +0100124 if (client->fd == fd) {
125 client->refs++;
Jens Axboeb66570d2011-10-01 11:11:35 -0600126 return client;
Jens Axboee55f8f32012-03-06 15:42:31 +0100127 }
Jens Axboeb66570d2011-10-01 11:11:35 -0600128 }
129
130 return NULL;
131}
132
Jens Axboe3af45202012-03-13 09:59:53 +0100133void fio_put_client(struct fio_client *client)
Jens Axboeb66570d2011-10-01 11:11:35 -0600134{
Jens Axboe5121a9a2012-03-05 13:32:47 +0100135 if (--client->refs)
136 return;
137
Jens Axboeb66570d2011-10-01 11:11:35 -0600138 free(client->hostname);
Jens Axboe81179ee2011-10-04 12:42:06 +0200139 if (client->argv)
140 free(client->argv);
Jens Axboeb5296dd2011-10-07 16:35:56 +0200141 if (client->name)
142 free(client->name);
Jens Axboe35899182014-10-07 20:56:28 -0600143 while (client->nr_files) {
144 struct client_file *cf = &client->files[--client->nr_files];
145
146 free(cf->file);
147 }
148 if (client->files)
149 free(client->files);
Jens Axboe81179ee2011-10-04 12:42:06 +0200150
Jens Axboe108fea72012-11-14 13:09:45 -0700151 if (!client->did_stat)
Jens Axboebfa8f5d2014-10-13 10:09:28 -0600152 sum_stat_clients--;
Jens Axboe108fea72012-11-14 13:09:45 -0700153
Jens Axboeb66570d2011-10-01 11:11:35 -0600154 free(client);
155}
Jens Axboe132159a2011-09-30 15:01:32 -0600156
Jens Axboe3af45202012-03-13 09:59:53 +0100157static void remove_client(struct fio_client *client)
Jens Axboe5121a9a2012-03-05 13:32:47 +0100158{
Jens Axboe3af45202012-03-13 09:59:53 +0100159 assert(client->refs);
160
161 dprint(FD_NET, "client: removed <%s>\n", client->hostname);
162
163 if (!flist_empty(&client->list))
164 flist_del_init(&client->list);
165
166 fio_client_remove_hash(client);
167
168 if (!flist_empty(&client->eta_list)) {
169 flist_del_init(&client->eta_list);
170 fio_client_dec_jobs_eta(client->eta_in_flight, client->ops->eta);
171 }
172
Jens Axboe1e0c1842012-03-20 15:15:00 +0100173 close(client->fd);
174 client->fd = -1;
175
Jens Axboe0cf3ece2012-03-21 10:15:20 +0100176 if (client->ops->removed)
177 client->ops->removed(client);
178
Jens Axboe3af45202012-03-13 09:59:53 +0100179 nr_clients--;
Jens Axboe3af45202012-03-13 09:59:53 +0100180 fio_put_client(client);
Jens Axboe5121a9a2012-03-05 13:32:47 +0100181}
182
Jens Axboe343cb4a2012-03-09 17:16:51 +0100183struct fio_client *fio_get_client(struct fio_client *client)
184{
185 client->refs++;
186 return client;
187}
188
Jens Axboefa2ea802011-10-08 21:07:29 +0200189static void __fio_client_add_cmd_option(struct fio_client *client,
190 const char *opt)
Jens Axboe81179ee2011-10-04 12:42:06 +0200191{
Jens Axboe39e8e012011-10-04 13:46:08 +0200192 int index;
193
194 index = client->argc++;
Jens Axboe81179ee2011-10-04 12:42:06 +0200195 client->argv = realloc(client->argv, sizeof(char *) * client->argc);
Jens Axboe39e8e012011-10-04 13:46:08 +0200196 client->argv[index] = strdup(opt);
197 dprint(FD_NET, "client: add cmd %d: %s\n", index, opt);
Jens Axboe81179ee2011-10-04 12:42:06 +0200198}
199
Jens Axboefa2ea802011-10-08 21:07:29 +0200200void fio_client_add_cmd_option(void *cookie, const char *opt)
Jens Axboe81179ee2011-10-04 12:42:06 +0200201{
Jens Axboebebe6392011-10-07 10:00:51 +0200202 struct fio_client *client = cookie;
Jens Axboe3f3a4542011-10-10 21:11:09 +0200203 struct flist_head *entry;
Jens Axboe81179ee2011-10-04 12:42:06 +0200204
Jens Axboebebe6392011-10-07 10:00:51 +0200205 if (!client || !opt)
Jens Axboefa2ea802011-10-08 21:07:29 +0200206 return;
Jens Axboe81179ee2011-10-04 12:42:06 +0200207
Jens Axboefa2ea802011-10-08 21:07:29 +0200208 __fio_client_add_cmd_option(client, opt);
Jens Axboe3f3a4542011-10-10 21:11:09 +0200209
210 /*
211 * Duplicate arguments to shared client group
212 */
213 flist_for_each(entry, &arg_list) {
214 client = flist_entry(entry, struct fio_client, arg_list);
215
216 __fio_client_add_cmd_option(client, opt);
217 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200218}
219
Jens Axboea5276612012-03-04 15:15:08 +0100220struct fio_client *fio_client_add_explicit(struct client_ops *ops,
221 const char *hostname, int type,
Jens Axboe3ec62ec2012-03-01 12:01:29 +0100222 int port)
223{
224 struct fio_client *client;
225
226 client = malloc(sizeof(*client));
227 memset(client, 0, sizeof(*client));
228
229 INIT_FLIST_HEAD(&client->list);
230 INIT_FLIST_HEAD(&client->hash_list);
231 INIT_FLIST_HEAD(&client->arg_list);
232 INIT_FLIST_HEAD(&client->eta_list);
233 INIT_FLIST_HEAD(&client->cmd_list);
234
235 client->hostname = strdup(hostname);
236
237 if (type == Fio_client_socket)
238 client->is_sock = 1;
239 else {
240 int ipv6;
241
242 ipv6 = type == Fio_client_ipv6;
Jens Axboe3aa3cee2014-01-24 18:56:56 -0800243 if (fio_server_parse_host(hostname, ipv6,
Jens Axboe3ec62ec2012-03-01 12:01:29 +0100244 &client->addr.sin_addr,
245 &client->addr6.sin6_addr))
246 goto err;
247
248 client->port = port;
249 }
250
251 client->fd = -1;
Jens Axboea5276612012-03-04 15:15:08 +0100252 client->ops = ops;
Jens Axboe5121a9a2012-03-05 13:32:47 +0100253 client->refs = 1;
Jens Axboe46bcd492012-03-14 11:31:21 +0100254 client->type = ops->client_type;
Jens Axboe3ec62ec2012-03-01 12:01:29 +0100255
256 __fio_client_add_cmd_option(client, "fio");
257
258 flist_add(&client->list, &client_list);
259 nr_clients++;
260 dprint(FD_NET, "client: added <%s>\n", client->hostname);
261 return client;
262err:
263 free(client);
264 return NULL;
265}
266
Jens Axboe35899182014-10-07 20:56:28 -0600267int fio_client_add_ini_file(void *cookie, const char *ini_file, int remote)
Jens Axboe14ea90e2012-08-26 17:33:34 +0200268{
269 struct fio_client *client = cookie;
Jens Axboe35899182014-10-07 20:56:28 -0600270 struct client_file *cf;
Jens Axboe14ea90e2012-08-26 17:33:34 +0200271 size_t new_size;
Jens Axboe35899182014-10-07 20:56:28 -0600272 void *new_files;
Jens Axboe14ea90e2012-08-26 17:33:34 +0200273
Jens Axboe722b5cd2014-10-14 19:56:25 -0600274 if (!client)
275 return 1;
276
Jens Axboe14ea90e2012-08-26 17:33:34 +0200277 dprint(FD_NET, "client <%s>: add ini %s\n", client->hostname, ini_file);
278
Jens Axboe35899182014-10-07 20:56:28 -0600279 new_size = (client->nr_files + 1) * sizeof(struct client_file);
280 new_files = realloc(client->files, new_size);
281 if (!new_files)
282 return 1;
283
284 client->files = new_files;
285 cf = &client->files[client->nr_files];
286 cf->file = strdup(ini_file);
287 cf->remote = remote;
288 client->nr_files++;
289 return 0;
Jens Axboe14ea90e2012-08-26 17:33:34 +0200290}
291
Jens Axboea5276612012-03-04 15:15:08 +0100292int fio_client_add(struct client_ops *ops, const char *hostname, void **cookie)
Jens Axboe132159a2011-09-30 15:01:32 -0600293{
Jens Axboe3f3a4542011-10-10 21:11:09 +0200294 struct fio_client *existing = *cookie;
Jens Axboeb66570d2011-10-01 11:11:35 -0600295 struct fio_client *client;
Jens Axboe132159a2011-09-30 15:01:32 -0600296
Jens Axboe3f3a4542011-10-10 21:11:09 +0200297 if (existing) {
298 /*
299 * We always add our "exec" name as the option, hence 1
300 * means empty.
301 */
302 if (existing->argc == 1)
303 flist_add_tail(&existing->arg_list, &arg_list);
304 else {
305 while (!flist_empty(&arg_list))
306 flist_del_init(arg_list.next);
307 }
308 }
309
Jens Axboeb66570d2011-10-01 11:11:35 -0600310 client = malloc(sizeof(*client));
Jens Axboea37f69b2011-10-01 12:24:21 -0600311 memset(client, 0, sizeof(*client));
Jens Axboe81179ee2011-10-04 12:42:06 +0200312
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200313 INIT_FLIST_HEAD(&client->list);
Jens Axboebebe6392011-10-07 10:00:51 +0200314 INIT_FLIST_HEAD(&client->hash_list);
Jens Axboe3f3a4542011-10-10 21:11:09 +0200315 INIT_FLIST_HEAD(&client->arg_list);
Jens Axboe82c1ed32011-10-10 08:56:18 +0200316 INIT_FLIST_HEAD(&client->eta_list);
Jens Axboe89c17072011-10-11 10:15:51 +0200317 INIT_FLIST_HEAD(&client->cmd_list);
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200318
Jens Axboebebe6392011-10-07 10:00:51 +0200319 if (fio_server_parse_string(hostname, &client->hostname,
320 &client->is_sock, &client->port,
Jens Axboe811826b2011-10-24 09:11:50 +0200321 &client->addr.sin_addr,
322 &client->addr6.sin6_addr,
323 &client->ipv6))
Jens Axboebebe6392011-10-07 10:00:51 +0200324 return -1;
325
Jens Axboea37f69b2011-10-01 12:24:21 -0600326 client->fd = -1;
Jens Axboea5276612012-03-04 15:15:08 +0100327 client->ops = ops;
Jens Axboe5121a9a2012-03-05 13:32:47 +0100328 client->refs = 1;
Jens Axboe46bcd492012-03-14 11:31:21 +0100329 client->type = ops->client_type;
Jens Axboe81179ee2011-10-04 12:42:06 +0200330
331 __fio_client_add_cmd_option(client, "fio");
332
Jens Axboea37f69b2011-10-01 12:24:21 -0600333 flist_add(&client->list, &client_list);
334 nr_clients++;
Jens Axboebebe6392011-10-07 10:00:51 +0200335 dprint(FD_NET, "client: added <%s>\n", client->hostname);
336 *cookie = client;
337 return 0;
Jens Axboea37f69b2011-10-01 12:24:21 -0600338}
339
Jens Axboed824c3b2012-03-09 17:45:43 +0100340static void probe_client(struct fio_client *client)
341{
Jens Axboe3989b142013-04-12 13:13:24 +0200342 struct cmd_client_probe_pdu pdu;
343 uint64_t tag;
344
Jens Axboed824c3b2012-03-09 17:45:43 +0100345 dprint(FD_NET, "client: send probe\n");
346
Jens Axboe3989b142013-04-12 13:13:24 +0200347#ifdef CONFIG_ZLIB
348 pdu.flags = __le64_to_cpu(FIO_PROBE_FLAG_ZLIB);
349#else
350 pdu.flags = 0;
351#endif
352
353 fio_net_send_cmd(client->fd, FIO_NET_CMD_PROBE, &pdu, sizeof(pdu), &tag, &client->cmd_list);
Jens Axboed824c3b2012-03-09 17:45:43 +0100354}
355
Jens Axboe87aa8f12011-10-06 21:24:13 +0200356static int fio_client_connect_ip(struct fio_client *client)
Jens Axboea37f69b2011-10-01 12:24:21 -0600357{
Jens Axboe811826b2011-10-24 09:11:50 +0200358 struct sockaddr *addr;
Jens Axboe67bf9822013-01-10 11:23:19 +0100359 socklen_t socklen;
Jens Axboe811826b2011-10-24 09:11:50 +0200360 int fd, domain;
Jens Axboe132159a2011-09-30 15:01:32 -0600361
Jens Axboe811826b2011-10-24 09:11:50 +0200362 if (client->ipv6) {
363 client->addr6.sin6_family = AF_INET6;
364 client->addr6.sin6_port = htons(client->port);
365 domain = AF_INET6;
366 addr = (struct sockaddr *) &client->addr6;
367 socklen = sizeof(client->addr6);
368 } else {
369 client->addr.sin_family = AF_INET;
370 client->addr.sin_port = htons(client->port);
371 domain = AF_INET;
372 addr = (struct sockaddr *) &client->addr;
373 socklen = sizeof(client->addr);
374 }
Jens Axboe132159a2011-09-30 15:01:32 -0600375
Jens Axboe811826b2011-10-24 09:11:50 +0200376 fd = socket(domain, SOCK_STREAM, 0);
Jens Axboe132159a2011-09-30 15:01:32 -0600377 if (fd < 0) {
Jens Axboe25095e12012-03-09 17:09:55 +0100378 int ret = -errno;
379
Jens Axboe132159a2011-09-30 15:01:32 -0600380 log_err("fio: socket: %s\n", strerror(errno));
Jens Axboe25095e12012-03-09 17:09:55 +0100381 return ret;
Jens Axboe132159a2011-09-30 15:01:32 -0600382 }
383
Jens Axboe811826b2011-10-24 09:11:50 +0200384 if (connect(fd, addr, socklen) < 0) {
Jens Axboe25095e12012-03-09 17:09:55 +0100385 int ret = -errno;
386
Jens Axboe132159a2011-09-30 15:01:32 -0600387 log_err("fio: connect: %s\n", strerror(errno));
Jens Axboea7de0a12011-10-07 12:55:14 +0200388 log_err("fio: failed to connect to %s:%u\n", client->hostname,
389 client->port);
Jens Axboeb94cba42011-10-06 21:27:10 +0200390 close(fd);
Jens Axboe25095e12012-03-09 17:09:55 +0100391 return ret;
Jens Axboe132159a2011-09-30 15:01:32 -0600392 }
393
Jens Axboe87aa8f12011-10-06 21:24:13 +0200394 return fd;
395}
396
397static int fio_client_connect_sock(struct fio_client *client)
398{
399 struct sockaddr_un *addr = &client->addr_un;
Jens Axboe67bf9822013-01-10 11:23:19 +0100400 socklen_t len;
Jens Axboe87aa8f12011-10-06 21:24:13 +0200401 int fd;
402
403 memset(addr, 0, sizeof(*addr));
404 addr->sun_family = AF_UNIX;
Jens Axboe1cb96412014-04-14 08:50:33 -0600405 strncpy(addr->sun_path, client->hostname, sizeof(addr->sun_path) - 1);
Jens Axboe87aa8f12011-10-06 21:24:13 +0200406
407 fd = socket(AF_UNIX, SOCK_STREAM, 0);
408 if (fd < 0) {
Jens Axboe25095e12012-03-09 17:09:55 +0100409 int ret = -errno;
410
Jens Axboe87aa8f12011-10-06 21:24:13 +0200411 log_err("fio: socket: %s\n", strerror(errno));
Jens Axboe25095e12012-03-09 17:09:55 +0100412 return ret;
Jens Axboe87aa8f12011-10-06 21:24:13 +0200413 }
414
415 len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
416 if (connect(fd, (struct sockaddr *) addr, len) < 0) {
Jens Axboe25095e12012-03-09 17:09:55 +0100417 int ret = -errno;
418
Jens Axboe87aa8f12011-10-06 21:24:13 +0200419 log_err("fio: connect; %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200420 close(fd);
Jens Axboe25095e12012-03-09 17:09:55 +0100421 return ret;
Jens Axboe87aa8f12011-10-06 21:24:13 +0200422 }
423
424 return fd;
425}
426
Jens Axboe2f99deb2012-03-09 14:37:29 +0100427int fio_client_connect(struct fio_client *client)
Jens Axboe87aa8f12011-10-06 21:24:13 +0200428{
429 int fd;
430
431 dprint(FD_NET, "client: connect to host %s\n", client->hostname);
432
Jens Axboe87aa8f12011-10-06 21:24:13 +0200433 if (client->is_sock)
434 fd = fio_client_connect_sock(client);
435 else
436 fd = fio_client_connect_ip(client);
437
Jens Axboe89c17072011-10-11 10:15:51 +0200438 dprint(FD_NET, "client: %s connected %d\n", client->hostname, fd);
439
Jens Axboe87aa8f12011-10-06 21:24:13 +0200440 if (fd < 0)
Jens Axboea1a3ed52012-03-09 17:00:01 +0100441 return fd;
Jens Axboe87aa8f12011-10-06 21:24:13 +0200442
Jens Axboeb66570d2011-10-01 11:11:35 -0600443 client->fd = fd;
Jens Axboebebe6392011-10-07 10:00:51 +0200444 fio_client_add_hash(client);
Jens Axboe81179ee2011-10-04 12:42:06 +0200445 client->state = Client_connected;
Jens Axboed824c3b2012-03-09 17:45:43 +0100446
447 probe_client(client);
Jens Axboe132159a2011-09-30 15:01:32 -0600448 return 0;
449}
450
Jens Axboe0cf3ece2012-03-21 10:15:20 +0100451int fio_client_terminate(struct fio_client *client)
Jens Axboe2f99deb2012-03-09 14:37:29 +0100452{
Jens Axboe0cf3ece2012-03-21 10:15:20 +0100453 return fio_net_send_quit(client->fd);
Jens Axboe2f99deb2012-03-09 14:37:29 +0100454}
455
Jens Axboecc0df002011-10-03 20:53:32 +0200456void fio_clients_terminate(void)
457{
458 struct flist_head *entry;
459 struct fio_client *client;
460
Jens Axboe60efd142011-10-04 13:30:11 +0200461 dprint(FD_NET, "client: terminate clients\n");
462
Jens Axboecc0df002011-10-03 20:53:32 +0200463 flist_for_each(entry, &client_list) {
464 client = flist_entry(entry, struct fio_client, list);
Jens Axboe2f99deb2012-03-09 14:37:29 +0100465 fio_client_terminate(client);
Jens Axboecc0df002011-10-03 20:53:32 +0200466 }
467}
468
469static void sig_int(int sig)
470{
Jens Axboebebe6392011-10-07 10:00:51 +0200471 dprint(FD_NET, "client: got signal %d\n", sig);
Jens Axboecc0df002011-10-03 20:53:32 +0200472 fio_clients_terminate();
473}
474
Jens Axboeb852e7c2012-03-30 10:30:35 +0200475static void sig_show_status(int sig)
476{
477 show_running_run_stats();
478}
479
Jens Axboecc0df002011-10-03 20:53:32 +0200480static void client_signal_handler(void)
481{
482 struct sigaction act;
483
484 memset(&act, 0, sizeof(act));
485 act.sa_handler = sig_int;
486 act.sa_flags = SA_RESTART;
487 sigaction(SIGINT, &act, NULL);
488
489 memset(&act, 0, sizeof(act));
490 act.sa_handler = sig_int;
491 act.sa_flags = SA_RESTART;
492 sigaction(SIGTERM, &act, NULL);
Jens Axboeb852e7c2012-03-30 10:30:35 +0200493
Bruce Cran2f694502012-10-10 16:34:13 +0100494/* Windows uses SIGBREAK as a quit signal from other applications */
495#ifdef WIN32
496 memset(&act, 0, sizeof(act));
497 act.sa_handler = sig_int;
498 act.sa_flags = SA_RESTART;
499 sigaction(SIGBREAK, &act, NULL);
500#endif
501
Jens Axboeb852e7c2012-03-30 10:30:35 +0200502 memset(&act, 0, sizeof(act));
503 act.sa_handler = sig_show_status;
504 act.sa_flags = SA_RESTART;
505 sigaction(SIGUSR1, &act, NULL);
Jens Axboecc0df002011-10-03 20:53:32 +0200506}
507
Jens Axboe81179ee2011-10-04 12:42:06 +0200508static int send_client_cmd_line(struct fio_client *client)
509{
Jens Axboefa2ea802011-10-08 21:07:29 +0200510 struct cmd_single_line_pdu *cslp;
511 struct cmd_line_pdu *clp;
512 unsigned long offset;
Jens Axboe7f868312011-10-10 09:55:21 +0200513 unsigned int *lens;
Jens Axboefa2ea802011-10-08 21:07:29 +0200514 void *pdu;
515 size_t mem;
Jens Axboe81179ee2011-10-04 12:42:06 +0200516 int i, ret;
517
Jens Axboe39e8e012011-10-04 13:46:08 +0200518 dprint(FD_NET, "client: send cmdline %d\n", client->argc);
Jens Axboe60efd142011-10-04 13:30:11 +0200519
Jens Axboe7f868312011-10-10 09:55:21 +0200520 lens = malloc(client->argc * sizeof(unsigned int));
521
Jens Axboefa2ea802011-10-08 21:07:29 +0200522 /*
523 * Find out how much mem we need
524 */
Jens Axboe7f868312011-10-10 09:55:21 +0200525 for (i = 0, mem = 0; i < client->argc; i++) {
526 lens[i] = strlen(client->argv[i]) + 1;
527 mem += lens[i];
528 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200529
Jens Axboefa2ea802011-10-08 21:07:29 +0200530 /*
531 * We need one cmd_line_pdu, and argc number of cmd_single_line_pdu
532 */
533 mem += sizeof(*clp) + (client->argc * sizeof(*cslp));
534
535 pdu = malloc(mem);
536 clp = pdu;
537 offset = sizeof(*clp);
538
539 for (i = 0; i < client->argc; i++) {
Jens Axboe7f868312011-10-10 09:55:21 +0200540 uint16_t arg_len = lens[i];
Jens Axboefa2ea802011-10-08 21:07:29 +0200541
542 cslp = pdu + offset;
543 strcpy((char *) cslp->text, client->argv[i]);
544 cslp->len = cpu_to_le16(arg_len);
545 offset += sizeof(*cslp) + arg_len;
546 }
547
Jens Axboe7f868312011-10-10 09:55:21 +0200548 free(lens);
Jens Axboefa2ea802011-10-08 21:07:29 +0200549 clp->lines = cpu_to_le16(client->argc);
Jens Axboe46bcd492012-03-14 11:31:21 +0100550 clp->client_type = __cpu_to_le16(client->type);
Jens Axboe40c60512012-03-27 16:03:04 +0200551 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOBLINE, pdu, mem, NULL, NULL);
Jens Axboe81179ee2011-10-04 12:42:06 +0200552 free(pdu);
553 return ret;
554}
555
Jens Axboea37f69b2011-10-01 12:24:21 -0600556int fio_clients_connect(void)
557{
558 struct fio_client *client;
559 struct flist_head *entry, *tmp;
560 int ret;
561
Bruce Cran93bcfd22012-02-20 20:18:19 +0100562#ifdef WIN32
563 WSADATA wsd;
Jens Axboe3c3ed072012-03-27 09:12:39 +0200564 WSAStartup(MAKEWORD(2, 2), &wsd);
Bruce Cran93bcfd22012-02-20 20:18:19 +0100565#endif
566
Jens Axboe60efd142011-10-04 13:30:11 +0200567 dprint(FD_NET, "client: connect all\n");
568
Jens Axboecc0df002011-10-03 20:53:32 +0200569 client_signal_handler();
570
Jens Axboea37f69b2011-10-01 12:24:21 -0600571 flist_for_each_safe(entry, tmp, &client_list) {
572 client = flist_entry(entry, struct fio_client, list);
573
574 ret = fio_client_connect(client);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200575 if (ret) {
Jens Axboea37f69b2011-10-01 12:24:21 -0600576 remove_client(client);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200577 continue;
578 }
579
Jens Axboe81179ee2011-10-04 12:42:06 +0200580 if (client->argc > 1)
581 send_client_cmd_line(client);
Jens Axboea37f69b2011-10-01 12:24:21 -0600582 }
583
584 return !nr_clients;
585}
586
Jens Axboeb9d2f302012-03-08 20:36:28 +0100587int fio_start_client(struct fio_client *client)
588{
589 dprint(FD_NET, "client: start %s\n", client->hostname);
590 return fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_RUN, 0, NULL);
591}
592
593int fio_start_all_clients(void)
594{
595 struct fio_client *client;
596 struct flist_head *entry, *tmp;
597 int ret;
598
599 dprint(FD_NET, "client: start all\n");
600
Castor Fu952b05e2013-10-31 11:00:34 -0600601 fio_client_json_init();
602
Jens Axboeb9d2f302012-03-08 20:36:28 +0100603 flist_for_each_safe(entry, tmp, &client_list) {
604 client = flist_entry(entry, struct fio_client, list);
605
606 ret = fio_start_client(client);
607 if (ret) {
608 remove_client(client);
609 continue;
610 }
611 }
612
613 return flist_empty(&client_list);
614}
615
Jens Axboe35899182014-10-07 20:56:28 -0600616static int __fio_client_send_remote_ini(struct fio_client *client,
617 const char *filename)
618{
619 struct cmd_load_file_pdu *pdu;
620 size_t p_size;
621 int ret;
622
623 dprint(FD_NET, "send remote ini %s to %s\n", filename, client->hostname);
624
Jens Axboe79ecc302014-10-07 22:02:21 -0600625 p_size = sizeof(*pdu) + strlen(filename) + 1;
Jens Axboe35899182014-10-07 20:56:28 -0600626 pdu = malloc(p_size);
Jens Axboe79ecc302014-10-07 22:02:21 -0600627 memset(pdu, 0, p_size);
Jens Axboe35899182014-10-07 20:56:28 -0600628 pdu->name_len = strlen(filename);
629 strcpy((char *) pdu->file, filename);
630 pdu->client_type = cpu_to_le16((uint16_t) client->type);
631
632 client->sent_job = 1;
633 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_LOAD_FILE, pdu, p_size,NULL, NULL);
634 free(pdu);
635 return ret;
636}
637
Jens Axboe132159a2011-09-30 15:01:32 -0600638/*
639 * Send file contents to server backend. We could use sendfile(), but to remain
640 * more portable lets just read/write the darn thing.
641 */
Jens Axboe35899182014-10-07 20:56:28 -0600642static int __fio_client_send_local_ini(struct fio_client *client,
643 const char *filename)
Jens Axboe132159a2011-09-30 15:01:32 -0600644{
Jens Axboe46bcd492012-03-14 11:31:21 +0100645 struct cmd_job_pdu *pdu;
646 size_t p_size;
Jens Axboe132159a2011-09-30 15:01:32 -0600647 struct stat sb;
Jens Axboe46bcd492012-03-14 11:31:21 +0100648 char *p;
649 void *buf;
Jens Axboe132159a2011-09-30 15:01:32 -0600650 off_t len;
651 int fd, ret;
652
Jens Axboe46c48f12011-10-01 12:50:51 -0600653 dprint(FD_NET, "send ini %s to %s\n", filename, client->hostname);
654
Jens Axboe132159a2011-09-30 15:01:32 -0600655 fd = open(filename, O_RDONLY);
656 if (fd < 0) {
Jens Axboe25095e12012-03-09 17:09:55 +0100657 int ret = -errno;
658
Jens Axboee951bdc2011-10-05 21:58:45 +0200659 log_err("fio: job file <%s> open: %s\n", filename, strerror(errno));
Jens Axboe25095e12012-03-09 17:09:55 +0100660 return ret;
Jens Axboe132159a2011-09-30 15:01:32 -0600661 }
662
663 if (fstat(fd, &sb) < 0) {
Jens Axboe25095e12012-03-09 17:09:55 +0100664 int ret = -errno;
665
Jens Axboe132159a2011-09-30 15:01:32 -0600666 log_err("fio: job file stat: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200667 close(fd);
Jens Axboe25095e12012-03-09 17:09:55 +0100668 return ret;
Jens Axboe132159a2011-09-30 15:01:32 -0600669 }
670
Jens Axboe46bcd492012-03-14 11:31:21 +0100671 p_size = sb.st_size + sizeof(*pdu);
672 pdu = malloc(p_size);
673 buf = pdu->buf;
Jens Axboe132159a2011-09-30 15:01:32 -0600674
675 len = sb.st_size;
676 p = buf;
677 do {
678 ret = read(fd, p, len);
679 if (ret > 0) {
680 len -= ret;
681 if (!len)
682 break;
683 p += ret;
684 continue;
685 } else if (!ret)
686 break;
687 else if (errno == EAGAIN || errno == EINTR)
688 continue;
689 } while (1);
690
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200691 if (len) {
692 log_err("fio: failed reading job file %s\n", filename);
Jens Axboeb94cba42011-10-06 21:27:10 +0200693 close(fd);
Hong Zhiguo03a22d92013-10-16 08:35:12 -0600694 free(pdu);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200695 return 1;
696 }
697
Jens Axboe46bcd492012-03-14 11:31:21 +0100698 pdu->buf_len = __cpu_to_le32(sb.st_size);
699 pdu->client_type = cpu_to_le32(client->type);
700
Jens Axboec2cb6862012-02-23 20:56:12 +0100701 client->sent_job = 1;
Jens Axboe40c60512012-03-27 16:03:04 +0200702 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, pdu, p_size, NULL, NULL);
Jens Axboe46bcd492012-03-14 11:31:21 +0100703 free(pdu);
Jens Axboeb94cba42011-10-06 21:27:10 +0200704 close(fd);
Jens Axboe132159a2011-09-30 15:01:32 -0600705 return ret;
706}
Jens Axboe37db14f2011-09-30 17:00:42 -0600707
Jens Axboe35899182014-10-07 20:56:28 -0600708int fio_client_send_ini(struct fio_client *client, const char *filename,
709 int remote)
Jens Axboe9988ca72012-03-09 15:14:06 +0100710{
Jens Axboe25095e12012-03-09 17:09:55 +0100711 int ret;
712
Jens Axboe35899182014-10-07 20:56:28 -0600713 if (!remote)
714 ret = __fio_client_send_local_ini(client, filename);
715 else
716 ret = __fio_client_send_remote_ini(client, filename);
717
Jens Axboe3af45202012-03-13 09:59:53 +0100718 if (!ret)
719 client->sent_job = 1;
Jens Axboe9988ca72012-03-09 15:14:06 +0100720
Jens Axboe25095e12012-03-09 17:09:55 +0100721 return ret;
Jens Axboe9988ca72012-03-09 15:14:06 +0100722}
723
Jens Axboe35899182014-10-07 20:56:28 -0600724static int fio_client_send_cf(struct fio_client *client,
725 struct client_file *cf)
726{
727 return fio_client_send_ini(client, cf->file, cf->remote);
728}
729
Jens Axboea37f69b2011-10-01 12:24:21 -0600730int fio_clients_send_ini(const char *filename)
731{
732 struct fio_client *client;
733 struct flist_head *entry, *tmp;
734
735 flist_for_each_safe(entry, tmp, &client_list) {
736 client = flist_entry(entry, struct fio_client, list);
737
Jens Axboe35899182014-10-07 20:56:28 -0600738 if (client->nr_files) {
Jens Axboe14ea90e2012-08-26 17:33:34 +0200739 int i;
740
Jens Axboe35899182014-10-07 20:56:28 -0600741 for (i = 0; i < client->nr_files; i++) {
742 struct client_file *cf;
Jens Axboe14ea90e2012-08-26 17:33:34 +0200743
Jens Axboe35899182014-10-07 20:56:28 -0600744 cf = &client->files[i];
745
746 if (fio_client_send_cf(client, cf)) {
Jens Axboe14ea90e2012-08-26 17:33:34 +0200747 remove_client(client);
748 break;
749 }
750 }
Jens Axboe35899182014-10-07 20:56:28 -0600751 }
752 if (client->sent_job)
753 continue;
754 if (!filename || fio_client_send_ini(client, filename, 0))
Jens Axboe3af45202012-03-13 09:59:53 +0100755 remove_client(client);
Jens Axboea37f69b2011-10-01 12:24:21 -0600756 }
757
758 return !nr_clients;
759}
760
Jens Axboe40c60512012-03-27 16:03:04 +0200761int fio_client_update_options(struct fio_client *client,
762 struct thread_options *o, uint64_t *tag)
763{
764 struct cmd_add_job_pdu pdu;
765
766 pdu.thread_number = cpu_to_le32(client->thread_number);
767 pdu.groupid = cpu_to_le32(client->groupid);
768 convert_thread_options_to_net(&pdu.top, o);
Castor Fu952b05e2013-10-31 11:00:34 -0600769
Jens Axboe40c60512012-03-27 16:03:04 +0200770 return fio_net_send_cmd(client->fd, FIO_NET_CMD_UPDATE_JOB, &pdu, sizeof(pdu), tag, &client->cmd_list);
771}
772
Jens Axboea64e88d2011-10-03 14:20:01 +0200773static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
774{
775 dst->max_val = le64_to_cpu(src->max_val);
776 dst->min_val = le64_to_cpu(src->min_val);
777 dst->samples = le64_to_cpu(src->samples);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200778
779 /*
780 * Floats arrive as IEEE 754 encoded uint64_t, convert back to double
781 */
782 dst->mean.u.f = fio_uint64_to_double(le64_to_cpu(dst->mean.u.i));
783 dst->S.u.f = fio_uint64_to_double(le64_to_cpu(dst->S.u.i));
Jens Axboea64e88d2011-10-03 14:20:01 +0200784}
785
786static void convert_ts(struct thread_stat *dst, struct thread_stat *src)
787{
788 int i, j;
789
Jens Axboe2f122b12012-03-15 13:10:19 +0100790 dst->error = le32_to_cpu(src->error);
791 dst->thread_number = le32_to_cpu(src->thread_number);
792 dst->groupid = le32_to_cpu(src->groupid);
793 dst->pid = le32_to_cpu(src->pid);
794 dst->members = le32_to_cpu(src->members);
Jens Axboe771e58b2013-01-30 12:56:23 +0100795 dst->unified_rw_rep = le32_to_cpu(src->unified_rw_rep);
Jens Axboea64e88d2011-10-03 14:20:01 +0200796
Jens Axboe298921d2012-09-24 18:47:01 +0200797 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
Jens Axboea64e88d2011-10-03 14:20:01 +0200798 convert_io_stat(&dst->clat_stat[i], &src->clat_stat[i]);
799 convert_io_stat(&dst->slat_stat[i], &src->slat_stat[i]);
800 convert_io_stat(&dst->lat_stat[i], &src->lat_stat[i]);
801 convert_io_stat(&dst->bw_stat[i], &src->bw_stat[i]);
802 }
803
804 dst->usr_time = le64_to_cpu(src->usr_time);
805 dst->sys_time = le64_to_cpu(src->sys_time);
806 dst->ctx = le64_to_cpu(src->ctx);
807 dst->minf = le64_to_cpu(src->minf);
808 dst->majf = le64_to_cpu(src->majf);
809 dst->clat_percentiles = le64_to_cpu(src->clat_percentiles);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200810
811 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
812 fio_fp64_t *fps = &src->percentile_list[i];
813 fio_fp64_t *fpd = &dst->percentile_list[i];
814
815 fpd->u.f = fio_uint64_to_double(le64_to_cpu(fps->u.i));
816 }
Jens Axboea64e88d2011-10-03 14:20:01 +0200817
818 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
819 dst->io_u_map[i] = le32_to_cpu(src->io_u_map[i]);
820 dst->io_u_submit[i] = le32_to_cpu(src->io_u_submit[i]);
821 dst->io_u_complete[i] = le32_to_cpu(src->io_u_complete[i]);
822 }
823
824 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
825 dst->io_u_lat_u[i] = le32_to_cpu(src->io_u_lat_u[i]);
826 dst->io_u_lat_m[i] = le32_to_cpu(src->io_u_lat_m[i]);
827 }
828
Jens Axboe298921d2012-09-24 18:47:01 +0200829 for (i = 0; i < DDIR_RWDIR_CNT; i++)
Jens Axboea64e88d2011-10-03 14:20:01 +0200830 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
831 dst->io_u_plat[i][j] = le32_to_cpu(src->io_u_plat[i][j]);
832
Jens Axboe78799de2013-01-29 20:53:52 +0100833 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
Jens Axboea64e88d2011-10-03 14:20:01 +0200834 dst->total_io_u[i] = le64_to_cpu(src->total_io_u[i]);
Jens Axboe93eee042011-10-03 21:08:48 +0200835 dst->short_io_u[i] = le64_to_cpu(src->short_io_u[i]);
Jens Axboe67e149c2014-10-09 13:27:44 -0600836 dst->drop_io_u[i] = le64_to_cpu(src->drop_io_u[i]);
Jens Axboea64e88d2011-10-03 14:20:01 +0200837 }
838
839 dst->total_submit = le64_to_cpu(src->total_submit);
840 dst->total_complete = le64_to_cpu(src->total_complete);
841
Jens Axboe298921d2012-09-24 18:47:01 +0200842 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
Jens Axboea64e88d2011-10-03 14:20:01 +0200843 dst->io_bytes[i] = le64_to_cpu(src->io_bytes[i]);
844 dst->runtime[i] = le64_to_cpu(src->runtime[i]);
845 }
846
847 dst->total_run_time = le64_to_cpu(src->total_run_time);
848 dst->continue_on_error = le16_to_cpu(src->continue_on_error);
849 dst->total_err_count = le64_to_cpu(src->total_err_count);
Jens Axboeddcc0b62011-10-03 14:45:27 +0200850 dst->first_error = le32_to_cpu(src->first_error);
851 dst->kb_base = le32_to_cpu(src->kb_base);
Steven Noonanad705bc2013-04-08 15:05:25 -0700852 dst->unit_base = le32_to_cpu(src->unit_base);
Jens Axboe3e260a42013-12-09 12:38:53 -0700853
854 dst->latency_depth = le32_to_cpu(src->latency_depth);
855 dst->latency_target = le64_to_cpu(src->latency_target);
856 dst->latency_window = le64_to_cpu(src->latency_window);
857 dst->latency_percentile.u.f = fio_uint64_to_double(le64_to_cpu(src->latency_percentile.u.i));
Jens Axboea64e88d2011-10-03 14:20:01 +0200858}
859
860static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
861{
862 int i;
863
Jens Axboe298921d2012-09-24 18:47:01 +0200864 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
Jens Axboea64e88d2011-10-03 14:20:01 +0200865 dst->max_run[i] = le64_to_cpu(src->max_run[i]);
866 dst->min_run[i] = le64_to_cpu(src->min_run[i]);
867 dst->max_bw[i] = le64_to_cpu(src->max_bw[i]);
868 dst->min_bw[i] = le64_to_cpu(src->min_bw[i]);
869 dst->io_kb[i] = le64_to_cpu(src->io_kb[i]);
870 dst->agg[i] = le64_to_cpu(src->agg[i]);
871 }
872
873 dst->kb_base = le32_to_cpu(src->kb_base);
Steven Noonanad705bc2013-04-08 15:05:25 -0700874 dst->unit_base = le32_to_cpu(src->unit_base);
Jens Axboea64e88d2011-10-03 14:20:01 +0200875 dst->groupid = le32_to_cpu(src->groupid);
Jens Axboe771e58b2013-01-30 12:56:23 +0100876 dst->unified_rw_rep = le32_to_cpu(src->unified_rw_rep);
Jens Axboea64e88d2011-10-03 14:20:01 +0200877}
878
Castor Fu952b05e2013-10-31 11:00:34 -0600879static void json_object_add_client_info(struct json_object *obj,
Castor Fu54bcda52014-08-22 17:16:44 -0500880 struct fio_client *client)
Castor Fu952b05e2013-10-31 11:00:34 -0600881{
Castor Fu54bcda52014-08-22 17:16:44 -0500882 const char *hostname = client->hostname ? client->hostname : "";
883
884 json_object_add_value_string(obj, "hostname", hostname);
Castor Fu952b05e2013-10-31 11:00:34 -0600885 json_object_add_value_int(obj, "port", client->port);
886}
887
Jens Axboe89e5fad2012-03-05 09:21:12 +0100888static void handle_ts(struct fio_client *client, struct fio_net_cmd *cmd)
Jens Axboea64e88d2011-10-03 14:20:01 +0200889{
890 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
Castor Fu952b05e2013-10-31 11:00:34 -0600891 struct json_object *tsobj;
Jens Axboea64e88d2011-10-03 14:20:01 +0200892
Castor Fu952b05e2013-10-31 11:00:34 -0600893 tsobj = show_thread_status(&p->ts, &p->rs);
Jens Axboe108fea72012-11-14 13:09:45 -0700894 client->did_stat = 1;
Castor Fu952b05e2013-10-31 11:00:34 -0600895 if (tsobj) {
896 json_object_add_client_info(tsobj, client);
897 json_array_add_value_object(clients_array, tsobj);
898 }
Jens Axboe37f0c1a2011-10-11 14:08:33 +0200899
Jens Axboebfa8f5d2014-10-13 10:09:28 -0600900 if (sum_stat_clients <= 1)
Jens Axboe37f0c1a2011-10-11 14:08:33 +0200901 return;
902
903 sum_thread_stats(&client_ts, &p->ts, sum_stat_nr);
904 sum_group_stats(&client_gs, &p->rs);
905
906 client_ts.members++;
Jens Axboe2f122b12012-03-15 13:10:19 +0100907 client_ts.thread_number = p->ts.thread_number;
Jens Axboe37f0c1a2011-10-11 14:08:33 +0200908 client_ts.groupid = p->ts.groupid;
Jens Axboe771e58b2013-01-30 12:56:23 +0100909 client_ts.unified_rw_rep = p->ts.unified_rw_rep;
Jens Axboe37f0c1a2011-10-11 14:08:33 +0200910
911 if (++sum_stat_nr == sum_stat_clients) {
912 strcpy(client_ts.name, "All clients");
Castor Fu952b05e2013-10-31 11:00:34 -0600913 tsobj = show_thread_status(&client_ts, &client_gs);
914 if (tsobj) {
915 json_object_add_client_info(tsobj, client);
916 json_array_add_value_object(clients_array, tsobj);
917 }
Jens Axboe37f0c1a2011-10-11 14:08:33 +0200918 }
Jens Axboea64e88d2011-10-03 14:20:01 +0200919}
920
Jens Axboe89e5fad2012-03-05 09:21:12 +0100921static void handle_gs(struct fio_client *client, struct fio_net_cmd *cmd)
Jens Axboea64e88d2011-10-03 14:20:01 +0200922{
923 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
924
Jens Axboea64e88d2011-10-03 14:20:01 +0200925 show_group_stats(gs);
926}
927
Jens Axboe3bf236c2012-03-03 20:35:50 +0100928static void handle_text(struct fio_client *client, struct fio_net_cmd *cmd)
929{
930 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
931 const char *buf = (const char *) pdu->buf;
932 const char *name;
933 int fio_unused ret;
934
935 name = client->name ? client->name : client->hostname;
936
937 if (!client->skip_newline)
938 fprintf(f_out, "<%s> ", name);
939 ret = fwrite(buf, pdu->buf_len, 1, f_out);
940 fflush(f_out);
941 client->skip_newline = strchr(buf, '\n') == NULL;
942}
943
Jens Axboed09a64a2011-10-13 11:38:56 +0200944static void convert_agg(struct disk_util_agg *agg)
945{
946 int i;
947
948 for (i = 0; i < 2; i++) {
949 agg->ios[i] = le32_to_cpu(agg->ios[i]);
950 agg->merges[i] = le32_to_cpu(agg->merges[i]);
951 agg->sectors[i] = le64_to_cpu(agg->sectors[i]);
952 agg->ticks[i] = le32_to_cpu(agg->ticks[i]);
953 }
954
955 agg->io_ticks = le32_to_cpu(agg->io_ticks);
956 agg->time_in_queue = le32_to_cpu(agg->time_in_queue);
957 agg->slavecount = le32_to_cpu(agg->slavecount);
Jens Axboe2b827fa2014-10-13 16:05:10 -0600958 agg->max_util.u.f = fio_uint64_to_double(le64_to_cpu(agg->max_util.u.i));
Jens Axboed09a64a2011-10-13 11:38:56 +0200959}
960
961static void convert_dus(struct disk_util_stat *dus)
962{
963 int i;
964
965 for (i = 0; i < 2; i++) {
Jens Axboea3b4cf72014-04-11 12:17:53 -0600966 dus->s.ios[i] = le32_to_cpu(dus->s.ios[i]);
967 dus->s.merges[i] = le32_to_cpu(dus->s.merges[i]);
968 dus->s.sectors[i] = le64_to_cpu(dus->s.sectors[i]);
969 dus->s.ticks[i] = le32_to_cpu(dus->s.ticks[i]);
Jens Axboed09a64a2011-10-13 11:38:56 +0200970 }
971
Jens Axboea3b4cf72014-04-11 12:17:53 -0600972 dus->s.io_ticks = le32_to_cpu(dus->s.io_ticks);
973 dus->s.time_in_queue = le32_to_cpu(dus->s.time_in_queue);
974 dus->s.msec = le64_to_cpu(dus->s.msec);
Jens Axboed09a64a2011-10-13 11:38:56 +0200975}
976
977static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd)
978{
979 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
980
Jens Axboed09a64a2011-10-13 11:38:56 +0200981 if (!client->disk_stats_shown) {
982 client->disk_stats_shown = 1;
983 log_info("\nDisk stats (read/write):\n");
984 }
985
Castor Fu952b05e2013-10-31 11:00:34 -0600986 if (output_format == FIO_OUTPUT_JSON) {
987 struct json_object *duobj;
988 json_array_add_disk_util(&du->dus, &du->agg, du_array);
989 duobj = json_array_last_value_object(du_array);
990 json_object_add_client_info(duobj, client);
991 } else
992 print_disk_util(&du->dus, &du->agg, output_format == FIO_OUTPUT_TERSE);
Jens Axboed09a64a2011-10-13 11:38:56 +0200993}
994
Jens Axboe3bf236c2012-03-03 20:35:50 +0100995static void convert_jobs_eta(struct jobs_eta *je)
Jens Axboecf451d12011-10-03 16:48:30 +0200996{
Jens Axboecf451d12011-10-03 16:48:30 +0200997 int i;
998
999 je->nr_running = le32_to_cpu(je->nr_running);
1000 je->nr_ramp = le32_to_cpu(je->nr_ramp);
1001 je->nr_pending = le32_to_cpu(je->nr_pending);
Jens Axboe714e85f2013-04-15 10:21:56 +02001002 je->nr_setting_up = le32_to_cpu(je->nr_setting_up);
Jens Axboecf451d12011-10-03 16:48:30 +02001003 je->files_open = le32_to_cpu(je->files_open);
Jens Axboecf451d12011-10-03 16:48:30 +02001004
Jens Axboe298921d2012-09-24 18:47:01 +02001005 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1006 je->m_rate[i] = le32_to_cpu(je->m_rate[i]);
1007 je->t_rate[i] = le32_to_cpu(je->t_rate[i]);
1008 je->m_iops[i] = le32_to_cpu(je->m_iops[i]);
1009 je->t_iops[i] = le32_to_cpu(je->t_iops[i]);
Jens Axboe16725bb2013-04-11 12:43:05 +02001010 je->rate[i] = le32_to_cpu(je->rate[i]);
1011 je->iops[i] = le32_to_cpu(je->iops[i]);
Jens Axboecf451d12011-10-03 16:48:30 +02001012 }
1013
Jens Axboeb51eedb2011-10-09 12:13:39 +02001014 je->elapsed_sec = le64_to_cpu(je->elapsed_sec);
Jens Axboecf451d12011-10-03 16:48:30 +02001015 je->eta_sec = le64_to_cpu(je->eta_sec);
Jens Axboe8c621fb2012-03-08 12:30:48 +01001016 je->nr_threads = le32_to_cpu(je->nr_threads);
Jens Axboeb7f05eb2012-05-11 20:33:02 +02001017 je->is_pow2 = le32_to_cpu(je->is_pow2);
Jens Axboeed2c0a12013-04-11 12:55:16 +02001018 je->unit_base = le32_to_cpu(je->unit_base);
Jens Axboe48fbb462011-10-09 12:19:08 +02001019}
Jens Axboecf451d12011-10-03 16:48:30 +02001020
Jens Axboe3e47bd22012-02-29 13:45:02 +01001021void fio_client_sum_jobs_eta(struct jobs_eta *dst, struct jobs_eta *je)
Jens Axboe48fbb462011-10-09 12:19:08 +02001022{
Jens Axboe48fbb462011-10-09 12:19:08 +02001023 int i;
1024
1025 dst->nr_running += je->nr_running;
1026 dst->nr_ramp += je->nr_ramp;
1027 dst->nr_pending += je->nr_pending;
Jens Axboe714e85f2013-04-15 10:21:56 +02001028 dst->nr_setting_up += je->nr_setting_up;
Jens Axboe48fbb462011-10-09 12:19:08 +02001029 dst->files_open += je->files_open;
Jens Axboe48fbb462011-10-09 12:19:08 +02001030
Jens Axboe298921d2012-09-24 18:47:01 +02001031 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
Jens Axboe3e47bd22012-02-29 13:45:02 +01001032 dst->m_rate[i] += je->m_rate[i];
1033 dst->t_rate[i] += je->t_rate[i];
1034 dst->m_iops[i] += je->m_iops[i];
1035 dst->t_iops[i] += je->t_iops[i];
Jens Axboe16725bb2013-04-11 12:43:05 +02001036 dst->rate[i] += je->rate[i];
1037 dst->iops[i] += je->iops[i];
Jens Axboe48fbb462011-10-09 12:19:08 +02001038 }
1039
1040 dst->elapsed_sec += je->elapsed_sec;
1041
1042 if (je->eta_sec > dst->eta_sec)
1043 dst->eta_sec = je->eta_sec;
Jens Axboe8c621fb2012-03-08 12:30:48 +01001044
1045 dst->nr_threads += je->nr_threads;
Jens Axboe27b91552014-06-27 15:30:06 -06001046
1047 /*
1048 * This wont be correct for multiple strings, but at least it
1049 * works for the basic cases.
1050 */
1051 strcpy((char *) dst->run_str, (char *) je->run_str);
Jens Axboe48fbb462011-10-09 12:19:08 +02001052}
1053
Jens Axboea5276612012-03-04 15:15:08 +01001054void fio_client_dec_jobs_eta(struct client_eta *eta, client_eta_op eta_fn)
Jens Axboe82c1ed32011-10-10 08:56:18 +02001055{
1056 if (!--eta->pending) {
Jens Axboea5276612012-03-04 15:15:08 +01001057 eta_fn(&eta->eta);
Jens Axboe82c1ed32011-10-10 08:56:18 +02001058 free(eta);
1059 }
1060}
1061
Jens Axboe89c17072011-10-11 10:15:51 +02001062static void remove_reply_cmd(struct fio_client *client, struct fio_net_cmd *cmd)
1063{
Jens Axboe40c60512012-03-27 16:03:04 +02001064 struct fio_net_cmd_reply *reply = NULL;
Jens Axboe89c17072011-10-11 10:15:51 +02001065 struct flist_head *entry;
1066
1067 flist_for_each(entry, &client->cmd_list) {
Jens Axboe40c60512012-03-27 16:03:04 +02001068 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
Jens Axboe89c17072011-10-11 10:15:51 +02001069
Jens Axboe40c60512012-03-27 16:03:04 +02001070 if (cmd->tag == (uintptr_t) reply)
Jens Axboe89c17072011-10-11 10:15:51 +02001071 break;
1072
Jens Axboe40c60512012-03-27 16:03:04 +02001073 reply = NULL;
Jens Axboe89c17072011-10-11 10:15:51 +02001074 }
1075
Jens Axboe40c60512012-03-27 16:03:04 +02001076 if (!reply) {
Jens Axboe4e0a8fa2013-04-15 11:40:57 +02001077 log_err("fio: client: unable to find matching tag (%llx)\n", (unsigned long long) cmd->tag);
Jens Axboe89c17072011-10-11 10:15:51 +02001078 return;
1079 }
1080
Jens Axboe40c60512012-03-27 16:03:04 +02001081 flist_del(&reply->list);
1082 cmd->tag = reply->saved_tag;
1083 free(reply);
1084}
1085
1086int fio_client_wait_for_reply(struct fio_client *client, uint64_t tag)
1087{
1088 do {
1089 struct fio_net_cmd_reply *reply = NULL;
1090 struct flist_head *entry;
1091
1092 flist_for_each(entry, &client->cmd_list) {
1093 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
1094
1095 if (tag == (uintptr_t) reply)
1096 break;
1097
1098 reply = NULL;
1099 }
1100
1101 if (!reply)
1102 break;
1103
1104 usleep(1000);
1105 } while (1);
1106
1107 return 0;
Jens Axboe89c17072011-10-11 10:15:51 +02001108}
1109
Jens Axboe82c1ed32011-10-10 08:56:18 +02001110static void handle_eta(struct fio_client *client, struct fio_net_cmd *cmd)
Jens Axboe48fbb462011-10-09 12:19:08 +02001111{
1112 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
Jens Axboedf380932011-10-11 14:25:08 +02001113 struct client_eta *eta = (struct client_eta *) (uintptr_t) cmd->tag;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001114
1115 dprint(FD_NET, "client: got eta tag %p, %d\n", eta, eta->pending);
Jens Axboe48fbb462011-10-09 12:19:08 +02001116
Jens Axboef77d2672011-10-10 14:36:07 +02001117 assert(client->eta_in_flight == eta);
1118
1119 client->eta_in_flight = NULL;
Jens Axboe82c1ed32011-10-10 08:56:18 +02001120 flist_del_init(&client->eta_list);
1121
Jens Axboe2f99deb2012-03-09 14:37:29 +01001122 if (client->ops->jobs_eta)
1123 client->ops->jobs_eta(client, je);
1124
Jens Axboe3e47bd22012-02-29 13:45:02 +01001125 fio_client_sum_jobs_eta(&eta->eta, je);
Jens Axboea5276612012-03-04 15:15:08 +01001126 fio_client_dec_jobs_eta(eta, client->ops->eta);
Jens Axboecf451d12011-10-03 16:48:30 +02001127}
1128
Jens Axboeb5296dd2011-10-07 16:35:56 +02001129static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd)
Jens Axboe2e03b4b2011-10-04 09:00:40 +02001130{
Jens Axboe3989b142013-04-12 13:13:24 +02001131 struct cmd_probe_reply_pdu *probe = (struct cmd_probe_reply_pdu *) cmd->payload;
Jens Axboed2333352011-10-11 15:07:23 +02001132 const char *os, *arch;
1133 char bit[16];
Jens Axboe2e03b4b2011-10-04 09:00:40 +02001134
Jens Axboecca84642011-10-07 12:47:57 +02001135 os = fio_get_os_string(probe->os);
1136 if (!os)
1137 os = "unknown";
1138
1139 arch = fio_get_arch_string(probe->arch);
1140 if (!arch)
1141 os = "unknown";
1142
Jens Axboed2333352011-10-11 15:07:23 +02001143 sprintf(bit, "%d-bit", probe->bpp * 8);
Jens Axboe3989b142013-04-12 13:13:24 +02001144 probe->flags = le64_to_cpu(probe->flags);
Jens Axboe38fdef22011-10-11 14:30:06 +02001145
Jens Axboe3989b142013-04-12 13:13:24 +02001146 log_info("hostname=%s, be=%u, %s, os=%s, arch=%s, fio=%s, flags=%lx\n",
Jens Axboe38fdef22011-10-11 14:30:06 +02001147 probe->hostname, probe->bigendian, bit, os, arch,
Jens Axboe3989b142013-04-12 13:13:24 +02001148 probe->fio_version, (unsigned long) probe->flags);
Jens Axboeb5296dd2011-10-07 16:35:56 +02001149
1150 if (!client->name)
1151 client->name = strdup((char *) probe->hostname);
Jens Axboe2e03b4b2011-10-04 09:00:40 +02001152}
1153
Jens Axboe11e950b2011-10-16 21:34:14 +02001154static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd)
1155{
1156 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
1157
1158 client->state = Client_started;
1159 client->jobs = le32_to_cpu(pdu->jobs);
Jens Axboe30f8e312014-10-13 11:33:27 -06001160 client->nr_stat = le32_to_cpu(pdu->stat_outputs);
Jens Axboe108fea72012-11-14 13:09:45 -07001161
Jens Axboe30f8e312014-10-13 11:33:27 -06001162 sum_stat_clients += client->nr_stat;
Jens Axboe11e950b2011-10-16 21:34:14 +02001163}
1164
1165static void handle_stop(struct fio_client *client, struct fio_net_cmd *cmd)
1166{
Jens Axboe498c92c2011-10-17 09:14:42 +02001167 if (client->error)
1168 log_info("client <%s>: exited with error %d\n", client->hostname, client->error);
Jens Axboe11e950b2011-10-16 21:34:14 +02001169}
1170
Jens Axboe6b79c802012-03-08 10:51:36 +01001171static void convert_stop(struct fio_net_cmd *cmd)
1172{
1173 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
1174
1175 pdu->error = le32_to_cpu(pdu->error);
1176}
1177
Jens Axboe084d1c62012-03-03 20:28:07 +01001178static void convert_text(struct fio_net_cmd *cmd)
1179{
1180 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
1181
1182 pdu->level = le32_to_cpu(pdu->level);
1183 pdu->buf_len = le32_to_cpu(pdu->buf_len);
1184 pdu->log_sec = le64_to_cpu(pdu->log_sec);
1185 pdu->log_usec = le64_to_cpu(pdu->log_usec);
1186}
1187
Jens Axboe3989b142013-04-12 13:13:24 +02001188static struct cmd_iolog_pdu *convert_iolog_gz(struct fio_net_cmd *cmd,
1189 struct cmd_iolog_pdu *pdu)
Jens Axboe1b427252012-03-14 15:03:03 +01001190{
Jens Axboe3989b142013-04-12 13:13:24 +02001191#ifdef CONFIG_ZLIB
Jens Axboe1b427252012-03-14 15:03:03 +01001192 struct cmd_iolog_pdu *ret;
Jens Axboe1b427252012-03-14 15:03:03 +01001193 z_stream stream;
Jens Axboe3989b142013-04-12 13:13:24 +02001194 uint32_t nr_samples;
1195 size_t total;
Jens Axboe1b427252012-03-14 15:03:03 +01001196 void *p;
1197
1198 stream.zalloc = Z_NULL;
1199 stream.zfree = Z_NULL;
1200 stream.opaque = Z_NULL;
1201 stream.avail_in = 0;
1202 stream.next_in = Z_NULL;
1203
1204 if (inflateInit(&stream) != Z_OK)
1205 return NULL;
1206
1207 /*
Jens Axboef5ed7652012-03-14 16:28:07 +01001208 * Get header first, it's not compressed
Jens Axboe1b427252012-03-14 15:03:03 +01001209 */
Jens Axboef2b9a672014-07-01 08:47:11 -06001210 nr_samples = le64_to_cpu(pdu->nr_samples);
Jens Axboe1b427252012-03-14 15:03:03 +01001211
Jens Axboef2b9a672014-07-01 08:47:11 -06001212 total = nr_samples * __log_entry_sz(le32_to_cpu(pdu->log_offset));
Jens Axboef5ed7652012-03-14 16:28:07 +01001213 ret = malloc(total + sizeof(*pdu));
Jens Axboe1b427252012-03-14 15:03:03 +01001214 ret->nr_samples = nr_samples;
Jens Axboe3989b142013-04-12 13:13:24 +02001215
1216 memcpy(ret, pdu, sizeof(*pdu));
Jens Axboe1b427252012-03-14 15:03:03 +01001217
Jens Axboef5ed7652012-03-14 16:28:07 +01001218 p = (void *) ret + sizeof(*pdu);
1219
1220 stream.avail_in = cmd->pdu_len - sizeof(*pdu);
1221 stream.next_in = (void *) pdu + sizeof(*pdu);
Jens Axboe1b427252012-03-14 15:03:03 +01001222 while (stream.avail_in) {
1223 unsigned int this_chunk = 65536;
1224 unsigned int this_len;
1225 int err;
1226
1227 if (this_chunk > total)
1228 this_chunk = total;
1229
1230 stream.avail_out = this_chunk;
1231 stream.next_out = p;
1232 err = inflate(&stream, Z_NO_FLUSH);
Jens Axboe3c547fe2012-03-14 21:53:00 +01001233 /* may be Z_OK, or Z_STREAM_END */
1234 if (err < 0) {
Jens Axboe1b427252012-03-14 15:03:03 +01001235 log_err("fio: inflate error %d\n", err);
Jens Axboef5ed7652012-03-14 16:28:07 +01001236 free(ret);
1237 ret = NULL;
Jens Axboe3989b142013-04-12 13:13:24 +02001238 goto err;
Jens Axboe1b427252012-03-14 15:03:03 +01001239 }
1240
1241 this_len = this_chunk - stream.avail_out;
1242 p += this_len;
1243 total -= this_len;
1244 }
1245
Jens Axboe3989b142013-04-12 13:13:24 +02001246err:
1247 inflateEnd(&stream);
1248 return ret;
1249#else
1250 return NULL;
1251#endif
1252}
1253
1254/*
1255 * This has been compressed on the server side, since it can be big.
1256 * Uncompress here.
1257 */
1258static struct cmd_iolog_pdu *convert_iolog(struct fio_net_cmd *cmd)
1259{
1260 struct cmd_iolog_pdu *pdu = (struct cmd_iolog_pdu *) cmd->payload;
1261 struct cmd_iolog_pdu *ret;
Jens Axboeccefd5f2014-06-30 20:59:03 -06001262 uint64_t i;
1263 void *samples;
Jens Axboe3989b142013-04-12 13:13:24 +02001264
1265 /*
1266 * Convert if compressed and we support it. If it's not
1267 * compressed, we need not do anything.
1268 */
1269 if (le32_to_cpu(pdu->compressed)) {
1270#ifndef CONFIG_ZLIB
1271 log_err("fio: server sent compressed data by mistake\n");
1272 return NULL;
1273#endif
1274 ret = convert_iolog_gz(cmd, pdu);
1275 if (!ret) {
1276 log_err("fio: failed decompressing log\n");
1277 return NULL;
1278 }
1279 } else
1280 ret = pdu;
1281
Jens Axboeccefd5f2014-06-30 20:59:03 -06001282 ret->nr_samples = le64_to_cpu(ret->nr_samples);
Jens Axboe3989b142013-04-12 13:13:24 +02001283 ret->thread_number = le32_to_cpu(ret->thread_number);
Jens Axboe3989b142013-04-12 13:13:24 +02001284 ret->log_type = le32_to_cpu(ret->log_type);
1285 ret->compressed = le32_to_cpu(ret->compressed);
Jens Axboeccefd5f2014-06-30 20:59:03 -06001286 ret->log_offset = le32_to_cpu(ret->log_offset);
Jens Axboe3989b142013-04-12 13:13:24 +02001287
Jens Axboee8c4fb02014-07-01 16:07:59 -06001288 samples = &ret->samples[0];
Jens Axboef5ed7652012-03-14 16:28:07 +01001289 for (i = 0; i < ret->nr_samples; i++) {
Jens Axboeccefd5f2014-06-30 20:59:03 -06001290 struct io_sample *s;
Jens Axboef5ed7652012-03-14 16:28:07 +01001291
Jens Axboeccefd5f2014-06-30 20:59:03 -06001292 s = __get_sample(samples, ret->log_offset, i);
Jens Axboebac4af12014-07-03 13:42:28 -06001293 s->time = le64_to_cpu(s->time);
1294 s->val = le64_to_cpu(s->val);
1295 s->__ddir = le32_to_cpu(s->__ddir);
1296 s->bs = le32_to_cpu(s->bs);
Jens Axboeccefd5f2014-06-30 20:59:03 -06001297
1298 if (ret->log_offset) {
1299 struct io_sample_offset *so = (void *) s;
1300
1301 so->offset = le64_to_cpu(so->offset);
1302 }
Jens Axboef5ed7652012-03-14 16:28:07 +01001303 }
1304
Jens Axboe1b427252012-03-14 15:03:03 +01001305 return ret;
1306}
1307
Jens Axboea5276612012-03-04 15:15:08 +01001308int fio_handle_client(struct fio_client *client)
Jens Axboe37db14f2011-09-30 17:00:42 -06001309{
Jens Axboea5276612012-03-04 15:15:08 +01001310 struct client_ops *ops = client->ops;
Jens Axboe37db14f2011-09-30 17:00:42 -06001311 struct fio_net_cmd *cmd;
1312
Jens Axboe60efd142011-10-04 13:30:11 +02001313 dprint(FD_NET, "client: handle %s\n", client->hostname);
1314
Jens Axboee951bdc2011-10-05 21:58:45 +02001315 cmd = fio_net_recv_cmd(client->fd);
1316 if (!cmd)
1317 return 0;
Jens Axboec2c94582011-10-05 20:31:30 +02001318
Jens Axboeb9d2f302012-03-08 20:36:28 +01001319 dprint(FD_NET, "client: got cmd op %s from %s (pdu=%u)\n",
1320 fio_server_op(cmd->opcode), client->hostname, cmd->pdu_len);
Jens Axboe46c48f12011-10-01 12:50:51 -06001321
Jens Axboee951bdc2011-10-05 21:58:45 +02001322 switch (cmd->opcode) {
1323 case FIO_NET_CMD_QUIT:
Jens Axboe3ec62ec2012-03-01 12:01:29 +01001324 if (ops->quit)
Jens Axboe35c0ba72012-03-14 10:56:40 +01001325 ops->quit(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +02001326 remove_client(client);
1327 free(cmd);
1328 break;
Jens Axboe084d1c62012-03-03 20:28:07 +01001329 case FIO_NET_CMD_TEXT:
1330 convert_text(cmd);
Jens Axboe35c0ba72012-03-14 10:56:40 +01001331 ops->text(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +02001332 free(cmd);
1333 break;
Jens Axboe3bf236c2012-03-03 20:35:50 +01001334 case FIO_NET_CMD_DU: {
1335 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
1336
1337 convert_dus(&du->dus);
1338 convert_agg(&du->agg);
1339
Stephen M. Camerondd366722012-02-24 08:17:30 +01001340 ops->disk_util(client, cmd);
Jens Axboed09a64a2011-10-13 11:38:56 +02001341 free(cmd);
1342 break;
Jens Axboe3bf236c2012-03-03 20:35:50 +01001343 }
1344 case FIO_NET_CMD_TS: {
1345 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
1346
1347 convert_ts(&p->ts, &p->ts);
1348 convert_gs(&p->rs, &p->rs);
1349
Jens Axboe89e5fad2012-03-05 09:21:12 +01001350 ops->thread_status(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +02001351 free(cmd);
1352 break;
Jens Axboe3bf236c2012-03-03 20:35:50 +01001353 }
1354 case FIO_NET_CMD_GS: {
1355 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
1356
1357 convert_gs(gs, gs);
1358
Jens Axboe89e5fad2012-03-05 09:21:12 +01001359 ops->group_stats(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +02001360 free(cmd);
1361 break;
Jens Axboe3bf236c2012-03-03 20:35:50 +01001362 }
1363 case FIO_NET_CMD_ETA: {
1364 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
1365
Jens Axboe89c17072011-10-11 10:15:51 +02001366 remove_reply_cmd(client, cmd);
Jens Axboe3bf236c2012-03-03 20:35:50 +01001367 convert_jobs_eta(je);
Jens Axboea5276612012-03-04 15:15:08 +01001368 handle_eta(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +02001369 free(cmd);
1370 break;
Jens Axboe3bf236c2012-03-03 20:35:50 +01001371 }
Jens Axboee951bdc2011-10-05 21:58:45 +02001372 case FIO_NET_CMD_PROBE:
Jens Axboe89c17072011-10-11 10:15:51 +02001373 remove_reply_cmd(client, cmd);
Stephen M. Camerondd366722012-02-24 08:17:30 +01001374 ops->probe(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +02001375 free(cmd);
1376 break;
Jens Axboe5d7793a2012-03-08 19:59:16 +01001377 case FIO_NET_CMD_SERVER_START:
Jens Axboe01be0382011-10-15 14:43:41 +02001378 client->state = Client_running;
Jens Axboe85dd01e2012-03-12 14:33:16 +01001379 if (ops->job_start)
1380 ops->job_start(client, cmd);
Jens Axboe01be0382011-10-15 14:43:41 +02001381 free(cmd);
1382 break;
Jens Axboe85dd01e2012-03-12 14:33:16 +01001383 case FIO_NET_CMD_START: {
1384 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
1385
1386 pdu->jobs = le32_to_cpu(pdu->jobs);
1387 ops->start(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +02001388 free(cmd);
1389 break;
Jens Axboe85dd01e2012-03-12 14:33:16 +01001390 }
Jens Axboe6b79c802012-03-08 10:51:36 +01001391 case FIO_NET_CMD_STOP: {
1392 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
1393
1394 convert_stop(cmd);
1395 client->state = Client_stopped;
Jens Axboe122c7722012-03-19 09:13:15 +01001396 client->error = le32_to_cpu(pdu->error);
1397 client->signal = le32_to_cpu(pdu->signal);
Jens Axboe6b79c802012-03-08 10:51:36 +01001398 ops->stop(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +02001399 free(cmd);
1400 break;
Jens Axboe6b79c802012-03-08 10:51:36 +01001401 }
Jens Axboe40c60512012-03-27 16:03:04 +02001402 case FIO_NET_CMD_ADD_JOB: {
1403 struct cmd_add_job_pdu *pdu = (struct cmd_add_job_pdu *) cmd->payload;
1404
1405 client->thread_number = le32_to_cpu(pdu->thread_number);
1406 client->groupid = le32_to_cpu(pdu->groupid);
1407
Jens Axboe807f9972012-03-02 10:25:24 +01001408 if (ops->add_job)
1409 ops->add_job(client, cmd);
1410 free(cmd);
1411 break;
Jens Axboe40c60512012-03-27 16:03:04 +02001412 }
Jens Axboe1b427252012-03-14 15:03:03 +01001413 case FIO_NET_CMD_IOLOG:
1414 if (ops->iolog) {
1415 struct cmd_iolog_pdu *pdu;
1416
1417 pdu = convert_iolog(cmd);
1418 ops->iolog(client, pdu);
1419 }
1420 free(cmd);
1421 break;
Jens Axboe40c60512012-03-27 16:03:04 +02001422 case FIO_NET_CMD_UPDATE_JOB:
Jens Axboe40c60512012-03-27 16:03:04 +02001423 ops->update_job(client, cmd);
Jens Axboe649cee92012-03-28 09:15:32 +02001424 remove_reply_cmd(client, cmd);
Jens Axboe40c60512012-03-27 16:03:04 +02001425 free(cmd);
1426 break;
Jens Axboee951bdc2011-10-05 21:58:45 +02001427 default:
Jens Axboe89c17072011-10-11 10:15:51 +02001428 log_err("fio: unknown client op: %s\n", fio_server_op(cmd->opcode));
Jens Axboee951bdc2011-10-05 21:58:45 +02001429 free(cmd);
1430 break;
Jens Axboe37db14f2011-09-30 17:00:42 -06001431 }
1432
Jens Axboee951bdc2011-10-05 21:58:45 +02001433 return 1;
Jens Axboe37db14f2011-09-30 17:00:42 -06001434}
Jens Axboeb66570d2011-10-01 11:11:35 -06001435
Jens Axboea5276612012-03-04 15:15:08 +01001436static void request_client_etas(struct client_ops *ops)
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001437{
1438 struct fio_client *client;
1439 struct flist_head *entry;
1440 struct client_eta *eta;
Jens Axboe82c1ed32011-10-10 08:56:18 +02001441 int skipped = 0;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001442
1443 dprint(FD_NET, "client: request eta (%d)\n", nr_clients);
1444
Jens Axboe27b91552014-06-27 15:30:06 -06001445 eta = calloc(1, sizeof(*eta) + __THREAD_RUNSTR_SZ(REAL_MAX_JOBS));
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001446 eta->pending = nr_clients;
1447
1448 flist_for_each(entry, &client_list) {
1449 client = flist_entry(entry, struct fio_client, list);
1450
Jens Axboe82c1ed32011-10-10 08:56:18 +02001451 if (!flist_empty(&client->eta_list)) {
1452 skipped++;
1453 continue;
1454 }
Jens Axboe01be0382011-10-15 14:43:41 +02001455 if (client->state != Client_running)
1456 continue;
Jens Axboe82c1ed32011-10-10 08:56:18 +02001457
Jens Axboef77d2672011-10-10 14:36:07 +02001458 assert(!client->eta_in_flight);
Jens Axboe82c1ed32011-10-10 08:56:18 +02001459 flist_add_tail(&client->eta_list, &eta_list);
Jens Axboef77d2672011-10-10 14:36:07 +02001460 client->eta_in_flight = eta;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001461 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_SEND_ETA,
Jens Axboedf380932011-10-11 14:25:08 +02001462 (uintptr_t) eta, &client->cmd_list);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001463 }
1464
Jens Axboe82c1ed32011-10-10 08:56:18 +02001465 while (skipped--)
Jens Axboea5276612012-03-04 15:15:08 +01001466 fio_client_dec_jobs_eta(eta, ops->eta);
Jens Axboe82c1ed32011-10-10 08:56:18 +02001467
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001468 dprint(FD_NET, "client: requested eta tag %p\n", eta);
1469}
1470
Jens Axboe89c17072011-10-11 10:15:51 +02001471static int client_check_cmd_timeout(struct fio_client *client,
1472 struct timeval *now)
1473{
Jens Axboe40c60512012-03-27 16:03:04 +02001474 struct fio_net_cmd_reply *reply;
Jens Axboe89c17072011-10-11 10:15:51 +02001475 struct flist_head *entry, *tmp;
1476 int ret = 0;
1477
1478 flist_for_each_safe(entry, tmp, &client->cmd_list) {
Jens Axboe40c60512012-03-27 16:03:04 +02001479 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
Jens Axboe89c17072011-10-11 10:15:51 +02001480
Jens Axboe40c60512012-03-27 16:03:04 +02001481 if (mtime_since(&reply->tv, now) < FIO_NET_CLIENT_TIMEOUT)
Jens Axboe89c17072011-10-11 10:15:51 +02001482 continue;
1483
1484 log_err("fio: client %s, timeout on cmd %s\n", client->hostname,
Jens Axboe40c60512012-03-27 16:03:04 +02001485 fio_server_op(reply->opcode));
1486 flist_del(&reply->list);
1487 free(reply);
Jens Axboe89c17072011-10-11 10:15:51 +02001488 ret = 1;
1489 }
1490
1491 return flist_empty(&client->cmd_list) && ret;
1492}
1493
Jens Axboea5276612012-03-04 15:15:08 +01001494static int fio_check_clients_timed_out(void)
Jens Axboe89c17072011-10-11 10:15:51 +02001495{
1496 struct fio_client *client;
1497 struct flist_head *entry, *tmp;
1498 struct timeval tv;
1499 int ret = 0;
1500
Jens Axboe67bf9822013-01-10 11:23:19 +01001501 fio_gettime(&tv, NULL);
Jens Axboe89c17072011-10-11 10:15:51 +02001502
1503 flist_for_each_safe(entry, tmp, &client_list) {
1504 client = flist_entry(entry, struct fio_client, list);
1505
1506 if (flist_empty(&client->cmd_list))
1507 continue;
1508
1509 if (!client_check_cmd_timeout(client, &tv))
1510 continue;
1511
Jens Axboea5276612012-03-04 15:15:08 +01001512 if (client->ops->timed_out)
1513 client->ops->timed_out(client);
Jens Axboeed727a42012-03-02 12:14:40 +01001514 else
1515 log_err("fio: client %s timed out\n", client->hostname);
1516
Jens Axboe89c17072011-10-11 10:15:51 +02001517 remove_client(client);
1518 ret = 1;
1519 }
1520
1521 return ret;
1522}
1523
Stephen M. Camerondd366722012-02-24 08:17:30 +01001524int fio_handle_clients(struct client_ops *ops)
Jens Axboeb66570d2011-10-01 11:11:35 -06001525{
Jens Axboeb66570d2011-10-01 11:11:35 -06001526 struct pollfd *pfds;
Jens Axboe498c92c2011-10-17 09:14:42 +02001527 int i, ret = 0, retval = 0;
Jens Axboeb66570d2011-10-01 11:11:35 -06001528
Jens Axboe67bf9822013-01-10 11:23:19 +01001529 fio_gettime(&eta_tv, NULL);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001530
Jens Axboeb66570d2011-10-01 11:11:35 -06001531 pfds = malloc(nr_clients * sizeof(struct pollfd));
1532
Jens Axboe37f0c1a2011-10-11 14:08:33 +02001533 init_thread_stat(&client_ts);
1534 init_group_run_stat(&client_gs);
1535
Jens Axboeb66570d2011-10-01 11:11:35 -06001536 while (!exit_backend && nr_clients) {
Jens Axboec2cb6862012-02-23 20:56:12 +01001537 struct flist_head *entry, *tmp;
1538 struct fio_client *client;
1539
Jens Axboe82a4be12011-10-01 12:40:32 -06001540 i = 0;
Jens Axboec2cb6862012-02-23 20:56:12 +01001541 flist_for_each_safe(entry, tmp, &client_list) {
Jens Axboe82a4be12011-10-01 12:40:32 -06001542 client = flist_entry(entry, struct fio_client, list);
1543
Jens Axboea5276612012-03-04 15:15:08 +01001544 if (!client->sent_job && !client->ops->stay_connected &&
Jens Axboec2cb6862012-02-23 20:56:12 +01001545 flist_empty(&client->cmd_list)) {
1546 remove_client(client);
1547 continue;
1548 }
1549
Jens Axboe82a4be12011-10-01 12:40:32 -06001550 pfds[i].fd = client->fd;
1551 pfds[i].events = POLLIN;
1552 i++;
1553 }
1554
Jens Axboec2cb6862012-02-23 20:56:12 +01001555 if (!nr_clients)
1556 break;
1557
Jens Axboe82a4be12011-10-01 12:40:32 -06001558 assert(i == nr_clients);
1559
Jens Axboe5c2857f2011-10-04 13:14:32 +02001560 do {
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001561 struct timeval tv;
1562
Jens Axboe67bf9822013-01-10 11:23:19 +01001563 fio_gettime(&tv, NULL);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001564 if (mtime_since(&eta_tv, &tv) >= 900) {
Jens Axboea5276612012-03-04 15:15:08 +01001565 request_client_etas(ops);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001566 memcpy(&eta_tv, &tv, sizeof(tv));
Jens Axboe89c17072011-10-11 10:15:51 +02001567
Jens Axboea5276612012-03-04 15:15:08 +01001568 if (fio_check_clients_timed_out())
Jens Axboe89c17072011-10-11 10:15:51 +02001569 break;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001570 }
1571
Jens Axboe80102c82012-03-23 13:19:31 +01001572 ret = poll(pfds, nr_clients, ops->eta_msec);
Jens Axboe5c2857f2011-10-04 13:14:32 +02001573 if (ret < 0) {
1574 if (errno == EINTR)
1575 continue;
1576 log_err("fio: poll clients: %s\n", strerror(errno));
1577 break;
1578 } else if (!ret)
Jens Axboeb66570d2011-10-01 11:11:35 -06001579 continue;
Jens Axboe5c2857f2011-10-04 13:14:32 +02001580 } while (ret <= 0);
Jens Axboeb66570d2011-10-01 11:11:35 -06001581
1582 for (i = 0; i < nr_clients; i++) {
1583 if (!(pfds[i].revents & POLLIN))
1584 continue;
1585
1586 client = find_client_by_fd(pfds[i].fd);
1587 if (!client) {
Jens Axboe65e1a122013-08-30 10:13:36 -06001588 log_err("fio: unknown client fd %ld\n", (long) pfds[i].fd);
Jens Axboeb66570d2011-10-01 11:11:35 -06001589 continue;
1590 }
Jens Axboea5276612012-03-04 15:15:08 +01001591 if (!fio_handle_client(client)) {
Jens Axboe28d3ab02011-10-05 20:45:37 +02001592 log_info("client: host=%s disconnected\n",
1593 client->hostname);
1594 remove_client(client);
Jens Axboe498c92c2011-10-17 09:14:42 +02001595 retval = 1;
Jens Axboe38990762011-10-17 13:31:33 +02001596 } else if (client->error)
Jens Axboe498c92c2011-10-17 09:14:42 +02001597 retval = 1;
Jens Axboe343cb4a2012-03-09 17:16:51 +01001598 fio_put_client(client);
Jens Axboeb66570d2011-10-01 11:11:35 -06001599 }
1600 }
1601
Castor Fu952b05e2013-10-31 11:00:34 -06001602 fio_client_json_fini();
1603
Jens Axboeb66570d2011-10-01 11:11:35 -06001604 free(pfds);
Jens Axboe498c92c2011-10-17 09:14:42 +02001605 return retval;
Jens Axboeb66570d2011-10-01 11:11:35 -06001606}