2 * block queue tracing application
4 * Copyright (C) 2005 Jens Axboe <axboe@suse.de>
5 * Copyright (C) 2006 Jens Axboe <axboe@kernel.dk>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <sys/types.h>
30 #include <sys/ioctl.h>
31 #include <sys/param.h>
32 #include <sys/statfs.h>
35 #include <sys/socket.h>
42 #include <netinet/in.h>
43 #include <arpa/inet.h>
45 #include <sys/sendfile.h>
50 static char blktrace_version[] = "0.99.3";
53 * You may want to increase this even more, if you are logging at a high
54 * rate and see skipped/missed events
56 #define BUF_SIZE (512 * 1024)
59 #define OFILE_BUF (128 * 1024)
61 #define DEBUGFS_TYPE 0x64626720
63 #define S_OPTS "d:a:A:r:o:kw:Vb:n:D:lh:p:sI:"
64 static struct option l_opts[] = {
67 .has_arg = required_argument,
73 .has_arg = required_argument,
79 .has_arg = required_argument,
85 .has_arg = required_argument,
91 .has_arg = required_argument,
97 .has_arg = required_argument,
103 .has_arg = no_argument,
109 .has_arg = required_argument,
115 .has_arg = no_argument,
120 .name = "buffer-size",
121 .has_arg = required_argument,
126 .name = "num-sub-buffers",
127 .has_arg = required_argument,
132 .name = "output-dir",
133 .has_arg = required_argument,
139 .has_arg = no_argument,
145 .has_arg = required_argument,
151 .has_arg = required_argument,
156 .name = "no-sendfile",
157 .has_arg = no_argument,
169 unsigned int max_len;
172 #define FIFO_SIZE (1024) /* should be plenty big! */
173 #define CL_SIZE (128) /* cache line, any bigger? */
175 struct tip_subbuf_fifo {
176 int tail __attribute__((aligned(CL_SIZE)));
177 int head __attribute__((aligned(CL_SIZE)));
178 struct tip_subbuf *q[FIFO_SIZE];
181 struct thread_information {
187 char fn[MAXPATHLEN + 64];
195 int (*get_subbuf)(struct thread_information *, unsigned int);
196 int (*flush_subbuf)(struct thread_information *, struct tip_subbuf *);
197 int (*read_data)(struct thread_information *, void *, unsigned int);
199 unsigned long events_processed;
200 unsigned long long data_read;
201 unsigned long long data_queued;
202 struct device_information *device;
209 struct tip_subbuf_fifo fifo;
210 struct tip_subbuf *leftover_ts;
213 * mmap controlled output files
215 unsigned long long fs_size;
216 unsigned long long fs_max_size;
217 unsigned long fs_off;
219 unsigned long fs_buf_len;
221 struct net_connection *nc;
224 struct device_information {
228 volatile int trace_started;
229 unsigned long drop_count;
230 struct thread_information *threads;
231 unsigned long buf_size;
232 unsigned long buf_nr;
233 unsigned int page_size;
237 time_t cl_connect_time;
241 static struct thread_information *thread_information;
243 static struct device_information *device_information;
245 /* command line option globals */
246 static char *debugfs_path;
247 static char *output_name;
248 static char *output_dir;
249 static int act_mask = ~0U;
250 static int kill_running_trace;
251 static unsigned long buf_size = BUF_SIZE;
252 static unsigned long buf_nr = BUF_NR;
253 static unsigned int page_size;
255 #define is_done() (*(volatile int *)(&done))
256 static volatile int done;
258 #define is_trace_stopped() (*(volatile int *)(&trace_stopped))
259 static volatile int trace_stopped;
261 #define is_stat_shown() (*(volatile int *)(&stat_shown))
262 static volatile int stat_shown;
264 int data_is_native = -1;
266 static void exit_trace(int status);
268 #define dip_tracing(dip) (*(volatile int *)(&(dip)->trace_started))
269 #define dip_set_tracing(dip, v) ((dip)->trace_started = (v))
271 #define __for_each_dip(__d, __di, __e, __i) \
272 for (__i = 0, __d = __di; __i < __e; __i++, __d++)
274 #define for_each_dip(__d, __i) \
275 __for_each_dip(__d, device_information, ndevs, __i)
276 #define for_each_nc_dip(__nc, __d, __i) \
277 __for_each_dip(__d, (__nc)->ch->device_information, (__nc)->ch->ndevs, __i)
279 #define __for_each_tip(__d, __t, __ncpus, __j) \
280 for (__j = 0, __t = (__d)->threads; __j < __ncpus; __j++, __t++)
281 #define for_each_tip(__d, __t, __j) \
282 __for_each_tip(__d, __t, ncpus, __j)
283 #define for_each_cl_host(__c) \
284 for (__c = cl_host_list; __c; __c = __c->list_next)
287 * networking stuff follows. we include a magic number so we know whether
288 * to endianness convert or not
290 struct blktrace_net_hdr {
291 u32 magic; /* same as trace magic */
292 char buts_name[32]; /* trace name */
293 u32 cpu; /* for which cpu */
295 u32 len; /* length of following trace data */
296 u32 cl_id; /* id for set of client per-cpu connections */
297 u32 buf_size; /* client buf_size for this trace */
298 u32 buf_nr; /* client buf_nr for this trace */
299 u32 page_size; /* client page_size for this trace */
302 #define TRACE_NET_PORT (8462)
311 * network cmd line params
313 static char hostname[MAXHOSTNAMELEN];
314 static int net_port = TRACE_NET_PORT;
315 static int net_mode = 0;
316 static int net_use_sendfile = 1;
319 struct cl_host *list_next;
320 struct in_addr cl_in_addr;
321 struct net_connection *net_connections;
323 struct device_information *device_information;
329 struct net_connection {
337 #define NET_MAX_CL_HOSTS (1024)
338 static struct cl_host *cl_host_list;
340 static int net_connects;
342 static int *net_out_fd;
344 static void handle_sigint(__attribute__((__unused__)) int sig)
346 struct device_information *dip;
350 * stop trace so we can reap currently produced data
352 for_each_dip(dip, i) {
355 if (ioctl(dip->fd, BLKTRACESTOP) < 0)
356 perror("BLKTRACESTOP");
362 static int get_dropped_count(const char *buts_name)
365 char tmp[MAXPATHLEN + 64];
367 snprintf(tmp, sizeof(tmp), "%s/block/%s/dropped",
368 debugfs_path, buts_name);
370 fd = open(tmp, O_RDONLY);
373 * this may be ok, if the kernel doesn't support dropped counts
378 fprintf(stderr, "Couldn't open dropped file %s\n", tmp);
382 if (read(fd, tmp, sizeof(tmp)) < 0) {
393 static int start_trace(struct device_information *dip)
395 struct blk_user_trace_setup buts;
397 memset(&buts, 0, sizeof(buts));
398 buts.buf_size = dip->buf_size;
399 buts.buf_nr = dip->buf_nr;
400 buts.act_mask = act_mask;
402 if (ioctl(dip->fd, BLKTRACESETUP, &buts) < 0) {
403 perror("BLKTRACESETUP");
407 if (ioctl(dip->fd, BLKTRACESTART) < 0) {
408 perror("BLKTRACESTART");
412 memcpy(dip->buts_name, buts.name, sizeof(dip->buts_name));
413 dip_set_tracing(dip, 1);
417 static void stop_trace(struct device_information *dip)
419 if (dip_tracing(dip) || kill_running_trace) {
420 dip_set_tracing(dip, 0);
423 * should be stopped, just don't complain if it isn't
425 ioctl(dip->fd, BLKTRACESTOP);
427 if (ioctl(dip->fd, BLKTRACETEARDOWN) < 0)
428 perror("BLKTRACETEARDOWN");
435 static void stop_all_traces(void)
437 struct device_information *dip;
440 for_each_dip(dip, i) {
441 dip->drop_count = get_dropped_count(dip->buts_name);
446 static void wait_for_data(struct thread_information *tip, int timeout)
448 struct pollfd pfd = { .fd = tip->fd, .events = POLLIN };
451 if (poll(&pfd, 1, timeout) < 0) {
455 if (pfd.revents & POLLIN)
457 if (tip->ofile_stdout)
462 static int read_data_file(struct thread_information *tip, void *buf,
468 wait_for_data(tip, 100);
470 ret = read(tip->fd, buf, len);
476 if (errno != EAGAIN) {
478 fprintf(stderr,"Thread %d failed read of %s\n",
484 } while (!is_done());
490 static int read_data_net(struct thread_information *tip, void *buf,
493 struct net_connection *nc = tip->nc;
494 unsigned int bytes_left = len;
498 ret = recv(nc->in_fd, buf, bytes_left, MSG_WAITALL);
503 if (errno != EAGAIN) {
505 fprintf(stderr, "server: failed read\n");
513 } while (!is_done() && bytes_left);
515 return len - bytes_left;
518 static inline struct tip_subbuf *
519 subbuf_fifo_dequeue(struct thread_information *tip)
521 const int head = tip->fifo.head;
522 const int next = (head + 1) & (FIFO_SIZE - 1);
524 if (head != tip->fifo.tail) {
525 struct tip_subbuf *ts = tip->fifo.q[head];
528 tip->fifo.head = next;
535 static inline int subbuf_fifo_queue(struct thread_information *tip,
536 struct tip_subbuf *ts)
538 const int tail = tip->fifo.tail;
539 const int next = (tail + 1) & (FIFO_SIZE - 1);
541 if (next != tip->fifo.head) {
542 tip->fifo.q[tail] = ts;
544 tip->fifo.tail = next;
548 fprintf(stderr, "fifo too small!\n");
553 * For file output, truncate and mmap the file appropriately
555 static int mmap_subbuf(struct thread_information *tip, unsigned int maxlen)
557 int ofd = fileno(tip->ofile);
562 * extend file, if we have to. use chunks of 16 subbuffers.
564 if (tip->fs_off + maxlen > tip->fs_buf_len) {
566 munlock(tip->fs_buf, tip->fs_buf_len);
567 munmap(tip->fs_buf, tip->fs_buf_len);
571 tip->fs_off = tip->fs_size & (tip->device->page_size - 1);
572 nr = max(16, tip->device->buf_nr);
573 tip->fs_buf_len = (nr * tip->device->buf_size) - tip->fs_off;
574 tip->fs_max_size += tip->fs_buf_len;
576 if (ftruncate(ofd, tip->fs_max_size) < 0) {
581 tip->fs_buf = mmap(NULL, tip->fs_buf_len, PROT_WRITE,
582 MAP_SHARED, ofd, tip->fs_size - tip->fs_off);
583 if (tip->fs_buf == MAP_FAILED) {
587 mlock(tip->fs_buf, tip->fs_buf_len);
590 ret = tip->read_data(tip, tip->fs_buf + tip->fs_off, maxlen);
592 tip->data_read += ret;
602 * Use the copy approach for pipes and network
604 static int get_subbuf(struct thread_information *tip, unsigned int maxlen)
606 struct tip_subbuf *ts = malloc(sizeof(*ts));
609 ts->buf = malloc(tip->device->buf_size);
610 ts->max_len = maxlen;
612 ret = tip->read_data(tip, ts->buf, ts->max_len);
615 tip->data_read += ret;
616 if (subbuf_fifo_queue(tip, ts))
628 static void close_thread(struct thread_information *tip)
634 if (tip->ofile_buffer)
635 free(tip->ofile_buffer);
641 tip->ofile_buffer = NULL;
645 static void tip_ftrunc_final(struct thread_information *tip)
648 * truncate to right size and cleanup mmap
650 if (tip->ofile_mmap && tip->ofile) {
651 int ofd = fileno(tip->ofile);
654 munmap(tip->fs_buf, tip->fs_buf_len);
656 ftruncate(ofd, tip->fs_size);
660 static void *thread_main(void *arg)
662 struct thread_information *tip = arg;
663 pid_t pid = getpid();
667 CPU_SET((tip->cpu), &cpu_mask);
669 if (sched_setaffinity(pid, sizeof(cpu_mask), &cpu_mask) == -1) {
670 perror("sched_setaffinity");
674 snprintf(tip->fn, sizeof(tip->fn), "%s/block/%s/trace%d",
675 debugfs_path, tip->device->buts_name, tip->cpu);
676 tip->fd = open(tip->fn, O_RDONLY);
679 fprintf(stderr,"Thread %d failed open of %s\n", tip->cpu,
685 if (tip->get_subbuf(tip, tip->device->buf_size) < 0)
690 * trace is stopped, pull data until we get a short read
692 while (tip->get_subbuf(tip, tip->device->buf_size) > 0)
695 tip_ftrunc_final(tip);
700 static int write_data_net(int fd, void *buf, unsigned int buf_len)
702 unsigned int bytes_left = buf_len;
706 ret = send(fd, buf, bytes_left, 0);
719 static int net_send_header(struct thread_information *tip, unsigned int len)
721 struct blktrace_net_hdr hdr;
723 hdr.magic = BLK_IO_TRACE_MAGIC;
724 strcpy(hdr.buts_name, tip->device->buts_name);
726 hdr.max_cpus = ncpus;
728 hdr.cl_id = getpid();
729 hdr.buf_size = tip->device->buf_size;
730 hdr.buf_nr = tip->device->buf_nr;
731 hdr.page_size = tip->device->page_size;
733 return write_data_net(net_out_fd[tip->cpu], &hdr, sizeof(hdr));
737 * send header with 0 length to signal end-of-run
739 static void net_client_send_close(void)
741 struct device_information *dip;
742 struct blktrace_net_hdr hdr;
745 for_each_dip(dip, i) {
746 hdr.magic = BLK_IO_TRACE_MAGIC;
747 hdr.max_cpus = ncpus;
749 strcpy(hdr.buts_name, dip->buts_name);
750 hdr.cpu = get_dropped_count(dip->buts_name);
751 hdr.cl_id = getpid();
752 hdr.buf_size = dip->buf_size;
753 hdr.buf_nr = dip->buf_nr;
754 hdr.page_size = dip->page_size;
756 write_data_net(net_out_fd[0], &hdr, sizeof(hdr));
761 static int flush_subbuf_net(struct thread_information *tip,
762 struct tip_subbuf *ts)
764 if (net_send_header(tip, ts->len))
766 if (write_data_net(net_out_fd[tip->cpu], ts->buf, ts->len))
774 static int net_sendfile(struct thread_information *tip, struct tip_subbuf *ts)
776 int ret = sendfile(net_out_fd[tip->cpu], tip->fd, NULL, ts->len);
781 } else if (ret < (int) ts->len) {
782 fprintf(stderr, "short sendfile send (%d of %d)\n", ret, ts->len);
789 static int flush_subbuf_sendfile(struct thread_information *tip,
790 struct tip_subbuf *ts)
794 if (net_send_header(tip, ts->len))
796 if (net_sendfile(tip, ts))
799 tip->data_read += ts->len;
806 static int get_subbuf_sendfile(struct thread_information *tip,
807 __attribute__((__unused__)) unsigned int maxlen)
809 struct tip_subbuf *ts;
813 wait_for_data(tip, -1);
815 if (fstat(tip->fd, &sb) < 0) {
816 perror("trace stat");
820 ready = sb.st_size - tip->data_queued;
826 ts = malloc(sizeof(*ts));
830 tip->data_queued += ready;
832 if (flush_subbuf_sendfile(tip, ts) < 0)
838 static int write_data(struct thread_information *tip, void *buf,
839 unsigned int buf_len)
846 ret = fwrite(buf, buf_len, 1, tip->ofile);
847 if (ferror(tip->ofile) || ret != 1) {
849 clearerr(tip->ofile);
853 if (tip->ofile_stdout)
859 static int flush_subbuf_file(struct thread_information *tip,
860 struct tip_subbuf *ts)
862 unsigned int offset = 0;
863 struct blk_io_trace *t;
864 int pdu_len, events = 0;
867 * surplus from last run
869 if (tip->leftover_ts) {
870 struct tip_subbuf *prev_ts = tip->leftover_ts;
872 if (prev_ts->len + ts->len > prev_ts->max_len) {
873 prev_ts->max_len += ts->len;
874 prev_ts->buf = realloc(prev_ts->buf, prev_ts->max_len);
877 memcpy(prev_ts->buf + prev_ts->len, ts->buf, ts->len);
878 prev_ts->len += ts->len;
884 tip->leftover_ts = NULL;
887 while (offset + sizeof(*t) <= ts->len) {
888 t = ts->buf + offset;
890 if (verify_trace(t)) {
891 write_data(tip, ts->buf, offset);
895 pdu_len = t->pdu_len;
897 if (offset + sizeof(*t) + pdu_len > ts->len)
900 offset += sizeof(*t) + pdu_len;
901 tip->events_processed++;
902 tip->data_read += sizeof(*t) + pdu_len;
906 if (write_data(tip, ts->buf, offset))
910 * leftover bytes, save them for next time
912 if (offset != ts->len) {
913 tip->leftover_ts = ts;
915 memmove(ts->buf, ts->buf + offset, ts->len);
924 static int write_tip_events(struct thread_information *tip)
926 struct tip_subbuf *ts = subbuf_fifo_dequeue(tip);
929 return tip->flush_subbuf(tip, ts);
935 * scans the tips we know and writes out the subbuffers we accumulate
937 static void get_and_write_events(void)
939 struct device_information *dip;
940 struct thread_information *tip;
941 int i, j, events, ret, tips_running;
946 for_each_dip(dip, i) {
947 for_each_tip(dip, tip, j) {
948 ret = write_tip_events(tip);
964 for_each_dip(dip, i) {
965 for_each_tip(dip, tip, j) {
966 ret = write_tip_events(tip);
969 tips_running += !tip->exited;
973 } while (events || tips_running);
976 static void wait_for_threads(void)
979 * for piped or network output, poll and fetch data for writeout.
980 * for files, we just wait around for trace threads to exit
982 if ((output_name && !strcmp(output_name, "-")) ||
983 ((net_mode == Net_client) && !net_use_sendfile))
984 get_and_write_events();
986 struct device_information *dip;
987 struct thread_information *tip;
988 int i, j, tips_running;
995 for_each_tip(dip, tip, j)
996 tips_running += !tip->exited;
997 } while (tips_running);
1000 if (net_mode == Net_client)
1001 net_client_send_close();
1004 static int fill_ofname(struct device_information *dip,
1005 struct thread_information *tip, char *dst,
1012 len = sprintf(dst, "%s/", output_dir);
1014 len = sprintf(dst, "./");
1016 if (net_mode == Net_server) {
1017 struct net_connection *nc = tip->nc;
1019 len += sprintf(dst + len, "%s-", inet_ntoa(nc->ch->cl_in_addr));
1020 len += strftime(dst + len, 64, "%F-%T/", gmtime(&dip->cl_connect_time));
1023 if (stat(dst, &sb) < 0) {
1024 if (errno != ENOENT) {
1028 if (mkdir(dst, 0755) < 0) {
1030 fprintf(stderr, "Can't make output dir\n");
1036 sprintf(dst + len, "%s.blktrace.%d", output_name, tip->cpu);
1038 sprintf(dst + len, "%s.blktrace.%d", buts_name, tip->cpu);
1043 static void fill_ops(struct thread_information *tip)
1048 if (net_mode == Net_client) {
1049 if (net_use_sendfile) {
1050 tip->get_subbuf = get_subbuf_sendfile;
1051 tip->flush_subbuf = NULL;
1053 tip->get_subbuf = get_subbuf;
1054 tip->flush_subbuf = flush_subbuf_net;
1057 if (tip->ofile_mmap)
1058 tip->get_subbuf = mmap_subbuf;
1060 tip->get_subbuf = get_subbuf;
1062 tip->flush_subbuf = flush_subbuf_file;
1065 if (net_mode == Net_server)
1066 tip->read_data = read_data_net;
1068 tip->read_data = read_data_file;
1071 static int tip_open_output(struct device_information *dip,
1072 struct thread_information *tip)
1074 int pipeline = output_name && !strcmp(output_name, "-");
1075 int mode, vbuf_size;
1078 if (net_mode == Net_client) {
1080 tip->ofile_stdout = 0;
1081 tip->ofile_mmap = 0;
1083 } else if (pipeline) {
1084 tip->ofile = fdopen(STDOUT_FILENO, "w");
1085 tip->ofile_stdout = 1;
1086 tip->ofile_mmap = 0;
1090 if (fill_ofname(dip, tip, op, dip->buts_name))
1092 tip->ofile = fopen(op, "w+");
1093 tip->ofile_stdout = 0;
1094 tip->ofile_mmap = 1;
1096 vbuf_size = OFILE_BUF;
1099 if (tip->ofile == NULL) {
1104 tip->ofile_buffer = malloc(vbuf_size);
1105 if (setvbuf(tip->ofile, tip->ofile_buffer, mode, vbuf_size)) {
1116 static int start_threads(struct device_information *dip)
1118 struct thread_information *tip;
1121 for_each_tip(dip, tip, j) {
1124 tip->events_processed = 0;
1126 memset(&tip->fifo, 0, sizeof(tip->fifo));
1127 tip->leftover_ts = NULL;
1129 if (tip_open_output(dip, tip))
1132 if (pthread_create(&tip->thread, NULL, thread_main, tip)) {
1133 perror("pthread_create");
1142 static void stop_threads(struct device_information *dip)
1144 struct thread_information *tip;
1148 for_each_tip(dip, tip, i) {
1149 (void) pthread_join(tip->thread, (void *) &ret);
1154 static void stop_all_threads(void)
1156 struct device_information *dip;
1159 for_each_dip(dip, i)
1163 static void stop_all_tracing(void)
1165 struct device_information *dip;
1168 for_each_dip(dip, i)
1172 static void exit_trace(int status)
1174 if (!is_trace_stopped()) {
1183 static int resize_devices(char *path)
1185 int size = (ndevs + 1) * sizeof(struct device_information);
1187 device_information = realloc(device_information, size);
1188 if (!device_information) {
1189 fprintf(stderr, "Out of memory, device %s (%d)\n", path, size);
1192 device_information[ndevs].path = path;
1197 static int open_devices(void)
1199 struct device_information *dip;
1202 for_each_dip(dip, i) {
1203 dip->fd = open(dip->path, O_RDONLY | O_NONBLOCK);
1208 dip->buf_size = buf_size;
1209 dip->buf_nr = buf_nr;
1210 dip->page_size = page_size;
1216 static int start_devices(void)
1218 struct device_information *dip;
1221 size = ncpus * sizeof(struct thread_information);
1222 thread_information = malloc(size * ndevs);
1223 if (!thread_information) {
1224 fprintf(stderr, "Out of memory, threads (%d)\n", size * ndevs);
1227 memset(thread_information, 0, size * ndevs);
1229 for_each_dip(dip, i) {
1230 if (start_trace(dip)) {
1232 fprintf(stderr, "Failed to start trace on %s\n",
1239 __for_each_dip(dip, device_information, i, j)
1245 for_each_dip(dip, i) {
1246 dip->threads = thread_information + (i * ncpus);
1247 if (start_threads(dip)) {
1248 fprintf(stderr, "Failed to start worker threads\n");
1254 __for_each_dip(dip, device_information, i, j)
1256 for_each_dip(dip, i)
1265 static void show_stats(struct device_information *dips, int ndips, int cpus)
1267 struct device_information *dip;
1268 struct thread_information *tip;
1269 unsigned long long events_processed, data_read;
1270 unsigned long total_drops;
1271 int i, j, no_stdout = 0;
1273 if (is_stat_shown())
1276 if (output_name && !strcmp(output_name, "-"))
1282 __for_each_dip(dip, dips, ndips, i) {
1284 printf("Device: %s\n", dip->path);
1285 events_processed = 0;
1287 __for_each_tip(dip, tip, cpus, j) {
1289 printf(" CPU%3d: %20lu events, %8llu KiB data\n",
1290 tip->cpu, tip->events_processed,
1291 (tip->data_read + 1023) >> 10);
1292 events_processed += tip->events_processed;
1293 data_read += tip->data_read;
1295 total_drops += dip->drop_count;
1297 printf(" Total: %20llu events (dropped %lu), %8llu KiB data\n",
1298 events_processed, dip->drop_count,
1299 (data_read + 1023) >> 10);
1303 fprintf(stderr, "You have dropped events, consider using a larger buffer size (-b)\n");
1306 static struct device_information *net_get_dip(struct net_connection *nc,
1307 struct blktrace_net_hdr *bnh)
1309 struct device_information *dip, *cl_dip = NULL;
1310 struct cl_host *ch = nc->ch;
1313 for (i = 0; i < ch->ndevs; i++) {
1314 dip = &ch->device_information[i];
1316 if (!strcmp(dip->buts_name, bnh->buts_name))
1319 if (dip->cl_id == bnh->cl_id)
1323 ch->device_information = realloc(ch->device_information, (ch->ndevs + 1) * sizeof(*dip));
1324 dip = &ch->device_information[ch->ndevs];
1325 memset(dip, 0, sizeof(*dip));
1328 dip->cl_id = bnh->cl_id;
1329 dip->buf_size = bnh->buf_size;
1330 dip->buf_nr = bnh->buf_nr;
1331 dip->page_size = bnh->page_size;
1334 dip->cl_connect_time = cl_dip->cl_connect_time;
1336 dip->cl_connect_time = nc->connect_time;
1337 strcpy(dip->buts_name, bnh->buts_name);
1338 dip->path = strdup(bnh->buts_name);
1339 dip->trace_started = 1;
1341 dip->threads = malloc(nc->ncpus * sizeof(struct thread_information));
1342 memset(dip->threads, 0, nc->ncpus * sizeof(struct thread_information));
1347 for (i = 0; i < nc->ncpus; i++) {
1348 struct thread_information *tip = &dip->threads[i];
1355 if (tip_open_output(dip, tip))
1364 static struct thread_information *net_get_tip(struct net_connection *nc,
1365 struct blktrace_net_hdr *bnh)
1367 struct device_information *dip;
1368 struct thread_information *tip;
1370 dip = net_get_dip(nc, bnh);
1371 if (!dip->trace_started) {
1372 fprintf(stderr, "Events for closed devices %s\n", dip->buts_name);
1376 tip = &dip->threads[bnh->cpu];
1383 static int net_get_header(struct net_connection *nc,
1384 struct blktrace_net_hdr *bnh)
1386 int fl = fcntl(nc->in_fd, F_GETFL);
1387 int bytes_left, ret;
1390 fcntl(nc->in_fd, F_SETFL, fl | O_NONBLOCK);
1391 bytes_left = sizeof(*bnh);
1392 while (bytes_left && !is_done()) {
1393 ret = recv(nc->in_fd, p, bytes_left, MSG_WAITALL);
1395 if (errno != EAGAIN) {
1396 perror("recv header");
1409 fcntl(nc->in_fd, F_SETFL, fl & ~O_NONBLOCK);
1414 * finalize a net client: truncate files, show stats, cleanup, etc
1416 static void device_done(struct net_connection *nc, struct device_information *dip)
1418 struct thread_information *tip;
1421 __for_each_tip(dip, tip, nc->ncpus, i)
1422 tip_ftrunc_final(tip);
1424 show_stats(dip, 1, nc->ncpus);
1427 * cleanup for next run
1429 __for_each_tip(dip, tip, nc->ncpus, i) {
1443 static inline int in_addr_eq(struct in_addr a, struct in_addr b)
1445 return a.s_addr == b.s_addr;
1448 static void net_add_client_host(struct cl_host *ch)
1450 ch->list_next = cl_host_list;
1455 static void net_remove_client_host(struct cl_host *ch)
1457 struct cl_host *p, *c;
1459 for (p = c = cl_host_list; c; c = c->list_next) {
1462 cl_host_list = c->list_next;
1464 p->list_next = c->list_next;
1472 static struct cl_host *net_find_client_host(struct in_addr cl_in_addr)
1474 struct cl_host *ch = cl_host_list;
1477 if (in_addr_eq(ch->cl_in_addr, cl_in_addr))
1485 static void net_client_host_done(struct cl_host *ch)
1487 free(ch->device_information);
1488 free(ch->net_connections);
1489 net_connects -= ch->nconn;
1490 net_remove_client_host(ch);
1495 * handle incoming events from a net client
1497 static int net_client_data(struct net_connection *nc)
1499 struct thread_information *tip;
1500 struct blktrace_net_hdr bnh;
1502 if (net_get_header(nc, &bnh))
1505 if (data_is_native == -1 && check_data_endianness(bnh.magic)) {
1506 fprintf(stderr, "server: received data is bad\n");
1510 if (!data_is_native) {
1511 bnh.magic = be32_to_cpu(bnh.magic);
1512 bnh.cpu = be32_to_cpu(bnh.cpu);
1513 bnh.max_cpus = be32_to_cpu(bnh.max_cpus);
1514 bnh.len = be32_to_cpu(bnh.len);
1515 bnh.cl_id = be32_to_cpu(bnh.cl_id);
1516 bnh.buf_size = be32_to_cpu(bnh.buf_size);
1517 bnh.buf_nr = be32_to_cpu(bnh.buf_nr);
1518 bnh.page_size = be32_to_cpu(bnh.page_size);
1521 if ((bnh.magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) {
1522 fprintf(stderr, "server: bad data magic\n");
1526 if (nc->ncpus == -1)
1527 nc->ncpus = bnh.max_cpus;
1530 * len == 0 means that the other end signalled end-of-run
1534 * overload cpu count with dropped events
1536 struct device_information *dip;
1538 dip = net_get_dip(nc, &bnh);
1539 dip->drop_count = bnh.cpu;
1540 dip->trace_started = 0;
1542 printf("server: end of run for %s\n", dip->buts_name);
1544 device_done(nc, dip);
1546 if (++nc->ch->ndevs_done == nc->ch->ndevs)
1547 net_client_host_done(nc->ch);
1552 tip = net_get_tip(nc, &bnh);
1556 if (mmap_subbuf(tip, bnh.len))
1562 static void net_add_connection(int listen_fd, struct sockaddr_in *addr)
1564 socklen_t socklen = sizeof(*addr);
1565 struct net_connection *nc;
1569 in_fd = accept(listen_fd, (struct sockaddr *) addr, &socklen);
1575 ch = net_find_client_host(addr->sin_addr);
1577 if (cl_hosts == NET_MAX_CL_HOSTS) {
1578 fprintf(stderr, "server: no more clients allowed\n");
1581 ch = malloc(sizeof(struct cl_host));
1582 memset(ch, 0, sizeof(*ch));
1583 ch->cl_in_addr = addr->sin_addr;
1584 net_add_client_host(ch);
1586 printf("server: connection from %s\n", inet_ntoa(addr->sin_addr));
1589 ch->net_connections = realloc(ch->net_connections, (ch->nconn + 1) * sizeof(*nc));
1590 nc = &ch->net_connections[ch->nconn++];
1591 memset(nc, 0, sizeof(*nc));
1593 time(&nc->connect_time);
1601 * event driven loop, handle new incoming connections and data from
1602 * existing connections
1604 static void net_server_handle_connections(int listen_fd,
1605 struct sockaddr_in *addr)
1607 struct pollfd *pfds = NULL;
1608 struct net_connection **ncs = NULL;
1609 int max_connects = 0;
1610 int i, nconns, events;
1612 struct net_connection *nc;
1614 printf("server: waiting for connections...\n");
1616 while (!is_done()) {
1617 if (net_connects >= max_connects) {
1618 pfds = realloc(pfds, (net_connects + 1) * sizeof(*pfds));
1619 ncs = realloc(ncs, (net_connects + 1) * sizeof(*ncs));
1620 max_connects = net_connects + 1;
1623 * the zero entry is for incoming connections, remaining
1624 * entries for clients
1626 pfds[0].fd = listen_fd;
1627 pfds[0].events = POLLIN;
1629 for_each_cl_host(ch) {
1630 for (i = 0; i < ch->nconn; i++) {
1631 nc = &ch->net_connections[i];
1632 pfds[nconns + 1].fd = nc->in_fd;
1633 pfds[nconns + 1].events = POLLIN;
1638 events = poll(pfds, 1 + nconns, -1);
1648 if (pfds[0].revents & POLLIN) {
1649 net_add_connection(listen_fd, addr);
1653 for (i = 0; events && i < nconns; i++) {
1654 if (pfds[i + 1].revents & POLLIN) {
1655 net_client_data(ncs[i]);
1663 * Start here when we are in server mode - just fetch data from the network
1666 static int net_server(void)
1668 struct sockaddr_in addr;
1671 fd = socket(AF_INET, SOCK_STREAM, 0);
1673 perror("server: socket");
1678 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
1679 perror("setsockopt");
1683 memset(&addr, 0, sizeof(addr));
1684 addr.sin_family = AF_INET;
1685 addr.sin_addr.s_addr = htonl(INADDR_ANY);
1686 addr.sin_port = htons(net_port);
1688 if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1693 if (listen(fd, 1) < 0) {
1698 net_server_handle_connections(fd, &addr);
1703 * Setup outgoing network connection where we will transmit data
1705 static int net_setup_client_cpu(int i, struct sockaddr_in *addr)
1709 fd = socket(AF_INET, SOCK_STREAM, 0);
1711 perror("client: socket");
1715 if (connect(fd, (struct sockaddr *) addr, sizeof(*addr)) < 0) {
1716 perror("client: connect");
1724 static int net_setup_client(void)
1726 struct sockaddr_in addr;
1729 memset(&addr, 0, sizeof(addr));
1730 addr.sin_family = AF_INET;
1731 addr.sin_port = htons(net_port);
1733 if (inet_aton(hostname, &addr.sin_addr) != 1) {
1734 struct hostent *hent = gethostbyname(hostname);
1736 perror("gethostbyname");
1740 memcpy(&addr.sin_addr, hent->h_addr, 4);
1741 strcpy(hostname, hent->h_name);
1744 printf("blktrace: connecting to %s\n", hostname);
1746 net_out_fd = malloc(ncpus * sizeof(*net_out_fd));
1747 for (i = 0; i < ncpus; i++) {
1748 if (net_setup_client_cpu(i, &addr))
1752 printf("blktrace: connected!\n");
1757 static char usage_str[] = \
1758 "-d <dev> [ -r debugfs path ] [ -o <output> ] [-k ] [ -w time ]\n" \
1759 "[ -a action ] [ -A action mask ] [ -I <devs file> ] [ -v ]\n\n" \
1760 "\t-d Use specified device. May also be given last after options\n" \
1761 "\t-r Path to mounted debugfs, defaults to /sys/kernel/debug\n" \
1762 "\t-o File(s) to send output to\n" \
1763 "\t-D Directory to prepend to output file names\n" \
1764 "\t-k Kill a running trace\n" \
1765 "\t-w Stop after defined time, in seconds\n" \
1766 "\t-a Only trace specified actions. See documentation\n" \
1767 "\t-A Give trace mask as a single value. See documentation\n" \
1768 "\t-b Sub buffer size in KiB\n" \
1769 "\t-n Number of sub buffers\n" \
1770 "\t-l Run in network listen mode (blktrace server)\n" \
1771 "\t-h Run in network client mode, connecting to the given host\n" \
1772 "\t-p Network port to use (default 8462)\n" \
1773 "\t-s Make the network client NOT use sendfile() to transfer data\n" \
1774 "\t-I Add devices found in <devs file>\n" \
1775 "\t-V Print program version info\n\n";
1777 static void show_usage(char *program)
1779 fprintf(stderr, "Usage: %s %s %s",program, blktrace_version, usage_str);
1782 int main(int argc, char *argv[])
1784 static char default_debugfs_path[] = "/sys/kernel/debug";
1788 int act_mask_tmp = 0;
1790 while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) >= 0) {
1793 i = find_mask_map(optarg);
1795 fprintf(stderr,"Invalid action mask %s\n",
1803 if ((sscanf(optarg, "%x", &i) != 1) ||
1804 !valid_act_opt(i)) {
1806 "Invalid set action mask %s/0x%x\n",
1814 if (resize_devices(optarg) != 0)
1820 FILE *ifp = fopen(optarg, "r");
1824 "Invalid file for devices %s\n",
1829 while (fscanf(ifp, "%s\n", dev_line) == 1)
1830 if (resize_devices(strdup(dev_line)) != 0)
1837 debugfs_path = optarg;
1841 output_name = optarg;
1844 kill_running_trace = 1;
1847 stop_watch = atoi(optarg);
1848 if (stop_watch <= 0) {
1850 "Invalid stopwatch value (%d secs)\n",
1856 printf("%s version %s\n", argv[0], blktrace_version);
1859 buf_size = strtoul(optarg, NULL, 10);
1860 if (buf_size <= 0 || buf_size > 16*1024) {
1862 "Invalid buffer size (%lu)\n",buf_size);
1868 buf_nr = strtoul(optarg, NULL, 10);
1871 "Invalid buffer nr (%lu)\n", buf_nr);
1876 output_dir = optarg;
1879 net_mode = Net_client;
1880 strcpy(hostname, optarg);
1883 net_mode = Net_server;
1886 net_port = atoi(optarg);
1889 net_use_sendfile = 0;
1892 show_usage(argv[0]);
1897 setlocale(LC_NUMERIC, "en_US");
1899 page_size = getpagesize();
1901 if (net_mode == Net_server) {
1903 fprintf(stderr, "-o ignored in server mode\n");
1907 return net_server();
1910 while (optind < argc) {
1911 if (resize_devices(argv[optind++]) != 0)
1916 show_usage(argv[0]);
1920 if (act_mask_tmp != 0)
1921 act_mask = act_mask_tmp;
1924 debugfs_path = default_debugfs_path;
1926 if (statfs(debugfs_path, &st) < 0) {
1928 fprintf(stderr,"%s does not appear to be a valid path\n",
1931 } else if (st.f_type != (long) DEBUGFS_TYPE) {
1932 fprintf(stderr,"%s does not appear to be a debug filesystem\n",
1937 if (open_devices() != 0)
1940 if (kill_running_trace) {
1945 ncpus = sysconf(_SC_NPROCESSORS_ONLN);
1947 fprintf(stderr, "sysconf(_SC_NPROCESSORS_ONLN) failed\n");
1951 signal(SIGINT, handle_sigint);
1952 signal(SIGHUP, handle_sigint);
1953 signal(SIGTERM, handle_sigint);
1954 signal(SIGALRM, handle_sigint);
1955 signal(SIGPIPE, SIG_IGN);
1957 if (net_mode == Net_client && net_setup_client())
1960 if (start_devices() != 0)
1963 atexit(stop_all_tracing);
1970 if (!is_trace_stopped()) {
1976 show_stats(device_information, ndevs, ncpus);