-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathvis.html
More file actions
419 lines (394 loc) · 15.8 KB
/
Copy pathvis.html
File metadata and controls
419 lines (394 loc) · 15.8 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
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
<!DOCTYPE html>
<html lang="en">
<head>
<title>EspruinoMemView</title>
<!--https://visjs.github.io/vis-network/docs/network/-->
<script type="text/javascript" src="lib/vis-network.min.js"></script> <!-- https://visjs.github.io/vis-network/standalone/umd/vis-network.min.js -->
<script src="https://www.espruino.com/js/uart.js"></script>
<style type="text/css">
#mynetwork {
width: 100%;
height: 1000px;
border: 1px solid lightgray;
}
</style>
</head>
<body>
<p id="instructions"><b>Espruino Memory Usage Visualiser</b> <a href="https://espruino.github.io/EspruinoMemView">More Info</a>- click <button id="connect">Connect</button> and a graph of all vars is shown.</p>
<p id="diffinstructions" style="display:none">Click <button id="diff">Diff</button> to read again (<input type="checkbox" id="diffgc" checked></input>GC?), and to colour only <b>new variables</b></p>
<button id="clearpath" style="display:none;">Clear Highlighted Path</button>
<pre id="info"></pre>
<div id="mynetwork"></div>
<script type="text/javascript">
var response = []; // data from E.dumpVariables()
var headers = {}; // map of data name to CSV index - ref,size,name,links...
// for debug - list of nodes
var nodeList = [];
var edgeList = [];
// the actual network
var network;
var container = document.getElementById("mynetwork");
var infoDiv = document.getElementById("info");
var clearBtn = document.getElementById("clearpath");
clearBtn.onclick = function () {
clearPathHighlight();
};
function log(txt) {
infoDiv.innerText = txt;
}
function getTitle() {
var c, r, t;
t = document.createElement('table');
for (var i = 0; i < arguments.length; i+=2) {
var key = arguments[i];
var value = arguments[i + 1];
if (value && value != "") {
r = t.insertRow(-1);
c = r.insertCell(0);
c.innerHTML = String(key + ":");
c = r.insertCell(1);
c.innerHTML = String(value);
}
}
return t;
}
UART.debug=3;
document.getElementById("connect").onclick = function() {
document.getElementById("instructions").style = "display:none";
log("Connecting...");
UART.write("\x03", function() {
log("Reading variables...");
setTimeout(function() {
requestVariables(true/*gc*/).then(() => handleResponse());
}, 500);
});
};
document.getElementById("diff").onclick = function() {
document.getElementById("diffinstructions").style = "display:none";
let oldNodes = nodeList;
nodeList = [];
log("Reading variables...");
let gcFirst = document.getElementById("diffgc").checked;
requestVariables(gcFirst).then(response => {
handleResponse(oldNodes);
});
}
function requestVariables(gcFirst) {
// process.memory(); forces a GC, just in case we had floating vars that needed a clean up
return new Promise((resolve,reject) => {
UART.write(`\x03\x10${gcFirst?"process.memory();":""}E.dumpVariables()\n`, function(r) {
response = r.trim().split("\n").map(l=>l.trim());
headers = {};
response.shift().split(",").forEach((v,n) => headers[v]=n); // ref,size,name,links...
if (headers.ref!==0) {
log("Unexpected data received");
console.log("Invalid data : "+ JSON.stringify(r));
document.getElementById("instructions").style = "";
return reject();
}
log("Got Data ("+response.length+" items)");
resolve(response);
});
});
}
// Follow ownedBy chain from nodeId back to ROOT, return highlighted edge IDs
function findPathToRoot(nodeId) {
var edgeIds = [];
var seen = {};
seen[nodeId] = true;
while (nodeId !== 1 && nodeId !== 3) {
var node = nodeList.find(function(n) { return n.id === nodeId; });
if (!node || node.ownedBy === undefined || node.ownedBy === null) break;
var parent = node.ownedBy;
var edge = edgeList.find(function(e) {
return !e.isConnectingNode && e.from === parent && e.to === nodeId;
});
if (edge) edgeIds.push(edge.id);
if (seen[parent]) break;
seen[parent] = true;
nodeId = parent;
}
return edgeIds;
}
// Track highlighted edges and their original styles so we can reset them
var _highlightedEdges = [];
function clearPathHighlight() {
if (!_highlightedEdges.length || !network) return;
network.body.data.edges.update(_highlightedEdges.map(function(h) { return h.orig; }));
_highlightedEdges = [];
clearBtn.style.display = "none";
}
function highlightPathEdges(edgeIds) {
clearPathHighlight();
if (!edgeIds.length) return;
_highlightedEdges = edgeIds.map(function(id) {
var edge = edgeList.find(function(e) { return e.id === id; });
return {
id: id,
orig: { id: id, width: edge ? edge.width : undefined, color: edge ? edge.color : undefined, dashes: edge ? edge.dashes : undefined }
};
});
network.body.data.edges.update(edgeIds.map(function(id) {
return { id: id, width: 10, color: "#5b9bd5", dashes: true };
}));
clearBtn.style.display = "inline-block";
}
function handleResponse(oldNodes) {
nodeList = [];
edgeList = [];
response.forEach(rl=> {
var l = rl.split(",");
var id = parseInt(l[headers.ref]),
size = parseInt(l[headers.size]),
// name = l[headers.name] ? l[headers.name].replace('"', '').replace('\"','') : "",
name = l[headers.name],
flags = headers.flags ? parseInt(l[headers.flags]) : undefined;
var node = {
id:id,
label : name,
value : size,
title: getTitle("Name", name, "Size", size, "Flags", flags),
flags : flags // not used for vis, this is just for us
};
if (id==1) { node.label="ROOT"; node.color = "red"; }
if (id==3) { node.label="HIDDENROOT?"; node.color = "red"; }
if (oldNodes && oldNodes.find(n=>n.id==id))
node.color = "grey";
if (!isNaN(node.id)) {
nodeList.push(node);
}
var lastNode;
for (var i=headers["links..."];i< l.length;i++)
if (l[i]!="") {
var currNode = parseInt(l[i]);
edgeList.push({
from : id,
to : currNode,
arrows: "to"
});
if (lastNode)
edgeList.push({ // a connecting edge
from : lastNode,
to : currNode,
color : "grey",
length: 5,
isConnectingNode : true
});
lastNode = currNode;
}
});
// Create 'ownedBy' based on edgeList
log("Propagating edgess...");
edgeList.forEach(edge => {
if (edge.isConnectingNode) return;
let n = nodeList.find(n => n.id == edge.to);
if (n) n.ownedBy = edge.from;
});
// Name merging
log("Merging names...");
nodeList.forEach(node => {
if (node.label=="") return; // don't care
var linksOut = edgeList.filter(e=>e.from == node.id);
if (linksOut.length != 1) return; // only interested in single links
var linkTo = nodeList.find(n=>n.id==linksOut[0].to);
if (linkTo===undefined) {
console.log("linkTo not found for "+node.id);
return;
}
if (linkTo.label != "") return; // only interested in links to non-names
var linksInTo = edgeList.filter(e=>e.to == linkTo.id);
if (linksInTo.length != 1) return; // we only care about single point->point items
console.log("Combining name ",node," to link ",linkTo);
linkTo.label = node.label;
linkTo.value += node.value;
edgeList = edgeList.filter(e=>{
if (e.to == node.id) e.to = linkTo.id;
return e.from != node.id;
});
nodeList = nodeList.filter( n => n.id!=node.id );
});
// Property-value merging: combine property-name nodes with their value nodes
// e.g. "myObject" > Object becomes "myObject": Object
log("Merging property-value pairs...");
var merges = []; // collect candidates first to avoid mutating during iteration
nodeList.forEach(function(node) {
if (!node.label) return;
if (node.color === "red") return;
var linksOut = edgeList.filter(function(e) { return !e.isConnectingNode && e.from === node.id; });
if (linksOut.length !== 1) return;
var linkTo = nodeList.find(function(n) { return n.id === linksOut[0].to; });
if (!linkTo || !linkTo.label) return;
if (linkTo.color === "red") return;
var linksInTo = edgeList.filter(function(e) { return !e.isConnectingNode && e.to === linkTo.id; });
if (linksInTo.length !== 1) return;
merges.push({ prop: node, value: linkTo });
});
merges.forEach(function(m) {
var node = m.prop, linkTo = m.value;
// Verify both nodes still exist (earlier merges may have consumed one)
if (nodeList.indexOf(node) === -1 || nodeList.indexOf(linkTo) === -1) return;
console.log("Combining property ", node.label, " with value ", linkTo.label);
node.label = node.label + ": " + linkTo.label;
node.value += linkTo.value;
node.shape = "diamond"; // TODO: Could perhaps use different shapes per data type?
node.title = getTitle("Name", node.label, "Size", node.value, "Flags", node.flags);
edgeList = edgeList.filter(function(e) { return !(e.from === node.id && e.to === linkTo.id && !e.isConnectingNode); });
edgeList.forEach(function(e) {
if (e.from === linkTo.id) e.from = node.id;
if (e.to === linkTo.id) e.to = node.id;
});
nodeList.forEach(function(n) {
if (n.ownedBy === linkTo.id) n.ownedBy = node.id;
});
nodeList = nodeList.filter(function(n) { return n.id !== linkTo.id; });
});
// labelling
nodeList.forEach(node => {
if (node.color != "grey" && node.flags==7) {
node.color = "green";
/*edgeList.forEach(e=>{
if (e.from = node.id) ...
});*/
}
let path = [];
let parentHistory = [];
let parent = nodeList.find(n => n.id == node.ownedBy);
while (parent) {
if (parent.label=="Object" || parent.label=="ROOT") { // try and format links via objects
let n = path[0];
try { n = JSON.parse(path[0]); } catch {};
path[0] = "."+n;
if (parent.label=="ROOT") path.unshift("global");
} else if (parent.label=="Array") {
let n = path[0];
try {
let v = JSON.parse(path[0]);
if (isFinite(v)) n=v;
} catch {};
path[0] = "["+n+"]";
} else path.unshift(parent.label);
if (parentHistory.includes(parent.ownedBy)) {
path.unshift("...");
parent = undefined; // stop recursive loops
} else {
parentHistory.push(parent.ownedBy);
parent = nodeList.find(n => n.id == parent.ownedBy);
}
}
node.path = path.join("");
});
// Find and highlight circular dependency edges in orange
log("Finding circular references...");
var adj = {};
nodeList.forEach(function(n) { adj[n.id] = []; });
edgeList.forEach(function(edge) {
if (edge.isConnectingNode) return;
if (adj[edge.from]) adj[edge.from].push(edge);
});
var WHITE = 0, GRAY = 1, BLACK = 2;
var color = {};
var parent = {};
var cycleEdges = [];
nodeList.forEach(function(n) { color[n.id] = WHITE; });
function dfsCycleDetect(u) {
color[u] = GRAY;
(adj[u] || []).forEach(function(edge) {
var v = edge.to;
if (color[v] === GRAY) {
// Found a back edge — this edge completes a cycle
cycleEdges.push(edge);
// Walk parent pointers back from u to v to catch all cycle edges
var curr = u;
while (curr !== v) {
var parentEdge = parent[curr];
if (parentEdge) {
cycleEdges.push(parentEdge);
curr = parentEdge.from;
} else {
break;
}
}
}
if (color[v] === WHITE) {
parent[v] = edge;
dfsCycleDetect(v);
}
});
color[u] = BLACK;
}
Object.keys(adj).forEach(function(id) {
if (color[id] === WHITE) dfsCycleDetect(parseInt(id));
});
cycleEdges.forEach(function(edge) {
edge.color = "#f0a030";
edge.dashes = true;
edge.width = 2;
});
log(`Found ${cycleEdges.length} circular reference edge(s)`);
// Assign IDs to every edge for edge highlighting
edgeList.forEach(function(edge, i) { edge.id = i + 1; });
createNetwork(oldNodes);
}
function createNetwork(oldNodes) {
log("Creating network...");
var data = {
nodes: new vis.DataSet(nodeList),
edges: new vis.DataSet(edgeList),
};
var options = {
nodes: {
shape: "dot",
scaling : { min:16, max:48 }
},
physics: {
forceAtlas2Based: {
gravitationalConstant: -60,
centralGravity: 0.01,
springLength: 40,
springConstant: 0.6,
damping: 0.35,
},
maxVelocity: 250,
solver: "forceAtlas2Based",
timestep: 0.35,
stabilization: {
enabled:true, iterations: 250, updateInterval: 25,
},
}
};
network = new vis.Network(container, data, options);
network.on("stabilizationProgress", function (params) {
var percent = Math.round(100 * params.iterations / params.total);
infoDiv.innerText = "Calculating "+percent+"%";
});
network.once("stabilizationIterationsDone", function () {
// Switch to less crazy physics to prevent weird vibration, maybe
network.setOptions({
physics: {
forceAtlas2Based: {
gravitationalConstant: -60,
centralGravity: 0.01,
springLength: 40,
springConstant: 0.6,
damping: 0.85,
},
maxVelocity: 15,
timestep: 0.35,
stabilization: false,
}
});
infoDiv.innerText = "Ready";
if (!oldNodes) document.getElementById("diffinstructions").style = "display:inherit";
});
network.on("click", function (e) {
clearPathHighlight();
if (e.nodes.length === 1) {
highlightPathEdges(findPathToRoot(e.nodes[0]));
}
infoDiv.innerText = JSON.stringify(e.nodes.map(function(n) { return nodeList.find(function(l) { return l.id === n; }); }), null, 2);
});
log("Network created");
}
</script>
</body>
</html>