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("/dev/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 fprint(2, "%s: failed to open /dev/swap\n", argv0);
60 /* cap it to keep the size within 32 bits */
61 /* FIXME: we use signed 32-bit integers in some places for some fucking reason.
62 * Limiting accordingly for now.
63 * if (size >= 3840UL * 1024 * 1024)
64 * size = 3840UL * 1024 * 1024;
66 if (size >= 2047UL * 1024 * 1024) {
67 size = 2047UL * 1024 * 1024;
68 fprint(2, "%s: mem pct overflows: restricting to 2047MiB\n", argv0);
74 allocminima(Allocs *all) /* enforce minima for sanity */
76 if (all->icmem < 6 * 1024 * 1024)
77 all->icmem = 6 * 1024 * 1024;
78 if (all->mem < 1024 * 1024 || all->mem == Unspecified) /* lumps */
79 all->mem = 1024 * 1024;
80 if (all->bcmem < 2 * 1024 * 1024)
81 all->bcmem = 2 * 1024 * 1024;
84 /* automatic memory allocations sizing per venti(8) guidelines */
86 allocbypcnt(u32int mempcnt, u32int stfree)
93 all.mem = Unspecified;
94 all.bcmem = all.icmem = 0;
95 all.mempcnt = mempcnt;
100 blmsize = stfree - free;
103 avail = ((vlong)stfree * mempcnt) / 100;
104 if (blmsize >= avail || (avail -= blmsize) <= (1 + 2 + 6) * 1024 * 1024)
105 fprint(2, "%s: bloom filter bigger than mem pcnt; "
106 "resorting to minimum values (9MB total)\n", argv0);
108 if (avail >= 2047UL * 1024 * 1024){
109 avail = 2047UL * 1024 * 1024; /* sanity */
110 fprint(2, "%s: mem pct overflows: restricting to 2047MiB\n", argv0);
116 all.bcmem = 2 * avail;
122 * we compute default values for allocations,
123 * which can be overridden by (in order):
124 * configuration file parameters,
125 * command-line options other than -m, and -m.
128 sizeallocs(Allocs opt, Config *cfg)
132 /* work out sane defaults */
133 all = allocbypcnt(20, opt.stfree);
135 /* config file parameters override */
136 if (cfg->mem && cfg->mem != Unspecified)
139 all.bcmem = cfg->bcmem;
141 all.icmem = cfg->icmem;
143 /* command-line options override */
144 if (opt.mem && opt.mem != Unspecified)
147 all.bcmem = opt.bcmem;
149 all.icmem = opt.icmem;
151 /* automatic memory sizing? */
153 all = allocbypcnt(opt.mempcnt, opt.stfree);
162 fprint(2, "usage: venti [-Ldrsw] [-a ventiaddr] [-c config] "
163 "[-h httpaddr] [-m %%mem] [-B blockcachesize] [-C cachesize] [-I icachesize] "
165 threadexitsall("usage");
169 threadmain(int argc, char *argv[])
171 char *configfile, *haddr, *vaddr, *webroot;
172 u32int mem, icmem, bcmem, minbcmem, mempcnt, stfree;
177 threadsetname("main");
188 vaddr = EARGF(usage());
191 bcmem = unittoull(EARGF(usage()));
194 configfile = EARGF(usage());
197 mem = unittoull(EARGF(usage()));
200 settrace(EARGF(usage()));
207 haddr = EARGF(usage());
210 mempcnt = atoi(EARGF(usage()));
211 if (mempcnt <= 0 || mempcnt >= 100)
215 icmem = unittoull(EARGF(usage()));
226 case 'w': /* compatibility with old venti */
230 webroot = EARGF(usage());
244 /* sigh - needed to avoid signals when writing to hungup networks */
246 memset(&sa, 0, sizeof sa);
247 sa.sa_handler = SIG_IGN;
248 sigaction(SIGPIPE, &sa, nil);
253 trace(TraceQuiet, "venti started");
254 fprint(2, "%T venti: ");
256 if(configfile == nil)
257 configfile = "venti.conf";
259 /* remember free memory before initventi & loadbloom, for auto-sizing */
261 fprint(2, "conf...");
262 if(initventi(configfile, &config) < 0)
263 sysfatal("can't init server: %r");
267 if(mainindex->bloom && loadbloom(mainindex->bloom) < 0)
268 sysfatal("can't load bloom filter: %r");
271 * size memory allocations; assumes bloom filter is loaded
273 allocs = sizeallocs((Allocs){mem, bcmem, icmem, stfree, mempcnt},
276 bcmem = allocs.bcmem;
277 icmem = allocs.icmem;
278 fprint(2, "%s: mem %,ud bcmem %,ud icmem %,ud...",
279 argv0, mem, bcmem, icmem);
282 * default other configuration-file parameters
285 haddr = config.haddr;
287 vaddr = config.vaddr;
289 vaddr = "tcp!*!venti";
291 webroot = config.webroot;
293 queuewrites = config.queuewrites;
296 fprint(2, "httpd %s...", haddr);
297 if(httpdinit(haddr, webroot) < 0)
298 fprint(2, "warning: can't start http server: %r");
300 fprint(2, "init...");
305 if(0) fprint(2, "initialize %d bytes of lump cache for %d lumps\n",
306 mem, mem / (8 * 1024));
307 initlumpcache(mem, mem / (8 * 1024));
316 * block cache: need a block for every arena and every process
318 minbcmem = maxblocksize *
319 (mainindex->narenas + mainindex->nsects*4 + 16);
322 if(0) fprint(2, "initialize %d bytes of disk block cache\n", bcmem);
326 startbloomproc(mainindex->bloom);
328 fprint(2, "sync...");
329 if(!readonly && syncindex(mainindex) < 0)
330 sysfatal("can't sync server: %r");
332 if(!readonly && queuewrites){
333 fprint(2, "queue...");
334 if(initlumpqueues(mainindex->nsects) < 0){
335 fprint(2, "can't initialize lump queues,"
336 " disabling write queueing: %r");
341 if(initarenasum() < 0)
342 fprint(2, "warning: can't initialize arena summing process: %r");
344 fprint(2, "announce %s...", vaddr);
345 ventisrv = vtlisten(vaddr);
347 sysfatal("can't announce %s: %r", vaddr);
349 fprint(2, "serving.\n");
353 vtproc(ventiserver, nil);
359 vtrerror(VtReq *r, char *error)
361 r->rx.msgtype = VtRerror;
362 r->rx.error = estrdup(error);
375 threadsetname("ventiserver");
376 trace(TraceWork, "start");
377 while((r = vtgetreq(ventisrv)) != nil){
378 trace(TraceWork, "finish");
379 trace(TraceWork, "start request %F", &r->tx);
380 trace(TraceRpc, "<- %F", &r->tx);
381 r->rx.msgtype = r->tx.msgtype+1;
382 addstat(StatRpcTotal, 1);
383 if(0) print("req (arenas[0]=%p sects[0]=%p) %F\n",
384 mainindex->arenas[0], mainindex->sects[0], &r->tx);
385 switch(r->tx.msgtype){
387 vtrerror(r, "unknown request");
391 r->rx.data = readlump(r->tx.score, r->tx.blocktype, r->tx.count, &cached);
393 addstat2(StatRpcRead, 1, StatRpcReadTime, ms);
394 if(r->rx.data == nil){
395 addstat(StatRpcReadFail, 1);
396 rerrstr(err, sizeof err);
399 addstat(StatRpcReadBytes, packetsize(r->rx.data));
400 addstat(StatRpcReadOk, 1);
402 addstat2(StatRpcReadCached, 1, StatRpcReadCachedTime, ms);
404 addstat2(StatRpcReadUncached, 1, StatRpcReadUncachedTime, ms);
409 vtrerror(r, "read only");
414 addstat(StatRpcWriteBytes, packetsize(p));
416 ok = writelump(p, r->rx.score, r->tx.blocktype, 0, ms);
418 addstat2(StatRpcWrite, 1, StatRpcWriteTime, ms);
421 addstat(StatRpcWriteFail, 1);
422 rerrstr(err, sizeof err);
431 trace(TraceRpc, "-> %F", &r->rx);
433 trace(TraceWork, "start");