2 * blktrace output analysis: generate a timeline & gather statistics
4 * Copyright (C) 2006 Alan D. Brunelle <Alan.Brunelle@hp.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include <sys/types.h>
32 #define DEF_LEN (16 * 1024 * 1024)
35 static void *cur_map = MAP_FAILED;
36 static off_t cur_min, cur, cur_max, total_size;
38 static struct blk_io_trace *next_t;
41 int data_is_native = -1;
43 static inline size_t min_len(size_t a, size_t b) { return a < b ? a : b; }
45 static inline size_t convert_to_cpu(struct blk_io_trace *t,
46 struct blk_io_trace *tp,
49 if (data_is_native == -1)
50 check_data_endianness(t->magic);
53 memcpy(tp, t, sizeof(*tp));
55 tp->magic = be32_to_cpu(t->magic);
56 tp->sequence = be32_to_cpu(t->sequence);
57 tp->time = be64_to_cpu(t->time);
58 tp->sector = be64_to_cpu(t->sector);
59 tp->bytes = be32_to_cpu(t->bytes);
60 tp->action = be32_to_cpu(t->action);
61 tp->pid = be32_to_cpu(t->pid);
62 tp->device = be32_to_cpu(t->device);
63 tp->cpu = be16_to_cpu(t->cpu);
64 tp->error = be16_to_cpu(t->error);
65 tp->pdu_len = be16_to_cpu(t->pdu_len);
69 *pdu = malloc(tp->pdu_len);
70 memcpy(*pdu, t+1, tp->pdu_len);
75 return sizeof(*tp) + tp->pdu_len;
78 static int move_map(void)
80 if (cur_map != MAP_FAILED)
83 cur_min = (cur & ~(pgsz-1));
84 len = min_len(DEF_LEN, total_size - cur_min);
85 if (len < sizeof(*next_t))
88 cur_map = mmap(NULL, len, PROT_READ, MAP_SHARED, fd,
90 if (cur_map == MAP_FAILED) {
95 cur_max = cur_min + len;
96 return (cur < cur_max);
99 void setup_ifile(char *fname)
103 pgsz = sysconf(_SC_PAGESIZE);
105 fd = open(fname, O_RDONLY);
110 if (fstat(fd, &buf) < 0) {
114 total_size = buf.st_size;
120 void cleanup_ifile(void)
122 if (cur_map != MAP_FAILED)
123 munmap(cur_map, len);
127 int next_trace(struct blk_io_trace *t, void **pdu)
131 if ((cur + 512) > cur_max)
135 next_t = cur_map + (cur - cur_min);
136 this_len = convert_to_cpu(next_t, t, pdu);