9 #include <sys/socket.h>
13 #include <netinet/in.h>
14 #include <arpa/inet.h>
25 #include "crc/crc16.h"
26 #include "lib/ieee754.h"
27 #include "verify-state.h"
30 int fio_net_port = FIO_NET_PORT;
32 bool exit_backend = false;
43 struct flist_head list; /* link on sk_out->list */
44 int flags; /* SK_F_* */
45 int opcode; /* Actual command fields */
49 struct flist_head next; /* Other sk_entry's, if linked command */
52 static char *fio_server_arg;
53 static char *bind_sock;
54 static struct sockaddr_in saddr_in;
55 static struct sockaddr_in6 saddr_in6;
58 static unsigned int has_zlib = 1;
60 static unsigned int has_zlib = 0;
62 static unsigned int use_zlib;
65 static pthread_key_t sk_out_key;
68 static char *fio_server_pipe_name = NULL;
69 static HANDLE hjob = INVALID_HANDLE_VALUE;
79 struct fio_fork_item {
80 struct flist_head list;
85 struct ffi_element element;
98 static const char *fio_server_ops[FIO_NET_CMD_NR] = {
124 static void sk_lock(struct sk_out *sk_out)
126 fio_sem_down(&sk_out->lock);
129 static void sk_unlock(struct sk_out *sk_out)
131 fio_sem_up(&sk_out->lock);
134 void sk_out_assign(struct sk_out *sk_out)
142 pthread_setspecific(sk_out_key, sk_out);
145 static void sk_out_free(struct sk_out *sk_out)
147 __fio_sem_remove(&sk_out->lock);
148 __fio_sem_remove(&sk_out->wait);
149 __fio_sem_remove(&sk_out->xmit);
153 static int __sk_out_drop(struct sk_out *sk_out)
159 assert(sk_out->refs != 0);
160 refs = --sk_out->refs;
165 pthread_setspecific(sk_out_key, NULL);
173 void sk_out_drop(void)
175 struct sk_out *sk_out;
177 sk_out = pthread_getspecific(sk_out_key);
178 __sk_out_drop(sk_out);
181 static void __fio_init_net_cmd(struct fio_net_cmd *cmd, uint16_t opcode,
182 uint32_t pdu_len, uint64_t tag)
184 memset(cmd, 0, sizeof(*cmd));
186 cmd->version = __cpu_to_le16(FIO_SERVER_VER);
187 cmd->opcode = cpu_to_le16(opcode);
188 cmd->tag = cpu_to_le64(tag);
189 cmd->pdu_len = cpu_to_le32(pdu_len);
193 static void fio_init_net_cmd(struct fio_net_cmd *cmd, uint16_t opcode,
194 const void *pdu, uint32_t pdu_len, uint64_t tag)
196 __fio_init_net_cmd(cmd, opcode, pdu_len, tag);
199 memcpy(&cmd->payload, pdu, pdu_len);
202 const char *fio_server_op(unsigned int op)
206 if (op < FIO_NET_CMD_NR)
207 return fio_server_ops[op];
209 sprintf(buf, "UNKNOWN/%d", op);
213 static ssize_t iov_total_len(const struct iovec *iov, int count)
225 static int fio_sendv_data(int sk, struct iovec *iov, int count)
227 ssize_t total_len = iov_total_len(iov, count);
231 ret = writev(sk, iov, count);
238 if (ret >= iov->iov_len) {
243 iov->iov_base += ret;
249 else if (errno == EAGAIN || errno == EINTR)
253 } while (!exit_backend);
261 static int fio_send_data(int sk, const void *p, unsigned int len)
263 struct iovec iov = { .iov_base = (void *) p, .iov_len = len };
265 assert(len <= sizeof(struct fio_net_cmd) + FIO_SERVER_MAX_FRAGMENT_PDU);
267 return fio_sendv_data(sk, &iov, 1);
270 bool fio_server_poll_fd(int fd, short events, int timeout)
272 struct pollfd pfd = {
278 ret = poll(&pfd, 1, timeout);
282 log_err("fio: poll: %s\n", strerror(errno));
287 if (pfd.revents & events)
292 static int fio_recv_data(int sk, void *buf, unsigned int len, bool wait)
300 flags = OS_MSG_DONTWAIT;
303 int ret = recv(sk, p, len, flags);
313 else if (errno == EAGAIN || errno == EINTR) {
319 } while (!exit_backend);
327 static int verify_convert_cmd(struct fio_net_cmd *cmd)
331 cmd->cmd_crc16 = le16_to_cpu(cmd->cmd_crc16);
332 cmd->pdu_crc16 = le16_to_cpu(cmd->pdu_crc16);
334 crc = fio_crc16(cmd, FIO_NET_CMD_CRC_SZ);
335 if (crc != cmd->cmd_crc16) {
336 log_err("fio: server bad crc on command (got %x, wanted %x)\n",
337 cmd->cmd_crc16, crc);
338 fprintf(f_err, "fio: server bad crc on command (got %x, wanted %x)\n",
339 cmd->cmd_crc16, crc);
343 cmd->version = le16_to_cpu(cmd->version);
344 cmd->opcode = le16_to_cpu(cmd->opcode);
345 cmd->flags = le32_to_cpu(cmd->flags);
346 cmd->tag = le64_to_cpu(cmd->tag);
347 cmd->pdu_len = le32_to_cpu(cmd->pdu_len);
349 switch (cmd->version) {
353 log_err("fio: bad server cmd version %d\n", cmd->version);
354 fprintf(f_err, "fio: client/server version mismatch (%d != %d)\n",
355 cmd->version, FIO_SERVER_VER);
359 if (cmd->pdu_len > FIO_SERVER_MAX_FRAGMENT_PDU) {
360 log_err("fio: command payload too large: %u\n", cmd->pdu_len);
368 * Read (and defragment, if necessary) incoming commands
370 struct fio_net_cmd *fio_net_recv_cmd(int sk, bool wait)
372 struct fio_net_cmd cmd, *tmp, *cmdret = NULL;
373 size_t cmd_size = 0, pdu_offset = 0;
379 ret = fio_recv_data(sk, &cmd, sizeof(cmd), wait);
383 /* We have a command, verify it and swap if need be */
384 ret = verify_convert_cmd(&cmd);
389 /* if this is text, add room for \0 at the end */
390 cmd_size = sizeof(cmd) + cmd.pdu_len + 1;
393 cmd_size += cmd.pdu_len;
395 if (cmd_size / 1024 > FIO_SERVER_MAX_CMD_MB * 1024) {
396 log_err("fio: cmd+pdu too large (%llu)\n", (unsigned long long) cmd_size);
401 tmp = realloc(cmdret, cmd_size);
403 log_err("fio: server failed allocating cmd\n");
410 memcpy(cmdret, &cmd, sizeof(cmd));
411 else if (cmdret->opcode != cmd.opcode) {
412 log_err("fio: fragment opcode mismatch (%d != %d)\n",
413 cmdret->opcode, cmd.opcode);
421 /* There's payload, get it */
422 pdu = (char *) cmdret->payload + pdu_offset;
423 ret = fio_recv_data(sk, pdu, cmd.pdu_len, wait);
427 /* Verify payload crc */
428 crc = fio_crc16(pdu, cmd.pdu_len);
429 if (crc != cmd.pdu_crc16) {
430 log_err("fio: server bad crc on payload ");
431 log_err("(got %x, wanted %x)\n", cmd.pdu_crc16, crc);
436 pdu_offset += cmd.pdu_len;
438 cmdret->pdu_len += cmd.pdu_len;
440 } while (cmd.flags & FIO_NET_CMD_F_MORE);
446 /* zero-terminate text input */
447 if (cmdret->pdu_len) {
448 if (cmdret->opcode == FIO_NET_CMD_TEXT) {
449 struct cmd_text_pdu *__pdu = (struct cmd_text_pdu *) cmdret->payload;
450 char *buf = (char *) __pdu->buf;
451 int len = le32_to_cpu(__pdu->buf_len);
454 } else if (cmdret->opcode == FIO_NET_CMD_JOB) {
455 struct cmd_job_pdu *__pdu = (struct cmd_job_pdu *) cmdret->payload;
456 char *buf = (char *) __pdu->buf;
457 int len = le32_to_cpu(__pdu->buf_len);
463 /* frag flag is internal */
464 cmdret->flags &= ~FIO_NET_CMD_F_MORE;
470 static void add_reply(uint64_t tag, struct flist_head *list)
472 struct fio_net_cmd_reply *reply;
474 reply = (struct fio_net_cmd_reply *) (uintptr_t) tag;
475 flist_add_tail(&reply->list, list);
478 static uint64_t alloc_reply(uint64_t tag, uint16_t opcode)
480 struct fio_net_cmd_reply *reply;
482 reply = calloc(1, sizeof(*reply));
483 INIT_FLIST_HEAD(&reply->list);
484 fio_gettime(&reply->ts, NULL);
485 reply->saved_tag = tag;
486 reply->opcode = opcode;
488 return (uintptr_t) reply;
491 static void free_reply(uint64_t tag)
493 struct fio_net_cmd_reply *reply;
495 reply = (struct fio_net_cmd_reply *) (uintptr_t) tag;
499 static void fio_net_cmd_crc_pdu(struct fio_net_cmd *cmd, const void *pdu)
503 cmd->cmd_crc16 = __cpu_to_le16(fio_crc16(cmd, FIO_NET_CMD_CRC_SZ));
505 pdu_len = le32_to_cpu(cmd->pdu_len);
506 cmd->pdu_crc16 = __cpu_to_le16(fio_crc16(pdu, pdu_len));
509 static void fio_net_cmd_crc(struct fio_net_cmd *cmd)
511 fio_net_cmd_crc_pdu(cmd, cmd->payload);
514 int fio_net_send_cmd(int fd, uint16_t opcode, const void *buf, off_t size,
515 uint64_t *tagptr, struct flist_head *list)
517 struct fio_net_cmd *cmd = NULL;
518 size_t this_len, cur_len = 0;
524 tag = *tagptr = alloc_reply(*tagptr, opcode);
526 tag = tagptr ? *tagptr : 0;
530 if (this_len > FIO_SERVER_MAX_FRAGMENT_PDU)
531 this_len = FIO_SERVER_MAX_FRAGMENT_PDU;
533 if (!cmd || cur_len < sizeof(*cmd) + this_len) {
537 cur_len = sizeof(*cmd) + this_len;
538 cmd = malloc(cur_len);
541 fio_init_net_cmd(cmd, opcode, buf, this_len, tag);
544 cmd->flags = __cpu_to_le32(FIO_NET_CMD_F_MORE);
546 fio_net_cmd_crc(cmd);
548 ret = fio_send_data(fd, cmd, sizeof(*cmd) + this_len);
551 } while (!ret && size);
557 add_reply(tag, list);
566 static struct sk_entry *fio_net_prep_cmd(uint16_t opcode, void *buf,
567 size_t size, uint64_t *tagptr,
570 struct sk_entry *entry;
572 entry = smalloc(sizeof(*entry));
576 INIT_FLIST_HEAD(&entry->next);
577 entry->opcode = opcode;
578 if (flags & SK_F_COPY) {
579 entry->buf = smalloc(size);
580 memcpy(entry->buf, buf, size);
586 entry->tag = *tagptr;
589 entry->flags = flags;
593 static int handle_sk_entry(struct sk_out *sk_out, struct sk_entry *entry);
595 static void fio_net_queue_entry(struct sk_entry *entry)
597 struct sk_out *sk_out = pthread_getspecific(sk_out_key);
599 if (entry->flags & SK_F_INLINE)
600 handle_sk_entry(sk_out, entry);
603 flist_add_tail(&entry->list, &sk_out->list);
606 fio_sem_up(&sk_out->wait);
610 static int fio_net_queue_cmd(uint16_t opcode, void *buf, off_t size,
611 uint64_t *tagptr, int flags)
613 struct sk_entry *entry;
615 entry = fio_net_prep_cmd(opcode, buf, size, tagptr, flags);
617 fio_net_queue_entry(entry);
624 static int fio_net_send_simple_stack_cmd(int sk, uint16_t opcode, uint64_t tag)
626 struct fio_net_cmd cmd;
628 fio_init_net_cmd(&cmd, opcode, NULL, 0, tag);
629 fio_net_cmd_crc(&cmd);
631 return fio_send_data(sk, &cmd, sizeof(cmd));
635 * If 'list' is non-NULL, then allocate and store the sent command for
636 * later verification.
638 int fio_net_send_simple_cmd(int sk, uint16_t opcode, uint64_t tag,
639 struct flist_head *list)
644 tag = alloc_reply(tag, opcode);
646 ret = fio_net_send_simple_stack_cmd(sk, opcode, tag);
655 add_reply(tag, list);
660 static int fio_net_queue_quit(void)
662 dprint(FD_NET, "server: sending quit\n");
664 return fio_net_queue_cmd(FIO_NET_CMD_QUIT, NULL, 0, NULL, SK_F_SIMPLE);
667 int fio_net_send_quit(int sk)
669 dprint(FD_NET, "server: sending quit\n");
671 return fio_net_send_simple_cmd(sk, FIO_NET_CMD_QUIT, 0, NULL);
674 static int fio_net_send_ack(struct fio_net_cmd *cmd, int error, int signal)
676 struct cmd_end_pdu epdu;
682 epdu.error = __cpu_to_le32(error);
683 epdu.signal = __cpu_to_le32(signal);
684 return fio_net_queue_cmd(FIO_NET_CMD_STOP, &epdu, sizeof(epdu), &tag, SK_F_COPY);
687 static int fio_net_queue_stop(int error, int signal)
689 dprint(FD_NET, "server: sending stop (%d, %d)\n", error, signal);
690 return fio_net_send_ack(NULL, error, signal);
694 static void fio_server_add_fork_item(struct ffi_element *element, struct flist_head *list)
696 struct fio_fork_item *ffi;
698 ffi = malloc(sizeof(*ffi));
702 ffi->element = *element;
703 flist_add_tail(&ffi->list, list);
706 static void fio_server_add_conn_pid(struct flist_head *conn_list, HANDLE hProcess)
708 struct ffi_element element = {.hProcess = hProcess, .is_thread=FALSE};
709 dprint(FD_NET, "server: forked off connection job (tid=%u)\n", (int) element.thread);
711 fio_server_add_fork_item(&element, conn_list);
714 static void fio_server_add_job_pid(struct flist_head *job_list, pthread_t thread)
716 struct ffi_element element = {.thread = thread, .is_thread=TRUE};
717 dprint(FD_NET, "server: forked off job job (tid=%u)\n", (int) element.thread);
718 fio_server_add_fork_item(&element, job_list);
721 static void fio_server_check_fork_item(struct fio_fork_item *ffi)
725 if (ffi->element.is_thread) {
727 ret = pthread_kill(ffi->element.thread, 0);
730 pthread_join(ffi->element.thread, (void**) &rev_val); /*if the thread is dead, then join it to get status*/
732 ffi->exitval = rev_val;
734 log_err("thread (tid=%u) exited with %x\n", (int) ffi->element.thread, (int) ffi->exitval);
735 dprint(FD_PROCESS, "thread (tid=%u) exited with %x\n", (int) ffi->element.thread, (int) ffi->exitval);
740 GetExitCodeProcess(ffi->element.hProcess, &exit_val);
742 if (exit_val != STILL_ACTIVE) {
743 dprint(FD_PROCESS, "process %u exited with %d\n", GetProcessId(ffi->element.hProcess), exit_val);
745 ffi->exitval = exit_val;
750 static void fio_server_add_fork_item(pid_t pid, struct flist_head *list)
752 struct fio_fork_item *ffi;
754 ffi = malloc(sizeof(*ffi));
759 flist_add_tail(&ffi->list, list);
762 static void fio_server_add_conn_pid(struct flist_head *conn_list, pid_t pid)
764 dprint(FD_NET, "server: forked off connection job (pid=%u)\n", (int) pid);
765 fio_server_add_fork_item(pid, conn_list);
768 static void fio_server_add_job_pid(struct flist_head *job_list, pid_t pid)
770 dprint(FD_NET, "server: forked off job job (pid=%u)\n", (int) pid);
771 fio_server_add_fork_item(pid, job_list);
774 static void fio_server_check_fork_item(struct fio_fork_item *ffi)
778 ret = waitpid(ffi->pid, &status, WNOHANG);
780 if (errno == ECHILD) {
781 log_err("fio: connection pid %u disappeared\n", (int) ffi->pid);
784 log_err("fio: waitpid: %s\n", strerror(errno));
785 } else if (ret == ffi->pid) {
786 if (WIFSIGNALED(status)) {
787 ffi->signal = WTERMSIG(status);
790 if (WIFEXITED(status)) {
791 if (WEXITSTATUS(status))
792 ffi->exitval = WEXITSTATUS(status);
799 static void fio_server_fork_item_done(struct fio_fork_item *ffi, bool stop)
802 if (ffi->element.is_thread)
803 dprint(FD_NET, "tid %u exited, sig=%u, exitval=%d\n", (int) ffi->element.thread, ffi->signal, ffi->exitval);
805 dprint(FD_NET, "pid %u exited, sig=%u, exitval=%d\n", (int) GetProcessId(ffi->element.hProcess), ffi->signal, ffi->exitval);
806 CloseHandle(ffi->element.hProcess);
807 ffi->element.hProcess = INVALID_HANDLE_VALUE;
810 dprint(FD_NET, "pid %u exited, sig=%u, exitval=%d\n", (int) ffi->pid, ffi->signal, ffi->exitval);
814 * Fold STOP and QUIT...
817 fio_net_queue_stop(ffi->exitval, ffi->signal);
818 fio_net_queue_quit();
821 flist_del(&ffi->list);
825 static void fio_server_check_fork_items(struct flist_head *list, bool stop)
827 struct flist_head *entry, *tmp;
828 struct fio_fork_item *ffi;
830 flist_for_each_safe(entry, tmp, list) {
831 ffi = flist_entry(entry, struct fio_fork_item, list);
833 fio_server_check_fork_item(ffi);
836 fio_server_fork_item_done(ffi, stop);
840 static void fio_server_check_jobs(struct flist_head *job_list)
842 fio_server_check_fork_items(job_list, true);
845 static void fio_server_check_conns(struct flist_head *conn_list)
847 fio_server_check_fork_items(conn_list, false);
850 static int handle_load_file_cmd(struct fio_net_cmd *cmd)
852 struct cmd_load_file_pdu *pdu = (struct cmd_load_file_pdu *) cmd->payload;
853 void *file_name = pdu->file;
854 struct cmd_start_pdu spdu;
856 dprint(FD_NET, "server: loading local file %s\n", (char *) file_name);
858 pdu->name_len = le16_to_cpu(pdu->name_len);
859 pdu->client_type = le16_to_cpu(pdu->client_type);
861 if (parse_jobs_ini(file_name, 0, 0, pdu->client_type)) {
862 fio_net_queue_quit();
866 spdu.jobs = cpu_to_le32(thread_number);
867 spdu.stat_outputs = cpu_to_le32(stat_number);
868 fio_net_queue_cmd(FIO_NET_CMD_START, &spdu, sizeof(spdu), NULL, SK_F_COPY);
873 static void *fio_backend_thread(void *data)
876 struct sk_out *sk_out = (struct sk_out *) data;
878 sk_out_assign(sk_out);
880 ret = fio_backend(sk_out);
883 pthread_exit((void*) (intptr_t) ret);
888 static int handle_run_cmd(struct sk_out *sk_out, struct flist_head *job_list,
889 struct fio_net_cmd *cmd)
899 /* both this thread and backend_thread call sk_out_assign() to double increment
900 * the ref count. This ensures struct is valid until both threads are done with it
902 sk_out_assign(sk_out);
903 ret = pthread_create(&thread, NULL, fio_backend_thread, sk_out);
905 log_err("pthread_create: %s\n", strerror(ret));
909 fio_server_add_job_pid(job_list, thread);
915 sk_out_assign(sk_out);
918 fio_server_add_job_pid(job_list, pid);
922 ret = fio_backend(sk_out);
930 static int handle_job_cmd(struct fio_net_cmd *cmd)
932 struct cmd_job_pdu *pdu = (struct cmd_job_pdu *) cmd->payload;
933 void *buf = pdu->buf;
934 struct cmd_start_pdu spdu;
936 pdu->buf_len = le32_to_cpu(pdu->buf_len);
937 pdu->client_type = le32_to_cpu(pdu->client_type);
939 if (parse_jobs_ini(buf, 1, 0, pdu->client_type)) {
940 fio_net_queue_quit();
944 spdu.jobs = cpu_to_le32(thread_number);
945 spdu.stat_outputs = cpu_to_le32(stat_number);
947 fio_net_queue_cmd(FIO_NET_CMD_START, &spdu, sizeof(spdu), NULL, SK_F_COPY);
951 static int handle_jobline_cmd(struct fio_net_cmd *cmd)
953 void *pdu = cmd->payload;
954 struct cmd_single_line_pdu *cslp;
955 struct cmd_line_pdu *clp;
956 unsigned long offset;
957 struct cmd_start_pdu spdu;
962 clp->lines = le16_to_cpu(clp->lines);
963 clp->client_type = le16_to_cpu(clp->client_type);
964 argv = malloc(clp->lines * sizeof(char *));
965 offset = sizeof(*clp);
967 dprint(FD_NET, "server: %d command line args\n", clp->lines);
969 for (i = 0; i < clp->lines; i++) {
971 argv[i] = (char *) cslp->text;
973 offset += sizeof(*cslp) + le16_to_cpu(cslp->len);
974 dprint(FD_NET, "server: %d: %s\n", i, argv[i]);
977 if (parse_cmd_line(clp->lines, argv, clp->client_type)) {
978 fio_net_queue_quit();
985 spdu.jobs = cpu_to_le32(thread_number);
986 spdu.stat_outputs = cpu_to_le32(stat_number);
988 fio_net_queue_cmd(FIO_NET_CMD_START, &spdu, sizeof(spdu), NULL, SK_F_COPY);
992 static int handle_probe_cmd(struct fio_net_cmd *cmd)
994 struct cmd_client_probe_pdu *pdu = (struct cmd_client_probe_pdu *) cmd->payload;
995 uint64_t tag = cmd->tag;
996 struct cmd_probe_reply_pdu probe = {
997 #ifdef CONFIG_BIG_ENDIAN
1002 .bpp = sizeof(void *),
1003 .cpus = __cpu_to_le32(cpus_configured()),
1006 dprint(FD_NET, "server: sending probe reply\n");
1008 strcpy(me, (char *) pdu->server);
1010 gethostname((char *) probe.hostname, sizeof(probe.hostname));
1011 snprintf((char *) probe.fio_version, sizeof(probe.fio_version), "%s",
1012 fio_version_string);
1015 * If the client supports compression and we do too, then enable it
1017 if (has_zlib && le64_to_cpu(pdu->flags) & FIO_PROBE_FLAG_ZLIB) {
1018 probe.flags = __cpu_to_le64(FIO_PROBE_FLAG_ZLIB);
1025 return fio_net_queue_cmd(FIO_NET_CMD_PROBE, &probe, sizeof(probe), &tag, SK_F_COPY);
1028 static int handle_send_eta_cmd(struct fio_net_cmd *cmd)
1030 struct jobs_eta *je;
1031 uint64_t tag = cmd->tag;
1035 dprint(FD_NET, "server sending status\n");
1038 * Fake ETA return if we don't have a local one, otherwise the client
1039 * will end up timing out waiting for a response to the ETA request
1041 je = get_jobs_eta(true, &size);
1044 je = calloc(1, size);
1046 je->nr_running = cpu_to_le32(je->nr_running);
1047 je->nr_ramp = cpu_to_le32(je->nr_ramp);
1048 je->nr_pending = cpu_to_le32(je->nr_pending);
1049 je->nr_setting_up = cpu_to_le32(je->nr_setting_up);
1050 je->files_open = cpu_to_le32(je->files_open);
1052 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1053 je->m_rate[i] = cpu_to_le64(je->m_rate[i]);
1054 je->t_rate[i] = cpu_to_le64(je->t_rate[i]);
1055 je->m_iops[i] = cpu_to_le32(je->m_iops[i]);
1056 je->t_iops[i] = cpu_to_le32(je->t_iops[i]);
1057 je->rate[i] = cpu_to_le64(je->rate[i]);
1058 je->iops[i] = cpu_to_le32(je->iops[i]);
1061 je->elapsed_sec = cpu_to_le64(je->elapsed_sec);
1062 je->eta_sec = cpu_to_le64(je->eta_sec);
1063 je->nr_threads = cpu_to_le32(je->nr_threads);
1064 je->is_pow2 = cpu_to_le32(je->is_pow2);
1065 je->unit_base = cpu_to_le32(je->unit_base);
1068 fio_net_queue_cmd(FIO_NET_CMD_ETA, je, size, &tag, SK_F_FREE);
1072 static int send_update_job_reply(uint64_t __tag, int error)
1074 uint64_t tag = __tag;
1077 pdu_error = __cpu_to_le32(error);
1078 return fio_net_queue_cmd(FIO_NET_CMD_UPDATE_JOB, &pdu_error, sizeof(pdu_error), &tag, SK_F_COPY);
1081 static int handle_update_job_cmd(struct fio_net_cmd *cmd)
1083 struct cmd_add_job_pdu *pdu = (struct cmd_add_job_pdu *) cmd->payload;
1084 struct thread_data *td;
1088 tnumber = le32_to_cpu(pdu->thread_number);
1090 dprint(FD_NET, "server: updating options for job %u\n", tnumber);
1092 if (!tnumber || tnumber > thread_number) {
1093 send_update_job_reply(cmd->tag, ENODEV);
1097 td = tnumber_to_td(tnumber);
1098 ret = convert_thread_options_to_cpu(&td->o, &pdu->top,
1099 cmd->pdu_len - offsetof(struct cmd_add_job_pdu, top));
1100 send_update_job_reply(cmd->tag, ret);
1104 static int handle_trigger_cmd(struct fio_net_cmd *cmd, struct flist_head *job_list)
1106 struct cmd_vtrigger_pdu *pdu = (struct cmd_vtrigger_pdu *) cmd->payload;
1107 char *buf = (char *) pdu->cmd;
1108 struct all_io_list *rep;
1111 pdu->len = le16_to_cpu(pdu->len);
1112 buf[pdu->len] = '\0';
1114 rep = get_all_io_list(IO_LIST_ALL, &sz);
1116 struct all_io_list state;
1118 state.threads = cpu_to_le64((uint64_t) 0);
1119 fio_net_queue_cmd(FIO_NET_CMD_VTRIGGER, &state, sizeof(state), NULL, SK_F_COPY | SK_F_INLINE);
1121 fio_net_queue_cmd(FIO_NET_CMD_VTRIGGER, rep, sz, NULL, SK_F_FREE | SK_F_INLINE);
1123 fio_terminate_threads(TERMINATE_ALL, TERMINATE_ALL);
1124 fio_server_check_jobs(job_list);
1129 static int handle_command(struct sk_out *sk_out, struct flist_head *job_list,
1130 struct fio_net_cmd *cmd)
1134 dprint(FD_NET, "server: got op [%s], pdu=%u, tag=%llx\n",
1135 fio_server_op(cmd->opcode), cmd->pdu_len,
1136 (unsigned long long) cmd->tag);
1138 switch (cmd->opcode) {
1139 case FIO_NET_CMD_QUIT:
1140 fio_terminate_threads(TERMINATE_ALL, TERMINATE_ALL);
1143 case FIO_NET_CMD_EXIT:
1144 exit_backend = true;
1146 case FIO_NET_CMD_LOAD_FILE:
1147 ret = handle_load_file_cmd(cmd);
1149 case FIO_NET_CMD_JOB:
1150 ret = handle_job_cmd(cmd);
1152 case FIO_NET_CMD_JOBLINE:
1153 ret = handle_jobline_cmd(cmd);
1155 case FIO_NET_CMD_PROBE:
1156 ret = handle_probe_cmd(cmd);
1158 case FIO_NET_CMD_SEND_ETA:
1159 ret = handle_send_eta_cmd(cmd);
1161 case FIO_NET_CMD_RUN:
1162 ret = handle_run_cmd(sk_out, job_list, cmd);
1164 case FIO_NET_CMD_UPDATE_JOB:
1165 ret = handle_update_job_cmd(cmd);
1167 case FIO_NET_CMD_VTRIGGER:
1168 ret = handle_trigger_cmd(cmd, job_list);
1170 case FIO_NET_CMD_SENDFILE: {
1171 struct cmd_sendfile_reply *in;
1172 struct cmd_reply *rep;
1174 rep = (struct cmd_reply *) (uintptr_t) cmd->tag;
1176 in = (struct cmd_sendfile_reply *) cmd->payload;
1177 in->size = le32_to_cpu(in->size);
1178 in->error = le32_to_cpu(in->error);
1181 rep->error = in->error;
1184 rep->data = smalloc(in->size);
1187 rep->error = ENOMEM;
1189 rep->size = in->size;
1190 memcpy(rep->data, in->data, in->size);
1193 fio_sem_up(&rep->lock);
1197 log_err("fio: unknown opcode: %s\n", fio_server_op(cmd->opcode));
1205 * Send a command with a separate PDU, not inlined in the command
1207 static int fio_send_cmd_ext_pdu(int sk, uint16_t opcode, const void *buf,
1208 off_t size, uint64_t tag, uint32_t flags)
1210 struct fio_net_cmd cmd;
1211 struct iovec iov[2];
1215 iov[0].iov_base = (void *) &cmd;
1216 iov[0].iov_len = sizeof(cmd);
1219 uint32_t this_flags = flags;
1222 if (this_len > FIO_SERVER_MAX_FRAGMENT_PDU)
1223 this_len = FIO_SERVER_MAX_FRAGMENT_PDU;
1225 if (this_len < size)
1226 this_flags |= FIO_NET_CMD_F_MORE;
1228 __fio_init_net_cmd(&cmd, opcode, this_len, tag);
1229 cmd.flags = __cpu_to_le32(this_flags);
1230 fio_net_cmd_crc_pdu(&cmd, buf);
1232 iov[1].iov_base = (void *) buf;
1233 iov[1].iov_len = this_len;
1235 ret = fio_sendv_data(sk, iov, 2);
1238 } while (!ret && size);
1243 static void finish_entry(struct sk_entry *entry)
1245 if (entry->flags & SK_F_FREE)
1247 else if (entry->flags & SK_F_COPY)
1253 static void entry_set_flags(struct sk_entry *entry, struct flist_head *list,
1254 unsigned int *flags)
1256 if (!flist_empty(list))
1257 *flags = FIO_NET_CMD_F_MORE;
1262 static int send_vec_entry(struct sk_out *sk_out, struct sk_entry *first)
1267 entry_set_flags(first, &first->next, &flags);
1269 ret = fio_send_cmd_ext_pdu(sk_out->sk, first->opcode, first->buf,
1270 first->size, first->tag, flags);
1272 while (!flist_empty(&first->next)) {
1273 struct sk_entry *next;
1275 next = flist_first_entry(&first->next, struct sk_entry, list);
1276 flist_del_init(&next->list);
1278 entry_set_flags(next, &first->next, &flags);
1280 ret += fio_send_cmd_ext_pdu(sk_out->sk, next->opcode, next->buf,
1281 next->size, next->tag, flags);
1288 static int handle_sk_entry(struct sk_out *sk_out, struct sk_entry *entry)
1292 fio_sem_down(&sk_out->xmit);
1294 if (entry->flags & SK_F_VEC)
1295 ret = send_vec_entry(sk_out, entry);
1296 else if (entry->flags & SK_F_SIMPLE) {
1297 ret = fio_net_send_simple_cmd(sk_out->sk, entry->opcode,
1300 ret = fio_net_send_cmd(sk_out->sk, entry->opcode, entry->buf,
1301 entry->size, &entry->tag, NULL);
1304 fio_sem_up(&sk_out->xmit);
1307 log_err("fio: failed handling cmd %s\n", fio_server_op(entry->opcode));
1309 finish_entry(entry);
1313 static int handle_xmits(struct sk_out *sk_out)
1315 struct sk_entry *entry;
1320 if (flist_empty(&sk_out->list)) {
1325 flist_splice_init(&sk_out->list, &list);
1328 while (!flist_empty(&list)) {
1329 entry = flist_first_entry(&list, struct sk_entry, list);
1330 flist_del(&entry->list);
1331 ret += handle_sk_entry(sk_out, entry);
1337 static int handle_connection(struct sk_out *sk_out)
1339 struct fio_net_cmd *cmd = NULL;
1340 FLIST_HEAD(job_list);
1346 while (!exit_backend) {
1347 struct pollfd pfd = {
1355 if (!flist_empty(&job_list))
1358 handle_xmits(sk_out);
1360 ret = poll(&pfd, 1, 0);
1364 log_err("fio: poll: %s\n", strerror(errno));
1367 fio_server_check_jobs(&job_list);
1368 fio_sem_down_timeout(&sk_out->wait, timeout);
1372 if (pfd.revents & POLLIN)
1374 if (pfd.revents & (POLLERR|POLLHUP)) {
1378 } while (!exit_backend);
1380 fio_server_check_jobs(&job_list);
1385 if (pfd.revents & POLLIN)
1386 cmd = fio_net_recv_cmd(sk_out->sk, true);
1392 ret = handle_command(sk_out, &job_list, cmd);
1403 handle_xmits(sk_out);
1407 __sk_out_drop(sk_out);
1411 /* get the address on this host bound by the input socket,
1412 * whether it is ipv6 or ipv4 */
1414 static int get_my_addr_str(int sk)
1416 struct sockaddr_in6 myaddr6 = { 0, };
1417 struct sockaddr_in myaddr4 = { 0, };
1418 struct sockaddr *sockaddr_p;
1424 len = sizeof(myaddr6);
1425 sockaddr_p = (struct sockaddr * )&myaddr6;
1426 net_addr = (char * )&myaddr6.sin6_addr;
1428 len = sizeof(myaddr4);
1429 sockaddr_p = (struct sockaddr * )&myaddr4;
1430 net_addr = (char * )&myaddr4.sin_addr;
1433 ret = getsockname(sk, sockaddr_p, &len);
1435 log_err("fio: getsockname: %s\n", strerror(errno));
1439 if (!inet_ntop(use_ipv6?AF_INET6:AF_INET, net_addr, client_sockaddr_str, INET6_ADDRSTRLEN - 1)) {
1440 log_err("inet_ntop: failed to convert addr to string\n");
1444 dprint(FD_NET, "fio server bound to addr %s\n", client_sockaddr_str);
1449 static int handle_connection_process(void)
1451 WSAPROTOCOL_INFO protocol_info;
1455 struct sk_out *sk_out;
1457 char *msg = (char *) "connected";
1459 log_info("server enter accept loop. ProcessID %d\n", GetCurrentProcessId());
1462 fio_server_pipe_name,
1463 GENERIC_READ | GENERIC_WRITE,
1468 if (hpipe == INVALID_HANDLE_VALUE) {
1469 log_err("couldnt open pipe %s error %lu\n",
1470 fio_server_pipe_name, GetLastError());
1474 if (!ReadFile(hpipe, &protocol_info, sizeof(protocol_info), &bytes_read, NULL)) {
1475 log_err("couldnt read pi from pipe %s error %lu\n", fio_server_pipe_name,
1479 if (use_ipv6) /* use protocol_info to create a duplicate of parents socket */
1480 sk = WSASocket(AF_INET6, SOCK_STREAM, 0, &protocol_info, 0, 0);
1482 sk = WSASocket(AF_INET, SOCK_STREAM, 0, &protocol_info, 0, 0);
1484 sk_out = scalloc(1, sizeof(*sk_out));
1492 sk_out->hProcess = INVALID_HANDLE_VALUE;
1493 INIT_FLIST_HEAD(&sk_out->list);
1494 __fio_sem_init(&sk_out->lock, FIO_SEM_UNLOCKED);
1495 __fio_sem_init(&sk_out->wait, FIO_SEM_LOCKED);
1496 __fio_sem_init(&sk_out->xmit, FIO_SEM_UNLOCKED);
1498 get_my_addr_str(sk);
1500 if (!WriteFile(hpipe, msg, strlen(msg), NULL, NULL)) {
1501 log_err("couldnt write pipe\n");
1507 sk_out_assign(sk_out);
1509 ret = handle_connection(sk_out);
1510 __sk_out_drop(sk_out);
1515 static int accept_loop(int listen_sk)
1517 struct sockaddr_in addr;
1518 struct sockaddr_in6 addr6;
1519 socklen_t len = use_ipv6 ? sizeof(addr6) : sizeof(addr);
1521 int ret = 0, sk, exitval = 0;
1522 FLIST_HEAD(conn_list);
1524 dprint(FD_NET, "server enter accept loop\n");
1526 fio_set_fd_nonblocking(listen_sk, "server");
1528 while (!exit_backend) {
1529 struct sk_out *sk_out;
1538 pfd.events = POLLIN;
1542 if (!flist_empty(&conn_list))
1545 ret = poll(&pfd, 1, timeout);
1549 log_err("fio: poll: %s\n", strerror(errno));
1552 fio_server_check_conns(&conn_list);
1556 if (pfd.revents & POLLIN)
1558 } while (!exit_backend);
1560 fio_server_check_conns(&conn_list);
1562 if (exit_backend || ret < 0)
1566 sk = accept(listen_sk, (struct sockaddr *) &addr6, &len);
1568 sk = accept(listen_sk, (struct sockaddr *) &addr, &len);
1571 log_err("fio: accept: %s\n", strerror(errno));
1576 from = inet_ntop(AF_INET6, (struct sockaddr *) &addr6.sin6_addr, buf, sizeof(buf));
1578 from = inet_ntop(AF_INET, (struct sockaddr *) &addr.sin_addr, buf, sizeof(buf));
1580 dprint(FD_NET, "server: connect from %s\n", from);
1582 sk_out = scalloc(1, sizeof(*sk_out));
1589 INIT_FLIST_HEAD(&sk_out->list);
1590 __fio_sem_init(&sk_out->lock, FIO_SEM_UNLOCKED);
1591 __fio_sem_init(&sk_out->wait, FIO_SEM_LOCKED);
1592 __fio_sem_init(&sk_out->xmit, FIO_SEM_UNLOCKED);
1595 hProcess = windows_handle_connection(hjob, sk);
1596 if (hProcess == INVALID_HANDLE_VALUE)
1598 sk_out->hProcess = hProcess;
1599 fio_server_add_conn_pid(&conn_list, hProcess);
1604 fio_server_add_conn_pid(&conn_list, pid);
1608 /* if error, it's already logged, non-fatal */
1609 get_my_addr_str(sk);
1612 * Assign sk_out here, it'll be dropped in handle_connection()
1613 * since that function calls _exit() when done
1615 sk_out_assign(sk_out);
1616 handle_connection(sk_out);
1623 int fio_server_text_output(int level, const char *buf, size_t len)
1625 struct sk_out *sk_out = pthread_getspecific(sk_out_key);
1626 struct cmd_text_pdu *pdu;
1630 if (!sk_out || sk_out->sk == -1)
1633 tlen = sizeof(*pdu) + len;
1636 pdu->level = __cpu_to_le32(level);
1637 pdu->buf_len = __cpu_to_le32(len);
1639 gettimeofday(&tv, NULL);
1640 pdu->log_sec = __cpu_to_le64(tv.tv_sec);
1641 pdu->log_usec = __cpu_to_le64(tv.tv_usec);
1643 memcpy(pdu->buf, buf, len);
1645 fio_net_queue_cmd(FIO_NET_CMD_TEXT, pdu, tlen, NULL, SK_F_COPY);
1650 static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
1652 dst->max_val = cpu_to_le64(src->max_val);
1653 dst->min_val = cpu_to_le64(src->min_val);
1654 dst->samples = cpu_to_le64(src->samples);
1657 * Encode to IEEE 754 for network transfer
1659 dst->mean.u.i = cpu_to_le64(fio_double_to_uint64(src->mean.u.f));
1660 dst->S.u.i = cpu_to_le64(fio_double_to_uint64(src->S.u.f));
1663 static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
1667 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1668 dst->max_run[i] = cpu_to_le64(src->max_run[i]);
1669 dst->min_run[i] = cpu_to_le64(src->min_run[i]);
1670 dst->max_bw[i] = cpu_to_le64(src->max_bw[i]);
1671 dst->min_bw[i] = cpu_to_le64(src->min_bw[i]);
1672 dst->iobytes[i] = cpu_to_le64(src->iobytes[i]);
1673 dst->agg[i] = cpu_to_le64(src->agg[i]);
1676 dst->kb_base = cpu_to_le32(src->kb_base);
1677 dst->unit_base = cpu_to_le32(src->unit_base);
1678 dst->groupid = cpu_to_le32(src->groupid);
1679 dst->unified_rw_rep = cpu_to_le32(src->unified_rw_rep);
1680 dst->sig_figs = cpu_to_le32(src->sig_figs);
1684 * Send a CMD_TS, which packs struct thread_stat and group_run_stats
1685 * into a single payload.
1687 void fio_server_send_ts(struct thread_stat *ts, struct group_run_stats *rs)
1689 struct cmd_ts_pdu p;
1691 size_t clat_prio_stats_extra_size = 0;
1692 size_t ss_extra_size = 0;
1693 size_t extended_buf_size = 0;
1695 void *extended_buf_wp;
1697 dprint(FD_NET, "server sending end stats\n");
1699 memset(&p, 0, sizeof(p));
1701 snprintf(p.ts.name, sizeof(p.ts.name), "%s", ts->name);
1702 snprintf(p.ts.verror, sizeof(p.ts.verror), "%s", ts->verror);
1703 snprintf(p.ts.description, sizeof(p.ts.description), "%s",
1706 p.ts.error = cpu_to_le32(ts->error);
1707 p.ts.thread_number = cpu_to_le32(ts->thread_number);
1708 p.ts.groupid = cpu_to_le32(ts->groupid);
1709 p.ts.job_start = cpu_to_le64(ts->job_start);
1710 p.ts.pid = cpu_to_le32(ts->pid);
1711 p.ts.members = cpu_to_le32(ts->members);
1712 p.ts.unified_rw_rep = cpu_to_le32(ts->unified_rw_rep);
1713 p.ts.ioprio = cpu_to_le32(ts->ioprio);
1714 p.ts.disable_prio_stat = cpu_to_le32(ts->disable_prio_stat);
1716 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1717 convert_io_stat(&p.ts.clat_stat[i], &ts->clat_stat[i]);
1718 convert_io_stat(&p.ts.slat_stat[i], &ts->slat_stat[i]);
1719 convert_io_stat(&p.ts.lat_stat[i], &ts->lat_stat[i]);
1720 convert_io_stat(&p.ts.bw_stat[i], &ts->bw_stat[i]);
1721 convert_io_stat(&p.ts.iops_stat[i], &ts->iops_stat[i]);
1723 convert_io_stat(&p.ts.sync_stat, &ts->sync_stat);
1725 p.ts.usr_time = cpu_to_le64(ts->usr_time);
1726 p.ts.sys_time = cpu_to_le64(ts->sys_time);
1727 p.ts.ctx = cpu_to_le64(ts->ctx);
1728 p.ts.minf = cpu_to_le64(ts->minf);
1729 p.ts.majf = cpu_to_le64(ts->majf);
1730 p.ts.clat_percentiles = cpu_to_le32(ts->clat_percentiles);
1731 p.ts.lat_percentiles = cpu_to_le32(ts->lat_percentiles);
1732 p.ts.slat_percentiles = cpu_to_le32(ts->slat_percentiles);
1733 p.ts.percentile_precision = cpu_to_le64(ts->percentile_precision);
1735 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
1736 fio_fp64_t *src = &ts->percentile_list[i];
1737 fio_fp64_t *dst = &p.ts.percentile_list[i];
1739 dst->u.i = cpu_to_le64(fio_double_to_uint64(src->u.f));
1742 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
1743 p.ts.io_u_map[i] = cpu_to_le64(ts->io_u_map[i]);
1744 p.ts.io_u_submit[i] = cpu_to_le64(ts->io_u_submit[i]);
1745 p.ts.io_u_complete[i] = cpu_to_le64(ts->io_u_complete[i]);
1748 for (i = 0; i < FIO_IO_U_LAT_N_NR; i++)
1749 p.ts.io_u_lat_n[i] = cpu_to_le64(ts->io_u_lat_n[i]);
1750 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++)
1751 p.ts.io_u_lat_u[i] = cpu_to_le64(ts->io_u_lat_u[i]);
1752 for (i = 0; i < FIO_IO_U_LAT_M_NR; i++)
1753 p.ts.io_u_lat_m[i] = cpu_to_le64(ts->io_u_lat_m[i]);
1755 for (i = 0; i < FIO_LAT_CNT; i++)
1756 for (j = 0; j < DDIR_RWDIR_CNT; j++)
1757 for (k = 0; k < FIO_IO_U_PLAT_NR; k++)
1758 p.ts.io_u_plat[i][j][k] = cpu_to_le64(ts->io_u_plat[i][j][k]);
1760 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
1761 p.ts.io_u_sync_plat[j] = cpu_to_le64(ts->io_u_sync_plat[j]);
1763 for (i = 0; i < DDIR_RWDIR_SYNC_CNT; i++)
1764 p.ts.total_io_u[i] = cpu_to_le64(ts->total_io_u[i]);
1766 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1767 p.ts.short_io_u[i] = cpu_to_le64(ts->short_io_u[i]);
1768 p.ts.drop_io_u[i] = cpu_to_le64(ts->drop_io_u[i]);
1771 p.ts.total_submit = cpu_to_le64(ts->total_submit);
1772 p.ts.total_complete = cpu_to_le64(ts->total_complete);
1773 p.ts.nr_zone_resets = cpu_to_le64(ts->nr_zone_resets);
1775 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1776 p.ts.io_bytes[i] = cpu_to_le64(ts->io_bytes[i]);
1777 p.ts.runtime[i] = cpu_to_le64(ts->runtime[i]);
1780 p.ts.total_run_time = cpu_to_le64(ts->total_run_time);
1781 p.ts.continue_on_error = cpu_to_le16(ts->continue_on_error);
1782 p.ts.total_err_count = cpu_to_le64(ts->total_err_count);
1783 p.ts.first_error = cpu_to_le32(ts->first_error);
1784 p.ts.kb_base = cpu_to_le32(ts->kb_base);
1785 p.ts.unit_base = cpu_to_le32(ts->unit_base);
1787 p.ts.latency_depth = cpu_to_le32(ts->latency_depth);
1788 p.ts.latency_target = cpu_to_le64(ts->latency_target);
1789 p.ts.latency_window = cpu_to_le64(ts->latency_window);
1790 p.ts.latency_percentile.u.i = cpu_to_le64(fio_double_to_uint64(ts->latency_percentile.u.f));
1792 p.ts.sig_figs = cpu_to_le32(ts->sig_figs);
1794 p.ts.nr_block_infos = cpu_to_le64(ts->nr_block_infos);
1795 for (i = 0; i < p.ts.nr_block_infos; i++)
1796 p.ts.block_infos[i] = cpu_to_le32(ts->block_infos[i]);
1798 p.ts.ss_dur = cpu_to_le64(ts->ss_dur);
1799 p.ts.ss_state = cpu_to_le32(ts->ss_state);
1800 p.ts.ss_head = cpu_to_le32(ts->ss_head);
1801 p.ts.ss_limit.u.i = cpu_to_le64(fio_double_to_uint64(ts->ss_limit.u.f));
1802 p.ts.ss_slope.u.i = cpu_to_le64(fio_double_to_uint64(ts->ss_slope.u.f));
1803 p.ts.ss_deviation.u.i = cpu_to_le64(fio_double_to_uint64(ts->ss_deviation.u.f));
1804 p.ts.ss_criterion.u.i = cpu_to_le64(fio_double_to_uint64(ts->ss_criterion.u.f));
1806 p.ts.cachehit = cpu_to_le64(ts->cachehit);
1807 p.ts.cachemiss = cpu_to_le64(ts->cachemiss);
1809 convert_gs(&p.rs, rs);
1811 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1812 if (ts->nr_clat_prio[i])
1813 clat_prio_stats_extra_size += ts->nr_clat_prio[i] * sizeof(*ts->clat_prio[i]);
1815 extended_buf_size += clat_prio_stats_extra_size;
1817 dprint(FD_NET, "ts->ss_state = %d\n", ts->ss_state);
1818 if (ts->ss_state & FIO_SS_DATA)
1819 ss_extra_size = 2 * ts->ss_dur * sizeof(uint64_t);
1821 extended_buf_size += ss_extra_size;
1822 if (!extended_buf_size) {
1823 fio_net_queue_cmd(FIO_NET_CMD_TS, &p, sizeof(p), NULL, SK_F_COPY);
1827 extended_buf_size += sizeof(p);
1828 extended_buf = calloc(1, extended_buf_size);
1829 if (!extended_buf) {
1830 log_err("fio: failed to allocate FIO_NET_CMD_TS buffer\n");
1834 memcpy(extended_buf, &p, sizeof(p));
1835 extended_buf_wp = (struct cmd_ts_pdu *)extended_buf + 1;
1837 if (clat_prio_stats_extra_size) {
1838 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1839 struct clat_prio_stat *prio = (struct clat_prio_stat *) extended_buf_wp;
1841 for (j = 0; j < ts->nr_clat_prio[i]; j++) {
1842 for (k = 0; k < FIO_IO_U_PLAT_NR; k++)
1843 prio->io_u_plat[k] =
1844 cpu_to_le64(ts->clat_prio[i][j].io_u_plat[k]);
1845 convert_io_stat(&prio->clat_stat,
1846 &ts->clat_prio[i][j].clat_stat);
1847 prio->ioprio = cpu_to_le32(ts->clat_prio[i][j].ioprio);
1851 if (ts->nr_clat_prio[i]) {
1852 uint64_t offset = (char *)extended_buf_wp - (char *)extended_buf;
1853 struct cmd_ts_pdu *ptr = extended_buf;
1855 ptr->ts.clat_prio_offset[i] = cpu_to_le64(offset);
1856 ptr->ts.nr_clat_prio[i] = cpu_to_le32(ts->nr_clat_prio[i]);
1859 extended_buf_wp = prio;
1863 if (ss_extra_size) {
1864 uint64_t *ss_iops, *ss_bw;
1866 struct cmd_ts_pdu *ptr = extended_buf;
1868 dprint(FD_NET, "server sending steadystate ring buffers\n");
1871 ss_iops = (uint64_t *) extended_buf_wp;
1872 for (i = 0; i < ts->ss_dur; i++)
1873 ss_iops[i] = cpu_to_le64(ts->ss_iops_data[i]);
1875 offset = (char *)extended_buf_wp - (char *)extended_buf;
1876 ptr->ts.ss_iops_data_offset = cpu_to_le64(offset);
1877 extended_buf_wp = ss_iops + (int) ts->ss_dur;
1880 ss_bw = extended_buf_wp;
1881 for (i = 0; i < ts->ss_dur; i++)
1882 ss_bw[i] = cpu_to_le64(ts->ss_bw_data[i]);
1884 offset = (char *)extended_buf_wp - (char *)extended_buf;
1885 ptr->ts.ss_bw_data_offset = cpu_to_le64(offset);
1888 fio_net_queue_cmd(FIO_NET_CMD_TS, extended_buf, extended_buf_size, NULL, SK_F_COPY);
1892 void fio_server_send_gs(struct group_run_stats *rs)
1894 struct group_run_stats gs;
1896 dprint(FD_NET, "server sending group run stats\n");
1898 convert_gs(&gs, rs);
1899 fio_net_queue_cmd(FIO_NET_CMD_GS, &gs, sizeof(gs), NULL, SK_F_COPY);
1902 void fio_server_send_job_options(struct flist_head *opt_list,
1905 struct cmd_job_option pdu;
1906 struct flist_head *entry;
1908 if (flist_empty(opt_list))
1911 flist_for_each(entry, opt_list) {
1912 struct print_option *p;
1915 p = flist_entry(entry, struct print_option, list);
1916 memset(&pdu, 0, sizeof(pdu));
1919 pdu.global = __cpu_to_le16(1);
1923 pdu.groupid = cpu_to_le32(gid);
1925 len = strlen(p->name);
1926 if (len >= sizeof(pdu.name)) {
1927 len = sizeof(pdu.name) - 1;
1928 pdu.truncated = __cpu_to_le16(1);
1930 memcpy(pdu.name, p->name, len);
1932 len = strlen(p->value);
1933 if (len >= sizeof(pdu.value)) {
1934 len = sizeof(pdu.value) - 1;
1935 pdu.truncated = __cpu_to_le16(1);
1937 memcpy(pdu.value, p->value, len);
1939 fio_net_queue_cmd(FIO_NET_CMD_JOB_OPT, &pdu, sizeof(pdu), NULL, SK_F_COPY);
1943 static void convert_agg(struct disk_util_agg *dst, struct disk_util_agg *src)
1947 for (i = 0; i < 2; i++) {
1948 dst->ios[i] = cpu_to_le64(src->ios[i]);
1949 dst->merges[i] = cpu_to_le64(src->merges[i]);
1950 dst->sectors[i] = cpu_to_le64(src->sectors[i]);
1951 dst->ticks[i] = cpu_to_le64(src->ticks[i]);
1954 dst->io_ticks = cpu_to_le64(src->io_ticks);
1955 dst->time_in_queue = cpu_to_le64(src->time_in_queue);
1956 dst->slavecount = cpu_to_le32(src->slavecount);
1957 dst->max_util.u.i = cpu_to_le64(fio_double_to_uint64(src->max_util.u.f));
1960 static void convert_dus(struct disk_util_stat *dst, struct disk_util_stat *src)
1964 snprintf((char *) dst->name, sizeof(dst->name), "%s", src->name);
1966 for (i = 0; i < 2; i++) {
1967 dst->s.ios[i] = cpu_to_le64(src->s.ios[i]);
1968 dst->s.merges[i] = cpu_to_le64(src->s.merges[i]);
1969 dst->s.sectors[i] = cpu_to_le64(src->s.sectors[i]);
1970 dst->s.ticks[i] = cpu_to_le64(src->s.ticks[i]);
1973 dst->s.io_ticks = cpu_to_le64(src->s.io_ticks);
1974 dst->s.time_in_queue = cpu_to_le64(src->s.time_in_queue);
1975 dst->s.msec = cpu_to_le64(src->s.msec);
1978 void fio_server_send_du(void)
1980 struct disk_util *du;
1981 struct flist_head *entry;
1982 struct cmd_du_pdu pdu;
1984 dprint(FD_NET, "server: sending disk_util %d\n", !flist_empty(&disk_list));
1986 memset(&pdu, 0, sizeof(pdu));
1988 flist_for_each(entry, &disk_list) {
1989 du = flist_entry(entry, struct disk_util, list);
1991 convert_dus(&pdu.dus, &du->dus);
1992 convert_agg(&pdu.agg, &du->agg);
1994 fio_net_queue_cmd(FIO_NET_CMD_DU, &pdu, sizeof(pdu), NULL, SK_F_COPY);
2000 static inline void __fio_net_prep_tail(z_stream *stream, void *out_pdu,
2001 struct sk_entry **last_entry,
2002 struct sk_entry *first)
2004 unsigned int this_len = FIO_SERVER_MAX_FRAGMENT_PDU - stream->avail_out;
2006 *last_entry = fio_net_prep_cmd(FIO_NET_CMD_IOLOG, out_pdu, this_len,
2007 NULL, SK_F_VEC | SK_F_INLINE | SK_F_FREE);
2009 flist_add_tail(&(*last_entry)->list, &first->next);
2013 * Deflates the next input given, creating as many new packets in the
2014 * linked list as necessary.
2016 static int __deflate_pdu_buffer(void *next_in, unsigned int next_sz, void **out_pdu,
2017 struct sk_entry **last_entry, z_stream *stream,
2018 struct sk_entry *first)
2022 stream->next_in = next_in;
2023 stream->avail_in = next_sz;
2025 if (!stream->avail_out) {
2026 __fio_net_prep_tail(stream, *out_pdu, last_entry, first);
2027 if (*last_entry == NULL)
2030 *out_pdu = malloc(FIO_SERVER_MAX_FRAGMENT_PDU);
2032 stream->avail_out = FIO_SERVER_MAX_FRAGMENT_PDU;
2033 stream->next_out = *out_pdu;
2036 ret = deflate(stream, Z_BLOCK);
2042 } while (stream->avail_in);
2047 static int __fio_append_iolog_gz_hist(struct sk_entry *first, struct io_log *log,
2048 struct io_logs *cur_log, z_stream *stream)
2050 struct sk_entry *entry;
2053 int sample_sz = log_entry_sz(log);
2055 out_pdu = malloc(FIO_SERVER_MAX_FRAGMENT_PDU);
2056 stream->avail_out = FIO_SERVER_MAX_FRAGMENT_PDU;
2057 stream->next_out = out_pdu;
2059 for (i = 0; i < cur_log->nr_samples; i++) {
2060 struct io_sample *s;
2061 struct io_u_plat_entry *cur_plat_entry, *prev_plat_entry;
2062 uint64_t *cur_plat, *prev_plat;
2064 s = get_sample(log, cur_log, i);
2065 ret = __deflate_pdu_buffer(s, sample_sz, &out_pdu, &entry, stream, first);
2069 /* Do the subtraction on server side so that client doesn't have to
2070 * reconstruct our linked list from packets.
2072 cur_plat_entry = s->data.plat_entry;
2073 prev_plat_entry = flist_first_entry(&cur_plat_entry->list, struct io_u_plat_entry, list);
2074 cur_plat = cur_plat_entry->io_u_plat;
2075 prev_plat = prev_plat_entry->io_u_plat;
2077 for (j = 0; j < FIO_IO_U_PLAT_NR; j++) {
2078 cur_plat[j] -= prev_plat[j];
2081 flist_del(&prev_plat_entry->list);
2082 free(prev_plat_entry);
2084 ret = __deflate_pdu_buffer(cur_plat_entry, sizeof(*cur_plat_entry),
2085 &out_pdu, &entry, stream, first);
2091 __fio_net_prep_tail(stream, out_pdu, &entry, first);
2092 return entry == NULL;
2095 static int __fio_append_iolog_gz(struct sk_entry *first, struct io_log *log,
2096 struct io_logs *cur_log, z_stream *stream)
2098 unsigned int this_len;
2102 if (log->log_type == IO_LOG_TYPE_HIST)
2103 return __fio_append_iolog_gz_hist(first, log, cur_log, stream);
2105 stream->next_in = (void *) cur_log->log;
2106 stream->avail_in = cur_log->nr_samples * log_entry_sz(log);
2109 struct sk_entry *entry;
2112 * Dirty - since the log is potentially huge, compress it into
2113 * FIO_SERVER_MAX_FRAGMENT_PDU chunks and let the receiving
2114 * side defragment it.
2116 out_pdu = malloc(FIO_SERVER_MAX_FRAGMENT_PDU);
2118 stream->avail_out = FIO_SERVER_MAX_FRAGMENT_PDU;
2119 stream->next_out = out_pdu;
2120 ret = deflate(stream, Z_BLOCK);
2121 /* may be Z_OK, or Z_STREAM_END */
2127 this_len = FIO_SERVER_MAX_FRAGMENT_PDU - stream->avail_out;
2129 entry = fio_net_prep_cmd(FIO_NET_CMD_IOLOG, out_pdu, this_len,
2130 NULL, SK_F_VEC | SK_F_INLINE | SK_F_FREE);
2135 flist_add_tail(&entry->list, &first->next);
2136 } while (stream->avail_in);
2141 static int fio_append_iolog_gz(struct sk_entry *first, struct io_log *log)
2150 if (deflateInit(&stream, Z_DEFAULT_COMPRESSION) != Z_OK)
2153 while (!flist_empty(&log->io_logs)) {
2154 struct io_logs *cur_log;
2156 cur_log = flist_first_entry(&log->io_logs, struct io_logs, list);
2157 flist_del_init(&cur_log->list);
2159 ret = __fio_append_iolog_gz(first, log, cur_log, &stream);
2164 ret = deflate(&stream, Z_FINISH);
2166 while (ret != Z_STREAM_END) {
2167 struct sk_entry *entry;
2168 unsigned int this_len;
2171 out_pdu = malloc(FIO_SERVER_MAX_FRAGMENT_PDU);
2172 stream.avail_out = FIO_SERVER_MAX_FRAGMENT_PDU;
2173 stream.next_out = out_pdu;
2175 ret = deflate(&stream, Z_FINISH);
2176 /* may be Z_OK, or Z_STREAM_END */
2182 this_len = FIO_SERVER_MAX_FRAGMENT_PDU - stream.avail_out;
2184 entry = fio_net_prep_cmd(FIO_NET_CMD_IOLOG, out_pdu, this_len,
2185 NULL, SK_F_VEC | SK_F_INLINE | SK_F_FREE);
2190 flist_add_tail(&entry->list, &first->next);
2193 ret = deflateEnd(&stream);
2200 static int fio_append_iolog_gz(struct sk_entry *first, struct io_log *log)
2206 static int fio_append_gz_chunks(struct sk_entry *first, struct io_log *log)
2208 struct sk_entry *entry;
2209 struct flist_head *node;
2212 pthread_mutex_lock(&log->chunk_lock);
2213 flist_for_each(node, &log->chunk_list) {
2214 struct iolog_compress *c;
2216 c = flist_entry(node, struct iolog_compress, list);
2217 entry = fio_net_prep_cmd(FIO_NET_CMD_IOLOG, c->buf, c->len,
2218 NULL, SK_F_VEC | SK_F_INLINE);
2223 flist_add_tail(&entry->list, &first->next);
2225 pthread_mutex_unlock(&log->chunk_lock);
2229 static int fio_append_text_log(struct sk_entry *first, struct io_log *log)
2231 struct sk_entry *entry;
2234 while (!flist_empty(&log->io_logs)) {
2235 struct io_logs *cur_log;
2238 cur_log = flist_first_entry(&log->io_logs, struct io_logs, list);
2239 flist_del_init(&cur_log->list);
2241 size = cur_log->nr_samples * log_entry_sz(log);
2243 entry = fio_net_prep_cmd(FIO_NET_CMD_IOLOG, cur_log->log, size,
2244 NULL, SK_F_VEC | SK_F_INLINE);
2249 flist_add_tail(&entry->list, &first->next);
2255 int fio_send_iolog(struct thread_data *td, struct io_log *log, const char *name)
2257 struct cmd_iolog_pdu pdu = {
2258 .nr_samples = cpu_to_le64(iolog_nr_samples(log)),
2259 .thread_number = cpu_to_le32(td->thread_number),
2260 .log_type = cpu_to_le32(log->log_type),
2261 .log_hist_coarseness = cpu_to_le32(log->hist_coarseness),
2262 .per_job_logs = cpu_to_le32(td->o.per_job_logs),
2264 struct sk_entry *first;
2265 struct flist_head *entry;
2268 if (!flist_empty(&log->chunk_list))
2269 pdu.compressed = __cpu_to_le32(STORE_COMPRESSED);
2271 pdu.compressed = __cpu_to_le32(XMIT_COMPRESSED);
2275 snprintf((char *) pdu.name, sizeof(pdu.name), "%s", name);
2278 * We can't do this for a pre-compressed log, but for that case,
2279 * log->nr_samples is zero anyway.
2281 flist_for_each(entry, &log->io_logs) {
2282 struct io_logs *cur_log;
2285 cur_log = flist_entry(entry, struct io_logs, list);
2287 for (i = 0; i < cur_log->nr_samples; i++) {
2288 struct io_sample *s = get_sample(log, cur_log, i);
2290 s->time = cpu_to_le64(s->time);
2291 if (log->log_type != IO_LOG_TYPE_HIST) {
2292 s->data.val.val0 = cpu_to_le64(s->data.val.val0);
2293 s->data.val.val1 = cpu_to_le64(s->data.val.val1);
2295 s->__ddir = __cpu_to_le32(s->__ddir);
2296 s->bs = cpu_to_le64(s->bs);
2298 if (log->log_offset) {
2299 struct io_sample_offset *so = (void *) s;
2301 so->offset = cpu_to_le64(so->offset);
2307 * Assemble header entry first
2309 first = fio_net_prep_cmd(FIO_NET_CMD_IOLOG, &pdu, sizeof(pdu), NULL, SK_F_VEC | SK_F_INLINE | SK_F_COPY);
2314 * Now append actual log entries. If log compression was enabled on
2315 * the job, just send out the compressed chunks directly. If we
2316 * have a plain log, compress if we can, then send. Otherwise, send
2317 * the plain text output.
2319 if (!flist_empty(&log->chunk_list))
2320 ret = fio_append_gz_chunks(first, log);
2322 ret = fio_append_iolog_gz(first, log);
2324 ret = fio_append_text_log(first, log);
2326 fio_net_queue_entry(first);
2330 void fio_server_send_add_job(struct thread_data *td)
2332 struct cmd_add_job_pdu *pdu;
2333 size_t cmd_sz = offsetof(struct cmd_add_job_pdu, top) +
2334 thread_options_pack_size(&td->o);
2336 pdu = malloc(cmd_sz);
2337 pdu->thread_number = cpu_to_le32(td->thread_number);
2338 pdu->groupid = cpu_to_le32(td->groupid);
2340 convert_thread_options_to_net(&pdu->top, &td->o);
2342 fio_net_queue_cmd(FIO_NET_CMD_ADD_JOB, pdu, cmd_sz, NULL, SK_F_COPY);
2346 void fio_server_send_start(struct thread_data *td)
2348 struct sk_out *sk_out = pthread_getspecific(sk_out_key);
2350 if (sk_out->sk == -1) {
2351 log_err("pthread getting specific for key failed, sk_out %p, sk %i, err: %i:%s",
2352 sk_out, sk_out->sk, errno, strerror(errno));
2356 fio_net_queue_cmd(FIO_NET_CMD_SERVER_START, NULL, 0, NULL, SK_F_SIMPLE);
2359 int fio_server_get_verify_state(const char *name, int threadnumber,
2362 struct thread_io_list *s;
2363 struct cmd_sendfile out;
2364 struct cmd_reply *rep;
2369 dprint(FD_NET, "server: request verify state\n");
2371 rep = smalloc(sizeof(*rep));
2375 __fio_sem_init(&rep->lock, FIO_SEM_LOCKED);
2379 verify_state_gen_name((char *) out.path, sizeof(out.path), name, me,
2381 tag = (uint64_t) (uintptr_t) rep;
2382 fio_net_queue_cmd(FIO_NET_CMD_SENDFILE, &out, sizeof(out), &tag,
2386 * Wait for the backend to receive the reply
2388 if (fio_sem_down_timeout(&rep->lock, 10000)) {
2389 log_err("fio: timed out waiting for reply\n");
2395 log_err("fio: failure on receiving state file %s: %s\n",
2396 out.path, strerror(rep->error));
2401 fio_net_queue_quit();
2406 * The format is verify_state_hdr, then thread_io_list. Verify
2407 * the header, and the thread_io_list checksum
2409 s = rep->data + sizeof(struct verify_state_hdr);
2410 if (verify_state_hdr(rep->data, s)) {
2416 * Don't need the header from now, copy just the thread_io_list
2419 rep->size -= sizeof(struct verify_state_hdr);
2420 data = malloc(rep->size);
2421 memcpy(data, s, rep->size);
2425 __fio_sem_remove(&rep->lock);
2430 static int fio_init_server_ip(void)
2432 struct sockaddr *addr;
2439 sk = socket(AF_INET6, SOCK_STREAM, 0);
2441 sk = socket(AF_INET, SOCK_STREAM, 0);
2444 log_err("fio: socket: %s\n", strerror(errno));
2449 if (setsockopt(sk, SOL_SOCKET, SO_REUSEADDR, (void *)&opt, sizeof(opt)) < 0) {
2450 log_err("fio: setsockopt(REUSEADDR): %s\n", strerror(errno));
2456 * Not fatal if fails, so just ignore it if that happens
2458 if (setsockopt(sk, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt))) {
2463 void *src = &saddr_in6.sin6_addr;
2465 addr = (struct sockaddr *) &saddr_in6;
2466 socklen = sizeof(saddr_in6);
2467 saddr_in6.sin6_family = AF_INET6;
2468 str = inet_ntop(AF_INET6, src, buf, sizeof(buf));
2470 void *src = &saddr_in.sin_addr;
2472 addr = (struct sockaddr *) &saddr_in;
2473 socklen = sizeof(saddr_in);
2474 saddr_in.sin_family = AF_INET;
2475 str = inet_ntop(AF_INET, src, buf, sizeof(buf));
2478 if (bind(sk, addr, socklen) < 0) {
2479 log_err("fio: bind: %s\n", strerror(errno));
2480 log_info("fio: failed with IPv%c %s\n", use_ipv6 ? '6' : '4', str);
2488 static int fio_init_server_sock(void)
2490 struct sockaddr_un addr;
2495 sk = socket(AF_UNIX, SOCK_STREAM, 0);
2497 log_err("fio: socket: %s\n", strerror(errno));
2503 addr.sun_family = AF_UNIX;
2504 snprintf(addr.sun_path, sizeof(addr.sun_path), "%s", bind_sock);
2506 len = sizeof(addr.sun_family) + strlen(bind_sock) + 1;
2508 if (bind(sk, (struct sockaddr *) &addr, len) < 0) {
2509 log_err("fio: bind: %s\n", strerror(errno));
2518 static int fio_init_server_connection(void)
2523 dprint(FD_NET, "starting server\n");
2526 sk = fio_init_server_ip();
2528 sk = fio_init_server_sock();
2533 memset(bind_str, 0, sizeof(bind_str));
2542 src = &saddr_in6.sin6_addr;
2545 src = &saddr_in.sin_addr;
2548 p = (char *) inet_ntop(af, src, bind_str, sizeof(bind_str));
2550 sprintf(port, ",%u", fio_net_port);
2554 snprintf(bind_str, sizeof(bind_str), "%s", port);
2556 snprintf(bind_str, sizeof(bind_str), "%s", bind_sock);
2558 log_info("fio: server listening on %s\n", bind_str);
2560 if (listen(sk, 4) < 0) {
2561 log_err("fio: listen: %s\n", strerror(errno));
2569 int fio_server_parse_host(const char *host, int ipv6, struct in_addr *inp,
2570 struct in6_addr *inp6)
2576 ret = inet_pton(AF_INET6, host, inp6);
2578 ret = inet_pton(AF_INET, host, inp);
2581 struct addrinfo *res, hints = {
2582 .ai_family = ipv6 ? AF_INET6 : AF_INET,
2583 .ai_socktype = SOCK_STREAM,
2586 ret = getaddrinfo(host, NULL, &hints, &res);
2588 log_err("fio: failed to resolve <%s> (%s)\n", host,
2594 memcpy(inp6, &((struct sockaddr_in6 *) res->ai_addr)->sin6_addr, sizeof(*inp6));
2596 memcpy(inp, &((struct sockaddr_in *) res->ai_addr)->sin_addr, sizeof(*inp));
2606 * Parse a host/ip/port string. Reads from 'str'.
2611 * *ptr is the host, *port is the port, inp is the destination.
2613 * *ptr is the host, *port is the port, inp6 is the dest, and *ipv6 is 1.
2614 * For local domain sockets:
2615 * *ptr is the filename, *is_sock is 1.
2617 int fio_server_parse_string(const char *str, char **ptr, bool *is_sock,
2618 int *port, struct in_addr *inp,
2619 struct in6_addr *inp6, int *ipv6)
2621 const char *host = str;
2627 *port = fio_net_port;
2630 if (!strncmp(str, "sock:", 5)) {
2631 *ptr = strdup(str + 5);
2638 * Is it ip:<ip or host>:port
2640 if (!strncmp(host, "ip:", 3))
2642 else if (!strncmp(host, "ip4:", 4))
2644 else if (!strncmp(host, "ip6:", 4)) {
2647 } else if (host[0] == ':') {
2648 /* String is :port */
2651 if (!lport || lport > 65535) {
2652 log_err("fio: bad server port %u\n", lport);
2655 /* no hostname given, we are done */
2661 * If no port seen yet, check if there's a last ',' at the end
2664 portp = strchr(host, ',');
2668 lport = atoi(portp);
2669 if (!lport || lport > 65535) {
2670 log_err("fio: bad server port %u\n", lport);
2682 *ptr = strdup(host);
2684 if (fio_server_parse_host(*ptr, *ipv6, inp, inp6)) {
2691 *port = fio_net_port;
2697 * Server arg should be one of:
2699 * sock:/path/to/socket
2703 * Where sock uses unix domain sockets, and ip binds the server to
2704 * a specific interface. If no arguments are given to the server, it
2705 * uses IP and binds to 0.0.0.0.
2708 static int fio_handle_server_arg(void)
2710 int port = fio_net_port;
2714 saddr_in.sin_addr.s_addr = htonl(INADDR_ANY);
2716 if (!fio_server_arg)
2719 ret = fio_server_parse_string(fio_server_arg, &bind_sock, &is_sock,
2720 &port, &saddr_in.sin_addr,
2721 &saddr_in6.sin6_addr, &use_ipv6);
2723 if (!is_sock && bind_sock) {
2729 fio_net_port = port;
2730 saddr_in.sin_port = htons(port);
2731 saddr_in6.sin6_port = htons(port);
2735 static void sig_int(int sig)
2741 static void set_sig_handlers(void)
2743 struct sigaction act = {
2744 .sa_handler = sig_int,
2745 .sa_flags = SA_RESTART,
2748 sigaction(SIGINT, &act, NULL);
2750 /* Windows uses SIGBREAK as a quit signal from other applications */
2752 sigaction(SIGBREAK, &act, NULL);
2756 void fio_server_destroy_sk_key(void)
2758 pthread_key_delete(sk_out_key);
2761 int fio_server_create_sk_key(void)
2763 if (pthread_key_create(&sk_out_key, NULL)) {
2764 log_err("fio: can't create sk_out backend key\n");
2768 pthread_setspecific(sk_out_key, NULL);
2772 static int fio_server(void)
2776 dprint(FD_NET, "starting server\n");
2778 if (fio_handle_server_arg())
2784 /* if this is a child process, go handle the connection */
2785 if (fio_server_pipe_name != NULL) {
2786 ret = handle_connection_process();
2790 /* job to link child processes so they terminate together */
2791 hjob = windows_create_job();
2792 if (hjob == INVALID_HANDLE_VALUE)
2796 sk = fio_init_server_connection();
2800 ret = accept_loop(sk);
2804 if (fio_server_arg) {
2805 free(fio_server_arg);
2806 fio_server_arg = NULL;
2814 void fio_server_got_signal(int signal)
2816 struct sk_out *sk_out = pthread_getspecific(sk_out_key);
2820 if (signal == SIGPIPE)
2823 log_info("\nfio: terminating on signal %d\n", signal);
2824 exit_backend = true;
2828 static int check_existing_pidfile(const char *pidfile)
2835 if (stat(pidfile, &sb))
2838 f = fopen(pidfile, "r");
2842 if (fread(buf, sb.st_size, 1, f) <= 0) {
2849 if (kill(pid, SIGCONT) < 0)
2850 return errno != ESRCH;
2855 static int write_pid(pid_t pid, const char *pidfile)
2859 fpid = fopen(pidfile, "w");
2861 log_err("fio: failed opening pid file %s\n", pidfile);
2865 fprintf(fpid, "%u\n", (unsigned int) pid);
2871 * If pidfile is specified, background us.
2873 int fio_start_server(char *pidfile)
2881 WSAStartup(MAKEWORD(2, 2), &wsd);
2885 return fio_server();
2887 if (check_existing_pidfile(pidfile)) {
2888 log_err("fio: pidfile %s exists and server appears alive\n",
2896 log_err("fio: failed server fork: %s\n", strerror(errno));
2900 ret = write_pid(pid, pidfile);
2906 openlog("fio", LOG_NDELAY|LOG_NOWAIT|LOG_PID, LOG_USER);
2909 file = freopen("/dev/null", "r", stdin);
2913 file = freopen("/dev/null", "w", stdout);
2917 file = freopen("/dev/null", "w", stderr);
2936 void fio_server_set_arg(const char *arg)
2938 fio_server_arg = strdup(arg);
2942 void fio_server_internal_set(const char *arg)
2944 fio_server_pipe_name = strdup(arg);