[PATCH] kernel: update patch to 2.6.14-rc3
[blktrace.git] / blktrace.c
index 9304c248bd6f6ccc271e23f4bc777a33db93fa5a..d9a8910a9131042ab573c3472033a0ce47bf8f80 100644 (file)
@@ -38,6 +38,8 @@
 
 #include "blktrace.h"
 
+static char blktrace_version[] = "0.90";
+
 #define BUF_SIZE       (128 *1024)
 #define BUF_NR         (4)
 
@@ -56,7 +58,7 @@ struct mask_map {
        char *long_form;
 };
 
-struct mask_map mask_maps[] = {
+static struct mask_map mask_maps[] = {
        DECLARE_MASK_MAP(READ),
        DECLARE_MASK_MAP(WRITE),
        DECLARE_MASK_MAP(BARRIER),
@@ -69,56 +71,68 @@ struct mask_map mask_maps[] = {
        DECLARE_MASK_MAP(PC),
 };
 
-#define S_OPTS "d:a:A:r:o:kw:"
+#define S_OPTS "d:a:A:r:o:kw:vb:n:"
 static struct option l_opts[] = {
        {
                .name = "dev",
-               .has_arg = 1,
+               .has_arg = required_argument,
                .flag = NULL,
                .val = 'd'
        },
        {
                .name = "act-mask",
-               .has_arg = 1,
+               .has_arg = required_argument,
                .flag = NULL,
                .val = 'a'
        },
        {
                .name = "set-mask",
-               .has_arg = 1,
+               .has_arg = required_argument,
                .flag = NULL,
                .val = 'A'
        },
        {
                .name = "relay",
-               .has_arg = 1,
+               .has_arg = required_argument,
                .flag = NULL,
                .val = 'r'
        },
        {
                .name = "output",
-               .has_arg = 1,
+               .has_arg = required_argument,
                .flag = NULL,
                .val = 'o'
        },
        {
                .name = "kill",
-               .has_arg = 0,
+               .has_arg = no_argument,
                .flag = NULL,
                .val = 'k'
        },
        {
                .name = "stopwatch",
-               .has_arg = 1,
+               .has_arg = required_argument,
                .flag = NULL,
                .val = 'w'
        },
        {
-               .name = NULL,
-               .has_arg = 0,
+               .name = "version",
+               .has_arg = no_argument,
                .flag = NULL,
-               .val = 0
-       }
+               .val = 'v'
+       },
+       {
+               .name = "buffer size",
+               .has_arg = required_argument,
+               .flag = NULL,
+               .val = 'b'
+       },
+       {
+               .name = "nr of sub buffers",
+               .has_arg = required_argument,
+               .flag = NULL,
+               .val = 'n'
+       },
 };
 
 struct thread_information {
@@ -158,6 +172,8 @@ static char *output_name;
 static int act_mask = ~0U;
 static int kill_running_trace;
 static int use_mmap;
+static unsigned int buf_size = BUF_SIZE;
+static unsigned int buf_nr = BUF_NR;
 
 #define is_done()      (*(volatile int *)(&done))
 static volatile int done;
@@ -168,7 +184,7 @@ static void exit_trace(int status);
 
 static int find_mask_map(char *string)
 {
-       int i;
+       unsigned int i;
 
        for (i = 0; i < sizeof(mask_maps)/sizeof(mask_maps[0]); i++)
                if (COMPARE_MASK_MAP(&mask_maps[i], string))
@@ -182,8 +198,8 @@ static int start_trace(struct device_information *dip)
        struct blk_user_trace_setup buts;
 
        memset(&buts, 0, sizeof(buts));
-       buts.buf_size = BUF_SIZE;
-       buts.buf_nr = BUF_NR;
+       buts.buf_size = buf_size;
+       buts.buf_nr = buf_nr;
        buts.act_mask = act_mask;
 
        if (ioctl(dip->fd, BLKSTARTTRACE, &buts) < 0) {
@@ -223,7 +239,7 @@ static int get_data_read(struct thread_information *tip, void *buf, int len)
        while (!is_done() && bytes_left > 0) {
                ret = read(tip->fd, p, bytes_left);
                if (ret == len)
-                       break;
+                       return 0;
 
                if (ret < 0) {
                        perror(tip->fn);
@@ -242,21 +258,21 @@ static int get_data_read(struct thread_information *tip, void *buf, int len)
                usleep(10000);
        }
 
-       return 0;
+       return -1;
 }
 
-static int get_data_mmap(struct thread_information *tip, void *buf, int len,
-                        int check_magic)
+static int get_data_mmap(struct thread_information *tip, void *buf,
+                        unsigned int len, int check_magic)
 {
-       if (len > (BUF_SIZE * (tip->buf_subbuf + 1)) - tip->buf_offset) {
+       if (len > (buf_size * (tip->buf_subbuf + 1)) - tip->buf_offset) {
                tip->buf_subbuf++;
-               if (tip->buf_subbuf == BUF_NR)
+               if (tip->buf_subbuf == buf_nr)
                        tip->buf_subbuf = 0;
 
-               tip->buf_offset = tip->buf_subbuf * BUF_SIZE;
+               tip->buf_offset = tip->buf_subbuf * buf_size;
        }
 
-       while (!is_done()) {
+       while (1) {
                struct blk_io_trace *t = buf;
 
                memcpy(buf, tip->buf + tip->buf_offset, len);
@@ -268,6 +284,9 @@ static int get_data_mmap(struct thread_information *tip, void *buf, int len,
                        tip->sequence = t->sequence;
                        break;
                }
+       
+               if (is_done())
+                       return -1;
 
                usleep(10000);
        }
@@ -285,7 +304,7 @@ static int get_data(struct thread_information *tip, void *buf, int len,
                return get_data_read(tip, buf, len);
 }
 
-static void *extract_data(struct thread_information *tip, char *ofn, int nb)
+static void *extract_data(struct thread_information *tip, int nb)
 {
        unsigned char *buf;
 
@@ -314,7 +333,7 @@ static void *extract(void *arg)
 {
        struct thread_information *tip = arg;
        int ret, pdu_len;
-       char dp[64], *pdu_data;
+       char *pdu_data;
        struct blk_io_trace t;
        pid_t pid = getpid();
        cpu_set_t cpu_mask;
@@ -338,7 +357,7 @@ static void *extract(void *arg)
        }
 
        if (use_mmap) {
-               tip->buf = mmap(NULL, BUF_SIZE * BUF_NR, PROT_READ,
+               tip->buf = mmap(NULL, buf_size * buf_nr, PROT_READ,
                                        MAP_PRIVATE | MAP_POPULATE, tip->fd, 0);
                if (tip->buf == MAP_FAILED) {
                        perror("mmap");
@@ -359,7 +378,7 @@ static void *extract(void *arg)
                trace_to_be(&t);
 
                if (pdu_len)
-                       pdu_data = extract_data(tip, dp, pdu_len);
+                       pdu_data = extract_data(tip, pdu_len);
 
                /*
                 * now we have both trace and payload, get a lock on the
@@ -409,12 +428,13 @@ static int start_threads(struct device_information *dip)
                        tip->ofd = dup(STDOUT_FILENO);
                        tip->fd_lock = &stdout_mutex;
                } else {
-                       if (output_name)
-                               sprintf(op, "%s_%s_out.%d", output_name,
-                                       dip->buts_name, tip->cpu);
-                       else
-                               sprintf(op, "%s_out.%d",
+                       if (output_name) {
+                               sprintf(op, "%s.blktrace.%d", output_name,
+                                       tip->cpu);
+                       } else {
+                               sprintf(op, "%s.blktrace.%d",
                                        dip->buts_name, tip->cpu);
+                       }
                        tip->ofd = open(op, O_CREAT|O_TRUNC|O_WRONLY, 0644);
                }
 
@@ -436,7 +456,7 @@ static int start_threads(struct device_information *dip)
 static void close_thread(struct thread_information *tip)
 {
        if (tip->buf)
-               munmap(tip->buf, BUF_SIZE * BUF_NR);
+               munmap(tip->buf, buf_size * buf_nr);
 
        if (tip->fd != -1)
                close(tip->fd);
@@ -566,7 +586,7 @@ static void show_stats(void)
        struct device_information *dip;
        struct thread_information *tip;
        unsigned long long events_processed;
-  
+
        if (output_name && !strcmp(output_name, "-"))
                return;
 
@@ -581,15 +601,27 @@ static void show_stats(void)
                printf("  Total:  %20lld events\n", events_processed);
        }
 }
-  
+
+static char usage_str[] = \
+       "-d <dev> [ -r relay path ] [ -o <output> ] [-k ] [ -w time ]\n" \
+       "[ -a action ] [ -A action mask ] [ -v ]\n\n" \
+       "\t-d Use specified device. May also be given last after options\n" \
+       "\t-r Path to mounted relayfs, defaults to /relay\n" \
+       "\t-o File(s) to send output to\n" \
+       "\t-k Kill a running trace\n" \
+       "\t-w Stop after defined time, in seconds\n" \
+       "\t-a Only trace specified actions. See documentation\n" \
+       "\t-A Give trace mask as a single value. See documentation\n" \
+       "\t-b Sub buffer size in KiB\n" \
+       "\t-n Number of sub buffers\n" \
+       "\t-v Print program version info\n\n";
+
 static void show_usage(char *program)
 {
-       fprintf(stderr,"Usage: %s [-d <dev>] "
-                      "[-a <trace> [-a <trace>]] <dev>\n",
-               program);
+       fprintf(stderr, "Usage: %s %s %s",program, blktrace_version, usage_str);
 }
 
-static void handle_sigint(int sig)
+static void handle_sigint(__attribute__((__unused__)) int sig)
 {
        done = 1;
 }
@@ -648,7 +680,26 @@ int main(int argc, char *argv[])
                                return 1;
                        }
                        break;
-
+               case 'v':
+                       printf("%s version %s\n", argv[0], blktrace_version);
+                       return 0;
+               case 'b':
+                       buf_size = atoi(optarg);
+                       if (buf_size <= 0) {
+                               fprintf(stderr,
+                                       "Invalid buffer size (%d)\n", buf_size);
+                               return 1;
+                       }
+                       buf_size <<= 10;
+                       break;
+               case 'n':
+                       buf_nr = atoi(optarg);
+                       if (buf_nr <= 0) {
+                               fprintf(stderr,
+                                       "Invalid buffer nr (%d)\n", buf_nr);
+                               return 1;
+                       }
+                       break;
                default:
                        show_usage(argv[0]);
                        return 1;