Debian Codegraph @

contrib/hwloc-ps.www/assets/script.js

raw
1 /*
2 * SPDX-License-Identifier: BSD-3-Clause
3 * Copyright © 2019 Inria. All rights reserved.
4 * See COPYING in top-level directory.
5 */
6
7 function start(svgObject){
8 const texts = svgObject.getElementsByTagName('text')
9 for (let text of texts) {
10 text.setAttribute("style", text.getAttribute("style") + "; pointer-events:none")
11 }
12 }
13
14 async function createProcesses(svgdoc, req, json = null){
15 await clear(svgdoc)
16 clearTable()
17 if(!json){
18 response = await fetch("/" + req)
19 json = await response.json()
20 }
21 json.forEach(proc => {
22 displayProcess(svgdoc, proc, req)
23 })
24 createTooltip(svgdoc)
25 if (/^proc\/\d+$/.test(req)) {
26 //only show if single PID
27 displayInfo(svgdoc, json)
28 //threads auto-enabled for single PID
29 document.getElementById('threads').checked = true
30 }
31 }
32
33 async function displayProcess(svgdoc, proc, req = null){
34 if(!proc.object)
35 return
36 //create table except when looking at a single process
37 if(!/proc\/\d+$/.test(req))
38 fillTable(svgdoc, proc)
39 bullet(svgdoc, proc, "")
40 text(svgdoc, proc, "")
41 if(proc.threads){
42 proc.threads.forEach(async thread =>{
43 if(thread.object){
44 bullet(svgdoc, thread, "thread")
45 text(svgdoc, thread, "thread")
46 }
47 })
48 }
49 }
50
51 function bullet(svgdoc, obj, objType){
52 let bulletCircle = svgdoc.getElementById("bulletCircle" + objType + "_" + obj.object.replace(':','_'))
53 //if a bullet doesn't already exist, if it does, just add the process id to the bullet attributes
54 if(!bulletCircle){
55 let id = obj.object.replace(':','_')+"_rect"
56 let rect = svgdoc.getElementById(id)
57
58 bulletCircle = svgdoc.createElementNS("http://www.w3.org/2000/svg","rect")
59
60 //difference thread and process
61 if(objType === "thread"){
62 bulletCircle.setAttribute("thread", true)
63 bulletCircle.setAttribute("height", "15")
64 bulletCircle.setAttribute("fill", "#7cacf9")
65 bulletCircle.setAttribute("y", rect.y.animVal.value + 12)
66 }else{
67 bulletCircle.setAttribute("y", rect.y.animVal.value - 5)
68 bulletCircle.setAttribute("height", "15")
69 bulletCircle.setAttribute("fill", "#FFFFFF")
70 }
71
72 //set the bullet in the middle if the process is on machine_O
73 if(id == "Machine_0_rect"){
74 const svg = svgdoc.getElementsByTagName('svg')[0]
75 bulletCircle.setAttribute("x", svg.width.baseVal.value / 2 - 10)
76 bulletCircle.setAttribute("y", 5)
77 if(objType === "thread"){
78 bulletCircle.setAttribute("x", svg.width.baseVal.value / 2 + 45)
79 bulletCircle.setAttribute("y", 5)
80 }
81 }else{
82 bulletCircle.setAttribute("x", rect.x.animVal.value-5)
83 }
84 bulletCircle.setAttribute("rx", 10)
85 bulletCircle.setAttribute("ry", 10)
86 bulletCircle.setAttribute("circle", true)
87 bulletCircle.setAttribute("stroke-width", "1")
88 bulletCircle.setAttribute("stroke", "#000000")
89 bulletCircle.classList.add("tooltip-trigger")
90 rect.parentNode.appendChild(bulletCircle)
91 bulletCircle.id = "bulletCircle" + objType + "_" + obj.object.replace(':','_')
92 let secondId
93 if(objType === "thread")
94 secondId = "thread id : " + obj.PID
95 else
96 secondId = "process id : " + obj.PID
97 bulletCircle.setAttribute('secondId', secondId)
98 }else {
99 let secondId = bulletCircle.getAttribute('secondId')
100 if(objType === "thread")
101 secondId += ';' + "thread id : " + obj.PID
102 else
103 secondId += ';' + "process id : " + obj.PID
104 bulletCircle.setAttribute('secondId', secondId)
105 }
106 }
107
108 function text(svgdoc, obj, objType){
109 let id = obj.object.replace(':','_')+"_rect"
110 let rect = svgdoc.getElementById(id)
111 let bulletText = svgdoc.createElementNS("http://www.w3.org/2000/svg","text")
112 let bulletCircle = svgdoc.getElementById("bulletCircle" + objType + "_" + obj.object.replace(':','_'))
113
114 //if a bullet doesn't already exist, if it does, increment the text
115 if(!svgdoc.getElementById("bulletText" + objType + "_" + obj.object.replace(':','_'))){
116
117 bulletText.setAttribute("circle", true)
118
119 //set the text in the middle if the process is on machine_O
120 if(id == "Machine_0_rect"){
121 const svg = svgdoc.getElementsByTagName('svg')[0]
122 bulletText.setAttribute("x", svg.width.baseVal.value / 2 - 3)
123 bulletText.setAttribute("y", 15)
124 if(objType === "thread"){
125 bulletText.setAttribute("x", svg.width.baseVal.value / 2 + 52)
126 bulletText.setAttribute("y", 15)
127 }
128 }else{
129 bulletText.setAttribute("x", rect.x.animVal.value+2)
130 bulletText.setAttribute("y", rect.y.animVal.value+6)
131 }
132
133 //difference thread and process
134 if(objType === "thread" && id != "Machine_0_rect"){
135 bulletText.setAttribute("thread", true)
136 bulletText.setAttribute("y", rect.y.animVal.value+22)
137 }
138
139 bulletText.setAttribute("style", bulletText.getAttribute("style") + "; pointer-events:none")
140 bulletText.setAttribute("font-size", "10px")
141 updateBulletText(bulletText, obj.PID, bulletCircle)
142 bulletText.id = "bulletText" + objType + "_" + obj.object.replace(':','_')
143 rect.parentNode.appendChild(bulletText)
144 }else{
145 let text = svgdoc.getElementById("bulletText" + objType + "_" + obj.object.replace(':','_'))
146 updateBulletText(text, findPid(text, '+'), bulletCircle)
147
148 //set the text in the middle if the process is on machine_O
149 if(id == "Machine_0_rect"){
150 if (text.textContent == 10)
151 text.setAttribute("x", parseInt(text.getAttribute('x')) - 3)
152 else if(text.textContent == 100)
153 text.setAttribute("x", parseInt(text.getAttribute('x')) - 4)
154 return
155 }
156 }
157 }
158
159 async function updateBulletText(text, pid, bulletCircle){
160 text.textContent = await pid
161 let length = text.getComputedTextLength()
162 bulletCircle.setAttribute("width", length + 15)
163 }
164
165 function findPid(text, op){
166 let nb
167 if(!text.getAttribute('nb')){
168 nb = "[2]"
169 text.setAttribute('nb', nb)
170 }else{
171 nb = text.getAttribute('nb')
172 nb = nb.replace('[','')
173 nb = nb.replace(']','')
174 if(op == '+')
175 nb = parseInt(nb) + 1
176 else{
177 nb = parseInt(nb) - 1
178 }
179 nb = '[' + nb + ']'
180 text.setAttribute('nb', nb)
181 }
182 return nb
183 }
184
185 function clear(svgdoc){
186 document.getElementById('searchId').value = ''
187 const info = document.getElementById('info')
188 info.classList = 'hidden'
189 const infoNotFound = document.getElementById('info-not-found')
190 infoNotFound.classList = 'hidden'
191 svgElements = svgdoc.getElementsByTagName('svg')[0].children
192 let i = 0;
193 for(element of svgElements){
194 if(element.id.includes('bulletCircle'))
195 i += 2
196 }
197 if(i == 0)
198 return
199 for(i ; i >= 0 ; i--){
200 svgElements[svgElements.length - 1].remove()
201 }
202 }
203
204 function createTooltip(svgdoc){
205 if(svgdoc.getElementById('tooltip'))
206 svgdoc.getElementById('tooltip').remove()
207 //Create tooltip to display process id
208 const g = svgdoc.createElementNS("http://www.w3.org/2000/svg","g")
209 const blackRect = svgdoc.createElementNS("http://www.w3.org/2000/svg",'rect')
210 const whiteRect = svgdoc.createElementNS("http://www.w3.org/2000/svg",'rect')
211 const tooltip = svgdoc.createElementNS("http://www.w3.org/2000/svg",'text')
212
213 blackRect.setAttribute('fill','black')
214 blackRect.setAttribute('opacity','0.4')
215 blackRect.setAttribute('rx','2')
216 blackRect.setAttribute('ry','2')
217 blackRect.setAttribute('x','2')
218 blackRect.setAttribute('y','-8')
219 blackRect.setAttribute('height','11')
220 blackRect.setAttribute('opacity','0.4')
221 whiteRect.setAttribute('y','-10')
222 whiteRect.setAttribute('fill','white')
223 whiteRect.setAttribute('rx','2')
224 whiteRect.setAttribute('ry','2')
225 whiteRect.setAttribute('height','11')
226 tooltip.setAttribute('x','4')
227 tooltip.setAttribute('y','6')
228
229 g.setAttribute('visibility', 'hidden')
230 g.id = "tooltip"
231
232 g.appendChild(blackRect)
233 g.appendChild(whiteRect)
234 g.appendChild(tooltip)
235 svgdoc.getElementsByTagName('svg')[0].appendChild(g)
236
237 let triggers = svgdoc.getElementsByClassName('tooltip-trigger')
238 for (let i = 0; i < triggers.length; i++) {
239 triggers[i].onmousemove = showTooltip
240 triggers[i].onmouseout = hideTooltip
241 triggers[i].onclick = async function(){
242 //on click : display process info for the first process in the bullet
243 procs = triggers[i].getAttribute('secondId').split(";")
244 let data = new Array()
245 response = await fetch("/json")
246 json = await response.json()
247 await procs.forEach(proc => {
248 json.forEach(jsonProcess => {
249 if(proc.substring(13) == jsonProcess.PID)
250 data.push(jsonProcess)
251 })
252 })
253 let req = data.length == 1 ? "proc/" + data[0].PID : null
254 createProcesses(svgdoc, req, data)
255 }
256 }
257
258 function showTooltip(evt) {
259 const tooltip = svgdoc.getElementById('tooltip')
260 let tooltipText = tooltip.getElementsByTagName('text')[0]
261 let tooltipRects = tooltip.getElementsByTagName('rect')
262
263 //clear tooltip
264 while(tooltipText.firstChild){
265 tooltipText.removeChild(tooltipText.firstChild)
266 for (let i = 0; i < tooltipRects.length; i++) {
267 tooltipRects[i].setAttribute("height", parseInt(tooltipRects[i].getAttribute("height")) - 15)
268 }
269 }
270
271 //display all processes id in the bullet
272 let text = evt.target.getAttribute('secondId')
273 let tspan
274 text = text.split(';')
275 let length = 0
276 for (str of text ) {
277 //create the text element
278 tspan = svgdoc.createElementNS("http://www.w3.org/2000/svg",'tspan')
279 if(text.indexOf(str) !== 0)
280 tspan.setAttribute('dy', '1em')
281 tspan.setAttribute('x', '0')
282 tspan.textContent = str
283 tooltipText.appendChild(tspan)
284 //find the length of the longest text
285 if(tspan.getComputedTextLength() > length)
286 length = tspan.getComputedTextLength()
287 //ajust the height of the tooltip element ( and its shadow )
288 for (let i = 0; i < tooltipRects.length; i++) {
289 tooltipRects[i].setAttribute("height", parseInt(tooltipRects[i].getAttribute("height")) + 15)
290 }
291 //display "..." if there is more than 5 processes
292 if(text.indexOf(str) === 5){
293 tspan.textContent = '\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0...'
294 tooltipText.appendChild(tspan)
295 break
296 }
297 }
298 //set the tooltip ( and its shadow ) width according to the longest element
299 for (let i = 0; i < tooltipRects.length; i++) {
300 tooltipRects[i].setAttributeNS(null, "width", length + 8)
301 }
302 //display the tooltip at the mouse coordonates
303 let CTM = svgdoc.getElementsByTagName('svg')[0].getScreenCTM()
304 let x = (evt.clientX - CTM.e + 6) / CTM.a;
305 let y = (evt.clientY - CTM.f + 20) / CTM.d;
306 tooltip.setAttributeNS(null, "transform", "translate(" + x + " " + y + ")");
307 tooltip.setAttributeNS(null, "visibility", "visible")
308 }
309
310 function hideTooltip() {
311 const tooltip = svgdoc.getElementById('tooltip')
312 tooltip.setAttributeNS(null, "visibility", "hidden")
313 }
314 }
315
316 async function displayInfo(svgdoc, data){
317 //handle 2 div : one if the process is found, and one if it isn't
318 const info = document.getElementById('info')
319 const infoNotFound = document.getElementById('info-not-found')
320 info.classList = 'hidden'
321 infoNotFound.classList = 'hidden'
322
323 if(!data[0].object){
324 infoNotFound.classList.toggle('hidden')
325 return
326 }
327 const infos = info.children[0].children
328 infos[0].innerHTML = 'Process: ' + data[0].PID
329 infos[1].innerHTML = 'Name: ' + data[0].name
330 infos[2].innerHTML = 'Object: ' + data[0].object.replace(':',' L#')
331 if(data[0].threads){
332 infos[3].innerHTML = 'Threads: ' + (data[0].threads.length - 1)
333 }else{
334 infos[3].classList = 'hidden'
335 }
336 info.classList.toggle('hidden')
337 }
338
339 function fillTable(svgdoc, proc){
340 document.getElementById('processTable').classList = "table100 ver2 m-b-110"
341 const table = document.getElementById('table')
342 const tr = document.createElement('tr')
343 const td = document.createElement('td')
344 const td2 = document.createElement('td')
345 const td3 = document.createElement('td')
346 const checkbox = document.getElementById('mainCheck')
347
348 tr.classList.add("row100", "head")
349 td.classList.add("cell100", "column1")
350 td2.classList.add("cell100", "column1")
351 td3.classList.add("cell100", "column1")
352
353 tr.setAttribute("object", proc.object)
354 td.innerHTML = proc.PID
355 td2.innerHTML = proc.name
356 td3.innerHTML = "<input class='subCheck' checked='true' type='checkbox'>"
357 td3.children[0].addEventListener('change', displayTableElement)
358 checkbox.checked = true
359 checkbox.onchange = changeAll
360
361 tr.appendChild(td)
362 tr.appendChild(td2)
363 tr.appendChild(td3)
364 table.appendChild(tr)
365
366 //toggle dispay - hide according to checkbox value
367 function displayTableElement(event){
368 const tr = event.target.parentElement.parentElement
369 const tds = tr.children
370 const proc = { PID : tds[0].innerHTML, name : tds[1].innerHTML, object : tr.getAttribute('object') }
371 if(event.target.checked == false){
372 const element = svgdoc.getElementById("bulletCircle" + "_" + proc.object.replace(':','_'))
373 removeProc(element, proc)
374 }else{
375 bullet(svgdoc, proc, "")
376 text(svgdoc, proc, "")
377 createTooltip(svgdoc)
378 }
379 }
380
381 function removeProc(element, proc){
382 let id = element.id
383 let text = svgdoc.getElementById(id.replace('bulletCircle', 'bulletText'))
384 let secondId = element.getAttribute('secondId').replace('process id : ' + proc.PID + ';', '')
385 if(!text.getAttribute('nb') || text.getAttribute('nb') == '[1]'){
386 text.remove()
387 element.remove()
388 }else{
389 updateBulletText(text, findPid(text, '-'), element)
390 }
391 element.setAttribute('secondId', secondId)
392 }
393
394 //handle click on main checkbox -> set all checkbox value to its value
395 function changeAll(event){
396 const checkboxes = document.getElementsByClassName('subCheck')
397 size = checkboxes.length
398 for(let i = 0 ; i < size ; i++ ){
399 if(checkboxes[i].checked != event.target.checked){
400 checkboxes[i].checked = event.target.checked
401 displayTableElement({target: checkboxes[i]})
402 }
403 }
404 }
405 }
406
407 function clearProc(){
408 document.getElementById('searchId').value = ""
409 const checkboxes = document.getElementsByClassName('checkbox')
410 for( let i = 0 ; i < checkboxes.length ; i++ ){
411 checkboxes[i].checked = false
412 }
413
414 let svgObject = document.getElementById('svg-object').contentDocument
415 clear(svgObject)
416 }
417
418 function clearTable(){
419 const table = document.getElementById("table");
420 while (table.firstChild) {
421 table.removeChild(table.firstChild);
422 }
423 document.getElementById("processTable").classList = "table100 ver2 m-b-110 hidden"
424 }
425
426 function handleProcButton(){
427 let functionIsRunning = false
428 //use function is running so that the function doesn't run twice if the user double click.
429 async function addProc(req){
430 if (!functionIsRunning) {
431 functionIsRunning = true
432 if(req == 'bound' || req == 'all'){
433 clearProc()
434 clearTable()
435 }
436 let svgObject = document.getElementById('svg-object').contentDocument
437 await createProcesses(svgObject, req)
438 functionIsRunning = false
439 }
440 }
441
442 const procButtons = document.getElementsByClassName('procButton')
443 for(let i = 0 ; i < procButtons.length ; i++){
444 procButtons[i].addEventListener('click', function(){
445 addProc(procButtons[i].type)
446 })
447 }
448 }
449
450 function handleCheckbox(){
451 const checkboxes = document.getElementsByClassName('checkbox')
452 for( let i = 0 ; i < checkboxes.length ; i++ ){
453 checkboxes[i].checked = false
454 checkboxes[i].addEventListener('change',function(e){
455 clearTable()
456 let svgObject = document.getElementById('svg-object').contentDocument
457 createProcesses(svgObject, e.target.id)
458 })
459 }
460 }
461
462 function procByid(){
463 let svgObject = document.getElementById('svg-object').contentDocument
464 let id = document.getElementById('searchId').value
465 clearProc()
466 createProcesses(svgObject, 'proc/'+id)
467 }
468
469 //time out so that the svg has the time to load
470 setTimeout(function() {
471 handleCheckbox()
472 handleProcButton()
473 const input = document.getElementById('searchId')
474 input.value = ''
475 input.addEventListener('keyup',function(event){
476 if(event.keyCode === 13){
477 event.preventDefault()
478 procByid()
479 }
480 })
481
482 let svgObject = document.getElementById('svg-object').contentDocument
483 let svgElement = svgObject.getElementById('Machine_0_rect')
484
485 if(svgObject.getElementById('Machine_0_rect')){
486 createProcesses(svgObject, 'bound')
487 start(svgObject)
488 }else{
489 const h1 = document.createElement('h1')
490 h1.innerHTML = "Your svg file doesn't have the good format to load javascript"
491 document.body.appendChild(h1)
492 }
493 }, 200)