blob: 7267333d3128017e09d67ef7af8fa23db6a26951 [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 Axboe3650a3c2012-03-05 14:09:03 +010055struct thread_stat client_ts;
56struct group_run_stats client_gs;
57int sum_stat_clients;
58
Jens Axboe37f0c1a2011-10-11 14:08:33 +020059static 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
Jens Axboe5121a9a2012-03-05 13:32:47 +010097 if (client->fd == fd) {
98 client->refs++;
Jens Axboeb66570d2011-10-01 11:11:35 -060099 return client;
Jens Axboe5121a9a2012-03-05 13:32:47 +0100100 }
Jens Axboeb66570d2011-10-01 11:11:35 -0600101 }
102
103 return NULL;
104}
105
Jens Axboeb66570d2011-10-01 11:11:35 -0600106static void remove_client(struct fio_client *client)
107{
Jens Axboe5121a9a2012-03-05 13:32:47 +0100108 assert(client->refs);
109
110 if (--client->refs)
111 return;
112
Jens Axboe39e8e012011-10-04 13:46:08 +0200113 dprint(FD_NET, "client: removed <%s>\n", client->hostname);
Jens Axboeb66570d2011-10-01 11:11:35 -0600114 flist_del(&client->list);
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200115
Jens Axboebebe6392011-10-07 10:00:51 +0200116 fio_client_remove_hash(client);
Jens Axboe81179ee2011-10-04 12:42:06 +0200117
Jens Axboe82c1ed32011-10-10 08:56:18 +0200118 if (!flist_empty(&client->eta_list)) {
119 flist_del_init(&client->eta_list);
Jens Axboea5276612012-03-04 15:15:08 +0100120 fio_client_dec_jobs_eta(client->eta_in_flight, client->ops->eta);
Jens Axboe82c1ed32011-10-10 08:56:18 +0200121 }
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200122
Jens Axboeb66570d2011-10-01 11:11:35 -0600123 free(client->hostname);
Jens Axboe81179ee2011-10-04 12:42:06 +0200124 if (client->argv)
125 free(client->argv);
Jens Axboeb5296dd2011-10-07 16:35:56 +0200126 if (client->name)
127 free(client->name);
Jens Axboe81179ee2011-10-04 12:42:06 +0200128
Jens Axboeb66570d2011-10-01 11:11:35 -0600129 free(client);
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200130 nr_clients--;
Jens Axboe5fd0acb2011-10-11 14:20:22 +0200131 sum_stat_clients--;
Jens Axboeb66570d2011-10-01 11:11:35 -0600132}
Jens Axboe132159a2011-09-30 15:01:32 -0600133
Jens Axboe5121a9a2012-03-05 13:32:47 +0100134static void put_client(struct fio_client *client)
135{
136 remove_client(client);
137}
138
Jens Axboefa2ea802011-10-08 21:07:29 +0200139static void __fio_client_add_cmd_option(struct fio_client *client,
140 const char *opt)
Jens Axboe81179ee2011-10-04 12:42:06 +0200141{
Jens Axboe39e8e012011-10-04 13:46:08 +0200142 int index;
143
144 index = client->argc++;
Jens Axboe81179ee2011-10-04 12:42:06 +0200145 client->argv = realloc(client->argv, sizeof(char *) * client->argc);
Jens Axboe39e8e012011-10-04 13:46:08 +0200146 client->argv[index] = strdup(opt);
147 dprint(FD_NET, "client: add cmd %d: %s\n", index, opt);
Jens Axboe81179ee2011-10-04 12:42:06 +0200148}
149
Jens Axboefa2ea802011-10-08 21:07:29 +0200150void fio_client_add_cmd_option(void *cookie, const char *opt)
Jens Axboe81179ee2011-10-04 12:42:06 +0200151{
Jens Axboebebe6392011-10-07 10:00:51 +0200152 struct fio_client *client = cookie;
Jens Axboe3f3a4542011-10-10 21:11:09 +0200153 struct flist_head *entry;
Jens Axboe81179ee2011-10-04 12:42:06 +0200154
Jens Axboebebe6392011-10-07 10:00:51 +0200155 if (!client || !opt)
Jens Axboefa2ea802011-10-08 21:07:29 +0200156 return;
Jens Axboe81179ee2011-10-04 12:42:06 +0200157
Jens Axboefa2ea802011-10-08 21:07:29 +0200158 __fio_client_add_cmd_option(client, opt);
Jens Axboe3f3a4542011-10-10 21:11:09 +0200159
160 /*
161 * Duplicate arguments to shared client group
162 */
163 flist_for_each(entry, &arg_list) {
164 client = flist_entry(entry, struct fio_client, arg_list);
165
166 __fio_client_add_cmd_option(client, opt);
167 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200168}
169
Jens Axboea5276612012-03-04 15:15:08 +0100170struct fio_client *fio_client_add_explicit(struct client_ops *ops,
171 const char *hostname, int type,
Jens Axboe3ec62ec2012-03-01 12:01:29 +0100172 int port)
173{
174 struct fio_client *client;
175
176 client = malloc(sizeof(*client));
177 memset(client, 0, sizeof(*client));
178
179 INIT_FLIST_HEAD(&client->list);
180 INIT_FLIST_HEAD(&client->hash_list);
181 INIT_FLIST_HEAD(&client->arg_list);
182 INIT_FLIST_HEAD(&client->eta_list);
183 INIT_FLIST_HEAD(&client->cmd_list);
184
185 client->hostname = strdup(hostname);
186
187 if (type == Fio_client_socket)
188 client->is_sock = 1;
189 else {
190 int ipv6;
191
192 ipv6 = type == Fio_client_ipv6;
193 if (fio_server_parse_host(hostname, &ipv6,
194 &client->addr.sin_addr,
195 &client->addr6.sin6_addr))
196 goto err;
197
198 client->port = port;
199 }
200
201 client->fd = -1;
Jens Axboea5276612012-03-04 15:15:08 +0100202 client->ops = ops;
Jens Axboe5121a9a2012-03-05 13:32:47 +0100203 client->refs = 1;
Jens Axboe3ec62ec2012-03-01 12:01:29 +0100204
205 __fio_client_add_cmd_option(client, "fio");
206
207 flist_add(&client->list, &client_list);
208 nr_clients++;
209 dprint(FD_NET, "client: added <%s>\n", client->hostname);
210 return client;
211err:
212 free(client);
213 return NULL;
214}
215
Jens Axboea5276612012-03-04 15:15:08 +0100216int fio_client_add(struct client_ops *ops, const char *hostname, void **cookie)
Jens Axboe132159a2011-09-30 15:01:32 -0600217{
Jens Axboe3f3a4542011-10-10 21:11:09 +0200218 struct fio_client *existing = *cookie;
Jens Axboeb66570d2011-10-01 11:11:35 -0600219 struct fio_client *client;
Jens Axboe132159a2011-09-30 15:01:32 -0600220
Jens Axboe3f3a4542011-10-10 21:11:09 +0200221 if (existing) {
222 /*
223 * We always add our "exec" name as the option, hence 1
224 * means empty.
225 */
226 if (existing->argc == 1)
227 flist_add_tail(&existing->arg_list, &arg_list);
228 else {
229 while (!flist_empty(&arg_list))
230 flist_del_init(arg_list.next);
231 }
232 }
233
Jens Axboeb66570d2011-10-01 11:11:35 -0600234 client = malloc(sizeof(*client));
Jens Axboea37f69b2011-10-01 12:24:21 -0600235 memset(client, 0, sizeof(*client));
Jens Axboe81179ee2011-10-04 12:42:06 +0200236
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200237 INIT_FLIST_HEAD(&client->list);
Jens Axboebebe6392011-10-07 10:00:51 +0200238 INIT_FLIST_HEAD(&client->hash_list);
Jens Axboe3f3a4542011-10-10 21:11:09 +0200239 INIT_FLIST_HEAD(&client->arg_list);
Jens Axboe82c1ed32011-10-10 08:56:18 +0200240 INIT_FLIST_HEAD(&client->eta_list);
Jens Axboe89c17072011-10-11 10:15:51 +0200241 INIT_FLIST_HEAD(&client->cmd_list);
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200242
Jens Axboebebe6392011-10-07 10:00:51 +0200243 if (fio_server_parse_string(hostname, &client->hostname,
244 &client->is_sock, &client->port,
Jens Axboe811826b2011-10-24 09:11:50 +0200245 &client->addr.sin_addr,
246 &client->addr6.sin6_addr,
247 &client->ipv6))
Jens Axboebebe6392011-10-07 10:00:51 +0200248 return -1;
249
Jens Axboea37f69b2011-10-01 12:24:21 -0600250 client->fd = -1;
Jens Axboea5276612012-03-04 15:15:08 +0100251 client->ops = ops;
Jens Axboe5121a9a2012-03-05 13:32:47 +0100252 client->refs = 1;
Jens Axboe81179ee2011-10-04 12:42:06 +0200253
254 __fio_client_add_cmd_option(client, "fio");
255
Jens Axboea37f69b2011-10-01 12:24:21 -0600256 flist_add(&client->list, &client_list);
257 nr_clients++;
Jens Axboebebe6392011-10-07 10:00:51 +0200258 dprint(FD_NET, "client: added <%s>\n", client->hostname);
259 *cookie = client;
260 return 0;
Jens Axboea37f69b2011-10-01 12:24:21 -0600261}
262
Jens Axboe87aa8f12011-10-06 21:24:13 +0200263static int fio_client_connect_ip(struct fio_client *client)
Jens Axboea37f69b2011-10-01 12:24:21 -0600264{
Jens Axboe811826b2011-10-24 09:11:50 +0200265 struct sockaddr *addr;
266 fio_socklen_t socklen;
267 int fd, domain;
Jens Axboe132159a2011-09-30 15:01:32 -0600268
Jens Axboe811826b2011-10-24 09:11:50 +0200269 if (client->ipv6) {
270 client->addr6.sin6_family = AF_INET6;
271 client->addr6.sin6_port = htons(client->port);
272 domain = AF_INET6;
273 addr = (struct sockaddr *) &client->addr6;
274 socklen = sizeof(client->addr6);
275 } else {
276 client->addr.sin_family = AF_INET;
277 client->addr.sin_port = htons(client->port);
278 domain = AF_INET;
279 addr = (struct sockaddr *) &client->addr;
280 socklen = sizeof(client->addr);
281 }
Jens Axboe132159a2011-09-30 15:01:32 -0600282
Jens Axboe811826b2011-10-24 09:11:50 +0200283 fd = socket(domain, SOCK_STREAM, 0);
Jens Axboe132159a2011-09-30 15:01:32 -0600284 if (fd < 0) {
285 log_err("fio: socket: %s\n", strerror(errno));
Jens Axboe87aa8f12011-10-06 21:24:13 +0200286 return -1;
Jens Axboe132159a2011-09-30 15:01:32 -0600287 }
288
Jens Axboe811826b2011-10-24 09:11:50 +0200289 if (connect(fd, addr, socklen) < 0) {
Jens Axboe132159a2011-09-30 15:01:32 -0600290 log_err("fio: connect: %s\n", strerror(errno));
Jens Axboea7de0a12011-10-07 12:55:14 +0200291 log_err("fio: failed to connect to %s:%u\n", client->hostname,
292 client->port);
Jens Axboeb94cba42011-10-06 21:27:10 +0200293 close(fd);
Jens Axboe87aa8f12011-10-06 21:24:13 +0200294 return -1;
Jens Axboe132159a2011-09-30 15:01:32 -0600295 }
296
Jens Axboe87aa8f12011-10-06 21:24:13 +0200297 return fd;
298}
299
300static int fio_client_connect_sock(struct fio_client *client)
301{
302 struct sockaddr_un *addr = &client->addr_un;
303 fio_socklen_t len;
304 int fd;
305
306 memset(addr, 0, sizeof(*addr));
307 addr->sun_family = AF_UNIX;
308 strcpy(addr->sun_path, client->hostname);
309
310 fd = socket(AF_UNIX, SOCK_STREAM, 0);
311 if (fd < 0) {
312 log_err("fio: socket: %s\n", strerror(errno));
313 return -1;
314 }
315
316 len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
317 if (connect(fd, (struct sockaddr *) addr, len) < 0) {
318 log_err("fio: connect; %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200319 close(fd);
Jens Axboe87aa8f12011-10-06 21:24:13 +0200320 return -1;
321 }
322
323 return fd;
324}
325
326static int fio_client_connect(struct fio_client *client)
327{
328 int fd;
329
330 dprint(FD_NET, "client: connect to host %s\n", client->hostname);
331
Jens Axboe87aa8f12011-10-06 21:24:13 +0200332 if (client->is_sock)
333 fd = fio_client_connect_sock(client);
334 else
335 fd = fio_client_connect_ip(client);
336
Jens Axboe89c17072011-10-11 10:15:51 +0200337 dprint(FD_NET, "client: %s connected %d\n", client->hostname, fd);
338
Jens Axboe87aa8f12011-10-06 21:24:13 +0200339 if (fd < 0)
340 return 1;
341
Jens Axboeb66570d2011-10-01 11:11:35 -0600342 client->fd = fd;
Jens Axboebebe6392011-10-07 10:00:51 +0200343 fio_client_add_hash(client);
Jens Axboe81179ee2011-10-04 12:42:06 +0200344 client->state = Client_connected;
Jens Axboe132159a2011-09-30 15:01:32 -0600345 return 0;
346}
347
Jens Axboecc0df002011-10-03 20:53:32 +0200348void fio_clients_terminate(void)
349{
350 struct flist_head *entry;
351 struct fio_client *client;
352
Jens Axboe60efd142011-10-04 13:30:11 +0200353 dprint(FD_NET, "client: terminate clients\n");
354
Jens Axboecc0df002011-10-03 20:53:32 +0200355 flist_for_each(entry, &client_list) {
356 client = flist_entry(entry, struct fio_client, list);
357
Jens Axboe89c17072011-10-11 10:15:51 +0200358 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_QUIT, 0, NULL);
Jens Axboecc0df002011-10-03 20:53:32 +0200359 }
360}
361
362static void sig_int(int sig)
363{
Jens Axboebebe6392011-10-07 10:00:51 +0200364 dprint(FD_NET, "client: got signal %d\n", sig);
Jens Axboecc0df002011-10-03 20:53:32 +0200365 fio_clients_terminate();
366}
367
368static void client_signal_handler(void)
369{
370 struct sigaction act;
371
372 memset(&act, 0, sizeof(act));
373 act.sa_handler = sig_int;
374 act.sa_flags = SA_RESTART;
375 sigaction(SIGINT, &act, NULL);
376
377 memset(&act, 0, sizeof(act));
378 act.sa_handler = sig_int;
379 act.sa_flags = SA_RESTART;
380 sigaction(SIGTERM, &act, NULL);
381}
382
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200383static void probe_client(struct fio_client *client)
384{
Jens Axboe60efd142011-10-04 13:30:11 +0200385 dprint(FD_NET, "client: send probe\n");
386
Jens Axboe89c17072011-10-11 10:15:51 +0200387 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_PROBE, 0, &client->cmd_list);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200388}
389
Jens Axboe81179ee2011-10-04 12:42:06 +0200390static int send_client_cmd_line(struct fio_client *client)
391{
Jens Axboefa2ea802011-10-08 21:07:29 +0200392 struct cmd_single_line_pdu *cslp;
393 struct cmd_line_pdu *clp;
394 unsigned long offset;
Jens Axboe7f868312011-10-10 09:55:21 +0200395 unsigned int *lens;
Jens Axboefa2ea802011-10-08 21:07:29 +0200396 void *pdu;
397 size_t mem;
Jens Axboe81179ee2011-10-04 12:42:06 +0200398 int i, ret;
399
Jens Axboe39e8e012011-10-04 13:46:08 +0200400 dprint(FD_NET, "client: send cmdline %d\n", client->argc);
Jens Axboe60efd142011-10-04 13:30:11 +0200401
Jens Axboe7f868312011-10-10 09:55:21 +0200402 lens = malloc(client->argc * sizeof(unsigned int));
403
Jens Axboefa2ea802011-10-08 21:07:29 +0200404 /*
405 * Find out how much mem we need
406 */
Jens Axboe7f868312011-10-10 09:55:21 +0200407 for (i = 0, mem = 0; i < client->argc; i++) {
408 lens[i] = strlen(client->argv[i]) + 1;
409 mem += lens[i];
410 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200411
Jens Axboefa2ea802011-10-08 21:07:29 +0200412 /*
413 * We need one cmd_line_pdu, and argc number of cmd_single_line_pdu
414 */
415 mem += sizeof(*clp) + (client->argc * sizeof(*cslp));
416
417 pdu = malloc(mem);
418 clp = pdu;
419 offset = sizeof(*clp);
420
421 for (i = 0; i < client->argc; i++) {
Jens Axboe7f868312011-10-10 09:55:21 +0200422 uint16_t arg_len = lens[i];
Jens Axboefa2ea802011-10-08 21:07:29 +0200423
424 cslp = pdu + offset;
425 strcpy((char *) cslp->text, client->argv[i]);
426 cslp->len = cpu_to_le16(arg_len);
427 offset += sizeof(*cslp) + arg_len;
428 }
429
Jens Axboe7f868312011-10-10 09:55:21 +0200430 free(lens);
Jens Axboefa2ea802011-10-08 21:07:29 +0200431 clp->lines = cpu_to_le16(client->argc);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200432 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOBLINE, pdu, mem, 0);
Jens Axboe81179ee2011-10-04 12:42:06 +0200433 free(pdu);
434 return ret;
435}
436
Jens Axboea37f69b2011-10-01 12:24:21 -0600437int fio_clients_connect(void)
438{
439 struct fio_client *client;
440 struct flist_head *entry, *tmp;
441 int ret;
442
Bruce Cran93bcfd22012-02-20 20:18:19 +0100443#ifdef WIN32
444 WSADATA wsd;
445 WSAStartup(MAKEWORD(2,2), &wsd);
446#endif
447
Jens Axboe60efd142011-10-04 13:30:11 +0200448 dprint(FD_NET, "client: connect all\n");
449
Jens Axboecc0df002011-10-03 20:53:32 +0200450 client_signal_handler();
451
Jens Axboea37f69b2011-10-01 12:24:21 -0600452 flist_for_each_safe(entry, tmp, &client_list) {
453 client = flist_entry(entry, struct fio_client, list);
454
455 ret = fio_client_connect(client);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200456 if (ret) {
Jens Axboea37f69b2011-10-01 12:24:21 -0600457 remove_client(client);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200458 continue;
459 }
460
461 probe_client(client);
Jens Axboe81179ee2011-10-04 12:42:06 +0200462
463 if (client->argc > 1)
464 send_client_cmd_line(client);
Jens Axboea37f69b2011-10-01 12:24:21 -0600465 }
466
467 return !nr_clients;
468}
469
Jens Axboe132159a2011-09-30 15:01:32 -0600470/*
471 * Send file contents to server backend. We could use sendfile(), but to remain
472 * more portable lets just read/write the darn thing.
473 */
Jens Axboea37f69b2011-10-01 12:24:21 -0600474static int fio_client_send_ini(struct fio_client *client, const char *filename)
Jens Axboe132159a2011-09-30 15:01:32 -0600475{
476 struct stat sb;
477 char *p, *buf;
478 off_t len;
479 int fd, ret;
480
Jens Axboe46c48f12011-10-01 12:50:51 -0600481 dprint(FD_NET, "send ini %s to %s\n", filename, client->hostname);
482
Jens Axboe132159a2011-09-30 15:01:32 -0600483 fd = open(filename, O_RDONLY);
484 if (fd < 0) {
Jens Axboee951bdc2011-10-05 21:58:45 +0200485 log_err("fio: job file <%s> open: %s\n", filename, strerror(errno));
Jens Axboe132159a2011-09-30 15:01:32 -0600486 return 1;
487 }
488
489 if (fstat(fd, &sb) < 0) {
490 log_err("fio: job file stat: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200491 close(fd);
Jens Axboe132159a2011-09-30 15:01:32 -0600492 return 1;
493 }
494
495 buf = malloc(sb.st_size);
496
497 len = sb.st_size;
498 p = buf;
499 do {
500 ret = read(fd, p, len);
501 if (ret > 0) {
502 len -= ret;
503 if (!len)
504 break;
505 p += ret;
506 continue;
507 } else if (!ret)
508 break;
509 else if (errno == EAGAIN || errno == EINTR)
510 continue;
511 } while (1);
512
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200513 if (len) {
514 log_err("fio: failed reading job file %s\n", filename);
Jens Axboeb94cba42011-10-06 21:27:10 +0200515 close(fd);
Jens Axboec524ef72011-10-07 12:23:34 +0200516 free(buf);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200517 return 1;
518 }
519
Jens Axboec2cb6862012-02-23 20:56:12 +0100520 client->sent_job = 1;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200521 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, buf, sb.st_size, 0);
Jens Axboe132159a2011-09-30 15:01:32 -0600522 free(buf);
Jens Axboeb94cba42011-10-06 21:27:10 +0200523 close(fd);
Jens Axboe132159a2011-09-30 15:01:32 -0600524 return ret;
525}
Jens Axboe37db14f2011-09-30 17:00:42 -0600526
Jens Axboea37f69b2011-10-01 12:24:21 -0600527int fio_clients_send_ini(const char *filename)
528{
529 struct fio_client *client;
530 struct flist_head *entry, *tmp;
531
532 flist_for_each_safe(entry, tmp, &client_list) {
533 client = flist_entry(entry, struct fio_client, list);
534
535 if (fio_client_send_ini(client, filename))
536 remove_client(client);
Jens Axboec2cb6862012-02-23 20:56:12 +0100537
538 client->sent_job = 1;
Jens Axboea37f69b2011-10-01 12:24:21 -0600539 }
540
541 return !nr_clients;
542}
543
Jens Axboea64e88d2011-10-03 14:20:01 +0200544static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
545{
546 dst->max_val = le64_to_cpu(src->max_val);
547 dst->min_val = le64_to_cpu(src->min_val);
548 dst->samples = le64_to_cpu(src->samples);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200549
550 /*
551 * Floats arrive as IEEE 754 encoded uint64_t, convert back to double
552 */
553 dst->mean.u.f = fio_uint64_to_double(le64_to_cpu(dst->mean.u.i));
554 dst->S.u.f = fio_uint64_to_double(le64_to_cpu(dst->S.u.i));
Jens Axboea64e88d2011-10-03 14:20:01 +0200555}
556
557static void convert_ts(struct thread_stat *dst, struct thread_stat *src)
558{
559 int i, j;
560
561 dst->error = le32_to_cpu(src->error);
562 dst->groupid = le32_to_cpu(src->groupid);
563 dst->pid = le32_to_cpu(src->pid);
564 dst->members = le32_to_cpu(src->members);
565
566 for (i = 0; i < 2; i++) {
567 convert_io_stat(&dst->clat_stat[i], &src->clat_stat[i]);
568 convert_io_stat(&dst->slat_stat[i], &src->slat_stat[i]);
569 convert_io_stat(&dst->lat_stat[i], &src->lat_stat[i]);
570 convert_io_stat(&dst->bw_stat[i], &src->bw_stat[i]);
571 }
572
573 dst->usr_time = le64_to_cpu(src->usr_time);
574 dst->sys_time = le64_to_cpu(src->sys_time);
575 dst->ctx = le64_to_cpu(src->ctx);
576 dst->minf = le64_to_cpu(src->minf);
577 dst->majf = le64_to_cpu(src->majf);
578 dst->clat_percentiles = le64_to_cpu(src->clat_percentiles);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200579
580 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
581 fio_fp64_t *fps = &src->percentile_list[i];
582 fio_fp64_t *fpd = &dst->percentile_list[i];
583
584 fpd->u.f = fio_uint64_to_double(le64_to_cpu(fps->u.i));
585 }
Jens Axboea64e88d2011-10-03 14:20:01 +0200586
587 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
588 dst->io_u_map[i] = le32_to_cpu(src->io_u_map[i]);
589 dst->io_u_submit[i] = le32_to_cpu(src->io_u_submit[i]);
590 dst->io_u_complete[i] = le32_to_cpu(src->io_u_complete[i]);
591 }
592
593 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
594 dst->io_u_lat_u[i] = le32_to_cpu(src->io_u_lat_u[i]);
595 dst->io_u_lat_m[i] = le32_to_cpu(src->io_u_lat_m[i]);
596 }
597
598 for (i = 0; i < 2; i++)
599 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
600 dst->io_u_plat[i][j] = le32_to_cpu(src->io_u_plat[i][j]);
601
602 for (i = 0; i < 3; i++) {
603 dst->total_io_u[i] = le64_to_cpu(src->total_io_u[i]);
Jens Axboe93eee042011-10-03 21:08:48 +0200604 dst->short_io_u[i] = le64_to_cpu(src->short_io_u[i]);
Jens Axboea64e88d2011-10-03 14:20:01 +0200605 }
606
607 dst->total_submit = le64_to_cpu(src->total_submit);
608 dst->total_complete = le64_to_cpu(src->total_complete);
609
610 for (i = 0; i < 2; i++) {
611 dst->io_bytes[i] = le64_to_cpu(src->io_bytes[i]);
612 dst->runtime[i] = le64_to_cpu(src->runtime[i]);
613 }
614
615 dst->total_run_time = le64_to_cpu(src->total_run_time);
616 dst->continue_on_error = le16_to_cpu(src->continue_on_error);
617 dst->total_err_count = le64_to_cpu(src->total_err_count);
Jens Axboeddcc0b62011-10-03 14:45:27 +0200618 dst->first_error = le32_to_cpu(src->first_error);
619 dst->kb_base = le32_to_cpu(src->kb_base);
Jens Axboea64e88d2011-10-03 14:20:01 +0200620}
621
622static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
623{
624 int i;
625
626 for (i = 0; i < 2; i++) {
627 dst->max_run[i] = le64_to_cpu(src->max_run[i]);
628 dst->min_run[i] = le64_to_cpu(src->min_run[i]);
629 dst->max_bw[i] = le64_to_cpu(src->max_bw[i]);
630 dst->min_bw[i] = le64_to_cpu(src->min_bw[i]);
631 dst->io_kb[i] = le64_to_cpu(src->io_kb[i]);
632 dst->agg[i] = le64_to_cpu(src->agg[i]);
633 }
634
635 dst->kb_base = le32_to_cpu(src->kb_base);
636 dst->groupid = le32_to_cpu(src->groupid);
637}
638
Jens Axboe89e5fad2012-03-05 09:21:12 +0100639static void handle_ts(struct fio_client *client, struct fio_net_cmd *cmd)
Jens Axboea64e88d2011-10-03 14:20:01 +0200640{
641 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
642
Jens Axboea64e88d2011-10-03 14:20:01 +0200643 show_thread_status(&p->ts, &p->rs);
Jens Axboe37f0c1a2011-10-11 14:08:33 +0200644
645 if (sum_stat_clients == 1)
646 return;
647
648 sum_thread_stats(&client_ts, &p->ts, sum_stat_nr);
649 sum_group_stats(&client_gs, &p->rs);
650
651 client_ts.members++;
652 client_ts.groupid = p->ts.groupid;
653
654 if (++sum_stat_nr == sum_stat_clients) {
655 strcpy(client_ts.name, "All clients");
656 show_thread_status(&client_ts, &client_gs);
657 }
Jens Axboea64e88d2011-10-03 14:20:01 +0200658}
659
Jens Axboe89e5fad2012-03-05 09:21:12 +0100660static void handle_gs(struct fio_client *client, struct fio_net_cmd *cmd)
Jens Axboea64e88d2011-10-03 14:20:01 +0200661{
662 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
663
Jens Axboea64e88d2011-10-03 14:20:01 +0200664 show_group_stats(gs);
665}
666
Jens Axboe3bf236c2012-03-03 20:35:50 +0100667static void handle_text(struct fio_client *client, struct fio_net_cmd *cmd)
668{
669 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
670 const char *buf = (const char *) pdu->buf;
671 const char *name;
672 int fio_unused ret;
673
674 name = client->name ? client->name : client->hostname;
675
676 if (!client->skip_newline)
677 fprintf(f_out, "<%s> ", name);
678 ret = fwrite(buf, pdu->buf_len, 1, f_out);
679 fflush(f_out);
680 client->skip_newline = strchr(buf, '\n') == NULL;
681}
682
Jens Axboed09a64a2011-10-13 11:38:56 +0200683static void convert_agg(struct disk_util_agg *agg)
684{
685 int i;
686
687 for (i = 0; i < 2; i++) {
688 agg->ios[i] = le32_to_cpu(agg->ios[i]);
689 agg->merges[i] = le32_to_cpu(agg->merges[i]);
690 agg->sectors[i] = le64_to_cpu(agg->sectors[i]);
691 agg->ticks[i] = le32_to_cpu(agg->ticks[i]);
692 }
693
694 agg->io_ticks = le32_to_cpu(agg->io_ticks);
695 agg->time_in_queue = le32_to_cpu(agg->time_in_queue);
696 agg->slavecount = le32_to_cpu(agg->slavecount);
Anton Blanchard823ba542011-11-07 14:16:26 +0100697 agg->max_util.u.f = fio_uint64_to_double(__le64_to_cpu(agg->max_util.u.i));
Jens Axboed09a64a2011-10-13 11:38:56 +0200698}
699
700static void convert_dus(struct disk_util_stat *dus)
701{
702 int i;
703
704 for (i = 0; i < 2; i++) {
705 dus->ios[i] = le32_to_cpu(dus->ios[i]);
706 dus->merges[i] = le32_to_cpu(dus->merges[i]);
707 dus->sectors[i] = le64_to_cpu(dus->sectors[i]);
708 dus->ticks[i] = le32_to_cpu(dus->ticks[i]);
709 }
710
711 dus->io_ticks = le32_to_cpu(dus->io_ticks);
712 dus->time_in_queue = le32_to_cpu(dus->time_in_queue);
713 dus->msec = le64_to_cpu(dus->msec);
714}
715
716static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd)
717{
718 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
719
Jens Axboed09a64a2011-10-13 11:38:56 +0200720 if (!client->disk_stats_shown) {
721 client->disk_stats_shown = 1;
722 log_info("\nDisk stats (read/write):\n");
723 }
724
Jens Axboef2f788d2011-10-13 14:03:52 +0200725 print_disk_util(&du->dus, &du->agg, terse_output);
Jens Axboed09a64a2011-10-13 11:38:56 +0200726}
727
Jens Axboe3bf236c2012-03-03 20:35:50 +0100728static void convert_jobs_eta(struct jobs_eta *je)
Jens Axboecf451d12011-10-03 16:48:30 +0200729{
Jens Axboecf451d12011-10-03 16:48:30 +0200730 int i;
731
732 je->nr_running = le32_to_cpu(je->nr_running);
733 je->nr_ramp = le32_to_cpu(je->nr_ramp);
734 je->nr_pending = le32_to_cpu(je->nr_pending);
735 je->files_open = le32_to_cpu(je->files_open);
Jens Axboecf451d12011-10-03 16:48:30 +0200736
737 for (i = 0; i < 2; i++) {
Jens Axboe3e47bd22012-02-29 13:45:02 +0100738 je->m_rate[i] = le32_to_cpu(je->m_rate[i]);
739 je->t_rate[i] = le32_to_cpu(je->t_rate[i]);
740 je->m_iops[i] = le32_to_cpu(je->m_iops[i]);
741 je->t_iops[i] = le32_to_cpu(je->t_iops[i]);
Jens Axboecf451d12011-10-03 16:48:30 +0200742 je->rate[i] = le32_to_cpu(je->rate[i]);
743 je->iops[i] = le32_to_cpu(je->iops[i]);
744 }
745
Jens Axboeb51eedb2011-10-09 12:13:39 +0200746 je->elapsed_sec = le64_to_cpu(je->elapsed_sec);
Jens Axboecf451d12011-10-03 16:48:30 +0200747 je->eta_sec = le64_to_cpu(je->eta_sec);
Jens Axboe48fbb462011-10-09 12:19:08 +0200748}
Jens Axboecf451d12011-10-03 16:48:30 +0200749
Jens Axboe3e47bd22012-02-29 13:45:02 +0100750void fio_client_sum_jobs_eta(struct jobs_eta *dst, struct jobs_eta *je)
Jens Axboe48fbb462011-10-09 12:19:08 +0200751{
Jens Axboe48fbb462011-10-09 12:19:08 +0200752 int i;
753
754 dst->nr_running += je->nr_running;
755 dst->nr_ramp += je->nr_ramp;
756 dst->nr_pending += je->nr_pending;
757 dst->files_open += je->files_open;
Jens Axboe48fbb462011-10-09 12:19:08 +0200758
759 for (i = 0; i < 2; i++) {
Jens Axboe3e47bd22012-02-29 13:45:02 +0100760 dst->m_rate[i] += je->m_rate[i];
761 dst->t_rate[i] += je->t_rate[i];
762 dst->m_iops[i] += je->m_iops[i];
763 dst->t_iops[i] += je->t_iops[i];
Jens Axboe48fbb462011-10-09 12:19:08 +0200764 dst->rate[i] += je->rate[i];
765 dst->iops[i] += je->iops[i];
766 }
767
768 dst->elapsed_sec += je->elapsed_sec;
769
770 if (je->eta_sec > dst->eta_sec)
771 dst->eta_sec = je->eta_sec;
772}
773
Jens Axboea5276612012-03-04 15:15:08 +0100774void fio_client_dec_jobs_eta(struct client_eta *eta, client_eta_op eta_fn)
Jens Axboe82c1ed32011-10-10 08:56:18 +0200775{
776 if (!--eta->pending) {
Jens Axboea5276612012-03-04 15:15:08 +0100777 eta_fn(&eta->eta);
Jens Axboe82c1ed32011-10-10 08:56:18 +0200778 free(eta);
779 }
780}
781
Jens Axboe89c17072011-10-11 10:15:51 +0200782static void remove_reply_cmd(struct fio_client *client, struct fio_net_cmd *cmd)
783{
784 struct fio_net_int_cmd *icmd = NULL;
785 struct flist_head *entry;
786
787 flist_for_each(entry, &client->cmd_list) {
788 icmd = flist_entry(entry, struct fio_net_int_cmd, list);
789
Jens Axboedf380932011-10-11 14:25:08 +0200790 if (cmd->tag == (uintptr_t) icmd)
Jens Axboe89c17072011-10-11 10:15:51 +0200791 break;
792
793 icmd = NULL;
794 }
795
796 if (!icmd) {
797 log_err("fio: client: unable to find matching tag\n");
798 return;
799 }
800
801 flist_del(&icmd->list);
802 cmd->tag = icmd->saved_tag;
803 free(icmd);
804}
805
Jens Axboe82c1ed32011-10-10 08:56:18 +0200806static void handle_eta(struct fio_client *client, struct fio_net_cmd *cmd)
Jens Axboe48fbb462011-10-09 12:19:08 +0200807{
808 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
Jens Axboedf380932011-10-11 14:25:08 +0200809 struct client_eta *eta = (struct client_eta *) (uintptr_t) cmd->tag;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200810
811 dprint(FD_NET, "client: got eta tag %p, %d\n", eta, eta->pending);
Jens Axboe48fbb462011-10-09 12:19:08 +0200812
Jens Axboef77d2672011-10-10 14:36:07 +0200813 assert(client->eta_in_flight == eta);
814
815 client->eta_in_flight = NULL;
Jens Axboe82c1ed32011-10-10 08:56:18 +0200816 flist_del_init(&client->eta_list);
817
Jens Axboe3e47bd22012-02-29 13:45:02 +0100818 fio_client_sum_jobs_eta(&eta->eta, je);
Jens Axboea5276612012-03-04 15:15:08 +0100819 fio_client_dec_jobs_eta(eta, client->ops->eta);
Jens Axboecf451d12011-10-03 16:48:30 +0200820}
821
Jens Axboeb5296dd2011-10-07 16:35:56 +0200822static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd)
Jens Axboe2e03b4b2011-10-04 09:00:40 +0200823{
824 struct cmd_probe_pdu *probe = (struct cmd_probe_pdu *) cmd->payload;
Jens Axboed2333352011-10-11 15:07:23 +0200825 const char *os, *arch;
826 char bit[16];
Jens Axboe2e03b4b2011-10-04 09:00:40 +0200827
Jens Axboecca84642011-10-07 12:47:57 +0200828 os = fio_get_os_string(probe->os);
829 if (!os)
830 os = "unknown";
831
832 arch = fio_get_arch_string(probe->arch);
833 if (!arch)
834 os = "unknown";
835
Jens Axboed2333352011-10-11 15:07:23 +0200836 sprintf(bit, "%d-bit", probe->bpp * 8);
Jens Axboe38fdef22011-10-11 14:30:06 +0200837
838 log_info("hostname=%s, be=%u, %s, os=%s, arch=%s, fio=%u.%u.%u\n",
839 probe->hostname, probe->bigendian, bit, os, arch,
840 probe->fio_major, probe->fio_minor, probe->fio_patch);
Jens Axboeb5296dd2011-10-07 16:35:56 +0200841
842 if (!client->name)
843 client->name = strdup((char *) probe->hostname);
Jens Axboe2e03b4b2011-10-04 09:00:40 +0200844}
845
Jens Axboe11e950b2011-10-16 21:34:14 +0200846static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd)
847{
848 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
849
850 client->state = Client_started;
851 client->jobs = le32_to_cpu(pdu->jobs);
852}
853
854static void handle_stop(struct fio_client *client, struct fio_net_cmd *cmd)
855{
856 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
857
858 client->state = Client_stopped;
859 client->error = le32_to_cpu(pdu->error);
Jens Axboe498c92c2011-10-17 09:14:42 +0200860
861 if (client->error)
862 log_info("client <%s>: exited with error %d\n", client->hostname, client->error);
Jens Axboe11e950b2011-10-16 21:34:14 +0200863}
864
Jens Axboe084d1c62012-03-03 20:28:07 +0100865static void convert_text(struct fio_net_cmd *cmd)
866{
867 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
868
869 pdu->level = le32_to_cpu(pdu->level);
870 pdu->buf_len = le32_to_cpu(pdu->buf_len);
871 pdu->log_sec = le64_to_cpu(pdu->log_sec);
872 pdu->log_usec = le64_to_cpu(pdu->log_usec);
873}
874
Jens Axboea5276612012-03-04 15:15:08 +0100875int fio_handle_client(struct fio_client *client)
Jens Axboe37db14f2011-09-30 17:00:42 -0600876{
Jens Axboea5276612012-03-04 15:15:08 +0100877 struct client_ops *ops = client->ops;
Jens Axboe37db14f2011-09-30 17:00:42 -0600878 struct fio_net_cmd *cmd;
879
Jens Axboe60efd142011-10-04 13:30:11 +0200880 dprint(FD_NET, "client: handle %s\n", client->hostname);
881
Jens Axboee951bdc2011-10-05 21:58:45 +0200882 cmd = fio_net_recv_cmd(client->fd);
883 if (!cmd)
884 return 0;
Jens Axboec2c94582011-10-05 20:31:30 +0200885
Jens Axboe89c17072011-10-11 10:15:51 +0200886 dprint(FD_NET, "client: got cmd op %s from %s\n",
887 fio_server_op(cmd->opcode), client->hostname);
Jens Axboe46c48f12011-10-01 12:50:51 -0600888
Jens Axboee951bdc2011-10-05 21:58:45 +0200889 switch (cmd->opcode) {
890 case FIO_NET_CMD_QUIT:
Jens Axboe3ec62ec2012-03-01 12:01:29 +0100891 if (ops->quit)
892 ops->quit(client);
Jens Axboee951bdc2011-10-05 21:58:45 +0200893 remove_client(client);
894 free(cmd);
895 break;
Jens Axboe084d1c62012-03-03 20:28:07 +0100896 case FIO_NET_CMD_TEXT:
897 convert_text(cmd);
898 ops->text_op(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +0200899 free(cmd);
900 break;
Jens Axboe3bf236c2012-03-03 20:35:50 +0100901 case FIO_NET_CMD_DU: {
902 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
903
904 convert_dus(&du->dus);
905 convert_agg(&du->agg);
906
Stephen M. Camerondd366722012-02-24 08:17:30 +0100907 ops->disk_util(client, cmd);
Jens Axboed09a64a2011-10-13 11:38:56 +0200908 free(cmd);
909 break;
Jens Axboe3bf236c2012-03-03 20:35:50 +0100910 }
911 case FIO_NET_CMD_TS: {
912 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
913
914 convert_ts(&p->ts, &p->ts);
915 convert_gs(&p->rs, &p->rs);
916
Jens Axboe89e5fad2012-03-05 09:21:12 +0100917 ops->thread_status(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +0200918 free(cmd);
919 break;
Jens Axboe3bf236c2012-03-03 20:35:50 +0100920 }
921 case FIO_NET_CMD_GS: {
922 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
923
924 convert_gs(gs, gs);
925
Jens Axboe89e5fad2012-03-05 09:21:12 +0100926 ops->group_stats(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +0200927 free(cmd);
928 break;
Jens Axboe3bf236c2012-03-03 20:35:50 +0100929 }
930 case FIO_NET_CMD_ETA: {
931 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
932
Jens Axboe89c17072011-10-11 10:15:51 +0200933 remove_reply_cmd(client, cmd);
Jens Axboe3bf236c2012-03-03 20:35:50 +0100934 convert_jobs_eta(je);
Jens Axboea5276612012-03-04 15:15:08 +0100935 handle_eta(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +0200936 free(cmd);
937 break;
Jens Axboe3bf236c2012-03-03 20:35:50 +0100938 }
Jens Axboee951bdc2011-10-05 21:58:45 +0200939 case FIO_NET_CMD_PROBE:
Jens Axboe89c17072011-10-11 10:15:51 +0200940 remove_reply_cmd(client, cmd);
Stephen M. Camerondd366722012-02-24 08:17:30 +0100941 ops->probe(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +0200942 free(cmd);
943 break;
Jens Axboe01be0382011-10-15 14:43:41 +0200944 case FIO_NET_CMD_RUN:
945 client->state = Client_running;
946 free(cmd);
947 break;
Jens Axboee951bdc2011-10-05 21:58:45 +0200948 case FIO_NET_CMD_START:
Jens Axboe11e950b2011-10-16 21:34:14 +0200949 handle_start(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +0200950 free(cmd);
951 break;
952 case FIO_NET_CMD_STOP:
Jens Axboe11e950b2011-10-16 21:34:14 +0200953 handle_stop(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +0200954 free(cmd);
955 break;
Jens Axboe807f9972012-03-02 10:25:24 +0100956 case FIO_NET_CMD_ADD_JOB:
957 if (ops->add_job)
958 ops->add_job(client, cmd);
959 free(cmd);
960 break;
Jens Axboee951bdc2011-10-05 21:58:45 +0200961 default:
Jens Axboe89c17072011-10-11 10:15:51 +0200962 log_err("fio: unknown client op: %s\n", fio_server_op(cmd->opcode));
Jens Axboee951bdc2011-10-05 21:58:45 +0200963 free(cmd);
964 break;
Jens Axboe37db14f2011-09-30 17:00:42 -0600965 }
966
Jens Axboee951bdc2011-10-05 21:58:45 +0200967 return 1;
Jens Axboe37db14f2011-09-30 17:00:42 -0600968}
Jens Axboeb66570d2011-10-01 11:11:35 -0600969
Jens Axboea5276612012-03-04 15:15:08 +0100970static void request_client_etas(struct client_ops *ops)
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200971{
972 struct fio_client *client;
973 struct flist_head *entry;
974 struct client_eta *eta;
Jens Axboe82c1ed32011-10-10 08:56:18 +0200975 int skipped = 0;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200976
977 dprint(FD_NET, "client: request eta (%d)\n", nr_clients);
978
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200979 eta = malloc(sizeof(*eta));
980 memset(&eta->eta, 0, sizeof(eta->eta));
981 eta->pending = nr_clients;
982
983 flist_for_each(entry, &client_list) {
984 client = flist_entry(entry, struct fio_client, list);
985
Jens Axboe82c1ed32011-10-10 08:56:18 +0200986 if (!flist_empty(&client->eta_list)) {
987 skipped++;
988 continue;
989 }
Jens Axboe01be0382011-10-15 14:43:41 +0200990 if (client->state != Client_running)
991 continue;
Jens Axboe82c1ed32011-10-10 08:56:18 +0200992
Jens Axboef77d2672011-10-10 14:36:07 +0200993 assert(!client->eta_in_flight);
Jens Axboe82c1ed32011-10-10 08:56:18 +0200994 flist_add_tail(&client->eta_list, &eta_list);
Jens Axboef77d2672011-10-10 14:36:07 +0200995 client->eta_in_flight = eta;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200996 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_SEND_ETA,
Jens Axboedf380932011-10-11 14:25:08 +0200997 (uintptr_t) eta, &client->cmd_list);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200998 }
999
Jens Axboe82c1ed32011-10-10 08:56:18 +02001000 while (skipped--)
Jens Axboea5276612012-03-04 15:15:08 +01001001 fio_client_dec_jobs_eta(eta, ops->eta);
Jens Axboe82c1ed32011-10-10 08:56:18 +02001002
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001003 dprint(FD_NET, "client: requested eta tag %p\n", eta);
1004}
1005
Jens Axboe89c17072011-10-11 10:15:51 +02001006static int client_check_cmd_timeout(struct fio_client *client,
1007 struct timeval *now)
1008{
1009 struct fio_net_int_cmd *cmd;
1010 struct flist_head *entry, *tmp;
1011 int ret = 0;
1012
1013 flist_for_each_safe(entry, tmp, &client->cmd_list) {
1014 cmd = flist_entry(entry, struct fio_net_int_cmd, list);
1015
1016 if (mtime_since(&cmd->tv, now) < FIO_NET_CLIENT_TIMEOUT)
1017 continue;
1018
1019 log_err("fio: client %s, timeout on cmd %s\n", client->hostname,
1020 fio_server_op(cmd->cmd.opcode));
1021 flist_del(&cmd->list);
1022 free(cmd);
1023 ret = 1;
1024 }
1025
1026 return flist_empty(&client->cmd_list) && ret;
1027}
1028
Jens Axboea5276612012-03-04 15:15:08 +01001029static int fio_check_clients_timed_out(void)
Jens Axboe89c17072011-10-11 10:15:51 +02001030{
1031 struct fio_client *client;
1032 struct flist_head *entry, *tmp;
1033 struct timeval tv;
1034 int ret = 0;
1035
1036 gettimeofday(&tv, NULL);
1037
1038 flist_for_each_safe(entry, tmp, &client_list) {
1039 client = flist_entry(entry, struct fio_client, list);
1040
1041 if (flist_empty(&client->cmd_list))
1042 continue;
1043
1044 if (!client_check_cmd_timeout(client, &tv))
1045 continue;
1046
Jens Axboea5276612012-03-04 15:15:08 +01001047 if (client->ops->timed_out)
1048 client->ops->timed_out(client);
Jens Axboeed727a42012-03-02 12:14:40 +01001049 else
1050 log_err("fio: client %s timed out\n", client->hostname);
1051
Jens Axboe89c17072011-10-11 10:15:51 +02001052 remove_client(client);
1053 ret = 1;
1054 }
1055
1056 return ret;
1057}
1058
Stephen M. Camerondd366722012-02-24 08:17:30 +01001059int fio_handle_clients(struct client_ops *ops)
Jens Axboeb66570d2011-10-01 11:11:35 -06001060{
Jens Axboeb66570d2011-10-01 11:11:35 -06001061 struct pollfd *pfds;
Jens Axboe498c92c2011-10-17 09:14:42 +02001062 int i, ret = 0, retval = 0;
Jens Axboeb66570d2011-10-01 11:11:35 -06001063
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001064 gettimeofday(&eta_tv, NULL);
1065
Jens Axboeb66570d2011-10-01 11:11:35 -06001066 pfds = malloc(nr_clients * sizeof(struct pollfd));
1067
Jens Axboe37f0c1a2011-10-11 14:08:33 +02001068 sum_stat_clients = nr_clients;
1069 init_thread_stat(&client_ts);
1070 init_group_run_stat(&client_gs);
1071
Jens Axboeb66570d2011-10-01 11:11:35 -06001072 while (!exit_backend && nr_clients) {
Jens Axboec2cb6862012-02-23 20:56:12 +01001073 struct flist_head *entry, *tmp;
1074 struct fio_client *client;
1075
Jens Axboe82a4be12011-10-01 12:40:32 -06001076 i = 0;
Jens Axboec2cb6862012-02-23 20:56:12 +01001077 flist_for_each_safe(entry, tmp, &client_list) {
Jens Axboe82a4be12011-10-01 12:40:32 -06001078 client = flist_entry(entry, struct fio_client, list);
1079
Jens Axboea5276612012-03-04 15:15:08 +01001080 if (!client->sent_job && !client->ops->stay_connected &&
Jens Axboec2cb6862012-02-23 20:56:12 +01001081 flist_empty(&client->cmd_list)) {
1082 remove_client(client);
1083 continue;
1084 }
1085
Jens Axboe82a4be12011-10-01 12:40:32 -06001086 pfds[i].fd = client->fd;
1087 pfds[i].events = POLLIN;
1088 i++;
1089 }
1090
Jens Axboec2cb6862012-02-23 20:56:12 +01001091 if (!nr_clients)
1092 break;
1093
Jens Axboe82a4be12011-10-01 12:40:32 -06001094 assert(i == nr_clients);
1095
Jens Axboe5c2857f2011-10-04 13:14:32 +02001096 do {
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001097 struct timeval tv;
1098
1099 gettimeofday(&tv, NULL);
1100 if (mtime_since(&eta_tv, &tv) >= 900) {
Jens Axboea5276612012-03-04 15:15:08 +01001101 request_client_etas(ops);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001102 memcpy(&eta_tv, &tv, sizeof(tv));
Jens Axboe89c17072011-10-11 10:15:51 +02001103
Jens Axboea5276612012-03-04 15:15:08 +01001104 if (fio_check_clients_timed_out())
Jens Axboe89c17072011-10-11 10:15:51 +02001105 break;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001106 }
1107
Jens Axboe5c2857f2011-10-04 13:14:32 +02001108 ret = poll(pfds, nr_clients, 100);
1109 if (ret < 0) {
1110 if (errno == EINTR)
1111 continue;
1112 log_err("fio: poll clients: %s\n", strerror(errno));
1113 break;
1114 } else if (!ret)
Jens Axboeb66570d2011-10-01 11:11:35 -06001115 continue;
Jens Axboe5c2857f2011-10-04 13:14:32 +02001116 } while (ret <= 0);
Jens Axboeb66570d2011-10-01 11:11:35 -06001117
1118 for (i = 0; i < nr_clients; i++) {
1119 if (!(pfds[i].revents & POLLIN))
1120 continue;
1121
1122 client = find_client_by_fd(pfds[i].fd);
1123 if (!client) {
Jens Axboe3c5f57e2011-10-06 12:37:50 +02001124 log_err("fio: unknown client fd %d\n", pfds[i].fd);
Jens Axboeb66570d2011-10-01 11:11:35 -06001125 continue;
1126 }
Jens Axboea5276612012-03-04 15:15:08 +01001127 if (!fio_handle_client(client)) {
Jens Axboe28d3ab02011-10-05 20:45:37 +02001128 log_info("client: host=%s disconnected\n",
1129 client->hostname);
1130 remove_client(client);
Jens Axboe498c92c2011-10-17 09:14:42 +02001131 retval = 1;
Jens Axboe38990762011-10-17 13:31:33 +02001132 } else if (client->error)
Jens Axboe498c92c2011-10-17 09:14:42 +02001133 retval = 1;
Jens Axboe5121a9a2012-03-05 13:32:47 +01001134 put_client(client);
Jens Axboeb66570d2011-10-01 11:11:35 -06001135 }
1136 }
1137
1138 free(pfds);
Jens Axboe498c92c2011-10-17 09:14:42 +02001139 return retval;
Jens Axboeb66570d2011-10-01 11:11:35 -06001140}