venti.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. #include "stdinc.h"
  2. #include "dat.h"
  3. #include "fns.h"
  4. int debug;
  5. static Packet *srvRead(VtSession *z, uchar score[VtScoreSize], int type, int n);
  6. static int srvWrite(VtSession *z, uchar score[VtScoreSize], int type, Packet *p);
  7. static void srvSync(VtSession *z);
  8. static void srvClosing(VtSession *z, int clean);
  9. static void daemon(char *vaddr);
  10. static VtServerVtbl serverVtbl = {
  11. .read srvRead,
  12. .write srvWrite,
  13. .sync srvSync,
  14. .closing srvClosing,
  15. };
  16. void
  17. main(int argc, char *argv[])
  18. {
  19. char *config, *haddr, *vaddr;
  20. u32int mem, icmem, bcmem;
  21. vaddr = "tcp!*!venti";
  22. haddr = nil;
  23. config = nil;
  24. mem = 0xffffffffUL;
  25. icmem = 0;
  26. bcmem = 0;
  27. ARGBEGIN{
  28. case 'a':
  29. vaddr = ARGF();
  30. if(vaddr == nil)
  31. goto usage;
  32. break;
  33. case 'B':
  34. bcmem = unittoull(ARGF());
  35. break;
  36. case 'c':
  37. config = ARGF();
  38. if(config == nil)
  39. goto usage;
  40. break;
  41. case 'C':
  42. mem = unittoull(ARGF());
  43. break;
  44. case 'd':
  45. debug = 1;
  46. break;
  47. case 'h':
  48. haddr = ARGF();
  49. if(haddr == nil)
  50. goto usage;
  51. break;
  52. case 'I':
  53. icmem = unittoull(ARGF());
  54. break;
  55. case 'w':
  56. queueWrites = 1;
  57. break;
  58. default:
  59. goto usage;
  60. }ARGEND
  61. if(argc){
  62. usage:
  63. fprint(2, "usage: venti [-dw] [-a ventiaddress] [-h httpaddress] [-c config] [-C cachesize] [-I icachesize] [-B blockcachesize]\n");
  64. exits("usage");
  65. }
  66. if(config == nil)
  67. config = "venti.conf";
  68. vtAttach();
  69. if(!initArenaSum())
  70. fprint(2, "warning: can't initialize arena summing process: %R");
  71. if(!initVenti(config))
  72. fatal("can't init server: %R");
  73. if(mem == 0xffffffffUL)
  74. mem = 1 * 1024 * 1024;
  75. fprint(2, "initialize %d bytes of lump cache for %d lumps\n", mem, mem / (8 * 1024));
  76. initLumpCache(mem, mem / (8 * 1024));
  77. icmem = u64log2(icmem / (sizeof(IEntry) + sizeof(IEntry*)) / ICacheDepth);
  78. if(icmem < 4)
  79. icmem = 4;
  80. fprint(2, "initialize %d bytes of index cache for %d index entries\n",
  81. (sizeof(IEntry) + sizeof(IEntry*)) * (1 << icmem) * ICacheDepth, (1 << icmem) * ICacheDepth);
  82. initICache(icmem, ICacheDepth);
  83. /*
  84. * need a block for every arena and every process
  85. */
  86. if(bcmem < maxBlockSize * (mainIndex->narenas + mainIndex->nsects * 4 + 16))
  87. bcmem = maxBlockSize * (mainIndex->narenas + mainIndex->nsects * 4 + 16);
  88. fprint(2, "initialize %d bytes of disk block cache\n", bcmem);
  89. initDCache(bcmem);
  90. fprint(2, "sync arenas and index...\n");
  91. if(!syncIndex(mainIndex, 1))
  92. fatal("can't sync server: %R");
  93. if(queueWrites){
  94. fprint(2, "initialize write queue...\n");
  95. if(!initLumpQueues(mainIndex->nsects)){
  96. fprint(2, "can't initialize lump queues, disabling write queueing: %R");
  97. queueWrites = 0;
  98. }
  99. }
  100. if(haddr){
  101. fprint(2, "starting http server at %s\n", haddr);
  102. if(httpdInit(haddr) < 0)
  103. fprint(2, "warning: can't start http server: %R");
  104. }
  105. fprint(2, "starting server\n");
  106. daemon(vaddr);
  107. vtDetach();
  108. exits(0);
  109. }
  110. static void
  111. daemon(char *vaddr)
  112. {
  113. int cfd, lcfd, fd;
  114. char adir[100], ldir[100];
  115. VtSession *z;
  116. cfd = announce(vaddr, adir);
  117. if(cfd < 0)
  118. fatal("can't announce: %s", vtOSError());
  119. for(;;){
  120. /* listen for a call */
  121. lcfd = listen(adir, ldir);
  122. if(lcfd < 0)
  123. fatal("listen: %s", vtOSError());
  124. fprint(2, "new call on %s\n", ldir);
  125. checkLumpCache();
  126. checkDCache();
  127. //printStats();
  128. fd = accept(lcfd, ldir);
  129. close(lcfd);
  130. z = vtServerAlloc(&serverVtbl);
  131. vtSetDebug(z, debug);
  132. vtSetFd(z, fd);
  133. vtExport(z);
  134. }
  135. }
  136. static Packet *
  137. srvRead(VtSession *z, uchar score[VtScoreSize], int type, int n)
  138. {
  139. Packet *p;
  140. USED(z);
  141. p = readLump(score, type, n);
  142. return p;
  143. }
  144. static int
  145. srvWrite(VtSession *z, uchar score[VtScoreSize], int type, Packet *p)
  146. {
  147. int ok;
  148. USED(z);
  149. ok = writeLump(p, score, type, 0);
  150. return ok;
  151. }
  152. static void
  153. srvSync(VtSession *z)
  154. {
  155. USED(z);
  156. if(queueWrites)
  157. queueFlush();
  158. }
  159. static void
  160. srvClosing(VtSession *z, int clean)
  161. {
  162. long rt, t[4];
  163. USED(z);
  164. if(!clean)
  165. fprint(2, "not a clean exit: %s\n", vtGetError());
  166. rt = times(t);
  167. fprint(2, "times: %.2fu %.2fs %.2fr\n", t[0]*.001, t[1]*.001, rt*.001);
  168. packetStats();
  169. }