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];
189 int (*get_subbuf)(struct thread_information *, unsigned int);
190 int (*flush_subbuf)(struct thread_information *, struct tip_subbuf *);
191 int (*read_data)(struct thread_information *, void *, unsigned int);
193 unsigned long events_processed;
194 unsigned long long data_read;
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 thread_information *threads;
225 static struct thread_information *thread_information;
227 static struct device_information *device_information;
229 /* command line option globals */
230 static char *relay_path;
231 static char *output_name;
232 static char *output_dir;
233 static int act_mask = ~0U;
234 static int kill_running_trace;
235 static unsigned long buf_size = BUF_SIZE;
236 static unsigned long buf_nr = BUF_NR;
237 static unsigned int page_size;
239 #define is_done() (*(volatile int *)(&done))
240 static volatile int done;
242 #define is_trace_stopped() (*(volatile int *)(&trace_stopped))
243 static volatile int trace_stopped;
245 #define is_stat_shown() (*(volatile int *)(&stat_shown))
246 static volatile int stat_shown;
248 int data_is_native = -1;
250 static void exit_trace(int status);
252 #define dip_tracing(dip) (*(volatile int *)(&(dip)->trace_started))
253 #define dip_set_tracing(dip, v) ((dip)->trace_started = (v))
255 #define __for_each_dip(__d, __i, __e) \
256 for (__i = 0, __d = device_information; __i < __e; __i++, __d++)
258 #define for_each_dip(__d, __i) __for_each_dip(__d, __i, ndevs)
259 #define for_each_tip(__d, __t, __j) \
260 for (__j = 0, __t = (__d)->threads; __j < ncpus; __j++, __t++)
263 * networking stuff follows. we include a magic number so we know whether
264 * to endianness convert or not
266 struct blktrace_net_hdr {
267 u32 magic; /* same as trace magic */
268 char buts_name[32]; /* trace name */
269 u32 cpu; /* for which cpu */
271 u32 len; /* length of following trace data */
274 #define TRACE_NET_PORT (8462)
283 * network cmd line params
285 static char hostname[MAXHOSTNAMELEN];
286 static int net_port = TRACE_NET_PORT;
287 static int net_mode = 0;
288 static int net_sendfile;
290 static int net_in_fd = -1;
291 static int net_out_fd = -1;
293 static void handle_sigint(__attribute__((__unused__)) int sig)
298 static int get_dropped_count(const char *buts_name)
301 char tmp[MAXPATHLEN + 64];
303 snprintf(tmp, sizeof(tmp), "%s/block/%s/dropped",
304 relay_path, buts_name);
306 fd = open(tmp, O_RDONLY);
309 * this may be ok, if the kernel doesn't support dropped counts
314 fprintf(stderr, "Couldn't open dropped file %s\n", tmp);
318 if (read(fd, tmp, sizeof(tmp)) < 0) {
329 static int start_trace(struct device_information *dip)
331 struct blk_user_trace_setup buts;
333 memset(&buts, 0, sizeof(buts));
334 buts.buf_size = buf_size;
335 buts.buf_nr = buf_nr;
336 buts.act_mask = act_mask;
338 if (ioctl(dip->fd, BLKTRACESETUP, &buts) < 0) {
339 perror("BLKTRACESETUP");
343 if (ioctl(dip->fd, BLKTRACESTART) < 0) {
344 perror("BLKTRACESTART");
348 memcpy(dip->buts_name, buts.name, sizeof(dip->buts_name));
349 dip_set_tracing(dip, 1);
353 static void stop_trace(struct device_information *dip)
355 if (dip_tracing(dip) || kill_running_trace) {
356 dip_set_tracing(dip, 0);
358 if (ioctl(dip->fd, BLKTRACESTOP) < 0)
359 perror("BLKTRACESTOP");
360 if (ioctl(dip->fd, BLKTRACETEARDOWN) < 0)
361 perror("BLKTRACETEARDOWN");
368 static void stop_all_traces(void)
370 struct device_information *dip;
373 for_each_dip(dip, i) {
374 dip->drop_count = get_dropped_count(dip->buts_name);
379 static void wait_for_data(struct thread_information *tip)
381 struct pollfd pfd = { .fd = tip->fd, .events = POLLIN };
385 if (pfd.revents & POLLIN)
387 if (tip->ofile_stdout)
389 } while (!is_done());
392 static int read_data_file(struct thread_information *tip, void *buf,
400 ret = read(tip->fd, buf, len);
406 if (errno != EAGAIN) {
408 fprintf(stderr,"Thread %d failed read of %s\n",
414 } while (!is_done());
420 static int read_data_net(struct thread_information *tip, void *buf,
423 unsigned int bytes_left = len;
427 ret = recv(net_in_fd, buf, bytes_left, MSG_WAITALL);
432 if (errno != EAGAIN) {
434 fprintf(stderr, "server: failed read\n");
442 } while (!is_done() && bytes_left);
444 return len - bytes_left;
447 static int read_data(struct thread_information *tip, void *buf,
450 int ret = tip->read_data(tip, buf, len);
453 tip->data_read += ret;
458 static inline struct tip_subbuf *
459 subbuf_fifo_dequeue(struct thread_information *tip)
461 const int head = tip->fifo.head;
462 const int next = (head + 1) & (FIFO_SIZE - 1);
464 if (head != tip->fifo.tail) {
465 struct tip_subbuf *ts = tip->fifo.q[head];
468 tip->fifo.head = next;
475 static inline int subbuf_fifo_queue(struct thread_information *tip,
476 struct tip_subbuf *ts)
478 const int tail = tip->fifo.tail;
479 const int next = (tail + 1) & (FIFO_SIZE - 1);
481 if (next != tip->fifo.head) {
482 tip->fifo.q[tail] = ts;
484 tip->fifo.tail = next;
488 fprintf(stderr, "fifo too small!\n");
493 * For file output, truncate and mmap the file appropriately
495 static int mmap_subbuf(struct thread_information *tip, unsigned int maxlen)
497 int ofd = fileno(tip->ofile);
501 * extend file, if we have to. use chunks of 16 subbuffers.
503 if (tip->fs_off + buf_size > tip->fs_buf_len) {
505 munlock(tip->fs_buf, tip->fs_buf_len);
506 munmap(tip->fs_buf, tip->fs_buf_len);
510 tip->fs_off = tip->fs_size & (page_size - 1);
511 tip->fs_buf_len = (16 * buf_size) - tip->fs_off;
512 tip->fs_max_size += tip->fs_buf_len;
514 if (ftruncate(ofd, tip->fs_max_size) < 0) {
519 tip->fs_buf = mmap(NULL, tip->fs_buf_len, PROT_WRITE,
520 MAP_SHARED, ofd, tip->fs_size - tip->fs_off);
521 if (tip->fs_buf == MAP_FAILED) {
525 mlock(tip->fs_buf, tip->fs_buf_len);
528 ret = read_data(tip, tip->fs_buf + tip->fs_off, maxlen);
538 static int get_subbuf_sendfile(struct thread_information *tip,
541 struct tip_subbuf *ts = malloc(sizeof(*ts));
544 ts->buf = malloc(buf_size);
545 ts->max_len = maxlen;
548 if (fstat(tip->fd, &sb) < 0) {
549 perror("trace stat");
553 ts->len = sb.st_size - tip->ofile_offset;
554 ts->max_len = ts->len;
555 ts->offset = tip->ofile_offset;
556 tip->ofile_offset += ts->len;
557 return subbuf_fifo_queue(tip, ts);
561 * Use the copy approach for pipes and network
563 static int get_subbuf(struct thread_information *tip, unsigned int maxlen)
565 struct tip_subbuf *ts = malloc(sizeof(*ts));
568 ts->buf = malloc(buf_size);
569 ts->max_len = maxlen;
571 ret = read_data(tip, ts->buf, ts->max_len);
574 return subbuf_fifo_queue(tip, ts);
580 static void close_thread(struct thread_information *tip)
586 if (tip->ofile_buffer)
587 free(tip->ofile_buffer);
593 tip->ofile_buffer = NULL;
597 static void tip_ftrunc_final(struct thread_information *tip)
600 * truncate to right size and cleanup mmap
602 if (tip->ofile_mmap) {
603 int ofd = fileno(tip->ofile);
606 munmap(tip->fs_buf, tip->fs_buf_len);
608 ftruncate(ofd, tip->fs_size);
612 static void *thread_main(void *arg)
614 struct thread_information *tip = arg;
615 pid_t pid = getpid();
619 CPU_SET((tip->cpu), &cpu_mask);
621 if (sched_setaffinity(pid, sizeof(cpu_mask), &cpu_mask) == -1) {
622 perror("sched_setaffinity");
626 snprintf(tip->fn, sizeof(tip->fn), "%s/block/%s/trace%d",
627 relay_path, tip->device->buts_name, tip->cpu);
628 tip->fd = open(tip->fn, O_RDONLY);
631 fprintf(stderr,"Thread %d failed open of %s\n", tip->cpu,
637 if (tip->get_subbuf(tip, buf_size))
641 tip_ftrunc_final(tip);
646 static int write_data_net(int fd, void *buf, unsigned int buf_len)
648 unsigned int bytes_left = buf_len;
652 ret = send(fd, buf, bytes_left, 0);
665 static int net_send_header(struct thread_information *tip, unsigned int len)
667 struct blktrace_net_hdr hdr;
669 hdr.magic = BLK_IO_TRACE_MAGIC;
670 strcpy(hdr.buts_name, tip->device->buts_name);
672 hdr.max_cpus = ncpus;
675 return write_data_net(net_out_fd, &hdr, sizeof(hdr));
678 static int flush_subbuf_net(struct thread_information *tip,
679 struct tip_subbuf *ts)
681 if (net_send_header(tip, ts->len))
683 if (write_data_net(net_out_fd, ts->buf, ts->len))
691 static int flush_subbuf_sendfile(struct thread_information *tip,
692 struct tip_subbuf *ts)
694 if (net_send_header(tip, ts->len))
696 if (sendfile(net_out_fd, tip->fd, &ts->offset, ts->len) < 0) {
705 static int write_data(struct thread_information *tip, void *buf,
706 unsigned int buf_len)
714 ret = fwrite(buf, buf_len, 1, tip->ofile);
724 if (tip->ofile_stdout)
730 static int flush_subbuf_file(struct thread_information *tip,
731 struct tip_subbuf *ts)
733 unsigned int offset = 0;
734 struct blk_io_trace *t;
735 int pdu_len, events = 0;
738 * surplus from last run
740 if (tip->leftover_ts) {
741 struct tip_subbuf *prev_ts = tip->leftover_ts;
743 if (prev_ts->len + ts->len > prev_ts->max_len) {
744 prev_ts->max_len += ts->len;
745 prev_ts->buf = realloc(prev_ts->buf, prev_ts->max_len);
748 memcpy(prev_ts->buf + prev_ts->len, ts->buf, ts->len);
749 prev_ts->len += ts->len;
755 tip->leftover_ts = NULL;
758 while (offset + sizeof(*t) <= ts->len) {
759 t = ts->buf + offset;
761 if (verify_trace(t)) {
762 write_data(tip, ts->buf, offset);
766 pdu_len = t->pdu_len;
768 if (offset + sizeof(*t) + pdu_len > ts->len)
771 offset += sizeof(*t) + pdu_len;
772 tip->events_processed++;
773 tip->data_read += sizeof(*t) + pdu_len;
777 if (write_data(tip, ts->buf, offset))
781 * leftover bytes, save them for next time
783 if (offset != ts->len) {
784 tip->leftover_ts = ts;
786 memmove(ts->buf, ts->buf + offset, ts->len);
795 static int write_tip_events(struct thread_information *tip)
797 struct tip_subbuf *ts = subbuf_fifo_dequeue(tip);
800 return tip->flush_subbuf(tip, ts);
806 * scans the tips we know and writes out the subbuffers we accumulate
808 static void get_and_write_events(void)
810 struct device_information *dip;
811 struct thread_information *tip;
812 int i, j, events, ret, tips_running;
817 for_each_dip(dip, i) {
818 for_each_tip(dip, tip, j) {
819 ret = write_tip_events(tip);
835 for_each_dip(dip, i) {
836 for_each_tip(dip, tip, j) {
837 ret = write_tip_events(tip);
840 tips_running += !tip->exited;
844 } while (events || tips_running);
847 static void wait_for_threads(void)
850 * for piped or network output, poll and fetch data for writeout.
851 * for files, we just wait around for trace threads to exit
853 if ((output_name && !strcmp(output_name, "-")) ||
854 net_mode == Net_client)
855 get_and_write_events();
857 struct device_information *dip;
858 struct thread_information *tip;
859 int i, j, tips_running;
866 for_each_tip(dip, tip, j)
867 tips_running += !tip->exited;
868 } while (tips_running);
872 static void fill_ofname(char *dst, char *buts_name, int cpu)
877 len = sprintf(dst, "%s/", output_dir);
880 sprintf(dst + len, "%s.blktrace.%d", output_name, cpu);
882 sprintf(dst + len, "%s.blktrace.%d", buts_name, cpu);
885 static void fill_ops(struct thread_information *tip)
890 if (net_mode == Net_client) {
892 tip->get_subbuf = get_subbuf_sendfile;
893 tip->flush_subbuf = flush_subbuf_sendfile;
895 tip->get_subbuf = get_subbuf;
896 tip->flush_subbuf = flush_subbuf_net;
900 tip->get_subbuf = mmap_subbuf;
902 tip->get_subbuf = get_subbuf;
904 tip->flush_subbuf = flush_subbuf_file;
907 if (net_mode == Net_server)
908 tip->read_data = read_data_net;
910 tip->read_data = read_data_file;
913 static int start_threads(struct device_information *dip)
915 struct thread_information *tip;
916 int j, pipeline = output_name && !strcmp(output_name, "-");
920 for_each_tip(dip, tip, j) {
923 tip->events_processed = 0;
924 memset(&tip->fifo, 0, sizeof(tip->fifo));
925 tip->leftover_ts = NULL;
928 tip->ofile = fdopen(STDOUT_FILENO, "w");
929 tip->ofile_stdout = 1;
934 fill_ofname(op, dip->buts_name, tip->cpu);
935 tip->ofile = fopen(op, "w+");
936 tip->ofile_stdout = 0;
939 vbuf_size = OFILE_BUF;
942 if (tip->ofile == NULL) {
947 tip->ofile_buffer = malloc(vbuf_size);
948 if (setvbuf(tip->ofile, tip->ofile_buffer, mode, vbuf_size)) {
956 if (pthread_create(&tip->thread, NULL, thread_main, tip)) {
957 perror("pthread_create");
966 static void stop_threads(struct device_information *dip)
968 struct thread_information *tip;
972 for_each_tip(dip, tip, i) {
973 (void) pthread_join(tip->thread, (void *) &ret);
978 static void stop_all_threads(void)
980 struct device_information *dip;
987 static void stop_all_tracing(void)
989 struct device_information *dip;
996 static void exit_trace(int status)
998 if (!is_trace_stopped()) {
1007 static int resize_devices(char *path)
1009 int size = (ndevs + 1) * sizeof(struct device_information);
1011 device_information = realloc(device_information, size);
1012 if (!device_information) {
1013 fprintf(stderr, "Out of memory, device %s (%d)\n", path, size);
1016 device_information[ndevs].path = path;
1021 static int open_devices(void)
1023 struct device_information *dip;
1026 for_each_dip(dip, i) {
1027 dip->fd = open(dip->path, O_RDONLY | O_NONBLOCK);
1037 static int start_devices(void)
1039 struct device_information *dip;
1042 size = ncpus * sizeof(struct thread_information);
1043 thread_information = malloc(size * ndevs);
1044 if (!thread_information) {
1045 fprintf(stderr, "Out of memory, threads (%d)\n", size * ndevs);
1049 for_each_dip(dip, i) {
1050 if (start_trace(dip)) {
1052 fprintf(stderr, "Failed to start trace on %s\n",
1059 __for_each_dip(dip, j, i)
1065 for_each_dip(dip, i) {
1066 dip->threads = thread_information + (i * ncpus);
1067 if (start_threads(dip)) {
1068 fprintf(stderr, "Failed to start worker threads\n");
1074 __for_each_dip(dip, j, i)
1076 for_each_dip(dip, i)
1085 static void show_stats(void)
1087 struct device_information *dip;
1088 struct thread_information *tip;
1089 unsigned long long events_processed, data_read;
1090 unsigned long total_drops;
1091 int i, j, no_stdout = 0;
1093 if (is_stat_shown())
1096 if (output_name && !strcmp(output_name, "-"))
1102 for_each_dip(dip, i) {
1104 printf("Device: %s\n", dip->path);
1105 events_processed = 0;
1107 for_each_tip(dip, tip, j) {
1109 printf(" CPU%3d: %20lu events, %8llu KiB data\n",
1110 tip->cpu, tip->events_processed,
1111 tip->data_read >> 10);
1112 events_processed += tip->events_processed;
1113 data_read += tip->data_read;
1115 total_drops += dip->drop_count;
1117 printf(" Total: %20llu events (dropped %lu), %8llu KiB data\n",
1118 events_processed, dip->drop_count,
1123 fprintf(stderr, "You have dropped events, consider using a larger buffer size (-b)\n");
1126 static struct device_information *net_get_dip(char *buts_name)
1128 struct device_information *dip;
1131 for (i = 0; i < ndevs; i++) {
1132 dip = &device_information[i];
1134 if (!strcmp(dip->buts_name, buts_name))
1138 device_information = realloc(device_information, (ndevs + 1) * sizeof(*dip));
1139 dip = &device_information[ndevs];
1140 strcpy(dip->buts_name, buts_name);
1141 strcpy(dip->path, buts_name);
1143 dip->threads = malloc(ncpus * sizeof(struct thread_information));
1144 memset(dip->threads, 0, ncpus * sizeof(struct thread_information));
1149 for (i = 0; i < ncpus; i++) {
1150 struct thread_information *tip = &dip->threads[i];
1154 tip->ofile_stdout = 0;
1155 tip->ofile_mmap = 1;
1158 fill_ofname(op, dip->buts_name, tip->cpu);
1160 tip->ofile = fopen(op, "w+");
1170 static struct thread_information *net_get_tip(struct blktrace_net_hdr *bnh)
1172 struct device_information *dip;
1174 ncpus = bnh->max_cpus;
1175 dip = net_get_dip(bnh->buts_name);
1176 return &dip->threads[bnh->cpu];
1179 static int net_get_header(struct blktrace_net_hdr *bnh)
1181 int fl = fcntl(net_in_fd, F_GETFL);
1182 int bytes_left, ret;
1185 fcntl(net_in_fd, F_SETFL, fl | O_NONBLOCK);
1186 bytes_left = sizeof(*bnh);
1187 while (bytes_left && !is_done()) {
1188 ret = recv(net_in_fd, p, bytes_left, MSG_WAITALL);
1190 if (errno != EAGAIN) {
1191 perror("recv header");
1204 fcntl(net_in_fd, F_SETFL, fl & ~O_NONBLOCK);
1208 static int net_server_loop(void)
1210 struct thread_information *tip;
1211 struct blktrace_net_hdr bnh;
1213 if (net_get_header(&bnh))
1216 if (data_is_native == -1 && check_data_endianness(bnh.magic)) {
1217 fprintf(stderr, "server: received data is bad\n");
1221 if (!data_is_native) {
1222 bnh.cpu = be32_to_cpu(bnh.cpu);
1223 bnh.len = be32_to_cpu(bnh.len);
1226 tip = net_get_tip(&bnh);
1230 if (mmap_subbuf(tip, bnh.len))
1237 * Start here when we are in server mode - just fetch data from the network
1240 static int net_server(void)
1242 struct sockaddr_in addr;
1246 fd = socket(AF_INET, SOCK_STREAM, 0);
1248 perror("server: socket");
1253 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
1254 perror("setsockopt");
1258 memset(&addr, 0, sizeof(addr));
1259 addr.sin_family = AF_INET;
1260 addr.sin_addr.s_addr = htonl(INADDR_ANY);
1261 addr.sin_port = htons(net_port);
1263 if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1268 if (listen(fd, 1) < 0) {
1273 printf("blktrace: waiting for incoming connection...\n");
1275 socklen = sizeof(addr);
1276 net_in_fd = accept(fd, (struct sockaddr *) &addr, &socklen);
1277 if (net_in_fd < 0) {
1282 signal(SIGINT, handle_sigint);
1283 signal(SIGHUP, handle_sigint);
1284 signal(SIGTERM, handle_sigint);
1285 signal(SIGALRM, handle_sigint);
1287 printf("blktrace: connected!\n");
1289 while (!is_done()) {
1290 if (net_server_loop())
1294 for (i = 0; i < ndevs; i++) {
1295 struct device_information *dip = &device_information[i];
1297 for (j = 0; j < ncpus; j++)
1298 tip_ftrunc_final(&dip->threads[j]);
1306 * Setup outgoing network connection where we will transmit data
1308 static int net_setup_client(void)
1310 struct sockaddr_in addr;
1313 fd = socket(AF_INET, SOCK_STREAM, 0);
1315 perror("client: socket");
1319 memset(&addr, 0, sizeof(addr));
1320 addr.sin_family = AF_INET;
1321 addr.sin_port = htons(net_port);
1323 if (inet_aton(hostname, &addr.sin_addr) != 1) {
1324 struct hostent *hent = gethostbyname(hostname);
1326 perror("gethostbyname");
1330 memcpy(&addr.sin_addr, hent->h_addr, 4);
1331 strcpy(hostname, hent->h_name);
1334 printf("blktrace: connecting to %s\n", hostname);
1336 if (connect(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1337 perror("client: connect");
1341 printf("blktrace: connected!\n");
1346 static char usage_str[] = \
1347 "-d <dev> [ -r relay path ] [ -o <output> ] [-k ] [ -w time ]\n" \
1348 "[ -a action ] [ -A action mask ] [ -v ]\n\n" \
1349 "\t-d Use specified device. May also be given last after options\n" \
1350 "\t-r Path to mounted relayfs, defaults to /relay\n" \
1351 "\t-o File(s) to send output to\n" \
1352 "\t-D Directory to prepend to output file names\n" \
1353 "\t-k Kill a running trace\n" \
1354 "\t-w Stop after defined time, in seconds\n" \
1355 "\t-a Only trace specified actions. See documentation\n" \
1356 "\t-A Give trace mask as a single value. See documentation\n" \
1357 "\t-b Sub buffer size in KiB\n" \
1358 "\t-n Number of sub buffers\n" \
1359 "\t-v Print program version info\n\n";
1361 static void show_usage(char *program)
1363 fprintf(stderr, "Usage: %s %s %s",program, blktrace_version, usage_str);
1366 int main(int argc, char *argv[])
1368 static char default_relay_path[] = "/relay";
1372 int act_mask_tmp = 0;
1374 while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) >= 0) {
1377 i = find_mask_map(optarg);
1379 fprintf(stderr,"Invalid action mask %s\n",
1387 if ((sscanf(optarg, "%x", &i) != 1) ||
1388 !valid_act_opt(i)) {
1390 "Invalid set action mask %s/0x%x\n",
1398 if (resize_devices(optarg) != 0)
1403 relay_path = optarg;
1407 output_name = optarg;
1410 kill_running_trace = 1;
1413 stop_watch = atoi(optarg);
1414 if (stop_watch <= 0) {
1416 "Invalid stopwatch value (%d secs)\n",
1422 printf("%s version %s\n", argv[0], blktrace_version);
1425 buf_size = strtoul(optarg, NULL, 10);
1426 if (buf_size <= 0 || buf_size > 16*1024) {
1428 "Invalid buffer size (%lu)\n",buf_size);
1434 buf_nr = strtoul(optarg, NULL, 10);
1437 "Invalid buffer nr (%lu)\n", buf_nr);
1442 output_dir = optarg;
1445 net_mode = Net_client;
1446 strcpy(hostname, optarg);
1449 net_mode = Net_server;
1452 net_port = atoi(optarg);
1458 show_usage(argv[0]);
1463 setlocale(LC_NUMERIC, "en_US");
1465 page_size = getpagesize();
1467 if (net_mode == Net_server)
1468 return net_server();
1470 while (optind < argc) {
1471 if (resize_devices(argv[optind++]) != 0)
1476 show_usage(argv[0]);
1481 relay_path = default_relay_path;
1483 if (act_mask_tmp != 0)
1484 act_mask = act_mask_tmp;
1486 if (statfs(relay_path, &st) < 0) {
1488 fprintf(stderr,"%s does not appear to be a valid path\n",
1491 } else if (st.f_type != (long) RELAYFS_TYPE) {
1492 fprintf(stderr,"%s does not appear to be a relay filesystem\n",
1497 if (open_devices() != 0)
1500 if (kill_running_trace) {
1505 ncpus = sysconf(_SC_NPROCESSORS_ONLN);
1507 fprintf(stderr, "sysconf(_SC_NPROCESSORS_ONLN) failed\n");
1511 signal(SIGINT, handle_sigint);
1512 signal(SIGHUP, handle_sigint);
1513 signal(SIGTERM, handle_sigint);
1514 signal(SIGALRM, handle_sigint);
1516 if (net_mode == Net_client && net_setup_client())
1519 if (start_devices() != 0)
1522 atexit(stop_all_tracing);
1529 if (!is_trace_stopped()) {