aboutsummaryrefslogtreecommitdiff
path: root/btt/mmap.c
blob: 9d4eb3d62fdabd2de94d996cec4f51e3d5ed3a16 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
 * blktrace output analysis: generate a timeline & gather statistics
 *
 * Copyright (C) 2006 Alan D. Brunelle <Alan.Brunelle@hp.com>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <string.h>

#include "blktrace.h"
#include "globals.h"

#define DEF_LEN	(16 * 1024 * 1024)

static int fd;
static void *cur_map = MAP_FAILED;
static off_t cur_min, cur, cur_max, total_size;
static size_t len;
static struct blk_io_trace *next_t;
static long pgsz;

int data_is_native = -1;

static inline size_t min_len(size_t a, size_t b)
{
	return a < b ? a : b;
}

static inline size_t convert_to_cpu(struct blk_io_trace *t,
                                    struct blk_io_trace *tp,
				    void **pdu)
{
	if (data_is_native == -1)
		check_data_endianness(t->magic);

	if (data_is_native)
		memcpy(tp, t, sizeof(*tp));
	else {
		tp->magic	= be32_to_cpu(t->magic);
		tp->sequence	= be32_to_cpu(t->sequence);
		tp->time	= be64_to_cpu(t->time);
		tp->sector	= be64_to_cpu(t->sector);
		tp->bytes	= be32_to_cpu(t->bytes);
		tp->action	= be32_to_cpu(t->action);
		tp->pid		= be32_to_cpu(t->pid);
		tp->device	= be32_to_cpu(t->device);
		tp->cpu		= be16_to_cpu(t->cpu);
		tp->error	= be16_to_cpu(t->error);
		tp->pdu_len	= be16_to_cpu(t->pdu_len);
	}

	if (tp->pdu_len) {
		*pdu = malloc(tp->pdu_len);
		memcpy(*pdu, t+1, tp->pdu_len);
	} else
		*pdu = NULL;

	return sizeof(*tp) + tp->pdu_len;
}

static int move_map(void)
{
	if (cur_map != MAP_FAILED)
		munmap(cur_map, len);

	cur_min = (cur & ~(pgsz-1));
	len = min_len(DEF_LEN, total_size - cur_min);
	if (len < sizeof(*next_t))
		return 0;

	cur_map = mmap(NULL, len, PROT_READ, MAP_SHARED, fd,
		       cur_min);
	if (cur_map == MAP_FAILED) {
		perror("mmap");
		exit(1);
	}

	cur_max = cur_min + len;
	return (cur < cur_max);
}

void setup_ifile(char *fname)
{
	struct stat buf;

	pgsz = sysconf(_SC_PAGESIZE);

	fd = my_open(fname, O_RDONLY);
	if (fd < 0) {
		perror(fname);
		exit(1);
	}
	if (fstat(fd, &buf) < 0) {
		perror(fname);
		exit(1);
	}
	total_size = buf.st_size;

	if (!move_map())
		exit(0);
}

void cleanup_ifile(void)
{
	if (cur_map != MAP_FAILED)
		munmap(cur_map, len);
	close(fd);
}

int next_trace(struct blk_io_trace *t, void **pdu)
{
	size_t this_len;

	if ((cur + 512) > cur_max)
		if (!move_map()) {
			cleanup_ifile();
			return 0;
		}

	next_t = cur_map + (cur - cur_min);
	this_len = convert_to_cpu(next_t, t, pdu);
	cur += this_len;

	return 1;
}

double pct_done(void)
{
	return 100.0 * ((double)cur / (double)total_size);
}