blob: 828dd97b0b355c079fc978e9f65baa07ef37aa54 [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 Axboe14ea90e2012-08-26 17:33:34 +0200144 while (client->nr_ini_file)
145 free(client->ini_file[--client->nr_ini_file]);
146 if (client->ini_file)
147 free(client->ini_file);
Jens Axboe81179ee2011-10-04 12:42:06 +0200148
Jens Axboe108fea72012-11-14 13:09:45 -0700149 if (!client->did_stat)
150 sum_stat_clients -= client->nr_stat;
151
Jens Axboeb66570d2011-10-01 11:11:35 -0600152 free(client);
153}
Jens Axboe132159a2011-09-30 15:01:32 -0600154
Jens Axboe3af45202012-03-13 09:59:53 +0100155static void remove_client(struct fio_client *client)
Jens Axboe5121a9a2012-03-05 13:32:47 +0100156{
Jens Axboe3af45202012-03-13 09:59:53 +0100157 assert(client->refs);
158
159 dprint(FD_NET, "client: removed <%s>\n", client->hostname);
160
161 if (!flist_empty(&client->list))
162 flist_del_init(&client->list);
163
164 fio_client_remove_hash(client);
165
166 if (!flist_empty(&client->eta_list)) {
167 flist_del_init(&client->eta_list);
168 fio_client_dec_jobs_eta(client->eta_in_flight, client->ops->eta);
169 }
170
Jens Axboe1e0c1842012-03-20 15:15:00 +0100171 close(client->fd);
172 client->fd = -1;
173
Jens Axboe0cf3ece2012-03-21 10:15:20 +0100174 if (client->ops->removed)
175 client->ops->removed(client);
176
Jens Axboe3af45202012-03-13 09:59:53 +0100177 nr_clients--;
Jens Axboe3af45202012-03-13 09:59:53 +0100178 fio_put_client(client);
Jens Axboe5121a9a2012-03-05 13:32:47 +0100179}
180
Jens Axboe343cb4a2012-03-09 17:16:51 +0100181struct fio_client *fio_get_client(struct fio_client *client)
182{
183 client->refs++;
184 return client;
185}
186
Jens Axboefa2ea802011-10-08 21:07:29 +0200187static void __fio_client_add_cmd_option(struct fio_client *client,
188 const char *opt)
Jens Axboe81179ee2011-10-04 12:42:06 +0200189{
Jens Axboe39e8e012011-10-04 13:46:08 +0200190 int index;
191
192 index = client->argc++;
Jens Axboe81179ee2011-10-04 12:42:06 +0200193 client->argv = realloc(client->argv, sizeof(char *) * client->argc);
Jens Axboe39e8e012011-10-04 13:46:08 +0200194 client->argv[index] = strdup(opt);
195 dprint(FD_NET, "client: add cmd %d: %s\n", index, opt);
Jens Axboe81179ee2011-10-04 12:42:06 +0200196}
197
Jens Axboefa2ea802011-10-08 21:07:29 +0200198void fio_client_add_cmd_option(void *cookie, const char *opt)
Jens Axboe81179ee2011-10-04 12:42:06 +0200199{
Jens Axboebebe6392011-10-07 10:00:51 +0200200 struct fio_client *client = cookie;
Jens Axboe3f3a4542011-10-10 21:11:09 +0200201 struct flist_head *entry;
Jens Axboe81179ee2011-10-04 12:42:06 +0200202
Jens Axboebebe6392011-10-07 10:00:51 +0200203 if (!client || !opt)
Jens Axboefa2ea802011-10-08 21:07:29 +0200204 return;
Jens Axboe81179ee2011-10-04 12:42:06 +0200205
Jens Axboefa2ea802011-10-08 21:07:29 +0200206 __fio_client_add_cmd_option(client, opt);
Jens Axboe3f3a4542011-10-10 21:11:09 +0200207
208 /*
209 * Duplicate arguments to shared client group
210 */
211 flist_for_each(entry, &arg_list) {
212 client = flist_entry(entry, struct fio_client, arg_list);
213
214 __fio_client_add_cmd_option(client, opt);
215 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200216}
217
Jens Axboea5276612012-03-04 15:15:08 +0100218struct fio_client *fio_client_add_explicit(struct client_ops *ops,
219 const char *hostname, int type,
Jens Axboe3ec62ec2012-03-01 12:01:29 +0100220 int port)
221{
222 struct fio_client *client;
223
224 client = malloc(sizeof(*client));
225 memset(client, 0, sizeof(*client));
226
227 INIT_FLIST_HEAD(&client->list);
228 INIT_FLIST_HEAD(&client->hash_list);
229 INIT_FLIST_HEAD(&client->arg_list);
230 INIT_FLIST_HEAD(&client->eta_list);
231 INIT_FLIST_HEAD(&client->cmd_list);
232
233 client->hostname = strdup(hostname);
234
235 if (type == Fio_client_socket)
236 client->is_sock = 1;
237 else {
238 int ipv6;
239
240 ipv6 = type == Fio_client_ipv6;
Jens Axboe3aa3cee2014-01-24 18:56:56 -0800241 if (fio_server_parse_host(hostname, ipv6,
Jens Axboe3ec62ec2012-03-01 12:01:29 +0100242 &client->addr.sin_addr,
243 &client->addr6.sin6_addr))
244 goto err;
245
246 client->port = port;
247 }
248
249 client->fd = -1;
Jens Axboea5276612012-03-04 15:15:08 +0100250 client->ops = ops;
Jens Axboe5121a9a2012-03-05 13:32:47 +0100251 client->refs = 1;
Jens Axboe46bcd492012-03-14 11:31:21 +0100252 client->type = ops->client_type;
Jens Axboe3ec62ec2012-03-01 12:01:29 +0100253
254 __fio_client_add_cmd_option(client, "fio");
255
256 flist_add(&client->list, &client_list);
257 nr_clients++;
258 dprint(FD_NET, "client: added <%s>\n", client->hostname);
259 return client;
260err:
261 free(client);
262 return NULL;
263}
264
Jens Axboe14ea90e2012-08-26 17:33:34 +0200265void fio_client_add_ini_file(void *cookie, const char *ini_file)
266{
267 struct fio_client *client = cookie;
268 size_t new_size;
269
270 dprint(FD_NET, "client <%s>: add ini %s\n", client->hostname, ini_file);
271
272 new_size = (client->nr_ini_file + 1) * sizeof(char *);
273 client->ini_file = realloc(client->ini_file, new_size);
274 client->ini_file[client->nr_ini_file] = strdup(ini_file);
275 client->nr_ini_file++;
276}
277
Jens Axboea5276612012-03-04 15:15:08 +0100278int fio_client_add(struct client_ops *ops, const char *hostname, void **cookie)
Jens Axboe132159a2011-09-30 15:01:32 -0600279{
Jens Axboe3f3a4542011-10-10 21:11:09 +0200280 struct fio_client *existing = *cookie;
Jens Axboeb66570d2011-10-01 11:11:35 -0600281 struct fio_client *client;
Jens Axboe132159a2011-09-30 15:01:32 -0600282
Jens Axboe3f3a4542011-10-10 21:11:09 +0200283 if (existing) {
284 /*
285 * We always add our "exec" name as the option, hence 1
286 * means empty.
287 */
288 if (existing->argc == 1)
289 flist_add_tail(&existing->arg_list, &arg_list);
290 else {
291 while (!flist_empty(&arg_list))
292 flist_del_init(arg_list.next);
293 }
294 }
295
Jens Axboeb66570d2011-10-01 11:11:35 -0600296 client = malloc(sizeof(*client));
Jens Axboea37f69b2011-10-01 12:24:21 -0600297 memset(client, 0, sizeof(*client));
Jens Axboe81179ee2011-10-04 12:42:06 +0200298
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200299 INIT_FLIST_HEAD(&client->list);
Jens Axboebebe6392011-10-07 10:00:51 +0200300 INIT_FLIST_HEAD(&client->hash_list);
Jens Axboe3f3a4542011-10-10 21:11:09 +0200301 INIT_FLIST_HEAD(&client->arg_list);
Jens Axboe82c1ed32011-10-10 08:56:18 +0200302 INIT_FLIST_HEAD(&client->eta_list);
Jens Axboe89c17072011-10-11 10:15:51 +0200303 INIT_FLIST_HEAD(&client->cmd_list);
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200304
Jens Axboebebe6392011-10-07 10:00:51 +0200305 if (fio_server_parse_string(hostname, &client->hostname,
306 &client->is_sock, &client->port,
Jens Axboe811826b2011-10-24 09:11:50 +0200307 &client->addr.sin_addr,
308 &client->addr6.sin6_addr,
309 &client->ipv6))
Jens Axboebebe6392011-10-07 10:00:51 +0200310 return -1;
311
Jens Axboea37f69b2011-10-01 12:24:21 -0600312 client->fd = -1;
Jens Axboea5276612012-03-04 15:15:08 +0100313 client->ops = ops;
Jens Axboe5121a9a2012-03-05 13:32:47 +0100314 client->refs = 1;
Jens Axboe46bcd492012-03-14 11:31:21 +0100315 client->type = ops->client_type;
Jens Axboe81179ee2011-10-04 12:42:06 +0200316
317 __fio_client_add_cmd_option(client, "fio");
318
Jens Axboea37f69b2011-10-01 12:24:21 -0600319 flist_add(&client->list, &client_list);
320 nr_clients++;
Jens Axboebebe6392011-10-07 10:00:51 +0200321 dprint(FD_NET, "client: added <%s>\n", client->hostname);
322 *cookie = client;
323 return 0;
Jens Axboea37f69b2011-10-01 12:24:21 -0600324}
325
Jens Axboed824c3b2012-03-09 17:45:43 +0100326static void probe_client(struct fio_client *client)
327{
Jens Axboe3989b142013-04-12 13:13:24 +0200328 struct cmd_client_probe_pdu pdu;
329 uint64_t tag;
330
Jens Axboed824c3b2012-03-09 17:45:43 +0100331 dprint(FD_NET, "client: send probe\n");
332
Jens Axboe3989b142013-04-12 13:13:24 +0200333#ifdef CONFIG_ZLIB
334 pdu.flags = __le64_to_cpu(FIO_PROBE_FLAG_ZLIB);
335#else
336 pdu.flags = 0;
337#endif
338
339 fio_net_send_cmd(client->fd, FIO_NET_CMD_PROBE, &pdu, sizeof(pdu), &tag, &client->cmd_list);
Jens Axboed824c3b2012-03-09 17:45:43 +0100340}
341
Jens Axboe87aa8f12011-10-06 21:24:13 +0200342static int fio_client_connect_ip(struct fio_client *client)
Jens Axboea37f69b2011-10-01 12:24:21 -0600343{
Jens Axboe811826b2011-10-24 09:11:50 +0200344 struct sockaddr *addr;
Jens Axboe67bf9822013-01-10 11:23:19 +0100345 socklen_t socklen;
Jens Axboe811826b2011-10-24 09:11:50 +0200346 int fd, domain;
Jens Axboe132159a2011-09-30 15:01:32 -0600347
Jens Axboe811826b2011-10-24 09:11:50 +0200348 if (client->ipv6) {
349 client->addr6.sin6_family = AF_INET6;
350 client->addr6.sin6_port = htons(client->port);
351 domain = AF_INET6;
352 addr = (struct sockaddr *) &client->addr6;
353 socklen = sizeof(client->addr6);
354 } else {
355 client->addr.sin_family = AF_INET;
356 client->addr.sin_port = htons(client->port);
357 domain = AF_INET;
358 addr = (struct sockaddr *) &client->addr;
359 socklen = sizeof(client->addr);
360 }
Jens Axboe132159a2011-09-30 15:01:32 -0600361
Jens Axboe811826b2011-10-24 09:11:50 +0200362 fd = socket(domain, SOCK_STREAM, 0);
Jens Axboe132159a2011-09-30 15:01:32 -0600363 if (fd < 0) {
Jens Axboe25095e12012-03-09 17:09:55 +0100364 int ret = -errno;
365
Jens Axboe132159a2011-09-30 15:01:32 -0600366 log_err("fio: socket: %s\n", strerror(errno));
Jens Axboe25095e12012-03-09 17:09:55 +0100367 return ret;
Jens Axboe132159a2011-09-30 15:01:32 -0600368 }
369
Jens Axboe811826b2011-10-24 09:11:50 +0200370 if (connect(fd, addr, socklen) < 0) {
Jens Axboe25095e12012-03-09 17:09:55 +0100371 int ret = -errno;
372
Jens Axboe132159a2011-09-30 15:01:32 -0600373 log_err("fio: connect: %s\n", strerror(errno));
Jens Axboea7de0a12011-10-07 12:55:14 +0200374 log_err("fio: failed to connect to %s:%u\n", client->hostname,
375 client->port);
Jens Axboeb94cba42011-10-06 21:27:10 +0200376 close(fd);
Jens Axboe25095e12012-03-09 17:09:55 +0100377 return ret;
Jens Axboe132159a2011-09-30 15:01:32 -0600378 }
379
Jens Axboe87aa8f12011-10-06 21:24:13 +0200380 return fd;
381}
382
383static int fio_client_connect_sock(struct fio_client *client)
384{
385 struct sockaddr_un *addr = &client->addr_un;
Jens Axboe67bf9822013-01-10 11:23:19 +0100386 socklen_t len;
Jens Axboe87aa8f12011-10-06 21:24:13 +0200387 int fd;
388
389 memset(addr, 0, sizeof(*addr));
390 addr->sun_family = AF_UNIX;
Jens Axboe1cb96412014-04-14 08:50:33 -0600391 strncpy(addr->sun_path, client->hostname, sizeof(addr->sun_path) - 1);
Jens Axboe87aa8f12011-10-06 21:24:13 +0200392
393 fd = socket(AF_UNIX, SOCK_STREAM, 0);
394 if (fd < 0) {
Jens Axboe25095e12012-03-09 17:09:55 +0100395 int ret = -errno;
396
Jens Axboe87aa8f12011-10-06 21:24:13 +0200397 log_err("fio: socket: %s\n", strerror(errno));
Jens Axboe25095e12012-03-09 17:09:55 +0100398 return ret;
Jens Axboe87aa8f12011-10-06 21:24:13 +0200399 }
400
401 len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
402 if (connect(fd, (struct sockaddr *) addr, len) < 0) {
Jens Axboe25095e12012-03-09 17:09:55 +0100403 int ret = -errno;
404
Jens Axboe87aa8f12011-10-06 21:24:13 +0200405 log_err("fio: connect; %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200406 close(fd);
Jens Axboe25095e12012-03-09 17:09:55 +0100407 return ret;
Jens Axboe87aa8f12011-10-06 21:24:13 +0200408 }
409
410 return fd;
411}
412
Jens Axboe2f99deb2012-03-09 14:37:29 +0100413int fio_client_connect(struct fio_client *client)
Jens Axboe87aa8f12011-10-06 21:24:13 +0200414{
415 int fd;
416
417 dprint(FD_NET, "client: connect to host %s\n", client->hostname);
418
Jens Axboe87aa8f12011-10-06 21:24:13 +0200419 if (client->is_sock)
420 fd = fio_client_connect_sock(client);
421 else
422 fd = fio_client_connect_ip(client);
423
Jens Axboe89c17072011-10-11 10:15:51 +0200424 dprint(FD_NET, "client: %s connected %d\n", client->hostname, fd);
425
Jens Axboe87aa8f12011-10-06 21:24:13 +0200426 if (fd < 0)
Jens Axboea1a3ed52012-03-09 17:00:01 +0100427 return fd;
Jens Axboe87aa8f12011-10-06 21:24:13 +0200428
Jens Axboeb66570d2011-10-01 11:11:35 -0600429 client->fd = fd;
Jens Axboebebe6392011-10-07 10:00:51 +0200430 fio_client_add_hash(client);
Jens Axboe81179ee2011-10-04 12:42:06 +0200431 client->state = Client_connected;
Jens Axboed824c3b2012-03-09 17:45:43 +0100432
433 probe_client(client);
Jens Axboe132159a2011-09-30 15:01:32 -0600434 return 0;
435}
436
Jens Axboe0cf3ece2012-03-21 10:15:20 +0100437int fio_client_terminate(struct fio_client *client)
Jens Axboe2f99deb2012-03-09 14:37:29 +0100438{
Jens Axboe0cf3ece2012-03-21 10:15:20 +0100439 return fio_net_send_quit(client->fd);
Jens Axboe2f99deb2012-03-09 14:37:29 +0100440}
441
Jens Axboecc0df002011-10-03 20:53:32 +0200442void fio_clients_terminate(void)
443{
444 struct flist_head *entry;
445 struct fio_client *client;
446
Jens Axboe60efd142011-10-04 13:30:11 +0200447 dprint(FD_NET, "client: terminate clients\n");
448
Jens Axboecc0df002011-10-03 20:53:32 +0200449 flist_for_each(entry, &client_list) {
450 client = flist_entry(entry, struct fio_client, list);
Jens Axboe2f99deb2012-03-09 14:37:29 +0100451 fio_client_terminate(client);
Jens Axboecc0df002011-10-03 20:53:32 +0200452 }
453}
454
455static void sig_int(int sig)
456{
Jens Axboebebe6392011-10-07 10:00:51 +0200457 dprint(FD_NET, "client: got signal %d\n", sig);
Jens Axboecc0df002011-10-03 20:53:32 +0200458 fio_clients_terminate();
459}
460
Jens Axboeb852e7c2012-03-30 10:30:35 +0200461static void sig_show_status(int sig)
462{
463 show_running_run_stats();
464}
465
Jens Axboecc0df002011-10-03 20:53:32 +0200466static void client_signal_handler(void)
467{
468 struct sigaction act;
469
470 memset(&act, 0, sizeof(act));
471 act.sa_handler = sig_int;
472 act.sa_flags = SA_RESTART;
473 sigaction(SIGINT, &act, NULL);
474
475 memset(&act, 0, sizeof(act));
476 act.sa_handler = sig_int;
477 act.sa_flags = SA_RESTART;
478 sigaction(SIGTERM, &act, NULL);
Jens Axboeb852e7c2012-03-30 10:30:35 +0200479
Bruce Cran2f694502012-10-10 16:34:13 +0100480/* Windows uses SIGBREAK as a quit signal from other applications */
481#ifdef WIN32
482 memset(&act, 0, sizeof(act));
483 act.sa_handler = sig_int;
484 act.sa_flags = SA_RESTART;
485 sigaction(SIGBREAK, &act, NULL);
486#endif
487
Jens Axboeb852e7c2012-03-30 10:30:35 +0200488 memset(&act, 0, sizeof(act));
489 act.sa_handler = sig_show_status;
490 act.sa_flags = SA_RESTART;
491 sigaction(SIGUSR1, &act, NULL);
Jens Axboecc0df002011-10-03 20:53:32 +0200492}
493
Jens Axboe81179ee2011-10-04 12:42:06 +0200494static int send_client_cmd_line(struct fio_client *client)
495{
Jens Axboefa2ea802011-10-08 21:07:29 +0200496 struct cmd_single_line_pdu *cslp;
497 struct cmd_line_pdu *clp;
498 unsigned long offset;
Jens Axboe7f868312011-10-10 09:55:21 +0200499 unsigned int *lens;
Jens Axboefa2ea802011-10-08 21:07:29 +0200500 void *pdu;
501 size_t mem;
Jens Axboe81179ee2011-10-04 12:42:06 +0200502 int i, ret;
503
Jens Axboe39e8e012011-10-04 13:46:08 +0200504 dprint(FD_NET, "client: send cmdline %d\n", client->argc);
Jens Axboe60efd142011-10-04 13:30:11 +0200505
Jens Axboe7f868312011-10-10 09:55:21 +0200506 lens = malloc(client->argc * sizeof(unsigned int));
507
Jens Axboefa2ea802011-10-08 21:07:29 +0200508 /*
509 * Find out how much mem we need
510 */
Jens Axboe7f868312011-10-10 09:55:21 +0200511 for (i = 0, mem = 0; i < client->argc; i++) {
512 lens[i] = strlen(client->argv[i]) + 1;
513 mem += lens[i];
514 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200515
Jens Axboefa2ea802011-10-08 21:07:29 +0200516 /*
517 * We need one cmd_line_pdu, and argc number of cmd_single_line_pdu
518 */
519 mem += sizeof(*clp) + (client->argc * sizeof(*cslp));
520
521 pdu = malloc(mem);
522 clp = pdu;
523 offset = sizeof(*clp);
524
525 for (i = 0; i < client->argc; i++) {
Jens Axboe7f868312011-10-10 09:55:21 +0200526 uint16_t arg_len = lens[i];
Jens Axboefa2ea802011-10-08 21:07:29 +0200527
528 cslp = pdu + offset;
529 strcpy((char *) cslp->text, client->argv[i]);
530 cslp->len = cpu_to_le16(arg_len);
531 offset += sizeof(*cslp) + arg_len;
532 }
533
Jens Axboe7f868312011-10-10 09:55:21 +0200534 free(lens);
Jens Axboefa2ea802011-10-08 21:07:29 +0200535 clp->lines = cpu_to_le16(client->argc);
Jens Axboe46bcd492012-03-14 11:31:21 +0100536 clp->client_type = __cpu_to_le16(client->type);
Jens Axboe40c60512012-03-27 16:03:04 +0200537 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOBLINE, pdu, mem, NULL, NULL);
Jens Axboe81179ee2011-10-04 12:42:06 +0200538 free(pdu);
539 return ret;
540}
541
Jens Axboea37f69b2011-10-01 12:24:21 -0600542int fio_clients_connect(void)
543{
544 struct fio_client *client;
545 struct flist_head *entry, *tmp;
546 int ret;
547
Bruce Cran93bcfd22012-02-20 20:18:19 +0100548#ifdef WIN32
549 WSADATA wsd;
Jens Axboe3c3ed072012-03-27 09:12:39 +0200550 WSAStartup(MAKEWORD(2, 2), &wsd);
Bruce Cran93bcfd22012-02-20 20:18:19 +0100551#endif
552
Jens Axboe60efd142011-10-04 13:30:11 +0200553 dprint(FD_NET, "client: connect all\n");
554
Jens Axboecc0df002011-10-03 20:53:32 +0200555 client_signal_handler();
556
Jens Axboea37f69b2011-10-01 12:24:21 -0600557 flist_for_each_safe(entry, tmp, &client_list) {
558 client = flist_entry(entry, struct fio_client, list);
559
560 ret = fio_client_connect(client);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200561 if (ret) {
Jens Axboea37f69b2011-10-01 12:24:21 -0600562 remove_client(client);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200563 continue;
564 }
565
Jens Axboe81179ee2011-10-04 12:42:06 +0200566 if (client->argc > 1)
567 send_client_cmd_line(client);
Jens Axboea37f69b2011-10-01 12:24:21 -0600568 }
569
570 return !nr_clients;
571}
572
Jens Axboeb9d2f302012-03-08 20:36:28 +0100573int fio_start_client(struct fio_client *client)
574{
575 dprint(FD_NET, "client: start %s\n", client->hostname);
576 return fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_RUN, 0, NULL);
577}
578
579int fio_start_all_clients(void)
580{
581 struct fio_client *client;
582 struct flist_head *entry, *tmp;
583 int ret;
584
585 dprint(FD_NET, "client: start all\n");
586
Castor Fu952b05e2013-10-31 11:00:34 -0600587 fio_client_json_init();
588
Jens Axboeb9d2f302012-03-08 20:36:28 +0100589 flist_for_each_safe(entry, tmp, &client_list) {
590 client = flist_entry(entry, struct fio_client, list);
591
592 ret = fio_start_client(client);
593 if (ret) {
594 remove_client(client);
595 continue;
596 }
597 }
598
599 return flist_empty(&client_list);
600}
601
Jens Axboe132159a2011-09-30 15:01:32 -0600602/*
603 * Send file contents to server backend. We could use sendfile(), but to remain
604 * more portable lets just read/write the darn thing.
605 */
Jens Axboe9988ca72012-03-09 15:14:06 +0100606static int __fio_client_send_ini(struct fio_client *client, const char *filename)
Jens Axboe132159a2011-09-30 15:01:32 -0600607{
Jens Axboe46bcd492012-03-14 11:31:21 +0100608 struct cmd_job_pdu *pdu;
609 size_t p_size;
Jens Axboe132159a2011-09-30 15:01:32 -0600610 struct stat sb;
Jens Axboe46bcd492012-03-14 11:31:21 +0100611 char *p;
612 void *buf;
Jens Axboe132159a2011-09-30 15:01:32 -0600613 off_t len;
614 int fd, ret;
615
Jens Axboe46c48f12011-10-01 12:50:51 -0600616 dprint(FD_NET, "send ini %s to %s\n", filename, client->hostname);
617
Jens Axboe132159a2011-09-30 15:01:32 -0600618 fd = open(filename, O_RDONLY);
619 if (fd < 0) {
Jens Axboe25095e12012-03-09 17:09:55 +0100620 int ret = -errno;
621
Jens Axboee951bdc2011-10-05 21:58:45 +0200622 log_err("fio: job file <%s> open: %s\n", filename, strerror(errno));
Jens Axboe25095e12012-03-09 17:09:55 +0100623 return ret;
Jens Axboe132159a2011-09-30 15:01:32 -0600624 }
625
626 if (fstat(fd, &sb) < 0) {
Jens Axboe25095e12012-03-09 17:09:55 +0100627 int ret = -errno;
628
Jens Axboe132159a2011-09-30 15:01:32 -0600629 log_err("fio: job file stat: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200630 close(fd);
Jens Axboe25095e12012-03-09 17:09:55 +0100631 return ret;
Jens Axboe132159a2011-09-30 15:01:32 -0600632 }
633
Jens Axboe46bcd492012-03-14 11:31:21 +0100634 p_size = sb.st_size + sizeof(*pdu);
635 pdu = malloc(p_size);
636 buf = pdu->buf;
Jens Axboe132159a2011-09-30 15:01:32 -0600637
638 len = sb.st_size;
639 p = buf;
640 do {
641 ret = read(fd, p, len);
642 if (ret > 0) {
643 len -= ret;
644 if (!len)
645 break;
646 p += ret;
647 continue;
648 } else if (!ret)
649 break;
650 else if (errno == EAGAIN || errno == EINTR)
651 continue;
652 } while (1);
653
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200654 if (len) {
655 log_err("fio: failed reading job file %s\n", filename);
Jens Axboeb94cba42011-10-06 21:27:10 +0200656 close(fd);
Hong Zhiguo03a22d92013-10-16 08:35:12 -0600657 free(pdu);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200658 return 1;
659 }
660
Jens Axboe46bcd492012-03-14 11:31:21 +0100661 pdu->buf_len = __cpu_to_le32(sb.st_size);
662 pdu->client_type = cpu_to_le32(client->type);
663
Jens Axboec2cb6862012-02-23 20:56:12 +0100664 client->sent_job = 1;
Jens Axboe40c60512012-03-27 16:03:04 +0200665 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, pdu, p_size, NULL, NULL);
Jens Axboe46bcd492012-03-14 11:31:21 +0100666 free(pdu);
Jens Axboeb94cba42011-10-06 21:27:10 +0200667 close(fd);
Jens Axboe132159a2011-09-30 15:01:32 -0600668 return ret;
669}
Jens Axboe37db14f2011-09-30 17:00:42 -0600670
Jens Axboe9988ca72012-03-09 15:14:06 +0100671int fio_client_send_ini(struct fio_client *client, const char *filename)
672{
Jens Axboe25095e12012-03-09 17:09:55 +0100673 int ret;
674
675 ret = __fio_client_send_ini(client, filename);
Jens Axboe3af45202012-03-13 09:59:53 +0100676 if (!ret)
677 client->sent_job = 1;
Jens Axboe9988ca72012-03-09 15:14:06 +0100678
Jens Axboe25095e12012-03-09 17:09:55 +0100679 return ret;
Jens Axboe9988ca72012-03-09 15:14:06 +0100680}
681
Jens Axboea37f69b2011-10-01 12:24:21 -0600682int fio_clients_send_ini(const char *filename)
683{
684 struct fio_client *client;
685 struct flist_head *entry, *tmp;
686
687 flist_for_each_safe(entry, tmp, &client_list) {
688 client = flist_entry(entry, struct fio_client, list);
689
Jens Axboe14ea90e2012-08-26 17:33:34 +0200690 if (client->nr_ini_file) {
691 int i;
692
693 for (i = 0; i < client->nr_ini_file; i++) {
694 const char *ini = client->ini_file[i];
695
696 if (fio_client_send_ini(client, ini)) {
697 remove_client(client);
698 break;
699 }
700 }
701 } else if (!filename || fio_client_send_ini(client, filename))
Jens Axboe3af45202012-03-13 09:59:53 +0100702 remove_client(client);
Jens Axboea37f69b2011-10-01 12:24:21 -0600703 }
704
705 return !nr_clients;
706}
707
Jens Axboe40c60512012-03-27 16:03:04 +0200708int fio_client_update_options(struct fio_client *client,
709 struct thread_options *o, uint64_t *tag)
710{
711 struct cmd_add_job_pdu pdu;
712
713 pdu.thread_number = cpu_to_le32(client->thread_number);
714 pdu.groupid = cpu_to_le32(client->groupid);
715 convert_thread_options_to_net(&pdu.top, o);
Castor Fu952b05e2013-10-31 11:00:34 -0600716
Jens Axboe40c60512012-03-27 16:03:04 +0200717 return fio_net_send_cmd(client->fd, FIO_NET_CMD_UPDATE_JOB, &pdu, sizeof(pdu), tag, &client->cmd_list);
718}
719
Jens Axboea64e88d2011-10-03 14:20:01 +0200720static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
721{
722 dst->max_val = le64_to_cpu(src->max_val);
723 dst->min_val = le64_to_cpu(src->min_val);
724 dst->samples = le64_to_cpu(src->samples);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200725
726 /*
727 * Floats arrive as IEEE 754 encoded uint64_t, convert back to double
728 */
729 dst->mean.u.f = fio_uint64_to_double(le64_to_cpu(dst->mean.u.i));
730 dst->S.u.f = fio_uint64_to_double(le64_to_cpu(dst->S.u.i));
Jens Axboea64e88d2011-10-03 14:20:01 +0200731}
732
733static void convert_ts(struct thread_stat *dst, struct thread_stat *src)
734{
735 int i, j;
736
Jens Axboe2f122b12012-03-15 13:10:19 +0100737 dst->error = le32_to_cpu(src->error);
738 dst->thread_number = le32_to_cpu(src->thread_number);
739 dst->groupid = le32_to_cpu(src->groupid);
740 dst->pid = le32_to_cpu(src->pid);
741 dst->members = le32_to_cpu(src->members);
Jens Axboe771e58b2013-01-30 12:56:23 +0100742 dst->unified_rw_rep = le32_to_cpu(src->unified_rw_rep);
Jens Axboea64e88d2011-10-03 14:20:01 +0200743
Jens Axboe298921d2012-09-24 18:47:01 +0200744 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
Jens Axboea64e88d2011-10-03 14:20:01 +0200745 convert_io_stat(&dst->clat_stat[i], &src->clat_stat[i]);
746 convert_io_stat(&dst->slat_stat[i], &src->slat_stat[i]);
747 convert_io_stat(&dst->lat_stat[i], &src->lat_stat[i]);
748 convert_io_stat(&dst->bw_stat[i], &src->bw_stat[i]);
749 }
750
751 dst->usr_time = le64_to_cpu(src->usr_time);
752 dst->sys_time = le64_to_cpu(src->sys_time);
753 dst->ctx = le64_to_cpu(src->ctx);
754 dst->minf = le64_to_cpu(src->minf);
755 dst->majf = le64_to_cpu(src->majf);
756 dst->clat_percentiles = le64_to_cpu(src->clat_percentiles);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200757
758 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
759 fio_fp64_t *fps = &src->percentile_list[i];
760 fio_fp64_t *fpd = &dst->percentile_list[i];
761
762 fpd->u.f = fio_uint64_to_double(le64_to_cpu(fps->u.i));
763 }
Jens Axboea64e88d2011-10-03 14:20:01 +0200764
765 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
766 dst->io_u_map[i] = le32_to_cpu(src->io_u_map[i]);
767 dst->io_u_submit[i] = le32_to_cpu(src->io_u_submit[i]);
768 dst->io_u_complete[i] = le32_to_cpu(src->io_u_complete[i]);
769 }
770
771 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
772 dst->io_u_lat_u[i] = le32_to_cpu(src->io_u_lat_u[i]);
773 dst->io_u_lat_m[i] = le32_to_cpu(src->io_u_lat_m[i]);
774 }
775
Jens Axboe298921d2012-09-24 18:47:01 +0200776 for (i = 0; i < DDIR_RWDIR_CNT; i++)
Jens Axboea64e88d2011-10-03 14:20:01 +0200777 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
778 dst->io_u_plat[i][j] = le32_to_cpu(src->io_u_plat[i][j]);
779
Jens Axboe78799de2013-01-29 20:53:52 +0100780 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
Jens Axboea64e88d2011-10-03 14:20:01 +0200781 dst->total_io_u[i] = le64_to_cpu(src->total_io_u[i]);
Jens Axboe93eee042011-10-03 21:08:48 +0200782 dst->short_io_u[i] = le64_to_cpu(src->short_io_u[i]);
Jens Axboea64e88d2011-10-03 14:20:01 +0200783 }
784
785 dst->total_submit = le64_to_cpu(src->total_submit);
786 dst->total_complete = le64_to_cpu(src->total_complete);
787
Jens Axboe298921d2012-09-24 18:47:01 +0200788 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
Jens Axboea64e88d2011-10-03 14:20:01 +0200789 dst->io_bytes[i] = le64_to_cpu(src->io_bytes[i]);
790 dst->runtime[i] = le64_to_cpu(src->runtime[i]);
791 }
792
793 dst->total_run_time = le64_to_cpu(src->total_run_time);
794 dst->continue_on_error = le16_to_cpu(src->continue_on_error);
795 dst->total_err_count = le64_to_cpu(src->total_err_count);
Jens Axboeddcc0b62011-10-03 14:45:27 +0200796 dst->first_error = le32_to_cpu(src->first_error);
797 dst->kb_base = le32_to_cpu(src->kb_base);
Steven Noonanad705bc2013-04-08 15:05:25 -0700798 dst->unit_base = le32_to_cpu(src->unit_base);
Jens Axboe3e260a42013-12-09 12:38:53 -0700799
800 dst->latency_depth = le32_to_cpu(src->latency_depth);
801 dst->latency_target = le64_to_cpu(src->latency_target);
802 dst->latency_window = le64_to_cpu(src->latency_window);
803 dst->latency_percentile.u.f = fio_uint64_to_double(le64_to_cpu(src->latency_percentile.u.i));
Jens Axboea64e88d2011-10-03 14:20:01 +0200804}
805
806static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
807{
808 int i;
809
Jens Axboe298921d2012-09-24 18:47:01 +0200810 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
Jens Axboea64e88d2011-10-03 14:20:01 +0200811 dst->max_run[i] = le64_to_cpu(src->max_run[i]);
812 dst->min_run[i] = le64_to_cpu(src->min_run[i]);
813 dst->max_bw[i] = le64_to_cpu(src->max_bw[i]);
814 dst->min_bw[i] = le64_to_cpu(src->min_bw[i]);
815 dst->io_kb[i] = le64_to_cpu(src->io_kb[i]);
816 dst->agg[i] = le64_to_cpu(src->agg[i]);
817 }
818
819 dst->kb_base = le32_to_cpu(src->kb_base);
Steven Noonanad705bc2013-04-08 15:05:25 -0700820 dst->unit_base = le32_to_cpu(src->unit_base);
Jens Axboea64e88d2011-10-03 14:20:01 +0200821 dst->groupid = le32_to_cpu(src->groupid);
Jens Axboe771e58b2013-01-30 12:56:23 +0100822 dst->unified_rw_rep = le32_to_cpu(src->unified_rw_rep);
Jens Axboea64e88d2011-10-03 14:20:01 +0200823}
824
Castor Fu952b05e2013-10-31 11:00:34 -0600825static void json_object_add_client_info(struct json_object *obj,
826struct fio_client *client)
827{
828 json_object_add_value_string(obj, "hostname", client->hostname);
829 json_object_add_value_int(obj, "port", client->port);
830}
831
Jens Axboe89e5fad2012-03-05 09:21:12 +0100832static void handle_ts(struct fio_client *client, struct fio_net_cmd *cmd)
Jens Axboea64e88d2011-10-03 14:20:01 +0200833{
834 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
Castor Fu952b05e2013-10-31 11:00:34 -0600835 struct json_object *tsobj;
Jens Axboea64e88d2011-10-03 14:20:01 +0200836
Castor Fu952b05e2013-10-31 11:00:34 -0600837 tsobj = show_thread_status(&p->ts, &p->rs);
Jens Axboe108fea72012-11-14 13:09:45 -0700838 client->did_stat = 1;
Castor Fu952b05e2013-10-31 11:00:34 -0600839 if (tsobj) {
840 json_object_add_client_info(tsobj, client);
841 json_array_add_value_object(clients_array, tsobj);
842 }
Jens Axboe37f0c1a2011-10-11 14:08:33 +0200843
Jens Axboe108fea72012-11-14 13:09:45 -0700844 if (!do_output_all_clients)
Jens Axboe37f0c1a2011-10-11 14:08:33 +0200845 return;
846
847 sum_thread_stats(&client_ts, &p->ts, sum_stat_nr);
848 sum_group_stats(&client_gs, &p->rs);
849
850 client_ts.members++;
Jens Axboe2f122b12012-03-15 13:10:19 +0100851 client_ts.thread_number = p->ts.thread_number;
Jens Axboe37f0c1a2011-10-11 14:08:33 +0200852 client_ts.groupid = p->ts.groupid;
Jens Axboe771e58b2013-01-30 12:56:23 +0100853 client_ts.unified_rw_rep = p->ts.unified_rw_rep;
Jens Axboe37f0c1a2011-10-11 14:08:33 +0200854
855 if (++sum_stat_nr == sum_stat_clients) {
856 strcpy(client_ts.name, "All clients");
Castor Fu952b05e2013-10-31 11:00:34 -0600857 tsobj = show_thread_status(&client_ts, &client_gs);
858 if (tsobj) {
859 json_object_add_client_info(tsobj, client);
860 json_array_add_value_object(clients_array, tsobj);
861 }
Jens Axboe37f0c1a2011-10-11 14:08:33 +0200862 }
Jens Axboea64e88d2011-10-03 14:20:01 +0200863}
864
Jens Axboe89e5fad2012-03-05 09:21:12 +0100865static void handle_gs(struct fio_client *client, struct fio_net_cmd *cmd)
Jens Axboea64e88d2011-10-03 14:20:01 +0200866{
867 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
868
Jens Axboea64e88d2011-10-03 14:20:01 +0200869 show_group_stats(gs);
870}
871
Jens Axboe3bf236c2012-03-03 20:35:50 +0100872static void handle_text(struct fio_client *client, struct fio_net_cmd *cmd)
873{
874 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
875 const char *buf = (const char *) pdu->buf;
876 const char *name;
877 int fio_unused ret;
878
879 name = client->name ? client->name : client->hostname;
880
881 if (!client->skip_newline)
882 fprintf(f_out, "<%s> ", name);
883 ret = fwrite(buf, pdu->buf_len, 1, f_out);
884 fflush(f_out);
885 client->skip_newline = strchr(buf, '\n') == NULL;
886}
887
Jens Axboed09a64a2011-10-13 11:38:56 +0200888static void convert_agg(struct disk_util_agg *agg)
889{
890 int i;
891
892 for (i = 0; i < 2; i++) {
893 agg->ios[i] = le32_to_cpu(agg->ios[i]);
894 agg->merges[i] = le32_to_cpu(agg->merges[i]);
895 agg->sectors[i] = le64_to_cpu(agg->sectors[i]);
896 agg->ticks[i] = le32_to_cpu(agg->ticks[i]);
897 }
898
899 agg->io_ticks = le32_to_cpu(agg->io_ticks);
900 agg->time_in_queue = le32_to_cpu(agg->time_in_queue);
901 agg->slavecount = le32_to_cpu(agg->slavecount);
Anton Blanchard823ba542011-11-07 14:16:26 +0100902 agg->max_util.u.f = fio_uint64_to_double(__le64_to_cpu(agg->max_util.u.i));
Jens Axboed09a64a2011-10-13 11:38:56 +0200903}
904
905static void convert_dus(struct disk_util_stat *dus)
906{
907 int i;
908
909 for (i = 0; i < 2; i++) {
Jens Axboea3b4cf72014-04-11 12:17:53 -0600910 dus->s.ios[i] = le32_to_cpu(dus->s.ios[i]);
911 dus->s.merges[i] = le32_to_cpu(dus->s.merges[i]);
912 dus->s.sectors[i] = le64_to_cpu(dus->s.sectors[i]);
913 dus->s.ticks[i] = le32_to_cpu(dus->s.ticks[i]);
Jens Axboed09a64a2011-10-13 11:38:56 +0200914 }
915
Jens Axboea3b4cf72014-04-11 12:17:53 -0600916 dus->s.io_ticks = le32_to_cpu(dus->s.io_ticks);
917 dus->s.time_in_queue = le32_to_cpu(dus->s.time_in_queue);
918 dus->s.msec = le64_to_cpu(dus->s.msec);
Jens Axboed09a64a2011-10-13 11:38:56 +0200919}
920
921static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd)
922{
923 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
924
Jens Axboed09a64a2011-10-13 11:38:56 +0200925 if (!client->disk_stats_shown) {
926 client->disk_stats_shown = 1;
927 log_info("\nDisk stats (read/write):\n");
928 }
929
Castor Fu952b05e2013-10-31 11:00:34 -0600930 if (output_format == FIO_OUTPUT_JSON) {
931 struct json_object *duobj;
932 json_array_add_disk_util(&du->dus, &du->agg, du_array);
933 duobj = json_array_last_value_object(du_array);
934 json_object_add_client_info(duobj, client);
935 } else
936 print_disk_util(&du->dus, &du->agg, output_format == FIO_OUTPUT_TERSE);
Jens Axboed09a64a2011-10-13 11:38:56 +0200937}
938
Jens Axboe3bf236c2012-03-03 20:35:50 +0100939static void convert_jobs_eta(struct jobs_eta *je)
Jens Axboecf451d12011-10-03 16:48:30 +0200940{
Jens Axboecf451d12011-10-03 16:48:30 +0200941 int i;
942
943 je->nr_running = le32_to_cpu(je->nr_running);
944 je->nr_ramp = le32_to_cpu(je->nr_ramp);
945 je->nr_pending = le32_to_cpu(je->nr_pending);
Jens Axboe714e85f2013-04-15 10:21:56 +0200946 je->nr_setting_up = le32_to_cpu(je->nr_setting_up);
Jens Axboecf451d12011-10-03 16:48:30 +0200947 je->files_open = le32_to_cpu(je->files_open);
Jens Axboecf451d12011-10-03 16:48:30 +0200948
Jens Axboe298921d2012-09-24 18:47:01 +0200949 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
950 je->m_rate[i] = le32_to_cpu(je->m_rate[i]);
951 je->t_rate[i] = le32_to_cpu(je->t_rate[i]);
952 je->m_iops[i] = le32_to_cpu(je->m_iops[i]);
953 je->t_iops[i] = le32_to_cpu(je->t_iops[i]);
Jens Axboe16725bb2013-04-11 12:43:05 +0200954 je->rate[i] = le32_to_cpu(je->rate[i]);
955 je->iops[i] = le32_to_cpu(je->iops[i]);
Jens Axboecf451d12011-10-03 16:48:30 +0200956 }
957
Jens Axboeb51eedb2011-10-09 12:13:39 +0200958 je->elapsed_sec = le64_to_cpu(je->elapsed_sec);
Jens Axboecf451d12011-10-03 16:48:30 +0200959 je->eta_sec = le64_to_cpu(je->eta_sec);
Jens Axboe8c621fb2012-03-08 12:30:48 +0100960 je->nr_threads = le32_to_cpu(je->nr_threads);
Jens Axboeb7f05eb2012-05-11 20:33:02 +0200961 je->is_pow2 = le32_to_cpu(je->is_pow2);
Jens Axboeed2c0a12013-04-11 12:55:16 +0200962 je->unit_base = le32_to_cpu(je->unit_base);
Jens Axboe48fbb462011-10-09 12:19:08 +0200963}
Jens Axboecf451d12011-10-03 16:48:30 +0200964
Jens Axboe3e47bd22012-02-29 13:45:02 +0100965void fio_client_sum_jobs_eta(struct jobs_eta *dst, struct jobs_eta *je)
Jens Axboe48fbb462011-10-09 12:19:08 +0200966{
Jens Axboe48fbb462011-10-09 12:19:08 +0200967 int i;
968
969 dst->nr_running += je->nr_running;
970 dst->nr_ramp += je->nr_ramp;
971 dst->nr_pending += je->nr_pending;
Jens Axboe714e85f2013-04-15 10:21:56 +0200972 dst->nr_setting_up += je->nr_setting_up;
Jens Axboe48fbb462011-10-09 12:19:08 +0200973 dst->files_open += je->files_open;
Jens Axboe48fbb462011-10-09 12:19:08 +0200974
Jens Axboe298921d2012-09-24 18:47:01 +0200975 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
Jens Axboe3e47bd22012-02-29 13:45:02 +0100976 dst->m_rate[i] += je->m_rate[i];
977 dst->t_rate[i] += je->t_rate[i];
978 dst->m_iops[i] += je->m_iops[i];
979 dst->t_iops[i] += je->t_iops[i];
Jens Axboe16725bb2013-04-11 12:43:05 +0200980 dst->rate[i] += je->rate[i];
981 dst->iops[i] += je->iops[i];
Jens Axboe48fbb462011-10-09 12:19:08 +0200982 }
983
984 dst->elapsed_sec += je->elapsed_sec;
985
986 if (je->eta_sec > dst->eta_sec)
987 dst->eta_sec = je->eta_sec;
Jens Axboe8c621fb2012-03-08 12:30:48 +0100988
989 dst->nr_threads += je->nr_threads;
Jens Axboe27b91552014-06-27 15:30:06 -0600990
991 /*
992 * This wont be correct for multiple strings, but at least it
993 * works for the basic cases.
994 */
995 strcpy((char *) dst->run_str, (char *) je->run_str);
Jens Axboe48fbb462011-10-09 12:19:08 +0200996}
997
Jens Axboea5276612012-03-04 15:15:08 +0100998void fio_client_dec_jobs_eta(struct client_eta *eta, client_eta_op eta_fn)
Jens Axboe82c1ed32011-10-10 08:56:18 +0200999{
1000 if (!--eta->pending) {
Jens Axboea5276612012-03-04 15:15:08 +01001001 eta_fn(&eta->eta);
Jens Axboe82c1ed32011-10-10 08:56:18 +02001002 free(eta);
1003 }
1004}
1005
Jens Axboe89c17072011-10-11 10:15:51 +02001006static void remove_reply_cmd(struct fio_client *client, struct fio_net_cmd *cmd)
1007{
Jens Axboe40c60512012-03-27 16:03:04 +02001008 struct fio_net_cmd_reply *reply = NULL;
Jens Axboe89c17072011-10-11 10:15:51 +02001009 struct flist_head *entry;
1010
1011 flist_for_each(entry, &client->cmd_list) {
Jens Axboe40c60512012-03-27 16:03:04 +02001012 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
Jens Axboe89c17072011-10-11 10:15:51 +02001013
Jens Axboe40c60512012-03-27 16:03:04 +02001014 if (cmd->tag == (uintptr_t) reply)
Jens Axboe89c17072011-10-11 10:15:51 +02001015 break;
1016
Jens Axboe40c60512012-03-27 16:03:04 +02001017 reply = NULL;
Jens Axboe89c17072011-10-11 10:15:51 +02001018 }
1019
Jens Axboe40c60512012-03-27 16:03:04 +02001020 if (!reply) {
Jens Axboe4e0a8fa2013-04-15 11:40:57 +02001021 log_err("fio: client: unable to find matching tag (%llx)\n", (unsigned long long) cmd->tag);
Jens Axboe89c17072011-10-11 10:15:51 +02001022 return;
1023 }
1024
Jens Axboe40c60512012-03-27 16:03:04 +02001025 flist_del(&reply->list);
1026 cmd->tag = reply->saved_tag;
1027 free(reply);
1028}
1029
1030int fio_client_wait_for_reply(struct fio_client *client, uint64_t tag)
1031{
1032 do {
1033 struct fio_net_cmd_reply *reply = NULL;
1034 struct flist_head *entry;
1035
1036 flist_for_each(entry, &client->cmd_list) {
1037 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
1038
1039 if (tag == (uintptr_t) reply)
1040 break;
1041
1042 reply = NULL;
1043 }
1044
1045 if (!reply)
1046 break;
1047
1048 usleep(1000);
1049 } while (1);
1050
1051 return 0;
Jens Axboe89c17072011-10-11 10:15:51 +02001052}
1053
Jens Axboe82c1ed32011-10-10 08:56:18 +02001054static void handle_eta(struct fio_client *client, struct fio_net_cmd *cmd)
Jens Axboe48fbb462011-10-09 12:19:08 +02001055{
1056 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
Jens Axboedf380932011-10-11 14:25:08 +02001057 struct client_eta *eta = (struct client_eta *) (uintptr_t) cmd->tag;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001058
1059 dprint(FD_NET, "client: got eta tag %p, %d\n", eta, eta->pending);
Jens Axboe48fbb462011-10-09 12:19:08 +02001060
Jens Axboef77d2672011-10-10 14:36:07 +02001061 assert(client->eta_in_flight == eta);
1062
1063 client->eta_in_flight = NULL;
Jens Axboe82c1ed32011-10-10 08:56:18 +02001064 flist_del_init(&client->eta_list);
1065
Jens Axboe2f99deb2012-03-09 14:37:29 +01001066 if (client->ops->jobs_eta)
1067 client->ops->jobs_eta(client, je);
1068
Jens Axboe3e47bd22012-02-29 13:45:02 +01001069 fio_client_sum_jobs_eta(&eta->eta, je);
Jens Axboea5276612012-03-04 15:15:08 +01001070 fio_client_dec_jobs_eta(eta, client->ops->eta);
Jens Axboecf451d12011-10-03 16:48:30 +02001071}
1072
Jens Axboeb5296dd2011-10-07 16:35:56 +02001073static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd)
Jens Axboe2e03b4b2011-10-04 09:00:40 +02001074{
Jens Axboe3989b142013-04-12 13:13:24 +02001075 struct cmd_probe_reply_pdu *probe = (struct cmd_probe_reply_pdu *) cmd->payload;
Jens Axboed2333352011-10-11 15:07:23 +02001076 const char *os, *arch;
1077 char bit[16];
Jens Axboe2e03b4b2011-10-04 09:00:40 +02001078
Jens Axboecca84642011-10-07 12:47:57 +02001079 os = fio_get_os_string(probe->os);
1080 if (!os)
1081 os = "unknown";
1082
1083 arch = fio_get_arch_string(probe->arch);
1084 if (!arch)
1085 os = "unknown";
1086
Jens Axboed2333352011-10-11 15:07:23 +02001087 sprintf(bit, "%d-bit", probe->bpp * 8);
Jens Axboe3989b142013-04-12 13:13:24 +02001088 probe->flags = le64_to_cpu(probe->flags);
Jens Axboe38fdef22011-10-11 14:30:06 +02001089
Jens Axboe3989b142013-04-12 13:13:24 +02001090 log_info("hostname=%s, be=%u, %s, os=%s, arch=%s, fio=%s, flags=%lx\n",
Jens Axboe38fdef22011-10-11 14:30:06 +02001091 probe->hostname, probe->bigendian, bit, os, arch,
Jens Axboe3989b142013-04-12 13:13:24 +02001092 probe->fio_version, (unsigned long) probe->flags);
Jens Axboeb5296dd2011-10-07 16:35:56 +02001093
1094 if (!client->name)
1095 client->name = strdup((char *) probe->hostname);
Jens Axboe2e03b4b2011-10-04 09:00:40 +02001096}
1097
Jens Axboe11e950b2011-10-16 21:34:14 +02001098static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd)
1099{
1100 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
1101
1102 client->state = Client_started;
1103 client->jobs = le32_to_cpu(pdu->jobs);
Jens Axboe108fea72012-11-14 13:09:45 -07001104 client->nr_stat = le32_to_cpu(pdu->stat_outputs);
1105
1106 if (sum_stat_clients > 1)
1107 do_output_all_clients = 1;
1108
1109 sum_stat_clients += client->nr_stat;
Jens Axboe11e950b2011-10-16 21:34:14 +02001110}
1111
1112static void handle_stop(struct fio_client *client, struct fio_net_cmd *cmd)
1113{
Jens Axboe498c92c2011-10-17 09:14:42 +02001114 if (client->error)
1115 log_info("client <%s>: exited with error %d\n", client->hostname, client->error);
Jens Axboe11e950b2011-10-16 21:34:14 +02001116}
1117
Jens Axboe6b79c802012-03-08 10:51:36 +01001118static void convert_stop(struct fio_net_cmd *cmd)
1119{
1120 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
1121
1122 pdu->error = le32_to_cpu(pdu->error);
1123}
1124
Jens Axboe084d1c62012-03-03 20:28:07 +01001125static void convert_text(struct fio_net_cmd *cmd)
1126{
1127 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
1128
1129 pdu->level = le32_to_cpu(pdu->level);
1130 pdu->buf_len = le32_to_cpu(pdu->buf_len);
1131 pdu->log_sec = le64_to_cpu(pdu->log_sec);
1132 pdu->log_usec = le64_to_cpu(pdu->log_usec);
1133}
1134
Jens Axboe3989b142013-04-12 13:13:24 +02001135static struct cmd_iolog_pdu *convert_iolog_gz(struct fio_net_cmd *cmd,
1136 struct cmd_iolog_pdu *pdu)
Jens Axboe1b427252012-03-14 15:03:03 +01001137{
Jens Axboe3989b142013-04-12 13:13:24 +02001138#ifdef CONFIG_ZLIB
Jens Axboe1b427252012-03-14 15:03:03 +01001139 struct cmd_iolog_pdu *ret;
Jens Axboe1b427252012-03-14 15:03:03 +01001140 z_stream stream;
Jens Axboe3989b142013-04-12 13:13:24 +02001141 uint32_t nr_samples;
1142 size_t total;
Jens Axboe1b427252012-03-14 15:03:03 +01001143 void *p;
1144
1145 stream.zalloc = Z_NULL;
1146 stream.zfree = Z_NULL;
1147 stream.opaque = Z_NULL;
1148 stream.avail_in = 0;
1149 stream.next_in = Z_NULL;
1150
1151 if (inflateInit(&stream) != Z_OK)
1152 return NULL;
1153
1154 /*
Jens Axboef5ed7652012-03-14 16:28:07 +01001155 * Get header first, it's not compressed
Jens Axboe1b427252012-03-14 15:03:03 +01001156 */
1157 nr_samples = le32_to_cpu(pdu->nr_samples);
1158
Jens Axboef5ed7652012-03-14 16:28:07 +01001159 total = nr_samples * sizeof(struct io_sample);
1160 ret = malloc(total + sizeof(*pdu));
Jens Axboe1b427252012-03-14 15:03:03 +01001161 ret->nr_samples = nr_samples;
Jens Axboe3989b142013-04-12 13:13:24 +02001162
1163 memcpy(ret, pdu, sizeof(*pdu));
Jens Axboe1b427252012-03-14 15:03:03 +01001164
Jens Axboef5ed7652012-03-14 16:28:07 +01001165 p = (void *) ret + sizeof(*pdu);
1166
1167 stream.avail_in = cmd->pdu_len - sizeof(*pdu);
1168 stream.next_in = (void *) pdu + sizeof(*pdu);
Jens Axboe1b427252012-03-14 15:03:03 +01001169 while (stream.avail_in) {
1170 unsigned int this_chunk = 65536;
1171 unsigned int this_len;
1172 int err;
1173
1174 if (this_chunk > total)
1175 this_chunk = total;
1176
1177 stream.avail_out = this_chunk;
1178 stream.next_out = p;
1179 err = inflate(&stream, Z_NO_FLUSH);
Jens Axboe3c547fe2012-03-14 21:53:00 +01001180 /* may be Z_OK, or Z_STREAM_END */
1181 if (err < 0) {
Jens Axboe1b427252012-03-14 15:03:03 +01001182 log_err("fio: inflate error %d\n", err);
Jens Axboef5ed7652012-03-14 16:28:07 +01001183 free(ret);
1184 ret = NULL;
Jens Axboe3989b142013-04-12 13:13:24 +02001185 goto err;
Jens Axboe1b427252012-03-14 15:03:03 +01001186 }
1187
1188 this_len = this_chunk - stream.avail_out;
1189 p += this_len;
1190 total -= this_len;
1191 }
1192
Jens Axboe3989b142013-04-12 13:13:24 +02001193err:
1194 inflateEnd(&stream);
1195 return ret;
1196#else
1197 return NULL;
1198#endif
1199}
1200
1201/*
1202 * This has been compressed on the server side, since it can be big.
1203 * Uncompress here.
1204 */
1205static struct cmd_iolog_pdu *convert_iolog(struct fio_net_cmd *cmd)
1206{
1207 struct cmd_iolog_pdu *pdu = (struct cmd_iolog_pdu *) cmd->payload;
1208 struct cmd_iolog_pdu *ret;
1209 int i;
1210
1211 /*
1212 * Convert if compressed and we support it. If it's not
1213 * compressed, we need not do anything.
1214 */
1215 if (le32_to_cpu(pdu->compressed)) {
1216#ifndef CONFIG_ZLIB
1217 log_err("fio: server sent compressed data by mistake\n");
1218 return NULL;
1219#endif
1220 ret = convert_iolog_gz(cmd, pdu);
1221 if (!ret) {
1222 log_err("fio: failed decompressing log\n");
1223 return NULL;
1224 }
1225 } else
1226 ret = pdu;
1227
1228 ret->thread_number = le32_to_cpu(ret->thread_number);
1229 ret->nr_samples = le32_to_cpu(ret->nr_samples);
1230 ret->log_type = le32_to_cpu(ret->log_type);
1231 ret->compressed = le32_to_cpu(ret->compressed);
1232
Jens Axboef5ed7652012-03-14 16:28:07 +01001233 for (i = 0; i < ret->nr_samples; i++) {
1234 struct io_sample *s = &ret->samples[i];
1235
1236 s->time = le64_to_cpu(s->time);
1237 s->val = le64_to_cpu(s->val);
1238 s->ddir = le32_to_cpu(s->ddir);
1239 s->bs = le32_to_cpu(s->bs);
1240 }
1241
Jens Axboe1b427252012-03-14 15:03:03 +01001242 return ret;
1243}
1244
Jens Axboea5276612012-03-04 15:15:08 +01001245int fio_handle_client(struct fio_client *client)
Jens Axboe37db14f2011-09-30 17:00:42 -06001246{
Jens Axboea5276612012-03-04 15:15:08 +01001247 struct client_ops *ops = client->ops;
Jens Axboe37db14f2011-09-30 17:00:42 -06001248 struct fio_net_cmd *cmd;
1249
Jens Axboe60efd142011-10-04 13:30:11 +02001250 dprint(FD_NET, "client: handle %s\n", client->hostname);
1251
Jens Axboee951bdc2011-10-05 21:58:45 +02001252 cmd = fio_net_recv_cmd(client->fd);
1253 if (!cmd)
1254 return 0;
Jens Axboec2c94582011-10-05 20:31:30 +02001255
Jens Axboeb9d2f302012-03-08 20:36:28 +01001256 dprint(FD_NET, "client: got cmd op %s from %s (pdu=%u)\n",
1257 fio_server_op(cmd->opcode), client->hostname, cmd->pdu_len);
Jens Axboe46c48f12011-10-01 12:50:51 -06001258
Jens Axboee951bdc2011-10-05 21:58:45 +02001259 switch (cmd->opcode) {
1260 case FIO_NET_CMD_QUIT:
Jens Axboe3ec62ec2012-03-01 12:01:29 +01001261 if (ops->quit)
Jens Axboe35c0ba72012-03-14 10:56:40 +01001262 ops->quit(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +02001263 remove_client(client);
1264 free(cmd);
1265 break;
Jens Axboe084d1c62012-03-03 20:28:07 +01001266 case FIO_NET_CMD_TEXT:
1267 convert_text(cmd);
Jens Axboe35c0ba72012-03-14 10:56:40 +01001268 ops->text(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +02001269 free(cmd);
1270 break;
Jens Axboe3bf236c2012-03-03 20:35:50 +01001271 case FIO_NET_CMD_DU: {
1272 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
1273
1274 convert_dus(&du->dus);
1275 convert_agg(&du->agg);
1276
Stephen M. Camerondd366722012-02-24 08:17:30 +01001277 ops->disk_util(client, cmd);
Jens Axboed09a64a2011-10-13 11:38:56 +02001278 free(cmd);
1279 break;
Jens Axboe3bf236c2012-03-03 20:35:50 +01001280 }
1281 case FIO_NET_CMD_TS: {
1282 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
1283
1284 convert_ts(&p->ts, &p->ts);
1285 convert_gs(&p->rs, &p->rs);
1286
Jens Axboe89e5fad2012-03-05 09:21:12 +01001287 ops->thread_status(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +02001288 free(cmd);
1289 break;
Jens Axboe3bf236c2012-03-03 20:35:50 +01001290 }
1291 case FIO_NET_CMD_GS: {
1292 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
1293
1294 convert_gs(gs, gs);
1295
Jens Axboe89e5fad2012-03-05 09:21:12 +01001296 ops->group_stats(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +02001297 free(cmd);
1298 break;
Jens Axboe3bf236c2012-03-03 20:35:50 +01001299 }
1300 case FIO_NET_CMD_ETA: {
1301 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
1302
Jens Axboe89c17072011-10-11 10:15:51 +02001303 remove_reply_cmd(client, cmd);
Jens Axboe3bf236c2012-03-03 20:35:50 +01001304 convert_jobs_eta(je);
Jens Axboea5276612012-03-04 15:15:08 +01001305 handle_eta(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +02001306 free(cmd);
1307 break;
Jens Axboe3bf236c2012-03-03 20:35:50 +01001308 }
Jens Axboee951bdc2011-10-05 21:58:45 +02001309 case FIO_NET_CMD_PROBE:
Jens Axboe89c17072011-10-11 10:15:51 +02001310 remove_reply_cmd(client, cmd);
Stephen M. Camerondd366722012-02-24 08:17:30 +01001311 ops->probe(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +02001312 free(cmd);
1313 break;
Jens Axboe5d7793a2012-03-08 19:59:16 +01001314 case FIO_NET_CMD_SERVER_START:
Jens Axboe01be0382011-10-15 14:43:41 +02001315 client->state = Client_running;
Jens Axboe85dd01e2012-03-12 14:33:16 +01001316 if (ops->job_start)
1317 ops->job_start(client, cmd);
Jens Axboe01be0382011-10-15 14:43:41 +02001318 free(cmd);
1319 break;
Jens Axboe85dd01e2012-03-12 14:33:16 +01001320 case FIO_NET_CMD_START: {
1321 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
1322
1323 pdu->jobs = le32_to_cpu(pdu->jobs);
1324 ops->start(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +02001325 free(cmd);
1326 break;
Jens Axboe85dd01e2012-03-12 14:33:16 +01001327 }
Jens Axboe6b79c802012-03-08 10:51:36 +01001328 case FIO_NET_CMD_STOP: {
1329 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
1330
1331 convert_stop(cmd);
1332 client->state = Client_stopped;
Jens Axboe122c7722012-03-19 09:13:15 +01001333 client->error = le32_to_cpu(pdu->error);
1334 client->signal = le32_to_cpu(pdu->signal);
Jens Axboe6b79c802012-03-08 10:51:36 +01001335 ops->stop(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +02001336 free(cmd);
1337 break;
Jens Axboe6b79c802012-03-08 10:51:36 +01001338 }
Jens Axboe40c60512012-03-27 16:03:04 +02001339 case FIO_NET_CMD_ADD_JOB: {
1340 struct cmd_add_job_pdu *pdu = (struct cmd_add_job_pdu *) cmd->payload;
1341
1342 client->thread_number = le32_to_cpu(pdu->thread_number);
1343 client->groupid = le32_to_cpu(pdu->groupid);
1344
Jens Axboe807f9972012-03-02 10:25:24 +01001345 if (ops->add_job)
1346 ops->add_job(client, cmd);
1347 free(cmd);
1348 break;
Jens Axboe40c60512012-03-27 16:03:04 +02001349 }
Jens Axboe1b427252012-03-14 15:03:03 +01001350 case FIO_NET_CMD_IOLOG:
1351 if (ops->iolog) {
1352 struct cmd_iolog_pdu *pdu;
1353
1354 pdu = convert_iolog(cmd);
1355 ops->iolog(client, pdu);
1356 }
1357 free(cmd);
1358 break;
Jens Axboe40c60512012-03-27 16:03:04 +02001359 case FIO_NET_CMD_UPDATE_JOB:
Jens Axboe40c60512012-03-27 16:03:04 +02001360 ops->update_job(client, cmd);
Jens Axboe649cee92012-03-28 09:15:32 +02001361 remove_reply_cmd(client, cmd);
Jens Axboe40c60512012-03-27 16:03:04 +02001362 free(cmd);
1363 break;
Jens Axboee951bdc2011-10-05 21:58:45 +02001364 default:
Jens Axboe89c17072011-10-11 10:15:51 +02001365 log_err("fio: unknown client op: %s\n", fio_server_op(cmd->opcode));
Jens Axboee951bdc2011-10-05 21:58:45 +02001366 free(cmd);
1367 break;
Jens Axboe37db14f2011-09-30 17:00:42 -06001368 }
1369
Jens Axboee951bdc2011-10-05 21:58:45 +02001370 return 1;
Jens Axboe37db14f2011-09-30 17:00:42 -06001371}
Jens Axboeb66570d2011-10-01 11:11:35 -06001372
Jens Axboea5276612012-03-04 15:15:08 +01001373static void request_client_etas(struct client_ops *ops)
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001374{
1375 struct fio_client *client;
1376 struct flist_head *entry;
1377 struct client_eta *eta;
Jens Axboe82c1ed32011-10-10 08:56:18 +02001378 int skipped = 0;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001379
1380 dprint(FD_NET, "client: request eta (%d)\n", nr_clients);
1381
Jens Axboe27b91552014-06-27 15:30:06 -06001382 eta = calloc(1, sizeof(*eta) + __THREAD_RUNSTR_SZ(REAL_MAX_JOBS));
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001383 eta->pending = nr_clients;
1384
1385 flist_for_each(entry, &client_list) {
1386 client = flist_entry(entry, struct fio_client, list);
1387
Jens Axboe82c1ed32011-10-10 08:56:18 +02001388 if (!flist_empty(&client->eta_list)) {
1389 skipped++;
1390 continue;
1391 }
Jens Axboe01be0382011-10-15 14:43:41 +02001392 if (client->state != Client_running)
1393 continue;
Jens Axboe82c1ed32011-10-10 08:56:18 +02001394
Jens Axboef77d2672011-10-10 14:36:07 +02001395 assert(!client->eta_in_flight);
Jens Axboe82c1ed32011-10-10 08:56:18 +02001396 flist_add_tail(&client->eta_list, &eta_list);
Jens Axboef77d2672011-10-10 14:36:07 +02001397 client->eta_in_flight = eta;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001398 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_SEND_ETA,
Jens Axboedf380932011-10-11 14:25:08 +02001399 (uintptr_t) eta, &client->cmd_list);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001400 }
1401
Jens Axboe82c1ed32011-10-10 08:56:18 +02001402 while (skipped--)
Jens Axboea5276612012-03-04 15:15:08 +01001403 fio_client_dec_jobs_eta(eta, ops->eta);
Jens Axboe82c1ed32011-10-10 08:56:18 +02001404
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001405 dprint(FD_NET, "client: requested eta tag %p\n", eta);
1406}
1407
Jens Axboe89c17072011-10-11 10:15:51 +02001408static int client_check_cmd_timeout(struct fio_client *client,
1409 struct timeval *now)
1410{
Jens Axboe40c60512012-03-27 16:03:04 +02001411 struct fio_net_cmd_reply *reply;
Jens Axboe89c17072011-10-11 10:15:51 +02001412 struct flist_head *entry, *tmp;
1413 int ret = 0;
1414
1415 flist_for_each_safe(entry, tmp, &client->cmd_list) {
Jens Axboe40c60512012-03-27 16:03:04 +02001416 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
Jens Axboe89c17072011-10-11 10:15:51 +02001417
Jens Axboe40c60512012-03-27 16:03:04 +02001418 if (mtime_since(&reply->tv, now) < FIO_NET_CLIENT_TIMEOUT)
Jens Axboe89c17072011-10-11 10:15:51 +02001419 continue;
1420
1421 log_err("fio: client %s, timeout on cmd %s\n", client->hostname,
Jens Axboe40c60512012-03-27 16:03:04 +02001422 fio_server_op(reply->opcode));
1423 flist_del(&reply->list);
1424 free(reply);
Jens Axboe89c17072011-10-11 10:15:51 +02001425 ret = 1;
1426 }
1427
1428 return flist_empty(&client->cmd_list) && ret;
1429}
1430
Jens Axboea5276612012-03-04 15:15:08 +01001431static int fio_check_clients_timed_out(void)
Jens Axboe89c17072011-10-11 10:15:51 +02001432{
1433 struct fio_client *client;
1434 struct flist_head *entry, *tmp;
1435 struct timeval tv;
1436 int ret = 0;
1437
Jens Axboe67bf9822013-01-10 11:23:19 +01001438 fio_gettime(&tv, NULL);
Jens Axboe89c17072011-10-11 10:15:51 +02001439
1440 flist_for_each_safe(entry, tmp, &client_list) {
1441 client = flist_entry(entry, struct fio_client, list);
1442
1443 if (flist_empty(&client->cmd_list))
1444 continue;
1445
1446 if (!client_check_cmd_timeout(client, &tv))
1447 continue;
1448
Jens Axboea5276612012-03-04 15:15:08 +01001449 if (client->ops->timed_out)
1450 client->ops->timed_out(client);
Jens Axboeed727a42012-03-02 12:14:40 +01001451 else
1452 log_err("fio: client %s timed out\n", client->hostname);
1453
Jens Axboe89c17072011-10-11 10:15:51 +02001454 remove_client(client);
1455 ret = 1;
1456 }
1457
1458 return ret;
1459}
1460
Stephen M. Camerondd366722012-02-24 08:17:30 +01001461int fio_handle_clients(struct client_ops *ops)
Jens Axboeb66570d2011-10-01 11:11:35 -06001462{
Jens Axboeb66570d2011-10-01 11:11:35 -06001463 struct pollfd *pfds;
Jens Axboe498c92c2011-10-17 09:14:42 +02001464 int i, ret = 0, retval = 0;
Jens Axboeb66570d2011-10-01 11:11:35 -06001465
Jens Axboe67bf9822013-01-10 11:23:19 +01001466 fio_gettime(&eta_tv, NULL);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001467
Jens Axboeb66570d2011-10-01 11:11:35 -06001468 pfds = malloc(nr_clients * sizeof(struct pollfd));
1469
Jens Axboe37f0c1a2011-10-11 14:08:33 +02001470 init_thread_stat(&client_ts);
1471 init_group_run_stat(&client_gs);
1472
Jens Axboeb66570d2011-10-01 11:11:35 -06001473 while (!exit_backend && nr_clients) {
Jens Axboec2cb6862012-02-23 20:56:12 +01001474 struct flist_head *entry, *tmp;
1475 struct fio_client *client;
1476
Jens Axboe82a4be12011-10-01 12:40:32 -06001477 i = 0;
Jens Axboec2cb6862012-02-23 20:56:12 +01001478 flist_for_each_safe(entry, tmp, &client_list) {
Jens Axboe82a4be12011-10-01 12:40:32 -06001479 client = flist_entry(entry, struct fio_client, list);
1480
Jens Axboea5276612012-03-04 15:15:08 +01001481 if (!client->sent_job && !client->ops->stay_connected &&
Jens Axboec2cb6862012-02-23 20:56:12 +01001482 flist_empty(&client->cmd_list)) {
1483 remove_client(client);
1484 continue;
1485 }
1486
Jens Axboe82a4be12011-10-01 12:40:32 -06001487 pfds[i].fd = client->fd;
1488 pfds[i].events = POLLIN;
1489 i++;
1490 }
1491
Jens Axboec2cb6862012-02-23 20:56:12 +01001492 if (!nr_clients)
1493 break;
1494
Jens Axboe82a4be12011-10-01 12:40:32 -06001495 assert(i == nr_clients);
1496
Jens Axboe5c2857f2011-10-04 13:14:32 +02001497 do {
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001498 struct timeval tv;
1499
Jens Axboe67bf9822013-01-10 11:23:19 +01001500 fio_gettime(&tv, NULL);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001501 if (mtime_since(&eta_tv, &tv) >= 900) {
Jens Axboea5276612012-03-04 15:15:08 +01001502 request_client_etas(ops);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001503 memcpy(&eta_tv, &tv, sizeof(tv));
Jens Axboe89c17072011-10-11 10:15:51 +02001504
Jens Axboea5276612012-03-04 15:15:08 +01001505 if (fio_check_clients_timed_out())
Jens Axboe89c17072011-10-11 10:15:51 +02001506 break;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001507 }
1508
Jens Axboe80102c82012-03-23 13:19:31 +01001509 ret = poll(pfds, nr_clients, ops->eta_msec);
Jens Axboe5c2857f2011-10-04 13:14:32 +02001510 if (ret < 0) {
1511 if (errno == EINTR)
1512 continue;
1513 log_err("fio: poll clients: %s\n", strerror(errno));
1514 break;
1515 } else if (!ret)
Jens Axboeb66570d2011-10-01 11:11:35 -06001516 continue;
Jens Axboe5c2857f2011-10-04 13:14:32 +02001517 } while (ret <= 0);
Jens Axboeb66570d2011-10-01 11:11:35 -06001518
1519 for (i = 0; i < nr_clients; i++) {
1520 if (!(pfds[i].revents & POLLIN))
1521 continue;
1522
1523 client = find_client_by_fd(pfds[i].fd);
1524 if (!client) {
Jens Axboe65e1a122013-08-30 10:13:36 -06001525 log_err("fio: unknown client fd %ld\n", (long) pfds[i].fd);
Jens Axboeb66570d2011-10-01 11:11:35 -06001526 continue;
1527 }
Jens Axboea5276612012-03-04 15:15:08 +01001528 if (!fio_handle_client(client)) {
Jens Axboe28d3ab02011-10-05 20:45:37 +02001529 log_info("client: host=%s disconnected\n",
1530 client->hostname);
1531 remove_client(client);
Jens Axboe498c92c2011-10-17 09:14:42 +02001532 retval = 1;
Jens Axboe38990762011-10-17 13:31:33 +02001533 } else if (client->error)
Jens Axboe498c92c2011-10-17 09:14:42 +02001534 retval = 1;
Jens Axboe343cb4a2012-03-09 17:16:51 +01001535 fio_put_client(client);
Jens Axboeb66570d2011-10-01 11:11:35 -06001536 }
1537 }
1538
Castor Fu952b05e2013-10-31 11:00:34 -06001539 fio_client_json_fini();
1540
Jens Axboeb66570d2011-10-01 11:11:35 -06001541 free(pfds);
Jens Axboe498c92c2011-10-17 09:14:42 +02001542 return retval;
Jens Axboeb66570d2011-10-01 11:11:35 -06001543}