-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathdemo.html
592 lines (518 loc) · 18.5 KB
/
demo.html
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
<html>
<head>
<script src="https://www.gstatic.com/charts/loader.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.2.9/ace.js"></script>
<script src="lfs.js"></script>
<script src="lfs_js.js"></script>
<script src="lfs_js_bd.js"></script>
<script>
google.charts.load('current', {'packages':['corechart']});
google.charts.setOnLoadCallback(init);
// config settings
// capitalized so they can be thought of as "constants" in user scripts
// but what are constants really
var ERASE_CYCLES
var BLOCK_SIZE
var BLOCK_COUNT
var STATIC_FILE_COUNT
var STATIC_FILE_SIZE
var DYNAMIC_FILE_COUNT
var DYNAMIC_FILE_SIZE
var SIM_DELAY
// bd and lfs
var lfs
var bd
// stats
var reads
var progs
var erases
var readops
var progops
var eraseops
var boots
// charts
var erasedata, eraseoptions, erasechart
var useddata, usedoptions, usedchart
var opdata, opoptions, opchart
// stats
var bootsstat
var readsstat
var progsstat
var erasesstat
var wearstat
// inputs
var blocksizeinput
var storagesizeinput
var staticfilescountinput
var staticfilessizeinput
var dynamicfilescountinput
var dynamicfilessizeinput
var erasecyclesinput
var simspeedinput
// editors
var initeditor
var stepeditor
// functions below
function init() {
// get DOM elements
bootsstat = document.getElementById('boots')
readsstat = document.getElementById('reads')
progsstat = document.getElementById('programs')
erasesstat = document.getElementById('erases')
wearstat = document.getElementById('wear')
blocksizeinput = document.getElementById('block-size')
storagesizeinput = document.getElementById('storage-size')
staticfilescountinput = document.getElementById('static-files-count')
staticfilessizeinput = document.getElementById('static-files-size')
dynamicfilescountinput = document.getElementById('dynamic-files-count')
dynamicfilessizeinput = document.getElementById('dynamic-files-size')
erasecyclesinput = document.getElementById('erase-cycles')
simspeedinput = document.getElementById('sim-speed')
document.getElementById('apply').addEventListener('click', apply)
document.getElementById('reset').addEventListener('click', reset)
document.getElementById('step').addEventListener('click', step)
document.getElementById('simulate').addEventListener('click', simulate)
// setup googly charts
erasedata = new google.visualization.DataTable();
erasedata.addColumn('number', 'block');
erasedata.addColumn('number', 'wear');
eraseoptions = {
title: name,
colors: ['#dd8330'],
vAxis: {
title: '% of wear',
textPosition: 'none',
viewWindowMode: 'explicit',
viewWindow: {min: 0, max: 100},
gridlines: {color: '#ccc', count: 4}
},
hAxis: {
gridlines: {color: '#ccc'}
},
legend: {position: 'none'},
enableInteractivity: false,
chartArea: {left:25, top:0, width:700, height:100}
}
erasechart = new google.visualization.SteppedAreaChart(
document.getElementById('erase-chart'));
useddata = new google.visualization.DataTable();
useddata.addColumn('number', 'block');
useddata.addColumn('number', 'wear');
usedoptions = {
title: name,
colors: ['#2e4895'],
vAxis: {
title: 'in use',
textPosition: 'none',
viewWindowMode: 'explicit',
viewWindow: {min: 0, max: 1},
gridlines: {color: '#ccc', count: 3}
},
hAxis: {
gridlines: {color: '#ccc'}
},
legend: {position: 'none'},
enableInteractivity: false,
chartArea: {left:25, top:0, width:700, height:50}
}
usedchart = new google.visualization.SteppedAreaChart(
document.getElementById('used-chart'));
opdata = new google.visualization.DataTable();
opdata.addColumn('number', 'block');
opdata.addColumn('number', 'read');
opdata.addColumn('number', 'prog');
opdata.addColumn('number', 'erase');
opoptions = {
title: name,
colors: ['#25a93c', '#2e4895', '#dd8330'],
vAxis: {
title: 'bd ops',
textPosition: 'none',
viewWindowMode: 'explicit',
viewWindow: {min: 0, max: 1},
gridlines: {color: '#ccc', count: 3}
},
hAxis: {
title: 'storage block address',
gridlines: {color: '#ccc'}
},
legend: {position: 'none'},
enableInteractivity: false,
chartArea: {left:25, top:0, width:700, height:50}
}
opchart = new google.visualization.SteppedAreaChart(
document.getElementById('op-chart'));
// setup Ace editor
initeditor = ace.edit("init-editor")
initeditor.setTheme("ace/theme/tomorrow_night_eighties")
initeditor.getSession().setMode("ace/mode/javascript")
initeditor.getSession().setUseWorker(false)
stepeditor = ace.edit("step-editor")
stepeditor.setTheme("ace/theme/tomorrow_night_eighties")
stepeditor.getSession().setMode("ace/mode/javascript")
stepeditor.getSession().setUseWorker(false)
// cascade to reset and apply
reset()
}
var redrawinflight = false
var allowpending = false
var pendingredraw = false
function redraw() {
// run in animation frame to avoid melting
if (redrawinflight) {
if (allowpending) {
pendingredraw = true
}
return
}
redrawinflight = true
allowpending = false
pendingredraw = false
requestAnimationFrame(function() {
var start = performance.now()
// update charts
for (var i = 0; i < BLOCK_COUNT; i++) {
erasedata.setCell(1+i, 1, erases[i] || 0);
}
erasechart.draw(erasedata, eraseoptions)
for (var i = 0; i < BLOCK_COUNT; i++) {
opdata.setCell(1+i, 1+0, readops[i]?1:0);
opdata.setCell(1+i, 1+1, progops[i]?1:0);
opdata.setCell(1+i, 1+2, eraseops[i]?1:0);
reads += readops[i]?1:0
progs += progops[i]?1:0
}
opchart.draw(opdata, opoptions)
for (var i = 0; i < BLOCK_COUNT; i++) {
useddata.setCell(1+i, 1, 0);
}
var err = lfs.mount()
if (err >= 0) {
lfs.traverse(function(block) {
if (block < BLOCK_COUNT) {
useddata.setCell(1+block, 1, 1);
}
})
lfs.unmount()
}
usedchart.draw(useddata, usedoptions)
// update stats
bootsstat.innerHTML = boots
readsstat.innerHTML = reads
progsstat.innerHTML = progs
var e = 0
for (var i = 0; i < erases.length; i++) {
e += erases[i] || 0
}
erasesstat.innerHTML = e
var w = e / (BLOCK_COUNT*ERASE_CYCLES)
wearstat.innerHTML = (w*100).toFixed(1) + "%"
// clear frame-limited stats
readops = []
progops = []
eraseops = []
// give us some breathing room
var time = performance.now() - start
allowpending = true
setTimeout(function() {
redrawinflight = false
if (pendingredraw) {
redraw()
}
}, time)
})
}
function onread(block) {
readops[block] = (readops[block] || 0) + 1
redraw()
}
function onprog(block) {
if (erases[block] > ERASE_CYCLES) {
redraw()
return false
}
progops[block] = (progops[block] || 0) + 1
redraw()
}
function onerase(block) {
if (erases[block] > ERASE_CYCLES) {
redraw()
return false
}
erases[block] = (erases[block] || 0) + 1
eraseops[block] = (eraseops[block] || 0) + 1
redraw()
}
var finit
var fstep
function apply() {
simulating = false
// get config settings
BLOCK_SIZE = eval(blocksizeinput.value)
BLOCK_COUNT = ~~(eval(storagesizeinput.value) / BLOCK_SIZE)
STATIC_FILE_COUNT = eval(staticfilescountinput.value)
STATIC_FILE_SIZE = eval(staticfilessizeinput.value)
DYNAMIC_FILE_COUNT = eval(dynamicfilescountinput.value)
DYNAMIC_FILE_SIZE = eval(dynamicfilessizeinput.value)
ERASE_CYCLES = eval(erasecyclesinput.value)
SIM_DELAY = 1000/eval(simspeedinput.value)
var gridlines = BLOCK_COUNT;
var multiplier = 1
while (gridlines > 64) {
gridlines /= 2
multiplier *= 2
}
var ticks = []
for (var i = 0; i <= gridlines; i++) {
ticks.push(i*multiplier)
}
// setup block devices and lfs
bd = new BD(1, 1, BLOCK_SIZE, BLOCK_SIZE*BLOCK_COUNT)
bd.onread = onread
bd.onprog = onprog
bd.onerase = onerase
lfs = new LFS(bd)
// rejigger charts
if (erasedata.getNumberOfRows()-1 > BLOCK_COUNT) {
erasedata.removeRows(1+BLOCK_COUNT,
erasedata.getNumberOfRows()-1-BLOCK_COUNT)
}
for (var i = erasedata.getNumberOfRows(); i <= BLOCK_COUNT; i++) {
erasedata.addRow([i, 0])
}
eraseoptions.hAxis.ticks = ticks
eraseoptions.vAxis.viewWindow.max = ERASE_CYCLES
if (useddata.getNumberOfRows()-1 > BLOCK_COUNT) {
useddata.removeRows(1+BLOCK_COUNT,
useddata.getNumberOfRows()-1-BLOCK_COUNT)
}
for (var i = useddata.getNumberOfRows(); i <= BLOCK_COUNT; i++) {
useddata.addRow([i, 0])
}
usedoptions.hAxis.ticks = ticks
if (opdata.getNumberOfRows()-1 > BLOCK_COUNT) {
opdata.removeRows(1+BLOCK_COUNT,
opdata.getNumberOfRows()-1-BLOCK_COUNT)
}
for (var i = opdata.getNumberOfRows(); i <= BLOCK_COUNT; i++) {
opdata.addRow([i, 0, 0, 0])
}
opoptions.hAxis.ticks = ticks
// grab user scripts
finit = Function(initeditor.getValue())
fstep = Function(stepeditor.getValue())
// setup filesystem and storage
var err = finit()
boots += 1
if (err) {
console.log('error:', err)
}
// trigger first draw
redraw()
}
function reset() {
simulating = false
// set things to zero
erases = []
reads = 0
progs = 0
readops = []
progops = []
eraseops = []
boots = 0
// pass on to apply
apply()
// trigger redraw
redraw()
}
function step() {
simulating = false
var err = fstep()
boots += 1
if (err < 0) {
console.log('error:', err)
}
}
var simulating = false
function simulate() {
if (simulating) {
simulating = false
return
}
function sim() {
if (!simulating) {
return
}
var err = fstep()
boots += 1
if (err < 0) {
console.log('error:', err)
simulating = false
}
setTimeout(sim, SIM_DELAY)
}
simulating = true
setTimeout(sim, SIM_DELAY)
}
</script>
<style>
body {
width: 900px;
margin-left: auto;
margin-right: auto;
}
body, table {
font-family: Arial;
font-size: 12px;
}
</style>
</head>
<body>
<div style="margin-top:30px; width:850px; margin-left:2px">
<b>littlefs simulator</b><br>
This page contains a fully functional simulation of the
<a href="https://github.com/geky/littlefs">little filesystem</a>.
littlefs is a small filesystem designed for resource-constrained
microcontrollers, which are usually paired with small flash chips with
limited erase-cycles. This simulation renders the wear on a simulated
flash chip to demonstrate the dynamic wear-leveling algorithm used in
littlefs. As flash blocks wear out, the littlefs is able to move files
onto unused blocks to extend the lifetime of the storage.<br><br>
This simulation is built using
<a href="https://github.com/kripken/emscripten">emscripten</a>,
a backend for the LLVM that can compile C to javscript. The result is a
fully functional javascript API for littlefs. The charts are provided by
<a href="https://developers.google.com/chart">Google Charts</a>
and editor provided by the
<a href="https://ace.c9.io">Ace code editor</a>.
The full implementation can be found
<a href="https://github.com/geky/littlefs-js">here</a>,
on GitHub where it is hosted.
</div>
<div style="margin-top:60px">
<div id="littlefs-stats" style="float:left; width:125px; margin-top:-5px; margin-left:2px">
<b>littlefs</b><br>
boots: <span id="boots">0</span><br>
reads: <span id="reads">0</span><br>
programs: <span id="programs">0</span><br>
erases: <span id="erases">0</span><br>
wear: <span id="wear">0%</span><br>
</div>
<div id="charts" style="float:left">
<div id="erase-chart" style="width:725px; height:130px"></div>
<div id="used-chart" style="width:725px; height:80px"></div>
<div id="op-chart" style="width:725px; height:100px"></div>
</div>
<div style="clear:both"></div>
</div>
<div style="margin-top:15px">
<table style="float:left">
<tr>
<td><label>block size:</label></td>
<td><input id="block-size" type="text" size="6" value="512"> bytes</td>
</tr>
<tr>
<td><label>storage size:</label></td>
<td><input id="storage-size" type="text" size="6" value="64*512"> bytes</td>
</tr>
</table>
<table style="float:left; margin-left:15px">
<tr>
<td><label>static files:</label></td>
<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>
</tr>
<tr>
<td><label>dynamic files:</label></td>
<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>
</tr>
</table>
<table style="float:left; margin-left:15px; margin-right:15px">
<tr>
<td><label>erase cycles:</label></td>
<td><input id="erase-cycles" type="text" size=6" value="100"></td>
</tr>
<tr>
<td><label>simulation speed:</label></td>
<td><input id="sim-speed" type="text" size=6" value="60"> hz</td>
</tr>
</table>
<div style="float:left; transform:translateY(50%); margin-bottom:15px">
<button id="apply">apply</button>
<button id="reset">reset</button>
<button id="step">step</button>
<button id="simulate">simulate</button>
</div>
<div style="clear:both"></div>
</div>
<div style="margin-top:30px; margin-left:2px">
<div>Operations on initialization:</div>
<div id="init-editor" style="width:850px; height:500px">
// format and mount
var err = lfs.format()
if (err) {
return err
}
var err = lfs.mount()
if (err) {
return err
}
// create static files
for (var i = 0; i < STATIC_FILE_COUNT; i++) {
var f = lfs.open('static' + i, ['wronly', 'creat'])
if (f < 0) {
return err
}
for (var j = 0; j < STATIC_FILE_SIZE; j += 16) {
var err = f.write('0123456789abcdef')
if (err < 0) {
return err
}
}
var err = f.close()
if (err) {
return err
}
}
// unmount
var err = lfs.unmount()
if (err) {
return err
}
return 0
</div>
</div>
<div style="margin-top:15px; margin-left:2px">
Operations on boot:
<div id="step-editor" style="width:850px; height:500px">
// mount
var err = lfs.mount()
if (err) {
return err
}
// rebuild dynamic files
for (var i = 0; i < DYNAMIC_FILE_COUNT; i++) {
var f = lfs.open('dynamic' + i, ['wronly', 'creat'])
if (f < 0) {
return err;
}
for (var j = 0; j < DYNAMIC_FILE_SIZE; j += 16) {
var err = f.write('0123456789abcdef')
if (err < 0) {
return err
}
}
var err = f.close()
if (err < 0) {
return err
}
}
// unmount
var err = lfs.unmount()
if (err) {
return err
}
return 0
</div>
</div>
</body>
</html>