venti: fix memory layers

This commit is contained in:
Noam Preil 2021-07-21 05:06:05 +00:00
parent 82c7251dc3
commit 2eadf1fa17
14 changed files with 73 additions and 131 deletions

View file

@ -207,10 +207,10 @@ int vtscorefmt(Fmt*);
* error-checking malloc et al. * error-checking malloc et al.
*/ */
void vtfree(void *); void vtfree(void *);
void* vtmalloc(int); void* vtmalloc(ulong);
void* vtmallocz(int); void* vtmallocz(ulong);
void* vtrealloc(void *p, int); void* vtrealloc(void *p, ulong);
void* vtbrk(int n); void* vtbrk(ulong);
char* vtstrdup(char *); char* vtstrdup(char *);
/* /*

View file

@ -375,7 +375,7 @@ parseamap(IFile *f, AMapN *amn)
for(i = 0; i < n; i++){ for(i = 0; i < n; i++){
s = ifileline(f); s = ifileline(f);
if(s) if(s)
t = estrdup(s); t = vtstrdup(s);
else else
t = nil; t = nil;
if(s == nil || getfields(s, flds, 4, 0, "\t") != 3){ if(s == nil || getfields(s, flds, 4, 0, "\t") != 3){

View file

@ -446,7 +446,7 @@ mkipool(ISect *isect, Minibuf *mbuf, u32int nmbuf,
IEntryLink *l; IEntryLink *l;
nentry = (nmbuf+1)*bufsize / IEntrySize; nentry = (nmbuf+1)*bufsize / IEntrySize;
p = ezmalloc(sizeof(IPool) p = vtmallocz(sizeof(IPool)
+nentry*sizeof(IEntry) +nentry*sizeof(IEntry)
+nmbuf*sizeof(IEntryLink*) +nmbuf*sizeof(IEntryLink*)
+nmbuf*sizeof(u32int) +nmbuf*sizeof(u32int)
@ -676,7 +676,7 @@ sortminibuffer(ISect *is, Minibuf *mb, uchar *buf, u32int nbuf, u32int bufsize)
Part *part; Part *part;
part = is->part; part = is->part;
buckdata = emalloc(is->blocksize); buckdata = vtmalloc(is->blocksize);
if(mb->nwentry == 0) if(mb->nwentry == 0)
return; return;
@ -691,7 +691,6 @@ sortminibuffer(ISect *is, Minibuf *mb, uchar *buf, u32int nbuf, u32int bufsize)
errors = 1; errors = 1;
return; return;
} }
assert(*(uint*)buf != 0xa5a5a5a5);
/* /*
* remove fragmentation due to IEntrySize * remove fragmentation due to IEntrySize
@ -820,7 +819,7 @@ isectproc(void *v)
bufsize = MinBufSize; bufsize = MinBufSize;
while(bufsize*2*nbuf <= isectmem && bufsize < MaxBufSize) while(bufsize*2*nbuf <= isectmem && bufsize < MaxBufSize)
bufsize *= 2; bufsize *= 2;
data = emalloc(nbuf*bufsize); data = vtmalloc(nbuf*bufsize);
epbuf = bufsize/IEntrySize; epbuf = bufsize/IEntrySize;
fprint(2, "%T %s: %,ud buckets, %,ud groups, %,ud minigroups, %,ud buffer\n", fprint(2, "%T %s: %,ud buckets, %,ud groups, %,ud minigroups, %,ud buffer\n",
is->part->name, nbucket, nbuf, nminibuf, bufsize); is->part->name, nbucket, nbuf, nminibuf, bufsize);
@ -951,7 +950,7 @@ isectproc(void *v)
if(space < mbuf[j].woffset - mbuf[j].boffset) if(space < mbuf[j].woffset - mbuf[j].boffset)
space = mbuf[j].woffset - mbuf[j].boffset; space = mbuf[j].woffset - mbuf[j].boffset;
data = emalloc(space); data = vtmalloc(space);
for(j=0; j<nminibuf; j++){ for(j=0; j<nminibuf; j++){
mb = &mbuf[j]; mb = &mbuf[j];
sortminibuffer(is, mb, data, space, bufsize); sortminibuffer(is, mb, data, space, bufsize);

View file

@ -84,7 +84,7 @@ runconfig(char *file, Config *config)
ok = 0; ok = 0;
break; break;
} }
line = estrdup(s); line = vtstrdup(s);
i = getfields(s, flds, MaxArgs + 1, 1, " \t\r"); i = getfields(s, flds, MaxArgs + 1, 1, " \t\r");
if(i == 2 && strcmp(flds[0], "isect") == 0){ if(i == 2 && strcmp(flds[0], "isect") == 0){
sv = MKN(ISect*, config->nsects + 1); sv = MKN(ISect*, config->nsects + 1);
@ -122,7 +122,7 @@ runconfig(char *file, Config *config)
seterr(EAdmin, "duplicate indices in config file %s", file); seterr(EAdmin, "duplicate indices in config file %s", file);
break; break;
} }
config->index = estrdup(flds[1]); config->index = vtstrdup(flds[1]);
}else if(i == 2 && strcmp(flds[0], "bcmem") == 0){ }else if(i == 2 && strcmp(flds[0], "bcmem") == 0){
if(numok(flds[1]) < 0){ if(numok(flds[1]) < 0){
seterr(EAdmin, "illegal size %s in config file %s", seterr(EAdmin, "illegal size %s in config file %s",
@ -163,19 +163,19 @@ runconfig(char *file, Config *config)
seterr(EAdmin, "duplicate httpaddr lines in configuration file %s", file); seterr(EAdmin, "duplicate httpaddr lines in configuration file %s", file);
break; break;
} }
config->haddr = estrdup(flds[1]); config->haddr = vtstrdup(flds[1]);
}else if(i == 2 && strcmp(flds[0], "webroot") == 0){ }else if(i == 2 && strcmp(flds[0], "webroot") == 0){
if(config->webroot){ if(config->webroot){
seterr(EAdmin, "duplicate webroot lines in configuration file %s", file); seterr(EAdmin, "duplicate webroot lines in configuration file %s", file);
break; break;
} }
config->webroot = estrdup(flds[1]); config->webroot = vtstrdup(flds[1]);
}else if(i == 2 && strcmp(flds[0], "addr") == 0){ }else if(i == 2 && strcmp(flds[0], "addr") == 0){
if(config->vaddr){ if(config->vaddr){
seterr(EAdmin, "duplicate addr lines in configuration file %s", file); seterr(EAdmin, "duplicate addr lines in configuration file %s", file);
break; break;
} }
config->vaddr = estrdup(flds[1]); config->vaddr = vtstrdup(flds[1]);
}else{ }else{
seterr(EAdmin, "illegal line '%s' in configuration file %s", line, file); seterr(EAdmin, "illegal line '%s' in configuration file %s", line, file);
break; break;

View file

@ -82,10 +82,11 @@ initdcache(u32int mem)
int i; int i;
u8int *p; u8int *p;
if(mem < maxblocksize * 2)
sysfatal("need at least %d bytes for the disk cache", maxblocksize * 2);
if(maxblocksize == 0) if(maxblocksize == 0)
sysfatal("no max. block size given for disk cache"); sysfatal("no max. block size given for disk cache");
if(mem < maxblocksize * 2)
sysfatal("need at least 2 max-size blocks (%d bytes) for the disk cache", maxblocksize * 2);
blocksize = maxblocksize; blocksize = maxblocksize;
nblocks = mem / blocksize; nblocks = mem / blocksize;
dcache.full.l = &dcache.lock; dcache.full.l = &dcache.lock;
@ -94,11 +95,11 @@ initdcache(u32int mem)
trace(TraceProc, "initialize disk cache with %d blocks of %d bytes, maximum %d dirty blocks\n", trace(TraceProc, "initialize disk cache with %d blocks of %d bytes, maximum %d dirty blocks\n",
nblocks, blocksize, dcache.maxdirty); nblocks, blocksize, dcache.maxdirty);
dcache.size = blocksize; dcache.size = blocksize;
dcache.heads = MKNZ(DBlock*, HashSize); dcache.heads = vtbrk(sizeof(DBlock*) * HashSize);
dcache.heap = MKNZ(DBlock*, nblocks); dcache.heap = vtbrk(sizeof(DBlock*) * nblocks);
dcache.blocks = MKNZ(DBlock, nblocks); dcache.blocks = vtbrk(sizeof(DBlock) * nblocks);
dcache.write = MKNZ(DBlock*, nblocks); dcache.write = vtbrk(sizeof(DBlock*) * nblocks);
dcache.mem = MKNZ(u8int, (nblocks+1+128) * blocksize); dcache.mem = vtbrk((nblocks+1+128) * blocksize);
last = nil; last = nil;
p = (u8int*)(((uintptr)dcache.mem+blocksize-1)&~(uintptr)(blocksize-1)); p = (u8int*)(((uintptr)dcache.mem+blocksize-1)&~(uintptr)(blocksize-1));

View file

@ -29,13 +29,9 @@ void delaykickroundproc(void*);
void dirtydblock(DBlock*, int); void dirtydblock(DBlock*, int);
void diskaccess(int); void diskaccess(int);
void disksched(void); void disksched(void);
void *emalloc(ulong);
void emptydcache(void); void emptydcache(void);
void emptyicache(void); void emptyicache(void);
void emptylumpcache(void); void emptylumpcache(void);
void *erealloc(void *, ulong);
char *estrdup(char*);
void *ezmalloc(ulong);
Arena *findarena(char *name); Arena *findarena(char *name);
int flushciblocks(Arena *arena); int flushciblocks(Arena *arena);
void flushdcache(void); void flushdcache(void);
@ -151,6 +147,8 @@ int readclumpinfos(Arena *arena, int clump, ClumpInfo *cis, int n);
ZBlock *readfile(char *name); ZBlock *readfile(char *name);
int readifile(IFile *f, char *name); int readifile(IFile *f, char *name);
Packet *readlump(u8int *score, int type, u32int size, int *cached); Packet *readlump(u8int *score, int type, u32int size, int *cached);
// If the return value is not negative one, n bytes were successfully read.
// If n == -1, this will ALWAYS report failure, even when the read succeeded.
int readpart(Part *part, u64int addr, u8int *buf, u32int n); int readpart(Part *part, u64int addr, u8int *buf, u32int n);
int resetbloom(Bloom*); int resetbloom(Bloom*);
int runconfig(char *config, Config*); int runconfig(char *config, Config*);
@ -221,8 +219,8 @@ void zeropart(Part *part, int blocksize);
#define scorecmp(h1,h2) memcmp((h1),(h2),VtScoreSize) #define scorecmp(h1,h2) memcmp((h1),(h2),VtScoreSize)
#define scorecp(h1,h2) memmove((h1),(h2),VtScoreSize) #define scorecp(h1,h2) memmove((h1),(h2),VtScoreSize)
#define MK(t) ((t*)emalloc(sizeof(t))) #define MK(t) ((t*)vtmalloc(sizeof(t)))
#define MKZ(t) ((t*)ezmalloc(sizeof(t))) #define MKZ(t) ((t*)vtmallocz(sizeof(t)))
#define MKN(t,n) ((t*)emalloc((n)*sizeof(t))) #define MKN(t,n) ((t*)vtmalloc((n)*sizeof(t)))
#define MKNZ(t,n) ((t*)ezmalloc((n)*sizeof(t))) #define MKNZ(t,n) ((t*)vtmallocz((n)*sizeof(t)))
#define MKNA(t,at,n) ((t*)emalloc(sizeof(t) + (n)*sizeof(at))) #define MKNA(t,at,n) ((t*)vtmalloc(sizeof(t) + (n)*sizeof(at)))

View file

@ -278,14 +278,12 @@ scachemiss(u64int addr)
*/ */
void void
initicache(u32int mem0) initicache(u32int mem)
{ {
u32int mem;
int i, entries, scache; int i, entries, scache;
icache.full.l = &icache.lock; icache.full.l = &icache.lock;
mem = mem0;
entries = mem / (sizeof(IEntry)+sizeof(IEntry*)); entries = mem / (sizeof(IEntry)+sizeof(IEntry*));
scache = (entries/8) / ArenaCIGSize; scache = (entries/8) / ArenaCIGSize;
entries -= entries/8; entries -= entries/8;
@ -295,7 +293,7 @@ initicache(u32int mem0)
scache = 16; scache = 16;
if(entries < 1000) if(entries < 1000)
entries = 1000; entries = 1000;
fprint(2, "icache %,d bytes = %,d entries; %d scache\n", mem0, entries, scache); fprint(2, "icache %,lud bytes = %,lud entries; %lud scache\n", mem, entries, scache);
icache.clean.prev = icache.clean.next = &icache.clean; icache.clean.prev = icache.clean.next = &icache.clean;
icache.dirty.prev = icache.dirty.next = &icache.dirty; icache.dirty.prev = icache.dirty.next = &icache.dirty;
@ -304,7 +302,7 @@ fprint(2, "icache %,d bytes = %,d entries; %d scache\n", mem0, entries, scache);
icache.hash = mkihash(entries); icache.hash = mkihash(entries);
icache.nentries = entries; icache.nentries = entries;
setstat(StatIcacheSize, entries); setstat(StatIcacheSize, entries);
icache.entries = vtmallocz(entries*sizeof icache.entries[0]); icache.entries = vtbrk(entries*sizeof icache.entries[0]);
icache.maxdirty = entries / 2; icache.maxdirty = entries / 2;
for(i=0; i<entries; i++) for(i=0; i<entries; i++)
pushfirst(&icache.free, &icache.entries[i]); pushfirst(&icache.free, &icache.entries[i]);

View file

@ -216,7 +216,7 @@ icachewriteproc(void *v)
threadsetname("icachewriteproc:%s", is->part->name); threadsetname("icachewriteproc:%s", is->part->name);
bsize = 1<<is->blocklog; bsize = 1<<is->blocklog;
buf = emalloc(Bufsize+bsize); buf = vtmalloc(Bufsize+bsize);
buf = (u8int*)(((uintptr)buf+bsize-1)&~(uintptr)(bsize-1)); buf = (u8int*)(((uintptr)buf+bsize-1)&~(uintptr)(bsize-1));
for(;;){ for(;;){

View file

@ -47,9 +47,9 @@ initlumpcache(u32int size, u32int nblocks)
lumpcache.nblocks = nblocks; lumpcache.nblocks = nblocks;
lumpcache.allowed = size; lumpcache.allowed = size;
lumpcache.avail = size; lumpcache.avail = size;
lumpcache.heads = MKNZ(Lump*, HashSize); lumpcache.heads = vtbrk(sizeof(Lump*) * HashSize);
lumpcache.heap = MKNZ(Lump*, nblocks); lumpcache.heap = vtbrk(sizeof(Lump*) * nblocks);
lumpcache.blocks = MKNZ(Lump, nblocks); lumpcache.blocks = vtbrk(sizeof(Lump) * nblocks);
setstat(StatLcacheSize, lumpcache.nblocks); setstat(StatLcacheSize, lumpcache.nblocks);
last = nil; last = nil;
@ -413,7 +413,7 @@ checklumpcache(void)
} }
if(lumpcache.avail != lumpcache.allowed - size){ if(lumpcache.avail != lumpcache.allowed - size){
fprint(2, "mismatched available=%d and allowed=%d - used=%d space", lumpcache.avail, lumpcache.allowed, size); fprint(2, "mismatched available=%d and allowed=%d - used=%d space", lumpcache.avail, lumpcache.allowed, size);
*(int*)0=0; abort();
} }
nfree = 0; nfree = 0;

View file

@ -47,7 +47,7 @@ parsepart(char *name, char **file, u64int *lo, u64int *hi)
{ {
char *p; char *p;
*file = estrdup(name); *file = vtstrdup(name);
if((p = strrchr(*file, ':')) == nil){ if((p = strrchr(*file, ':')) == nil){
*lo = 0; *lo = 0;
*hi = 0; *hi = 0;
@ -87,8 +87,8 @@ initpart(char *name, int mode)
return nil; return nil;
trace(TraceDisk, "initpart %s file %s lo 0x%llx hi 0x%llx", name, file, lo, hi); trace(TraceDisk, "initpart %s file %s lo 0x%llx hi 0x%llx", name, file, lo, hi);
part = MKZ(Part); part = MKZ(Part);
part->name = estrdup(name); part->name = vtstrdup(name);
part->filename = estrdup(file); part->filename = vtstrdup(file);
if(readonly){ if(readonly){
mode &= ~(OREAD|OWRITE|ORDWR); mode &= ~(OREAD|OWRITE|ORDWR);
mode |= OREAD; mode |= OREAD;

View file

@ -44,7 +44,7 @@ dumpisect(ISect *is)
IBucket ib; IBucket ib;
IEntry ie; IEntry ie;
buf = emalloc(is->blocksize); buf = vtmalloc(is->blocksize);
for(i=0; i<is->blocks; i++){ for(i=0; i<is->blocks; i++){
off = is->blockbase+(u64int)is->blocksize*i; off = is->blockbase+(u64int)is->blocksize*i;
if(readpart(is->part, off, buf, is->blocksize) < 0) if(readpart(is->part, off, buf, is->blocksize) < 0)

View file

@ -136,70 +136,6 @@ now(void)
return time(nil); return time(nil);
} }
int abortonmem = 1;
void *
emalloc(ulong n)
{
void *p;
p = malloc(n);
if(p == nil){
if(abortonmem)
abort();
sysfatal("out of memory allocating %lud", n);
}
memset(p, 0xa5, n);
setmalloctag(p, getcallerpc(&n));
if(0)print("emalloc %p-%p by %#p\n", p, (char*)p+n, getcallerpc(&n));
return p;
}
void *
ezmalloc(ulong n)
{
void *p;
p = malloc(n);
if(p == nil){
if(abortonmem)
abort();
sysfatal("out of memory allocating %lud", n);
}
memset(p, 0, n);
setmalloctag(p, getcallerpc(&n));
if(0)print("ezmalloc %p-%p by %#p\n", p, (char*)p+n, getcallerpc(&n));
return p;
}
void *
erealloc(void *p, ulong n)
{
p = realloc(p, n);
if(p == nil){
if(abortonmem)
abort();
sysfatal("out of memory allocating %lud", n);
}
setrealloctag(p, getcallerpc(&p));
if(0)print("erealloc %p-%p by %#p\n", p, (char*)p+n, getcallerpc(&p));
return p;
}
char *
estrdup(char *s)
{
char *t;
int n;
n = strlen(s) + 1;
t = emalloc(n);
memmove(t, s, n);
setmalloctag(t, getcallerpc(&s));
if(0)print("estrdup %p-%p by %#p\n", t, (char*)t+n, getcallerpc(&s));
return t;
}
/* /*
* return floor(log2(v)) * return floor(log2(v))
*/ */

View file

@ -359,7 +359,7 @@ static void
vtrerror(VtReq *r, char *error) vtrerror(VtReq *r, char *error)
{ {
r->rx.msgtype = VtRerror; r->rx.msgtype = VtRerror;
r->rx.error = estrdup(error); r->rx.error = vtstrdup(error);
} }
static void static void

View file

@ -7,55 +7,55 @@ enum {
ChunkSize = 128*1024 ChunkSize = 128*1024
}; };
void void
vtfree(void *p) vtfree(void *p)
{ {
if(p == 0)
return;
free(p); free(p);
} }
void * void *
vtmalloc(int size) vtmalloc(ulong size)
{ {
void *p; void *p = mallocz(size, 0);
if(p == nil){
p = malloc(size); fprint(2, "vtmalloc: out of memory allocating %lud", size);
if(p == 0) abort();
sysfatal("vtmalloc: out of memory"); }
setmalloctag(p, getcallerpc(&size)); setmalloctag(p, getcallerpc(&size));
return p; return p;
} }
void * void *
vtmallocz(int size) vtmallocz(ulong size)
{ {
void *p = vtmalloc(size); void *p = mallocz(size, 1);
memset(p, 0, size); if(p == nil){
fprint(2, "vtmallocz: out of memory allocating %lud", size);
abort();
}
setmalloctag(p, getcallerpc(&size)); setmalloctag(p, getcallerpc(&size));
return p; return p;
} }
void * void *
vtrealloc(void *p, int size) vtrealloc(void *p, ulong size)
{ {
if(p == nil)
return vtmalloc(size);
p = realloc(p, size); p = realloc(p, size);
if(p == 0) if(p == 0 && size != 0){
sysfatal("vtMemRealloc: out of memory"); fprint(2, "vtrealloc: out of memory allocating %lud", size);
abort();
}
setrealloctag(p, getcallerpc(&size)); setrealloctag(p, getcallerpc(&size));
return p; return p;
} }
void * void *
vtbrk(int n) vtbrk(ulong n)
{ {
static Lock lk; static Lock lk;
static uchar *buf; static uchar *buf;
static int nbuf, nchunk; static ulong nbuf, nchunk;
int align, pad; ulong align, pad;
void *p; void *p;
if(n >= IdealAlignment) if(n >= IdealAlignment)
@ -65,10 +65,20 @@ vtbrk(int n)
else else
align = 4; align = 4;
if(n > ChunkSize){
p = sbrk(n);
if(p == (void*)-1)
sysfatal("Failed to allocate permanent chunk size %lud", n);
memset(p, 0, n);
return (uchar*)p;
}
lock(&lk); lock(&lk);
pad = (align - (uintptr)buf) & (align-1); pad = (align - (uintptr)buf) & (align-1);
if(n + pad > nbuf) { if(n + pad > nbuf) {
buf = vtmallocz(ChunkSize); buf = sbrk(ChunkSize);
if(buf == (void*)-1)
sysfatal("Failed to allocate permanent chunk size %ud", ChunkSize);
memset(buf, 0, ChunkSize);
nbuf = ChunkSize; nbuf = ChunkSize;
pad = (align - (uintptr)buf) & (align-1); pad = (align - (uintptr)buf) & (align-1);
nchunk++; nchunk++;