Skip to content

Commit 4ef57f6

Browse files
committed
Penalized the drawing loop when it behaves badly, now breathing room
1 parent a515c62 commit 4ef57f6

File tree

1 file changed

+50
-7
lines changed

1 file changed

+50
-7
lines changed

demo.html

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// config settings
1313
// capitalized so they can be thought of as "constants" in user scripts
1414
// but what are constants really
15-
var ERASE_CYCLES = 100
15+
var ERASE_CYCLES
1616
var BLOCK_SIZE
1717
var BLOCK_COUNT
1818
var STATIC_FILE_COUNT
@@ -53,6 +53,7 @@
5353
var staticfilessizeinput
5454
var dynamicfilescountinput
5555
var dynamicfilessizeinput
56+
var erasecyclesinput
5657
var simspeedinput
5758

5859
// editors
@@ -74,10 +75,12 @@
7475
staticfilessizeinput = document.getElementById('static-files-size')
7576
dynamicfilescountinput = document.getElementById('dynamic-files-count')
7677
dynamicfilessizeinput = document.getElementById('dynamic-files-size')
78+
erasecyclesinput = document.getElementById('erase-cycles')
7779
simspeedinput = document.getElementById('sim-speed')
7880

7981
document.getElementById('apply').addEventListener('click', apply)
8082
document.getElementById('reset').addEventListener('click', reset)
83+
document.getElementById('step').addEventListener('click', step)
8184
document.getElementById('simulate').addEventListener('click', simulate)
8285

8386
// setup googly charts
@@ -173,14 +176,24 @@
173176
}
174177

175178
var redrawinflight = false
179+
var allowpending = false
180+
var pendingredraw = false
176181
function redraw() {
177182
// run in animation frame to avoid melting
178183
if (redrawinflight) {
184+
if (allowpending) {
185+
pendingredraw = true
186+
}
179187
return
180188
}
189+
181190
redrawinflight = true
191+
allowpending = false
192+
pendingredraw = false
182193

183194
requestAnimationFrame(function() {
195+
var start = performance.now()
196+
184197
// update charts
185198
for (var i = 0; i < BLOCK_COUNT; i++) {
186199
erasedata.setCell(1+i, 1, erases[i] || 0);
@@ -228,7 +241,16 @@
228241
readops = []
229242
progops = []
230243
eraseops = []
231-
redrawinflight = false;
244+
245+
// give us some breathing room
246+
var time = performance.now() - start
247+
allowpending = true
248+
setTimeout(function() {
249+
redrawinflight = false
250+
if (pendingredraw) {
251+
redraw()
252+
}
253+
}, time)
232254
})
233255
}
234256

@@ -272,6 +294,7 @@
272294
STATIC_FILE_SIZE = eval(staticfilessizeinput.value)
273295
DYNAMIC_FILE_COUNT = eval(dynamicfilescountinput.value)
274296
DYNAMIC_FILE_SIZE = eval(dynamicfilessizeinput.value)
297+
ERASE_CYCLES = eval(erasecyclesinput.value)
275298
SIM_DELAY = 1000/eval(simspeedinput.value)
276299

277300
var gridlines = BLOCK_COUNT;
@@ -302,6 +325,7 @@
302325
erasedata.addRow([i, 0])
303326
}
304327
eraseoptions.hAxis.ticks = ticks
328+
eraseoptions.vAxis.viewWindow.max = ERASE_CYCLES
305329

306330
if (useddata.getNumberOfRows()-1 > BLOCK_COUNT) {
307331
useddata.removeRows(1+BLOCK_COUNT,
@@ -326,8 +350,11 @@
326350
fstep = Function(stepeditor.getValue())
327351

328352
// setup filesystem and storage
329-
finit()
353+
var err = finit()
330354
boots += 1
355+
if (err) {
356+
console.log('error:', err)
357+
}
331358

332359
// trigger first draw
333360
redraw()
@@ -352,6 +379,16 @@
352379
redraw()
353380
}
354381

382+
function step() {
383+
simulating = false
384+
385+
var err = fstep()
386+
boots += 1
387+
if (err < 0) {
388+
console.log('error:', err)
389+
}
390+
}
391+
355392
var simulating = false
356393
function simulate() {
357394
if (simulating) {
@@ -367,6 +404,7 @@
367404
var err = fstep()
368405
boots += 1
369406
if (err < 0) {
407+
console.log('error:', err)
370408
simulating = false
371409
}
372410

@@ -410,24 +448,28 @@
410448
<table style="float:left">
411449
<tr>
412450
<td><label>block size:</label></td>
413-
<td><input id="block-size" type="text" size="6" value="4096"> bytes</td>
451+
<td><input id="block-size" type="text" size="6" value="512"> bytes</td>
414452
</tr>
415453
<tr>
416454
<td><label>storage size:</label></td>
417-
<td><input id="storage-size" type="text" size="6" value="64*4096"> bytes</td>
455+
<td><input id="storage-size" type="text" size="6" value="64*512"> bytes</td>
418456
</tr>
419457
</table>
420458
<table style="float:left; margin-left:15px">
421459
<tr>
422460
<td><label>static files:</label></td>
423-
<td><input id="static-files-count" type="text" size="2" value="4"> x <input id="static-files-size" type="text" size="6" value="1024"> bytes</td>
461+
<td><input id="static-files-count" type="text" size="2" value="3"> x <input id="static-files-size" type="text" size="6" value="512"> bytes</td>
424462
</tr>
425463
<tr>
426464
<td><label>dynamic files:</label></td>
427-
<td><input id="dynamic-files-count" type="text" size="2" value="4"> x <input id="dynamic-files-size" type="text" size="6" value="1024"> bytes</td>
465+
<td><input id="dynamic-files-count" type="text" size="2" value="3"> x <input id="dynamic-files-size" type="text" size="6" value="512"> bytes</td>
428466
</tr>
429467
</table>
430468
<table style="float:left; margin-left:15px">
469+
<tr>
470+
<td><label>erase cycles:</label></td>
471+
<td><input id="erase-cycles" type="text" size=6" value="100"></td>
472+
</tr>
431473
<tr>
432474
<td><label>simulation speed:</label></td>
433475
<td><input id="sim-speed" type="text" size=6" value="60"> hz</td>
@@ -436,6 +478,7 @@
436478
<div style="float:left; margin-left:15px; transform:translateY(50%)">
437479
<button id="apply">apply</button>
438480
<button id="reset">reset</button>
481+
<button id="step">step</button>
439482
<button id="simulate">simulate</button>
440483
</div>
441484
<div style="clear:both"></div>

0 commit comments

Comments
 (0)