srcload.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. #include "stdinc.h"
  2. #include <bio.h>
  3. #include "vac.h"
  4. #include "dat.h"
  5. #include "fns.h"
  6. #include "error.h"
  7. int num = 1000;
  8. int length = 20*1024;
  9. int block= 1024;
  10. int bush = 4;
  11. int iter = 10000;
  12. Biobuf *bout;
  13. int maxdepth;
  14. Source *mkroot(Cache*);
  15. void new(Source*, int trace, int);
  16. int delete(Source*);
  17. void dump(Source*, int indent, ulong nentry);
  18. void dumpone(Source *s);
  19. int count(Source *s, int);
  20. void stats(Source *s);
  21. void
  22. main(int argc, char *argv[])
  23. {
  24. int i;
  25. Cache *c;
  26. char *host = nil;
  27. VtSession *z;
  28. int csize = 10000;
  29. Source *r;
  30. ulong t;
  31. t = time(0);
  32. fprint(1, "time = %lud\n", t);
  33. srand(t);
  34. ARGBEGIN{
  35. case 'i':
  36. iter = atoi(ARGF());
  37. break;
  38. case 'n':
  39. num = atoi(ARGF());
  40. break;
  41. case 'l':
  42. length = atoi(ARGF());
  43. break;
  44. case 'b':
  45. block = atoi(ARGF());
  46. break;
  47. case 'h':
  48. host = ARGF();
  49. break;
  50. case 'u':
  51. bush = atoi(ARGF());
  52. break;
  53. case 'c':
  54. csize = atoi(ARGF());
  55. break;
  56. }ARGEND;
  57. vtAttach();
  58. bout = vtMemAllocZ(sizeof(Biobuf));
  59. Binit(bout, 1, OWRITE);
  60. fmtinstall('V', vtScoreFmt);
  61. fmtinstall('R', vtErrFmt);
  62. z = vtDial(host);
  63. if(z == nil)
  64. vtFatal("could not connect to server: %s", vtGetError());
  65. if(!vtConnect(z, 0))
  66. sysfatal("vtConnect: %r");
  67. c = cacheAlloc(z, block, csize);
  68. r = mkroot(c);
  69. for(i=0; i<num; i++)
  70. new(r, 0, 0);
  71. for(i=0; i<iter; i++) {
  72. if(i % 10000 == 0)
  73. stats(r);
  74. new(r, 0, 0);
  75. delete(r);
  76. }
  77. fprint(2, "count = %d top = %lud\n", count(r, 0), sourceGetDirSize(r));
  78. // cacheCheck(c);
  79. fprint(2, "deleting\n");
  80. for(i=0; i<num; i++)
  81. delete(r);
  82. // dump(r, 0, 0);
  83. lumpDecRef(r->lump, 0);
  84. sourceRemove(r);
  85. cacheCheck(c);
  86. vtClose(z);
  87. vtDetach();
  88. exits(0);
  89. }
  90. Source *
  91. mkroot(Cache *c)
  92. {
  93. Lump *u;
  94. VtEntry *dir;
  95. Source *r;
  96. u = cacheAllocLump(c, VtDirType, cacheGetBlockSize(c), 1);
  97. dir = (VtEntry*)u->data;
  98. vtPutUint16(dir->psize, cacheGetBlockSize(c));
  99. vtPutUint16(dir->dsize, cacheGetBlockSize(c));
  100. dir->flag = VtEntryActive|VtEntryDir;
  101. memmove(dir->score, vtZeroScore, VtScoreSize);
  102. r = sourceAlloc(c, u, 0, 0);
  103. vtUnlock(u->lk);
  104. if(r == nil)
  105. sysfatal("could not create root source: %R");
  106. return r;
  107. }
  108. void
  109. new(Source *s, int trace, int depth)
  110. {
  111. int i, n;
  112. Source *ss;
  113. if(depth > maxdepth)
  114. maxdepth = depth;
  115. n = sourceGetDirSize(s);
  116. for(i=0; i<n; i++) {
  117. ss = sourceOpen(s, nrand(n), 0);
  118. if(ss == nil)
  119. continue;
  120. if(ss->dir && frand() < 1./bush) {
  121. if(trace) {
  122. int j;
  123. for(j=0; j<trace; j++)
  124. Bprint(bout, " ");
  125. Bprint(bout, "decend %d\n", i);
  126. }
  127. new(ss, trace?trace+1:0, depth+1);
  128. sourceFree(ss);
  129. return;
  130. }
  131. sourceFree(ss);
  132. }
  133. ss = sourceCreate(s, s->psize, s->dsize, 1+frand()>.5, 0);
  134. if(ss == nil)
  135. fprint(2, "could not create directory: %R\n");
  136. if(trace) {
  137. int j;
  138. for(j=1; j<trace; j++)
  139. Bprint(bout, " ");
  140. Bprint(bout, "create %d %V\n", ss->entry, ss->lump->score);
  141. }
  142. sourceFree(ss);
  143. }
  144. int
  145. delete(Source *s)
  146. {
  147. int i, n;
  148. Source *ss;
  149. assert(s->dir);
  150. n = sourceGetDirSize(s);
  151. /* check if empty */
  152. for(i=0; i<n; i++) {
  153. ss = sourceOpen(s, i, 1);
  154. if(ss != nil) {
  155. sourceFree(ss);
  156. break;
  157. }
  158. }
  159. if(i == n)
  160. return 0;
  161. for(;;) {
  162. ss = sourceOpen(s, nrand(n), 0);
  163. if(ss == nil)
  164. continue;
  165. if(ss->dir && delete(ss)) {
  166. sourceFree(ss);
  167. return 1;
  168. }
  169. if(1)
  170. break;
  171. sourceFree(ss);
  172. }
  173. sourceRemove(ss);
  174. return 1;
  175. }
  176. void
  177. dumpone(Source *s)
  178. {
  179. ulong i, n;
  180. Source *ss;
  181. Bprint(bout, "gen %4lud depth %d %V", s->gen, s->depth, s->lump->score);
  182. if(!s->dir) {
  183. Bprint(bout, " data size: %llud\n", s->size);
  184. return;
  185. }
  186. n = sourceGetDirSize(s);
  187. Bprint(bout, " dir size: %lud\n", n);
  188. for(i=0; i<n; i++) {
  189. ss = sourceOpen(s, i, 1);
  190. if(ss == nil) {
  191. fprint(2, "%lud: %R\n", i);
  192. continue;
  193. }
  194. Bprint(bout, "\t%lud %d %llud %V\n", i, ss->dir, ss->size, ss->lump->score);
  195. sourceFree(ss);
  196. }
  197. return;
  198. }
  199. void
  200. dump(Source *s, int ident, ulong entry)
  201. {
  202. ulong i, n;
  203. Source *ss;
  204. for(i=0; i<ident; i++)
  205. Bprint(bout, " ");
  206. Bprint(bout, "%4lud: gen %4lud depth %d", entry, s->gen, s->depth);
  207. if(!s->dir) {
  208. Bprint(bout, " data size: %llud\n", s->size);
  209. return;
  210. }
  211. n = sourceGetDirSize(s);
  212. Bprint(bout, " dir size: %lud\n", n);
  213. for(i=0; i<n; i++) {
  214. ss = sourceOpen(s, i, 1);
  215. if(ss == nil)
  216. continue;
  217. dump(ss, ident+1, i);
  218. sourceFree(ss);
  219. }
  220. return;
  221. }
  222. int
  223. count(Source *s, int rec)
  224. {
  225. ulong i, n;
  226. int c;
  227. Source *ss;
  228. if(!s->dir)
  229. return 0;
  230. n = sourceGetDirSize(s);
  231. c = 0;
  232. for(i=0; i<n; i++) {
  233. ss = sourceOpen(s, i, 1);
  234. if(ss == nil)
  235. continue;
  236. if(rec)
  237. c += count(ss, rec);
  238. c++;
  239. sourceFree(ss);
  240. }
  241. return c;
  242. }
  243. void
  244. stats(Source *s)
  245. {
  246. int n, i, c, cc, max;
  247. Source *ss;
  248. cc = 0;
  249. max = 0;
  250. n = sourceGetDirSize(s);
  251. for(i=0; i<n; i++) {
  252. ss = sourceOpen(s, i, 1);
  253. if(ss == nil)
  254. continue;
  255. cc++;
  256. c = count(ss, 1);
  257. if(c > max)
  258. max = c;
  259. sourceFree(ss);
  260. }
  261. fprint(2, "count = %d top = %d depth=%d maxcount %d\n", cc, n, maxdepth, max);
  262. }