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 freq(list: Array<any>, res: Array<any>, startNS: number, endNS: number, totalNS: number, frame: any, use: boolean) { 19 if (use && res.length > 0) { 20 res.forEach(it => CpuFreqStruct.setFreqFrame(it, 5, startNS || 0, endNS || 0, totalNS || 0, frame)); 21 return; 22 } 23 res.length = 0; 24 if (list) { 25 for (let i = 0, len = list.length; i < len; i++) { 26 let it = list[i]; 27 if (i === list.length - 1) { 28 it.dur = (endNS || 0) - (it.startNS || 0) 29 } else { 30 it.dur = (list[i + 1].startNS || 0) - (it.startNS || 0) 31 } 32 if ((it.startNS || 0) + (it.dur || 0) > (startNS || 0) && (it.startNS || 0) < (endNS || 0)) { 33 CpuFreqStruct.setFreqFrame(list[i], 5, startNS || 0, endNS || 0, totalNS || 0, frame) 34 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))) { 35 36 } else { 37 res.push(it) 38 } 39 } 40 } 41 } 42 43} 44 45export class CpuFreqStruct extends BaseStruct { 46 static maxFreq: number = 0 47 static maxFreqName: string = "0 GHz" 48 static hoverCpuFreqStruct: CpuFreqStruct | undefined; 49 static selectCpuFreqStruct: CpuFreqStruct | undefined; 50 cpu: number | undefined 51 value: number | undefined 52 startNS: number | undefined 53 dur: number | undefined //自补充,数据库没有返回 54 55 static draw(ctx: CanvasRenderingContext2D, data: CpuFreqStruct) { 56 if (data.frame) { 57 let width = data.frame.width || 0; 58 let index = data.cpu || 0 59 index += 2 60 ctx.fillStyle = ColorUtils.colorForTid(index) 61 ctx.strokeStyle = ColorUtils.colorForTid(index) 62 if (data.startNS === CpuFreqStruct.hoverCpuFreqStruct?.startNS) { 63 ctx.lineWidth = 1; 64 ctx.globalAlpha = 0.6; 65 let drawHeight: number = Math.floor(((data.value || 0) * (data.frame.height || 0) * 1.0) / CpuFreqStruct.maxFreq); 66 ctx.fillRect(data.frame.x, data.frame.y + data.frame.height - drawHeight, width, drawHeight) 67 ctx.beginPath() 68 ctx.arc(data.frame.x, data.frame.y + data.frame.height - drawHeight, 3, 0, 2 * Math.PI, true) 69 ctx.fill() 70 ctx.globalAlpha = 1.0; 71 ctx.stroke(); 72 ctx.beginPath() 73 ctx.moveTo(data.frame.x + 3, data.frame.y + data.frame.height - drawHeight); 74 ctx.lineWidth = 3; 75 ctx.lineTo(data.frame.x + width, data.frame.y + data.frame.height - drawHeight) 76 ctx.stroke(); 77 } else { 78 ctx.globalAlpha = 0.6; 79 ctx.lineWidth = 1; 80 let drawHeight: number = Math.floor(((data.value || 0) * (data.frame.height || 0)) / CpuFreqStruct.maxFreq); 81 ctx.fillRect(data.frame.x, data.frame.y + data.frame.height - drawHeight, width, drawHeight) 82 } 83 } 84 ctx.globalAlpha = 1.0; 85 ctx.lineWidth = 1; 86 } 87 88 static setFreqFrame(node: any, padding: number, startNS: number, endNS: number, totalNS: number, frame: any) { 89 let x1: number, x2: number; 90 if ((node.startNS || 0) < startNS) { 91 x1 = 0; 92 } else { 93 x1 = ns2x((node.startNS || 0), startNS, endNS, totalNS, frame); 94 } 95 if ((node.startNS || 0) + (node.dur || 0) > endNS) { 96 x2 = frame.width; 97 } else { 98 x2 = ns2x((node.startNS || 0) + (node.dur || 0), startNS, endNS, totalNS, frame); 99 } 100 let getV: number = x2 - x1 <= 1 ? 1 : x2 - x1; 101 if (!node.frame) { 102 node.frame = {}; 103 } 104 node.frame.x = Math.floor(x1); 105 node.frame.y = frame.y + padding; 106 node.frame.width = Math.ceil(getV); 107 node.frame.height = Math.floor(frame.height - padding * 2); 108 } 109} 110 111const textPadding = 2;