blob: ff3983bdf84c37e727ce5b481fe0574fddeff7f5 [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);
25static void handle_ts(struct fio_net_cmd *cmd);
26static void handle_gs(struct fio_net_cmd *cmd);
27static void handle_eta(struct fio_client *client, struct fio_net_cmd *cmd);
28static 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);
Stephen M. Camerondd366722012-02-24 08:17:30 +010030
31struct client_ops fio_client_ops = {
Jens Axboe084d1c62012-03-03 20:28:07 +010032 .text_op = handle_text,
Jens Axboe0420ba62012-02-29 11:16:52 +010033 .disk_util = handle_du,
34 .thread_status = handle_ts,
35 .group_stats = handle_gs,
36 .eta = handle_eta,
37 .probe = handle_probe,
Stephen M. Camerondd366722012-02-24 08:17:30 +010038};
39
Jens Axboeaf9c9fb2011-10-09 21:54:10 +020040static struct timeval eta_tv;
Jens Axboe48fbb462011-10-09 12:19:08 +020041
Jens Axboe81179ee2011-10-04 12:42:06 +020042enum {
Jens Axboe5c2857f2011-10-04 13:14:32 +020043 Client_created = 0,
Jens Axboe81179ee2011-10-04 12:42:06 +020044 Client_connected = 1,
45 Client_started = 2,
Jens Axboe01be0382011-10-15 14:43:41 +020046 Client_running = 3,
47 Client_stopped = 4,
48 Client_exited = 5,
Jens Axboeb66570d2011-10-01 11:11:35 -060049};
50
51static FLIST_HEAD(client_list);
Jens Axboe82c1ed32011-10-10 08:56:18 +020052static FLIST_HEAD(eta_list);
Jens Axboeb66570d2011-10-01 11:11:35 -060053
Jens Axboe3f3a4542011-10-10 21:11:09 +020054static FLIST_HEAD(arg_list);
55
Jens Axboe37f0c1a2011-10-11 14:08:33 +020056static struct thread_stat client_ts;
57static struct group_run_stats client_gs;
58static int sum_stat_clients;
59static int sum_stat_nr;
60
Jens Axboe3c5f57e2011-10-06 12:37:50 +020061#define FIO_CLIENT_HASH_BITS 7
62#define FIO_CLIENT_HASH_SZ (1 << FIO_CLIENT_HASH_BITS)
63#define FIO_CLIENT_HASH_MASK (FIO_CLIENT_HASH_SZ - 1)
Jens Axboebebe6392011-10-07 10:00:51 +020064static struct flist_head client_hash[FIO_CLIENT_HASH_SZ];
Jens Axboe3c5f57e2011-10-06 12:37:50 +020065
Jens Axboebebe6392011-10-07 10:00:51 +020066static void fio_client_add_hash(struct fio_client *client)
Jens Axboe3c5f57e2011-10-06 12:37:50 +020067{
68 int bucket = hash_long(client->fd, FIO_CLIENT_HASH_BITS);
69
70 bucket &= FIO_CLIENT_HASH_MASK;
Jens Axboebebe6392011-10-07 10:00:51 +020071 flist_add(&client->hash_list, &client_hash[bucket]);
Jens Axboe3c5f57e2011-10-06 12:37:50 +020072}
73
Jens Axboebebe6392011-10-07 10:00:51 +020074static void fio_client_remove_hash(struct fio_client *client)
Jens Axboe3c5f57e2011-10-06 12:37:50 +020075{
Jens Axboebebe6392011-10-07 10:00:51 +020076 if (!flist_empty(&client->hash_list))
77 flist_del_init(&client->hash_list);
Jens Axboe3c5f57e2011-10-06 12:37:50 +020078}
79
80static void fio_init fio_client_hash_init(void)
81{
82 int i;
83
Jens Axboebebe6392011-10-07 10:00:51 +020084 for (i = 0; i < FIO_CLIENT_HASH_SZ; i++)
85 INIT_FLIST_HEAD(&client_hash[i]);
Jens Axboe3c5f57e2011-10-06 12:37:50 +020086}
87
Jens Axboeb66570d2011-10-01 11:11:35 -060088static struct fio_client *find_client_by_fd(int fd)
89{
Jens Axboe3c5f57e2011-10-06 12:37:50 +020090 int bucket = hash_long(fd, FIO_CLIENT_HASH_BITS) & FIO_CLIENT_HASH_MASK;
Jens Axboeb66570d2011-10-01 11:11:35 -060091 struct fio_client *client;
92 struct flist_head *entry;
93
Jens Axboebebe6392011-10-07 10:00:51 +020094 flist_for_each(entry, &client_hash[bucket]) {
95 client = flist_entry(entry, struct fio_client, hash_list);
Jens Axboeb66570d2011-10-01 11:11:35 -060096
97 if (client->fd == fd)
98 return client;
99 }
100
101 return NULL;
102}
103
Jens Axboeb66570d2011-10-01 11:11:35 -0600104static void remove_client(struct fio_client *client)
105{
Jens Axboe39e8e012011-10-04 13:46:08 +0200106 dprint(FD_NET, "client: removed <%s>\n", client->hostname);
Jens Axboeb66570d2011-10-01 11:11:35 -0600107 flist_del(&client->list);
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200108
Jens Axboebebe6392011-10-07 10:00:51 +0200109 fio_client_remove_hash(client);
Jens Axboe81179ee2011-10-04 12:42:06 +0200110
Jens Axboe82c1ed32011-10-10 08:56:18 +0200111 if (!flist_empty(&client->eta_list)) {
112 flist_del_init(&client->eta_list);
Jens Axboe3e47bd22012-02-29 13:45:02 +0100113 fio_client_dec_jobs_eta(client->eta_in_flight, display_thread_status);
Jens Axboe82c1ed32011-10-10 08:56:18 +0200114 }
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200115
Jens Axboeb66570d2011-10-01 11:11:35 -0600116 free(client->hostname);
Jens Axboe81179ee2011-10-04 12:42:06 +0200117 if (client->argv)
118 free(client->argv);
Jens Axboeb5296dd2011-10-07 16:35:56 +0200119 if (client->name)
120 free(client->name);
Jens Axboe81179ee2011-10-04 12:42:06 +0200121
Jens Axboeb66570d2011-10-01 11:11:35 -0600122 free(client);
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200123 nr_clients--;
Jens Axboe5fd0acb2011-10-11 14:20:22 +0200124 sum_stat_clients--;
Jens Axboeb66570d2011-10-01 11:11:35 -0600125}
Jens Axboe132159a2011-09-30 15:01:32 -0600126
Jens Axboefa2ea802011-10-08 21:07:29 +0200127static void __fio_client_add_cmd_option(struct fio_client *client,
128 const char *opt)
Jens Axboe81179ee2011-10-04 12:42:06 +0200129{
Jens Axboe39e8e012011-10-04 13:46:08 +0200130 int index;
131
132 index = client->argc++;
Jens Axboe81179ee2011-10-04 12:42:06 +0200133 client->argv = realloc(client->argv, sizeof(char *) * client->argc);
Jens Axboe39e8e012011-10-04 13:46:08 +0200134 client->argv[index] = strdup(opt);
135 dprint(FD_NET, "client: add cmd %d: %s\n", index, opt);
Jens Axboe81179ee2011-10-04 12:42:06 +0200136}
137
Jens Axboefa2ea802011-10-08 21:07:29 +0200138void fio_client_add_cmd_option(void *cookie, const char *opt)
Jens Axboe81179ee2011-10-04 12:42:06 +0200139{
Jens Axboebebe6392011-10-07 10:00:51 +0200140 struct fio_client *client = cookie;
Jens Axboe3f3a4542011-10-10 21:11:09 +0200141 struct flist_head *entry;
Jens Axboe81179ee2011-10-04 12:42:06 +0200142
Jens Axboebebe6392011-10-07 10:00:51 +0200143 if (!client || !opt)
Jens Axboefa2ea802011-10-08 21:07:29 +0200144 return;
Jens Axboe81179ee2011-10-04 12:42:06 +0200145
Jens Axboefa2ea802011-10-08 21:07:29 +0200146 __fio_client_add_cmd_option(client, opt);
Jens Axboe3f3a4542011-10-10 21:11:09 +0200147
148 /*
149 * Duplicate arguments to shared client group
150 */
151 flist_for_each(entry, &arg_list) {
152 client = flist_entry(entry, struct fio_client, arg_list);
153
154 __fio_client_add_cmd_option(client, opt);
155 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200156}
157
Jens Axboe3ec62ec2012-03-01 12:01:29 +0100158struct fio_client *fio_client_add_explicit(const char *hostname, int type,
159 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;
189
190 __fio_client_add_cmd_option(client, "fio");
191
192 flist_add(&client->list, &client_list);
193 nr_clients++;
194 dprint(FD_NET, "client: added <%s>\n", client->hostname);
195 return client;
196err:
197 free(client);
198 return NULL;
199}
200
Jens Axboebebe6392011-10-07 10:00:51 +0200201int fio_client_add(const char *hostname, void **cookie)
Jens Axboe132159a2011-09-30 15:01:32 -0600202{
Jens Axboe3f3a4542011-10-10 21:11:09 +0200203 struct fio_client *existing = *cookie;
Jens Axboeb66570d2011-10-01 11:11:35 -0600204 struct fio_client *client;
Jens Axboe132159a2011-09-30 15:01:32 -0600205
Jens Axboe3f3a4542011-10-10 21:11:09 +0200206 if (existing) {
207 /*
208 * We always add our "exec" name as the option, hence 1
209 * means empty.
210 */
211 if (existing->argc == 1)
212 flist_add_tail(&existing->arg_list, &arg_list);
213 else {
214 while (!flist_empty(&arg_list))
215 flist_del_init(arg_list.next);
216 }
217 }
218
Jens Axboeb66570d2011-10-01 11:11:35 -0600219 client = malloc(sizeof(*client));
Jens Axboea37f69b2011-10-01 12:24:21 -0600220 memset(client, 0, sizeof(*client));
Jens Axboe81179ee2011-10-04 12:42:06 +0200221
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200222 INIT_FLIST_HEAD(&client->list);
Jens Axboebebe6392011-10-07 10:00:51 +0200223 INIT_FLIST_HEAD(&client->hash_list);
Jens Axboe3f3a4542011-10-10 21:11:09 +0200224 INIT_FLIST_HEAD(&client->arg_list);
Jens Axboe82c1ed32011-10-10 08:56:18 +0200225 INIT_FLIST_HEAD(&client->eta_list);
Jens Axboe89c17072011-10-11 10:15:51 +0200226 INIT_FLIST_HEAD(&client->cmd_list);
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200227
Jens Axboebebe6392011-10-07 10:00:51 +0200228 if (fio_server_parse_string(hostname, &client->hostname,
229 &client->is_sock, &client->port,
Jens Axboe811826b2011-10-24 09:11:50 +0200230 &client->addr.sin_addr,
231 &client->addr6.sin6_addr,
232 &client->ipv6))
Jens Axboebebe6392011-10-07 10:00:51 +0200233 return -1;
234
Jens Axboea37f69b2011-10-01 12:24:21 -0600235 client->fd = -1;
Jens Axboe81179ee2011-10-04 12:42:06 +0200236
237 __fio_client_add_cmd_option(client, "fio");
238
Jens Axboea37f69b2011-10-01 12:24:21 -0600239 flist_add(&client->list, &client_list);
240 nr_clients++;
Jens Axboebebe6392011-10-07 10:00:51 +0200241 dprint(FD_NET, "client: added <%s>\n", client->hostname);
242 *cookie = client;
243 return 0;
Jens Axboea37f69b2011-10-01 12:24:21 -0600244}
245
Jens Axboe87aa8f12011-10-06 21:24:13 +0200246static int fio_client_connect_ip(struct fio_client *client)
Jens Axboea37f69b2011-10-01 12:24:21 -0600247{
Jens Axboe811826b2011-10-24 09:11:50 +0200248 struct sockaddr *addr;
249 fio_socklen_t socklen;
250 int fd, domain;
Jens Axboe132159a2011-09-30 15:01:32 -0600251
Jens Axboe811826b2011-10-24 09:11:50 +0200252 if (client->ipv6) {
253 client->addr6.sin6_family = AF_INET6;
254 client->addr6.sin6_port = htons(client->port);
255 domain = AF_INET6;
256 addr = (struct sockaddr *) &client->addr6;
257 socklen = sizeof(client->addr6);
258 } else {
259 client->addr.sin_family = AF_INET;
260 client->addr.sin_port = htons(client->port);
261 domain = AF_INET;
262 addr = (struct sockaddr *) &client->addr;
263 socklen = sizeof(client->addr);
264 }
Jens Axboe132159a2011-09-30 15:01:32 -0600265
Jens Axboe811826b2011-10-24 09:11:50 +0200266 fd = socket(domain, SOCK_STREAM, 0);
Jens Axboe132159a2011-09-30 15:01:32 -0600267 if (fd < 0) {
268 log_err("fio: socket: %s\n", strerror(errno));
Jens Axboe87aa8f12011-10-06 21:24:13 +0200269 return -1;
Jens Axboe132159a2011-09-30 15:01:32 -0600270 }
271
Jens Axboe811826b2011-10-24 09:11:50 +0200272 if (connect(fd, addr, socklen) < 0) {
Jens Axboe132159a2011-09-30 15:01:32 -0600273 log_err("fio: connect: %s\n", strerror(errno));
Jens Axboea7de0a12011-10-07 12:55:14 +0200274 log_err("fio: failed to connect to %s:%u\n", client->hostname,
275 client->port);
Jens Axboeb94cba42011-10-06 21:27:10 +0200276 close(fd);
Jens Axboe87aa8f12011-10-06 21:24:13 +0200277 return -1;
Jens Axboe132159a2011-09-30 15:01:32 -0600278 }
279
Jens Axboe87aa8f12011-10-06 21:24:13 +0200280 return fd;
281}
282
283static int fio_client_connect_sock(struct fio_client *client)
284{
285 struct sockaddr_un *addr = &client->addr_un;
286 fio_socklen_t len;
287 int fd;
288
289 memset(addr, 0, sizeof(*addr));
290 addr->sun_family = AF_UNIX;
291 strcpy(addr->sun_path, client->hostname);
292
293 fd = socket(AF_UNIX, SOCK_STREAM, 0);
294 if (fd < 0) {
295 log_err("fio: socket: %s\n", strerror(errno));
296 return -1;
297 }
298
299 len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
300 if (connect(fd, (struct sockaddr *) addr, len) < 0) {
301 log_err("fio: connect; %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200302 close(fd);
Jens Axboe87aa8f12011-10-06 21:24:13 +0200303 return -1;
304 }
305
306 return fd;
307}
308
309static int fio_client_connect(struct fio_client *client)
310{
311 int fd;
312
313 dprint(FD_NET, "client: connect to host %s\n", client->hostname);
314
Jens Axboe87aa8f12011-10-06 21:24:13 +0200315 if (client->is_sock)
316 fd = fio_client_connect_sock(client);
317 else
318 fd = fio_client_connect_ip(client);
319
Jens Axboe89c17072011-10-11 10:15:51 +0200320 dprint(FD_NET, "client: %s connected %d\n", client->hostname, fd);
321
Jens Axboe87aa8f12011-10-06 21:24:13 +0200322 if (fd < 0)
323 return 1;
324
Jens Axboeb66570d2011-10-01 11:11:35 -0600325 client->fd = fd;
Jens Axboebebe6392011-10-07 10:00:51 +0200326 fio_client_add_hash(client);
Jens Axboe81179ee2011-10-04 12:42:06 +0200327 client->state = Client_connected;
Jens Axboe132159a2011-09-30 15:01:32 -0600328 return 0;
329}
330
Jens Axboecc0df002011-10-03 20:53:32 +0200331void fio_clients_terminate(void)
332{
333 struct flist_head *entry;
334 struct fio_client *client;
335
Jens Axboe60efd142011-10-04 13:30:11 +0200336 dprint(FD_NET, "client: terminate clients\n");
337
Jens Axboecc0df002011-10-03 20:53:32 +0200338 flist_for_each(entry, &client_list) {
339 client = flist_entry(entry, struct fio_client, list);
340
Jens Axboe89c17072011-10-11 10:15:51 +0200341 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_QUIT, 0, NULL);
Jens Axboecc0df002011-10-03 20:53:32 +0200342 }
343}
344
345static void sig_int(int sig)
346{
Jens Axboebebe6392011-10-07 10:00:51 +0200347 dprint(FD_NET, "client: got signal %d\n", sig);
Jens Axboecc0df002011-10-03 20:53:32 +0200348 fio_clients_terminate();
349}
350
351static void client_signal_handler(void)
352{
353 struct sigaction act;
354
355 memset(&act, 0, sizeof(act));
356 act.sa_handler = sig_int;
357 act.sa_flags = SA_RESTART;
358 sigaction(SIGINT, &act, NULL);
359
360 memset(&act, 0, sizeof(act));
361 act.sa_handler = sig_int;
362 act.sa_flags = SA_RESTART;
363 sigaction(SIGTERM, &act, NULL);
364}
365
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200366static void probe_client(struct fio_client *client)
367{
Jens Axboe60efd142011-10-04 13:30:11 +0200368 dprint(FD_NET, "client: send probe\n");
369
Jens Axboe89c17072011-10-11 10:15:51 +0200370 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_PROBE, 0, &client->cmd_list);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200371}
372
Jens Axboe81179ee2011-10-04 12:42:06 +0200373static int send_client_cmd_line(struct fio_client *client)
374{
Jens Axboefa2ea802011-10-08 21:07:29 +0200375 struct cmd_single_line_pdu *cslp;
376 struct cmd_line_pdu *clp;
377 unsigned long offset;
Jens Axboe7f868312011-10-10 09:55:21 +0200378 unsigned int *lens;
Jens Axboefa2ea802011-10-08 21:07:29 +0200379 void *pdu;
380 size_t mem;
Jens Axboe81179ee2011-10-04 12:42:06 +0200381 int i, ret;
382
Jens Axboe39e8e012011-10-04 13:46:08 +0200383 dprint(FD_NET, "client: send cmdline %d\n", client->argc);
Jens Axboe60efd142011-10-04 13:30:11 +0200384
Jens Axboe7f868312011-10-10 09:55:21 +0200385 lens = malloc(client->argc * sizeof(unsigned int));
386
Jens Axboefa2ea802011-10-08 21:07:29 +0200387 /*
388 * Find out how much mem we need
389 */
Jens Axboe7f868312011-10-10 09:55:21 +0200390 for (i = 0, mem = 0; i < client->argc; i++) {
391 lens[i] = strlen(client->argv[i]) + 1;
392 mem += lens[i];
393 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200394
Jens Axboefa2ea802011-10-08 21:07:29 +0200395 /*
396 * We need one cmd_line_pdu, and argc number of cmd_single_line_pdu
397 */
398 mem += sizeof(*clp) + (client->argc * sizeof(*cslp));
399
400 pdu = malloc(mem);
401 clp = pdu;
402 offset = sizeof(*clp);
403
404 for (i = 0; i < client->argc; i++) {
Jens Axboe7f868312011-10-10 09:55:21 +0200405 uint16_t arg_len = lens[i];
Jens Axboefa2ea802011-10-08 21:07:29 +0200406
407 cslp = pdu + offset;
408 strcpy((char *) cslp->text, client->argv[i]);
409 cslp->len = cpu_to_le16(arg_len);
410 offset += sizeof(*cslp) + arg_len;
411 }
412
Jens Axboe7f868312011-10-10 09:55:21 +0200413 free(lens);
Jens Axboefa2ea802011-10-08 21:07:29 +0200414 clp->lines = cpu_to_le16(client->argc);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200415 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOBLINE, pdu, mem, 0);
Jens Axboe81179ee2011-10-04 12:42:06 +0200416 free(pdu);
417 return ret;
418}
419
Jens Axboea37f69b2011-10-01 12:24:21 -0600420int fio_clients_connect(void)
421{
422 struct fio_client *client;
423 struct flist_head *entry, *tmp;
424 int ret;
425
Bruce Cran93bcfd22012-02-20 20:18:19 +0100426#ifdef WIN32
427 WSADATA wsd;
428 WSAStartup(MAKEWORD(2,2), &wsd);
429#endif
430
Jens Axboe60efd142011-10-04 13:30:11 +0200431 dprint(FD_NET, "client: connect all\n");
432
Jens Axboecc0df002011-10-03 20:53:32 +0200433 client_signal_handler();
434
Jens Axboea37f69b2011-10-01 12:24:21 -0600435 flist_for_each_safe(entry, tmp, &client_list) {
436 client = flist_entry(entry, struct fio_client, list);
437
438 ret = fio_client_connect(client);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200439 if (ret) {
Jens Axboea37f69b2011-10-01 12:24:21 -0600440 remove_client(client);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200441 continue;
442 }
443
444 probe_client(client);
Jens Axboe81179ee2011-10-04 12:42:06 +0200445
446 if (client->argc > 1)
447 send_client_cmd_line(client);
Jens Axboea37f69b2011-10-01 12:24:21 -0600448 }
449
450 return !nr_clients;
451}
452
Jens Axboe132159a2011-09-30 15:01:32 -0600453/*
454 * Send file contents to server backend. We could use sendfile(), but to remain
455 * more portable lets just read/write the darn thing.
456 */
Jens Axboea37f69b2011-10-01 12:24:21 -0600457static int fio_client_send_ini(struct fio_client *client, const char *filename)
Jens Axboe132159a2011-09-30 15:01:32 -0600458{
459 struct stat sb;
460 char *p, *buf;
461 off_t len;
462 int fd, ret;
463
Jens Axboe46c48f12011-10-01 12:50:51 -0600464 dprint(FD_NET, "send ini %s to %s\n", filename, client->hostname);
465
Jens Axboe132159a2011-09-30 15:01:32 -0600466 fd = open(filename, O_RDONLY);
467 if (fd < 0) {
Jens Axboee951bdc2011-10-05 21:58:45 +0200468 log_err("fio: job file <%s> open: %s\n", filename, strerror(errno));
Jens Axboe132159a2011-09-30 15:01:32 -0600469 return 1;
470 }
471
472 if (fstat(fd, &sb) < 0) {
473 log_err("fio: job file stat: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200474 close(fd);
Jens Axboe132159a2011-09-30 15:01:32 -0600475 return 1;
476 }
477
478 buf = malloc(sb.st_size);
479
480 len = sb.st_size;
481 p = buf;
482 do {
483 ret = read(fd, p, len);
484 if (ret > 0) {
485 len -= ret;
486 if (!len)
487 break;
488 p += ret;
489 continue;
490 } else if (!ret)
491 break;
492 else if (errno == EAGAIN || errno == EINTR)
493 continue;
494 } while (1);
495
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200496 if (len) {
497 log_err("fio: failed reading job file %s\n", filename);
Jens Axboeb94cba42011-10-06 21:27:10 +0200498 close(fd);
Jens Axboec524ef72011-10-07 12:23:34 +0200499 free(buf);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200500 return 1;
501 }
502
Jens Axboec2cb6862012-02-23 20:56:12 +0100503 client->sent_job = 1;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200504 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, buf, sb.st_size, 0);
Jens Axboe132159a2011-09-30 15:01:32 -0600505 free(buf);
Jens Axboeb94cba42011-10-06 21:27:10 +0200506 close(fd);
Jens Axboe132159a2011-09-30 15:01:32 -0600507 return ret;
508}
Jens Axboe37db14f2011-09-30 17:00:42 -0600509
Jens Axboea37f69b2011-10-01 12:24:21 -0600510int fio_clients_send_ini(const char *filename)
511{
512 struct fio_client *client;
513 struct flist_head *entry, *tmp;
514
515 flist_for_each_safe(entry, tmp, &client_list) {
516 client = flist_entry(entry, struct fio_client, list);
517
518 if (fio_client_send_ini(client, filename))
519 remove_client(client);
Jens Axboec2cb6862012-02-23 20:56:12 +0100520
521 client->sent_job = 1;
Jens Axboea37f69b2011-10-01 12:24:21 -0600522 }
523
524 return !nr_clients;
525}
526
Jens Axboea64e88d2011-10-03 14:20:01 +0200527static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
528{
529 dst->max_val = le64_to_cpu(src->max_val);
530 dst->min_val = le64_to_cpu(src->min_val);
531 dst->samples = le64_to_cpu(src->samples);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200532
533 /*
534 * Floats arrive as IEEE 754 encoded uint64_t, convert back to double
535 */
536 dst->mean.u.f = fio_uint64_to_double(le64_to_cpu(dst->mean.u.i));
537 dst->S.u.f = fio_uint64_to_double(le64_to_cpu(dst->S.u.i));
Jens Axboea64e88d2011-10-03 14:20:01 +0200538}
539
540static void convert_ts(struct thread_stat *dst, struct thread_stat *src)
541{
542 int i, j;
543
544 dst->error = le32_to_cpu(src->error);
545 dst->groupid = le32_to_cpu(src->groupid);
546 dst->pid = le32_to_cpu(src->pid);
547 dst->members = le32_to_cpu(src->members);
548
549 for (i = 0; i < 2; i++) {
550 convert_io_stat(&dst->clat_stat[i], &src->clat_stat[i]);
551 convert_io_stat(&dst->slat_stat[i], &src->slat_stat[i]);
552 convert_io_stat(&dst->lat_stat[i], &src->lat_stat[i]);
553 convert_io_stat(&dst->bw_stat[i], &src->bw_stat[i]);
554 }
555
556 dst->usr_time = le64_to_cpu(src->usr_time);
557 dst->sys_time = le64_to_cpu(src->sys_time);
558 dst->ctx = le64_to_cpu(src->ctx);
559 dst->minf = le64_to_cpu(src->minf);
560 dst->majf = le64_to_cpu(src->majf);
561 dst->clat_percentiles = le64_to_cpu(src->clat_percentiles);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200562
563 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
564 fio_fp64_t *fps = &src->percentile_list[i];
565 fio_fp64_t *fpd = &dst->percentile_list[i];
566
567 fpd->u.f = fio_uint64_to_double(le64_to_cpu(fps->u.i));
568 }
Jens Axboea64e88d2011-10-03 14:20:01 +0200569
570 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
571 dst->io_u_map[i] = le32_to_cpu(src->io_u_map[i]);
572 dst->io_u_submit[i] = le32_to_cpu(src->io_u_submit[i]);
573 dst->io_u_complete[i] = le32_to_cpu(src->io_u_complete[i]);
574 }
575
576 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
577 dst->io_u_lat_u[i] = le32_to_cpu(src->io_u_lat_u[i]);
578 dst->io_u_lat_m[i] = le32_to_cpu(src->io_u_lat_m[i]);
579 }
580
581 for (i = 0; i < 2; i++)
582 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
583 dst->io_u_plat[i][j] = le32_to_cpu(src->io_u_plat[i][j]);
584
585 for (i = 0; i < 3; i++) {
586 dst->total_io_u[i] = le64_to_cpu(src->total_io_u[i]);
Jens Axboe93eee042011-10-03 21:08:48 +0200587 dst->short_io_u[i] = le64_to_cpu(src->short_io_u[i]);
Jens Axboea64e88d2011-10-03 14:20:01 +0200588 }
589
590 dst->total_submit = le64_to_cpu(src->total_submit);
591 dst->total_complete = le64_to_cpu(src->total_complete);
592
593 for (i = 0; i < 2; i++) {
594 dst->io_bytes[i] = le64_to_cpu(src->io_bytes[i]);
595 dst->runtime[i] = le64_to_cpu(src->runtime[i]);
596 }
597
598 dst->total_run_time = le64_to_cpu(src->total_run_time);
599 dst->continue_on_error = le16_to_cpu(src->continue_on_error);
600 dst->total_err_count = le64_to_cpu(src->total_err_count);
Jens Axboeddcc0b62011-10-03 14:45:27 +0200601 dst->first_error = le32_to_cpu(src->first_error);
602 dst->kb_base = le32_to_cpu(src->kb_base);
Jens Axboea64e88d2011-10-03 14:20:01 +0200603}
604
605static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
606{
607 int i;
608
609 for (i = 0; i < 2; i++) {
610 dst->max_run[i] = le64_to_cpu(src->max_run[i]);
611 dst->min_run[i] = le64_to_cpu(src->min_run[i]);
612 dst->max_bw[i] = le64_to_cpu(src->max_bw[i]);
613 dst->min_bw[i] = le64_to_cpu(src->min_bw[i]);
614 dst->io_kb[i] = le64_to_cpu(src->io_kb[i]);
615 dst->agg[i] = le64_to_cpu(src->agg[i]);
616 }
617
618 dst->kb_base = le32_to_cpu(src->kb_base);
619 dst->groupid = le32_to_cpu(src->groupid);
620}
621
622static void handle_ts(struct fio_net_cmd *cmd)
623{
624 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
625
626 convert_ts(&p->ts, &p->ts);
627 convert_gs(&p->rs, &p->rs);
628
629 show_thread_status(&p->ts, &p->rs);
Jens Axboe37f0c1a2011-10-11 14:08:33 +0200630
631 if (sum_stat_clients == 1)
632 return;
633
634 sum_thread_stats(&client_ts, &p->ts, sum_stat_nr);
635 sum_group_stats(&client_gs, &p->rs);
636
637 client_ts.members++;
638 client_ts.groupid = p->ts.groupid;
639
640 if (++sum_stat_nr == sum_stat_clients) {
641 strcpy(client_ts.name, "All clients");
642 show_thread_status(&client_ts, &client_gs);
643 }
Jens Axboea64e88d2011-10-03 14:20:01 +0200644}
645
646static void handle_gs(struct fio_net_cmd *cmd)
647{
648 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
649
650 convert_gs(gs, gs);
651 show_group_stats(gs);
652}
653
Jens Axboed09a64a2011-10-13 11:38:56 +0200654static void convert_agg(struct disk_util_agg *agg)
655{
656 int i;
657
658 for (i = 0; i < 2; i++) {
659 agg->ios[i] = le32_to_cpu(agg->ios[i]);
660 agg->merges[i] = le32_to_cpu(agg->merges[i]);
661 agg->sectors[i] = le64_to_cpu(agg->sectors[i]);
662 agg->ticks[i] = le32_to_cpu(agg->ticks[i]);
663 }
664
665 agg->io_ticks = le32_to_cpu(agg->io_ticks);
666 agg->time_in_queue = le32_to_cpu(agg->time_in_queue);
667 agg->slavecount = le32_to_cpu(agg->slavecount);
Anton Blanchard823ba542011-11-07 14:16:26 +0100668 agg->max_util.u.f = fio_uint64_to_double(__le64_to_cpu(agg->max_util.u.i));
Jens Axboed09a64a2011-10-13 11:38:56 +0200669}
670
671static void convert_dus(struct disk_util_stat *dus)
672{
673 int i;
674
675 for (i = 0; i < 2; i++) {
676 dus->ios[i] = le32_to_cpu(dus->ios[i]);
677 dus->merges[i] = le32_to_cpu(dus->merges[i]);
678 dus->sectors[i] = le64_to_cpu(dus->sectors[i]);
679 dus->ticks[i] = le32_to_cpu(dus->ticks[i]);
680 }
681
682 dus->io_ticks = le32_to_cpu(dus->io_ticks);
683 dus->time_in_queue = le32_to_cpu(dus->time_in_queue);
684 dus->msec = le64_to_cpu(dus->msec);
685}
686
Jens Axboe084d1c62012-03-03 20:28:07 +0100687static void handle_text(struct fio_client *client, struct fio_net_cmd *cmd)
688{
689 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
690 const char *buf = (const char *) pdu->buf;
691 const char *name;
692 int fio_unused ret;
693
694 name = client->name ? client->name : client->hostname;
695
696 if (!client->skip_newline)
697 fprintf(f_out, "<%s> ", name);
698 ret = fwrite(buf, pdu->buf_len, 1, f_out);
699 fflush(f_out);
700 client->skip_newline = strchr(buf, '\n') == NULL;
701}
702
703
Jens Axboed09a64a2011-10-13 11:38:56 +0200704static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd)
705{
706 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
707
708 convert_dus(&du->dus);
709 convert_agg(&du->agg);
710
711 if (!client->disk_stats_shown) {
712 client->disk_stats_shown = 1;
713 log_info("\nDisk stats (read/write):\n");
714 }
715
Jens Axboef2f788d2011-10-13 14:03:52 +0200716 print_disk_util(&du->dus, &du->agg, terse_output);
Jens Axboed09a64a2011-10-13 11:38:56 +0200717}
718
Jens Axboe3e47bd22012-02-29 13:45:02 +0100719void fio_client_convert_jobs_eta(struct jobs_eta *je)
Jens Axboecf451d12011-10-03 16:48:30 +0200720{
Jens Axboecf451d12011-10-03 16:48:30 +0200721 int i;
722
723 je->nr_running = le32_to_cpu(je->nr_running);
724 je->nr_ramp = le32_to_cpu(je->nr_ramp);
725 je->nr_pending = le32_to_cpu(je->nr_pending);
726 je->files_open = le32_to_cpu(je->files_open);
Jens Axboecf451d12011-10-03 16:48:30 +0200727
728 for (i = 0; i < 2; i++) {
Jens Axboe3e47bd22012-02-29 13:45:02 +0100729 je->m_rate[i] = le32_to_cpu(je->m_rate[i]);
730 je->t_rate[i] = le32_to_cpu(je->t_rate[i]);
731 je->m_iops[i] = le32_to_cpu(je->m_iops[i]);
732 je->t_iops[i] = le32_to_cpu(je->t_iops[i]);
Jens Axboecf451d12011-10-03 16:48:30 +0200733 je->rate[i] = le32_to_cpu(je->rate[i]);
734 je->iops[i] = le32_to_cpu(je->iops[i]);
735 }
736
Jens Axboeb51eedb2011-10-09 12:13:39 +0200737 je->elapsed_sec = le64_to_cpu(je->elapsed_sec);
Jens Axboecf451d12011-10-03 16:48:30 +0200738 je->eta_sec = le64_to_cpu(je->eta_sec);
Jens Axboe48fbb462011-10-09 12:19:08 +0200739}
Jens Axboecf451d12011-10-03 16:48:30 +0200740
Jens Axboe3e47bd22012-02-29 13:45:02 +0100741void fio_client_sum_jobs_eta(struct jobs_eta *dst, struct jobs_eta *je)
Jens Axboe48fbb462011-10-09 12:19:08 +0200742{
Jens Axboe48fbb462011-10-09 12:19:08 +0200743 int i;
744
745 dst->nr_running += je->nr_running;
746 dst->nr_ramp += je->nr_ramp;
747 dst->nr_pending += je->nr_pending;
748 dst->files_open += je->files_open;
Jens Axboe48fbb462011-10-09 12:19:08 +0200749
750 for (i = 0; i < 2; i++) {
Jens Axboe3e47bd22012-02-29 13:45:02 +0100751 dst->m_rate[i] += je->m_rate[i];
752 dst->t_rate[i] += je->t_rate[i];
753 dst->m_iops[i] += je->m_iops[i];
754 dst->t_iops[i] += je->t_iops[i];
Jens Axboe48fbb462011-10-09 12:19:08 +0200755 dst->rate[i] += je->rate[i];
756 dst->iops[i] += je->iops[i];
757 }
758
759 dst->elapsed_sec += je->elapsed_sec;
760
761 if (je->eta_sec > dst->eta_sec)
762 dst->eta_sec = je->eta_sec;
763}
764
Jens Axboe3e47bd22012-02-29 13:45:02 +0100765void fio_client_dec_jobs_eta(struct client_eta *eta, void (*fn)(struct jobs_eta *))
Jens Axboe82c1ed32011-10-10 08:56:18 +0200766{
767 if (!--eta->pending) {
Jens Axboe3e47bd22012-02-29 13:45:02 +0100768 fn(&eta->eta);
Jens Axboe82c1ed32011-10-10 08:56:18 +0200769 free(eta);
770 }
771}
772
Jens Axboe89c17072011-10-11 10:15:51 +0200773static void remove_reply_cmd(struct fio_client *client, struct fio_net_cmd *cmd)
774{
775 struct fio_net_int_cmd *icmd = NULL;
776 struct flist_head *entry;
777
778 flist_for_each(entry, &client->cmd_list) {
779 icmd = flist_entry(entry, struct fio_net_int_cmd, list);
780
Jens Axboedf380932011-10-11 14:25:08 +0200781 if (cmd->tag == (uintptr_t) icmd)
Jens Axboe89c17072011-10-11 10:15:51 +0200782 break;
783
784 icmd = NULL;
785 }
786
787 if (!icmd) {
788 log_err("fio: client: unable to find matching tag\n");
789 return;
790 }
791
792 flist_del(&icmd->list);
793 cmd->tag = icmd->saved_tag;
794 free(icmd);
795}
796
Jens Axboe82c1ed32011-10-10 08:56:18 +0200797static void handle_eta(struct fio_client *client, struct fio_net_cmd *cmd)
Jens Axboe48fbb462011-10-09 12:19:08 +0200798{
799 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
Jens Axboedf380932011-10-11 14:25:08 +0200800 struct client_eta *eta = (struct client_eta *) (uintptr_t) cmd->tag;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200801
802 dprint(FD_NET, "client: got eta tag %p, %d\n", eta, eta->pending);
Jens Axboe48fbb462011-10-09 12:19:08 +0200803
Jens Axboef77d2672011-10-10 14:36:07 +0200804 assert(client->eta_in_flight == eta);
805
806 client->eta_in_flight = NULL;
Jens Axboe82c1ed32011-10-10 08:56:18 +0200807 flist_del_init(&client->eta_list);
808
Jens Axboe3e47bd22012-02-29 13:45:02 +0100809 fio_client_convert_jobs_eta(je);
810 fio_client_sum_jobs_eta(&eta->eta, je);
811 fio_client_dec_jobs_eta(eta, display_thread_status);
Jens Axboecf451d12011-10-03 16:48:30 +0200812}
813
Jens Axboeb5296dd2011-10-07 16:35:56 +0200814static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd)
Jens Axboe2e03b4b2011-10-04 09:00:40 +0200815{
816 struct cmd_probe_pdu *probe = (struct cmd_probe_pdu *) cmd->payload;
Jens Axboed2333352011-10-11 15:07:23 +0200817 const char *os, *arch;
818 char bit[16];
Jens Axboe2e03b4b2011-10-04 09:00:40 +0200819
Jens Axboecca84642011-10-07 12:47:57 +0200820 os = fio_get_os_string(probe->os);
821 if (!os)
822 os = "unknown";
823
824 arch = fio_get_arch_string(probe->arch);
825 if (!arch)
826 os = "unknown";
827
Jens Axboed2333352011-10-11 15:07:23 +0200828 sprintf(bit, "%d-bit", probe->bpp * 8);
Jens Axboe38fdef22011-10-11 14:30:06 +0200829
830 log_info("hostname=%s, be=%u, %s, os=%s, arch=%s, fio=%u.%u.%u\n",
831 probe->hostname, probe->bigendian, bit, os, arch,
832 probe->fio_major, probe->fio_minor, probe->fio_patch);
Jens Axboeb5296dd2011-10-07 16:35:56 +0200833
834 if (!client->name)
835 client->name = strdup((char *) probe->hostname);
Jens Axboe2e03b4b2011-10-04 09:00:40 +0200836}
837
Jens Axboe11e950b2011-10-16 21:34:14 +0200838static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd)
839{
840 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
841
842 client->state = Client_started;
843 client->jobs = le32_to_cpu(pdu->jobs);
844}
845
846static void handle_stop(struct fio_client *client, struct fio_net_cmd *cmd)
847{
848 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
849
850 client->state = Client_stopped;
851 client->error = le32_to_cpu(pdu->error);
Jens Axboe498c92c2011-10-17 09:14:42 +0200852
853 if (client->error)
854 log_info("client <%s>: exited with error %d\n", client->hostname, client->error);
Jens Axboe11e950b2011-10-16 21:34:14 +0200855}
856
Jens Axboe084d1c62012-03-03 20:28:07 +0100857static void convert_text(struct fio_net_cmd *cmd)
858{
859 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
860
861 pdu->level = le32_to_cpu(pdu->level);
862 pdu->buf_len = le32_to_cpu(pdu->buf_len);
863 pdu->log_sec = le64_to_cpu(pdu->log_sec);
864 pdu->log_usec = le64_to_cpu(pdu->log_usec);
865}
866
Jens Axboe3e47bd22012-02-29 13:45:02 +0100867int fio_handle_client(struct fio_client *client, struct client_ops *ops)
Jens Axboe37db14f2011-09-30 17:00:42 -0600868{
869 struct fio_net_cmd *cmd;
870
Jens Axboe60efd142011-10-04 13:30:11 +0200871 dprint(FD_NET, "client: handle %s\n", client->hostname);
872
Jens Axboee951bdc2011-10-05 21:58:45 +0200873 cmd = fio_net_recv_cmd(client->fd);
874 if (!cmd)
875 return 0;
Jens Axboec2c94582011-10-05 20:31:30 +0200876
Jens Axboe89c17072011-10-11 10:15:51 +0200877 dprint(FD_NET, "client: got cmd op %s from %s\n",
878 fio_server_op(cmd->opcode), client->hostname);
Jens Axboe46c48f12011-10-01 12:50:51 -0600879
Jens Axboee951bdc2011-10-05 21:58:45 +0200880 switch (cmd->opcode) {
881 case FIO_NET_CMD_QUIT:
Jens Axboe3ec62ec2012-03-01 12:01:29 +0100882 if (ops->quit)
883 ops->quit(client);
Jens Axboee951bdc2011-10-05 21:58:45 +0200884 remove_client(client);
885 free(cmd);
886 break;
Jens Axboe084d1c62012-03-03 20:28:07 +0100887 case FIO_NET_CMD_TEXT:
888 convert_text(cmd);
889 ops->text_op(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +0200890 free(cmd);
891 break;
Jens Axboed09a64a2011-10-13 11:38:56 +0200892 case FIO_NET_CMD_DU:
Stephen M. Camerondd366722012-02-24 08:17:30 +0100893 ops->disk_util(client, cmd);
Jens Axboed09a64a2011-10-13 11:38:56 +0200894 free(cmd);
895 break;
Jens Axboee951bdc2011-10-05 21:58:45 +0200896 case FIO_NET_CMD_TS:
Stephen M. Camerondd366722012-02-24 08:17:30 +0100897 ops->thread_status(cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +0200898 free(cmd);
899 break;
900 case FIO_NET_CMD_GS:
Stephen M. Camerondd366722012-02-24 08:17:30 +0100901 ops->group_stats(cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +0200902 free(cmd);
903 break;
904 case FIO_NET_CMD_ETA:
Jens Axboe89c17072011-10-11 10:15:51 +0200905 remove_reply_cmd(client, cmd);
Stephen M. Camerondd366722012-02-24 08:17:30 +0100906 ops->eta(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +0200907 free(cmd);
908 break;
909 case FIO_NET_CMD_PROBE:
Jens Axboe89c17072011-10-11 10:15:51 +0200910 remove_reply_cmd(client, cmd);
Stephen M. Camerondd366722012-02-24 08:17:30 +0100911 ops->probe(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +0200912 free(cmd);
913 break;
Jens Axboe01be0382011-10-15 14:43:41 +0200914 case FIO_NET_CMD_RUN:
915 client->state = Client_running;
916 free(cmd);
917 break;
Jens Axboee951bdc2011-10-05 21:58:45 +0200918 case FIO_NET_CMD_START:
Jens Axboe11e950b2011-10-16 21:34:14 +0200919 handle_start(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +0200920 free(cmd);
921 break;
922 case FIO_NET_CMD_STOP:
Jens Axboe11e950b2011-10-16 21:34:14 +0200923 handle_stop(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +0200924 free(cmd);
925 break;
Jens Axboe807f9972012-03-02 10:25:24 +0100926 case FIO_NET_CMD_ADD_JOB:
927 if (ops->add_job)
928 ops->add_job(client, cmd);
929 free(cmd);
930 break;
Jens Axboee951bdc2011-10-05 21:58:45 +0200931 default:
Jens Axboe89c17072011-10-11 10:15:51 +0200932 log_err("fio: unknown client op: %s\n", fio_server_op(cmd->opcode));
Jens Axboee951bdc2011-10-05 21:58:45 +0200933 free(cmd);
934 break;
Jens Axboe37db14f2011-09-30 17:00:42 -0600935 }
936
Jens Axboee951bdc2011-10-05 21:58:45 +0200937 return 1;
Jens Axboe37db14f2011-09-30 17:00:42 -0600938}
Jens Axboeb66570d2011-10-01 11:11:35 -0600939
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200940static void request_client_etas(void)
941{
942 struct fio_client *client;
943 struct flist_head *entry;
944 struct client_eta *eta;
Jens Axboe82c1ed32011-10-10 08:56:18 +0200945 int skipped = 0;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200946
947 dprint(FD_NET, "client: request eta (%d)\n", nr_clients);
948
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200949 eta = malloc(sizeof(*eta));
950 memset(&eta->eta, 0, sizeof(eta->eta));
951 eta->pending = nr_clients;
952
953 flist_for_each(entry, &client_list) {
954 client = flist_entry(entry, struct fio_client, list);
955
Jens Axboe82c1ed32011-10-10 08:56:18 +0200956 if (!flist_empty(&client->eta_list)) {
957 skipped++;
958 continue;
959 }
Jens Axboe01be0382011-10-15 14:43:41 +0200960 if (client->state != Client_running)
961 continue;
Jens Axboe82c1ed32011-10-10 08:56:18 +0200962
Jens Axboef77d2672011-10-10 14:36:07 +0200963 assert(!client->eta_in_flight);
Jens Axboe82c1ed32011-10-10 08:56:18 +0200964 flist_add_tail(&client->eta_list, &eta_list);
Jens Axboef77d2672011-10-10 14:36:07 +0200965 client->eta_in_flight = eta;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200966 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_SEND_ETA,
Jens Axboedf380932011-10-11 14:25:08 +0200967 (uintptr_t) eta, &client->cmd_list);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200968 }
969
Jens Axboe82c1ed32011-10-10 08:56:18 +0200970 while (skipped--)
Jens Axboe3e47bd22012-02-29 13:45:02 +0100971 fio_client_dec_jobs_eta(eta, display_thread_status);
Jens Axboe82c1ed32011-10-10 08:56:18 +0200972
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200973 dprint(FD_NET, "client: requested eta tag %p\n", eta);
974}
975
Jens Axboe89c17072011-10-11 10:15:51 +0200976static int client_check_cmd_timeout(struct fio_client *client,
977 struct timeval *now)
978{
979 struct fio_net_int_cmd *cmd;
980 struct flist_head *entry, *tmp;
981 int ret = 0;
982
983 flist_for_each_safe(entry, tmp, &client->cmd_list) {
984 cmd = flist_entry(entry, struct fio_net_int_cmd, list);
985
986 if (mtime_since(&cmd->tv, now) < FIO_NET_CLIENT_TIMEOUT)
987 continue;
988
989 log_err("fio: client %s, timeout on cmd %s\n", client->hostname,
990 fio_server_op(cmd->cmd.opcode));
991 flist_del(&cmd->list);
992 free(cmd);
993 ret = 1;
994 }
995
996 return flist_empty(&client->cmd_list) && ret;
997}
998
Jens Axboeed727a42012-03-02 12:14:40 +0100999static int fio_client_timed_out(struct client_ops *ops)
Jens Axboe89c17072011-10-11 10:15:51 +02001000{
1001 struct fio_client *client;
1002 struct flist_head *entry, *tmp;
1003 struct timeval tv;
1004 int ret = 0;
1005
1006 gettimeofday(&tv, NULL);
1007
1008 flist_for_each_safe(entry, tmp, &client_list) {
1009 client = flist_entry(entry, struct fio_client, list);
1010
1011 if (flist_empty(&client->cmd_list))
1012 continue;
1013
1014 if (!client_check_cmd_timeout(client, &tv))
1015 continue;
1016
Jens Axboeed727a42012-03-02 12:14:40 +01001017 if (ops->timed_out)
1018 ops->timed_out(client);
1019 else
1020 log_err("fio: client %s timed out\n", client->hostname);
1021
Jens Axboe89c17072011-10-11 10:15:51 +02001022 remove_client(client);
1023 ret = 1;
1024 }
1025
1026 return ret;
1027}
1028
Stephen M. Camerondd366722012-02-24 08:17:30 +01001029int fio_handle_clients(struct client_ops *ops)
Jens Axboeb66570d2011-10-01 11:11:35 -06001030{
Jens Axboeb66570d2011-10-01 11:11:35 -06001031 struct pollfd *pfds;
Jens Axboe498c92c2011-10-17 09:14:42 +02001032 int i, ret = 0, retval = 0;
Jens Axboeb66570d2011-10-01 11:11:35 -06001033
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001034 gettimeofday(&eta_tv, NULL);
1035
Jens Axboeb66570d2011-10-01 11:11:35 -06001036 pfds = malloc(nr_clients * sizeof(struct pollfd));
1037
Jens Axboe37f0c1a2011-10-11 14:08:33 +02001038 sum_stat_clients = nr_clients;
1039 init_thread_stat(&client_ts);
1040 init_group_run_stat(&client_gs);
1041
Jens Axboeb66570d2011-10-01 11:11:35 -06001042 while (!exit_backend && nr_clients) {
Jens Axboec2cb6862012-02-23 20:56:12 +01001043 struct flist_head *entry, *tmp;
1044 struct fio_client *client;
1045
Jens Axboe82a4be12011-10-01 12:40:32 -06001046 i = 0;
Jens Axboec2cb6862012-02-23 20:56:12 +01001047 flist_for_each_safe(entry, tmp, &client_list) {
Jens Axboe82a4be12011-10-01 12:40:32 -06001048 client = flist_entry(entry, struct fio_client, list);
1049
Jens Axboe3ec62ec2012-03-01 12:01:29 +01001050 if (!client->sent_job && !ops->stay_connected &&
Jens Axboec2cb6862012-02-23 20:56:12 +01001051 flist_empty(&client->cmd_list)) {
1052 remove_client(client);
1053 continue;
1054 }
1055
Jens Axboe82a4be12011-10-01 12:40:32 -06001056 pfds[i].fd = client->fd;
1057 pfds[i].events = POLLIN;
1058 i++;
1059 }
1060
Jens Axboec2cb6862012-02-23 20:56:12 +01001061 if (!nr_clients)
1062 break;
1063
Jens Axboe82a4be12011-10-01 12:40:32 -06001064 assert(i == nr_clients);
1065
Jens Axboe5c2857f2011-10-04 13:14:32 +02001066 do {
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001067 struct timeval tv;
1068
1069 gettimeofday(&tv, NULL);
1070 if (mtime_since(&eta_tv, &tv) >= 900) {
1071 request_client_etas();
1072 memcpy(&eta_tv, &tv, sizeof(tv));
Jens Axboe89c17072011-10-11 10:15:51 +02001073
Jens Axboeed727a42012-03-02 12:14:40 +01001074 if (fio_client_timed_out(ops))
Jens Axboe89c17072011-10-11 10:15:51 +02001075 break;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001076 }
1077
Jens Axboe5c2857f2011-10-04 13:14:32 +02001078 ret = poll(pfds, nr_clients, 100);
1079 if (ret < 0) {
1080 if (errno == EINTR)
1081 continue;
1082 log_err("fio: poll clients: %s\n", strerror(errno));
1083 break;
1084 } else if (!ret)
Jens Axboeb66570d2011-10-01 11:11:35 -06001085 continue;
Jens Axboe5c2857f2011-10-04 13:14:32 +02001086 } while (ret <= 0);
Jens Axboeb66570d2011-10-01 11:11:35 -06001087
1088 for (i = 0; i < nr_clients; i++) {
1089 if (!(pfds[i].revents & POLLIN))
1090 continue;
1091
1092 client = find_client_by_fd(pfds[i].fd);
1093 if (!client) {
Jens Axboe3c5f57e2011-10-06 12:37:50 +02001094 log_err("fio: unknown client fd %d\n", pfds[i].fd);
Jens Axboeb66570d2011-10-01 11:11:35 -06001095 continue;
1096 }
Jens Axboe3e47bd22012-02-29 13:45:02 +01001097 if (!fio_handle_client(client, ops)) {
Jens Axboe28d3ab02011-10-05 20:45:37 +02001098 log_info("client: host=%s disconnected\n",
1099 client->hostname);
1100 remove_client(client);
Jens Axboe498c92c2011-10-17 09:14:42 +02001101 retval = 1;
Jens Axboe38990762011-10-17 13:31:33 +02001102 } else if (client->error)
Jens Axboe498c92c2011-10-17 09:14:42 +02001103 retval = 1;
Jens Axboeb66570d2011-10-01 11:11:35 -06001104 }
1105 }
1106
1107 free(pfds);
Jens Axboe498c92c2011-10-17 09:14:42 +02001108 return retval;
Jens Axboeb66570d2011-10-01 11:11:35 -06001109}