aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/venti/checkindex.c
blob: fa6f5efc318d84add042f0dc0d8ccc9742cc2f05 (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
#include "stdinc.h"
#include "dat.h"
#include "fns.h"

static int
checkbucket(Index *ix, u32int buck, IBucket *ib)
{
	ISect *is;
	DBlock *eb;
	IBucket eib;
	IEntry ie, eie;
	int i, ei, ok, c;

	is = findisect(ix, buck);
	if(is == nil){
		seterr(EAdmin, "bad math in checkbuckets");
		return -1;
	}
	buck -= is->start;
	eb = getdblock(is->part, is->blockbase + ((u64int)buck << is->blocklog), 1);
	if(eb == nil)
		return -1;
	unpackibucket(&eib, eb->data);

	ok = 0;
	ei = 0;
	for(i = 0; i < ib->n; i++){
		while(ei < eib.n){
			c = ientrycmp(&ib->data[i * IEntrySize], &eib.data[ei * IEntrySize]);
			if(c == 0){
				unpackientry(&ie, &ib->data[i * IEntrySize]);
				unpackientry(&eie, &eib.data[ei * IEntrySize]);
				if(iaddrcmp(&ie.ia, &eie.ia) != 0){
					fprint(2, "bad entry in index for score=%V\n", &ib->data[i * IEntrySize]);
					fprint(2, "\taddr=%lld type=%d size=%d blocks=%d\n",
						ie.ia.addr, ie.ia.type, ie.ia.size, ie.ia.blocks);
					fprint(2, "\taddr=%lld type=%d size=%d blocks=%d\n",
						eie.ia.addr, eie.ia.type, eie.ia.size, eie.ia.blocks);
				}
				ei++;
				goto cont;
			}
			if(c < 0)
				break;
if(1)
			fprint(2, "spurious entry in index for score=%V type=%d\n",
				&eib.data[ei * IEntrySize], eib.data[ei * IEntrySize + IEntryTypeOff]);
			ei++;
			ok = -1;
		}
		fprint(2, "missing entry in index for score=%V type=%d\n",
			&ib->data[i * IEntrySize], ib->data[i * IEntrySize + IEntryTypeOff]);
		ok = -1;
	cont:;
	}
	for(; ei < eib.n; ei++){
if(1)		fprint(2, "spurious entry in index for score=%V; found %d entries expected %d\n",
			&eib.data[ei * IEntrySize], eib.n, ib->n);
		ok = -1;
		break;
	}
	putdblock(eb);
	return ok;
}

int
checkindex(Index *ix, Part *part, u64int off, u64int clumps, int zero)
{
	IEStream *ies;
	IBucket ib, zib;
	ZBlock *z, *b;
	u32int next, buck;
	int ok, bok;
u64int found = 0;

//ZZZ make buffer size configurable
	b = alloczblock(ix->blocksize, 0);
	z = alloczblock(ix->blocksize, 1);
	ies = initiestream(part, off, clumps, 64*1024);
	if(b == nil || z == nil || ies == nil){
		ok = -1;
		goto breakout;
		return -1;
	}
	ok = 0;
	next = 0;
	ib.data = b->data;
	zib.data = z->data;
	zib.n = 0;
	zib.next = 0;
	for(;;){
		buck = buildbucket(ix, ies, &ib);
		found += ib.n;
		if(zero){
			for(; next != buck; next++){
				if(next == ix->buckets){
					if(buck != TWID32)
						fprint(2, "bucket out of range\n");
					goto breakout;
				}
				bok = checkbucket(ix, next, &zib);
				if(bok < 0){
					fprint(2, "bad bucket=%d found: %r\n", next);
					ok = -1;
				}
			}
		}
		if(buck >= ix->buckets){
			if(buck == TWID32)
				break;
			fprint(2, "bucket out of range\n");
			ok = -1;
			goto breakout;
		}
		bok = checkbucket(ix, buck, &ib);
		if(bok < 0){
			fprint(2, "bad bucket found=%lld: %r\n", found);
			ok = -1;
		}
		next = buck + 1;
	}
breakout:;
fprint(2, "found %lld entries in sorted list\n", found);
	freeiestream(ies);
	freezblock(z);
	freezblock(b);
	return ok;
}

void
usage(void)
{
	fprint(2, "usage: checkindex [-f] [-B blockcachesize] config tmp\n");
	threadexitsall(0);
}

void
threadmain(int argc, char *argv[])
{
	Part *part;
	u64int clumps, base;
	u32int bcmem;
	int fix, skipz;

	fix = 0;
	bcmem = 0;
	skipz = 0;
	ARGBEGIN{
	case 'B':
		bcmem = unittoull(ARGF());
		break;
	case 'f':
		fix++;
		break;
	case 'Z':
		skipz = 1;
		break;
	default:
		usage();
		break;
	}ARGEND

	if(!fix)
		readonly = 1;

	if(argc != 2)
		usage();

	if(initventi(argv[0]) < 0)
		sysfatal("can't init venti: %r");

	if(bcmem < maxblocksize * (mainindex->narenas + mainindex->nsects * 4 + 16))
		bcmem = maxblocksize * (mainindex->narenas + mainindex->nsects * 4 + 16);
	fprint(2, "initialize %d bytes of disk block cache\n", bcmem);
	initdcache(bcmem);

	part = initpart(argv[1], 1);
	if(part == nil)
		sysfatal("can't initialize temporary partition: %r");

	clumps = sortrawientries(mainindex, part, &base);
	if(clumps == TWID64)
		sysfatal("can't build sorted index: %r");
	fprint(2, "found and sorted index entries for clumps=%lld at %lld\n", clumps, base);
	checkindex(mainindex, part, base, clumps, !skipz);
	
	threadexitsall(0);
}