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 cpuAbility(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 index = 0; index < res.length; index++) { 21 let item = res[index]; 22 if ((item.startNS || 0) + (item.dur || 0) > (startNS || 0) && (item.startNS || 0) < (endNS || 0)) { 23 CpuAbilityMonitorStruct.setCpuAbilityFrame(res[index], 5, startNS || 0, endNS || 0, totalNS || 0, frame) 24 } else { 25 res[index].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 CpuAbilityMonitorStruct.setCpuAbilityFrame(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 CpuAbilityMonitorStruct extends BaseStruct { 52 static maxCpuUtilization: number = 0 53 static maxCpuUtilizationName: string = "0 %" 54 static hoverCpuAbilityStruct: CpuAbilityMonitorStruct | undefined; 55 static selectCpuAbilityStruct: CpuAbilityMonitorStruct | undefined; 56 57 type: number | undefined 58 value: number | undefined 59 startNS: number | undefined 60 dur: number | undefined //自补充,数据库没有返回 61 62 static draw(context2D: CanvasRenderingContext2D, data: CpuAbilityMonitorStruct) { 63 if (data.frame) { 64 let width = data.frame.width || 0; 65 let index = 2; 66 context2D.fillStyle = ColorUtils.colorForTid(index) 67 context2D.strokeStyle = ColorUtils.colorForTid(index) 68 if (data.startNS === CpuAbilityMonitorStruct.hoverCpuAbilityStruct?.startNS) { 69 context2D.lineWidth = 1; 70 context2D.globalAlpha = 0.6; 71 let drawHeight: number = Math.floor(((data.value || 0) * (data.frame.height || 0) * 1.0) / CpuAbilityMonitorStruct.maxCpuUtilization); 72 context2D.fillRect(data.frame.x, data.frame.y + data.frame.height - drawHeight + 4, width, drawHeight) 73 context2D.beginPath() 74 context2D.arc(data.frame.x, data.frame.y + data.frame.height - drawHeight + 4, 3, 0, 2 * Math.PI, true) 75 context2D.fill() 76 context2D.globalAlpha = 1.0; 77 context2D.stroke(); 78 context2D.beginPath() 79 context2D.moveTo(data.frame.x + 3, data.frame.y + data.frame.height - drawHeight + 4); 80 context2D.lineWidth = 3; 81 context2D.lineTo(data.frame.x + width, data.frame.y + data.frame.height - drawHeight + 4) 82 context2D.stroke(); 83 } else { 84 context2D.globalAlpha = 0.6; 85 context2D.lineWidth = 1; 86 let drawHeight: number = Math.floor(((data.value || 0) * (data.frame.height || 0)) / CpuAbilityMonitorStruct.maxCpuUtilization); 87 context2D.fillRect(data.frame.x, data.frame.y + data.frame.height - drawHeight + 4, width, drawHeight) 88 } 89 } 90 context2D.globalAlpha = 1.0; 91 context2D.lineWidth = 1; 92 } 93 94 static setCpuAbilityFrame(node: any, padding: number, startNS: number, endNS: number, totalNS: number, frame: any) { 95 let startPointX: number, endPointX: number 96 97 if ((node.startNS || 0) < startNS) { 98 startPointX = 0 99 } else { 100 startPointX = ns2x((node.startNS || 0), startNS, endNS, totalNS, frame); 101 } 102 if ((node.startNS || 0) + (node.dur || 0) > endNS) { 103 endPointX = frame.width; 104 } else { 105 endPointX = ns2x((node.startNS || 0) + (node.dur || 0), startNS, endNS, totalNS, frame); 106 } 107 let frameWidth: number = endPointX - startPointX <= 1 ? 1 : endPointX - startPointX; 108 if (!node.frame) { 109 node.frame = {}; 110 } 111 node.frame.x = Math.floor(startPointX); 112 node.frame.y = frame.y + padding; 113 node.frame.width = Math.ceil(frameWidth); 114 node.frame.height = Math.floor(frame.height - padding * 2); 115 } 116} 117 118export class CpuAbility { 119 context: any 120 params: any 121}