12 typedef struct Allocs Allocs;
17 u32int stfree; /* free memory at start */
23 int mainstacksize = 256*1024;
26 static void ventiserver(void*);
32 uvlong size, userpgs = 0, userused = 0;
38 bp = Bopen("#c/swap", OREAD);
40 while ((ln = Brdline(bp, '\n')) != nil) {
41 ln[Blinelen(bp)-1] = '\0';
42 nf = tokenize(ln, fields, nelem(fields));
45 if (strcmp(fields[1], "pagesize") == 0)
46 pgsize = atoi(fields[0]);
47 else if (strcmp(fields[1], "user") == 0) {
48 sl = strchr(fields[0], '/');
51 userpgs = atoll(sl+1);
52 userused = atoll(fields[0]);
56 if (pgsize > 0 && userpgs > 0 && userused > 0)
57 size = (userpgs - userused) * pgsize;
59 /* cap it to keep the size within 32 bits */
60 if (size >= 3840UL * 1024 * 1024)
61 size = 3840UL * 1024 * 1024;
66 allocminima(Allocs *all) /* enforce minima for sanity */
68 if (all->icmem < 6 * 1024 * 1024)
69 all->icmem = 6 * 1024 * 1024;
70 if (all->mem < 1024 * 1024 || all->mem == Unspecified) /* lumps */
71 all->mem = 1024 * 1024;
72 if (all->bcmem < 2 * 1024 * 1024)
73 all->bcmem = 2 * 1024 * 1024;
76 /* automatic memory allocations sizing per venti(8) guidelines */
78 allocbypcnt(u32int mempcnt, u32int stfree)
85 all.mem = Unspecified;
86 all.bcmem = all.icmem = 0;
87 all.mempcnt = mempcnt;
92 blmsize = stfree - free;
95 avail = ((vlong)stfree * mempcnt) / 100;
96 if (blmsize >= avail || (avail -= blmsize) <= (1 + 2 + 6) * 1024 * 1024)
97 fprint(2, "%s: bloom filter bigger than mem pcnt; "
98 "resorting to minimum values (9MB total)\n", argv0);
100 if (avail >= 3840UL * 1024 * 1024)
101 avail = 3840UL * 1024 * 1024; /* sanity */
106 all.bcmem = 2 * avail;
112 * we compute default values for allocations,
113 * which can be overridden by (in order):
114 * configuration file parameters,
115 * command-line options other than -m, and -m.
118 sizeallocs(Allocs opt, Config *cfg)
122 /* work out sane defaults */
123 all = allocbypcnt(20, opt.stfree);
125 /* config file parameters override */
126 if (cfg->mem && cfg->mem != Unspecified)
129 all.bcmem = cfg->bcmem;
131 all.icmem = cfg->icmem;
133 /* command-line options override */
134 if (opt.mem && opt.mem != Unspecified)
137 all.bcmem = opt.bcmem;
139 all.icmem = opt.icmem;
141 /* automatic memory sizing? */
143 all = allocbypcnt(opt.mempcnt, opt.stfree);
152 fprint(2, "usage: venti [-Ldrsw] [-a ventiaddr] [-c config] "
153 "[-h httpaddr] [-m %%mem] [-B blockcachesize] [-C cachesize] [-I icachesize] "
155 threadexitsall("usage");
159 threadmain(int argc, char *argv[])
161 char *configfile, *haddr, *vaddr, *webroot;
162 u32int mem, icmem, bcmem, minbcmem, mempcnt, stfree;
167 threadsetname("main");
178 vaddr = EARGF(usage());
181 bcmem = unittoull(EARGF(usage()));
184 configfile = EARGF(usage());
187 mem = unittoull(EARGF(usage()));
190 settrace(EARGF(usage()));
197 haddr = EARGF(usage());
200 mempcnt = atoi(EARGF(usage()));
201 if (mempcnt <= 0 || mempcnt >= 100)
205 icmem = unittoull(EARGF(usage()));
216 case 'w': /* compatibility with old venti */
220 webroot = EARGF(usage());
234 /* sigh - needed to avoid signals when writing to hungup networks */
236 memset(&sa, 0, sizeof sa);
237 sa.sa_handler = SIG_IGN;
238 sigaction(SIGPIPE, &sa, nil);
243 trace(TraceQuiet, "venti started");
244 fprint(2, "%T venti: ");
246 if(configfile == nil)
247 configfile = "venti.conf";
249 /* remember free memory before initventi & loadbloom, for auto-sizing */
251 fprint(2, "conf...");
252 if(initventi(configfile, &config) < 0)
253 sysfatal("can't init server: %r");
257 if(mainindex->bloom && loadbloom(mainindex->bloom) < 0)
258 sysfatal("can't load bloom filter: %r");
261 * size memory allocations; assumes bloom filter is loaded
263 allocs = sizeallocs((Allocs){mem, bcmem, icmem, stfree, mempcnt},
266 bcmem = allocs.bcmem;
267 icmem = allocs.icmem;
268 fprint(2, "%s: mem %,ud bcmem %,ud icmem %,ud...",
269 argv0, mem, bcmem, icmem);
272 * default other configuration-file parameters
275 haddr = config.haddr;
277 vaddr = config.vaddr;
279 vaddr = "tcp!*!venti";
281 webroot = config.webroot;
283 queuewrites = config.queuewrites;
286 fprint(2, "httpd %s...", haddr);
287 if(httpdinit(haddr, webroot) < 0)
288 fprint(2, "warning: can't start http server: %r");
290 fprint(2, "init...");
295 if(0) fprint(2, "initialize %d bytes of lump cache for %d lumps\n",
296 mem, mem / (8 * 1024));
297 initlumpcache(mem, mem / (8 * 1024));
306 * block cache: need a block for every arena and every process
308 minbcmem = maxblocksize *
309 (mainindex->narenas + mainindex->nsects*4 + 16);
312 if(0) fprint(2, "initialize %d bytes of disk block cache\n", bcmem);
316 startbloomproc(mainindex->bloom);
318 fprint(2, "sync...");
319 if(!readonly && syncindex(mainindex) < 0)
320 sysfatal("can't sync server: %r");
322 if(!readonly && queuewrites){
323 fprint(2, "queue...");
324 if(initlumpqueues(mainindex->nsects) < 0){
325 fprint(2, "can't initialize lump queues,"
326 " disabling write queueing: %r");
331 if(initarenasum() < 0)
332 fprint(2, "warning: can't initialize arena summing process: %r");
334 fprint(2, "announce %s...", vaddr);
335 ventisrv = vtlisten(vaddr);
337 sysfatal("can't announce %s: %r", vaddr);
339 fprint(2, "serving.\n");
343 vtproc(ventiserver, nil);
349 vtrerror(VtReq *r, char *error)
351 r->rx.msgtype = VtRerror;
352 r->rx.error = estrdup(error);
365 threadsetname("ventiserver");
366 trace(TraceWork, "start");
367 while((r = vtgetreq(ventisrv)) != nil){
368 trace(TraceWork, "finish");
369 trace(TraceWork, "start request %F", &r->tx);
370 trace(TraceRpc, "<- %F", &r->tx);
371 r->rx.msgtype = r->tx.msgtype+1;
372 addstat(StatRpcTotal, 1);
373 if(0) print("req (arenas[0]=%p sects[0]=%p) %F\n",
374 mainindex->arenas[0], mainindex->sects[0], &r->tx);
375 switch(r->tx.msgtype){
377 vtrerror(r, "unknown request");
381 r->rx.data = readlump(r->tx.score, r->tx.blocktype, r->tx.count, &cached);
383 addstat2(StatRpcRead, 1, StatRpcReadTime, ms);
384 if(r->rx.data == nil){
385 addstat(StatRpcReadFail, 1);
386 rerrstr(err, sizeof err);
389 addstat(StatRpcReadBytes, packetsize(r->rx.data));
390 addstat(StatRpcReadOk, 1);
392 addstat2(StatRpcReadCached, 1, StatRpcReadCachedTime, ms);
394 addstat2(StatRpcReadUncached, 1, StatRpcReadUncachedTime, ms);
399 vtrerror(r, "read only");
404 addstat(StatRpcWriteBytes, packetsize(p));
406 ok = writelump(p, r->rx.score, r->tx.blocktype, 0, ms);
408 addstat2(StatRpcWrite, 1, StatRpcWriteTime, ms);
411 addstat(StatRpcWriteFail, 1);
412 rerrstr(err, sizeof err);
421 trace(TraceRpc, "-> %F", &r->rx);
423 trace(TraceWork, "start");