kernel: fix /dev/swap and `stats -m`

This commit is contained in:
Giacomo Tesio 2017-01-12 00:35:27 +01:00
parent 4e8d766fe8
commit 0de2dfc95b
4 changed files with 35 additions and 31 deletions

View File

@ -467,7 +467,7 @@ readswap(Machine *m, uint64_t *a)
{ {
if(strstr(m->buf, "memory\n")){ if(strstr(m->buf, "memory\n")){
/* new /dev/swap - skip first 3 numbers */ /* new /dev/swap - skip first 3 numbers */
if(!readnums(m, 7, a, 1)) if(!readnums(m, 5, a, 1))
return 0; return 0;
a[0] = a[3]; a[0] = a[3];
a[1] = a[4]; a[1] = a[4];

View File

@ -831,7 +831,8 @@ consread(Chan *c, void *buf, long n, int64_t off)
{ {
uint64_t l; uint64_t l;
Mach *mp; Mach *mp;
char *b, *bp, *s, ch; MemoryStats mstats;
char *b, *bp, ch;
char tmp[6*NUMSIZE+1]; /* must be >= 6*NUMSIZE (Qcputime) */ char tmp[6*NUMSIZE+1]; /* must be >= 6*NUMSIZE (Qcputime) */
int i, id, send; int i, id, send;
long offset; long offset;
@ -997,26 +998,18 @@ consread(Chan *c, void *buf, long n, int64_t off)
return n; return n;
case Qswap: case Qswap:
bp = smalloc(READSTR); memory_stats(&mstats);
if(waserror()){ snprint(tmp, sizeof tmp,
free(bp); "%llud memory\n"
nexterror(); "%llud pagesize\n"
} "%lud kernel\n"
s = pages_stats(bp, bp+READSTR); "%lud/%lud user\n",
s = seprintphysstats(s, bp+READSTR); mstats.memory,
l = s - bp; (unsigned long)PGSZ,
b = buf; mstats.kernel,
i = readstr(offset, b, n, bp); mstats.user, mstats.user_available);
poperror();
free(bp);
b += i;
n -= i;
if(offset > l)
offset -= l;
else
offset = 0;
return i + mallocreadsummary(c, b, n, offset); return readstr(offset, buf, n, tmp);
case Qsysname: case Qsysname:
if(sysname == nil) if(sysname == nil)

View File

@ -100,15 +100,18 @@ umem_init(void)
} }
char* void
pages_stats(char *s, char *e) memory_stats(MemoryStats *stats)
{ {
plock(); uintptr_t km;
s = seprint(s, e, "%lud/%lud %dK user pages avail\n", if(stats == nil)
pool.freepages + pool.blankpages, panic("memory_stats: nil pointer, pc %#p", getcallerpc());
pool.npages, (pool.freepages + pool.blankpages)*PGSZ/KiB); km = ROUNDUP((uintptr_t)end - KTZERO, PGSZ);
punlock(); km += ROUNDDN(sys->vmunmapped - (uintptr_t)end, PGSZ);
return s; stats->memory = sys->pmoccupied;
stats->kernel = km;
stats->user_available = pool.npages*PGSZ;
stats->user = (pool.npages - pool.blankpages - pool.freepages)*PGSZ;
} }
KMap* KMap*

View File

@ -55,8 +55,6 @@ extern int page_dispose(PagePointer *slot);
*/ */
extern int page_assign(PagePointer *target, PagePointer page); extern int page_assign(PagePointer *target, PagePointer page);
extern char* pages_stats(char *s, char *e);
/* Replace the page in *slot with a copy /* Replace the page in *slot with a copy
* *
* Returns 1 on success, 0 on failure. * Returns 1 on success, 0 on failure.
@ -247,3 +245,13 @@ extern void proc_check_pages(void);
extern int proc_own_pagepool(Proc *p); extern int proc_own_pagepool(Proc *p);
extern int proc_segment_detach(Proc *p, uintptr_t va); extern int proc_segment_detach(Proc *p, uintptr_t va);
typedef struct MemoryStats
{
unsigned long memory;
unsigned long kernel;
unsigned long user_available;
unsigned long user;
} MemoryStats;
/* fills stats with memory statistics */
extern void memory_stats(MemoryStats *stats);