ref: c466d3b923d12274cef1ff088c77a0a7491a8fd6
parent: eae30ea614b8988d2859946ea36076f948cbfca6
author: Ori Bernstein <ori@eigenstate.org>
date: Thu Jan 6 22:22:55 EST 2022
cons: add 'df' command
--- a/blk.c
+++ b/blk.c
@@ -178,6 +178,20 @@
}
int
+syncarena(Arena *a)
+{
+ char *p;
+
+ p = a->b->data;
+ PBIT64(p, a->log.head.addr); p += 8; /* freelist addr */
+ PBIT64(p, a->log.head.hash); p += 8; /* freelist hash */
+ PBIT64(p, a->size); p += 8; /* arena size */
+ PBIT64(p, a->used); /* arena used */
+ finalize(a->b);
+ return syncblk(a->b);
+}
+
+int
grabrange(Avltree *t, vlong off, vlong len)
{
Arange *r, *s, q;
@@ -470,7 +484,7 @@
Range *log, *nlog;
vlong v, bp, nb, graft, oldhd;
int i, n, sz;
- Blk *hd, *ab, *b;
+ Blk *hd, *b;
Oplog ol;
char *p;
@@ -555,11 +569,7 @@
a->log.head.addr = hd->bp.addr;
a->log.head.hash = blkhash(hd);
a->log.head.gen = -1;
- ab = a->b;
- PBIT64(ab->data + 0, a->log.head.addr);
- PBIT64(ab->data + 8, a->log.head.hash);
- finalize(ab);
- if(syncblk(ab) == -1)
+ if(syncarena(a) == -1)
return -1;
if(oldhd != -1){
for(bp = oldhd; bp != -1; bp = nb){
@@ -626,6 +636,7 @@
avldelete(t, r);
free(r);
}
+ a->used += Blksz;
return b;
}
@@ -641,6 +652,7 @@
goto out;
if(logop(a, b, LogFree) == -1)
goto out;
+ a->used -= Blksz;
r = 0;
out:
return r;
@@ -972,6 +984,8 @@
a = &fs->arenas[i];
finalize(a->log.tail);
if(syncblk(a->log.tail) == -1)
+ r = -1;
+ if(syncarena(a) == -1)
r = -1;
}
//
--- a/cons.c
+++ b/cons.c
@@ -139,9 +139,30 @@
fprint(fd, "\n");
}
runlock(&fs->userlk);
-}
+}
static void
+showdf(int fd, char**, int)
+{
+ vlong size, used;
+ double pct;
+ Arena *a;
+ int i;
+
+ size = 0;
+ used = 0;
+ for(i = 0; i < fs->narena; i++){
+ a = &fs->arenas[i];
+ lock(a);
+ size += a->size;
+ used += a->used;
+ unlock(a);
+ }
+ pct = 100.0*(double)used/(double)size;
+ fprint(fd, "used: %lld / %lld (%.2f%%)\n", used, size, pct);
+}
+
+static void
help(int fd, char**, int)
{
char *msg =
@@ -179,6 +200,7 @@
{.name="snap", .sub=nil, .minarg=2, .maxarg=2, .fn=snapfs},
{.name="check", .sub=nil, .minarg=1, .maxarg=1, .fn=fsckfs},
{.name="help", .sub=nil, .minarg=0, .maxarg=0, .fn=help},
+ {.name="df", .sub=nil, .minarg=0, .maxarg=0, .fn=showdf},
{.name="users", .sub=nil, .minarg=0, .maxarg=1, .fn=refreshusers},
/* debugging */
--- a/dat.h
+++ b/dat.h
@@ -442,12 +442,13 @@
struct Arena {
Lock;
Avltree *free;
- Avltree *partial;
Blk **queue;
int nqueue;
Blk *b; /* arena block */
Blk **q; /* write queue */
vlong nq;
+ vlong size;
+ vlong used;
Oplog log;
};
--- a/load.c
+++ b/load.c
@@ -16,15 +16,19 @@
loadarena(Arena *a, vlong o)
{
Blk *b;
+ char *p;
if((a->free = avlcreate(rangecmp)) == nil)
return -1;
if((b = readblk(o, 0)) == nil)
return -1;
+ p = b->data;
a->b = b;
- a->log.head.addr = GBIT64(b->data+0);
- a->log.head.hash = GBIT64(b->data+8);
+ a->log.head.addr = GBIT64(p); p += 8;
+ a->log.head.hash = GBIT64(p); p += 8;
a->log.head.gen = -1;
+ a->size = GBIT64(p); p += 8;
+ a->used = GBIT64(p);
if(loadlog(a) == -1)
return -1;
if(compresslog(a) == -1)
--- a/ream.c
+++ b/ream.c
@@ -81,7 +81,7 @@
}
static void
-reamarena(Arena *a, vlong start, vlong asz)
+initarena(Arena *a, vlong start, vlong asz)
{
vlong addr, bo, bh;
char *p;
@@ -104,11 +104,11 @@
b->flag |= Bdirty;
p = b->data+Loghdsz;
- PBIT64(p+ 0, addr|LogFree); /* addr */
- PBIT64(p+ 8, asz); /* len */
- PBIT64(p+16, b->bp.addr|LogAlloc); /* addr */
- PBIT64(p+24, Blksz); /* len */
- PBIT64(p+32, (uvlong)LogEnd); /* done */
+ PBIT64(p, addr|LogFree); p += 8; /* addr */
+ PBIT64(p, asz); p += 8; /* len */
+ PBIT64(p, b->bp.addr|LogAlloc); p += 8; /* addr */
+ PBIT64(p, Blksz); p += 8; /* len */
+ PBIT64(p, (uvlong)LogEnd); /* done */
finalize(b);
if(syncblk(b) == -1)
sysfatal("ream: init log");
@@ -120,8 +120,10 @@
b->type = Tarena;
b->bp.addr = start;
p = b->buf + Hdrsz;
- PBIT64(p+0, bo);
- PBIT64(p+8, bh);
+ PBIT64(p, bo); p += 8; /* freelist addr */
+ PBIT64(p, bh); p += 8; /* freelist hash */
+ PBIT64(p, asz); p += 8; /* arena size */
+ PBIT64(p, Blksz); /* arena used */
finalize(b);
if(syncblk(b) == -1)
sysfatal("ream: write arena: %r");
@@ -153,7 +155,7 @@
sz = d->length;
sz = sz - (sz % Blksz) - Blksz;
- fs->narena = d->length / (64*GiB);
+ fs->narena = (d->length + 64*GiB - 1) / (64*GiB);
if(fs->narena < 1)
fs->narena = 1;
if(fs->narena >= 128)
@@ -170,7 +172,7 @@
for(i = 0; i < fs->narena; i++){
print("\tarena %d: %lld blocks at %llx\n", i, asz/Blksz, off);
- reamarena(&fs->arenas[i], off, asz);
+ initarena(&fs->arenas[i], off, asz);
off += asz;
}