1/* 2 * Copyright (C) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import {BaseStruct, ColorUtils, ns2x} from "./ProcedureWorkerCommon.js"; 17 18function setMemFrame(node: any, padding: number, startNS: number, endNS: number, totalNS: number, frame: any) { 19 let x1: number; 20 let x2: number; 21 if ((node.startTime || 0) <= startNS) { 22 x1 = 0; 23 } else { 24 x1 = ns2x((node.startTime || 0), startNS, endNS, totalNS, frame); 25 } 26 if ((node.startTime || 0) + (node.duration || 0) >= endNS) { 27 x2 = frame.width; 28 } else { 29 x2 = ns2x((node.startTime || 0) + (node.duration || 0), startNS, endNS, totalNS, frame); 30 } 31 let getV: number = x2 - x1 <= 1 ? 1 : x2 - x1; 32 if (!node.frame) { 33 node.frame = {}; 34 } 35 node.frame.x = Math.floor(x1); 36 node.frame.y = Math.floor(frame.y + padding); 37 node.frame.width = Math.ceil(getV); 38 node.frame.height = Math.floor(frame.height - padding * 2); 39} 40 41export function mem(list: Array<any>, res: Array<any>, startNS: number, endNS: number, totalNS: number, frame: any, use: boolean) { 42 if (use && res.length > 0) { 43 for (let i = 0, len = res.length; i < len; i++) { 44 if ((res[i].startTime || 0) + (res[i].duration || 0) > startNS && (res[i].startTime || 0) < endNS) { 45 setMemFrame(res[i], 5, startNS, endNS, totalNS, frame) 46 }else{ 47 res[i].frame = null; 48 } 49 } 50 return; 51 } 52 res.length = 0; 53 if (list) { 54 for (let i = 0, len = list.length; i < len; i++) { 55 let it = list[i]; 56 if ((it.startTime || 0) + (it.duration || 0) > startNS && (it.startTime || 0) < endNS) { 57 setMemFrame(list[i], 5, startNS, endNS, totalNS, frame) 58 if (i > 0 && ((list[i - 1].frame?.x || 0) == (list[i].frame?.x || 0) && (list[i - 1].frame?.width || 0) == (list[i].frame?.width || 0))) { 59 60 } else { 61 res.push(list[i]) 62 } 63 } 64 } 65 } 66} 67 68export class ProcessMemStruct extends BaseStruct { 69 static hoverProcessMemStruct: ProcessMemStruct | undefined; 70 trackId: number | undefined 71 processName: string | undefined 72 pid: number | undefined 73 upid: number | undefined 74 trackName: string | undefined 75 type: string | undefined 76 track_id: string | undefined 77 value: number | undefined 78 startTime: number | undefined 79 duration: number | undefined 80 maxValue: number | undefined 81 delta: number | undefined; 82 83 static draw(ctx: CanvasRenderingContext2D, data: ProcessMemStruct) { 84 if (data.frame) { 85 let width = data.frame.width || 0; 86 ctx.fillStyle = ColorUtils.colorForTid(data.maxValue || 0) 87 ctx.strokeStyle = ColorUtils.colorForTid(data.maxValue || 0) 88 if (data.track_id === ProcessMemStruct.hoverProcessMemStruct?.track_id && data.startTime === ProcessMemStruct.hoverProcessMemStruct?.startTime) { 89 ctx.lineWidth = 1; 90 ctx.globalAlpha = 0.6; 91 let drawHeight: number = Math.floor(((data.value || 0) * (data.frame.height || 0) * 1.0) / (data.maxValue || 0)); 92 ctx.fillRect(data.frame.x, data.frame.y + data.frame.height - drawHeight, width, drawHeight) 93 ctx.beginPath() 94 ctx.arc(data.frame.x, data.frame.y + data.frame.height - drawHeight, 3, 0, 2 * Math.PI, true) 95 ctx.fill() 96 ctx.globalAlpha = 1.0; 97 ctx.stroke(); 98 ctx.beginPath() 99 ctx.moveTo(data.frame.x + 3, data.frame.y + data.frame.height - drawHeight); 100 ctx.lineWidth = 3; 101 ctx.lineTo(data.frame.x + width, data.frame.y + data.frame.height - drawHeight) 102 ctx.stroke(); 103 } else { 104 ctx.globalAlpha = 0.6; 105 ctx.lineWidth = 1; 106 let drawHeight: number = ((data.value || 0) * (data.frame.height || 0) * 1.0) / (data.maxValue || 1); 107 ctx.fillRect(data.frame.x, data.frame.y + data.frame.height - drawHeight, width, drawHeight) 108 } 109 } 110 ctx.globalAlpha = 1.0; 111 ctx.lineWidth = 1; 112 } 113}