blob: 80096bf8a81a7bc652fab31fc146dca24718d6e7 [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 Axboede54cfd2014-11-10 20:34:00 -070026#include "verify.h"
Jens Axboe132159a2011-09-30 15:01:32 -060027
Stephen M. Camerondd366722012-02-24 08:17:30 +010028static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd);
Jens Axboe89e5fad2012-03-05 09:21:12 +010029static void handle_ts(struct fio_client *client, struct fio_net_cmd *cmd);
30static void handle_gs(struct fio_client *client, struct fio_net_cmd *cmd);
Stephen M. Camerondd366722012-02-24 08:17:30 +010031static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd);
Jens Axboe084d1c62012-03-03 20:28:07 +010032static void handle_text(struct fio_client *client, struct fio_net_cmd *cmd);
Jens Axboe6b79c802012-03-08 10:51:36 +010033static void handle_stop(struct fio_client *client, struct fio_net_cmd *cmd);
Jens Axboe85dd01e2012-03-12 14:33:16 +010034static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd);
Stephen M. Camerondd366722012-02-24 08:17:30 +010035
Elliott Hugheseda3a602017-05-19 18:53:02 -070036static void convert_text(struct fio_net_cmd *cmd);
37
Stephen M. Camerondd366722012-02-24 08:17:30 +010038struct client_ops fio_client_ops = {
Jens Axboe35c0ba72012-03-14 10:56:40 +010039 .text = handle_text,
Jens Axboe0420ba62012-02-29 11:16:52 +010040 .disk_util = handle_du,
41 .thread_status = handle_ts,
42 .group_stats = handle_gs,
Jens Axboe6b79c802012-03-08 10:51:36 +010043 .stop = handle_stop,
Jens Axboe85dd01e2012-03-12 14:33:16 +010044 .start = handle_start,
Jens Axboea5276612012-03-04 15:15:08 +010045 .eta = display_thread_status,
Jens Axboe0420ba62012-02-29 11:16:52 +010046 .probe = handle_probe,
Jens Axboe6433ee02012-03-09 20:10:51 +010047 .eta_msec = FIO_CLIENT_DEF_ETA_MSEC,
Jens Axboe46bcd492012-03-14 11:31:21 +010048 .client_type = FIO_CLIENT_TYPE_CLI,
Stephen M. Camerondd366722012-02-24 08:17:30 +010049};
50
Jens Axboeaf9c9fb2011-10-09 21:54:10 +020051static struct timeval eta_tv;
Jens Axboe48fbb462011-10-09 12:19:08 +020052
Jens Axboeb66570d2011-10-01 11:11:35 -060053static FLIST_HEAD(client_list);
Jens Axboe82c1ed32011-10-10 08:56:18 +020054static FLIST_HEAD(eta_list);
Jens Axboeb66570d2011-10-01 11:11:35 -060055
Jens Axboe3f3a4542011-10-10 21:11:09 +020056static FLIST_HEAD(arg_list);
57
Jens Axboe3650a3c2012-03-05 14:09:03 +010058struct thread_stat client_ts;
59struct group_run_stats client_gs;
60int sum_stat_clients;
61
Jens Axboe37f0c1a2011-10-11 14:08:33 +020062static int sum_stat_nr;
Castor Fu952b05e2013-10-31 11:00:34 -060063static struct json_object *root = NULL;
Elliott Hugheseda3a602017-05-19 18:53:02 -070064static struct json_object *job_opt_object = NULL;
Castor Fu952b05e2013-10-31 11:00:34 -060065static struct json_array *clients_array = NULL;
66static struct json_array *du_array = NULL;
Jens Axboe37f0c1a2011-10-11 14:08:33 +020067
Jens Axboe4811fff2015-01-13 21:47:43 -070068static int error_clients;
69
Jens Axboe3c5f57e2011-10-06 12:37:50 +020070#define FIO_CLIENT_HASH_BITS 7
71#define FIO_CLIENT_HASH_SZ (1 << FIO_CLIENT_HASH_BITS)
72#define FIO_CLIENT_HASH_MASK (FIO_CLIENT_HASH_SZ - 1)
Jens Axboebebe6392011-10-07 10:00:51 +020073static struct flist_head client_hash[FIO_CLIENT_HASH_SZ];
Jens Axboe3c5f57e2011-10-06 12:37:50 +020074
Elliott Hugheseda3a602017-05-19 18:53:02 -070075static struct cmd_iolog_pdu *convert_iolog(struct fio_net_cmd *, bool *);
76
Jens Axboebebe6392011-10-07 10:00:51 +020077static void fio_client_add_hash(struct fio_client *client)
Jens Axboe3c5f57e2011-10-06 12:37:50 +020078{
79 int bucket = hash_long(client->fd, FIO_CLIENT_HASH_BITS);
80
81 bucket &= FIO_CLIENT_HASH_MASK;
Jens Axboebebe6392011-10-07 10:00:51 +020082 flist_add(&client->hash_list, &client_hash[bucket]);
Jens Axboe3c5f57e2011-10-06 12:37:50 +020083}
84
Jens Axboebebe6392011-10-07 10:00:51 +020085static void fio_client_remove_hash(struct fio_client *client)
Jens Axboe3c5f57e2011-10-06 12:37:50 +020086{
Jens Axboebebe6392011-10-07 10:00:51 +020087 if (!flist_empty(&client->hash_list))
88 flist_del_init(&client->hash_list);
Jens Axboe3c5f57e2011-10-06 12:37:50 +020089}
90
91static void fio_init fio_client_hash_init(void)
92{
93 int i;
94
Jens Axboebebe6392011-10-07 10:00:51 +020095 for (i = 0; i < FIO_CLIENT_HASH_SZ; i++)
96 INIT_FLIST_HEAD(&client_hash[i]);
Jens Axboe3c5f57e2011-10-06 12:37:50 +020097}
98
Jens Axboe3d0be7c2014-11-11 10:24:00 -070099static int read_data(int fd, void *data, size_t size)
100{
101 ssize_t ret;
102
103 while (size) {
104 ret = read(fd, data, size);
105 if (ret < 0) {
106 if (errno == EAGAIN || errno == EINTR)
107 continue;
108 break;
109 } else if (!ret)
110 break;
111 else {
112 data += ret;
113 size -= ret;
114 }
115 }
116
117 if (size)
118 return EAGAIN;
119
120 return 0;
121}
122
Castor Fu952b05e2013-10-31 11:00:34 -0600123static void fio_client_json_init(void)
124{
Elliott Hugheseda3a602017-05-19 18:53:02 -0700125 char time_buf[32];
126 time_t time_p;
127
128 if (!(output_format & FIO_OUTPUT_JSON))
Castor Fu952b05e2013-10-31 11:00:34 -0600129 return;
Elliott Hugheseda3a602017-05-19 18:53:02 -0700130
131 time(&time_p);
132 os_ctime_r((const time_t *) &time_p, time_buf, sizeof(time_buf));
133 time_buf[strlen(time_buf) - 1] = '\0';
134
Castor Fu952b05e2013-10-31 11:00:34 -0600135 root = json_create_object();
136 json_object_add_value_string(root, "fio version", fio_version_string);
Elliott Hugheseda3a602017-05-19 18:53:02 -0700137 json_object_add_value_int(root, "timestamp", time_p);
138 json_object_add_value_string(root, "time", time_buf);
139
140 job_opt_object = json_create_object();
141 json_object_add_value_object(root, "global options", job_opt_object);
Castor Fu952b05e2013-10-31 11:00:34 -0600142 clients_array = json_create_array();
143 json_object_add_value_array(root, "client_stats", clients_array);
144 du_array = json_create_array();
145 json_object_add_value_array(root, "disk_util", du_array);
146}
147
148static void fio_client_json_fini(void)
149{
Elliott Hugheseda3a602017-05-19 18:53:02 -0700150 if (!(output_format & FIO_OUTPUT_JSON))
Castor Fu952b05e2013-10-31 11:00:34 -0600151 return;
Elliott Hugheseda3a602017-05-19 18:53:02 -0700152
153 log_info("\n");
154 json_print_object(root, NULL);
Castor Fu952b05e2013-10-31 11:00:34 -0600155 log_info("\n");
156 json_free_object(root);
157 root = NULL;
158 clients_array = NULL;
159 du_array = NULL;
160}
161
Jens Axboeb66570d2011-10-01 11:11:35 -0600162static struct fio_client *find_client_by_fd(int fd)
163{
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200164 int bucket = hash_long(fd, FIO_CLIENT_HASH_BITS) & FIO_CLIENT_HASH_MASK;
Jens Axboeb66570d2011-10-01 11:11:35 -0600165 struct fio_client *client;
166 struct flist_head *entry;
167
Jens Axboebebe6392011-10-07 10:00:51 +0200168 flist_for_each(entry, &client_hash[bucket]) {
169 client = flist_entry(entry, struct fio_client, hash_list);
Jens Axboeb66570d2011-10-01 11:11:35 -0600170
Jens Axboee55f8f32012-03-06 15:42:31 +0100171 if (client->fd == fd) {
172 client->refs++;
Jens Axboeb66570d2011-10-01 11:11:35 -0600173 return client;
Jens Axboee55f8f32012-03-06 15:42:31 +0100174 }
Jens Axboeb66570d2011-10-01 11:11:35 -0600175 }
176
177 return NULL;
178}
179
Jens Axboe3af45202012-03-13 09:59:53 +0100180void fio_put_client(struct fio_client *client)
Jens Axboeb66570d2011-10-01 11:11:35 -0600181{
Jens Axboe5121a9a2012-03-05 13:32:47 +0100182 if (--client->refs)
183 return;
184
Jens Axboeb66570d2011-10-01 11:11:35 -0600185 free(client->hostname);
Jens Axboe81179ee2011-10-04 12:42:06 +0200186 if (client->argv)
187 free(client->argv);
Jens Axboeb5296dd2011-10-07 16:35:56 +0200188 if (client->name)
189 free(client->name);
Jens Axboe35899182014-10-07 20:56:28 -0600190 while (client->nr_files) {
191 struct client_file *cf = &client->files[--client->nr_files];
192
193 free(cf->file);
194 }
195 if (client->files)
196 free(client->files);
Elliott Hugheseda3a602017-05-19 18:53:02 -0700197 if (client->opt_lists)
198 free(client->opt_lists);
Jens Axboe81179ee2011-10-04 12:42:06 +0200199
Jens Axboe108fea72012-11-14 13:09:45 -0700200 if (!client->did_stat)
Jens Axboebfa8f5d2014-10-13 10:09:28 -0600201 sum_stat_clients--;
Jens Axboe108fea72012-11-14 13:09:45 -0700202
Jens Axboe4811fff2015-01-13 21:47:43 -0700203 if (client->error)
204 error_clients++;
205
Jens Axboeb66570d2011-10-01 11:11:35 -0600206 free(client);
207}
Jens Axboe132159a2011-09-30 15:01:32 -0600208
Elliott Hugheseda3a602017-05-19 18:53:02 -0700209static int fio_client_dec_jobs_eta(struct client_eta *eta, client_eta_op eta_fn)
210{
211 if (!--eta->pending) {
212 eta_fn(&eta->eta);
213 free(eta);
214 return 0;
215 }
216
217 return 1;
218}
219
220static void fio_drain_client_text(struct fio_client *client)
221{
222 do {
223 struct fio_net_cmd *cmd;
224
225 cmd = fio_net_recv_cmd(client->fd, false);
226 if (!cmd)
227 break;
228
229 if (cmd->opcode == FIO_NET_CMD_TEXT) {
230 convert_text(cmd);
231 client->ops->text(client, cmd);
232 }
233
234 free(cmd);
235 } while (1);
236}
237
Jens Axboe3af45202012-03-13 09:59:53 +0100238static void remove_client(struct fio_client *client)
Jens Axboe5121a9a2012-03-05 13:32:47 +0100239{
Jens Axboe3af45202012-03-13 09:59:53 +0100240 assert(client->refs);
241
242 dprint(FD_NET, "client: removed <%s>\n", client->hostname);
243
Elliott Hugheseda3a602017-05-19 18:53:02 -0700244 fio_drain_client_text(client);
245
Jens Axboe3af45202012-03-13 09:59:53 +0100246 if (!flist_empty(&client->list))
247 flist_del_init(&client->list);
248
249 fio_client_remove_hash(client);
250
251 if (!flist_empty(&client->eta_list)) {
252 flist_del_init(&client->eta_list);
253 fio_client_dec_jobs_eta(client->eta_in_flight, client->ops->eta);
254 }
255
Jens Axboe1e0c1842012-03-20 15:15:00 +0100256 close(client->fd);
257 client->fd = -1;
258
Jens Axboe0cf3ece2012-03-21 10:15:20 +0100259 if (client->ops->removed)
260 client->ops->removed(client);
261
Jens Axboe3af45202012-03-13 09:59:53 +0100262 nr_clients--;
Jens Axboe3af45202012-03-13 09:59:53 +0100263 fio_put_client(client);
Jens Axboe5121a9a2012-03-05 13:32:47 +0100264}
265
Jens Axboe343cb4a2012-03-09 17:16:51 +0100266struct fio_client *fio_get_client(struct fio_client *client)
267{
268 client->refs++;
269 return client;
270}
271
Jens Axboefa2ea802011-10-08 21:07:29 +0200272static void __fio_client_add_cmd_option(struct fio_client *client,
273 const char *opt)
Jens Axboe81179ee2011-10-04 12:42:06 +0200274{
Jens Axboe39e8e012011-10-04 13:46:08 +0200275 int index;
276
277 index = client->argc++;
Jens Axboe81179ee2011-10-04 12:42:06 +0200278 client->argv = realloc(client->argv, sizeof(char *) * client->argc);
Jens Axboe39e8e012011-10-04 13:46:08 +0200279 client->argv[index] = strdup(opt);
280 dprint(FD_NET, "client: add cmd %d: %s\n", index, opt);
Jens Axboe81179ee2011-10-04 12:42:06 +0200281}
282
Jens Axboefa2ea802011-10-08 21:07:29 +0200283void fio_client_add_cmd_option(void *cookie, const char *opt)
Jens Axboe81179ee2011-10-04 12:42:06 +0200284{
Jens Axboebebe6392011-10-07 10:00:51 +0200285 struct fio_client *client = cookie;
Jens Axboe3f3a4542011-10-10 21:11:09 +0200286 struct flist_head *entry;
Jens Axboe81179ee2011-10-04 12:42:06 +0200287
Jens Axboebebe6392011-10-07 10:00:51 +0200288 if (!client || !opt)
Jens Axboefa2ea802011-10-08 21:07:29 +0200289 return;
Jens Axboe81179ee2011-10-04 12:42:06 +0200290
Jens Axboefa2ea802011-10-08 21:07:29 +0200291 __fio_client_add_cmd_option(client, opt);
Jens Axboe3f3a4542011-10-10 21:11:09 +0200292
293 /*
294 * Duplicate arguments to shared client group
295 */
296 flist_for_each(entry, &arg_list) {
297 client = flist_entry(entry, struct fio_client, arg_list);
298
299 __fio_client_add_cmd_option(client, opt);
300 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200301}
302
Jens Axboea5276612012-03-04 15:15:08 +0100303struct fio_client *fio_client_add_explicit(struct client_ops *ops,
304 const char *hostname, int type,
Jens Axboe3ec62ec2012-03-01 12:01:29 +0100305 int port)
306{
307 struct fio_client *client;
308
309 client = malloc(sizeof(*client));
310 memset(client, 0, sizeof(*client));
311
312 INIT_FLIST_HEAD(&client->list);
313 INIT_FLIST_HEAD(&client->hash_list);
314 INIT_FLIST_HEAD(&client->arg_list);
315 INIT_FLIST_HEAD(&client->eta_list);
316 INIT_FLIST_HEAD(&client->cmd_list);
317
318 client->hostname = strdup(hostname);
319
320 if (type == Fio_client_socket)
321 client->is_sock = 1;
322 else {
323 int ipv6;
324
325 ipv6 = type == Fio_client_ipv6;
Jens Axboe3aa3cee2014-01-24 18:56:56 -0800326 if (fio_server_parse_host(hostname, ipv6,
Jens Axboe3ec62ec2012-03-01 12:01:29 +0100327 &client->addr.sin_addr,
328 &client->addr6.sin6_addr))
329 goto err;
330
331 client->port = port;
332 }
333
334 client->fd = -1;
Jens Axboea5276612012-03-04 15:15:08 +0100335 client->ops = ops;
Jens Axboe5121a9a2012-03-05 13:32:47 +0100336 client->refs = 1;
Jens Axboe46bcd492012-03-14 11:31:21 +0100337 client->type = ops->client_type;
Jens Axboe3ec62ec2012-03-01 12:01:29 +0100338
339 __fio_client_add_cmd_option(client, "fio");
340
341 flist_add(&client->list, &client_list);
342 nr_clients++;
343 dprint(FD_NET, "client: added <%s>\n", client->hostname);
344 return client;
345err:
346 free(client);
347 return NULL;
348}
349
Elliott Hugheseda3a602017-05-19 18:53:02 -0700350int fio_client_add_ini_file(void *cookie, const char *ini_file, bool remote)
Jens Axboe14ea90e2012-08-26 17:33:34 +0200351{
352 struct fio_client *client = cookie;
Jens Axboe35899182014-10-07 20:56:28 -0600353 struct client_file *cf;
Jens Axboe14ea90e2012-08-26 17:33:34 +0200354 size_t new_size;
Jens Axboe35899182014-10-07 20:56:28 -0600355 void *new_files;
Jens Axboe14ea90e2012-08-26 17:33:34 +0200356
Jens Axboe722b5cd2014-10-14 19:56:25 -0600357 if (!client)
358 return 1;
359
Jens Axboe14ea90e2012-08-26 17:33:34 +0200360 dprint(FD_NET, "client <%s>: add ini %s\n", client->hostname, ini_file);
361
Jens Axboe35899182014-10-07 20:56:28 -0600362 new_size = (client->nr_files + 1) * sizeof(struct client_file);
363 new_files = realloc(client->files, new_size);
364 if (!new_files)
365 return 1;
366
367 client->files = new_files;
368 cf = &client->files[client->nr_files];
369 cf->file = strdup(ini_file);
370 cf->remote = remote;
371 client->nr_files++;
372 return 0;
Jens Axboe14ea90e2012-08-26 17:33:34 +0200373}
374
Jens Axboea5276612012-03-04 15:15:08 +0100375int fio_client_add(struct client_ops *ops, const char *hostname, void **cookie)
Jens Axboe132159a2011-09-30 15:01:32 -0600376{
Jens Axboe3f3a4542011-10-10 21:11:09 +0200377 struct fio_client *existing = *cookie;
Jens Axboeb66570d2011-10-01 11:11:35 -0600378 struct fio_client *client;
Jens Axboe132159a2011-09-30 15:01:32 -0600379
Jens Axboe3f3a4542011-10-10 21:11:09 +0200380 if (existing) {
381 /*
382 * We always add our "exec" name as the option, hence 1
383 * means empty.
384 */
385 if (existing->argc == 1)
386 flist_add_tail(&existing->arg_list, &arg_list);
387 else {
388 while (!flist_empty(&arg_list))
389 flist_del_init(arg_list.next);
390 }
391 }
392
Jens Axboeb66570d2011-10-01 11:11:35 -0600393 client = malloc(sizeof(*client));
Jens Axboea37f69b2011-10-01 12:24:21 -0600394 memset(client, 0, sizeof(*client));
Jens Axboe81179ee2011-10-04 12:42:06 +0200395
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200396 INIT_FLIST_HEAD(&client->list);
Jens Axboebebe6392011-10-07 10:00:51 +0200397 INIT_FLIST_HEAD(&client->hash_list);
Jens Axboe3f3a4542011-10-10 21:11:09 +0200398 INIT_FLIST_HEAD(&client->arg_list);
Jens Axboe82c1ed32011-10-10 08:56:18 +0200399 INIT_FLIST_HEAD(&client->eta_list);
Jens Axboe89c17072011-10-11 10:15:51 +0200400 INIT_FLIST_HEAD(&client->cmd_list);
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200401
Jens Axboebebe6392011-10-07 10:00:51 +0200402 if (fio_server_parse_string(hostname, &client->hostname,
403 &client->is_sock, &client->port,
Jens Axboe811826b2011-10-24 09:11:50 +0200404 &client->addr.sin_addr,
405 &client->addr6.sin6_addr,
406 &client->ipv6))
Jens Axboebebe6392011-10-07 10:00:51 +0200407 return -1;
408
Jens Axboea37f69b2011-10-01 12:24:21 -0600409 client->fd = -1;
Jens Axboea5276612012-03-04 15:15:08 +0100410 client->ops = ops;
Jens Axboe5121a9a2012-03-05 13:32:47 +0100411 client->refs = 1;
Jens Axboe46bcd492012-03-14 11:31:21 +0100412 client->type = ops->client_type;
Jens Axboe81179ee2011-10-04 12:42:06 +0200413
414 __fio_client_add_cmd_option(client, "fio");
415
Jens Axboea37f69b2011-10-01 12:24:21 -0600416 flist_add(&client->list, &client_list);
417 nr_clients++;
Jens Axboebebe6392011-10-07 10:00:51 +0200418 dprint(FD_NET, "client: added <%s>\n", client->hostname);
419 *cookie = client;
420 return 0;
Jens Axboea37f69b2011-10-01 12:24:21 -0600421}
422
Jens Axboede54cfd2014-11-10 20:34:00 -0700423static const char *server_name(struct fio_client *client, char *buf,
424 size_t bufsize)
425{
426 const char *from;
427
428 if (client->ipv6)
429 from = inet_ntop(AF_INET6, (struct sockaddr *) &client->addr6.sin6_addr, buf, bufsize);
430 else if (client->is_sock)
431 from = "sock";
432 else
433 from = inet_ntop(AF_INET, (struct sockaddr *) &client->addr.sin_addr, buf, bufsize);
434
435 return from;
436}
437
Jens Axboed824c3b2012-03-09 17:45:43 +0100438static void probe_client(struct fio_client *client)
439{
Jens Axboe3989b142013-04-12 13:13:24 +0200440 struct cmd_client_probe_pdu pdu;
Jens Axboe75289ee2015-01-05 08:47:01 -0700441 const char *sname;
Jens Axboe3989b142013-04-12 13:13:24 +0200442 uint64_t tag;
Jens Axboede54cfd2014-11-10 20:34:00 -0700443 char buf[64];
Jens Axboe3989b142013-04-12 13:13:24 +0200444
Jens Axboed824c3b2012-03-09 17:45:43 +0100445 dprint(FD_NET, "client: send probe\n");
446
Jens Axboe3989b142013-04-12 13:13:24 +0200447#ifdef CONFIG_ZLIB
448 pdu.flags = __le64_to_cpu(FIO_PROBE_FLAG_ZLIB);
449#else
450 pdu.flags = 0;
451#endif
452
Jens Axboe75289ee2015-01-05 08:47:01 -0700453 sname = server_name(client, buf, sizeof(buf));
454 memset(pdu.server, 0, sizeof(pdu.server));
455 strncpy((char *) pdu.server, sname, sizeof(pdu.server) - 1);
Jens Axboede54cfd2014-11-10 20:34:00 -0700456
Jens Axboe3989b142013-04-12 13:13:24 +0200457 fio_net_send_cmd(client->fd, FIO_NET_CMD_PROBE, &pdu, sizeof(pdu), &tag, &client->cmd_list);
Jens Axboed824c3b2012-03-09 17:45:43 +0100458}
459
Jens Axboe87aa8f12011-10-06 21:24:13 +0200460static int fio_client_connect_ip(struct fio_client *client)
Jens Axboea37f69b2011-10-01 12:24:21 -0600461{
Jens Axboe811826b2011-10-24 09:11:50 +0200462 struct sockaddr *addr;
Jens Axboe67bf9822013-01-10 11:23:19 +0100463 socklen_t socklen;
Jens Axboe811826b2011-10-24 09:11:50 +0200464 int fd, domain;
Jens Axboe132159a2011-09-30 15:01:32 -0600465
Jens Axboe811826b2011-10-24 09:11:50 +0200466 if (client->ipv6) {
467 client->addr6.sin6_family = AF_INET6;
468 client->addr6.sin6_port = htons(client->port);
469 domain = AF_INET6;
470 addr = (struct sockaddr *) &client->addr6;
471 socklen = sizeof(client->addr6);
472 } else {
473 client->addr.sin_family = AF_INET;
474 client->addr.sin_port = htons(client->port);
475 domain = AF_INET;
476 addr = (struct sockaddr *) &client->addr;
477 socklen = sizeof(client->addr);
478 }
Jens Axboe132159a2011-09-30 15:01:32 -0600479
Jens Axboe811826b2011-10-24 09:11:50 +0200480 fd = socket(domain, SOCK_STREAM, 0);
Jens Axboe132159a2011-09-30 15:01:32 -0600481 if (fd < 0) {
Jens Axboe25095e12012-03-09 17:09:55 +0100482 int ret = -errno;
483
Jens Axboe132159a2011-09-30 15:01:32 -0600484 log_err("fio: socket: %s\n", strerror(errno));
Jens Axboe25095e12012-03-09 17:09:55 +0100485 return ret;
Jens Axboe132159a2011-09-30 15:01:32 -0600486 }
487
Jens Axboe811826b2011-10-24 09:11:50 +0200488 if (connect(fd, addr, socklen) < 0) {
Jens Axboe25095e12012-03-09 17:09:55 +0100489 int ret = -errno;
490
Jens Axboe132159a2011-09-30 15:01:32 -0600491 log_err("fio: connect: %s\n", strerror(errno));
Jens Axboea7de0a12011-10-07 12:55:14 +0200492 log_err("fio: failed to connect to %s:%u\n", client->hostname,
493 client->port);
Jens Axboeb94cba42011-10-06 21:27:10 +0200494 close(fd);
Jens Axboe25095e12012-03-09 17:09:55 +0100495 return ret;
Jens Axboe132159a2011-09-30 15:01:32 -0600496 }
497
Jens Axboe87aa8f12011-10-06 21:24:13 +0200498 return fd;
499}
500
501static int fio_client_connect_sock(struct fio_client *client)
502{
503 struct sockaddr_un *addr = &client->addr_un;
Jens Axboe67bf9822013-01-10 11:23:19 +0100504 socklen_t len;
Jens Axboe87aa8f12011-10-06 21:24:13 +0200505 int fd;
506
507 memset(addr, 0, sizeof(*addr));
508 addr->sun_family = AF_UNIX;
Jens Axboe1cb96412014-04-14 08:50:33 -0600509 strncpy(addr->sun_path, client->hostname, sizeof(addr->sun_path) - 1);
Jens Axboe87aa8f12011-10-06 21:24:13 +0200510
511 fd = socket(AF_UNIX, SOCK_STREAM, 0);
512 if (fd < 0) {
Jens Axboe25095e12012-03-09 17:09:55 +0100513 int ret = -errno;
514
Jens Axboe87aa8f12011-10-06 21:24:13 +0200515 log_err("fio: socket: %s\n", strerror(errno));
Jens Axboe25095e12012-03-09 17:09:55 +0100516 return ret;
Jens Axboe87aa8f12011-10-06 21:24:13 +0200517 }
518
519 len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
520 if (connect(fd, (struct sockaddr *) addr, len) < 0) {
Jens Axboe25095e12012-03-09 17:09:55 +0100521 int ret = -errno;
522
Jens Axboe87aa8f12011-10-06 21:24:13 +0200523 log_err("fio: connect; %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200524 close(fd);
Jens Axboe25095e12012-03-09 17:09:55 +0100525 return ret;
Jens Axboe87aa8f12011-10-06 21:24:13 +0200526 }
527
528 return fd;
529}
530
Jens Axboe2f99deb2012-03-09 14:37:29 +0100531int fio_client_connect(struct fio_client *client)
Jens Axboe87aa8f12011-10-06 21:24:13 +0200532{
533 int fd;
534
535 dprint(FD_NET, "client: connect to host %s\n", client->hostname);
536
Jens Axboe87aa8f12011-10-06 21:24:13 +0200537 if (client->is_sock)
538 fd = fio_client_connect_sock(client);
539 else
540 fd = fio_client_connect_ip(client);
541
Jens Axboe89c17072011-10-11 10:15:51 +0200542 dprint(FD_NET, "client: %s connected %d\n", client->hostname, fd);
543
Jens Axboe87aa8f12011-10-06 21:24:13 +0200544 if (fd < 0)
Jens Axboea1a3ed52012-03-09 17:00:01 +0100545 return fd;
Jens Axboe87aa8f12011-10-06 21:24:13 +0200546
Jens Axboeb66570d2011-10-01 11:11:35 -0600547 client->fd = fd;
Jens Axboebebe6392011-10-07 10:00:51 +0200548 fio_client_add_hash(client);
Jens Axboe81179ee2011-10-04 12:42:06 +0200549 client->state = Client_connected;
Jens Axboed824c3b2012-03-09 17:45:43 +0100550
551 probe_client(client);
Jens Axboe132159a2011-09-30 15:01:32 -0600552 return 0;
553}
554
Jens Axboe0cf3ece2012-03-21 10:15:20 +0100555int fio_client_terminate(struct fio_client *client)
Jens Axboe2f99deb2012-03-09 14:37:29 +0100556{
Jens Axboe0cf3ece2012-03-21 10:15:20 +0100557 return fio_net_send_quit(client->fd);
Jens Axboe2f99deb2012-03-09 14:37:29 +0100558}
559
Elliott Hugheseda3a602017-05-19 18:53:02 -0700560static void fio_clients_terminate(void)
Jens Axboecc0df002011-10-03 20:53:32 +0200561{
562 struct flist_head *entry;
563 struct fio_client *client;
564
Jens Axboe60efd142011-10-04 13:30:11 +0200565 dprint(FD_NET, "client: terminate clients\n");
566
Jens Axboecc0df002011-10-03 20:53:32 +0200567 flist_for_each(entry, &client_list) {
568 client = flist_entry(entry, struct fio_client, list);
Jens Axboe2f99deb2012-03-09 14:37:29 +0100569 fio_client_terminate(client);
Jens Axboecc0df002011-10-03 20:53:32 +0200570 }
571}
572
573static void sig_int(int sig)
574{
Jens Axboebebe6392011-10-07 10:00:51 +0200575 dprint(FD_NET, "client: got signal %d\n", sig);
Jens Axboecc0df002011-10-03 20:53:32 +0200576 fio_clients_terminate();
577}
578
579static void client_signal_handler(void)
580{
581 struct sigaction act;
582
583 memset(&act, 0, sizeof(act));
584 act.sa_handler = sig_int;
585 act.sa_flags = SA_RESTART;
586 sigaction(SIGINT, &act, NULL);
587
588 memset(&act, 0, sizeof(act));
589 act.sa_handler = sig_int;
590 act.sa_flags = SA_RESTART;
591 sigaction(SIGTERM, &act, NULL);
Jens Axboeb852e7c2012-03-30 10:30:35 +0200592
Bruce Cran2f694502012-10-10 16:34:13 +0100593/* Windows uses SIGBREAK as a quit signal from other applications */
594#ifdef WIN32
595 memset(&act, 0, sizeof(act));
596 act.sa_handler = sig_int;
597 act.sa_flags = SA_RESTART;
598 sigaction(SIGBREAK, &act, NULL);
599#endif
600
Jens Axboeb852e7c2012-03-30 10:30:35 +0200601 memset(&act, 0, sizeof(act));
602 act.sa_handler = sig_show_status;
603 act.sa_flags = SA_RESTART;
604 sigaction(SIGUSR1, &act, NULL);
Jens Axboecc0df002011-10-03 20:53:32 +0200605}
606
Jens Axboe81179ee2011-10-04 12:42:06 +0200607static int send_client_cmd_line(struct fio_client *client)
608{
Jens Axboefa2ea802011-10-08 21:07:29 +0200609 struct cmd_single_line_pdu *cslp;
610 struct cmd_line_pdu *clp;
611 unsigned long offset;
Jens Axboe7f868312011-10-10 09:55:21 +0200612 unsigned int *lens;
Jens Axboefa2ea802011-10-08 21:07:29 +0200613 void *pdu;
614 size_t mem;
Jens Axboe81179ee2011-10-04 12:42:06 +0200615 int i, ret;
616
Jens Axboe39e8e012011-10-04 13:46:08 +0200617 dprint(FD_NET, "client: send cmdline %d\n", client->argc);
Jens Axboe60efd142011-10-04 13:30:11 +0200618
Jens Axboe7f868312011-10-10 09:55:21 +0200619 lens = malloc(client->argc * sizeof(unsigned int));
620
Jens Axboefa2ea802011-10-08 21:07:29 +0200621 /*
622 * Find out how much mem we need
623 */
Jens Axboe7f868312011-10-10 09:55:21 +0200624 for (i = 0, mem = 0; i < client->argc; i++) {
625 lens[i] = strlen(client->argv[i]) + 1;
626 mem += lens[i];
627 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200628
Jens Axboefa2ea802011-10-08 21:07:29 +0200629 /*
630 * We need one cmd_line_pdu, and argc number of cmd_single_line_pdu
631 */
632 mem += sizeof(*clp) + (client->argc * sizeof(*cslp));
633
634 pdu = malloc(mem);
635 clp = pdu;
636 offset = sizeof(*clp);
637
638 for (i = 0; i < client->argc; i++) {
Jens Axboe7f868312011-10-10 09:55:21 +0200639 uint16_t arg_len = lens[i];
Jens Axboefa2ea802011-10-08 21:07:29 +0200640
641 cslp = pdu + offset;
642 strcpy((char *) cslp->text, client->argv[i]);
643 cslp->len = cpu_to_le16(arg_len);
644 offset += sizeof(*cslp) + arg_len;
645 }
646
Jens Axboe7f868312011-10-10 09:55:21 +0200647 free(lens);
Jens Axboefa2ea802011-10-08 21:07:29 +0200648 clp->lines = cpu_to_le16(client->argc);
Jens Axboe46bcd492012-03-14 11:31:21 +0100649 clp->client_type = __cpu_to_le16(client->type);
Jens Axboe40c60512012-03-27 16:03:04 +0200650 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOBLINE, pdu, mem, NULL, NULL);
Jens Axboe81179ee2011-10-04 12:42:06 +0200651 free(pdu);
652 return ret;
653}
654
Jens Axboea37f69b2011-10-01 12:24:21 -0600655int fio_clients_connect(void)
656{
657 struct fio_client *client;
658 struct flist_head *entry, *tmp;
659 int ret;
660
Bruce Cran93bcfd22012-02-20 20:18:19 +0100661#ifdef WIN32
662 WSADATA wsd;
Jens Axboe3c3ed072012-03-27 09:12:39 +0200663 WSAStartup(MAKEWORD(2, 2), &wsd);
Bruce Cran93bcfd22012-02-20 20:18:19 +0100664#endif
665
Jens Axboe60efd142011-10-04 13:30:11 +0200666 dprint(FD_NET, "client: connect all\n");
667
Jens Axboecc0df002011-10-03 20:53:32 +0200668 client_signal_handler();
669
Jens Axboea37f69b2011-10-01 12:24:21 -0600670 flist_for_each_safe(entry, tmp, &client_list) {
671 client = flist_entry(entry, struct fio_client, list);
672
673 ret = fio_client_connect(client);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200674 if (ret) {
Jens Axboea37f69b2011-10-01 12:24:21 -0600675 remove_client(client);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200676 continue;
677 }
678
Jens Axboe81179ee2011-10-04 12:42:06 +0200679 if (client->argc > 1)
680 send_client_cmd_line(client);
Jens Axboea37f69b2011-10-01 12:24:21 -0600681 }
682
683 return !nr_clients;
684}
685
Jens Axboeb9d2f302012-03-08 20:36:28 +0100686int fio_start_client(struct fio_client *client)
687{
688 dprint(FD_NET, "client: start %s\n", client->hostname);
689 return fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_RUN, 0, NULL);
690}
691
692int fio_start_all_clients(void)
693{
694 struct fio_client *client;
695 struct flist_head *entry, *tmp;
696 int ret;
697
698 dprint(FD_NET, "client: start all\n");
699
Castor Fu952b05e2013-10-31 11:00:34 -0600700 fio_client_json_init();
701
Jens Axboeb9d2f302012-03-08 20:36:28 +0100702 flist_for_each_safe(entry, tmp, &client_list) {
703 client = flist_entry(entry, struct fio_client, list);
704
705 ret = fio_start_client(client);
706 if (ret) {
707 remove_client(client);
708 continue;
709 }
710 }
711
712 return flist_empty(&client_list);
713}
714
Jens Axboe35899182014-10-07 20:56:28 -0600715static int __fio_client_send_remote_ini(struct fio_client *client,
716 const char *filename)
717{
718 struct cmd_load_file_pdu *pdu;
719 size_t p_size;
720 int ret;
721
722 dprint(FD_NET, "send remote ini %s to %s\n", filename, client->hostname);
723
Jens Axboe79ecc302014-10-07 22:02:21 -0600724 p_size = sizeof(*pdu) + strlen(filename) + 1;
Jens Axboe35899182014-10-07 20:56:28 -0600725 pdu = malloc(p_size);
Jens Axboe79ecc302014-10-07 22:02:21 -0600726 memset(pdu, 0, p_size);
Jens Axboe35899182014-10-07 20:56:28 -0600727 pdu->name_len = strlen(filename);
728 strcpy((char *) pdu->file, filename);
729 pdu->client_type = cpu_to_le16((uint16_t) client->type);
730
731 client->sent_job = 1;
732 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_LOAD_FILE, pdu, p_size,NULL, NULL);
733 free(pdu);
734 return ret;
735}
736
Jens Axboe132159a2011-09-30 15:01:32 -0600737/*
738 * Send file contents to server backend. We could use sendfile(), but to remain
739 * more portable lets just read/write the darn thing.
740 */
Jens Axboe35899182014-10-07 20:56:28 -0600741static int __fio_client_send_local_ini(struct fio_client *client,
742 const char *filename)
Jens Axboe132159a2011-09-30 15:01:32 -0600743{
Jens Axboe46bcd492012-03-14 11:31:21 +0100744 struct cmd_job_pdu *pdu;
745 size_t p_size;
Jens Axboe132159a2011-09-30 15:01:32 -0600746 struct stat sb;
Jens Axboe46bcd492012-03-14 11:31:21 +0100747 char *p;
748 void *buf;
Jens Axboe132159a2011-09-30 15:01:32 -0600749 off_t len;
750 int fd, ret;
751
Jens Axboe46c48f12011-10-01 12:50:51 -0600752 dprint(FD_NET, "send ini %s to %s\n", filename, client->hostname);
753
Jens Axboe132159a2011-09-30 15:01:32 -0600754 fd = open(filename, O_RDONLY);
755 if (fd < 0) {
Jens Axboe0f7f9a92014-11-06 09:21:10 -0700756 ret = -errno;
Jens Axboee951bdc2011-10-05 21:58:45 +0200757 log_err("fio: job file <%s> open: %s\n", filename, strerror(errno));
Jens Axboe25095e12012-03-09 17:09:55 +0100758 return ret;
Jens Axboe132159a2011-09-30 15:01:32 -0600759 }
760
761 if (fstat(fd, &sb) < 0) {
Jens Axboe0f7f9a92014-11-06 09:21:10 -0700762 ret = -errno;
Jens Axboe132159a2011-09-30 15:01:32 -0600763 log_err("fio: job file stat: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200764 close(fd);
Jens Axboe25095e12012-03-09 17:09:55 +0100765 return ret;
Jens Axboe132159a2011-09-30 15:01:32 -0600766 }
767
Jens Axboe46bcd492012-03-14 11:31:21 +0100768 p_size = sb.st_size + sizeof(*pdu);
769 pdu = malloc(p_size);
770 buf = pdu->buf;
Jens Axboe132159a2011-09-30 15:01:32 -0600771
772 len = sb.st_size;
773 p = buf;
Jens Axboe3d0be7c2014-11-11 10:24:00 -0700774 if (read_data(fd, p, len)) {
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200775 log_err("fio: failed reading job file %s\n", filename);
Jens Axboeb94cba42011-10-06 21:27:10 +0200776 close(fd);
Hong Zhiguo03a22d92013-10-16 08:35:12 -0600777 free(pdu);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200778 return 1;
779 }
780
Jens Axboe46bcd492012-03-14 11:31:21 +0100781 pdu->buf_len = __cpu_to_le32(sb.st_size);
782 pdu->client_type = cpu_to_le32(client->type);
783
Jens Axboec2cb6862012-02-23 20:56:12 +0100784 client->sent_job = 1;
Jens Axboe40c60512012-03-27 16:03:04 +0200785 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, pdu, p_size, NULL, NULL);
Jens Axboe46bcd492012-03-14 11:31:21 +0100786 free(pdu);
Jens Axboeb94cba42011-10-06 21:27:10 +0200787 close(fd);
Jens Axboe132159a2011-09-30 15:01:32 -0600788 return ret;
789}
Jens Axboe37db14f2011-09-30 17:00:42 -0600790
Jens Axboe35899182014-10-07 20:56:28 -0600791int fio_client_send_ini(struct fio_client *client, const char *filename,
Elliott Hugheseda3a602017-05-19 18:53:02 -0700792 bool remote)
Jens Axboe9988ca72012-03-09 15:14:06 +0100793{
Jens Axboe25095e12012-03-09 17:09:55 +0100794 int ret;
795
Jens Axboe35899182014-10-07 20:56:28 -0600796 if (!remote)
797 ret = __fio_client_send_local_ini(client, filename);
798 else
799 ret = __fio_client_send_remote_ini(client, filename);
800
Jens Axboe3af45202012-03-13 09:59:53 +0100801 if (!ret)
802 client->sent_job = 1;
Jens Axboe9988ca72012-03-09 15:14:06 +0100803
Jens Axboe25095e12012-03-09 17:09:55 +0100804 return ret;
Jens Axboe9988ca72012-03-09 15:14:06 +0100805}
806
Jens Axboe35899182014-10-07 20:56:28 -0600807static int fio_client_send_cf(struct fio_client *client,
808 struct client_file *cf)
809{
810 return fio_client_send_ini(client, cf->file, cf->remote);
811}
812
Jens Axboea37f69b2011-10-01 12:24:21 -0600813int fio_clients_send_ini(const char *filename)
814{
815 struct fio_client *client;
816 struct flist_head *entry, *tmp;
817
818 flist_for_each_safe(entry, tmp, &client_list) {
Elliott Hugheseda3a602017-05-19 18:53:02 -0700819 bool failed = false;
820
Jens Axboea37f69b2011-10-01 12:24:21 -0600821 client = flist_entry(entry, struct fio_client, list);
822
Jens Axboe35899182014-10-07 20:56:28 -0600823 if (client->nr_files) {
Jens Axboe14ea90e2012-08-26 17:33:34 +0200824 int i;
825
Jens Axboe35899182014-10-07 20:56:28 -0600826 for (i = 0; i < client->nr_files; i++) {
827 struct client_file *cf;
Jens Axboe14ea90e2012-08-26 17:33:34 +0200828
Jens Axboe35899182014-10-07 20:56:28 -0600829 cf = &client->files[i];
830
831 if (fio_client_send_cf(client, cf)) {
Elliott Hugheseda3a602017-05-19 18:53:02 -0700832 failed = true;
Jens Axboe14ea90e2012-08-26 17:33:34 +0200833 remove_client(client);
834 break;
835 }
836 }
Jens Axboe35899182014-10-07 20:56:28 -0600837 }
Elliott Hugheseda3a602017-05-19 18:53:02 -0700838 if (client->sent_job || failed)
Jens Axboe35899182014-10-07 20:56:28 -0600839 continue;
840 if (!filename || fio_client_send_ini(client, filename, 0))
Jens Axboe3af45202012-03-13 09:59:53 +0100841 remove_client(client);
Jens Axboea37f69b2011-10-01 12:24:21 -0600842 }
843
844 return !nr_clients;
845}
846
Jens Axboe40c60512012-03-27 16:03:04 +0200847int fio_client_update_options(struct fio_client *client,
848 struct thread_options *o, uint64_t *tag)
849{
850 struct cmd_add_job_pdu pdu;
851
852 pdu.thread_number = cpu_to_le32(client->thread_number);
853 pdu.groupid = cpu_to_le32(client->groupid);
854 convert_thread_options_to_net(&pdu.top, o);
Castor Fu952b05e2013-10-31 11:00:34 -0600855
Jens Axboe40c60512012-03-27 16:03:04 +0200856 return fio_net_send_cmd(client->fd, FIO_NET_CMD_UPDATE_JOB, &pdu, sizeof(pdu), tag, &client->cmd_list);
857}
858
Jens Axboea64e88d2011-10-03 14:20:01 +0200859static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
860{
861 dst->max_val = le64_to_cpu(src->max_val);
862 dst->min_val = le64_to_cpu(src->min_val);
863 dst->samples = le64_to_cpu(src->samples);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200864
865 /*
866 * Floats arrive as IEEE 754 encoded uint64_t, convert back to double
867 */
868 dst->mean.u.f = fio_uint64_to_double(le64_to_cpu(dst->mean.u.i));
869 dst->S.u.f = fio_uint64_to_double(le64_to_cpu(dst->S.u.i));
Jens Axboea64e88d2011-10-03 14:20:01 +0200870}
871
872static void convert_ts(struct thread_stat *dst, struct thread_stat *src)
873{
874 int i, j;
875
Jens Axboe2f122b12012-03-15 13:10:19 +0100876 dst->error = le32_to_cpu(src->error);
877 dst->thread_number = le32_to_cpu(src->thread_number);
878 dst->groupid = le32_to_cpu(src->groupid);
879 dst->pid = le32_to_cpu(src->pid);
880 dst->members = le32_to_cpu(src->members);
Jens Axboe771e58b2013-01-30 12:56:23 +0100881 dst->unified_rw_rep = le32_to_cpu(src->unified_rw_rep);
Jens Axboea64e88d2011-10-03 14:20:01 +0200882
Jens Axboe298921d2012-09-24 18:47:01 +0200883 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
Jens Axboea64e88d2011-10-03 14:20:01 +0200884 convert_io_stat(&dst->clat_stat[i], &src->clat_stat[i]);
885 convert_io_stat(&dst->slat_stat[i], &src->slat_stat[i]);
886 convert_io_stat(&dst->lat_stat[i], &src->lat_stat[i]);
887 convert_io_stat(&dst->bw_stat[i], &src->bw_stat[i]);
888 }
889
890 dst->usr_time = le64_to_cpu(src->usr_time);
891 dst->sys_time = le64_to_cpu(src->sys_time);
892 dst->ctx = le64_to_cpu(src->ctx);
893 dst->minf = le64_to_cpu(src->minf);
894 dst->majf = le64_to_cpu(src->majf);
895 dst->clat_percentiles = le64_to_cpu(src->clat_percentiles);
Jens Axboe623216d2014-11-07 18:47:41 -0700896 dst->percentile_precision = le64_to_cpu(src->percentile_precision);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200897
898 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
899 fio_fp64_t *fps = &src->percentile_list[i];
900 fio_fp64_t *fpd = &dst->percentile_list[i];
901
902 fpd->u.f = fio_uint64_to_double(le64_to_cpu(fps->u.i));
903 }
Jens Axboea64e88d2011-10-03 14:20:01 +0200904
905 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
906 dst->io_u_map[i] = le32_to_cpu(src->io_u_map[i]);
907 dst->io_u_submit[i] = le32_to_cpu(src->io_u_submit[i]);
908 dst->io_u_complete[i] = le32_to_cpu(src->io_u_complete[i]);
909 }
910
Elliott Hugheseda3a602017-05-19 18:53:02 -0700911 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++)
Jens Axboea64e88d2011-10-03 14:20:01 +0200912 dst->io_u_lat_u[i] = le32_to_cpu(src->io_u_lat_u[i]);
Elliott Hugheseda3a602017-05-19 18:53:02 -0700913 for (i = 0; i < FIO_IO_U_LAT_M_NR; i++)
Jens Axboea64e88d2011-10-03 14:20:01 +0200914 dst->io_u_lat_m[i] = le32_to_cpu(src->io_u_lat_m[i]);
Jens Axboea64e88d2011-10-03 14:20:01 +0200915
Jens Axboe298921d2012-09-24 18:47:01 +0200916 for (i = 0; i < DDIR_RWDIR_CNT; i++)
Jens Axboea64e88d2011-10-03 14:20:01 +0200917 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
918 dst->io_u_plat[i][j] = le32_to_cpu(src->io_u_plat[i][j]);
919
Jens Axboe78799de2013-01-29 20:53:52 +0100920 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
Jens Axboea64e88d2011-10-03 14:20:01 +0200921 dst->total_io_u[i] = le64_to_cpu(src->total_io_u[i]);
Jens Axboe93eee042011-10-03 21:08:48 +0200922 dst->short_io_u[i] = le64_to_cpu(src->short_io_u[i]);
Jens Axboe67e149c2014-10-09 13:27:44 -0600923 dst->drop_io_u[i] = le64_to_cpu(src->drop_io_u[i]);
Jens Axboea64e88d2011-10-03 14:20:01 +0200924 }
925
926 dst->total_submit = le64_to_cpu(src->total_submit);
927 dst->total_complete = le64_to_cpu(src->total_complete);
928
Jens Axboe298921d2012-09-24 18:47:01 +0200929 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
Jens Axboea64e88d2011-10-03 14:20:01 +0200930 dst->io_bytes[i] = le64_to_cpu(src->io_bytes[i]);
931 dst->runtime[i] = le64_to_cpu(src->runtime[i]);
932 }
933
934 dst->total_run_time = le64_to_cpu(src->total_run_time);
935 dst->continue_on_error = le16_to_cpu(src->continue_on_error);
936 dst->total_err_count = le64_to_cpu(src->total_err_count);
Jens Axboeddcc0b62011-10-03 14:45:27 +0200937 dst->first_error = le32_to_cpu(src->first_error);
938 dst->kb_base = le32_to_cpu(src->kb_base);
Steven Noonanad705bc2013-04-08 15:05:25 -0700939 dst->unit_base = le32_to_cpu(src->unit_base);
Jens Axboe3e260a42013-12-09 12:38:53 -0700940
941 dst->latency_depth = le32_to_cpu(src->latency_depth);
942 dst->latency_target = le64_to_cpu(src->latency_target);
943 dst->latency_window = le64_to_cpu(src->latency_window);
944 dst->latency_percentile.u.f = fio_uint64_to_double(le64_to_cpu(src->latency_percentile.u.i));
Elliott Hugheseda3a602017-05-19 18:53:02 -0700945
946 dst->nr_block_infos = le64_to_cpu(src->nr_block_infos);
947 for (i = 0; i < dst->nr_block_infos; i++)
948 dst->block_infos[i] = le32_to_cpu(src->block_infos[i]);
949
950 dst->ss_dur = le64_to_cpu(src->ss_dur);
951 dst->ss_state = le32_to_cpu(src->ss_state);
952 dst->ss_head = le32_to_cpu(src->ss_head);
953 dst->ss_limit.u.f = fio_uint64_to_double(le64_to_cpu(src->ss_limit.u.i));
954 dst->ss_slope.u.f = fio_uint64_to_double(le64_to_cpu(src->ss_slope.u.i));
955 dst->ss_deviation.u.f = fio_uint64_to_double(le64_to_cpu(src->ss_deviation.u.i));
956 dst->ss_criterion.u.f = fio_uint64_to_double(le64_to_cpu(src->ss_criterion.u.i));
957
958 if (dst->ss_state & __FIO_SS_DATA) {
959 for (i = 0; i < dst->ss_dur; i++ ) {
960 dst->ss_iops_data[i] = le64_to_cpu(src->ss_iops_data[i]);
961 dst->ss_bw_data[i] = le64_to_cpu(src->ss_bw_data[i]);
962 }
963 }
Jens Axboea64e88d2011-10-03 14:20:01 +0200964}
965
966static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
967{
968 int i;
969
Jens Axboe298921d2012-09-24 18:47:01 +0200970 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
Jens Axboea64e88d2011-10-03 14:20:01 +0200971 dst->max_run[i] = le64_to_cpu(src->max_run[i]);
972 dst->min_run[i] = le64_to_cpu(src->min_run[i]);
973 dst->max_bw[i] = le64_to_cpu(src->max_bw[i]);
974 dst->min_bw[i] = le64_to_cpu(src->min_bw[i]);
Elliott Hugheseda3a602017-05-19 18:53:02 -0700975 dst->iobytes[i] = le64_to_cpu(src->iobytes[i]);
Jens Axboea64e88d2011-10-03 14:20:01 +0200976 dst->agg[i] = le64_to_cpu(src->agg[i]);
977 }
978
979 dst->kb_base = le32_to_cpu(src->kb_base);
Steven Noonanad705bc2013-04-08 15:05:25 -0700980 dst->unit_base = le32_to_cpu(src->unit_base);
Jens Axboea64e88d2011-10-03 14:20:01 +0200981 dst->groupid = le32_to_cpu(src->groupid);
Jens Axboe771e58b2013-01-30 12:56:23 +0100982 dst->unified_rw_rep = le32_to_cpu(src->unified_rw_rep);
Jens Axboea64e88d2011-10-03 14:20:01 +0200983}
984
Castor Fu952b05e2013-10-31 11:00:34 -0600985static void json_object_add_client_info(struct json_object *obj,
Castor Fu54bcda52014-08-22 17:16:44 -0500986 struct fio_client *client)
Castor Fu952b05e2013-10-31 11:00:34 -0600987{
Castor Fu54bcda52014-08-22 17:16:44 -0500988 const char *hostname = client->hostname ? client->hostname : "";
989
990 json_object_add_value_string(obj, "hostname", hostname);
Castor Fu952b05e2013-10-31 11:00:34 -0600991 json_object_add_value_int(obj, "port", client->port);
992}
993
Jens Axboe89e5fad2012-03-05 09:21:12 +0100994static void handle_ts(struct fio_client *client, struct fio_net_cmd *cmd)
Jens Axboea64e88d2011-10-03 14:20:01 +0200995{
996 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
Elliott Hugheseda3a602017-05-19 18:53:02 -0700997 struct flist_head *opt_list = NULL;
Castor Fu952b05e2013-10-31 11:00:34 -0600998 struct json_object *tsobj;
Jens Axboea64e88d2011-10-03 14:20:01 +0200999
Elliott Hugheseda3a602017-05-19 18:53:02 -07001000 if (client->opt_lists && p->ts.thread_number <= client->jobs)
1001 opt_list = &client->opt_lists[p->ts.thread_number - 1];
1002
1003 tsobj = show_thread_status(&p->ts, &p->rs, opt_list, NULL);
Jens Axboe108fea72012-11-14 13:09:45 -07001004 client->did_stat = 1;
Castor Fu952b05e2013-10-31 11:00:34 -06001005 if (tsobj) {
1006 json_object_add_client_info(tsobj, client);
1007 json_array_add_value_object(clients_array, tsobj);
1008 }
Jens Axboe37f0c1a2011-10-11 14:08:33 +02001009
Jens Axboebfa8f5d2014-10-13 10:09:28 -06001010 if (sum_stat_clients <= 1)
Jens Axboe37f0c1a2011-10-11 14:08:33 +02001011 return;
1012
Elliott Hugheseda3a602017-05-19 18:53:02 -07001013 sum_thread_stats(&client_ts, &p->ts, sum_stat_nr == 1);
Jens Axboe37f0c1a2011-10-11 14:08:33 +02001014 sum_group_stats(&client_gs, &p->rs);
1015
1016 client_ts.members++;
Jens Axboe2f122b12012-03-15 13:10:19 +01001017 client_ts.thread_number = p->ts.thread_number;
Jens Axboe37f0c1a2011-10-11 14:08:33 +02001018 client_ts.groupid = p->ts.groupid;
Jens Axboe771e58b2013-01-30 12:56:23 +01001019 client_ts.unified_rw_rep = p->ts.unified_rw_rep;
Jens Axboe37f0c1a2011-10-11 14:08:33 +02001020
1021 if (++sum_stat_nr == sum_stat_clients) {
1022 strcpy(client_ts.name, "All clients");
Elliott Hugheseda3a602017-05-19 18:53:02 -07001023 tsobj = show_thread_status(&client_ts, &client_gs, NULL, NULL);
Castor Fu952b05e2013-10-31 11:00:34 -06001024 if (tsobj) {
1025 json_object_add_client_info(tsobj, client);
1026 json_array_add_value_object(clients_array, tsobj);
1027 }
Jens Axboe37f0c1a2011-10-11 14:08:33 +02001028 }
Jens Axboea64e88d2011-10-03 14:20:01 +02001029}
1030
Jens Axboe89e5fad2012-03-05 09:21:12 +01001031static void handle_gs(struct fio_client *client, struct fio_net_cmd *cmd)
Jens Axboea64e88d2011-10-03 14:20:01 +02001032{
1033 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
1034
Elliott Hugheseda3a602017-05-19 18:53:02 -07001035 if (output_format & FIO_OUTPUT_NORMAL)
1036 show_group_stats(gs, NULL);
1037}
1038
1039static void handle_job_opt(struct fio_client *client, struct fio_net_cmd *cmd)
1040{
1041 struct cmd_job_option *pdu = (struct cmd_job_option *) cmd->payload;
1042 struct print_option *p;
1043
1044 if (!job_opt_object)
1045 return;
1046
1047 pdu->global = le16_to_cpu(pdu->global);
1048 pdu->truncated = le16_to_cpu(pdu->truncated);
1049 pdu->groupid = le32_to_cpu(pdu->groupid);
1050
1051 p = malloc(sizeof(*p));
1052 p->name = strdup((char *) pdu->name);
1053 if (pdu->value[0] != '\0')
1054 p->value = strdup((char *) pdu->value);
1055 else
1056 p->value = NULL;
1057
1058 if (pdu->global) {
1059 const char *pos = "";
1060
1061 if (p->value)
1062 pos = p->value;
1063
1064 json_object_add_value_string(job_opt_object, p->name, pos);
1065 } else if (client->opt_lists) {
1066 struct flist_head *opt_list = &client->opt_lists[pdu->groupid];
1067
1068 flist_add_tail(&p->list, opt_list);
1069 }
Jens Axboea64e88d2011-10-03 14:20:01 +02001070}
1071
Jens Axboe3bf236c2012-03-03 20:35:50 +01001072static void handle_text(struct fio_client *client, struct fio_net_cmd *cmd)
1073{
1074 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
1075 const char *buf = (const char *) pdu->buf;
1076 const char *name;
1077 int fio_unused ret;
1078
1079 name = client->name ? client->name : client->hostname;
1080
1081 if (!client->skip_newline)
1082 fprintf(f_out, "<%s> ", name);
1083 ret = fwrite(buf, pdu->buf_len, 1, f_out);
1084 fflush(f_out);
1085 client->skip_newline = strchr(buf, '\n') == NULL;
1086}
1087
Jens Axboed09a64a2011-10-13 11:38:56 +02001088static void convert_agg(struct disk_util_agg *agg)
1089{
1090 int i;
1091
1092 for (i = 0; i < 2; i++) {
Jens Axboe2a3bf762014-12-01 09:42:48 -07001093 agg->ios[i] = le64_to_cpu(agg->ios[i]);
1094 agg->merges[i] = le64_to_cpu(agg->merges[i]);
Jens Axboed09a64a2011-10-13 11:38:56 +02001095 agg->sectors[i] = le64_to_cpu(agg->sectors[i]);
Jens Axboe2a3bf762014-12-01 09:42:48 -07001096 agg->ticks[i] = le64_to_cpu(agg->ticks[i]);
Jens Axboed09a64a2011-10-13 11:38:56 +02001097 }
1098
Jens Axboe2a3bf762014-12-01 09:42:48 -07001099 agg->io_ticks = le64_to_cpu(agg->io_ticks);
1100 agg->time_in_queue = le64_to_cpu(agg->time_in_queue);
Jens Axboed09a64a2011-10-13 11:38:56 +02001101 agg->slavecount = le32_to_cpu(agg->slavecount);
Jens Axboe2b827fa2014-10-13 16:05:10 -06001102 agg->max_util.u.f = fio_uint64_to_double(le64_to_cpu(agg->max_util.u.i));
Jens Axboed09a64a2011-10-13 11:38:56 +02001103}
1104
1105static void convert_dus(struct disk_util_stat *dus)
1106{
1107 int i;
1108
1109 for (i = 0; i < 2; i++) {
Jens Axboe2a3bf762014-12-01 09:42:48 -07001110 dus->s.ios[i] = le64_to_cpu(dus->s.ios[i]);
1111 dus->s.merges[i] = le64_to_cpu(dus->s.merges[i]);
Jens Axboea3b4cf72014-04-11 12:17:53 -06001112 dus->s.sectors[i] = le64_to_cpu(dus->s.sectors[i]);
Jens Axboe2a3bf762014-12-01 09:42:48 -07001113 dus->s.ticks[i] = le64_to_cpu(dus->s.ticks[i]);
Jens Axboed09a64a2011-10-13 11:38:56 +02001114 }
1115
Jens Axboe2a3bf762014-12-01 09:42:48 -07001116 dus->s.io_ticks = le64_to_cpu(dus->s.io_ticks);
1117 dus->s.time_in_queue = le64_to_cpu(dus->s.time_in_queue);
Jens Axboea3b4cf72014-04-11 12:17:53 -06001118 dus->s.msec = le64_to_cpu(dus->s.msec);
Jens Axboed09a64a2011-10-13 11:38:56 +02001119}
1120
1121static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd)
1122{
1123 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
1124
Jens Axboed09a64a2011-10-13 11:38:56 +02001125 if (!client->disk_stats_shown) {
1126 client->disk_stats_shown = 1;
1127 log_info("\nDisk stats (read/write):\n");
1128 }
1129
Elliott Hugheseda3a602017-05-19 18:53:02 -07001130 if (output_format & FIO_OUTPUT_JSON) {
Castor Fu952b05e2013-10-31 11:00:34 -06001131 struct json_object *duobj;
1132 json_array_add_disk_util(&du->dus, &du->agg, du_array);
1133 duobj = json_array_last_value_object(du_array);
1134 json_object_add_client_info(duobj, client);
Elliott Hugheseda3a602017-05-19 18:53:02 -07001135 }
1136 if (output_format & FIO_OUTPUT_TERSE)
1137 print_disk_util(&du->dus, &du->agg, 1, NULL);
1138 if (output_format & FIO_OUTPUT_NORMAL)
1139 print_disk_util(&du->dus, &du->agg, 0, NULL);
Jens Axboed09a64a2011-10-13 11:38:56 +02001140}
1141
Jens Axboe3bf236c2012-03-03 20:35:50 +01001142static void convert_jobs_eta(struct jobs_eta *je)
Jens Axboecf451d12011-10-03 16:48:30 +02001143{
Jens Axboecf451d12011-10-03 16:48:30 +02001144 int i;
1145
1146 je->nr_running = le32_to_cpu(je->nr_running);
1147 je->nr_ramp = le32_to_cpu(je->nr_ramp);
1148 je->nr_pending = le32_to_cpu(je->nr_pending);
Jens Axboe714e85f2013-04-15 10:21:56 +02001149 je->nr_setting_up = le32_to_cpu(je->nr_setting_up);
Jens Axboecf451d12011-10-03 16:48:30 +02001150 je->files_open = le32_to_cpu(je->files_open);
Jens Axboecf451d12011-10-03 16:48:30 +02001151
Jens Axboe298921d2012-09-24 18:47:01 +02001152 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
Elliott Hugheseda3a602017-05-19 18:53:02 -07001153 je->m_rate[i] = le64_to_cpu(je->m_rate[i]);
1154 je->t_rate[i] = le64_to_cpu(je->t_rate[i]);
Jens Axboe298921d2012-09-24 18:47:01 +02001155 je->m_iops[i] = le32_to_cpu(je->m_iops[i]);
1156 je->t_iops[i] = le32_to_cpu(je->t_iops[i]);
Elliott Hugheseda3a602017-05-19 18:53:02 -07001157 je->rate[i] = le64_to_cpu(je->rate[i]);
Jens Axboe16725bb2013-04-11 12:43:05 +02001158 je->iops[i] = le32_to_cpu(je->iops[i]);
Jens Axboecf451d12011-10-03 16:48:30 +02001159 }
1160
Jens Axboeb51eedb2011-10-09 12:13:39 +02001161 je->elapsed_sec = le64_to_cpu(je->elapsed_sec);
Jens Axboecf451d12011-10-03 16:48:30 +02001162 je->eta_sec = le64_to_cpu(je->eta_sec);
Jens Axboe8c621fb2012-03-08 12:30:48 +01001163 je->nr_threads = le32_to_cpu(je->nr_threads);
Jens Axboeb7f05eb2012-05-11 20:33:02 +02001164 je->is_pow2 = le32_to_cpu(je->is_pow2);
Jens Axboeed2c0a12013-04-11 12:55:16 +02001165 je->unit_base = le32_to_cpu(je->unit_base);
Jens Axboe48fbb462011-10-09 12:19:08 +02001166}
Jens Axboecf451d12011-10-03 16:48:30 +02001167
Jens Axboe3e47bd22012-02-29 13:45:02 +01001168void fio_client_sum_jobs_eta(struct jobs_eta *dst, struct jobs_eta *je)
Jens Axboe48fbb462011-10-09 12:19:08 +02001169{
Jens Axboe48fbb462011-10-09 12:19:08 +02001170 int i;
1171
1172 dst->nr_running += je->nr_running;
1173 dst->nr_ramp += je->nr_ramp;
1174 dst->nr_pending += je->nr_pending;
Jens Axboe714e85f2013-04-15 10:21:56 +02001175 dst->nr_setting_up += je->nr_setting_up;
Jens Axboe48fbb462011-10-09 12:19:08 +02001176 dst->files_open += je->files_open;
Jens Axboe48fbb462011-10-09 12:19:08 +02001177
Jens Axboe298921d2012-09-24 18:47:01 +02001178 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
Jens Axboe3e47bd22012-02-29 13:45:02 +01001179 dst->m_rate[i] += je->m_rate[i];
1180 dst->t_rate[i] += je->t_rate[i];
1181 dst->m_iops[i] += je->m_iops[i];
1182 dst->t_iops[i] += je->t_iops[i];
Jens Axboe16725bb2013-04-11 12:43:05 +02001183 dst->rate[i] += je->rate[i];
1184 dst->iops[i] += je->iops[i];
Jens Axboe48fbb462011-10-09 12:19:08 +02001185 }
1186
1187 dst->elapsed_sec += je->elapsed_sec;
1188
1189 if (je->eta_sec > dst->eta_sec)
1190 dst->eta_sec = je->eta_sec;
Jens Axboe8c621fb2012-03-08 12:30:48 +01001191
1192 dst->nr_threads += je->nr_threads;
Jens Axboe27b91552014-06-27 15:30:06 -06001193
1194 /*
1195 * This wont be correct for multiple strings, but at least it
1196 * works for the basic cases.
1197 */
1198 strcpy((char *) dst->run_str, (char *) je->run_str);
Jens Axboe48fbb462011-10-09 12:19:08 +02001199}
1200
Elliott Hugheseda3a602017-05-19 18:53:02 -07001201static bool remove_reply_cmd(struct fio_client *client, struct fio_net_cmd *cmd)
Jens Axboe89c17072011-10-11 10:15:51 +02001202{
Jens Axboe40c60512012-03-27 16:03:04 +02001203 struct fio_net_cmd_reply *reply = NULL;
Jens Axboe89c17072011-10-11 10:15:51 +02001204 struct flist_head *entry;
1205
1206 flist_for_each(entry, &client->cmd_list) {
Jens Axboe40c60512012-03-27 16:03:04 +02001207 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
Jens Axboe89c17072011-10-11 10:15:51 +02001208
Jens Axboe40c60512012-03-27 16:03:04 +02001209 if (cmd->tag == (uintptr_t) reply)
Jens Axboe89c17072011-10-11 10:15:51 +02001210 break;
1211
Jens Axboe40c60512012-03-27 16:03:04 +02001212 reply = NULL;
Jens Axboe89c17072011-10-11 10:15:51 +02001213 }
1214
Jens Axboe40c60512012-03-27 16:03:04 +02001215 if (!reply) {
Jens Axboe4e0a8fa2013-04-15 11:40:57 +02001216 log_err("fio: client: unable to find matching tag (%llx)\n", (unsigned long long) cmd->tag);
Elliott Hugheseda3a602017-05-19 18:53:02 -07001217 return false;
Jens Axboe89c17072011-10-11 10:15:51 +02001218 }
1219
Jens Axboe40c60512012-03-27 16:03:04 +02001220 flist_del(&reply->list);
1221 cmd->tag = reply->saved_tag;
1222 free(reply);
Elliott Hugheseda3a602017-05-19 18:53:02 -07001223 return true;
Jens Axboe40c60512012-03-27 16:03:04 +02001224}
1225
1226int fio_client_wait_for_reply(struct fio_client *client, uint64_t tag)
1227{
1228 do {
1229 struct fio_net_cmd_reply *reply = NULL;
1230 struct flist_head *entry;
1231
1232 flist_for_each(entry, &client->cmd_list) {
1233 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
1234
1235 if (tag == (uintptr_t) reply)
1236 break;
1237
1238 reply = NULL;
1239 }
1240
1241 if (!reply)
1242 break;
1243
1244 usleep(1000);
1245 } while (1);
1246
1247 return 0;
Jens Axboe89c17072011-10-11 10:15:51 +02001248}
1249
Jens Axboe82c1ed32011-10-10 08:56:18 +02001250static void handle_eta(struct fio_client *client, struct fio_net_cmd *cmd)
Jens Axboe48fbb462011-10-09 12:19:08 +02001251{
1252 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
Jens Axboedf380932011-10-11 14:25:08 +02001253 struct client_eta *eta = (struct client_eta *) (uintptr_t) cmd->tag;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001254
1255 dprint(FD_NET, "client: got eta tag %p, %d\n", eta, eta->pending);
Jens Axboe48fbb462011-10-09 12:19:08 +02001256
Jens Axboef77d2672011-10-10 14:36:07 +02001257 assert(client->eta_in_flight == eta);
1258
1259 client->eta_in_flight = NULL;
Jens Axboe82c1ed32011-10-10 08:56:18 +02001260 flist_del_init(&client->eta_list);
Elliott Hugheseda3a602017-05-19 18:53:02 -07001261 client->eta_timeouts = 0;
Jens Axboe82c1ed32011-10-10 08:56:18 +02001262
Jens Axboe2f99deb2012-03-09 14:37:29 +01001263 if (client->ops->jobs_eta)
1264 client->ops->jobs_eta(client, je);
1265
Jens Axboe3e47bd22012-02-29 13:45:02 +01001266 fio_client_sum_jobs_eta(&eta->eta, je);
Jens Axboea5276612012-03-04 15:15:08 +01001267 fio_client_dec_jobs_eta(eta, client->ops->eta);
Jens Axboecf451d12011-10-03 16:48:30 +02001268}
1269
Elliott Hugheseda3a602017-05-19 18:53:02 -07001270static void client_flush_hist_samples(FILE *f, int hist_coarseness, void *samples,
1271 uint64_t sample_size)
1272{
1273 struct io_sample *s;
1274 int log_offset;
1275 uint64_t i, j, nr_samples;
1276 struct io_u_plat_entry *entry;
1277 unsigned int *io_u_plat;
1278
1279 int stride = 1 << hist_coarseness;
1280
1281 if (!sample_size)
1282 return;
1283
1284 s = __get_sample(samples, 0, 0);
1285 log_offset = (s->__ddir & LOG_OFFSET_SAMPLE_BIT) != 0;
1286
1287 nr_samples = sample_size / __log_entry_sz(log_offset);
1288
1289 for (i = 0; i < nr_samples; i++) {
1290
1291 s = (struct io_sample *)((char *)__get_sample(samples, log_offset, i) +
1292 i * sizeof(struct io_u_plat_entry));
1293
1294 entry = s->data.plat_entry;
1295 io_u_plat = entry->io_u_plat;
1296
1297 fprintf(f, "%lu, %u, %u, ", (unsigned long) s->time,
1298 io_sample_ddir(s), s->bs);
1299 for (j = 0; j < FIO_IO_U_PLAT_NR - stride; j += stride) {
1300 fprintf(f, "%lu, ", hist_sum(j, stride, io_u_plat, NULL));
1301 }
1302 fprintf(f, "%lu\n", (unsigned long)
1303 hist_sum(FIO_IO_U_PLAT_NR - stride, stride, io_u_plat, NULL));
1304
1305 }
1306}
1307
1308static int fio_client_handle_iolog(struct fio_client *client,
1309 struct fio_net_cmd *cmd)
1310{
1311 struct cmd_iolog_pdu *pdu;
1312 bool store_direct;
1313 char *log_pathname;
1314
1315 pdu = convert_iolog(cmd, &store_direct);
1316 if (!pdu) {
1317 log_err("fio: failed converting IO log\n");
1318 return 1;
1319 }
1320
1321 /* allocate buffer big enough for next sprintf() call */
1322 log_pathname = malloc(10 + strlen((char *)pdu->name) +
1323 strlen(client->hostname));
1324 if (!log_pathname) {
1325 log_err("fio: memory allocation of unique pathname failed\n");
1326 return -1;
1327 }
1328 /* generate a unique pathname for the log file using hostname */
1329 sprintf(log_pathname, "%s.%s", pdu->name, client->hostname);
1330
1331 if (store_direct) {
1332 ssize_t ret;
1333 size_t sz;
1334 int fd;
1335
1336 fd = open((const char *) log_pathname,
1337 O_WRONLY | O_CREAT | O_TRUNC, 0644);
1338 if (fd < 0) {
1339 log_err("fio: open log %s: %s\n",
1340 log_pathname, strerror(errno));
1341 return 1;
1342 }
1343
1344 sz = cmd->pdu_len - sizeof(*pdu);
1345 ret = write(fd, pdu->samples, sz);
1346 close(fd);
1347
1348 if (ret != sz) {
1349 log_err("fio: short write on compressed log\n");
1350 return 1;
1351 }
1352
1353 return 0;
1354 } else {
1355 FILE *f;
1356 f = fopen((const char *) log_pathname, "w");
1357 if (!f) {
1358 log_err("fio: fopen log %s : %s\n",
1359 log_pathname, strerror(errno));
1360 return 1;
1361 }
1362
1363 if (pdu->log_type == IO_LOG_TYPE_HIST) {
1364 client_flush_hist_samples(f, pdu->log_hist_coarseness, pdu->samples,
1365 pdu->nr_samples * sizeof(struct io_sample));
1366 } else {
1367 flush_samples(f, pdu->samples,
1368 pdu->nr_samples * sizeof(struct io_sample));
1369 }
1370 fclose(f);
1371 return 0;
1372 }
1373}
1374
Jens Axboeb5296dd2011-10-07 16:35:56 +02001375static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd)
Jens Axboe2e03b4b2011-10-04 09:00:40 +02001376{
Jens Axboe3989b142013-04-12 13:13:24 +02001377 struct cmd_probe_reply_pdu *probe = (struct cmd_probe_reply_pdu *) cmd->payload;
Jens Axboed2333352011-10-11 15:07:23 +02001378 const char *os, *arch;
1379 char bit[16];
Jens Axboe2e03b4b2011-10-04 09:00:40 +02001380
Jens Axboecca84642011-10-07 12:47:57 +02001381 os = fio_get_os_string(probe->os);
1382 if (!os)
1383 os = "unknown";
1384
1385 arch = fio_get_arch_string(probe->arch);
1386 if (!arch)
1387 os = "unknown";
1388
Jens Axboed2333352011-10-11 15:07:23 +02001389 sprintf(bit, "%d-bit", probe->bpp * 8);
Jens Axboe3989b142013-04-12 13:13:24 +02001390 probe->flags = le64_to_cpu(probe->flags);
Jens Axboe38fdef22011-10-11 14:30:06 +02001391
Jens Axboe3989b142013-04-12 13:13:24 +02001392 log_info("hostname=%s, be=%u, %s, os=%s, arch=%s, fio=%s, flags=%lx\n",
Jens Axboe38fdef22011-10-11 14:30:06 +02001393 probe->hostname, probe->bigendian, bit, os, arch,
Jens Axboe3989b142013-04-12 13:13:24 +02001394 probe->fio_version, (unsigned long) probe->flags);
Jens Axboeb5296dd2011-10-07 16:35:56 +02001395
1396 if (!client->name)
1397 client->name = strdup((char *) probe->hostname);
Jens Axboe2e03b4b2011-10-04 09:00:40 +02001398}
1399
Jens Axboe11e950b2011-10-16 21:34:14 +02001400static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd)
1401{
1402 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
1403
1404 client->state = Client_started;
1405 client->jobs = le32_to_cpu(pdu->jobs);
Jens Axboe30f8e312014-10-13 11:33:27 -06001406 client->nr_stat = le32_to_cpu(pdu->stat_outputs);
Jens Axboe108fea72012-11-14 13:09:45 -07001407
Elliott Hugheseda3a602017-05-19 18:53:02 -07001408 if (client->jobs) {
1409 int i;
1410
1411 if (client->opt_lists)
1412 free(client->opt_lists);
1413
1414 client->opt_lists = malloc(client->jobs * sizeof(struct flist_head));
1415 for (i = 0; i < client->jobs; i++)
1416 INIT_FLIST_HEAD(&client->opt_lists[i]);
1417 }
1418
Jens Axboe30f8e312014-10-13 11:33:27 -06001419 sum_stat_clients += client->nr_stat;
Jens Axboe11e950b2011-10-16 21:34:14 +02001420}
1421
1422static void handle_stop(struct fio_client *client, struct fio_net_cmd *cmd)
1423{
Jens Axboe498c92c2011-10-17 09:14:42 +02001424 if (client->error)
1425 log_info("client <%s>: exited with error %d\n", client->hostname, client->error);
Jens Axboe11e950b2011-10-16 21:34:14 +02001426}
1427
Jens Axboe6b79c802012-03-08 10:51:36 +01001428static void convert_stop(struct fio_net_cmd *cmd)
1429{
1430 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
1431
1432 pdu->error = le32_to_cpu(pdu->error);
1433}
1434
Jens Axboe084d1c62012-03-03 20:28:07 +01001435static void convert_text(struct fio_net_cmd *cmd)
1436{
1437 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
1438
1439 pdu->level = le32_to_cpu(pdu->level);
1440 pdu->buf_len = le32_to_cpu(pdu->buf_len);
1441 pdu->log_sec = le64_to_cpu(pdu->log_sec);
1442 pdu->log_usec = le64_to_cpu(pdu->log_usec);
1443}
1444
Jens Axboe3989b142013-04-12 13:13:24 +02001445static struct cmd_iolog_pdu *convert_iolog_gz(struct fio_net_cmd *cmd,
1446 struct cmd_iolog_pdu *pdu)
Jens Axboe1b427252012-03-14 15:03:03 +01001447{
Jens Axboe3989b142013-04-12 13:13:24 +02001448#ifdef CONFIG_ZLIB
Jens Axboe1b427252012-03-14 15:03:03 +01001449 struct cmd_iolog_pdu *ret;
Jens Axboe1b427252012-03-14 15:03:03 +01001450 z_stream stream;
Jens Axboe3989b142013-04-12 13:13:24 +02001451 uint32_t nr_samples;
1452 size_t total;
Jens Axboe1b427252012-03-14 15:03:03 +01001453 void *p;
1454
1455 stream.zalloc = Z_NULL;
1456 stream.zfree = Z_NULL;
1457 stream.opaque = Z_NULL;
1458 stream.avail_in = 0;
1459 stream.next_in = Z_NULL;
1460
1461 if (inflateInit(&stream) != Z_OK)
1462 return NULL;
1463
1464 /*
Jens Axboef5ed7652012-03-14 16:28:07 +01001465 * Get header first, it's not compressed
Jens Axboe1b427252012-03-14 15:03:03 +01001466 */
Jens Axboef2b9a672014-07-01 08:47:11 -06001467 nr_samples = le64_to_cpu(pdu->nr_samples);
Jens Axboe1b427252012-03-14 15:03:03 +01001468
Elliott Hugheseda3a602017-05-19 18:53:02 -07001469 if (pdu->log_type == IO_LOG_TYPE_HIST)
1470 total = nr_samples * (__log_entry_sz(le32_to_cpu(pdu->log_offset)) +
1471 sizeof(struct io_u_plat_entry));
1472 else
1473 total = nr_samples * __log_entry_sz(le32_to_cpu(pdu->log_offset));
Jens Axboef5ed7652012-03-14 16:28:07 +01001474 ret = malloc(total + sizeof(*pdu));
Jens Axboe1b427252012-03-14 15:03:03 +01001475 ret->nr_samples = nr_samples;
Jens Axboe3989b142013-04-12 13:13:24 +02001476
1477 memcpy(ret, pdu, sizeof(*pdu));
Jens Axboe1b427252012-03-14 15:03:03 +01001478
Jens Axboef5ed7652012-03-14 16:28:07 +01001479 p = (void *) ret + sizeof(*pdu);
1480
1481 stream.avail_in = cmd->pdu_len - sizeof(*pdu);
1482 stream.next_in = (void *) pdu + sizeof(*pdu);
Jens Axboe1b427252012-03-14 15:03:03 +01001483 while (stream.avail_in) {
1484 unsigned int this_chunk = 65536;
1485 unsigned int this_len;
1486 int err;
1487
1488 if (this_chunk > total)
1489 this_chunk = total;
1490
1491 stream.avail_out = this_chunk;
1492 stream.next_out = p;
1493 err = inflate(&stream, Z_NO_FLUSH);
Jens Axboe3c547fe2012-03-14 21:53:00 +01001494 /* may be Z_OK, or Z_STREAM_END */
1495 if (err < 0) {
Jens Axboe1b427252012-03-14 15:03:03 +01001496 log_err("fio: inflate error %d\n", err);
Jens Axboef5ed7652012-03-14 16:28:07 +01001497 free(ret);
1498 ret = NULL;
Jens Axboe3989b142013-04-12 13:13:24 +02001499 goto err;
Jens Axboe1b427252012-03-14 15:03:03 +01001500 }
1501
1502 this_len = this_chunk - stream.avail_out;
1503 p += this_len;
1504 total -= this_len;
1505 }
1506
Jens Axboe3989b142013-04-12 13:13:24 +02001507err:
1508 inflateEnd(&stream);
1509 return ret;
1510#else
1511 return NULL;
1512#endif
1513}
1514
1515/*
1516 * This has been compressed on the server side, since it can be big.
1517 * Uncompress here.
1518 */
Elliott Hugheseda3a602017-05-19 18:53:02 -07001519static struct cmd_iolog_pdu *convert_iolog(struct fio_net_cmd *cmd,
1520 bool *store_direct)
Jens Axboe3989b142013-04-12 13:13:24 +02001521{
1522 struct cmd_iolog_pdu *pdu = (struct cmd_iolog_pdu *) cmd->payload;
1523 struct cmd_iolog_pdu *ret;
Jens Axboeccefd5f2014-06-30 20:59:03 -06001524 uint64_t i;
Elliott Hugheseda3a602017-05-19 18:53:02 -07001525 int compressed;
Jens Axboeccefd5f2014-06-30 20:59:03 -06001526 void *samples;
Jens Axboe3989b142013-04-12 13:13:24 +02001527
Elliott Hugheseda3a602017-05-19 18:53:02 -07001528 *store_direct = false;
1529
Jens Axboe3989b142013-04-12 13:13:24 +02001530 /*
1531 * Convert if compressed and we support it. If it's not
1532 * compressed, we need not do anything.
1533 */
Elliott Hugheseda3a602017-05-19 18:53:02 -07001534 compressed = le32_to_cpu(pdu->compressed);
1535 if (compressed == XMIT_COMPRESSED) {
Jens Axboe3989b142013-04-12 13:13:24 +02001536#ifndef CONFIG_ZLIB
1537 log_err("fio: server sent compressed data by mistake\n");
1538 return NULL;
1539#endif
1540 ret = convert_iolog_gz(cmd, pdu);
1541 if (!ret) {
1542 log_err("fio: failed decompressing log\n");
1543 return NULL;
1544 }
Elliott Hugheseda3a602017-05-19 18:53:02 -07001545 } else if (compressed == STORE_COMPRESSED) {
1546 *store_direct = true;
1547 ret = pdu;
Jens Axboe3989b142013-04-12 13:13:24 +02001548 } else
1549 ret = pdu;
1550
Jens Axboeccefd5f2014-06-30 20:59:03 -06001551 ret->nr_samples = le64_to_cpu(ret->nr_samples);
Jens Axboe3989b142013-04-12 13:13:24 +02001552 ret->thread_number = le32_to_cpu(ret->thread_number);
Jens Axboe3989b142013-04-12 13:13:24 +02001553 ret->log_type = le32_to_cpu(ret->log_type);
1554 ret->compressed = le32_to_cpu(ret->compressed);
Jens Axboeccefd5f2014-06-30 20:59:03 -06001555 ret->log_offset = le32_to_cpu(ret->log_offset);
Elliott Hugheseda3a602017-05-19 18:53:02 -07001556 ret->log_hist_coarseness = le32_to_cpu(ret->log_hist_coarseness);
1557
1558 if (*store_direct)
1559 return ret;
Jens Axboe3989b142013-04-12 13:13:24 +02001560
Jens Axboee8c4fb02014-07-01 16:07:59 -06001561 samples = &ret->samples[0];
Jens Axboef5ed7652012-03-14 16:28:07 +01001562 for (i = 0; i < ret->nr_samples; i++) {
Jens Axboeccefd5f2014-06-30 20:59:03 -06001563 struct io_sample *s;
Jens Axboef5ed7652012-03-14 16:28:07 +01001564
Jens Axboeccefd5f2014-06-30 20:59:03 -06001565 s = __get_sample(samples, ret->log_offset, i);
Elliott Hugheseda3a602017-05-19 18:53:02 -07001566 if (ret->log_type == IO_LOG_TYPE_HIST)
1567 s = (struct io_sample *)((void *)s + sizeof(struct io_u_plat_entry) * i);
1568
Jens Axboebac4af12014-07-03 13:42:28 -06001569 s->time = le64_to_cpu(s->time);
Elliott Hugheseda3a602017-05-19 18:53:02 -07001570 s->data.val = le64_to_cpu(s->data.val);
Jens Axboebac4af12014-07-03 13:42:28 -06001571 s->__ddir = le32_to_cpu(s->__ddir);
1572 s->bs = le32_to_cpu(s->bs);
Jens Axboeccefd5f2014-06-30 20:59:03 -06001573
1574 if (ret->log_offset) {
1575 struct io_sample_offset *so = (void *) s;
1576
1577 so->offset = le64_to_cpu(so->offset);
1578 }
Elliott Hugheseda3a602017-05-19 18:53:02 -07001579
1580 if (ret->log_type == IO_LOG_TYPE_HIST) {
1581 s->data.plat_entry = (struct io_u_plat_entry *)(((void *)s) + sizeof(*s));
1582 s->data.plat_entry->list.next = NULL;
1583 s->data.plat_entry->list.prev = NULL;
1584 }
Jens Axboef5ed7652012-03-14 16:28:07 +01001585 }
1586
Jens Axboe1b427252012-03-14 15:03:03 +01001587 return ret;
1588}
1589
Jens Axboede54cfd2014-11-10 20:34:00 -07001590static void sendfile_reply(int fd, struct cmd_sendfile_reply *rep,
1591 size_t size, uint64_t tag)
1592{
1593 rep->error = cpu_to_le32(rep->error);
1594 fio_net_send_cmd(fd, FIO_NET_CMD_SENDFILE, rep, size, &tag, NULL);
1595}
1596
Elliott Hugheseda3a602017-05-19 18:53:02 -07001597static int fio_send_file(struct fio_client *client, struct cmd_sendfile *pdu,
1598 uint64_t tag)
Jens Axboede54cfd2014-11-10 20:34:00 -07001599{
1600 struct cmd_sendfile_reply *rep;
1601 struct stat sb;
1602 size_t size;
1603 int fd;
1604
1605 size = sizeof(*rep);
1606 rep = malloc(size);
1607
1608 if (stat((char *)pdu->path, &sb) < 0) {
1609fail:
1610 rep->error = errno;
1611 sendfile_reply(client->fd, rep, size, tag);
1612 free(rep);
1613 return 1;
1614 }
1615
1616 size += sb.st_size;
1617 rep = realloc(rep, size);
1618 rep->size = cpu_to_le32((uint32_t) sb.st_size);
1619
1620 fd = open((char *)pdu->path, O_RDONLY);
1621 if (fd == -1 )
1622 goto fail;
1623
1624 rep->error = read_data(fd, &rep->data, sb.st_size);
1625 sendfile_reply(client->fd, rep, size, tag);
1626 free(rep);
1627 close(fd);
1628 return 0;
1629}
1630
Jens Axboea5276612012-03-04 15:15:08 +01001631int fio_handle_client(struct fio_client *client)
Jens Axboe37db14f2011-09-30 17:00:42 -06001632{
Jens Axboea5276612012-03-04 15:15:08 +01001633 struct client_ops *ops = client->ops;
Jens Axboe37db14f2011-09-30 17:00:42 -06001634 struct fio_net_cmd *cmd;
Elliott Hugheseda3a602017-05-19 18:53:02 -07001635 int size;
Jens Axboe37db14f2011-09-30 17:00:42 -06001636
Jens Axboe60efd142011-10-04 13:30:11 +02001637 dprint(FD_NET, "client: handle %s\n", client->hostname);
1638
Elliott Hugheseda3a602017-05-19 18:53:02 -07001639 cmd = fio_net_recv_cmd(client->fd, true);
Jens Axboee951bdc2011-10-05 21:58:45 +02001640 if (!cmd)
1641 return 0;
Jens Axboec2c94582011-10-05 20:31:30 +02001642
Jens Axboeb9d2f302012-03-08 20:36:28 +01001643 dprint(FD_NET, "client: got cmd op %s from %s (pdu=%u)\n",
1644 fio_server_op(cmd->opcode), client->hostname, cmd->pdu_len);
Jens Axboe46c48f12011-10-01 12:50:51 -06001645
Jens Axboee951bdc2011-10-05 21:58:45 +02001646 switch (cmd->opcode) {
1647 case FIO_NET_CMD_QUIT:
Jens Axboe3ec62ec2012-03-01 12:01:29 +01001648 if (ops->quit)
Jens Axboe35c0ba72012-03-14 10:56:40 +01001649 ops->quit(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +02001650 remove_client(client);
Jens Axboee951bdc2011-10-05 21:58:45 +02001651 break;
Jens Axboe084d1c62012-03-03 20:28:07 +01001652 case FIO_NET_CMD_TEXT:
1653 convert_text(cmd);
Jens Axboe35c0ba72012-03-14 10:56:40 +01001654 ops->text(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +02001655 break;
Jens Axboe3bf236c2012-03-03 20:35:50 +01001656 case FIO_NET_CMD_DU: {
1657 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
1658
1659 convert_dus(&du->dus);
1660 convert_agg(&du->agg);
1661
Stephen M. Camerondd366722012-02-24 08:17:30 +01001662 ops->disk_util(client, cmd);
Jens Axboed09a64a2011-10-13 11:38:56 +02001663 break;
Jens Axboe3bf236c2012-03-03 20:35:50 +01001664 }
1665 case FIO_NET_CMD_TS: {
1666 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
1667
Elliott Hugheseda3a602017-05-19 18:53:02 -07001668 dprint(FD_NET, "client: ts->ss_state = %u\n", (unsigned int) le32_to_cpu(p->ts.ss_state));
1669 if (le32_to_cpu(p->ts.ss_state) & __FIO_SS_DATA) {
1670 dprint(FD_NET, "client: received steadystate ring buffers\n");
1671
1672 size = le64_to_cpu(p->ts.ss_dur);
1673 p->ts.ss_iops_data = (uint64_t *) ((struct cmd_ts_pdu *)cmd->payload + 1);
1674 p->ts.ss_bw_data = p->ts.ss_iops_data + size;
1675 }
1676
Jens Axboe3bf236c2012-03-03 20:35:50 +01001677 convert_ts(&p->ts, &p->ts);
1678 convert_gs(&p->rs, &p->rs);
1679
Jens Axboe89e5fad2012-03-05 09:21:12 +01001680 ops->thread_status(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +02001681 break;
Jens Axboe3bf236c2012-03-03 20:35:50 +01001682 }
1683 case FIO_NET_CMD_GS: {
1684 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
1685
1686 convert_gs(gs, gs);
1687
Jens Axboe89e5fad2012-03-05 09:21:12 +01001688 ops->group_stats(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +02001689 break;
Jens Axboe3bf236c2012-03-03 20:35:50 +01001690 }
1691 case FIO_NET_CMD_ETA: {
1692 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
1693
Elliott Hugheseda3a602017-05-19 18:53:02 -07001694 if (!remove_reply_cmd(client, cmd))
1695 break;
Jens Axboe3bf236c2012-03-03 20:35:50 +01001696 convert_jobs_eta(je);
Jens Axboea5276612012-03-04 15:15:08 +01001697 handle_eta(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +02001698 break;
Jens Axboe3bf236c2012-03-03 20:35:50 +01001699 }
Jens Axboee951bdc2011-10-05 21:58:45 +02001700 case FIO_NET_CMD_PROBE:
Jens Axboe89c17072011-10-11 10:15:51 +02001701 remove_reply_cmd(client, cmd);
Stephen M. Camerondd366722012-02-24 08:17:30 +01001702 ops->probe(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +02001703 break;
Jens Axboe5d7793a2012-03-08 19:59:16 +01001704 case FIO_NET_CMD_SERVER_START:
Jens Axboe01be0382011-10-15 14:43:41 +02001705 client->state = Client_running;
Jens Axboe85dd01e2012-03-12 14:33:16 +01001706 if (ops->job_start)
1707 ops->job_start(client, cmd);
Jens Axboe01be0382011-10-15 14:43:41 +02001708 break;
Jens Axboe85dd01e2012-03-12 14:33:16 +01001709 case FIO_NET_CMD_START: {
1710 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
1711
1712 pdu->jobs = le32_to_cpu(pdu->jobs);
1713 ops->start(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +02001714 break;
Jens Axboe85dd01e2012-03-12 14:33:16 +01001715 }
Jens Axboe6b79c802012-03-08 10:51:36 +01001716 case FIO_NET_CMD_STOP: {
1717 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
1718
1719 convert_stop(cmd);
1720 client->state = Client_stopped;
Jens Axboe122c7722012-03-19 09:13:15 +01001721 client->error = le32_to_cpu(pdu->error);
1722 client->signal = le32_to_cpu(pdu->signal);
Jens Axboe6b79c802012-03-08 10:51:36 +01001723 ops->stop(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +02001724 break;
Jens Axboe6b79c802012-03-08 10:51:36 +01001725 }
Jens Axboe40c60512012-03-27 16:03:04 +02001726 case FIO_NET_CMD_ADD_JOB: {
1727 struct cmd_add_job_pdu *pdu = (struct cmd_add_job_pdu *) cmd->payload;
1728
1729 client->thread_number = le32_to_cpu(pdu->thread_number);
1730 client->groupid = le32_to_cpu(pdu->groupid);
1731
Jens Axboe807f9972012-03-02 10:25:24 +01001732 if (ops->add_job)
1733 ops->add_job(client, cmd);
Jens Axboe807f9972012-03-02 10:25:24 +01001734 break;
Jens Axboe40c60512012-03-27 16:03:04 +02001735 }
Jens Axboe1b427252012-03-14 15:03:03 +01001736 case FIO_NET_CMD_IOLOG:
Elliott Hugheseda3a602017-05-19 18:53:02 -07001737 fio_client_handle_iolog(client, cmd);
Jens Axboe1b427252012-03-14 15:03:03 +01001738 break;
Jens Axboe40c60512012-03-27 16:03:04 +02001739 case FIO_NET_CMD_UPDATE_JOB:
Jens Axboe40c60512012-03-27 16:03:04 +02001740 ops->update_job(client, cmd);
Jens Axboe649cee92012-03-28 09:15:32 +02001741 remove_reply_cmd(client, cmd);
Jens Axboe40c60512012-03-27 16:03:04 +02001742 break;
Jens Axboede54cfd2014-11-10 20:34:00 -07001743 case FIO_NET_CMD_VTRIGGER: {
1744 struct all_io_list *pdu = (struct all_io_list *) cmd->payload;
Elliott Hugheseda3a602017-05-19 18:53:02 -07001745 char buf[128];
1746 int off = 0;
Jens Axboede54cfd2014-11-10 20:34:00 -07001747
Elliott Hugheseda3a602017-05-19 18:53:02 -07001748 if (aux_path) {
1749 strcpy(buf, aux_path);
1750 off = strlen(buf);
1751 }
1752
1753 __verify_save_state(pdu, server_name(client, &buf[off], sizeof(buf) - off));
Jens Axboe7eebf502014-11-19 09:09:01 -07001754 exec_trigger(trigger_cmd);
Jens Axboede54cfd2014-11-10 20:34:00 -07001755 break;
1756 }
1757 case FIO_NET_CMD_SENDFILE: {
1758 struct cmd_sendfile *pdu = (struct cmd_sendfile *) cmd->payload;
Elliott Hugheseda3a602017-05-19 18:53:02 -07001759 fio_send_file(client, pdu, cmd->tag);
Jens Axboede54cfd2014-11-10 20:34:00 -07001760 break;
1761 }
Elliott Hugheseda3a602017-05-19 18:53:02 -07001762 case FIO_NET_CMD_JOB_OPT: {
1763 handle_job_opt(client, cmd);
1764 break;
1765 }
Jens Axboee951bdc2011-10-05 21:58:45 +02001766 default:
Jens Axboe89c17072011-10-11 10:15:51 +02001767 log_err("fio: unknown client op: %s\n", fio_server_op(cmd->opcode));
Jens Axboee951bdc2011-10-05 21:58:45 +02001768 break;
Jens Axboe37db14f2011-09-30 17:00:42 -06001769 }
1770
Jens Axboede54cfd2014-11-10 20:34:00 -07001771 free(cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +02001772 return 1;
Jens Axboe37db14f2011-09-30 17:00:42 -06001773}
Jens Axboeb66570d2011-10-01 11:11:35 -06001774
Jens Axboede54cfd2014-11-10 20:34:00 -07001775int fio_clients_send_trigger(const char *cmd)
1776{
1777 struct flist_head *entry;
1778 struct fio_client *client;
1779 size_t slen;
1780
1781 dprint(FD_NET, "client: send vtrigger: %s\n", cmd);
1782
1783 if (!cmd)
1784 slen = 0;
1785 else
1786 slen = strlen(cmd);
1787
1788 flist_for_each(entry, &client_list) {
1789 struct cmd_vtrigger_pdu *pdu;
1790
1791 client = flist_entry(entry, struct fio_client, list);
1792
1793 pdu = malloc(sizeof(*pdu) + slen);
1794 pdu->len = cpu_to_le16((uint16_t) slen);
1795 if (slen)
1796 memcpy(pdu->cmd, cmd, slen);
1797 fio_net_send_cmd(client->fd, FIO_NET_CMD_VTRIGGER, pdu,
1798 sizeof(*pdu) + slen, NULL, NULL);
1799 free(pdu);
1800 }
1801
1802 return 0;
1803}
1804
Jens Axboea5276612012-03-04 15:15:08 +01001805static void request_client_etas(struct client_ops *ops)
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001806{
1807 struct fio_client *client;
1808 struct flist_head *entry;
1809 struct client_eta *eta;
Jens Axboe82c1ed32011-10-10 08:56:18 +02001810 int skipped = 0;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001811
1812 dprint(FD_NET, "client: request eta (%d)\n", nr_clients);
1813
Jens Axboe27b91552014-06-27 15:30:06 -06001814 eta = calloc(1, sizeof(*eta) + __THREAD_RUNSTR_SZ(REAL_MAX_JOBS));
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001815 eta->pending = nr_clients;
1816
1817 flist_for_each(entry, &client_list) {
1818 client = flist_entry(entry, struct fio_client, list);
1819
Jens Axboe82c1ed32011-10-10 08:56:18 +02001820 if (!flist_empty(&client->eta_list)) {
1821 skipped++;
1822 continue;
1823 }
Jens Axboe01be0382011-10-15 14:43:41 +02001824 if (client->state != Client_running)
1825 continue;
Jens Axboe82c1ed32011-10-10 08:56:18 +02001826
Jens Axboef77d2672011-10-10 14:36:07 +02001827 assert(!client->eta_in_flight);
Jens Axboe82c1ed32011-10-10 08:56:18 +02001828 flist_add_tail(&client->eta_list, &eta_list);
Jens Axboef77d2672011-10-10 14:36:07 +02001829 client->eta_in_flight = eta;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001830 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_SEND_ETA,
Jens Axboedf380932011-10-11 14:25:08 +02001831 (uintptr_t) eta, &client->cmd_list);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001832 }
1833
Elliott Hugheseda3a602017-05-19 18:53:02 -07001834 while (skipped--) {
1835 if (!fio_client_dec_jobs_eta(eta, ops->eta))
1836 break;
1837 }
Jens Axboe82c1ed32011-10-10 08:56:18 +02001838
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001839 dprint(FD_NET, "client: requested eta tag %p\n", eta);
1840}
1841
Elliott Hugheseda3a602017-05-19 18:53:02 -07001842/*
1843 * A single SEND_ETA timeout isn't fatal. Attempt to recover.
1844 */
1845static int handle_cmd_timeout(struct fio_client *client,
1846 struct fio_net_cmd_reply *reply)
1847{
1848 flist_del(&reply->list);
1849 free(reply);
1850
1851 if (reply->opcode != FIO_NET_CMD_SEND_ETA)
1852 return 1;
1853
1854 log_info("client <%s>: timeout on SEND_ETA\n", client->hostname);
1855
1856 flist_del_init(&client->eta_list);
1857 if (client->eta_in_flight) {
1858 fio_client_dec_jobs_eta(client->eta_in_flight, client->ops->eta);
1859 client->eta_in_flight = NULL;
1860 }
1861
1862 /*
1863 * If we fail 5 in a row, give up...
1864 */
1865 if (client->eta_timeouts++ > 5)
1866 return 1;
1867
1868 return 0;
1869}
1870
Jens Axboe89c17072011-10-11 10:15:51 +02001871static int client_check_cmd_timeout(struct fio_client *client,
1872 struct timeval *now)
1873{
Jens Axboe40c60512012-03-27 16:03:04 +02001874 struct fio_net_cmd_reply *reply;
Jens Axboe89c17072011-10-11 10:15:51 +02001875 struct flist_head *entry, *tmp;
1876 int ret = 0;
1877
1878 flist_for_each_safe(entry, tmp, &client->cmd_list) {
Jens Axboe40c60512012-03-27 16:03:04 +02001879 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
Jens Axboe89c17072011-10-11 10:15:51 +02001880
Jens Axboe40c60512012-03-27 16:03:04 +02001881 if (mtime_since(&reply->tv, now) < FIO_NET_CLIENT_TIMEOUT)
Jens Axboe89c17072011-10-11 10:15:51 +02001882 continue;
1883
Elliott Hugheseda3a602017-05-19 18:53:02 -07001884 if (!handle_cmd_timeout(client, reply))
1885 continue;
1886
Jens Axboe89c17072011-10-11 10:15:51 +02001887 log_err("fio: client %s, timeout on cmd %s\n", client->hostname,
Jens Axboe40c60512012-03-27 16:03:04 +02001888 fio_server_op(reply->opcode));
Jens Axboe89c17072011-10-11 10:15:51 +02001889 ret = 1;
1890 }
1891
1892 return flist_empty(&client->cmd_list) && ret;
1893}
1894
Jens Axboea5276612012-03-04 15:15:08 +01001895static int fio_check_clients_timed_out(void)
Jens Axboe89c17072011-10-11 10:15:51 +02001896{
1897 struct fio_client *client;
1898 struct flist_head *entry, *tmp;
1899 struct timeval tv;
1900 int ret = 0;
1901
Jens Axboe67bf9822013-01-10 11:23:19 +01001902 fio_gettime(&tv, NULL);
Jens Axboe89c17072011-10-11 10:15:51 +02001903
1904 flist_for_each_safe(entry, tmp, &client_list) {
1905 client = flist_entry(entry, struct fio_client, list);
1906
1907 if (flist_empty(&client->cmd_list))
1908 continue;
1909
1910 if (!client_check_cmd_timeout(client, &tv))
1911 continue;
1912
Jens Axboea5276612012-03-04 15:15:08 +01001913 if (client->ops->timed_out)
1914 client->ops->timed_out(client);
Jens Axboeed727a42012-03-02 12:14:40 +01001915 else
1916 log_err("fio: client %s timed out\n", client->hostname);
1917
Jens Axboe4811fff2015-01-13 21:47:43 -07001918 client->error = ETIMEDOUT;
Jens Axboe89c17072011-10-11 10:15:51 +02001919 remove_client(client);
1920 ret = 1;
1921 }
1922
1923 return ret;
1924}
1925
Stephen M. Camerondd366722012-02-24 08:17:30 +01001926int fio_handle_clients(struct client_ops *ops)
Jens Axboeb66570d2011-10-01 11:11:35 -06001927{
Jens Axboeb66570d2011-10-01 11:11:35 -06001928 struct pollfd *pfds;
Jens Axboe498c92c2011-10-17 09:14:42 +02001929 int i, ret = 0, retval = 0;
Jens Axboeb66570d2011-10-01 11:11:35 -06001930
Jens Axboe67bf9822013-01-10 11:23:19 +01001931 fio_gettime(&eta_tv, NULL);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001932
Jens Axboeb66570d2011-10-01 11:11:35 -06001933 pfds = malloc(nr_clients * sizeof(struct pollfd));
1934
Jens Axboe37f0c1a2011-10-11 14:08:33 +02001935 init_thread_stat(&client_ts);
1936 init_group_run_stat(&client_gs);
1937
Jens Axboeb66570d2011-10-01 11:11:35 -06001938 while (!exit_backend && nr_clients) {
Jens Axboec2cb6862012-02-23 20:56:12 +01001939 struct flist_head *entry, *tmp;
1940 struct fio_client *client;
1941
Jens Axboe82a4be12011-10-01 12:40:32 -06001942 i = 0;
Jens Axboec2cb6862012-02-23 20:56:12 +01001943 flist_for_each_safe(entry, tmp, &client_list) {
Jens Axboe82a4be12011-10-01 12:40:32 -06001944 client = flist_entry(entry, struct fio_client, list);
1945
Jens Axboea5276612012-03-04 15:15:08 +01001946 if (!client->sent_job && !client->ops->stay_connected &&
Jens Axboec2cb6862012-02-23 20:56:12 +01001947 flist_empty(&client->cmd_list)) {
1948 remove_client(client);
1949 continue;
1950 }
1951
Jens Axboe82a4be12011-10-01 12:40:32 -06001952 pfds[i].fd = client->fd;
1953 pfds[i].events = POLLIN;
1954 i++;
1955 }
1956
Jens Axboec2cb6862012-02-23 20:56:12 +01001957 if (!nr_clients)
1958 break;
1959
Jens Axboe82a4be12011-10-01 12:40:32 -06001960 assert(i == nr_clients);
1961
Jens Axboe5c2857f2011-10-04 13:14:32 +02001962 do {
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001963 struct timeval tv;
Jens Axboede54cfd2014-11-10 20:34:00 -07001964 int timeout;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001965
Jens Axboe67bf9822013-01-10 11:23:19 +01001966 fio_gettime(&tv, NULL);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001967 if (mtime_since(&eta_tv, &tv) >= 900) {
Jens Axboea5276612012-03-04 15:15:08 +01001968 request_client_etas(ops);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001969 memcpy(&eta_tv, &tv, sizeof(tv));
Jens Axboe89c17072011-10-11 10:15:51 +02001970
Jens Axboea5276612012-03-04 15:15:08 +01001971 if (fio_check_clients_timed_out())
Jens Axboe89c17072011-10-11 10:15:51 +02001972 break;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001973 }
1974
Jens Axboede54cfd2014-11-10 20:34:00 -07001975 check_trigger_file();
1976
1977 timeout = min(100u, ops->eta_msec);
1978
1979 ret = poll(pfds, nr_clients, timeout);
Jens Axboe5c2857f2011-10-04 13:14:32 +02001980 if (ret < 0) {
1981 if (errno == EINTR)
1982 continue;
1983 log_err("fio: poll clients: %s\n", strerror(errno));
1984 break;
1985 } else if (!ret)
Jens Axboeb66570d2011-10-01 11:11:35 -06001986 continue;
Jens Axboe5c2857f2011-10-04 13:14:32 +02001987 } while (ret <= 0);
Jens Axboeb66570d2011-10-01 11:11:35 -06001988
1989 for (i = 0; i < nr_clients; i++) {
1990 if (!(pfds[i].revents & POLLIN))
1991 continue;
1992
1993 client = find_client_by_fd(pfds[i].fd);
1994 if (!client) {
Jens Axboe65e1a122013-08-30 10:13:36 -06001995 log_err("fio: unknown client fd %ld\n", (long) pfds[i].fd);
Jens Axboeb66570d2011-10-01 11:11:35 -06001996 continue;
1997 }
Jens Axboea5276612012-03-04 15:15:08 +01001998 if (!fio_handle_client(client)) {
Jens Axboe28d3ab02011-10-05 20:45:37 +02001999 log_info("client: host=%s disconnected\n",
2000 client->hostname);
2001 remove_client(client);
Jens Axboe498c92c2011-10-17 09:14:42 +02002002 retval = 1;
Jens Axboe38990762011-10-17 13:31:33 +02002003 } else if (client->error)
Jens Axboe498c92c2011-10-17 09:14:42 +02002004 retval = 1;
Jens Axboe343cb4a2012-03-09 17:16:51 +01002005 fio_put_client(client);
Jens Axboeb66570d2011-10-01 11:11:35 -06002006 }
2007 }
2008
Castor Fu952b05e2013-10-31 11:00:34 -06002009 fio_client_json_fini();
2010
Jens Axboeb66570d2011-10-01 11:11:35 -06002011 free(pfds);
Jens Axboe4811fff2015-01-13 21:47:43 -07002012 return retval || error_clients;
Jens Axboeb66570d2011-10-01 11:11:35 -06002013}