Add some mem stat

This commit is contained in:
Mathieu Maret 2021-01-23 21:51:00 +01:00
parent c3d345bb87
commit 73b721dbc1
4 changed files with 46 additions and 8 deletions

View File

@ -101,10 +101,8 @@ void kmain(unsigned long magic, unsigned long addr)
for (uint i = 0; i < size; i++) {
printf(" base_addr 0x%llx, length = 0x%llx, type = 0x%x\n", mmap[i].addr,
mmap[i].len, (uint32_t)mmap[i].type);
memAddBank(
max(mmap[i].addr, (multiboot_uint64_t)lastUsedByMem),
min((multiboot_uint64_t)(upperMemKB * 1024), mmap[i].addr + mmap[i].len),
mmap[i].type == MULTIBOOT_MEMORY_AVAILABLE);
memAddBank(max(mmap[i].addr, (multiboot_uint64_t)lastUsedByMem),
mmap[i].addr + mmap[i].len, mmap[i].type == MULTIBOOT_MEMORY_AVAILABLE);
}
} else {
printf("Cannot get memory Mapping information, using default value\n");
@ -134,7 +132,13 @@ void kmain(unsigned long magic, unsigned long addr)
#ifdef RUN_TEST
run_test();
#endif
printf("\nSystem init done\n");
printf("\nSystem init done: ");
{
uint free, used;
memGetStat(&free, &used);
printf("%dKB free %dKB Used\n", free * (PAGE_SIZE / 1024), used * (PAGE_SIZE / 1024));
}
// There is no real caller behind this point
// So finish this by ourself

View File

@ -1,3 +1,4 @@
#include "assert.h"
#include "mem.h"
#include "kernel.h"
#include "klibc.h"
@ -7,6 +8,8 @@
static struct memDesc *pageDesc = (struct memDesc *)&__ld_kernel_end;
static struct memDesc *freePage;
static struct memDesc *usedPage;
paddr_t pageDescEnd;
paddr_t upperMem;
static unsigned long allocatedPage = 0;
@ -16,7 +19,7 @@ int memSetup(paddr_t upperMemKB, paddr_t *lastMemUsedOut)
list_init(usedPage);
// Align upper mem (in kB) on page size even if it does loose a page
upperMemKB = ALIGN_DOWN(upperMemKB, PAGE_SIZE / 1024);
upperMemKB = ALIGN_DOWN(upperMemKB, PAGE_SIZE / 1024);
unsigned long nbPage = ((upperMemKB) / (PAGE_SIZE / 1024));
printf("Available Mem from 0x%x to 0x%x: %dMB in %d Pages of %dB\n", &__ld_kernel_end,
@ -24,8 +27,9 @@ int memSetup(paddr_t upperMemKB, paddr_t *lastMemUsedOut)
nbPage, PAGE_SIZE);
// Memory description is stored after the kernel. We need some place to store it
unsigned long pageDescEnd = (unsigned long)pageDesc + nbPage * sizeof(struct memDesc);
*lastMemUsedOut = ALIGN(pageDescEnd, PAGE_SIZE);
pageDescEnd = (unsigned long)pageDesc + nbPage * sizeof(struct memDesc);
*lastMemUsedOut = ALIGN(pageDescEnd, PAGE_SIZE);
upperMem = upperMemKB * 1024;
memAddBank(0, *lastMemUsedOut, 0);
return 0;
@ -33,6 +37,7 @@ int memSetup(paddr_t upperMemKB, paddr_t *lastMemUsedOut)
int memAddBank(paddr_t bottomMem, paddr_t topMem, int isFree)
{
topMem = min(topMem, upperMem);
for (uint i = (bottomMem >> PAGE_SHIFT); i < (topMem >> PAGE_SHIFT); i++) {
struct memDesc *mem = &pageDesc[i];
if (isFree) {
@ -48,6 +53,22 @@ int memAddBank(paddr_t bottomMem, paddr_t topMem, int isFree)
return 0;
}
void memGetStat(uint *free, uint *used)
{
uint idx;
struct memDesc *mem;
list_foreach(freePage, mem, idx)
{
continue;
}
*free = idx;
list_foreach(usedPage, mem, idx)
{
continue;
}
*used = idx;
}
struct memDesc *addr2memDesc(paddr_t addr)
{
int idx = addr >> PAGE_SHIFT;

View File

@ -21,3 +21,4 @@ paddr_t allocPhyPage(uint nbPage);
int unrefPhyPage(paddr_t addr);
int refPhyPage(paddr_t addr);
unsigned long getNbAllocatedPage(void);
void memGetStat(uint *free, uint *used);

View File

@ -21,12 +21,21 @@ void testPhymem(void)
int allocCount = 0;
int freeCount = 0;
uint freePageStatBegin, usedPageStatBegin;
uint freePageStatAlloc, usedPageStatAlloc;
uint freePageStatFree, usedPageStatFree;
memGetStat(&freePageStatBegin, &usedPageStatBegin);
while ((page = (struct memDesc *)allocPhyPage(1)) != NULL) {
page->phy_addr = allocCount;
allocCount++;
list_add_tail(allocated_page_list, page);
}
printf("%d pages allocated\n", allocCount);
memGetStat(&freePageStatAlloc, &usedPageStatAlloc);
assert(freePageStatAlloc == 0);
assert((usedPageStatAlloc - usedPageStatBegin) == (uint)allocCount);
while ((page = list_pop_head(allocated_page_list)) != NULL) {
assertmsg(page->phy_addr == (ulong)freeCount, "page %d modified", page);
@ -34,6 +43,9 @@ void testPhymem(void)
freeCount++;
}
printf("%d pages freed\n", freeCount);
memGetStat(&freePageStatFree, &usedPageStatFree);
assert(freePageStatFree == freePageStatBegin);
assert(usedPageStatFree == usedPageStatBegin);
assertmsg((page = (struct memDesc *)allocPhyPage(1)) != NULL, "Cannot allocate memory\n");
unrefPhyPage((ulong)page);