blob: 40372699b4fbc76a20186abc72e868d77211dcd2 [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
Jens Axboe1e0c1842012-03-20 15:15:00 +0100134 close(client->fd);
135 client->fd = -1;
136
Jens Axboe0cf3ece2012-03-21 10:15:20 +0100137 if (client->ops->removed)
138 client->ops->removed(client);
139
Jens Axboe3af45202012-03-13 09:59:53 +0100140 nr_clients--;
141 sum_stat_clients--;
142
143 fio_put_client(client);
Jens Axboe5121a9a2012-03-05 13:32:47 +0100144}
145
Jens Axboe343cb4a2012-03-09 17:16:51 +0100146struct fio_client *fio_get_client(struct fio_client *client)
147{
148 client->refs++;
149 return client;
150}
151
Jens Axboefa2ea802011-10-08 21:07:29 +0200152static void __fio_client_add_cmd_option(struct fio_client *client,
153 const char *opt)
Jens Axboe81179ee2011-10-04 12:42:06 +0200154{
Jens Axboe39e8e012011-10-04 13:46:08 +0200155 int index;
156
157 index = client->argc++;
Jens Axboe81179ee2011-10-04 12:42:06 +0200158 client->argv = realloc(client->argv, sizeof(char *) * client->argc);
Jens Axboe39e8e012011-10-04 13:46:08 +0200159 client->argv[index] = strdup(opt);
160 dprint(FD_NET, "client: add cmd %d: %s\n", index, opt);
Jens Axboe81179ee2011-10-04 12:42:06 +0200161}
162
Jens Axboefa2ea802011-10-08 21:07:29 +0200163void fio_client_add_cmd_option(void *cookie, const char *opt)
Jens Axboe81179ee2011-10-04 12:42:06 +0200164{
Jens Axboebebe6392011-10-07 10:00:51 +0200165 struct fio_client *client = cookie;
Jens Axboe3f3a4542011-10-10 21:11:09 +0200166 struct flist_head *entry;
Jens Axboe81179ee2011-10-04 12:42:06 +0200167
Jens Axboebebe6392011-10-07 10:00:51 +0200168 if (!client || !opt)
Jens Axboefa2ea802011-10-08 21:07:29 +0200169 return;
Jens Axboe81179ee2011-10-04 12:42:06 +0200170
Jens Axboefa2ea802011-10-08 21:07:29 +0200171 __fio_client_add_cmd_option(client, opt);
Jens Axboe3f3a4542011-10-10 21:11:09 +0200172
173 /*
174 * Duplicate arguments to shared client group
175 */
176 flist_for_each(entry, &arg_list) {
177 client = flist_entry(entry, struct fio_client, arg_list);
178
179 __fio_client_add_cmd_option(client, opt);
180 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200181}
182
Jens Axboea5276612012-03-04 15:15:08 +0100183struct fio_client *fio_client_add_explicit(struct client_ops *ops,
184 const char *hostname, int type,
Jens Axboe3ec62ec2012-03-01 12:01:29 +0100185 int port)
186{
187 struct fio_client *client;
188
189 client = malloc(sizeof(*client));
190 memset(client, 0, sizeof(*client));
191
192 INIT_FLIST_HEAD(&client->list);
193 INIT_FLIST_HEAD(&client->hash_list);
194 INIT_FLIST_HEAD(&client->arg_list);
195 INIT_FLIST_HEAD(&client->eta_list);
196 INIT_FLIST_HEAD(&client->cmd_list);
197
198 client->hostname = strdup(hostname);
199
200 if (type == Fio_client_socket)
201 client->is_sock = 1;
202 else {
203 int ipv6;
204
205 ipv6 = type == Fio_client_ipv6;
206 if (fio_server_parse_host(hostname, &ipv6,
207 &client->addr.sin_addr,
208 &client->addr6.sin6_addr))
209 goto err;
210
211 client->port = port;
212 }
213
214 client->fd = -1;
Jens Axboea5276612012-03-04 15:15:08 +0100215 client->ops = ops;
Jens Axboe5121a9a2012-03-05 13:32:47 +0100216 client->refs = 1;
Jens Axboe46bcd492012-03-14 11:31:21 +0100217 client->type = ops->client_type;
Jens Axboe3ec62ec2012-03-01 12:01:29 +0100218
219 __fio_client_add_cmd_option(client, "fio");
220
221 flist_add(&client->list, &client_list);
222 nr_clients++;
223 dprint(FD_NET, "client: added <%s>\n", client->hostname);
224 return client;
225err:
226 free(client);
227 return NULL;
228}
229
Jens Axboea5276612012-03-04 15:15:08 +0100230int fio_client_add(struct client_ops *ops, const char *hostname, void **cookie)
Jens Axboe132159a2011-09-30 15:01:32 -0600231{
Jens Axboe3f3a4542011-10-10 21:11:09 +0200232 struct fio_client *existing = *cookie;
Jens Axboeb66570d2011-10-01 11:11:35 -0600233 struct fio_client *client;
Jens Axboe132159a2011-09-30 15:01:32 -0600234
Jens Axboe3f3a4542011-10-10 21:11:09 +0200235 if (existing) {
236 /*
237 * We always add our "exec" name as the option, hence 1
238 * means empty.
239 */
240 if (existing->argc == 1)
241 flist_add_tail(&existing->arg_list, &arg_list);
242 else {
243 while (!flist_empty(&arg_list))
244 flist_del_init(arg_list.next);
245 }
246 }
247
Jens Axboeb66570d2011-10-01 11:11:35 -0600248 client = malloc(sizeof(*client));
Jens Axboea37f69b2011-10-01 12:24:21 -0600249 memset(client, 0, sizeof(*client));
Jens Axboe81179ee2011-10-04 12:42:06 +0200250
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200251 INIT_FLIST_HEAD(&client->list);
Jens Axboebebe6392011-10-07 10:00:51 +0200252 INIT_FLIST_HEAD(&client->hash_list);
Jens Axboe3f3a4542011-10-10 21:11:09 +0200253 INIT_FLIST_HEAD(&client->arg_list);
Jens Axboe82c1ed32011-10-10 08:56:18 +0200254 INIT_FLIST_HEAD(&client->eta_list);
Jens Axboe89c17072011-10-11 10:15:51 +0200255 INIT_FLIST_HEAD(&client->cmd_list);
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200256
Jens Axboebebe6392011-10-07 10:00:51 +0200257 if (fio_server_parse_string(hostname, &client->hostname,
258 &client->is_sock, &client->port,
Jens Axboe811826b2011-10-24 09:11:50 +0200259 &client->addr.sin_addr,
260 &client->addr6.sin6_addr,
261 &client->ipv6))
Jens Axboebebe6392011-10-07 10:00:51 +0200262 return -1;
263
Jens Axboea37f69b2011-10-01 12:24:21 -0600264 client->fd = -1;
Jens Axboea5276612012-03-04 15:15:08 +0100265 client->ops = ops;
Jens Axboe5121a9a2012-03-05 13:32:47 +0100266 client->refs = 1;
Jens Axboe46bcd492012-03-14 11:31:21 +0100267 client->type = ops->client_type;
Jens Axboe81179ee2011-10-04 12:42:06 +0200268
269 __fio_client_add_cmd_option(client, "fio");
270
Jens Axboea37f69b2011-10-01 12:24:21 -0600271 flist_add(&client->list, &client_list);
272 nr_clients++;
Jens Axboebebe6392011-10-07 10:00:51 +0200273 dprint(FD_NET, "client: added <%s>\n", client->hostname);
274 *cookie = client;
275 return 0;
Jens Axboea37f69b2011-10-01 12:24:21 -0600276}
277
Jens Axboed824c3b2012-03-09 17:45:43 +0100278static void probe_client(struct fio_client *client)
279{
280 dprint(FD_NET, "client: send probe\n");
281
282 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_PROBE, 0, &client->cmd_list);
283}
284
Jens Axboe87aa8f12011-10-06 21:24:13 +0200285static int fio_client_connect_ip(struct fio_client *client)
Jens Axboea37f69b2011-10-01 12:24:21 -0600286{
Jens Axboe811826b2011-10-24 09:11:50 +0200287 struct sockaddr *addr;
288 fio_socklen_t socklen;
289 int fd, domain;
Jens Axboe132159a2011-09-30 15:01:32 -0600290
Jens Axboe811826b2011-10-24 09:11:50 +0200291 if (client->ipv6) {
292 client->addr6.sin6_family = AF_INET6;
293 client->addr6.sin6_port = htons(client->port);
294 domain = AF_INET6;
295 addr = (struct sockaddr *) &client->addr6;
296 socklen = sizeof(client->addr6);
297 } else {
298 client->addr.sin_family = AF_INET;
299 client->addr.sin_port = htons(client->port);
300 domain = AF_INET;
301 addr = (struct sockaddr *) &client->addr;
302 socklen = sizeof(client->addr);
303 }
Jens Axboe132159a2011-09-30 15:01:32 -0600304
Jens Axboe811826b2011-10-24 09:11:50 +0200305 fd = socket(domain, SOCK_STREAM, 0);
Jens Axboe132159a2011-09-30 15:01:32 -0600306 if (fd < 0) {
Jens Axboe25095e12012-03-09 17:09:55 +0100307 int ret = -errno;
308
Jens Axboe132159a2011-09-30 15:01:32 -0600309 log_err("fio: socket: %s\n", strerror(errno));
Jens Axboe25095e12012-03-09 17:09:55 +0100310 return ret;
Jens Axboe132159a2011-09-30 15:01:32 -0600311 }
312
Jens Axboe811826b2011-10-24 09:11:50 +0200313 if (connect(fd, addr, socklen) < 0) {
Jens Axboe25095e12012-03-09 17:09:55 +0100314 int ret = -errno;
315
Jens Axboe132159a2011-09-30 15:01:32 -0600316 log_err("fio: connect: %s\n", strerror(errno));
Jens Axboea7de0a12011-10-07 12:55:14 +0200317 log_err("fio: failed to connect to %s:%u\n", client->hostname,
318 client->port);
Jens Axboeb94cba42011-10-06 21:27:10 +0200319 close(fd);
Jens Axboe25095e12012-03-09 17:09:55 +0100320 return ret;
Jens Axboe132159a2011-09-30 15:01:32 -0600321 }
322
Jens Axboe87aa8f12011-10-06 21:24:13 +0200323 return fd;
324}
325
326static int fio_client_connect_sock(struct fio_client *client)
327{
328 struct sockaddr_un *addr = &client->addr_un;
329 fio_socklen_t len;
330 int fd;
331
332 memset(addr, 0, sizeof(*addr));
333 addr->sun_family = AF_UNIX;
334 strcpy(addr->sun_path, client->hostname);
335
336 fd = socket(AF_UNIX, SOCK_STREAM, 0);
337 if (fd < 0) {
Jens Axboe25095e12012-03-09 17:09:55 +0100338 int ret = -errno;
339
Jens Axboe87aa8f12011-10-06 21:24:13 +0200340 log_err("fio: socket: %s\n", strerror(errno));
Jens Axboe25095e12012-03-09 17:09:55 +0100341 return ret;
Jens Axboe87aa8f12011-10-06 21:24:13 +0200342 }
343
344 len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
345 if (connect(fd, (struct sockaddr *) addr, len) < 0) {
Jens Axboe25095e12012-03-09 17:09:55 +0100346 int ret = -errno;
347
Jens Axboe87aa8f12011-10-06 21:24:13 +0200348 log_err("fio: connect; %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200349 close(fd);
Jens Axboe25095e12012-03-09 17:09:55 +0100350 return ret;
Jens Axboe87aa8f12011-10-06 21:24:13 +0200351 }
352
353 return fd;
354}
355
Jens Axboe2f99deb2012-03-09 14:37:29 +0100356int fio_client_connect(struct fio_client *client)
Jens Axboe87aa8f12011-10-06 21:24:13 +0200357{
358 int fd;
359
360 dprint(FD_NET, "client: connect to host %s\n", client->hostname);
361
Jens Axboe87aa8f12011-10-06 21:24:13 +0200362 if (client->is_sock)
363 fd = fio_client_connect_sock(client);
364 else
365 fd = fio_client_connect_ip(client);
366
Jens Axboe89c17072011-10-11 10:15:51 +0200367 dprint(FD_NET, "client: %s connected %d\n", client->hostname, fd);
368
Jens Axboe87aa8f12011-10-06 21:24:13 +0200369 if (fd < 0)
Jens Axboea1a3ed52012-03-09 17:00:01 +0100370 return fd;
Jens Axboe87aa8f12011-10-06 21:24:13 +0200371
Jens Axboeb66570d2011-10-01 11:11:35 -0600372 client->fd = fd;
Jens Axboebebe6392011-10-07 10:00:51 +0200373 fio_client_add_hash(client);
Jens Axboe81179ee2011-10-04 12:42:06 +0200374 client->state = Client_connected;
Jens Axboed824c3b2012-03-09 17:45:43 +0100375
376 probe_client(client);
Jens Axboe132159a2011-09-30 15:01:32 -0600377 return 0;
378}
379
Jens Axboe0cf3ece2012-03-21 10:15:20 +0100380int fio_client_terminate(struct fio_client *client)
Jens Axboe2f99deb2012-03-09 14:37:29 +0100381{
Jens Axboe0cf3ece2012-03-21 10:15:20 +0100382 return fio_net_send_quit(client->fd);
Jens Axboe2f99deb2012-03-09 14:37:29 +0100383}
384
Jens Axboecc0df002011-10-03 20:53:32 +0200385void fio_clients_terminate(void)
386{
387 struct flist_head *entry;
388 struct fio_client *client;
389
Jens Axboe60efd142011-10-04 13:30:11 +0200390 dprint(FD_NET, "client: terminate clients\n");
391
Jens Axboecc0df002011-10-03 20:53:32 +0200392 flist_for_each(entry, &client_list) {
393 client = flist_entry(entry, struct fio_client, list);
Jens Axboe2f99deb2012-03-09 14:37:29 +0100394 fio_client_terminate(client);
Jens Axboecc0df002011-10-03 20:53:32 +0200395 }
396}
397
398static void sig_int(int sig)
399{
Jens Axboebebe6392011-10-07 10:00:51 +0200400 dprint(FD_NET, "client: got signal %d\n", sig);
Jens Axboecc0df002011-10-03 20:53:32 +0200401 fio_clients_terminate();
402}
403
404static void client_signal_handler(void)
405{
406 struct sigaction act;
407
408 memset(&act, 0, sizeof(act));
409 act.sa_handler = sig_int;
410 act.sa_flags = SA_RESTART;
411 sigaction(SIGINT, &act, NULL);
412
413 memset(&act, 0, sizeof(act));
414 act.sa_handler = sig_int;
415 act.sa_flags = SA_RESTART;
416 sigaction(SIGTERM, &act, NULL);
417}
418
Jens Axboe81179ee2011-10-04 12:42:06 +0200419static int send_client_cmd_line(struct fio_client *client)
420{
Jens Axboefa2ea802011-10-08 21:07:29 +0200421 struct cmd_single_line_pdu *cslp;
422 struct cmd_line_pdu *clp;
423 unsigned long offset;
Jens Axboe7f868312011-10-10 09:55:21 +0200424 unsigned int *lens;
Jens Axboefa2ea802011-10-08 21:07:29 +0200425 void *pdu;
426 size_t mem;
Jens Axboe81179ee2011-10-04 12:42:06 +0200427 int i, ret;
428
Jens Axboe39e8e012011-10-04 13:46:08 +0200429 dprint(FD_NET, "client: send cmdline %d\n", client->argc);
Jens Axboe60efd142011-10-04 13:30:11 +0200430
Jens Axboe7f868312011-10-10 09:55:21 +0200431 lens = malloc(client->argc * sizeof(unsigned int));
432
Jens Axboefa2ea802011-10-08 21:07:29 +0200433 /*
434 * Find out how much mem we need
435 */
Jens Axboe7f868312011-10-10 09:55:21 +0200436 for (i = 0, mem = 0; i < client->argc; i++) {
437 lens[i] = strlen(client->argv[i]) + 1;
438 mem += lens[i];
439 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200440
Jens Axboefa2ea802011-10-08 21:07:29 +0200441 /*
442 * We need one cmd_line_pdu, and argc number of cmd_single_line_pdu
443 */
444 mem += sizeof(*clp) + (client->argc * sizeof(*cslp));
445
446 pdu = malloc(mem);
447 clp = pdu;
448 offset = sizeof(*clp);
449
450 for (i = 0; i < client->argc; i++) {
Jens Axboe7f868312011-10-10 09:55:21 +0200451 uint16_t arg_len = lens[i];
Jens Axboefa2ea802011-10-08 21:07:29 +0200452
453 cslp = pdu + offset;
454 strcpy((char *) cslp->text, client->argv[i]);
455 cslp->len = cpu_to_le16(arg_len);
456 offset += sizeof(*cslp) + arg_len;
457 }
458
Jens Axboe7f868312011-10-10 09:55:21 +0200459 free(lens);
Jens Axboefa2ea802011-10-08 21:07:29 +0200460 clp->lines = cpu_to_le16(client->argc);
Jens Axboe46bcd492012-03-14 11:31:21 +0100461 clp->client_type = __cpu_to_le16(client->type);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200462 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOBLINE, pdu, mem, 0);
Jens Axboe81179ee2011-10-04 12:42:06 +0200463 free(pdu);
464 return ret;
465}
466
Jens Axboea37f69b2011-10-01 12:24:21 -0600467int fio_clients_connect(void)
468{
469 struct fio_client *client;
470 struct flist_head *entry, *tmp;
471 int ret;
472
Bruce Cran93bcfd22012-02-20 20:18:19 +0100473#ifdef WIN32
474 WSADATA wsd;
475 WSAStartup(MAKEWORD(2,2), &wsd);
476#endif
477
Jens Axboe60efd142011-10-04 13:30:11 +0200478 dprint(FD_NET, "client: connect all\n");
479
Jens Axboecc0df002011-10-03 20:53:32 +0200480 client_signal_handler();
481
Jens Axboea37f69b2011-10-01 12:24:21 -0600482 flist_for_each_safe(entry, tmp, &client_list) {
483 client = flist_entry(entry, struct fio_client, list);
484
485 ret = fio_client_connect(client);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200486 if (ret) {
Jens Axboea37f69b2011-10-01 12:24:21 -0600487 remove_client(client);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200488 continue;
489 }
490
Jens Axboe81179ee2011-10-04 12:42:06 +0200491 if (client->argc > 1)
492 send_client_cmd_line(client);
Jens Axboea37f69b2011-10-01 12:24:21 -0600493 }
494
495 return !nr_clients;
496}
497
Jens Axboeb9d2f302012-03-08 20:36:28 +0100498int fio_start_client(struct fio_client *client)
499{
500 dprint(FD_NET, "client: start %s\n", client->hostname);
501 return fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_RUN, 0, NULL);
502}
503
504int fio_start_all_clients(void)
505{
506 struct fio_client *client;
507 struct flist_head *entry, *tmp;
508 int ret;
509
510 dprint(FD_NET, "client: start all\n");
511
512 flist_for_each_safe(entry, tmp, &client_list) {
513 client = flist_entry(entry, struct fio_client, list);
514
515 ret = fio_start_client(client);
516 if (ret) {
517 remove_client(client);
518 continue;
519 }
520 }
521
522 return flist_empty(&client_list);
523}
524
Jens Axboe132159a2011-09-30 15:01:32 -0600525/*
526 * Send file contents to server backend. We could use sendfile(), but to remain
527 * more portable lets just read/write the darn thing.
528 */
Jens Axboe9988ca72012-03-09 15:14:06 +0100529static int __fio_client_send_ini(struct fio_client *client, const char *filename)
Jens Axboe132159a2011-09-30 15:01:32 -0600530{
Jens Axboe46bcd492012-03-14 11:31:21 +0100531 struct cmd_job_pdu *pdu;
532 size_t p_size;
Jens Axboe132159a2011-09-30 15:01:32 -0600533 struct stat sb;
Jens Axboe46bcd492012-03-14 11:31:21 +0100534 char *p;
535 void *buf;
Jens Axboe132159a2011-09-30 15:01:32 -0600536 off_t len;
537 int fd, ret;
538
Jens Axboe46c48f12011-10-01 12:50:51 -0600539 dprint(FD_NET, "send ini %s to %s\n", filename, client->hostname);
540
Jens Axboe132159a2011-09-30 15:01:32 -0600541 fd = open(filename, O_RDONLY);
542 if (fd < 0) {
Jens Axboe25095e12012-03-09 17:09:55 +0100543 int ret = -errno;
544
Jens Axboee951bdc2011-10-05 21:58:45 +0200545 log_err("fio: job file <%s> open: %s\n", filename, strerror(errno));
Jens Axboe25095e12012-03-09 17:09:55 +0100546 return ret;
Jens Axboe132159a2011-09-30 15:01:32 -0600547 }
548
549 if (fstat(fd, &sb) < 0) {
Jens Axboe25095e12012-03-09 17:09:55 +0100550 int ret = -errno;
551
Jens Axboe132159a2011-09-30 15:01:32 -0600552 log_err("fio: job file stat: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200553 close(fd);
Jens Axboe25095e12012-03-09 17:09:55 +0100554 return ret;
Jens Axboe132159a2011-09-30 15:01:32 -0600555 }
556
Jens Axboe46bcd492012-03-14 11:31:21 +0100557 p_size = sb.st_size + sizeof(*pdu);
558 pdu = malloc(p_size);
559 buf = pdu->buf;
Jens Axboe132159a2011-09-30 15:01:32 -0600560
561 len = sb.st_size;
562 p = buf;
563 do {
564 ret = read(fd, p, len);
565 if (ret > 0) {
566 len -= ret;
567 if (!len)
568 break;
569 p += ret;
570 continue;
571 } else if (!ret)
572 break;
573 else if (errno == EAGAIN || errno == EINTR)
574 continue;
575 } while (1);
576
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200577 if (len) {
578 log_err("fio: failed reading job file %s\n", filename);
Jens Axboeb94cba42011-10-06 21:27:10 +0200579 close(fd);
Jens Axboec524ef72011-10-07 12:23:34 +0200580 free(buf);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200581 return 1;
582 }
583
Jens Axboe46bcd492012-03-14 11:31:21 +0100584 pdu->buf_len = __cpu_to_le32(sb.st_size);
585 pdu->client_type = cpu_to_le32(client->type);
586
Jens Axboec2cb6862012-02-23 20:56:12 +0100587 client->sent_job = 1;
Jens Axboe46bcd492012-03-14 11:31:21 +0100588 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, pdu, p_size, 0);
589 free(pdu);
Jens Axboeb94cba42011-10-06 21:27:10 +0200590 close(fd);
Jens Axboe132159a2011-09-30 15:01:32 -0600591 return ret;
592}
Jens Axboe37db14f2011-09-30 17:00:42 -0600593
Jens Axboe9988ca72012-03-09 15:14:06 +0100594int fio_client_send_ini(struct fio_client *client, const char *filename)
595{
Jens Axboe25095e12012-03-09 17:09:55 +0100596 int ret;
597
598 ret = __fio_client_send_ini(client, filename);
Jens Axboe3af45202012-03-13 09:59:53 +0100599 if (!ret)
600 client->sent_job = 1;
Jens Axboe9988ca72012-03-09 15:14:06 +0100601
Jens Axboe25095e12012-03-09 17:09:55 +0100602 return ret;
Jens Axboe9988ca72012-03-09 15:14:06 +0100603}
604
Jens Axboea37f69b2011-10-01 12:24:21 -0600605int fio_clients_send_ini(const char *filename)
606{
607 struct fio_client *client;
608 struct flist_head *entry, *tmp;
609
610 flist_for_each_safe(entry, tmp, &client_list) {
611 client = flist_entry(entry, struct fio_client, list);
612
Jens Axboe3af45202012-03-13 09:59:53 +0100613 if (fio_client_send_ini(client, filename))
614 remove_client(client);
Jens Axboea37f69b2011-10-01 12:24:21 -0600615 }
616
617 return !nr_clients;
618}
619
Jens Axboea64e88d2011-10-03 14:20:01 +0200620static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
621{
622 dst->max_val = le64_to_cpu(src->max_val);
623 dst->min_val = le64_to_cpu(src->min_val);
624 dst->samples = le64_to_cpu(src->samples);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200625
626 /*
627 * Floats arrive as IEEE 754 encoded uint64_t, convert back to double
628 */
629 dst->mean.u.f = fio_uint64_to_double(le64_to_cpu(dst->mean.u.i));
630 dst->S.u.f = fio_uint64_to_double(le64_to_cpu(dst->S.u.i));
Jens Axboea64e88d2011-10-03 14:20:01 +0200631}
632
633static void convert_ts(struct thread_stat *dst, struct thread_stat *src)
634{
635 int i, j;
636
Jens Axboe2f122b12012-03-15 13:10:19 +0100637 dst->error = le32_to_cpu(src->error);
638 dst->thread_number = le32_to_cpu(src->thread_number);
639 dst->groupid = le32_to_cpu(src->groupid);
640 dst->pid = le32_to_cpu(src->pid);
641 dst->members = le32_to_cpu(src->members);
Jens Axboea64e88d2011-10-03 14:20:01 +0200642
643 for (i = 0; i < 2; i++) {
644 convert_io_stat(&dst->clat_stat[i], &src->clat_stat[i]);
645 convert_io_stat(&dst->slat_stat[i], &src->slat_stat[i]);
646 convert_io_stat(&dst->lat_stat[i], &src->lat_stat[i]);
647 convert_io_stat(&dst->bw_stat[i], &src->bw_stat[i]);
648 }
649
650 dst->usr_time = le64_to_cpu(src->usr_time);
651 dst->sys_time = le64_to_cpu(src->sys_time);
652 dst->ctx = le64_to_cpu(src->ctx);
653 dst->minf = le64_to_cpu(src->minf);
654 dst->majf = le64_to_cpu(src->majf);
655 dst->clat_percentiles = le64_to_cpu(src->clat_percentiles);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200656
657 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
658 fio_fp64_t *fps = &src->percentile_list[i];
659 fio_fp64_t *fpd = &dst->percentile_list[i];
660
661 fpd->u.f = fio_uint64_to_double(le64_to_cpu(fps->u.i));
662 }
Jens Axboea64e88d2011-10-03 14:20:01 +0200663
664 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
665 dst->io_u_map[i] = le32_to_cpu(src->io_u_map[i]);
666 dst->io_u_submit[i] = le32_to_cpu(src->io_u_submit[i]);
667 dst->io_u_complete[i] = le32_to_cpu(src->io_u_complete[i]);
668 }
669
670 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
671 dst->io_u_lat_u[i] = le32_to_cpu(src->io_u_lat_u[i]);
672 dst->io_u_lat_m[i] = le32_to_cpu(src->io_u_lat_m[i]);
673 }
674
675 for (i = 0; i < 2; i++)
676 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
677 dst->io_u_plat[i][j] = le32_to_cpu(src->io_u_plat[i][j]);
678
679 for (i = 0; i < 3; i++) {
680 dst->total_io_u[i] = le64_to_cpu(src->total_io_u[i]);
Jens Axboe93eee042011-10-03 21:08:48 +0200681 dst->short_io_u[i] = le64_to_cpu(src->short_io_u[i]);
Jens Axboea64e88d2011-10-03 14:20:01 +0200682 }
683
684 dst->total_submit = le64_to_cpu(src->total_submit);
685 dst->total_complete = le64_to_cpu(src->total_complete);
686
687 for (i = 0; i < 2; i++) {
688 dst->io_bytes[i] = le64_to_cpu(src->io_bytes[i]);
689 dst->runtime[i] = le64_to_cpu(src->runtime[i]);
690 }
691
692 dst->total_run_time = le64_to_cpu(src->total_run_time);
693 dst->continue_on_error = le16_to_cpu(src->continue_on_error);
694 dst->total_err_count = le64_to_cpu(src->total_err_count);
Jens Axboeddcc0b62011-10-03 14:45:27 +0200695 dst->first_error = le32_to_cpu(src->first_error);
696 dst->kb_base = le32_to_cpu(src->kb_base);
Jens Axboea64e88d2011-10-03 14:20:01 +0200697}
698
699static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
700{
701 int i;
702
703 for (i = 0; i < 2; i++) {
704 dst->max_run[i] = le64_to_cpu(src->max_run[i]);
705 dst->min_run[i] = le64_to_cpu(src->min_run[i]);
706 dst->max_bw[i] = le64_to_cpu(src->max_bw[i]);
707 dst->min_bw[i] = le64_to_cpu(src->min_bw[i]);
708 dst->io_kb[i] = le64_to_cpu(src->io_kb[i]);
709 dst->agg[i] = le64_to_cpu(src->agg[i]);
710 }
711
712 dst->kb_base = le32_to_cpu(src->kb_base);
713 dst->groupid = le32_to_cpu(src->groupid);
714}
715
Jens Axboe89e5fad2012-03-05 09:21:12 +0100716static void handle_ts(struct fio_client *client, struct fio_net_cmd *cmd)
Jens Axboea64e88d2011-10-03 14:20:01 +0200717{
718 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
719
Jens Axboea64e88d2011-10-03 14:20:01 +0200720 show_thread_status(&p->ts, &p->rs);
Jens Axboe37f0c1a2011-10-11 14:08:33 +0200721
722 if (sum_stat_clients == 1)
723 return;
724
725 sum_thread_stats(&client_ts, &p->ts, sum_stat_nr);
726 sum_group_stats(&client_gs, &p->rs);
727
728 client_ts.members++;
Jens Axboe2f122b12012-03-15 13:10:19 +0100729 client_ts.thread_number = p->ts.thread_number;
Jens Axboe37f0c1a2011-10-11 14:08:33 +0200730 client_ts.groupid = p->ts.groupid;
731
732 if (++sum_stat_nr == sum_stat_clients) {
733 strcpy(client_ts.name, "All clients");
734 show_thread_status(&client_ts, &client_gs);
735 }
Jens Axboea64e88d2011-10-03 14:20:01 +0200736}
737
Jens Axboe89e5fad2012-03-05 09:21:12 +0100738static void handle_gs(struct fio_client *client, struct fio_net_cmd *cmd)
Jens Axboea64e88d2011-10-03 14:20:01 +0200739{
740 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
741
Jens Axboea64e88d2011-10-03 14:20:01 +0200742 show_group_stats(gs);
743}
744
Jens Axboe3bf236c2012-03-03 20:35:50 +0100745static void handle_text(struct fio_client *client, struct fio_net_cmd *cmd)
746{
747 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
748 const char *buf = (const char *) pdu->buf;
749 const char *name;
750 int fio_unused ret;
751
752 name = client->name ? client->name : client->hostname;
753
754 if (!client->skip_newline)
755 fprintf(f_out, "<%s> ", name);
756 ret = fwrite(buf, pdu->buf_len, 1, f_out);
757 fflush(f_out);
758 client->skip_newline = strchr(buf, '\n') == NULL;
759}
760
Jens Axboed09a64a2011-10-13 11:38:56 +0200761static void convert_agg(struct disk_util_agg *agg)
762{
763 int i;
764
765 for (i = 0; i < 2; i++) {
766 agg->ios[i] = le32_to_cpu(agg->ios[i]);
767 agg->merges[i] = le32_to_cpu(agg->merges[i]);
768 agg->sectors[i] = le64_to_cpu(agg->sectors[i]);
769 agg->ticks[i] = le32_to_cpu(agg->ticks[i]);
770 }
771
772 agg->io_ticks = le32_to_cpu(agg->io_ticks);
773 agg->time_in_queue = le32_to_cpu(agg->time_in_queue);
774 agg->slavecount = le32_to_cpu(agg->slavecount);
Anton Blanchard823ba542011-11-07 14:16:26 +0100775 agg->max_util.u.f = fio_uint64_to_double(__le64_to_cpu(agg->max_util.u.i));
Jens Axboed09a64a2011-10-13 11:38:56 +0200776}
777
778static void convert_dus(struct disk_util_stat *dus)
779{
780 int i;
781
782 for (i = 0; i < 2; i++) {
783 dus->ios[i] = le32_to_cpu(dus->ios[i]);
784 dus->merges[i] = le32_to_cpu(dus->merges[i]);
785 dus->sectors[i] = le64_to_cpu(dus->sectors[i]);
786 dus->ticks[i] = le32_to_cpu(dus->ticks[i]);
787 }
788
789 dus->io_ticks = le32_to_cpu(dus->io_ticks);
790 dus->time_in_queue = le32_to_cpu(dus->time_in_queue);
791 dus->msec = le64_to_cpu(dus->msec);
792}
793
794static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd)
795{
796 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
797
Jens Axboed09a64a2011-10-13 11:38:56 +0200798 if (!client->disk_stats_shown) {
799 client->disk_stats_shown = 1;
800 log_info("\nDisk stats (read/write):\n");
801 }
802
Jens Axboef2f788d2011-10-13 14:03:52 +0200803 print_disk_util(&du->dus, &du->agg, terse_output);
Jens Axboed09a64a2011-10-13 11:38:56 +0200804}
805
Jens Axboe3bf236c2012-03-03 20:35:50 +0100806static void convert_jobs_eta(struct jobs_eta *je)
Jens Axboecf451d12011-10-03 16:48:30 +0200807{
Jens Axboecf451d12011-10-03 16:48:30 +0200808 int i;
809
810 je->nr_running = le32_to_cpu(je->nr_running);
811 je->nr_ramp = le32_to_cpu(je->nr_ramp);
812 je->nr_pending = le32_to_cpu(je->nr_pending);
813 je->files_open = le32_to_cpu(je->files_open);
Jens Axboecf451d12011-10-03 16:48:30 +0200814
815 for (i = 0; i < 2; i++) {
Jens Axboe3e47bd22012-02-29 13:45:02 +0100816 je->m_rate[i] = le32_to_cpu(je->m_rate[i]);
817 je->t_rate[i] = le32_to_cpu(je->t_rate[i]);
818 je->m_iops[i] = le32_to_cpu(je->m_iops[i]);
819 je->t_iops[i] = le32_to_cpu(je->t_iops[i]);
Jens Axboecf451d12011-10-03 16:48:30 +0200820 je->rate[i] = le32_to_cpu(je->rate[i]);
821 je->iops[i] = le32_to_cpu(je->iops[i]);
822 }
823
Jens Axboeb51eedb2011-10-09 12:13:39 +0200824 je->elapsed_sec = le64_to_cpu(je->elapsed_sec);
Jens Axboecf451d12011-10-03 16:48:30 +0200825 je->eta_sec = le64_to_cpu(je->eta_sec);
Jens Axboe8c621fb2012-03-08 12:30:48 +0100826 je->nr_threads = le32_to_cpu(je->nr_threads);
Jens Axboe48fbb462011-10-09 12:19:08 +0200827}
Jens Axboecf451d12011-10-03 16:48:30 +0200828
Jens Axboe3e47bd22012-02-29 13:45:02 +0100829void fio_client_sum_jobs_eta(struct jobs_eta *dst, struct jobs_eta *je)
Jens Axboe48fbb462011-10-09 12:19:08 +0200830{
Jens Axboe48fbb462011-10-09 12:19:08 +0200831 int i;
832
833 dst->nr_running += je->nr_running;
834 dst->nr_ramp += je->nr_ramp;
835 dst->nr_pending += je->nr_pending;
836 dst->files_open += je->files_open;
Jens Axboe48fbb462011-10-09 12:19:08 +0200837
838 for (i = 0; i < 2; i++) {
Jens Axboe3e47bd22012-02-29 13:45:02 +0100839 dst->m_rate[i] += je->m_rate[i];
840 dst->t_rate[i] += je->t_rate[i];
841 dst->m_iops[i] += je->m_iops[i];
842 dst->t_iops[i] += je->t_iops[i];
Jens Axboe48fbb462011-10-09 12:19:08 +0200843 dst->rate[i] += je->rate[i];
844 dst->iops[i] += je->iops[i];
845 }
846
847 dst->elapsed_sec += je->elapsed_sec;
848
849 if (je->eta_sec > dst->eta_sec)
850 dst->eta_sec = je->eta_sec;
Jens Axboe8c621fb2012-03-08 12:30:48 +0100851
852 dst->nr_threads += je->nr_threads;
853 /* we need to handle je->run_str too ... */
Jens Axboe48fbb462011-10-09 12:19:08 +0200854}
855
Jens Axboea5276612012-03-04 15:15:08 +0100856void fio_client_dec_jobs_eta(struct client_eta *eta, client_eta_op eta_fn)
Jens Axboe82c1ed32011-10-10 08:56:18 +0200857{
858 if (!--eta->pending) {
Jens Axboea5276612012-03-04 15:15:08 +0100859 eta_fn(&eta->eta);
Jens Axboe82c1ed32011-10-10 08:56:18 +0200860 free(eta);
861 }
862}
863
Jens Axboe89c17072011-10-11 10:15:51 +0200864static void remove_reply_cmd(struct fio_client *client, struct fio_net_cmd *cmd)
865{
866 struct fio_net_int_cmd *icmd = NULL;
867 struct flist_head *entry;
868
869 flist_for_each(entry, &client->cmd_list) {
870 icmd = flist_entry(entry, struct fio_net_int_cmd, list);
871
Jens Axboedf380932011-10-11 14:25:08 +0200872 if (cmd->tag == (uintptr_t) icmd)
Jens Axboe89c17072011-10-11 10:15:51 +0200873 break;
874
875 icmd = NULL;
876 }
877
878 if (!icmd) {
879 log_err("fio: client: unable to find matching tag\n");
880 return;
881 }
882
883 flist_del(&icmd->list);
884 cmd->tag = icmd->saved_tag;
885 free(icmd);
886}
887
Jens Axboe82c1ed32011-10-10 08:56:18 +0200888static void handle_eta(struct fio_client *client, struct fio_net_cmd *cmd)
Jens Axboe48fbb462011-10-09 12:19:08 +0200889{
890 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
Jens Axboedf380932011-10-11 14:25:08 +0200891 struct client_eta *eta = (struct client_eta *) (uintptr_t) cmd->tag;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200892
893 dprint(FD_NET, "client: got eta tag %p, %d\n", eta, eta->pending);
Jens Axboe48fbb462011-10-09 12:19:08 +0200894
Jens Axboef77d2672011-10-10 14:36:07 +0200895 assert(client->eta_in_flight == eta);
896
897 client->eta_in_flight = NULL;
Jens Axboe82c1ed32011-10-10 08:56:18 +0200898 flist_del_init(&client->eta_list);
899
Jens Axboe2f99deb2012-03-09 14:37:29 +0100900 if (client->ops->jobs_eta)
901 client->ops->jobs_eta(client, je);
902
Jens Axboe3e47bd22012-02-29 13:45:02 +0100903 fio_client_sum_jobs_eta(&eta->eta, je);
Jens Axboea5276612012-03-04 15:15:08 +0100904 fio_client_dec_jobs_eta(eta, client->ops->eta);
Jens Axboecf451d12011-10-03 16:48:30 +0200905}
906
Jens Axboeb5296dd2011-10-07 16:35:56 +0200907static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd)
Jens Axboe2e03b4b2011-10-04 09:00:40 +0200908{
909 struct cmd_probe_pdu *probe = (struct cmd_probe_pdu *) cmd->payload;
Jens Axboed2333352011-10-11 15:07:23 +0200910 const char *os, *arch;
911 char bit[16];
Jens Axboe2e03b4b2011-10-04 09:00:40 +0200912
Jens Axboecca84642011-10-07 12:47:57 +0200913 os = fio_get_os_string(probe->os);
914 if (!os)
915 os = "unknown";
916
917 arch = fio_get_arch_string(probe->arch);
918 if (!arch)
919 os = "unknown";
920
Jens Axboed2333352011-10-11 15:07:23 +0200921 sprintf(bit, "%d-bit", probe->bpp * 8);
Jens Axboe38fdef22011-10-11 14:30:06 +0200922
923 log_info("hostname=%s, be=%u, %s, os=%s, arch=%s, fio=%u.%u.%u\n",
924 probe->hostname, probe->bigendian, bit, os, arch,
925 probe->fio_major, probe->fio_minor, probe->fio_patch);
Jens Axboeb5296dd2011-10-07 16:35:56 +0200926
927 if (!client->name)
928 client->name = strdup((char *) probe->hostname);
Jens Axboe2e03b4b2011-10-04 09:00:40 +0200929}
930
Jens Axboe11e950b2011-10-16 21:34:14 +0200931static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd)
932{
933 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
934
935 client->state = Client_started;
Jens Axboe85dd01e2012-03-12 14:33:16 +0100936 client->jobs = pdu->jobs;
Jens Axboe11e950b2011-10-16 21:34:14 +0200937}
938
939static void handle_stop(struct fio_client *client, struct fio_net_cmd *cmd)
940{
Jens Axboe498c92c2011-10-17 09:14:42 +0200941 if (client->error)
942 log_info("client <%s>: exited with error %d\n", client->hostname, client->error);
Jens Axboe11e950b2011-10-16 21:34:14 +0200943}
944
Jens Axboe6b79c802012-03-08 10:51:36 +0100945static void convert_stop(struct fio_net_cmd *cmd)
946{
947 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
948
949 pdu->error = le32_to_cpu(pdu->error);
950}
951
Jens Axboe084d1c62012-03-03 20:28:07 +0100952static void convert_text(struct fio_net_cmd *cmd)
953{
954 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
955
956 pdu->level = le32_to_cpu(pdu->level);
957 pdu->buf_len = le32_to_cpu(pdu->buf_len);
958 pdu->log_sec = le64_to_cpu(pdu->log_sec);
959 pdu->log_usec = le64_to_cpu(pdu->log_usec);
960}
961
Jens Axboe1b427252012-03-14 15:03:03 +0100962/*
963 * This has been compressed on the server side, since it can be big.
964 * Uncompress here.
965 */
966static struct cmd_iolog_pdu *convert_iolog(struct fio_net_cmd *cmd)
967{
968 struct cmd_iolog_pdu *pdu = (struct cmd_iolog_pdu *) cmd->payload;
969 struct cmd_iolog_pdu *ret;
970 uint32_t nr_samples;
971 unsigned long total;
972 z_stream stream;
973 void *p;
Jens Axboef5ed7652012-03-14 16:28:07 +0100974 int i;
Jens Axboe1b427252012-03-14 15:03:03 +0100975
976 stream.zalloc = Z_NULL;
977 stream.zfree = Z_NULL;
978 stream.opaque = Z_NULL;
979 stream.avail_in = 0;
980 stream.next_in = Z_NULL;
981
982 if (inflateInit(&stream) != Z_OK)
983 return NULL;
984
985 /*
Jens Axboef5ed7652012-03-14 16:28:07 +0100986 * Get header first, it's not compressed
Jens Axboe1b427252012-03-14 15:03:03 +0100987 */
988 nr_samples = le32_to_cpu(pdu->nr_samples);
989
Jens Axboef5ed7652012-03-14 16:28:07 +0100990 total = nr_samples * sizeof(struct io_sample);
991 ret = malloc(total + sizeof(*pdu));
Jens Axboe2f122b12012-03-15 13:10:19 +0100992 ret->thread_number = le32_to_cpu(pdu->thread_number);
Jens Axboe1b427252012-03-14 15:03:03 +0100993 ret->nr_samples = nr_samples;
Jens Axboef5ed7652012-03-14 16:28:07 +0100994 ret->log_type = le32_to_cpu(pdu->log_type);
995 strcpy((char *) ret->name, (char *) pdu->name);
Jens Axboe1b427252012-03-14 15:03:03 +0100996
Jens Axboef5ed7652012-03-14 16:28:07 +0100997 p = (void *) ret + sizeof(*pdu);
998
999 stream.avail_in = cmd->pdu_len - sizeof(*pdu);
1000 stream.next_in = (void *) pdu + sizeof(*pdu);
Jens Axboe1b427252012-03-14 15:03:03 +01001001 while (stream.avail_in) {
1002 unsigned int this_chunk = 65536;
1003 unsigned int this_len;
1004 int err;
1005
1006 if (this_chunk > total)
1007 this_chunk = total;
1008
1009 stream.avail_out = this_chunk;
1010 stream.next_out = p;
1011 err = inflate(&stream, Z_NO_FLUSH);
Jens Axboe3c547fe2012-03-14 21:53:00 +01001012 /* may be Z_OK, or Z_STREAM_END */
1013 if (err < 0) {
Jens Axboe1b427252012-03-14 15:03:03 +01001014 log_err("fio: inflate error %d\n", err);
Jens Axboef5ed7652012-03-14 16:28:07 +01001015 free(ret);
1016 ret = NULL;
Jens Axboe1b427252012-03-14 15:03:03 +01001017 goto out;
1018 }
1019
1020 this_len = this_chunk - stream.avail_out;
1021 p += this_len;
1022 total -= this_len;
1023 }
1024
Jens Axboef5ed7652012-03-14 16:28:07 +01001025 for (i = 0; i < ret->nr_samples; i++) {
1026 struct io_sample *s = &ret->samples[i];
1027
1028 s->time = le64_to_cpu(s->time);
1029 s->val = le64_to_cpu(s->val);
1030 s->ddir = le32_to_cpu(s->ddir);
1031 s->bs = le32_to_cpu(s->bs);
1032 }
1033
Jens Axboe1b427252012-03-14 15:03:03 +01001034out:
1035 inflateEnd(&stream);
1036 return ret;
1037}
1038
Jens Axboea5276612012-03-04 15:15:08 +01001039int fio_handle_client(struct fio_client *client)
Jens Axboe37db14f2011-09-30 17:00:42 -06001040{
Jens Axboea5276612012-03-04 15:15:08 +01001041 struct client_ops *ops = client->ops;
Jens Axboe37db14f2011-09-30 17:00:42 -06001042 struct fio_net_cmd *cmd;
1043
Jens Axboe60efd142011-10-04 13:30:11 +02001044 dprint(FD_NET, "client: handle %s\n", client->hostname);
1045
Jens Axboee951bdc2011-10-05 21:58:45 +02001046 cmd = fio_net_recv_cmd(client->fd);
1047 if (!cmd)
1048 return 0;
Jens Axboec2c94582011-10-05 20:31:30 +02001049
Jens Axboeb9d2f302012-03-08 20:36:28 +01001050 dprint(FD_NET, "client: got cmd op %s from %s (pdu=%u)\n",
1051 fio_server_op(cmd->opcode), client->hostname, cmd->pdu_len);
Jens Axboe46c48f12011-10-01 12:50:51 -06001052
Jens Axboee951bdc2011-10-05 21:58:45 +02001053 switch (cmd->opcode) {
1054 case FIO_NET_CMD_QUIT:
Jens Axboe3ec62ec2012-03-01 12:01:29 +01001055 if (ops->quit)
Jens Axboe35c0ba72012-03-14 10:56:40 +01001056 ops->quit(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +02001057 remove_client(client);
1058 free(cmd);
1059 break;
Jens Axboe084d1c62012-03-03 20:28:07 +01001060 case FIO_NET_CMD_TEXT:
1061 convert_text(cmd);
Jens Axboe35c0ba72012-03-14 10:56:40 +01001062 ops->text(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +02001063 free(cmd);
1064 break;
Jens Axboe3bf236c2012-03-03 20:35:50 +01001065 case FIO_NET_CMD_DU: {
1066 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
1067
1068 convert_dus(&du->dus);
1069 convert_agg(&du->agg);
1070
Stephen M. Camerondd366722012-02-24 08:17:30 +01001071 ops->disk_util(client, cmd);
Jens Axboed09a64a2011-10-13 11:38:56 +02001072 free(cmd);
1073 break;
Jens Axboe3bf236c2012-03-03 20:35:50 +01001074 }
1075 case FIO_NET_CMD_TS: {
1076 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
1077
1078 convert_ts(&p->ts, &p->ts);
1079 convert_gs(&p->rs, &p->rs);
1080
Jens Axboe89e5fad2012-03-05 09:21:12 +01001081 ops->thread_status(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_GS: {
1086 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
1087
1088 convert_gs(gs, gs);
1089
Jens Axboe89e5fad2012-03-05 09:21:12 +01001090 ops->group_stats(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +02001091 free(cmd);
1092 break;
Jens Axboe3bf236c2012-03-03 20:35:50 +01001093 }
1094 case FIO_NET_CMD_ETA: {
1095 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
1096
Jens Axboe89c17072011-10-11 10:15:51 +02001097 remove_reply_cmd(client, cmd);
Jens Axboe3bf236c2012-03-03 20:35:50 +01001098 convert_jobs_eta(je);
Jens Axboea5276612012-03-04 15:15:08 +01001099 handle_eta(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +02001100 free(cmd);
1101 break;
Jens Axboe3bf236c2012-03-03 20:35:50 +01001102 }
Jens Axboee951bdc2011-10-05 21:58:45 +02001103 case FIO_NET_CMD_PROBE:
Jens Axboe89c17072011-10-11 10:15:51 +02001104 remove_reply_cmd(client, cmd);
Stephen M. Camerondd366722012-02-24 08:17:30 +01001105 ops->probe(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +02001106 free(cmd);
1107 break;
Jens Axboe5d7793a2012-03-08 19:59:16 +01001108 case FIO_NET_CMD_SERVER_START:
Jens Axboe01be0382011-10-15 14:43:41 +02001109 client->state = Client_running;
Jens Axboe85dd01e2012-03-12 14:33:16 +01001110 if (ops->job_start)
1111 ops->job_start(client, cmd);
Jens Axboe01be0382011-10-15 14:43:41 +02001112 free(cmd);
1113 break;
Jens Axboe85dd01e2012-03-12 14:33:16 +01001114 case FIO_NET_CMD_START: {
1115 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
1116
1117 pdu->jobs = le32_to_cpu(pdu->jobs);
1118 ops->start(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +02001119 free(cmd);
1120 break;
Jens Axboe85dd01e2012-03-12 14:33:16 +01001121 }
Jens Axboe6b79c802012-03-08 10:51:36 +01001122 case FIO_NET_CMD_STOP: {
1123 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
1124
1125 convert_stop(cmd);
1126 client->state = Client_stopped;
Jens Axboe122c7722012-03-19 09:13:15 +01001127 client->error = le32_to_cpu(pdu->error);
1128 client->signal = le32_to_cpu(pdu->signal);
Jens Axboe6b79c802012-03-08 10:51:36 +01001129 ops->stop(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +02001130 free(cmd);
1131 break;
Jens Axboe6b79c802012-03-08 10:51:36 +01001132 }
Jens Axboe807f9972012-03-02 10:25:24 +01001133 case FIO_NET_CMD_ADD_JOB:
1134 if (ops->add_job)
1135 ops->add_job(client, cmd);
1136 free(cmd);
1137 break;
Jens Axboe1b427252012-03-14 15:03:03 +01001138 case FIO_NET_CMD_IOLOG:
1139 if (ops->iolog) {
1140 struct cmd_iolog_pdu *pdu;
1141
1142 pdu = convert_iolog(cmd);
1143 ops->iolog(client, pdu);
1144 }
1145 free(cmd);
1146 break;
Jens Axboee951bdc2011-10-05 21:58:45 +02001147 default:
Jens Axboe89c17072011-10-11 10:15:51 +02001148 log_err("fio: unknown client op: %s\n", fio_server_op(cmd->opcode));
Jens Axboee951bdc2011-10-05 21:58:45 +02001149 free(cmd);
1150 break;
Jens Axboe37db14f2011-09-30 17:00:42 -06001151 }
1152
Jens Axboee951bdc2011-10-05 21:58:45 +02001153 return 1;
Jens Axboe37db14f2011-09-30 17:00:42 -06001154}
Jens Axboeb66570d2011-10-01 11:11:35 -06001155
Jens Axboea5276612012-03-04 15:15:08 +01001156static void request_client_etas(struct client_ops *ops)
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001157{
1158 struct fio_client *client;
1159 struct flist_head *entry;
1160 struct client_eta *eta;
Jens Axboe82c1ed32011-10-10 08:56:18 +02001161 int skipped = 0;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001162
1163 dprint(FD_NET, "client: request eta (%d)\n", nr_clients);
1164
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001165 eta = malloc(sizeof(*eta));
1166 memset(&eta->eta, 0, sizeof(eta->eta));
1167 eta->pending = nr_clients;
1168
1169 flist_for_each(entry, &client_list) {
1170 client = flist_entry(entry, struct fio_client, list);
1171
Jens Axboe82c1ed32011-10-10 08:56:18 +02001172 if (!flist_empty(&client->eta_list)) {
1173 skipped++;
1174 continue;
1175 }
Jens Axboe01be0382011-10-15 14:43:41 +02001176 if (client->state != Client_running)
1177 continue;
Jens Axboe82c1ed32011-10-10 08:56:18 +02001178
Jens Axboef77d2672011-10-10 14:36:07 +02001179 assert(!client->eta_in_flight);
Jens Axboe82c1ed32011-10-10 08:56:18 +02001180 flist_add_tail(&client->eta_list, &eta_list);
Jens Axboef77d2672011-10-10 14:36:07 +02001181 client->eta_in_flight = eta;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001182 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_SEND_ETA,
Jens Axboedf380932011-10-11 14:25:08 +02001183 (uintptr_t) eta, &client->cmd_list);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001184 }
1185
Jens Axboe82c1ed32011-10-10 08:56:18 +02001186 while (skipped--)
Jens Axboea5276612012-03-04 15:15:08 +01001187 fio_client_dec_jobs_eta(eta, ops->eta);
Jens Axboe82c1ed32011-10-10 08:56:18 +02001188
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001189 dprint(FD_NET, "client: requested eta tag %p\n", eta);
1190}
1191
Jens Axboe89c17072011-10-11 10:15:51 +02001192static int client_check_cmd_timeout(struct fio_client *client,
1193 struct timeval *now)
1194{
1195 struct fio_net_int_cmd *cmd;
1196 struct flist_head *entry, *tmp;
1197 int ret = 0;
1198
1199 flist_for_each_safe(entry, tmp, &client->cmd_list) {
1200 cmd = flist_entry(entry, struct fio_net_int_cmd, list);
1201
1202 if (mtime_since(&cmd->tv, now) < FIO_NET_CLIENT_TIMEOUT)
1203 continue;
1204
1205 log_err("fio: client %s, timeout on cmd %s\n", client->hostname,
1206 fio_server_op(cmd->cmd.opcode));
1207 flist_del(&cmd->list);
1208 free(cmd);
1209 ret = 1;
1210 }
1211
1212 return flist_empty(&client->cmd_list) && ret;
1213}
1214
Jens Axboea5276612012-03-04 15:15:08 +01001215static int fio_check_clients_timed_out(void)
Jens Axboe89c17072011-10-11 10:15:51 +02001216{
1217 struct fio_client *client;
1218 struct flist_head *entry, *tmp;
1219 struct timeval tv;
1220 int ret = 0;
1221
1222 gettimeofday(&tv, NULL);
1223
1224 flist_for_each_safe(entry, tmp, &client_list) {
1225 client = flist_entry(entry, struct fio_client, list);
1226
1227 if (flist_empty(&client->cmd_list))
1228 continue;
1229
1230 if (!client_check_cmd_timeout(client, &tv))
1231 continue;
1232
Jens Axboea5276612012-03-04 15:15:08 +01001233 if (client->ops->timed_out)
1234 client->ops->timed_out(client);
Jens Axboeed727a42012-03-02 12:14:40 +01001235 else
1236 log_err("fio: client %s timed out\n", client->hostname);
1237
Jens Axboe89c17072011-10-11 10:15:51 +02001238 remove_client(client);
1239 ret = 1;
1240 }
1241
1242 return ret;
1243}
1244
Stephen M. Camerondd366722012-02-24 08:17:30 +01001245int fio_handle_clients(struct client_ops *ops)
Jens Axboeb66570d2011-10-01 11:11:35 -06001246{
Jens Axboeb66570d2011-10-01 11:11:35 -06001247 struct pollfd *pfds;
Jens Axboe498c92c2011-10-17 09:14:42 +02001248 int i, ret = 0, retval = 0;
Jens Axboeb66570d2011-10-01 11:11:35 -06001249
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001250 gettimeofday(&eta_tv, NULL);
1251
Jens Axboeb66570d2011-10-01 11:11:35 -06001252 pfds = malloc(nr_clients * sizeof(struct pollfd));
1253
Jens Axboe37f0c1a2011-10-11 14:08:33 +02001254 sum_stat_clients = nr_clients;
1255 init_thread_stat(&client_ts);
1256 init_group_run_stat(&client_gs);
1257
Jens Axboeb66570d2011-10-01 11:11:35 -06001258 while (!exit_backend && nr_clients) {
Jens Axboec2cb6862012-02-23 20:56:12 +01001259 struct flist_head *entry, *tmp;
1260 struct fio_client *client;
1261
Jens Axboe82a4be12011-10-01 12:40:32 -06001262 i = 0;
Jens Axboec2cb6862012-02-23 20:56:12 +01001263 flist_for_each_safe(entry, tmp, &client_list) {
Jens Axboe82a4be12011-10-01 12:40:32 -06001264 client = flist_entry(entry, struct fio_client, list);
1265
Jens Axboea5276612012-03-04 15:15:08 +01001266 if (!client->sent_job && !client->ops->stay_connected &&
Jens Axboec2cb6862012-02-23 20:56:12 +01001267 flist_empty(&client->cmd_list)) {
1268 remove_client(client);
1269 continue;
1270 }
1271
Jens Axboe82a4be12011-10-01 12:40:32 -06001272 pfds[i].fd = client->fd;
1273 pfds[i].events = POLLIN;
1274 i++;
1275 }
1276
Jens Axboec2cb6862012-02-23 20:56:12 +01001277 if (!nr_clients)
1278 break;
1279
Jens Axboe82a4be12011-10-01 12:40:32 -06001280 assert(i == nr_clients);
1281
Jens Axboe5c2857f2011-10-04 13:14:32 +02001282 do {
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001283 struct timeval tv;
1284
1285 gettimeofday(&tv, NULL);
Jens Axboe6433ee02012-03-09 20:10:51 +01001286 if (mtime_since(&eta_tv, &tv) >= ops->eta_msec) {
Jens Axboea5276612012-03-04 15:15:08 +01001287 request_client_etas(ops);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001288 memcpy(&eta_tv, &tv, sizeof(tv));
Jens Axboe89c17072011-10-11 10:15:51 +02001289
Jens Axboea5276612012-03-04 15:15:08 +01001290 if (fio_check_clients_timed_out())
Jens Axboe89c17072011-10-11 10:15:51 +02001291 break;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001292 }
1293
Jens Axboe5c2857f2011-10-04 13:14:32 +02001294 ret = poll(pfds, nr_clients, 100);
1295 if (ret < 0) {
1296 if (errno == EINTR)
1297 continue;
1298 log_err("fio: poll clients: %s\n", strerror(errno));
1299 break;
1300 } else if (!ret)
Jens Axboeb66570d2011-10-01 11:11:35 -06001301 continue;
Jens Axboe5c2857f2011-10-04 13:14:32 +02001302 } while (ret <= 0);
Jens Axboeb66570d2011-10-01 11:11:35 -06001303
1304 for (i = 0; i < nr_clients; i++) {
1305 if (!(pfds[i].revents & POLLIN))
1306 continue;
1307
1308 client = find_client_by_fd(pfds[i].fd);
1309 if (!client) {
Jens Axboe3c5f57e2011-10-06 12:37:50 +02001310 log_err("fio: unknown client fd %d\n", pfds[i].fd);
Jens Axboeb66570d2011-10-01 11:11:35 -06001311 continue;
1312 }
Jens Axboea5276612012-03-04 15:15:08 +01001313 if (!fio_handle_client(client)) {
Jens Axboe28d3ab02011-10-05 20:45:37 +02001314 log_info("client: host=%s disconnected\n",
1315 client->hostname);
1316 remove_client(client);
Jens Axboe498c92c2011-10-17 09:14:42 +02001317 retval = 1;
Jens Axboe38990762011-10-17 13:31:33 +02001318 } else if (client->error)
Jens Axboe498c92c2011-10-17 09:14:42 +02001319 retval = 1;
Jens Axboe343cb4a2012-03-09 17:16:51 +01001320 fio_put_client(client);
Jens Axboeb66570d2011-10-01 11:11:35 -06001321 }
1322 }
1323
1324 free(pfds);
Jens Axboe498c92c2011-10-17 09:14:42 +02001325 return retval;
Jens Axboeb66570d2011-10-01 11:11:35 -06001326}