From 566dcc18bdcadc1350b24d91769a25e0828ca0dc Mon Sep 17 00:00:00 2001 From: hasslesstech Date: Tue, 18 Mar 2025 19:36:56 +0200 Subject: [PATCH] add config option to disable logging output --- Makefile | 2 +- config.h | 4 ++++ src/kernel.c | 32 ++++++++++++++++++++++++++++++++ src/main.c | 2 ++ src/mmu.c | 5 +++++ src/process.c | 6 ++++++ 6 files changed, 50 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 0264925..e00cdd7 100644 --- a/Makefile +++ b/Makefile @@ -10,7 +10,7 @@ main.o: src/main.c config.h kernel.o: src/kernel.c config.h gcc $(CCFLAGS) -c -o kernel.o src/kernel.c -mmu.o: src/mmu.c +mmu.o: src/mmu.c config.h gcc $(CCFLAGS) -c -o mmu.o src/mmu.c process.o: src/process.c config.h diff --git a/config.h b/config.h index 53ec5a4..143a6c4 100644 --- a/config.h +++ b/config.h @@ -6,3 +6,7 @@ #define VERBOSE_SANITY_CHECK 0 #define WS_CHANGE_INVERSE_CHANCE 1000 + +/* --- Debug options --- */ + +#define DISABLE_LOGGING 0 diff --git a/src/kernel.c b/src/kernel.c index 379b306..9bbe1c8 100644 --- a/src/kernel.c +++ b/src/kernel.c @@ -62,7 +62,9 @@ RUNQ_remove_current_process(void) { struct Process *tp = runq->current_proc; +#if DISABLE_LOGGING != 1 printf("[kernel:remove_current_process] Cleaning up process id %d\n", tp->id); +#endif if (runq->current_proc == runq->current_proc->next) { runq->current_proc = NULL; @@ -80,7 +82,9 @@ RUNQ_remove_current_process(void) for (size_t i = 0; i < PHYSICAL_PAGE_AMOUNT; i++) { if (cp->pt == tp->pt) { +#if DISABLE_LOGGING != 1 printf("[kernel:remove_current_process] Found ppn #%d (pte %d#%d), freeing it\n", cp->ppn, tp->id, cp->pt_index); +#endif // detach page from the busy list if (cp == cp->next) { first_busy_page = NULL; @@ -130,11 +134,15 @@ RUNQ_remove_current_process(void) void KERNEL_page_fault(struct PageTableEntry *pt, size_t page_no) { +#if DISABLE_LOGGING != 1 printf("[kernel:page_fault] Handling %d started\n", page_no); +#endif if (first_free_page) { free_pages_cnt--; +#if DISABLE_LOGGING != 1 printf("[kernel:page_fault] Found free page #%d, using it\n", first_free_page->ppn); +#endif pt[page_no].ppn = first_free_page->ppn; pt[page_no].p = 1; @@ -170,10 +178,14 @@ KERNEL_page_fault(struct PageTableEntry *pt, size_t page_no) busy_pages_cnt++; } else { +#if DISABLE_LOGGING != 1 printf("[kernel:page_fault] No free pages available, trying to swap...\n"); +#endif #if PAGE_REPLACEMENT_ALGORITHM == 1 +#if DISABLE_LOGGING != 1 printf("[kernel:page_fault:random] Selected physical page #%d for replacement\n", first_busy_page->ppn); +#endif // clear presence 'bit' from old PTE first_busy_page->pt[first_busy_page->pt_index].p = 0; @@ -189,21 +201,27 @@ KERNEL_page_fault(struct PageTableEntry *pt, size_t page_no) // move hand to next physical page first_busy_page = first_busy_page->next; +#if DISABLE_LOGGING != 1 printf("[kernel:page_fault:random] Auto-advanced the list of busy pages to ppn %d\n", first_busy_page->ppn); +#endif #elif PAGE_REPLACEMENT_ALGORITHM == 2 // first pass (no reference flag + out of time window) for (size_t i = 0; i < PHYSICAL_PAGE_AMOUNT; i++) { if (first_busy_page->pt[first_busy_page->pt_index].r) { +#if DISABLE_LOGGING != 1 printf("[kernel:page_fault:wsclock] ppn %d: r = 1, time %d -> %d\n", first_busy_page->ppn, first_busy_page->last_accessed, system_time); +#endif first_busy_page->last_accessed = system_time; first_busy_page->pt[first_busy_page->pt_index].r = 0; first_busy_page = first_busy_page->next; } else if ((system_time - first_busy_page->last_accessed) > WSCLOCK_TIME_WINDOW) { +#if DISABLE_LOGGING != 1 printf("[kernel:page_fault:wsclock] ppn %d: r = 0, time_window = %d (> %d), using it\n", first_busy_page->ppn, system_time - first_busy_page->last_accessed, WSCLOCK_TIME_WINDOW); +#endif first_busy_page->pt[first_busy_page->pt_index].p = 0; first_busy_page->pt = pt; first_busy_page->pt_index = page_no; @@ -214,15 +232,19 @@ KERNEL_page_fault(struct PageTableEntry *pt, size_t page_no) goto page_replacement_resolved; } else { +#if DISABLE_LOGGING != 1 printf("[kernel:page_fault:wsclock] ppn %d: r = 0, time_window = %d (<= %d), still active\n", first_busy_page->ppn, system_time - first_busy_page->last_accessed, WSCLOCK_TIME_WINDOW); +#endif first_busy_page = first_busy_page->next; } } // fallback to random +#if DISABLE_LOGGING != 1 printf("[kernel:page_fault:wsclock] Failed to find non-active page, randomly selecting ppn %d\n", first_busy_page->ppn); +#endif // clear presence 'bit' from old PTE first_busy_page->pt[first_busy_page->pt_index].p = 0; @@ -238,7 +260,9 @@ KERNEL_page_fault(struct PageTableEntry *pt, size_t page_no) // move hand to next physical page first_busy_page = first_busy_page->next; +#if DISABLE_LOGGING != 1 printf("[kernel:page_fault:wsclock] Auto-advanced the list of busy pages to ppn %d\n", first_busy_page->ppn); +#endif page_replacement_resolved: #endif @@ -252,7 +276,9 @@ page_replacement_resolved: void KERNEL_update_job(size_t page_amount) { if (first_busy_page == NULL) { +#if DISABLE_LOGGING != 1 printf("[kernel:update_job] No pages to update\n"); +#endif return; } @@ -260,17 +286,23 @@ void KERNEL_update_job(size_t page_amount) for (size_t i = 0; i < page_amount; i++) { if (first_busy_page->pt[first_busy_page->pt_index].r) { +#if DISABLE_LOGGING != 1 printf("[kernel:update_job] ppn %d updated time %d -> %d\n", first_busy_page->ppn, first_busy_page->last_accessed, system_time); +#endif first_busy_page->pt[first_busy_page->pt_index].r = 0; first_busy_page->last_accessed = system_time; } else { +#if DISABLE_LOGGING != 1 printf("[kernel:update_job] ppn %d is up-to-date (time = %d)\n", first_busy_page->ppn, first_busy_page->last_accessed); +#endif } first_busy_page = first_busy_page->next; if (first_busy_page == starting_page) { +#if DISABLE_LOGGING != 1 printf("[kernel:update_job] Looped around the busy page list, exiting after %d iterations\n", i); +#endif break; } } diff --git a/src/main.c b/src/main.c index 7909846..810d5af 100644 --- a/src/main.c +++ b/src/main.c @@ -77,7 +77,9 @@ main(void) runq->current_proc = runq->current_proc->next; } +#if DISABLE_LOGGING != 1 printf("[main:metrics] Memory usage stats: busy %d, free %d, total %d\n", busy_pages_cnt, free_pages_cnt, free_pages_cnt+busy_pages_cnt); +#endif //KERNEL_update_job(8); KERNEL_update_job(16); diff --git a/src/mmu.c b/src/mmu.c index 49050f8..cd79e30 100644 --- a/src/mmu.c +++ b/src/mmu.c @@ -2,6 +2,7 @@ #include "kernel.h" +#include "config.h" extern size_t system_time; @@ -11,7 +12,9 @@ void MMU_read(struct PageTableEntry *pt, size_t page_no) if (!pt[page_no].p) KERNEL_page_fault(pt, page_no); +#if DISABLE_LOGGING != 1 printf("[mmu:read] pt[%d] -> ppn %d\n", page_no, pt[page_no].ppn); +#endif pt[page_no].r = 1; system_time++; @@ -22,7 +25,9 @@ void MMU_write(struct PageTableEntry *pt, size_t page_no) if (!pt[page_no].p) KERNEL_page_fault(pt, page_no); +#if DISABLE_LOGGING != 1 printf("[mmu:write] pt[%d] -> ppn %d\n", page_no, pt[page_no].ppn); +#endif pt[page_no].r = 1; pt[page_no].m = 1; diff --git a/src/process.c b/src/process.c index e851155..f08b7a0 100644 --- a/src/process.c +++ b/src/process.c @@ -54,10 +54,14 @@ PROCESS_run_for(struct Process *p, size_t time_bits) : p->ws[randint(p->ws_size)]; if (randint(10) > 7) { +#if DISABLE_LOGGING != 1 printf("[process %d] Writing to page #%d\n", p->id, accessed_page_no); +#endif MMU_write(p->pt, accessed_page_no); } else { +#if DISABLE_LOGGING != 1 printf("[process %d] Reading from page #%d\n", p->id, accessed_page_no); +#endif MMU_read(p->pt, accessed_page_no); } @@ -71,7 +75,9 @@ PROCESS_run_for(struct Process *p, size_t time_bits) void PROCESS_change_working_set(struct Process *p) { +#if DISABLE_LOGGING != 1 printf("[process %d] Changing working set after %d accesses\n", p->id, p->pages_accessed); +#endif for (size_t i = 0; i < p->ws_size; i++) p->ws[i] = randint(p->total_pages_owned);