diff --git a/Kernel.cpp b/Kernel.cpp index 7335c4e..5aaf416 100644 --- a/Kernel.cpp +++ b/Kernel.cpp @@ -27,6 +27,7 @@ void Kernel::page_fault(PageTable *page_table, const unsigned int idx) { } if (page) { + page->take_up(page_table, idx); (*page_table)[idx].PPN = page->PPN; (*page_table)[idx].P = true; (*page_table)[idx].R = false; diff --git a/List.h b/List.h index 8eff143..4c6054a 100644 --- a/List.h +++ b/List.h @@ -45,20 +45,22 @@ void List::insert_head(T *elem) { } this->head = elem; - ++this->num; + ++num; } template void List::insert_tail(T *elem) { - if (!head) + if (!head) { elem->next = elem->prev = elem; + head = elem; + } else { elem->next = head; elem->prev = head->prev; head->prev->next = elem; head->prev = elem; } - ++this->num; + ++num; } template @@ -70,7 +72,7 @@ void List::insert_before(T *elem, T *reference_point) { this->head = elem; else elem->prev->next = elem; - ++this->num; + ++num; } template @@ -81,7 +83,7 @@ void List::remove(T *elem) { elem->prev->next = elem->next; if (this->head == elem) this->head = elem->next; - --this->num; + --num; } template @@ -91,7 +93,7 @@ bool List::is_empty() { template unsigned int List::get_num() const { - return this->num; + return num; } #endif //LIST_H diff --git a/PTE.h b/PTE.h index f7d43aa..063db99 100644 --- a/PTE.h +++ b/PTE.h @@ -1,7 +1,3 @@ -// -// Created by rhinemann on 10.03.25. -// - #ifndef PTE_H #define PTE_H diff --git a/Process.cpp b/Process.cpp index 991f895..6ea1286 100644 --- a/Process.cpp +++ b/Process.cpp @@ -1,9 +1,9 @@ #include "Process.h" Process::Process(PageTable *pageTable, const unsigned int executionTime) -: pageTable(pageTable), executionTime(executionTime), elapsedTime(0) { +: pageTable(pageTable), executionTime(executionTime) { } -bool Process::is_finished() const { +bool Process::is_finished(const unsigned int elapsedTime) const { return elapsedTime >= executionTime; } diff --git a/Process.h b/Process.h index da575ce..691db55 100644 --- a/Process.h +++ b/Process.h @@ -6,9 +6,9 @@ class Process { public: PageTable *pageTable; - unsigned int executionTime, elapsedTime; + unsigned int executionTime; Process(PageTable *pageTable, unsigned int executionTime); - [[nodiscard]] bool is_finished() const; + [[nodiscard]] bool is_finished(unsigned int elapsedTime) const; }; diff --git a/main.cpp b/main.cpp index 4671899..9a9158f 100644 --- a/main.cpp +++ b/main.cpp @@ -1,4 +1,5 @@ #include +#include #include #include "Kernel.h" @@ -12,11 +13,11 @@ int main() { std::mt19937 gen(rd()); std::uniform_int_distribution distribution(500, 600), access_type_distr(1, 10); - List free_pages, busy_pages; - Kernel kernel(MAX_PROC, free_pages, busy_pages); + Kernel kernel(MAX_PROC, List(), List()); + unsigned int elapsed_time = 0; - for (int i = 1; i < PAGE_N; ++i) { - free_pages.insert_head(new PhysicalPage(i)); + for (int i = 1; i <= PAGE_N; ++i) { + kernel.free_pages.insert_head(new PhysicalPage(i)); } for (int i = 0; i < kernel.RunQ.capacity(); ++i) { @@ -27,14 +28,40 @@ int main() { if (kernel.RunQ.empty()) { break; } + + std::uniform_int_distribution index_distribution(0, kernel.RunQ.at(0).pageTable->entries.size() - 1); for (int j = 0; j < 30; ++j) { const AccessType type = access_type_distr(gen) == 1 ? WRITE : READ; - constexpr unsigned int index = 1; + const unsigned int index = index_distribution(gen); MMU::access(&kernel, kernel.RunQ.at(0).pageTable, index, type); } - ++kernel.RunQ.at(0).elapsedTime; - if (kernel.RunQ.at(0).is_finished()) { + + ++elapsed_time; + + // kernel.stat_update(); + + if (kernel.RunQ.at(0).is_finished(elapsed_time)) { + PhysicalPage *curr, *cached_next; + unsigned int j; + const unsigned int cached_len = kernel.busy_pages.get_num(); + for (curr = kernel.busy_pages.get_head(), j = 0; + curr && curr->next && j < cached_len; + curr = cached_next, ++j) { + cached_next = curr->next; + if (curr->PT == kernel.RunQ.at(0).pageTable) { + kernel.busy_pages.remove(curr); + kernel.free_pages.insert_tail(curr); + } + } + delete kernel.RunQ.at(0).pageTable; kernel.RunQ.erase(kernel.RunQ.begin()); + + kernel.RunQ.emplace_back(new PageTable(distribution(gen)), distribution(gen)); + } + + { + unsigned int busy_stat = kernel.busy_pages.get_num(), free_stat = kernel.free_pages.get_num(); + std::cout << std::format("[main:metrics] Memory usage stats: busy {}, free {}, total {}", busy_stat, free_stat, free_stat + busy_stat) << std::endl; } }