2025-03-31 17:19:43 +03:00
|
|
|
#include <iostream>
|
2025-04-01 15:22:38 +03:00
|
|
|
#include <format>
|
2025-03-31 17:19:43 +03:00
|
|
|
#include <random>
|
|
|
|
|
|
|
|
#include "Kernel.h"
|
2025-04-01 14:10:01 +03:00
|
|
|
#include "MMU.h"
|
2025-03-31 17:19:43 +03:00
|
|
|
|
|
|
|
static int MAX_PROC = 10;
|
|
|
|
static int PAGE_N = 10;
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
std::random_device rd;
|
|
|
|
std::mt19937 gen(rd());
|
2025-04-01 14:10:01 +03:00
|
|
|
std::uniform_int_distribution<unsigned int> distribution(500, 600), access_type_distr(1, 10);
|
2025-03-31 17:19:43 +03:00
|
|
|
|
2025-04-01 15:22:38 +03:00
|
|
|
Kernel kernel(MAX_PROC, List<PhysicalPage>(), List<PhysicalPage>());
|
|
|
|
unsigned int elapsed_time = 0;
|
2025-03-31 17:19:43 +03:00
|
|
|
|
2025-04-01 15:22:38 +03:00
|
|
|
for (int i = 1; i <= PAGE_N; ++i) {
|
|
|
|
kernel.free_pages.insert_head(new PhysicalPage(i));
|
2025-03-31 17:19:43 +03:00
|
|
|
}
|
|
|
|
|
2025-04-01 14:10:01 +03:00
|
|
|
for (int i = 0; i < kernel.RunQ.capacity(); ++i) {
|
|
|
|
kernel.RunQ.emplace_back(new PageTable(distribution(gen)), distribution(gen));
|
2025-03-31 17:19:43 +03:00
|
|
|
}
|
|
|
|
|
2025-04-01 14:10:01 +03:00
|
|
|
for (int i = 0; i < 10000; ++i) {
|
|
|
|
if (kernel.RunQ.empty()) {
|
|
|
|
break;
|
|
|
|
}
|
2025-04-01 15:22:38 +03:00
|
|
|
|
|
|
|
std::uniform_int_distribution<unsigned int> index_distribution(0, kernel.RunQ.at(0).pageTable->entries.size() - 1);
|
2025-04-01 14:10:01 +03:00
|
|
|
for (int j = 0; j < 30; ++j) {
|
|
|
|
const AccessType type = access_type_distr(gen) == 1 ? WRITE : READ;
|
2025-04-01 15:22:38 +03:00
|
|
|
const unsigned int index = index_distribution(gen);
|
2025-04-01 14:10:01 +03:00
|
|
|
MMU::access(&kernel, kernel.RunQ.at(0).pageTable, index, type);
|
|
|
|
}
|
2025-04-01 15:22:38 +03:00
|
|
|
|
|
|
|
++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;
|
2025-04-01 14:10:01 +03:00
|
|
|
kernel.RunQ.erase(kernel.RunQ.begin());
|
2025-04-01 15:22:38 +03:00
|
|
|
|
|
|
|
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;
|
2025-04-01 14:10:01 +03:00
|
|
|
}
|
2025-03-31 22:20:01 +03:00
|
|
|
}
|
|
|
|
|
2025-03-31 17:19:43 +03:00
|
|
|
return 0;
|
|
|
|
}
|