spz3/Kernel.cpp

63 lines
2.2 KiB
C++
Raw Permalink Normal View History

2025-03-31 17:19:43 +03:00
#include "Kernel.h"
2025-04-01 16:29:33 +03:00
#include <iostream>
using std::next;
2025-03-31 17:19:43 +03:00
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) {
2025-04-01 14:10:01 +03:00
RunQ.reserve(RunQ_max_length);
}
2025-03-31 17:19:43 +03:00
2025-04-01 16:29:33 +03:00
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;
2025-03-31 17:19:43 +03:00
if (!this->free_pages.is_empty()) {
page = free_pages.get_head()->prev;
2025-03-31 17:19:43 +03:00
free_pages.remove(page);
busy_pages.insert_tail(page);
2025-04-01 16:29:33 +03:00
std::cout << std::format("[kernel:page_fault:free] Found free page #{}, using it",
page->PPN) << std::endl;
2025-03-31 17:19:43 +03:00
} else {
2025-04-01 16:29:33 +03:00
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;
2025-04-01 16:29:33 +03:00
// curr->PT->erase(next(curr->PT->begin(), curr->idx));
(*curr->PT)[curr->idx].P = false;
page = curr;
busy_pages.set_head(curr->next);
2025-04-01 16:29:33 +03:00
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;
}
}
2025-03-31 17:19:43 +03:00
}
2025-04-01 16:29:33 +03:00
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;
2025-03-31 17:19:43 +03:00
}
void Kernel::stat_update() {
int i;
PhysicalPage *curr;
2025-04-01 16:29:33 +03:00
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;
2025-04-01 16:29:33 +03:00
std::cout << std::format("[kernel:update_job] Updating ppn {} status", curr->PPN) << std::endl;
}
2025-03-31 17:19:43 +03:00
}
busy_pages.set_head(curr);
2025-03-31 17:19:43 +03:00
}