[PATCH] Make the sequence per-CPU so the kernel side can be lockless
[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
52 extern FILE *ofp;
53
54 #define CHECK_MAGIC(t)          (((t)->magic & 0xffffff00) == BLK_IO_TRACE_MAGIC)
55 #define SUPPORTED_VERSION       (0x06)
56
57 #if __BYTE_ORDER == __LITTLE_ENDIAN
58 #define be16_to_cpu(x)          __bswap_16(x)
59 #define be32_to_cpu(x)          __bswap_32(x)
60 #define be64_to_cpu(x)          __bswap_64(x)
61 #define cpu_to_be16(x)          __bswap_16(x)
62 #define cpu_to_be32(x)          __bswap_32(x)
63 #define cpu_to_be64(x)          __bswap_64(x)
64 #elif __BYTE_ORDER == __BIG_ENDIAN
65 #define be16_to_cpu(x)          (x)
66 #define be32_to_cpu(x)          (x)
67 #define be64_to_cpu(x)          (x)
68 #define cpu_to_be16(x)          (x)
69 #define cpu_to_be32(x)          (x)
70 #define cpu_to_be64(x)          (x)
71 #else
72 #error "Bad arch"
73 #endif
74
75 static inline int verify_trace(struct blk_io_trace *t)
76 {
77         if (!CHECK_MAGIC(t)) {
78                 fprintf(stderr, "bad trace magic %x\n", t->magic);
79                 return 1;
80         }
81         if ((t->magic & 0xff) != SUPPORTED_VERSION) {
82                 fprintf(stderr, "unsupported trace version %x\n", 
83                         t->magic & 0xff);
84                 return 1;
85         }
86
87         return 0;
88 }
89
90 static inline void trace_to_be(struct blk_io_trace *t)
91 {
92         t->magic        = cpu_to_be32(t->magic);
93         t->sequence     = cpu_to_be32(t->sequence);
94         t->time         = cpu_to_be64(t->time);
95         t->sector       = cpu_to_be64(t->sector);
96         t->bytes        = cpu_to_be32(t->bytes);
97         t->action       = cpu_to_be32(t->action);
98         t->pid          = cpu_to_be32(t->pid);
99         t->cpu          = cpu_to_be32(t->cpu);
100         t->error        = cpu_to_be16(t->error);
101         t->pdu_len      = cpu_to_be16(t->pdu_len);
102         t->device       = cpu_to_be32(t->device);
103         /* t->comm is a string (endian neutral) */
104 }
105
106 static inline void trace_to_cpu(struct blk_io_trace *t)
107 {
108         t->magic        = be32_to_cpu(t->magic);
109         t->sequence     = be32_to_cpu(t->sequence);
110         t->time         = be64_to_cpu(t->time);
111         t->sector       = be64_to_cpu(t->sector);
112         t->bytes        = be32_to_cpu(t->bytes);
113         t->action       = be32_to_cpu(t->action);
114         t->pid          = be32_to_cpu(t->pid);
115         t->cpu          = be32_to_cpu(t->cpu);
116         t->error        = be16_to_cpu(t->error);
117         t->pdu_len      = be16_to_cpu(t->pdu_len);
118         t->device       = be32_to_cpu(t->device);
119         /* t->comm is a string (endian neutral) */
120 }
121
122 extern void set_all_format_specs(char *);
123 extern int add_format_spec(char *);
124 extern void process_fmt(char *, struct per_cpu_info *, struct blk_io_trace *,
125                         unsigned long long, int, unsigned char *);
126 extern int valid_act_opt(int);
127 extern int find_mask_map(char *);
128
129 #endif