]> git.lizzy.rs Git - plan9front.git/blob - sys/src/cmd/venti/srv/venti.c
87361a0e1afb6e6a73d9a70e201e3d4037ac574f
[plan9front.git] / sys / src / cmd / venti / srv / venti.c
1 #ifdef PLAN9PORT
2 #include <u.h>
3 #include <signal.h>
4 #endif
5 #include "stdinc.h"
6 #include <bio.h>
7 #include "dat.h"
8 #include "fns.h"
9
10 #include "whack.h"
11
12 typedef struct Allocs Allocs;
13 struct Allocs {
14         u32int  mem;
15         u32int  bcmem;
16         u32int  icmem;
17         u32int  stfree;                         /* free memory at start */
18         uint    mempcnt;
19 };
20
21 int debug;
22 int nofork;
23 int mainstacksize = 256*1024;
24 VtSrv *ventisrv;
25
26 static void     ventiserver(void*);
27
28 static ulong
29 freemem(void)
30 {
31         int nf, pgsize = 0;
32         uvlong size, userpgs = 0, userused = 0;
33         char *ln, *sl;
34         char *fields[2];
35         Biobuf *bp;
36
37         size = 64*1024*1024;
38         bp = Bopen("/dev/swap", OREAD);
39         if (bp != nil) {
40                 while ((ln = Brdline(bp, '\n')) != nil) {
41                         ln[Blinelen(bp)-1] = '\0';
42                         nf = tokenize(ln, fields, nelem(fields));
43                         if (nf != 2)
44                                 continue;
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], '/');
49                                 if (sl == nil)
50                                         continue;
51                                 userpgs = atoll(sl+1);
52                                 userused = atoll(fields[0]);
53                         }
54                 }
55                 Bterm(bp);
56                 if (pgsize > 0 && userpgs > 0 && userused > 0)
57                         size = (userpgs - userused) * pgsize;
58         } else
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;
65          */
66         if (size >= 2047UL * 1024 * 1024) {
67                 size = 2047UL * 1024 * 1024;
68                 fprint(2, "%s: mem pct overflows: restricting to 2047MiB\n", argv0);
69         }
70         return size;
71 }
72
73 static void
74 allocminima(Allocs *all)                        /* enforce minima for sanity */
75 {
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;
82 }
83
84 /* automatic memory allocations sizing per venti(8) guidelines */
85 static Allocs
86 allocbypcnt(u32int mempcnt, u32int stfree)
87 {
88         u32int avail;
89         vlong blmsize;
90         Allocs all;
91         static u32int free;
92
93         all.mem = Unspecified;
94         all.bcmem = all.icmem = 0;
95         all.mempcnt = mempcnt;
96         all.stfree = stfree;
97
98         if (free == 0)
99                 free = freemem();
100         blmsize = stfree - free;
101         if (blmsize <= 0)
102                 blmsize = 0;
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);
107         else {
108                 if (avail >= 2047UL * 1024 * 1024){
109                         avail = 2047UL * 1024 * 1024;   /* sanity */
110                         fprint(2, "%s: mem pct overflows: restricting to 2047MiB\n", argv0);
111                 }
112                 avail /= 2;
113                 all.icmem = avail;
114                 avail /= 3;
115                 all.mem = avail;
116                 all.bcmem = 2 * avail;
117         }
118         return all;
119 }
120
121 /*
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.
126  */
127 static Allocs
128 sizeallocs(Allocs opt, Config *cfg)
129 {
130         Allocs all;
131
132         /* work out sane defaults */
133         all = allocbypcnt(20, opt.stfree);
134
135         /* config file parameters override */
136         if (cfg->mem && cfg->mem != Unspecified)
137                 all.mem = cfg->mem;
138         if (cfg->bcmem)
139                 all.bcmem = cfg->bcmem;
140         if (cfg->icmem)
141                 all.icmem = cfg->icmem;
142
143         /* command-line options override */
144         if (opt.mem && opt.mem != Unspecified)
145                 all.mem = opt.mem;
146         if (opt.bcmem)
147                 all.bcmem = opt.bcmem;
148         if (opt.icmem)
149                 all.icmem = opt.icmem;
150
151         /* automatic memory sizing? */
152         if(opt.mempcnt > 0)
153                 all = allocbypcnt(opt.mempcnt, opt.stfree);
154
155         allocminima(&all);
156         return all;
157 }
158
159 void
160 usage(void)
161 {
162         fprint(2, "usage: venti [-Ldrsw] [-a ventiaddr] [-c config] "
163 "[-h httpaddr] [-m %%mem] [-B blockcachesize] [-C cachesize] [-I icachesize] "
164 "[-W webroot]\n");
165         threadexitsall("usage");
166 }
167
168 void
169 threadmain(int argc, char *argv[])
170 {
171         char *configfile, *haddr, *vaddr, *webroot;
172         u32int mem, icmem, bcmem, minbcmem, mempcnt, stfree;
173         Allocs allocs;
174         Config config;
175
176         traceinit();
177         threadsetname("main");
178         mempcnt = 0;
179         vaddr = nil;
180         haddr = nil;
181         configfile = nil;
182         webroot = nil;
183         mem = Unspecified;
184         icmem = 0;
185         bcmem = 0;
186         ARGBEGIN{
187         case 'a':
188                 vaddr = EARGF(usage());
189                 break;
190         case 'B':
191                 bcmem = unittoull(EARGF(usage()));
192                 break;
193         case 'c':
194                 configfile = EARGF(usage());
195                 break;
196         case 'C':
197                 mem = unittoull(EARGF(usage()));
198                 break;
199         case 'D':
200                 settrace(EARGF(usage()));
201                 break;
202         case 'd':
203                 debug = 1;
204                 nofork = 1;
205                 break;
206         case 'h':
207                 haddr = EARGF(usage());
208                 break;
209         case 'm':
210                 mempcnt = atoi(EARGF(usage()));
211                 if (mempcnt <= 0 || mempcnt >= 100)
212                         usage();
213                 break;
214         case 'I':
215                 icmem = unittoull(EARGF(usage()));
216                 break;
217         case 'L':
218                 ventilogging = 1;
219                 break;
220         case 'r':
221                 readonly = 1;
222                 break;
223         case 's':
224                 nofork = 1;
225                 break;
226         case 'w':                       /* compatibility with old venti */
227                 queuewrites = 1;
228                 break;
229         case 'W':
230                 webroot = EARGF(usage());
231                 break;
232         default:
233                 usage();
234         }ARGEND
235
236         if(argc)
237                 usage();
238
239         if(!nofork)
240                 rfork(RFNOTEG);
241
242 #ifdef PLAN9PORT
243         {
244                 /* sigh - needed to avoid signals when writing to hungup networks */
245                 struct sigaction sa;
246                 memset(&sa, 0, sizeof sa);
247                 sa.sa_handler = SIG_IGN;
248                 sigaction(SIGPIPE, &sa, nil);
249         }
250 #endif
251
252         ventifmtinstall();
253         trace(TraceQuiet, "venti started");
254         fprint(2, "%T venti: ");
255
256         if(configfile == nil)
257                 configfile = "venti.conf";
258
259         /* remember free memory before initventi & loadbloom, for auto-sizing */
260         stfree = freemem();      
261         fprint(2, "conf...");
262         if(initventi(configfile, &config) < 0)
263                 sysfatal("can't init server: %r");
264         /*
265          * load bloom filter
266          */
267         if(mainindex->bloom && loadbloom(mainindex->bloom) < 0)
268                 sysfatal("can't load bloom filter: %r");
269
270         /*
271          * size memory allocations; assumes bloom filter is loaded
272          */
273         allocs = sizeallocs((Allocs){mem, bcmem, icmem, stfree, mempcnt},
274                 &config);
275         mem = allocs.mem;
276         bcmem = allocs.bcmem;
277         icmem = allocs.icmem;
278         fprint(2, "%s: mem %,ud bcmem %,ud icmem %,ud...",
279                 argv0, mem, bcmem, icmem);
280
281         /*
282          * default other configuration-file parameters
283          */
284         if(haddr == nil)
285                 haddr = config.haddr;
286         if(vaddr == nil)
287                 vaddr = config.vaddr;
288         if(vaddr == nil)
289                 vaddr = "tcp!*!venti";
290         if(webroot == nil)
291                 webroot = config.webroot;
292         if(queuewrites == 0)
293                 queuewrites = config.queuewrites;
294
295         if(haddr){
296                 fprint(2, "httpd %s...", haddr);
297                 if(httpdinit(haddr, webroot) < 0)
298                         fprint(2, "warning: can't start http server: %r");
299         }
300         fprint(2, "init...");
301
302         /*
303          * lump cache
304          */
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));
308
309         /*
310          * index cache
311          */
312         initicache(icmem);
313         initicachewrite();
314
315         /*
316          * block cache: need a block for every arena and every process
317          */
318         minbcmem = maxblocksize * 
319                 (mainindex->narenas + mainindex->nsects*4 + 16);
320         if(bcmem < minbcmem)
321                 bcmem = minbcmem;
322         if(0) fprint(2, "initialize %d bytes of disk block cache\n", bcmem);
323         initdcache(bcmem);
324
325         if(mainindex->bloom)
326                 startbloomproc(mainindex->bloom);
327
328         fprint(2, "sync...");
329         if(!readonly && syncindex(mainindex) < 0)
330                 sysfatal("can't sync server: %r");
331
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");
337                         queuewrites = 0;
338                 }
339         }
340
341         if(initarenasum() < 0)
342                 fprint(2, "warning: can't initialize arena summing process: %r");
343
344         fprint(2, "announce %s...", vaddr);
345         ventisrv = vtlisten(vaddr);
346         if(ventisrv == nil)
347                 sysfatal("can't announce %s: %r", vaddr);
348
349         fprint(2, "serving.\n");
350         if(nofork)
351                 ventiserver(nil);
352         else
353                 vtproc(ventiserver, nil);
354
355         threadexits(nil);
356 }
357
358 static void
359 vtrerror(VtReq *r, char *error)
360 {
361         r->rx.msgtype = VtRerror;
362         r->rx.error = estrdup(error);
363 }
364
365 static void
366 ventiserver(void *v)
367 {
368         Packet *p;
369         VtReq *r;
370         char err[ERRMAX];
371         uint ms;
372         int cached, ok;
373
374         USED(v);
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){
386                 default:
387                         vtrerror(r, "unknown request");
388                         break;
389                 case VtTread:
390                         ms = msec();
391                         r->rx.data = readlump(r->tx.score, r->tx.blocktype, r->tx.count, &cached);
392                         ms = msec() - ms;
393                         addstat2(StatRpcRead, 1, StatRpcReadTime, ms);
394                         if(r->rx.data == nil){
395                                 addstat(StatRpcReadFail, 1);
396                                 rerrstr(err, sizeof err);
397                                 vtrerror(r, err);
398                         }else{
399                                 addstat(StatRpcReadBytes, packetsize(r->rx.data));
400                                 addstat(StatRpcReadOk, 1);
401                                 if(cached)
402                                         addstat2(StatRpcReadCached, 1, StatRpcReadCachedTime, ms);
403                                 else
404                                         addstat2(StatRpcReadUncached, 1, StatRpcReadUncachedTime, ms);
405                         }
406                         break;
407                 case VtTwrite:
408                         if(readonly){
409                                 vtrerror(r, "read only");
410                                 break;
411                         }
412                         p = r->tx.data;
413                         r->tx.data = nil;
414                         addstat(StatRpcWriteBytes, packetsize(p));
415                         ms = msec();
416                         ok = writelump(p, r->rx.score, r->tx.blocktype, 0, ms);
417                         ms = msec() - ms;
418                         addstat2(StatRpcWrite, 1, StatRpcWriteTime, ms);
419
420                         if(ok < 0){
421                                 addstat(StatRpcWriteFail, 1);
422                                 rerrstr(err, sizeof err);
423                                 vtrerror(r, err);
424                         }
425                         break;
426                 case VtTsync:
427                         flushqueue();
428                         flushdcache();
429                         break;
430                 }
431                 trace(TraceRpc, "-> %F", &r->rx);
432                 vtrespond(r);
433                 trace(TraceWork, "start");
434         }
435         flushdcache();
436         flushicache();
437         threadexitsall(0);
438 }