checkindex.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. }
  58. putDBlock(eb);
  59. return ok;
  60. }
  61. int
  62. checkIndex(Index *ix, Part *part, u64int off, u64int clumps, int zero)
  63. {
  64. IEStream *ies;
  65. IBucket ib, zib;
  66. ZBlock *z, *b;
  67. u32int next, buck;
  68. int ok, bok;
  69. u64int found = 0;
  70. //ZZZ make buffer size configurable
  71. b = allocZBlock(ix->blockSize, 0);
  72. z = allocZBlock(ix->blockSize, 1);
  73. ies = initIEStream(part, off, clumps, 64*1024);
  74. if(b == nil || z == nil || ies == nil){
  75. ok = 0;
  76. goto breakout;
  77. }
  78. ok = 1;
  79. next = 0;
  80. ib.data = b->data;
  81. zib.data = z->data;
  82. zib.n = 0;
  83. zib.next = 0;
  84. for(;;){
  85. buck = buildBucket(ix, ies, &ib);
  86. found += ib.n;
  87. if(zero){
  88. for(; next != buck; next++){
  89. if(next == ix->buckets){
  90. if(buck != TWID32)
  91. fprint(2, "bucket out of range\n");
  92. goto breakout;
  93. }
  94. bok = checkBucket(ix, next, &zib);
  95. if(!bok){
  96. fprint(2, "bad bucket=%d found: %R\n", next);
  97. ok = 0;
  98. }
  99. }
  100. }
  101. if(buck >= ix->buckets){
  102. if(buck == TWID32)
  103. break;
  104. fprint(2, "bucket out of range\n");
  105. ok = 0;
  106. goto breakout;
  107. }
  108. bok = checkBucket(ix, buck, &ib);
  109. if(!bok){
  110. fprint(2, "bad bucket found=%lld: %R\n", found);
  111. ok = 0;
  112. }
  113. next = buck + 1;
  114. }
  115. breakout:;
  116. fprint(2, "found %lld entries in sorted list\n", found);
  117. freeIEStream(ies);
  118. freeZBlock(z);
  119. freeZBlock(b);
  120. return ok;
  121. }
  122. void
  123. usage(void)
  124. {
  125. fprint(2, "usage: checkindex [-f] [-B blockcachesize] config tmp\n");
  126. exits(0);
  127. }
  128. int
  129. main(int argc, char *argv[])
  130. {
  131. Part *part;
  132. u64int clumps, base;
  133. u32int bcmem;
  134. int fix, skipz;
  135. vtAttach();
  136. fix = 0;
  137. bcmem = 0;
  138. skipz = 0;
  139. ARGBEGIN{
  140. case 'B':
  141. bcmem = unittoull(ARGF());
  142. break;
  143. case 'f':
  144. fix++;
  145. break;
  146. case 'Z':
  147. skipz = 1;
  148. break;
  149. default:
  150. usage();
  151. break;
  152. }ARGEND
  153. if(!fix)
  154. readonly = 1;
  155. if(argc != 2)
  156. usage();
  157. if(!initVenti(argv[0], nil))
  158. fatal("can't init venti: %R");
  159. if(bcmem < maxBlockSize * (mainIndex->narenas + mainIndex->nsects * 4 + 16))
  160. bcmem = maxBlockSize * (mainIndex->narenas + mainIndex->nsects * 4 + 16);
  161. fprint(2, "initialize %d bytes of disk block cache\n", bcmem);
  162. initDCache(bcmem);
  163. part = initPart(argv[1], 1);
  164. if(part == nil)
  165. fatal("can't initialize temporary partition: %R");
  166. clumps = sortRawIEntries(mainIndex, part, &base);
  167. if(clumps == TWID64)
  168. fatal("can't build sorted index: %R");
  169. fprint(2, "found and sorted index entries for clumps=%lld at %lld\n", clumps, base);
  170. checkIndex(mainIndex, part, base, clumps, !skipz);
  171. exits(0);
  172. return 0; /* shut up stupid compiler */
  173. }