blob: 851aeba6246d5669b2b5b17cd4c3f90783ccbd93 [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"
19#include "server.h"
Jens Axboeb66570d2011-10-01 11:11:35 -060020#include "flist.h"
Jens Axboe3c5f57e2011-10-06 12:37:50 +020021#include "hash.h"
Jens Axboe132159a2011-09-30 15:01:32 -060022
Jens Axboeb66570d2011-10-01 11:11:35 -060023struct fio_client {
24 struct flist_head list;
Jens Axboebebe6392011-10-07 10:00:51 +020025 struct flist_head hash_list;
Jens Axboeb66570d2011-10-01 11:11:35 -060026 struct sockaddr_in addr;
Jens Axboe87aa8f12011-10-06 21:24:13 +020027 struct sockaddr_un addr_un;
Jens Axboeb66570d2011-10-01 11:11:35 -060028 char *hostname;
Jens Axboebebe6392011-10-07 10:00:51 +020029 int port;
Jens Axboeb66570d2011-10-01 11:11:35 -060030 int fd;
Jens Axboe81179ee2011-10-04 12:42:06 +020031
Jens Axboeb5296dd2011-10-07 16:35:56 +020032 char *name;
33
Jens Axboe81179ee2011-10-04 12:42:06 +020034 int state;
Jens Axboe17dd1762011-10-04 13:27:34 +020035 int skip_newline;
Jens Axboe87aa8f12011-10-06 21:24:13 +020036 int is_sock;
Jens Axboe81179ee2011-10-04 12:42:06 +020037
38 uint16_t argc;
39 char **argv;
40};
41
42enum {
Jens Axboe5c2857f2011-10-04 13:14:32 +020043 Client_created = 0,
Jens Axboe81179ee2011-10-04 12:42:06 +020044 Client_connected = 1,
45 Client_started = 2,
46 Client_stopped = 3,
Jens Axboe5c2857f2011-10-04 13:14:32 +020047 Client_exited = 4,
Jens Axboeb66570d2011-10-01 11:11:35 -060048};
49
50static FLIST_HEAD(client_list);
Jens Axboeb66570d2011-10-01 11:11:35 -060051
Jens Axboe3c5f57e2011-10-06 12:37:50 +020052#define FIO_CLIENT_HASH_BITS 7
53#define FIO_CLIENT_HASH_SZ (1 << FIO_CLIENT_HASH_BITS)
54#define FIO_CLIENT_HASH_MASK (FIO_CLIENT_HASH_SZ - 1)
Jens Axboebebe6392011-10-07 10:00:51 +020055static struct flist_head client_hash[FIO_CLIENT_HASH_SZ];
Jens Axboe3c5f57e2011-10-06 12:37:50 +020056
Jens Axboee951bdc2011-10-05 21:58:45 +020057static int handle_client(struct fio_client *client);
Jens Axboe0b8f30a2011-10-04 10:31:53 +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
90 if (client->fd == fd)
91 return client;
92 }
93
94 return NULL;
95}
96
Jens Axboeb66570d2011-10-01 11:11:35 -060097static void remove_client(struct fio_client *client)
98{
Jens Axboe39e8e012011-10-04 13:46:08 +020099 dprint(FD_NET, "client: removed <%s>\n", client->hostname);
Jens Axboeb66570d2011-10-01 11:11:35 -0600100 flist_del(&client->list);
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200101
Jens Axboebebe6392011-10-07 10:00:51 +0200102 fio_client_remove_hash(client);
Jens Axboe81179ee2011-10-04 12:42:06 +0200103
Jens Axboeb66570d2011-10-01 11:11:35 -0600104 free(client->hostname);
Jens Axboe81179ee2011-10-04 12:42:06 +0200105 if (client->argv)
106 free(client->argv);
Jens Axboeb5296dd2011-10-07 16:35:56 +0200107 if (client->name)
108 free(client->name);
Jens Axboe81179ee2011-10-04 12:42:06 +0200109
Jens Axboeb66570d2011-10-01 11:11:35 -0600110 free(client);
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200111 nr_clients--;
Jens Axboeb66570d2011-10-01 11:11:35 -0600112}
Jens Axboe132159a2011-09-30 15:01:32 -0600113
Jens Axboefa2ea802011-10-08 21:07:29 +0200114static void __fio_client_add_cmd_option(struct fio_client *client,
115 const char *opt)
Jens Axboe81179ee2011-10-04 12:42:06 +0200116{
Jens Axboe39e8e012011-10-04 13:46:08 +0200117 int index;
118
119 index = client->argc++;
Jens Axboe81179ee2011-10-04 12:42:06 +0200120 client->argv = realloc(client->argv, sizeof(char *) * client->argc);
Jens Axboe39e8e012011-10-04 13:46:08 +0200121 client->argv[index] = strdup(opt);
122 dprint(FD_NET, "client: add cmd %d: %s\n", index, opt);
Jens Axboe81179ee2011-10-04 12:42:06 +0200123}
124
Jens Axboefa2ea802011-10-08 21:07:29 +0200125void fio_client_add_cmd_option(void *cookie, const char *opt)
Jens Axboe81179ee2011-10-04 12:42:06 +0200126{
Jens Axboebebe6392011-10-07 10:00:51 +0200127 struct fio_client *client = cookie;
Jens Axboe81179ee2011-10-04 12:42:06 +0200128
Jens Axboebebe6392011-10-07 10:00:51 +0200129 if (!client || !opt)
Jens Axboefa2ea802011-10-08 21:07:29 +0200130 return;
Jens Axboe81179ee2011-10-04 12:42:06 +0200131
Jens Axboefa2ea802011-10-08 21:07:29 +0200132 __fio_client_add_cmd_option(client, opt);
Jens Axboe81179ee2011-10-04 12:42:06 +0200133}
134
Jens Axboebebe6392011-10-07 10:00:51 +0200135int fio_client_add(const char *hostname, void **cookie)
Jens Axboe132159a2011-09-30 15:01:32 -0600136{
Jens Axboeb66570d2011-10-01 11:11:35 -0600137 struct fio_client *client;
Jens Axboe132159a2011-09-30 15:01:32 -0600138
Jens Axboeb66570d2011-10-01 11:11:35 -0600139 client = malloc(sizeof(*client));
Jens Axboea37f69b2011-10-01 12:24:21 -0600140 memset(client, 0, sizeof(*client));
Jens Axboe81179ee2011-10-04 12:42:06 +0200141
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200142 INIT_FLIST_HEAD(&client->list);
Jens Axboebebe6392011-10-07 10:00:51 +0200143 INIT_FLIST_HEAD(&client->hash_list);
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200144
Jens Axboebebe6392011-10-07 10:00:51 +0200145 if (fio_server_parse_string(hostname, &client->hostname,
146 &client->is_sock, &client->port,
147 &client->addr.sin_addr))
148 return -1;
149
Jens Axboea37f69b2011-10-01 12:24:21 -0600150 client->fd = -1;
Jens Axboe81179ee2011-10-04 12:42:06 +0200151
152 __fio_client_add_cmd_option(client, "fio");
153
Jens Axboea37f69b2011-10-01 12:24:21 -0600154 flist_add(&client->list, &client_list);
155 nr_clients++;
Jens Axboebebe6392011-10-07 10:00:51 +0200156 dprint(FD_NET, "client: added <%s>\n", client->hostname);
157 *cookie = client;
158 return 0;
Jens Axboea37f69b2011-10-01 12:24:21 -0600159}
160
Jens Axboe87aa8f12011-10-06 21:24:13 +0200161static int fio_client_connect_ip(struct fio_client *client)
Jens Axboea37f69b2011-10-01 12:24:21 -0600162{
163 int fd;
Jens Axboe132159a2011-09-30 15:01:32 -0600164
Jens Axboeb66570d2011-10-01 11:11:35 -0600165 client->addr.sin_family = AF_INET;
Jens Axboebebe6392011-10-07 10:00:51 +0200166 client->addr.sin_port = htons(client->port);
Jens Axboe132159a2011-09-30 15:01:32 -0600167
168 fd = socket(AF_INET, SOCK_STREAM, 0);
169 if (fd < 0) {
170 log_err("fio: socket: %s\n", strerror(errno));
Jens Axboe87aa8f12011-10-06 21:24:13 +0200171 return -1;
Jens Axboe132159a2011-09-30 15:01:32 -0600172 }
173
Jens Axboeb66570d2011-10-01 11:11:35 -0600174 if (connect(fd, (struct sockaddr *) &client->addr, sizeof(client->addr)) < 0) {
Jens Axboe132159a2011-09-30 15:01:32 -0600175 log_err("fio: connect: %s\n", strerror(errno));
Jens Axboea7de0a12011-10-07 12:55:14 +0200176 log_err("fio: failed to connect to %s:%u\n", client->hostname,
177 client->port);
Jens Axboeb94cba42011-10-06 21:27:10 +0200178 close(fd);
Jens Axboe87aa8f12011-10-06 21:24:13 +0200179 return -1;
Jens Axboe132159a2011-09-30 15:01:32 -0600180 }
181
Jens Axboe87aa8f12011-10-06 21:24:13 +0200182 return fd;
183}
184
185static int fio_client_connect_sock(struct fio_client *client)
186{
187 struct sockaddr_un *addr = &client->addr_un;
188 fio_socklen_t len;
189 int fd;
190
191 memset(addr, 0, sizeof(*addr));
192 addr->sun_family = AF_UNIX;
193 strcpy(addr->sun_path, client->hostname);
194
195 fd = socket(AF_UNIX, SOCK_STREAM, 0);
196 if (fd < 0) {
197 log_err("fio: socket: %s\n", strerror(errno));
198 return -1;
199 }
200
201 len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
202 if (connect(fd, (struct sockaddr *) addr, len) < 0) {
203 log_err("fio: connect; %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200204 close(fd);
Jens Axboe87aa8f12011-10-06 21:24:13 +0200205 return -1;
206 }
207
208 return fd;
209}
210
211static int fio_client_connect(struct fio_client *client)
212{
213 int fd;
214
215 dprint(FD_NET, "client: connect to host %s\n", client->hostname);
216
Jens Axboe87aa8f12011-10-06 21:24:13 +0200217 if (client->is_sock)
218 fd = fio_client_connect_sock(client);
219 else
220 fd = fio_client_connect_ip(client);
221
222 if (fd < 0)
223 return 1;
224
Jens Axboeb66570d2011-10-01 11:11:35 -0600225 client->fd = fd;
Jens Axboebebe6392011-10-07 10:00:51 +0200226 fio_client_add_hash(client);
Jens Axboe81179ee2011-10-04 12:42:06 +0200227 client->state = Client_connected;
Jens Axboe132159a2011-09-30 15:01:32 -0600228 return 0;
229}
230
Jens Axboecc0df002011-10-03 20:53:32 +0200231void fio_clients_terminate(void)
232{
233 struct flist_head *entry;
234 struct fio_client *client;
235
Jens Axboe60efd142011-10-04 13:30:11 +0200236 dprint(FD_NET, "client: terminate clients\n");
237
Jens Axboecc0df002011-10-03 20:53:32 +0200238 flist_for_each(entry, &client_list) {
239 client = flist_entry(entry, struct fio_client, list);
240
241 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_QUIT, 0);
242 }
243}
244
245static void sig_int(int sig)
246{
Jens Axboebebe6392011-10-07 10:00:51 +0200247 dprint(FD_NET, "client: got signal %d\n", sig);
Jens Axboecc0df002011-10-03 20:53:32 +0200248 fio_clients_terminate();
249}
250
251static void client_signal_handler(void)
252{
253 struct sigaction act;
254
255 memset(&act, 0, sizeof(act));
256 act.sa_handler = sig_int;
257 act.sa_flags = SA_RESTART;
258 sigaction(SIGINT, &act, NULL);
259
260 memset(&act, 0, sizeof(act));
261 act.sa_handler = sig_int;
262 act.sa_flags = SA_RESTART;
263 sigaction(SIGTERM, &act, NULL);
264}
265
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200266static void probe_client(struct fio_client *client)
267{
Jens Axboe60efd142011-10-04 13:30:11 +0200268 dprint(FD_NET, "client: send probe\n");
269
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200270 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_PROBE, 0);
Jens Axboee951bdc2011-10-05 21:58:45 +0200271 handle_client(client);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200272}
273
Jens Axboe81179ee2011-10-04 12:42:06 +0200274static int send_client_cmd_line(struct fio_client *client)
275{
Jens Axboefa2ea802011-10-08 21:07:29 +0200276 struct cmd_single_line_pdu *cslp;
277 struct cmd_line_pdu *clp;
278 unsigned long offset;
279 void *pdu;
280 size_t mem;
Jens Axboe81179ee2011-10-04 12:42:06 +0200281 int i, ret;
282
Jens Axboe39e8e012011-10-04 13:46:08 +0200283 dprint(FD_NET, "client: send cmdline %d\n", client->argc);
Jens Axboe60efd142011-10-04 13:30:11 +0200284
Jens Axboefa2ea802011-10-08 21:07:29 +0200285 /*
286 * Find out how much mem we need
287 */
288 for (i = 0, mem = 0; i < client->argc; i++)
289 mem += strlen(client->argv[i]) + 1;
Jens Axboe81179ee2011-10-04 12:42:06 +0200290
Jens Axboefa2ea802011-10-08 21:07:29 +0200291 /*
292 * We need one cmd_line_pdu, and argc number of cmd_single_line_pdu
293 */
294 mem += sizeof(*clp) + (client->argc * sizeof(*cslp));
295
296 pdu = malloc(mem);
297 clp = pdu;
298 offset = sizeof(*clp);
299
300 for (i = 0; i < client->argc; i++) {
301 uint16_t arg_len = strlen(client->argv[i]) + 1;
302
303 cslp = pdu + offset;
304 strcpy((char *) cslp->text, client->argv[i]);
305 cslp->len = cpu_to_le16(arg_len);
306 offset += sizeof(*cslp) + arg_len;
307 }
308
309 clp->lines = cpu_to_le16(client->argc);
310 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOBLINE, pdu, mem);
Jens Axboe81179ee2011-10-04 12:42:06 +0200311 free(pdu);
312 return ret;
313}
314
Jens Axboea37f69b2011-10-01 12:24:21 -0600315int fio_clients_connect(void)
316{
317 struct fio_client *client;
318 struct flist_head *entry, *tmp;
319 int ret;
320
Jens Axboe60efd142011-10-04 13:30:11 +0200321 dprint(FD_NET, "client: connect all\n");
322
Jens Axboecc0df002011-10-03 20:53:32 +0200323 client_signal_handler();
324
Jens Axboea37f69b2011-10-01 12:24:21 -0600325 flist_for_each_safe(entry, tmp, &client_list) {
326 client = flist_entry(entry, struct fio_client, list);
327
328 ret = fio_client_connect(client);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200329 if (ret) {
Jens Axboea37f69b2011-10-01 12:24:21 -0600330 remove_client(client);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200331 continue;
332 }
333
334 probe_client(client);
Jens Axboe81179ee2011-10-04 12:42:06 +0200335
336 if (client->argc > 1)
337 send_client_cmd_line(client);
Jens Axboea37f69b2011-10-01 12:24:21 -0600338 }
339
340 return !nr_clients;
341}
342
Jens Axboe132159a2011-09-30 15:01:32 -0600343/*
344 * Send file contents to server backend. We could use sendfile(), but to remain
345 * more portable lets just read/write the darn thing.
346 */
Jens Axboea37f69b2011-10-01 12:24:21 -0600347static int fio_client_send_ini(struct fio_client *client, const char *filename)
Jens Axboe132159a2011-09-30 15:01:32 -0600348{
349 struct stat sb;
350 char *p, *buf;
351 off_t len;
352 int fd, ret;
353
Jens Axboe46c48f12011-10-01 12:50:51 -0600354 dprint(FD_NET, "send ini %s to %s\n", filename, client->hostname);
355
Jens Axboe132159a2011-09-30 15:01:32 -0600356 fd = open(filename, O_RDONLY);
357 if (fd < 0) {
Jens Axboee951bdc2011-10-05 21:58:45 +0200358 log_err("fio: job file <%s> open: %s\n", filename, strerror(errno));
Jens Axboe132159a2011-09-30 15:01:32 -0600359 return 1;
360 }
361
362 if (fstat(fd, &sb) < 0) {
363 log_err("fio: job file stat: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200364 close(fd);
Jens Axboe132159a2011-09-30 15:01:32 -0600365 return 1;
366 }
367
368 buf = malloc(sb.st_size);
369
370 len = sb.st_size;
371 p = buf;
372 do {
373 ret = read(fd, p, len);
374 if (ret > 0) {
375 len -= ret;
376 if (!len)
377 break;
378 p += ret;
379 continue;
380 } else if (!ret)
381 break;
382 else if (errno == EAGAIN || errno == EINTR)
383 continue;
384 } while (1);
385
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200386 if (len) {
387 log_err("fio: failed reading job file %s\n", filename);
Jens Axboeb94cba42011-10-06 21:27:10 +0200388 close(fd);
Jens Axboec524ef72011-10-07 12:23:34 +0200389 free(buf);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200390 return 1;
391 }
392
Jens Axboe81179ee2011-10-04 12:42:06 +0200393 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, buf, sb.st_size);
Jens Axboe132159a2011-09-30 15:01:32 -0600394 free(buf);
Jens Axboeb94cba42011-10-06 21:27:10 +0200395 close(fd);
Jens Axboe132159a2011-09-30 15:01:32 -0600396 return ret;
397}
Jens Axboe37db14f2011-09-30 17:00:42 -0600398
Jens Axboea37f69b2011-10-01 12:24:21 -0600399int fio_clients_send_ini(const char *filename)
400{
401 struct fio_client *client;
402 struct flist_head *entry, *tmp;
403
404 flist_for_each_safe(entry, tmp, &client_list) {
405 client = flist_entry(entry, struct fio_client, list);
406
407 if (fio_client_send_ini(client, filename))
408 remove_client(client);
409 }
410
411 return !nr_clients;
412}
413
Jens Axboea64e88d2011-10-03 14:20:01 +0200414static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
415{
416 dst->max_val = le64_to_cpu(src->max_val);
417 dst->min_val = le64_to_cpu(src->min_val);
418 dst->samples = le64_to_cpu(src->samples);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200419
420 /*
421 * Floats arrive as IEEE 754 encoded uint64_t, convert back to double
422 */
423 dst->mean.u.f = fio_uint64_to_double(le64_to_cpu(dst->mean.u.i));
424 dst->S.u.f = fio_uint64_to_double(le64_to_cpu(dst->S.u.i));
Jens Axboea64e88d2011-10-03 14:20:01 +0200425}
426
427static void convert_ts(struct thread_stat *dst, struct thread_stat *src)
428{
429 int i, j;
430
431 dst->error = le32_to_cpu(src->error);
432 dst->groupid = le32_to_cpu(src->groupid);
433 dst->pid = le32_to_cpu(src->pid);
434 dst->members = le32_to_cpu(src->members);
435
436 for (i = 0; i < 2; i++) {
437 convert_io_stat(&dst->clat_stat[i], &src->clat_stat[i]);
438 convert_io_stat(&dst->slat_stat[i], &src->slat_stat[i]);
439 convert_io_stat(&dst->lat_stat[i], &src->lat_stat[i]);
440 convert_io_stat(&dst->bw_stat[i], &src->bw_stat[i]);
441 }
442
443 dst->usr_time = le64_to_cpu(src->usr_time);
444 dst->sys_time = le64_to_cpu(src->sys_time);
445 dst->ctx = le64_to_cpu(src->ctx);
446 dst->minf = le64_to_cpu(src->minf);
447 dst->majf = le64_to_cpu(src->majf);
448 dst->clat_percentiles = le64_to_cpu(src->clat_percentiles);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200449
450 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
451 fio_fp64_t *fps = &src->percentile_list[i];
452 fio_fp64_t *fpd = &dst->percentile_list[i];
453
454 fpd->u.f = fio_uint64_to_double(le64_to_cpu(fps->u.i));
455 }
Jens Axboea64e88d2011-10-03 14:20:01 +0200456
457 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
458 dst->io_u_map[i] = le32_to_cpu(src->io_u_map[i]);
459 dst->io_u_submit[i] = le32_to_cpu(src->io_u_submit[i]);
460 dst->io_u_complete[i] = le32_to_cpu(src->io_u_complete[i]);
461 }
462
463 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
464 dst->io_u_lat_u[i] = le32_to_cpu(src->io_u_lat_u[i]);
465 dst->io_u_lat_m[i] = le32_to_cpu(src->io_u_lat_m[i]);
466 }
467
468 for (i = 0; i < 2; i++)
469 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
470 dst->io_u_plat[i][j] = le32_to_cpu(src->io_u_plat[i][j]);
471
472 for (i = 0; i < 3; i++) {
473 dst->total_io_u[i] = le64_to_cpu(src->total_io_u[i]);
Jens Axboe93eee042011-10-03 21:08:48 +0200474 dst->short_io_u[i] = le64_to_cpu(src->short_io_u[i]);
Jens Axboea64e88d2011-10-03 14:20:01 +0200475 }
476
477 dst->total_submit = le64_to_cpu(src->total_submit);
478 dst->total_complete = le64_to_cpu(src->total_complete);
479
480 for (i = 0; i < 2; i++) {
481 dst->io_bytes[i] = le64_to_cpu(src->io_bytes[i]);
482 dst->runtime[i] = le64_to_cpu(src->runtime[i]);
483 }
484
485 dst->total_run_time = le64_to_cpu(src->total_run_time);
486 dst->continue_on_error = le16_to_cpu(src->continue_on_error);
487 dst->total_err_count = le64_to_cpu(src->total_err_count);
Jens Axboeddcc0b62011-10-03 14:45:27 +0200488 dst->first_error = le32_to_cpu(src->first_error);
489 dst->kb_base = le32_to_cpu(src->kb_base);
Jens Axboea64e88d2011-10-03 14:20:01 +0200490}
491
492static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
493{
494 int i;
495
496 for (i = 0; i < 2; i++) {
497 dst->max_run[i] = le64_to_cpu(src->max_run[i]);
498 dst->min_run[i] = le64_to_cpu(src->min_run[i]);
499 dst->max_bw[i] = le64_to_cpu(src->max_bw[i]);
500 dst->min_bw[i] = le64_to_cpu(src->min_bw[i]);
501 dst->io_kb[i] = le64_to_cpu(src->io_kb[i]);
502 dst->agg[i] = le64_to_cpu(src->agg[i]);
503 }
504
505 dst->kb_base = le32_to_cpu(src->kb_base);
506 dst->groupid = le32_to_cpu(src->groupid);
507}
508
509static void handle_ts(struct fio_net_cmd *cmd)
510{
511 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
512
513 convert_ts(&p->ts, &p->ts);
514 convert_gs(&p->rs, &p->rs);
515
516 show_thread_status(&p->ts, &p->rs);
517}
518
519static void handle_gs(struct fio_net_cmd *cmd)
520{
521 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
522
523 convert_gs(gs, gs);
524 show_group_stats(gs);
525}
526
Jens Axboecf451d12011-10-03 16:48:30 +0200527static void handle_eta(struct fio_net_cmd *cmd)
528{
529 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
530 int i;
531
532 je->nr_running = le32_to_cpu(je->nr_running);
533 je->nr_ramp = le32_to_cpu(je->nr_ramp);
534 je->nr_pending = le32_to_cpu(je->nr_pending);
535 je->files_open = le32_to_cpu(je->files_open);
536 je->m_rate = le32_to_cpu(je->m_rate);
537 je->t_rate = le32_to_cpu(je->t_rate);
538 je->m_iops = le32_to_cpu(je->m_iops);
539 je->t_iops = le32_to_cpu(je->t_iops);
540
541 for (i = 0; i < 2; i++) {
542 je->rate[i] = le32_to_cpu(je->rate[i]);
543 je->iops[i] = le32_to_cpu(je->iops[i]);
544 }
545
Jens Axboeb51eedb2011-10-09 12:13:39 +0200546 je->elapsed_sec = le64_to_cpu(je->elapsed_sec);
Jens Axboecf451d12011-10-03 16:48:30 +0200547 je->eta_sec = le64_to_cpu(je->eta_sec);
548
549 display_thread_status(je);
550}
551
Jens Axboeb5296dd2011-10-07 16:35:56 +0200552static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd)
Jens Axboe2e03b4b2011-10-04 09:00:40 +0200553{
554 struct cmd_probe_pdu *probe = (struct cmd_probe_pdu *) cmd->payload;
Jens Axboecca84642011-10-07 12:47:57 +0200555 const char *os, *arch;
Jens Axboe2e03b4b2011-10-04 09:00:40 +0200556
Jens Axboecca84642011-10-07 12:47:57 +0200557 os = fio_get_os_string(probe->os);
558 if (!os)
559 os = "unknown";
560
561 arch = fio_get_arch_string(probe->arch);
562 if (!arch)
563 os = "unknown";
564
Jens Axboec71233e2011-10-07 13:11:14 +0200565 log_info("hostname=%s, be=%u, os=%s, arch=%s, fio=%u.%u.%u\n",
Jens Axboecca84642011-10-07 12:47:57 +0200566 probe->hostname, probe->bigendian, os, arch, probe->fio_major,
Jens Axboe6eb24792011-10-04 15:00:30 +0200567 probe->fio_minor, probe->fio_patch);
Jens Axboeb5296dd2011-10-07 16:35:56 +0200568
569 if (!client->name)
570 client->name = strdup((char *) probe->hostname);
Jens Axboe2e03b4b2011-10-04 09:00:40 +0200571}
572
Jens Axboee951bdc2011-10-05 21:58:45 +0200573static int handle_client(struct fio_client *client)
Jens Axboe37db14f2011-09-30 17:00:42 -0600574{
575 struct fio_net_cmd *cmd;
576
Jens Axboe60efd142011-10-04 13:30:11 +0200577 dprint(FD_NET, "client: handle %s\n", client->hostname);
578
Jens Axboee951bdc2011-10-05 21:58:45 +0200579 cmd = fio_net_recv_cmd(client->fd);
580 if (!cmd)
581 return 0;
Jens Axboec2c94582011-10-05 20:31:30 +0200582
Jens Axboee951bdc2011-10-05 21:58:45 +0200583 dprint(FD_NET, "client: got cmd op %d from %s\n",
Jens Axboec2c94582011-10-05 20:31:30 +0200584 cmd->opcode, client->hostname);
Jens Axboe46c48f12011-10-01 12:50:51 -0600585
Jens Axboee951bdc2011-10-05 21:58:45 +0200586 switch (cmd->opcode) {
587 case FIO_NET_CMD_QUIT:
588 remove_client(client);
589 free(cmd);
590 break;
591 case FIO_NET_CMD_TEXT: {
592 const char *buf = (const char *) cmd->payload;
Jens Axboeb5296dd2011-10-07 16:35:56 +0200593 const char *name;
Jens Axboee951bdc2011-10-05 21:58:45 +0200594 int fio_unused ret;
Jens Axboe17dd1762011-10-04 13:27:34 +0200595
Jens Axboeb5296dd2011-10-07 16:35:56 +0200596 name = client->name ? client->name : client->hostname;
597
Jens Axboee951bdc2011-10-05 21:58:45 +0200598 if (!client->skip_newline)
Jens Axboeb5296dd2011-10-07 16:35:56 +0200599 fprintf(f_out, "<%s> ", name);
Jens Axboee951bdc2011-10-05 21:58:45 +0200600 ret = fwrite(buf, cmd->pdu_len, 1, f_out);
601 fflush(f_out);
602 client->skip_newline = strchr(buf, '\n') == NULL;
603 free(cmd);
604 break;
Jens Axboe37db14f2011-09-30 17:00:42 -0600605 }
Jens Axboee951bdc2011-10-05 21:58:45 +0200606 case FIO_NET_CMD_TS:
607 handle_ts(cmd);
608 free(cmd);
609 break;
610 case FIO_NET_CMD_GS:
611 handle_gs(cmd);
612 free(cmd);
613 break;
614 case FIO_NET_CMD_ETA:
615 handle_eta(cmd);
616 free(cmd);
617 break;
618 case FIO_NET_CMD_PROBE:
Jens Axboeb5296dd2011-10-07 16:35:56 +0200619 handle_probe(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +0200620 free(cmd);
621 break;
622 case FIO_NET_CMD_START:
623 client->state = Client_started;
624 free(cmd);
625 break;
626 case FIO_NET_CMD_STOP:
627 client->state = Client_stopped;
628 free(cmd);
629 break;
630 default:
631 log_err("fio: unknown client op: %d\n", cmd->opcode);
632 free(cmd);
633 break;
Jens Axboe37db14f2011-09-30 17:00:42 -0600634 }
635
Jens Axboee951bdc2011-10-05 21:58:45 +0200636 return 1;
Jens Axboe37db14f2011-09-30 17:00:42 -0600637}
Jens Axboeb66570d2011-10-01 11:11:35 -0600638
639int fio_handle_clients(void)
640{
641 struct fio_client *client;
642 struct flist_head *entry;
643 struct pollfd *pfds;
Jens Axboe82a4be12011-10-01 12:40:32 -0600644 int i, ret = 0;
Jens Axboeb66570d2011-10-01 11:11:35 -0600645
646 pfds = malloc(nr_clients * sizeof(struct pollfd));
647
Jens Axboeb66570d2011-10-01 11:11:35 -0600648 while (!exit_backend && nr_clients) {
Jens Axboe82a4be12011-10-01 12:40:32 -0600649 i = 0;
650 flist_for_each(entry, &client_list) {
651 client = flist_entry(entry, struct fio_client, list);
652
653 pfds[i].fd = client->fd;
654 pfds[i].events = POLLIN;
655 i++;
656 }
657
658 assert(i == nr_clients);
659
Jens Axboe5c2857f2011-10-04 13:14:32 +0200660 do {
661 ret = poll(pfds, nr_clients, 100);
662 if (ret < 0) {
663 if (errno == EINTR)
664 continue;
665 log_err("fio: poll clients: %s\n", strerror(errno));
666 break;
667 } else if (!ret)
Jens Axboeb66570d2011-10-01 11:11:35 -0600668 continue;
Jens Axboe5c2857f2011-10-04 13:14:32 +0200669 } while (ret <= 0);
Jens Axboeb66570d2011-10-01 11:11:35 -0600670
671 for (i = 0; i < nr_clients; i++) {
672 if (!(pfds[i].revents & POLLIN))
673 continue;
674
675 client = find_client_by_fd(pfds[i].fd);
676 if (!client) {
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200677 log_err("fio: unknown client fd %d\n", pfds[i].fd);
Jens Axboeb66570d2011-10-01 11:11:35 -0600678 continue;
679 }
Jens Axboee951bdc2011-10-05 21:58:45 +0200680 if (!handle_client(client)) {
Jens Axboe28d3ab02011-10-05 20:45:37 +0200681 log_info("client: host=%s disconnected\n",
682 client->hostname);
683 remove_client(client);
684 }
Jens Axboeb66570d2011-10-01 11:11:35 -0600685 }
686 }
687
688 free(pfds);
Jens Axboeb66570d2011-10-01 11:11:35 -0600689 return 0;
690}