add working set concept for processes

This commit is contained in:
ІО-23 Шмуляр Олег 2025-03-07 22:52:43 +02:00
parent d416a446aa
commit 819379055d
7 changed files with 31 additions and 16 deletions

View File

@ -1,4 +1,4 @@
CCFLAGS = -g -O0 -Iinc/ -I.
CCFLAGS = -g -O3 -Iinc/ -I.
main: main.o kernel.o mmu.o process.o random.o

View File

@ -1,5 +1,5 @@
#define PHYSICAL_PAGE_AMOUNT 1536
#define PHYSICAL_PAGE_AMOUNT 384
#define PAGE_REPLACEMENT_ALGORITHM 2
#define WSCLOCK_TIME_WINDOW 200
#define WSCLOCK_TIME_WINDOW 1500
#define VERBOSE_SANITY_CHECK 0

View File

@ -11,7 +11,7 @@ struct RunQ {
};
struct RunQ *RunQ(size_t max_procs);
void RUNQ_add_process(size_t max_page_accesses, size_t total_pages_owned);
void RUNQ_add_process(size_t max_page_accesses, size_t total_pages_owned, size_t ws_size);
void RUNQ_remove_current_process(void);

View File

@ -18,12 +18,16 @@ struct Process {
size_t max_accesses;
size_t total_pages_owned;
struct PageTableEntry *pt;
size_t *ws;
size_t ws_size;
};
struct Process *Process(size_t proc_id,
size_t max_accesses,
size_t total_pages_owned);
size_t total_pages_owned,
size_t ws_size);
size_t PROCESS_run_for(struct Process *p, size_t time_bits);
void PROCESS_change_working_set(struct Process *p);
#endif

View File

@ -33,12 +33,13 @@ RunQ(size_t max_procs)
void
RUNQ_add_process(size_t max_page_accesses,
size_t total_pages_owned)
size_t total_pages_owned,
size_t ws_size)
{
if (runq->proc_amount >= runq->max_procs)
return;
struct Process *new_p = Process(runq->next_proc_id, max_page_accesses, total_pages_owned);
struct Process *new_p = Process(runq->next_proc_id, max_page_accesses, total_pages_owned, ws_size);
if (!runq->current_proc) {
runq->current_proc = new_p;
@ -119,6 +120,7 @@ RUNQ_remove_current_process(void)
}
free(tp);
runq->proc_amount--;
}

View File

@ -63,22 +63,24 @@ main(void)
for (size_t i = 0; i < 10; i++) {
//RUNQ_add_process(1000 + randint(29000), 10 + randint(190));
RUNQ_add_process(30000, 200);
RUNQ_add_process(30000, 200, 10);
}
for (int i = 0; i < 100000; i++) {
for (int i = 0; i < 10000; i++) {
//int removal_requested = PROCESS_run_for(runq->current_proc, 50);
int removal_requested = PROCESS_run_for(runq->current_proc, 150);
int removal_requested = PROCESS_run_for(runq->current_proc, 70);
if (removal_requested)
if (removal_requested) {
RUNQ_remove_current_process();
else
RUNQ_add_process(30000, 200, 10);
} else {
runq->current_proc = runq->current_proc->next;
}
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);
//KERNEL_update_job(8);
KERNEL_update_job(64);
KERNEL_update_job(16);
if (!runq->current_proc) {
printf("[main:scheduling] No processes left to execute!\n");

View File

@ -6,7 +6,7 @@
#include "random.h"
struct Process *
Process(size_t proc_id, size_t max_accesses, size_t total_pages_owned)
Process(size_t proc_id, size_t max_accesses, size_t total_pages_owned, size_t ws_size)
{
struct Process *p = malloc(sizeof(struct Process));
p->id = proc_id;
@ -18,6 +18,12 @@ Process(size_t proc_id, size_t max_accesses, size_t total_pages_owned)
for (size_t i = 0; i < p->total_pages_owned; i++)
p->pt[i].p = 0;
p->ws_size = ws_size;
p->ws = malloc(sizeof(size_t) * ws_size);
for (size_t i = 0; i < ws_size; i++)
p->ws[i] = randint(total_pages_owned);
return p;
}
@ -32,8 +38,9 @@ size_t PROCESS_run_for(struct Process *p, size_t time_bits)
(i < time_bits) && (p->pages_accessed < p->max_accesses);
i++, p->pages_accessed++)
{
// no working set yet
int accessed_page_no = randint(p->total_pages_owned);
int accessed_page_no = randint(10) == 0
? randint(p->total_pages_owned)
: p->ws[randint(p->ws_size)];
if (randint(10) > 7) {
printf("[process %d] Writing to page #%d\n", p->id, accessed_page_no);