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>
48 static char blktrace_version[] = "0.99";
51 * You may want to increase this even more, if you are logging at a high
52 * rate and see skipped/missed events
54 #define BUF_SIZE (512 * 1024)
57 #define OFILE_BUF (128 * 1024)
59 #define RELAYFS_TYPE 0xF0B4A981
61 #define S_OPTS "d:a:A:r:o:kw:Vb:n:D:lh:p:"
62 static struct option l_opts[] = {
65 .has_arg = required_argument,
71 .has_arg = required_argument,
77 .has_arg = required_argument,
83 .has_arg = required_argument,
89 .has_arg = required_argument,
95 .has_arg = no_argument,
101 .has_arg = required_argument,
107 .has_arg = no_argument,
112 .name = "buffer-size",
113 .has_arg = required_argument,
118 .name = "num-sub-buffers",
119 .has_arg = required_argument,
124 .name = "output-dir",
125 .has_arg = required_argument,
131 .has_arg = no_argument,
137 .has_arg = required_argument,
143 .has_arg = required_argument,
155 unsigned int max_len;
158 #define FIFO_SIZE (1024) /* should be plenty big! */
159 #define CL_SIZE (128) /* cache line, any bigger? */
161 struct tip_subbuf_fifo {
162 int tail __attribute__((aligned(CL_SIZE)));
163 int head __attribute__((aligned(CL_SIZE)));
164 struct tip_subbuf *q[FIFO_SIZE];
167 struct thread_information {
173 char fn[MAXPATHLEN + 64];
180 int (*get_subbuf)(struct thread_information *, unsigned int);
181 int (*flush_subbuf)(struct thread_information *, struct tip_subbuf *);
182 int (*read_data)(struct thread_information *, void *, unsigned int);
184 unsigned long events_processed;
185 unsigned long long data_read;
186 struct device_information *device;
193 struct tip_subbuf_fifo fifo;
194 struct tip_subbuf *leftover_ts;
197 * mmap controlled output files
199 unsigned long long fs_size;
200 unsigned long long fs_max_size;
201 unsigned long fs_off;
203 unsigned long fs_buf_len;
206 struct device_information {
210 volatile int trace_started;
211 unsigned long drop_count;
212 struct thread_information *threads;
216 static struct thread_information *thread_information;
218 static struct device_information *device_information;
220 /* command line option globals */
221 static char *relay_path;
222 static char *output_name;
223 static char *output_dir;
224 static int act_mask = ~0U;
225 static int kill_running_trace;
226 static unsigned long buf_size = BUF_SIZE;
227 static unsigned long buf_nr = BUF_NR;
228 static unsigned int page_size;
230 #define is_done() (*(volatile int *)(&done))
231 static volatile int done;
233 #define is_trace_stopped() (*(volatile int *)(&trace_stopped))
234 static volatile int trace_stopped;
236 #define is_stat_shown() (*(volatile int *)(&stat_shown))
237 static volatile int stat_shown;
239 int data_is_native = -1;
241 static void exit_trace(int status);
243 #define dip_tracing(dip) (*(volatile int *)(&(dip)->trace_started))
244 #define dip_set_tracing(dip, v) ((dip)->trace_started = (v))
246 #define __for_each_dip(__d, __i, __e) \
247 for (__i = 0, __d = device_information; __i < __e; __i++, __d++)
249 #define for_each_dip(__d, __i) __for_each_dip(__d, __i, ndevs)
250 #define for_each_tip(__d, __t, __j) \
251 for (__j = 0, __t = (__d)->threads; __j < ncpus; __j++, __t++)
254 * networking stuff follows. we include a magic number so we know whether
255 * to endianness convert or not
257 struct blktrace_net_hdr {
258 u32 magic; /* same as trace magic */
259 char buts_name[32]; /* trace name */
260 u32 cpu; /* for which cpu */
262 u32 len; /* length of following trace data */
265 #define TRACE_NET_PORT (8462)
274 * network cmd line params
276 static char hostname[MAXHOSTNAMELEN];
277 static int net_port = TRACE_NET_PORT;
278 static int net_mode = 0;
280 static int net_in_fd = -1;
281 static int net_out_fd = -1;
283 static void handle_sigint(__attribute__((__unused__)) int sig)
288 static int get_dropped_count(const char *buts_name)
291 char tmp[MAXPATHLEN + 64];
293 snprintf(tmp, sizeof(tmp), "%s/block/%s/dropped",
294 relay_path, buts_name);
296 fd = open(tmp, O_RDONLY);
299 * this may be ok, if the kernel doesn't support dropped counts
304 fprintf(stderr, "Couldn't open dropped file %s\n", tmp);
308 if (read(fd, tmp, sizeof(tmp)) < 0) {
319 static int start_trace(struct device_information *dip)
321 struct blk_user_trace_setup buts;
323 memset(&buts, 0, sizeof(buts));
324 buts.buf_size = buf_size;
325 buts.buf_nr = buf_nr;
326 buts.act_mask = act_mask;
328 if (ioctl(dip->fd, BLKTRACESETUP, &buts) < 0) {
329 perror("BLKTRACESETUP");
333 if (ioctl(dip->fd, BLKTRACESTART) < 0) {
334 perror("BLKTRACESTART");
338 memcpy(dip->buts_name, buts.name, sizeof(dip->buts_name));
339 dip_set_tracing(dip, 1);
343 static void stop_trace(struct device_information *dip)
345 if (dip_tracing(dip) || kill_running_trace) {
346 dip_set_tracing(dip, 0);
348 if (ioctl(dip->fd, BLKTRACESTOP) < 0)
349 perror("BLKTRACESTOP");
350 if (ioctl(dip->fd, BLKTRACETEARDOWN) < 0)
351 perror("BLKTRACETEARDOWN");
358 static void stop_all_traces(void)
360 struct device_information *dip;
363 for_each_dip(dip, i) {
364 dip->drop_count = get_dropped_count(dip->buts_name);
369 static void wait_for_data(struct thread_information *tip)
371 struct pollfd pfd = { .fd = tip->fd, .events = POLLIN };
375 if (pfd.revents & POLLIN)
377 if (tip->ofile_stdout)
379 } while (!is_done());
382 static int read_data_file(struct thread_information *tip, void *buf,
390 ret = read(tip->fd, buf, len);
396 if (errno != EAGAIN) {
398 fprintf(stderr,"Thread %d failed read of %s\n",
404 } while (!is_done());
410 static int read_data_net(struct thread_information *tip, void *buf,
413 unsigned int bytes_left = len;
417 ret = recv(net_in_fd, buf, bytes_left, MSG_WAITALL);
422 if (errno != EAGAIN) {
424 fprintf(stderr, "server: failed read\n");
432 } while (!is_done() && bytes_left);
434 return len - bytes_left;
437 static int read_data(struct thread_information *tip, void *buf,
440 int ret = tip->read_data(tip, buf, len);
443 tip->data_read += ret;
448 static inline struct tip_subbuf *
449 subbuf_fifo_dequeue(struct thread_information *tip)
451 const int head = tip->fifo.head;
452 const int next = (head + 1) & (FIFO_SIZE - 1);
454 if (head != tip->fifo.tail) {
455 struct tip_subbuf *ts = tip->fifo.q[head];
458 tip->fifo.head = next;
465 static inline int subbuf_fifo_queue(struct thread_information *tip,
466 struct tip_subbuf *ts)
468 const int tail = tip->fifo.tail;
469 const int next = (tail + 1) & (FIFO_SIZE - 1);
471 if (next != tip->fifo.head) {
472 tip->fifo.q[tail] = ts;
474 tip->fifo.tail = next;
478 fprintf(stderr, "fifo too small!\n");
483 * For file output, truncate and mmap the file appropriately
485 static int mmap_subbuf(struct thread_information *tip, unsigned int maxlen)
487 int ofd = fileno(tip->ofile);
491 * extend file, if we have to. use chunks of 16 subbuffers.
493 if (tip->fs_off + buf_size > tip->fs_buf_len) {
495 munlock(tip->fs_buf, tip->fs_buf_len);
496 munmap(tip->fs_buf, tip->fs_buf_len);
500 tip->fs_off = tip->fs_size & (page_size - 1);
501 tip->fs_buf_len = (16 * buf_size) - tip->fs_off;
502 tip->fs_max_size += tip->fs_buf_len;
504 if (ftruncate(ofd, tip->fs_max_size) < 0) {
509 tip->fs_buf = mmap(NULL, tip->fs_buf_len, PROT_WRITE,
510 MAP_SHARED, ofd, tip->fs_size - tip->fs_off);
511 if (tip->fs_buf == MAP_FAILED) {
515 mlock(tip->fs_buf, tip->fs_buf_len);
518 ret = read_data(tip, tip->fs_buf + tip->fs_off, maxlen);
529 * Use the copy approach for pipes and network
531 static int get_subbuf(struct thread_information *tip, unsigned int maxlen)
533 struct tip_subbuf *ts = malloc(sizeof(*ts));
536 ts->buf = malloc(buf_size);
537 ts->max_len = maxlen;
539 ret = read_data(tip, ts->buf, ts->max_len);
542 return subbuf_fifo_queue(tip, ts);
548 static void close_thread(struct thread_information *tip)
554 if (tip->ofile_buffer)
555 free(tip->ofile_buffer);
561 tip->ofile_buffer = NULL;
565 static void tip_ftrunc_final(struct thread_information *tip)
568 * truncate to right size and cleanup mmap
570 if (tip->ofile_mmap) {
571 int ofd = fileno(tip->ofile);
574 munmap(tip->fs_buf, tip->fs_buf_len);
576 ftruncate(ofd, tip->fs_size);
580 static void *thread_main(void *arg)
582 struct thread_information *tip = arg;
583 pid_t pid = getpid();
587 CPU_SET((tip->cpu), &cpu_mask);
589 if (sched_setaffinity(pid, sizeof(cpu_mask), &cpu_mask) == -1) {
590 perror("sched_setaffinity");
594 snprintf(tip->fn, sizeof(tip->fn), "%s/block/%s/trace%d",
595 relay_path, tip->device->buts_name, tip->cpu);
596 tip->fd = open(tip->fn, O_RDONLY);
599 fprintf(stderr,"Thread %d failed open of %s\n", tip->cpu,
605 if (tip->get_subbuf(tip, buf_size))
609 tip_ftrunc_final(tip);
614 static int write_data_net(int fd, void *buf, unsigned int buf_len)
616 unsigned int bytes_left = buf_len;
620 ret = send(fd, buf, bytes_left, 0);
633 static int flush_subbuf_net(struct thread_information *tip,
634 struct tip_subbuf *ts)
636 struct blktrace_net_hdr hdr;
638 hdr.magic = BLK_IO_TRACE_MAGIC;
639 strcpy(hdr.buts_name, tip->device->buts_name);
641 hdr.max_cpus = ncpus;
644 if (write_data_net(net_out_fd, &hdr, sizeof(hdr)))
647 if (write_data_net(net_out_fd, ts->buf, ts->len))
655 static int write_data(struct thread_information *tip, void *buf,
656 unsigned int buf_len)
664 ret = fwrite(buf, buf_len, 1, tip->ofile);
674 if (tip->ofile_stdout)
680 static int flush_subbuf_file(struct thread_information *tip,
681 struct tip_subbuf *ts)
683 unsigned int offset = 0;
684 struct blk_io_trace *t;
685 int pdu_len, events = 0;
688 * surplus from last run
690 if (tip->leftover_ts) {
691 struct tip_subbuf *prev_ts = tip->leftover_ts;
693 if (prev_ts->len + ts->len > prev_ts->max_len) {
694 prev_ts->max_len += ts->len;
695 prev_ts->buf = realloc(prev_ts->buf, prev_ts->max_len);
698 memcpy(prev_ts->buf + prev_ts->len, ts->buf, ts->len);
699 prev_ts->len += ts->len;
705 tip->leftover_ts = NULL;
708 while (offset + sizeof(*t) <= ts->len) {
709 t = ts->buf + offset;
711 if (verify_trace(t)) {
712 write_data(tip, ts->buf, offset);
716 pdu_len = t->pdu_len;
718 if (offset + sizeof(*t) + pdu_len > ts->len)
721 offset += sizeof(*t) + pdu_len;
722 tip->events_processed++;
723 tip->data_read += sizeof(*t) + pdu_len;
727 if (write_data(tip, ts->buf, offset))
731 * leftover bytes, save them for next time
733 if (offset != ts->len) {
734 tip->leftover_ts = ts;
736 memmove(ts->buf, ts->buf + offset, ts->len);
745 static int write_tip_events(struct thread_information *tip)
747 struct tip_subbuf *ts = subbuf_fifo_dequeue(tip);
750 return tip->flush_subbuf(tip, ts);
756 * scans the tips we know and writes out the subbuffers we accumulate
758 static void get_and_write_events(void)
760 struct device_information *dip;
761 struct thread_information *tip;
762 int i, j, events, ret, tips_running;
767 for_each_dip(dip, i) {
768 for_each_tip(dip, tip, j) {
769 ret = write_tip_events(tip);
785 for_each_dip(dip, i) {
786 for_each_tip(dip, tip, j) {
787 ret = write_tip_events(tip);
790 tips_running += !tip->exited;
794 } while (events || tips_running);
797 static void wait_for_threads(void)
800 * for piped or network output, poll and fetch data for writeout.
801 * for files, we just wait around for trace threads to exit
803 if ((output_name && !strcmp(output_name, "-")) ||
804 net_mode == Net_client)
805 get_and_write_events();
807 struct device_information *dip;
808 struct thread_information *tip;
809 int i, j, tips_running;
816 for_each_tip(dip, tip, j)
817 tips_running += !tip->exited;
818 } while (tips_running);
822 static void fill_ofname(char *dst, char *buts_name, int cpu)
827 len = sprintf(dst, "%s/", output_dir);
830 sprintf(dst + len, "%s.blktrace.%d", output_name, cpu);
832 sprintf(dst + len, "%s.blktrace.%d", buts_name, cpu);
835 static void fill_ops(struct thread_information *tip)
840 if (tip->ofile_mmap && net_mode != Net_client)
841 tip->get_subbuf = mmap_subbuf;
843 tip->get_subbuf = get_subbuf;
845 if (net_mode == Net_client)
846 tip->flush_subbuf = flush_subbuf_net;
848 tip->flush_subbuf = flush_subbuf_file;
850 if (net_mode == Net_server)
851 tip->read_data = read_data_net;
853 tip->read_data = read_data_file;
856 static int start_threads(struct device_information *dip)
858 struct thread_information *tip;
859 int j, pipeline = output_name && !strcmp(output_name, "-");
863 for_each_tip(dip, tip, j) {
866 tip->events_processed = 0;
867 memset(&tip->fifo, 0, sizeof(tip->fifo));
868 tip->leftover_ts = NULL;
871 tip->ofile = fdopen(STDOUT_FILENO, "w");
872 tip->ofile_stdout = 1;
877 fill_ofname(op, dip->buts_name, tip->cpu);
878 tip->ofile = fopen(op, "w+");
879 tip->ofile_stdout = 0;
882 vbuf_size = OFILE_BUF;
885 if (tip->ofile == NULL) {
890 tip->ofile_buffer = malloc(vbuf_size);
891 if (setvbuf(tip->ofile, tip->ofile_buffer, mode, vbuf_size)) {
899 if (pthread_create(&tip->thread, NULL, thread_main, tip)) {
900 perror("pthread_create");
909 static void stop_threads(struct device_information *dip)
911 struct thread_information *tip;
915 for_each_tip(dip, tip, i) {
916 (void) pthread_join(tip->thread, (void *) &ret);
921 static void stop_all_threads(void)
923 struct device_information *dip;
930 static void stop_all_tracing(void)
932 struct device_information *dip;
939 static void exit_trace(int status)
941 if (!is_trace_stopped()) {
950 static int resize_devices(char *path)
952 int size = (ndevs + 1) * sizeof(struct device_information);
954 device_information = realloc(device_information, size);
955 if (!device_information) {
956 fprintf(stderr, "Out of memory, device %s (%d)\n", path, size);
959 device_information[ndevs].path = path;
964 static int open_devices(void)
966 struct device_information *dip;
969 for_each_dip(dip, i) {
970 dip->fd = open(dip->path, O_RDONLY | O_NONBLOCK);
980 static int start_devices(void)
982 struct device_information *dip;
985 size = ncpus * sizeof(struct thread_information);
986 thread_information = malloc(size * ndevs);
987 if (!thread_information) {
988 fprintf(stderr, "Out of memory, threads (%d)\n", size * ndevs);
992 for_each_dip(dip, i) {
993 if (start_trace(dip)) {
995 fprintf(stderr, "Failed to start trace on %s\n",
1002 __for_each_dip(dip, j, i)
1008 for_each_dip(dip, i) {
1009 dip->threads = thread_information + (i * ncpus);
1010 if (start_threads(dip)) {
1011 fprintf(stderr, "Failed to start worker threads\n");
1017 __for_each_dip(dip, j, i)
1019 for_each_dip(dip, i)
1028 static void show_stats(void)
1030 struct device_information *dip;
1031 struct thread_information *tip;
1032 unsigned long long events_processed, data_read;
1033 unsigned long total_drops;
1034 int i, j, no_stdout = 0;
1036 if (is_stat_shown())
1039 if (output_name && !strcmp(output_name, "-"))
1045 for_each_dip(dip, i) {
1047 printf("Device: %s\n", dip->path);
1048 events_processed = 0;
1050 for_each_tip(dip, tip, j) {
1052 printf(" CPU%3d: %20lu events, %8llu KiB data\n",
1053 tip->cpu, tip->events_processed,
1054 tip->data_read >> 10);
1055 events_processed += tip->events_processed;
1056 data_read += tip->data_read;
1058 total_drops += dip->drop_count;
1060 printf(" Total: %20llu events (dropped %lu), %8llu KiB data\n",
1061 events_processed, dip->drop_count,
1066 fprintf(stderr, "You have dropped events, consider using a larger buffer size (-b)\n");
1069 static struct device_information *net_get_dip(char *buts_name)
1071 struct device_information *dip;
1074 for (i = 0; i < ndevs; i++) {
1075 dip = &device_information[i];
1077 if (!strcmp(dip->buts_name, buts_name))
1081 device_information = realloc(device_information, (ndevs + 1) * sizeof(*dip));
1082 dip = &device_information[ndevs];
1083 strcpy(dip->buts_name, buts_name);
1084 strcpy(dip->path, buts_name);
1086 dip->threads = malloc(ncpus * sizeof(struct thread_information));
1087 memset(dip->threads, 0, ncpus * sizeof(struct thread_information));
1092 for (i = 0; i < ncpus; i++) {
1093 struct thread_information *tip = &dip->threads[i];
1097 tip->ofile_stdout = 0;
1098 tip->ofile_mmap = 1;
1101 fill_ofname(op, dip->buts_name, tip->cpu);
1103 tip->ofile = fopen(op, "w+");
1113 static struct thread_information *net_get_tip(struct blktrace_net_hdr *bnh)
1115 struct device_information *dip;
1117 ncpus = bnh->max_cpus;
1118 dip = net_get_dip(bnh->buts_name);
1119 return &dip->threads[bnh->cpu];
1122 static int net_get_header(struct blktrace_net_hdr *bnh)
1124 int fl = fcntl(net_in_fd, F_GETFL);
1125 int bytes_left, ret;
1128 fcntl(net_in_fd, F_SETFL, fl | O_NONBLOCK);
1129 bytes_left = sizeof(*bnh);
1130 while (bytes_left && !is_done()) {
1131 ret = recv(net_in_fd, p, bytes_left, MSG_WAITALL);
1133 if (errno != EAGAIN) {
1134 perror("recv header");
1147 fcntl(net_in_fd, F_SETFL, fl & ~O_NONBLOCK);
1151 static int net_server_loop(void)
1153 struct thread_information *tip;
1154 struct blktrace_net_hdr bnh;
1156 if (net_get_header(&bnh))
1159 if (data_is_native == -1 && check_data_endianness(bnh.magic)) {
1160 fprintf(stderr, "server: received data is bad\n");
1164 if (!data_is_native) {
1165 bnh.cpu = be32_to_cpu(bnh.cpu);
1166 bnh.len = be32_to_cpu(bnh.len);
1169 tip = net_get_tip(&bnh);
1173 if (mmap_subbuf(tip, bnh.len))
1180 * Start here when we are in server mode - just fetch data from the network
1183 static int net_server(void)
1185 struct sockaddr_in addr;
1189 fd = socket(AF_INET, SOCK_STREAM, 0);
1191 perror("server: socket");
1196 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
1197 perror("setsockopt");
1201 memset(&addr, 0, sizeof(addr));
1202 addr.sin_family = AF_INET;
1203 addr.sin_addr.s_addr = htonl(INADDR_ANY);
1204 addr.sin_port = htons(net_port);
1206 if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1211 if (listen(fd, 1) < 0) {
1216 printf("blktrace: waiting for incoming connection...\n");
1218 socklen = sizeof(addr);
1219 net_in_fd = accept(fd, (struct sockaddr *) &addr, &socklen);
1220 if (net_in_fd < 0) {
1225 signal(SIGINT, handle_sigint);
1226 signal(SIGHUP, handle_sigint);
1227 signal(SIGTERM, handle_sigint);
1228 signal(SIGALRM, handle_sigint);
1230 printf("blktrace: connected!\n");
1232 while (!is_done()) {
1233 if (net_server_loop())
1237 for (i = 0; i < ndevs; i++) {
1238 struct device_information *dip = &device_information[i];
1240 for (j = 0; j < ncpus; j++)
1241 tip_ftrunc_final(&dip->threads[j]);
1249 * Setup outgoing network connection where we will transmit data
1251 static int net_setup_client(void)
1253 struct sockaddr_in addr;
1256 fd = socket(AF_INET, SOCK_STREAM, 0);
1258 perror("client: socket");
1262 memset(&addr, 0, sizeof(addr));
1263 addr.sin_family = AF_INET;
1264 addr.sin_port = htons(net_port);
1266 if (inet_aton(hostname, &addr.sin_addr) != 1) {
1267 struct hostent *hent = gethostbyname(hostname);
1269 perror("gethostbyname");
1273 memcpy(&addr.sin_addr, hent->h_addr, 4);
1274 strcpy(hostname, hent->h_name);
1277 printf("blktrace: connecting to %s\n", hostname);
1279 if (connect(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1280 perror("client: connect");
1284 printf("blktrace: connected!\n");
1289 static char usage_str[] = \
1290 "-d <dev> [ -r relay path ] [ -o <output> ] [-k ] [ -w time ]\n" \
1291 "[ -a action ] [ -A action mask ] [ -v ]\n\n" \
1292 "\t-d Use specified device. May also be given last after options\n" \
1293 "\t-r Path to mounted relayfs, defaults to /relay\n" \
1294 "\t-o File(s) to send output to\n" \
1295 "\t-D Directory to prepend to output file names\n" \
1296 "\t-k Kill a running trace\n" \
1297 "\t-w Stop after defined time, in seconds\n" \
1298 "\t-a Only trace specified actions. See documentation\n" \
1299 "\t-A Give trace mask as a single value. See documentation\n" \
1300 "\t-b Sub buffer size in KiB\n" \
1301 "\t-n Number of sub buffers\n" \
1302 "\t-v Print program version info\n\n";
1304 static void show_usage(char *program)
1306 fprintf(stderr, "Usage: %s %s %s",program, blktrace_version, usage_str);
1309 int main(int argc, char *argv[])
1311 static char default_relay_path[] = "/relay";
1315 int act_mask_tmp = 0;
1317 while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) >= 0) {
1320 i = find_mask_map(optarg);
1322 fprintf(stderr,"Invalid action mask %s\n",
1330 if ((sscanf(optarg, "%x", &i) != 1) ||
1331 !valid_act_opt(i)) {
1333 "Invalid set action mask %s/0x%x\n",
1341 if (resize_devices(optarg) != 0)
1346 relay_path = optarg;
1350 output_name = optarg;
1353 kill_running_trace = 1;
1356 stop_watch = atoi(optarg);
1357 if (stop_watch <= 0) {
1359 "Invalid stopwatch value (%d secs)\n",
1365 printf("%s version %s\n", argv[0], blktrace_version);
1368 buf_size = strtoul(optarg, NULL, 10);
1369 if (buf_size <= 0 || buf_size > 16*1024) {
1371 "Invalid buffer size (%lu)\n",buf_size);
1377 buf_nr = strtoul(optarg, NULL, 10);
1380 "Invalid buffer nr (%lu)\n", buf_nr);
1385 output_dir = optarg;
1388 net_mode = Net_client;
1389 strcpy(hostname, optarg);
1392 net_mode = Net_server;
1395 net_port = atoi(optarg);
1398 show_usage(argv[0]);
1403 setlocale(LC_NUMERIC, "en_US");
1405 page_size = getpagesize();
1407 if (net_mode == Net_server)
1408 return net_server();
1410 while (optind < argc) {
1411 if (resize_devices(argv[optind++]) != 0)
1416 show_usage(argv[0]);
1421 relay_path = default_relay_path;
1423 if (act_mask_tmp != 0)
1424 act_mask = act_mask_tmp;
1426 if (statfs(relay_path, &st) < 0) {
1428 fprintf(stderr,"%s does not appear to be a valid path\n",
1431 } else if (st.f_type != (long) RELAYFS_TYPE) {
1432 fprintf(stderr,"%s does not appear to be a relay filesystem\n",
1437 if (open_devices() != 0)
1440 if (kill_running_trace) {
1445 ncpus = sysconf(_SC_NPROCESSORS_ONLN);
1447 fprintf(stderr, "sysconf(_SC_NPROCESSORS_ONLN) failed\n");
1451 signal(SIGINT, handle_sigint);
1452 signal(SIGHUP, handle_sigint);
1453 signal(SIGTERM, handle_sigint);
1454 signal(SIGALRM, handle_sigint);
1456 if (net_mode == Net_client && net_setup_client())
1459 if (start_devices() != 0)
1462 atexit(stop_all_tracing);
1469 if (!is_trace_stopped()) {