blob: 678a62753021035568cec1fb54ac1f23e8d7bcca [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 Axboe1b427252012-03-14 15:03:03 +010017#include <zlib.h>
Jens Axboe132159a2011-09-30 15:01:32 -060018
19#include "fio.h"
Stephen M. Camerondd366722012-02-24 08:17:30 +010020#include "client.h"
Jens Axboe132159a2011-09-30 15:01:32 -060021#include "server.h"
Jens Axboeb66570d2011-10-01 11:11:35 -060022#include "flist.h"
Jens Axboe3c5f57e2011-10-06 12:37:50 +020023#include "hash.h"
Jens Axboe132159a2011-09-30 15:01:32 -060024
Stephen M. Camerondd366722012-02-24 08:17:30 +010025static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd);
Jens Axboe89e5fad2012-03-05 09:21:12 +010026static void handle_ts(struct fio_client *client, struct fio_net_cmd *cmd);
27static void handle_gs(struct fio_client *client, struct fio_net_cmd *cmd);
Stephen M. Camerondd366722012-02-24 08:17:30 +010028static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd);
Jens Axboe084d1c62012-03-03 20:28:07 +010029static void handle_text(struct fio_client *client, struct fio_net_cmd *cmd);
Jens Axboe6b79c802012-03-08 10:51:36 +010030static void handle_stop(struct fio_client *client, struct fio_net_cmd *cmd);
Jens Axboe85dd01e2012-03-12 14:33:16 +010031static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd);
Stephen M. Camerondd366722012-02-24 08:17:30 +010032
33struct client_ops fio_client_ops = {
Jens Axboe35c0ba72012-03-14 10:56:40 +010034 .text = handle_text,
Jens Axboe0420ba62012-02-29 11:16:52 +010035 .disk_util = handle_du,
36 .thread_status = handle_ts,
37 .group_stats = handle_gs,
Jens Axboe6b79c802012-03-08 10:51:36 +010038 .stop = handle_stop,
Jens Axboe85dd01e2012-03-12 14:33:16 +010039 .start = handle_start,
Jens Axboea5276612012-03-04 15:15:08 +010040 .eta = display_thread_status,
Jens Axboe0420ba62012-02-29 11:16:52 +010041 .probe = handle_probe,
Jens Axboe6433ee02012-03-09 20:10:51 +010042 .eta_msec = FIO_CLIENT_DEF_ETA_MSEC,
Jens Axboe46bcd492012-03-14 11:31:21 +010043 .client_type = FIO_CLIENT_TYPE_CLI,
Stephen M. Camerondd366722012-02-24 08:17:30 +010044};
45
Jens Axboeaf9c9fb2011-10-09 21:54:10 +020046static struct timeval eta_tv;
Jens Axboe48fbb462011-10-09 12:19:08 +020047
Jens Axboeb66570d2011-10-01 11:11:35 -060048static FLIST_HEAD(client_list);
Jens Axboe82c1ed32011-10-10 08:56:18 +020049static FLIST_HEAD(eta_list);
Jens Axboeb66570d2011-10-01 11:11:35 -060050
Jens Axboe3f3a4542011-10-10 21:11:09 +020051static FLIST_HEAD(arg_list);
52
Jens Axboe3650a3c2012-03-05 14:09:03 +010053struct thread_stat client_ts;
54struct group_run_stats client_gs;
55int sum_stat_clients;
56
Jens Axboe37f0c1a2011-10-11 14:08:33 +020057static int sum_stat_nr;
58
Jens Axboe3c5f57e2011-10-06 12:37:50 +020059#define FIO_CLIENT_HASH_BITS 7
60#define FIO_CLIENT_HASH_SZ (1 << FIO_CLIENT_HASH_BITS)
61#define FIO_CLIENT_HASH_MASK (FIO_CLIENT_HASH_SZ - 1)
Jens Axboebebe6392011-10-07 10:00:51 +020062static struct flist_head client_hash[FIO_CLIENT_HASH_SZ];
Jens Axboe3c5f57e2011-10-06 12:37:50 +020063
Jens Axboebebe6392011-10-07 10:00:51 +020064static void fio_client_add_hash(struct fio_client *client)
Jens Axboe3c5f57e2011-10-06 12:37:50 +020065{
66 int bucket = hash_long(client->fd, FIO_CLIENT_HASH_BITS);
67
68 bucket &= FIO_CLIENT_HASH_MASK;
Jens Axboebebe6392011-10-07 10:00:51 +020069 flist_add(&client->hash_list, &client_hash[bucket]);
Jens Axboe3c5f57e2011-10-06 12:37:50 +020070}
71
Jens Axboebebe6392011-10-07 10:00:51 +020072static void fio_client_remove_hash(struct fio_client *client)
Jens Axboe3c5f57e2011-10-06 12:37:50 +020073{
Jens Axboebebe6392011-10-07 10:00:51 +020074 if (!flist_empty(&client->hash_list))
75 flist_del_init(&client->hash_list);
Jens Axboe3c5f57e2011-10-06 12:37:50 +020076}
77
78static void fio_init fio_client_hash_init(void)
79{
80 int i;
81
Jens Axboebebe6392011-10-07 10:00:51 +020082 for (i = 0; i < FIO_CLIENT_HASH_SZ; i++)
83 INIT_FLIST_HEAD(&client_hash[i]);
Jens Axboe3c5f57e2011-10-06 12:37:50 +020084}
85
Jens Axboeb66570d2011-10-01 11:11:35 -060086static struct fio_client *find_client_by_fd(int fd)
87{
Jens Axboe3c5f57e2011-10-06 12:37:50 +020088 int bucket = hash_long(fd, FIO_CLIENT_HASH_BITS) & FIO_CLIENT_HASH_MASK;
Jens Axboeb66570d2011-10-01 11:11:35 -060089 struct fio_client *client;
90 struct flist_head *entry;
91
Jens Axboebebe6392011-10-07 10:00:51 +020092 flist_for_each(entry, &client_hash[bucket]) {
93 client = flist_entry(entry, struct fio_client, hash_list);
Jens Axboeb66570d2011-10-01 11:11:35 -060094
Jens Axboee55f8f32012-03-06 15:42:31 +010095 if (client->fd == fd) {
96 client->refs++;
Jens Axboeb66570d2011-10-01 11:11:35 -060097 return client;
Jens Axboee55f8f32012-03-06 15:42:31 +010098 }
Jens Axboeb66570d2011-10-01 11:11:35 -060099 }
100
101 return NULL;
102}
103
Jens Axboe3af45202012-03-13 09:59:53 +0100104void fio_put_client(struct fio_client *client)
Jens Axboeb66570d2011-10-01 11:11:35 -0600105{
Jens Axboe5121a9a2012-03-05 13:32:47 +0100106 if (--client->refs)
107 return;
108
Jens Axboeb66570d2011-10-01 11:11:35 -0600109 free(client->hostname);
Jens Axboe81179ee2011-10-04 12:42:06 +0200110 if (client->argv)
111 free(client->argv);
Jens Axboeb5296dd2011-10-07 16:35:56 +0200112 if (client->name)
113 free(client->name);
Jens Axboe81179ee2011-10-04 12:42:06 +0200114
Jens Axboeb66570d2011-10-01 11:11:35 -0600115 free(client);
116}
Jens Axboe132159a2011-09-30 15:01:32 -0600117
Jens Axboe3af45202012-03-13 09:59:53 +0100118static void remove_client(struct fio_client *client)
Jens Axboe5121a9a2012-03-05 13:32:47 +0100119{
Jens Axboe3af45202012-03-13 09:59:53 +0100120 assert(client->refs);
121
122 dprint(FD_NET, "client: removed <%s>\n", client->hostname);
123
124 if (!flist_empty(&client->list))
125 flist_del_init(&client->list);
126
127 fio_client_remove_hash(client);
128
129 if (!flist_empty(&client->eta_list)) {
130 flist_del_init(&client->eta_list);
131 fio_client_dec_jobs_eta(client->eta_in_flight, client->ops->eta);
132 }
133
134 nr_clients--;
135 sum_stat_clients--;
136
137 fio_put_client(client);
Jens Axboe5121a9a2012-03-05 13:32:47 +0100138}
139
Jens Axboe343cb4a2012-03-09 17:16:51 +0100140struct fio_client *fio_get_client(struct fio_client *client)
141{
142 client->refs++;
143 return client;
144}
145
Jens Axboefa2ea802011-10-08 21:07:29 +0200146static void __fio_client_add_cmd_option(struct fio_client *client,
147 const char *opt)
Jens Axboe81179ee2011-10-04 12:42:06 +0200148{
Jens Axboe39e8e012011-10-04 13:46:08 +0200149 int index;
150
151 index = client->argc++;
Jens Axboe81179ee2011-10-04 12:42:06 +0200152 client->argv = realloc(client->argv, sizeof(char *) * client->argc);
Jens Axboe39e8e012011-10-04 13:46:08 +0200153 client->argv[index] = strdup(opt);
154 dprint(FD_NET, "client: add cmd %d: %s\n", index, opt);
Jens Axboe81179ee2011-10-04 12:42:06 +0200155}
156
Jens Axboefa2ea802011-10-08 21:07:29 +0200157void fio_client_add_cmd_option(void *cookie, const char *opt)
Jens Axboe81179ee2011-10-04 12:42:06 +0200158{
Jens Axboebebe6392011-10-07 10:00:51 +0200159 struct fio_client *client = cookie;
Jens Axboe3f3a4542011-10-10 21:11:09 +0200160 struct flist_head *entry;
Jens Axboe81179ee2011-10-04 12:42:06 +0200161
Jens Axboebebe6392011-10-07 10:00:51 +0200162 if (!client || !opt)
Jens Axboefa2ea802011-10-08 21:07:29 +0200163 return;
Jens Axboe81179ee2011-10-04 12:42:06 +0200164
Jens Axboefa2ea802011-10-08 21:07:29 +0200165 __fio_client_add_cmd_option(client, opt);
Jens Axboe3f3a4542011-10-10 21:11:09 +0200166
167 /*
168 * Duplicate arguments to shared client group
169 */
170 flist_for_each(entry, &arg_list) {
171 client = flist_entry(entry, struct fio_client, arg_list);
172
173 __fio_client_add_cmd_option(client, opt);
174 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200175}
176
Jens Axboea5276612012-03-04 15:15:08 +0100177struct fio_client *fio_client_add_explicit(struct client_ops *ops,
178 const char *hostname, int type,
Jens Axboe3ec62ec2012-03-01 12:01:29 +0100179 int port)
180{
181 struct fio_client *client;
182
183 client = malloc(sizeof(*client));
184 memset(client, 0, sizeof(*client));
185
186 INIT_FLIST_HEAD(&client->list);
187 INIT_FLIST_HEAD(&client->hash_list);
188 INIT_FLIST_HEAD(&client->arg_list);
189 INIT_FLIST_HEAD(&client->eta_list);
190 INIT_FLIST_HEAD(&client->cmd_list);
191
192 client->hostname = strdup(hostname);
193
194 if (type == Fio_client_socket)
195 client->is_sock = 1;
196 else {
197 int ipv6;
198
199 ipv6 = type == Fio_client_ipv6;
200 if (fio_server_parse_host(hostname, &ipv6,
201 &client->addr.sin_addr,
202 &client->addr6.sin6_addr))
203 goto err;
204
205 client->port = port;
206 }
207
208 client->fd = -1;
Jens Axboea5276612012-03-04 15:15:08 +0100209 client->ops = ops;
Jens Axboe5121a9a2012-03-05 13:32:47 +0100210 client->refs = 1;
Jens Axboe46bcd492012-03-14 11:31:21 +0100211 client->type = ops->client_type;
Jens Axboe3ec62ec2012-03-01 12:01:29 +0100212
213 __fio_client_add_cmd_option(client, "fio");
214
215 flist_add(&client->list, &client_list);
216 nr_clients++;
217 dprint(FD_NET, "client: added <%s>\n", client->hostname);
218 return client;
219err:
220 free(client);
221 return NULL;
222}
223
Jens Axboea5276612012-03-04 15:15:08 +0100224int fio_client_add(struct client_ops *ops, const char *hostname, void **cookie)
Jens Axboe132159a2011-09-30 15:01:32 -0600225{
Jens Axboe3f3a4542011-10-10 21:11:09 +0200226 struct fio_client *existing = *cookie;
Jens Axboeb66570d2011-10-01 11:11:35 -0600227 struct fio_client *client;
Jens Axboe132159a2011-09-30 15:01:32 -0600228
Jens Axboe3f3a4542011-10-10 21:11:09 +0200229 if (existing) {
230 /*
231 * We always add our "exec" name as the option, hence 1
232 * means empty.
233 */
234 if (existing->argc == 1)
235 flist_add_tail(&existing->arg_list, &arg_list);
236 else {
237 while (!flist_empty(&arg_list))
238 flist_del_init(arg_list.next);
239 }
240 }
241
Jens Axboeb66570d2011-10-01 11:11:35 -0600242 client = malloc(sizeof(*client));
Jens Axboea37f69b2011-10-01 12:24:21 -0600243 memset(client, 0, sizeof(*client));
Jens Axboe81179ee2011-10-04 12:42:06 +0200244
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200245 INIT_FLIST_HEAD(&client->list);
Jens Axboebebe6392011-10-07 10:00:51 +0200246 INIT_FLIST_HEAD(&client->hash_list);
Jens Axboe3f3a4542011-10-10 21:11:09 +0200247 INIT_FLIST_HEAD(&client->arg_list);
Jens Axboe82c1ed32011-10-10 08:56:18 +0200248 INIT_FLIST_HEAD(&client->eta_list);
Jens Axboe89c17072011-10-11 10:15:51 +0200249 INIT_FLIST_HEAD(&client->cmd_list);
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200250
Jens Axboebebe6392011-10-07 10:00:51 +0200251 if (fio_server_parse_string(hostname, &client->hostname,
252 &client->is_sock, &client->port,
Jens Axboe811826b2011-10-24 09:11:50 +0200253 &client->addr.sin_addr,
254 &client->addr6.sin6_addr,
255 &client->ipv6))
Jens Axboebebe6392011-10-07 10:00:51 +0200256 return -1;
257
Jens Axboea37f69b2011-10-01 12:24:21 -0600258 client->fd = -1;
Jens Axboea5276612012-03-04 15:15:08 +0100259 client->ops = ops;
Jens Axboe5121a9a2012-03-05 13:32:47 +0100260 client->refs = 1;
Jens Axboe46bcd492012-03-14 11:31:21 +0100261 client->type = ops->client_type;
Jens Axboe81179ee2011-10-04 12:42:06 +0200262
263 __fio_client_add_cmd_option(client, "fio");
264
Jens Axboea37f69b2011-10-01 12:24:21 -0600265 flist_add(&client->list, &client_list);
266 nr_clients++;
Jens Axboebebe6392011-10-07 10:00:51 +0200267 dprint(FD_NET, "client: added <%s>\n", client->hostname);
268 *cookie = client;
269 return 0;
Jens Axboea37f69b2011-10-01 12:24:21 -0600270}
271
Jens Axboed824c3b2012-03-09 17:45:43 +0100272static void probe_client(struct fio_client *client)
273{
274 dprint(FD_NET, "client: send probe\n");
275
276 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_PROBE, 0, &client->cmd_list);
277}
278
Jens Axboe87aa8f12011-10-06 21:24:13 +0200279static int fio_client_connect_ip(struct fio_client *client)
Jens Axboea37f69b2011-10-01 12:24:21 -0600280{
Jens Axboe811826b2011-10-24 09:11:50 +0200281 struct sockaddr *addr;
282 fio_socklen_t socklen;
283 int fd, domain;
Jens Axboe132159a2011-09-30 15:01:32 -0600284
Jens Axboe811826b2011-10-24 09:11:50 +0200285 if (client->ipv6) {
286 client->addr6.sin6_family = AF_INET6;
287 client->addr6.sin6_port = htons(client->port);
288 domain = AF_INET6;
289 addr = (struct sockaddr *) &client->addr6;
290 socklen = sizeof(client->addr6);
291 } else {
292 client->addr.sin_family = AF_INET;
293 client->addr.sin_port = htons(client->port);
294 domain = AF_INET;
295 addr = (struct sockaddr *) &client->addr;
296 socklen = sizeof(client->addr);
297 }
Jens Axboe132159a2011-09-30 15:01:32 -0600298
Jens Axboe811826b2011-10-24 09:11:50 +0200299 fd = socket(domain, SOCK_STREAM, 0);
Jens Axboe132159a2011-09-30 15:01:32 -0600300 if (fd < 0) {
Jens Axboe25095e12012-03-09 17:09:55 +0100301 int ret = -errno;
302
Jens Axboe132159a2011-09-30 15:01:32 -0600303 log_err("fio: socket: %s\n", strerror(errno));
Jens Axboe25095e12012-03-09 17:09:55 +0100304 return ret;
Jens Axboe132159a2011-09-30 15:01:32 -0600305 }
306
Jens Axboe811826b2011-10-24 09:11:50 +0200307 if (connect(fd, addr, socklen) < 0) {
Jens Axboe25095e12012-03-09 17:09:55 +0100308 int ret = -errno;
309
Jens Axboe132159a2011-09-30 15:01:32 -0600310 log_err("fio: connect: %s\n", strerror(errno));
Jens Axboea7de0a12011-10-07 12:55:14 +0200311 log_err("fio: failed to connect to %s:%u\n", client->hostname,
312 client->port);
Jens Axboeb94cba42011-10-06 21:27:10 +0200313 close(fd);
Jens Axboe25095e12012-03-09 17:09:55 +0100314 return ret;
Jens Axboe132159a2011-09-30 15:01:32 -0600315 }
316
Jens Axboe87aa8f12011-10-06 21:24:13 +0200317 return fd;
318}
319
320static int fio_client_connect_sock(struct fio_client *client)
321{
322 struct sockaddr_un *addr = &client->addr_un;
323 fio_socklen_t len;
324 int fd;
325
326 memset(addr, 0, sizeof(*addr));
327 addr->sun_family = AF_UNIX;
328 strcpy(addr->sun_path, client->hostname);
329
330 fd = socket(AF_UNIX, SOCK_STREAM, 0);
331 if (fd < 0) {
Jens Axboe25095e12012-03-09 17:09:55 +0100332 int ret = -errno;
333
Jens Axboe87aa8f12011-10-06 21:24:13 +0200334 log_err("fio: socket: %s\n", strerror(errno));
Jens Axboe25095e12012-03-09 17:09:55 +0100335 return ret;
Jens Axboe87aa8f12011-10-06 21:24:13 +0200336 }
337
338 len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
339 if (connect(fd, (struct sockaddr *) addr, len) < 0) {
Jens Axboe25095e12012-03-09 17:09:55 +0100340 int ret = -errno;
341
Jens Axboe87aa8f12011-10-06 21:24:13 +0200342 log_err("fio: connect; %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200343 close(fd);
Jens Axboe25095e12012-03-09 17:09:55 +0100344 return ret;
Jens Axboe87aa8f12011-10-06 21:24:13 +0200345 }
346
347 return fd;
348}
349
Jens Axboe2f99deb2012-03-09 14:37:29 +0100350int fio_client_connect(struct fio_client *client)
Jens Axboe87aa8f12011-10-06 21:24:13 +0200351{
352 int fd;
353
354 dprint(FD_NET, "client: connect to host %s\n", client->hostname);
355
Jens Axboe87aa8f12011-10-06 21:24:13 +0200356 if (client->is_sock)
357 fd = fio_client_connect_sock(client);
358 else
359 fd = fio_client_connect_ip(client);
360
Jens Axboe89c17072011-10-11 10:15:51 +0200361 dprint(FD_NET, "client: %s connected %d\n", client->hostname, fd);
362
Jens Axboe87aa8f12011-10-06 21:24:13 +0200363 if (fd < 0)
Jens Axboea1a3ed52012-03-09 17:00:01 +0100364 return fd;
Jens Axboe87aa8f12011-10-06 21:24:13 +0200365
Jens Axboeb66570d2011-10-01 11:11:35 -0600366 client->fd = fd;
Jens Axboebebe6392011-10-07 10:00:51 +0200367 fio_client_add_hash(client);
Jens Axboe81179ee2011-10-04 12:42:06 +0200368 client->state = Client_connected;
Jens Axboed824c3b2012-03-09 17:45:43 +0100369
370 probe_client(client);
Jens Axboe132159a2011-09-30 15:01:32 -0600371 return 0;
372}
373
Jens Axboe2f99deb2012-03-09 14:37:29 +0100374void fio_client_terminate(struct fio_client *client)
375{
376 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_QUIT, 0, NULL);
377}
378
Jens Axboecc0df002011-10-03 20:53:32 +0200379void fio_clients_terminate(void)
380{
381 struct flist_head *entry;
382 struct fio_client *client;
383
Jens Axboe60efd142011-10-04 13:30:11 +0200384 dprint(FD_NET, "client: terminate clients\n");
385
Jens Axboecc0df002011-10-03 20:53:32 +0200386 flist_for_each(entry, &client_list) {
387 client = flist_entry(entry, struct fio_client, list);
Jens Axboe2f99deb2012-03-09 14:37:29 +0100388 fio_client_terminate(client);
Jens Axboecc0df002011-10-03 20:53:32 +0200389 }
390}
391
392static void sig_int(int sig)
393{
Jens Axboebebe6392011-10-07 10:00:51 +0200394 dprint(FD_NET, "client: got signal %d\n", sig);
Jens Axboecc0df002011-10-03 20:53:32 +0200395 fio_clients_terminate();
396}
397
398static void client_signal_handler(void)
399{
400 struct sigaction act;
401
402 memset(&act, 0, sizeof(act));
403 act.sa_handler = sig_int;
404 act.sa_flags = SA_RESTART;
405 sigaction(SIGINT, &act, NULL);
406
407 memset(&act, 0, sizeof(act));
408 act.sa_handler = sig_int;
409 act.sa_flags = SA_RESTART;
410 sigaction(SIGTERM, &act, NULL);
411}
412
Jens Axboe81179ee2011-10-04 12:42:06 +0200413static int send_client_cmd_line(struct fio_client *client)
414{
Jens Axboefa2ea802011-10-08 21:07:29 +0200415 struct cmd_single_line_pdu *cslp;
416 struct cmd_line_pdu *clp;
417 unsigned long offset;
Jens Axboe7f868312011-10-10 09:55:21 +0200418 unsigned int *lens;
Jens Axboefa2ea802011-10-08 21:07:29 +0200419 void *pdu;
420 size_t mem;
Jens Axboe81179ee2011-10-04 12:42:06 +0200421 int i, ret;
422
Jens Axboe39e8e012011-10-04 13:46:08 +0200423 dprint(FD_NET, "client: send cmdline %d\n", client->argc);
Jens Axboe60efd142011-10-04 13:30:11 +0200424
Jens Axboe7f868312011-10-10 09:55:21 +0200425 lens = malloc(client->argc * sizeof(unsigned int));
426
Jens Axboefa2ea802011-10-08 21:07:29 +0200427 /*
428 * Find out how much mem we need
429 */
Jens Axboe7f868312011-10-10 09:55:21 +0200430 for (i = 0, mem = 0; i < client->argc; i++) {
431 lens[i] = strlen(client->argv[i]) + 1;
432 mem += lens[i];
433 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200434
Jens Axboefa2ea802011-10-08 21:07:29 +0200435 /*
436 * We need one cmd_line_pdu, and argc number of cmd_single_line_pdu
437 */
438 mem += sizeof(*clp) + (client->argc * sizeof(*cslp));
439
440 pdu = malloc(mem);
441 clp = pdu;
442 offset = sizeof(*clp);
443
444 for (i = 0; i < client->argc; i++) {
Jens Axboe7f868312011-10-10 09:55:21 +0200445 uint16_t arg_len = lens[i];
Jens Axboefa2ea802011-10-08 21:07:29 +0200446
447 cslp = pdu + offset;
448 strcpy((char *) cslp->text, client->argv[i]);
449 cslp->len = cpu_to_le16(arg_len);
450 offset += sizeof(*cslp) + arg_len;
451 }
452
Jens Axboe7f868312011-10-10 09:55:21 +0200453 free(lens);
Jens Axboefa2ea802011-10-08 21:07:29 +0200454 clp->lines = cpu_to_le16(client->argc);
Jens Axboe46bcd492012-03-14 11:31:21 +0100455 clp->client_type = __cpu_to_le16(client->type);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200456 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOBLINE, pdu, mem, 0);
Jens Axboe81179ee2011-10-04 12:42:06 +0200457 free(pdu);
458 return ret;
459}
460
Jens Axboea37f69b2011-10-01 12:24:21 -0600461int fio_clients_connect(void)
462{
463 struct fio_client *client;
464 struct flist_head *entry, *tmp;
465 int ret;
466
Bruce Cran93bcfd22012-02-20 20:18:19 +0100467#ifdef WIN32
468 WSADATA wsd;
469 WSAStartup(MAKEWORD(2,2), &wsd);
470#endif
471
Jens Axboe60efd142011-10-04 13:30:11 +0200472 dprint(FD_NET, "client: connect all\n");
473
Jens Axboecc0df002011-10-03 20:53:32 +0200474 client_signal_handler();
475
Jens Axboea37f69b2011-10-01 12:24:21 -0600476 flist_for_each_safe(entry, tmp, &client_list) {
477 client = flist_entry(entry, struct fio_client, list);
478
479 ret = fio_client_connect(client);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200480 if (ret) {
Jens Axboea37f69b2011-10-01 12:24:21 -0600481 remove_client(client);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200482 continue;
483 }
484
Jens Axboe81179ee2011-10-04 12:42:06 +0200485 if (client->argc > 1)
486 send_client_cmd_line(client);
Jens Axboea37f69b2011-10-01 12:24:21 -0600487 }
488
489 return !nr_clients;
490}
491
Jens Axboeb9d2f302012-03-08 20:36:28 +0100492int fio_start_client(struct fio_client *client)
493{
494 dprint(FD_NET, "client: start %s\n", client->hostname);
495 return fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_RUN, 0, NULL);
496}
497
498int fio_start_all_clients(void)
499{
500 struct fio_client *client;
501 struct flist_head *entry, *tmp;
502 int ret;
503
504 dprint(FD_NET, "client: start all\n");
505
506 flist_for_each_safe(entry, tmp, &client_list) {
507 client = flist_entry(entry, struct fio_client, list);
508
509 ret = fio_start_client(client);
510 if (ret) {
511 remove_client(client);
512 continue;
513 }
514 }
515
516 return flist_empty(&client_list);
517}
518
Jens Axboe132159a2011-09-30 15:01:32 -0600519/*
520 * Send file contents to server backend. We could use sendfile(), but to remain
521 * more portable lets just read/write the darn thing.
522 */
Jens Axboe9988ca72012-03-09 15:14:06 +0100523static int __fio_client_send_ini(struct fio_client *client, const char *filename)
Jens Axboe132159a2011-09-30 15:01:32 -0600524{
Jens Axboe46bcd492012-03-14 11:31:21 +0100525 struct cmd_job_pdu *pdu;
526 size_t p_size;
Jens Axboe132159a2011-09-30 15:01:32 -0600527 struct stat sb;
Jens Axboe46bcd492012-03-14 11:31:21 +0100528 char *p;
529 void *buf;
Jens Axboe132159a2011-09-30 15:01:32 -0600530 off_t len;
531 int fd, ret;
532
Jens Axboe46c48f12011-10-01 12:50:51 -0600533 dprint(FD_NET, "send ini %s to %s\n", filename, client->hostname);
534
Jens Axboe132159a2011-09-30 15:01:32 -0600535 fd = open(filename, O_RDONLY);
536 if (fd < 0) {
Jens Axboe25095e12012-03-09 17:09:55 +0100537 int ret = -errno;
538
Jens Axboee951bdc2011-10-05 21:58:45 +0200539 log_err("fio: job file <%s> open: %s\n", filename, strerror(errno));
Jens Axboe25095e12012-03-09 17:09:55 +0100540 return ret;
Jens Axboe132159a2011-09-30 15:01:32 -0600541 }
542
543 if (fstat(fd, &sb) < 0) {
Jens Axboe25095e12012-03-09 17:09:55 +0100544 int ret = -errno;
545
Jens Axboe132159a2011-09-30 15:01:32 -0600546 log_err("fio: job file stat: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200547 close(fd);
Jens Axboe25095e12012-03-09 17:09:55 +0100548 return ret;
Jens Axboe132159a2011-09-30 15:01:32 -0600549 }
550
Jens Axboe46bcd492012-03-14 11:31:21 +0100551 p_size = sb.st_size + sizeof(*pdu);
552 pdu = malloc(p_size);
553 buf = pdu->buf;
Jens Axboe132159a2011-09-30 15:01:32 -0600554
555 len = sb.st_size;
556 p = buf;
557 do {
558 ret = read(fd, p, len);
559 if (ret > 0) {
560 len -= ret;
561 if (!len)
562 break;
563 p += ret;
564 continue;
565 } else if (!ret)
566 break;
567 else if (errno == EAGAIN || errno == EINTR)
568 continue;
569 } while (1);
570
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200571 if (len) {
572 log_err("fio: failed reading job file %s\n", filename);
Jens Axboeb94cba42011-10-06 21:27:10 +0200573 close(fd);
Jens Axboec524ef72011-10-07 12:23:34 +0200574 free(buf);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200575 return 1;
576 }
577
Jens Axboe46bcd492012-03-14 11:31:21 +0100578 pdu->buf_len = __cpu_to_le32(sb.st_size);
579 pdu->client_type = cpu_to_le32(client->type);
580
Jens Axboec2cb6862012-02-23 20:56:12 +0100581 client->sent_job = 1;
Jens Axboe46bcd492012-03-14 11:31:21 +0100582 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, pdu, p_size, 0);
583 free(pdu);
Jens Axboeb94cba42011-10-06 21:27:10 +0200584 close(fd);
Jens Axboe132159a2011-09-30 15:01:32 -0600585 return ret;
586}
Jens Axboe37db14f2011-09-30 17:00:42 -0600587
Jens Axboe9988ca72012-03-09 15:14:06 +0100588int fio_client_send_ini(struct fio_client *client, const char *filename)
589{
Jens Axboe25095e12012-03-09 17:09:55 +0100590 int ret;
591
592 ret = __fio_client_send_ini(client, filename);
Jens Axboe3af45202012-03-13 09:59:53 +0100593 if (!ret)
594 client->sent_job = 1;
Jens Axboe9988ca72012-03-09 15:14:06 +0100595
Jens Axboe25095e12012-03-09 17:09:55 +0100596 return ret;
Jens Axboe9988ca72012-03-09 15:14:06 +0100597}
598
Jens Axboea37f69b2011-10-01 12:24:21 -0600599int fio_clients_send_ini(const char *filename)
600{
601 struct fio_client *client;
602 struct flist_head *entry, *tmp;
603
604 flist_for_each_safe(entry, tmp, &client_list) {
605 client = flist_entry(entry, struct fio_client, list);
606
Jens Axboe3af45202012-03-13 09:59:53 +0100607 if (fio_client_send_ini(client, filename))
608 remove_client(client);
Jens Axboea37f69b2011-10-01 12:24:21 -0600609 }
610
611 return !nr_clients;
612}
613
Jens Axboea64e88d2011-10-03 14:20:01 +0200614static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
615{
616 dst->max_val = le64_to_cpu(src->max_val);
617 dst->min_val = le64_to_cpu(src->min_val);
618 dst->samples = le64_to_cpu(src->samples);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200619
620 /*
621 * Floats arrive as IEEE 754 encoded uint64_t, convert back to double
622 */
623 dst->mean.u.f = fio_uint64_to_double(le64_to_cpu(dst->mean.u.i));
624 dst->S.u.f = fio_uint64_to_double(le64_to_cpu(dst->S.u.i));
Jens Axboea64e88d2011-10-03 14:20:01 +0200625}
626
627static void convert_ts(struct thread_stat *dst, struct thread_stat *src)
628{
629 int i, j;
630
631 dst->error = le32_to_cpu(src->error);
632 dst->groupid = le32_to_cpu(src->groupid);
633 dst->pid = le32_to_cpu(src->pid);
634 dst->members = le32_to_cpu(src->members);
635
636 for (i = 0; i < 2; i++) {
637 convert_io_stat(&dst->clat_stat[i], &src->clat_stat[i]);
638 convert_io_stat(&dst->slat_stat[i], &src->slat_stat[i]);
639 convert_io_stat(&dst->lat_stat[i], &src->lat_stat[i]);
640 convert_io_stat(&dst->bw_stat[i], &src->bw_stat[i]);
641 }
642
643 dst->usr_time = le64_to_cpu(src->usr_time);
644 dst->sys_time = le64_to_cpu(src->sys_time);
645 dst->ctx = le64_to_cpu(src->ctx);
646 dst->minf = le64_to_cpu(src->minf);
647 dst->majf = le64_to_cpu(src->majf);
648 dst->clat_percentiles = le64_to_cpu(src->clat_percentiles);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200649
650 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
651 fio_fp64_t *fps = &src->percentile_list[i];
652 fio_fp64_t *fpd = &dst->percentile_list[i];
653
654 fpd->u.f = fio_uint64_to_double(le64_to_cpu(fps->u.i));
655 }
Jens Axboea64e88d2011-10-03 14:20:01 +0200656
657 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
658 dst->io_u_map[i] = le32_to_cpu(src->io_u_map[i]);
659 dst->io_u_submit[i] = le32_to_cpu(src->io_u_submit[i]);
660 dst->io_u_complete[i] = le32_to_cpu(src->io_u_complete[i]);
661 }
662
663 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
664 dst->io_u_lat_u[i] = le32_to_cpu(src->io_u_lat_u[i]);
665 dst->io_u_lat_m[i] = le32_to_cpu(src->io_u_lat_m[i]);
666 }
667
668 for (i = 0; i < 2; i++)
669 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
670 dst->io_u_plat[i][j] = le32_to_cpu(src->io_u_plat[i][j]);
671
672 for (i = 0; i < 3; i++) {
673 dst->total_io_u[i] = le64_to_cpu(src->total_io_u[i]);
Jens Axboe93eee042011-10-03 21:08:48 +0200674 dst->short_io_u[i] = le64_to_cpu(src->short_io_u[i]);
Jens Axboea64e88d2011-10-03 14:20:01 +0200675 }
676
677 dst->total_submit = le64_to_cpu(src->total_submit);
678 dst->total_complete = le64_to_cpu(src->total_complete);
679
680 for (i = 0; i < 2; i++) {
681 dst->io_bytes[i] = le64_to_cpu(src->io_bytes[i]);
682 dst->runtime[i] = le64_to_cpu(src->runtime[i]);
683 }
684
685 dst->total_run_time = le64_to_cpu(src->total_run_time);
686 dst->continue_on_error = le16_to_cpu(src->continue_on_error);
687 dst->total_err_count = le64_to_cpu(src->total_err_count);
Jens Axboeddcc0b62011-10-03 14:45:27 +0200688 dst->first_error = le32_to_cpu(src->first_error);
689 dst->kb_base = le32_to_cpu(src->kb_base);
Jens Axboea64e88d2011-10-03 14:20:01 +0200690}
691
692static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
693{
694 int i;
695
696 for (i = 0; i < 2; i++) {
697 dst->max_run[i] = le64_to_cpu(src->max_run[i]);
698 dst->min_run[i] = le64_to_cpu(src->min_run[i]);
699 dst->max_bw[i] = le64_to_cpu(src->max_bw[i]);
700 dst->min_bw[i] = le64_to_cpu(src->min_bw[i]);
701 dst->io_kb[i] = le64_to_cpu(src->io_kb[i]);
702 dst->agg[i] = le64_to_cpu(src->agg[i]);
703 }
704
705 dst->kb_base = le32_to_cpu(src->kb_base);
706 dst->groupid = le32_to_cpu(src->groupid);
707}
708
Jens Axboe89e5fad2012-03-05 09:21:12 +0100709static void handle_ts(struct fio_client *client, struct fio_net_cmd *cmd)
Jens Axboea64e88d2011-10-03 14:20:01 +0200710{
711 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
712
Jens Axboea64e88d2011-10-03 14:20:01 +0200713 show_thread_status(&p->ts, &p->rs);
Jens Axboe37f0c1a2011-10-11 14:08:33 +0200714
715 if (sum_stat_clients == 1)
716 return;
717
718 sum_thread_stats(&client_ts, &p->ts, sum_stat_nr);
719 sum_group_stats(&client_gs, &p->rs);
720
721 client_ts.members++;
722 client_ts.groupid = p->ts.groupid;
723
724 if (++sum_stat_nr == sum_stat_clients) {
725 strcpy(client_ts.name, "All clients");
726 show_thread_status(&client_ts, &client_gs);
727 }
Jens Axboea64e88d2011-10-03 14:20:01 +0200728}
729
Jens Axboe89e5fad2012-03-05 09:21:12 +0100730static void handle_gs(struct fio_client *client, struct fio_net_cmd *cmd)
Jens Axboea64e88d2011-10-03 14:20:01 +0200731{
732 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
733
Jens Axboea64e88d2011-10-03 14:20:01 +0200734 show_group_stats(gs);
735}
736
Jens Axboe3bf236c2012-03-03 20:35:50 +0100737static void handle_text(struct fio_client *client, struct fio_net_cmd *cmd)
738{
739 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
740 const char *buf = (const char *) pdu->buf;
741 const char *name;
742 int fio_unused ret;
743
744 name = client->name ? client->name : client->hostname;
745
746 if (!client->skip_newline)
747 fprintf(f_out, "<%s> ", name);
748 ret = fwrite(buf, pdu->buf_len, 1, f_out);
749 fflush(f_out);
750 client->skip_newline = strchr(buf, '\n') == NULL;
751}
752
Jens Axboed09a64a2011-10-13 11:38:56 +0200753static void convert_agg(struct disk_util_agg *agg)
754{
755 int i;
756
757 for (i = 0; i < 2; i++) {
758 agg->ios[i] = le32_to_cpu(agg->ios[i]);
759 agg->merges[i] = le32_to_cpu(agg->merges[i]);
760 agg->sectors[i] = le64_to_cpu(agg->sectors[i]);
761 agg->ticks[i] = le32_to_cpu(agg->ticks[i]);
762 }
763
764 agg->io_ticks = le32_to_cpu(agg->io_ticks);
765 agg->time_in_queue = le32_to_cpu(agg->time_in_queue);
766 agg->slavecount = le32_to_cpu(agg->slavecount);
Anton Blanchard823ba542011-11-07 14:16:26 +0100767 agg->max_util.u.f = fio_uint64_to_double(__le64_to_cpu(agg->max_util.u.i));
Jens Axboed09a64a2011-10-13 11:38:56 +0200768}
769
770static void convert_dus(struct disk_util_stat *dus)
771{
772 int i;
773
774 for (i = 0; i < 2; i++) {
775 dus->ios[i] = le32_to_cpu(dus->ios[i]);
776 dus->merges[i] = le32_to_cpu(dus->merges[i]);
777 dus->sectors[i] = le64_to_cpu(dus->sectors[i]);
778 dus->ticks[i] = le32_to_cpu(dus->ticks[i]);
779 }
780
781 dus->io_ticks = le32_to_cpu(dus->io_ticks);
782 dus->time_in_queue = le32_to_cpu(dus->time_in_queue);
783 dus->msec = le64_to_cpu(dus->msec);
784}
785
786static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd)
787{
788 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
789
Jens Axboed09a64a2011-10-13 11:38:56 +0200790 if (!client->disk_stats_shown) {
791 client->disk_stats_shown = 1;
792 log_info("\nDisk stats (read/write):\n");
793 }
794
Jens Axboef2f788d2011-10-13 14:03:52 +0200795 print_disk_util(&du->dus, &du->agg, terse_output);
Jens Axboed09a64a2011-10-13 11:38:56 +0200796}
797
Jens Axboe3bf236c2012-03-03 20:35:50 +0100798static void convert_jobs_eta(struct jobs_eta *je)
Jens Axboecf451d12011-10-03 16:48:30 +0200799{
Jens Axboecf451d12011-10-03 16:48:30 +0200800 int i;
801
802 je->nr_running = le32_to_cpu(je->nr_running);
803 je->nr_ramp = le32_to_cpu(je->nr_ramp);
804 je->nr_pending = le32_to_cpu(je->nr_pending);
805 je->files_open = le32_to_cpu(je->files_open);
Jens Axboecf451d12011-10-03 16:48:30 +0200806
807 for (i = 0; i < 2; i++) {
Jens Axboe3e47bd22012-02-29 13:45:02 +0100808 je->m_rate[i] = le32_to_cpu(je->m_rate[i]);
809 je->t_rate[i] = le32_to_cpu(je->t_rate[i]);
810 je->m_iops[i] = le32_to_cpu(je->m_iops[i]);
811 je->t_iops[i] = le32_to_cpu(je->t_iops[i]);
Jens Axboecf451d12011-10-03 16:48:30 +0200812 je->rate[i] = le32_to_cpu(je->rate[i]);
813 je->iops[i] = le32_to_cpu(je->iops[i]);
814 }
815
Jens Axboeb51eedb2011-10-09 12:13:39 +0200816 je->elapsed_sec = le64_to_cpu(je->elapsed_sec);
Jens Axboecf451d12011-10-03 16:48:30 +0200817 je->eta_sec = le64_to_cpu(je->eta_sec);
Jens Axboe8c621fb2012-03-08 12:30:48 +0100818 je->nr_threads = le32_to_cpu(je->nr_threads);
Jens Axboe48fbb462011-10-09 12:19:08 +0200819}
Jens Axboecf451d12011-10-03 16:48:30 +0200820
Jens Axboe3e47bd22012-02-29 13:45:02 +0100821void fio_client_sum_jobs_eta(struct jobs_eta *dst, struct jobs_eta *je)
Jens Axboe48fbb462011-10-09 12:19:08 +0200822{
Jens Axboe48fbb462011-10-09 12:19:08 +0200823 int i;
824
825 dst->nr_running += je->nr_running;
826 dst->nr_ramp += je->nr_ramp;
827 dst->nr_pending += je->nr_pending;
828 dst->files_open += je->files_open;
Jens Axboe48fbb462011-10-09 12:19:08 +0200829
830 for (i = 0; i < 2; i++) {
Jens Axboe3e47bd22012-02-29 13:45:02 +0100831 dst->m_rate[i] += je->m_rate[i];
832 dst->t_rate[i] += je->t_rate[i];
833 dst->m_iops[i] += je->m_iops[i];
834 dst->t_iops[i] += je->t_iops[i];
Jens Axboe48fbb462011-10-09 12:19:08 +0200835 dst->rate[i] += je->rate[i];
836 dst->iops[i] += je->iops[i];
837 }
838
839 dst->elapsed_sec += je->elapsed_sec;
840
841 if (je->eta_sec > dst->eta_sec)
842 dst->eta_sec = je->eta_sec;
Jens Axboe8c621fb2012-03-08 12:30:48 +0100843
844 dst->nr_threads += je->nr_threads;
845 /* we need to handle je->run_str too ... */
Jens Axboe48fbb462011-10-09 12:19:08 +0200846}
847
Jens Axboea5276612012-03-04 15:15:08 +0100848void fio_client_dec_jobs_eta(struct client_eta *eta, client_eta_op eta_fn)
Jens Axboe82c1ed32011-10-10 08:56:18 +0200849{
850 if (!--eta->pending) {
Jens Axboea5276612012-03-04 15:15:08 +0100851 eta_fn(&eta->eta);
Jens Axboe82c1ed32011-10-10 08:56:18 +0200852 free(eta);
853 }
854}
855
Jens Axboe89c17072011-10-11 10:15:51 +0200856static void remove_reply_cmd(struct fio_client *client, struct fio_net_cmd *cmd)
857{
858 struct fio_net_int_cmd *icmd = NULL;
859 struct flist_head *entry;
860
861 flist_for_each(entry, &client->cmd_list) {
862 icmd = flist_entry(entry, struct fio_net_int_cmd, list);
863
Jens Axboedf380932011-10-11 14:25:08 +0200864 if (cmd->tag == (uintptr_t) icmd)
Jens Axboe89c17072011-10-11 10:15:51 +0200865 break;
866
867 icmd = NULL;
868 }
869
870 if (!icmd) {
871 log_err("fio: client: unable to find matching tag\n");
872 return;
873 }
874
875 flist_del(&icmd->list);
876 cmd->tag = icmd->saved_tag;
877 free(icmd);
878}
879
Jens Axboe82c1ed32011-10-10 08:56:18 +0200880static void handle_eta(struct fio_client *client, struct fio_net_cmd *cmd)
Jens Axboe48fbb462011-10-09 12:19:08 +0200881{
882 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
Jens Axboedf380932011-10-11 14:25:08 +0200883 struct client_eta *eta = (struct client_eta *) (uintptr_t) cmd->tag;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200884
885 dprint(FD_NET, "client: got eta tag %p, %d\n", eta, eta->pending);
Jens Axboe48fbb462011-10-09 12:19:08 +0200886
Jens Axboef77d2672011-10-10 14:36:07 +0200887 assert(client->eta_in_flight == eta);
888
889 client->eta_in_flight = NULL;
Jens Axboe82c1ed32011-10-10 08:56:18 +0200890 flist_del_init(&client->eta_list);
891
Jens Axboe2f99deb2012-03-09 14:37:29 +0100892 if (client->ops->jobs_eta)
893 client->ops->jobs_eta(client, je);
894
Jens Axboe3e47bd22012-02-29 13:45:02 +0100895 fio_client_sum_jobs_eta(&eta->eta, je);
Jens Axboea5276612012-03-04 15:15:08 +0100896 fio_client_dec_jobs_eta(eta, client->ops->eta);
Jens Axboecf451d12011-10-03 16:48:30 +0200897}
898
Jens Axboeb5296dd2011-10-07 16:35:56 +0200899static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd)
Jens Axboe2e03b4b2011-10-04 09:00:40 +0200900{
901 struct cmd_probe_pdu *probe = (struct cmd_probe_pdu *) cmd->payload;
Jens Axboed2333352011-10-11 15:07:23 +0200902 const char *os, *arch;
903 char bit[16];
Jens Axboe2e03b4b2011-10-04 09:00:40 +0200904
Jens Axboecca84642011-10-07 12:47:57 +0200905 os = fio_get_os_string(probe->os);
906 if (!os)
907 os = "unknown";
908
909 arch = fio_get_arch_string(probe->arch);
910 if (!arch)
911 os = "unknown";
912
Jens Axboed2333352011-10-11 15:07:23 +0200913 sprintf(bit, "%d-bit", probe->bpp * 8);
Jens Axboe38fdef22011-10-11 14:30:06 +0200914
915 log_info("hostname=%s, be=%u, %s, os=%s, arch=%s, fio=%u.%u.%u\n",
916 probe->hostname, probe->bigendian, bit, os, arch,
917 probe->fio_major, probe->fio_minor, probe->fio_patch);
Jens Axboeb5296dd2011-10-07 16:35:56 +0200918
919 if (!client->name)
920 client->name = strdup((char *) probe->hostname);
Jens Axboe2e03b4b2011-10-04 09:00:40 +0200921}
922
Jens Axboe11e950b2011-10-16 21:34:14 +0200923static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd)
924{
925 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
926
927 client->state = Client_started;
Jens Axboe85dd01e2012-03-12 14:33:16 +0100928 client->jobs = pdu->jobs;
Jens Axboe11e950b2011-10-16 21:34:14 +0200929}
930
931static void handle_stop(struct fio_client *client, struct fio_net_cmd *cmd)
932{
Jens Axboe498c92c2011-10-17 09:14:42 +0200933 if (client->error)
934 log_info("client <%s>: exited with error %d\n", client->hostname, client->error);
Jens Axboe11e950b2011-10-16 21:34:14 +0200935}
936
Jens Axboe6b79c802012-03-08 10:51:36 +0100937static void convert_stop(struct fio_net_cmd *cmd)
938{
939 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
940
941 pdu->error = le32_to_cpu(pdu->error);
942}
943
Jens Axboe084d1c62012-03-03 20:28:07 +0100944static void convert_text(struct fio_net_cmd *cmd)
945{
946 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
947
948 pdu->level = le32_to_cpu(pdu->level);
949 pdu->buf_len = le32_to_cpu(pdu->buf_len);
950 pdu->log_sec = le64_to_cpu(pdu->log_sec);
951 pdu->log_usec = le64_to_cpu(pdu->log_usec);
952}
953
Jens Axboe1b427252012-03-14 15:03:03 +0100954/*
955 * This has been compressed on the server side, since it can be big.
956 * Uncompress here.
957 */
958static struct cmd_iolog_pdu *convert_iolog(struct fio_net_cmd *cmd)
959{
960 struct cmd_iolog_pdu *pdu = (struct cmd_iolog_pdu *) cmd->payload;
961 struct cmd_iolog_pdu *ret;
962 uint32_t nr_samples;
963 unsigned long total;
964 z_stream stream;
965 void *p;
Jens Axboef5ed7652012-03-14 16:28:07 +0100966 int i;
Jens Axboe1b427252012-03-14 15:03:03 +0100967
968 stream.zalloc = Z_NULL;
969 stream.zfree = Z_NULL;
970 stream.opaque = Z_NULL;
971 stream.avail_in = 0;
972 stream.next_in = Z_NULL;
973
974 if (inflateInit(&stream) != Z_OK)
975 return NULL;
976
977 /*
Jens Axboef5ed7652012-03-14 16:28:07 +0100978 * Get header first, it's not compressed
Jens Axboe1b427252012-03-14 15:03:03 +0100979 */
980 nr_samples = le32_to_cpu(pdu->nr_samples);
981
Jens Axboef5ed7652012-03-14 16:28:07 +0100982 total = nr_samples * sizeof(struct io_sample);
983 ret = malloc(total + sizeof(*pdu));
Jens Axboe1b427252012-03-14 15:03:03 +0100984 ret->nr_samples = nr_samples;
Jens Axboef5ed7652012-03-14 16:28:07 +0100985 ret->log_type = le32_to_cpu(pdu->log_type);
986 strcpy((char *) ret->name, (char *) pdu->name);
Jens Axboe1b427252012-03-14 15:03:03 +0100987
Jens Axboef5ed7652012-03-14 16:28:07 +0100988 p = (void *) ret + sizeof(*pdu);
989
990 stream.avail_in = cmd->pdu_len - sizeof(*pdu);
991 stream.next_in = (void *) pdu + sizeof(*pdu);
Jens Axboe1b427252012-03-14 15:03:03 +0100992 while (stream.avail_in) {
993 unsigned int this_chunk = 65536;
994 unsigned int this_len;
995 int err;
996
997 if (this_chunk > total)
998 this_chunk = total;
999
1000 stream.avail_out = this_chunk;
1001 stream.next_out = p;
1002 err = inflate(&stream, Z_NO_FLUSH);
Jens Axboe3c547fe2012-03-14 21:53:00 +01001003 /* may be Z_OK, or Z_STREAM_END */
1004 if (err < 0) {
Jens Axboe1b427252012-03-14 15:03:03 +01001005 log_err("fio: inflate error %d\n", err);
Jens Axboef5ed7652012-03-14 16:28:07 +01001006 free(ret);
1007 ret = NULL;
Jens Axboe1b427252012-03-14 15:03:03 +01001008 goto out;
1009 }
1010
1011 this_len = this_chunk - stream.avail_out;
1012 p += this_len;
1013 total -= this_len;
1014 }
1015
Jens Axboef5ed7652012-03-14 16:28:07 +01001016 for (i = 0; i < ret->nr_samples; i++) {
1017 struct io_sample *s = &ret->samples[i];
1018
1019 s->time = le64_to_cpu(s->time);
1020 s->val = le64_to_cpu(s->val);
1021 s->ddir = le32_to_cpu(s->ddir);
1022 s->bs = le32_to_cpu(s->bs);
1023 }
1024
Jens Axboe1b427252012-03-14 15:03:03 +01001025out:
1026 inflateEnd(&stream);
1027 return ret;
1028}
1029
Jens Axboea5276612012-03-04 15:15:08 +01001030int fio_handle_client(struct fio_client *client)
Jens Axboe37db14f2011-09-30 17:00:42 -06001031{
Jens Axboea5276612012-03-04 15:15:08 +01001032 struct client_ops *ops = client->ops;
Jens Axboe37db14f2011-09-30 17:00:42 -06001033 struct fio_net_cmd *cmd;
1034
Jens Axboe60efd142011-10-04 13:30:11 +02001035 dprint(FD_NET, "client: handle %s\n", client->hostname);
1036
Jens Axboee951bdc2011-10-05 21:58:45 +02001037 cmd = fio_net_recv_cmd(client->fd);
1038 if (!cmd)
1039 return 0;
Jens Axboec2c94582011-10-05 20:31:30 +02001040
Jens Axboeb9d2f302012-03-08 20:36:28 +01001041 dprint(FD_NET, "client: got cmd op %s from %s (pdu=%u)\n",
1042 fio_server_op(cmd->opcode), client->hostname, cmd->pdu_len);
Jens Axboe46c48f12011-10-01 12:50:51 -06001043
Jens Axboee951bdc2011-10-05 21:58:45 +02001044 switch (cmd->opcode) {
1045 case FIO_NET_CMD_QUIT:
Jens Axboe3ec62ec2012-03-01 12:01:29 +01001046 if (ops->quit)
Jens Axboe35c0ba72012-03-14 10:56:40 +01001047 ops->quit(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +02001048 remove_client(client);
1049 free(cmd);
1050 break;
Jens Axboe084d1c62012-03-03 20:28:07 +01001051 case FIO_NET_CMD_TEXT:
1052 convert_text(cmd);
Jens Axboe35c0ba72012-03-14 10:56:40 +01001053 ops->text(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +02001054 free(cmd);
1055 break;
Jens Axboe3bf236c2012-03-03 20:35:50 +01001056 case FIO_NET_CMD_DU: {
1057 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
1058
1059 convert_dus(&du->dus);
1060 convert_agg(&du->agg);
1061
Stephen M. Camerondd366722012-02-24 08:17:30 +01001062 ops->disk_util(client, cmd);
Jens Axboed09a64a2011-10-13 11:38:56 +02001063 free(cmd);
1064 break;
Jens Axboe3bf236c2012-03-03 20:35:50 +01001065 }
1066 case FIO_NET_CMD_TS: {
1067 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
1068
1069 convert_ts(&p->ts, &p->ts);
1070 convert_gs(&p->rs, &p->rs);
1071
Jens Axboe89e5fad2012-03-05 09:21:12 +01001072 ops->thread_status(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +02001073 free(cmd);
1074 break;
Jens Axboe3bf236c2012-03-03 20:35:50 +01001075 }
1076 case FIO_NET_CMD_GS: {
1077 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
1078
1079 convert_gs(gs, gs);
1080
Jens Axboe89e5fad2012-03-05 09:21:12 +01001081 ops->group_stats(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +02001082 free(cmd);
1083 break;
Jens Axboe3bf236c2012-03-03 20:35:50 +01001084 }
1085 case FIO_NET_CMD_ETA: {
1086 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
1087
Jens Axboe89c17072011-10-11 10:15:51 +02001088 remove_reply_cmd(client, cmd);
Jens Axboe3bf236c2012-03-03 20:35:50 +01001089 convert_jobs_eta(je);
Jens Axboea5276612012-03-04 15:15:08 +01001090 handle_eta(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +02001091 free(cmd);
1092 break;
Jens Axboe3bf236c2012-03-03 20:35:50 +01001093 }
Jens Axboee951bdc2011-10-05 21:58:45 +02001094 case FIO_NET_CMD_PROBE:
Jens Axboe89c17072011-10-11 10:15:51 +02001095 remove_reply_cmd(client, cmd);
Stephen M. Camerondd366722012-02-24 08:17:30 +01001096 ops->probe(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +02001097 free(cmd);
1098 break;
Jens Axboe5d7793a2012-03-08 19:59:16 +01001099 case FIO_NET_CMD_SERVER_START:
Jens Axboe01be0382011-10-15 14:43:41 +02001100 client->state = Client_running;
Jens Axboe85dd01e2012-03-12 14:33:16 +01001101 if (ops->job_start)
1102 ops->job_start(client, cmd);
Jens Axboe01be0382011-10-15 14:43:41 +02001103 free(cmd);
1104 break;
Jens Axboe85dd01e2012-03-12 14:33:16 +01001105 case FIO_NET_CMD_START: {
1106 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
1107
1108 pdu->jobs = le32_to_cpu(pdu->jobs);
1109 ops->start(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +02001110 free(cmd);
1111 break;
Jens Axboe85dd01e2012-03-12 14:33:16 +01001112 }
Jens Axboe6b79c802012-03-08 10:51:36 +01001113 case FIO_NET_CMD_STOP: {
1114 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
1115
1116 convert_stop(cmd);
1117 client->state = Client_stopped;
1118 client->error = pdu->error;
1119 ops->stop(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +02001120 free(cmd);
1121 break;
Jens Axboe6b79c802012-03-08 10:51:36 +01001122 }
Jens Axboe807f9972012-03-02 10:25:24 +01001123 case FIO_NET_CMD_ADD_JOB:
1124 if (ops->add_job)
1125 ops->add_job(client, cmd);
1126 free(cmd);
1127 break;
Jens Axboe1b427252012-03-14 15:03:03 +01001128 case FIO_NET_CMD_IOLOG:
1129 if (ops->iolog) {
1130 struct cmd_iolog_pdu *pdu;
1131
1132 pdu = convert_iolog(cmd);
1133 ops->iolog(client, pdu);
1134 }
1135 free(cmd);
1136 break;
Jens Axboee951bdc2011-10-05 21:58:45 +02001137 default:
Jens Axboe89c17072011-10-11 10:15:51 +02001138 log_err("fio: unknown client op: %s\n", fio_server_op(cmd->opcode));
Jens Axboee951bdc2011-10-05 21:58:45 +02001139 free(cmd);
1140 break;
Jens Axboe37db14f2011-09-30 17:00:42 -06001141 }
1142
Jens Axboee951bdc2011-10-05 21:58:45 +02001143 return 1;
Jens Axboe37db14f2011-09-30 17:00:42 -06001144}
Jens Axboeb66570d2011-10-01 11:11:35 -06001145
Jens Axboea5276612012-03-04 15:15:08 +01001146static void request_client_etas(struct client_ops *ops)
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001147{
1148 struct fio_client *client;
1149 struct flist_head *entry;
1150 struct client_eta *eta;
Jens Axboe82c1ed32011-10-10 08:56:18 +02001151 int skipped = 0;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001152
1153 dprint(FD_NET, "client: request eta (%d)\n", nr_clients);
1154
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001155 eta = malloc(sizeof(*eta));
1156 memset(&eta->eta, 0, sizeof(eta->eta));
1157 eta->pending = nr_clients;
1158
1159 flist_for_each(entry, &client_list) {
1160 client = flist_entry(entry, struct fio_client, list);
1161
Jens Axboe82c1ed32011-10-10 08:56:18 +02001162 if (!flist_empty(&client->eta_list)) {
1163 skipped++;
1164 continue;
1165 }
Jens Axboe01be0382011-10-15 14:43:41 +02001166 if (client->state != Client_running)
1167 continue;
Jens Axboe82c1ed32011-10-10 08:56:18 +02001168
Jens Axboef77d2672011-10-10 14:36:07 +02001169 assert(!client->eta_in_flight);
Jens Axboe82c1ed32011-10-10 08:56:18 +02001170 flist_add_tail(&client->eta_list, &eta_list);
Jens Axboef77d2672011-10-10 14:36:07 +02001171 client->eta_in_flight = eta;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001172 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_SEND_ETA,
Jens Axboedf380932011-10-11 14:25:08 +02001173 (uintptr_t) eta, &client->cmd_list);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001174 }
1175
Jens Axboe82c1ed32011-10-10 08:56:18 +02001176 while (skipped--)
Jens Axboea5276612012-03-04 15:15:08 +01001177 fio_client_dec_jobs_eta(eta, ops->eta);
Jens Axboe82c1ed32011-10-10 08:56:18 +02001178
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001179 dprint(FD_NET, "client: requested eta tag %p\n", eta);
1180}
1181
Jens Axboe89c17072011-10-11 10:15:51 +02001182static int client_check_cmd_timeout(struct fio_client *client,
1183 struct timeval *now)
1184{
1185 struct fio_net_int_cmd *cmd;
1186 struct flist_head *entry, *tmp;
1187 int ret = 0;
1188
1189 flist_for_each_safe(entry, tmp, &client->cmd_list) {
1190 cmd = flist_entry(entry, struct fio_net_int_cmd, list);
1191
1192 if (mtime_since(&cmd->tv, now) < FIO_NET_CLIENT_TIMEOUT)
1193 continue;
1194
1195 log_err("fio: client %s, timeout on cmd %s\n", client->hostname,
1196 fio_server_op(cmd->cmd.opcode));
1197 flist_del(&cmd->list);
1198 free(cmd);
1199 ret = 1;
1200 }
1201
1202 return flist_empty(&client->cmd_list) && ret;
1203}
1204
Jens Axboea5276612012-03-04 15:15:08 +01001205static int fio_check_clients_timed_out(void)
Jens Axboe89c17072011-10-11 10:15:51 +02001206{
1207 struct fio_client *client;
1208 struct flist_head *entry, *tmp;
1209 struct timeval tv;
1210 int ret = 0;
1211
1212 gettimeofday(&tv, NULL);
1213
1214 flist_for_each_safe(entry, tmp, &client_list) {
1215 client = flist_entry(entry, struct fio_client, list);
1216
1217 if (flist_empty(&client->cmd_list))
1218 continue;
1219
1220 if (!client_check_cmd_timeout(client, &tv))
1221 continue;
1222
Jens Axboea5276612012-03-04 15:15:08 +01001223 if (client->ops->timed_out)
1224 client->ops->timed_out(client);
Jens Axboeed727a42012-03-02 12:14:40 +01001225 else
1226 log_err("fio: client %s timed out\n", client->hostname);
1227
Jens Axboe89c17072011-10-11 10:15:51 +02001228 remove_client(client);
1229 ret = 1;
1230 }
1231
1232 return ret;
1233}
1234
Stephen M. Camerondd366722012-02-24 08:17:30 +01001235int fio_handle_clients(struct client_ops *ops)
Jens Axboeb66570d2011-10-01 11:11:35 -06001236{
Jens Axboeb66570d2011-10-01 11:11:35 -06001237 struct pollfd *pfds;
Jens Axboe498c92c2011-10-17 09:14:42 +02001238 int i, ret = 0, retval = 0;
Jens Axboeb66570d2011-10-01 11:11:35 -06001239
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001240 gettimeofday(&eta_tv, NULL);
1241
Jens Axboeb66570d2011-10-01 11:11:35 -06001242 pfds = malloc(nr_clients * sizeof(struct pollfd));
1243
Jens Axboe37f0c1a2011-10-11 14:08:33 +02001244 sum_stat_clients = nr_clients;
1245 init_thread_stat(&client_ts);
1246 init_group_run_stat(&client_gs);
1247
Jens Axboeb66570d2011-10-01 11:11:35 -06001248 while (!exit_backend && nr_clients) {
Jens Axboec2cb6862012-02-23 20:56:12 +01001249 struct flist_head *entry, *tmp;
1250 struct fio_client *client;
1251
Jens Axboe82a4be12011-10-01 12:40:32 -06001252 i = 0;
Jens Axboec2cb6862012-02-23 20:56:12 +01001253 flist_for_each_safe(entry, tmp, &client_list) {
Jens Axboe82a4be12011-10-01 12:40:32 -06001254 client = flist_entry(entry, struct fio_client, list);
1255
Jens Axboea5276612012-03-04 15:15:08 +01001256 if (!client->sent_job && !client->ops->stay_connected &&
Jens Axboec2cb6862012-02-23 20:56:12 +01001257 flist_empty(&client->cmd_list)) {
1258 remove_client(client);
1259 continue;
1260 }
1261
Jens Axboe82a4be12011-10-01 12:40:32 -06001262 pfds[i].fd = client->fd;
1263 pfds[i].events = POLLIN;
1264 i++;
1265 }
1266
Jens Axboec2cb6862012-02-23 20:56:12 +01001267 if (!nr_clients)
1268 break;
1269
Jens Axboe82a4be12011-10-01 12:40:32 -06001270 assert(i == nr_clients);
1271
Jens Axboe5c2857f2011-10-04 13:14:32 +02001272 do {
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001273 struct timeval tv;
1274
1275 gettimeofday(&tv, NULL);
Jens Axboe6433ee02012-03-09 20:10:51 +01001276 if (mtime_since(&eta_tv, &tv) >= ops->eta_msec) {
Jens Axboea5276612012-03-04 15:15:08 +01001277 request_client_etas(ops);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001278 memcpy(&eta_tv, &tv, sizeof(tv));
Jens Axboe89c17072011-10-11 10:15:51 +02001279
Jens Axboea5276612012-03-04 15:15:08 +01001280 if (fio_check_clients_timed_out())
Jens Axboe89c17072011-10-11 10:15:51 +02001281 break;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001282 }
1283
Jens Axboe5c2857f2011-10-04 13:14:32 +02001284 ret = poll(pfds, nr_clients, 100);
1285 if (ret < 0) {
1286 if (errno == EINTR)
1287 continue;
1288 log_err("fio: poll clients: %s\n", strerror(errno));
1289 break;
1290 } else if (!ret)
Jens Axboeb66570d2011-10-01 11:11:35 -06001291 continue;
Jens Axboe5c2857f2011-10-04 13:14:32 +02001292 } while (ret <= 0);
Jens Axboeb66570d2011-10-01 11:11:35 -06001293
1294 for (i = 0; i < nr_clients; i++) {
1295 if (!(pfds[i].revents & POLLIN))
1296 continue;
1297
1298 client = find_client_by_fd(pfds[i].fd);
1299 if (!client) {
Jens Axboe3c5f57e2011-10-06 12:37:50 +02001300 log_err("fio: unknown client fd %d\n", pfds[i].fd);
Jens Axboeb66570d2011-10-01 11:11:35 -06001301 continue;
1302 }
Jens Axboea5276612012-03-04 15:15:08 +01001303 if (!fio_handle_client(client)) {
Jens Axboe28d3ab02011-10-05 20:45:37 +02001304 log_info("client: host=%s disconnected\n",
1305 client->hostname);
1306 remove_client(client);
Jens Axboe498c92c2011-10-17 09:14:42 +02001307 retval = 1;
Jens Axboe38990762011-10-17 13:31:33 +02001308 } else if (client->error)
Jens Axboe498c92c2011-10-17 09:14:42 +02001309 retval = 1;
Jens Axboe343cb4a2012-03-09 17:16:51 +01001310 fio_put_client(client);
Jens Axboeb66570d2011-10-01 11:11:35 -06001311 }
1312 }
1313
1314 free(pfds);
Jens Axboe498c92c2011-10-17 09:14:42 +02001315 return retval;
Jens Axboeb66570d2011-10-01 11:11:35 -06001316}