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;
166 #define FIFO_SIZE (1024) /* should be plenty big! */
167 #define CL_SIZE (128) /* cache line, any bigger? */
169 struct tip_subbuf_fifo {
170 int tail __attribute__((aligned(CL_SIZE)));
171 int head __attribute__((aligned(CL_SIZE)));
172 struct tip_subbuf *q[FIFO_SIZE];
175 struct thread_information {
181 char fn[MAXPATHLEN + 64];
192 int (*get_subbuf)(struct thread_information *, unsigned int);
193 int (*flush_subbuf)(struct thread_information *, struct tip_subbuf *);
194 int (*read_data)(struct thread_information *, void *, unsigned int);
196 unsigned long events_processed;
197 unsigned long long data_read;
198 struct device_information *device;
205 struct tip_subbuf_fifo fifo;
206 struct tip_subbuf *leftover_ts;
209 * mmap controlled output files
211 unsigned long long fs_size;
212 unsigned long long fs_max_size;
213 unsigned long fs_off;
215 unsigned long fs_buf_len;
218 struct device_information {
222 volatile int trace_started;
223 unsigned long drop_count;
224 struct thread_information *threads;
228 static struct thread_information *thread_information;
230 static struct device_information *device_information;
232 /* command line option globals */
233 static char *relay_path;
234 static char *output_name;
235 static char *output_dir;
236 static int act_mask = ~0U;
237 static int kill_running_trace;
238 static unsigned long buf_size = BUF_SIZE;
239 static unsigned long buf_nr = BUF_NR;
240 static unsigned int page_size;
242 #define is_done() (*(volatile int *)(&done))
243 static volatile int done;
245 #define is_trace_stopped() (*(volatile int *)(&trace_stopped))
246 static volatile int trace_stopped;
248 #define is_stat_shown() (*(volatile int *)(&stat_shown))
249 static volatile int stat_shown;
251 int data_is_native = -1;
253 static void exit_trace(int status);
255 #define dip_tracing(dip) (*(volatile int *)(&(dip)->trace_started))
256 #define dip_set_tracing(dip, v) ((dip)->trace_started = (v))
258 #define __for_each_dip(__d, __i, __e) \
259 for (__i = 0, __d = device_information; __i < __e; __i++, __d++)
261 #define for_each_dip(__d, __i) __for_each_dip(__d, __i, ndevs)
262 #define for_each_tip(__d, __t, __j) \
263 for (__j = 0, __t = (__d)->threads; __j < ncpus; __j++, __t++)
266 * networking stuff follows. we include a magic number so we know whether
267 * to endianness convert or not
269 struct blktrace_net_hdr {
270 u32 magic; /* same as trace magic */
271 char buts_name[32]; /* trace name */
272 u32 cpu; /* for which cpu */
274 u32 len; /* length of following trace data */
277 #define TRACE_NET_PORT (8462)
286 * network cmd line params
288 static char hostname[MAXHOSTNAMELEN];
289 static int net_port = TRACE_NET_PORT;
290 static int net_mode = 0;
291 static int net_sendfile;
293 static int net_in_fd = -1;
294 static int net_out_fd = -1;
296 static void handle_sigint(__attribute__((__unused__)) int sig)
301 static int get_dropped_count(const char *buts_name)
304 char tmp[MAXPATHLEN + 64];
306 snprintf(tmp, sizeof(tmp), "%s/block/%s/dropped",
307 relay_path, buts_name);
309 fd = open(tmp, O_RDONLY);
312 * this may be ok, if the kernel doesn't support dropped counts
317 fprintf(stderr, "Couldn't open dropped file %s\n", tmp);
321 if (read(fd, tmp, sizeof(tmp)) < 0) {
332 static size_t get_subbuf_padding(struct thread_information *tip,
335 size_t padding_size = buf_nr * sizeof(size_t);
338 if (read(tip->pfd, tip->pfd_buf, padding_size) < 0) {
339 perror("tip pad read");
342 ret = tip->pfd_buf[subbuf];
347 static int start_trace(struct device_information *dip)
349 struct blk_user_trace_setup buts;
351 memset(&buts, 0, sizeof(buts));
352 buts.buf_size = buf_size;
353 buts.buf_nr = buf_nr;
354 buts.act_mask = act_mask;
356 if (ioctl(dip->fd, BLKTRACESETUP, &buts) < 0) {
357 perror("BLKTRACESETUP");
361 if (ioctl(dip->fd, BLKTRACESTART) < 0) {
362 perror("BLKTRACESTART");
366 memcpy(dip->buts_name, buts.name, sizeof(dip->buts_name));
367 dip_set_tracing(dip, 1);
371 static void stop_trace(struct device_information *dip)
373 if (dip_tracing(dip) || kill_running_trace) {
374 dip_set_tracing(dip, 0);
376 if (ioctl(dip->fd, BLKTRACESTOP) < 0)
377 perror("BLKTRACESTOP");
378 if (ioctl(dip->fd, BLKTRACETEARDOWN) < 0)
379 perror("BLKTRACETEARDOWN");
386 static void stop_all_traces(void)
388 struct device_information *dip;
391 for_each_dip(dip, i) {
392 dip->drop_count = get_dropped_count(dip->buts_name);
397 static void wait_for_data(struct thread_information *tip)
399 struct pollfd pfd = { .fd = tip->fd, .events = POLLIN };
403 if (pfd.revents & POLLIN)
405 if (tip->ofile_stdout)
407 } while (!is_done());
410 static int read_data_file(struct thread_information *tip, void *buf,
418 ret = read(tip->fd, buf, len);
424 if (errno != EAGAIN) {
426 fprintf(stderr,"Thread %d failed read of %s\n",
432 } while (!is_done());
438 static int read_data_net(struct thread_information *tip, void *buf,
441 unsigned int bytes_left = len;
445 ret = recv(net_in_fd, buf, bytes_left, MSG_WAITALL);
450 if (errno != EAGAIN) {
452 fprintf(stderr, "server: failed read\n");
460 } while (!is_done() && bytes_left);
462 return len - bytes_left;
465 static int read_data(struct thread_information *tip, void *buf,
468 return tip->read_data(tip, buf, len);
471 static inline struct tip_subbuf *
472 subbuf_fifo_dequeue(struct thread_information *tip)
474 const int head = tip->fifo.head;
475 const int next = (head + 1) & (FIFO_SIZE - 1);
477 if (head != tip->fifo.tail) {
478 struct tip_subbuf *ts = tip->fifo.q[head];
481 tip->fifo.head = next;
488 static inline int subbuf_fifo_queue(struct thread_information *tip,
489 struct tip_subbuf *ts)
491 const int tail = tip->fifo.tail;
492 const int next = (tail + 1) & (FIFO_SIZE - 1);
494 if (next != tip->fifo.head) {
495 tip->fifo.q[tail] = ts;
497 tip->fifo.tail = next;
501 fprintf(stderr, "fifo too small!\n");
506 * For file output, truncate and mmap the file appropriately
508 static int mmap_subbuf(struct thread_information *tip, unsigned int maxlen)
510 int ofd = fileno(tip->ofile);
514 * extend file, if we have to. use chunks of 16 subbuffers.
516 if (tip->fs_off + buf_size > tip->fs_buf_len) {
518 munlock(tip->fs_buf, tip->fs_buf_len);
519 munmap(tip->fs_buf, tip->fs_buf_len);
523 tip->fs_off = tip->fs_size & (page_size - 1);
524 tip->fs_buf_len = (16 * buf_size) - tip->fs_off;
525 tip->fs_max_size += tip->fs_buf_len;
527 if (ftruncate(ofd, tip->fs_max_size) < 0) {
532 tip->fs_buf = mmap(NULL, tip->fs_buf_len, PROT_WRITE,
533 MAP_SHARED, ofd, tip->fs_size - tip->fs_off);
534 if (tip->fs_buf == MAP_FAILED) {
538 mlock(tip->fs_buf, tip->fs_buf_len);
541 ret = read_data(tip, tip->fs_buf + tip->fs_off, maxlen);
543 tip->data_read += ret;
553 * Use the copy approach for pipes and network
555 static int get_subbuf(struct thread_information *tip, unsigned int maxlen)
557 struct tip_subbuf *ts = malloc(sizeof(*ts));
560 ts->buf = malloc(buf_size);
561 ts->max_len = maxlen;
563 ret = read_data(tip, ts->buf, ts->max_len);
566 tip->data_read += ret;
567 return subbuf_fifo_queue(tip, ts);
573 static int get_subbuf_sendfile(struct thread_information *tip,
576 struct tip_subbuf *ts;
578 unsigned int ready, this_size;
584 * hack to get last data out, we can't use sendfile for that
587 return get_subbuf(tip, maxlen);
589 if (fstat(tip->fd, &sb) < 0) {
590 perror("trace stat");
594 ready = sb.st_size - tip->ofile_offset;
597 * delay a little, since we poll() will return data available
598 * until sendfile() is run
604 this_size = buf_size;
606 if (this_size > ready)
609 ts = malloc(sizeof(*ts));
611 ts->max_len = maxlen;
615 ts->max_len = ts->len;
616 ts->offset = tip->ofile_offset;
617 tip->ofile_offset += ts->len;
619 err = subbuf_fifo_queue(tip, ts);
629 static void close_thread(struct thread_information *tip)
637 if (tip->ofile_buffer)
638 free(tip->ofile_buffer);
647 tip->ofile_buffer = NULL;
651 static void tip_ftrunc_final(struct thread_information *tip)
654 * truncate to right size and cleanup mmap
656 if (tip->ofile_mmap) {
657 int ofd = fileno(tip->ofile);
660 munmap(tip->fs_buf, tip->fs_buf_len);
662 ftruncate(ofd, tip->fs_size);
666 static void *thread_main(void *arg)
668 struct thread_information *tip = arg;
669 pid_t pid = getpid();
673 CPU_SET((tip->cpu), &cpu_mask);
675 if (sched_setaffinity(pid, sizeof(cpu_mask), &cpu_mask) == -1) {
676 perror("sched_setaffinity");
680 snprintf(tip->fn, sizeof(tip->fn), "%s/block/%s/trace%d",
681 relay_path, tip->device->buts_name, tip->cpu);
682 tip->fd = open(tip->fn, O_RDONLY);
685 fprintf(stderr,"Thread %d failed open of %s\n", tip->cpu,
690 if (net_mode == Net_client && net_sendfile) {
691 char tmp[MAXPATHLEN + 64];
693 snprintf(tmp, sizeof(tmp), "%s/block/%s/trace%d.padding",
694 relay_path, tip->device->buts_name, tip->cpu);
696 tip->pfd = open(tmp, O_RDONLY);
698 fprintf(stderr, "Couldn't open padding file %s\n", tmp);
702 tip->pfd_buf = malloc(buf_nr * sizeof(size_t));
706 if (tip->get_subbuf(tip, buf_size))
710 tip_ftrunc_final(tip);
715 static int write_data_net(int fd, void *buf, unsigned int buf_len)
717 unsigned int bytes_left = buf_len;
721 ret = send(fd, buf, bytes_left, 0);
734 static int net_send_header(struct thread_information *tip, unsigned int len)
736 struct blktrace_net_hdr hdr;
738 hdr.magic = BLK_IO_TRACE_MAGIC;
739 strcpy(hdr.buts_name, tip->device->buts_name);
741 hdr.max_cpus = ncpus;
744 return write_data_net(net_out_fd, &hdr, sizeof(hdr));
748 * send header with 0 length to signal end-of-run
750 static void net_client_send_close(void)
752 struct blktrace_net_hdr hdr;
754 hdr.magic = BLK_IO_TRACE_MAGIC;
756 hdr.max_cpus = ncpus;
759 write_data_net(net_out_fd, &hdr, sizeof(hdr));
762 static int flush_subbuf_net(struct thread_information *tip,
763 struct tip_subbuf *ts)
765 if (net_send_header(tip, ts->len))
767 if (write_data_net(net_out_fd, ts->buf, ts->len))
770 tip->data_read += ts->len;
776 static int flush_subbuf_sendfile(struct thread_information *tip,
777 struct tip_subbuf *ts)
784 * currently we cannot use sendfile() on the last bytes read, as they
785 * may not be a full subbuffer. get_subbuf_sendfile() falls back to
786 * the read approach for those, so use send() to ship them out
789 return flush_subbuf_net(tip, ts);
791 subbuf = (ts->offset / buf_size) % buf_nr;
792 padding = get_subbuf_padding(tip, subbuf);
793 len = ts->len - padding;
795 if (net_send_header(tip, len))
797 if (sendfile(net_out_fd, tip->fd, &ts->offset, len) < 0) {
802 tip->data_read += ts->len;
807 static int write_data(struct thread_information *tip, void *buf,
808 unsigned int buf_len)
816 ret = fwrite(buf, buf_len, 1, tip->ofile);
826 if (tip->ofile_stdout)
832 static int flush_subbuf_file(struct thread_information *tip,
833 struct tip_subbuf *ts)
835 unsigned int offset = 0;
836 struct blk_io_trace *t;
837 int pdu_len, events = 0;
840 * surplus from last run
842 if (tip->leftover_ts) {
843 struct tip_subbuf *prev_ts = tip->leftover_ts;
845 if (prev_ts->len + ts->len > prev_ts->max_len) {
846 prev_ts->max_len += ts->len;
847 prev_ts->buf = realloc(prev_ts->buf, prev_ts->max_len);
850 memcpy(prev_ts->buf + prev_ts->len, ts->buf, ts->len);
851 prev_ts->len += ts->len;
857 tip->leftover_ts = NULL;
860 while (offset + sizeof(*t) <= ts->len) {
861 t = ts->buf + offset;
863 if (verify_trace(t)) {
864 write_data(tip, ts->buf, offset);
868 pdu_len = t->pdu_len;
870 if (offset + sizeof(*t) + pdu_len > ts->len)
873 offset += sizeof(*t) + pdu_len;
874 tip->events_processed++;
875 tip->data_read += sizeof(*t) + pdu_len;
879 if (write_data(tip, ts->buf, offset))
883 * leftover bytes, save them for next time
885 if (offset != ts->len) {
886 tip->leftover_ts = ts;
888 memmove(ts->buf, ts->buf + offset, ts->len);
897 static int write_tip_events(struct thread_information *tip)
899 struct tip_subbuf *ts = subbuf_fifo_dequeue(tip);
902 return tip->flush_subbuf(tip, ts);
908 * scans the tips we know and writes out the subbuffers we accumulate
910 static void get_and_write_events(void)
912 struct device_information *dip;
913 struct thread_information *tip;
914 int i, j, events, ret, tips_running;
919 for_each_dip(dip, i) {
920 for_each_tip(dip, tip, j) {
921 ret = write_tip_events(tip);
937 for_each_dip(dip, i) {
938 for_each_tip(dip, tip, j) {
939 ret = write_tip_events(tip);
942 tips_running += !tip->exited;
946 } while (events || tips_running);
949 static void wait_for_threads(void)
952 * for piped or network output, poll and fetch data for writeout.
953 * for files, we just wait around for trace threads to exit
955 if ((output_name && !strcmp(output_name, "-")) ||
956 net_mode == Net_client)
957 get_and_write_events();
959 struct device_information *dip;
960 struct thread_information *tip;
961 int i, j, tips_running;
968 for_each_tip(dip, tip, j)
969 tips_running += !tip->exited;
970 } while (tips_running);
973 if (net_mode == Net_client)
974 net_client_send_close();
977 static void fill_ofname(char *dst, char *buts_name, int cpu)
982 len = sprintf(dst, "%s/", output_dir);
985 sprintf(dst + len, "%s.blktrace.%d", output_name, cpu);
987 sprintf(dst + len, "%s.blktrace.%d", buts_name, cpu);
990 static void fill_ops(struct thread_information *tip)
995 if (net_mode == Net_client) {
997 tip->get_subbuf = get_subbuf_sendfile;
998 tip->flush_subbuf = flush_subbuf_sendfile;
1000 tip->get_subbuf = get_subbuf;
1001 tip->flush_subbuf = flush_subbuf_net;
1004 if (tip->ofile_mmap)
1005 tip->get_subbuf = mmap_subbuf;
1007 tip->get_subbuf = get_subbuf;
1009 tip->flush_subbuf = flush_subbuf_file;
1012 if (net_mode == Net_server)
1013 tip->read_data = read_data_net;
1015 tip->read_data = read_data_file;
1018 static int tip_open_output(struct device_information *dip,
1019 struct thread_information *tip)
1021 int pipeline = output_name && !strcmp(output_name, "-");
1022 int mode, vbuf_size;
1025 if (net_mode == Net_client) {
1027 tip->ofile_stdout = 0;
1028 tip->ofile_mmap = 0;
1030 mode = 0; /* gcc 4.x issues a bogus warning */
1031 } else if (pipeline) {
1032 tip->ofile = fdopen(STDOUT_FILENO, "w");
1033 tip->ofile_stdout = 1;
1034 tip->ofile_mmap = 0;
1038 fill_ofname(op, dip->buts_name, tip->cpu);
1039 tip->ofile = fopen(op, "w+");
1040 tip->ofile_stdout = 0;
1041 tip->ofile_mmap = 1;
1043 vbuf_size = OFILE_BUF;
1046 if (net_mode != Net_client && tip->ofile == NULL) {
1052 tip->ofile_buffer = malloc(vbuf_size);
1053 if (setvbuf(tip->ofile, tip->ofile_buffer, mode, vbuf_size)) {
1064 static int start_threads(struct device_information *dip)
1066 struct thread_information *tip;
1069 for_each_tip(dip, tip, j) {
1072 tip->events_processed = 0;
1075 memset(&tip->fifo, 0, sizeof(tip->fifo));
1076 tip->leftover_ts = NULL;
1078 if (tip_open_output(dip, tip))
1081 if (pthread_create(&tip->thread, NULL, thread_main, tip)) {
1082 perror("pthread_create");
1091 static void stop_threads(struct device_information *dip)
1093 struct thread_information *tip;
1097 for_each_tip(dip, tip, i) {
1098 (void) pthread_join(tip->thread, (void *) &ret);
1103 static void stop_all_threads(void)
1105 struct device_information *dip;
1108 for_each_dip(dip, i)
1112 static void stop_all_tracing(void)
1114 struct device_information *dip;
1117 for_each_dip(dip, i)
1121 static void exit_trace(int status)
1123 if (!is_trace_stopped()) {
1132 static int resize_devices(char *path)
1134 int size = (ndevs + 1) * sizeof(struct device_information);
1136 device_information = realloc(device_information, size);
1137 if (!device_information) {
1138 fprintf(stderr, "Out of memory, device %s (%d)\n", path, size);
1141 device_information[ndevs].path = path;
1146 static int open_devices(void)
1148 struct device_information *dip;
1151 for_each_dip(dip, i) {
1152 dip->fd = open(dip->path, O_RDONLY | O_NONBLOCK);
1162 static int start_devices(void)
1164 struct device_information *dip;
1167 size = ncpus * sizeof(struct thread_information);
1168 thread_information = malloc(size * ndevs);
1169 if (!thread_information) {
1170 fprintf(stderr, "Out of memory, threads (%d)\n", size * ndevs);
1174 for_each_dip(dip, i) {
1175 if (start_trace(dip)) {
1177 fprintf(stderr, "Failed to start trace on %s\n",
1184 __for_each_dip(dip, j, i)
1190 for_each_dip(dip, i) {
1191 dip->threads = thread_information + (i * ncpus);
1192 if (start_threads(dip)) {
1193 fprintf(stderr, "Failed to start worker threads\n");
1199 __for_each_dip(dip, j, i)
1201 for_each_dip(dip, i)
1210 static void show_stats(void)
1212 struct device_information *dip;
1213 struct thread_information *tip;
1214 unsigned long long events_processed, data_read;
1215 unsigned long total_drops;
1216 int i, j, no_stdout = 0;
1218 if (is_stat_shown())
1221 if (output_name && !strcmp(output_name, "-"))
1227 for_each_dip(dip, i) {
1229 printf("Device: %s\n", dip->path);
1230 events_processed = 0;
1232 for_each_tip(dip, tip, j) {
1234 printf(" CPU%3d: %20lu events, %8llu KiB data\n",
1235 tip->cpu, tip->events_processed,
1236 (tip->data_read + 1023) >> 10);
1237 events_processed += tip->events_processed;
1238 data_read += tip->data_read;
1240 total_drops += dip->drop_count;
1242 printf(" Total: %20llu events (dropped %lu), %8llu KiB data\n",
1243 events_processed, dip->drop_count,
1244 (data_read + 1023) >> 10);
1248 fprintf(stderr, "You have dropped events, consider using a larger buffer size (-b)\n");
1251 static struct device_information *net_get_dip(char *buts_name)
1253 struct device_information *dip;
1256 for (i = 0; i < ndevs; i++) {
1257 dip = &device_information[i];
1259 if (!strcmp(dip->buts_name, buts_name))
1263 device_information = realloc(device_information, (ndevs + 1) * sizeof(*dip));
1264 dip = &device_information[ndevs];
1265 strcpy(dip->buts_name, buts_name);
1266 strcpy(dip->path, buts_name);
1268 dip->threads = malloc(ncpus * sizeof(struct thread_information));
1269 memset(dip->threads, 0, ncpus * sizeof(struct thread_information));
1274 for (i = 0; i < ncpus; i++) {
1275 struct thread_information *tip = &dip->threads[i];
1280 if (tip_open_output(dip, tip))
1287 static struct thread_information *net_get_tip(struct blktrace_net_hdr *bnh)
1289 struct device_information *dip;
1291 ncpus = bnh->max_cpus;
1292 dip = net_get_dip(bnh->buts_name);
1293 return &dip->threads[bnh->cpu];
1296 static int net_get_header(struct blktrace_net_hdr *bnh)
1298 int fl = fcntl(net_in_fd, F_GETFL);
1299 int bytes_left, ret;
1302 fcntl(net_in_fd, F_SETFL, fl | O_NONBLOCK);
1303 bytes_left = sizeof(*bnh);
1304 while (bytes_left && !is_done()) {
1305 ret = recv(net_in_fd, p, bytes_left, MSG_WAITALL);
1307 if (errno != EAGAIN) {
1308 perror("recv header");
1321 fcntl(net_in_fd, F_SETFL, fl & ~O_NONBLOCK);
1325 static int net_server_loop(void)
1327 struct thread_information *tip;
1328 struct blktrace_net_hdr bnh;
1330 if (net_get_header(&bnh))
1333 if (data_is_native == -1 && check_data_endianness(bnh.magic)) {
1334 fprintf(stderr, "server: received data is bad\n");
1338 if (!data_is_native) {
1339 bnh.cpu = be32_to_cpu(bnh.cpu);
1340 bnh.len = be32_to_cpu(bnh.len);
1344 * len == 0 means that the other end signalled end-of-run
1347 fprintf(stderr, "server: end of run\n");
1351 tip = net_get_tip(&bnh);
1355 if (mmap_subbuf(tip, bnh.len))
1362 * Start here when we are in server mode - just fetch data from the network
1365 static int net_server(void)
1367 struct device_information *dip;
1368 struct thread_information *tip;
1369 struct sockaddr_in addr;
1373 fd = socket(AF_INET, SOCK_STREAM, 0);
1375 perror("server: socket");
1380 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
1381 perror("setsockopt");
1385 memset(&addr, 0, sizeof(addr));
1386 addr.sin_family = AF_INET;
1387 addr.sin_addr.s_addr = htonl(INADDR_ANY);
1388 addr.sin_port = htons(net_port);
1390 if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1395 if (listen(fd, 1) < 0) {
1401 signal(SIGINT, NULL);
1402 signal(SIGHUP, NULL);
1403 signal(SIGTERM, NULL);
1404 signal(SIGALRM, NULL);
1406 printf("blktrace: waiting for incoming connection...\n");
1408 socklen = sizeof(addr);
1409 net_in_fd = accept(fd, (struct sockaddr *) &addr, &socklen);
1410 if (net_in_fd < 0) {
1415 signal(SIGINT, handle_sigint);
1416 signal(SIGHUP, handle_sigint);
1417 signal(SIGTERM, handle_sigint);
1418 signal(SIGALRM, handle_sigint);
1420 printf("blktrace: connection from %s\n", inet_ntoa(addr.sin_addr));
1422 while (!is_done()) {
1423 if (net_server_loop())
1427 for_each_dip(dip, i)
1428 for_each_tip(dip, tip, j)
1429 tip_ftrunc_final(tip);
1437 * cleanup for next run
1439 for_each_dip(dip, i) {
1440 for_each_tip(dip, tip, j)
1446 free(device_information);
1447 device_information = NULL;
1457 * Setup outgoing network connection where we will transmit data
1459 static int net_setup_client(void)
1461 struct sockaddr_in addr;
1464 fd = socket(AF_INET, SOCK_STREAM, 0);
1466 perror("client: socket");
1470 memset(&addr, 0, sizeof(addr));
1471 addr.sin_family = AF_INET;
1472 addr.sin_port = htons(net_port);
1474 if (inet_aton(hostname, &addr.sin_addr) != 1) {
1475 struct hostent *hent = gethostbyname(hostname);
1477 perror("gethostbyname");
1481 memcpy(&addr.sin_addr, hent->h_addr, 4);
1482 strcpy(hostname, hent->h_name);
1485 printf("blktrace: connecting to %s\n", hostname);
1487 if (connect(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1488 perror("client: connect");
1492 printf("blktrace: connected!\n");
1497 static char usage_str[] = \
1498 "-d <dev> [ -r relay path ] [ -o <output> ] [-k ] [ -w time ]\n" \
1499 "[ -a action ] [ -A action mask ] [ -v ]\n\n" \
1500 "\t-d Use specified device. May also be given last after options\n" \
1501 "\t-r Path to mounted relayfs, defaults to /relay\n" \
1502 "\t-o File(s) to send output to\n" \
1503 "\t-D Directory to prepend to output file names\n" \
1504 "\t-k Kill a running trace\n" \
1505 "\t-w Stop after defined time, in seconds\n" \
1506 "\t-a Only trace specified actions. See documentation\n" \
1507 "\t-A Give trace mask as a single value. See documentation\n" \
1508 "\t-b Sub buffer size in KiB\n" \
1509 "\t-n Number of sub buffers\n" \
1510 "\t-l Run in network listen mode (blktrace server)\n" \
1511 "\t-h Run in network client mode, connecting to the given host\n" \
1512 "\t-p Network port to use (default 8462)\n" \
1513 "\t-s Make the network client use sendfile() to transfer data\n" \
1514 "\t-V Print program version info\n\n";
1516 static void show_usage(char *program)
1518 fprintf(stderr, "Usage: %s %s %s",program, blktrace_version, usage_str);
1521 int main(int argc, char *argv[])
1523 static char default_relay_path[] = "/relay";
1527 int act_mask_tmp = 0;
1529 while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) >= 0) {
1532 i = find_mask_map(optarg);
1534 fprintf(stderr,"Invalid action mask %s\n",
1542 if ((sscanf(optarg, "%x", &i) != 1) ||
1543 !valid_act_opt(i)) {
1545 "Invalid set action mask %s/0x%x\n",
1553 if (resize_devices(optarg) != 0)
1558 relay_path = optarg;
1562 output_name = optarg;
1565 kill_running_trace = 1;
1568 stop_watch = atoi(optarg);
1569 if (stop_watch <= 0) {
1571 "Invalid stopwatch value (%d secs)\n",
1577 printf("%s version %s\n", argv[0], blktrace_version);
1580 buf_size = strtoul(optarg, NULL, 10);
1581 if (buf_size <= 0 || buf_size > 16*1024) {
1583 "Invalid buffer size (%lu)\n",buf_size);
1589 buf_nr = strtoul(optarg, NULL, 10);
1592 "Invalid buffer nr (%lu)\n", buf_nr);
1597 output_dir = optarg;
1600 net_mode = Net_client;
1601 strcpy(hostname, optarg);
1604 net_mode = Net_server;
1607 net_port = atoi(optarg);
1613 show_usage(argv[0]);
1618 setlocale(LC_NUMERIC, "en_US");
1620 page_size = getpagesize();
1622 if (net_mode == Net_server)
1623 return net_server();
1625 while (optind < argc) {
1626 if (resize_devices(argv[optind++]) != 0)
1631 show_usage(argv[0]);
1636 relay_path = default_relay_path;
1638 if (act_mask_tmp != 0)
1639 act_mask = act_mask_tmp;
1641 if (statfs(relay_path, &st) < 0) {
1643 fprintf(stderr,"%s does not appear to be a valid path\n",
1646 } else if (st.f_type != (long) RELAYFS_TYPE) {
1647 fprintf(stderr,"%s does not appear to be a relay filesystem\n",
1652 if (open_devices() != 0)
1655 if (kill_running_trace) {
1660 ncpus = sysconf(_SC_NPROCESSORS_ONLN);
1662 fprintf(stderr, "sysconf(_SC_NPROCESSORS_ONLN) failed\n");
1666 signal(SIGINT, handle_sigint);
1667 signal(SIGHUP, handle_sigint);
1668 signal(SIGTERM, handle_sigint);
1669 signal(SIGALRM, handle_sigint);
1671 if (net_mode == Net_client && net_setup_client())
1674 if (start_devices() != 0)
1677 atexit(stop_all_tracing);
1684 if (!is_trace_stopped()) {