-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalytics_engine.h
More file actions
252 lines (227 loc) · 10.2 KB
/
Copy pathanalytics_engine.h
File metadata and controls
252 lines (227 loc) · 10.2 KB
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
241
242
243
244
245
246
247
248
249
250
251
252
#ifndef ANALYTICS_ENGINE_H
#define ANALYTICS_ENGINE_H
#include <iostream>
#include <vector>
#include <string>
#include <queue>
#include <unordered_map>
#include <algorithm>
#include <set>
#include <iomanip>
#include <limits> // Fix #4: Replaced magic number with professional limits
// ============================================================================
// 1. DATA CONFIGURATIONS
// ============================================================================
struct ProcessMetrics {
int pid;
std::string type; // "CREATE" or "REDIRECT"
int burst_time;
int remaining_time;
int arrival_time;
int waiting_time = 0;
int turnaround_time = 0;
};
// ============================================================================
// 2. ANALYTICS ENGINE CLASS
// ============================================================================
class AnalyticsEngine {
public:
// --- CPU SCHEDULING SIMULATION ENGINE ---
static void computeSchedulingAlgorithms(std::vector<ProcessMetrics> processes, int quantum = 2) {
if (processes.empty()) return;
std::cout << "\n=======================================================\n";
std::cout << " 📊 BACKEND OS SCHEDULER SIMULATION \n";
std::cout << "=======================================================\n";
// 1. First-Come, First-Served (FCFS)
auto fcfs = processes;
std::sort(fcfs.begin(), fcfs.end(), [](const ProcessMetrics& a, const ProcessMetrics& b) {
return a.arrival_time < b.arrival_time;
});
int clock = 0;
double fcfs_wt = 0, fcfs_tat = 0;
for (auto& p : fcfs) {
if (clock < p.arrival_time) clock = p.arrival_time;
p.waiting_time = clock - p.arrival_time;
clock += p.burst_time;
p.turnaround_time = p.waiting_time + p.burst_time;
fcfs_wt += p.waiting_time;
fcfs_tat += p.turnaround_time;
}
printMetricResult("FCFS", fcfs_wt / processes.size(), fcfs_tat / processes.size());
// 2. Shortest Job First (SJF - Non-Preemptive)
auto sjf = processes;
clock = 0;
double sjf_wt = 0, sjf_tat = 0;
std::vector<bool> finished(sjf.size(), false);
for (size_t i = 0; i < sjf.size(); ++i) {
int optimal_idx = -1;
// Fix #4: Replaced 1e9 with professional standard limits
int shortest_burst = std::numeric_limits<int>::max();
for (size_t j = 0; j < sjf.size(); ++j) {
if (!finished[j] && sjf[j].arrival_time <= clock) {
if (sjf[j].burst_time < shortest_burst) {
shortest_burst = sjf[j].burst_time;
optimal_idx = j;
}
}
}
if (optimal_idx == -1) {
clock++;
i--;
continue;
}
finished[optimal_idx] = true;
sjf[optimal_idx].waiting_time = clock - sjf[optimal_idx].arrival_time;
clock += sjf[optimal_idx].burst_time;
sjf[optimal_idx].turnaround_time = sjf[optimal_idx].waiting_time + sjf[optimal_idx].burst_time;
sjf_wt += sjf[optimal_idx].waiting_time;
sjf_tat += sjf[optimal_idx].turnaround_time;
}
printMetricResult("SJF", sjf_wt / processes.size(), sjf_tat / processes.size());
// 3. Round Robin (RR)
auto rr = processes;
// Fix #2: Explicitly ensuring remaining_time equals burst_time before starting
for (auto& p : rr) {
p.remaining_time = p.burst_time;
}
clock = 0;
double rr_wt = 0, rr_tat = 0;
std::queue<size_t> ready_q;
std::vector<bool> in_queue(rr.size(), false);
for (size_t i = 0; i < rr.size(); ++i) {
if (rr[i].arrival_time == 0) { ready_q.push(i); in_queue[i] = true; }
}
size_t done_count = 0;
while (done_count < rr.size()) {
if (ready_q.empty()) {
clock++;
for (size_t i = 0; i < rr.size(); ++i) {
if (!in_queue[i] && rr[i].arrival_time <= clock && rr[i].remaining_time > 0) {
ready_q.push(i); in_queue[i] = true;
}
}
continue;
}
size_t curr = ready_q.front(); ready_q.pop();
int slice = std::min(rr[curr].remaining_time, quantum);
rr[curr].remaining_time -= slice;
clock += slice;
for (size_t i = 0; i < rr.size(); ++i) {
if (!in_queue[i] && rr[i].arrival_time <= clock && rr[i].remaining_time > 0) {
ready_q.push(i); in_queue[i] = true;
}
}
if (rr[curr].remaining_time == 0) {
rr[curr].turnaround_time = clock - rr[curr].arrival_time;
rr[curr].waiting_time = rr[curr].turnaround_time - rr[curr].burst_time;
rr_wt += rr[curr].waiting_time;
rr_tat += rr[curr].turnaround_time;
done_count++;
} else {
ready_q.push(curr);
}
}
printMetricResult("Round Robin", rr_wt / processes.size(), rr_tat / processes.size());
// Fix #1: Safety check against division by zero for system throughput calculation
std::cout << " > System Execution Throughput : ";
if (clock > 0) {
std::cout << std::fixed << std::setprecision(4) << (double)processes.size() / clock << " requests/ms\n";
} else {
std::cout << "N/A\n";
}
}
// --- MEMORY PAGE REPLACEMENT SIMULATION ENGINE ---
static void computePageReplacements(const std::vector<std::string>& memory_logs, size_t dynamic_capacity = 3) {
if (memory_logs.empty()) return;
std::cout << "\n=======================================================\n";
std::cout << " 💾 VIRTUAL MEMORY MANAGEMENT SIMULATOR \n";
std::cout << "=======================================================\n";
// Fix #3: Safety validation handling capacity = 0 boundary errors gracefully
if (dynamic_capacity == 0) {
std::cout << " Invalid frame size: Frame allocation pool cannot be 0.\n";
std::cout << "=======================================================\n";
return;
}
std::cout << " Cache / Memory Frame Pool Capacity: " << dynamic_capacity << " Allocations\n\n";
// 1. FIFO Logic
{
std::set<std::string> pages;
std::queue<std::string> order;
int faults = 0;
for (const auto& block : memory_logs) {
if (pages.find(block) == pages.end()) {
if (pages.size() == dynamic_capacity) {
std::string front_item = order.front(); order.pop();
pages.erase(front_item);
}
pages.insert(block);
order.push(block);
faults++;
}
}
std::cout << " [FIFO Page Replacement] Total Page Faults: " << faults << "\n";
}
// 2. LRU Logic
{
std::set<std::string> pages;
std::unordered_map<std::string, int> timeline;
int faults = 0;
for (int i = 0; i < (int)memory_logs.size(); i++) {
if (pages.find(memory_logs[i]) == pages.end()) {
if (pages.size() == dynamic_capacity) {
// Fix #4: Replaced magic number with standard limits
int oldest_use = std::numeric_limits<int>::max();
std::string target_eviction;
for (auto cache_item : pages) {
if (timeline[cache_item] < oldest_use) {
oldest_use = timeline[cache_item];
target_eviction = cache_item;
}
}
pages.erase(target_eviction);
}
pages.insert(memory_logs[i]);
faults++;
}
timeline[memory_logs[i]] = i;
}
std::cout << " [LRU Page Replacement] Total Page Faults: " << faults << "\n";
}
// 3. Optimal Page Replacement Logic
{
std::vector<std::string> frames;
int faults = 0;
for (size_t i = 0; i < memory_logs.size(); ++i) {
auto it = std::find(frames.begin(), frames.end(), memory_logs[i]);
if (it == frames.end()) {
if (frames.size() < dynamic_capacity) {
frames.push_back(memory_logs[i]);
} else {
int farthest = -1, select_idx = -1;
for (size_t j = 0; j < frames.size(); ++j) {
size_t next_use = i + 1;
for (; next_use < memory_logs.size(); ++next_use) {
if (memory_logs[next_use] == frames[j]) break;
}
if ((int)next_use > farthest) {
farthest = (int)next_use;
select_idx = (int)j;
}
}
frames[select_idx] = memory_logs[i];
}
faults++;
}
}
std::cout << " [Optimal Replacement] Total Page Faults: " << faults << "\n";
}
std::cout << "=======================================================\n";
}
private:
static void printMetricResult(const std::string& algo, double avg_wt, double avg_tat) {
std::cout << " [" << std::left << std::setw(12) << algo << "] -> Avg Waiting: "
<< std::fixed << std::setprecision(2) << std::setw(6) << avg_wt
<< " ms | Avg Turnaround: " << avg_tat << " ms\n";
}
};
#endif // ANALYTICS_ENGINE_H