shithub: gefs

Download patch

ref: e1720e7817812dea1bb6aea1db30ebb3b58befb6
parent: 383da8d9753c8e215932940215e58b9cc5a0c933
author: Ori Bernstein <ori@eigenstate.org>
date: Tue Dec 5 00:10:28 EST 2023

fs: probe available memory for default cache size

--- a/main.c
+++ b/main.c
@@ -2,6 +2,7 @@
 #include <libc.h>
 #include <avl.h>
 #include <fcall.h>
+#include <bio.h>
 
 #include "dat.h"
 #include "fns.h"
@@ -26,6 +27,30 @@
 Blk	*blkbuf;
 void	**errctx;
 
+static uvlong
+memsize(void)
+{
+	char *ln, *f[2];
+	vlong mem;
+	Biobuf *bp;
+
+	mem = 512*MiB;
+	if((bp = Bopen("/dev/swap", OREAD)) == nil)
+		return mem;
+	while((ln = Brdstr(bp, '\n', 1)) != nil){
+		if(tokenize(ln, f, nelem(f)) != 2)
+			continue;
+		if(strcmp(f[1], "memory") == 0){
+			mem = strtoll(f[0], 0, 0);
+			free(ln);
+			break;
+		}
+		free(ln);
+	}
+	Bterm(bp);
+	return mem;
+}
+
 jmp_buf*
 _waserror(void)
 {
@@ -204,11 +229,14 @@
 main(int argc, char **argv)
 {
 	int i, srvfd, ctlfd, nann, check;
-	char *s, *ann[16];
+	char *s, *e, *ann[16];
+	vlong v, memsz;
 	Conn *c;
 
 	nann = 0;
 	check = 0;
+	memsz = memsize();
+	cachesz = 25*memsz/100;
 	ARGBEGIN{
 	case 'a':
 		if(nann == nelem(ann))
@@ -223,7 +251,20 @@
 		grow = 1;
 		break;
 	case 'm':
-		cachesz = strtoll(EARGF(usage()), nil, 0)*MiB;
+		v = strtoll(EARGF(usage()), &e, 0);
+		switch(*e){
+		case 'M': case 'm': case 0:
+			cachesz = v*MiB;
+			break;
+		case 'G': case 'g':
+			cachesz = v*GiB;
+			break;
+		case '%':
+			cachesz = v*memsz/100;
+			break;
+		default:
+			sysfatal("unknown suffix %s", e);
+		}
 		break;
 	case 'd':
 		debug++;