blob: d538e8ab95cdecb6dabde63dbf7469c1416fef80 [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 Axboe132159a2011-09-30 15:01:32 -060012#include <netinet/in.h>
13#include <arpa/inet.h>
14#include <netdb.h>
Jens Axboe9e22ecb2011-10-04 14:50:21 +020015#include <signal.h>
Jens Axboe132159a2011-09-30 15:01:32 -060016
17#include "fio.h"
18#include "server.h"
19#include "crc/crc32.h"
Jens Axboeb66570d2011-10-01 11:11:35 -060020#include "flist.h"
Jens Axboe132159a2011-09-30 15:01:32 -060021
Jens Axboeb66570d2011-10-01 11:11:35 -060022struct fio_client {
23 struct flist_head list;
24 struct sockaddr_in addr;
25 char *hostname;
26 int fd;
Jens Axboe81179ee2011-10-04 12:42:06 +020027
28 int state;
Jens Axboe17dd1762011-10-04 13:27:34 +020029 int skip_newline;
Jens Axboe81179ee2011-10-04 12:42:06 +020030
31 uint16_t argc;
32 char **argv;
33};
34
35enum {
Jens Axboe5c2857f2011-10-04 13:14:32 +020036 Client_created = 0,
Jens Axboe81179ee2011-10-04 12:42:06 +020037 Client_connected = 1,
38 Client_started = 2,
39 Client_stopped = 3,
Jens Axboe5c2857f2011-10-04 13:14:32 +020040 Client_exited = 4,
Jens Axboeb66570d2011-10-01 11:11:35 -060041};
42
43static FLIST_HEAD(client_list);
Jens Axboeb66570d2011-10-01 11:11:35 -060044
Jens Axboe0b8f30a2011-10-04 10:31:53 +020045static int handle_client(struct fio_client *client, int one);
46
Jens Axboeb66570d2011-10-01 11:11:35 -060047static struct fio_client *find_client_by_fd(int fd)
48{
49 struct fio_client *client;
50 struct flist_head *entry;
51
52 flist_for_each(entry, &client_list) {
53 client = flist_entry(entry, struct fio_client, list);
54
55 if (client->fd == fd)
56 return client;
57 }
58
59 return NULL;
60}
61
62static struct fio_client *find_client_by_name(const char *name)
63{
64 struct fio_client *client;
65 struct flist_head *entry;
66
67 flist_for_each(entry, &client_list) {
68 client = flist_entry(entry, struct fio_client, list);
69
70 if (!strcmp(name, client->hostname))
71 return client;
72 }
73
74 return NULL;
75}
76
77static void remove_client(struct fio_client *client)
78{
Jens Axboe39e8e012011-10-04 13:46:08 +020079 dprint(FD_NET, "client: removed <%s>\n", client->hostname);
Jens Axboeb66570d2011-10-01 11:11:35 -060080 flist_del(&client->list);
81 nr_clients--;
Jens Axboe81179ee2011-10-04 12:42:06 +020082
Jens Axboeb66570d2011-10-01 11:11:35 -060083 free(client->hostname);
Jens Axboe81179ee2011-10-04 12:42:06 +020084 if (client->argv)
85 free(client->argv);
86
Jens Axboeb66570d2011-10-01 11:11:35 -060087 free(client);
88}
Jens Axboe132159a2011-09-30 15:01:32 -060089
Jens Axboe81179ee2011-10-04 12:42:06 +020090static void __fio_client_add_cmd_option(struct fio_client *client,
91 const char *opt)
92{
Jens Axboe39e8e012011-10-04 13:46:08 +020093 int index;
94
95 index = client->argc++;
Jens Axboe81179ee2011-10-04 12:42:06 +020096 client->argv = realloc(client->argv, sizeof(char *) * client->argc);
Jens Axboe39e8e012011-10-04 13:46:08 +020097 client->argv[index] = strdup(opt);
98 dprint(FD_NET, "client: add cmd %d: %s\n", index, opt);
Jens Axboe81179ee2011-10-04 12:42:06 +020099}
100
101void fio_client_add_cmd_option(const char *hostname, const char *opt)
102{
103 struct fio_client *client;
104
105 if (!hostname || !opt)
106 return;
107
108 client = find_client_by_name(hostname);
109 if (!client) {
110 log_err("fio: unknown client %s\n", hostname);
111 return;
112 }
113
114 __fio_client_add_cmd_option(client, opt);
115}
116
Jens Axboea37f69b2011-10-01 12:24:21 -0600117void fio_client_add(const char *hostname)
Jens Axboe132159a2011-09-30 15:01:32 -0600118{
Jens Axboeb66570d2011-10-01 11:11:35 -0600119 struct fio_client *client;
Jens Axboe132159a2011-09-30 15:01:32 -0600120
Jens Axboe39e8e012011-10-04 13:46:08 +0200121 dprint(FD_NET, "client: added <%s>\n", hostname);
Jens Axboeb66570d2011-10-01 11:11:35 -0600122 client = malloc(sizeof(*client));
Jens Axboea37f69b2011-10-01 12:24:21 -0600123 memset(client, 0, sizeof(*client));
Jens Axboe81179ee2011-10-04 12:42:06 +0200124
Jens Axboea37f69b2011-10-01 12:24:21 -0600125 client->hostname = strdup(hostname);
126 client->fd = -1;
Jens Axboe81179ee2011-10-04 12:42:06 +0200127
128 __fio_client_add_cmd_option(client, "fio");
129
Jens Axboea37f69b2011-10-01 12:24:21 -0600130 flist_add(&client->list, &client_list);
131 nr_clients++;
132}
133
134static int fio_client_connect(struct fio_client *client)
135{
136 int fd;
Jens Axboe132159a2011-09-30 15:01:32 -0600137
Jens Axboe39e8e012011-10-04 13:46:08 +0200138 dprint(FD_NET, "client: connect to host %s\n", client->hostname);
Jens Axboe46c48f12011-10-01 12:50:51 -0600139
Jens Axboeb66570d2011-10-01 11:11:35 -0600140 memset(&client->addr, 0, sizeof(client->addr));
141 client->addr.sin_family = AF_INET;
142 client->addr.sin_port = htons(fio_net_port);
143
Jens Axboea37f69b2011-10-01 12:24:21 -0600144 if (inet_aton(client->hostname, &client->addr.sin_addr) != 1) {
Jens Axboe132159a2011-09-30 15:01:32 -0600145 struct hostent *hent;
146
Jens Axboea37f69b2011-10-01 12:24:21 -0600147 hent = gethostbyname(client->hostname);
Jens Axboe132159a2011-09-30 15:01:32 -0600148 if (!hent) {
149 log_err("fio: gethostbyname: %s\n", strerror(errno));
150 return 1;
151 }
152
Jens Axboeb66570d2011-10-01 11:11:35 -0600153 memcpy(&client->addr.sin_addr, hent->h_addr, 4);
Jens Axboe132159a2011-09-30 15:01:32 -0600154 }
155
156 fd = socket(AF_INET, SOCK_STREAM, 0);
157 if (fd < 0) {
158 log_err("fio: socket: %s\n", strerror(errno));
159 return 1;
160 }
161
Jens Axboeb66570d2011-10-01 11:11:35 -0600162 if (connect(fd, (struct sockaddr *) &client->addr, sizeof(client->addr)) < 0) {
Jens Axboe132159a2011-09-30 15:01:32 -0600163 log_err("fio: connect: %s\n", strerror(errno));
Jens Axboecdf54d82011-10-04 08:31:40 +0200164 log_err("fio: failed to connect to %s\n", client->hostname);
Jens Axboe132159a2011-09-30 15:01:32 -0600165 return 1;
166 }
167
Jens Axboeb66570d2011-10-01 11:11:35 -0600168 client->fd = fd;
Jens Axboe81179ee2011-10-04 12:42:06 +0200169 client->state = Client_connected;
Jens Axboe132159a2011-09-30 15:01:32 -0600170 return 0;
171}
172
Jens Axboecc0df002011-10-03 20:53:32 +0200173void fio_clients_terminate(void)
174{
175 struct flist_head *entry;
176 struct fio_client *client;
177
Jens Axboe60efd142011-10-04 13:30:11 +0200178 dprint(FD_NET, "client: terminate clients\n");
179
Jens Axboecc0df002011-10-03 20:53:32 +0200180 flist_for_each(entry, &client_list) {
181 client = flist_entry(entry, struct fio_client, list);
182
183 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_QUIT, 0);
184 }
185}
186
187static void sig_int(int sig)
188{
Jens Axboe60efd142011-10-04 13:30:11 +0200189 dprint(FD_NET, "client: got sign %d\n", sig);
Jens Axboecc0df002011-10-03 20:53:32 +0200190 fio_clients_terminate();
191}
192
193static void client_signal_handler(void)
194{
195 struct sigaction act;
196
197 memset(&act, 0, sizeof(act));
198 act.sa_handler = sig_int;
199 act.sa_flags = SA_RESTART;
200 sigaction(SIGINT, &act, NULL);
201
202 memset(&act, 0, sizeof(act));
203 act.sa_handler = sig_int;
204 act.sa_flags = SA_RESTART;
205 sigaction(SIGTERM, &act, NULL);
206}
207
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200208static void probe_client(struct fio_client *client)
209{
Jens Axboe60efd142011-10-04 13:30:11 +0200210 dprint(FD_NET, "client: send probe\n");
211
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200212 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_PROBE, 0);
213 handle_client(client, 1);
214}
215
Jens Axboe81179ee2011-10-04 12:42:06 +0200216static int send_client_cmd_line(struct fio_client *client)
217{
218 struct cmd_line_pdu *pdu;
219 int i, ret;
220
Jens Axboe39e8e012011-10-04 13:46:08 +0200221 dprint(FD_NET, "client: send cmdline %d\n", client->argc);
Jens Axboe60efd142011-10-04 13:30:11 +0200222
Jens Axboe81179ee2011-10-04 12:42:06 +0200223 pdu = malloc(sizeof(*pdu));
224 for (i = 0; i < client->argc; i++)
225 strcpy((char *) pdu->argv[i], client->argv[i]);
226
227 pdu->argc = cpu_to_le16(client->argc);
228 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOBLINE, pdu, sizeof(*pdu));
229 free(pdu);
230 return ret;
231}
232
Jens Axboea37f69b2011-10-01 12:24:21 -0600233int fio_clients_connect(void)
234{
235 struct fio_client *client;
236 struct flist_head *entry, *tmp;
237 int ret;
238
Jens Axboe60efd142011-10-04 13:30:11 +0200239 dprint(FD_NET, "client: connect all\n");
240
Jens Axboecc0df002011-10-03 20:53:32 +0200241 client_signal_handler();
242
Jens Axboea37f69b2011-10-01 12:24:21 -0600243 flist_for_each_safe(entry, tmp, &client_list) {
244 client = flist_entry(entry, struct fio_client, list);
245
246 ret = fio_client_connect(client);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200247 if (ret) {
Jens Axboea37f69b2011-10-01 12:24:21 -0600248 remove_client(client);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200249 continue;
250 }
251
252 probe_client(client);
Jens Axboe81179ee2011-10-04 12:42:06 +0200253
254 if (client->argc > 1)
255 send_client_cmd_line(client);
Jens Axboea37f69b2011-10-01 12:24:21 -0600256 }
257
258 return !nr_clients;
259}
260
Jens Axboe132159a2011-09-30 15:01:32 -0600261/*
262 * Send file contents to server backend. We could use sendfile(), but to remain
263 * more portable lets just read/write the darn thing.
264 */
Jens Axboea37f69b2011-10-01 12:24:21 -0600265static int fio_client_send_ini(struct fio_client *client, const char *filename)
Jens Axboe132159a2011-09-30 15:01:32 -0600266{
267 struct stat sb;
268 char *p, *buf;
269 off_t len;
270 int fd, ret;
271
Jens Axboe46c48f12011-10-01 12:50:51 -0600272 dprint(FD_NET, "send ini %s to %s\n", filename, client->hostname);
273
Jens Axboe132159a2011-09-30 15:01:32 -0600274 fd = open(filename, O_RDONLY);
275 if (fd < 0) {
276 log_err("fio: job file open: %s\n", strerror(errno));
277 return 1;
278 }
279
280 if (fstat(fd, &sb) < 0) {
281 log_err("fio: job file stat: %s\n", strerror(errno));
282 return 1;
283 }
284
285 buf = malloc(sb.st_size);
286
287 len = sb.st_size;
288 p = buf;
289 do {
290 ret = read(fd, p, len);
291 if (ret > 0) {
292 len -= ret;
293 if (!len)
294 break;
295 p += ret;
296 continue;
297 } else if (!ret)
298 break;
299 else if (errno == EAGAIN || errno == EINTR)
300 continue;
301 } while (1);
302
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200303 if (len) {
304 log_err("fio: failed reading job file %s\n", filename);
305 return 1;
306 }
307
Jens Axboe81179ee2011-10-04 12:42:06 +0200308 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, buf, sb.st_size);
Jens Axboe132159a2011-09-30 15:01:32 -0600309 free(buf);
310 return ret;
311}
Jens Axboe37db14f2011-09-30 17:00:42 -0600312
Jens Axboea37f69b2011-10-01 12:24:21 -0600313int fio_clients_send_ini(const char *filename)
314{
315 struct fio_client *client;
316 struct flist_head *entry, *tmp;
317
318 flist_for_each_safe(entry, tmp, &client_list) {
319 client = flist_entry(entry, struct fio_client, list);
320
321 if (fio_client_send_ini(client, filename))
322 remove_client(client);
323 }
324
325 return !nr_clients;
326}
327
Jens Axboea64e88d2011-10-03 14:20:01 +0200328static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
329{
330 dst->max_val = le64_to_cpu(src->max_val);
331 dst->min_val = le64_to_cpu(src->min_val);
332 dst->samples = le64_to_cpu(src->samples);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200333
334 /*
335 * Floats arrive as IEEE 754 encoded uint64_t, convert back to double
336 */
337 dst->mean.u.f = fio_uint64_to_double(le64_to_cpu(dst->mean.u.i));
338 dst->S.u.f = fio_uint64_to_double(le64_to_cpu(dst->S.u.i));
Jens Axboea64e88d2011-10-03 14:20:01 +0200339}
340
341static void convert_ts(struct thread_stat *dst, struct thread_stat *src)
342{
343 int i, j;
344
345 dst->error = le32_to_cpu(src->error);
346 dst->groupid = le32_to_cpu(src->groupid);
347 dst->pid = le32_to_cpu(src->pid);
348 dst->members = le32_to_cpu(src->members);
349
350 for (i = 0; i < 2; i++) {
351 convert_io_stat(&dst->clat_stat[i], &src->clat_stat[i]);
352 convert_io_stat(&dst->slat_stat[i], &src->slat_stat[i]);
353 convert_io_stat(&dst->lat_stat[i], &src->lat_stat[i]);
354 convert_io_stat(&dst->bw_stat[i], &src->bw_stat[i]);
355 }
356
357 dst->usr_time = le64_to_cpu(src->usr_time);
358 dst->sys_time = le64_to_cpu(src->sys_time);
359 dst->ctx = le64_to_cpu(src->ctx);
360 dst->minf = le64_to_cpu(src->minf);
361 dst->majf = le64_to_cpu(src->majf);
362 dst->clat_percentiles = le64_to_cpu(src->clat_percentiles);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200363
364 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
365 fio_fp64_t *fps = &src->percentile_list[i];
366 fio_fp64_t *fpd = &dst->percentile_list[i];
367
368 fpd->u.f = fio_uint64_to_double(le64_to_cpu(fps->u.i));
369 }
Jens Axboea64e88d2011-10-03 14:20:01 +0200370
371 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
372 dst->io_u_map[i] = le32_to_cpu(src->io_u_map[i]);
373 dst->io_u_submit[i] = le32_to_cpu(src->io_u_submit[i]);
374 dst->io_u_complete[i] = le32_to_cpu(src->io_u_complete[i]);
375 }
376
377 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
378 dst->io_u_lat_u[i] = le32_to_cpu(src->io_u_lat_u[i]);
379 dst->io_u_lat_m[i] = le32_to_cpu(src->io_u_lat_m[i]);
380 }
381
382 for (i = 0; i < 2; i++)
383 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
384 dst->io_u_plat[i][j] = le32_to_cpu(src->io_u_plat[i][j]);
385
386 for (i = 0; i < 3; i++) {
387 dst->total_io_u[i] = le64_to_cpu(src->total_io_u[i]);
Jens Axboe93eee042011-10-03 21:08:48 +0200388 dst->short_io_u[i] = le64_to_cpu(src->short_io_u[i]);
Jens Axboea64e88d2011-10-03 14:20:01 +0200389 }
390
391 dst->total_submit = le64_to_cpu(src->total_submit);
392 dst->total_complete = le64_to_cpu(src->total_complete);
393
394 for (i = 0; i < 2; i++) {
395 dst->io_bytes[i] = le64_to_cpu(src->io_bytes[i]);
396 dst->runtime[i] = le64_to_cpu(src->runtime[i]);
397 }
398
399 dst->total_run_time = le64_to_cpu(src->total_run_time);
400 dst->continue_on_error = le16_to_cpu(src->continue_on_error);
401 dst->total_err_count = le64_to_cpu(src->total_err_count);
Jens Axboeddcc0b62011-10-03 14:45:27 +0200402 dst->first_error = le32_to_cpu(src->first_error);
403 dst->kb_base = le32_to_cpu(src->kb_base);
Jens Axboea64e88d2011-10-03 14:20:01 +0200404}
405
406static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
407{
408 int i;
409
410 for (i = 0; i < 2; i++) {
411 dst->max_run[i] = le64_to_cpu(src->max_run[i]);
412 dst->min_run[i] = le64_to_cpu(src->min_run[i]);
413 dst->max_bw[i] = le64_to_cpu(src->max_bw[i]);
414 dst->min_bw[i] = le64_to_cpu(src->min_bw[i]);
415 dst->io_kb[i] = le64_to_cpu(src->io_kb[i]);
416 dst->agg[i] = le64_to_cpu(src->agg[i]);
417 }
418
419 dst->kb_base = le32_to_cpu(src->kb_base);
420 dst->groupid = le32_to_cpu(src->groupid);
421}
422
423static void handle_ts(struct fio_net_cmd *cmd)
424{
425 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
426
427 convert_ts(&p->ts, &p->ts);
428 convert_gs(&p->rs, &p->rs);
429
430 show_thread_status(&p->ts, &p->rs);
431}
432
433static void handle_gs(struct fio_net_cmd *cmd)
434{
435 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
436
437 convert_gs(gs, gs);
438 show_group_stats(gs);
439}
440
Jens Axboecf451d12011-10-03 16:48:30 +0200441static void handle_eta(struct fio_net_cmd *cmd)
442{
443 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
444 int i;
445
446 je->nr_running = le32_to_cpu(je->nr_running);
447 je->nr_ramp = le32_to_cpu(je->nr_ramp);
448 je->nr_pending = le32_to_cpu(je->nr_pending);
449 je->files_open = le32_to_cpu(je->files_open);
450 je->m_rate = le32_to_cpu(je->m_rate);
451 je->t_rate = le32_to_cpu(je->t_rate);
452 je->m_iops = le32_to_cpu(je->m_iops);
453 je->t_iops = le32_to_cpu(je->t_iops);
454
455 for (i = 0; i < 2; i++) {
456 je->rate[i] = le32_to_cpu(je->rate[i]);
457 je->iops[i] = le32_to_cpu(je->iops[i]);
458 }
459
460 je->elapsed_sec = le32_to_cpu(je->nr_running);
461 je->eta_sec = le64_to_cpu(je->eta_sec);
462
463 display_thread_status(je);
464}
465
Jens Axboe2e03b4b2011-10-04 09:00:40 +0200466static void handle_probe(struct fio_net_cmd *cmd)
467{
468 struct cmd_probe_pdu *probe = (struct cmd_probe_pdu *) cmd->payload;
469
Jens Axboe6eb24792011-10-04 15:00:30 +0200470 log_info("Probe: hostname=%s, be=%u, fio ver %u.%u.%u\n",
471 probe->hostname, probe->bigendian, probe->fio_major,
472 probe->fio_minor, probe->fio_patch);
Jens Axboe2e03b4b2011-10-04 09:00:40 +0200473}
474
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200475static int handle_client(struct fio_client *client, int one)
Jens Axboe37db14f2011-09-30 17:00:42 -0600476{
477 struct fio_net_cmd *cmd;
Jens Axboea450e492011-10-03 14:24:03 +0200478 int done = 0;
Jens Axboe37db14f2011-09-30 17:00:42 -0600479
Jens Axboe60efd142011-10-04 13:30:11 +0200480 dprint(FD_NET, "client: handle %s\n", client->hostname);
481
Jens Axboe70e0c312011-10-04 09:18:30 +0200482 while ((cmd = fio_net_recv_cmd(client->fd, 1)) != NULL) {
Jens Axboe46c48f12011-10-01 12:50:51 -0600483 dprint(FD_NET, "%s: got cmd op %d\n", client->hostname,
484 cmd->opcode);
485
Jens Axboea64e88d2011-10-03 14:20:01 +0200486 switch (cmd->opcode) {
Jens Axboea64e88d2011-10-03 14:20:01 +0200487 case FIO_NET_CMD_QUIT:
Jens Axboeb66570d2011-10-01 11:11:35 -0600488 remove_client(client);
Jens Axboe437377e2011-10-01 08:31:30 -0600489 free(cmd);
Jens Axboea450e492011-10-03 14:24:03 +0200490 done = 1;
Jens Axboe437377e2011-10-01 08:31:30 -0600491 break;
Jens Axboe17dd1762011-10-04 13:27:34 +0200492 case FIO_NET_CMD_TEXT: {
493 const char *buf = (const char *) cmd->payload;
494
495 if (!client->skip_newline)
496 fprintf(f_out, "Client <%s>: ", client->hostname);
497 fwrite(buf, cmd->pdu_len, 1, f_out);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200498 fflush(f_out);
Jens Axboe17dd1762011-10-04 13:27:34 +0200499 client->skip_newline = strchr(buf, '\n') == NULL;
Jens Axboe37db14f2011-09-30 17:00:42 -0600500 free(cmd);
Jens Axboea64e88d2011-10-03 14:20:01 +0200501 break;
Jens Axboe17dd1762011-10-04 13:27:34 +0200502 }
Jens Axboea64e88d2011-10-03 14:20:01 +0200503 case FIO_NET_CMD_TS:
504 handle_ts(cmd);
505 free(cmd);
506 break;
507 case FIO_NET_CMD_GS:
508 handle_gs(cmd);
509 free(cmd);
510 break;
Jens Axboecf451d12011-10-03 16:48:30 +0200511 case FIO_NET_CMD_ETA:
512 handle_eta(cmd);
513 free(cmd);
514 break;
Jens Axboe2e03b4b2011-10-04 09:00:40 +0200515 case FIO_NET_CMD_PROBE:
516 handle_probe(cmd);
517 free(cmd);
518 break;
Jens Axboe81179ee2011-10-04 12:42:06 +0200519 case FIO_NET_CMD_START:
520 client->state = Client_started;
521 free(cmd);
522 break;
523 case FIO_NET_CMD_STOP:
524 client->state = Client_stopped;
525 free(cmd);
526 break;
Jens Axboea64e88d2011-10-03 14:20:01 +0200527 default:
528 log_err("fio: unknown client op: %d\n", cmd->opcode);
529 free(cmd);
530 break;
Jens Axboe37db14f2011-09-30 17:00:42 -0600531 }
Jens Axboea450e492011-10-03 14:24:03 +0200532
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200533 if (done || one)
Jens Axboea450e492011-10-03 14:24:03 +0200534 break;
Jens Axboe37db14f2011-09-30 17:00:42 -0600535 }
536
537 return 0;
538}
Jens Axboeb66570d2011-10-01 11:11:35 -0600539
540int fio_handle_clients(void)
541{
542 struct fio_client *client;
543 struct flist_head *entry;
544 struct pollfd *pfds;
Jens Axboe82a4be12011-10-01 12:40:32 -0600545 int i, ret = 0;
Jens Axboeb66570d2011-10-01 11:11:35 -0600546
547 pfds = malloc(nr_clients * sizeof(struct pollfd));
548
Jens Axboeb66570d2011-10-01 11:11:35 -0600549 while (!exit_backend && nr_clients) {
Jens Axboe82a4be12011-10-01 12:40:32 -0600550 i = 0;
551 flist_for_each(entry, &client_list) {
552 client = flist_entry(entry, struct fio_client, list);
553
554 pfds[i].fd = client->fd;
555 pfds[i].events = POLLIN;
556 i++;
557 }
558
559 assert(i == nr_clients);
560
Jens Axboe5c2857f2011-10-04 13:14:32 +0200561 do {
562 ret = poll(pfds, nr_clients, 100);
563 if (ret < 0) {
564 if (errno == EINTR)
565 continue;
566 log_err("fio: poll clients: %s\n", strerror(errno));
567 break;
568 } else if (!ret)
Jens Axboeb66570d2011-10-01 11:11:35 -0600569 continue;
Jens Axboe5c2857f2011-10-04 13:14:32 +0200570 } while (ret <= 0);
Jens Axboeb66570d2011-10-01 11:11:35 -0600571
572 for (i = 0; i < nr_clients; i++) {
573 if (!(pfds[i].revents & POLLIN))
574 continue;
575
576 client = find_client_by_fd(pfds[i].fd);
577 if (!client) {
578 log_err("fio: unknown client\n");
579 continue;
580 }
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200581 handle_client(client, 0);
Jens Axboeb66570d2011-10-01 11:11:35 -0600582 }
583 }
584
585 free(pfds);
Jens Axboeb66570d2011-10-01 11:11:35 -0600586 return 0;
587}