srcload.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include "stdinc.h"
  10. #include <bio.h>
  11. #include "dat.h"
  12. #include "fns.h"
  13. #include "error.h"
  14. int num = 100;
  15. int length = 20*1024;
  16. int block= 1024;
  17. int bush = 4;
  18. int iter = 100;
  19. Biobuf *bout;
  20. int maxdepth;
  21. Source *mkroot(Cache*);
  22. void new(Source*, int trace, int);
  23. int delete(Source*);
  24. int count(Source *s, int);
  25. void stats(Source *s);
  26. void dump(Source *s, int ident, uint32_t entry);
  27. static void bench(Source *r);
  28. void
  29. main(int argc, char *argv[])
  30. {
  31. int i;
  32. Fs *fs;
  33. int csize = 1000;
  34. uint32_t t;
  35. Source *r;
  36. ARGBEGIN{
  37. case 'i':
  38. iter = atoi(ARGF());
  39. break;
  40. case 'n':
  41. num = atoi(ARGF());
  42. break;
  43. case 'l':
  44. length = atoi(ARGF());
  45. break;
  46. case 'b':
  47. block = atoi(ARGF());
  48. break;
  49. case 'u':
  50. bush = atoi(ARGF());
  51. break;
  52. case 'c':
  53. csize = atoi(ARGF());
  54. break;
  55. }ARGEND;
  56. vtAttach();
  57. bout = vtMemAllocZ(sizeof(Biobuf));
  58. Binit(bout, 1, OWRITE);
  59. fmtinstall('V', vtScoreFmt);
  60. fmtinstall('R', vtErrFmt);
  61. fs = fsOpen(argv[0], nil, csize, OReadWrite);
  62. if(fs == nil)
  63. sysfatal("could not open fs: %r");
  64. t = time(0);
  65. srand(0);
  66. r = fs->source;
  67. dump(r, 0, 0);
  68. fprint(2, "count = %d\n", count(r, 1));
  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. // dump(r, 0, 0);
  78. fprint(2, "count = %d\n", count(r, 1));
  79. // cacheCheck(c);
  80. fprint(2, "deleting\n");
  81. for(i=0; i<num; i++)
  82. delete(r);
  83. // dump(r, 0, 0);
  84. fprint(2, "count = %d\n", count(r, 1));
  85. fprint(2, "total time = %ld\n", time(0)-t);
  86. fsClose(fs);
  87. vtDetach();
  88. exits(0);
  89. }
  90. static void
  91. bench(Source *r)
  92. {
  93. int64_t t;
  94. Entry e;
  95. int i;
  96. t = nsec();
  97. for(i=0; i<1000000; i++)
  98. sourceGetEntry(r, &e);
  99. fprint(2, "%f\n", 1e-9*(nsec() - t));
  100. }
  101. void
  102. new(Source *s, int trace, int depth)
  103. {
  104. int i, n;
  105. Source *ss;
  106. Entry e;
  107. if(depth > maxdepth)
  108. maxdepth = depth;
  109. Bflush(bout);
  110. n = sourceGetDirSize(s);
  111. for(i=0; i<n; i++){
  112. ss = sourceOpen(s, nrand(n), OReadWrite);
  113. if(ss == nil || !sourceGetEntry(ss, &e))
  114. continue;
  115. if((e.flags & VtEntryDir) && frand() < 1./bush){
  116. if(trace){
  117. int j;
  118. for(j=0; j<trace; j++)
  119. Bprint(bout, " ");
  120. Bprint(bout, "decend %d\n", i);
  121. }
  122. new(ss, trace?trace+1:0, depth+1);
  123. sourceClose(ss);
  124. return;
  125. }
  126. sourceClose(ss);
  127. }
  128. ss = sourceCreate(s, s->dsize, 1+frand()>.5, 0);
  129. if(ss == nil){
  130. Bprint(bout, "could not create directory: %R\n");
  131. return;
  132. }
  133. if(trace){
  134. int j;
  135. for(j=1; j<trace; j++)
  136. Bprint(bout, " ");
  137. Bprint(bout, "create %d\n", ss->offset);
  138. }
  139. sourceClose(ss);
  140. }
  141. int
  142. delete(Source *s)
  143. {
  144. int i, n;
  145. Source *ss;
  146. n = sourceGetDirSize(s);
  147. /* check if empty */
  148. for(i=0; i<n; i++){
  149. ss = sourceOpen(s, i, OReadWrite);
  150. if(ss != nil){
  151. sourceClose(ss);
  152. break;
  153. }
  154. }
  155. if(i == n)
  156. return 0;
  157. for(;;){
  158. ss = sourceOpen(s, nrand(n), OReadWrite);
  159. if(ss == nil)
  160. continue;
  161. if(s->dir && delete(ss)){
  162. sourceClose(ss);
  163. return 1;
  164. }
  165. if(1)
  166. break;
  167. sourceClose(ss);
  168. }
  169. sourceRemove(ss);
  170. return 1;
  171. }
  172. void
  173. dump(Source *s, int ident, uint32_t entry)
  174. {
  175. uint32_t i, n;
  176. Source *ss;
  177. Entry e;
  178. for(i=0; i<ident; i++)
  179. Bprint(bout, " ");
  180. if(!sourceGetEntry(s, &e)){
  181. fprint(2, "sourceGetEntry failed: %r\n");
  182. return;
  183. }
  184. Bprint(bout, "%4lud: gen %4ud depth %d tag=%x score=%V",
  185. entry, e.gen, e.depth, e.tag, e.score);
  186. if(!s->dir){
  187. Bprint(bout, " data size: %llu\n", e.size);
  188. return;
  189. }
  190. n = sourceGetDirSize(s);
  191. Bprint(bout, " dir size: %lu\n", n);
  192. for(i=0; i<n; i++){
  193. ss = sourceOpen(s, i, 1);
  194. if(ss == nil)
  195. continue;
  196. dump(ss, ident+1, i);
  197. sourceClose(ss);
  198. }
  199. return;
  200. }
  201. int
  202. count(Source *s, int rec)
  203. {
  204. uint32_t i, n;
  205. int c;
  206. Source *ss;
  207. n = sourceGetDirSize(s);
  208. c = 0;
  209. for(i=0; i<n; i++){
  210. ss = sourceOpen(s, i, OReadOnly);
  211. if(ss == nil)
  212. continue;
  213. if(rec)
  214. c += count(ss, rec);
  215. c++;
  216. sourceClose(ss);
  217. }
  218. return c;
  219. }
  220. void
  221. stats(Source *s)
  222. {
  223. int n, i, c, cc, max;
  224. Source *ss;
  225. cc = 0;
  226. max = 0;
  227. n = sourceGetDirSize(s);
  228. for(i=0; i<n; i++){
  229. ss = sourceOpen(s, i, 1);
  230. if(ss == nil)
  231. continue;
  232. cc++;
  233. c = count(ss, 1);
  234. if(c > max)
  235. max = c;
  236. sourceClose(ss);
  237. }
  238. fprint(2, "count = %d top = %d depth=%d maxcount %d\n", cc, n, maxdepth, max);
  239. }