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