2 * block queue tracing application
4 * Copyright (C) 2005 Jens Axboe <axboe@suse.de>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <sys/types.h>
29 #include <sys/ioctl.h>
30 #include <sys/param.h>
31 #include <sys/statfs.h>
34 #include <sys/socket.h>
41 #include <netinet/in.h>
42 #include <arpa/inet.h>
44 #include <sys/sendfile.h>
49 static char blktrace_version[] = "0.99";
52 * You may want to increase this even more, if you are logging at a high
53 * rate and see skipped/missed events
55 #define BUF_SIZE (512 * 1024)
58 #define OFILE_BUF (128 * 1024)
60 #define RELAYFS_TYPE 0xF0B4A981
62 #define S_OPTS "d:a:A:r:o:kw:Vb:n:D:lh:p:s"
63 static struct option l_opts[] = {
66 .has_arg = required_argument,
72 .has_arg = required_argument,
78 .has_arg = required_argument,
84 .has_arg = required_argument,
90 .has_arg = required_argument,
96 .has_arg = no_argument,
102 .has_arg = required_argument,
108 .has_arg = no_argument,
113 .name = "buffer-size",
114 .has_arg = required_argument,
119 .name = "num-sub-buffers",
120 .has_arg = required_argument,
125 .name = "output-dir",
126 .has_arg = required_argument,
132 .has_arg = no_argument,
138 .has_arg = required_argument,
144 .has_arg = required_argument,
150 .has_arg = no_argument,
162 unsigned int max_len;
165 #define FIFO_SIZE (1024) /* should be plenty big! */
166 #define CL_SIZE (128) /* cache line, any bigger? */
168 struct tip_subbuf_fifo {
169 int tail __attribute__((aligned(CL_SIZE)));
170 int head __attribute__((aligned(CL_SIZE)));
171 struct tip_subbuf *q[FIFO_SIZE];
174 struct thread_information {
180 char fn[MAXPATHLEN + 64];
188 int (*get_subbuf)(struct thread_information *, unsigned int);
189 int (*flush_subbuf)(struct thread_information *, struct tip_subbuf *);
190 int (*read_data)(struct thread_information *, void *, unsigned int);
192 unsigned long events_processed;
193 unsigned long long data_read;
194 unsigned long long data_queued;
195 struct device_information *device;
202 struct tip_subbuf_fifo fifo;
203 struct tip_subbuf *leftover_ts;
206 * mmap controlled output files
208 unsigned long long fs_size;
209 unsigned long long fs_max_size;
210 unsigned long fs_off;
212 unsigned long fs_buf_len;
215 struct device_information {
219 volatile int trace_started;
220 unsigned long drop_count;
221 struct in_addr cl_in_addr;
222 struct thread_information *threads;
226 static struct thread_information *thread_information;
228 static struct device_information *device_information;
229 static int ndevs_running;
231 /* command line option globals */
232 static char *relay_path;
233 static char *output_name;
234 static char *output_dir;
235 static int act_mask = ~0U;
236 static int kill_running_trace;
237 static unsigned long buf_size = BUF_SIZE;
238 static unsigned long buf_nr = BUF_NR;
239 static unsigned int page_size;
241 #define is_done() (*(volatile int *)(&done))
242 static volatile int done;
244 #define is_trace_stopped() (*(volatile int *)(&trace_stopped))
245 static volatile int trace_stopped;
247 #define is_stat_shown() (*(volatile int *)(&stat_shown))
248 static volatile int stat_shown;
250 int data_is_native = -1;
252 static void exit_trace(int status);
254 #define dip_tracing(dip) (*(volatile int *)(&(dip)->trace_started))
255 #define dip_set_tracing(dip, v) ((dip)->trace_started = (v))
257 #define __for_each_dip(__d, __i, __e) \
258 for (__i = 0, __d = device_information; __i < __e; __i++, __d++)
260 #define for_each_dip(__d, __i) __for_each_dip(__d, __i, ndevs)
261 #define for_each_tip(__d, __t, __j) \
262 for (__j = 0, __t = (__d)->threads; __j < ncpus; __j++, __t++)
265 * networking stuff follows. we include a magic number so we know whether
266 * to endianness convert or not
268 struct blktrace_net_hdr {
269 u32 magic; /* same as trace magic */
270 char buts_name[32]; /* trace name */
271 u32 cpu; /* for which cpu */
273 u32 len; /* length of following trace data */
276 #define TRACE_NET_PORT (8462)
285 * network cmd line params
287 static char hostname[MAXHOSTNAMELEN];
288 static int net_port = TRACE_NET_PORT;
289 static int net_mode = 0;
290 static int net_use_sendfile;
292 static int net_in_fd = -1;
293 static time_t net_connect_time;
294 static int net_out_fd = -1;
296 static void handle_sigint(__attribute__((__unused__)) int sig)
298 struct device_information *dip;
302 * stop trace so we can reap currently produced data
304 for_each_dip(dip, i) {
307 if (ioctl(dip->fd, BLKTRACESTOP) < 0)
308 perror("BLKTRACESTOP");
314 static int get_dropped_count(const char *buts_name)
317 char tmp[MAXPATHLEN + 64];
319 snprintf(tmp, sizeof(tmp), "%s/block/%s/dropped",
320 relay_path, buts_name);
322 fd = open(tmp, O_RDONLY);
325 * this may be ok, if the kernel doesn't support dropped counts
330 fprintf(stderr, "Couldn't open dropped file %s\n", tmp);
334 if (read(fd, tmp, sizeof(tmp)) < 0) {
345 static int start_trace(struct device_information *dip)
347 struct blk_user_trace_setup buts;
349 memset(&buts, 0, sizeof(buts));
350 buts.buf_size = buf_size;
351 buts.buf_nr = buf_nr;
352 buts.act_mask = act_mask;
354 if (ioctl(dip->fd, BLKTRACESETUP, &buts) < 0) {
355 perror("BLKTRACESETUP");
359 if (ioctl(dip->fd, BLKTRACESTART) < 0) {
360 perror("BLKTRACESTART");
364 memcpy(dip->buts_name, buts.name, sizeof(dip->buts_name));
365 dip_set_tracing(dip, 1);
369 static void stop_trace(struct device_information *dip)
371 if (dip_tracing(dip) || kill_running_trace) {
372 dip_set_tracing(dip, 0);
375 * should be stopped, just don't complain if it isn't
377 ioctl(dip->fd, BLKTRACESTOP);
379 if (ioctl(dip->fd, BLKTRACETEARDOWN) < 0)
380 perror("BLKTRACETEARDOWN");
387 static void stop_all_traces(void)
389 struct device_information *dip;
392 for_each_dip(dip, i) {
393 dip->drop_count = get_dropped_count(dip->buts_name);
398 static void wait_for_data(struct thread_information *tip, int events)
400 struct pollfd pfd = { .fd = tip->fd, .events = events };
403 if (poll(&pfd, 1, 100) < 0) {
407 if (pfd.revents & events)
409 if (tip->ofile_stdout)
411 } while (!is_done());
414 static int read_data_file(struct thread_information *tip, void *buf,
420 wait_for_data(tip, POLLIN);
422 ret = read(tip->fd, buf, len);
428 if (errno != EAGAIN) {
430 fprintf(stderr,"Thread %d failed read of %s\n",
436 } while (!is_done());
442 static int read_data_net(struct thread_information *tip, void *buf,
445 unsigned int bytes_left = len;
449 ret = recv(net_in_fd, buf, bytes_left, MSG_WAITALL);
454 if (errno != EAGAIN) {
456 fprintf(stderr, "server: failed read\n");
464 } while (!is_done() && bytes_left);
466 return len - bytes_left;
469 static inline struct tip_subbuf *
470 subbuf_fifo_dequeue(struct thread_information *tip)
472 const int head = tip->fifo.head;
473 const int next = (head + 1) & (FIFO_SIZE - 1);
475 if (head != tip->fifo.tail) {
476 struct tip_subbuf *ts = tip->fifo.q[head];
479 tip->fifo.head = next;
486 static inline int subbuf_fifo_queue(struct thread_information *tip,
487 struct tip_subbuf *ts)
489 const int tail = tip->fifo.tail;
490 const int next = (tail + 1) & (FIFO_SIZE - 1);
492 if (next != tip->fifo.head) {
493 tip->fifo.q[tail] = ts;
495 tip->fifo.tail = next;
499 fprintf(stderr, "fifo too small!\n");
504 * For file output, truncate and mmap the file appropriately
506 static int mmap_subbuf(struct thread_information *tip, unsigned int maxlen)
508 int ofd = fileno(tip->ofile);
512 * extend file, if we have to. use chunks of 16 subbuffers.
514 if (tip->fs_off + buf_size > tip->fs_buf_len) {
516 munlock(tip->fs_buf, tip->fs_buf_len);
517 munmap(tip->fs_buf, tip->fs_buf_len);
521 tip->fs_off = tip->fs_size & (page_size - 1);
522 tip->fs_buf_len = (16 * buf_size) - tip->fs_off;
523 tip->fs_max_size += tip->fs_buf_len;
525 if (ftruncate(ofd, tip->fs_max_size) < 0) {
530 tip->fs_buf = mmap(NULL, tip->fs_buf_len, PROT_WRITE,
531 MAP_SHARED, ofd, tip->fs_size - tip->fs_off);
532 if (tip->fs_buf == MAP_FAILED) {
536 mlock(tip->fs_buf, tip->fs_buf_len);
539 ret = tip->read_data(tip, tip->fs_buf + tip->fs_off, maxlen);
541 tip->data_read += ret;
551 * Use the copy approach for pipes and network
553 static int get_subbuf(struct thread_information *tip, unsigned int maxlen)
555 struct tip_subbuf *ts = malloc(sizeof(*ts));
558 ts->buf = malloc(buf_size);
559 ts->max_len = maxlen;
561 ret = tip->read_data(tip, ts->buf, ts->max_len);
564 tip->data_read += ret;
565 if (subbuf_fifo_queue(tip, ts))
572 static int get_subbuf_sendfile(struct thread_information *tip,
575 struct tip_subbuf *ts;
579 wait_for_data(tip, POLLMSG);
582 * hack to get last data out, we can't use sendfile for that
585 return get_subbuf(tip, maxlen);
587 if (fstat(tip->fd, &sb) < 0) {
588 perror("trace stat");
591 ready = sb.st_size - tip->data_queued;
597 ts = malloc(sizeof(*ts));
601 tip->data_queued += ready;
603 if (subbuf_fifo_queue(tip, ts))
609 static void close_thread(struct thread_information *tip)
615 if (tip->ofile_buffer)
616 free(tip->ofile_buffer);
622 tip->ofile_buffer = NULL;
626 static void tip_ftrunc_final(struct thread_information *tip)
629 * truncate to right size and cleanup mmap
631 if (tip->ofile_mmap) {
632 int ofd = fileno(tip->ofile);
635 munmap(tip->fs_buf, tip->fs_buf_len);
637 ftruncate(ofd, tip->fs_size);
641 static void *thread_main(void *arg)
643 struct thread_information *tip = arg;
644 pid_t pid = getpid();
648 CPU_SET((tip->cpu), &cpu_mask);
650 if (sched_setaffinity(pid, sizeof(cpu_mask), &cpu_mask) == -1) {
651 perror("sched_setaffinity");
655 snprintf(tip->fn, sizeof(tip->fn), "%s/block/%s/trace%d",
656 relay_path, tip->device->buts_name, tip->cpu);
657 tip->fd = open(tip->fn, O_RDONLY);
660 fprintf(stderr,"Thread %d failed open of %s\n", tip->cpu,
666 if (tip->get_subbuf(tip, buf_size) < 0)
671 * trace is stopped, pull data until we get a short read
673 while (tip->get_subbuf(tip, buf_size) > 0)
676 tip_ftrunc_final(tip);
681 static int write_data_net(int fd, void *buf, unsigned int buf_len)
683 unsigned int bytes_left = buf_len;
687 ret = send(fd, buf, bytes_left, 0);
700 static int net_send_header(struct thread_information *tip, unsigned int len)
702 struct blktrace_net_hdr hdr;
704 hdr.magic = BLK_IO_TRACE_MAGIC;
705 strcpy(hdr.buts_name, tip->device->buts_name);
707 hdr.max_cpus = ncpus;
710 return write_data_net(net_out_fd, &hdr, sizeof(hdr));
714 * send header with 0 length to signal end-of-run
716 static void net_client_send_close(void)
718 struct device_information *dip;
719 struct blktrace_net_hdr hdr;
722 for_each_dip(dip, i) {
723 hdr.magic = BLK_IO_TRACE_MAGIC;
724 hdr.max_cpus = ncpus;
726 strcpy(hdr.buts_name, dip->buts_name);
727 hdr.cpu = get_dropped_count(dip->buts_name);
729 write_data_net(net_out_fd, &hdr, sizeof(hdr));
734 static int flush_subbuf_net(struct thread_information *tip,
735 struct tip_subbuf *ts)
737 if (net_send_header(tip, ts->len))
739 if (write_data_net(net_out_fd, ts->buf, ts->len))
747 static int net_sendfile(struct thread_information *tip, struct tip_subbuf *ts)
749 int ret = sendfile(net_out_fd, tip->fd, NULL, ts->len);
754 } else if (ret < (int) ts->len) {
755 fprintf(stderr, "short sendfile send (%d of %d)\n", ret, ts->len);
762 static int flush_subbuf_sendfile(struct thread_information *tip,
763 struct tip_subbuf *ts)
768 * currently we cannot use sendfile() on the last bytes read, as they
769 * may not be a full subbuffer. get_subbuf_sendfile() falls back to
770 * the read approach for those, so use send() to ship them out
773 return flush_subbuf_net(tip, ts);
775 if (net_send_header(tip, ts->len))
777 if (net_sendfile(tip, ts))
780 tip->data_read += ts->len;
781 tip->ofile_offset += buf_size;
788 static int write_data(struct thread_information *tip, void *buf,
789 unsigned int buf_len)
797 ret = fwrite(buf, buf_len, 1, tip->ofile);
807 if (tip->ofile_stdout)
813 static int flush_subbuf_file(struct thread_information *tip,
814 struct tip_subbuf *ts)
816 unsigned int offset = 0;
817 struct blk_io_trace *t;
818 int pdu_len, events = 0;
821 * surplus from last run
823 if (tip->leftover_ts) {
824 struct tip_subbuf *prev_ts = tip->leftover_ts;
826 if (prev_ts->len + ts->len > prev_ts->max_len) {
827 prev_ts->max_len += ts->len;
828 prev_ts->buf = realloc(prev_ts->buf, prev_ts->max_len);
831 memcpy(prev_ts->buf + prev_ts->len, ts->buf, ts->len);
832 prev_ts->len += ts->len;
838 tip->leftover_ts = NULL;
841 while (offset + sizeof(*t) <= ts->len) {
842 t = ts->buf + offset;
844 if (verify_trace(t)) {
845 write_data(tip, ts->buf, offset);
849 pdu_len = t->pdu_len;
851 if (offset + sizeof(*t) + pdu_len > ts->len)
854 offset += sizeof(*t) + pdu_len;
855 tip->events_processed++;
856 tip->data_read += sizeof(*t) + pdu_len;
860 if (write_data(tip, ts->buf, offset))
864 * leftover bytes, save them for next time
866 if (offset != ts->len) {
867 tip->leftover_ts = ts;
869 memmove(ts->buf, ts->buf + offset, ts->len);
878 static int write_tip_events(struct thread_information *tip)
880 struct tip_subbuf *ts = subbuf_fifo_dequeue(tip);
883 return tip->flush_subbuf(tip, ts);
889 * scans the tips we know and writes out the subbuffers we accumulate
891 static void get_and_write_events(void)
893 struct device_information *dip;
894 struct thread_information *tip;
895 int i, j, events, ret, tips_running;
900 for_each_dip(dip, i) {
901 for_each_tip(dip, tip, j) {
902 ret = write_tip_events(tip);
918 for_each_dip(dip, i) {
919 for_each_tip(dip, tip, j) {
920 ret = write_tip_events(tip);
923 tips_running += !tip->exited;
927 } while (events || tips_running);
930 static void wait_for_threads(void)
933 * for piped or network output, poll and fetch data for writeout.
934 * for files, we just wait around for trace threads to exit
936 if ((output_name && !strcmp(output_name, "-")) ||
937 net_mode == Net_client)
938 get_and_write_events();
940 struct device_information *dip;
941 struct thread_information *tip;
942 int i, j, tips_running;
949 for_each_tip(dip, tip, j)
950 tips_running += !tip->exited;
951 } while (tips_running);
954 if (net_mode == Net_client)
955 net_client_send_close();
958 static int fill_ofname(struct device_information *dip,
959 struct thread_information *tip, char *dst,
966 len = sprintf(dst, "%s/", output_dir);
968 if (net_mode == Net_server) {
969 len += sprintf(dst + len, "%s-", inet_ntoa(dip->cl_in_addr));
970 len += strftime(dst + len, 64, "%F-%T/", gmtime(&net_connect_time));
973 if (stat(dst, &sb) < 0) {
974 if (errno != ENOENT) {
978 if (mkdir(dst, 0755) < 0) {
980 fprintf(stderr, "Can't make output dir\n");
986 sprintf(dst + len, "%s.blktrace.%d", output_name, tip->cpu);
988 sprintf(dst + len, "%s.blktrace.%d", buts_name, tip->cpu);
993 static void fill_ops(struct thread_information *tip)
998 if (net_mode == Net_client) {
999 if (net_use_sendfile) {
1000 tip->get_subbuf = get_subbuf_sendfile;
1001 tip->flush_subbuf = flush_subbuf_sendfile;
1003 tip->get_subbuf = get_subbuf;
1004 tip->flush_subbuf = flush_subbuf_net;
1007 if (tip->ofile_mmap)
1008 tip->get_subbuf = mmap_subbuf;
1010 tip->get_subbuf = get_subbuf;
1012 tip->flush_subbuf = flush_subbuf_file;
1015 if (net_mode == Net_server)
1016 tip->read_data = read_data_net;
1018 tip->read_data = read_data_file;
1021 static int tip_open_output(struct device_information *dip,
1022 struct thread_information *tip)
1024 int pipeline = output_name && !strcmp(output_name, "-");
1025 int mode, vbuf_size;
1028 if (net_mode == Net_client) {
1030 tip->ofile_stdout = 0;
1031 tip->ofile_mmap = 0;
1033 } else if (pipeline) {
1034 tip->ofile = fdopen(STDOUT_FILENO, "w");
1035 tip->ofile_stdout = 1;
1036 tip->ofile_mmap = 0;
1040 if (fill_ofname(dip, tip, op, dip->buts_name))
1042 tip->ofile = fopen(op, "w+");
1043 tip->ofile_stdout = 0;
1044 tip->ofile_mmap = 1;
1046 vbuf_size = OFILE_BUF;
1049 if (tip->ofile == NULL) {
1054 tip->ofile_buffer = malloc(vbuf_size);
1055 if (setvbuf(tip->ofile, tip->ofile_buffer, mode, vbuf_size)) {
1066 static int start_threads(struct device_information *dip)
1068 struct thread_information *tip;
1071 for_each_tip(dip, tip, j) {
1074 tip->events_processed = 0;
1076 memset(&tip->fifo, 0, sizeof(tip->fifo));
1077 tip->leftover_ts = NULL;
1079 if (tip_open_output(dip, tip))
1082 if (pthread_create(&tip->thread, NULL, thread_main, tip)) {
1083 perror("pthread_create");
1092 static void stop_threads(struct device_information *dip)
1094 struct thread_information *tip;
1098 for_each_tip(dip, tip, i) {
1099 (void) pthread_join(tip->thread, (void *) &ret);
1104 static void stop_all_threads(void)
1106 struct device_information *dip;
1109 for_each_dip(dip, i)
1113 static void stop_all_tracing(void)
1115 struct device_information *dip;
1118 for_each_dip(dip, i)
1122 static void exit_trace(int status)
1124 if (!is_trace_stopped()) {
1133 static int resize_devices(char *path)
1135 int size = (ndevs + 1) * sizeof(struct device_information);
1137 device_information = realloc(device_information, size);
1138 if (!device_information) {
1139 fprintf(stderr, "Out of memory, device %s (%d)\n", path, size);
1142 device_information[ndevs].path = path;
1147 static int open_devices(void)
1149 struct device_information *dip;
1152 for_each_dip(dip, i) {
1153 dip->fd = open(dip->path, O_RDONLY | O_NONBLOCK);
1163 static int start_devices(void)
1165 struct device_information *dip;
1168 size = ncpus * sizeof(struct thread_information);
1169 thread_information = malloc(size * ndevs);
1170 if (!thread_information) {
1171 fprintf(stderr, "Out of memory, threads (%d)\n", size * ndevs);
1175 for_each_dip(dip, i) {
1176 if (start_trace(dip)) {
1178 fprintf(stderr, "Failed to start trace on %s\n",
1185 __for_each_dip(dip, j, i)
1191 for_each_dip(dip, i) {
1192 dip->threads = thread_information + (i * ncpus);
1193 if (start_threads(dip)) {
1194 fprintf(stderr, "Failed to start worker threads\n");
1200 __for_each_dip(dip, j, i)
1202 for_each_dip(dip, i)
1211 static void show_stats(void)
1213 struct device_information *dip;
1214 struct thread_information *tip;
1215 unsigned long long events_processed, data_read;
1216 unsigned long total_drops;
1217 int i, j, no_stdout = 0;
1219 if (is_stat_shown())
1222 if (output_name && !strcmp(output_name, "-"))
1228 for_each_dip(dip, i) {
1230 printf("Device: %s\n", dip->path);
1231 events_processed = 0;
1233 for_each_tip(dip, tip, j) {
1235 printf(" CPU%3d: %20lu events, %8llu KiB data\n",
1236 tip->cpu, tip->events_processed,
1237 (tip->data_read + 1023) >> 10);
1238 events_processed += tip->events_processed;
1239 data_read += tip->data_read;
1241 total_drops += dip->drop_count;
1243 printf(" Total: %20llu events (dropped %lu), %8llu KiB data\n",
1244 events_processed, dip->drop_count,
1245 (data_read + 1023) >> 10);
1249 fprintf(stderr, "You have dropped events, consider using a larger buffer size (-b)\n");
1252 static struct device_information *net_get_dip(char *buts_name,
1253 struct in_addr *cl_in_addr)
1255 struct device_information *dip;
1258 for (i = 0; i < ndevs; i++) {
1259 dip = &device_information[i];
1261 if (!strcmp(dip->buts_name, buts_name))
1265 device_information = realloc(device_information, (ndevs + 1) * sizeof(*dip));
1266 dip = &device_information[ndevs];
1267 memset(dip, 0, sizeof(*dip));
1269 dip->cl_in_addr = *cl_in_addr;
1270 strcpy(dip->buts_name, buts_name);
1271 dip->path = strdup(buts_name);
1272 dip->trace_started = 1;
1275 dip->threads = malloc(ncpus * sizeof(struct thread_information));
1276 memset(dip->threads, 0, ncpus * sizeof(struct thread_information));
1281 for (i = 0; i < ncpus; i++) {
1282 struct thread_information *tip = &dip->threads[i];
1288 if (tip_open_output(dip, tip))
1295 static struct thread_information *net_get_tip(struct blktrace_net_hdr *bnh,
1296 struct in_addr *cl_in_addr)
1298 struct device_information *dip;
1300 ncpus = bnh->max_cpus;
1301 dip = net_get_dip(bnh->buts_name, cl_in_addr);
1302 if (!dip->trace_started) {
1303 fprintf(stderr, "Events for closed devices %s\n", dip->buts_name);
1307 return &dip->threads[bnh->cpu];
1310 static int net_get_header(struct blktrace_net_hdr *bnh)
1312 int fl = fcntl(net_in_fd, F_GETFL);
1313 int bytes_left, ret;
1316 fcntl(net_in_fd, F_SETFL, fl | O_NONBLOCK);
1317 bytes_left = sizeof(*bnh);
1318 while (bytes_left && !is_done()) {
1319 ret = recv(net_in_fd, p, bytes_left, MSG_WAITALL);
1321 if (errno != EAGAIN) {
1322 perror("recv header");
1335 fcntl(net_in_fd, F_SETFL, fl & ~O_NONBLOCK);
1339 static int net_server_loop(struct in_addr *cl_in_addr)
1341 struct thread_information *tip;
1342 struct blktrace_net_hdr bnh;
1344 if (net_get_header(&bnh))
1347 if (data_is_native == -1 && check_data_endianness(bnh.magic)) {
1348 fprintf(stderr, "server: received data is bad\n");
1352 if (!data_is_native) {
1353 bnh.magic = be32_to_cpu(bnh.magic);
1354 bnh.cpu = be32_to_cpu(bnh.cpu);
1355 bnh.len = be32_to_cpu(bnh.len);
1358 if ((bnh.magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) {
1359 fprintf(stderr, "server: bad data magic\n");
1364 * len == 0 means that the other end signalled end-of-run
1368 * overload cpu count with dropped events
1370 struct device_information *dip;
1372 dip = net_get_dip(bnh.buts_name, cl_in_addr);
1373 dip->drop_count = bnh.cpu;
1374 dip->trace_started = 0;
1377 fprintf(stderr, "server: end of run for %s\n", dip->buts_name);
1378 return !ndevs_running;
1381 tip = net_get_tip(&bnh, cl_in_addr);
1385 if (mmap_subbuf(tip, bnh.len))
1391 static int get_connection(int fd, struct sockaddr_in *addr)
1393 struct pollfd pfd = { .fd = fd, .events = POLLIN };
1396 printf("blktrace: waiting for incoming connection...\n");
1398 if (poll(&pfd, 1, -1) < 0) {
1399 perror("poll for connection");
1402 if ((pfd.revents & POLLIN) == 0)
1405 socklen = sizeof(*addr);
1406 net_in_fd = accept(fd, (struct sockaddr *) addr, &socklen);
1407 if (net_in_fd < 0) {
1412 time(&net_connect_time);
1413 printf("blktrace: connection from %s\n", inet_ntoa(addr->sin_addr));
1418 * Start here when we are in server mode - just fetch data from the network
1421 static int net_server(void)
1423 struct device_information *dip;
1424 struct thread_information *tip;
1425 struct sockaddr_in addr;
1428 fd = socket(AF_INET, SOCK_STREAM, 0);
1430 perror("server: socket");
1435 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
1436 perror("setsockopt");
1440 memset(&addr, 0, sizeof(addr));
1441 addr.sin_family = AF_INET;
1442 addr.sin_addr.s_addr = htonl(INADDR_ANY);
1443 addr.sin_port = htons(net_port);
1445 if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1450 if (listen(fd, 1) < 0) {
1456 if (get_connection(fd, &addr))
1459 while (!is_done()) {
1460 if (net_server_loop(&addr.sin_addr))
1464 for_each_dip(dip, i)
1465 for_each_tip(dip, tip, j)
1466 tip_ftrunc_final(tip);
1474 * cleanup for next run
1476 for_each_dip(dip, i) {
1477 for_each_tip(dip, tip, j)
1484 free(device_information);
1485 device_information = NULL;
1495 * Setup outgoing network connection where we will transmit data
1497 static int net_setup_client(void)
1499 struct sockaddr_in addr;
1502 fd = socket(AF_INET, SOCK_STREAM, 0);
1504 perror("client: socket");
1508 memset(&addr, 0, sizeof(addr));
1509 addr.sin_family = AF_INET;
1510 addr.sin_port = htons(net_port);
1512 if (inet_aton(hostname, &addr.sin_addr) != 1) {
1513 struct hostent *hent = gethostbyname(hostname);
1515 perror("gethostbyname");
1519 memcpy(&addr.sin_addr, hent->h_addr, 4);
1520 strcpy(hostname, hent->h_name);
1523 printf("blktrace: connecting to %s\n", hostname);
1525 if (connect(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1526 perror("client: connect");
1530 printf("blktrace: connected!\n");
1535 static char usage_str[] = \
1536 "-d <dev> [ -r relay path ] [ -o <output> ] [-k ] [ -w time ]\n" \
1537 "[ -a action ] [ -A action mask ] [ -v ]\n\n" \
1538 "\t-d Use specified device. May also be given last after options\n" \
1539 "\t-r Path to mounted relayfs, defaults to /relay\n" \
1540 "\t-o File(s) to send output to\n" \
1541 "\t-D Directory to prepend to output file names\n" \
1542 "\t-k Kill a running trace\n" \
1543 "\t-w Stop after defined time, in seconds\n" \
1544 "\t-a Only trace specified actions. See documentation\n" \
1545 "\t-A Give trace mask as a single value. See documentation\n" \
1546 "\t-b Sub buffer size in KiB\n" \
1547 "\t-n Number of sub buffers\n" \
1548 "\t-l Run in network listen mode (blktrace server)\n" \
1549 "\t-h Run in network client mode, connecting to the given host\n" \
1550 "\t-p Network port to use (default 8462)\n" \
1551 "\t-s Make the network client use sendfile() to transfer data\n" \
1552 "\t-V Print program version info\n\n";
1554 static void show_usage(char *program)
1556 fprintf(stderr, "Usage: %s %s %s",program, blktrace_version, usage_str);
1559 int main(int argc, char *argv[])
1561 static char default_relay_path[] = "/relay";
1565 int act_mask_tmp = 0;
1567 while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) >= 0) {
1570 i = find_mask_map(optarg);
1572 fprintf(stderr,"Invalid action mask %s\n",
1580 if ((sscanf(optarg, "%x", &i) != 1) ||
1581 !valid_act_opt(i)) {
1583 "Invalid set action mask %s/0x%x\n",
1591 if (resize_devices(optarg) != 0)
1596 relay_path = optarg;
1600 output_name = optarg;
1603 kill_running_trace = 1;
1606 stop_watch = atoi(optarg);
1607 if (stop_watch <= 0) {
1609 "Invalid stopwatch value (%d secs)\n",
1615 printf("%s version %s\n", argv[0], blktrace_version);
1618 buf_size = strtoul(optarg, NULL, 10);
1619 if (buf_size <= 0 || buf_size > 16*1024) {
1621 "Invalid buffer size (%lu)\n",buf_size);
1627 buf_nr = strtoul(optarg, NULL, 10);
1630 "Invalid buffer nr (%lu)\n", buf_nr);
1635 output_dir = optarg;
1638 net_mode = Net_client;
1639 strcpy(hostname, optarg);
1642 net_mode = Net_server;
1645 net_port = atoi(optarg);
1648 net_use_sendfile = 1;
1651 show_usage(argv[0]);
1656 setlocale(LC_NUMERIC, "en_US");
1658 page_size = getpagesize();
1660 if (net_mode == Net_server)
1661 return net_server();
1663 while (optind < argc) {
1664 if (resize_devices(argv[optind++]) != 0)
1669 show_usage(argv[0]);
1674 relay_path = default_relay_path;
1676 if (act_mask_tmp != 0)
1677 act_mask = act_mask_tmp;
1679 if (statfs(relay_path, &st) < 0) {
1681 fprintf(stderr,"%s does not appear to be a valid path\n",
1684 } else if (st.f_type != (long) RELAYFS_TYPE) {
1685 fprintf(stderr,"%s does not appear to be a relay filesystem\n",
1690 if (open_devices() != 0)
1693 if (kill_running_trace) {
1698 ncpus = sysconf(_SC_NPROCESSORS_ONLN);
1700 fprintf(stderr, "sysconf(_SC_NPROCESSORS_ONLN) failed\n");
1704 signal(SIGINT, handle_sigint);
1705 signal(SIGHUP, handle_sigint);
1706 signal(SIGTERM, handle_sigint);
1707 signal(SIGALRM, handle_sigint);
1709 if (net_mode == Net_client && net_setup_client())
1712 if (start_devices() != 0)
1715 atexit(stop_all_tracing);
1722 if (!is_trace_stopped()) {