blob: 47a2380c129c64b529c8bb3a887bcb4e8462f1be [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;
58
Jens Axboe3c5f57e2011-10-06 12:37:50 +020059#define FIO_CLIENT_HASH_BITS 7
60#define FIO_CLIENT_HASH_SZ (1 << FIO_CLIENT_HASH_BITS)
61#define FIO_CLIENT_HASH_MASK (FIO_CLIENT_HASH_SZ - 1)
Jens Axboebebe6392011-10-07 10:00:51 +020062static struct flist_head client_hash[FIO_CLIENT_HASH_SZ];
Jens Axboe3c5f57e2011-10-06 12:37:50 +020063
Jens Axboebebe6392011-10-07 10:00:51 +020064static void fio_client_add_hash(struct fio_client *client)
Jens Axboe3c5f57e2011-10-06 12:37:50 +020065{
66 int bucket = hash_long(client->fd, FIO_CLIENT_HASH_BITS);
67
68 bucket &= FIO_CLIENT_HASH_MASK;
Jens Axboebebe6392011-10-07 10:00:51 +020069 flist_add(&client->hash_list, &client_hash[bucket]);
Jens Axboe3c5f57e2011-10-06 12:37:50 +020070}
71
Jens Axboebebe6392011-10-07 10:00:51 +020072static void fio_client_remove_hash(struct fio_client *client)
Jens Axboe3c5f57e2011-10-06 12:37:50 +020073{
Jens Axboebebe6392011-10-07 10:00:51 +020074 if (!flist_empty(&client->hash_list))
75 flist_del_init(&client->hash_list);
Jens Axboe3c5f57e2011-10-06 12:37:50 +020076}
77
78static void fio_init fio_client_hash_init(void)
79{
80 int i;
81
Jens Axboebebe6392011-10-07 10:00:51 +020082 for (i = 0; i < FIO_CLIENT_HASH_SZ; i++)
83 INIT_FLIST_HEAD(&client_hash[i]);
Jens Axboe3c5f57e2011-10-06 12:37:50 +020084}
85
Jens Axboeb66570d2011-10-01 11:11:35 -060086static struct fio_client *find_client_by_fd(int fd)
87{
Jens Axboe3c5f57e2011-10-06 12:37:50 +020088 int bucket = hash_long(fd, FIO_CLIENT_HASH_BITS) & FIO_CLIENT_HASH_MASK;
Jens Axboeb66570d2011-10-01 11:11:35 -060089 struct fio_client *client;
90 struct flist_head *entry;
91
Jens Axboebebe6392011-10-07 10:00:51 +020092 flist_for_each(entry, &client_hash[bucket]) {
93 client = flist_entry(entry, struct fio_client, hash_list);
Jens Axboeb66570d2011-10-01 11:11:35 -060094
Jens Axboee55f8f32012-03-06 15:42:31 +010095 if (client->fd == fd) {
96 client->refs++;
Jens Axboeb66570d2011-10-01 11:11:35 -060097 return client;
Jens Axboee55f8f32012-03-06 15:42:31 +010098 }
Jens Axboeb66570d2011-10-01 11:11:35 -060099 }
100
101 return NULL;
102}
103
Jens Axboe3af45202012-03-13 09:59:53 +0100104void fio_put_client(struct fio_client *client)
Jens Axboeb66570d2011-10-01 11:11:35 -0600105{
Jens Axboe5121a9a2012-03-05 13:32:47 +0100106 if (--client->refs)
107 return;
108
Jens Axboeb66570d2011-10-01 11:11:35 -0600109 free(client->hostname);
Jens Axboe81179ee2011-10-04 12:42:06 +0200110 if (client->argv)
111 free(client->argv);
Jens Axboeb5296dd2011-10-07 16:35:56 +0200112 if (client->name)
113 free(client->name);
Jens Axboe14ea90e2012-08-26 17:33:34 +0200114 while (client->nr_ini_file)
115 free(client->ini_file[--client->nr_ini_file]);
116 if (client->ini_file)
117 free(client->ini_file);
Jens Axboe81179ee2011-10-04 12:42:06 +0200118
Jens Axboeb66570d2011-10-01 11:11:35 -0600119 free(client);
120}
Jens Axboe132159a2011-09-30 15:01:32 -0600121
Jens Axboe3af45202012-03-13 09:59:53 +0100122static void remove_client(struct fio_client *client)
Jens Axboe5121a9a2012-03-05 13:32:47 +0100123{
Jens Axboe3af45202012-03-13 09:59:53 +0100124 assert(client->refs);
125
126 dprint(FD_NET, "client: removed <%s>\n", client->hostname);
127
128 if (!flist_empty(&client->list))
129 flist_del_init(&client->list);
130
131 fio_client_remove_hash(client);
132
133 if (!flist_empty(&client->eta_list)) {
134 flist_del_init(&client->eta_list);
135 fio_client_dec_jobs_eta(client->eta_in_flight, client->ops->eta);
136 }
137
Jens Axboe1e0c1842012-03-20 15:15:00 +0100138 close(client->fd);
139 client->fd = -1;
140
Jens Axboe0cf3ece2012-03-21 10:15:20 +0100141 if (client->ops->removed)
142 client->ops->removed(client);
143
Jens Axboe3af45202012-03-13 09:59:53 +0100144 nr_clients--;
145 sum_stat_clients--;
146
147 fio_put_client(client);
Jens Axboe5121a9a2012-03-05 13:32:47 +0100148}
149
Jens Axboe343cb4a2012-03-09 17:16:51 +0100150struct fio_client *fio_get_client(struct fio_client *client)
151{
152 client->refs++;
153 return client;
154}
155
Jens Axboefa2ea802011-10-08 21:07:29 +0200156static void __fio_client_add_cmd_option(struct fio_client *client,
157 const char *opt)
Jens Axboe81179ee2011-10-04 12:42:06 +0200158{
Jens Axboe39e8e012011-10-04 13:46:08 +0200159 int index;
160
161 index = client->argc++;
Jens Axboe81179ee2011-10-04 12:42:06 +0200162 client->argv = realloc(client->argv, sizeof(char *) * client->argc);
Jens Axboe39e8e012011-10-04 13:46:08 +0200163 client->argv[index] = strdup(opt);
164 dprint(FD_NET, "client: add cmd %d: %s\n", index, opt);
Jens Axboe81179ee2011-10-04 12:42:06 +0200165}
166
Jens Axboefa2ea802011-10-08 21:07:29 +0200167void fio_client_add_cmd_option(void *cookie, const char *opt)
Jens Axboe81179ee2011-10-04 12:42:06 +0200168{
Jens Axboebebe6392011-10-07 10:00:51 +0200169 struct fio_client *client = cookie;
Jens Axboe3f3a4542011-10-10 21:11:09 +0200170 struct flist_head *entry;
Jens Axboe81179ee2011-10-04 12:42:06 +0200171
Jens Axboebebe6392011-10-07 10:00:51 +0200172 if (!client || !opt)
Jens Axboefa2ea802011-10-08 21:07:29 +0200173 return;
Jens Axboe81179ee2011-10-04 12:42:06 +0200174
Jens Axboefa2ea802011-10-08 21:07:29 +0200175 __fio_client_add_cmd_option(client, opt);
Jens Axboe3f3a4542011-10-10 21:11:09 +0200176
177 /*
178 * Duplicate arguments to shared client group
179 */
180 flist_for_each(entry, &arg_list) {
181 client = flist_entry(entry, struct fio_client, arg_list);
182
183 __fio_client_add_cmd_option(client, opt);
184 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200185}
186
Jens Axboea5276612012-03-04 15:15:08 +0100187struct fio_client *fio_client_add_explicit(struct client_ops *ops,
188 const char *hostname, int type,
Jens Axboe3ec62ec2012-03-01 12:01:29 +0100189 int port)
190{
191 struct fio_client *client;
192
193 client = malloc(sizeof(*client));
194 memset(client, 0, sizeof(*client));
195
196 INIT_FLIST_HEAD(&client->list);
197 INIT_FLIST_HEAD(&client->hash_list);
198 INIT_FLIST_HEAD(&client->arg_list);
199 INIT_FLIST_HEAD(&client->eta_list);
200 INIT_FLIST_HEAD(&client->cmd_list);
201
202 client->hostname = strdup(hostname);
203
204 if (type == Fio_client_socket)
205 client->is_sock = 1;
206 else {
207 int ipv6;
208
209 ipv6 = type == Fio_client_ipv6;
210 if (fio_server_parse_host(hostname, &ipv6,
211 &client->addr.sin_addr,
212 &client->addr6.sin6_addr))
213 goto err;
214
215 client->port = port;
216 }
217
218 client->fd = -1;
Jens Axboea5276612012-03-04 15:15:08 +0100219 client->ops = ops;
Jens Axboe5121a9a2012-03-05 13:32:47 +0100220 client->refs = 1;
Jens Axboe46bcd492012-03-14 11:31:21 +0100221 client->type = ops->client_type;
Jens Axboe3ec62ec2012-03-01 12:01:29 +0100222
223 __fio_client_add_cmd_option(client, "fio");
224
225 flist_add(&client->list, &client_list);
226 nr_clients++;
227 dprint(FD_NET, "client: added <%s>\n", client->hostname);
228 return client;
229err:
230 free(client);
231 return NULL;
232}
233
Jens Axboe14ea90e2012-08-26 17:33:34 +0200234void fio_client_add_ini_file(void *cookie, const char *ini_file)
235{
236 struct fio_client *client = cookie;
237 size_t new_size;
238
239 dprint(FD_NET, "client <%s>: add ini %s\n", client->hostname, ini_file);
240
241 new_size = (client->nr_ini_file + 1) * sizeof(char *);
242 client->ini_file = realloc(client->ini_file, new_size);
243 client->ini_file[client->nr_ini_file] = strdup(ini_file);
244 client->nr_ini_file++;
245}
246
Jens Axboea5276612012-03-04 15:15:08 +0100247int fio_client_add(struct client_ops *ops, const char *hostname, void **cookie)
Jens Axboe132159a2011-09-30 15:01:32 -0600248{
Jens Axboe3f3a4542011-10-10 21:11:09 +0200249 struct fio_client *existing = *cookie;
Jens Axboeb66570d2011-10-01 11:11:35 -0600250 struct fio_client *client;
Jens Axboe132159a2011-09-30 15:01:32 -0600251
Jens Axboe3f3a4542011-10-10 21:11:09 +0200252 if (existing) {
253 /*
254 * We always add our "exec" name as the option, hence 1
255 * means empty.
256 */
257 if (existing->argc == 1)
258 flist_add_tail(&existing->arg_list, &arg_list);
259 else {
260 while (!flist_empty(&arg_list))
261 flist_del_init(arg_list.next);
262 }
263 }
264
Jens Axboeb66570d2011-10-01 11:11:35 -0600265 client = malloc(sizeof(*client));
Jens Axboea37f69b2011-10-01 12:24:21 -0600266 memset(client, 0, sizeof(*client));
Jens Axboe81179ee2011-10-04 12:42:06 +0200267
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200268 INIT_FLIST_HEAD(&client->list);
Jens Axboebebe6392011-10-07 10:00:51 +0200269 INIT_FLIST_HEAD(&client->hash_list);
Jens Axboe3f3a4542011-10-10 21:11:09 +0200270 INIT_FLIST_HEAD(&client->arg_list);
Jens Axboe82c1ed32011-10-10 08:56:18 +0200271 INIT_FLIST_HEAD(&client->eta_list);
Jens Axboe89c17072011-10-11 10:15:51 +0200272 INIT_FLIST_HEAD(&client->cmd_list);
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200273
Jens Axboebebe6392011-10-07 10:00:51 +0200274 if (fio_server_parse_string(hostname, &client->hostname,
275 &client->is_sock, &client->port,
Jens Axboe811826b2011-10-24 09:11:50 +0200276 &client->addr.sin_addr,
277 &client->addr6.sin6_addr,
278 &client->ipv6))
Jens Axboebebe6392011-10-07 10:00:51 +0200279 return -1;
280
Jens Axboea37f69b2011-10-01 12:24:21 -0600281 client->fd = -1;
Jens Axboea5276612012-03-04 15:15:08 +0100282 client->ops = ops;
Jens Axboe5121a9a2012-03-05 13:32:47 +0100283 client->refs = 1;
Jens Axboe46bcd492012-03-14 11:31:21 +0100284 client->type = ops->client_type;
Jens Axboe81179ee2011-10-04 12:42:06 +0200285
286 __fio_client_add_cmd_option(client, "fio");
287
Jens Axboea37f69b2011-10-01 12:24:21 -0600288 flist_add(&client->list, &client_list);
289 nr_clients++;
Jens Axboebebe6392011-10-07 10:00:51 +0200290 dprint(FD_NET, "client: added <%s>\n", client->hostname);
291 *cookie = client;
292 return 0;
Jens Axboea37f69b2011-10-01 12:24:21 -0600293}
294
Jens Axboed824c3b2012-03-09 17:45:43 +0100295static void probe_client(struct fio_client *client)
296{
297 dprint(FD_NET, "client: send probe\n");
298
299 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_PROBE, 0, &client->cmd_list);
300}
301
Jens Axboe87aa8f12011-10-06 21:24:13 +0200302static int fio_client_connect_ip(struct fio_client *client)
Jens Axboea37f69b2011-10-01 12:24:21 -0600303{
Jens Axboe811826b2011-10-24 09:11:50 +0200304 struct sockaddr *addr;
305 fio_socklen_t socklen;
306 int fd, domain;
Jens Axboe132159a2011-09-30 15:01:32 -0600307
Jens Axboe811826b2011-10-24 09:11:50 +0200308 if (client->ipv6) {
309 client->addr6.sin6_family = AF_INET6;
310 client->addr6.sin6_port = htons(client->port);
311 domain = AF_INET6;
312 addr = (struct sockaddr *) &client->addr6;
313 socklen = sizeof(client->addr6);
314 } else {
315 client->addr.sin_family = AF_INET;
316 client->addr.sin_port = htons(client->port);
317 domain = AF_INET;
318 addr = (struct sockaddr *) &client->addr;
319 socklen = sizeof(client->addr);
320 }
Jens Axboe132159a2011-09-30 15:01:32 -0600321
Jens Axboe811826b2011-10-24 09:11:50 +0200322 fd = socket(domain, SOCK_STREAM, 0);
Jens Axboe132159a2011-09-30 15:01:32 -0600323 if (fd < 0) {
Jens Axboe25095e12012-03-09 17:09:55 +0100324 int ret = -errno;
325
Jens Axboe132159a2011-09-30 15:01:32 -0600326 log_err("fio: socket: %s\n", strerror(errno));
Jens Axboe25095e12012-03-09 17:09:55 +0100327 return ret;
Jens Axboe132159a2011-09-30 15:01:32 -0600328 }
329
Jens Axboe811826b2011-10-24 09:11:50 +0200330 if (connect(fd, addr, socklen) < 0) {
Jens Axboe25095e12012-03-09 17:09:55 +0100331 int ret = -errno;
332
Jens Axboe132159a2011-09-30 15:01:32 -0600333 log_err("fio: connect: %s\n", strerror(errno));
Jens Axboea7de0a12011-10-07 12:55:14 +0200334 log_err("fio: failed to connect to %s:%u\n", client->hostname,
335 client->port);
Jens Axboeb94cba42011-10-06 21:27:10 +0200336 close(fd);
Jens Axboe25095e12012-03-09 17:09:55 +0100337 return ret;
Jens Axboe132159a2011-09-30 15:01:32 -0600338 }
339
Jens Axboe87aa8f12011-10-06 21:24:13 +0200340 return fd;
341}
342
343static int fio_client_connect_sock(struct fio_client *client)
344{
345 struct sockaddr_un *addr = &client->addr_un;
346 fio_socklen_t len;
347 int fd;
348
349 memset(addr, 0, sizeof(*addr));
350 addr->sun_family = AF_UNIX;
351 strcpy(addr->sun_path, client->hostname);
352
353 fd = socket(AF_UNIX, SOCK_STREAM, 0);
354 if (fd < 0) {
Jens Axboe25095e12012-03-09 17:09:55 +0100355 int ret = -errno;
356
Jens Axboe87aa8f12011-10-06 21:24:13 +0200357 log_err("fio: socket: %s\n", strerror(errno));
Jens Axboe25095e12012-03-09 17:09:55 +0100358 return ret;
Jens Axboe87aa8f12011-10-06 21:24:13 +0200359 }
360
361 len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
362 if (connect(fd, (struct sockaddr *) addr, len) < 0) {
Jens Axboe25095e12012-03-09 17:09:55 +0100363 int ret = -errno;
364
Jens Axboe87aa8f12011-10-06 21:24:13 +0200365 log_err("fio: connect; %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200366 close(fd);
Jens Axboe25095e12012-03-09 17:09:55 +0100367 return ret;
Jens Axboe87aa8f12011-10-06 21:24:13 +0200368 }
369
370 return fd;
371}
372
Jens Axboe2f99deb2012-03-09 14:37:29 +0100373int fio_client_connect(struct fio_client *client)
Jens Axboe87aa8f12011-10-06 21:24:13 +0200374{
375 int fd;
376
377 dprint(FD_NET, "client: connect to host %s\n", client->hostname);
378
Jens Axboe87aa8f12011-10-06 21:24:13 +0200379 if (client->is_sock)
380 fd = fio_client_connect_sock(client);
381 else
382 fd = fio_client_connect_ip(client);
383
Jens Axboe89c17072011-10-11 10:15:51 +0200384 dprint(FD_NET, "client: %s connected %d\n", client->hostname, fd);
385
Jens Axboe87aa8f12011-10-06 21:24:13 +0200386 if (fd < 0)
Jens Axboea1a3ed52012-03-09 17:00:01 +0100387 return fd;
Jens Axboe87aa8f12011-10-06 21:24:13 +0200388
Jens Axboeb66570d2011-10-01 11:11:35 -0600389 client->fd = fd;
Jens Axboebebe6392011-10-07 10:00:51 +0200390 fio_client_add_hash(client);
Jens Axboe81179ee2011-10-04 12:42:06 +0200391 client->state = Client_connected;
Jens Axboed824c3b2012-03-09 17:45:43 +0100392
393 probe_client(client);
Jens Axboe132159a2011-09-30 15:01:32 -0600394 return 0;
395}
396
Jens Axboe0cf3ece2012-03-21 10:15:20 +0100397int fio_client_terminate(struct fio_client *client)
Jens Axboe2f99deb2012-03-09 14:37:29 +0100398{
Jens Axboe0cf3ece2012-03-21 10:15:20 +0100399 return fio_net_send_quit(client->fd);
Jens Axboe2f99deb2012-03-09 14:37:29 +0100400}
401
Jens Axboecc0df002011-10-03 20:53:32 +0200402void fio_clients_terminate(void)
403{
404 struct flist_head *entry;
405 struct fio_client *client;
406
Jens Axboe60efd142011-10-04 13:30:11 +0200407 dprint(FD_NET, "client: terminate clients\n");
408
Jens Axboecc0df002011-10-03 20:53:32 +0200409 flist_for_each(entry, &client_list) {
410 client = flist_entry(entry, struct fio_client, list);
Jens Axboe2f99deb2012-03-09 14:37:29 +0100411 fio_client_terminate(client);
Jens Axboecc0df002011-10-03 20:53:32 +0200412 }
413}
414
415static void sig_int(int sig)
416{
Jens Axboebebe6392011-10-07 10:00:51 +0200417 dprint(FD_NET, "client: got signal %d\n", sig);
Jens Axboecc0df002011-10-03 20:53:32 +0200418 fio_clients_terminate();
419}
420
Jens Axboeb852e7c2012-03-30 10:30:35 +0200421static void sig_show_status(int sig)
422{
423 show_running_run_stats();
424}
425
Jens Axboecc0df002011-10-03 20:53:32 +0200426static void client_signal_handler(void)
427{
428 struct sigaction act;
429
430 memset(&act, 0, sizeof(act));
431 act.sa_handler = sig_int;
432 act.sa_flags = SA_RESTART;
433 sigaction(SIGINT, &act, NULL);
434
435 memset(&act, 0, sizeof(act));
436 act.sa_handler = sig_int;
437 act.sa_flags = SA_RESTART;
438 sigaction(SIGTERM, &act, NULL);
Jens Axboeb852e7c2012-03-30 10:30:35 +0200439
440 memset(&act, 0, sizeof(act));
441 act.sa_handler = sig_show_status;
442 act.sa_flags = SA_RESTART;
443 sigaction(SIGUSR1, &act, NULL);
Jens Axboecc0df002011-10-03 20:53:32 +0200444}
445
Jens Axboe81179ee2011-10-04 12:42:06 +0200446static int send_client_cmd_line(struct fio_client *client)
447{
Jens Axboefa2ea802011-10-08 21:07:29 +0200448 struct cmd_single_line_pdu *cslp;
449 struct cmd_line_pdu *clp;
450 unsigned long offset;
Jens Axboe7f868312011-10-10 09:55:21 +0200451 unsigned int *lens;
Jens Axboefa2ea802011-10-08 21:07:29 +0200452 void *pdu;
453 size_t mem;
Jens Axboe81179ee2011-10-04 12:42:06 +0200454 int i, ret;
455
Jens Axboe39e8e012011-10-04 13:46:08 +0200456 dprint(FD_NET, "client: send cmdline %d\n", client->argc);
Jens Axboe60efd142011-10-04 13:30:11 +0200457
Jens Axboe7f868312011-10-10 09:55:21 +0200458 lens = malloc(client->argc * sizeof(unsigned int));
459
Jens Axboefa2ea802011-10-08 21:07:29 +0200460 /*
461 * Find out how much mem we need
462 */
Jens Axboe7f868312011-10-10 09:55:21 +0200463 for (i = 0, mem = 0; i < client->argc; i++) {
464 lens[i] = strlen(client->argv[i]) + 1;
465 mem += lens[i];
466 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200467
Jens Axboefa2ea802011-10-08 21:07:29 +0200468 /*
469 * We need one cmd_line_pdu, and argc number of cmd_single_line_pdu
470 */
471 mem += sizeof(*clp) + (client->argc * sizeof(*cslp));
472
473 pdu = malloc(mem);
474 clp = pdu;
475 offset = sizeof(*clp);
476
477 for (i = 0; i < client->argc; i++) {
Jens Axboe7f868312011-10-10 09:55:21 +0200478 uint16_t arg_len = lens[i];
Jens Axboefa2ea802011-10-08 21:07:29 +0200479
480 cslp = pdu + offset;
481 strcpy((char *) cslp->text, client->argv[i]);
482 cslp->len = cpu_to_le16(arg_len);
483 offset += sizeof(*cslp) + arg_len;
484 }
485
Jens Axboe7f868312011-10-10 09:55:21 +0200486 free(lens);
Jens Axboefa2ea802011-10-08 21:07:29 +0200487 clp->lines = cpu_to_le16(client->argc);
Jens Axboe46bcd492012-03-14 11:31:21 +0100488 clp->client_type = __cpu_to_le16(client->type);
Jens Axboe40c60512012-03-27 16:03:04 +0200489 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOBLINE, pdu, mem, NULL, NULL);
Jens Axboe81179ee2011-10-04 12:42:06 +0200490 free(pdu);
491 return ret;
492}
493
Jens Axboea37f69b2011-10-01 12:24:21 -0600494int fio_clients_connect(void)
495{
496 struct fio_client *client;
497 struct flist_head *entry, *tmp;
498 int ret;
499
Bruce Cran93bcfd22012-02-20 20:18:19 +0100500#ifdef WIN32
501 WSADATA wsd;
Jens Axboe3c3ed072012-03-27 09:12:39 +0200502 WSAStartup(MAKEWORD(2, 2), &wsd);
Bruce Cran93bcfd22012-02-20 20:18:19 +0100503#endif
504
Jens Axboe60efd142011-10-04 13:30:11 +0200505 dprint(FD_NET, "client: connect all\n");
506
Jens Axboecc0df002011-10-03 20:53:32 +0200507 client_signal_handler();
508
Jens Axboea37f69b2011-10-01 12:24:21 -0600509 flist_for_each_safe(entry, tmp, &client_list) {
510 client = flist_entry(entry, struct fio_client, list);
511
512 ret = fio_client_connect(client);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200513 if (ret) {
Jens Axboea37f69b2011-10-01 12:24:21 -0600514 remove_client(client);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200515 continue;
516 }
517
Jens Axboe81179ee2011-10-04 12:42:06 +0200518 if (client->argc > 1)
519 send_client_cmd_line(client);
Jens Axboea37f69b2011-10-01 12:24:21 -0600520 }
521
522 return !nr_clients;
523}
524
Jens Axboeb9d2f302012-03-08 20:36:28 +0100525int fio_start_client(struct fio_client *client)
526{
527 dprint(FD_NET, "client: start %s\n", client->hostname);
528 return fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_RUN, 0, NULL);
529}
530
531int fio_start_all_clients(void)
532{
533 struct fio_client *client;
534 struct flist_head *entry, *tmp;
535 int ret;
536
537 dprint(FD_NET, "client: start all\n");
538
539 flist_for_each_safe(entry, tmp, &client_list) {
540 client = flist_entry(entry, struct fio_client, list);
541
542 ret = fio_start_client(client);
543 if (ret) {
544 remove_client(client);
545 continue;
546 }
547 }
548
549 return flist_empty(&client_list);
550}
551
Jens Axboe132159a2011-09-30 15:01:32 -0600552/*
553 * Send file contents to server backend. We could use sendfile(), but to remain
554 * more portable lets just read/write the darn thing.
555 */
Jens Axboe9988ca72012-03-09 15:14:06 +0100556static int __fio_client_send_ini(struct fio_client *client, const char *filename)
Jens Axboe132159a2011-09-30 15:01:32 -0600557{
Jens Axboe46bcd492012-03-14 11:31:21 +0100558 struct cmd_job_pdu *pdu;
559 size_t p_size;
Jens Axboe132159a2011-09-30 15:01:32 -0600560 struct stat sb;
Jens Axboe46bcd492012-03-14 11:31:21 +0100561 char *p;
562 void *buf;
Jens Axboe132159a2011-09-30 15:01:32 -0600563 off_t len;
564 int fd, ret;
565
Jens Axboe46c48f12011-10-01 12:50:51 -0600566 dprint(FD_NET, "send ini %s to %s\n", filename, client->hostname);
567
Jens Axboe132159a2011-09-30 15:01:32 -0600568 fd = open(filename, O_RDONLY);
569 if (fd < 0) {
Jens Axboe25095e12012-03-09 17:09:55 +0100570 int ret = -errno;
571
Jens Axboee951bdc2011-10-05 21:58:45 +0200572 log_err("fio: job file <%s> open: %s\n", filename, strerror(errno));
Jens Axboe25095e12012-03-09 17:09:55 +0100573 return ret;
Jens Axboe132159a2011-09-30 15:01:32 -0600574 }
575
576 if (fstat(fd, &sb) < 0) {
Jens Axboe25095e12012-03-09 17:09:55 +0100577 int ret = -errno;
578
Jens Axboe132159a2011-09-30 15:01:32 -0600579 log_err("fio: job file stat: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200580 close(fd);
Jens Axboe25095e12012-03-09 17:09:55 +0100581 return ret;
Jens Axboe132159a2011-09-30 15:01:32 -0600582 }
583
Jens Axboe46bcd492012-03-14 11:31:21 +0100584 p_size = sb.st_size + sizeof(*pdu);
585 pdu = malloc(p_size);
586 buf = pdu->buf;
Jens Axboe132159a2011-09-30 15:01:32 -0600587
588 len = sb.st_size;
589 p = buf;
590 do {
591 ret = read(fd, p, len);
592 if (ret > 0) {
593 len -= ret;
594 if (!len)
595 break;
596 p += ret;
597 continue;
598 } else if (!ret)
599 break;
600 else if (errno == EAGAIN || errno == EINTR)
601 continue;
602 } while (1);
603
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200604 if (len) {
605 log_err("fio: failed reading job file %s\n", filename);
Jens Axboeb94cba42011-10-06 21:27:10 +0200606 close(fd);
Jens Axboec524ef72011-10-07 12:23:34 +0200607 free(buf);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200608 return 1;
609 }
610
Jens Axboe46bcd492012-03-14 11:31:21 +0100611 pdu->buf_len = __cpu_to_le32(sb.st_size);
612 pdu->client_type = cpu_to_le32(client->type);
613
Jens Axboec2cb6862012-02-23 20:56:12 +0100614 client->sent_job = 1;
Jens Axboe40c60512012-03-27 16:03:04 +0200615 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, pdu, p_size, NULL, NULL);
Jens Axboe46bcd492012-03-14 11:31:21 +0100616 free(pdu);
Jens Axboeb94cba42011-10-06 21:27:10 +0200617 close(fd);
Jens Axboe132159a2011-09-30 15:01:32 -0600618 return ret;
619}
Jens Axboe37db14f2011-09-30 17:00:42 -0600620
Jens Axboe9988ca72012-03-09 15:14:06 +0100621int fio_client_send_ini(struct fio_client *client, const char *filename)
622{
Jens Axboe25095e12012-03-09 17:09:55 +0100623 int ret;
624
625 ret = __fio_client_send_ini(client, filename);
Jens Axboe3af45202012-03-13 09:59:53 +0100626 if (!ret)
627 client->sent_job = 1;
Jens Axboe9988ca72012-03-09 15:14:06 +0100628
Jens Axboe25095e12012-03-09 17:09:55 +0100629 return ret;
Jens Axboe9988ca72012-03-09 15:14:06 +0100630}
631
Jens Axboea37f69b2011-10-01 12:24:21 -0600632int fio_clients_send_ini(const char *filename)
633{
634 struct fio_client *client;
635 struct flist_head *entry, *tmp;
636
637 flist_for_each_safe(entry, tmp, &client_list) {
638 client = flist_entry(entry, struct fio_client, list);
639
Jens Axboe14ea90e2012-08-26 17:33:34 +0200640 if (client->nr_ini_file) {
641 int i;
642
643 for (i = 0; i < client->nr_ini_file; i++) {
644 const char *ini = client->ini_file[i];
645
646 if (fio_client_send_ini(client, ini)) {
647 remove_client(client);
648 break;
649 }
650 }
651 } else if (!filename || fio_client_send_ini(client, filename))
Jens Axboe3af45202012-03-13 09:59:53 +0100652 remove_client(client);
Jens Axboea37f69b2011-10-01 12:24:21 -0600653 }
654
655 return !nr_clients;
656}
657
Jens Axboe40c60512012-03-27 16:03:04 +0200658int fio_client_update_options(struct fio_client *client,
659 struct thread_options *o, uint64_t *tag)
660{
661 struct cmd_add_job_pdu pdu;
662
663 pdu.thread_number = cpu_to_le32(client->thread_number);
664 pdu.groupid = cpu_to_le32(client->groupid);
665 convert_thread_options_to_net(&pdu.top, o);
666
667 return fio_net_send_cmd(client->fd, FIO_NET_CMD_UPDATE_JOB, &pdu, sizeof(pdu), tag, &client->cmd_list);
668}
669
Jens Axboea64e88d2011-10-03 14:20:01 +0200670static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
671{
672 dst->max_val = le64_to_cpu(src->max_val);
673 dst->min_val = le64_to_cpu(src->min_val);
674 dst->samples = le64_to_cpu(src->samples);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200675
676 /*
677 * Floats arrive as IEEE 754 encoded uint64_t, convert back to double
678 */
679 dst->mean.u.f = fio_uint64_to_double(le64_to_cpu(dst->mean.u.i));
680 dst->S.u.f = fio_uint64_to_double(le64_to_cpu(dst->S.u.i));
Jens Axboea64e88d2011-10-03 14:20:01 +0200681}
682
683static void convert_ts(struct thread_stat *dst, struct thread_stat *src)
684{
685 int i, j;
686
Jens Axboe2f122b12012-03-15 13:10:19 +0100687 dst->error = le32_to_cpu(src->error);
688 dst->thread_number = le32_to_cpu(src->thread_number);
689 dst->groupid = le32_to_cpu(src->groupid);
690 dst->pid = le32_to_cpu(src->pid);
691 dst->members = le32_to_cpu(src->members);
Jens Axboea64e88d2011-10-03 14:20:01 +0200692
Jens Axboe298921d2012-09-24 18:47:01 +0200693 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
Jens Axboea64e88d2011-10-03 14:20:01 +0200694 convert_io_stat(&dst->clat_stat[i], &src->clat_stat[i]);
695 convert_io_stat(&dst->slat_stat[i], &src->slat_stat[i]);
696 convert_io_stat(&dst->lat_stat[i], &src->lat_stat[i]);
697 convert_io_stat(&dst->bw_stat[i], &src->bw_stat[i]);
698 }
699
700 dst->usr_time = le64_to_cpu(src->usr_time);
701 dst->sys_time = le64_to_cpu(src->sys_time);
702 dst->ctx = le64_to_cpu(src->ctx);
703 dst->minf = le64_to_cpu(src->minf);
704 dst->majf = le64_to_cpu(src->majf);
705 dst->clat_percentiles = le64_to_cpu(src->clat_percentiles);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200706
707 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
708 fio_fp64_t *fps = &src->percentile_list[i];
709 fio_fp64_t *fpd = &dst->percentile_list[i];
710
711 fpd->u.f = fio_uint64_to_double(le64_to_cpu(fps->u.i));
712 }
Jens Axboea64e88d2011-10-03 14:20:01 +0200713
714 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
715 dst->io_u_map[i] = le32_to_cpu(src->io_u_map[i]);
716 dst->io_u_submit[i] = le32_to_cpu(src->io_u_submit[i]);
717 dst->io_u_complete[i] = le32_to_cpu(src->io_u_complete[i]);
718 }
719
720 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
721 dst->io_u_lat_u[i] = le32_to_cpu(src->io_u_lat_u[i]);
722 dst->io_u_lat_m[i] = le32_to_cpu(src->io_u_lat_m[i]);
723 }
724
Jens Axboe298921d2012-09-24 18:47:01 +0200725 for (i = 0; i < DDIR_RWDIR_CNT; i++)
Jens Axboea64e88d2011-10-03 14:20:01 +0200726 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
727 dst->io_u_plat[i][j] = le32_to_cpu(src->io_u_plat[i][j]);
728
729 for (i = 0; i < 3; i++) {
730 dst->total_io_u[i] = le64_to_cpu(src->total_io_u[i]);
Jens Axboe93eee042011-10-03 21:08:48 +0200731 dst->short_io_u[i] = le64_to_cpu(src->short_io_u[i]);
Jens Axboea64e88d2011-10-03 14:20:01 +0200732 }
733
734 dst->total_submit = le64_to_cpu(src->total_submit);
735 dst->total_complete = le64_to_cpu(src->total_complete);
736
Jens Axboe298921d2012-09-24 18:47:01 +0200737 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
Jens Axboea64e88d2011-10-03 14:20:01 +0200738 dst->io_bytes[i] = le64_to_cpu(src->io_bytes[i]);
739 dst->runtime[i] = le64_to_cpu(src->runtime[i]);
740 }
741
742 dst->total_run_time = le64_to_cpu(src->total_run_time);
743 dst->continue_on_error = le16_to_cpu(src->continue_on_error);
744 dst->total_err_count = le64_to_cpu(src->total_err_count);
Jens Axboeddcc0b62011-10-03 14:45:27 +0200745 dst->first_error = le32_to_cpu(src->first_error);
746 dst->kb_base = le32_to_cpu(src->kb_base);
Jens Axboea64e88d2011-10-03 14:20:01 +0200747}
748
749static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
750{
751 int i;
752
Jens Axboe298921d2012-09-24 18:47:01 +0200753 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
Jens Axboea64e88d2011-10-03 14:20:01 +0200754 dst->max_run[i] = le64_to_cpu(src->max_run[i]);
755 dst->min_run[i] = le64_to_cpu(src->min_run[i]);
756 dst->max_bw[i] = le64_to_cpu(src->max_bw[i]);
757 dst->min_bw[i] = le64_to_cpu(src->min_bw[i]);
758 dst->io_kb[i] = le64_to_cpu(src->io_kb[i]);
759 dst->agg[i] = le64_to_cpu(src->agg[i]);
760 }
761
762 dst->kb_base = le32_to_cpu(src->kb_base);
763 dst->groupid = le32_to_cpu(src->groupid);
764}
765
Jens Axboe89e5fad2012-03-05 09:21:12 +0100766static void handle_ts(struct fio_client *client, struct fio_net_cmd *cmd)
Jens Axboea64e88d2011-10-03 14:20:01 +0200767{
768 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
769
Jens Axboea64e88d2011-10-03 14:20:01 +0200770 show_thread_status(&p->ts, &p->rs);
Jens Axboe37f0c1a2011-10-11 14:08:33 +0200771
772 if (sum_stat_clients == 1)
773 return;
774
775 sum_thread_stats(&client_ts, &p->ts, sum_stat_nr);
776 sum_group_stats(&client_gs, &p->rs);
777
778 client_ts.members++;
Jens Axboe2f122b12012-03-15 13:10:19 +0100779 client_ts.thread_number = p->ts.thread_number;
Jens Axboe37f0c1a2011-10-11 14:08:33 +0200780 client_ts.groupid = p->ts.groupid;
781
782 if (++sum_stat_nr == sum_stat_clients) {
783 strcpy(client_ts.name, "All clients");
784 show_thread_status(&client_ts, &client_gs);
785 }
Jens Axboea64e88d2011-10-03 14:20:01 +0200786}
787
Jens Axboe89e5fad2012-03-05 09:21:12 +0100788static void handle_gs(struct fio_client *client, struct fio_net_cmd *cmd)
Jens Axboea64e88d2011-10-03 14:20:01 +0200789{
790 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
791
Jens Axboea64e88d2011-10-03 14:20:01 +0200792 show_group_stats(gs);
793}
794
Jens Axboe3bf236c2012-03-03 20:35:50 +0100795static void handle_text(struct fio_client *client, struct fio_net_cmd *cmd)
796{
797 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
798 const char *buf = (const char *) pdu->buf;
799 const char *name;
800 int fio_unused ret;
801
802 name = client->name ? client->name : client->hostname;
803
804 if (!client->skip_newline)
805 fprintf(f_out, "<%s> ", name);
806 ret = fwrite(buf, pdu->buf_len, 1, f_out);
807 fflush(f_out);
808 client->skip_newline = strchr(buf, '\n') == NULL;
809}
810
Jens Axboed09a64a2011-10-13 11:38:56 +0200811static void convert_agg(struct disk_util_agg *agg)
812{
813 int i;
814
815 for (i = 0; i < 2; i++) {
816 agg->ios[i] = le32_to_cpu(agg->ios[i]);
817 agg->merges[i] = le32_to_cpu(agg->merges[i]);
818 agg->sectors[i] = le64_to_cpu(agg->sectors[i]);
819 agg->ticks[i] = le32_to_cpu(agg->ticks[i]);
820 }
821
822 agg->io_ticks = le32_to_cpu(agg->io_ticks);
823 agg->time_in_queue = le32_to_cpu(agg->time_in_queue);
824 agg->slavecount = le32_to_cpu(agg->slavecount);
Anton Blanchard823ba542011-11-07 14:16:26 +0100825 agg->max_util.u.f = fio_uint64_to_double(__le64_to_cpu(agg->max_util.u.i));
Jens Axboed09a64a2011-10-13 11:38:56 +0200826}
827
828static void convert_dus(struct disk_util_stat *dus)
829{
830 int i;
831
832 for (i = 0; i < 2; i++) {
833 dus->ios[i] = le32_to_cpu(dus->ios[i]);
834 dus->merges[i] = le32_to_cpu(dus->merges[i]);
835 dus->sectors[i] = le64_to_cpu(dus->sectors[i]);
836 dus->ticks[i] = le32_to_cpu(dus->ticks[i]);
837 }
838
839 dus->io_ticks = le32_to_cpu(dus->io_ticks);
840 dus->time_in_queue = le32_to_cpu(dus->time_in_queue);
841 dus->msec = le64_to_cpu(dus->msec);
842}
843
844static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd)
845{
846 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
847
Jens Axboed09a64a2011-10-13 11:38:56 +0200848 if (!client->disk_stats_shown) {
849 client->disk_stats_shown = 1;
850 log_info("\nDisk stats (read/write):\n");
851 }
852
Jens Axboef3afa572012-09-17 13:34:16 +0200853 print_disk_util(&du->dus, &du->agg, output_format == FIO_OUTPUT_TERSE);
Jens Axboed09a64a2011-10-13 11:38:56 +0200854}
855
Jens Axboe3bf236c2012-03-03 20:35:50 +0100856static void convert_jobs_eta(struct jobs_eta *je)
Jens Axboecf451d12011-10-03 16:48:30 +0200857{
Jens Axboecf451d12011-10-03 16:48:30 +0200858 int i;
859
860 je->nr_running = le32_to_cpu(je->nr_running);
861 je->nr_ramp = le32_to_cpu(je->nr_ramp);
862 je->nr_pending = le32_to_cpu(je->nr_pending);
863 je->files_open = le32_to_cpu(je->files_open);
Jens Axboecf451d12011-10-03 16:48:30 +0200864
Jens Axboe298921d2012-09-24 18:47:01 +0200865 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
866 je->m_rate[i] = le32_to_cpu(je->m_rate[i]);
867 je->t_rate[i] = le32_to_cpu(je->t_rate[i]);
868 je->m_iops[i] = le32_to_cpu(je->m_iops[i]);
869 je->t_iops[i] = le32_to_cpu(je->t_iops[i]);
Jens Axboecf451d12011-10-03 16:48:30 +0200870 je->rate[i] = le32_to_cpu(je->rate[i]);
871 je->iops[i] = le32_to_cpu(je->iops[i]);
872 }
873
Jens Axboeb51eedb2011-10-09 12:13:39 +0200874 je->elapsed_sec = le64_to_cpu(je->elapsed_sec);
Jens Axboecf451d12011-10-03 16:48:30 +0200875 je->eta_sec = le64_to_cpu(je->eta_sec);
Jens Axboe8c621fb2012-03-08 12:30:48 +0100876 je->nr_threads = le32_to_cpu(je->nr_threads);
Jens Axboeb7f05eb2012-05-11 20:33:02 +0200877 je->is_pow2 = le32_to_cpu(je->is_pow2);
Jens Axboe48fbb462011-10-09 12:19:08 +0200878}
Jens Axboecf451d12011-10-03 16:48:30 +0200879
Jens Axboe3e47bd22012-02-29 13:45:02 +0100880void fio_client_sum_jobs_eta(struct jobs_eta *dst, struct jobs_eta *je)
Jens Axboe48fbb462011-10-09 12:19:08 +0200881{
Jens Axboe48fbb462011-10-09 12:19:08 +0200882 int i;
883
884 dst->nr_running += je->nr_running;
885 dst->nr_ramp += je->nr_ramp;
886 dst->nr_pending += je->nr_pending;
887 dst->files_open += je->files_open;
Jens Axboe48fbb462011-10-09 12:19:08 +0200888
Jens Axboe298921d2012-09-24 18:47:01 +0200889 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
Jens Axboe3e47bd22012-02-29 13:45:02 +0100890 dst->m_rate[i] += je->m_rate[i];
891 dst->t_rate[i] += je->t_rate[i];
892 dst->m_iops[i] += je->m_iops[i];
893 dst->t_iops[i] += je->t_iops[i];
Jens Axboe48fbb462011-10-09 12:19:08 +0200894 dst->rate[i] += je->rate[i];
895 dst->iops[i] += je->iops[i];
896 }
897
898 dst->elapsed_sec += je->elapsed_sec;
899
900 if (je->eta_sec > dst->eta_sec)
901 dst->eta_sec = je->eta_sec;
Jens Axboe8c621fb2012-03-08 12:30:48 +0100902
903 dst->nr_threads += je->nr_threads;
904 /* we need to handle je->run_str too ... */
Jens Axboe48fbb462011-10-09 12:19:08 +0200905}
906
Jens Axboea5276612012-03-04 15:15:08 +0100907void fio_client_dec_jobs_eta(struct client_eta *eta, client_eta_op eta_fn)
Jens Axboe82c1ed32011-10-10 08:56:18 +0200908{
909 if (!--eta->pending) {
Jens Axboea5276612012-03-04 15:15:08 +0100910 eta_fn(&eta->eta);
Jens Axboe82c1ed32011-10-10 08:56:18 +0200911 free(eta);
912 }
913}
914
Jens Axboe89c17072011-10-11 10:15:51 +0200915static void remove_reply_cmd(struct fio_client *client, struct fio_net_cmd *cmd)
916{
Jens Axboe40c60512012-03-27 16:03:04 +0200917 struct fio_net_cmd_reply *reply = NULL;
Jens Axboe89c17072011-10-11 10:15:51 +0200918 struct flist_head *entry;
919
920 flist_for_each(entry, &client->cmd_list) {
Jens Axboe40c60512012-03-27 16:03:04 +0200921 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
Jens Axboe89c17072011-10-11 10:15:51 +0200922
Jens Axboe40c60512012-03-27 16:03:04 +0200923 if (cmd->tag == (uintptr_t) reply)
Jens Axboe89c17072011-10-11 10:15:51 +0200924 break;
925
Jens Axboe40c60512012-03-27 16:03:04 +0200926 reply = NULL;
Jens Axboe89c17072011-10-11 10:15:51 +0200927 }
928
Jens Axboe40c60512012-03-27 16:03:04 +0200929 if (!reply) {
930 log_err("fio: client: unable to find matching tag (%lx)\n", cmd->tag);
Jens Axboe89c17072011-10-11 10:15:51 +0200931 return;
932 }
933
Jens Axboe40c60512012-03-27 16:03:04 +0200934 flist_del(&reply->list);
935 cmd->tag = reply->saved_tag;
936 free(reply);
937}
938
939int fio_client_wait_for_reply(struct fio_client *client, uint64_t tag)
940{
941 do {
942 struct fio_net_cmd_reply *reply = NULL;
943 struct flist_head *entry;
944
945 flist_for_each(entry, &client->cmd_list) {
946 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
947
948 if (tag == (uintptr_t) reply)
949 break;
950
951 reply = NULL;
952 }
953
954 if (!reply)
955 break;
956
957 usleep(1000);
958 } while (1);
959
960 return 0;
Jens Axboe89c17072011-10-11 10:15:51 +0200961}
962
Jens Axboe82c1ed32011-10-10 08:56:18 +0200963static void handle_eta(struct fio_client *client, struct fio_net_cmd *cmd)
Jens Axboe48fbb462011-10-09 12:19:08 +0200964{
965 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
Jens Axboedf380932011-10-11 14:25:08 +0200966 struct client_eta *eta = (struct client_eta *) (uintptr_t) cmd->tag;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200967
968 dprint(FD_NET, "client: got eta tag %p, %d\n", eta, eta->pending);
Jens Axboe48fbb462011-10-09 12:19:08 +0200969
Jens Axboef77d2672011-10-10 14:36:07 +0200970 assert(client->eta_in_flight == eta);
971
972 client->eta_in_flight = NULL;
Jens Axboe82c1ed32011-10-10 08:56:18 +0200973 flist_del_init(&client->eta_list);
974
Jens Axboe2f99deb2012-03-09 14:37:29 +0100975 if (client->ops->jobs_eta)
976 client->ops->jobs_eta(client, je);
977
Jens Axboe3e47bd22012-02-29 13:45:02 +0100978 fio_client_sum_jobs_eta(&eta->eta, je);
Jens Axboea5276612012-03-04 15:15:08 +0100979 fio_client_dec_jobs_eta(eta, client->ops->eta);
Jens Axboecf451d12011-10-03 16:48:30 +0200980}
981
Jens Axboeb5296dd2011-10-07 16:35:56 +0200982static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd)
Jens Axboe2e03b4b2011-10-04 09:00:40 +0200983{
984 struct cmd_probe_pdu *probe = (struct cmd_probe_pdu *) cmd->payload;
Jens Axboed2333352011-10-11 15:07:23 +0200985 const char *os, *arch;
986 char bit[16];
Jens Axboe2e03b4b2011-10-04 09:00:40 +0200987
Jens Axboecca84642011-10-07 12:47:57 +0200988 os = fio_get_os_string(probe->os);
989 if (!os)
990 os = "unknown";
991
992 arch = fio_get_arch_string(probe->arch);
993 if (!arch)
994 os = "unknown";
995
Jens Axboed2333352011-10-11 15:07:23 +0200996 sprintf(bit, "%d-bit", probe->bpp * 8);
Jens Axboe38fdef22011-10-11 14:30:06 +0200997
Jens Axboe80295422012-04-16 09:42:53 +0200998 log_info("hostname=%s, be=%u, %s, os=%s, arch=%s, fio=%s\n",
Jens Axboe38fdef22011-10-11 14:30:06 +0200999 probe->hostname, probe->bigendian, bit, os, arch,
Jens Axboe80295422012-04-16 09:42:53 +02001000 probe->fio_version);
Jens Axboeb5296dd2011-10-07 16:35:56 +02001001
1002 if (!client->name)
1003 client->name = strdup((char *) probe->hostname);
Jens Axboe2e03b4b2011-10-04 09:00:40 +02001004}
1005
Jens Axboe11e950b2011-10-16 21:34:14 +02001006static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd)
1007{
1008 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
1009
1010 client->state = Client_started;
Jens Axboe85dd01e2012-03-12 14:33:16 +01001011 client->jobs = pdu->jobs;
Jens Axboe11e950b2011-10-16 21:34:14 +02001012}
1013
1014static void handle_stop(struct fio_client *client, struct fio_net_cmd *cmd)
1015{
Jens Axboe498c92c2011-10-17 09:14:42 +02001016 if (client->error)
1017 log_info("client <%s>: exited with error %d\n", client->hostname, client->error);
Jens Axboe11e950b2011-10-16 21:34:14 +02001018}
1019
Jens Axboe6b79c802012-03-08 10:51:36 +01001020static void convert_stop(struct fio_net_cmd *cmd)
1021{
1022 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
1023
1024 pdu->error = le32_to_cpu(pdu->error);
1025}
1026
Jens Axboe084d1c62012-03-03 20:28:07 +01001027static void convert_text(struct fio_net_cmd *cmd)
1028{
1029 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
1030
1031 pdu->level = le32_to_cpu(pdu->level);
1032 pdu->buf_len = le32_to_cpu(pdu->buf_len);
1033 pdu->log_sec = le64_to_cpu(pdu->log_sec);
1034 pdu->log_usec = le64_to_cpu(pdu->log_usec);
1035}
1036
Jens Axboe1b427252012-03-14 15:03:03 +01001037/*
1038 * This has been compressed on the server side, since it can be big.
1039 * Uncompress here.
1040 */
1041static struct cmd_iolog_pdu *convert_iolog(struct fio_net_cmd *cmd)
1042{
1043 struct cmd_iolog_pdu *pdu = (struct cmd_iolog_pdu *) cmd->payload;
1044 struct cmd_iolog_pdu *ret;
1045 uint32_t nr_samples;
1046 unsigned long total;
1047 z_stream stream;
1048 void *p;
Jens Axboef5ed7652012-03-14 16:28:07 +01001049 int i;
Jens Axboe1b427252012-03-14 15:03:03 +01001050
1051 stream.zalloc = Z_NULL;
1052 stream.zfree = Z_NULL;
1053 stream.opaque = Z_NULL;
1054 stream.avail_in = 0;
1055 stream.next_in = Z_NULL;
1056
1057 if (inflateInit(&stream) != Z_OK)
1058 return NULL;
1059
1060 /*
Jens Axboef5ed7652012-03-14 16:28:07 +01001061 * Get header first, it's not compressed
Jens Axboe1b427252012-03-14 15:03:03 +01001062 */
1063 nr_samples = le32_to_cpu(pdu->nr_samples);
1064
Jens Axboef5ed7652012-03-14 16:28:07 +01001065 total = nr_samples * sizeof(struct io_sample);
1066 ret = malloc(total + sizeof(*pdu));
Jens Axboe2f122b12012-03-15 13:10:19 +01001067 ret->thread_number = le32_to_cpu(pdu->thread_number);
Jens Axboe1b427252012-03-14 15:03:03 +01001068 ret->nr_samples = nr_samples;
Jens Axboef5ed7652012-03-14 16:28:07 +01001069 ret->log_type = le32_to_cpu(pdu->log_type);
1070 strcpy((char *) ret->name, (char *) pdu->name);
Jens Axboe1b427252012-03-14 15:03:03 +01001071
Jens Axboef5ed7652012-03-14 16:28:07 +01001072 p = (void *) ret + sizeof(*pdu);
1073
1074 stream.avail_in = cmd->pdu_len - sizeof(*pdu);
1075 stream.next_in = (void *) pdu + sizeof(*pdu);
Jens Axboe1b427252012-03-14 15:03:03 +01001076 while (stream.avail_in) {
1077 unsigned int this_chunk = 65536;
1078 unsigned int this_len;
1079 int err;
1080
1081 if (this_chunk > total)
1082 this_chunk = total;
1083
1084 stream.avail_out = this_chunk;
1085 stream.next_out = p;
1086 err = inflate(&stream, Z_NO_FLUSH);
Jens Axboe3c547fe2012-03-14 21:53:00 +01001087 /* may be Z_OK, or Z_STREAM_END */
1088 if (err < 0) {
Jens Axboe1b427252012-03-14 15:03:03 +01001089 log_err("fio: inflate error %d\n", err);
Jens Axboef5ed7652012-03-14 16:28:07 +01001090 free(ret);
1091 ret = NULL;
Jens Axboe1b427252012-03-14 15:03:03 +01001092 goto out;
1093 }
1094
1095 this_len = this_chunk - stream.avail_out;
1096 p += this_len;
1097 total -= this_len;
1098 }
1099
Jens Axboef5ed7652012-03-14 16:28:07 +01001100 for (i = 0; i < ret->nr_samples; i++) {
1101 struct io_sample *s = &ret->samples[i];
1102
1103 s->time = le64_to_cpu(s->time);
1104 s->val = le64_to_cpu(s->val);
1105 s->ddir = le32_to_cpu(s->ddir);
1106 s->bs = le32_to_cpu(s->bs);
1107 }
1108
Jens Axboe1b427252012-03-14 15:03:03 +01001109out:
1110 inflateEnd(&stream);
1111 return ret;
1112}
1113
Jens Axboea5276612012-03-04 15:15:08 +01001114int fio_handle_client(struct fio_client *client)
Jens Axboe37db14f2011-09-30 17:00:42 -06001115{
Jens Axboea5276612012-03-04 15:15:08 +01001116 struct client_ops *ops = client->ops;
Jens Axboe37db14f2011-09-30 17:00:42 -06001117 struct fio_net_cmd *cmd;
1118
Jens Axboe60efd142011-10-04 13:30:11 +02001119 dprint(FD_NET, "client: handle %s\n", client->hostname);
1120
Jens Axboee951bdc2011-10-05 21:58:45 +02001121 cmd = fio_net_recv_cmd(client->fd);
1122 if (!cmd)
1123 return 0;
Jens Axboec2c94582011-10-05 20:31:30 +02001124
Jens Axboeb9d2f302012-03-08 20:36:28 +01001125 dprint(FD_NET, "client: got cmd op %s from %s (pdu=%u)\n",
1126 fio_server_op(cmd->opcode), client->hostname, cmd->pdu_len);
Jens Axboe46c48f12011-10-01 12:50:51 -06001127
Jens Axboee951bdc2011-10-05 21:58:45 +02001128 switch (cmd->opcode) {
1129 case FIO_NET_CMD_QUIT:
Jens Axboe3ec62ec2012-03-01 12:01:29 +01001130 if (ops->quit)
Jens Axboe35c0ba72012-03-14 10:56:40 +01001131 ops->quit(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +02001132 remove_client(client);
1133 free(cmd);
1134 break;
Jens Axboe084d1c62012-03-03 20:28:07 +01001135 case FIO_NET_CMD_TEXT:
1136 convert_text(cmd);
Jens Axboe35c0ba72012-03-14 10:56:40 +01001137 ops->text(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +02001138 free(cmd);
1139 break;
Jens Axboe3bf236c2012-03-03 20:35:50 +01001140 case FIO_NET_CMD_DU: {
1141 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
1142
1143 convert_dus(&du->dus);
1144 convert_agg(&du->agg);
1145
Stephen M. Camerondd366722012-02-24 08:17:30 +01001146 ops->disk_util(client, cmd);
Jens Axboed09a64a2011-10-13 11:38:56 +02001147 free(cmd);
1148 break;
Jens Axboe3bf236c2012-03-03 20:35:50 +01001149 }
1150 case FIO_NET_CMD_TS: {
1151 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
1152
1153 convert_ts(&p->ts, &p->ts);
1154 convert_gs(&p->rs, &p->rs);
1155
Jens Axboe89e5fad2012-03-05 09:21:12 +01001156 ops->thread_status(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +02001157 free(cmd);
1158 break;
Jens Axboe3bf236c2012-03-03 20:35:50 +01001159 }
1160 case FIO_NET_CMD_GS: {
1161 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
1162
1163 convert_gs(gs, gs);
1164
Jens Axboe89e5fad2012-03-05 09:21:12 +01001165 ops->group_stats(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +02001166 free(cmd);
1167 break;
Jens Axboe3bf236c2012-03-03 20:35:50 +01001168 }
1169 case FIO_NET_CMD_ETA: {
1170 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
1171
Jens Axboe89c17072011-10-11 10:15:51 +02001172 remove_reply_cmd(client, cmd);
Jens Axboe3bf236c2012-03-03 20:35:50 +01001173 convert_jobs_eta(je);
Jens Axboea5276612012-03-04 15:15:08 +01001174 handle_eta(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +02001175 free(cmd);
1176 break;
Jens Axboe3bf236c2012-03-03 20:35:50 +01001177 }
Jens Axboee951bdc2011-10-05 21:58:45 +02001178 case FIO_NET_CMD_PROBE:
Jens Axboe89c17072011-10-11 10:15:51 +02001179 remove_reply_cmd(client, cmd);
Stephen M. Camerondd366722012-02-24 08:17:30 +01001180 ops->probe(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +02001181 free(cmd);
1182 break;
Jens Axboe5d7793a2012-03-08 19:59:16 +01001183 case FIO_NET_CMD_SERVER_START:
Jens Axboe01be0382011-10-15 14:43:41 +02001184 client->state = Client_running;
Jens Axboe85dd01e2012-03-12 14:33:16 +01001185 if (ops->job_start)
1186 ops->job_start(client, cmd);
Jens Axboe01be0382011-10-15 14:43:41 +02001187 free(cmd);
1188 break;
Jens Axboe85dd01e2012-03-12 14:33:16 +01001189 case FIO_NET_CMD_START: {
1190 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
1191
1192 pdu->jobs = le32_to_cpu(pdu->jobs);
1193 ops->start(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +02001194 free(cmd);
1195 break;
Jens Axboe85dd01e2012-03-12 14:33:16 +01001196 }
Jens Axboe6b79c802012-03-08 10:51:36 +01001197 case FIO_NET_CMD_STOP: {
1198 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
1199
1200 convert_stop(cmd);
1201 client->state = Client_stopped;
Jens Axboe122c7722012-03-19 09:13:15 +01001202 client->error = le32_to_cpu(pdu->error);
1203 client->signal = le32_to_cpu(pdu->signal);
Jens Axboe6b79c802012-03-08 10:51:36 +01001204 ops->stop(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +02001205 free(cmd);
1206 break;
Jens Axboe6b79c802012-03-08 10:51:36 +01001207 }
Jens Axboe40c60512012-03-27 16:03:04 +02001208 case FIO_NET_CMD_ADD_JOB: {
1209 struct cmd_add_job_pdu *pdu = (struct cmd_add_job_pdu *) cmd->payload;
1210
1211 client->thread_number = le32_to_cpu(pdu->thread_number);
1212 client->groupid = le32_to_cpu(pdu->groupid);
1213
Jens Axboe807f9972012-03-02 10:25:24 +01001214 if (ops->add_job)
1215 ops->add_job(client, cmd);
1216 free(cmd);
1217 break;
Jens Axboe40c60512012-03-27 16:03:04 +02001218 }
Jens Axboe1b427252012-03-14 15:03:03 +01001219 case FIO_NET_CMD_IOLOG:
1220 if (ops->iolog) {
1221 struct cmd_iolog_pdu *pdu;
1222
1223 pdu = convert_iolog(cmd);
1224 ops->iolog(client, pdu);
1225 }
1226 free(cmd);
1227 break;
Jens Axboe40c60512012-03-27 16:03:04 +02001228 case FIO_NET_CMD_UPDATE_JOB:
Jens Axboe40c60512012-03-27 16:03:04 +02001229 ops->update_job(client, cmd);
Jens Axboe649cee92012-03-28 09:15:32 +02001230 remove_reply_cmd(client, cmd);
Jens Axboe40c60512012-03-27 16:03:04 +02001231 free(cmd);
1232 break;
Jens Axboee951bdc2011-10-05 21:58:45 +02001233 default:
Jens Axboe89c17072011-10-11 10:15:51 +02001234 log_err("fio: unknown client op: %s\n", fio_server_op(cmd->opcode));
Jens Axboee951bdc2011-10-05 21:58:45 +02001235 free(cmd);
1236 break;
Jens Axboe37db14f2011-09-30 17:00:42 -06001237 }
1238
Jens Axboee951bdc2011-10-05 21:58:45 +02001239 return 1;
Jens Axboe37db14f2011-09-30 17:00:42 -06001240}
Jens Axboeb66570d2011-10-01 11:11:35 -06001241
Jens Axboea5276612012-03-04 15:15:08 +01001242static void request_client_etas(struct client_ops *ops)
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001243{
1244 struct fio_client *client;
1245 struct flist_head *entry;
1246 struct client_eta *eta;
Jens Axboe82c1ed32011-10-10 08:56:18 +02001247 int skipped = 0;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001248
1249 dprint(FD_NET, "client: request eta (%d)\n", nr_clients);
1250
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001251 eta = malloc(sizeof(*eta));
1252 memset(&eta->eta, 0, sizeof(eta->eta));
1253 eta->pending = nr_clients;
1254
1255 flist_for_each(entry, &client_list) {
1256 client = flist_entry(entry, struct fio_client, list);
1257
Jens Axboe82c1ed32011-10-10 08:56:18 +02001258 if (!flist_empty(&client->eta_list)) {
1259 skipped++;
1260 continue;
1261 }
Jens Axboe01be0382011-10-15 14:43:41 +02001262 if (client->state != Client_running)
1263 continue;
Jens Axboe82c1ed32011-10-10 08:56:18 +02001264
Jens Axboef77d2672011-10-10 14:36:07 +02001265 assert(!client->eta_in_flight);
Jens Axboe82c1ed32011-10-10 08:56:18 +02001266 flist_add_tail(&client->eta_list, &eta_list);
Jens Axboef77d2672011-10-10 14:36:07 +02001267 client->eta_in_flight = eta;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001268 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_SEND_ETA,
Jens Axboedf380932011-10-11 14:25:08 +02001269 (uintptr_t) eta, &client->cmd_list);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001270 }
1271
Jens Axboe82c1ed32011-10-10 08:56:18 +02001272 while (skipped--)
Jens Axboea5276612012-03-04 15:15:08 +01001273 fio_client_dec_jobs_eta(eta, ops->eta);
Jens Axboe82c1ed32011-10-10 08:56:18 +02001274
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001275 dprint(FD_NET, "client: requested eta tag %p\n", eta);
1276}
1277
Jens Axboe89c17072011-10-11 10:15:51 +02001278static int client_check_cmd_timeout(struct fio_client *client,
1279 struct timeval *now)
1280{
Jens Axboe40c60512012-03-27 16:03:04 +02001281 struct fio_net_cmd_reply *reply;
Jens Axboe89c17072011-10-11 10:15:51 +02001282 struct flist_head *entry, *tmp;
1283 int ret = 0;
1284
1285 flist_for_each_safe(entry, tmp, &client->cmd_list) {
Jens Axboe40c60512012-03-27 16:03:04 +02001286 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
Jens Axboe89c17072011-10-11 10:15:51 +02001287
Jens Axboe40c60512012-03-27 16:03:04 +02001288 if (mtime_since(&reply->tv, now) < FIO_NET_CLIENT_TIMEOUT)
Jens Axboe89c17072011-10-11 10:15:51 +02001289 continue;
1290
1291 log_err("fio: client %s, timeout on cmd %s\n", client->hostname,
Jens Axboe40c60512012-03-27 16:03:04 +02001292 fio_server_op(reply->opcode));
1293 flist_del(&reply->list);
1294 free(reply);
Jens Axboe89c17072011-10-11 10:15:51 +02001295 ret = 1;
1296 }
1297
1298 return flist_empty(&client->cmd_list) && ret;
1299}
1300
Jens Axboea5276612012-03-04 15:15:08 +01001301static int fio_check_clients_timed_out(void)
Jens Axboe89c17072011-10-11 10:15:51 +02001302{
1303 struct fio_client *client;
1304 struct flist_head *entry, *tmp;
1305 struct timeval tv;
1306 int ret = 0;
1307
1308 gettimeofday(&tv, NULL);
1309
1310 flist_for_each_safe(entry, tmp, &client_list) {
1311 client = flist_entry(entry, struct fio_client, list);
1312
1313 if (flist_empty(&client->cmd_list))
1314 continue;
1315
1316 if (!client_check_cmd_timeout(client, &tv))
1317 continue;
1318
Jens Axboea5276612012-03-04 15:15:08 +01001319 if (client->ops->timed_out)
1320 client->ops->timed_out(client);
Jens Axboeed727a42012-03-02 12:14:40 +01001321 else
1322 log_err("fio: client %s timed out\n", client->hostname);
1323
Jens Axboe89c17072011-10-11 10:15:51 +02001324 remove_client(client);
1325 ret = 1;
1326 }
1327
1328 return ret;
1329}
1330
Stephen M. Camerondd366722012-02-24 08:17:30 +01001331int fio_handle_clients(struct client_ops *ops)
Jens Axboeb66570d2011-10-01 11:11:35 -06001332{
Jens Axboeb66570d2011-10-01 11:11:35 -06001333 struct pollfd *pfds;
Jens Axboe498c92c2011-10-17 09:14:42 +02001334 int i, ret = 0, retval = 0;
Jens Axboeb66570d2011-10-01 11:11:35 -06001335
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001336 gettimeofday(&eta_tv, NULL);
1337
Jens Axboeb66570d2011-10-01 11:11:35 -06001338 pfds = malloc(nr_clients * sizeof(struct pollfd));
1339
Jens Axboe37f0c1a2011-10-11 14:08:33 +02001340 sum_stat_clients = nr_clients;
1341 init_thread_stat(&client_ts);
1342 init_group_run_stat(&client_gs);
1343
Jens Axboeb66570d2011-10-01 11:11:35 -06001344 while (!exit_backend && nr_clients) {
Jens Axboec2cb6862012-02-23 20:56:12 +01001345 struct flist_head *entry, *tmp;
1346 struct fio_client *client;
1347
Jens Axboe82a4be12011-10-01 12:40:32 -06001348 i = 0;
Jens Axboec2cb6862012-02-23 20:56:12 +01001349 flist_for_each_safe(entry, tmp, &client_list) {
Jens Axboe82a4be12011-10-01 12:40:32 -06001350 client = flist_entry(entry, struct fio_client, list);
1351
Jens Axboea5276612012-03-04 15:15:08 +01001352 if (!client->sent_job && !client->ops->stay_connected &&
Jens Axboec2cb6862012-02-23 20:56:12 +01001353 flist_empty(&client->cmd_list)) {
1354 remove_client(client);
1355 continue;
1356 }
1357
Jens Axboe82a4be12011-10-01 12:40:32 -06001358 pfds[i].fd = client->fd;
1359 pfds[i].events = POLLIN;
1360 i++;
1361 }
1362
Jens Axboec2cb6862012-02-23 20:56:12 +01001363 if (!nr_clients)
1364 break;
1365
Jens Axboe82a4be12011-10-01 12:40:32 -06001366 assert(i == nr_clients);
1367
Jens Axboe5c2857f2011-10-04 13:14:32 +02001368 do {
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001369 struct timeval tv;
1370
1371 gettimeofday(&tv, NULL);
Jens Axboe6433ee02012-03-09 20:10:51 +01001372 if (mtime_since(&eta_tv, &tv) >= ops->eta_msec) {
Jens Axboea5276612012-03-04 15:15:08 +01001373 request_client_etas(ops);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001374 memcpy(&eta_tv, &tv, sizeof(tv));
Jens Axboe89c17072011-10-11 10:15:51 +02001375
Jens Axboea5276612012-03-04 15:15:08 +01001376 if (fio_check_clients_timed_out())
Jens Axboe89c17072011-10-11 10:15:51 +02001377 break;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001378 }
1379
Jens Axboe80102c82012-03-23 13:19:31 +01001380 ret = poll(pfds, nr_clients, ops->eta_msec);
Jens Axboe5c2857f2011-10-04 13:14:32 +02001381 if (ret < 0) {
1382 if (errno == EINTR)
1383 continue;
1384 log_err("fio: poll clients: %s\n", strerror(errno));
1385 break;
1386 } else if (!ret)
Jens Axboeb66570d2011-10-01 11:11:35 -06001387 continue;
Jens Axboe5c2857f2011-10-04 13:14:32 +02001388 } while (ret <= 0);
Jens Axboeb66570d2011-10-01 11:11:35 -06001389
1390 for (i = 0; i < nr_clients; i++) {
1391 if (!(pfds[i].revents & POLLIN))
1392 continue;
1393
1394 client = find_client_by_fd(pfds[i].fd);
1395 if (!client) {
Jens Axboe3c5f57e2011-10-06 12:37:50 +02001396 log_err("fio: unknown client fd %d\n", pfds[i].fd);
Jens Axboeb66570d2011-10-01 11:11:35 -06001397 continue;
1398 }
Jens Axboea5276612012-03-04 15:15:08 +01001399 if (!fio_handle_client(client)) {
Jens Axboe28d3ab02011-10-05 20:45:37 +02001400 log_info("client: host=%s disconnected\n",
1401 client->hostname);
1402 remove_client(client);
Jens Axboe498c92c2011-10-17 09:14:42 +02001403 retval = 1;
Jens Axboe38990762011-10-17 13:31:33 +02001404 } else if (client->error)
Jens Axboe498c92c2011-10-17 09:14:42 +02001405 retval = 1;
Jens Axboe343cb4a2012-03-09 17:16:51 +01001406 fio_put_client(client);
Jens Axboeb66570d2011-10-01 11:11:35 -06001407 }
1408 }
1409
1410 free(pfds);
Jens Axboe498c92c2011-10-17 09:14:42 +02001411 return retval;
Jens Axboeb66570d2011-10-01 11:11:35 -06001412}