blob: a65cbcba9d505795f76eb72ce2ad958c87569c97 [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 Axboe132159a2011-09-30 15:01:32 -060017
18#include "fio.h"
Stephen M. Camerondd366722012-02-24 08:17:30 +010019#include "client.h"
Jens Axboe132159a2011-09-30 15:01:32 -060020#include "server.h"
Jens Axboeb66570d2011-10-01 11:11:35 -060021#include "flist.h"
Jens Axboe3c5f57e2011-10-06 12:37:50 +020022#include "hash.h"
Jens Axboe132159a2011-09-30 15:01:32 -060023
Stephen M. Camerondd366722012-02-24 08:17:30 +010024static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd);
Jens Axboe89e5fad2012-03-05 09:21:12 +010025static void handle_ts(struct fio_client *client, struct fio_net_cmd *cmd);
26static void handle_gs(struct fio_client *client, struct fio_net_cmd *cmd);
Stephen M. Camerondd366722012-02-24 08:17:30 +010027static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd);
Jens Axboe084d1c62012-03-03 20:28:07 +010028static void handle_text(struct fio_client *client, struct fio_net_cmd *cmd);
Stephen M. Camerondd366722012-02-24 08:17:30 +010029
30struct client_ops fio_client_ops = {
Jens Axboe084d1c62012-03-03 20:28:07 +010031 .text_op = handle_text,
Jens Axboe0420ba62012-02-29 11:16:52 +010032 .disk_util = handle_du,
33 .thread_status = handle_ts,
34 .group_stats = handle_gs,
Jens Axboea5276612012-03-04 15:15:08 +010035 .eta = display_thread_status,
Jens Axboe0420ba62012-02-29 11:16:52 +010036 .probe = handle_probe,
Stephen M. Camerondd366722012-02-24 08:17:30 +010037};
38
Jens Axboeaf9c9fb2011-10-09 21:54:10 +020039static struct timeval eta_tv;
Jens Axboe48fbb462011-10-09 12:19:08 +020040
Jens Axboe81179ee2011-10-04 12:42:06 +020041enum {
Jens Axboe5c2857f2011-10-04 13:14:32 +020042 Client_created = 0,
Jens Axboe81179ee2011-10-04 12:42:06 +020043 Client_connected = 1,
44 Client_started = 2,
Jens Axboe01be0382011-10-15 14:43:41 +020045 Client_running = 3,
46 Client_stopped = 4,
47 Client_exited = 5,
Jens Axboeb66570d2011-10-01 11:11:35 -060048};
49
50static FLIST_HEAD(client_list);
Jens Axboe82c1ed32011-10-10 08:56:18 +020051static FLIST_HEAD(eta_list);
Jens Axboeb66570d2011-10-01 11:11:35 -060052
Jens Axboe3f3a4542011-10-10 21:11:09 +020053static FLIST_HEAD(arg_list);
54
Jens Axboe37f0c1a2011-10-11 14:08:33 +020055static struct thread_stat client_ts;
56static struct group_run_stats client_gs;
57static int sum_stat_clients;
58static int sum_stat_nr;
59
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
96 if (client->fd == fd)
97 return client;
98 }
99
100 return NULL;
101}
102
Jens Axboeb66570d2011-10-01 11:11:35 -0600103static void remove_client(struct fio_client *client)
104{
Jens Axboe39e8e012011-10-04 13:46:08 +0200105 dprint(FD_NET, "client: removed <%s>\n", client->hostname);
Jens Axboeb66570d2011-10-01 11:11:35 -0600106 flist_del(&client->list);
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200107
Jens Axboebebe6392011-10-07 10:00:51 +0200108 fio_client_remove_hash(client);
Jens Axboe81179ee2011-10-04 12:42:06 +0200109
Jens Axboe82c1ed32011-10-10 08:56:18 +0200110 if (!flist_empty(&client->eta_list)) {
111 flist_del_init(&client->eta_list);
Jens Axboea5276612012-03-04 15:15:08 +0100112 fio_client_dec_jobs_eta(client->eta_in_flight, client->ops->eta);
Jens Axboe82c1ed32011-10-10 08:56:18 +0200113 }
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200114
Jens Axboeb66570d2011-10-01 11:11:35 -0600115 free(client->hostname);
Jens Axboe81179ee2011-10-04 12:42:06 +0200116 if (client->argv)
117 free(client->argv);
Jens Axboeb5296dd2011-10-07 16:35:56 +0200118 if (client->name)
119 free(client->name);
Jens Axboe81179ee2011-10-04 12:42:06 +0200120
Jens Axboeb66570d2011-10-01 11:11:35 -0600121 free(client);
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200122 nr_clients--;
Jens Axboe5fd0acb2011-10-11 14:20:22 +0200123 sum_stat_clients--;
Jens Axboeb66570d2011-10-01 11:11:35 -0600124}
Jens Axboe132159a2011-09-30 15:01:32 -0600125
Jens Axboefa2ea802011-10-08 21:07:29 +0200126static void __fio_client_add_cmd_option(struct fio_client *client,
127 const char *opt)
Jens Axboe81179ee2011-10-04 12:42:06 +0200128{
Jens Axboe39e8e012011-10-04 13:46:08 +0200129 int index;
130
131 index = client->argc++;
Jens Axboe81179ee2011-10-04 12:42:06 +0200132 client->argv = realloc(client->argv, sizeof(char *) * client->argc);
Jens Axboe39e8e012011-10-04 13:46:08 +0200133 client->argv[index] = strdup(opt);
134 dprint(FD_NET, "client: add cmd %d: %s\n", index, opt);
Jens Axboe81179ee2011-10-04 12:42:06 +0200135}
136
Jens Axboefa2ea802011-10-08 21:07:29 +0200137void fio_client_add_cmd_option(void *cookie, const char *opt)
Jens Axboe81179ee2011-10-04 12:42:06 +0200138{
Jens Axboebebe6392011-10-07 10:00:51 +0200139 struct fio_client *client = cookie;
Jens Axboe3f3a4542011-10-10 21:11:09 +0200140 struct flist_head *entry;
Jens Axboe81179ee2011-10-04 12:42:06 +0200141
Jens Axboebebe6392011-10-07 10:00:51 +0200142 if (!client || !opt)
Jens Axboefa2ea802011-10-08 21:07:29 +0200143 return;
Jens Axboe81179ee2011-10-04 12:42:06 +0200144
Jens Axboefa2ea802011-10-08 21:07:29 +0200145 __fio_client_add_cmd_option(client, opt);
Jens Axboe3f3a4542011-10-10 21:11:09 +0200146
147 /*
148 * Duplicate arguments to shared client group
149 */
150 flist_for_each(entry, &arg_list) {
151 client = flist_entry(entry, struct fio_client, arg_list);
152
153 __fio_client_add_cmd_option(client, opt);
154 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200155}
156
Jens Axboea5276612012-03-04 15:15:08 +0100157struct fio_client *fio_client_add_explicit(struct client_ops *ops,
158 const char *hostname, int type,
Jens Axboe3ec62ec2012-03-01 12:01:29 +0100159 int port)
160{
161 struct fio_client *client;
162
163 client = malloc(sizeof(*client));
164 memset(client, 0, sizeof(*client));
165
166 INIT_FLIST_HEAD(&client->list);
167 INIT_FLIST_HEAD(&client->hash_list);
168 INIT_FLIST_HEAD(&client->arg_list);
169 INIT_FLIST_HEAD(&client->eta_list);
170 INIT_FLIST_HEAD(&client->cmd_list);
171
172 client->hostname = strdup(hostname);
173
174 if (type == Fio_client_socket)
175 client->is_sock = 1;
176 else {
177 int ipv6;
178
179 ipv6 = type == Fio_client_ipv6;
180 if (fio_server_parse_host(hostname, &ipv6,
181 &client->addr.sin_addr,
182 &client->addr6.sin6_addr))
183 goto err;
184
185 client->port = port;
186 }
187
188 client->fd = -1;
Jens Axboea5276612012-03-04 15:15:08 +0100189 client->ops = ops;
Jens Axboe3ec62ec2012-03-01 12:01:29 +0100190
191 __fio_client_add_cmd_option(client, "fio");
192
193 flist_add(&client->list, &client_list);
194 nr_clients++;
195 dprint(FD_NET, "client: added <%s>\n", client->hostname);
196 return client;
197err:
198 free(client);
199 return NULL;
200}
201
Jens Axboea5276612012-03-04 15:15:08 +0100202int fio_client_add(struct client_ops *ops, const char *hostname, void **cookie)
Jens Axboe132159a2011-09-30 15:01:32 -0600203{
Jens Axboe3f3a4542011-10-10 21:11:09 +0200204 struct fio_client *existing = *cookie;
Jens Axboeb66570d2011-10-01 11:11:35 -0600205 struct fio_client *client;
Jens Axboe132159a2011-09-30 15:01:32 -0600206
Jens Axboe3f3a4542011-10-10 21:11:09 +0200207 if (existing) {
208 /*
209 * We always add our "exec" name as the option, hence 1
210 * means empty.
211 */
212 if (existing->argc == 1)
213 flist_add_tail(&existing->arg_list, &arg_list);
214 else {
215 while (!flist_empty(&arg_list))
216 flist_del_init(arg_list.next);
217 }
218 }
219
Jens Axboeb66570d2011-10-01 11:11:35 -0600220 client = malloc(sizeof(*client));
Jens Axboea37f69b2011-10-01 12:24:21 -0600221 memset(client, 0, sizeof(*client));
Jens Axboe81179ee2011-10-04 12:42:06 +0200222
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200223 INIT_FLIST_HEAD(&client->list);
Jens Axboebebe6392011-10-07 10:00:51 +0200224 INIT_FLIST_HEAD(&client->hash_list);
Jens Axboe3f3a4542011-10-10 21:11:09 +0200225 INIT_FLIST_HEAD(&client->arg_list);
Jens Axboe82c1ed32011-10-10 08:56:18 +0200226 INIT_FLIST_HEAD(&client->eta_list);
Jens Axboe89c17072011-10-11 10:15:51 +0200227 INIT_FLIST_HEAD(&client->cmd_list);
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200228
Jens Axboebebe6392011-10-07 10:00:51 +0200229 if (fio_server_parse_string(hostname, &client->hostname,
230 &client->is_sock, &client->port,
Jens Axboe811826b2011-10-24 09:11:50 +0200231 &client->addr.sin_addr,
232 &client->addr6.sin6_addr,
233 &client->ipv6))
Jens Axboebebe6392011-10-07 10:00:51 +0200234 return -1;
235
Jens Axboea37f69b2011-10-01 12:24:21 -0600236 client->fd = -1;
Jens Axboea5276612012-03-04 15:15:08 +0100237 client->ops = ops;
Jens Axboe81179ee2011-10-04 12:42:06 +0200238
239 __fio_client_add_cmd_option(client, "fio");
240
Jens Axboea37f69b2011-10-01 12:24:21 -0600241 flist_add(&client->list, &client_list);
242 nr_clients++;
Jens Axboebebe6392011-10-07 10:00:51 +0200243 dprint(FD_NET, "client: added <%s>\n", client->hostname);
244 *cookie = client;
245 return 0;
Jens Axboea37f69b2011-10-01 12:24:21 -0600246}
247
Jens Axboe87aa8f12011-10-06 21:24:13 +0200248static int fio_client_connect_ip(struct fio_client *client)
Jens Axboea37f69b2011-10-01 12:24:21 -0600249{
Jens Axboe811826b2011-10-24 09:11:50 +0200250 struct sockaddr *addr;
251 fio_socklen_t socklen;
252 int fd, domain;
Jens Axboe132159a2011-09-30 15:01:32 -0600253
Jens Axboe811826b2011-10-24 09:11:50 +0200254 if (client->ipv6) {
255 client->addr6.sin6_family = AF_INET6;
256 client->addr6.sin6_port = htons(client->port);
257 domain = AF_INET6;
258 addr = (struct sockaddr *) &client->addr6;
259 socklen = sizeof(client->addr6);
260 } else {
261 client->addr.sin_family = AF_INET;
262 client->addr.sin_port = htons(client->port);
263 domain = AF_INET;
264 addr = (struct sockaddr *) &client->addr;
265 socklen = sizeof(client->addr);
266 }
Jens Axboe132159a2011-09-30 15:01:32 -0600267
Jens Axboe811826b2011-10-24 09:11:50 +0200268 fd = socket(domain, SOCK_STREAM, 0);
Jens Axboe132159a2011-09-30 15:01:32 -0600269 if (fd < 0) {
270 log_err("fio: socket: %s\n", strerror(errno));
Jens Axboe87aa8f12011-10-06 21:24:13 +0200271 return -1;
Jens Axboe132159a2011-09-30 15:01:32 -0600272 }
273
Jens Axboe811826b2011-10-24 09:11:50 +0200274 if (connect(fd, addr, socklen) < 0) {
Jens Axboe132159a2011-09-30 15:01:32 -0600275 log_err("fio: connect: %s\n", strerror(errno));
Jens Axboea7de0a12011-10-07 12:55:14 +0200276 log_err("fio: failed to connect to %s:%u\n", client->hostname,
277 client->port);
Jens Axboeb94cba42011-10-06 21:27:10 +0200278 close(fd);
Jens Axboe87aa8f12011-10-06 21:24:13 +0200279 return -1;
Jens Axboe132159a2011-09-30 15:01:32 -0600280 }
281
Jens Axboe87aa8f12011-10-06 21:24:13 +0200282 return fd;
283}
284
285static int fio_client_connect_sock(struct fio_client *client)
286{
287 struct sockaddr_un *addr = &client->addr_un;
288 fio_socklen_t len;
289 int fd;
290
291 memset(addr, 0, sizeof(*addr));
292 addr->sun_family = AF_UNIX;
293 strcpy(addr->sun_path, client->hostname);
294
295 fd = socket(AF_UNIX, SOCK_STREAM, 0);
296 if (fd < 0) {
297 log_err("fio: socket: %s\n", strerror(errno));
298 return -1;
299 }
300
301 len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
302 if (connect(fd, (struct sockaddr *) addr, len) < 0) {
303 log_err("fio: connect; %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200304 close(fd);
Jens Axboe87aa8f12011-10-06 21:24:13 +0200305 return -1;
306 }
307
308 return fd;
309}
310
311static int fio_client_connect(struct fio_client *client)
312{
313 int fd;
314
315 dprint(FD_NET, "client: connect to host %s\n", client->hostname);
316
Jens Axboe87aa8f12011-10-06 21:24:13 +0200317 if (client->is_sock)
318 fd = fio_client_connect_sock(client);
319 else
320 fd = fio_client_connect_ip(client);
321
Jens Axboe89c17072011-10-11 10:15:51 +0200322 dprint(FD_NET, "client: %s connected %d\n", client->hostname, fd);
323
Jens Axboe87aa8f12011-10-06 21:24:13 +0200324 if (fd < 0)
325 return 1;
326
Jens Axboeb66570d2011-10-01 11:11:35 -0600327 client->fd = fd;
Jens Axboebebe6392011-10-07 10:00:51 +0200328 fio_client_add_hash(client);
Jens Axboe81179ee2011-10-04 12:42:06 +0200329 client->state = Client_connected;
Jens Axboe132159a2011-09-30 15:01:32 -0600330 return 0;
331}
332
Jens Axboecc0df002011-10-03 20:53:32 +0200333void fio_clients_terminate(void)
334{
335 struct flist_head *entry;
336 struct fio_client *client;
337
Jens Axboe60efd142011-10-04 13:30:11 +0200338 dprint(FD_NET, "client: terminate clients\n");
339
Jens Axboecc0df002011-10-03 20:53:32 +0200340 flist_for_each(entry, &client_list) {
341 client = flist_entry(entry, struct fio_client, list);
342
Jens Axboe89c17072011-10-11 10:15:51 +0200343 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_QUIT, 0, NULL);
Jens Axboecc0df002011-10-03 20:53:32 +0200344 }
345}
346
347static void sig_int(int sig)
348{
Jens Axboebebe6392011-10-07 10:00:51 +0200349 dprint(FD_NET, "client: got signal %d\n", sig);
Jens Axboecc0df002011-10-03 20:53:32 +0200350 fio_clients_terminate();
351}
352
353static void client_signal_handler(void)
354{
355 struct sigaction act;
356
357 memset(&act, 0, sizeof(act));
358 act.sa_handler = sig_int;
359 act.sa_flags = SA_RESTART;
360 sigaction(SIGINT, &act, NULL);
361
362 memset(&act, 0, sizeof(act));
363 act.sa_handler = sig_int;
364 act.sa_flags = SA_RESTART;
365 sigaction(SIGTERM, &act, NULL);
366}
367
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200368static void probe_client(struct fio_client *client)
369{
Jens Axboe60efd142011-10-04 13:30:11 +0200370 dprint(FD_NET, "client: send probe\n");
371
Jens Axboe89c17072011-10-11 10:15:51 +0200372 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_PROBE, 0, &client->cmd_list);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200373}
374
Jens Axboe81179ee2011-10-04 12:42:06 +0200375static int send_client_cmd_line(struct fio_client *client)
376{
Jens Axboefa2ea802011-10-08 21:07:29 +0200377 struct cmd_single_line_pdu *cslp;
378 struct cmd_line_pdu *clp;
379 unsigned long offset;
Jens Axboe7f868312011-10-10 09:55:21 +0200380 unsigned int *lens;
Jens Axboefa2ea802011-10-08 21:07:29 +0200381 void *pdu;
382 size_t mem;
Jens Axboe81179ee2011-10-04 12:42:06 +0200383 int i, ret;
384
Jens Axboe39e8e012011-10-04 13:46:08 +0200385 dprint(FD_NET, "client: send cmdline %d\n", client->argc);
Jens Axboe60efd142011-10-04 13:30:11 +0200386
Jens Axboe7f868312011-10-10 09:55:21 +0200387 lens = malloc(client->argc * sizeof(unsigned int));
388
Jens Axboefa2ea802011-10-08 21:07:29 +0200389 /*
390 * Find out how much mem we need
391 */
Jens Axboe7f868312011-10-10 09:55:21 +0200392 for (i = 0, mem = 0; i < client->argc; i++) {
393 lens[i] = strlen(client->argv[i]) + 1;
394 mem += lens[i];
395 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200396
Jens Axboefa2ea802011-10-08 21:07:29 +0200397 /*
398 * We need one cmd_line_pdu, and argc number of cmd_single_line_pdu
399 */
400 mem += sizeof(*clp) + (client->argc * sizeof(*cslp));
401
402 pdu = malloc(mem);
403 clp = pdu;
404 offset = sizeof(*clp);
405
406 for (i = 0; i < client->argc; i++) {
Jens Axboe7f868312011-10-10 09:55:21 +0200407 uint16_t arg_len = lens[i];
Jens Axboefa2ea802011-10-08 21:07:29 +0200408
409 cslp = pdu + offset;
410 strcpy((char *) cslp->text, client->argv[i]);
411 cslp->len = cpu_to_le16(arg_len);
412 offset += sizeof(*cslp) + arg_len;
413 }
414
Jens Axboe7f868312011-10-10 09:55:21 +0200415 free(lens);
Jens Axboefa2ea802011-10-08 21:07:29 +0200416 clp->lines = cpu_to_le16(client->argc);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200417 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOBLINE, pdu, mem, 0);
Jens Axboe81179ee2011-10-04 12:42:06 +0200418 free(pdu);
419 return ret;
420}
421
Jens Axboea37f69b2011-10-01 12:24:21 -0600422int fio_clients_connect(void)
423{
424 struct fio_client *client;
425 struct flist_head *entry, *tmp;
426 int ret;
427
Bruce Cran93bcfd22012-02-20 20:18:19 +0100428#ifdef WIN32
429 WSADATA wsd;
430 WSAStartup(MAKEWORD(2,2), &wsd);
431#endif
432
Jens Axboe60efd142011-10-04 13:30:11 +0200433 dprint(FD_NET, "client: connect all\n");
434
Jens Axboecc0df002011-10-03 20:53:32 +0200435 client_signal_handler();
436
Jens Axboea37f69b2011-10-01 12:24:21 -0600437 flist_for_each_safe(entry, tmp, &client_list) {
438 client = flist_entry(entry, struct fio_client, list);
439
440 ret = fio_client_connect(client);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200441 if (ret) {
Jens Axboea37f69b2011-10-01 12:24:21 -0600442 remove_client(client);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200443 continue;
444 }
445
446 probe_client(client);
Jens Axboe81179ee2011-10-04 12:42:06 +0200447
448 if (client->argc > 1)
449 send_client_cmd_line(client);
Jens Axboea37f69b2011-10-01 12:24:21 -0600450 }
451
452 return !nr_clients;
453}
454
Jens Axboe132159a2011-09-30 15:01:32 -0600455/*
456 * Send file contents to server backend. We could use sendfile(), but to remain
457 * more portable lets just read/write the darn thing.
458 */
Jens Axboea37f69b2011-10-01 12:24:21 -0600459static int fio_client_send_ini(struct fio_client *client, const char *filename)
Jens Axboe132159a2011-09-30 15:01:32 -0600460{
461 struct stat sb;
462 char *p, *buf;
463 off_t len;
464 int fd, ret;
465
Jens Axboe46c48f12011-10-01 12:50:51 -0600466 dprint(FD_NET, "send ini %s to %s\n", filename, client->hostname);
467
Jens Axboe132159a2011-09-30 15:01:32 -0600468 fd = open(filename, O_RDONLY);
469 if (fd < 0) {
Jens Axboee951bdc2011-10-05 21:58:45 +0200470 log_err("fio: job file <%s> open: %s\n", filename, strerror(errno));
Jens Axboe132159a2011-09-30 15:01:32 -0600471 return 1;
472 }
473
474 if (fstat(fd, &sb) < 0) {
475 log_err("fio: job file stat: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200476 close(fd);
Jens Axboe132159a2011-09-30 15:01:32 -0600477 return 1;
478 }
479
480 buf = malloc(sb.st_size);
481
482 len = sb.st_size;
483 p = buf;
484 do {
485 ret = read(fd, p, len);
486 if (ret > 0) {
487 len -= ret;
488 if (!len)
489 break;
490 p += ret;
491 continue;
492 } else if (!ret)
493 break;
494 else if (errno == EAGAIN || errno == EINTR)
495 continue;
496 } while (1);
497
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200498 if (len) {
499 log_err("fio: failed reading job file %s\n", filename);
Jens Axboeb94cba42011-10-06 21:27:10 +0200500 close(fd);
Jens Axboec524ef72011-10-07 12:23:34 +0200501 free(buf);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200502 return 1;
503 }
504
Jens Axboec2cb6862012-02-23 20:56:12 +0100505 client->sent_job = 1;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200506 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, buf, sb.st_size, 0);
Jens Axboe132159a2011-09-30 15:01:32 -0600507 free(buf);
Jens Axboeb94cba42011-10-06 21:27:10 +0200508 close(fd);
Jens Axboe132159a2011-09-30 15:01:32 -0600509 return ret;
510}
Jens Axboe37db14f2011-09-30 17:00:42 -0600511
Jens Axboea37f69b2011-10-01 12:24:21 -0600512int fio_clients_send_ini(const char *filename)
513{
514 struct fio_client *client;
515 struct flist_head *entry, *tmp;
516
517 flist_for_each_safe(entry, tmp, &client_list) {
518 client = flist_entry(entry, struct fio_client, list);
519
520 if (fio_client_send_ini(client, filename))
521 remove_client(client);
Jens Axboec2cb6862012-02-23 20:56:12 +0100522
523 client->sent_job = 1;
Jens Axboea37f69b2011-10-01 12:24:21 -0600524 }
525
526 return !nr_clients;
527}
528
Jens Axboea64e88d2011-10-03 14:20:01 +0200529static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
530{
531 dst->max_val = le64_to_cpu(src->max_val);
532 dst->min_val = le64_to_cpu(src->min_val);
533 dst->samples = le64_to_cpu(src->samples);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200534
535 /*
536 * Floats arrive as IEEE 754 encoded uint64_t, convert back to double
537 */
538 dst->mean.u.f = fio_uint64_to_double(le64_to_cpu(dst->mean.u.i));
539 dst->S.u.f = fio_uint64_to_double(le64_to_cpu(dst->S.u.i));
Jens Axboea64e88d2011-10-03 14:20:01 +0200540}
541
542static void convert_ts(struct thread_stat *dst, struct thread_stat *src)
543{
544 int i, j;
545
546 dst->error = le32_to_cpu(src->error);
547 dst->groupid = le32_to_cpu(src->groupid);
548 dst->pid = le32_to_cpu(src->pid);
549 dst->members = le32_to_cpu(src->members);
550
551 for (i = 0; i < 2; i++) {
552 convert_io_stat(&dst->clat_stat[i], &src->clat_stat[i]);
553 convert_io_stat(&dst->slat_stat[i], &src->slat_stat[i]);
554 convert_io_stat(&dst->lat_stat[i], &src->lat_stat[i]);
555 convert_io_stat(&dst->bw_stat[i], &src->bw_stat[i]);
556 }
557
558 dst->usr_time = le64_to_cpu(src->usr_time);
559 dst->sys_time = le64_to_cpu(src->sys_time);
560 dst->ctx = le64_to_cpu(src->ctx);
561 dst->minf = le64_to_cpu(src->minf);
562 dst->majf = le64_to_cpu(src->majf);
563 dst->clat_percentiles = le64_to_cpu(src->clat_percentiles);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200564
565 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
566 fio_fp64_t *fps = &src->percentile_list[i];
567 fio_fp64_t *fpd = &dst->percentile_list[i];
568
569 fpd->u.f = fio_uint64_to_double(le64_to_cpu(fps->u.i));
570 }
Jens Axboea64e88d2011-10-03 14:20:01 +0200571
572 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
573 dst->io_u_map[i] = le32_to_cpu(src->io_u_map[i]);
574 dst->io_u_submit[i] = le32_to_cpu(src->io_u_submit[i]);
575 dst->io_u_complete[i] = le32_to_cpu(src->io_u_complete[i]);
576 }
577
578 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
579 dst->io_u_lat_u[i] = le32_to_cpu(src->io_u_lat_u[i]);
580 dst->io_u_lat_m[i] = le32_to_cpu(src->io_u_lat_m[i]);
581 }
582
583 for (i = 0; i < 2; i++)
584 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
585 dst->io_u_plat[i][j] = le32_to_cpu(src->io_u_plat[i][j]);
586
587 for (i = 0; i < 3; i++) {
588 dst->total_io_u[i] = le64_to_cpu(src->total_io_u[i]);
Jens Axboe93eee042011-10-03 21:08:48 +0200589 dst->short_io_u[i] = le64_to_cpu(src->short_io_u[i]);
Jens Axboea64e88d2011-10-03 14:20:01 +0200590 }
591
592 dst->total_submit = le64_to_cpu(src->total_submit);
593 dst->total_complete = le64_to_cpu(src->total_complete);
594
595 for (i = 0; i < 2; i++) {
596 dst->io_bytes[i] = le64_to_cpu(src->io_bytes[i]);
597 dst->runtime[i] = le64_to_cpu(src->runtime[i]);
598 }
599
600 dst->total_run_time = le64_to_cpu(src->total_run_time);
601 dst->continue_on_error = le16_to_cpu(src->continue_on_error);
602 dst->total_err_count = le64_to_cpu(src->total_err_count);
Jens Axboeddcc0b62011-10-03 14:45:27 +0200603 dst->first_error = le32_to_cpu(src->first_error);
604 dst->kb_base = le32_to_cpu(src->kb_base);
Jens Axboea64e88d2011-10-03 14:20:01 +0200605}
606
607static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
608{
609 int i;
610
611 for (i = 0; i < 2; i++) {
612 dst->max_run[i] = le64_to_cpu(src->max_run[i]);
613 dst->min_run[i] = le64_to_cpu(src->min_run[i]);
614 dst->max_bw[i] = le64_to_cpu(src->max_bw[i]);
615 dst->min_bw[i] = le64_to_cpu(src->min_bw[i]);
616 dst->io_kb[i] = le64_to_cpu(src->io_kb[i]);
617 dst->agg[i] = le64_to_cpu(src->agg[i]);
618 }
619
620 dst->kb_base = le32_to_cpu(src->kb_base);
621 dst->groupid = le32_to_cpu(src->groupid);
622}
623
Jens Axboe89e5fad2012-03-05 09:21:12 +0100624static void handle_ts(struct fio_client *client, struct fio_net_cmd *cmd)
Jens Axboea64e88d2011-10-03 14:20:01 +0200625{
626 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
627
Jens Axboea64e88d2011-10-03 14:20:01 +0200628 show_thread_status(&p->ts, &p->rs);
Jens Axboe37f0c1a2011-10-11 14:08:33 +0200629
630 if (sum_stat_clients == 1)
631 return;
632
633 sum_thread_stats(&client_ts, &p->ts, sum_stat_nr);
634 sum_group_stats(&client_gs, &p->rs);
635
636 client_ts.members++;
637 client_ts.groupid = p->ts.groupid;
638
639 if (++sum_stat_nr == sum_stat_clients) {
640 strcpy(client_ts.name, "All clients");
641 show_thread_status(&client_ts, &client_gs);
642 }
Jens Axboea64e88d2011-10-03 14:20:01 +0200643}
644
Jens Axboe89e5fad2012-03-05 09:21:12 +0100645static void handle_gs(struct fio_client *client, struct fio_net_cmd *cmd)
Jens Axboea64e88d2011-10-03 14:20:01 +0200646{
647 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
648
Jens Axboea64e88d2011-10-03 14:20:01 +0200649 show_group_stats(gs);
650}
651
Jens Axboe3bf236c2012-03-03 20:35:50 +0100652static void handle_text(struct fio_client *client, struct fio_net_cmd *cmd)
653{
654 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
655 const char *buf = (const char *) pdu->buf;
656 const char *name;
657 int fio_unused ret;
658
659 name = client->name ? client->name : client->hostname;
660
661 if (!client->skip_newline)
662 fprintf(f_out, "<%s> ", name);
663 ret = fwrite(buf, pdu->buf_len, 1, f_out);
664 fflush(f_out);
665 client->skip_newline = strchr(buf, '\n') == NULL;
666}
667
Jens Axboed09a64a2011-10-13 11:38:56 +0200668static void convert_agg(struct disk_util_agg *agg)
669{
670 int i;
671
672 for (i = 0; i < 2; i++) {
673 agg->ios[i] = le32_to_cpu(agg->ios[i]);
674 agg->merges[i] = le32_to_cpu(agg->merges[i]);
675 agg->sectors[i] = le64_to_cpu(agg->sectors[i]);
676 agg->ticks[i] = le32_to_cpu(agg->ticks[i]);
677 }
678
679 agg->io_ticks = le32_to_cpu(agg->io_ticks);
680 agg->time_in_queue = le32_to_cpu(agg->time_in_queue);
681 agg->slavecount = le32_to_cpu(agg->slavecount);
Anton Blanchard823ba542011-11-07 14:16:26 +0100682 agg->max_util.u.f = fio_uint64_to_double(__le64_to_cpu(agg->max_util.u.i));
Jens Axboed09a64a2011-10-13 11:38:56 +0200683}
684
685static void convert_dus(struct disk_util_stat *dus)
686{
687 int i;
688
689 for (i = 0; i < 2; i++) {
690 dus->ios[i] = le32_to_cpu(dus->ios[i]);
691 dus->merges[i] = le32_to_cpu(dus->merges[i]);
692 dus->sectors[i] = le64_to_cpu(dus->sectors[i]);
693 dus->ticks[i] = le32_to_cpu(dus->ticks[i]);
694 }
695
696 dus->io_ticks = le32_to_cpu(dus->io_ticks);
697 dus->time_in_queue = le32_to_cpu(dus->time_in_queue);
698 dus->msec = le64_to_cpu(dus->msec);
699}
700
701static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd)
702{
703 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
704
Jens Axboed09a64a2011-10-13 11:38:56 +0200705 if (!client->disk_stats_shown) {
706 client->disk_stats_shown = 1;
707 log_info("\nDisk stats (read/write):\n");
708 }
709
Jens Axboef2f788d2011-10-13 14:03:52 +0200710 print_disk_util(&du->dus, &du->agg, terse_output);
Jens Axboed09a64a2011-10-13 11:38:56 +0200711}
712
Jens Axboe3bf236c2012-03-03 20:35:50 +0100713static void convert_jobs_eta(struct jobs_eta *je)
Jens Axboecf451d12011-10-03 16:48:30 +0200714{
Jens Axboecf451d12011-10-03 16:48:30 +0200715 int i;
716
717 je->nr_running = le32_to_cpu(je->nr_running);
718 je->nr_ramp = le32_to_cpu(je->nr_ramp);
719 je->nr_pending = le32_to_cpu(je->nr_pending);
720 je->files_open = le32_to_cpu(je->files_open);
Jens Axboecf451d12011-10-03 16:48:30 +0200721
722 for (i = 0; i < 2; i++) {
Jens Axboe3e47bd22012-02-29 13:45:02 +0100723 je->m_rate[i] = le32_to_cpu(je->m_rate[i]);
724 je->t_rate[i] = le32_to_cpu(je->t_rate[i]);
725 je->m_iops[i] = le32_to_cpu(je->m_iops[i]);
726 je->t_iops[i] = le32_to_cpu(je->t_iops[i]);
Jens Axboecf451d12011-10-03 16:48:30 +0200727 je->rate[i] = le32_to_cpu(je->rate[i]);
728 je->iops[i] = le32_to_cpu(je->iops[i]);
729 }
730
Jens Axboeb51eedb2011-10-09 12:13:39 +0200731 je->elapsed_sec = le64_to_cpu(je->elapsed_sec);
Jens Axboecf451d12011-10-03 16:48:30 +0200732 je->eta_sec = le64_to_cpu(je->eta_sec);
Jens Axboe48fbb462011-10-09 12:19:08 +0200733}
Jens Axboecf451d12011-10-03 16:48:30 +0200734
Jens Axboe3e47bd22012-02-29 13:45:02 +0100735void fio_client_sum_jobs_eta(struct jobs_eta *dst, struct jobs_eta *je)
Jens Axboe48fbb462011-10-09 12:19:08 +0200736{
Jens Axboe48fbb462011-10-09 12:19:08 +0200737 int i;
738
739 dst->nr_running += je->nr_running;
740 dst->nr_ramp += je->nr_ramp;
741 dst->nr_pending += je->nr_pending;
742 dst->files_open += je->files_open;
Jens Axboe48fbb462011-10-09 12:19:08 +0200743
744 for (i = 0; i < 2; i++) {
Jens Axboe3e47bd22012-02-29 13:45:02 +0100745 dst->m_rate[i] += je->m_rate[i];
746 dst->t_rate[i] += je->t_rate[i];
747 dst->m_iops[i] += je->m_iops[i];
748 dst->t_iops[i] += je->t_iops[i];
Jens Axboe48fbb462011-10-09 12:19:08 +0200749 dst->rate[i] += je->rate[i];
750 dst->iops[i] += je->iops[i];
751 }
752
753 dst->elapsed_sec += je->elapsed_sec;
754
755 if (je->eta_sec > dst->eta_sec)
756 dst->eta_sec = je->eta_sec;
757}
758
Jens Axboea5276612012-03-04 15:15:08 +0100759void fio_client_dec_jobs_eta(struct client_eta *eta, client_eta_op eta_fn)
Jens Axboe82c1ed32011-10-10 08:56:18 +0200760{
761 if (!--eta->pending) {
Jens Axboea5276612012-03-04 15:15:08 +0100762 eta_fn(&eta->eta);
Jens Axboe82c1ed32011-10-10 08:56:18 +0200763 free(eta);
764 }
765}
766
Jens Axboe89c17072011-10-11 10:15:51 +0200767static void remove_reply_cmd(struct fio_client *client, struct fio_net_cmd *cmd)
768{
769 struct fio_net_int_cmd *icmd = NULL;
770 struct flist_head *entry;
771
772 flist_for_each(entry, &client->cmd_list) {
773 icmd = flist_entry(entry, struct fio_net_int_cmd, list);
774
Jens Axboedf380932011-10-11 14:25:08 +0200775 if (cmd->tag == (uintptr_t) icmd)
Jens Axboe89c17072011-10-11 10:15:51 +0200776 break;
777
778 icmd = NULL;
779 }
780
781 if (!icmd) {
782 log_err("fio: client: unable to find matching tag\n");
783 return;
784 }
785
786 flist_del(&icmd->list);
787 cmd->tag = icmd->saved_tag;
788 free(icmd);
789}
790
Jens Axboe82c1ed32011-10-10 08:56:18 +0200791static void handle_eta(struct fio_client *client, struct fio_net_cmd *cmd)
Jens Axboe48fbb462011-10-09 12:19:08 +0200792{
793 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
Jens Axboedf380932011-10-11 14:25:08 +0200794 struct client_eta *eta = (struct client_eta *) (uintptr_t) cmd->tag;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200795
796 dprint(FD_NET, "client: got eta tag %p, %d\n", eta, eta->pending);
Jens Axboe48fbb462011-10-09 12:19:08 +0200797
Jens Axboef77d2672011-10-10 14:36:07 +0200798 assert(client->eta_in_flight == eta);
799
800 client->eta_in_flight = NULL;
Jens Axboe82c1ed32011-10-10 08:56:18 +0200801 flist_del_init(&client->eta_list);
802
Jens Axboe3e47bd22012-02-29 13:45:02 +0100803 fio_client_sum_jobs_eta(&eta->eta, je);
Jens Axboea5276612012-03-04 15:15:08 +0100804 fio_client_dec_jobs_eta(eta, client->ops->eta);
Jens Axboecf451d12011-10-03 16:48:30 +0200805}
806
Jens Axboeb5296dd2011-10-07 16:35:56 +0200807static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd)
Jens Axboe2e03b4b2011-10-04 09:00:40 +0200808{
809 struct cmd_probe_pdu *probe = (struct cmd_probe_pdu *) cmd->payload;
Jens Axboed2333352011-10-11 15:07:23 +0200810 const char *os, *arch;
811 char bit[16];
Jens Axboe2e03b4b2011-10-04 09:00:40 +0200812
Jens Axboecca84642011-10-07 12:47:57 +0200813 os = fio_get_os_string(probe->os);
814 if (!os)
815 os = "unknown";
816
817 arch = fio_get_arch_string(probe->arch);
818 if (!arch)
819 os = "unknown";
820
Jens Axboed2333352011-10-11 15:07:23 +0200821 sprintf(bit, "%d-bit", probe->bpp * 8);
Jens Axboe38fdef22011-10-11 14:30:06 +0200822
823 log_info("hostname=%s, be=%u, %s, os=%s, arch=%s, fio=%u.%u.%u\n",
824 probe->hostname, probe->bigendian, bit, os, arch,
825 probe->fio_major, probe->fio_minor, probe->fio_patch);
Jens Axboeb5296dd2011-10-07 16:35:56 +0200826
827 if (!client->name)
828 client->name = strdup((char *) probe->hostname);
Jens Axboe2e03b4b2011-10-04 09:00:40 +0200829}
830
Jens Axboe11e950b2011-10-16 21:34:14 +0200831static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd)
832{
833 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
834
835 client->state = Client_started;
836 client->jobs = le32_to_cpu(pdu->jobs);
837}
838
839static void handle_stop(struct fio_client *client, struct fio_net_cmd *cmd)
840{
841 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
842
843 client->state = Client_stopped;
844 client->error = le32_to_cpu(pdu->error);
Jens Axboe498c92c2011-10-17 09:14:42 +0200845
846 if (client->error)
847 log_info("client <%s>: exited with error %d\n", client->hostname, client->error);
Jens Axboe11e950b2011-10-16 21:34:14 +0200848}
849
Jens Axboe084d1c62012-03-03 20:28:07 +0100850static void convert_text(struct fio_net_cmd *cmd)
851{
852 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
853
854 pdu->level = le32_to_cpu(pdu->level);
855 pdu->buf_len = le32_to_cpu(pdu->buf_len);
856 pdu->log_sec = le64_to_cpu(pdu->log_sec);
857 pdu->log_usec = le64_to_cpu(pdu->log_usec);
858}
859
Jens Axboea5276612012-03-04 15:15:08 +0100860int fio_handle_client(struct fio_client *client)
Jens Axboe37db14f2011-09-30 17:00:42 -0600861{
Jens Axboea5276612012-03-04 15:15:08 +0100862 struct client_ops *ops = client->ops;
Jens Axboe37db14f2011-09-30 17:00:42 -0600863 struct fio_net_cmd *cmd;
864
Jens Axboe60efd142011-10-04 13:30:11 +0200865 dprint(FD_NET, "client: handle %s\n", client->hostname);
866
Jens Axboee951bdc2011-10-05 21:58:45 +0200867 cmd = fio_net_recv_cmd(client->fd);
868 if (!cmd)
869 return 0;
Jens Axboec2c94582011-10-05 20:31:30 +0200870
Jens Axboe89c17072011-10-11 10:15:51 +0200871 dprint(FD_NET, "client: got cmd op %s from %s\n",
872 fio_server_op(cmd->opcode), client->hostname);
Jens Axboe46c48f12011-10-01 12:50:51 -0600873
Jens Axboee951bdc2011-10-05 21:58:45 +0200874 switch (cmd->opcode) {
875 case FIO_NET_CMD_QUIT:
Jens Axboe3ec62ec2012-03-01 12:01:29 +0100876 if (ops->quit)
877 ops->quit(client);
Jens Axboee951bdc2011-10-05 21:58:45 +0200878 remove_client(client);
879 free(cmd);
880 break;
Jens Axboe084d1c62012-03-03 20:28:07 +0100881 case FIO_NET_CMD_TEXT:
882 convert_text(cmd);
883 ops->text_op(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +0200884 free(cmd);
885 break;
Jens Axboe3bf236c2012-03-03 20:35:50 +0100886 case FIO_NET_CMD_DU: {
887 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
888
889 convert_dus(&du->dus);
890 convert_agg(&du->agg);
891
Stephen M. Camerondd366722012-02-24 08:17:30 +0100892 ops->disk_util(client, cmd);
Jens Axboed09a64a2011-10-13 11:38:56 +0200893 free(cmd);
894 break;
Jens Axboe3bf236c2012-03-03 20:35:50 +0100895 }
896 case FIO_NET_CMD_TS: {
897 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
898
899 convert_ts(&p->ts, &p->ts);
900 convert_gs(&p->rs, &p->rs);
901
Jens Axboe89e5fad2012-03-05 09:21:12 +0100902 ops->thread_status(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +0200903 free(cmd);
904 break;
Jens Axboe3bf236c2012-03-03 20:35:50 +0100905 }
906 case FIO_NET_CMD_GS: {
907 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
908
909 convert_gs(gs, gs);
910
Jens Axboe89e5fad2012-03-05 09:21:12 +0100911 ops->group_stats(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +0200912 free(cmd);
913 break;
Jens Axboe3bf236c2012-03-03 20:35:50 +0100914 }
915 case FIO_NET_CMD_ETA: {
916 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
917
Jens Axboe89c17072011-10-11 10:15:51 +0200918 remove_reply_cmd(client, cmd);
Jens Axboe3bf236c2012-03-03 20:35:50 +0100919 convert_jobs_eta(je);
Jens Axboea5276612012-03-04 15:15:08 +0100920 handle_eta(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +0200921 free(cmd);
922 break;
Jens Axboe3bf236c2012-03-03 20:35:50 +0100923 }
Jens Axboee951bdc2011-10-05 21:58:45 +0200924 case FIO_NET_CMD_PROBE:
Jens Axboe89c17072011-10-11 10:15:51 +0200925 remove_reply_cmd(client, cmd);
Stephen M. Camerondd366722012-02-24 08:17:30 +0100926 ops->probe(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +0200927 free(cmd);
928 break;
Jens Axboe01be0382011-10-15 14:43:41 +0200929 case FIO_NET_CMD_RUN:
930 client->state = Client_running;
931 free(cmd);
932 break;
Jens Axboee951bdc2011-10-05 21:58:45 +0200933 case FIO_NET_CMD_START:
Jens Axboe11e950b2011-10-16 21:34:14 +0200934 handle_start(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +0200935 free(cmd);
936 break;
937 case FIO_NET_CMD_STOP:
Jens Axboe11e950b2011-10-16 21:34:14 +0200938 handle_stop(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +0200939 free(cmd);
940 break;
Jens Axboe807f9972012-03-02 10:25:24 +0100941 case FIO_NET_CMD_ADD_JOB:
942 if (ops->add_job)
943 ops->add_job(client, cmd);
944 free(cmd);
945 break;
Jens Axboee951bdc2011-10-05 21:58:45 +0200946 default:
Jens Axboe89c17072011-10-11 10:15:51 +0200947 log_err("fio: unknown client op: %s\n", fio_server_op(cmd->opcode));
Jens Axboee951bdc2011-10-05 21:58:45 +0200948 free(cmd);
949 break;
Jens Axboe37db14f2011-09-30 17:00:42 -0600950 }
951
Jens Axboee951bdc2011-10-05 21:58:45 +0200952 return 1;
Jens Axboe37db14f2011-09-30 17:00:42 -0600953}
Jens Axboeb66570d2011-10-01 11:11:35 -0600954
Jens Axboea5276612012-03-04 15:15:08 +0100955static void request_client_etas(struct client_ops *ops)
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200956{
957 struct fio_client *client;
958 struct flist_head *entry;
959 struct client_eta *eta;
Jens Axboe82c1ed32011-10-10 08:56:18 +0200960 int skipped = 0;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200961
962 dprint(FD_NET, "client: request eta (%d)\n", nr_clients);
963
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200964 eta = malloc(sizeof(*eta));
965 memset(&eta->eta, 0, sizeof(eta->eta));
966 eta->pending = nr_clients;
967
968 flist_for_each(entry, &client_list) {
969 client = flist_entry(entry, struct fio_client, list);
970
Jens Axboe82c1ed32011-10-10 08:56:18 +0200971 if (!flist_empty(&client->eta_list)) {
972 skipped++;
973 continue;
974 }
Jens Axboe01be0382011-10-15 14:43:41 +0200975 if (client->state != Client_running)
976 continue;
Jens Axboe82c1ed32011-10-10 08:56:18 +0200977
Jens Axboef77d2672011-10-10 14:36:07 +0200978 assert(!client->eta_in_flight);
Jens Axboe82c1ed32011-10-10 08:56:18 +0200979 flist_add_tail(&client->eta_list, &eta_list);
Jens Axboef77d2672011-10-10 14:36:07 +0200980 client->eta_in_flight = eta;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200981 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_SEND_ETA,
Jens Axboedf380932011-10-11 14:25:08 +0200982 (uintptr_t) eta, &client->cmd_list);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200983 }
984
Jens Axboe82c1ed32011-10-10 08:56:18 +0200985 while (skipped--)
Jens Axboea5276612012-03-04 15:15:08 +0100986 fio_client_dec_jobs_eta(eta, ops->eta);
Jens Axboe82c1ed32011-10-10 08:56:18 +0200987
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200988 dprint(FD_NET, "client: requested eta tag %p\n", eta);
989}
990
Jens Axboe89c17072011-10-11 10:15:51 +0200991static int client_check_cmd_timeout(struct fio_client *client,
992 struct timeval *now)
993{
994 struct fio_net_int_cmd *cmd;
995 struct flist_head *entry, *tmp;
996 int ret = 0;
997
998 flist_for_each_safe(entry, tmp, &client->cmd_list) {
999 cmd = flist_entry(entry, struct fio_net_int_cmd, list);
1000
1001 if (mtime_since(&cmd->tv, now) < FIO_NET_CLIENT_TIMEOUT)
1002 continue;
1003
1004 log_err("fio: client %s, timeout on cmd %s\n", client->hostname,
1005 fio_server_op(cmd->cmd.opcode));
1006 flist_del(&cmd->list);
1007 free(cmd);
1008 ret = 1;
1009 }
1010
1011 return flist_empty(&client->cmd_list) && ret;
1012}
1013
Jens Axboea5276612012-03-04 15:15:08 +01001014static int fio_check_clients_timed_out(void)
Jens Axboe89c17072011-10-11 10:15:51 +02001015{
1016 struct fio_client *client;
1017 struct flist_head *entry, *tmp;
1018 struct timeval tv;
1019 int ret = 0;
1020
1021 gettimeofday(&tv, NULL);
1022
1023 flist_for_each_safe(entry, tmp, &client_list) {
1024 client = flist_entry(entry, struct fio_client, list);
1025
1026 if (flist_empty(&client->cmd_list))
1027 continue;
1028
1029 if (!client_check_cmd_timeout(client, &tv))
1030 continue;
1031
Jens Axboea5276612012-03-04 15:15:08 +01001032 if (client->ops->timed_out)
1033 client->ops->timed_out(client);
Jens Axboeed727a42012-03-02 12:14:40 +01001034 else
1035 log_err("fio: client %s timed out\n", client->hostname);
1036
Jens Axboe89c17072011-10-11 10:15:51 +02001037 remove_client(client);
1038 ret = 1;
1039 }
1040
1041 return ret;
1042}
1043
Stephen M. Camerondd366722012-02-24 08:17:30 +01001044int fio_handle_clients(struct client_ops *ops)
Jens Axboeb66570d2011-10-01 11:11:35 -06001045{
Jens Axboeb66570d2011-10-01 11:11:35 -06001046 struct pollfd *pfds;
Jens Axboe498c92c2011-10-17 09:14:42 +02001047 int i, ret = 0, retval = 0;
Jens Axboeb66570d2011-10-01 11:11:35 -06001048
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001049 gettimeofday(&eta_tv, NULL);
1050
Jens Axboeb66570d2011-10-01 11:11:35 -06001051 pfds = malloc(nr_clients * sizeof(struct pollfd));
1052
Jens Axboe37f0c1a2011-10-11 14:08:33 +02001053 sum_stat_clients = nr_clients;
1054 init_thread_stat(&client_ts);
1055 init_group_run_stat(&client_gs);
1056
Jens Axboeb66570d2011-10-01 11:11:35 -06001057 while (!exit_backend && nr_clients) {
Jens Axboec2cb6862012-02-23 20:56:12 +01001058 struct flist_head *entry, *tmp;
1059 struct fio_client *client;
1060
Jens Axboe82a4be12011-10-01 12:40:32 -06001061 i = 0;
Jens Axboec2cb6862012-02-23 20:56:12 +01001062 flist_for_each_safe(entry, tmp, &client_list) {
Jens Axboe82a4be12011-10-01 12:40:32 -06001063 client = flist_entry(entry, struct fio_client, list);
1064
Jens Axboea5276612012-03-04 15:15:08 +01001065 if (!client->sent_job && !client->ops->stay_connected &&
Jens Axboec2cb6862012-02-23 20:56:12 +01001066 flist_empty(&client->cmd_list)) {
1067 remove_client(client);
1068 continue;
1069 }
1070
Jens Axboe82a4be12011-10-01 12:40:32 -06001071 pfds[i].fd = client->fd;
1072 pfds[i].events = POLLIN;
1073 i++;
1074 }
1075
Jens Axboec2cb6862012-02-23 20:56:12 +01001076 if (!nr_clients)
1077 break;
1078
Jens Axboe82a4be12011-10-01 12:40:32 -06001079 assert(i == nr_clients);
1080
Jens Axboe5c2857f2011-10-04 13:14:32 +02001081 do {
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001082 struct timeval tv;
1083
1084 gettimeofday(&tv, NULL);
1085 if (mtime_since(&eta_tv, &tv) >= 900) {
Jens Axboea5276612012-03-04 15:15:08 +01001086 request_client_etas(ops);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001087 memcpy(&eta_tv, &tv, sizeof(tv));
Jens Axboe89c17072011-10-11 10:15:51 +02001088
Jens Axboea5276612012-03-04 15:15:08 +01001089 if (fio_check_clients_timed_out())
Jens Axboe89c17072011-10-11 10:15:51 +02001090 break;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001091 }
1092
Jens Axboe5c2857f2011-10-04 13:14:32 +02001093 ret = poll(pfds, nr_clients, 100);
1094 if (ret < 0) {
1095 if (errno == EINTR)
1096 continue;
1097 log_err("fio: poll clients: %s\n", strerror(errno));
1098 break;
1099 } else if (!ret)
Jens Axboeb66570d2011-10-01 11:11:35 -06001100 continue;
Jens Axboe5c2857f2011-10-04 13:14:32 +02001101 } while (ret <= 0);
Jens Axboeb66570d2011-10-01 11:11:35 -06001102
1103 for (i = 0; i < nr_clients; i++) {
1104 if (!(pfds[i].revents & POLLIN))
1105 continue;
1106
1107 client = find_client_by_fd(pfds[i].fd);
1108 if (!client) {
Jens Axboe3c5f57e2011-10-06 12:37:50 +02001109 log_err("fio: unknown client fd %d\n", pfds[i].fd);
Jens Axboeb66570d2011-10-01 11:11:35 -06001110 continue;
1111 }
Jens Axboea5276612012-03-04 15:15:08 +01001112 if (!fio_handle_client(client)) {
Jens Axboe28d3ab02011-10-05 20:45:37 +02001113 log_info("client: host=%s disconnected\n",
1114 client->hostname);
1115 remove_client(client);
Jens Axboe498c92c2011-10-17 09:14:42 +02001116 retval = 1;
Jens Axboe38990762011-10-17 13:31:33 +02001117 } else if (client->error)
Jens Axboe498c92c2011-10-17 09:14:42 +02001118 retval = 1;
Jens Axboeb66570d2011-10-01 11:11:35 -06001119 }
1120 }
1121
1122 free(pfds);
Jens Axboe498c92c2011-10-17 09:14:42 +02001123 return retval;
Jens Axboeb66570d2011-10-01 11:11:35 -06001124}