[PATCH] Added in Q2C and D2C latency output option.
[blktrace.git] / blktrace.h
1 #ifndef BLKTRACE_H
2 #define BLKTRACE_H
3
4 #include <stdio.h>
5 #include <byteswap.h>
6 #include <endian.h>
7
8 #include "blktrace_api.h"
9 #include "rbtree.h"
10
11 #define MINORBITS       20
12 #define MINORMASK       ((1U << MINORBITS) - 1)
13 #define MAJOR(dev)      ((unsigned int) ((dev) >> MINORBITS))
14 #define MINOR(dev)      ((unsigned int) ((dev) & MINORMASK))
15
16 #define SECONDS(x)              ((unsigned long long)(x) / 1000000000)
17 #define NANO_SECONDS(x)         ((unsigned long long)(x) % 1000000000)
18 #define DOUBLE_TO_NANO_ULL(d)   ((unsigned long long)((d) * 1000000000))
19
20 #define min(a, b)       ((a) < (b) ? (a) : (b))
21 #define max(a, b)       ((a) > (b) ? (a) : (b))
22
23 #define t_sec(t)        ((t)->bytes >> 9)
24 #define t_kb(t)         ((t)->bytes >> 10)
25
26 typedef __u32 u32;
27 typedef __u8 u8;
28
29 struct io_stats {
30         unsigned long qreads, qwrites, creads, cwrites, mreads, mwrites;
31         unsigned long ireads, iwrites, rrqueue, wrqueue;
32         unsigned long long qread_kb, qwrite_kb, cread_kb, cwrite_kb;
33         unsigned long long iread_kb, iwrite_kb;
34         unsigned long io_unplugs, timer_unplugs;
35 };
36
37 struct per_cpu_info {
38         unsigned int cpu;
39         unsigned int nelems;
40
41         int fd;
42         int fdblock;
43         char fname[128];
44
45         struct io_stats io_stats;
46
47         struct rb_root rb_last;
48         unsigned long rb_last_entries;
49         unsigned long last_sequence;
50         unsigned long smallest_seq_read;
51
52         struct skip_info *skips_head;
53         struct skip_info *skips_tail;
54 };
55
56 extern FILE *ofp;
57 extern int data_is_native;
58
59 #define CHECK_MAGIC(t)          (((t)->magic & 0xffffff00) == BLK_IO_TRACE_MAGIC)
60 #define SUPPORTED_VERSION       (0x07)
61
62 #if __BYTE_ORDER == __LITTLE_ENDIAN
63 #define be16_to_cpu(x)          __bswap_16(x)
64 #define be32_to_cpu(x)          __bswap_32(x)
65 #define be64_to_cpu(x)          __bswap_64(x)
66 #define cpu_to_be16(x)          __bswap_16(x)
67 #define cpu_to_be32(x)          __bswap_32(x)
68 #define cpu_to_be64(x)          __bswap_64(x)
69 #elif __BYTE_ORDER == __BIG_ENDIAN
70 #define be16_to_cpu(x)          (x)
71 #define be32_to_cpu(x)          (x)
72 #define be64_to_cpu(x)          (x)
73 #define cpu_to_be16(x)          (x)
74 #define cpu_to_be32(x)          (x)
75 #define cpu_to_be64(x)          (x)
76 #else
77 #error "Bad arch"
78 #endif
79
80 static inline int verify_trace(struct blk_io_trace *t)
81 {
82         if (!CHECK_MAGIC(t)) {
83                 fprintf(stderr, "bad trace magic %x\n", t->magic);
84                 return 1;
85         }
86         if ((t->magic & 0xff) != SUPPORTED_VERSION) {
87                 fprintf(stderr, "unsupported trace version %x\n", 
88                         t->magic & 0xff);
89                 return 1;
90         }
91
92         return 0;
93 }
94
95 static inline void trace_to_cpu(struct blk_io_trace *t)
96 {
97         if (data_is_native)
98                 return;
99
100         t->magic        = be32_to_cpu(t->magic);
101         t->sequence     = be32_to_cpu(t->sequence);
102         t->time         = be64_to_cpu(t->time);
103         t->sector       = be64_to_cpu(t->sector);
104         t->bytes        = be32_to_cpu(t->bytes);
105         t->action       = be32_to_cpu(t->action);
106         t->pid          = be32_to_cpu(t->pid);
107         t->device       = be32_to_cpu(t->device);
108         t->cpu          = be16_to_cpu(t->cpu);
109         t->error        = be16_to_cpu(t->error);
110         t->pdu_len      = be16_to_cpu(t->pdu_len);
111 }
112
113 /*
114  * check whether data is native or not
115  */
116 static inline int check_data_endianness(u32 magic)
117 {
118         if ((magic & 0xffffff00) == BLK_IO_TRACE_MAGIC) {
119                 data_is_native = 1;
120                 return 0;
121         }
122
123         magic = __bswap_32(magic);
124         if ((magic & 0xffffff00) == BLK_IO_TRACE_MAGIC) {
125                 data_is_native = 0;
126                 return 0;
127         }
128
129         return 1;
130 }
131
132 extern void set_all_format_specs(char *);
133 extern int add_format_spec(char *);
134 extern void process_fmt(char *, struct per_cpu_info *, struct blk_io_trace *,
135                         unsigned long long, int, unsigned char *);
136 extern int valid_act_opt(int);
137 extern int find_mask_map(char *);
138 extern char *find_process_name(pid_t);
139
140 #endif