blob: 74f5e6ad8ff36e53713deafbe1eb6272d26f99a1 [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) {
Jens Axboe5fc58b02011-10-03 21:09:14 +0200131 if (errno == EINTR)
132 break;
Jens Axboecc0df002011-10-03 20:53:32 +0200133 log_err("fio: poll: %s\n", strerror(errno));
134 break;
135 } else if (!ret)
136 continue;
137
138 if (pfd.revents & POLLIN)
139 break;
140 if (pfd.revents & (POLLERR|POLLHUP)) {
141 ret = 1;
142 break;
143 }
144 } while (ret >= 0);
145
146 if (ret < 0)
147 break;
148
Jens Axboea64e88d2011-10-03 14:20:01 +0200149 ret = fio_recv_data(sk, &cmd, sizeof(cmd));
150 if (ret)
151 break;
Jens Axboe132159a2011-09-30 15:01:32 -0600152
Jens Axboea64e88d2011-10-03 14:20:01 +0200153 /* We have a command, verify it and swap if need be */
154 ret = verify_convert_cmd(&cmd);
155 if (ret)
156 break;
Jens Axboe132159a2011-09-30 15:01:32 -0600157
Jens Axboea64e88d2011-10-03 14:20:01 +0200158 if (first)
159 cmd_size = sizeof(cmd) + cmd.pdu_len;
160 else
161 cmd_size += cmd.pdu_len;
Jens Axboe132159a2011-09-30 15:01:32 -0600162
Jens Axboea64e88d2011-10-03 14:20:01 +0200163 cmdret = realloc(cmdret, cmd_size);
Jens Axboe132159a2011-09-30 15:01:32 -0600164
Jens Axboea64e88d2011-10-03 14:20:01 +0200165 if (first)
166 memcpy(cmdret, &cmd, sizeof(cmd));
167 else
168 assert(cmdret->opcode == cmd.opcode);
169
170 if (!cmd.pdu_len)
171 break;
172
173 /* There's payload, get it */
174 pdu = (void *) cmdret->payload + pdu_offset;
175 ret = fio_recv_data(sk, pdu, cmd.pdu_len);
176 if (ret)
177 break;
178
179 /* Verify payload crc */
180 crc = crc16(pdu, cmd.pdu_len);
181 if (crc != cmd.pdu_crc16) {
182 log_err("fio: server bad crc on payload ");
183 log_err("(got %x, wanted %x)\n", cmd.pdu_crc16, crc);
184 ret = 1;
185 break;
186 }
187
188 pdu_offset += cmd.pdu_len;
Jens Axboe817f06b2011-10-03 15:03:08 +0200189 if (!first)
190 cmdret->pdu_len += cmd.pdu_len;
Jens Axboea64e88d2011-10-03 14:20:01 +0200191 first = 0;
192 } while (cmd.flags & FIO_NET_CMD_F_MORE);
193
194 if (ret) {
195 free(cmdret);
196 cmdret = NULL;
Jens Axboecc0df002011-10-03 20:53:32 +0200197 } else if (cmdret)
Jens Axboea64e88d2011-10-03 14:20:01 +0200198 cmdret->flags &= ~FIO_NET_CMD_F_MORE;
Jens Axboe132159a2011-09-30 15:01:32 -0600199
Jens Axboea64e88d2011-10-03 14:20:01 +0200200 return cmdret;
Jens Axboe132159a2011-09-30 15:01:32 -0600201}
202
203void fio_net_cmd_crc(struct fio_net_cmd *cmd)
204{
205 uint32_t pdu_len;
206
Jens Axboeddcc0b62011-10-03 14:45:27 +0200207 cmd->cmd_crc16 = __cpu_to_le16(crc16(cmd, FIO_NET_CMD_CRC_SZ));
Jens Axboe132159a2011-09-30 15:01:32 -0600208
209 pdu_len = le32_to_cpu(cmd->pdu_len);
210 if (pdu_len)
Jens Axboeddcc0b62011-10-03 14:45:27 +0200211 cmd->pdu_crc16 = __cpu_to_le16(crc16(cmd->payload, pdu_len));
Jens Axboe132159a2011-09-30 15:01:32 -0600212}
213
Jens Axboea64e88d2011-10-03 14:20:01 +0200214int fio_net_send_cmd(int fd, uint16_t opcode, const void *buf, off_t size)
Jens Axboe794d69c2011-10-01 08:48:50 -0600215{
216 struct fio_net_cmd *cmd;
217 size_t this_len;
218 int ret;
219
220 do {
221 this_len = size;
222 if (this_len > FIO_SERVER_MAX_PDU)
223 this_len = FIO_SERVER_MAX_PDU;
224
225 cmd = malloc(sizeof(*cmd) + this_len);
226
227 fio_init_net_cmd(cmd, opcode, buf, this_len);
228
229 if (this_len < size)
Jens Axboeddcc0b62011-10-03 14:45:27 +0200230 cmd->flags = __cpu_to_le32(FIO_NET_CMD_F_MORE);
Jens Axboe794d69c2011-10-01 08:48:50 -0600231
232 fio_net_cmd_crc(cmd);
233
234 ret = fio_send_data(fd, cmd, sizeof(*cmd) + this_len);
235 free(cmd);
236 size -= this_len;
237 buf += this_len;
238 } while (!ret && size);
239
240 return ret;
241}
242
Jens Axboecc0df002011-10-03 20:53:32 +0200243int fio_net_send_simple_cmd(int sk, uint16_t opcode, uint64_t serial)
Jens Axboe132159a2011-09-30 15:01:32 -0600244{
245 struct fio_net_cmd cmd = {
Jens Axboeddcc0b62011-10-03 14:45:27 +0200246 .version = __cpu_to_le16(FIO_SERVER_VER1),
Jens Axboe132159a2011-09-30 15:01:32 -0600247 .opcode = cpu_to_le16(opcode),
Jens Axboebdab4442011-09-30 22:58:13 -0600248 .serial = cpu_to_le64(serial),
Jens Axboe132159a2011-09-30 15:01:32 -0600249 };
250
251 fio_net_cmd_crc(&cmd);
252
253 return fio_send_data(sk, &cmd, sizeof(cmd));
254}
255
Jens Axboe437377e2011-10-01 08:31:30 -0600256static int send_quit_command(void)
257{
Jens Axboe46c48f12011-10-01 12:50:51 -0600258 dprint(FD_NET, "server: sending quit\n");
Jens Axboecc0df002011-10-03 20:53:32 +0200259 return fio_net_send_simple_cmd(server_fd, FIO_NET_CMD_QUIT, 0);
Jens Axboe437377e2011-10-01 08:31:30 -0600260}
261
Jens Axboe794d69c2011-10-01 08:48:50 -0600262static int handle_cur_job(struct fio_net_cmd *cmd)
Jens Axboe132159a2011-09-30 15:01:32 -0600263{
Jens Axboea64e88d2011-10-03 14:20:01 +0200264 void *buf = cmd->payload;
265 int ret;
Jens Axboe132159a2011-09-30 15:01:32 -0600266
Jens Axboea64e88d2011-10-03 14:20:01 +0200267 parse_jobs_ini(buf, 1, 0);
Jens Axboe794d69c2011-10-01 08:48:50 -0600268 ret = exec_run();
269 send_quit_command();
270 reset_fio_state();
Jens Axboe132159a2011-09-30 15:01:32 -0600271 return ret;
272}
273
274static int handle_command(struct fio_net_cmd *cmd)
275{
276 int ret;
277
Jens Axboe46c48f12011-10-01 12:50:51 -0600278 dprint(FD_NET, "server: got opcode %d\n", cmd->opcode);
279
Jens Axboe132159a2011-09-30 15:01:32 -0600280 switch (cmd->opcode) {
281 case FIO_NET_CMD_QUIT:
Jens Axboecc0df002011-10-03 20:53:32 +0200282 fio_terminate_threads(TERMINATE_ALL);
Jens Axboed7959182011-10-03 11:48:39 +0200283 return 1;
284 case FIO_NET_CMD_EXIT:
Jens Axboe132159a2011-09-30 15:01:32 -0600285 exit_backend = 1;
286 return 1;
287 case FIO_NET_CMD_ACK:
288 return 0;
289 case FIO_NET_CMD_NAK:
290 return 1;
291 case FIO_NET_CMD_JOB:
Jens Axboe794d69c2011-10-01 08:48:50 -0600292 ret = handle_cur_job(cmd);
Jens Axboe132159a2011-09-30 15:01:32 -0600293 break;
294 default:
295 log_err("fio: unknown opcode: %d\n", cmd->opcode);
296 ret = 1;
297 }
298
299 return ret;
300}
301
302static int handle_connection(int sk)
303{
304 struct fio_net_cmd *cmd = NULL;
305 int ret = 0;
306
307 /* read forever */
308 while (!exit_backend) {
Jens Axboea64e88d2011-10-03 14:20:01 +0200309 cmd = fio_net_recv_cmd(sk);
Jens Axboe132159a2011-09-30 15:01:32 -0600310 if (!cmd) {
311 ret = 1;
312 break;
313 }
314
Jens Axboe132159a2011-09-30 15:01:32 -0600315 ret = handle_command(cmd);
316 if (ret)
317 break;
318
319 free(cmd);
Jens Axboec77a99e2011-10-01 16:26:42 -0400320 cmd = NULL;
Jens Axboe132159a2011-09-30 15:01:32 -0600321 }
322
323 if (cmd)
324 free(cmd);
325
326 return ret;
327}
328
Jens Axboecc0df002011-10-03 20:53:32 +0200329void fio_server_idle_loop(void)
330{
331 if (server_fd != -1)
332 handle_connection(server_fd);
333}
334
Jens Axboe50d16972011-09-29 17:45:28 -0600335static int accept_loop(int listen_sk)
336{
337 struct sockaddr addr;
338 unsigned int len = sizeof(addr);
Jens Axboe009b1be2011-09-29 18:27:02 -0600339 struct pollfd pfd;
Jens Axboe132159a2011-09-30 15:01:32 -0600340 int ret, sk, flags, exitval = 0;
Jens Axboe50d16972011-09-29 17:45:28 -0600341
Jens Axboe009b1be2011-09-29 18:27:02 -0600342 flags = fcntl(listen_sk, F_GETFL);
343 flags |= O_NONBLOCK;
344 fcntl(listen_sk, F_SETFL, flags);
Jens Axboe50d16972011-09-29 17:45:28 -0600345again:
Jens Axboe009b1be2011-09-29 18:27:02 -0600346 pfd.fd = listen_sk;
347 pfd.events = POLLIN;
348 do {
349 ret = poll(&pfd, 1, 100);
350 if (ret < 0) {
351 if (errno == EINTR)
352 break;
Jens Axboefcee5ff2011-09-30 22:50:39 -0600353 log_err("fio: poll: %s\n", strerror(errno));
Jens Axboe009b1be2011-09-29 18:27:02 -0600354 goto out;
355 } else if (!ret)
356 continue;
357
358 if (pfd.revents & POLLIN)
359 break;
360 } while (!exit_backend);
361
362 if (exit_backend)
363 goto out;
364
Jens Axboe50d16972011-09-29 17:45:28 -0600365 sk = accept(listen_sk, &addr, &len);
Jens Axboe50d16972011-09-29 17:45:28 -0600366 if (sk < 0) {
Jens Axboe690e09a2011-09-29 18:38:08 -0600367 log_err("fio: accept: %s\n", strerror(errno));
Jens Axboe50d16972011-09-29 17:45:28 -0600368 return -1;
369 }
370
Jens Axboe46c48f12011-10-01 12:50:51 -0600371 dprint(FD_NET, "server got a connection\n");
372
Jens Axboe37db14f2011-09-30 17:00:42 -0600373 server_fd = sk;
374
Jens Axboe132159a2011-09-30 15:01:32 -0600375 exitval = handle_connection(sk);
Jens Axboe50d16972011-09-29 17:45:28 -0600376
Jens Axboe37db14f2011-09-30 17:00:42 -0600377 server_fd = -1;
Jens Axboe50d16972011-09-29 17:45:28 -0600378 close(sk);
Jens Axboe5c341e92011-09-29 18:00:35 -0600379
Jens Axboe009b1be2011-09-29 18:27:02 -0600380 if (!exit_backend)
Jens Axboe5c341e92011-09-29 18:00:35 -0600381 goto again;
382
Jens Axboe009b1be2011-09-29 18:27:02 -0600383out:
Jens Axboe132159a2011-09-30 15:01:32 -0600384 return exitval;
Jens Axboe50d16972011-09-29 17:45:28 -0600385}
386
Jens Axboee46d8092011-10-03 09:11:02 +0200387static int fio_server(void)
Jens Axboe50d16972011-09-29 17:45:28 -0600388{
389 struct sockaddr_in saddr_in;
390 struct sockaddr addr;
391 unsigned int len;
Jens Axboeafcf7752011-09-29 18:28:46 -0600392 int sk, opt, ret;
Jens Axboe50d16972011-09-29 17:45:28 -0600393
Jens Axboe46c48f12011-10-01 12:50:51 -0600394 dprint(FD_NET, "starting server\n");
395
Jens Axboe50d16972011-09-29 17:45:28 -0600396 sk = socket(AF_INET, SOCK_STREAM, 0);
397 if (sk < 0) {
Jens Axboe690e09a2011-09-29 18:38:08 -0600398 log_err("fio: socket: %s\n", strerror(errno));
Jens Axboe50d16972011-09-29 17:45:28 -0600399 return -1;
400 }
401
402 opt = 1;
403 if (setsockopt(sk, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
Jens Axboe690e09a2011-09-29 18:38:08 -0600404 log_err("fio: setsockopt: %s\n", strerror(errno));
Jens Axboe50d16972011-09-29 17:45:28 -0600405 return -1;
406 }
407#ifdef SO_REUSEPORT
408 if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt)) < 0) {
Jens Axboe690e09a2011-09-29 18:38:08 -0600409 log_err("fio: setsockopt: %s\n", strerror(errno));
Jens Axboe50d16972011-09-29 17:45:28 -0600410 return 1;
411 }
412#endif
413
414 saddr_in.sin_family = AF_INET;
415 saddr_in.sin_addr.s_addr = htonl(INADDR_ANY);
Jens Axboe132159a2011-09-30 15:01:32 -0600416 saddr_in.sin_port = htons(fio_net_port);
Jens Axboe50d16972011-09-29 17:45:28 -0600417
418 if (bind(sk, (struct sockaddr *) &saddr_in, sizeof(saddr_in)) < 0) {
Jens Axboe690e09a2011-09-29 18:38:08 -0600419 log_err("fio: bind: %s\n", strerror(errno));
Jens Axboe50d16972011-09-29 17:45:28 -0600420 return -1;
421 }
422
423 if (listen(sk, 1) < 0) {
Jens Axboe690e09a2011-09-29 18:38:08 -0600424 log_err("fio: listen: %s\n", strerror(errno));
Jens Axboe50d16972011-09-29 17:45:28 -0600425 return -1;
426 }
427
428 len = sizeof(addr);
429 if (getsockname(sk, &addr, &len) < 0) {
Jens Axboe690e09a2011-09-29 18:38:08 -0600430 log_err("fio: getsockname: %s\n", strerror(errno));
Jens Axboe50d16972011-09-29 17:45:28 -0600431 return -1;
432 }
433
Jens Axboeafcf7752011-09-29 18:28:46 -0600434 ret = accept_loop(sk);
435 close(sk);
436 return ret;
Jens Axboe50d16972011-09-29 17:45:28 -0600437}
Jens Axboe37db14f2011-09-30 17:00:42 -0600438
Jens Axboe142575e2011-09-30 17:35:45 -0600439int fio_server_text_output(const char *buf, unsigned int len)
Jens Axboe37db14f2011-09-30 17:00:42 -0600440{
Jens Axboe337d75a2011-10-01 12:36:32 -0600441 if (server_fd != -1)
442 return fio_net_send_cmd(server_fd, FIO_NET_CMD_TEXT, buf, len);
443
444 return 0;
Jens Axboe142575e2011-09-30 17:35:45 -0600445}
446
Jens Axboea64e88d2011-10-03 14:20:01 +0200447static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
448{
449 dst->max_val = cpu_to_le64(src->max_val);
450 dst->min_val = cpu_to_le64(src->min_val);
451 dst->samples = cpu_to_le64(src->samples);
452 /* FIXME */
Jens Axboeddcc0b62011-10-03 14:45:27 +0200453 dst->mean = __cpu_to_le64(src->mean);
454 dst->S = __cpu_to_le64(src->S);
Jens Axboea64e88d2011-10-03 14:20:01 +0200455}
456
457static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
458{
459 int i;
460
461 for (i = 0; i < 2; i++) {
462 dst->max_run[i] = cpu_to_le64(src->max_run[i]);
463 dst->min_run[i] = cpu_to_le64(src->min_run[i]);
464 dst->max_bw[i] = cpu_to_le64(src->max_bw[i]);
465 dst->min_bw[i] = cpu_to_le64(src->min_bw[i]);
466 dst->io_kb[i] = cpu_to_le64(src->io_kb[i]);
467 dst->agg[i] = cpu_to_le64(src->agg[i]);
468 }
469
470 dst->kb_base = cpu_to_le32(src->kb_base);
471 dst->groupid = cpu_to_le32(src->groupid);
472}
473
474/*
475 * Send a CMD_TS, which packs struct thread_stat and group_run_stats
476 * into a single payload.
477 */
478void fio_server_send_ts(struct thread_stat *ts, struct group_run_stats *rs)
479{
480 struct cmd_ts_pdu p;
481 int i, j;
482
Jens Axboe317b3c82011-10-03 21:47:27 +0200483 memset(&p, 0, sizeof(p));
484
Jens Axboea64e88d2011-10-03 14:20:01 +0200485 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]);
Jens Axboe93eee042011-10-03 21:08:48 +0200526 p.ts.short_io_u[i] = cpu_to_le64(ts->short_io_u[i]);
Jens Axboea64e88d2011-10-03 14:20:01 +0200527 }
528
Jens Axboe93eee042011-10-03 21:08:48 +0200529 p.ts.total_submit = cpu_to_le64(ts->total_submit);
Jens Axboea64e88d2011-10-03 14:20:01 +0200530 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}