-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmem.html
More file actions
294 lines (251 loc) · 8.89 KB
/
Copy pathmem.html
File metadata and controls
294 lines (251 loc) · 8.89 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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>CSV with IndexedDB Persistence</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0px;
}
.flex-row {
display: flex;
align-items: center;
gap: 10px;
}
#dataCanvas {
border: 1px solid #ccc;
margin-top: 20px;
}
</style>
</head>
<body>
<div class="flex-row">
<h1>CSV IndexedDB Loader</h1>
<input type="file" id="csvFile" accept=".csv" />
<button id="clearDB">Clear DB</button>
</div>
<div>
<canvas
id="dataCanvas"
width="800"
height="400"
style="border: 1px solid #000000"
></canvas>
</div>
<script>
// ---------------------
// CSV Parser
// ---------------------
function csvToJson(csv) {
const [headerLine, ...lines] = csv.trim().split("\n");
const headers = headerLine.split(",");
return lines.map((line) => {
const values = line.split(",");
return Object.fromEntries(headers.map((h, i) => [h, values[i]]));
});
}
// ---------------------
// IndexedDB Helpers
// ---------------------
const DB_NAME = "csvDB";
const STORE_NAME = "files";
function openDB(callback) {
const request = indexedDB.open(DB_NAME, 1);
request.onupgradeneeded = (e) => {
const db = e.target.result;
if (!db.objectStoreNames.contains(STORE_NAME)) {
db.createObjectStore(STORE_NAME);
}
};
request.onsuccess = (e) => callback(e.target.result);
request.onerror = (e) => console.error("DB open error", e);
}
function saveToDB(key, value) {
openDB((db) => {
const tx = db.transaction(STORE_NAME, "readwrite");
tx.objectStore(STORE_NAME).put(value, key);
tx.oncomplete = () => console.log("Saved to DB");
});
}
function loadFromDB(key, callback) {
openDB((db) => {
const tx = db.transaction(STORE_NAME, "readonly");
const req = tx.objectStore(STORE_NAME).get(key);
req.onsuccess = (e) => callback(e.target.result);
});
}
function clearDB() {
openDB((db) => {
const tx = db.transaction(STORE_NAME, "readwrite");
tx.objectStore(STORE_NAME).clear();
tx.oncomplete = () => {
console.log("DB cleared");
};
});
}
class MemBlock {
constructor(
name,
capacityBytes,
color,
nextLevel = null,
lineSize = 64
) {
this.name = name;
this.capacityBytes = capacityBytes; // bytes
this.lineSize = lineSize; // bytes per block/line
this.color = color;
this.nextLevel = nextLevel;
this.maxLines = Math.floor(capacityBytes / lineSize);
this.cache = new Set(); // stores line IDs
this.onHit = (offs, size) => {};
this.onMiss = (offs, size) => {};
}
// Convert address range [offs..offs+size) into cache line IDs
_getLineIds(offsBytes, sizeBytes) {
let ids = [];
let start = Math.floor(offsBytes / this.lineSize);
let end = Math.floor((offsBytes + sizeBytes - 1) / this.lineSize);
for (let i = start; i <= end; i++) ids.push(i);
return ids;
}
insert(offsBytes, sizeBytes) {
const ids = this._getLineIds(offsBytes, sizeBytes);
for (let id of ids) {
if (this.cache.has(id)) {
// ✅ Cache hit
this.onHit(offsBytes, sizeBytes, this.name);
} else {
// ❌ Cache miss → fetch this *line* from lower level
if (this.nextLevel) {
const nextAddr = id * this.lineSize;
this.nextLevel.insert(nextAddr, this.lineSize);
}
this.onMiss(offsBytes, sizeBytes, this.name);
// Insert new line, evict if full
if (this.cache.size >= this.maxLines) {
const first = this.cache.values().next().value;
this.cache.delete(first);
}
this.cache.add(id);
}
}
}
}
async function render_data(data) {
// create a canvas and draw something simple
const canvas = document.getElementById("dataCanvas");
// set width to page width
canvas.width = window.innerWidth;
canvas.height = 400;
const DATA_ARR_Y_OFFSET = 0;
const DATA_ARR_HEIGHT = 40;
const MEM_H_Y_OFFSET = 50;
const ctx = canvas.getContext("2d");
let ram = new MemBlock("RAM", Infinity, "#a0c4ff", null); // practically unbounded
let l3 = new MemBlock("L3", 8 * 1024 * 1024, "#bdb2ff", ram); // 8MB
let l2 = new MemBlock("L2", 256 * 1024, "#ffc6ff", l3); // 256KB
let l1 = new MemBlock("L1", 32 * 1024, "#fffffc", l2); // 32KB
// create array bars for each cluster (the color is based on where it is in memory, default ram)
ctx.fillStyle = ram.color;
const bar_width = canvas.width;
const bar_height = DATA_ARR_HEIGHT;
ctx.fillRect(0, DATA_ARR_Y_OFFSET, bar_width, bar_height);
// write in flexbox style memory hierarchy to canvas
const hierarchy = [ram, l3, l2, l1];
const width_per_level = canvas.width / hierarchy.length;
for (let i = 0; i < hierarchy.length; i++) {
const mem = hierarchy[i];
const x = i * width_per_level;
const y = MEM_H_Y_OFFSET;
const height = canvas.height;
ctx.fillStyle = mem.color;
ctx.fillRect(x, y, width_per_level, height);
ctx.fillStyle = "#000";
ctx.font = "16px Arial";
ctx.fillText(
`${mem.name} (${(mem.size / (1024 * 1024)).toFixed(1)} MB)`,
x + 10,
y + 30
);
mem.onHit = (offs, size, name) => {
ctx.fillStyle = "green";
ctx.fillRect(x + width_per_level - 10, y, 10, 10);
};
mem.onMiss = (offs, size, name) => {
ctx.fillStyle = "red";
ctx.fillRect(x + width_per_level - 10, y + 20, 10, 10);
};
}
function clear_mem_status () {
for (let i = 0; i < hierarchy.length; i++) {
const mem = hierarchy[i];
const x = i * width_per_level;
const y = MEM_H_Y_OFFSET;
ctx.fillStyle = mem.color;
ctx.fillRect(x + width_per_level - 10, y, 10, 30);
}
}
const total_node_size = data[0].node_size;
const el_w = bar_width / total_node_size;
for (let i = 0; i < data.length; i++) {
const row = data[i];
const stride = 8 * 4 + 4 + 2 * 8; // 8 floats + 1 float + 2 doubles
// l1.insert(row.node_offset * stride, row.node_size * stride);
const x = row.node_offset * el_w;
ctx.fillStyle = "red";
ctx.fillRect(
x,
DATA_ARR_Y_OFFSET + bar_height,
row.node_size * el_w,
10
);
// // Let's access memoy sequentially
// for (let j = 0; j < row.node_size; j++) {
// const offs = (row.node_offset + j) * stride;
// l1.insert(offs, stride);
// await new Promise((r) => setTimeout(r, 100)); // yield to UI thread
// clear_mem_status();
// }
await new Promise((r) => setTimeout(r, 50)); // yield to UI thread
ctx.fillStyle = "white";
ctx.fillRect(x, DATA_ARR_Y_OFFSET + bar_height, bar_width, 10);
clear_mem_status();
}
}
// ---------------------
// UI handlers
// ---------------------
document
.getElementById("csvFile")
.addEventListener("change", function (evt) {
const file = evt.target.files[0];
if (!file) return;
const reader = new FileReader();
reader.onload = (e) => {
const csv = e.target.result;
const json = csvToJson(csv);
// save as JSON string to IndexedDB
saveToDB("csvData", JSON.stringify(json));
console.log("Stored rows:", json.length);
// render data or do something with it
render_data(json);
};
reader.readAsText(file);
});
document.getElementById("clearDB").addEventListener("click", clearDB);
// On page load → auto reload from DB
window.addEventListener("load", () => {
loadFromDB("csvData", (val) => {
if (val) {
const json = JSON.parse(val);
console.log("Loaded from DB:", json.length, "rows");
render_data(json);
}
});
});
</script>
</body>
</html>