checkindex.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. #include "stdinc.h"
  2. #include "dat.h"
  3. #include "fns.h"
  4. static int
  5. checkBucket(Index *ix, u32int buck, IBucket *ib)
  6. {
  7. ISect *is;
  8. DBlock *eb;
  9. IBucket eib;
  10. IEntry ie, eie;
  11. int i, ei, ok, c;
  12. is = findISect(ix, buck);
  13. if(is == nil){
  14. setErr(EAdmin, "bad math in checkBuckets");
  15. return 0;
  16. }
  17. buck -= is->start;
  18. eb = getDBlock(is->part, is->blockBase + ((u64int)buck << is->blockLog), 1);
  19. if(eb == nil)
  20. return 0;
  21. unpackIBucket(&eib, eb->data);
  22. ok = 1;
  23. ei = 0;
  24. for(i = 0; i < ib->n; i++){
  25. while(ei < eib.n){
  26. c = ientryCmp(&ib->data[i * IEntrySize], &eib.data[ei * IEntrySize]);
  27. if(c == 0){
  28. unpackIEntry(&ie, &ib->data[i * IEntrySize]);
  29. unpackIEntry(&eie, &eib.data[ei * IEntrySize]);
  30. if(!iAddrEq(&ie.ia, &eie.ia)){
  31. fprint(2, "bad entry in index for score=%V\n", &ib->data[i * IEntrySize]);
  32. fprint(2, "\taddr=%lld type=%d size=%d blocks=%d\n",
  33. ie.ia.addr, ie.ia.type, ie.ia.size, ie.ia.blocks);
  34. fprint(2, "\taddr=%lld type=%d size=%d blocks=%d\n",
  35. eie.ia.addr, eie.ia.type, eie.ia.size, eie.ia.blocks);
  36. }
  37. ei++;
  38. goto cont;
  39. }
  40. if(c < 0)
  41. break;
  42. if(1)
  43. fprint(2, "spurious entry in index for score=%V type=%d\n",
  44. &eib.data[ei * IEntrySize], eib.data[ei * IEntrySize + IEntryTypeOff]);
  45. ei++;
  46. ok = 0;
  47. }
  48. fprint(2, "missing entry in index for score=%V type=%d\n",
  49. &ib->data[i * IEntrySize], ib->data[i * IEntrySize + IEntryTypeOff]);
  50. ok = 0;
  51. cont:;
  52. }
  53. for(; ei < eib.n; ei++){
  54. if(1) fprint(2, "spurious entry in index for score=%V; found %d entries expected %d\n",
  55. &eib.data[ei * IEntrySize], eib.n, ib->n);
  56. ok = 0;
  57. break;
  58. }
  59. putDBlock(eb);
  60. return ok;
  61. }
  62. int
  63. checkIndex(Index *ix, Part *part, u64int off, u64int clumps, int zero)
  64. {
  65. IEStream *ies;
  66. IBucket ib, zib;
  67. ZBlock *z, *b;
  68. u32int next, buck;
  69. int ok, bok;
  70. u64int found = 0;
  71. //ZZZ make buffer size configurable
  72. b = allocZBlock(ix->blockSize, 0);
  73. z = allocZBlock(ix->blockSize, 1);
  74. ies = initIEStream(part, off, clumps, 64*1024);
  75. if(b == nil || z == nil || ies == nil){
  76. ok = 0;
  77. goto breakout;
  78. return 0;
  79. }
  80. ok = 1;
  81. next = 0;
  82. ib.data = b->data;
  83. zib.data = z->data;
  84. zib.n = 0;
  85. zib.next = 0;
  86. for(;;){
  87. buck = buildBucket(ix, ies, &ib);
  88. found += ib.n;
  89. if(zero){
  90. for(; next != buck; next++){
  91. if(next == ix->buckets){
  92. if(buck != TWID32)
  93. fprint(2, "bucket out of range\n");
  94. goto breakout;
  95. }
  96. bok = checkBucket(ix, next, &zib);
  97. if(!bok){
  98. fprint(2, "bad bucket=%d found: %R\n", next);
  99. ok = 0;
  100. }
  101. }
  102. }
  103. if(buck >= ix->buckets){
  104. if(buck == TWID32)
  105. break;
  106. fprint(2, "bucket out of range\n");
  107. ok = 0;
  108. goto breakout;
  109. }
  110. bok = checkBucket(ix, buck, &ib);
  111. if(!bok){
  112. fprint(2, "bad bucket found=%lld: %R\n", found);
  113. ok = 0;
  114. }
  115. next = buck + 1;
  116. }
  117. breakout:;
  118. fprint(2, "found %lld entries in sorted list\n", found);
  119. freeIEStream(ies);
  120. freeZBlock(z);
  121. freeZBlock(b);
  122. return ok;
  123. }
  124. void
  125. usage(void)
  126. {
  127. fprint(2, "usage: checkindex [-f] [-B blockcachesize] config tmp\n");
  128. exits(0);
  129. }
  130. int
  131. main(int argc, char *argv[])
  132. {
  133. Part *part;
  134. u64int clumps, base;
  135. u32int bcmem;
  136. int fix, skipz;
  137. vtAttach();
  138. fix = 0;
  139. bcmem = 0;
  140. skipz = 0;
  141. ARGBEGIN{
  142. case 'B':
  143. bcmem = unittoull(ARGF());
  144. break;
  145. case 'f':
  146. fix++;
  147. break;
  148. case 'Z':
  149. skipz = 1;
  150. break;
  151. default:
  152. usage();
  153. break;
  154. }ARGEND
  155. if(!fix)
  156. readonly = 1;
  157. if(argc != 2)
  158. usage();
  159. if(!initVenti(argv[0], nil))
  160. fatal("can't init venti: %R");
  161. if(bcmem < maxBlockSize * (mainIndex->narenas + mainIndex->nsects * 4 + 16))
  162. bcmem = maxBlockSize * (mainIndex->narenas + mainIndex->nsects * 4 + 16);
  163. fprint(2, "initialize %d bytes of disk block cache\n", bcmem);
  164. initDCache(bcmem);
  165. part = initPart(argv[1], 1);
  166. if(part == nil)
  167. fatal("can't initialize temporary partition: %R");
  168. clumps = sortRawIEntries(mainIndex, part, &base);
  169. if(clumps == TWID64)
  170. fatal("can't build sorted index: %R");
  171. fprint(2, "found and sorted index entries for clumps=%lld at %lld\n", clumps, base);
  172. checkIndex(mainIndex, part, base, clumps, !skipz);
  173. exits(0);
  174. return 0; /* shut up stupid compiler */
  175. }