[PATCH] Allow trace data to stay CPU endianness
authorJens Axboe <axboe@suse.de>
Mon, 6 Feb 2006 11:06:44 +0000 (12:06 +0100)
committerJens Axboe <axboe@suse.de>
Mon, 6 Feb 2006 11:06:44 +0000 (12:06 +0100)
Basically just check whether we need to convert the trace or not
in blkparse, then we can get rid of the endianness conversion
in the hot path (blktrace:write_tip_events()).

blkparse.c
blkrawverify.c
blktrace.c
blktrace.h

index 7da67a609a7891e3118db33e0ae91509e8954b54..52078c49172badf2b902547140f925a74f4bdc29 100644 (file)
@@ -245,6 +245,7 @@ static int ppi_hash_by_pid = 1;
 static int verbose;
 static unsigned int act_mask = -1U;
 static int stats_printed;
+static int data_is_native = -1;
 
 static unsigned int t_alloc_cache;
 static unsigned int bit_alloc_cache;
@@ -1773,6 +1774,22 @@ static int read_data(int fd, void *buffer, int bytes, int block, int *fdblock)
        return 0;
 }
 
+static inline __u16 get_pdulen(struct blk_io_trace *bit)
+{
+       if (data_is_native)
+               return bit->pdu_len;
+
+       return __bswap_16(bit->pdu_len);
+}
+
+static inline __u32 get_magic(struct blk_io_trace *bit)
+{
+       if (data_is_native)
+               return bit->magic;
+
+       return __bswap_32(bit->magic);
+}
+
 static int read_events(int fd, int always_block, int *fdblock)
 {
        struct per_dev_info *pdi = NULL;
@@ -1796,13 +1813,20 @@ static int read_events(int fd, int always_block, int *fdblock)
                        break;
                }
 
-               magic = be32_to_cpu(bit->magic);
+               /*
+                * look at first trace to check whether we need to convert
+                * data in the future
+                */
+               if (data_is_native == -1 && check_data_endianness(bit))
+                       break;
+
+               magic = get_magic(bit);
                if ((magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) {
                        fprintf(stderr, "Bad magic %x\n", magic);
                        break;
                }
 
-               pdu_len = be16_to_cpu(bit->pdu_len);
+               pdu_len = get_pdulen(bit);
                if (pdu_len) {
                        void *ptr = realloc(bit, sizeof(*bit) + pdu_len);
 
index 190f7ee4c7fb16ba21005dc83b32c6274b28e573..6cc9b29b47edd45d32cc58f7010ee68b015fddef 100644 (file)
@@ -33,6 +33,8 @@ struct trace_info {
        char *string;
 };
 
+static int data_is_native = -1;
+
 #define TRACE_TO_STRING(f)     {.bit_field = f, .string = #f}
 static struct trace_info traces[] = {
        TRACE_TO_STRING( BLK_TC_READ ),
index 0054b877d88050741736724383333354763e7ddf..a7804813ba75652216584df2643e714bebf73444 100644 (file)
@@ -494,8 +494,6 @@ static int flush_subbuf(struct thread_information *tip, struct tip_subbuf *ts)
                if (offset + sizeof(*t) + pdu_len > ts->len)
                        break;
 
-               trace_to_be(t);
-
                offset += sizeof(*t) + pdu_len;
                tip->events_processed++;
                events++;
index af5ab66e11c3d5a25f3ea88c5989f2d644f8478e..13d6e03ac8b7fc93105c424d3a369fa47911817b 100644 (file)
@@ -53,6 +53,7 @@ struct per_cpu_info {
 };
 
 extern FILE *ofp;
+extern int data_is_native;
 
 #define CHECK_MAGIC(t)         (((t)->magic & 0xffffff00) == BLK_IO_TRACE_MAGIC)
 #define SUPPORTED_VERSION      (0x06)
@@ -90,24 +91,11 @@ static inline int verify_trace(struct blk_io_trace *t)
        return 0;
 }
 
-static inline void trace_to_be(struct blk_io_trace *t)
-{
-       t->magic        = cpu_to_be32(t->magic);
-       t->sequence     = cpu_to_be32(t->sequence);
-       t->time         = cpu_to_be64(t->time);
-       t->sector       = cpu_to_be64(t->sector);
-       t->bytes        = cpu_to_be32(t->bytes);
-       t->action       = cpu_to_be32(t->action);
-       t->pid          = cpu_to_be32(t->pid);
-       t->cpu          = cpu_to_be32(t->cpu);
-       t->error        = cpu_to_be16(t->error);
-       t->pdu_len      = cpu_to_be16(t->pdu_len);
-       t->device       = cpu_to_be32(t->device);
-       /* t->comm is a string (endian neutral) */
-}
-
 static inline void trace_to_cpu(struct blk_io_trace *t)
 {
+       if (data_is_native)
+               return;
+
        t->magic        = be32_to_cpu(t->magic);
        t->sequence     = be32_to_cpu(t->sequence);
        t->time         = be64_to_cpu(t->time);
@@ -122,6 +110,29 @@ static inline void trace_to_cpu(struct blk_io_trace *t)
        /* t->comm is a string (endian neutral) */
 }
 
+/*
+ * check whether data is native or not
+ */
+static inline int check_data_endianness(struct blk_io_trace *bit)
+{
+       u32 magic;
+
+       if ((bit->magic & 0xffffff00) == BLK_IO_TRACE_MAGIC) {
+               fprintf(stderr, "data is native\n");
+               data_is_native = 1;
+               return 0;
+       }
+
+       magic = __bswap_32(bit->magic);
+       if ((magic & 0xffffff00) == BLK_IO_TRACE_MAGIC) {
+               fprintf(stderr, "data is not native\n");
+               data_is_native = 0;
+               return 0;
+       }
+
+       return 1;
+}
+
 extern void set_all_format_specs(char *);
 extern int add_format_spec(char *);
 extern void process_fmt(char *, struct per_cpu_info *, struct blk_io_trace *,