venti.c 4.5 KB

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