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;
159 #define FIFO_SIZE (1024) /* should be plenty big! */
160 #define CL_SIZE (128) /* cache line, any bigger? */
162 struct tip_subbuf_fifo {
163 int tail __attribute__((aligned(CL_SIZE)));
164 int head __attribute__((aligned(CL_SIZE)));
165 struct tip_subbuf *q[FIFO_SIZE];
168 struct thread_information {
174 char fn[MAXPATHLEN + 64];
181 unsigned long events_processed;
182 unsigned long long data_read;
183 struct device_information *device;
190 struct tip_subbuf_fifo fifo;
191 struct tip_subbuf *leftover_ts;
194 * mmap controlled output files
196 unsigned long long fs_size;
197 unsigned long long fs_max_size;
198 unsigned long fs_off;
200 unsigned long fs_buf_len;
203 struct device_information {
207 volatile int trace_started;
208 unsigned long drop_count;
209 struct thread_information *threads;
213 static struct thread_information *thread_information;
215 static struct device_information *device_information;
217 /* command line option globals */
218 static char *relay_path;
219 static char *output_name;
220 static char *output_dir;
221 static int act_mask = ~0U;
222 static int kill_running_trace;
223 static unsigned long buf_size = BUF_SIZE;
224 static unsigned long buf_nr = BUF_NR;
225 static unsigned int page_size;
227 #define is_done() (*(volatile int *)(&done))
228 static volatile int done;
230 #define is_trace_stopped() (*(volatile int *)(&trace_stopped))
231 static volatile int trace_stopped;
233 #define is_stat_shown() (*(volatile int *)(&stat_shown))
234 static volatile int stat_shown;
236 int data_is_native = -1;
238 static void exit_trace(int status);
240 #define dip_tracing(dip) (*(volatile int *)(&(dip)->trace_started))
241 #define dip_set_tracing(dip, v) ((dip)->trace_started = (v))
243 #define __for_each_dip(__d, __i, __e) \
244 for (__i = 0, __d = device_information; __i < __e; __i++, __d++)
246 #define for_each_dip(__d, __i) __for_each_dip(__d, __i, ndevs)
247 #define for_each_tip(__d, __t, __j) \
248 for (__j = 0, __t = (__d)->threads; __j < ncpus; __j++, __t++)
251 * networking stuff follows. we include a magic number so we know whether
252 * to endianness convert or not
254 struct blktrace_net_hdr {
255 u32 magic; /* same as trace magic */
256 char buts_name[32]; /* trace name */
257 u32 cpu; /* for which cpu */
259 u32 len; /* length of following trace data */
262 #define TRACE_NET_PORT (8462)
271 * network cmd line params
273 static char hostname[MAXHOSTNAMELEN];
274 static int net_port = TRACE_NET_PORT;
275 static int net_mode = 0;
277 static int net_in_fd = -1;
278 static int net_out_fd = -1;
280 static void handle_sigint(__attribute__((__unused__)) int sig)
285 static int get_dropped_count(const char *buts_name)
288 char tmp[MAXPATHLEN + 64];
290 snprintf(tmp, sizeof(tmp), "%s/block/%s/dropped",
291 relay_path, buts_name);
293 fd = open(tmp, O_RDONLY);
296 * this may be ok, if the kernel doesn't support dropped counts
301 fprintf(stderr, "Couldn't open dropped file %s\n", tmp);
305 if (read(fd, tmp, sizeof(tmp)) < 0) {
316 static int start_trace(struct device_information *dip)
318 struct blk_user_trace_setup buts;
320 memset(&buts, 0, sizeof(buts));
321 buts.buf_size = buf_size;
322 buts.buf_nr = buf_nr;
323 buts.act_mask = act_mask;
325 if (ioctl(dip->fd, BLKTRACESETUP, &buts) < 0) {
326 perror("BLKTRACESETUP");
330 if (ioctl(dip->fd, BLKTRACESTART) < 0) {
331 perror("BLKTRACESTART");
335 memcpy(dip->buts_name, buts.name, sizeof(dip->buts_name));
336 dip_set_tracing(dip, 1);
340 static void stop_trace(struct device_information *dip)
342 if (dip_tracing(dip) || kill_running_trace) {
343 dip_set_tracing(dip, 0);
345 if (ioctl(dip->fd, BLKTRACESTOP) < 0)
346 perror("BLKTRACESTOP");
347 if (ioctl(dip->fd, BLKTRACETEARDOWN) < 0)
348 perror("BLKTRACETEARDOWN");
355 static void stop_all_traces(void)
357 struct device_information *dip;
360 for_each_dip(dip, i) {
361 dip->drop_count = get_dropped_count(dip->buts_name);
366 static void wait_for_data(struct thread_information *tip)
368 struct pollfd pfd = { .fd = tip->fd, .events = POLLIN };
372 if (pfd.revents & POLLIN)
374 if (tip->ofile_stdout)
376 } while (!is_done());
379 static int read_data_file(struct thread_information *tip, void *buf, int len)
386 ret = read(tip->fd, buf, len);
392 if (errno != EAGAIN) {
394 fprintf(stderr,"Thread %d failed read of %s\n",
400 } while (!is_done());
406 static int read_data_net(struct thread_information *tip, void *buf, int len)
408 unsigned int bytes_left = len;
412 ret = recv(net_in_fd, buf, bytes_left, MSG_WAITALL);
417 if (errno != EAGAIN) {
419 fprintf(stderr, "server: failed read\n");
427 } while (!is_done() && bytes_left);
432 static int read_data(struct thread_information *tip, void *buf, int len)
434 if (net_mode == Net_server)
435 return read_data_net(tip, buf, len);
437 return read_data_file(tip, buf, len);
440 static inline struct tip_subbuf *
441 subbuf_fifo_dequeue(struct thread_information *tip)
443 const int head = tip->fifo.head;
444 const int next = (head + 1) & (FIFO_SIZE - 1);
446 if (head != tip->fifo.tail) {
447 struct tip_subbuf *ts = tip->fifo.q[head];
450 tip->fifo.head = next;
457 static inline int subbuf_fifo_queue(struct thread_information *tip,
458 struct tip_subbuf *ts)
460 const int tail = tip->fifo.tail;
461 const int next = (tail + 1) & (FIFO_SIZE - 1);
463 if (next != tip->fifo.head) {
464 tip->fifo.q[tail] = ts;
466 tip->fifo.tail = next;
470 fprintf(stderr, "fifo too small!\n");
475 * For file output, truncate and mmap the file appropriately
477 static int mmap_subbuf(struct thread_information *tip, unsigned int maxlen)
479 int ofd = fileno(tip->ofile);
483 * extend file, if we have to. use chunks of 16 subbuffers.
485 if (tip->fs_off + buf_size > tip->fs_buf_len) {
487 munlock(tip->fs_buf, tip->fs_buf_len);
488 munmap(tip->fs_buf, tip->fs_buf_len);
492 tip->fs_off = tip->fs_size & (page_size - 1);
493 tip->fs_buf_len = (16 * buf_size) - tip->fs_off;
494 tip->fs_max_size += tip->fs_buf_len;
496 if (ftruncate(ofd, tip->fs_max_size) < 0) {
501 tip->fs_buf = mmap(NULL, tip->fs_buf_len, PROT_WRITE,
502 MAP_SHARED, ofd, tip->fs_size - tip->fs_off);
503 if (tip->fs_buf == MAP_FAILED) {
507 mlock(tip->fs_buf, tip->fs_buf_len);
510 ret = read_data(tip, tip->fs_buf + tip->fs_off, maxlen);
512 tip->data_read += ret;
522 * Use the copy approach for pipes and network
524 static int get_subbuf(struct thread_information *tip)
526 struct tip_subbuf *ts = malloc(sizeof(*ts));
529 ts->buf = malloc(buf_size);
530 ts->max_len = buf_size;
532 ret = read_data(tip, ts->buf, ts->max_len);
535 return subbuf_fifo_queue(tip, ts);
541 static void close_thread(struct thread_information *tip)
547 if (tip->ofile_buffer)
548 free(tip->ofile_buffer);
554 tip->ofile_buffer = NULL;
558 static void tip_ftrunc_final(struct thread_information *tip)
561 * truncate to right size and cleanup mmap
563 if (tip->ofile_mmap) {
564 int ofd = fileno(tip->ofile);
567 munmap(tip->fs_buf, tip->fs_buf_len);
569 ftruncate(ofd, tip->fs_size);
573 static void *thread_main(void *arg)
575 struct thread_information *tip = arg;
576 pid_t pid = getpid();
580 CPU_SET((tip->cpu), &cpu_mask);
582 if (sched_setaffinity(pid, sizeof(cpu_mask), &cpu_mask) == -1) {
583 perror("sched_setaffinity");
587 snprintf(tip->fn, sizeof(tip->fn), "%s/block/%s/trace%d",
588 relay_path, tip->device->buts_name, tip->cpu);
589 tip->fd = open(tip->fn, O_RDONLY);
592 fprintf(stderr,"Thread %d failed open of %s\n", tip->cpu,
598 if (tip->ofile_mmap && net_mode != Net_client) {
599 if (mmap_subbuf(tip, buf_size))
607 tip_ftrunc_final(tip);
612 static int write_data_net(int fd, void *buf, unsigned int buf_len)
614 unsigned int bytes_left = buf_len;
618 ret = send(fd, buf, bytes_left, 0);
631 static int flush_subbuf_net(struct thread_information *tip,
632 struct tip_subbuf *ts)
634 struct blktrace_net_hdr hdr;
636 hdr.magic = BLK_IO_TRACE_MAGIC;
637 strcpy(hdr.buts_name, tip->device->buts_name);
639 hdr.max_cpus = ncpus;
642 if (write_data_net(net_out_fd, &hdr, sizeof(hdr)))
645 if (write_data_net(net_out_fd, ts->buf, ts->len))
653 static int write_data(struct thread_information *tip, void *buf,
654 unsigned int buf_len)
662 ret = fwrite(buf, buf_len, 1, tip->ofile);
672 if (tip->ofile_stdout)
678 static int flush_subbuf_file(struct thread_information *tip,
679 struct tip_subbuf *ts)
681 unsigned int offset = 0;
682 struct blk_io_trace *t;
683 int pdu_len, events = 0;
686 * surplus from last run
688 if (tip->leftover_ts) {
689 struct tip_subbuf *prev_ts = tip->leftover_ts;
691 if (prev_ts->len + ts->len > prev_ts->max_len) {
692 prev_ts->max_len += ts->len;
693 prev_ts->buf = realloc(prev_ts->buf, prev_ts->max_len);
696 memcpy(prev_ts->buf + prev_ts->len, ts->buf, ts->len);
697 prev_ts->len += ts->len;
703 tip->leftover_ts = NULL;
706 while (offset + sizeof(*t) <= ts->len) {
707 t = ts->buf + offset;
709 if (verify_trace(t)) {
710 write_data(tip, ts->buf, offset);
714 pdu_len = t->pdu_len;
716 if (offset + sizeof(*t) + pdu_len > ts->len)
719 offset += sizeof(*t) + pdu_len;
720 tip->events_processed++;
721 tip->data_read += sizeof(*t) + pdu_len;
725 if (write_data(tip, ts->buf, offset))
729 * leftover bytes, save them for next time
731 if (offset != ts->len) {
732 tip->leftover_ts = ts;
734 memmove(ts->buf, ts->buf + offset, ts->len);
743 static int write_tip_events(struct thread_information *tip)
745 struct tip_subbuf *ts = subbuf_fifo_dequeue(tip);
748 if (net_mode == Net_client)
749 return flush_subbuf_net(tip, ts);
751 return flush_subbuf_file(tip, ts);
758 * scans the tips we know and writes out the subbuffers we accumulate
760 static void get_and_write_events(void)
762 struct device_information *dip;
763 struct thread_information *tip;
764 int i, j, events, ret, tips_running;
769 for_each_dip(dip, i) {
770 for_each_tip(dip, tip, j) {
771 ret = write_tip_events(tip);
787 for_each_dip(dip, i) {
788 for_each_tip(dip, tip, j) {
789 ret = write_tip_events(tip);
792 tips_running += !tip->exited;
796 } while (events || tips_running);
799 static void wait_for_threads(void)
802 * for piped or network output, poll and fetch data for writeout.
803 * for files, we just wait around for trace threads to exit
805 if ((output_name && !strcmp(output_name, "-")) ||
806 net_mode == Net_client)
807 get_and_write_events();
809 struct device_information *dip;
810 struct thread_information *tip;
811 int i, j, tips_running;
818 for_each_tip(dip, tip, j)
819 tips_running += !tip->exited;
820 } while (tips_running);
824 static void fill_ofname(char *dst, char *buts_name, int cpu)
829 len = sprintf(dst, "%s/", output_dir);
832 sprintf(dst + len, "%s.blktrace.%d", output_name, cpu);
834 sprintf(dst + len, "%s.blktrace.%d", buts_name, cpu);
837 static int start_threads(struct device_information *dip)
839 struct thread_information *tip;
840 int j, pipeline = output_name && !strcmp(output_name, "-");
844 for_each_tip(dip, tip, j) {
847 tip->events_processed = 0;
848 memset(&tip->fifo, 0, sizeof(tip->fifo));
849 tip->leftover_ts = NULL;
852 tip->ofile = fdopen(STDOUT_FILENO, "w");
853 tip->ofile_stdout = 1;
858 fill_ofname(op, dip->buts_name, tip->cpu);
859 tip->ofile = fopen(op, "w+");
860 tip->ofile_stdout = 0;
863 vbuf_size = OFILE_BUF;
866 if (tip->ofile == NULL) {
871 tip->ofile_buffer = malloc(vbuf_size);
872 if (setvbuf(tip->ofile, tip->ofile_buffer, mode, vbuf_size)) {
878 if (pthread_create(&tip->thread, NULL, thread_main, tip)) {
879 perror("pthread_create");
888 static void stop_threads(struct device_information *dip)
890 struct thread_information *tip;
894 for_each_tip(dip, tip, i) {
895 (void) pthread_join(tip->thread, (void *) &ret);
900 static void stop_all_threads(void)
902 struct device_information *dip;
909 static void stop_all_tracing(void)
911 struct device_information *dip;
918 static void exit_trace(int status)
920 if (!is_trace_stopped()) {
929 static int resize_devices(char *path)
931 int size = (ndevs + 1) * sizeof(struct device_information);
933 device_information = realloc(device_information, size);
934 if (!device_information) {
935 fprintf(stderr, "Out of memory, device %s (%d)\n", path, size);
938 device_information[ndevs].path = path;
943 static int open_devices(void)
945 struct device_information *dip;
948 for_each_dip(dip, i) {
949 dip->fd = open(dip->path, O_RDONLY | O_NONBLOCK);
959 static int start_devices(void)
961 struct device_information *dip;
964 size = ncpus * sizeof(struct thread_information);
965 thread_information = malloc(size * ndevs);
966 if (!thread_information) {
967 fprintf(stderr, "Out of memory, threads (%d)\n", size * ndevs);
971 for_each_dip(dip, i) {
972 if (start_trace(dip)) {
974 fprintf(stderr, "Failed to start trace on %s\n",
981 __for_each_dip(dip, j, i)
987 for_each_dip(dip, i) {
988 dip->threads = thread_information + (i * ncpus);
989 if (start_threads(dip)) {
990 fprintf(stderr, "Failed to start worker threads\n");
996 __for_each_dip(dip, j, i)
1007 static void show_stats(void)
1009 struct device_information *dip;
1010 struct thread_information *tip;
1011 unsigned long long events_processed, data_read;
1012 unsigned long total_drops;
1013 int i, j, no_stdout = 0;
1015 if (is_stat_shown())
1018 if (output_name && !strcmp(output_name, "-"))
1024 for_each_dip(dip, i) {
1026 printf("Device: %s\n", dip->path);
1027 events_processed = 0;
1029 for_each_tip(dip, tip, j) {
1031 printf(" CPU%3d: %20lu events, %8llu KiB data\n",
1032 tip->cpu, tip->events_processed,
1033 tip->data_read >> 10);
1034 events_processed += tip->events_processed;
1035 data_read += tip->data_read;
1037 total_drops += dip->drop_count;
1039 printf(" Total: %20llu events (dropped %lu), %8llu KiB data\n",
1040 events_processed, dip->drop_count,
1045 fprintf(stderr, "You have dropped events, consider using a larger buffer size (-b)\n");
1048 static struct device_information *net_get_dip(char *buts_name)
1050 struct device_information *dip;
1053 for (i = 0; i < ndevs; i++) {
1054 dip = &device_information[i];
1056 if (!strcmp(dip->buts_name, buts_name))
1060 device_information = realloc(device_information, (ndevs + 1) * sizeof(*dip));
1061 dip = &device_information[ndevs];
1062 strcpy(dip->buts_name, buts_name);
1064 dip->threads = malloc(ncpus * sizeof(struct thread_information));
1065 memset(dip->threads, 0, ncpus * sizeof(struct thread_information));
1070 for (i = 0; i < ncpus; i++) {
1071 struct thread_information *tip = &dip->threads[i];
1075 tip->ofile_stdout = 0;
1076 tip->ofile_mmap = 1;
1079 fill_ofname(op, dip->buts_name, tip->cpu);
1081 tip->ofile = fopen(op, "w+");
1091 static struct thread_information *net_get_tip(struct blktrace_net_hdr *bnh)
1093 struct device_information *dip;
1095 ncpus = bnh->max_cpus;
1096 dip = net_get_dip(bnh->buts_name);
1097 return &dip->threads[bnh->cpu];
1100 static int net_get_header(struct blktrace_net_hdr *bnh)
1102 int fl = fcntl(net_in_fd, F_GETFL);
1103 int bytes_left, ret;
1106 fcntl(net_in_fd, F_SETFL, fl | O_NONBLOCK);
1107 bytes_left = sizeof(*bnh);
1108 while (bytes_left && !is_done()) {
1109 ret = recv(net_in_fd, p, bytes_left, MSG_WAITALL);
1111 if (errno != EAGAIN) {
1112 perror("recv header");
1125 fcntl(net_in_fd, F_SETFL, fl & ~O_NONBLOCK);
1129 static int net_server_loop(void)
1131 struct thread_information *tip;
1132 struct blktrace_net_hdr bnh;
1134 if (net_get_header(&bnh))
1137 if (data_is_native == -1 && check_data_endianness(bnh.magic)) {
1138 fprintf(stderr, "server: received data is bad\n");
1142 if (!data_is_native) {
1143 bnh.cpu = be32_to_cpu(bnh.cpu);
1144 bnh.len = be32_to_cpu(bnh.len);
1147 tip = net_get_tip(&bnh);
1151 if (mmap_subbuf(tip, bnh.len))
1158 * Start here when we are in server mode - just fetch data from the network
1161 static int net_server(void)
1163 struct sockaddr_in addr;
1167 fd = socket(AF_INET, SOCK_STREAM, 0);
1169 perror("server: socket");
1174 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
1175 perror("setsockopt");
1179 memset(&addr, 0, sizeof(addr));
1180 addr.sin_family = AF_INET;
1181 addr.sin_addr.s_addr = htonl(INADDR_ANY);
1182 addr.sin_port = htons(net_port);
1184 if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1189 if (listen(fd, 1) < 0) {
1194 printf("blktrace: waiting for incoming connection...\n");
1196 socklen = sizeof(addr);
1197 net_in_fd = accept(fd, (struct sockaddr *) &addr, &socklen);
1198 if (net_in_fd < 0) {
1203 signal(SIGINT, handle_sigint);
1204 signal(SIGHUP, handle_sigint);
1205 signal(SIGTERM, handle_sigint);
1206 signal(SIGALRM, handle_sigint);
1208 printf("blktrace: connected!\n");
1210 while (!is_done()) {
1211 if (net_server_loop())
1215 for (i = 0; i < ndevs; i++) {
1216 struct device_information *dip = &device_information[i];
1218 for (j = 0; j < ncpus; j++)
1219 tip_ftrunc_final(&dip->threads[j]);
1226 * Setup outgoing network connection where we will transmit data
1228 static int net_setup_client(void)
1230 struct sockaddr_in addr;
1233 fd = socket(AF_INET, SOCK_STREAM, 0);
1235 perror("client: socket");
1239 memset(&addr, 0, sizeof(addr));
1240 addr.sin_family = AF_INET;
1241 addr.sin_port = htons(net_port);
1243 if (inet_aton(hostname, &addr.sin_addr) != 1) {
1244 struct hostent *hent = gethostbyname(hostname);
1246 perror("gethostbyname");
1250 memcpy(&addr.sin_addr, hent->h_addr, 4);
1251 strcpy(hostname, hent->h_name);
1254 printf("blktrace: connecting to %s\n", hostname);
1256 if (connect(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1257 perror("client: connect");
1261 printf("blktrace: connected!\n");
1266 static char usage_str[] = \
1267 "-d <dev> [ -r relay path ] [ -o <output> ] [-k ] [ -w time ]\n" \
1268 "[ -a action ] [ -A action mask ] [ -v ]\n\n" \
1269 "\t-d Use specified device. May also be given last after options\n" \
1270 "\t-r Path to mounted relayfs, defaults to /relay\n" \
1271 "\t-o File(s) to send output to\n" \
1272 "\t-D Directory to prepend to output file names\n" \
1273 "\t-k Kill a running trace\n" \
1274 "\t-w Stop after defined time, in seconds\n" \
1275 "\t-a Only trace specified actions. See documentation\n" \
1276 "\t-A Give trace mask as a single value. See documentation\n" \
1277 "\t-b Sub buffer size in KiB\n" \
1278 "\t-n Number of sub buffers\n" \
1279 "\t-v Print program version info\n\n";
1281 static void show_usage(char *program)
1283 fprintf(stderr, "Usage: %s %s %s",program, blktrace_version, usage_str);
1286 int main(int argc, char *argv[])
1288 static char default_relay_path[] = "/relay";
1292 int act_mask_tmp = 0;
1294 while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) >= 0) {
1297 i = find_mask_map(optarg);
1299 fprintf(stderr,"Invalid action mask %s\n",
1307 if ((sscanf(optarg, "%x", &i) != 1) ||
1308 !valid_act_opt(i)) {
1310 "Invalid set action mask %s/0x%x\n",
1318 if (resize_devices(optarg) != 0)
1323 relay_path = optarg;
1327 output_name = optarg;
1330 kill_running_trace = 1;
1333 stop_watch = atoi(optarg);
1334 if (stop_watch <= 0) {
1336 "Invalid stopwatch value (%d secs)\n",
1342 printf("%s version %s\n", argv[0], blktrace_version);
1345 buf_size = strtoul(optarg, NULL, 10);
1346 if (buf_size <= 0 || buf_size > 16*1024) {
1348 "Invalid buffer size (%lu)\n",buf_size);
1354 buf_nr = strtoul(optarg, NULL, 10);
1357 "Invalid buffer nr (%lu)\n", buf_nr);
1362 output_dir = optarg;
1365 net_mode = Net_client;
1366 strcpy(hostname, optarg);
1369 net_mode = Net_server;
1372 net_port = atoi(optarg);
1375 show_usage(argv[0]);
1380 setlocale(LC_NUMERIC, "en_US");
1382 page_size = getpagesize();
1384 if (net_mode == Net_server)
1385 return net_server();
1387 while (optind < argc) {
1388 if (resize_devices(argv[optind++]) != 0)
1393 show_usage(argv[0]);
1398 relay_path = default_relay_path;
1400 if (act_mask_tmp != 0)
1401 act_mask = act_mask_tmp;
1403 if (statfs(relay_path, &st) < 0) {
1405 fprintf(stderr,"%s does not appear to be a valid path\n",
1408 } else if (st.f_type != (long) RELAYFS_TYPE) {
1409 fprintf(stderr,"%s does not appear to be a relay filesystem\n",
1414 if (open_devices() != 0)
1417 if (kill_running_trace) {
1422 ncpus = sysconf(_SC_NPROCESSORS_ONLN);
1424 fprintf(stderr, "sysconf(_SC_NPROCESSORS_ONLN) failed\n");
1428 signal(SIGINT, handle_sigint);
1429 signal(SIGHUP, handle_sigint);
1430 signal(SIGTERM, handle_sigint);
1431 signal(SIGALRM, handle_sigint);
1433 if (net_mode == Net_client && net_setup_client())
1436 if (start_devices() != 0)
1439 atexit(stop_all_tracing);
1446 if (!is_trace_stopped()) {