Blame


1 686bd37d 2006-07-18 devnull #include "stdinc.h"
2 686bd37d 2006-07-18 devnull #include "dat.h"
3 686bd37d 2006-07-18 devnull #include "fns.h"
4 686bd37d 2006-07-18 devnull
5 686bd37d 2006-07-18 devnull static int verbose;
6 686bd37d 2006-07-18 devnull static int fd;
7 686bd37d 2006-07-18 devnull static int fd1;
8 686bd37d 2006-07-18 devnull static uchar *data;
9 686bd37d 2006-07-18 devnull static uchar *data1;
10 686bd37d 2006-07-18 devnull static int blocksize;
11 686bd37d 2006-07-18 devnull static int sleepms;
12 686bd37d 2006-07-18 devnull
13 686bd37d 2006-07-18 devnull void
14 686bd37d 2006-07-18 devnull usage(void)
15 686bd37d 2006-07-18 devnull {
16 686bd37d 2006-07-18 devnull fprint(2, "usage: cmparenas [-b blocksize] [-s ms] [-v] arenapart1 arenapart2 [name...]]\n");
17 686bd37d 2006-07-18 devnull threadexitsall(0);
18 686bd37d 2006-07-18 devnull }
19 686bd37d 2006-07-18 devnull
20 686bd37d 2006-07-18 devnull static int
21 686bd37d 2006-07-18 devnull preadblock(int fd, uchar *buf, int n, vlong off)
22 686bd37d 2006-07-18 devnull {
23 686bd37d 2006-07-18 devnull int nr, m;
24 686bd37d 2006-07-18 devnull
25 686bd37d 2006-07-18 devnull for(nr = 0; nr < n; nr += m){
26 686bd37d 2006-07-18 devnull m = n - nr;
27 686bd37d 2006-07-18 devnull m = pread(fd, &buf[nr], m, off+nr);
28 686bd37d 2006-07-18 devnull if(m <= 0){
29 686bd37d 2006-07-18 devnull if(m == 0)
30 686bd37d 2006-07-18 devnull werrstr("early eof");
31 686bd37d 2006-07-18 devnull return -1;
32 686bd37d 2006-07-18 devnull }
33 686bd37d 2006-07-18 devnull }
34 686bd37d 2006-07-18 devnull return 0;
35 686bd37d 2006-07-18 devnull }
36 686bd37d 2006-07-18 devnull
37 686bd37d 2006-07-18 devnull static int
38 686bd37d 2006-07-18 devnull readblock(int fd, uchar *buf, int n)
39 686bd37d 2006-07-18 devnull {
40 686bd37d 2006-07-18 devnull int nr, m;
41 686bd37d 2006-07-18 devnull
42 686bd37d 2006-07-18 devnull for(nr = 0; nr < n; nr += m){
43 686bd37d 2006-07-18 devnull m = n - nr;
44 686bd37d 2006-07-18 devnull m = read(fd, &buf[nr], m);
45 686bd37d 2006-07-18 devnull if(m <= 0){
46 686bd37d 2006-07-18 devnull if(m == 0)
47 686bd37d 2006-07-18 devnull werrstr("early eof");
48 686bd37d 2006-07-18 devnull return -1;
49 686bd37d 2006-07-18 devnull }
50 686bd37d 2006-07-18 devnull }
51 27d28098 2007-04-21 devnull return 0;
52 27d28098 2007-04-21 devnull }
53 27d28098 2007-04-21 devnull
54 27d28098 2007-04-21 devnull static int
55 27d28098 2007-04-21 devnull printheader(char *name, ArenaHead *head, int fd)
56 27d28098 2007-04-21 devnull {
57 27d28098 2007-04-21 devnull Arena arena;
58 27d28098 2007-04-21 devnull vlong baseoff, lo, hi, off;
59 27d28098 2007-04-21 devnull int clumpmax;
60 fa325e9b 2020-01-10 cross
61 27d28098 2007-04-21 devnull off = seek(fd, 0, 1);
62 27d28098 2007-04-21 devnull seek(fd, off + head->size - head->blocksize, 0);
63 27d28098 2007-04-21 devnull if(readblock(fd, data, head->blocksize) < 0){
64 27d28098 2007-04-21 devnull fprint(2, "%s: reading arena tail: %r\n", name);
65 27d28098 2007-04-21 devnull return -1;
66 27d28098 2007-04-21 devnull }
67 27d28098 2007-04-21 devnull seek(fd, off, 0);
68 27d28098 2007-04-21 devnull
69 27d28098 2007-04-21 devnull memset(&arena, 0, sizeof arena);
70 27d28098 2007-04-21 devnull if(unpackarena(&arena, data) < 0){
71 27d28098 2007-04-21 devnull fprint(2, "%s: unpack arena tail: %r\n", name);
72 27d28098 2007-04-21 devnull return -1;
73 27d28098 2007-04-21 devnull }
74 27d28098 2007-04-21 devnull arena.blocksize = head->blocksize;
75 27d28098 2007-04-21 devnull arena.base = off + head->blocksize;
76 27d28098 2007-04-21 devnull arena.clumpmax = arena.blocksize / ClumpInfoSize;
77 27d28098 2007-04-21 devnull arena.size = head->size - 2*head->blocksize;
78 27d28098 2007-04-21 devnull
79 27d28098 2007-04-21 devnull fprint(2, "%s: base=%llx size=%llx blocksize=%x\n", name, off, head->size, head->blocksize);
80 27d28098 2007-04-21 devnull
81 27d28098 2007-04-21 devnull baseoff = head->blocksize;
82 27d28098 2007-04-21 devnull fprint(2, "\t%llx-%llx: head\n", (vlong)0, baseoff);
83 27d28098 2007-04-21 devnull lo = baseoff;
84 27d28098 2007-04-21 devnull hi = baseoff + arena.diskstats.used;
85 27d28098 2007-04-21 devnull fprint(2, "\t%llx-%llx: data (%llx)\n", lo, hi, hi - lo);
86 27d28098 2007-04-21 devnull hi = head->size - head->blocksize;
87 27d28098 2007-04-21 devnull clumpmax = head->blocksize / ClumpInfoSize;
88 27d28098 2007-04-21 devnull if(clumpmax > 0)
89 27d28098 2007-04-21 devnull lo = hi - (u64int)arena.diskstats.clumps/clumpmax * head->blocksize;
90 27d28098 2007-04-21 devnull else
91 27d28098 2007-04-21 devnull lo = hi;
92 27d28098 2007-04-21 devnull fprint(2, "\t%llx-%llx: clumps (%llx)\n", lo, hi, hi - lo);
93 27d28098 2007-04-21 devnull fprint(2, "\t%llx-%llx: tail\n", hi, hi + head->blocksize);
94 fa325e9b 2020-01-10 cross
95 27d28098 2007-04-21 devnull fprint(2, "arena:\n");
96 27d28098 2007-04-21 devnull printarena(2, &arena);
97 686bd37d 2006-07-18 devnull return 0;
98 686bd37d 2006-07-18 devnull }
99 686bd37d 2006-07-18 devnull
100 686bd37d 2006-07-18 devnull static void
101 686bd37d 2006-07-18 devnull cmparena(char *name, vlong len)
102 686bd37d 2006-07-18 devnull {
103 686bd37d 2006-07-18 devnull ArenaHead head;
104 686bd37d 2006-07-18 devnull DigestState s;
105 686bd37d 2006-07-18 devnull u64int n, e;
106 686bd37d 2006-07-18 devnull u32int bs;
107 686bd37d 2006-07-18 devnull int i, j;
108 686bd37d 2006-07-18 devnull char buf[20];
109 686bd37d 2006-07-18 devnull
110 686bd37d 2006-07-18 devnull fprint(2, "cmp %s\n", name);
111 686bd37d 2006-07-18 devnull
112 686bd37d 2006-07-18 devnull memset(&s, 0, sizeof s);
113 686bd37d 2006-07-18 devnull
114 686bd37d 2006-07-18 devnull /*
115 686bd37d 2006-07-18 devnull * read a little bit, which will include the header
116 686bd37d 2006-07-18 devnull */
117 686bd37d 2006-07-18 devnull if(readblock(fd, data, HeadSize) < 0){
118 686bd37d 2006-07-18 devnull fprint(2, "%s: reading header: %r\n", name);
119 686bd37d 2006-07-18 devnull return;
120 686bd37d 2006-07-18 devnull }
121 686bd37d 2006-07-18 devnull if(unpackarenahead(&head, data) < 0){
122 686bd37d 2006-07-18 devnull fprint(2, "%s: corrupt arena header: %r\n", name);
123 686bd37d 2006-07-18 devnull return;
124 686bd37d 2006-07-18 devnull }
125 686bd37d 2006-07-18 devnull if(head.version != ArenaVersion4 && head.version != ArenaVersion5)
126 686bd37d 2006-07-18 devnull fprint(2, "%s: warning: unknown arena version %d\n", name, head.version);
127 686bd37d 2006-07-18 devnull if(len != 0 && len != head.size)
128 686bd37d 2006-07-18 devnull fprint(2, "%s: warning: unexpected length %lld != %lld\n", name, head.size, len);
129 686bd37d 2006-07-18 devnull if(strcmp(name, "<stdin>") != 0 && strcmp(head.name, name) != 0)
130 686bd37d 2006-07-18 devnull fprint(2, "%s: warning: unexpected name %s\n", name, head.name);
131 686bd37d 2006-07-18 devnull
132 686bd37d 2006-07-18 devnull if(readblock(fd1, data1, HeadSize) < 0){
133 686bd37d 2006-07-18 devnull fprint(2, "%s: reading header: %r\n", name);
134 686bd37d 2006-07-18 devnull return;
135 686bd37d 2006-07-18 devnull }
136 686bd37d 2006-07-18 devnull if(unpackarenahead(&head, data) < 0){
137 686bd37d 2006-07-18 devnull fprint(2, "%s: corrupt arena header: %r\n", name);
138 686bd37d 2006-07-18 devnull return;
139 686bd37d 2006-07-18 devnull }
140 686bd37d 2006-07-18 devnull if(head.version != ArenaVersion4 && head.version != ArenaVersion5)
141 686bd37d 2006-07-18 devnull fprint(2, "%s: warning: unknown arena version %d\n", name, head.version);
142 686bd37d 2006-07-18 devnull if(len != 0 && len != head.size)
143 686bd37d 2006-07-18 devnull fprint(2, "%s: warning: unexpected length %lld != %lld\n", name, head.size, len);
144 686bd37d 2006-07-18 devnull if(strcmp(name, "<stdin>") != 0 && strcmp(head.name, name) != 0)
145 686bd37d 2006-07-18 devnull fprint(2, "%s: warning: unexpected name %s\n", name, head.name);
146 686bd37d 2006-07-18 devnull
147 686bd37d 2006-07-18 devnull seek(fd, -HeadSize, 1);
148 686bd37d 2006-07-18 devnull seek(fd1, -HeadSize, 1);
149 686bd37d 2006-07-18 devnull
150 27d28098 2007-04-21 devnull if(printheader(name, &head, fd) < 0)
151 27d28098 2007-04-21 devnull return;
152 fa325e9b 2020-01-10 cross
153 686bd37d 2006-07-18 devnull /*
154 686bd37d 2006-07-18 devnull * now we know how much to read
155 686bd37d 2006-07-18 devnull * read everything but the last block, which is special
156 686bd37d 2006-07-18 devnull */
157 686bd37d 2006-07-18 devnull e = head.size;
158 686bd37d 2006-07-18 devnull bs = blocksize;
159 686bd37d 2006-07-18 devnull for(n = 0; n < e; n += bs){
160 686bd37d 2006-07-18 devnull if(n + bs > e)
161 686bd37d 2006-07-18 devnull bs = e - n;
162 686bd37d 2006-07-18 devnull if(readblock(fd, data, bs) < 0){
163 686bd37d 2006-07-18 devnull fprint(2, "%s: read data: %r\n", name);
164 686bd37d 2006-07-18 devnull return;
165 686bd37d 2006-07-18 devnull }
166 686bd37d 2006-07-18 devnull if(readblock(fd1, data1, bs) < 0){
167 686bd37d 2006-07-18 devnull fprint(2, "%s: read data: %r\n", name);
168 686bd37d 2006-07-18 devnull return;
169 686bd37d 2006-07-18 devnull }
170 686bd37d 2006-07-18 devnull if(memcmp(data, data1, bs) != 0){
171 686bd37d 2006-07-18 devnull print("mismatch at %llx\n", n);
172 686bd37d 2006-07-18 devnull for(i=0; i<bs; i+=16){
173 686bd37d 2006-07-18 devnull if(memcmp(data+i, data1+i, 16) != 0){
174 686bd37d 2006-07-18 devnull snprint(buf, sizeof buf, "%llx", n+i);
175 686bd37d 2006-07-18 devnull print("%s ", buf);
176 686bd37d 2006-07-18 devnull for(j=0; j<16; j++){
177 686bd37d 2006-07-18 devnull print(" %.2ux", data[i+j]);
178 686bd37d 2006-07-18 devnull if(j == 7)
179 686bd37d 2006-07-18 devnull print(" -");
180 686bd37d 2006-07-18 devnull }
181 686bd37d 2006-07-18 devnull print("\n");
182 686bd37d 2006-07-18 devnull print("%*s ", (int)strlen(buf), "");
183 686bd37d 2006-07-18 devnull for(j=0; j<16; j++){
184 686bd37d 2006-07-18 devnull print(" %.2ux", data1[i+j]);
185 686bd37d 2006-07-18 devnull if(j == 7)
186 686bd37d 2006-07-18 devnull print(" -");
187 686bd37d 2006-07-18 devnull }
188 686bd37d 2006-07-18 devnull print("\n");
189 686bd37d 2006-07-18 devnull }
190 686bd37d 2006-07-18 devnull }
191 686bd37d 2006-07-18 devnull }
192 686bd37d 2006-07-18 devnull }
193 686bd37d 2006-07-18 devnull }
194 686bd37d 2006-07-18 devnull
195 686bd37d 2006-07-18 devnull static int
196 686bd37d 2006-07-18 devnull shouldcheck(char *name, char **s, int n)
197 686bd37d 2006-07-18 devnull {
198 686bd37d 2006-07-18 devnull int i;
199 fa325e9b 2020-01-10 cross
200 686bd37d 2006-07-18 devnull if(n == 0)
201 686bd37d 2006-07-18 devnull return 1;
202 686bd37d 2006-07-18 devnull
203 686bd37d 2006-07-18 devnull for(i=0; i<n; i++){
204 686bd37d 2006-07-18 devnull if(s[i] && strcmp(name, s[i]) == 0){
205 686bd37d 2006-07-18 devnull s[i] = nil;
206 686bd37d 2006-07-18 devnull return 1;
207 686bd37d 2006-07-18 devnull }
208 686bd37d 2006-07-18 devnull }
209 686bd37d 2006-07-18 devnull return 0;
210 686bd37d 2006-07-18 devnull }
211 686bd37d 2006-07-18 devnull
212 686bd37d 2006-07-18 devnull char *
213 686bd37d 2006-07-18 devnull readap(int fd, ArenaPart *ap)
214 686bd37d 2006-07-18 devnull {
215 686bd37d 2006-07-18 devnull char *table;
216 fa325e9b 2020-01-10 cross
217 686bd37d 2006-07-18 devnull if(preadblock(fd, data, 8192, PartBlank) < 0)
218 686bd37d 2006-07-18 devnull sysfatal("read arena part header: %r");
219 686bd37d 2006-07-18 devnull if(unpackarenapart(ap, data) < 0)
220 686bd37d 2006-07-18 devnull sysfatal("corrupted arena part header: %r");
221 686bd37d 2006-07-18 devnull fprint(2, "# arena part version=%d blocksize=%d arenabase=%d\n",
222 686bd37d 2006-07-18 devnull ap->version, ap->blocksize, ap->arenabase);
223 686bd37d 2006-07-18 devnull ap->tabbase = (PartBlank+HeadSize+ap->blocksize-1)&~(ap->blocksize-1);
224 686bd37d 2006-07-18 devnull ap->tabsize = ap->arenabase - ap->tabbase;
225 686bd37d 2006-07-18 devnull table = malloc(ap->tabsize+1);
226 686bd37d 2006-07-18 devnull if(preadblock(fd, (uchar*)table, ap->tabsize, ap->tabbase) < 0)
227 686bd37d 2006-07-18 devnull sysfatal("reading arena part directory: %r");
228 686bd37d 2006-07-18 devnull table[ap->tabsize] = 0;
229 686bd37d 2006-07-18 devnull return table;
230 686bd37d 2006-07-18 devnull }
231 686bd37d 2006-07-18 devnull
232 686bd37d 2006-07-18 devnull void
233 686bd37d 2006-07-18 devnull threadmain(int argc, char *argv[])
234 686bd37d 2006-07-18 devnull {
235 686bd37d 2006-07-18 devnull int i, nline;
236 686bd37d 2006-07-18 devnull char *p, *q, *table, *table1, *f[10], line[256];
237 686bd37d 2006-07-18 devnull vlong start, stop;
238 686bd37d 2006-07-18 devnull ArenaPart ap;
239 686bd37d 2006-07-18 devnull ArenaPart ap1;
240 fa325e9b 2020-01-10 cross
241 686bd37d 2006-07-18 devnull ventifmtinstall();
242 686bd37d 2006-07-18 devnull blocksize = MaxIoSize;
243 686bd37d 2006-07-18 devnull ARGBEGIN{
244 686bd37d 2006-07-18 devnull case 'b':
245 686bd37d 2006-07-18 devnull blocksize = unittoull(EARGF(usage()));
246 686bd37d 2006-07-18 devnull break;
247 686bd37d 2006-07-18 devnull case 's':
248 686bd37d 2006-07-18 devnull sleepms = atoi(EARGF(usage()));
249 686bd37d 2006-07-18 devnull break;
250 686bd37d 2006-07-18 devnull case 'v':
251 686bd37d 2006-07-18 devnull verbose++;
252 686bd37d 2006-07-18 devnull break;
253 686bd37d 2006-07-18 devnull default:
254 686bd37d 2006-07-18 devnull usage();
255 686bd37d 2006-07-18 devnull break;
256 686bd37d 2006-07-18 devnull }ARGEND
257 686bd37d 2006-07-18 devnull
258 686bd37d 2006-07-18 devnull if(argc < 2)
259 686bd37d 2006-07-18 devnull usage();
260 686bd37d 2006-07-18 devnull
261 686bd37d 2006-07-18 devnull data = vtmalloc(blocksize);
262 686bd37d 2006-07-18 devnull data1 = vtmalloc(blocksize);
263 686bd37d 2006-07-18 devnull if((fd = open(argv[0], OREAD)) < 0)
264 686bd37d 2006-07-18 devnull sysfatal("open %s: %r", argv[0]);
265 686bd37d 2006-07-18 devnull if((fd1 = open(argv[1], OREAD)) < 0)
266 686bd37d 2006-07-18 devnull sysfatal("open %s: %r", argv[0]);
267 686bd37d 2006-07-18 devnull
268 686bd37d 2006-07-18 devnull table = readap(fd, &ap);
269 686bd37d 2006-07-18 devnull table1 = readap(fd1, &ap1);
270 686bd37d 2006-07-18 devnull if(strcmp(table, table1) != 0)
271 686bd37d 2006-07-18 devnull sysfatal("arena partitions do not have identical tables");
272 686bd37d 2006-07-18 devnull
273 686bd37d 2006-07-18 devnull nline = atoi(table);
274 686bd37d 2006-07-18 devnull p = strchr(table, '\n');
275 686bd37d 2006-07-18 devnull if(p)
276 686bd37d 2006-07-18 devnull p++;
277 686bd37d 2006-07-18 devnull for(i=0; i<nline; i++){
278 686bd37d 2006-07-18 devnull if(p == nil){
279 686bd37d 2006-07-18 devnull fprint(2, "warning: unexpected arena table end\n");
280 686bd37d 2006-07-18 devnull break;
281 686bd37d 2006-07-18 devnull }
282 686bd37d 2006-07-18 devnull q = strchr(p, '\n');
283 686bd37d 2006-07-18 devnull if(q)
284 686bd37d 2006-07-18 devnull *q++ = 0;
285 686bd37d 2006-07-18 devnull if(strlen(p) >= sizeof line){
286 686bd37d 2006-07-18 devnull fprint(2, "warning: long arena table line: %s\n", p);
287 686bd37d 2006-07-18 devnull p = q;
288 686bd37d 2006-07-18 devnull continue;
289 686bd37d 2006-07-18 devnull }
290 686bd37d 2006-07-18 devnull strcpy(line, p);
291 686bd37d 2006-07-18 devnull memset(f, 0, sizeof f);
292 686bd37d 2006-07-18 devnull if(tokenize(line, f, nelem(f)) < 3){
293 686bd37d 2006-07-18 devnull fprint(2, "warning: bad arena table line: %s\n", p);
294 686bd37d 2006-07-18 devnull p = q;
295 686bd37d 2006-07-18 devnull continue;
296 686bd37d 2006-07-18 devnull }
297 686bd37d 2006-07-18 devnull p = q;
298 686bd37d 2006-07-18 devnull if(shouldcheck(f[0], argv+1, argc-1)){
299 686bd37d 2006-07-18 devnull start = strtoull(f[1], 0, 0);
300 686bd37d 2006-07-18 devnull stop = strtoull(f[2], 0, 0);
301 686bd37d 2006-07-18 devnull if(stop <= start){
302 686bd37d 2006-07-18 devnull fprint(2, "%s: bad start,stop %lld,%lld\n", f[0], stop, start);
303 686bd37d 2006-07-18 devnull continue;
304 686bd37d 2006-07-18 devnull }
305 686bd37d 2006-07-18 devnull if(seek(fd, start, 0) < 0)
306 686bd37d 2006-07-18 devnull fprint(2, "%s: seek to start: %r\n", f[0]);
307 686bd37d 2006-07-18 devnull if(seek(fd1, start, 0) < 0)
308 686bd37d 2006-07-18 devnull fprint(2, "%s: seek to start: %r\n", f[0]);
309 686bd37d 2006-07-18 devnull cmparena(f[0], stop - start);
310 686bd37d 2006-07-18 devnull }
311 686bd37d 2006-07-18 devnull }
312 686bd37d 2006-07-18 devnull for(i=2; i<argc; i++)
313 686bd37d 2006-07-18 devnull if(argv[i] != 0)
314 686bd37d 2006-07-18 devnull fprint(2, "%s: did not find arena\n", argv[i]);
315 686bd37d 2006-07-18 devnull
316 686bd37d 2006-07-18 devnull threadexitsall(nil);
317 686bd37d 2006-07-18 devnull }