blob: ffccc5d4f54cacb526636a4663c93805df171512 [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 Axboe1b427252012-03-14 15:03:03 +010017#include <zlib.h>
Jens Axboe132159a2011-09-30 15:01:32 -060018
19#include "fio.h"
Stephen M. Camerondd366722012-02-24 08:17:30 +010020#include "client.h"
Jens Axboe132159a2011-09-30 15:01:32 -060021#include "server.h"
Jens Axboeb66570d2011-10-01 11:11:35 -060022#include "flist.h"
Jens Axboe3c5f57e2011-10-06 12:37:50 +020023#include "hash.h"
Jens Axboe132159a2011-09-30 15:01:32 -060024
Stephen M. Camerondd366722012-02-24 08:17:30 +010025static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd);
Jens Axboe89e5fad2012-03-05 09:21:12 +010026static void handle_ts(struct fio_client *client, struct fio_net_cmd *cmd);
27static void handle_gs(struct fio_client *client, struct fio_net_cmd *cmd);
Stephen M. Camerondd366722012-02-24 08:17:30 +010028static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd);
Jens Axboe084d1c62012-03-03 20:28:07 +010029static void handle_text(struct fio_client *client, struct fio_net_cmd *cmd);
Jens Axboe6b79c802012-03-08 10:51:36 +010030static void handle_stop(struct fio_client *client, struct fio_net_cmd *cmd);
Jens Axboe85dd01e2012-03-12 14:33:16 +010031static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd);
Stephen M. Camerondd366722012-02-24 08:17:30 +010032
33struct client_ops fio_client_ops = {
Jens Axboe35c0ba72012-03-14 10:56:40 +010034 .text = handle_text,
Jens Axboe0420ba62012-02-29 11:16:52 +010035 .disk_util = handle_du,
36 .thread_status = handle_ts,
37 .group_stats = handle_gs,
Jens Axboe6b79c802012-03-08 10:51:36 +010038 .stop = handle_stop,
Jens Axboe85dd01e2012-03-12 14:33:16 +010039 .start = handle_start,
Jens Axboea5276612012-03-04 15:15:08 +010040 .eta = display_thread_status,
Jens Axboe0420ba62012-02-29 11:16:52 +010041 .probe = handle_probe,
Jens Axboe6433ee02012-03-09 20:10:51 +010042 .eta_msec = FIO_CLIENT_DEF_ETA_MSEC,
Jens Axboe46bcd492012-03-14 11:31:21 +010043 .client_type = FIO_CLIENT_TYPE_CLI,
Stephen M. Camerondd366722012-02-24 08:17:30 +010044};
45
Jens Axboeaf9c9fb2011-10-09 21:54:10 +020046static struct timeval eta_tv;
Jens Axboe48fbb462011-10-09 12:19:08 +020047
Jens Axboeb66570d2011-10-01 11:11:35 -060048static FLIST_HEAD(client_list);
Jens Axboe82c1ed32011-10-10 08:56:18 +020049static FLIST_HEAD(eta_list);
Jens Axboeb66570d2011-10-01 11:11:35 -060050
Jens Axboe3f3a4542011-10-10 21:11:09 +020051static FLIST_HEAD(arg_list);
52
Jens Axboe3650a3c2012-03-05 14:09:03 +010053struct thread_stat client_ts;
54struct group_run_stats client_gs;
55int sum_stat_clients;
56
Jens Axboe37f0c1a2011-10-11 14:08:33 +020057static int sum_stat_nr;
Jens Axboe108fea72012-11-14 13:09:45 -070058static int do_output_all_clients;
Jens Axboe37f0c1a2011-10-11 14:08:33 +020059
Jens Axboe3c5f57e2011-10-06 12:37:50 +020060#define FIO_CLIENT_HASH_BITS 7
61#define FIO_CLIENT_HASH_SZ (1 << FIO_CLIENT_HASH_BITS)
62#define FIO_CLIENT_HASH_MASK (FIO_CLIENT_HASH_SZ - 1)
Jens Axboebebe6392011-10-07 10:00:51 +020063static struct flist_head client_hash[FIO_CLIENT_HASH_SZ];
Jens Axboe3c5f57e2011-10-06 12:37:50 +020064
Jens Axboebebe6392011-10-07 10:00:51 +020065static void fio_client_add_hash(struct fio_client *client)
Jens Axboe3c5f57e2011-10-06 12:37:50 +020066{
67 int bucket = hash_long(client->fd, FIO_CLIENT_HASH_BITS);
68
69 bucket &= FIO_CLIENT_HASH_MASK;
Jens Axboebebe6392011-10-07 10:00:51 +020070 flist_add(&client->hash_list, &client_hash[bucket]);
Jens Axboe3c5f57e2011-10-06 12:37:50 +020071}
72
Jens Axboebebe6392011-10-07 10:00:51 +020073static void fio_client_remove_hash(struct fio_client *client)
Jens Axboe3c5f57e2011-10-06 12:37:50 +020074{
Jens Axboebebe6392011-10-07 10:00:51 +020075 if (!flist_empty(&client->hash_list))
76 flist_del_init(&client->hash_list);
Jens Axboe3c5f57e2011-10-06 12:37:50 +020077}
78
79static void fio_init fio_client_hash_init(void)
80{
81 int i;
82
Jens Axboebebe6392011-10-07 10:00:51 +020083 for (i = 0; i < FIO_CLIENT_HASH_SZ; i++)
84 INIT_FLIST_HEAD(&client_hash[i]);
Jens Axboe3c5f57e2011-10-06 12:37:50 +020085}
86
Jens Axboeb66570d2011-10-01 11:11:35 -060087static struct fio_client *find_client_by_fd(int fd)
88{
Jens Axboe3c5f57e2011-10-06 12:37:50 +020089 int bucket = hash_long(fd, FIO_CLIENT_HASH_BITS) & FIO_CLIENT_HASH_MASK;
Jens Axboeb66570d2011-10-01 11:11:35 -060090 struct fio_client *client;
91 struct flist_head *entry;
92
Jens Axboebebe6392011-10-07 10:00:51 +020093 flist_for_each(entry, &client_hash[bucket]) {
94 client = flist_entry(entry, struct fio_client, hash_list);
Jens Axboeb66570d2011-10-01 11:11:35 -060095
Jens Axboee55f8f32012-03-06 15:42:31 +010096 if (client->fd == fd) {
97 client->refs++;
Jens Axboeb66570d2011-10-01 11:11:35 -060098 return client;
Jens Axboee55f8f32012-03-06 15:42:31 +010099 }
Jens Axboeb66570d2011-10-01 11:11:35 -0600100 }
101
102 return NULL;
103}
104
Jens Axboe3af45202012-03-13 09:59:53 +0100105void fio_put_client(struct fio_client *client)
Jens Axboeb66570d2011-10-01 11:11:35 -0600106{
Jens Axboe5121a9a2012-03-05 13:32:47 +0100107 if (--client->refs)
108 return;
109
Jens Axboeb66570d2011-10-01 11:11:35 -0600110 free(client->hostname);
Jens Axboe81179ee2011-10-04 12:42:06 +0200111 if (client->argv)
112 free(client->argv);
Jens Axboeb5296dd2011-10-07 16:35:56 +0200113 if (client->name)
114 free(client->name);
Jens Axboe14ea90e2012-08-26 17:33:34 +0200115 while (client->nr_ini_file)
116 free(client->ini_file[--client->nr_ini_file]);
117 if (client->ini_file)
118 free(client->ini_file);
Jens Axboe81179ee2011-10-04 12:42:06 +0200119
Jens Axboe108fea72012-11-14 13:09:45 -0700120 if (!client->did_stat)
121 sum_stat_clients -= client->nr_stat;
122
Jens Axboeb66570d2011-10-01 11:11:35 -0600123 free(client);
124}
Jens Axboe132159a2011-09-30 15:01:32 -0600125
Jens Axboe3af45202012-03-13 09:59:53 +0100126static void remove_client(struct fio_client *client)
Jens Axboe5121a9a2012-03-05 13:32:47 +0100127{
Jens Axboe3af45202012-03-13 09:59:53 +0100128 assert(client->refs);
129
130 dprint(FD_NET, "client: removed <%s>\n", client->hostname);
131
132 if (!flist_empty(&client->list))
133 flist_del_init(&client->list);
134
135 fio_client_remove_hash(client);
136
137 if (!flist_empty(&client->eta_list)) {
138 flist_del_init(&client->eta_list);
139 fio_client_dec_jobs_eta(client->eta_in_flight, client->ops->eta);
140 }
141
Jens Axboe1e0c1842012-03-20 15:15:00 +0100142 close(client->fd);
143 client->fd = -1;
144
Jens Axboe0cf3ece2012-03-21 10:15:20 +0100145 if (client->ops->removed)
146 client->ops->removed(client);
147
Jens Axboe3af45202012-03-13 09:59:53 +0100148 nr_clients--;
Jens Axboe3af45202012-03-13 09:59:53 +0100149 fio_put_client(client);
Jens Axboe5121a9a2012-03-05 13:32:47 +0100150}
151
Jens Axboe343cb4a2012-03-09 17:16:51 +0100152struct fio_client *fio_get_client(struct fio_client *client)
153{
154 client->refs++;
155 return client;
156}
157
Jens Axboefa2ea802011-10-08 21:07:29 +0200158static void __fio_client_add_cmd_option(struct fio_client *client,
159 const char *opt)
Jens Axboe81179ee2011-10-04 12:42:06 +0200160{
Jens Axboe39e8e012011-10-04 13:46:08 +0200161 int index;
162
163 index = client->argc++;
Jens Axboe81179ee2011-10-04 12:42:06 +0200164 client->argv = realloc(client->argv, sizeof(char *) * client->argc);
Jens Axboe39e8e012011-10-04 13:46:08 +0200165 client->argv[index] = strdup(opt);
166 dprint(FD_NET, "client: add cmd %d: %s\n", index, opt);
Jens Axboe81179ee2011-10-04 12:42:06 +0200167}
168
Jens Axboefa2ea802011-10-08 21:07:29 +0200169void fio_client_add_cmd_option(void *cookie, const char *opt)
Jens Axboe81179ee2011-10-04 12:42:06 +0200170{
Jens Axboebebe6392011-10-07 10:00:51 +0200171 struct fio_client *client = cookie;
Jens Axboe3f3a4542011-10-10 21:11:09 +0200172 struct flist_head *entry;
Jens Axboe81179ee2011-10-04 12:42:06 +0200173
Jens Axboebebe6392011-10-07 10:00:51 +0200174 if (!client || !opt)
Jens Axboefa2ea802011-10-08 21:07:29 +0200175 return;
Jens Axboe81179ee2011-10-04 12:42:06 +0200176
Jens Axboefa2ea802011-10-08 21:07:29 +0200177 __fio_client_add_cmd_option(client, opt);
Jens Axboe3f3a4542011-10-10 21:11:09 +0200178
179 /*
180 * Duplicate arguments to shared client group
181 */
182 flist_for_each(entry, &arg_list) {
183 client = flist_entry(entry, struct fio_client, arg_list);
184
185 __fio_client_add_cmd_option(client, opt);
186 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200187}
188
Jens Axboea5276612012-03-04 15:15:08 +0100189struct fio_client *fio_client_add_explicit(struct client_ops *ops,
190 const char *hostname, int type,
Jens Axboe3ec62ec2012-03-01 12:01:29 +0100191 int port)
192{
193 struct fio_client *client;
194
195 client = malloc(sizeof(*client));
196 memset(client, 0, sizeof(*client));
197
198 INIT_FLIST_HEAD(&client->list);
199 INIT_FLIST_HEAD(&client->hash_list);
200 INIT_FLIST_HEAD(&client->arg_list);
201 INIT_FLIST_HEAD(&client->eta_list);
202 INIT_FLIST_HEAD(&client->cmd_list);
203
204 client->hostname = strdup(hostname);
205
206 if (type == Fio_client_socket)
207 client->is_sock = 1;
208 else {
209 int ipv6;
210
211 ipv6 = type == Fio_client_ipv6;
212 if (fio_server_parse_host(hostname, &ipv6,
213 &client->addr.sin_addr,
214 &client->addr6.sin6_addr))
215 goto err;
216
217 client->port = port;
218 }
219
220 client->fd = -1;
Jens Axboea5276612012-03-04 15:15:08 +0100221 client->ops = ops;
Jens Axboe5121a9a2012-03-05 13:32:47 +0100222 client->refs = 1;
Jens Axboe46bcd492012-03-14 11:31:21 +0100223 client->type = ops->client_type;
Jens Axboe3ec62ec2012-03-01 12:01:29 +0100224
225 __fio_client_add_cmd_option(client, "fio");
226
227 flist_add(&client->list, &client_list);
228 nr_clients++;
229 dprint(FD_NET, "client: added <%s>\n", client->hostname);
230 return client;
231err:
232 free(client);
233 return NULL;
234}
235
Jens Axboe14ea90e2012-08-26 17:33:34 +0200236void fio_client_add_ini_file(void *cookie, const char *ini_file)
237{
238 struct fio_client *client = cookie;
239 size_t new_size;
240
241 dprint(FD_NET, "client <%s>: add ini %s\n", client->hostname, ini_file);
242
243 new_size = (client->nr_ini_file + 1) * sizeof(char *);
244 client->ini_file = realloc(client->ini_file, new_size);
245 client->ini_file[client->nr_ini_file] = strdup(ini_file);
246 client->nr_ini_file++;
247}
248
Jens Axboea5276612012-03-04 15:15:08 +0100249int fio_client_add(struct client_ops *ops, const char *hostname, void **cookie)
Jens Axboe132159a2011-09-30 15:01:32 -0600250{
Jens Axboe3f3a4542011-10-10 21:11:09 +0200251 struct fio_client *existing = *cookie;
Jens Axboeb66570d2011-10-01 11:11:35 -0600252 struct fio_client *client;
Jens Axboe132159a2011-09-30 15:01:32 -0600253
Jens Axboe3f3a4542011-10-10 21:11:09 +0200254 if (existing) {
255 /*
256 * We always add our "exec" name as the option, hence 1
257 * means empty.
258 */
259 if (existing->argc == 1)
260 flist_add_tail(&existing->arg_list, &arg_list);
261 else {
262 while (!flist_empty(&arg_list))
263 flist_del_init(arg_list.next);
264 }
265 }
266
Jens Axboeb66570d2011-10-01 11:11:35 -0600267 client = malloc(sizeof(*client));
Jens Axboea37f69b2011-10-01 12:24:21 -0600268 memset(client, 0, sizeof(*client));
Jens Axboe81179ee2011-10-04 12:42:06 +0200269
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200270 INIT_FLIST_HEAD(&client->list);
Jens Axboebebe6392011-10-07 10:00:51 +0200271 INIT_FLIST_HEAD(&client->hash_list);
Jens Axboe3f3a4542011-10-10 21:11:09 +0200272 INIT_FLIST_HEAD(&client->arg_list);
Jens Axboe82c1ed32011-10-10 08:56:18 +0200273 INIT_FLIST_HEAD(&client->eta_list);
Jens Axboe89c17072011-10-11 10:15:51 +0200274 INIT_FLIST_HEAD(&client->cmd_list);
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200275
Jens Axboebebe6392011-10-07 10:00:51 +0200276 if (fio_server_parse_string(hostname, &client->hostname,
277 &client->is_sock, &client->port,
Jens Axboe811826b2011-10-24 09:11:50 +0200278 &client->addr.sin_addr,
279 &client->addr6.sin6_addr,
280 &client->ipv6))
Jens Axboebebe6392011-10-07 10:00:51 +0200281 return -1;
282
Jens Axboea37f69b2011-10-01 12:24:21 -0600283 client->fd = -1;
Jens Axboea5276612012-03-04 15:15:08 +0100284 client->ops = ops;
Jens Axboe5121a9a2012-03-05 13:32:47 +0100285 client->refs = 1;
Jens Axboe46bcd492012-03-14 11:31:21 +0100286 client->type = ops->client_type;
Jens Axboe81179ee2011-10-04 12:42:06 +0200287
288 __fio_client_add_cmd_option(client, "fio");
289
Jens Axboea37f69b2011-10-01 12:24:21 -0600290 flist_add(&client->list, &client_list);
291 nr_clients++;
Jens Axboebebe6392011-10-07 10:00:51 +0200292 dprint(FD_NET, "client: added <%s>\n", client->hostname);
293 *cookie = client;
294 return 0;
Jens Axboea37f69b2011-10-01 12:24:21 -0600295}
296
Jens Axboed824c3b2012-03-09 17:45:43 +0100297static void probe_client(struct fio_client *client)
298{
299 dprint(FD_NET, "client: send probe\n");
300
301 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_PROBE, 0, &client->cmd_list);
302}
303
Jens Axboe87aa8f12011-10-06 21:24:13 +0200304static int fio_client_connect_ip(struct fio_client *client)
Jens Axboea37f69b2011-10-01 12:24:21 -0600305{
Jens Axboe811826b2011-10-24 09:11:50 +0200306 struct sockaddr *addr;
Jens Axboe67bf9822013-01-10 11:23:19 +0100307 socklen_t socklen;
Jens Axboe811826b2011-10-24 09:11:50 +0200308 int fd, domain;
Jens Axboe132159a2011-09-30 15:01:32 -0600309
Jens Axboe811826b2011-10-24 09:11:50 +0200310 if (client->ipv6) {
311 client->addr6.sin6_family = AF_INET6;
312 client->addr6.sin6_port = htons(client->port);
313 domain = AF_INET6;
314 addr = (struct sockaddr *) &client->addr6;
315 socklen = sizeof(client->addr6);
316 } else {
317 client->addr.sin_family = AF_INET;
318 client->addr.sin_port = htons(client->port);
319 domain = AF_INET;
320 addr = (struct sockaddr *) &client->addr;
321 socklen = sizeof(client->addr);
322 }
Jens Axboe132159a2011-09-30 15:01:32 -0600323
Jens Axboe811826b2011-10-24 09:11:50 +0200324 fd = socket(domain, SOCK_STREAM, 0);
Jens Axboe132159a2011-09-30 15:01:32 -0600325 if (fd < 0) {
Jens Axboe25095e12012-03-09 17:09:55 +0100326 int ret = -errno;
327
Jens Axboe132159a2011-09-30 15:01:32 -0600328 log_err("fio: socket: %s\n", strerror(errno));
Jens Axboe25095e12012-03-09 17:09:55 +0100329 return ret;
Jens Axboe132159a2011-09-30 15:01:32 -0600330 }
331
Jens Axboe811826b2011-10-24 09:11:50 +0200332 if (connect(fd, addr, socklen) < 0) {
Jens Axboe25095e12012-03-09 17:09:55 +0100333 int ret = -errno;
334
Jens Axboe132159a2011-09-30 15:01:32 -0600335 log_err("fio: connect: %s\n", strerror(errno));
Jens Axboea7de0a12011-10-07 12:55:14 +0200336 log_err("fio: failed to connect to %s:%u\n", client->hostname,
337 client->port);
Jens Axboeb94cba42011-10-06 21:27:10 +0200338 close(fd);
Jens Axboe25095e12012-03-09 17:09:55 +0100339 return ret;
Jens Axboe132159a2011-09-30 15:01:32 -0600340 }
341
Jens Axboe87aa8f12011-10-06 21:24:13 +0200342 return fd;
343}
344
345static int fio_client_connect_sock(struct fio_client *client)
346{
347 struct sockaddr_un *addr = &client->addr_un;
Jens Axboe67bf9822013-01-10 11:23:19 +0100348 socklen_t len;
Jens Axboe87aa8f12011-10-06 21:24:13 +0200349 int fd;
350
351 memset(addr, 0, sizeof(*addr));
352 addr->sun_family = AF_UNIX;
353 strcpy(addr->sun_path, client->hostname);
354
355 fd = socket(AF_UNIX, SOCK_STREAM, 0);
356 if (fd < 0) {
Jens Axboe25095e12012-03-09 17:09:55 +0100357 int ret = -errno;
358
Jens Axboe87aa8f12011-10-06 21:24:13 +0200359 log_err("fio: socket: %s\n", strerror(errno));
Jens Axboe25095e12012-03-09 17:09:55 +0100360 return ret;
Jens Axboe87aa8f12011-10-06 21:24:13 +0200361 }
362
363 len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
364 if (connect(fd, (struct sockaddr *) addr, len) < 0) {
Jens Axboe25095e12012-03-09 17:09:55 +0100365 int ret = -errno;
366
Jens Axboe87aa8f12011-10-06 21:24:13 +0200367 log_err("fio: connect; %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200368 close(fd);
Jens Axboe25095e12012-03-09 17:09:55 +0100369 return ret;
Jens Axboe87aa8f12011-10-06 21:24:13 +0200370 }
371
372 return fd;
373}
374
Jens Axboe2f99deb2012-03-09 14:37:29 +0100375int fio_client_connect(struct fio_client *client)
Jens Axboe87aa8f12011-10-06 21:24:13 +0200376{
377 int fd;
378
379 dprint(FD_NET, "client: connect to host %s\n", client->hostname);
380
Jens Axboe87aa8f12011-10-06 21:24:13 +0200381 if (client->is_sock)
382 fd = fio_client_connect_sock(client);
383 else
384 fd = fio_client_connect_ip(client);
385
Jens Axboe89c17072011-10-11 10:15:51 +0200386 dprint(FD_NET, "client: %s connected %d\n", client->hostname, fd);
387
Jens Axboe87aa8f12011-10-06 21:24:13 +0200388 if (fd < 0)
Jens Axboea1a3ed52012-03-09 17:00:01 +0100389 return fd;
Jens Axboe87aa8f12011-10-06 21:24:13 +0200390
Jens Axboeb66570d2011-10-01 11:11:35 -0600391 client->fd = fd;
Jens Axboebebe6392011-10-07 10:00:51 +0200392 fio_client_add_hash(client);
Jens Axboe81179ee2011-10-04 12:42:06 +0200393 client->state = Client_connected;
Jens Axboed824c3b2012-03-09 17:45:43 +0100394
395 probe_client(client);
Jens Axboe132159a2011-09-30 15:01:32 -0600396 return 0;
397}
398
Jens Axboe0cf3ece2012-03-21 10:15:20 +0100399int fio_client_terminate(struct fio_client *client)
Jens Axboe2f99deb2012-03-09 14:37:29 +0100400{
Jens Axboe0cf3ece2012-03-21 10:15:20 +0100401 return fio_net_send_quit(client->fd);
Jens Axboe2f99deb2012-03-09 14:37:29 +0100402}
403
Jens Axboecc0df002011-10-03 20:53:32 +0200404void fio_clients_terminate(void)
405{
406 struct flist_head *entry;
407 struct fio_client *client;
408
Jens Axboe60efd142011-10-04 13:30:11 +0200409 dprint(FD_NET, "client: terminate clients\n");
410
Jens Axboecc0df002011-10-03 20:53:32 +0200411 flist_for_each(entry, &client_list) {
412 client = flist_entry(entry, struct fio_client, list);
Jens Axboe2f99deb2012-03-09 14:37:29 +0100413 fio_client_terminate(client);
Jens Axboecc0df002011-10-03 20:53:32 +0200414 }
415}
416
417static void sig_int(int sig)
418{
Jens Axboebebe6392011-10-07 10:00:51 +0200419 dprint(FD_NET, "client: got signal %d\n", sig);
Jens Axboecc0df002011-10-03 20:53:32 +0200420 fio_clients_terminate();
421}
422
Jens Axboeb852e7c2012-03-30 10:30:35 +0200423static void sig_show_status(int sig)
424{
425 show_running_run_stats();
426}
427
Jens Axboecc0df002011-10-03 20:53:32 +0200428static void client_signal_handler(void)
429{
430 struct sigaction act;
431
432 memset(&act, 0, sizeof(act));
433 act.sa_handler = sig_int;
434 act.sa_flags = SA_RESTART;
435 sigaction(SIGINT, &act, NULL);
436
437 memset(&act, 0, sizeof(act));
438 act.sa_handler = sig_int;
439 act.sa_flags = SA_RESTART;
440 sigaction(SIGTERM, &act, NULL);
Jens Axboeb852e7c2012-03-30 10:30:35 +0200441
Bruce Cran2f694502012-10-10 16:34:13 +0100442/* Windows uses SIGBREAK as a quit signal from other applications */
443#ifdef WIN32
444 memset(&act, 0, sizeof(act));
445 act.sa_handler = sig_int;
446 act.sa_flags = SA_RESTART;
447 sigaction(SIGBREAK, &act, NULL);
448#endif
449
Jens Axboeb852e7c2012-03-30 10:30:35 +0200450 memset(&act, 0, sizeof(act));
451 act.sa_handler = sig_show_status;
452 act.sa_flags = SA_RESTART;
453 sigaction(SIGUSR1, &act, NULL);
Jens Axboecc0df002011-10-03 20:53:32 +0200454}
455
Jens Axboe81179ee2011-10-04 12:42:06 +0200456static int send_client_cmd_line(struct fio_client *client)
457{
Jens Axboefa2ea802011-10-08 21:07:29 +0200458 struct cmd_single_line_pdu *cslp;
459 struct cmd_line_pdu *clp;
460 unsigned long offset;
Jens Axboe7f868312011-10-10 09:55:21 +0200461 unsigned int *lens;
Jens Axboefa2ea802011-10-08 21:07:29 +0200462 void *pdu;
463 size_t mem;
Jens Axboe81179ee2011-10-04 12:42:06 +0200464 int i, ret;
465
Jens Axboe39e8e012011-10-04 13:46:08 +0200466 dprint(FD_NET, "client: send cmdline %d\n", client->argc);
Jens Axboe60efd142011-10-04 13:30:11 +0200467
Jens Axboe7f868312011-10-10 09:55:21 +0200468 lens = malloc(client->argc * sizeof(unsigned int));
469
Jens Axboefa2ea802011-10-08 21:07:29 +0200470 /*
471 * Find out how much mem we need
472 */
Jens Axboe7f868312011-10-10 09:55:21 +0200473 for (i = 0, mem = 0; i < client->argc; i++) {
474 lens[i] = strlen(client->argv[i]) + 1;
475 mem += lens[i];
476 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200477
Jens Axboefa2ea802011-10-08 21:07:29 +0200478 /*
479 * We need one cmd_line_pdu, and argc number of cmd_single_line_pdu
480 */
481 mem += sizeof(*clp) + (client->argc * sizeof(*cslp));
482
483 pdu = malloc(mem);
484 clp = pdu;
485 offset = sizeof(*clp);
486
487 for (i = 0; i < client->argc; i++) {
Jens Axboe7f868312011-10-10 09:55:21 +0200488 uint16_t arg_len = lens[i];
Jens Axboefa2ea802011-10-08 21:07:29 +0200489
490 cslp = pdu + offset;
491 strcpy((char *) cslp->text, client->argv[i]);
492 cslp->len = cpu_to_le16(arg_len);
493 offset += sizeof(*cslp) + arg_len;
494 }
495
Jens Axboe7f868312011-10-10 09:55:21 +0200496 free(lens);
Jens Axboefa2ea802011-10-08 21:07:29 +0200497 clp->lines = cpu_to_le16(client->argc);
Jens Axboe46bcd492012-03-14 11:31:21 +0100498 clp->client_type = __cpu_to_le16(client->type);
Jens Axboe40c60512012-03-27 16:03:04 +0200499 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOBLINE, pdu, mem, NULL, NULL);
Jens Axboe81179ee2011-10-04 12:42:06 +0200500 free(pdu);
501 return ret;
502}
503
Jens Axboea37f69b2011-10-01 12:24:21 -0600504int fio_clients_connect(void)
505{
506 struct fio_client *client;
507 struct flist_head *entry, *tmp;
508 int ret;
509
Bruce Cran93bcfd22012-02-20 20:18:19 +0100510#ifdef WIN32
511 WSADATA wsd;
Jens Axboe3c3ed072012-03-27 09:12:39 +0200512 WSAStartup(MAKEWORD(2, 2), &wsd);
Bruce Cran93bcfd22012-02-20 20:18:19 +0100513#endif
514
Jens Axboe60efd142011-10-04 13:30:11 +0200515 dprint(FD_NET, "client: connect all\n");
516
Jens Axboecc0df002011-10-03 20:53:32 +0200517 client_signal_handler();
518
Jens Axboea37f69b2011-10-01 12:24:21 -0600519 flist_for_each_safe(entry, tmp, &client_list) {
520 client = flist_entry(entry, struct fio_client, list);
521
522 ret = fio_client_connect(client);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200523 if (ret) {
Jens Axboea37f69b2011-10-01 12:24:21 -0600524 remove_client(client);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200525 continue;
526 }
527
Jens Axboe81179ee2011-10-04 12:42:06 +0200528 if (client->argc > 1)
529 send_client_cmd_line(client);
Jens Axboea37f69b2011-10-01 12:24:21 -0600530 }
531
532 return !nr_clients;
533}
534
Jens Axboeb9d2f302012-03-08 20:36:28 +0100535int fio_start_client(struct fio_client *client)
536{
537 dprint(FD_NET, "client: start %s\n", client->hostname);
538 return fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_RUN, 0, NULL);
539}
540
541int fio_start_all_clients(void)
542{
543 struct fio_client *client;
544 struct flist_head *entry, *tmp;
545 int ret;
546
547 dprint(FD_NET, "client: start all\n");
548
549 flist_for_each_safe(entry, tmp, &client_list) {
550 client = flist_entry(entry, struct fio_client, list);
551
552 ret = fio_start_client(client);
553 if (ret) {
554 remove_client(client);
555 continue;
556 }
557 }
558
559 return flist_empty(&client_list);
560}
561
Jens Axboe132159a2011-09-30 15:01:32 -0600562/*
563 * Send file contents to server backend. We could use sendfile(), but to remain
564 * more portable lets just read/write the darn thing.
565 */
Jens Axboe9988ca72012-03-09 15:14:06 +0100566static int __fio_client_send_ini(struct fio_client *client, const char *filename)
Jens Axboe132159a2011-09-30 15:01:32 -0600567{
Jens Axboe46bcd492012-03-14 11:31:21 +0100568 struct cmd_job_pdu *pdu;
569 size_t p_size;
Jens Axboe132159a2011-09-30 15:01:32 -0600570 struct stat sb;
Jens Axboe46bcd492012-03-14 11:31:21 +0100571 char *p;
572 void *buf;
Jens Axboe132159a2011-09-30 15:01:32 -0600573 off_t len;
574 int fd, ret;
575
Jens Axboe46c48f12011-10-01 12:50:51 -0600576 dprint(FD_NET, "send ini %s to %s\n", filename, client->hostname);
577
Jens Axboe132159a2011-09-30 15:01:32 -0600578 fd = open(filename, O_RDONLY);
579 if (fd < 0) {
Jens Axboe25095e12012-03-09 17:09:55 +0100580 int ret = -errno;
581
Jens Axboee951bdc2011-10-05 21:58:45 +0200582 log_err("fio: job file <%s> open: %s\n", filename, strerror(errno));
Jens Axboe25095e12012-03-09 17:09:55 +0100583 return ret;
Jens Axboe132159a2011-09-30 15:01:32 -0600584 }
585
586 if (fstat(fd, &sb) < 0) {
Jens Axboe25095e12012-03-09 17:09:55 +0100587 int ret = -errno;
588
Jens Axboe132159a2011-09-30 15:01:32 -0600589 log_err("fio: job file stat: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200590 close(fd);
Jens Axboe25095e12012-03-09 17:09:55 +0100591 return ret;
Jens Axboe132159a2011-09-30 15:01:32 -0600592 }
593
Jens Axboe46bcd492012-03-14 11:31:21 +0100594 p_size = sb.st_size + sizeof(*pdu);
595 pdu = malloc(p_size);
596 buf = pdu->buf;
Jens Axboe132159a2011-09-30 15:01:32 -0600597
598 len = sb.st_size;
599 p = buf;
600 do {
601 ret = read(fd, p, len);
602 if (ret > 0) {
603 len -= ret;
604 if (!len)
605 break;
606 p += ret;
607 continue;
608 } else if (!ret)
609 break;
610 else if (errno == EAGAIN || errno == EINTR)
611 continue;
612 } while (1);
613
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200614 if (len) {
615 log_err("fio: failed reading job file %s\n", filename);
Jens Axboeb94cba42011-10-06 21:27:10 +0200616 close(fd);
Jens Axboec524ef72011-10-07 12:23:34 +0200617 free(buf);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200618 return 1;
619 }
620
Jens Axboe46bcd492012-03-14 11:31:21 +0100621 pdu->buf_len = __cpu_to_le32(sb.st_size);
622 pdu->client_type = cpu_to_le32(client->type);
623
Jens Axboec2cb6862012-02-23 20:56:12 +0100624 client->sent_job = 1;
Jens Axboe40c60512012-03-27 16:03:04 +0200625 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, pdu, p_size, NULL, NULL);
Jens Axboe46bcd492012-03-14 11:31:21 +0100626 free(pdu);
Jens Axboeb94cba42011-10-06 21:27:10 +0200627 close(fd);
Jens Axboe132159a2011-09-30 15:01:32 -0600628 return ret;
629}
Jens Axboe37db14f2011-09-30 17:00:42 -0600630
Jens Axboe9988ca72012-03-09 15:14:06 +0100631int fio_client_send_ini(struct fio_client *client, const char *filename)
632{
Jens Axboe25095e12012-03-09 17:09:55 +0100633 int ret;
634
635 ret = __fio_client_send_ini(client, filename);
Jens Axboe3af45202012-03-13 09:59:53 +0100636 if (!ret)
637 client->sent_job = 1;
Jens Axboe9988ca72012-03-09 15:14:06 +0100638
Jens Axboe25095e12012-03-09 17:09:55 +0100639 return ret;
Jens Axboe9988ca72012-03-09 15:14:06 +0100640}
641
Jens Axboea37f69b2011-10-01 12:24:21 -0600642int fio_clients_send_ini(const char *filename)
643{
644 struct fio_client *client;
645 struct flist_head *entry, *tmp;
646
647 flist_for_each_safe(entry, tmp, &client_list) {
648 client = flist_entry(entry, struct fio_client, list);
649
Jens Axboe14ea90e2012-08-26 17:33:34 +0200650 if (client->nr_ini_file) {
651 int i;
652
653 for (i = 0; i < client->nr_ini_file; i++) {
654 const char *ini = client->ini_file[i];
655
656 if (fio_client_send_ini(client, ini)) {
657 remove_client(client);
658 break;
659 }
660 }
661 } else if (!filename || fio_client_send_ini(client, filename))
Jens Axboe3af45202012-03-13 09:59:53 +0100662 remove_client(client);
Jens Axboea37f69b2011-10-01 12:24:21 -0600663 }
664
665 return !nr_clients;
666}
667
Jens Axboe40c60512012-03-27 16:03:04 +0200668int fio_client_update_options(struct fio_client *client,
669 struct thread_options *o, uint64_t *tag)
670{
671 struct cmd_add_job_pdu pdu;
672
673 pdu.thread_number = cpu_to_le32(client->thread_number);
674 pdu.groupid = cpu_to_le32(client->groupid);
675 convert_thread_options_to_net(&pdu.top, o);
676
677 return fio_net_send_cmd(client->fd, FIO_NET_CMD_UPDATE_JOB, &pdu, sizeof(pdu), tag, &client->cmd_list);
678}
679
Jens Axboea64e88d2011-10-03 14:20:01 +0200680static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
681{
682 dst->max_val = le64_to_cpu(src->max_val);
683 dst->min_val = le64_to_cpu(src->min_val);
684 dst->samples = le64_to_cpu(src->samples);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200685
686 /*
687 * Floats arrive as IEEE 754 encoded uint64_t, convert back to double
688 */
689 dst->mean.u.f = fio_uint64_to_double(le64_to_cpu(dst->mean.u.i));
690 dst->S.u.f = fio_uint64_to_double(le64_to_cpu(dst->S.u.i));
Jens Axboea64e88d2011-10-03 14:20:01 +0200691}
692
693static void convert_ts(struct thread_stat *dst, struct thread_stat *src)
694{
695 int i, j;
696
Jens Axboe2f122b12012-03-15 13:10:19 +0100697 dst->error = le32_to_cpu(src->error);
698 dst->thread_number = le32_to_cpu(src->thread_number);
699 dst->groupid = le32_to_cpu(src->groupid);
700 dst->pid = le32_to_cpu(src->pid);
701 dst->members = le32_to_cpu(src->members);
Jens Axboe771e58b2013-01-30 12:56:23 +0100702 dst->unified_rw_rep = le32_to_cpu(src->unified_rw_rep);
Jens Axboea64e88d2011-10-03 14:20:01 +0200703
Jens Axboe298921d2012-09-24 18:47:01 +0200704 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
Jens Axboea64e88d2011-10-03 14:20:01 +0200705 convert_io_stat(&dst->clat_stat[i], &src->clat_stat[i]);
706 convert_io_stat(&dst->slat_stat[i], &src->slat_stat[i]);
707 convert_io_stat(&dst->lat_stat[i], &src->lat_stat[i]);
708 convert_io_stat(&dst->bw_stat[i], &src->bw_stat[i]);
709 }
710
711 dst->usr_time = le64_to_cpu(src->usr_time);
712 dst->sys_time = le64_to_cpu(src->sys_time);
713 dst->ctx = le64_to_cpu(src->ctx);
714 dst->minf = le64_to_cpu(src->minf);
715 dst->majf = le64_to_cpu(src->majf);
716 dst->clat_percentiles = le64_to_cpu(src->clat_percentiles);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200717
718 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
719 fio_fp64_t *fps = &src->percentile_list[i];
720 fio_fp64_t *fpd = &dst->percentile_list[i];
721
722 fpd->u.f = fio_uint64_to_double(le64_to_cpu(fps->u.i));
723 }
Jens Axboea64e88d2011-10-03 14:20:01 +0200724
725 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
726 dst->io_u_map[i] = le32_to_cpu(src->io_u_map[i]);
727 dst->io_u_submit[i] = le32_to_cpu(src->io_u_submit[i]);
728 dst->io_u_complete[i] = le32_to_cpu(src->io_u_complete[i]);
729 }
730
731 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
732 dst->io_u_lat_u[i] = le32_to_cpu(src->io_u_lat_u[i]);
733 dst->io_u_lat_m[i] = le32_to_cpu(src->io_u_lat_m[i]);
734 }
735
Jens Axboe298921d2012-09-24 18:47:01 +0200736 for (i = 0; i < DDIR_RWDIR_CNT; i++)
Jens Axboea64e88d2011-10-03 14:20:01 +0200737 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
738 dst->io_u_plat[i][j] = le32_to_cpu(src->io_u_plat[i][j]);
739
Jens Axboe78799de2013-01-29 20:53:52 +0100740 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
Jens Axboea64e88d2011-10-03 14:20:01 +0200741 dst->total_io_u[i] = le64_to_cpu(src->total_io_u[i]);
Jens Axboe93eee042011-10-03 21:08:48 +0200742 dst->short_io_u[i] = le64_to_cpu(src->short_io_u[i]);
Jens Axboea64e88d2011-10-03 14:20:01 +0200743 }
744
745 dst->total_submit = le64_to_cpu(src->total_submit);
746 dst->total_complete = le64_to_cpu(src->total_complete);
747
Jens Axboe298921d2012-09-24 18:47:01 +0200748 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
Jens Axboea64e88d2011-10-03 14:20:01 +0200749 dst->io_bytes[i] = le64_to_cpu(src->io_bytes[i]);
750 dst->runtime[i] = le64_to_cpu(src->runtime[i]);
751 }
752
753 dst->total_run_time = le64_to_cpu(src->total_run_time);
754 dst->continue_on_error = le16_to_cpu(src->continue_on_error);
755 dst->total_err_count = le64_to_cpu(src->total_err_count);
Jens Axboeddcc0b62011-10-03 14:45:27 +0200756 dst->first_error = le32_to_cpu(src->first_error);
757 dst->kb_base = le32_to_cpu(src->kb_base);
Steven Noonanad705bc2013-04-08 15:05:25 -0700758 dst->unit_base = le32_to_cpu(src->unit_base);
Jens Axboea64e88d2011-10-03 14:20:01 +0200759}
760
761static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
762{
763 int i;
764
Jens Axboe298921d2012-09-24 18:47:01 +0200765 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
Jens Axboea64e88d2011-10-03 14:20:01 +0200766 dst->max_run[i] = le64_to_cpu(src->max_run[i]);
767 dst->min_run[i] = le64_to_cpu(src->min_run[i]);
768 dst->max_bw[i] = le64_to_cpu(src->max_bw[i]);
769 dst->min_bw[i] = le64_to_cpu(src->min_bw[i]);
770 dst->io_kb[i] = le64_to_cpu(src->io_kb[i]);
771 dst->agg[i] = le64_to_cpu(src->agg[i]);
772 }
773
774 dst->kb_base = le32_to_cpu(src->kb_base);
Steven Noonanad705bc2013-04-08 15:05:25 -0700775 dst->unit_base = le32_to_cpu(src->unit_base);
Jens Axboea64e88d2011-10-03 14:20:01 +0200776 dst->groupid = le32_to_cpu(src->groupid);
Jens Axboe771e58b2013-01-30 12:56:23 +0100777 dst->unified_rw_rep = le32_to_cpu(src->unified_rw_rep);
Jens Axboea64e88d2011-10-03 14:20:01 +0200778}
779
Jens Axboe89e5fad2012-03-05 09:21:12 +0100780static void handle_ts(struct fio_client *client, struct fio_net_cmd *cmd)
Jens Axboea64e88d2011-10-03 14:20:01 +0200781{
782 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
783
Jens Axboea64e88d2011-10-03 14:20:01 +0200784 show_thread_status(&p->ts, &p->rs);
Jens Axboe108fea72012-11-14 13:09:45 -0700785 client->did_stat = 1;
Jens Axboe37f0c1a2011-10-11 14:08:33 +0200786
Jens Axboe108fea72012-11-14 13:09:45 -0700787 if (!do_output_all_clients)
Jens Axboe37f0c1a2011-10-11 14:08:33 +0200788 return;
789
790 sum_thread_stats(&client_ts, &p->ts, sum_stat_nr);
791 sum_group_stats(&client_gs, &p->rs);
792
793 client_ts.members++;
Jens Axboe2f122b12012-03-15 13:10:19 +0100794 client_ts.thread_number = p->ts.thread_number;
Jens Axboe37f0c1a2011-10-11 14:08:33 +0200795 client_ts.groupid = p->ts.groupid;
Jens Axboe771e58b2013-01-30 12:56:23 +0100796 client_ts.unified_rw_rep = p->ts.unified_rw_rep;
Jens Axboe37f0c1a2011-10-11 14:08:33 +0200797
798 if (++sum_stat_nr == sum_stat_clients) {
799 strcpy(client_ts.name, "All clients");
800 show_thread_status(&client_ts, &client_gs);
801 }
Jens Axboea64e88d2011-10-03 14:20:01 +0200802}
803
Jens Axboe89e5fad2012-03-05 09:21:12 +0100804static void handle_gs(struct fio_client *client, struct fio_net_cmd *cmd)
Jens Axboea64e88d2011-10-03 14:20:01 +0200805{
806 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
807
Jens Axboea64e88d2011-10-03 14:20:01 +0200808 show_group_stats(gs);
809}
810
Jens Axboe3bf236c2012-03-03 20:35:50 +0100811static void handle_text(struct fio_client *client, struct fio_net_cmd *cmd)
812{
813 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
814 const char *buf = (const char *) pdu->buf;
815 const char *name;
816 int fio_unused ret;
817
818 name = client->name ? client->name : client->hostname;
819
820 if (!client->skip_newline)
821 fprintf(f_out, "<%s> ", name);
822 ret = fwrite(buf, pdu->buf_len, 1, f_out);
823 fflush(f_out);
824 client->skip_newline = strchr(buf, '\n') == NULL;
825}
826
Jens Axboed09a64a2011-10-13 11:38:56 +0200827static void convert_agg(struct disk_util_agg *agg)
828{
829 int i;
830
831 for (i = 0; i < 2; i++) {
832 agg->ios[i] = le32_to_cpu(agg->ios[i]);
833 agg->merges[i] = le32_to_cpu(agg->merges[i]);
834 agg->sectors[i] = le64_to_cpu(agg->sectors[i]);
835 agg->ticks[i] = le32_to_cpu(agg->ticks[i]);
836 }
837
838 agg->io_ticks = le32_to_cpu(agg->io_ticks);
839 agg->time_in_queue = le32_to_cpu(agg->time_in_queue);
840 agg->slavecount = le32_to_cpu(agg->slavecount);
Anton Blanchard823ba542011-11-07 14:16:26 +0100841 agg->max_util.u.f = fio_uint64_to_double(__le64_to_cpu(agg->max_util.u.i));
Jens Axboed09a64a2011-10-13 11:38:56 +0200842}
843
844static void convert_dus(struct disk_util_stat *dus)
845{
846 int i;
847
848 for (i = 0; i < 2; i++) {
849 dus->ios[i] = le32_to_cpu(dus->ios[i]);
850 dus->merges[i] = le32_to_cpu(dus->merges[i]);
851 dus->sectors[i] = le64_to_cpu(dus->sectors[i]);
852 dus->ticks[i] = le32_to_cpu(dus->ticks[i]);
853 }
854
855 dus->io_ticks = le32_to_cpu(dus->io_ticks);
856 dus->time_in_queue = le32_to_cpu(dus->time_in_queue);
857 dus->msec = le64_to_cpu(dus->msec);
858}
859
860static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd)
861{
862 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
863
Jens Axboed09a64a2011-10-13 11:38:56 +0200864 if (!client->disk_stats_shown) {
865 client->disk_stats_shown = 1;
866 log_info("\nDisk stats (read/write):\n");
867 }
868
Jens Axboef3afa572012-09-17 13:34:16 +0200869 print_disk_util(&du->dus, &du->agg, output_format == FIO_OUTPUT_TERSE);
Jens Axboed09a64a2011-10-13 11:38:56 +0200870}
871
Jens Axboe3bf236c2012-03-03 20:35:50 +0100872static void convert_jobs_eta(struct jobs_eta *je)
Jens Axboecf451d12011-10-03 16:48:30 +0200873{
Jens Axboecf451d12011-10-03 16:48:30 +0200874 int i;
875
876 je->nr_running = le32_to_cpu(je->nr_running);
877 je->nr_ramp = le32_to_cpu(je->nr_ramp);
878 je->nr_pending = le32_to_cpu(je->nr_pending);
879 je->files_open = le32_to_cpu(je->files_open);
Jens Axboecf451d12011-10-03 16:48:30 +0200880
Jens Axboe298921d2012-09-24 18:47:01 +0200881 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
882 je->m_rate[i] = le32_to_cpu(je->m_rate[i]);
883 je->t_rate[i] = le32_to_cpu(je->t_rate[i]);
884 je->m_iops[i] = le32_to_cpu(je->m_iops[i]);
885 je->t_iops[i] = le32_to_cpu(je->t_iops[i]);
Jens Axboecf451d12011-10-03 16:48:30 +0200886 }
887
Jens Axboeb51eedb2011-10-09 12:13:39 +0200888 je->elapsed_sec = le64_to_cpu(je->elapsed_sec);
Jens Axboecf451d12011-10-03 16:48:30 +0200889 je->eta_sec = le64_to_cpu(je->eta_sec);
Jens Axboe8c621fb2012-03-08 12:30:48 +0100890 je->nr_threads = le32_to_cpu(je->nr_threads);
Jens Axboeb7f05eb2012-05-11 20:33:02 +0200891 je->is_pow2 = le32_to_cpu(je->is_pow2);
Steven Noonanad705bc2013-04-08 15:05:25 -0700892 je->unit_base = le32_to_cpu(je->unit_base);
Jens Axboe48fbb462011-10-09 12:19:08 +0200893}
Jens Axboecf451d12011-10-03 16:48:30 +0200894
Jens Axboe3e47bd22012-02-29 13:45:02 +0100895void fio_client_sum_jobs_eta(struct jobs_eta *dst, struct jobs_eta *je)
Jens Axboe48fbb462011-10-09 12:19:08 +0200896{
Jens Axboe48fbb462011-10-09 12:19:08 +0200897 int i;
898
899 dst->nr_running += je->nr_running;
900 dst->nr_ramp += je->nr_ramp;
901 dst->nr_pending += je->nr_pending;
902 dst->files_open += je->files_open;
Jens Axboe48fbb462011-10-09 12:19:08 +0200903
Jens Axboe298921d2012-09-24 18:47:01 +0200904 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
Jens Axboe3e47bd22012-02-29 13:45:02 +0100905 dst->m_rate[i] += je->m_rate[i];
906 dst->t_rate[i] += je->t_rate[i];
907 dst->m_iops[i] += je->m_iops[i];
908 dst->t_iops[i] += je->t_iops[i];
Jens Axboe48fbb462011-10-09 12:19:08 +0200909 }
910
911 dst->elapsed_sec += je->elapsed_sec;
912
913 if (je->eta_sec > dst->eta_sec)
914 dst->eta_sec = je->eta_sec;
Jens Axboe8c621fb2012-03-08 12:30:48 +0100915
916 dst->nr_threads += je->nr_threads;
917 /* we need to handle je->run_str too ... */
Jens Axboe48fbb462011-10-09 12:19:08 +0200918}
919
Jens Axboea5276612012-03-04 15:15:08 +0100920void fio_client_dec_jobs_eta(struct client_eta *eta, client_eta_op eta_fn)
Jens Axboe82c1ed32011-10-10 08:56:18 +0200921{
922 if (!--eta->pending) {
Jens Axboea5276612012-03-04 15:15:08 +0100923 eta_fn(&eta->eta);
Jens Axboe82c1ed32011-10-10 08:56:18 +0200924 free(eta);
925 }
926}
927
Jens Axboe89c17072011-10-11 10:15:51 +0200928static void remove_reply_cmd(struct fio_client *client, struct fio_net_cmd *cmd)
929{
Jens Axboe40c60512012-03-27 16:03:04 +0200930 struct fio_net_cmd_reply *reply = NULL;
Jens Axboe89c17072011-10-11 10:15:51 +0200931 struct flist_head *entry;
932
933 flist_for_each(entry, &client->cmd_list) {
Jens Axboe40c60512012-03-27 16:03:04 +0200934 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
Jens Axboe89c17072011-10-11 10:15:51 +0200935
Jens Axboe40c60512012-03-27 16:03:04 +0200936 if (cmd->tag == (uintptr_t) reply)
Jens Axboe89c17072011-10-11 10:15:51 +0200937 break;
938
Jens Axboe40c60512012-03-27 16:03:04 +0200939 reply = NULL;
Jens Axboe89c17072011-10-11 10:15:51 +0200940 }
941
Jens Axboe40c60512012-03-27 16:03:04 +0200942 if (!reply) {
943 log_err("fio: client: unable to find matching tag (%lx)\n", cmd->tag);
Jens Axboe89c17072011-10-11 10:15:51 +0200944 return;
945 }
946
Jens Axboe40c60512012-03-27 16:03:04 +0200947 flist_del(&reply->list);
948 cmd->tag = reply->saved_tag;
949 free(reply);
950}
951
952int fio_client_wait_for_reply(struct fio_client *client, uint64_t tag)
953{
954 do {
955 struct fio_net_cmd_reply *reply = NULL;
956 struct flist_head *entry;
957
958 flist_for_each(entry, &client->cmd_list) {
959 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
960
961 if (tag == (uintptr_t) reply)
962 break;
963
964 reply = NULL;
965 }
966
967 if (!reply)
968 break;
969
970 usleep(1000);
971 } while (1);
972
973 return 0;
Jens Axboe89c17072011-10-11 10:15:51 +0200974}
975
Jens Axboe82c1ed32011-10-10 08:56:18 +0200976static void handle_eta(struct fio_client *client, struct fio_net_cmd *cmd)
Jens Axboe48fbb462011-10-09 12:19:08 +0200977{
978 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
Jens Axboedf380932011-10-11 14:25:08 +0200979 struct client_eta *eta = (struct client_eta *) (uintptr_t) cmd->tag;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200980
981 dprint(FD_NET, "client: got eta tag %p, %d\n", eta, eta->pending);
Jens Axboe48fbb462011-10-09 12:19:08 +0200982
Jens Axboef77d2672011-10-10 14:36:07 +0200983 assert(client->eta_in_flight == eta);
984
985 client->eta_in_flight = NULL;
Jens Axboe82c1ed32011-10-10 08:56:18 +0200986 flist_del_init(&client->eta_list);
987
Jens Axboe2f99deb2012-03-09 14:37:29 +0100988 if (client->ops->jobs_eta)
989 client->ops->jobs_eta(client, je);
990
Jens Axboe3e47bd22012-02-29 13:45:02 +0100991 fio_client_sum_jobs_eta(&eta->eta, je);
Jens Axboea5276612012-03-04 15:15:08 +0100992 fio_client_dec_jobs_eta(eta, client->ops->eta);
Jens Axboecf451d12011-10-03 16:48:30 +0200993}
994
Jens Axboeb5296dd2011-10-07 16:35:56 +0200995static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd)
Jens Axboe2e03b4b2011-10-04 09:00:40 +0200996{
997 struct cmd_probe_pdu *probe = (struct cmd_probe_pdu *) cmd->payload;
Jens Axboed2333352011-10-11 15:07:23 +0200998 const char *os, *arch;
999 char bit[16];
Jens Axboe2e03b4b2011-10-04 09:00:40 +02001000
Jens Axboecca84642011-10-07 12:47:57 +02001001 os = fio_get_os_string(probe->os);
1002 if (!os)
1003 os = "unknown";
1004
1005 arch = fio_get_arch_string(probe->arch);
1006 if (!arch)
1007 os = "unknown";
1008
Jens Axboed2333352011-10-11 15:07:23 +02001009 sprintf(bit, "%d-bit", probe->bpp * 8);
Jens Axboe38fdef22011-10-11 14:30:06 +02001010
Jens Axboe80295422012-04-16 09:42:53 +02001011 log_info("hostname=%s, be=%u, %s, os=%s, arch=%s, fio=%s\n",
Jens Axboe38fdef22011-10-11 14:30:06 +02001012 probe->hostname, probe->bigendian, bit, os, arch,
Jens Axboe80295422012-04-16 09:42:53 +02001013 probe->fio_version);
Jens Axboeb5296dd2011-10-07 16:35:56 +02001014
1015 if (!client->name)
1016 client->name = strdup((char *) probe->hostname);
Jens Axboe2e03b4b2011-10-04 09:00:40 +02001017}
1018
Jens Axboe11e950b2011-10-16 21:34:14 +02001019static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd)
1020{
1021 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
1022
1023 client->state = Client_started;
1024 client->jobs = le32_to_cpu(pdu->jobs);
Jens Axboe108fea72012-11-14 13:09:45 -07001025 client->nr_stat = le32_to_cpu(pdu->stat_outputs);
1026
1027 if (sum_stat_clients > 1)
1028 do_output_all_clients = 1;
1029
1030 sum_stat_clients += client->nr_stat;
Jens Axboe11e950b2011-10-16 21:34:14 +02001031}
1032
1033static void handle_stop(struct fio_client *client, struct fio_net_cmd *cmd)
1034{
Jens Axboe498c92c2011-10-17 09:14:42 +02001035 if (client->error)
1036 log_info("client <%s>: exited with error %d\n", client->hostname, client->error);
Jens Axboe11e950b2011-10-16 21:34:14 +02001037}
1038
Jens Axboe6b79c802012-03-08 10:51:36 +01001039static void convert_stop(struct fio_net_cmd *cmd)
1040{
1041 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
1042
1043 pdu->error = le32_to_cpu(pdu->error);
1044}
1045
Jens Axboe084d1c62012-03-03 20:28:07 +01001046static void convert_text(struct fio_net_cmd *cmd)
1047{
1048 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
1049
1050 pdu->level = le32_to_cpu(pdu->level);
1051 pdu->buf_len = le32_to_cpu(pdu->buf_len);
1052 pdu->log_sec = le64_to_cpu(pdu->log_sec);
1053 pdu->log_usec = le64_to_cpu(pdu->log_usec);
1054}
1055
Jens Axboe1b427252012-03-14 15:03:03 +01001056/*
1057 * This has been compressed on the server side, since it can be big.
1058 * Uncompress here.
1059 */
1060static struct cmd_iolog_pdu *convert_iolog(struct fio_net_cmd *cmd)
1061{
1062 struct cmd_iolog_pdu *pdu = (struct cmd_iolog_pdu *) cmd->payload;
1063 struct cmd_iolog_pdu *ret;
1064 uint32_t nr_samples;
1065 unsigned long total;
1066 z_stream stream;
1067 void *p;
Jens Axboef5ed7652012-03-14 16:28:07 +01001068 int i;
Jens Axboe1b427252012-03-14 15:03:03 +01001069
1070 stream.zalloc = Z_NULL;
1071 stream.zfree = Z_NULL;
1072 stream.opaque = Z_NULL;
1073 stream.avail_in = 0;
1074 stream.next_in = Z_NULL;
1075
1076 if (inflateInit(&stream) != Z_OK)
1077 return NULL;
1078
1079 /*
Jens Axboef5ed7652012-03-14 16:28:07 +01001080 * Get header first, it's not compressed
Jens Axboe1b427252012-03-14 15:03:03 +01001081 */
1082 nr_samples = le32_to_cpu(pdu->nr_samples);
1083
Jens Axboef5ed7652012-03-14 16:28:07 +01001084 total = nr_samples * sizeof(struct io_sample);
1085 ret = malloc(total + sizeof(*pdu));
Jens Axboe2f122b12012-03-15 13:10:19 +01001086 ret->thread_number = le32_to_cpu(pdu->thread_number);
Jens Axboe1b427252012-03-14 15:03:03 +01001087 ret->nr_samples = nr_samples;
Jens Axboef5ed7652012-03-14 16:28:07 +01001088 ret->log_type = le32_to_cpu(pdu->log_type);
1089 strcpy((char *) ret->name, (char *) pdu->name);
Jens Axboe1b427252012-03-14 15:03:03 +01001090
Jens Axboef5ed7652012-03-14 16:28:07 +01001091 p = (void *) ret + sizeof(*pdu);
1092
1093 stream.avail_in = cmd->pdu_len - sizeof(*pdu);
1094 stream.next_in = (void *) pdu + sizeof(*pdu);
Jens Axboe1b427252012-03-14 15:03:03 +01001095 while (stream.avail_in) {
1096 unsigned int this_chunk = 65536;
1097 unsigned int this_len;
1098 int err;
1099
1100 if (this_chunk > total)
1101 this_chunk = total;
1102
1103 stream.avail_out = this_chunk;
1104 stream.next_out = p;
1105 err = inflate(&stream, Z_NO_FLUSH);
Jens Axboe3c547fe2012-03-14 21:53:00 +01001106 /* may be Z_OK, or Z_STREAM_END */
1107 if (err < 0) {
Jens Axboe1b427252012-03-14 15:03:03 +01001108 log_err("fio: inflate error %d\n", err);
Jens Axboef5ed7652012-03-14 16:28:07 +01001109 free(ret);
1110 ret = NULL;
Jens Axboe1b427252012-03-14 15:03:03 +01001111 goto out;
1112 }
1113
1114 this_len = this_chunk - stream.avail_out;
1115 p += this_len;
1116 total -= this_len;
1117 }
1118
Jens Axboef5ed7652012-03-14 16:28:07 +01001119 for (i = 0; i < ret->nr_samples; i++) {
1120 struct io_sample *s = &ret->samples[i];
1121
1122 s->time = le64_to_cpu(s->time);
1123 s->val = le64_to_cpu(s->val);
1124 s->ddir = le32_to_cpu(s->ddir);
1125 s->bs = le32_to_cpu(s->bs);
1126 }
1127
Jens Axboe1b427252012-03-14 15:03:03 +01001128out:
1129 inflateEnd(&stream);
1130 return ret;
1131}
1132
Jens Axboea5276612012-03-04 15:15:08 +01001133int fio_handle_client(struct fio_client *client)
Jens Axboe37db14f2011-09-30 17:00:42 -06001134{
Jens Axboea5276612012-03-04 15:15:08 +01001135 struct client_ops *ops = client->ops;
Jens Axboe37db14f2011-09-30 17:00:42 -06001136 struct fio_net_cmd *cmd;
1137
Jens Axboe60efd142011-10-04 13:30:11 +02001138 dprint(FD_NET, "client: handle %s\n", client->hostname);
1139
Jens Axboee951bdc2011-10-05 21:58:45 +02001140 cmd = fio_net_recv_cmd(client->fd);
1141 if (!cmd)
1142 return 0;
Jens Axboec2c94582011-10-05 20:31:30 +02001143
Jens Axboeb9d2f302012-03-08 20:36:28 +01001144 dprint(FD_NET, "client: got cmd op %s from %s (pdu=%u)\n",
1145 fio_server_op(cmd->opcode), client->hostname, cmd->pdu_len);
Jens Axboe46c48f12011-10-01 12:50:51 -06001146
Jens Axboee951bdc2011-10-05 21:58:45 +02001147 switch (cmd->opcode) {
1148 case FIO_NET_CMD_QUIT:
Jens Axboe3ec62ec2012-03-01 12:01:29 +01001149 if (ops->quit)
Jens Axboe35c0ba72012-03-14 10:56:40 +01001150 ops->quit(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +02001151 remove_client(client);
1152 free(cmd);
1153 break;
Jens Axboe084d1c62012-03-03 20:28:07 +01001154 case FIO_NET_CMD_TEXT:
1155 convert_text(cmd);
Jens Axboe35c0ba72012-03-14 10:56:40 +01001156 ops->text(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +02001157 free(cmd);
1158 break;
Jens Axboe3bf236c2012-03-03 20:35:50 +01001159 case FIO_NET_CMD_DU: {
1160 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
1161
1162 convert_dus(&du->dus);
1163 convert_agg(&du->agg);
1164
Stephen M. Camerondd366722012-02-24 08:17:30 +01001165 ops->disk_util(client, cmd);
Jens Axboed09a64a2011-10-13 11:38:56 +02001166 free(cmd);
1167 break;
Jens Axboe3bf236c2012-03-03 20:35:50 +01001168 }
1169 case FIO_NET_CMD_TS: {
1170 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
1171
1172 convert_ts(&p->ts, &p->ts);
1173 convert_gs(&p->rs, &p->rs);
1174
Jens Axboe89e5fad2012-03-05 09:21:12 +01001175 ops->thread_status(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +02001176 free(cmd);
1177 break;
Jens Axboe3bf236c2012-03-03 20:35:50 +01001178 }
1179 case FIO_NET_CMD_GS: {
1180 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
1181
1182 convert_gs(gs, gs);
1183
Jens Axboe89e5fad2012-03-05 09:21:12 +01001184 ops->group_stats(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +02001185 free(cmd);
1186 break;
Jens Axboe3bf236c2012-03-03 20:35:50 +01001187 }
1188 case FIO_NET_CMD_ETA: {
1189 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
1190
Jens Axboe89c17072011-10-11 10:15:51 +02001191 remove_reply_cmd(client, cmd);
Jens Axboe3bf236c2012-03-03 20:35:50 +01001192 convert_jobs_eta(je);
Jens Axboea5276612012-03-04 15:15:08 +01001193 handle_eta(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +02001194 free(cmd);
1195 break;
Jens Axboe3bf236c2012-03-03 20:35:50 +01001196 }
Jens Axboee951bdc2011-10-05 21:58:45 +02001197 case FIO_NET_CMD_PROBE:
Jens Axboe89c17072011-10-11 10:15:51 +02001198 remove_reply_cmd(client, cmd);
Stephen M. Camerondd366722012-02-24 08:17:30 +01001199 ops->probe(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +02001200 free(cmd);
1201 break;
Jens Axboe5d7793a2012-03-08 19:59:16 +01001202 case FIO_NET_CMD_SERVER_START:
Jens Axboe01be0382011-10-15 14:43:41 +02001203 client->state = Client_running;
Jens Axboe85dd01e2012-03-12 14:33:16 +01001204 if (ops->job_start)
1205 ops->job_start(client, cmd);
Jens Axboe01be0382011-10-15 14:43:41 +02001206 free(cmd);
1207 break;
Jens Axboe85dd01e2012-03-12 14:33:16 +01001208 case FIO_NET_CMD_START: {
1209 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
1210
1211 pdu->jobs = le32_to_cpu(pdu->jobs);
1212 ops->start(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +02001213 free(cmd);
1214 break;
Jens Axboe85dd01e2012-03-12 14:33:16 +01001215 }
Jens Axboe6b79c802012-03-08 10:51:36 +01001216 case FIO_NET_CMD_STOP: {
1217 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
1218
1219 convert_stop(cmd);
1220 client->state = Client_stopped;
Jens Axboe122c7722012-03-19 09:13:15 +01001221 client->error = le32_to_cpu(pdu->error);
1222 client->signal = le32_to_cpu(pdu->signal);
Jens Axboe6b79c802012-03-08 10:51:36 +01001223 ops->stop(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +02001224 free(cmd);
1225 break;
Jens Axboe6b79c802012-03-08 10:51:36 +01001226 }
Jens Axboe40c60512012-03-27 16:03:04 +02001227 case FIO_NET_CMD_ADD_JOB: {
1228 struct cmd_add_job_pdu *pdu = (struct cmd_add_job_pdu *) cmd->payload;
1229
1230 client->thread_number = le32_to_cpu(pdu->thread_number);
1231 client->groupid = le32_to_cpu(pdu->groupid);
1232
Jens Axboe807f9972012-03-02 10:25:24 +01001233 if (ops->add_job)
1234 ops->add_job(client, cmd);
1235 free(cmd);
1236 break;
Jens Axboe40c60512012-03-27 16:03:04 +02001237 }
Jens Axboe1b427252012-03-14 15:03:03 +01001238 case FIO_NET_CMD_IOLOG:
1239 if (ops->iolog) {
1240 struct cmd_iolog_pdu *pdu;
1241
1242 pdu = convert_iolog(cmd);
1243 ops->iolog(client, pdu);
1244 }
1245 free(cmd);
1246 break;
Jens Axboe40c60512012-03-27 16:03:04 +02001247 case FIO_NET_CMD_UPDATE_JOB:
Jens Axboe40c60512012-03-27 16:03:04 +02001248 ops->update_job(client, cmd);
Jens Axboe649cee92012-03-28 09:15:32 +02001249 remove_reply_cmd(client, cmd);
Jens Axboe40c60512012-03-27 16:03:04 +02001250 free(cmd);
1251 break;
Jens Axboee951bdc2011-10-05 21:58:45 +02001252 default:
Jens Axboe89c17072011-10-11 10:15:51 +02001253 log_err("fio: unknown client op: %s\n", fio_server_op(cmd->opcode));
Jens Axboee951bdc2011-10-05 21:58:45 +02001254 free(cmd);
1255 break;
Jens Axboe37db14f2011-09-30 17:00:42 -06001256 }
1257
Jens Axboee951bdc2011-10-05 21:58:45 +02001258 return 1;
Jens Axboe37db14f2011-09-30 17:00:42 -06001259}
Jens Axboeb66570d2011-10-01 11:11:35 -06001260
Jens Axboea5276612012-03-04 15:15:08 +01001261static void request_client_etas(struct client_ops *ops)
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001262{
1263 struct fio_client *client;
1264 struct flist_head *entry;
1265 struct client_eta *eta;
Jens Axboe82c1ed32011-10-10 08:56:18 +02001266 int skipped = 0;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001267
1268 dprint(FD_NET, "client: request eta (%d)\n", nr_clients);
1269
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001270 eta = malloc(sizeof(*eta));
1271 memset(&eta->eta, 0, sizeof(eta->eta));
1272 eta->pending = nr_clients;
1273
1274 flist_for_each(entry, &client_list) {
1275 client = flist_entry(entry, struct fio_client, list);
1276
Jens Axboe82c1ed32011-10-10 08:56:18 +02001277 if (!flist_empty(&client->eta_list)) {
1278 skipped++;
1279 continue;
1280 }
Jens Axboe01be0382011-10-15 14:43:41 +02001281 if (client->state != Client_running)
1282 continue;
Jens Axboe82c1ed32011-10-10 08:56:18 +02001283
Jens Axboef77d2672011-10-10 14:36:07 +02001284 assert(!client->eta_in_flight);
Jens Axboe82c1ed32011-10-10 08:56:18 +02001285 flist_add_tail(&client->eta_list, &eta_list);
Jens Axboef77d2672011-10-10 14:36:07 +02001286 client->eta_in_flight = eta;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001287 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_SEND_ETA,
Jens Axboedf380932011-10-11 14:25:08 +02001288 (uintptr_t) eta, &client->cmd_list);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001289 }
1290
Jens Axboe82c1ed32011-10-10 08:56:18 +02001291 while (skipped--)
Jens Axboea5276612012-03-04 15:15:08 +01001292 fio_client_dec_jobs_eta(eta, ops->eta);
Jens Axboe82c1ed32011-10-10 08:56:18 +02001293
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001294 dprint(FD_NET, "client: requested eta tag %p\n", eta);
1295}
1296
Jens Axboe89c17072011-10-11 10:15:51 +02001297static int client_check_cmd_timeout(struct fio_client *client,
1298 struct timeval *now)
1299{
Jens Axboe40c60512012-03-27 16:03:04 +02001300 struct fio_net_cmd_reply *reply;
Jens Axboe89c17072011-10-11 10:15:51 +02001301 struct flist_head *entry, *tmp;
1302 int ret = 0;
1303
1304 flist_for_each_safe(entry, tmp, &client->cmd_list) {
Jens Axboe40c60512012-03-27 16:03:04 +02001305 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
Jens Axboe89c17072011-10-11 10:15:51 +02001306
Jens Axboe40c60512012-03-27 16:03:04 +02001307 if (mtime_since(&reply->tv, now) < FIO_NET_CLIENT_TIMEOUT)
Jens Axboe89c17072011-10-11 10:15:51 +02001308 continue;
1309
1310 log_err("fio: client %s, timeout on cmd %s\n", client->hostname,
Jens Axboe40c60512012-03-27 16:03:04 +02001311 fio_server_op(reply->opcode));
1312 flist_del(&reply->list);
1313 free(reply);
Jens Axboe89c17072011-10-11 10:15:51 +02001314 ret = 1;
1315 }
1316
1317 return flist_empty(&client->cmd_list) && ret;
1318}
1319
Jens Axboea5276612012-03-04 15:15:08 +01001320static int fio_check_clients_timed_out(void)
Jens Axboe89c17072011-10-11 10:15:51 +02001321{
1322 struct fio_client *client;
1323 struct flist_head *entry, *tmp;
1324 struct timeval tv;
1325 int ret = 0;
1326
Jens Axboe67bf9822013-01-10 11:23:19 +01001327 fio_gettime(&tv, NULL);
Jens Axboe89c17072011-10-11 10:15:51 +02001328
1329 flist_for_each_safe(entry, tmp, &client_list) {
1330 client = flist_entry(entry, struct fio_client, list);
1331
1332 if (flist_empty(&client->cmd_list))
1333 continue;
1334
1335 if (!client_check_cmd_timeout(client, &tv))
1336 continue;
1337
Jens Axboea5276612012-03-04 15:15:08 +01001338 if (client->ops->timed_out)
1339 client->ops->timed_out(client);
Jens Axboeed727a42012-03-02 12:14:40 +01001340 else
1341 log_err("fio: client %s timed out\n", client->hostname);
1342
Jens Axboe89c17072011-10-11 10:15:51 +02001343 remove_client(client);
1344 ret = 1;
1345 }
1346
1347 return ret;
1348}
1349
Stephen M. Camerondd366722012-02-24 08:17:30 +01001350int fio_handle_clients(struct client_ops *ops)
Jens Axboeb66570d2011-10-01 11:11:35 -06001351{
Jens Axboeb66570d2011-10-01 11:11:35 -06001352 struct pollfd *pfds;
Jens Axboe498c92c2011-10-17 09:14:42 +02001353 int i, ret = 0, retval = 0;
Jens Axboeb66570d2011-10-01 11:11:35 -06001354
Jens Axboe67bf9822013-01-10 11:23:19 +01001355 fio_gettime(&eta_tv, NULL);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001356
Jens Axboeb66570d2011-10-01 11:11:35 -06001357 pfds = malloc(nr_clients * sizeof(struct pollfd));
1358
Jens Axboe37f0c1a2011-10-11 14:08:33 +02001359 init_thread_stat(&client_ts);
1360 init_group_run_stat(&client_gs);
1361
Jens Axboeb66570d2011-10-01 11:11:35 -06001362 while (!exit_backend && nr_clients) {
Jens Axboec2cb6862012-02-23 20:56:12 +01001363 struct flist_head *entry, *tmp;
1364 struct fio_client *client;
1365
Jens Axboe82a4be12011-10-01 12:40:32 -06001366 i = 0;
Jens Axboec2cb6862012-02-23 20:56:12 +01001367 flist_for_each_safe(entry, tmp, &client_list) {
Jens Axboe82a4be12011-10-01 12:40:32 -06001368 client = flist_entry(entry, struct fio_client, list);
1369
Jens Axboea5276612012-03-04 15:15:08 +01001370 if (!client->sent_job && !client->ops->stay_connected &&
Jens Axboec2cb6862012-02-23 20:56:12 +01001371 flist_empty(&client->cmd_list)) {
1372 remove_client(client);
1373 continue;
1374 }
1375
Jens Axboe82a4be12011-10-01 12:40:32 -06001376 pfds[i].fd = client->fd;
1377 pfds[i].events = POLLIN;
1378 i++;
1379 }
1380
Jens Axboec2cb6862012-02-23 20:56:12 +01001381 if (!nr_clients)
1382 break;
1383
Jens Axboe82a4be12011-10-01 12:40:32 -06001384 assert(i == nr_clients);
1385
Jens Axboe5c2857f2011-10-04 13:14:32 +02001386 do {
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001387 struct timeval tv;
1388
Jens Axboe67bf9822013-01-10 11:23:19 +01001389 fio_gettime(&tv, NULL);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001390 if (mtime_since(&eta_tv, &tv) >= 900) {
Jens Axboea5276612012-03-04 15:15:08 +01001391 request_client_etas(ops);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001392 memcpy(&eta_tv, &tv, sizeof(tv));
Jens Axboe89c17072011-10-11 10:15:51 +02001393
Jens Axboea5276612012-03-04 15:15:08 +01001394 if (fio_check_clients_timed_out())
Jens Axboe89c17072011-10-11 10:15:51 +02001395 break;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001396 }
1397
Jens Axboe80102c82012-03-23 13:19:31 +01001398 ret = poll(pfds, nr_clients, ops->eta_msec);
Jens Axboe5c2857f2011-10-04 13:14:32 +02001399 if (ret < 0) {
1400 if (errno == EINTR)
1401 continue;
1402 log_err("fio: poll clients: %s\n", strerror(errno));
1403 break;
1404 } else if (!ret)
Jens Axboeb66570d2011-10-01 11:11:35 -06001405 continue;
Jens Axboe5c2857f2011-10-04 13:14:32 +02001406 } while (ret <= 0);
Jens Axboeb66570d2011-10-01 11:11:35 -06001407
1408 for (i = 0; i < nr_clients; i++) {
1409 if (!(pfds[i].revents & POLLIN))
1410 continue;
1411
1412 client = find_client_by_fd(pfds[i].fd);
1413 if (!client) {
Jens Axboe3c5f57e2011-10-06 12:37:50 +02001414 log_err("fio: unknown client fd %d\n", pfds[i].fd);
Jens Axboeb66570d2011-10-01 11:11:35 -06001415 continue;
1416 }
Jens Axboea5276612012-03-04 15:15:08 +01001417 if (!fio_handle_client(client)) {
Jens Axboe28d3ab02011-10-05 20:45:37 +02001418 log_info("client: host=%s disconnected\n",
1419 client->hostname);
1420 remove_client(client);
Jens Axboe498c92c2011-10-17 09:14:42 +02001421 retval = 1;
Jens Axboe38990762011-10-17 13:31:33 +02001422 } else if (client->error)
Jens Axboe498c92c2011-10-17 09:14:42 +02001423 retval = 1;
Jens Axboe343cb4a2012-03-09 17:16:51 +01001424 fio_put_client(client);
Jens Axboeb66570d2011-10-01 11:11:35 -06001425 }
1426 }
1427
1428 free(pfds);
Jens Axboe498c92c2011-10-17 09:14:42 +02001429 return retval;
Jens Axboeb66570d2011-10-01 11:11:35 -06001430}