sortientry.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. #include "stdinc.h"
  2. #include "dat.h"
  3. #include "fns.h"
  4. typedef struct IEBuck IEBuck;
  5. typedef struct IEBucks IEBucks;
  6. enum
  7. {
  8. ClumpChunks = 32*1024
  9. };
  10. struct IEBuck
  11. {
  12. u32int head; /* head of chain of chunks on the disk */
  13. u32int used; /* usage of the last chunk */
  14. u64int total; /* total number of bytes in this bucket */
  15. u8int *buf; /* chunk of entries for this bucket */
  16. };
  17. struct IEBucks
  18. {
  19. Part *part;
  20. u64int off; /* offset for writing data in the partition */
  21. u32int chunks; /* total chunks written to fd */
  22. u64int max; /* max bytes entered in any one bucket */
  23. int bits; /* number of bits in initial bucket sort */
  24. int nbucks; /* 1 << bits, the number of buckets */
  25. u32int size; /* bytes in each of the buckets chunks */
  26. u32int usable; /* amount usable for IEntry data */
  27. u8int *buf; /* buffer for all chunks */
  28. IEBuck *bucks;
  29. };
  30. #define U32GET(p) (((p)[0]<<24)|((p)[1]<<16)|((p)[2]<<8)|(p)[3])
  31. #define U32PUT(p,v) (p)[0]=(v)>>24;(p)[1]=(v)>>16;(p)[2]=(v)>>8;(p)[3]=(v)
  32. static IEBucks *initIEBucks(Part *part, int bits, u32int size);
  33. static int flushIEBuck(IEBucks *ib, int b, int reset);
  34. static int flushIEBucks(IEBucks *ib);
  35. static u32int sortIEBuck(IEBucks *ib, int b);
  36. static u64int sortIEBucks(IEBucks *ib);
  37. static int sprayIEntry(IEBucks *ib, IEntry *ie);
  38. static u32int readArenaInfo(IEBucks *ib, Arena *arena, u64int a);
  39. static u32int readIEBuck(IEBucks *ib, int b);
  40. static void freeIEBucks(IEBucks *ib);
  41. /*
  42. * build a sorted file with all Ientries which should be in ix.
  43. * assumes the arenas' directories are up to date.
  44. * reads each, converts the entries to index entries,
  45. * and sorts them.
  46. */
  47. u64int
  48. sortRawIEntries(Index *ix, Part *tmp, u64int *base)
  49. {
  50. IEBucks *ib;
  51. u64int clumps, sorted;
  52. u32int n;
  53. int i, ok;
  54. //ZZZ should allow configuration of bits, bucket size
  55. ib = initIEBucks(tmp, 8, 64*1024);
  56. if(ib == nil){
  57. setErr(EOk, "can't create sorting buckets: %R");
  58. return TWID64;
  59. }
  60. ok = 1;
  61. clumps = 0;
  62. for(i = 0; i < ix->narenas; i++){
  63. n = readArenaInfo(ib, ix->arenas[i], ix->amap[i].start);
  64. if(n == TWID32){
  65. ok = 0;
  66. break;
  67. }
  68. clumps += n;
  69. }
  70. if(ok){
  71. fprint(2, "got %lld clumps - starting sort\n", clumps);
  72. sorted = sortIEBucks(ib);
  73. *base = (u64int)ib->chunks * ib->size;
  74. if(sorted != clumps){
  75. fprint(2, "sorting messed up: clumps=%lld sorted=%lld\n", clumps, sorted);
  76. ok = 0;
  77. }
  78. }
  79. freeIEBucks(ib);
  80. if(!ok)
  81. return TWID64;
  82. return clumps;
  83. }
  84. /*
  85. * read in all of the arena's clump directory,
  86. * convert to IEntry format, and bucket sort based
  87. * on the first few bits.
  88. */
  89. static u32int
  90. readArenaInfo(IEBucks *ib, Arena *arena, u64int a)
  91. {
  92. IEntry ie;
  93. ClumpInfo *ci, *cis;
  94. u32int clump;
  95. int i, n, ok;
  96. //ZZZ remove fprint?
  97. if(arena->clumps)
  98. fprint(2, "reading directory for arena=%s with %d entries\n", arena->name, arena->clumps);
  99. cis = MKN(ClumpInfo, ClumpChunks);
  100. ok = 1;
  101. memset(&ie, 0, sizeof(IEntry));
  102. for(clump = 0; clump < arena->clumps && ok; clump += n){
  103. n = ClumpChunks;
  104. if(n > arena->clumps - clump)
  105. n = arena->clumps - clump;
  106. if(readClumpInfos(arena, clump, cis, n) != n){
  107. setErr(EOk, "arena directory read failed: %R");
  108. ok = 0;
  109. break;
  110. }
  111. for(i = 0; i < n; i++){
  112. ci = &cis[i];
  113. ie.ia.type = ci->type;
  114. ie.ia.size = ci->uncsize;
  115. ie.ia.addr = a;
  116. a += ci->size + ClumpSize;
  117. ie.ia.blocks = (ci->size + ClumpSize + (1 << ABlockLog) - 1) >> ABlockLog;
  118. scoreCp(ie.score, ci->score);
  119. if(!sprayIEntry(ib, &ie)){
  120. setErr(EOk, "can't put entry into bucket: %R");
  121. ok = 0;
  122. break;
  123. }
  124. }
  125. }
  126. free(cis);
  127. if(!ok)
  128. return TWID32;
  129. return clump;
  130. }
  131. /*
  132. * initialize the external bucket sorting data structures
  133. */
  134. static IEBucks*
  135. initIEBucks(Part *part, int bits, u32int size)
  136. {
  137. IEBucks *ib;
  138. int i;
  139. ib = MKZ(IEBucks);
  140. if(ib == nil){
  141. setErr(EOk, "out of memory");
  142. return nil;
  143. }
  144. ib->bits = bits;
  145. ib->nbucks = 1 << bits;
  146. ib->size = size;
  147. ib->usable = (size - U32Size) / IEntrySize * IEntrySize;
  148. ib->bucks = MKNZ(IEBuck, ib->nbucks);
  149. if(ib->bucks == nil){
  150. setErr(EOk, "out of memory allocation sorting buckets");
  151. freeIEBucks(ib);
  152. return nil;
  153. }
  154. ib->buf = MKN(u8int, size * (1 << bits));
  155. if(ib->buf == nil){
  156. setErr(EOk, "out of memory allocating sorting buckets' buffers");
  157. freeIEBucks(ib);
  158. return nil;
  159. }
  160. for(i = 0; i < ib->nbucks; i++){
  161. ib->bucks[i].head = TWID32;
  162. ib->bucks[i].buf = &ib->buf[i * size];
  163. }
  164. ib->part = part;
  165. return ib;
  166. }
  167. static void
  168. freeIEBucks(IEBucks *ib)
  169. {
  170. if(ib == nil)
  171. return;
  172. free(ib->bucks);
  173. free(ib->buf);
  174. free(ib);
  175. }
  176. /*
  177. * initial sort: put the entry into the correct bucket
  178. */
  179. static int
  180. sprayIEntry(IEBucks *ib, IEntry *ie)
  181. {
  182. u32int n;
  183. int b;
  184. b = hashBits(ie->score, ib->bits);
  185. n = ib->bucks[b].used;
  186. packIEntry(ie, &ib->bucks[b].buf[n]);
  187. n += IEntrySize;
  188. ib->bucks[b].used = n;
  189. if(n + IEntrySize <= ib->usable)
  190. return 1;
  191. return flushIEBuck(ib, b, 1);
  192. }
  193. /*
  194. * finish sorting:
  195. * for each bucket, read it in and sort it
  196. * write out the the final file
  197. */
  198. static u64int
  199. sortIEBucks(IEBucks *ib)
  200. {
  201. u64int tot;
  202. u32int n;
  203. int i;
  204. if(!flushIEBucks(ib))
  205. return TWID64;
  206. for(i = 0; i < ib->nbucks; i++)
  207. ib->bucks[i].buf = nil;
  208. ib->off = (u64int)ib->chunks * ib->size;
  209. free(ib->buf);
  210. fprint(2, "ib->max = %lld\n", ib->max);
  211. fprint(2, "ib->chunks = %ud\n", ib->chunks);
  212. ib->buf = MKN(u8int, ib->max + U32Size);
  213. if(ib->buf == nil){
  214. setErr(EOk, "out of memory allocating final sorting buffer; try more buckets");
  215. return TWID64;
  216. }
  217. tot = 0;
  218. for(i = 0; i < ib->nbucks; i++){
  219. n = sortIEBuck(ib, i);
  220. if(n == TWID32)
  221. return TWID64;
  222. tot += n;
  223. }
  224. return tot;
  225. }
  226. /*
  227. * sort from bucket b of ib into the output file to
  228. */
  229. static u32int
  230. sortIEBuck(IEBucks *ib, int b)
  231. {
  232. u32int n;
  233. n = readIEBuck(ib, b);
  234. if(n == TWID32)
  235. return TWID32;
  236. qsort(ib->buf, n, IEntrySize, ientryCmp);
  237. if(!writePart(ib->part, ib->off, ib->buf, n * IEntrySize)){
  238. setErr(EOk, "can't write sorted bucket: %R");
  239. return TWID32;
  240. }
  241. ib->off += n * IEntrySize;
  242. return n;
  243. }
  244. /*
  245. * write out a single bucket
  246. */
  247. static int
  248. flushIEBuck(IEBucks *ib, int b, int reset)
  249. {
  250. u32int n;
  251. if(ib->bucks[b].used == 0)
  252. return 1;
  253. n = ib->bucks[b].used;
  254. U32PUT(&ib->bucks[b].buf[n], ib->bucks[b].head);
  255. n += U32Size;
  256. if(!writePart(ib->part, (u64int)ib->chunks * ib->size, ib->bucks[b].buf, n)){
  257. setErr(EOk, "can't write sorting bucket to file: %R");
  258. return 0;
  259. }
  260. ib->bucks[b].head = ib->chunks++;
  261. ib->bucks[b].total += ib->bucks[b].used;
  262. if(reset)
  263. ib->bucks[b].used = 0;
  264. return 1;
  265. }
  266. /*
  267. * write out all of the buckets, and compute
  268. * the maximum size of any bucket
  269. */
  270. static int
  271. flushIEBucks(IEBucks *ib)
  272. {
  273. int i;
  274. for(i = 0; i < ib->nbucks; i++){
  275. if(!flushIEBuck(ib, i, 0))
  276. return 0;
  277. if(ib->bucks[i].total > ib->max)
  278. ib->max = ib->bucks[i].total;
  279. }
  280. return 1;
  281. }
  282. /*
  283. * read in the chained buffers for bucket b,
  284. * and return it's total number of IEntries
  285. */
  286. static u32int
  287. readIEBuck(IEBucks *ib, int b)
  288. {
  289. u32int head, n, m;
  290. head = ib->bucks[b].head;
  291. n = 0;
  292. m = ib->bucks[b].used;
  293. if(m == 0)
  294. m = ib->usable;
  295. fprint(2, "%d total = %lld\n", b, ib->bucks[b].total);
  296. while(head != TWID32){
  297. if(!readPart(ib->part, (u64int)head * ib->size, &ib->buf[n], m + U32Size)){
  298. fprint(2, "n = %ud\n", n);
  299. setErr(EOk, "can't read index sort bucket: %R");
  300. return TWID32;
  301. }
  302. n += m;
  303. head = U32GET(&ib->buf[n]);
  304. m = ib->usable;
  305. }
  306. fprint(2, "n = %ud\n", n);
  307. return n / IEntrySize;
  308. }