-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonitoring.cpp
More file actions
108 lines (88 loc) · 2.77 KB
/
monitoring.cpp
File metadata and controls
108 lines (88 loc) · 2.77 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
#include "monitoring.h"
Monitor::Monitor() {
startTime = endTime = 0;
capacity = 50;
bottleneck = "None";
for(int i=0;i<4;i++)
taskCounts[i] = 0;
}
// Time Complexity: O(1)
void Monitor::startTimer() {
startTime = clock();
}
// Time Complexity: O(1)
void Monitor::stopTimer() {
endTime = clock();
}
// Latency = (endTime - startTime) / CLOCKS_PER_SEC * 1000 ms
// Time Complexity: O(1)
float Monitor::getLatency() {
return (float)(endTime - startTime) / CLOCKS_PER_SEC * 1000.0f;
}
// Time Complexity: O(1)
void Monitor::setTaskCount(int q, int count) {
if(q >= 1 && q <= 4)
taskCounts[q-1] = count;
}
// Load = Number of Active Tasks / Processing Capacity
// Time Complexity: O(1)
float Monitor::getLoad(int q) {
if(q >= 1 && q <= 4)
return (float)taskCounts[q-1] / capacity;
return 0;
}
// Time Complexity: O(1) - fixed 4 queues
void Monitor::analyzeLoad() {
cout << "SYS: Load Analysis:" << endl;
string names[] = {"Q1-Routine", "Q2-Surveillance", "Q3-Emergency", "Q4-Decision"};
for(int i=0;i<4;i++) {
float load = getLoad(i+1);
cout << names[i] << ": " << taskCounts[i] << " tasks | Load=" << load;
if(load > 0.8) cout << " [OVERLOADED]";
else if(load > 0.5) cout << " [WARNING]";
else cout << " [OK]";
cout << endl;
}
}
// identifies slowest part delaying overall processing
// Time Complexity: O(1)
void Monitor::detectBottleneck() {
int maxTasks = 0;
int bottleneckQ = -1;
string names[] = {"Q1", "Q2", "Q3", "Q4"};
for(int i=0;i<4;i++) {
if(taskCounts[i] > maxTasks) {
maxTasks = taskCounts[i];
bottleneckQ = i;
}
}
if(bottleneckQ >= 0 && maxTasks > capacity / 2) {
bottleneck = names[bottleneckQ];
cout << "SYS: Bottleneck -> " << bottleneck << " (" << maxTasks << " tasks)" << endl;
} else {
cout << "SYS: No bottleneck." << endl;
}
}
// Time Complexity: O(1)
void Monitor::showSystemHealth() {
float latency = getLatency();
cout << "SYS: System Health:" << endl;
cout << "Latency: " << latency << " ms" << endl;
cout << "Bottleneck: " << bottleneck << endl;
analyzeLoad();
if(latency < 5) cout << "Status: HEALTHY" << endl;
else if(latency < 20) cout << "Status: WARNING" << endl;
else cout << "Status: CRITICAL" << endl;
}
// redistribute workload when queue overloaded
// Time Complexity: O(1)
void Monitor::optimizePerformance() {
cout << "SYS: Optimizing..." << endl;
for(int i=0;i<4;i++) {
if(getLoad(i+1) > 0.8) {
cout << "SYS: Redistributing Q" << (i+1) << endl;
taskCounts[i] = capacity / 2;
}
}
cout << "SYS: Optimization complete." << endl;
}