spz3/main.cpp

70 lines
2.3 KiB
C++

#include <iostream>
#include <format>
#include <random>
#include "Kernel.h"
#include "MMU.h"
static int MAX_PROC = 10;
static int PAGE_N = 10;
int main() {
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<unsigned int> distribution(500, 600), access_type_distr(1, 10);
Kernel kernel(MAX_PROC, List<PhysicalPage>(), List<PhysicalPage>());
unsigned int elapsed_time = 0;
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) {
kernel.RunQ.emplace_back(new PageTable(distribution(gen)), distribution(gen));
}
for (int i = 0; i < 10000; ++i) {
if (kernel.RunQ.empty()) {
break;
}
std::uniform_int_distribution<unsigned int> 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;
const unsigned int index = index_distribution(gen);
MMU::access(&kernel, kernel.RunQ.at(0).pageTable, index, type);
}
++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;
}
}
return 0;
}