-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisualization.html
More file actions
303 lines (275 loc) · 8.58 KB
/
visualization.html
File metadata and controls
303 lines (275 loc) · 8.58 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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>HeliOS Scheduler Visualization</title>
<style>
:root {
--primary: #3b82f6;
--secondary: #10b981;
--bg: #0f172a;
--card: #1e293b;
--text: #f8fafc;
}
body {
font-family: "Inter", system-ui, -apple-system, sans-serif;
background-color: var(--bg);
color: var(--text);
margin: 0;
padding: 2rem;
}
.container {
max-width: 1200px;
margin: 0 auto;
}
h1,
h2 {
text-align: center;
margin-bottom: 2rem;
}
.controls {
background: var(--card);
padding: 1.5rem;
border-radius: 1rem;
margin-bottom: 2rem;
display: flex;
gap: 1rem;
flex-wrap: wrap;
justify-content: center;
align-items: center;
}
button {
background: var(--primary);
color: white;
border: none;
padding: 0.75rem 1.5rem;
border-radius: 0.5rem;
cursor: pointer;
font-weight: 600;
transition: opacity 0.2s;
}
button:hover {
opacity: 0.9;
}
button.secondary {
background: var(--secondary);
}
.grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 2rem;
}
.panel {
background: var(--card);
padding: 1.5rem;
border-radius: 1rem;
}
.gantt-chart {
display: flex;
height: 60px;
background: #334155;
border-radius: 0.5rem;
overflow: hidden;
margin: 1rem 0;
position: relative;
}
.block {
height: 100%;
display: flex;
align-items: center;
justify-content: center;
font-size: 0.8rem;
font-weight: bold;
color: white;
transition: width 0.3s ease;
}
.stats {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 1rem;
margin-top: 1rem;
text-align: center;
}
.stat-box {
background: rgba(255, 255, 255, 0.1);
padding: 0.5rem;
border-radius: 0.5rem;
}
.stat-value {
font-size: 1.5rem;
font-weight: bold;
}
.stat-label {
font-size: 0.8rem;
opacity: 0.7;
}
input[type="number"] {
padding: 0.5rem;
border-radius: 0.25rem;
border: 1px solid #475569;
background: #334155;
color: white;
width: 60px;
}
</style>
</head>
<body>
<div class="container">
<h1>HeliOS Scheduler Visualization</h1>
<div class="controls">
<div>
<label>Task 1 Burst:</label>
<input type="number" id="t1" value="10" min="1" max="50" />
</div>
<div>
<label>Task 2 Burst:</label>
<input type="number" id="t2" value="20" min="1" max="50" />
</div>
<div>
<label>Task 3 Burst:</label>
<input type="number" id="t3" value="15" min="1" max="50" />
</div>
<div>
<label>Quantum (RR):</label>
<input type="number" id="quantum" value="5" min="1" max="20" />
</div>
<button onclick="simulate()">Run Simulation</button>
</div>
<div class="grid">
<div class="panel">
<h2>Round Robin (Preemptive)</h2>
<div id="rr-chart" class="gantt-chart"></div>
<div class="stats">
<div class="stat-box">
<div class="stat-value" id="rr-wait">0</div>
<div class="stat-label">Avg Wait Time</div>
</div>
<div class="stat-box">
<div class="stat-value" id="rr-turn">0</div>
<div class="stat-label">Avg Turnaround</div>
</div>
<div class="stat-box">
<div class="stat-value" id="rr-switches">0</div>
<div class="stat-label">Context Switches</div>
</div>
</div>
</div>
<div class="panel">
<h2>Shortest Job First (Non-Preemptive)</h2>
<div id="sjf-chart" class="gantt-chart"></div>
<div class="stats">
<div class="stat-box">
<div class="stat-value" id="sjf-wait">0</div>
<div class="stat-label">Avg Wait Time</div>
</div>
<div class="stat-box">
<div class="stat-value" id="sjf-turn">0</div>
<div class="stat-label">Avg Turnaround</div>
</div>
<div class="stat-box">
<div class="stat-value" id="sjf-switches">0</div>
<div class="stat-label">Context Switches</div>
</div>
</div>
</div>
</div>
</div>
<script>
const colors = ["#ef4444", "#f59e0b", "#10b981", "#3b82f6", "#8b5cf6"];
function simulate() {
const tasks = [
{ id: 1, burst: parseInt(document.getElementById("t1").value) },
{ id: 2, burst: parseInt(document.getElementById("t2").value) },
{ id: 3, burst: parseInt(document.getElementById("t3").value) },
];
const quantum = parseInt(document.getElementById("quantum").value);
runRR(JSON.parse(JSON.stringify(tasks)), quantum);
runSJF(JSON.parse(JSON.stringify(tasks)));
}
function runRR(tasks, quantum) {
let time = 0;
let queue = [...tasks];
let timeline = [];
let completed = [];
let switches = 0;
// Initialize state
tasks.forEach((t) => {
t.remaining = t.burst;
t.arrival = 0;
t.wait = 0;
});
let lastTask = -1;
while (queue.length > 0) {
let task = queue.shift();
if (lastTask !== -1 && lastTask !== task.id) switches++;
lastTask = task.id;
let runTime = Math.min(task.remaining, quantum);
timeline.push({ id: task.id, duration: runTime, start: time });
time += runTime;
task.remaining -= runTime;
// Update wait time for others in queue
queue.forEach((t) => (t.wait += runTime));
if (task.remaining > 0) {
queue.push(task);
} else {
task.finish = time;
task.turnaround = task.finish - task.arrival;
// Wait time calculation in RR is complex iteratively,
// simpler: Turnaround - Burst
task.waitTotal = task.turnaround - task.burst;
completed.push(task);
}
}
renderChart("rr-chart", timeline, time);
updateStats("rr", completed, switches);
}
function runSJF(tasks) {
let time = 0;
let available = [...tasks];
let timeline = [];
let completed = [];
let switches = 0;
// Sort by burst time
available.sort((a, b) => a.burst - b.burst);
available.forEach((task, index) => {
if (index > 0) switches++;
timeline.push({ id: task.id, duration: task.burst, start: time });
// Wait time is current time (since all arrive at 0)
task.waitTotal = time;
time += task.burst;
task.turnaround = time;
completed.push(task);
});
renderChart("sjf-chart", timeline, time);
updateStats("sjf", completed, switches);
}
function renderChart(elementId, timeline, totalTime) {
const container = document.getElementById(elementId);
container.innerHTML = "";
timeline.forEach((block) => {
const div = document.createElement("div");
div.className = "block";
div.style.width = (block.duration / totalTime) * 100 + "%";
div.style.backgroundColor = colors[(block.id - 1) % colors.length];
div.textContent = `P${block.id}`;
div.title = `P${block.id}: ${block.duration} ticks`;
container.appendChild(div);
});
}
function updateStats(prefix, tasks, switches) {
const avgWait =
tasks.reduce((acc, t) => acc + t.waitTotal, 0) / tasks.length;
const avgTurn =
tasks.reduce((acc, t) => acc + t.turnaround, 0) / tasks.length;
document.getElementById(`${prefix}-wait`).textContent =
avgWait.toFixed(1);
document.getElementById(`${prefix}-turn`).textContent =
avgTurn.toFixed(1);
document.getElementById(`${prefix}-switches`).textContent = switches;
}
// Initial run
simulate();
</script>
</body>
</html>