blob: d0ff25a31a898b243aa3a043ccc64264929331ee [file] [log] [blame]
Jens Axboe50d16972011-09-29 17:45:28 -06001#include <stdio.h>
2#include <stdlib.h>
Jens Axboe142575e2011-09-30 17:35:45 -06003#include <stdarg.h>
Jens Axboe50d16972011-09-29 17:45:28 -06004#include <unistd.h>
5#include <limits.h>
Jens Axboe50d16972011-09-29 17:45:28 -06006#include <errno.h>
7#include <fcntl.h>
8#include <sys/poll.h>
Jens Axboe50d16972011-09-29 17:45:28 -06009#include <sys/types.h>
10#include <sys/wait.h>
Jens Axboe50d16972011-09-29 17:45:28 -060011#include <sys/mman.h>
Jens Axboe50d16972011-09-29 17:45:28 -060012#include <netinet/in.h>
13#include <arpa/inet.h>
14#include <netdb.h>
Jens Axboee46d8092011-10-03 09:11:02 +020015#include <syslog.h>
Jens Axboe50d16972011-09-29 17:45:28 -060016
17#include "fio.h"
Jens Axboe132159a2011-09-30 15:01:32 -060018#include "server.h"
Jens Axboefcee5ff2011-09-30 22:50:39 -060019#include "crc/crc16.h"
Jens Axboe50d16972011-09-29 17:45:28 -060020
Jens Axboe132159a2011-09-30 15:01:32 -060021int fio_net_port = 8765;
Jens Axboe50d16972011-09-29 17:45:28 -060022
Jens Axboe009b1be2011-09-29 18:27:02 -060023int exit_backend = 0;
24
Jens Axboe46c48f12011-10-01 12:50:51 -060025static int server_fd = -1;
Jens Axboe37db14f2011-09-30 17:00:42 -060026
Jens Axboe132159a2011-09-30 15:01:32 -060027int fio_send_data(int sk, const void *p, unsigned int len)
28{
Jens Axboe794d69c2011-10-01 08:48:50 -060029 assert(len <= sizeof(struct fio_net_cmd) + FIO_SERVER_MAX_PDU);
30
Jens Axboe132159a2011-09-30 15:01:32 -060031 do {
32 int ret = send(sk, p, len, 0);
33
34 if (ret > 0) {
35 len -= ret;
36 if (!len)
37 break;
38 p += ret;
39 continue;
40 } else if (!ret)
41 break;
42 else if (errno == EAGAIN || errno == EINTR)
43 continue;
44 } while (!exit_backend);
45
46 if (!len)
47 return 0;
48
49 return 1;
50}
51
52int fio_recv_data(int sk, void *p, unsigned int len)
53{
54 do {
55 int ret = recv(sk, p, len, MSG_WAITALL);
56
57 if (ret > 0) {
58 len -= ret;
59 if (!len)
60 break;
61 p += ret;
62 continue;
63 } else if (!ret)
64 break;
65 else if (errno == EAGAIN || errno == EINTR)
66 continue;
67 } while (!exit_backend);
68
69 if (!len)
70 return 0;
71
72 return -1;
73}
74
75static int verify_convert_cmd(struct fio_net_cmd *cmd)
76{
Jens Axboefcee5ff2011-09-30 22:50:39 -060077 uint16_t crc;
Jens Axboe132159a2011-09-30 15:01:32 -060078
Jens Axboefcee5ff2011-09-30 22:50:39 -060079 cmd->cmd_crc16 = le16_to_cpu(cmd->cmd_crc16);
80 cmd->pdu_crc16 = le16_to_cpu(cmd->pdu_crc16);
Jens Axboe132159a2011-09-30 15:01:32 -060081
Jens Axboefcee5ff2011-09-30 22:50:39 -060082 crc = crc16(cmd, FIO_NET_CMD_CRC_SZ);
83 if (crc != cmd->cmd_crc16) {
Jens Axboe132159a2011-09-30 15:01:32 -060084 log_err("fio: server bad crc on command (got %x, wanted %x)\n",
Jens Axboefcee5ff2011-09-30 22:50:39 -060085 cmd->cmd_crc16, crc);
Jens Axboe132159a2011-09-30 15:01:32 -060086 return 1;
87 }
88
89 cmd->version = le16_to_cpu(cmd->version);
90 cmd->opcode = le16_to_cpu(cmd->opcode);
91 cmd->flags = le32_to_cpu(cmd->flags);
92 cmd->serial = le64_to_cpu(cmd->serial);
93 cmd->pdu_len = le32_to_cpu(cmd->pdu_len);
94
95 switch (cmd->version) {
96 case FIO_SERVER_VER1:
97 break;
98 default:
99 log_err("fio: bad server cmd version %d\n", cmd->version);
100 return 1;
101 }
102
103 if (cmd->pdu_len > FIO_SERVER_MAX_PDU) {
104 log_err("fio: command payload too large: %u\n", cmd->pdu_len);
105 return 1;
106 }
107
108 return 0;
109}
110
Jens Axboea64e88d2011-10-03 14:20:01 +0200111/*
112 * Read (and defragment, if necessary) incoming commands
113 */
114struct fio_net_cmd *fio_net_recv_cmd(int sk)
Jens Axboe132159a2011-09-30 15:01:32 -0600115{
Jens Axboea64e88d2011-10-03 14:20:01 +0200116 struct fio_net_cmd cmd, *cmdret = NULL;
117 size_t cmd_size = 0, pdu_offset = 0;
Jens Axboefcee5ff2011-09-30 22:50:39 -0600118 uint16_t crc;
Jens Axboea64e88d2011-10-03 14:20:01 +0200119 int ret, first = 1;
120 void *pdu = NULL;
Jens Axboe132159a2011-09-30 15:01:32 -0600121
Jens Axboea64e88d2011-10-03 14:20:01 +0200122 do {
Jens Axboecc0df002011-10-03 20:53:32 +0200123 struct pollfd pfd;
124
125 pfd.fd = sk;
126 pfd.events = POLLIN;
127 ret = 0;
128 do {
129 ret = poll(&pfd, 1, 100);
130 if (ret < 0) {
131 log_err("fio: poll: %s\n", strerror(errno));
132 break;
133 } else if (!ret)
134 continue;
135
136 if (pfd.revents & POLLIN)
137 break;
138 if (pfd.revents & (POLLERR|POLLHUP)) {
139 ret = 1;
140 break;
141 }
142 } while (ret >= 0);
143
144 if (ret < 0)
145 break;
146
Jens Axboea64e88d2011-10-03 14:20:01 +0200147 ret = fio_recv_data(sk, &cmd, sizeof(cmd));
148 if (ret)
149 break;
Jens Axboe132159a2011-09-30 15:01:32 -0600150
Jens Axboea64e88d2011-10-03 14:20:01 +0200151 /* We have a command, verify it and swap if need be */
152 ret = verify_convert_cmd(&cmd);
153 if (ret)
154 break;
Jens Axboe132159a2011-09-30 15:01:32 -0600155
Jens Axboea64e88d2011-10-03 14:20:01 +0200156 if (first)
157 cmd_size = sizeof(cmd) + cmd.pdu_len;
158 else
159 cmd_size += cmd.pdu_len;
Jens Axboe132159a2011-09-30 15:01:32 -0600160
Jens Axboea64e88d2011-10-03 14:20:01 +0200161 cmdret = realloc(cmdret, cmd_size);
Jens Axboe132159a2011-09-30 15:01:32 -0600162
Jens Axboea64e88d2011-10-03 14:20:01 +0200163 if (first)
164 memcpy(cmdret, &cmd, sizeof(cmd));
165 else
166 assert(cmdret->opcode == cmd.opcode);
167
168 if (!cmd.pdu_len)
169 break;
170
171 /* There's payload, get it */
172 pdu = (void *) cmdret->payload + pdu_offset;
173 ret = fio_recv_data(sk, pdu, cmd.pdu_len);
174 if (ret)
175 break;
176
177 /* Verify payload crc */
178 crc = crc16(pdu, cmd.pdu_len);
179 if (crc != cmd.pdu_crc16) {
180 log_err("fio: server bad crc on payload ");
181 log_err("(got %x, wanted %x)\n", cmd.pdu_crc16, crc);
182 ret = 1;
183 break;
184 }
185
186 pdu_offset += cmd.pdu_len;
Jens Axboe817f06b2011-10-03 15:03:08 +0200187 if (!first)
188 cmdret->pdu_len += cmd.pdu_len;
Jens Axboea64e88d2011-10-03 14:20:01 +0200189 first = 0;
190 } while (cmd.flags & FIO_NET_CMD_F_MORE);
191
192 if (ret) {
193 free(cmdret);
194 cmdret = NULL;
Jens Axboecc0df002011-10-03 20:53:32 +0200195 } else if (cmdret)
Jens Axboea64e88d2011-10-03 14:20:01 +0200196 cmdret->flags &= ~FIO_NET_CMD_F_MORE;
Jens Axboe132159a2011-09-30 15:01:32 -0600197
Jens Axboea64e88d2011-10-03 14:20:01 +0200198 return cmdret;
Jens Axboe132159a2011-09-30 15:01:32 -0600199}
200
201void fio_net_cmd_crc(struct fio_net_cmd *cmd)
202{
203 uint32_t pdu_len;
204
Jens Axboeddcc0b62011-10-03 14:45:27 +0200205 cmd->cmd_crc16 = __cpu_to_le16(crc16(cmd, FIO_NET_CMD_CRC_SZ));
Jens Axboe132159a2011-09-30 15:01:32 -0600206
207 pdu_len = le32_to_cpu(cmd->pdu_len);
208 if (pdu_len)
Jens Axboeddcc0b62011-10-03 14:45:27 +0200209 cmd->pdu_crc16 = __cpu_to_le16(crc16(cmd->payload, pdu_len));
Jens Axboe132159a2011-09-30 15:01:32 -0600210}
211
Jens Axboea64e88d2011-10-03 14:20:01 +0200212int fio_net_send_cmd(int fd, uint16_t opcode, const void *buf, off_t size)
Jens Axboe794d69c2011-10-01 08:48:50 -0600213{
214 struct fio_net_cmd *cmd;
215 size_t this_len;
216 int ret;
217
218 do {
219 this_len = size;
220 if (this_len > FIO_SERVER_MAX_PDU)
221 this_len = FIO_SERVER_MAX_PDU;
222
223 cmd = malloc(sizeof(*cmd) + this_len);
224
225 fio_init_net_cmd(cmd, opcode, buf, this_len);
226
227 if (this_len < size)
Jens Axboeddcc0b62011-10-03 14:45:27 +0200228 cmd->flags = __cpu_to_le32(FIO_NET_CMD_F_MORE);
Jens Axboe794d69c2011-10-01 08:48:50 -0600229
230 fio_net_cmd_crc(cmd);
231
232 ret = fio_send_data(fd, cmd, sizeof(*cmd) + this_len);
233 free(cmd);
234 size -= this_len;
235 buf += this_len;
236 } while (!ret && size);
237
238 return ret;
239}
240
Jens Axboecc0df002011-10-03 20:53:32 +0200241int fio_net_send_simple_cmd(int sk, uint16_t opcode, uint64_t serial)
Jens Axboe132159a2011-09-30 15:01:32 -0600242{
243 struct fio_net_cmd cmd = {
Jens Axboeddcc0b62011-10-03 14:45:27 +0200244 .version = __cpu_to_le16(FIO_SERVER_VER1),
Jens Axboe132159a2011-09-30 15:01:32 -0600245 .opcode = cpu_to_le16(opcode),
Jens Axboebdab4442011-09-30 22:58:13 -0600246 .serial = cpu_to_le64(serial),
Jens Axboe132159a2011-09-30 15:01:32 -0600247 };
248
249 fio_net_cmd_crc(&cmd);
250
251 return fio_send_data(sk, &cmd, sizeof(cmd));
252}
253
Jens Axboe437377e2011-10-01 08:31:30 -0600254static int send_quit_command(void)
255{
Jens Axboe46c48f12011-10-01 12:50:51 -0600256 dprint(FD_NET, "server: sending quit\n");
Jens Axboecc0df002011-10-03 20:53:32 +0200257 return fio_net_send_simple_cmd(server_fd, FIO_NET_CMD_QUIT, 0);
Jens Axboe437377e2011-10-01 08:31:30 -0600258}
259
Jens Axboe794d69c2011-10-01 08:48:50 -0600260static int handle_cur_job(struct fio_net_cmd *cmd)
Jens Axboe132159a2011-09-30 15:01:32 -0600261{
Jens Axboea64e88d2011-10-03 14:20:01 +0200262 void *buf = cmd->payload;
263 int ret;
Jens Axboe132159a2011-09-30 15:01:32 -0600264
Jens Axboea64e88d2011-10-03 14:20:01 +0200265 parse_jobs_ini(buf, 1, 0);
Jens Axboe794d69c2011-10-01 08:48:50 -0600266 ret = exec_run();
267 send_quit_command();
268 reset_fio_state();
Jens Axboe132159a2011-09-30 15:01:32 -0600269 return ret;
270}
271
272static int handle_command(struct fio_net_cmd *cmd)
273{
274 int ret;
275
Jens Axboe46c48f12011-10-01 12:50:51 -0600276 dprint(FD_NET, "server: got opcode %d\n", cmd->opcode);
277
Jens Axboe132159a2011-09-30 15:01:32 -0600278 switch (cmd->opcode) {
279 case FIO_NET_CMD_QUIT:
Jens Axboecc0df002011-10-03 20:53:32 +0200280 fio_terminate_threads(TERMINATE_ALL);
Jens Axboed7959182011-10-03 11:48:39 +0200281 return 1;
282 case FIO_NET_CMD_EXIT:
Jens Axboe132159a2011-09-30 15:01:32 -0600283 exit_backend = 1;
284 return 1;
285 case FIO_NET_CMD_ACK:
286 return 0;
287 case FIO_NET_CMD_NAK:
288 return 1;
289 case FIO_NET_CMD_JOB:
Jens Axboe794d69c2011-10-01 08:48:50 -0600290 ret = handle_cur_job(cmd);
Jens Axboe132159a2011-09-30 15:01:32 -0600291 break;
292 default:
293 log_err("fio: unknown opcode: %d\n", cmd->opcode);
294 ret = 1;
295 }
296
297 return ret;
298}
299
300static int handle_connection(int sk)
301{
302 struct fio_net_cmd *cmd = NULL;
303 int ret = 0;
304
305 /* read forever */
306 while (!exit_backend) {
Jens Axboea64e88d2011-10-03 14:20:01 +0200307 cmd = fio_net_recv_cmd(sk);
Jens Axboe132159a2011-09-30 15:01:32 -0600308 if (!cmd) {
309 ret = 1;
310 break;
311 }
312
Jens Axboe132159a2011-09-30 15:01:32 -0600313 ret = handle_command(cmd);
314 if (ret)
315 break;
316
317 free(cmd);
Jens Axboec77a99e2011-10-01 16:26:42 -0400318 cmd = NULL;
Jens Axboe132159a2011-09-30 15:01:32 -0600319 }
320
321 if (cmd)
322 free(cmd);
323
324 return ret;
325}
326
Jens Axboecc0df002011-10-03 20:53:32 +0200327void fio_server_idle_loop(void)
328{
329 if (server_fd != -1)
330 handle_connection(server_fd);
331}
332
Jens Axboe50d16972011-09-29 17:45:28 -0600333static int accept_loop(int listen_sk)
334{
335 struct sockaddr addr;
336 unsigned int len = sizeof(addr);
Jens Axboe009b1be2011-09-29 18:27:02 -0600337 struct pollfd pfd;
Jens Axboe132159a2011-09-30 15:01:32 -0600338 int ret, sk, flags, exitval = 0;
Jens Axboe50d16972011-09-29 17:45:28 -0600339
Jens Axboe009b1be2011-09-29 18:27:02 -0600340 flags = fcntl(listen_sk, F_GETFL);
341 flags |= O_NONBLOCK;
342 fcntl(listen_sk, F_SETFL, flags);
Jens Axboe50d16972011-09-29 17:45:28 -0600343again:
Jens Axboe009b1be2011-09-29 18:27:02 -0600344 pfd.fd = listen_sk;
345 pfd.events = POLLIN;
346 do {
347 ret = poll(&pfd, 1, 100);
348 if (ret < 0) {
349 if (errno == EINTR)
350 break;
Jens Axboefcee5ff2011-09-30 22:50:39 -0600351 log_err("fio: poll: %s\n", strerror(errno));
Jens Axboe009b1be2011-09-29 18:27:02 -0600352 goto out;
353 } else if (!ret)
354 continue;
355
356 if (pfd.revents & POLLIN)
357 break;
358 } while (!exit_backend);
359
360 if (exit_backend)
361 goto out;
362
Jens Axboe50d16972011-09-29 17:45:28 -0600363 sk = accept(listen_sk, &addr, &len);
Jens Axboe50d16972011-09-29 17:45:28 -0600364 if (sk < 0) {
Jens Axboe690e09a2011-09-29 18:38:08 -0600365 log_err("fio: accept: %s\n", strerror(errno));
Jens Axboe50d16972011-09-29 17:45:28 -0600366 return -1;
367 }
368
Jens Axboe46c48f12011-10-01 12:50:51 -0600369 dprint(FD_NET, "server got a connection\n");
370
Jens Axboe37db14f2011-09-30 17:00:42 -0600371 server_fd = sk;
372
Jens Axboecc0df002011-10-03 20:53:32 +0200373 printf("handle\n");
374
Jens Axboe132159a2011-09-30 15:01:32 -0600375 exitval = handle_connection(sk);
Jens Axboe50d16972011-09-29 17:45:28 -0600376
Jens Axboecc0df002011-10-03 20:53:32 +0200377 printf("out, exit %d\n", exitval);
378
Jens Axboe37db14f2011-09-30 17:00:42 -0600379 server_fd = -1;
Jens Axboe50d16972011-09-29 17:45:28 -0600380 close(sk);
Jens Axboe5c341e92011-09-29 18:00:35 -0600381
Jens Axboe009b1be2011-09-29 18:27:02 -0600382 if (!exit_backend)
Jens Axboe5c341e92011-09-29 18:00:35 -0600383 goto again;
384
Jens Axboe009b1be2011-09-29 18:27:02 -0600385out:
Jens Axboe132159a2011-09-30 15:01:32 -0600386 return exitval;
Jens Axboe50d16972011-09-29 17:45:28 -0600387}
388
Jens Axboee46d8092011-10-03 09:11:02 +0200389static int fio_server(void)
Jens Axboe50d16972011-09-29 17:45:28 -0600390{
391 struct sockaddr_in saddr_in;
392 struct sockaddr addr;
393 unsigned int len;
Jens Axboeafcf7752011-09-29 18:28:46 -0600394 int sk, opt, ret;
Jens Axboe50d16972011-09-29 17:45:28 -0600395
Jens Axboe46c48f12011-10-01 12:50:51 -0600396 dprint(FD_NET, "starting server\n");
397
Jens Axboe50d16972011-09-29 17:45:28 -0600398 sk = socket(AF_INET, SOCK_STREAM, 0);
399 if (sk < 0) {
Jens Axboe690e09a2011-09-29 18:38:08 -0600400 log_err("fio: socket: %s\n", strerror(errno));
Jens Axboe50d16972011-09-29 17:45:28 -0600401 return -1;
402 }
403
404 opt = 1;
405 if (setsockopt(sk, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
Jens Axboe690e09a2011-09-29 18:38:08 -0600406 log_err("fio: setsockopt: %s\n", strerror(errno));
Jens Axboe50d16972011-09-29 17:45:28 -0600407 return -1;
408 }
409#ifdef SO_REUSEPORT
410 if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt)) < 0) {
Jens Axboe690e09a2011-09-29 18:38:08 -0600411 log_err("fio: setsockopt: %s\n", strerror(errno));
Jens Axboe50d16972011-09-29 17:45:28 -0600412 return 1;
413 }
414#endif
415
416 saddr_in.sin_family = AF_INET;
417 saddr_in.sin_addr.s_addr = htonl(INADDR_ANY);
Jens Axboe132159a2011-09-30 15:01:32 -0600418 saddr_in.sin_port = htons(fio_net_port);
Jens Axboe50d16972011-09-29 17:45:28 -0600419
420 if (bind(sk, (struct sockaddr *) &saddr_in, sizeof(saddr_in)) < 0) {
Jens Axboe690e09a2011-09-29 18:38:08 -0600421 log_err("fio: bind: %s\n", strerror(errno));
Jens Axboe50d16972011-09-29 17:45:28 -0600422 return -1;
423 }
424
425 if (listen(sk, 1) < 0) {
Jens Axboe690e09a2011-09-29 18:38:08 -0600426 log_err("fio: listen: %s\n", strerror(errno));
Jens Axboe50d16972011-09-29 17:45:28 -0600427 return -1;
428 }
429
430 len = sizeof(addr);
431 if (getsockname(sk, &addr, &len) < 0) {
Jens Axboe690e09a2011-09-29 18:38:08 -0600432 log_err("fio: getsockname: %s\n", strerror(errno));
Jens Axboe50d16972011-09-29 17:45:28 -0600433 return -1;
434 }
435
Jens Axboeafcf7752011-09-29 18:28:46 -0600436 ret = accept_loop(sk);
437 close(sk);
438 return ret;
Jens Axboe50d16972011-09-29 17:45:28 -0600439}
Jens Axboe37db14f2011-09-30 17:00:42 -0600440
Jens Axboe142575e2011-09-30 17:35:45 -0600441int fio_server_text_output(const char *buf, unsigned int len)
Jens Axboe37db14f2011-09-30 17:00:42 -0600442{
Jens Axboe337d75a2011-10-01 12:36:32 -0600443 if (server_fd != -1)
444 return fio_net_send_cmd(server_fd, FIO_NET_CMD_TEXT, buf, len);
445
446 return 0;
Jens Axboe142575e2011-09-30 17:35:45 -0600447}
448
Jens Axboea64e88d2011-10-03 14:20:01 +0200449static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
450{
451 dst->max_val = cpu_to_le64(src->max_val);
452 dst->min_val = cpu_to_le64(src->min_val);
453 dst->samples = cpu_to_le64(src->samples);
454 /* FIXME */
Jens Axboeddcc0b62011-10-03 14:45:27 +0200455 dst->mean = __cpu_to_le64(src->mean);
456 dst->S = __cpu_to_le64(src->S);
Jens Axboea64e88d2011-10-03 14:20:01 +0200457}
458
459static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
460{
461 int i;
462
463 for (i = 0; i < 2; i++) {
464 dst->max_run[i] = cpu_to_le64(src->max_run[i]);
465 dst->min_run[i] = cpu_to_le64(src->min_run[i]);
466 dst->max_bw[i] = cpu_to_le64(src->max_bw[i]);
467 dst->min_bw[i] = cpu_to_le64(src->min_bw[i]);
468 dst->io_kb[i] = cpu_to_le64(src->io_kb[i]);
469 dst->agg[i] = cpu_to_le64(src->agg[i]);
470 }
471
472 dst->kb_base = cpu_to_le32(src->kb_base);
473 dst->groupid = cpu_to_le32(src->groupid);
474}
475
476/*
477 * Send a CMD_TS, which packs struct thread_stat and group_run_stats
478 * into a single payload.
479 */
480void fio_server_send_ts(struct thread_stat *ts, struct group_run_stats *rs)
481{
482 struct cmd_ts_pdu p;
483 int i, j;
484
485 strcpy(p.ts.name, ts->name);
486 strcpy(p.ts.verror, ts->verror);
487 strcpy(p.ts.description, ts->description);
488
Jens Axboeddcc0b62011-10-03 14:45:27 +0200489 p.ts.error = cpu_to_le32(ts->error);
Jens Axboea64e88d2011-10-03 14:20:01 +0200490 p.ts.groupid = cpu_to_le32(ts->groupid);
Jens Axboeddcc0b62011-10-03 14:45:27 +0200491 p.ts.pid = cpu_to_le32(ts->pid);
Jens Axboea64e88d2011-10-03 14:20:01 +0200492 p.ts.members = cpu_to_le32(ts->members);
493
494 for (i = 0; i < 2; i++) {
495 convert_io_stat(&p.ts.clat_stat[i], &ts->clat_stat[i]);
496 convert_io_stat(&p.ts.slat_stat[i], &ts->slat_stat[i]);
497 convert_io_stat(&p.ts.lat_stat[i], &ts->lat_stat[i]);
498 convert_io_stat(&p.ts.bw_stat[i], &ts->bw_stat[i]);
499 }
500
501 p.ts.usr_time = cpu_to_le64(ts->usr_time);
502 p.ts.sys_time = cpu_to_le64(ts->sys_time);
503 p.ts.ctx = cpu_to_le64(ts->ctx);
504 p.ts.minf = cpu_to_le64(ts->minf);
505 p.ts.majf = cpu_to_le64(ts->majf);
506 p.ts.clat_percentiles = cpu_to_le64(ts->clat_percentiles);
507 p.ts.percentile_list = NULL;
508
509 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
510 p.ts.io_u_map[i] = cpu_to_le32(ts->io_u_map[i]);
511 p.ts.io_u_submit[i] = cpu_to_le32(ts->io_u_submit[i]);
512 p.ts.io_u_complete[i] = cpu_to_le32(ts->io_u_complete[i]);
513 }
514
515 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
516 p.ts.io_u_lat_u[i] = cpu_to_le32(ts->io_u_lat_u[i]);
517 p.ts.io_u_lat_m[i] = cpu_to_le32(ts->io_u_lat_m[i]);
518 }
519
520 for (i = 0; i < 2; i++)
521 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
522 p.ts.io_u_plat[i][j] = cpu_to_le32(ts->io_u_plat[i][j]);
523
524 for (i = 0; i < 3; i++) {
525 p.ts.total_io_u[i] = cpu_to_le64(ts->total_io_u[i]);
526 p.ts.short_io_u[i] = cpu_to_le64(ts->total_io_u[i]);
527 }
528
529 p.ts.total_submit = cpu_to_le64(ts->total_submit);
530 p.ts.total_complete = cpu_to_le64(ts->total_complete);
531
532 for (i = 0; i < 2; i++) {
533 p.ts.io_bytes[i] = cpu_to_le64(ts->io_bytes[i]);
534 p.ts.runtime[i] = cpu_to_le64(ts->runtime[i]);
535 }
536
537 p.ts.total_run_time = cpu_to_le64(ts->total_run_time);
538 p.ts.continue_on_error = cpu_to_le16(ts->continue_on_error);
539 p.ts.total_err_count = cpu_to_le64(ts->total_err_count);
Jens Axboeddcc0b62011-10-03 14:45:27 +0200540 p.ts.first_error = cpu_to_le32(ts->first_error);
541 p.ts.kb_base = cpu_to_le32(ts->kb_base);
Jens Axboea64e88d2011-10-03 14:20:01 +0200542
543 convert_gs(&p.rs, rs);
544
545 fio_net_send_cmd(server_fd, FIO_NET_CMD_TS, &p, sizeof(p));
546}
547
548void fio_server_send_gs(struct group_run_stats *rs)
549{
550 struct group_run_stats gs;
551
552 convert_gs(&gs, rs);
553 fio_net_send_cmd(server_fd, FIO_NET_CMD_GS, &gs, sizeof(gs));
554}
555
Jens Axboecf451d12011-10-03 16:48:30 +0200556void fio_server_send_status(void)
557{
Jens Axboe1d1f45a2011-10-03 19:44:41 +0200558 struct jobs_eta *je;
559 size_t size;
560 void *buf;
Jens Axboecf451d12011-10-03 16:48:30 +0200561 int i;
562
Jens Axboe1d1f45a2011-10-03 19:44:41 +0200563 size = sizeof(*je) + thread_number * sizeof(char);
564 buf = malloc(size);
565 memset(buf, 0, size);
566 je = buf;
567
568 if (!calc_thread_status(je)) {
569 free(je);
Jens Axboecf451d12011-10-03 16:48:30 +0200570 return;
Jens Axboecf451d12011-10-03 16:48:30 +0200571 }
572
Jens Axboe1d1f45a2011-10-03 19:44:41 +0200573 je->nr_running = cpu_to_le32(je->nr_running);
574 je->nr_ramp = cpu_to_le32(je->nr_ramp);
575 je->nr_pending = cpu_to_le32(je->nr_pending);
576 je->files_open = cpu_to_le32(je->files_open);
577 je->m_rate = cpu_to_le32(je->m_rate);
578 je->t_rate = cpu_to_le32(je->t_rate);
579 je->m_iops = cpu_to_le32(je->m_iops);
580 je->t_iops = cpu_to_le32(je->t_iops);
Jens Axboecf451d12011-10-03 16:48:30 +0200581
Jens Axboe1d1f45a2011-10-03 19:44:41 +0200582 for (i = 0; i < 2; i++) {
583 je->rate[i] = cpu_to_le32(je->rate[i]);
584 je->iops[i] = cpu_to_le32(je->iops[i]);
585 }
586
587 je->elapsed_sec = cpu_to_le32(je->nr_running);
588 je->eta_sec = cpu_to_le64(je->eta_sec);
589
590 fio_net_send_cmd(server_fd, FIO_NET_CMD_ETA, buf, size);
591 free(je);
Jens Axboecf451d12011-10-03 16:48:30 +0200592}
593
Jens Axboe142575e2011-09-30 17:35:45 -0600594int fio_server_log(const char *format, ...)
595{
596 char buffer[1024];
597 va_list args;
Jens Axboe82fa6b22011-09-30 22:29:08 -0600598 size_t len;
Jens Axboe142575e2011-09-30 17:35:45 -0600599
600 va_start(args, format);
Jens Axboe82fa6b22011-09-30 22:29:08 -0600601 len = vsnprintf(buffer, sizeof(buffer), format, args);
Jens Axboe142575e2011-09-30 17:35:45 -0600602 va_end(args);
603
Jens Axboe82fa6b22011-09-30 22:29:08 -0600604 return fio_server_text_output(buffer, len);
Jens Axboe37db14f2011-09-30 17:00:42 -0600605}
Jens Axboee46d8092011-10-03 09:11:02 +0200606
607int fio_start_server(int daemonize)
608{
609 pid_t pid;
610
611 if (!daemonize)
612 return fio_server();
613
614 openlog("fio", LOG_NDELAY|LOG_NOWAIT|LOG_PID, LOG_USER);
615 pid = fork();
616 if (pid < 0) {
617 syslog(LOG_ERR, "failed server fork");
618 return 1;
619 } else if (pid)
620 exit(0);
621
622 setsid();
623 close(STDIN_FILENO);
624 close(STDOUT_FILENO);
625 close(STDERR_FILENO);
626 f_out = NULL;
627 f_err = NULL;
628 log_syslog = 1;
629 return fio_server();
630}