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: Set<any>, startNS: number, endNS: number, totalNS: number, frame: any) { 42 res.clear(); 43 if (list) { 44 for (let i = 0, len = list.length; i < len; i++) { 45 let it = list[i]; 46 if ((it.startTime || 0) + (it.duration || 0) > startNS && (it.startTime || 0) < endNS) { 47 setMemFrame(list[i], 5, startNS, endNS, totalNS, frame) 48 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))) { 49 continue; 50 } else { 51 res.add(list[i]) 52 } 53 } 54 } 55 } 56} 57 58export class ProcessMemStruct extends BaseStruct{ 59 trackId:number|undefined 60 processName:string|undefined 61 pid:number|undefined 62 upid:number|undefined 63 trackName:string|undefined 64 65 type:string|undefined 66 track_id:string|undefined 67 value:number|undefined 68 startTime:number|undefined 69 duration:number|undefined 70 maxValue:number|undefined 71 delta: number | undefined; 72 73 static draw(ctx: CanvasRenderingContext2D, data: ProcessMemStruct) { 74 if (data.frame) { 75 let width = data.frame.width || 0; 76 ctx.fillStyle= ColorUtils.colorForTid(data.maxValue||0) 77 ctx.strokeStyle= ColorUtils.colorForTid(data.maxValue||0) 78 ctx.globalAlpha = 0.6; 79 ctx.lineWidth = 1; 80 let drawHeight: number = ((data.value || 0) * (data.frame.height || 0) * 1.0) / (data.maxValue||1); 81 ctx.fillRect(data.frame.x, data.frame.y + data.frame.height - drawHeight, width, drawHeight) 82 } 83 ctx.globalAlpha = 1.0; 84 ctx.lineWidth = 1; 85 } 86}