blob: 10dc53be98f75663f92bd0ebc1a3543dbd67c7ea [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);
Jens Axboe6b79c802012-03-08 10:51:36 +010029static void handle_stop(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,
Jens Axboe6b79c802012-03-08 10:51:36 +010036 .stop = handle_stop,
Jens Axboea5276612012-03-04 15:15:08 +010037 .eta = display_thread_status,
Jens Axboe0420ba62012-02-29 11:16:52 +010038 .probe = handle_probe,
Stephen M. Camerondd366722012-02-24 08:17:30 +010039};
40
Jens Axboeaf9c9fb2011-10-09 21:54:10 +020041static struct timeval eta_tv;
Jens Axboe48fbb462011-10-09 12:19:08 +020042
Jens Axboeb66570d2011-10-01 11:11:35 -060043static FLIST_HEAD(client_list);
Jens Axboe82c1ed32011-10-10 08:56:18 +020044static FLIST_HEAD(eta_list);
Jens Axboeb66570d2011-10-01 11:11:35 -060045
Jens Axboe3f3a4542011-10-10 21:11:09 +020046static FLIST_HEAD(arg_list);
47
Jens Axboe3650a3c2012-03-05 14:09:03 +010048struct thread_stat client_ts;
49struct group_run_stats client_gs;
50int sum_stat_clients;
51
Jens Axboe37f0c1a2011-10-11 14:08:33 +020052static int sum_stat_nr;
53
Jens Axboe3c5f57e2011-10-06 12:37:50 +020054#define FIO_CLIENT_HASH_BITS 7
55#define FIO_CLIENT_HASH_SZ (1 << FIO_CLIENT_HASH_BITS)
56#define FIO_CLIENT_HASH_MASK (FIO_CLIENT_HASH_SZ - 1)
Jens Axboebebe6392011-10-07 10:00:51 +020057static struct flist_head client_hash[FIO_CLIENT_HASH_SZ];
Jens Axboe3c5f57e2011-10-06 12:37:50 +020058
Jens Axboebebe6392011-10-07 10:00:51 +020059static void fio_client_add_hash(struct fio_client *client)
Jens Axboe3c5f57e2011-10-06 12:37:50 +020060{
61 int bucket = hash_long(client->fd, FIO_CLIENT_HASH_BITS);
62
63 bucket &= FIO_CLIENT_HASH_MASK;
Jens Axboebebe6392011-10-07 10:00:51 +020064 flist_add(&client->hash_list, &client_hash[bucket]);
Jens Axboe3c5f57e2011-10-06 12:37:50 +020065}
66
Jens Axboebebe6392011-10-07 10:00:51 +020067static void fio_client_remove_hash(struct fio_client *client)
Jens Axboe3c5f57e2011-10-06 12:37:50 +020068{
Jens Axboebebe6392011-10-07 10:00:51 +020069 if (!flist_empty(&client->hash_list))
70 flist_del_init(&client->hash_list);
Jens Axboe3c5f57e2011-10-06 12:37:50 +020071}
72
73static void fio_init fio_client_hash_init(void)
74{
75 int i;
76
Jens Axboebebe6392011-10-07 10:00:51 +020077 for (i = 0; i < FIO_CLIENT_HASH_SZ; i++)
78 INIT_FLIST_HEAD(&client_hash[i]);
Jens Axboe3c5f57e2011-10-06 12:37:50 +020079}
80
Jens Axboeb66570d2011-10-01 11:11:35 -060081static struct fio_client *find_client_by_fd(int fd)
82{
Jens Axboe3c5f57e2011-10-06 12:37:50 +020083 int bucket = hash_long(fd, FIO_CLIENT_HASH_BITS) & FIO_CLIENT_HASH_MASK;
Jens Axboeb66570d2011-10-01 11:11:35 -060084 struct fio_client *client;
85 struct flist_head *entry;
86
Jens Axboebebe6392011-10-07 10:00:51 +020087 flist_for_each(entry, &client_hash[bucket]) {
88 client = flist_entry(entry, struct fio_client, hash_list);
Jens Axboeb66570d2011-10-01 11:11:35 -060089
Jens Axboe5121a9a2012-03-05 13:32:47 +010090 if (client->fd == fd) {
91 client->refs++;
Jens Axboeb66570d2011-10-01 11:11:35 -060092 return client;
Jens Axboe5121a9a2012-03-05 13:32:47 +010093 }
Jens Axboeb66570d2011-10-01 11:11:35 -060094 }
95
96 return NULL;
97}
98
Jens Axboeb66570d2011-10-01 11:11:35 -060099static void remove_client(struct fio_client *client)
100{
Jens Axboe5121a9a2012-03-05 13:32:47 +0100101 assert(client->refs);
102
103 if (--client->refs)
104 return;
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 Axboea5276612012-03-04 15:15:08 +0100113 fio_client_dec_jobs_eta(client->eta_in_flight, client->ops->eta);
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 Axboe5121a9a2012-03-05 13:32:47 +0100127static void put_client(struct fio_client *client)
128{
129 remove_client(client);
130}
131
Jens Axboefa2ea802011-10-08 21:07:29 +0200132static void __fio_client_add_cmd_option(struct fio_client *client,
133 const char *opt)
Jens Axboe81179ee2011-10-04 12:42:06 +0200134{
Jens Axboe39e8e012011-10-04 13:46:08 +0200135 int index;
136
137 index = client->argc++;
Jens Axboe81179ee2011-10-04 12:42:06 +0200138 client->argv = realloc(client->argv, sizeof(char *) * client->argc);
Jens Axboe39e8e012011-10-04 13:46:08 +0200139 client->argv[index] = strdup(opt);
140 dprint(FD_NET, "client: add cmd %d: %s\n", index, opt);
Jens Axboe81179ee2011-10-04 12:42:06 +0200141}
142
Jens Axboefa2ea802011-10-08 21:07:29 +0200143void fio_client_add_cmd_option(void *cookie, const char *opt)
Jens Axboe81179ee2011-10-04 12:42:06 +0200144{
Jens Axboebebe6392011-10-07 10:00:51 +0200145 struct fio_client *client = cookie;
Jens Axboe3f3a4542011-10-10 21:11:09 +0200146 struct flist_head *entry;
Jens Axboe81179ee2011-10-04 12:42:06 +0200147
Jens Axboebebe6392011-10-07 10:00:51 +0200148 if (!client || !opt)
Jens Axboefa2ea802011-10-08 21:07:29 +0200149 return;
Jens Axboe81179ee2011-10-04 12:42:06 +0200150
Jens Axboefa2ea802011-10-08 21:07:29 +0200151 __fio_client_add_cmd_option(client, opt);
Jens Axboe3f3a4542011-10-10 21:11:09 +0200152
153 /*
154 * Duplicate arguments to shared client group
155 */
156 flist_for_each(entry, &arg_list) {
157 client = flist_entry(entry, struct fio_client, arg_list);
158
159 __fio_client_add_cmd_option(client, opt);
160 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200161}
162
Jens Axboea5276612012-03-04 15:15:08 +0100163struct fio_client *fio_client_add_explicit(struct client_ops *ops,
164 const char *hostname, int type,
Jens Axboe3ec62ec2012-03-01 12:01:29 +0100165 int port)
166{
167 struct fio_client *client;
168
169 client = malloc(sizeof(*client));
170 memset(client, 0, sizeof(*client));
171
172 INIT_FLIST_HEAD(&client->list);
173 INIT_FLIST_HEAD(&client->hash_list);
174 INIT_FLIST_HEAD(&client->arg_list);
175 INIT_FLIST_HEAD(&client->eta_list);
176 INIT_FLIST_HEAD(&client->cmd_list);
177
178 client->hostname = strdup(hostname);
179
180 if (type == Fio_client_socket)
181 client->is_sock = 1;
182 else {
183 int ipv6;
184
185 ipv6 = type == Fio_client_ipv6;
186 if (fio_server_parse_host(hostname, &ipv6,
187 &client->addr.sin_addr,
188 &client->addr6.sin6_addr))
189 goto err;
190
191 client->port = port;
192 }
193
194 client->fd = -1;
Jens Axboea5276612012-03-04 15:15:08 +0100195 client->ops = ops;
Jens Axboe5121a9a2012-03-05 13:32:47 +0100196 client->refs = 1;
Jens Axboe3ec62ec2012-03-01 12:01:29 +0100197
198 __fio_client_add_cmd_option(client, "fio");
199
200 flist_add(&client->list, &client_list);
201 nr_clients++;
202 dprint(FD_NET, "client: added <%s>\n", client->hostname);
203 return client;
204err:
205 free(client);
206 return NULL;
207}
208
Jens Axboea5276612012-03-04 15:15:08 +0100209int fio_client_add(struct client_ops *ops, const char *hostname, void **cookie)
Jens Axboe132159a2011-09-30 15:01:32 -0600210{
Jens Axboe3f3a4542011-10-10 21:11:09 +0200211 struct fio_client *existing = *cookie;
Jens Axboeb66570d2011-10-01 11:11:35 -0600212 struct fio_client *client;
Jens Axboe132159a2011-09-30 15:01:32 -0600213
Jens Axboe3f3a4542011-10-10 21:11:09 +0200214 if (existing) {
215 /*
216 * We always add our "exec" name as the option, hence 1
217 * means empty.
218 */
219 if (existing->argc == 1)
220 flist_add_tail(&existing->arg_list, &arg_list);
221 else {
222 while (!flist_empty(&arg_list))
223 flist_del_init(arg_list.next);
224 }
225 }
226
Jens Axboeb66570d2011-10-01 11:11:35 -0600227 client = malloc(sizeof(*client));
Jens Axboea37f69b2011-10-01 12:24:21 -0600228 memset(client, 0, sizeof(*client));
Jens Axboe81179ee2011-10-04 12:42:06 +0200229
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200230 INIT_FLIST_HEAD(&client->list);
Jens Axboebebe6392011-10-07 10:00:51 +0200231 INIT_FLIST_HEAD(&client->hash_list);
Jens Axboe3f3a4542011-10-10 21:11:09 +0200232 INIT_FLIST_HEAD(&client->arg_list);
Jens Axboe82c1ed32011-10-10 08:56:18 +0200233 INIT_FLIST_HEAD(&client->eta_list);
Jens Axboe89c17072011-10-11 10:15:51 +0200234 INIT_FLIST_HEAD(&client->cmd_list);
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200235
Jens Axboebebe6392011-10-07 10:00:51 +0200236 if (fio_server_parse_string(hostname, &client->hostname,
237 &client->is_sock, &client->port,
Jens Axboe811826b2011-10-24 09:11:50 +0200238 &client->addr.sin_addr,
239 &client->addr6.sin6_addr,
240 &client->ipv6))
Jens Axboebebe6392011-10-07 10:00:51 +0200241 return -1;
242
Jens Axboea37f69b2011-10-01 12:24:21 -0600243 client->fd = -1;
Jens Axboea5276612012-03-04 15:15:08 +0100244 client->ops = ops;
Jens Axboe5121a9a2012-03-05 13:32:47 +0100245 client->refs = 1;
Jens Axboe81179ee2011-10-04 12:42:06 +0200246
247 __fio_client_add_cmd_option(client, "fio");
248
Jens Axboea37f69b2011-10-01 12:24:21 -0600249 flist_add(&client->list, &client_list);
250 nr_clients++;
Jens Axboebebe6392011-10-07 10:00:51 +0200251 dprint(FD_NET, "client: added <%s>\n", client->hostname);
252 *cookie = client;
253 return 0;
Jens Axboea37f69b2011-10-01 12:24:21 -0600254}
255
Jens Axboe87aa8f12011-10-06 21:24:13 +0200256static int fio_client_connect_ip(struct fio_client *client)
Jens Axboea37f69b2011-10-01 12:24:21 -0600257{
Jens Axboe811826b2011-10-24 09:11:50 +0200258 struct sockaddr *addr;
259 fio_socklen_t socklen;
260 int fd, domain;
Jens Axboe132159a2011-09-30 15:01:32 -0600261
Jens Axboe811826b2011-10-24 09:11:50 +0200262 if (client->ipv6) {
263 client->addr6.sin6_family = AF_INET6;
264 client->addr6.sin6_port = htons(client->port);
265 domain = AF_INET6;
266 addr = (struct sockaddr *) &client->addr6;
267 socklen = sizeof(client->addr6);
268 } else {
269 client->addr.sin_family = AF_INET;
270 client->addr.sin_port = htons(client->port);
271 domain = AF_INET;
272 addr = (struct sockaddr *) &client->addr;
273 socklen = sizeof(client->addr);
274 }
Jens Axboe132159a2011-09-30 15:01:32 -0600275
Jens Axboe811826b2011-10-24 09:11:50 +0200276 fd = socket(domain, SOCK_STREAM, 0);
Jens Axboe132159a2011-09-30 15:01:32 -0600277 if (fd < 0) {
278 log_err("fio: socket: %s\n", strerror(errno));
Jens Axboe87aa8f12011-10-06 21:24:13 +0200279 return -1;
Jens Axboe132159a2011-09-30 15:01:32 -0600280 }
281
Jens Axboe811826b2011-10-24 09:11:50 +0200282 if (connect(fd, addr, socklen) < 0) {
Jens Axboe132159a2011-09-30 15:01:32 -0600283 log_err("fio: connect: %s\n", strerror(errno));
Jens Axboea7de0a12011-10-07 12:55:14 +0200284 log_err("fio: failed to connect to %s:%u\n", client->hostname,
285 client->port);
Jens Axboeb94cba42011-10-06 21:27:10 +0200286 close(fd);
Jens Axboe87aa8f12011-10-06 21:24:13 +0200287 return -1;
Jens Axboe132159a2011-09-30 15:01:32 -0600288 }
289
Jens Axboe87aa8f12011-10-06 21:24:13 +0200290 return fd;
291}
292
293static int fio_client_connect_sock(struct fio_client *client)
294{
295 struct sockaddr_un *addr = &client->addr_un;
296 fio_socklen_t len;
297 int fd;
298
299 memset(addr, 0, sizeof(*addr));
300 addr->sun_family = AF_UNIX;
301 strcpy(addr->sun_path, client->hostname);
302
303 fd = socket(AF_UNIX, SOCK_STREAM, 0);
304 if (fd < 0) {
305 log_err("fio: socket: %s\n", strerror(errno));
306 return -1;
307 }
308
309 len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
310 if (connect(fd, (struct sockaddr *) addr, len) < 0) {
311 log_err("fio: connect; %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200312 close(fd);
Jens Axboe87aa8f12011-10-06 21:24:13 +0200313 return -1;
314 }
315
316 return fd;
317}
318
319static int fio_client_connect(struct fio_client *client)
320{
321 int fd;
322
323 dprint(FD_NET, "client: connect to host %s\n", client->hostname);
324
Jens Axboe87aa8f12011-10-06 21:24:13 +0200325 if (client->is_sock)
326 fd = fio_client_connect_sock(client);
327 else
328 fd = fio_client_connect_ip(client);
329
Jens Axboe89c17072011-10-11 10:15:51 +0200330 dprint(FD_NET, "client: %s connected %d\n", client->hostname, fd);
331
Jens Axboe87aa8f12011-10-06 21:24:13 +0200332 if (fd < 0)
333 return 1;
334
Jens Axboeb66570d2011-10-01 11:11:35 -0600335 client->fd = fd;
Jens Axboebebe6392011-10-07 10:00:51 +0200336 fio_client_add_hash(client);
Jens Axboe81179ee2011-10-04 12:42:06 +0200337 client->state = Client_connected;
Jens Axboe132159a2011-09-30 15:01:32 -0600338 return 0;
339}
340
Jens Axboecc0df002011-10-03 20:53:32 +0200341void fio_clients_terminate(void)
342{
343 struct flist_head *entry;
344 struct fio_client *client;
345
Jens Axboe60efd142011-10-04 13:30:11 +0200346 dprint(FD_NET, "client: terminate clients\n");
347
Jens Axboecc0df002011-10-03 20:53:32 +0200348 flist_for_each(entry, &client_list) {
349 client = flist_entry(entry, struct fio_client, list);
350
Jens Axboe89c17072011-10-11 10:15:51 +0200351 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_QUIT, 0, NULL);
Jens Axboecc0df002011-10-03 20:53:32 +0200352 }
353}
354
355static void sig_int(int sig)
356{
Jens Axboebebe6392011-10-07 10:00:51 +0200357 dprint(FD_NET, "client: got signal %d\n", sig);
Jens Axboecc0df002011-10-03 20:53:32 +0200358 fio_clients_terminate();
359}
360
361static void client_signal_handler(void)
362{
363 struct sigaction act;
364
365 memset(&act, 0, sizeof(act));
366 act.sa_handler = sig_int;
367 act.sa_flags = SA_RESTART;
368 sigaction(SIGINT, &act, NULL);
369
370 memset(&act, 0, sizeof(act));
371 act.sa_handler = sig_int;
372 act.sa_flags = SA_RESTART;
373 sigaction(SIGTERM, &act, NULL);
374}
375
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200376static void probe_client(struct fio_client *client)
377{
Jens Axboe60efd142011-10-04 13:30:11 +0200378 dprint(FD_NET, "client: send probe\n");
379
Jens Axboe89c17072011-10-11 10:15:51 +0200380 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_PROBE, 0, &client->cmd_list);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200381}
382
Jens Axboe81179ee2011-10-04 12:42:06 +0200383static int send_client_cmd_line(struct fio_client *client)
384{
Jens Axboefa2ea802011-10-08 21:07:29 +0200385 struct cmd_single_line_pdu *cslp;
386 struct cmd_line_pdu *clp;
387 unsigned long offset;
Jens Axboe7f868312011-10-10 09:55:21 +0200388 unsigned int *lens;
Jens Axboefa2ea802011-10-08 21:07:29 +0200389 void *pdu;
390 size_t mem;
Jens Axboe81179ee2011-10-04 12:42:06 +0200391 int i, ret;
392
Jens Axboe39e8e012011-10-04 13:46:08 +0200393 dprint(FD_NET, "client: send cmdline %d\n", client->argc);
Jens Axboe60efd142011-10-04 13:30:11 +0200394
Jens Axboe7f868312011-10-10 09:55:21 +0200395 lens = malloc(client->argc * sizeof(unsigned int));
396
Jens Axboefa2ea802011-10-08 21:07:29 +0200397 /*
398 * Find out how much mem we need
399 */
Jens Axboe7f868312011-10-10 09:55:21 +0200400 for (i = 0, mem = 0; i < client->argc; i++) {
401 lens[i] = strlen(client->argv[i]) + 1;
402 mem += lens[i];
403 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200404
Jens Axboefa2ea802011-10-08 21:07:29 +0200405 /*
406 * We need one cmd_line_pdu, and argc number of cmd_single_line_pdu
407 */
408 mem += sizeof(*clp) + (client->argc * sizeof(*cslp));
409
410 pdu = malloc(mem);
411 clp = pdu;
412 offset = sizeof(*clp);
413
414 for (i = 0; i < client->argc; i++) {
Jens Axboe7f868312011-10-10 09:55:21 +0200415 uint16_t arg_len = lens[i];
Jens Axboefa2ea802011-10-08 21:07:29 +0200416
417 cslp = pdu + offset;
418 strcpy((char *) cslp->text, client->argv[i]);
419 cslp->len = cpu_to_le16(arg_len);
420 offset += sizeof(*cslp) + arg_len;
421 }
422
Jens Axboe7f868312011-10-10 09:55:21 +0200423 free(lens);
Jens Axboefa2ea802011-10-08 21:07:29 +0200424 clp->lines = cpu_to_le16(client->argc);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200425 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOBLINE, pdu, mem, 0);
Jens Axboe81179ee2011-10-04 12:42:06 +0200426 free(pdu);
427 return ret;
428}
429
Jens Axboea37f69b2011-10-01 12:24:21 -0600430int fio_clients_connect(void)
431{
432 struct fio_client *client;
433 struct flist_head *entry, *tmp;
434 int ret;
435
Bruce Cran93bcfd22012-02-20 20:18:19 +0100436#ifdef WIN32
437 WSADATA wsd;
438 WSAStartup(MAKEWORD(2,2), &wsd);
439#endif
440
Jens Axboe60efd142011-10-04 13:30:11 +0200441 dprint(FD_NET, "client: connect all\n");
442
Jens Axboecc0df002011-10-03 20:53:32 +0200443 client_signal_handler();
444
Jens Axboea37f69b2011-10-01 12:24:21 -0600445 flist_for_each_safe(entry, tmp, &client_list) {
446 client = flist_entry(entry, struct fio_client, list);
447
448 ret = fio_client_connect(client);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200449 if (ret) {
Jens Axboea37f69b2011-10-01 12:24:21 -0600450 remove_client(client);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200451 continue;
452 }
453
454 probe_client(client);
Jens Axboe81179ee2011-10-04 12:42:06 +0200455
456 if (client->argc > 1)
457 send_client_cmd_line(client);
Jens Axboea37f69b2011-10-01 12:24:21 -0600458 }
459
460 return !nr_clients;
461}
462
Jens Axboeb9d2f302012-03-08 20:36:28 +0100463int fio_start_client(struct fio_client *client)
464{
465 dprint(FD_NET, "client: start %s\n", client->hostname);
466 return fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_RUN, 0, NULL);
467}
468
469int fio_start_all_clients(void)
470{
471 struct fio_client *client;
472 struct flist_head *entry, *tmp;
473 int ret;
474
475 dprint(FD_NET, "client: start all\n");
476
477 flist_for_each_safe(entry, tmp, &client_list) {
478 client = flist_entry(entry, struct fio_client, list);
479
480 ret = fio_start_client(client);
481 if (ret) {
482 remove_client(client);
483 continue;
484 }
485 }
486
487 return flist_empty(&client_list);
488}
489
Jens Axboe132159a2011-09-30 15:01:32 -0600490/*
491 * Send file contents to server backend. We could use sendfile(), but to remain
492 * more portable lets just read/write the darn thing.
493 */
Jens Axboea37f69b2011-10-01 12:24:21 -0600494static int fio_client_send_ini(struct fio_client *client, const char *filename)
Jens Axboe132159a2011-09-30 15:01:32 -0600495{
496 struct stat sb;
497 char *p, *buf;
498 off_t len;
499 int fd, ret;
500
Jens Axboe46c48f12011-10-01 12:50:51 -0600501 dprint(FD_NET, "send ini %s to %s\n", filename, client->hostname);
502
Jens Axboe132159a2011-09-30 15:01:32 -0600503 fd = open(filename, O_RDONLY);
504 if (fd < 0) {
Jens Axboee951bdc2011-10-05 21:58:45 +0200505 log_err("fio: job file <%s> open: %s\n", filename, strerror(errno));
Jens Axboe132159a2011-09-30 15:01:32 -0600506 return 1;
507 }
508
509 if (fstat(fd, &sb) < 0) {
510 log_err("fio: job file stat: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200511 close(fd);
Jens Axboe132159a2011-09-30 15:01:32 -0600512 return 1;
513 }
514
515 buf = malloc(sb.st_size);
516
517 len = sb.st_size;
518 p = buf;
519 do {
520 ret = read(fd, p, len);
521 if (ret > 0) {
522 len -= ret;
523 if (!len)
524 break;
525 p += ret;
526 continue;
527 } else if (!ret)
528 break;
529 else if (errno == EAGAIN || errno == EINTR)
530 continue;
531 } while (1);
532
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200533 if (len) {
534 log_err("fio: failed reading job file %s\n", filename);
Jens Axboeb94cba42011-10-06 21:27:10 +0200535 close(fd);
Jens Axboec524ef72011-10-07 12:23:34 +0200536 free(buf);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200537 return 1;
538 }
539
Jens Axboec2cb6862012-02-23 20:56:12 +0100540 client->sent_job = 1;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200541 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, buf, sb.st_size, 0);
Jens Axboe132159a2011-09-30 15:01:32 -0600542 free(buf);
Jens Axboeb94cba42011-10-06 21:27:10 +0200543 close(fd);
Jens Axboe132159a2011-09-30 15:01:32 -0600544 return ret;
545}
Jens Axboe37db14f2011-09-30 17:00:42 -0600546
Jens Axboea37f69b2011-10-01 12:24:21 -0600547int fio_clients_send_ini(const char *filename)
548{
549 struct fio_client *client;
550 struct flist_head *entry, *tmp;
551
552 flist_for_each_safe(entry, tmp, &client_list) {
553 client = flist_entry(entry, struct fio_client, list);
554
555 if (fio_client_send_ini(client, filename))
556 remove_client(client);
Jens Axboec2cb6862012-02-23 20:56:12 +0100557
558 client->sent_job = 1;
Jens Axboea37f69b2011-10-01 12:24:21 -0600559 }
560
561 return !nr_clients;
562}
563
Jens Axboea64e88d2011-10-03 14:20:01 +0200564static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
565{
566 dst->max_val = le64_to_cpu(src->max_val);
567 dst->min_val = le64_to_cpu(src->min_val);
568 dst->samples = le64_to_cpu(src->samples);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200569
570 /*
571 * Floats arrive as IEEE 754 encoded uint64_t, convert back to double
572 */
573 dst->mean.u.f = fio_uint64_to_double(le64_to_cpu(dst->mean.u.i));
574 dst->S.u.f = fio_uint64_to_double(le64_to_cpu(dst->S.u.i));
Jens Axboea64e88d2011-10-03 14:20:01 +0200575}
576
577static void convert_ts(struct thread_stat *dst, struct thread_stat *src)
578{
579 int i, j;
580
581 dst->error = le32_to_cpu(src->error);
582 dst->groupid = le32_to_cpu(src->groupid);
583 dst->pid = le32_to_cpu(src->pid);
584 dst->members = le32_to_cpu(src->members);
585
586 for (i = 0; i < 2; i++) {
587 convert_io_stat(&dst->clat_stat[i], &src->clat_stat[i]);
588 convert_io_stat(&dst->slat_stat[i], &src->slat_stat[i]);
589 convert_io_stat(&dst->lat_stat[i], &src->lat_stat[i]);
590 convert_io_stat(&dst->bw_stat[i], &src->bw_stat[i]);
591 }
592
593 dst->usr_time = le64_to_cpu(src->usr_time);
594 dst->sys_time = le64_to_cpu(src->sys_time);
595 dst->ctx = le64_to_cpu(src->ctx);
596 dst->minf = le64_to_cpu(src->minf);
597 dst->majf = le64_to_cpu(src->majf);
598 dst->clat_percentiles = le64_to_cpu(src->clat_percentiles);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200599
600 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
601 fio_fp64_t *fps = &src->percentile_list[i];
602 fio_fp64_t *fpd = &dst->percentile_list[i];
603
604 fpd->u.f = fio_uint64_to_double(le64_to_cpu(fps->u.i));
605 }
Jens Axboea64e88d2011-10-03 14:20:01 +0200606
607 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
608 dst->io_u_map[i] = le32_to_cpu(src->io_u_map[i]);
609 dst->io_u_submit[i] = le32_to_cpu(src->io_u_submit[i]);
610 dst->io_u_complete[i] = le32_to_cpu(src->io_u_complete[i]);
611 }
612
613 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
614 dst->io_u_lat_u[i] = le32_to_cpu(src->io_u_lat_u[i]);
615 dst->io_u_lat_m[i] = le32_to_cpu(src->io_u_lat_m[i]);
616 }
617
618 for (i = 0; i < 2; i++)
619 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
620 dst->io_u_plat[i][j] = le32_to_cpu(src->io_u_plat[i][j]);
621
622 for (i = 0; i < 3; i++) {
623 dst->total_io_u[i] = le64_to_cpu(src->total_io_u[i]);
Jens Axboe93eee042011-10-03 21:08:48 +0200624 dst->short_io_u[i] = le64_to_cpu(src->short_io_u[i]);
Jens Axboea64e88d2011-10-03 14:20:01 +0200625 }
626
627 dst->total_submit = le64_to_cpu(src->total_submit);
628 dst->total_complete = le64_to_cpu(src->total_complete);
629
630 for (i = 0; i < 2; i++) {
631 dst->io_bytes[i] = le64_to_cpu(src->io_bytes[i]);
632 dst->runtime[i] = le64_to_cpu(src->runtime[i]);
633 }
634
635 dst->total_run_time = le64_to_cpu(src->total_run_time);
636 dst->continue_on_error = le16_to_cpu(src->continue_on_error);
637 dst->total_err_count = le64_to_cpu(src->total_err_count);
Jens Axboeddcc0b62011-10-03 14:45:27 +0200638 dst->first_error = le32_to_cpu(src->first_error);
639 dst->kb_base = le32_to_cpu(src->kb_base);
Jens Axboea64e88d2011-10-03 14:20:01 +0200640}
641
642static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
643{
644 int i;
645
646 for (i = 0; i < 2; i++) {
647 dst->max_run[i] = le64_to_cpu(src->max_run[i]);
648 dst->min_run[i] = le64_to_cpu(src->min_run[i]);
649 dst->max_bw[i] = le64_to_cpu(src->max_bw[i]);
650 dst->min_bw[i] = le64_to_cpu(src->min_bw[i]);
651 dst->io_kb[i] = le64_to_cpu(src->io_kb[i]);
652 dst->agg[i] = le64_to_cpu(src->agg[i]);
653 }
654
655 dst->kb_base = le32_to_cpu(src->kb_base);
656 dst->groupid = le32_to_cpu(src->groupid);
657}
658
Jens Axboe89e5fad2012-03-05 09:21:12 +0100659static void handle_ts(struct fio_client *client, struct fio_net_cmd *cmd)
Jens Axboea64e88d2011-10-03 14:20:01 +0200660{
661 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
662
Jens Axboea64e88d2011-10-03 14:20:01 +0200663 show_thread_status(&p->ts, &p->rs);
Jens Axboe37f0c1a2011-10-11 14:08:33 +0200664
665 if (sum_stat_clients == 1)
666 return;
667
668 sum_thread_stats(&client_ts, &p->ts, sum_stat_nr);
669 sum_group_stats(&client_gs, &p->rs);
670
671 client_ts.members++;
672 client_ts.groupid = p->ts.groupid;
673
674 if (++sum_stat_nr == sum_stat_clients) {
675 strcpy(client_ts.name, "All clients");
676 show_thread_status(&client_ts, &client_gs);
677 }
Jens Axboea64e88d2011-10-03 14:20:01 +0200678}
679
Jens Axboe89e5fad2012-03-05 09:21:12 +0100680static void handle_gs(struct fio_client *client, struct fio_net_cmd *cmd)
Jens Axboea64e88d2011-10-03 14:20:01 +0200681{
682 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
683
Jens Axboea64e88d2011-10-03 14:20:01 +0200684 show_group_stats(gs);
685}
686
Jens Axboe3bf236c2012-03-03 20:35:50 +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
Jens Axboed09a64a2011-10-13 11:38:56 +0200703static void convert_agg(struct disk_util_agg *agg)
704{
705 int i;
706
707 for (i = 0; i < 2; i++) {
708 agg->ios[i] = le32_to_cpu(agg->ios[i]);
709 agg->merges[i] = le32_to_cpu(agg->merges[i]);
710 agg->sectors[i] = le64_to_cpu(agg->sectors[i]);
711 agg->ticks[i] = le32_to_cpu(agg->ticks[i]);
712 }
713
714 agg->io_ticks = le32_to_cpu(agg->io_ticks);
715 agg->time_in_queue = le32_to_cpu(agg->time_in_queue);
716 agg->slavecount = le32_to_cpu(agg->slavecount);
Anton Blanchard823ba542011-11-07 14:16:26 +0100717 agg->max_util.u.f = fio_uint64_to_double(__le64_to_cpu(agg->max_util.u.i));
Jens Axboed09a64a2011-10-13 11:38:56 +0200718}
719
720static void convert_dus(struct disk_util_stat *dus)
721{
722 int i;
723
724 for (i = 0; i < 2; i++) {
725 dus->ios[i] = le32_to_cpu(dus->ios[i]);
726 dus->merges[i] = le32_to_cpu(dus->merges[i]);
727 dus->sectors[i] = le64_to_cpu(dus->sectors[i]);
728 dus->ticks[i] = le32_to_cpu(dus->ticks[i]);
729 }
730
731 dus->io_ticks = le32_to_cpu(dus->io_ticks);
732 dus->time_in_queue = le32_to_cpu(dus->time_in_queue);
733 dus->msec = le64_to_cpu(dus->msec);
734}
735
736static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd)
737{
738 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
739
Jens Axboed09a64a2011-10-13 11:38:56 +0200740 if (!client->disk_stats_shown) {
741 client->disk_stats_shown = 1;
742 log_info("\nDisk stats (read/write):\n");
743 }
744
Jens Axboef2f788d2011-10-13 14:03:52 +0200745 print_disk_util(&du->dus, &du->agg, terse_output);
Jens Axboed09a64a2011-10-13 11:38:56 +0200746}
747
Jens Axboe3bf236c2012-03-03 20:35:50 +0100748static void convert_jobs_eta(struct jobs_eta *je)
Jens Axboecf451d12011-10-03 16:48:30 +0200749{
Jens Axboecf451d12011-10-03 16:48:30 +0200750 int i;
751
752 je->nr_running = le32_to_cpu(je->nr_running);
753 je->nr_ramp = le32_to_cpu(je->nr_ramp);
754 je->nr_pending = le32_to_cpu(je->nr_pending);
755 je->files_open = le32_to_cpu(je->files_open);
Jens Axboecf451d12011-10-03 16:48:30 +0200756
757 for (i = 0; i < 2; i++) {
Jens Axboe3e47bd22012-02-29 13:45:02 +0100758 je->m_rate[i] = le32_to_cpu(je->m_rate[i]);
759 je->t_rate[i] = le32_to_cpu(je->t_rate[i]);
760 je->m_iops[i] = le32_to_cpu(je->m_iops[i]);
761 je->t_iops[i] = le32_to_cpu(je->t_iops[i]);
Jens Axboecf451d12011-10-03 16:48:30 +0200762 je->rate[i] = le32_to_cpu(je->rate[i]);
763 je->iops[i] = le32_to_cpu(je->iops[i]);
764 }
765
Jens Axboeb51eedb2011-10-09 12:13:39 +0200766 je->elapsed_sec = le64_to_cpu(je->elapsed_sec);
Jens Axboecf451d12011-10-03 16:48:30 +0200767 je->eta_sec = le64_to_cpu(je->eta_sec);
Jens Axboe8c621fb2012-03-08 12:30:48 +0100768 je->nr_threads = le32_to_cpu(je->nr_threads);
Jens Axboe48fbb462011-10-09 12:19:08 +0200769}
Jens Axboecf451d12011-10-03 16:48:30 +0200770
Jens Axboe3e47bd22012-02-29 13:45:02 +0100771void fio_client_sum_jobs_eta(struct jobs_eta *dst, struct jobs_eta *je)
Jens Axboe48fbb462011-10-09 12:19:08 +0200772{
Jens Axboe48fbb462011-10-09 12:19:08 +0200773 int i;
774
775 dst->nr_running += je->nr_running;
776 dst->nr_ramp += je->nr_ramp;
777 dst->nr_pending += je->nr_pending;
778 dst->files_open += je->files_open;
Jens Axboe48fbb462011-10-09 12:19:08 +0200779
780 for (i = 0; i < 2; i++) {
Jens Axboe3e47bd22012-02-29 13:45:02 +0100781 dst->m_rate[i] += je->m_rate[i];
782 dst->t_rate[i] += je->t_rate[i];
783 dst->m_iops[i] += je->m_iops[i];
784 dst->t_iops[i] += je->t_iops[i];
Jens Axboe48fbb462011-10-09 12:19:08 +0200785 dst->rate[i] += je->rate[i];
786 dst->iops[i] += je->iops[i];
787 }
788
789 dst->elapsed_sec += je->elapsed_sec;
790
791 if (je->eta_sec > dst->eta_sec)
792 dst->eta_sec = je->eta_sec;
Jens Axboe8c621fb2012-03-08 12:30:48 +0100793
794 dst->nr_threads += je->nr_threads;
795 /* we need to handle je->run_str too ... */
Jens Axboe48fbb462011-10-09 12:19:08 +0200796}
797
Jens Axboea5276612012-03-04 15:15:08 +0100798void fio_client_dec_jobs_eta(struct client_eta *eta, client_eta_op eta_fn)
Jens Axboe82c1ed32011-10-10 08:56:18 +0200799{
800 if (!--eta->pending) {
Jens Axboea5276612012-03-04 15:15:08 +0100801 eta_fn(&eta->eta);
Jens Axboe82c1ed32011-10-10 08:56:18 +0200802 free(eta);
803 }
804}
805
Jens Axboe89c17072011-10-11 10:15:51 +0200806static void remove_reply_cmd(struct fio_client *client, struct fio_net_cmd *cmd)
807{
808 struct fio_net_int_cmd *icmd = NULL;
809 struct flist_head *entry;
810
811 flist_for_each(entry, &client->cmd_list) {
812 icmd = flist_entry(entry, struct fio_net_int_cmd, list);
813
Jens Axboedf380932011-10-11 14:25:08 +0200814 if (cmd->tag == (uintptr_t) icmd)
Jens Axboe89c17072011-10-11 10:15:51 +0200815 break;
816
817 icmd = NULL;
818 }
819
820 if (!icmd) {
821 log_err("fio: client: unable to find matching tag\n");
822 return;
823 }
824
825 flist_del(&icmd->list);
826 cmd->tag = icmd->saved_tag;
827 free(icmd);
828}
829
Jens Axboe82c1ed32011-10-10 08:56:18 +0200830static void handle_eta(struct fio_client *client, struct fio_net_cmd *cmd)
Jens Axboe48fbb462011-10-09 12:19:08 +0200831{
832 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
Jens Axboedf380932011-10-11 14:25:08 +0200833 struct client_eta *eta = (struct client_eta *) (uintptr_t) cmd->tag;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200834
835 dprint(FD_NET, "client: got eta tag %p, %d\n", eta, eta->pending);
Jens Axboe48fbb462011-10-09 12:19:08 +0200836
Jens Axboef77d2672011-10-10 14:36:07 +0200837 assert(client->eta_in_flight == eta);
838
839 client->eta_in_flight = NULL;
Jens Axboe82c1ed32011-10-10 08:56:18 +0200840 flist_del_init(&client->eta_list);
841
Jens Axboe3e47bd22012-02-29 13:45:02 +0100842 fio_client_sum_jobs_eta(&eta->eta, je);
Jens Axboea5276612012-03-04 15:15:08 +0100843 fio_client_dec_jobs_eta(eta, client->ops->eta);
Jens Axboecf451d12011-10-03 16:48:30 +0200844}
845
Jens Axboeb5296dd2011-10-07 16:35:56 +0200846static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd)
Jens Axboe2e03b4b2011-10-04 09:00:40 +0200847{
848 struct cmd_probe_pdu *probe = (struct cmd_probe_pdu *) cmd->payload;
Jens Axboed2333352011-10-11 15:07:23 +0200849 const char *os, *arch;
850 char bit[16];
Jens Axboe2e03b4b2011-10-04 09:00:40 +0200851
Jens Axboecca84642011-10-07 12:47:57 +0200852 os = fio_get_os_string(probe->os);
853 if (!os)
854 os = "unknown";
855
856 arch = fio_get_arch_string(probe->arch);
857 if (!arch)
858 os = "unknown";
859
Jens Axboed2333352011-10-11 15:07:23 +0200860 sprintf(bit, "%d-bit", probe->bpp * 8);
Jens Axboe38fdef22011-10-11 14:30:06 +0200861
862 log_info("hostname=%s, be=%u, %s, os=%s, arch=%s, fio=%u.%u.%u\n",
863 probe->hostname, probe->bigendian, bit, os, arch,
864 probe->fio_major, probe->fio_minor, probe->fio_patch);
Jens Axboeb5296dd2011-10-07 16:35:56 +0200865
866 if (!client->name)
867 client->name = strdup((char *) probe->hostname);
Jens Axboe2e03b4b2011-10-04 09:00:40 +0200868}
869
Jens Axboe11e950b2011-10-16 21:34:14 +0200870static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd)
871{
872 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
873
874 client->state = Client_started;
875 client->jobs = le32_to_cpu(pdu->jobs);
876}
877
878static void handle_stop(struct fio_client *client, struct fio_net_cmd *cmd)
879{
Jens Axboe498c92c2011-10-17 09:14:42 +0200880 if (client->error)
881 log_info("client <%s>: exited with error %d\n", client->hostname, client->error);
Jens Axboe11e950b2011-10-16 21:34:14 +0200882}
883
Jens Axboe6b79c802012-03-08 10:51:36 +0100884static void convert_stop(struct fio_net_cmd *cmd)
885{
886 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
887
888 pdu->error = le32_to_cpu(pdu->error);
889}
890
Jens Axboe084d1c62012-03-03 20:28:07 +0100891static void convert_text(struct fio_net_cmd *cmd)
892{
893 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
894
895 pdu->level = le32_to_cpu(pdu->level);
896 pdu->buf_len = le32_to_cpu(pdu->buf_len);
897 pdu->log_sec = le64_to_cpu(pdu->log_sec);
898 pdu->log_usec = le64_to_cpu(pdu->log_usec);
899}
900
Jens Axboea5276612012-03-04 15:15:08 +0100901int fio_handle_client(struct fio_client *client)
Jens Axboe37db14f2011-09-30 17:00:42 -0600902{
Jens Axboea5276612012-03-04 15:15:08 +0100903 struct client_ops *ops = client->ops;
Jens Axboe37db14f2011-09-30 17:00:42 -0600904 struct fio_net_cmd *cmd;
905
Jens Axboe60efd142011-10-04 13:30:11 +0200906 dprint(FD_NET, "client: handle %s\n", client->hostname);
907
Jens Axboee951bdc2011-10-05 21:58:45 +0200908 cmd = fio_net_recv_cmd(client->fd);
909 if (!cmd)
910 return 0;
Jens Axboec2c94582011-10-05 20:31:30 +0200911
Jens Axboeb9d2f302012-03-08 20:36:28 +0100912 dprint(FD_NET, "client: got cmd op %s from %s (pdu=%u)\n",
913 fio_server_op(cmd->opcode), client->hostname, cmd->pdu_len);
Jens Axboe46c48f12011-10-01 12:50:51 -0600914
Jens Axboee951bdc2011-10-05 21:58:45 +0200915 switch (cmd->opcode) {
916 case FIO_NET_CMD_QUIT:
Jens Axboe3ec62ec2012-03-01 12:01:29 +0100917 if (ops->quit)
918 ops->quit(client);
Jens Axboee951bdc2011-10-05 21:58:45 +0200919 remove_client(client);
920 free(cmd);
921 break;
Jens Axboe084d1c62012-03-03 20:28:07 +0100922 case FIO_NET_CMD_TEXT:
923 convert_text(cmd);
924 ops->text_op(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +0200925 free(cmd);
926 break;
Jens Axboe3bf236c2012-03-03 20:35:50 +0100927 case FIO_NET_CMD_DU: {
928 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
929
930 convert_dus(&du->dus);
931 convert_agg(&du->agg);
932
Stephen M. Camerondd366722012-02-24 08:17:30 +0100933 ops->disk_util(client, cmd);
Jens Axboed09a64a2011-10-13 11:38:56 +0200934 free(cmd);
935 break;
Jens Axboe3bf236c2012-03-03 20:35:50 +0100936 }
937 case FIO_NET_CMD_TS: {
938 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
939
940 convert_ts(&p->ts, &p->ts);
941 convert_gs(&p->rs, &p->rs);
942
Jens Axboe89e5fad2012-03-05 09:21:12 +0100943 ops->thread_status(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +0200944 free(cmd);
945 break;
Jens Axboe3bf236c2012-03-03 20:35:50 +0100946 }
947 case FIO_NET_CMD_GS: {
948 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
949
950 convert_gs(gs, gs);
951
Jens Axboe89e5fad2012-03-05 09:21:12 +0100952 ops->group_stats(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +0200953 free(cmd);
954 break;
Jens Axboe3bf236c2012-03-03 20:35:50 +0100955 }
956 case FIO_NET_CMD_ETA: {
957 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
958
Jens Axboe89c17072011-10-11 10:15:51 +0200959 remove_reply_cmd(client, cmd);
Jens Axboe3bf236c2012-03-03 20:35:50 +0100960 convert_jobs_eta(je);
Jens Axboea5276612012-03-04 15:15:08 +0100961 handle_eta(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +0200962 free(cmd);
963 break;
Jens Axboe3bf236c2012-03-03 20:35:50 +0100964 }
Jens Axboee951bdc2011-10-05 21:58:45 +0200965 case FIO_NET_CMD_PROBE:
Jens Axboe89c17072011-10-11 10:15:51 +0200966 remove_reply_cmd(client, cmd);
Stephen M. Camerondd366722012-02-24 08:17:30 +0100967 ops->probe(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +0200968 free(cmd);
969 break;
Jens Axboe5d7793a2012-03-08 19:59:16 +0100970 case FIO_NET_CMD_SERVER_START:
Jens Axboe01be0382011-10-15 14:43:41 +0200971 client->state = Client_running;
972 free(cmd);
973 break;
Jens Axboee951bdc2011-10-05 21:58:45 +0200974 case FIO_NET_CMD_START:
Jens Axboe11e950b2011-10-16 21:34:14 +0200975 handle_start(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +0200976 free(cmd);
977 break;
Jens Axboe6b79c802012-03-08 10:51:36 +0100978 case FIO_NET_CMD_STOP: {
979 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
980
981 convert_stop(cmd);
982 client->state = Client_stopped;
983 client->error = pdu->error;
984 ops->stop(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +0200985 free(cmd);
986 break;
Jens Axboe6b79c802012-03-08 10:51:36 +0100987 }
Jens Axboe807f9972012-03-02 10:25:24 +0100988 case FIO_NET_CMD_ADD_JOB:
989 if (ops->add_job)
990 ops->add_job(client, cmd);
991 free(cmd);
992 break;
Jens Axboee951bdc2011-10-05 21:58:45 +0200993 default:
Jens Axboe89c17072011-10-11 10:15:51 +0200994 log_err("fio: unknown client op: %s\n", fio_server_op(cmd->opcode));
Jens Axboee951bdc2011-10-05 21:58:45 +0200995 free(cmd);
996 break;
Jens Axboe37db14f2011-09-30 17:00:42 -0600997 }
998
Jens Axboee951bdc2011-10-05 21:58:45 +0200999 return 1;
Jens Axboe37db14f2011-09-30 17:00:42 -06001000}
Jens Axboeb66570d2011-10-01 11:11:35 -06001001
Jens Axboea5276612012-03-04 15:15:08 +01001002static void request_client_etas(struct client_ops *ops)
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001003{
1004 struct fio_client *client;
1005 struct flist_head *entry;
1006 struct client_eta *eta;
Jens Axboe82c1ed32011-10-10 08:56:18 +02001007 int skipped = 0;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001008
1009 dprint(FD_NET, "client: request eta (%d)\n", nr_clients);
1010
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001011 eta = malloc(sizeof(*eta));
1012 memset(&eta->eta, 0, sizeof(eta->eta));
1013 eta->pending = nr_clients;
1014
1015 flist_for_each(entry, &client_list) {
1016 client = flist_entry(entry, struct fio_client, list);
1017
Jens Axboe82c1ed32011-10-10 08:56:18 +02001018 if (!flist_empty(&client->eta_list)) {
1019 skipped++;
1020 continue;
1021 }
Jens Axboe01be0382011-10-15 14:43:41 +02001022 if (client->state != Client_running)
1023 continue;
Jens Axboe82c1ed32011-10-10 08:56:18 +02001024
Jens Axboef77d2672011-10-10 14:36:07 +02001025 assert(!client->eta_in_flight);
Jens Axboe82c1ed32011-10-10 08:56:18 +02001026 flist_add_tail(&client->eta_list, &eta_list);
Jens Axboef77d2672011-10-10 14:36:07 +02001027 client->eta_in_flight = eta;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001028 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_SEND_ETA,
Jens Axboedf380932011-10-11 14:25:08 +02001029 (uintptr_t) eta, &client->cmd_list);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001030 }
1031
Jens Axboe82c1ed32011-10-10 08:56:18 +02001032 while (skipped--)
Jens Axboea5276612012-03-04 15:15:08 +01001033 fio_client_dec_jobs_eta(eta, ops->eta);
Jens Axboe82c1ed32011-10-10 08:56:18 +02001034
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001035 dprint(FD_NET, "client: requested eta tag %p\n", eta);
1036}
1037
Jens Axboe89c17072011-10-11 10:15:51 +02001038static int client_check_cmd_timeout(struct fio_client *client,
1039 struct timeval *now)
1040{
1041 struct fio_net_int_cmd *cmd;
1042 struct flist_head *entry, *tmp;
1043 int ret = 0;
1044
1045 flist_for_each_safe(entry, tmp, &client->cmd_list) {
1046 cmd = flist_entry(entry, struct fio_net_int_cmd, list);
1047
1048 if (mtime_since(&cmd->tv, now) < FIO_NET_CLIENT_TIMEOUT)
1049 continue;
1050
1051 log_err("fio: client %s, timeout on cmd %s\n", client->hostname,
1052 fio_server_op(cmd->cmd.opcode));
1053 flist_del(&cmd->list);
1054 free(cmd);
1055 ret = 1;
1056 }
1057
1058 return flist_empty(&client->cmd_list) && ret;
1059}
1060
Jens Axboea5276612012-03-04 15:15:08 +01001061static int fio_check_clients_timed_out(void)
Jens Axboe89c17072011-10-11 10:15:51 +02001062{
1063 struct fio_client *client;
1064 struct flist_head *entry, *tmp;
1065 struct timeval tv;
1066 int ret = 0;
1067
1068 gettimeofday(&tv, NULL);
1069
1070 flist_for_each_safe(entry, tmp, &client_list) {
1071 client = flist_entry(entry, struct fio_client, list);
1072
1073 if (flist_empty(&client->cmd_list))
1074 continue;
1075
1076 if (!client_check_cmd_timeout(client, &tv))
1077 continue;
1078
Jens Axboea5276612012-03-04 15:15:08 +01001079 if (client->ops->timed_out)
1080 client->ops->timed_out(client);
Jens Axboeed727a42012-03-02 12:14:40 +01001081 else
1082 log_err("fio: client %s timed out\n", client->hostname);
1083
Jens Axboe89c17072011-10-11 10:15:51 +02001084 remove_client(client);
1085 ret = 1;
1086 }
1087
1088 return ret;
1089}
1090
Stephen M. Camerondd366722012-02-24 08:17:30 +01001091int fio_handle_clients(struct client_ops *ops)
Jens Axboeb66570d2011-10-01 11:11:35 -06001092{
Jens Axboeb66570d2011-10-01 11:11:35 -06001093 struct pollfd *pfds;
Jens Axboe498c92c2011-10-17 09:14:42 +02001094 int i, ret = 0, retval = 0;
Jens Axboeb66570d2011-10-01 11:11:35 -06001095
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001096 gettimeofday(&eta_tv, NULL);
1097
Jens Axboeb66570d2011-10-01 11:11:35 -06001098 pfds = malloc(nr_clients * sizeof(struct pollfd));
1099
Jens Axboe37f0c1a2011-10-11 14:08:33 +02001100 sum_stat_clients = nr_clients;
1101 init_thread_stat(&client_ts);
1102 init_group_run_stat(&client_gs);
1103
Jens Axboeb66570d2011-10-01 11:11:35 -06001104 while (!exit_backend && nr_clients) {
Jens Axboec2cb6862012-02-23 20:56:12 +01001105 struct flist_head *entry, *tmp;
1106 struct fio_client *client;
1107
Jens Axboe82a4be12011-10-01 12:40:32 -06001108 i = 0;
Jens Axboec2cb6862012-02-23 20:56:12 +01001109 flist_for_each_safe(entry, tmp, &client_list) {
Jens Axboe82a4be12011-10-01 12:40:32 -06001110 client = flist_entry(entry, struct fio_client, list);
1111
Jens Axboea5276612012-03-04 15:15:08 +01001112 if (!client->sent_job && !client->ops->stay_connected &&
Jens Axboec2cb6862012-02-23 20:56:12 +01001113 flist_empty(&client->cmd_list)) {
1114 remove_client(client);
1115 continue;
1116 }
1117
Jens Axboe82a4be12011-10-01 12:40:32 -06001118 pfds[i].fd = client->fd;
1119 pfds[i].events = POLLIN;
1120 i++;
1121 }
1122
Jens Axboec2cb6862012-02-23 20:56:12 +01001123 if (!nr_clients)
1124 break;
1125
Jens Axboe82a4be12011-10-01 12:40:32 -06001126 assert(i == nr_clients);
1127
Jens Axboe5c2857f2011-10-04 13:14:32 +02001128 do {
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001129 struct timeval tv;
1130
1131 gettimeofday(&tv, NULL);
1132 if (mtime_since(&eta_tv, &tv) >= 900) {
Jens Axboea5276612012-03-04 15:15:08 +01001133 request_client_etas(ops);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001134 memcpy(&eta_tv, &tv, sizeof(tv));
Jens Axboe89c17072011-10-11 10:15:51 +02001135
Jens Axboea5276612012-03-04 15:15:08 +01001136 if (fio_check_clients_timed_out())
Jens Axboe89c17072011-10-11 10:15:51 +02001137 break;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001138 }
1139
Jens Axboe5c2857f2011-10-04 13:14:32 +02001140 ret = poll(pfds, nr_clients, 100);
1141 if (ret < 0) {
1142 if (errno == EINTR)
1143 continue;
1144 log_err("fio: poll clients: %s\n", strerror(errno));
1145 break;
1146 } else if (!ret)
Jens Axboeb66570d2011-10-01 11:11:35 -06001147 continue;
Jens Axboe5c2857f2011-10-04 13:14:32 +02001148 } while (ret <= 0);
Jens Axboeb66570d2011-10-01 11:11:35 -06001149
1150 for (i = 0; i < nr_clients; i++) {
1151 if (!(pfds[i].revents & POLLIN))
1152 continue;
1153
1154 client = find_client_by_fd(pfds[i].fd);
1155 if (!client) {
Jens Axboe3c5f57e2011-10-06 12:37:50 +02001156 log_err("fio: unknown client fd %d\n", pfds[i].fd);
Jens Axboeb66570d2011-10-01 11:11:35 -06001157 continue;
1158 }
Jens Axboea5276612012-03-04 15:15:08 +01001159 if (!fio_handle_client(client)) {
Jens Axboe28d3ab02011-10-05 20:45:37 +02001160 log_info("client: host=%s disconnected\n",
1161 client->hostname);
1162 remove_client(client);
Jens Axboe498c92c2011-10-17 09:14:42 +02001163 retval = 1;
Jens Axboe38990762011-10-17 13:31:33 +02001164 } else if (client->error)
Jens Axboe498c92c2011-10-17 09:14:42 +02001165 retval = 1;
Jens Axboe5121a9a2012-03-05 13:32:47 +01001166 put_client(client);
Jens Axboeb66570d2011-10-01 11:11:35 -06001167 }
1168 }
1169
1170 free(pfds);
Jens Axboe498c92c2011-10-17 09:14:42 +02001171 return retval;
Jens Axboeb66570d2011-10-01 11:11:35 -06001172}