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")){
/* new /dev/swap - skip first 3 numbers */
if(!readnums(m, 7, a, 1))
if(!readnums(m, 5, a, 1))
return 0;
a[0] = a[3];
a[1] = a[4];

View File

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

View File

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

View File

@ -55,8 +55,6 @@ extern int page_dispose(PagePointer *slot);
*/
extern int page_assign(PagePointer *target, PagePointer page);
extern char* pages_stats(char *s, char *e);
/* Replace the page in *slot with a copy
*
* 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_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);