-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConfigurationManager.cpp
240 lines (186 loc) · 6.49 KB
/
ConfigurationManager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#include <iostream>
#include <fstream>
#include <sstream>
#include <stdexcept>
#include "ConfigurationManager.h"
#include <random>
ConfigurationManager::ConfigurationManager()
: numCPU(0), quantumCycles(0), preemptive(false), batchProcessFrequency(0), minInstructions(0), maxInstructions(0), delayPerExec(0), maxOverallMemory(0), minMemoryPerProcess(0), maxMemoryPerProcess(0), minPagePerProcess(0), maxPagePerProcess(0), memoryManagerAlgorithm("")
{}
ConfigurationManager::~ConfigurationManager()
{}
bool ConfigurationManager::initialize()
{
try {
parseConfigFile(); // Parse the configuration file
printConfig(); // Print the configuration settings
initialized = true;
return true;
} catch (const std::exception& e) {
std::cerr << "Error initializing configuration: " << e.what() << std::endl;
return false;
}
}
bool ConfigurationManager::isInitialized()
{
return initialized;
}
int ConfigurationManager::getNumCPU() const {
return numCPU;
}
std::string ConfigurationManager::getSchedulerAlgorithm() const {
// "rr" or "fcfs" or "sjf"
size_t firstQuote = schedulerAlgorithm.find('\"');
size_t secondQuote = schedulerAlgorithm.find('\"', firstQuote + 1);
return schedulerAlgorithm.substr(firstQuote + 1, secondQuote - firstQuote - 1);
}
float ConfigurationManager::getQuantumCycles() const {
return quantumCycles;
}
bool ConfigurationManager::isPreemptive() const {
return preemptive;
}
float ConfigurationManager::getBatchProcessFrequency() const {
return batchProcessFrequency;
}
int ConfigurationManager::getMinInstructions() const {
return minInstructions;
}
int ConfigurationManager::getMaxInstructions() const {
return maxInstructions;
}
float ConfigurationManager::getDelayPerExec() const {
return delayPerExec;
}
float ConfigurationManager::getMaxOverallMemory() const {
return maxOverallMemory;
}
float ConfigurationManager::getMinMemoryPerProcess() const {
return minMemoryPerProcess;
}
float ConfigurationManager::getMaxMemoryPerProcess() const {
return maxMemoryPerProcess;
}
float ConfigurationManager::getMinPagePerProcess() const {
return minPagePerProcess;
}
float ConfigurationManager::getMaxPagePerProcess() const {
return maxPagePerProcess;
}
std::string ConfigurationManager::getMemoryManagerAlgorithm() const {
std::string memoryManagerAlgorithm;
if (minPagePerProcess == 1 && maxPagePerProcess == 1) {
memoryManagerAlgorithm = "flat";
}else {
memoryManagerAlgorithm = "paging";
}
return memoryManagerAlgorithm;
}
void ConfigurationManager::parseConfigFile() {
std::ifstream configFile("config.txt");
if (!configFile.is_open()) {
throw std::runtime_error("Could not open config file");
}
std::string line;
while (std::getline(configFile, line)) {
std::istringstream iss(line);
std::string key;
if (!(iss >> key)) {
continue; // Skip empty lines
}
// find key word
if (key == "num-cpu") {
iss >> numCPU;
} else if (key == "scheduler") {
iss >> schedulerAlgorithm;
} else if (key == "quantum-cycles"){
iss >> quantumCycles;
} else if (key == "preemptive") {
int preemptiveFlag;
iss >> preemptiveFlag;
if (preemptiveFlag == 1) {
preemptive = true;
}
else if (preemptiveFlag == 0) {
preemptive = false;
}
else {
throw std::runtime_error("Invalid value for preemptive flag");
}
}else if (key == "batch-process-freq") {
iss >> batchProcessFrequency;
} else if (key == "min-ins") {
iss >> minInstructions;
} else if (key == "max-ins") {
iss >> maxInstructions;
} else if (key == "delay-per-exec") {
iss >> delayPerExec;
} else if (key == "max-overall-mem") {
iss >> maxOverallMemory;
} else if (key == "min-mem-per-proc") {
iss >> minMemoryPerProcess;
} else if (key == "max-mem-per-proc") {
iss >> maxMemoryPerProcess;
} else if (key == "min-page-per-proc"){
iss >> minPagePerProcess;
} else if (key == "max-page-per-proc"){
iss >> maxPagePerProcess;
}
}
configFile.close();
// Determine memory manager algorithm
// If min-page-per-proc and max-page-per-proc are 1,
// then the memory manager must use a flat memory allocator.
// If it’s >1, it’s a paging allocator
if (minPagePerProcess == 1 && maxPagePerProcess == 1) {
memoryManagerAlgorithm = "flat";
} else {
memoryManagerAlgorithm = "paging";
// Since paging will have set number of frames per process
int randomMemorySize = getRandomInt2N(minMemoryPerProcess, maxMemoryPerProcess);
int randomPageSize = getRandomInt2N(minPagePerProcess, maxPagePerProcess);
minMemoryPerProcess = randomMemorySize;
maxMemoryPerProcess = randomMemorySize;
minPagePerProcess = randomPageSize;
maxPagePerProcess = randomPageSize;
// Calculate the maximum number of frames
maxFrames = maxOverallMemory / randomPageSize;
}
}
void ConfigurationManager::printConfig() {
std::cout << "--------------------------" << std::endl;
std::cout << "Configuration Settings:" << std::endl;
std::cout << "num-cpu: " << numCPU << std::endl;
std::cout << "scheduler: " << schedulerAlgorithm << std::endl;
std::cout << "quantum-cycles: " << quantumCycles << std::endl;
std::cout << "preemptive: " << preemptive << std::endl;
std::cout << "batch-process-freq: " << batchProcessFrequency << std::endl;
std::cout << "min-ins: " << minInstructions << std::endl;
std::cout << "max-ins: " << maxInstructions << std::endl;
std::cout << "delay-per-exec: " << delayPerExec << std::endl;
std::cout << "max-overall-mem: " << maxOverallMemory << std::endl;
std::cout << "min-mem-per-proc: " << minMemoryPerProcess << std::endl;
std::cout << "max-mem-per-proc: " << maxMemoryPerProcess << std::endl;
std::cout << "min-page-per-proc: " << minPagePerProcess << std::endl;
std::cout << "max-page-per-proc: " << maxPagePerProcess << std::endl;
std::cout << "memory-manager: " << memoryManagerAlgorithm << std::endl; // "flat" or "paging"
std::cout << "--------------------------" << std::endl;
}
int ConfigurationManager::getRandomInt2N(int min, int max) {
int minExp = std::ceil(std::log2(min));
// Calculate the largest power of 2 less than or equal to max
int maxExp = std::floor(std::log2(max));
// Ensure the range is valid
if (minExp > maxExp) {
throw std::invalid_argument("No power of 2 within the given range");
}
// Initialize random number generator
static std::random_device rd;
static std::mt19937 gen(rd());
std::uniform_int_distribution<> dis(minExp, maxExp);
// Generate a random exponent between minExp and maxExp
int randomExp = dis(gen);
// Calculate the power of 2
int result = std::pow(2, randomExp);
return result;
}