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 18export function memoryAbility(list: Array<any>, res: Array<any>, startNS: number, endNS: number, totalNS: number, frame: any, use: boolean) { 19 if (use && res.length > 0) { 20 for (let i = 0; i < res.length; i++) { 21 let item = res[i]; 22 if ((item.startNS || 0) + (item.dur || 0) > (startNS || 0) && (item.startNS || 0) < (endNS || 0)) { 23 MemoryAbilityMonitorStruct.setMemoryFrame(item, 5, startNS || 0, endNS || 0, totalNS || 0, frame) 24 } else { 25 item.frame = null; 26 } 27 } 28 return; 29 } 30 res.length = 0; 31 if (list) { 32 for (let index = 0; index < list.length; index++) { 33 let item = list[index]; 34 if (index === list.length - 1) { 35 item.dur = (endNS || 0) - (item.startNS || 0) 36 } else { 37 item.dur = (list[index + 1].startNS || 0) - (item.startNS || 0) 38 } 39 if ((item.startNS || 0) + (item.dur || 0) > (startNS || 0) && (item.startNS || 0) < (endNS || 0)) { 40 MemoryAbilityMonitorStruct.setMemoryFrame(list[index], 5, startNS || 0, endNS || 0, totalNS || 0, frame) 41 if (index > 0 && ((list[index - 1].frame?.x || 0) == (list[index].frame?.x || 0) && (list[index - 1].frame?.width || 0) == (list[index].frame?.width || 0))) { 42 43 } else { 44 res.push(item) 45 } 46 } 47 } 48 } 49} 50 51export class MemoryAbilityMonitorStruct extends BaseStruct { 52 static maxMemoryByte: number = 0 53 static maxMemoryByteName: string = "0 MB" 54 static hoverMemoryAbilityStruct: MemoryAbilityMonitorStruct | undefined; 55 static selectMemoryAbilityStruct: MemoryAbilityMonitorStruct | undefined; 56 cpu: number | undefined 57 value: number | undefined 58 startNS: number | undefined 59 dur: number | undefined 60 61 static draw(context2D: CanvasRenderingContext2D, data: MemoryAbilityMonitorStruct) { 62 if (data.frame) { 63 let width = data.frame.width || 0; 64 let index = 2; 65 context2D.fillStyle = ColorUtils.colorForTid(index) 66 context2D.strokeStyle = ColorUtils.colorForTid(index) 67 if (data.startNS === MemoryAbilityMonitorStruct.hoverMemoryAbilityStruct?.startNS) { 68 context2D.lineWidth = 1; 69 context2D.globalAlpha = 0.6; 70 let drawHeight: number = Math.floor(((data.value || 0) * (data.frame.height || 0) * 1.0) / MemoryAbilityMonitorStruct.maxMemoryByte); 71 context2D.fillRect(data.frame.x, data.frame.y + data.frame.height - drawHeight + 4, width, drawHeight) 72 context2D.beginPath() 73 context2D.arc(data.frame.x, data.frame.y + data.frame.height - drawHeight + 4, 3, 0, 2 * Math.PI, true) 74 context2D.fill() 75 context2D.globalAlpha = 1.0; 76 context2D.stroke(); 77 context2D.beginPath() 78 context2D.moveTo(data.frame.x + 3, data.frame.y + data.frame.height - drawHeight + 4); 79 context2D.lineWidth = 3; 80 context2D.lineTo(data.frame.x + width, data.frame.y + data.frame.height - drawHeight + 4) 81 context2D.stroke(); 82 } else { 83 context2D.globalAlpha = 0.6; 84 context2D.lineWidth = 1; 85 let drawHeight: number = Math.floor(((data.value || 0) * (data.frame.height || 0)) / MemoryAbilityMonitorStruct.maxMemoryByte); 86 context2D.fillRect(data.frame.x, data.frame.y + data.frame.height - drawHeight + 4, width, drawHeight) 87 } 88 } 89 context2D.globalAlpha = 1.0; 90 context2D.lineWidth = 1; 91 } 92 93 static setMemoryFrame(node: any, padding: number, startNS: number, endNS: number, totalNS: number, frame: any) { 94 let startPointX: number, endPointX: number 95 96 if ((node.startNS || 0) < startNS) { 97 startPointX = 0 98 } else { 99 startPointX = ns2x((node.startNS || 0), startNS, endNS, totalNS, frame); 100 } 101 if ((node.startNS || 0) + (node.dur || 0) > endNS) { 102 endPointX = frame.width; 103 } else { 104 endPointX = ns2x((node.startNS || 0) + (node.dur || 0), startNS, endNS, totalNS, frame); 105 } 106 let frameWidth: number = endPointX - startPointX <= 1 ? 1 : endPointX - startPointX; 107 if (!node.frame) { 108 node.frame = {}; 109 } 110 node.frame.x = Math.floor(startPointX); 111 node.frame.y = frame.y + padding; 112 node.frame.width = Math.ceil(frameWidth); 113 node.frame.height = Math.floor(frame.height - padding * 2); 114 } 115} 116 117const textPadding = 2; 118