]> git.lizzy.rs Git - plan9front.git/blob - sys/src/cmd/venti/srv/venti.c
venti: warn when opening /dev/swap fails
[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         }
61         /* cap it to keep the size within 32 bits */
62         if (size >= 3840UL * 1024 * 1024){
63                 size = 3840UL * 1024 * 1024;
64                 fprint(2, "%s: Reduced free memory detected to 3840MiB because we don't support 64-bit addresses yet.\n", argv0);
65         }
66         /* FIXME: we use signed 32-bit integers in some places for some fucking reason. 
67            Limiting accordingly for now.
68         */
69         if (size >= 2047UL * 1024 * 1024){
70                 size = 2047UL * 1024 * 1024;
71                 fprint(2, "%s: Reduced free memory detected to 2047MiB because we have bugz.\n", argv0);
72         }
73         return size;
74 }
75
76 static void
77 allocminima(Allocs *all)                        /* enforce minima for sanity */
78 {
79         if (all->icmem < 6 * 1024 * 1024)
80                 all->icmem = 6 * 1024 * 1024;
81         if (all->mem < 1024 * 1024 || all->mem == Unspecified)  /* lumps */
82                 all->mem = 1024 * 1024;
83         if (all->bcmem < 2 * 1024 * 1024)
84                 all->bcmem = 2 * 1024 * 1024;
85 }
86
87 /* automatic memory allocations sizing per venti(8) guidelines */
88 static Allocs
89 allocbypcnt(u32int mempcnt, u32int stfree)
90 {
91         u32int avail;
92         vlong blmsize;
93         Allocs all;
94         static u32int free;
95
96         all.mem = Unspecified;
97         all.bcmem = all.icmem = 0;
98         all.mempcnt = mempcnt;
99         all.stfree = stfree;
100
101         if (free == 0)
102                 free = freemem();
103         blmsize = stfree - free;
104         if (blmsize <= 0)
105                 blmsize = 0;
106         avail = ((vlong)stfree * mempcnt) / 100;
107         if (blmsize >= avail || (avail -= blmsize) <= (1 + 2 + 6) * 1024 * 1024)
108                 fprint(2, "%s: bloom filter bigger than mem pcnt; "
109                         "resorting to minimum values (9MB total)\n", argv0);
110         else {
111                 if (avail >= 2047UL * 1024 * 1024){
112                         avail = 2047UL * 1024 * 1024;   /* sanity */
113                         fprint(2, "%s: restricting memory usage to 2047MiB\n", argv0);
114                 }
115                 avail /= 2;
116                 all.icmem = avail;
117                 avail /= 3;
118                 all.mem = avail;
119                 all.bcmem = 2 * avail;
120         }
121         return all;
122 }
123
124 /*
125  * we compute default values for allocations,
126  * which can be overridden by (in order):
127  *      configuration file parameters,
128  *      command-line options other than -m, and -m.
129  */
130 static Allocs
131 sizeallocs(Allocs opt, Config *cfg)
132 {
133         Allocs all;
134
135         /* work out sane defaults */
136         all = allocbypcnt(20, opt.stfree);
137
138         /* config file parameters override */
139         if (cfg->mem && cfg->mem != Unspecified)
140                 all.mem = cfg->mem;
141         if (cfg->bcmem)
142                 all.bcmem = cfg->bcmem;
143         if (cfg->icmem)
144                 all.icmem = cfg->icmem;
145
146         /* command-line options override */
147         if (opt.mem && opt.mem != Unspecified)
148                 all.mem = opt.mem;
149         if (opt.bcmem)
150                 all.bcmem = opt.bcmem;
151         if (opt.icmem)
152                 all.icmem = opt.icmem;
153
154         /* automatic memory sizing? */
155         if(opt.mempcnt > 0)
156                 all = allocbypcnt(opt.mempcnt, opt.stfree);
157
158         allocminima(&all);
159         return all;
160 }
161
162 void
163 usage(void)
164 {
165         fprint(2, "usage: venti [-Ldrsw] [-a ventiaddr] [-c config] "
166 "[-h httpaddr] [-m %%mem] [-B blockcachesize] [-C cachesize] [-I icachesize] "
167 "[-W webroot]\n");
168         threadexitsall("usage");
169 }
170
171 void
172 threadmain(int argc, char *argv[])
173 {
174         char *configfile, *haddr, *vaddr, *webroot;
175         u32int mem, icmem, bcmem, minbcmem, mempcnt, stfree;
176         Allocs allocs;
177         Config config;
178
179         traceinit();
180         threadsetname("main");
181         mempcnt = 0;
182         vaddr = nil;
183         haddr = nil;
184         configfile = nil;
185         webroot = nil;
186         mem = Unspecified;
187         icmem = 0;
188         bcmem = 0;
189         ARGBEGIN{
190         case 'a':
191                 vaddr = EARGF(usage());
192                 break;
193         case 'B':
194                 bcmem = unittoull(EARGF(usage()));
195                 break;
196         case 'c':
197                 configfile = EARGF(usage());
198                 break;
199         case 'C':
200                 mem = unittoull(EARGF(usage()));
201                 break;
202         case 'D':
203                 settrace(EARGF(usage()));
204                 break;
205         case 'd':
206                 debug = 1;
207                 nofork = 1;
208                 break;
209         case 'h':
210                 haddr = EARGF(usage());
211                 break;
212         case 'm':
213                 mempcnt = atoi(EARGF(usage()));
214                 if (mempcnt <= 0 || mempcnt >= 100)
215                         usage();
216                 break;
217         case 'I':
218                 icmem = unittoull(EARGF(usage()));
219                 break;
220         case 'L':
221                 ventilogging = 1;
222                 break;
223         case 'r':
224                 readonly = 1;
225                 break;
226         case 's':
227                 nofork = 1;
228                 break;
229         case 'w':                       /* compatibility with old venti */
230                 queuewrites = 1;
231                 break;
232         case 'W':
233                 webroot = EARGF(usage());
234                 break;
235         default:
236                 usage();
237         }ARGEND
238
239         if(argc)
240                 usage();
241
242         if(!nofork)
243                 rfork(RFNOTEG);
244
245 #ifdef PLAN9PORT
246         {
247                 /* sigh - needed to avoid signals when writing to hungup networks */
248                 struct sigaction sa;
249                 memset(&sa, 0, sizeof sa);
250                 sa.sa_handler = SIG_IGN;
251                 sigaction(SIGPIPE, &sa, nil);
252         }
253 #endif
254
255         ventifmtinstall();
256         trace(TraceQuiet, "venti started");
257         fprint(2, "%T venti: ");
258
259         if(configfile == nil)
260                 configfile = "venti.conf";
261
262         /* remember free memory before initventi & loadbloom, for auto-sizing */
263         stfree = freemem();      
264         fprint(2, "conf...");
265         if(initventi(configfile, &config) < 0)
266                 sysfatal("can't init server: %r");
267         /*
268          * load bloom filter
269          */
270         if(mainindex->bloom && loadbloom(mainindex->bloom) < 0)
271                 sysfatal("can't load bloom filter: %r");
272
273         /*
274          * size memory allocations; assumes bloom filter is loaded
275          */
276         allocs = sizeallocs((Allocs){mem, bcmem, icmem, stfree, mempcnt},
277                 &config);
278         mem = allocs.mem;
279         bcmem = allocs.bcmem;
280         icmem = allocs.icmem;
281         fprint(2, "%s: mem %,ud bcmem %,ud icmem %,ud...",
282                 argv0, mem, bcmem, icmem);
283
284         /*
285          * default other configuration-file parameters
286          */
287         if(haddr == nil)
288                 haddr = config.haddr;
289         if(vaddr == nil)
290                 vaddr = config.vaddr;
291         if(vaddr == nil)
292                 vaddr = "tcp!*!venti";
293         if(webroot == nil)
294                 webroot = config.webroot;
295         if(queuewrites == 0)
296                 queuewrites = config.queuewrites;
297
298         if(haddr){
299                 fprint(2, "httpd %s...", haddr);
300                 if(httpdinit(haddr, webroot) < 0)
301                         fprint(2, "warning: can't start http server: %r");
302         }
303         fprint(2, "init...");
304
305         /*
306          * lump cache
307          */
308         if(0) fprint(2, "initialize %d bytes of lump cache for %d lumps\n",
309                 mem, mem / (8 * 1024));
310         initlumpcache(mem, mem / (8 * 1024));
311
312         /*
313          * index cache
314          */
315         initicache(icmem);
316         initicachewrite();
317
318         /*
319          * block cache: need a block for every arena and every process
320          */
321         minbcmem = maxblocksize * 
322                 (mainindex->narenas + mainindex->nsects*4 + 16);
323         if(bcmem < minbcmem)
324                 bcmem = minbcmem;
325         if(0) fprint(2, "initialize %d bytes of disk block cache\n", bcmem);
326         initdcache(bcmem);
327
328         if(mainindex->bloom)
329                 startbloomproc(mainindex->bloom);
330
331         fprint(2, "sync...");
332         if(!readonly && syncindex(mainindex) < 0)
333                 sysfatal("can't sync server: %r");
334
335         if(!readonly && queuewrites){
336                 fprint(2, "queue...");
337                 if(initlumpqueues(mainindex->nsects) < 0){
338                         fprint(2, "can't initialize lump queues,"
339                                 " disabling write queueing: %r");
340                         queuewrites = 0;
341                 }
342         }
343
344         if(initarenasum() < 0)
345                 fprint(2, "warning: can't initialize arena summing process: %r");
346
347         fprint(2, "announce %s...", vaddr);
348         ventisrv = vtlisten(vaddr);
349         if(ventisrv == nil)
350                 sysfatal("can't announce %s: %r", vaddr);
351
352         fprint(2, "serving.\n");
353         if(nofork)
354                 ventiserver(nil);
355         else
356                 vtproc(ventiserver, nil);
357
358         threadexits(nil);
359 }
360
361 static void
362 vtrerror(VtReq *r, char *error)
363 {
364         r->rx.msgtype = VtRerror;
365         r->rx.error = estrdup(error);
366 }
367
368 static void
369 ventiserver(void *v)
370 {
371         Packet *p;
372         VtReq *r;
373         char err[ERRMAX];
374         uint ms;
375         int cached, ok;
376
377         USED(v);
378         threadsetname("ventiserver");
379         trace(TraceWork, "start");
380         while((r = vtgetreq(ventisrv)) != nil){
381                 trace(TraceWork, "finish");
382                 trace(TraceWork, "start request %F", &r->tx);
383                 trace(TraceRpc, "<- %F", &r->tx);
384                 r->rx.msgtype = r->tx.msgtype+1;
385                 addstat(StatRpcTotal, 1);
386                 if(0) print("req (arenas[0]=%p sects[0]=%p) %F\n",
387                         mainindex->arenas[0], mainindex->sects[0], &r->tx);
388                 switch(r->tx.msgtype){
389                 default:
390                         vtrerror(r, "unknown request");
391                         break;
392                 case VtTread:
393                         ms = msec();
394                         r->rx.data = readlump(r->tx.score, r->tx.blocktype, r->tx.count, &cached);
395                         ms = msec() - ms;
396                         addstat2(StatRpcRead, 1, StatRpcReadTime, ms);
397                         if(r->rx.data == nil){
398                                 addstat(StatRpcReadFail, 1);
399                                 rerrstr(err, sizeof err);
400                                 vtrerror(r, err);
401                         }else{
402                                 addstat(StatRpcReadBytes, packetsize(r->rx.data));
403                                 addstat(StatRpcReadOk, 1);
404                                 if(cached)
405                                         addstat2(StatRpcReadCached, 1, StatRpcReadCachedTime, ms);
406                                 else
407                                         addstat2(StatRpcReadUncached, 1, StatRpcReadUncachedTime, ms);
408                         }
409                         break;
410                 case VtTwrite:
411                         if(readonly){
412                                 vtrerror(r, "read only");
413                                 break;
414                         }
415                         p = r->tx.data;
416                         r->tx.data = nil;
417                         addstat(StatRpcWriteBytes, packetsize(p));
418                         ms = msec();
419                         ok = writelump(p, r->rx.score, r->tx.blocktype, 0, ms);
420                         ms = msec() - ms;
421                         addstat2(StatRpcWrite, 1, StatRpcWriteTime, ms);
422
423                         if(ok < 0){
424                                 addstat(StatRpcWriteFail, 1);
425                                 rerrstr(err, sizeof err);
426                                 vtrerror(r, err);
427                         }
428                         break;
429                 case VtTsync:
430                         flushqueue();
431                         flushdcache();
432                         break;
433                 }
434                 trace(TraceRpc, "-> %F", &r->rx);
435                 vtrespond(r);
436                 trace(TraceWork, "start");
437         }
438         flushdcache();
439         flushicache();
440         threadexitsall(0);
441 }