af5ab66e11c3d5a25f3ea88c5989f2d644f8478e
[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
22 #define t_sec(t)        ((t)->bytes >> 9)
23 #define t_kb(t)         ((t)->bytes >> 10)
24
25 typedef __u32 u32;
26 typedef __u8 u8;
27
28 struct io_stats {
29         unsigned long qreads, qwrites, creads, cwrites, mreads, mwrites;
30         unsigned long ireads, iwrites, rrqueue, wrqueue;
31         unsigned long long qread_kb, qwrite_kb, cread_kb, cwrite_kb;
32         unsigned long long iread_kb, iwrite_kb;
33         unsigned long io_unplugs, timer_unplugs;
34 };
35
36 struct per_cpu_info {
37         unsigned int cpu;
38         unsigned int nelems;
39
40         int fd;
41         int fdblock;
42         char fname[128];
43
44         struct io_stats io_stats;
45
46         struct rb_root rb_last;
47         unsigned long rb_last_entries;
48         unsigned long last_sequence;
49         unsigned long smallest_seq_read;
50
51         struct skip_info *skips_head;
52         struct skip_info *skips_tail;
53 };
54
55 extern FILE *ofp;
56
57 #define CHECK_MAGIC(t)          (((t)->magic & 0xffffff00) == BLK_IO_TRACE_MAGIC)
58 #define SUPPORTED_VERSION       (0x06)
59
60 #if __BYTE_ORDER == __LITTLE_ENDIAN
61 #define be16_to_cpu(x)          __bswap_16(x)
62 #define be32_to_cpu(x)          __bswap_32(x)
63 #define be64_to_cpu(x)          __bswap_64(x)
64 #define cpu_to_be16(x)          __bswap_16(x)
65 #define cpu_to_be32(x)          __bswap_32(x)
66 #define cpu_to_be64(x)          __bswap_64(x)
67 #elif __BYTE_ORDER == __BIG_ENDIAN
68 #define be16_to_cpu(x)          (x)
69 #define be32_to_cpu(x)          (x)
70 #define be64_to_cpu(x)          (x)
71 #define cpu_to_be16(x)          (x)
72 #define cpu_to_be32(x)          (x)
73 #define cpu_to_be64(x)          (x)
74 #else
75 #error "Bad arch"
76 #endif
77
78 static inline int verify_trace(struct blk_io_trace *t)
79 {
80         if (!CHECK_MAGIC(t)) {
81                 fprintf(stderr, "bad trace magic %x\n", t->magic);
82                 return 1;
83         }
84         if ((t->magic & 0xff) != SUPPORTED_VERSION) {
85                 fprintf(stderr, "unsupported trace version %x\n", 
86                         t->magic & 0xff);
87                 return 1;
88         }
89
90         return 0;
91 }
92
93 static inline void trace_to_be(struct blk_io_trace *t)
94 {
95         t->magic        = cpu_to_be32(t->magic);
96         t->sequence     = cpu_to_be32(t->sequence);
97         t->time         = cpu_to_be64(t->time);
98         t->sector       = cpu_to_be64(t->sector);
99         t->bytes        = cpu_to_be32(t->bytes);
100         t->action       = cpu_to_be32(t->action);
101         t->pid          = cpu_to_be32(t->pid);
102         t->cpu          = cpu_to_be32(t->cpu);
103         t->error        = cpu_to_be16(t->error);
104         t->pdu_len      = cpu_to_be16(t->pdu_len);
105         t->device       = cpu_to_be32(t->device);
106         /* t->comm is a string (endian neutral) */
107 }
108
109 static inline void trace_to_cpu(struct blk_io_trace *t)
110 {
111         t->magic        = be32_to_cpu(t->magic);
112         t->sequence     = be32_to_cpu(t->sequence);
113         t->time         = be64_to_cpu(t->time);
114         t->sector       = be64_to_cpu(t->sector);
115         t->bytes        = be32_to_cpu(t->bytes);
116         t->action       = be32_to_cpu(t->action);
117         t->pid          = be32_to_cpu(t->pid);
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         t->device       = be32_to_cpu(t->device);
122         /* t->comm is a string (endian neutral) */
123 }
124
125 extern void set_all_format_specs(char *);
126 extern int add_format_spec(char *);
127 extern void process_fmt(char *, struct per_cpu_info *, struct blk_io_trace *,
128                         unsigned long long, int, unsigned char *);
129 extern int valid_act_opt(int);
130 extern int find_mask_map(char *);
131
132 #endif