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