Merge branch 'for-upstream' of https://github.com/andyprice/blktrace
[blktrace.git] / blktrace.h
CommitLineData
6fe4709e
JA
1#ifndef BLKTRACE_H
2#define BLKTRACE_H
d0ca268b 3
98f8386b 4#include <stdio.h>
c3ce73f5 5#include <limits.h>
6fe4709e 6#include <byteswap.h>
1e3c4225
JA
7#include <endian.h>
8
6fe4709e 9#include "blktrace_api.h"
210824c3 10#include "rbtree.h"
d0ca268b 11
71d5d4c9
JA
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))
6a6d3f0f 22#define max(a, b) ((a) > (b) ? (a) : (b))
71d5d4c9 23
ae957cbc
JA
24#define t_sec(t) ((t)->bytes >> 9)
25#define t_kb(t) ((t)->bytes >> 10)
cd0ae0f6 26#define t_b(t) ((t)->bytes & 1023)
ae957cbc 27
bf0720af
JA
28typedef __u32 u32;
29typedef __u8 u8;
30
71d5d4c9
JA
31struct io_stats {
32 unsigned long qreads, qwrites, creads, cwrites, mreads, mwrites;
4054070a 33 unsigned long ireads, iwrites, rrqueue, wrqueue;
71d5d4c9 34 unsigned long long qread_kb, qwrite_kb, cread_kb, cwrite_kb;
cd0ae0f6 35 unsigned long long qread_b, qwrite_b, cread_b, cwrite_b;
71d5d4c9 36 unsigned long long iread_kb, iwrite_kb;
fb2ec796 37 unsigned long long mread_kb, mwrite_kb;
cd0ae0f6 38 unsigned long long mread_b, mwrite_b, iread_b, iwrite_b;
801646d6
CS
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;
cd0ae0f6 42 unsigned long long qread_b_pc, qwrite_b_pc, iread_b_pc, iwrite_b_pc;
71d5d4c9
JA
43 unsigned long io_unplugs, timer_unplugs;
44};
45
46struct per_cpu_info {
e820abd7
JA
47 unsigned int cpu;
48 unsigned int nelems;
71d5d4c9
JA
49
50 int fd;
c0e0dbc2 51 int fdblock;
c3ce73f5 52 char fname[PATH_MAX];
71d5d4c9
JA
53
54 struct io_stats io_stats;
210824c3
JA
55
56 struct rb_root rb_last;
57 unsigned long rb_last_entries;
58 unsigned long last_sequence;
59 unsigned long smallest_seq_read;
66930177
JA
60
61 struct skip_info *skips_head;
62 struct skip_info *skips_tail;
71d5d4c9
JA
63};
64
65extern FILE *ofp;
017d1660 66extern int data_is_native;
7bd4fd0a 67extern struct timespec abs_start_time;
71d5d4c9 68
d0ca268b 69#define CHECK_MAGIC(t) (((t)->magic & 0xffffff00) == BLK_IO_TRACE_MAGIC)
bfc70ad5 70#define SUPPORTED_VERSION (0x07)
d0ca268b 71
1e3c4225 72#if __BYTE_ORDER == __LITTLE_ENDIAN
6fe4709e
JA
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)
ffcf46d8 79#elif __BYTE_ORDER == __BIG_ENDIAN
6fe4709e
JA
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
90static 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 }
d0ca268b 101
6fe4709e
JA
102 return 0;
103}
d0ca268b 104
6fe4709e
JA
105static inline void trace_to_cpu(struct blk_io_trace *t)
106{
017d1660
JA
107 if (data_is_native)
108 return;
109
6fe4709e
JA
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);
bfc70ad5 117 t->device = be32_to_cpu(t->device);
8e8bb835 118 t->cpu = be32_to_cpu(t->cpu);
6fe4709e
JA
119 t->error = be16_to_cpu(t->error);
120 t->pdu_len = be16_to_cpu(t->pdu_len);
121}
d0ca268b 122
017d1660
JA
123/*
124 * check whether data is native or not
125 */
e5413c3c 126static inline int check_data_endianness(u32 magic)
017d1660 127{
e5413c3c 128 if ((magic & 0xffffff00) == BLK_IO_TRACE_MAGIC) {
017d1660
JA
129 data_is_native = 1;
130 return 0;
131 }
132
e5413c3c 133 magic = __bswap_32(magic);
017d1660 134 if ((magic & 0xffffff00) == BLK_IO_TRACE_MAGIC) {
017d1660
JA
135 data_is_native = 0;
136 return 0;
137 }
138
139 return 1;
140}
141
71d5d4c9
JA
142extern void set_all_format_specs(char *);
143extern int add_format_spec(char *);
144extern void process_fmt(char *, struct per_cpu_info *, struct blk_io_trace *,
145 unsigned long long, int, unsigned char *);
98f8386b
AB
146extern int valid_act_opt(int);
147extern int find_mask_map(char *);
bfc70ad5 148extern char *find_process_name(pid_t);
71d5d4c9 149
d0ca268b 150#endif