summaryrefslogtreecommitdiff
path: root/src/block.c
blob: 73c10b254ef590bff24590b77acf9af393cb96fc (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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
   This file is part of odin, a memory profiler with fragmentation analysis.

   Copyright (C) 2007 Chris Wilson <chris@chris-wilson.co.uk>

   odin 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 3 of the
   License, or (at your option) any later version.

   odin 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 odin. If not, see <http://www.gnu.org/licenses/>/

   The GNU General Public License is contained in the file COPYING.
*/

#include <gtk/gtk.h>
#include <unistd.h>
#include <string.h>

#include "block.h"

struct meta {
    gulong base;
    struct meta *next;
    guint32 pages[128];
};

static guint
_log2 (gulong n)
{
    guint i = 0;
    while (1U<<i < n)
	i++;
    return i;
}

gulong
blocks_count_pages (const Block *blocks)
{
    struct meta *meta[671];
    const gulong per_meta = 32 * G_N_ELEMENTS (meta[0]->pages);
    gulong pagesize = sysconf (_SC_PAGESIZE);
    guint page_shift = _log2 (pagesize);
    guint meta_shift = _log2 (per_meta);
    const Block *b;
    gulong n_pages;
    guint n;

    memset (meta, 0, sizeof (meta));

    n_pages = 0;
    for (b = blocks; b != NULL; b = b->next) {
	guint index;
	gulong p = b->addr >> page_shift;
	gulong last_page = (b->addr + b->size) >> page_shift;
	gulong base = p & ~(per_meta - 1);
	guint32 *pages;
	struct meta *m;

	index = (base >> meta_shift) % G_N_ELEMENTS (meta);
	for (m = meta[index]; m != NULL; m = m->next)
	    if (m->base == base)
		break;
	if (m == NULL) {
	    m = g_try_new0 (struct meta, 1);
	    if (m == NULL)
		goto BAIL;

	    m->base = base;
	    m->next = meta[index];
	    meta[index] = m;
	}

	pages = m->pages;
	do {
	    guint po = p - base;
	    if ((pages[po / 32] & (1 << (po & 31 ))) == 0) {
		pages[po / 32] |= 1 << (po & 31);
		n_pages++;
	    }
	    if (++p > last_page)
		break;

	    if ((p & (per_meta - 1)) == 0) {
		base += per_meta;
		index = (index + 1) % G_N_ELEMENTS (meta);
		for (m = meta[index]; m != NULL; m = m->next)
		    if (m->base == base)
			break;
		if (m == NULL) {
		    m = g_try_new0 (struct meta, 1);
		    if (m == NULL)
			goto BAIL;

		    m->base = base;
		    m->next = meta[index];
		    meta[index] = m;
		}

		pages = m->pages;
	    }
	} while (TRUE);
    }

BAIL:
    for (n = 0; n < G_N_ELEMENTS (meta); n++) {
	while (meta[n] != NULL) {
	    struct meta *m = meta[n];
	    meta[n] = m->next;
	    g_free (m);
	}
    }

    return n_pages;
}

gulong
block_array_count_pages (Block **blocks, guint n_blocks)
{
    struct meta *meta[671];
    const gulong per_meta = 32 * G_N_ELEMENTS (meta[0]->pages);
    gulong pagesize = sysconf (_SC_PAGESIZE);
    guint page_shift = _log2 (pagesize);
    guint meta_shift = _log2 (per_meta);
    gulong n_pages;
    guint n;

    memset (meta, 0, sizeof (meta));

    n_pages = 0;
    for (n = 0; n < n_blocks; n++) {
	const Block *b = blocks[n];
	guint index;
	gulong p = b->addr >> page_shift;
	gulong last_page = (b->addr + b->size) >> page_shift;
	gulong base = p & ~(per_meta - 1);
	guint32 *pages;
	struct meta *m;

	index = (base >> meta_shift) % G_N_ELEMENTS (meta);
	for (m = meta[index]; m != NULL; m = m->next)
	    if (m->base == base)
		break;
	if (m == NULL) {
	    m = g_try_new0 (struct meta, 1);
	    if (m == NULL)
		goto BAIL;

	    m->base = base;
	    m->next = meta[index];
	    meta[index] = m;
	}

	pages = m->pages;
	do {
	    guint po = p - base;
	    if ((pages[po / 32] & (1 << (po & 31 ))) == 0) {
		pages[po / 32] |= 1 << (po & 31);
		n_pages++;
	    }
	    if (++p > last_page)
		break;

	    if ((p & (per_meta - 1)) == 0) {
		base += per_meta;
		index = (index + 1) % G_N_ELEMENTS (meta);
		for (m = meta[index]; m != NULL; m = m->next)
		    if (m->base == base)
			break;
		if (m == NULL) {
		    m = g_try_new0 (struct meta, 1);
		    if (m == NULL)
			goto BAIL;

		    m->base = base;
		    m->next = meta[index];
		    meta[index] = m;
		}

		pages = m->pages;
	    }
	} while (TRUE);
    }

BAIL:
    for (n = 0; n < G_N_ELEMENTS (meta); n++) {
	while (meta[n] != NULL) {
	    struct meta *m = meta[n];
	    meta[n] = m->next;
	    g_free (m);
	}
    }

    return n_pages;
}