63 lines
2.2 KiB
C++
63 lines
2.2 KiB
C++
#include "Kernel.h"
|
|
|
|
#include <iostream>
|
|
|
|
using std::next;
|
|
|
|
Kernel::Kernel(const unsigned int RunQ_max_length, const List<PhysicalPage> &free_pages,
|
|
const List<PhysicalPage> &busy_pages) : free_pages(free_pages), busy_pages(busy_pages) {
|
|
RunQ.reserve(RunQ_max_length);
|
|
}
|
|
|
|
void Kernel::page_fault(std::vector<PTE> *page_table, const unsigned int idx) {
|
|
std::cout << std::format("[kernel:page_fault] Handling {} started", idx) << std::endl;
|
|
|
|
PhysicalPage *page;
|
|
if (!this->free_pages.is_empty()) {
|
|
page = free_pages.get_head()->prev;
|
|
free_pages.remove(page);
|
|
busy_pages.insert_tail(page);
|
|
|
|
std::cout << std::format("[kernel:page_fault:free] Found free page #{}, using it",
|
|
page->PPN) << std::endl;
|
|
} else {
|
|
std::cout << "[kernel:page_fault] No free pages available, trying to swap..." << std::endl;
|
|
for (PhysicalPage *curr = busy_pages.get_head();; curr = curr->next) {
|
|
if ((*curr->PT)[curr->idx].R)
|
|
(*curr->PT)[curr->idx].R = false;
|
|
else {
|
|
std::cout << std::format("[kernel:page_fault:random] Selected physical page #{} for replacement",
|
|
curr->PPN) << std::endl;
|
|
|
|
// curr->PT->erase(next(curr->PT->begin(), curr->idx));
|
|
(*curr->PT)[curr->idx].P = false;
|
|
page = curr;
|
|
busy_pages.set_head(curr->next);
|
|
|
|
std::cout << std::format("[kernel:page_fault:random] Auto-advanced the list of busy pages to ppn {}",
|
|
busy_pages.get_head()->PPN) << std::endl;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
page->take_up(page_table, idx);
|
|
(*page_table)[idx].PPN = page->PPN;
|
|
(*page_table)[idx].P = true;
|
|
(*page_table)[idx].R = false;
|
|
(*page_table)[idx].M = false;
|
|
}
|
|
|
|
void Kernel::stat_update() {
|
|
int i;
|
|
PhysicalPage *curr;
|
|
for (i = 0, curr = busy_pages.get_head(); curr && i < 10; ++i, curr = curr->next) {
|
|
if ((*curr->PT)[curr->idx].R) {
|
|
(*curr->PT)[curr->idx].R = false;
|
|
|
|
std::cout << std::format("[kernel:update_job] Updating ppn {} status", curr->PPN) << std::endl;
|
|
}
|
|
}
|
|
busy_pages.set_head(curr);
|
|
}
|