1 a0d146ed 2005-07-12 devnull #ifdef PLAN9PORT
2 a0d146ed 2005-07-12 devnull #include <u.h>
3 a0d146ed 2005-07-12 devnull #include <signal.h>
5 a0d146ed 2005-07-12 devnull #include "stdinc.h"
6 a0d146ed 2005-07-12 devnull #include "dat.h"
7 a0d146ed 2005-07-12 devnull #include "fns.h"
9 a0d146ed 2005-07-12 devnull #include "whack.h"
11 a0d146ed 2005-07-12 devnull int debug;
12 a0d146ed 2005-07-12 devnull int nofork;
13 a0d146ed 2005-07-12 devnull int mainstacksize = 256*1024;
14 a0d146ed 2005-07-12 devnull VtSrv *ventisrv;
16 a0d146ed 2005-07-12 devnull static void ventiserver(void*);
19 a0d146ed 2005-07-12 devnull usage(void)
21 ac1747b0 2005-07-18 devnull fprint(2, "usage: venti [-dsw] [-a ventiaddress] [-h httpaddress] [-c config] [-C cachesize] [-I icachesize] [-B blockcachesize]\n");
22 a0d146ed 2005-07-12 devnull threadexitsall("usage");
25 a0d146ed 2005-07-12 devnull threadmain(int argc, char *argv[])
27 a0d146ed 2005-07-12 devnull char *configfile, *haddr, *vaddr, *webroot;
28 a0d146ed 2005-07-12 devnull u32int mem, icmem, bcmem, minbcmem;
29 a0d146ed 2005-07-12 devnull Config config;
31 a0d146ed 2005-07-12 devnull traceinit();
32 a0d146ed 2005-07-12 devnull threadsetname("main");
33 a0d146ed 2005-07-12 devnull vaddr = nil;
34 a0d146ed 2005-07-12 devnull haddr = nil;
35 a0d146ed 2005-07-12 devnull configfile = nil;
36 a0d146ed 2005-07-12 devnull webroot = nil;
38 a0d146ed 2005-07-12 devnull icmem = 0;
39 a0d146ed 2005-07-12 devnull bcmem = 0;
40 a0d146ed 2005-07-12 devnull ARGBEGIN{
41 a0d146ed 2005-07-12 devnull case 'a':
42 a0d146ed 2005-07-12 devnull vaddr = EARGF(usage());
44 a0d146ed 2005-07-12 devnull case 'B':
45 a0d146ed 2005-07-12 devnull bcmem = unittoull(EARGF(usage()));
47 a0d146ed 2005-07-12 devnull case 'c':
48 a0d146ed 2005-07-12 devnull configfile = EARGF(usage());
50 a0d146ed 2005-07-12 devnull case 'C':
51 a0d146ed 2005-07-12 devnull mem = unittoull(EARGF(usage()));
53 a0d146ed 2005-07-12 devnull case 'D':
54 a0d146ed 2005-07-12 devnull settrace(EARGF(usage()));
56 a0d146ed 2005-07-12 devnull case 'd':
57 a0d146ed 2005-07-12 devnull debug = 1;
58 a0d146ed 2005-07-12 devnull nofork = 1;
60 a0d146ed 2005-07-12 devnull case 'h':
61 a0d146ed 2005-07-12 devnull haddr = EARGF(usage());
63 a0d146ed 2005-07-12 devnull case 'I':
64 a0d146ed 2005-07-12 devnull icmem = unittoull(EARGF(usage()));
66 a0d146ed 2005-07-12 devnull case 'L':
67 a0d146ed 2005-07-12 devnull ventilogging = 1;
69 a0d146ed 2005-07-12 devnull case 's':
70 a0d146ed 2005-07-12 devnull nofork = 1;
72 a0d146ed 2005-07-12 devnull case 'W':
73 a0d146ed 2005-07-12 devnull webroot = EARGF(usage());
82 a0d146ed 2005-07-12 devnull if(!nofork)
83 a0d146ed 2005-07-12 devnull rfork(RFNOTEG);
85 a0d146ed 2005-07-12 devnull #ifdef PLAN9PORT
87 a0d146ed 2005-07-12 devnull /* sigh - needed to avoid signals when writing to hungup networks */
88 a0d146ed 2005-07-12 devnull struct sigaction sa;
89 a0d146ed 2005-07-12 devnull memset(&sa, 0, sizeof sa);
90 a0d146ed 2005-07-12 devnull sa.sa_handler = SIG_IGN;
91 a0d146ed 2005-07-12 devnull sigaction(SIGPIPE, &sa, nil);
95 a0d146ed 2005-07-12 devnull trace(TraceQuiet, "venti started");
96 a0d146ed 2005-07-12 devnull fprint(2, "venti: ");
98 a0d146ed 2005-07-12 devnull ventifmtinstall();
99 a0d146ed 2005-07-12 devnull if(configfile == nil)
100 a0d146ed 2005-07-12 devnull configfile = "venti.conf";
102 a0d146ed 2005-07-12 devnull if(initarenasum() < 0)
103 a0d146ed 2005-07-12 devnull fprint(2, "warning: can't initialize arena summing process: %r");
105 a0d146ed 2005-07-12 devnull fprint(2, "conf...");
106 a0d146ed 2005-07-12 devnull if(initventi(configfile, &config) < 0)
107 a0d146ed 2005-07-12 devnull sysfatal("can't init server: %r");
109 a0d146ed 2005-07-12 devnull if(mem == 0)
110 a0d146ed 2005-07-12 devnull mem = config.mem;
111 a0d146ed 2005-07-12 devnull if(bcmem == 0)
112 a0d146ed 2005-07-12 devnull bcmem = config.bcmem;
113 a0d146ed 2005-07-12 devnull if(icmem == 0)
114 a0d146ed 2005-07-12 devnull icmem = config.icmem;
115 a0d146ed 2005-07-12 devnull if(haddr == nil)
116 a0d146ed 2005-07-12 devnull haddr = config.haddr;
117 a0d146ed 2005-07-12 devnull if(vaddr == nil)
118 a0d146ed 2005-07-12 devnull vaddr = config.vaddr;
119 a0d146ed 2005-07-12 devnull if(vaddr == nil)
120 a0d146ed 2005-07-12 devnull vaddr = "tcp!*!venti";
121 a0d146ed 2005-07-12 devnull if(webroot == nil)
122 a0d146ed 2005-07-12 devnull webroot = config.webroot;
123 a0d146ed 2005-07-12 devnull if(queuewrites == 0)
124 a0d146ed 2005-07-12 devnull queuewrites = config.queuewrites;
126 a0d146ed 2005-07-12 devnull if(haddr){
127 a0d146ed 2005-07-12 devnull fprint(2, "httpd %s...", haddr);
128 a0d146ed 2005-07-12 devnull if(httpdinit(haddr, webroot) < 0)
129 a0d146ed 2005-07-12 devnull fprint(2, "warning: can't start http server: %r");
132 a0d146ed 2005-07-12 devnull fprint(2, "init...");
134 a0d146ed 2005-07-12 devnull if(mem == 0xffffffffUL)
135 a0d146ed 2005-07-12 devnull mem = 1 * 1024 * 1024;
136 a0d146ed 2005-07-12 devnull if(0) fprint(2, "initialize %d bytes of lump cache for %d lumps\n",
137 a0d146ed 2005-07-12 devnull mem, mem / (8 * 1024));
138 a0d146ed 2005-07-12 devnull initlumpcache(mem, mem / (8 * 1024));
140 a0d146ed 2005-07-12 devnull icmem = u64log2(icmem / (sizeof(IEntry)+sizeof(IEntry*)) / ICacheDepth);
141 a0d146ed 2005-07-12 devnull if(icmem < 4)
142 a0d146ed 2005-07-12 devnull icmem = 4;
143 a0d146ed 2005-07-12 devnull if(0) fprint(2, "initialize %d bytes of index cache for %d index entries\n",
144 a0d146ed 2005-07-12 devnull (sizeof(IEntry)+sizeof(IEntry*)) * (1 << icmem) * ICacheDepth,
145 a0d146ed 2005-07-12 devnull (1 << icmem) * ICacheDepth);
146 a0d146ed 2005-07-12 devnull initicache(icmem, ICacheDepth);
147 a0d146ed 2005-07-12 devnull initicachewrite();
150 a0d146ed 2005-07-12 devnull * need a block for every arena and every process
152 a0d146ed 2005-07-12 devnull minbcmem = maxblocksize *
153 a0d146ed 2005-07-12 devnull (mainindex->narenas + mainindex->nsects*4 + 16);
154 a0d146ed 2005-07-12 devnull if(bcmem < minbcmem)
155 a0d146ed 2005-07-12 devnull bcmem = minbcmem;
157 a0d146ed 2005-07-12 devnull if(0) fprint(2, "initialize %d bytes of disk block cache\n", bcmem);
158 a0d146ed 2005-07-12 devnull initdcache(bcmem);
160 a0d146ed 2005-07-12 devnull if(mainindex->bloom)
161 a0d146ed 2005-07-12 devnull startbloomproc(mainindex->bloom);
163 a0d146ed 2005-07-12 devnull fprint(2, "sync...");
164 a0d146ed 2005-07-12 devnull if(syncindex(mainindex, 1, 0, 0) < 0)
165 a0d146ed 2005-07-12 devnull sysfatal("can't sync server: %r");
167 a0d146ed 2005-07-12 devnull if(queuewrites){
168 a0d146ed 2005-07-12 devnull fprint(2, "queue...");
169 a0d146ed 2005-07-12 devnull if(initlumpqueues(mainindex->nsects) < 0){
170 a0d146ed 2005-07-12 devnull fprint(2, "can't initialize lump queues,"
171 a0d146ed 2005-07-12 devnull " disabling write queueing: %r");
172 a0d146ed 2005-07-12 devnull queuewrites = 0;
176 a0d146ed 2005-07-12 devnull fprint(2, "announce %s...", vaddr);
177 a0d146ed 2005-07-12 devnull ventisrv = vtlisten(vaddr);
178 a0d146ed 2005-07-12 devnull if(ventisrv == nil)
179 a0d146ed 2005-07-12 devnull sysfatal("can't announce %s: %r", vaddr);
181 a0d146ed 2005-07-12 devnull fprint(2, "serving.\n");
182 a0d146ed 2005-07-12 devnull if(nofork)
183 a0d146ed 2005-07-12 devnull ventiserver(nil);
185 a0d146ed 2005-07-12 devnull vtproc(ventiserver, nil);
188 a0d146ed 2005-07-12 devnull static void
189 a0d146ed 2005-07-12 devnull vtrerror(VtReq *r, char *error)
191 a0d146ed 2005-07-12 devnull r->rx.msgtype = VtRerror;
192 a0d146ed 2005-07-12 devnull r->rx.error = estrdup(error);
195 a0d146ed 2005-07-12 devnull static void
196 a0d146ed 2005-07-12 devnull ventiserver(void *v)
198 a0d146ed 2005-07-12 devnull Packet *p;
199 a0d146ed 2005-07-12 devnull VtReq *r;
200 a0d146ed 2005-07-12 devnull char err[ERRMAX];
201 a0d146ed 2005-07-12 devnull uint ms;
202 a0d146ed 2005-07-12 devnull int cached, ok;
204 a0d146ed 2005-07-12 devnull USED(v);
205 a0d146ed 2005-07-12 devnull threadsetname("ventiserver");
206 a0d146ed 2005-07-12 devnull trace(TraceWork, "start");
207 a0d146ed 2005-07-12 devnull while((r = vtgetreq(ventisrv)) != nil){
208 a0d146ed 2005-07-12 devnull trace(TraceWork, "finish");
209 a0d146ed 2005-07-12 devnull trace(TraceWork, "start request %F", &r->tx);
210 a0d146ed 2005-07-12 devnull trace(TraceRpc, "<- %F", &r->tx);
211 a0d146ed 2005-07-12 devnull r->rx.msgtype = r->tx.msgtype+1;
212 a0d146ed 2005-07-12 devnull addstat(StatRpcTotal, 1);
213 a0d146ed 2005-07-12 devnull // print("req (arenas[0]=%p sects[0]=%p) %F\n",
214 a0d146ed 2005-07-12 devnull // mainindex->arenas[0], mainindex->sects[0], &r->tx);
215 a0d146ed 2005-07-12 devnull switch(r->tx.msgtype){
216 a0d146ed 2005-07-12 devnull default:
217 a0d146ed 2005-07-12 devnull vtrerror(r, "unknown request");
219 a0d146ed 2005-07-12 devnull case VtTread:
220 a0d146ed 2005-07-12 devnull ms = msec();
221 a0d146ed 2005-07-12 devnull r->rx.data = readlump(r->tx.score, r->tx.blocktype, r->tx.count, &cached);
222 a0d146ed 2005-07-12 devnull ms = msec() - ms;
223 a0d146ed 2005-07-12 devnull addstat2(StatRpcRead, 1, StatRpcReadTime, ms);
224 a0d146ed 2005-07-12 devnull if(r->rx.data == nil){
225 a0d146ed 2005-07-12 devnull addstat(StatRpcReadFail, 1);
226 a0d146ed 2005-07-12 devnull rerrstr(err, sizeof err);
227 a0d146ed 2005-07-12 devnull vtrerror(r, err);
229 a0d146ed 2005-07-12 devnull addstat(StatRpcReadBytes, packetsize(r->rx.data));
230 a0d146ed 2005-07-12 devnull addstat(StatRpcReadOk, 1);
231 a0d146ed 2005-07-12 devnull if(cached)
232 a0d146ed 2005-07-12 devnull addstat2(StatRpcReadCached, 1, StatRpcReadCachedTime, ms);
234 a0d146ed 2005-07-12 devnull addstat2(StatRpcReadUncached, 1, StatRpcReadUncachedTime, ms);
237 a0d146ed 2005-07-12 devnull case VtTwrite:
238 a0d146ed 2005-07-12 devnull p = r->tx.data;
239 a0d146ed 2005-07-12 devnull r->tx.data = nil;
240 a0d146ed 2005-07-12 devnull addstat(StatRpcWriteBytes, packetsize(p));
241 a0d146ed 2005-07-12 devnull ms = msec();
242 a0d146ed 2005-07-12 devnull ok = writelump(p, r->rx.score, r->tx.blocktype, 0, ms);
243 a0d146ed 2005-07-12 devnull ms = msec() - ms;
244 a0d146ed 2005-07-12 devnull addstat2(StatRpcWrite, 1, StatRpcWriteTime, ms);
246 a0d146ed 2005-07-12 devnull if(ok < 0){
247 a0d146ed 2005-07-12 devnull addstat(StatRpcWriteFail, 1);
248 a0d146ed 2005-07-12 devnull rerrstr(err, sizeof err);
249 a0d146ed 2005-07-12 devnull vtrerror(r, err);
252 a0d146ed 2005-07-12 devnull case VtTsync:
253 a0d146ed 2005-07-12 devnull flushqueue();
254 a0d146ed 2005-07-12 devnull flushdcache();
257 a0d146ed 2005-07-12 devnull trace(TraceRpc, "-> %F", &r->rx);
258 a0d146ed 2005-07-12 devnull vtrespond(r);
259 a0d146ed 2005-07-12 devnull trace(TraceWork, "start");
261 a0d146ed 2005-07-12 devnull flushdcache();
262 a0d146ed 2005-07-12 devnull flushicache();
263 a0d146ed 2005-07-12 devnull threadexitsall(0);