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:"
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,
156 unsigned int max_len;
160 #define FIFO_SIZE (1024) /* should be plenty big! */
161 #define CL_SIZE (128) /* cache line, any bigger? */
163 struct tip_subbuf_fifo {
164 int tail __attribute__((aligned(CL_SIZE)));
165 int head __attribute__((aligned(CL_SIZE)));
166 struct tip_subbuf *q[FIFO_SIZE];
169 struct thread_information {
175 char fn[MAXPATHLEN + 64];
185 unsigned long events_processed;
186 unsigned long long data_read;
187 struct device_information *device;
194 struct tip_subbuf_fifo fifo;
195 struct tip_subbuf *leftover_ts;
198 * mmap controlled output files
200 unsigned long long fs_size;
201 unsigned long long fs_max_size;
202 unsigned long fs_off;
204 unsigned long fs_buf_len;
207 struct device_information {
211 volatile int trace_started;
212 unsigned long drop_count;
213 struct thread_information *threads;
217 static struct thread_information *thread_information;
219 static struct device_information *device_information;
221 /* command line option globals */
222 static char *relay_path;
223 static char *output_name;
224 static char *output_dir;
225 static int act_mask = ~0U;
226 static int kill_running_trace;
227 static unsigned long buf_size = BUF_SIZE;
228 static unsigned long buf_nr = BUF_NR;
229 static unsigned int page_size;
231 #define is_done() (*(volatile int *)(&done))
232 static volatile int done;
234 #define is_trace_stopped() (*(volatile int *)(&trace_stopped))
235 static volatile int trace_stopped;
237 #define is_stat_shown() (*(volatile int *)(&stat_shown))
238 static volatile int stat_shown;
240 int data_is_native = -1;
242 static void exit_trace(int status);
244 #define dip_tracing(dip) (*(volatile int *)(&(dip)->trace_started))
245 #define dip_set_tracing(dip, v) ((dip)->trace_started = (v))
247 #define __for_each_dip(__d, __i, __e) \
248 for (__i = 0, __d = device_information; __i < __e; __i++, __d++)
250 #define for_each_dip(__d, __i) __for_each_dip(__d, __i, ndevs)
251 #define for_each_tip(__d, __t, __j) \
252 for (__j = 0, __t = (__d)->threads; __j < ncpus; __j++, __t++)
255 * networking stuff follows. we include a magic number so we know whether
256 * to endianness convert or not
258 struct blktrace_net_hdr {
259 u32 magic; /* same as trace magic */
260 char ofname[64]; /* trace name */
261 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, int len)
389 ret = read(tip->fd, buf, len);
395 if (errno != EAGAIN) {
397 fprintf(stderr,"Thread %d failed read of %s\n",
403 } while (!is_done());
409 static int read_data_net(struct thread_information *tip, void *buf, int len)
411 unsigned int bytes_left = len;
415 ret = recv(net_in_fd, buf, bytes_left, MSG_WAITALL);
420 if (errno != EAGAIN) {
422 fprintf(stderr, "server: failed read\n");
430 } while (!is_done() && bytes_left);
435 static int read_data(struct thread_information *tip, void *buf, int len)
437 if (net_mode == Net_server)
438 return read_data_net(tip, buf, len);
440 return read_data_file(tip, buf, len);
443 static inline struct tip_subbuf *
444 subbuf_fifo_dequeue(struct thread_information *tip)
446 const int head = tip->fifo.head;
447 const int next = (head + 1) & (FIFO_SIZE - 1);
449 if (head != tip->fifo.tail) {
450 struct tip_subbuf *ts = tip->fifo.q[head];
453 tip->fifo.head = next;
460 static inline int subbuf_fifo_queue(struct thread_information *tip,
461 struct tip_subbuf *ts)
463 const int tail = tip->fifo.tail;
464 const int next = (tail + 1) & (FIFO_SIZE - 1);
466 if (next != tip->fifo.head) {
467 tip->fifo.q[tail] = ts;
469 tip->fifo.tail = next;
473 fprintf(stderr, "fifo too small!\n");
478 * For file output, truncate and mmap the file appropriately
480 static int mmap_subbuf(struct thread_information *tip, unsigned int maxlen)
482 int ofd = fileno(tip->ofile);
486 * extend file, if we have to. use chunks of 16 subbuffers.
488 if (tip->fs_off + buf_size > tip->fs_buf_len) {
490 munlock(tip->fs_buf, tip->fs_buf_len);
491 munmap(tip->fs_buf, tip->fs_buf_len);
495 tip->fs_off = tip->fs_size & (page_size - 1);
496 tip->fs_buf_len = (16 * buf_size) - tip->fs_off;
497 tip->fs_max_size += tip->fs_buf_len;
499 if (ftruncate(ofd, tip->fs_max_size) < 0) {
504 tip->fs_buf = mmap(NULL, tip->fs_buf_len, PROT_WRITE,
505 MAP_SHARED, ofd, tip->fs_size - tip->fs_off);
506 if (tip->fs_buf == MAP_FAILED) {
510 mlock(tip->fs_buf, tip->fs_buf_len);
513 ret = read_data(tip, tip->fs_buf + tip->fs_off, maxlen);
515 tip->data_read += ret;
525 * Use the copy approach for pipes
527 static int get_subbuf(struct thread_information *tip)
529 struct tip_subbuf *ts = malloc(sizeof(*ts));
533 * for the net client, don't actually read the data in as we will
534 * use sendfile to transmit it. just mark how much data we have
536 if (net_mode == Net_client) {
541 if (fstat(tip->fd, &sb) < 0) {
542 perror("trace stat");
546 ts->len = sb.st_size - tip->ofile_offset;
547 ts->max_len = ts->len;
548 ts->offset = tip->ofile_offset;
549 tip->ofile_offset += ts->len;
550 ret = subbuf_fifo_queue(tip, ts);
552 ts->buf = malloc(buf_size);
553 ts->max_len = buf_size;
555 ret = read_data(tip, ts->buf, ts->max_len);
558 ret = subbuf_fifo_queue(tip, ts);
572 static void close_thread(struct thread_information *tip)
578 if (tip->ofile_buffer)
579 free(tip->ofile_buffer);
585 tip->ofile_buffer = NULL;
589 static void tip_ftrunc_final(struct thread_information *tip)
592 * truncate to right size and cleanup mmap
594 if (tip->ofile_mmap) {
595 int ofd = fileno(tip->ofile);
598 munmap(tip->fs_buf, tip->fs_buf_len);
600 ftruncate(ofd, tip->fs_size);
604 static void *thread_main(void *arg)
606 struct thread_information *tip = arg;
607 pid_t pid = getpid();
611 CPU_SET((tip->cpu), &cpu_mask);
613 if (sched_setaffinity(pid, sizeof(cpu_mask), &cpu_mask) == -1) {
614 perror("sched_setaffinity");
618 snprintf(tip->fn, sizeof(tip->fn), "%s/block/%s/trace%d",
619 relay_path, tip->device->buts_name, tip->cpu);
620 tip->fd = open(tip->fn, O_RDONLY);
623 fprintf(stderr,"Thread %d failed open of %s\n", tip->cpu,
629 if (tip->ofile_mmap && net_mode != Net_client) {
630 if (mmap_subbuf(tip, buf_size))
638 tip_ftrunc_final(tip);
643 static int write_data_net(int fd, void *buf, unsigned int buf_len)
645 unsigned int bytes_left = buf_len;
649 ret = send(fd, buf, bytes_left, 0);
662 static int flush_subbuf_net(struct thread_information *tip,
663 struct tip_subbuf *ts)
665 struct blktrace_net_hdr hdr;
668 hdr.magic = BLK_IO_TRACE_MAGIC;
669 strcpy(hdr.ofname, tip->ofname);
673 if (write_data_net(net_out_fd, &hdr, sizeof(hdr)))
676 if (sendfile(net_out_fd, tip->fd, &ts->offset, ts->len) < 0) {
685 static int write_data(struct thread_information *tip, void *buf,
686 unsigned int buf_len)
694 ret = fwrite(buf, buf_len, 1, tip->ofile);
704 if (tip->ofile_stdout)
710 static int flush_subbuf_file(struct thread_information *tip,
711 struct tip_subbuf *ts)
713 unsigned int offset = 0;
714 struct blk_io_trace *t;
715 int pdu_len, events = 0;
718 * surplus from last run
720 if (tip->leftover_ts) {
721 struct tip_subbuf *prev_ts = tip->leftover_ts;
723 if (prev_ts->len + ts->len > prev_ts->max_len) {
724 prev_ts->max_len += ts->len;
725 prev_ts->buf = realloc(prev_ts->buf, prev_ts->max_len);
728 memcpy(prev_ts->buf + prev_ts->len, ts->buf, ts->len);
729 prev_ts->len += ts->len;
735 tip->leftover_ts = NULL;
738 while (offset + sizeof(*t) <= ts->len) {
739 t = ts->buf + offset;
741 if (verify_trace(t)) {
742 write_data(tip, ts->buf, offset);
746 pdu_len = t->pdu_len;
748 if (offset + sizeof(*t) + pdu_len > ts->len)
751 offset += sizeof(*t) + pdu_len;
752 tip->events_processed++;
753 tip->data_read += sizeof(*t) + pdu_len;
757 if (write_data(tip, ts->buf, offset))
761 * leftover bytes, save them for next time
763 if (offset != ts->len) {
764 tip->leftover_ts = ts;
766 memmove(ts->buf, ts->buf + offset, ts->len);
775 static int write_tip_events(struct thread_information *tip)
777 struct tip_subbuf *ts = subbuf_fifo_dequeue(tip);
780 if (net_mode == Net_client)
781 return flush_subbuf_net(tip, ts);
783 return flush_subbuf_file(tip, ts);
790 * scans the tips we know and writes out the subbuffers we accumulate
792 static void get_and_write_events(void)
794 struct device_information *dip;
795 struct thread_information *tip;
796 int i, j, events, ret, tips_running;
801 for_each_dip(dip, i) {
802 for_each_tip(dip, tip, j) {
803 ret = write_tip_events(tip);
819 for_each_dip(dip, i) {
820 for_each_tip(dip, tip, j) {
821 ret = write_tip_events(tip);
824 tips_running += !tip->exited;
828 } while (events || tips_running);
831 static void wait_for_threads(void)
834 * for piped or network output, poll and fetch data for writeout.
835 * for files, we just wait around for trace threads to exit
837 if ((output_name && !strcmp(output_name, "-")) ||
838 net_mode == Net_client)
839 get_and_write_events();
841 struct device_information *dip;
842 struct thread_information *tip;
843 int i, j, tips_running;
850 for_each_tip(dip, tip, j)
851 tips_running += !tip->exited;
852 } while (tips_running);
856 static void fill_ofname(char *dst, char *buts_name, int cpu)
861 len = sprintf(dst, "%s/", output_dir);
864 sprintf(dst + len, "%s.blktrace.%d", output_name, cpu);
866 sprintf(dst + len, "%s.blktrace.%d", buts_name, cpu);
869 static int start_threads(struct device_information *dip)
871 struct thread_information *tip;
872 int j, pipeline = output_name && !strcmp(output_name, "-");
875 for_each_tip(dip, tip, j) {
878 tip->events_processed = 0;
879 memset(&tip->fifo, 0, sizeof(tip->fifo));
880 tip->leftover_ts = NULL;
883 tip->ofile = fdopen(STDOUT_FILENO, "w");
884 tip->ofile_stdout = 1;
889 fill_ofname(tip->ofname, dip->buts_name, tip->cpu);
890 tip->ofile = fopen(tip->ofname, "w+");
891 tip->ofile_stdout = 0;
894 vbuf_size = OFILE_BUF;
897 if (tip->ofile == NULL) {
902 tip->ofile_buffer = malloc(vbuf_size);
903 if (setvbuf(tip->ofile, tip->ofile_buffer, mode, vbuf_size)) {
909 if (pthread_create(&tip->thread, NULL, thread_main, tip)) {
910 perror("pthread_create");
919 static void stop_threads(struct device_information *dip)
921 struct thread_information *tip;
925 for_each_tip(dip, tip, i) {
926 (void) pthread_join(tip->thread, (void *) &ret);
931 static void stop_all_threads(void)
933 struct device_information *dip;
940 static void stop_all_tracing(void)
942 struct device_information *dip;
949 static void exit_trace(int status)
951 if (!is_trace_stopped()) {
960 static int resize_devices(char *path)
962 int size = (ndevs + 1) * sizeof(struct device_information);
964 device_information = realloc(device_information, size);
965 if (!device_information) {
966 fprintf(stderr, "Out of memory, device %s (%d)\n", path, size);
969 device_information[ndevs].path = path;
974 static int open_devices(void)
976 struct device_information *dip;
979 for_each_dip(dip, i) {
980 dip->fd = open(dip->path, O_RDONLY | O_NONBLOCK);
990 static int start_devices(void)
992 struct device_information *dip;
995 size = ncpus * sizeof(struct thread_information);
996 thread_information = malloc(size * ndevs);
997 if (!thread_information) {
998 fprintf(stderr, "Out of memory, threads (%d)\n", size * ndevs);
1002 for_each_dip(dip, i) {
1003 if (start_trace(dip)) {
1005 fprintf(stderr, "Failed to start trace on %s\n",
1012 __for_each_dip(dip, j, i)
1018 for_each_dip(dip, i) {
1019 dip->threads = thread_information + (i * ncpus);
1020 if (start_threads(dip)) {
1021 fprintf(stderr, "Failed to start worker threads\n");
1027 __for_each_dip(dip, j, i)
1029 for_each_dip(dip, i)
1038 static void show_stats(void)
1040 struct device_information *dip;
1041 struct thread_information *tip;
1042 unsigned long long events_processed, data_read;
1043 unsigned long total_drops;
1044 int i, j, no_stdout = 0;
1046 if (is_stat_shown())
1049 if (output_name && !strcmp(output_name, "-"))
1055 for_each_dip(dip, i) {
1057 printf("Device: %s\n", dip->path);
1058 events_processed = 0;
1060 for_each_tip(dip, tip, j) {
1062 printf(" CPU%3d: %20lu events, %8llu KiB data\n",
1063 tip->cpu, tip->events_processed,
1064 tip->data_read >> 10);
1065 events_processed += tip->events_processed;
1066 data_read += tip->data_read;
1068 total_drops += dip->drop_count;
1070 printf(" Total: %20llu events (dropped %lu), %8llu KiB data\n",
1071 events_processed, dip->drop_count,
1076 fprintf(stderr, "You have dropped events, consider using a larger buffer size (-b)\n");
1079 static struct thread_information *net_tip_list;
1080 static int net_tip_len;
1082 static struct thread_information *net_get_tip(struct blktrace_net_hdr *bnh)
1084 struct thread_information *tip;
1087 for (i = 0; i < net_tip_len; i++) {
1088 tip = &net_tip_list[i];
1090 if (!strcmp(bnh->ofname, tip->ofname))
1094 net_tip_list = realloc(net_tip_list, (net_tip_len + 1) * sizeof(*tip));
1095 tip = &net_tip_list[net_tip_len];
1098 memset(tip, 0, sizeof(*tip));
1100 tip->cpu = bnh->cpu;
1101 strcpy(tip->ofname, bnh->ofname);
1103 tip->ofile = fopen(tip->ofname, "w+");
1109 tip->ofile_stdout = 0;
1110 tip->ofile_mmap = 1;
1114 static int net_get_header(struct blktrace_net_hdr *bnh)
1116 int fl = fcntl(net_in_fd, F_GETFL);
1117 int bytes_left, ret;
1120 fcntl(net_in_fd, F_SETFL, fl | O_NONBLOCK);
1121 bytes_left = sizeof(*bnh);
1122 while (bytes_left && !is_done()) {
1123 ret = recv(net_in_fd, p, bytes_left, MSG_WAITALL);
1125 if (errno != EAGAIN) {
1126 perror("recv header");
1139 fcntl(net_in_fd, F_SETFL, fl & ~O_NONBLOCK);
1143 static int net_server_loop(void)
1145 struct thread_information *tip;
1146 struct blktrace_net_hdr bnh;
1148 if (net_get_header(&bnh))
1151 if (data_is_native == -1 && check_data_endianness(bnh.magic)) {
1152 fprintf(stderr, "server: received data is bad\n");
1156 if (!data_is_native) {
1157 bnh.cpu = be32_to_cpu(bnh.cpu);
1158 bnh.len = be32_to_cpu(bnh.len);
1161 tip = net_get_tip(&bnh);
1165 if (mmap_subbuf(tip, bnh.len))
1172 * Start here when we are in server mode - just fetch data from the network
1175 static int net_server(void)
1177 struct sockaddr_in addr;
1181 fd = socket(AF_INET, SOCK_STREAM, 0);
1183 perror("server: socket");
1188 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
1189 perror("setsockopt");
1193 memset(&addr, 0, sizeof(addr));
1194 addr.sin_family = AF_INET;
1195 addr.sin_addr.s_addr = htonl(INADDR_ANY);
1196 addr.sin_port = htons(net_port);
1198 if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1203 if (listen(fd, 1) < 0) {
1208 printf("blktrace: waiting for incoming connection...\n");
1210 socklen = sizeof(addr);
1211 net_in_fd = accept(fd, (struct sockaddr *) &addr, &socklen);
1212 if (net_in_fd < 0) {
1217 signal(SIGINT, handle_sigint);
1218 signal(SIGHUP, handle_sigint);
1219 signal(SIGTERM, handle_sigint);
1220 signal(SIGALRM, handle_sigint);
1222 printf("blktrace: connected!\n");
1224 while (!is_done()) {
1225 if (net_server_loop())
1229 for (i = 0; i < net_tip_len; i++)
1230 tip_ftrunc_final(&net_tip_list[i]);
1236 * Setup outgoing network connection where we will transmit data
1238 static int net_setup_client(void)
1240 struct sockaddr_in addr;
1243 fd = socket(AF_INET, SOCK_STREAM, 0);
1245 perror("client: socket");
1249 memset(&addr, 0, sizeof(addr));
1250 addr.sin_family = AF_INET;
1251 addr.sin_port = htons(net_port);
1253 if (inet_aton(hostname, &addr.sin_addr) != 1) {
1254 struct hostent *hent = gethostbyname(hostname);
1256 perror("gethostbyname");
1260 memcpy(&addr.sin_addr, hent->h_addr, 4);
1261 strcpy(hostname, hent->h_name);
1264 printf("blktrace: connecting to %s\n", hostname);
1266 if (connect(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1267 perror("client: connect");
1271 printf("blktrace: connected!\n");
1276 static char usage_str[] = \
1277 "-d <dev> [ -r relay path ] [ -o <output> ] [-k ] [ -w time ]\n" \
1278 "[ -a action ] [ -A action mask ] [ -v ]\n\n" \
1279 "\t-d Use specified device. May also be given last after options\n" \
1280 "\t-r Path to mounted relayfs, defaults to /relay\n" \
1281 "\t-o File(s) to send output to\n" \
1282 "\t-D Directory to prepend to output file names\n" \
1283 "\t-k Kill a running trace\n" \
1284 "\t-w Stop after defined time, in seconds\n" \
1285 "\t-a Only trace specified actions. See documentation\n" \
1286 "\t-A Give trace mask as a single value. See documentation\n" \
1287 "\t-b Sub buffer size in KiB\n" \
1288 "\t-n Number of sub buffers\n" \
1289 "\t-v Print program version info\n\n";
1291 static void show_usage(char *program)
1293 fprintf(stderr, "Usage: %s %s %s",program, blktrace_version, usage_str);
1296 int main(int argc, char *argv[])
1298 static char default_relay_path[] = "/relay";
1302 int act_mask_tmp = 0;
1304 while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) >= 0) {
1307 i = find_mask_map(optarg);
1309 fprintf(stderr,"Invalid action mask %s\n",
1317 if ((sscanf(optarg, "%x", &i) != 1) ||
1318 !valid_act_opt(i)) {
1320 "Invalid set action mask %s/0x%x\n",
1328 if (resize_devices(optarg) != 0)
1333 relay_path = optarg;
1337 output_name = optarg;
1340 kill_running_trace = 1;
1343 stop_watch = atoi(optarg);
1344 if (stop_watch <= 0) {
1346 "Invalid stopwatch value (%d secs)\n",
1352 printf("%s version %s\n", argv[0], blktrace_version);
1355 buf_size = strtoul(optarg, NULL, 10);
1356 if (buf_size <= 0 || buf_size > 16*1024) {
1358 "Invalid buffer size (%lu)\n",buf_size);
1364 buf_nr = strtoul(optarg, NULL, 10);
1367 "Invalid buffer nr (%lu)\n", buf_nr);
1372 output_dir = optarg;
1375 net_mode = Net_client;
1376 strcpy(hostname, optarg);
1379 net_mode = Net_server;
1382 net_port = atoi(optarg);
1385 show_usage(argv[0]);
1390 while (optind < argc) {
1391 if (resize_devices(argv[optind++]) != 0)
1395 setlocale(LC_NUMERIC, "en_US");
1397 page_size = getpagesize();
1399 if (net_mode == Net_server)
1400 return net_server();
1403 show_usage(argv[0]);
1408 relay_path = default_relay_path;
1410 if (act_mask_tmp != 0)
1411 act_mask = act_mask_tmp;
1413 if (statfs(relay_path, &st) < 0) {
1415 fprintf(stderr,"%s does not appear to be a valid path\n",
1418 } else if (st.f_type != (long) RELAYFS_TYPE) {
1419 fprintf(stderr,"%s does not appear to be a relay filesystem\n",
1424 if (open_devices() != 0)
1427 if (kill_running_trace) {
1432 ncpus = sysconf(_SC_NPROCESSORS_ONLN);
1434 fprintf(stderr, "sysconf(_SC_NPROCESSORS_ONLN) failed\n");
1438 signal(SIGINT, handle_sigint);
1439 signal(SIGHUP, handle_sigint);
1440 signal(SIGTERM, handle_sigint);
1441 signal(SIGALRM, handle_sigint);
1443 if (net_mode == Net_client && net_setup_client())
1446 if (start_devices() != 0)
1449 atexit(stop_all_tracing);
1456 if (!is_trace_stopped()) {