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 { ColorUtils } from "../../component/trace/base/ColorUtils.js"; 17import { 18 BaseStruct, 19 drawFlagLine, 20 drawLines, 21 drawLoading, 22 drawSelection, drawWakeUp, 23 ns2x, Render, 24 RequestMessage 25} from "./ProcedureWorkerCommon.js"; 26 27export class FreqRender extends Render{ 28 render(req: RequestMessage, list: Array<any>, filter: Array<any>) { 29 if (req.lazyRefresh) { 30 freq(list, filter, req.startNS, req.endNS, req.totalNS, req.frame, req.useCache || !req.range.refresh); 31 } else { 32 if (!req.useCache) { 33 freq(list, filter, req.startNS, req.endNS, req.totalNS, req.frame, false); 34 } 35 } 36 if (req.canvas) { 37 req.context.clearRect(0, 0, req.frame.width, req.frame.height); 38 let arr = filter; 39 if (arr.length > 0 && !req.range.refresh && !req.useCache && req.lazyRefresh) { 40 drawLoading(req.context, req.startNS, req.endNS, req.totalNS, req.frame, arr[0].startNS, arr[arr.length - 1].startNS + arr[arr.length - 1].dur) 41 } 42 req.context.beginPath(); 43 CpuFreqStruct.maxFreq = req.params.maxFreq; 44 CpuFreqStruct.maxFreqName = req.params.maxFreqName; 45 drawLines(req.context, req.xs, req.frame.height, req.lineColor) 46 CpuFreqStruct.hoverCpuFreqStruct = undefined; 47 if (req.isHover) { 48 for (let re of filter) { 49 if (re.frame && req.hoverX >= re.frame.x && req.hoverX <= re.frame.x + re.frame.width && req.hoverY >= re.frame.y && req.hoverY <= re.frame.y + re.frame.height) { 50 CpuFreqStruct.hoverCpuFreqStruct = re; 51 break; 52 } 53 } 54 } else { 55 CpuFreqStruct.hoverCpuFreqStruct = req.params.hoverCpuFreqStruct; 56 } 57 CpuFreqStruct.selectCpuFreqStruct = req.params.selectCpuFreqStruct; 58 for (let re of filter) { 59 CpuFreqStruct.draw(req.context, re) 60 } 61 drawSelection(req.context, req.params); 62 req.context.closePath(); 63 let s = CpuFreqStruct.maxFreqName 64 let textMetrics = req.context.measureText(s); 65 req.context.globalAlpha = 0.8 66 req.context.fillStyle = "#f0f0f0" 67 req.context.fillRect(0, 5, textMetrics.width + 8, 18) 68 req.context.globalAlpha = 1 69 req.context.fillStyle = "#333" 70 req.context.textBaseline = "middle" 71 req.context.fillText(s, 4, 5 + 9) 72 drawWakeUp(req.context, req.wakeupBean, req.startNS, req.endNS, req.totalNS, req.frame); 73 drawFlagLine(req.context, req.flagMoveInfo, req.flagSelectedInfo, req.startNS, req.endNS, req.totalNS, req.frame, req.slicesTime); 74 } 75 // @ts-ignore 76 self.postMessage({ 77 id: req.id, 78 type: req.type, 79 results: req.canvas ? undefined : filter, 80 hover: CpuFreqStruct.hoverCpuFreqStruct 81 }); 82 } 83} 84 85export function freq(list: Array<any>, res: Array<any>, startNS: number, endNS: number, totalNS: number, frame: any, use: boolean) { 86 if (use && res.length > 0) { 87 res.forEach(it => CpuFreqStruct.setFreqFrame(it, 5, startNS || 0, endNS || 0, totalNS || 0, frame)); 88 return; 89 } 90 res.length = 0; 91 if (list) { 92 for (let i = 0, len = list.length; i < len; i++) { 93 let it = list[i]; 94 if (i === list.length - 1) { 95 it.dur = (endNS || 0) - (it.startNS || 0) 96 } else { 97 it.dur = (list[i + 1].startNS || 0) - (it.startNS || 0) 98 } 99 if ((it.startNS || 0) + (it.dur || 0) > (startNS || 0) && (it.startNS || 0) < (endNS || 0)) { 100 CpuFreqStruct.setFreqFrame(list[i], 5, startNS || 0, endNS || 0, totalNS || 0, frame) 101 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))) { 102 103 } else { 104 res.push(it) 105 } 106 } 107 } 108 } 109 110} 111 112export class CpuFreqStruct extends BaseStruct { 113 static maxFreq: number = 0 114 static maxFreqName: string = "0 GHz" 115 static hoverCpuFreqStruct: CpuFreqStruct | undefined; 116 static selectCpuFreqStruct: CpuFreqStruct | undefined; 117 cpu: number | undefined 118 value: number | undefined 119 startNS: number | undefined 120 dur: number | undefined //自补充,数据库没有返回 121 122 static draw(ctx: CanvasRenderingContext2D, data: CpuFreqStruct) { 123 if (data.frame) { 124 let width = data.frame.width || 0; 125 let index = data.cpu || 0 126 index += 2 127 ctx.fillStyle = ColorUtils.colorForTid(index) 128 ctx.strokeStyle = ColorUtils.colorForTid(index) 129 if (data.startNS === CpuFreqStruct.hoverCpuFreqStruct?.startNS||data.startNS === CpuFreqStruct.selectCpuFreqStruct?.startNS) { 130 ctx.lineWidth = 1; 131 ctx.globalAlpha = 0.6; 132 let drawHeight: number = Math.floor(((data.value || 0) * (data.frame.height || 0) * 1.0) / CpuFreqStruct.maxFreq); 133 ctx.fillRect(data.frame.x, data.frame.y + data.frame.height - drawHeight, width, drawHeight) 134 ctx.beginPath() 135 ctx.arc(data.frame.x, data.frame.y + data.frame.height - drawHeight, 3, 0, 2 * Math.PI, true) 136 ctx.fill() 137 ctx.globalAlpha = 1.0; 138 ctx.stroke(); 139 ctx.beginPath() 140 ctx.moveTo(data.frame.x + 3, data.frame.y + data.frame.height - drawHeight); 141 ctx.lineWidth = 3; 142 ctx.lineTo(data.frame.x + width, data.frame.y + data.frame.height - drawHeight) 143 ctx.stroke(); 144 } else { 145 ctx.globalAlpha = 0.6; 146 ctx.lineWidth = 1; 147 let drawHeight: number = Math.floor(((data.value || 0) * (data.frame.height || 0)) / CpuFreqStruct.maxFreq); 148 ctx.fillRect(data.frame.x, data.frame.y + data.frame.height - drawHeight, width, drawHeight) 149 } 150 } 151 ctx.globalAlpha = 1.0; 152 ctx.lineWidth = 1; 153 } 154 155 static setFreqFrame(node: any, padding: number, startNS: number, endNS: number, totalNS: number, frame: any) { 156 let x1: number, x2: number; 157 if ((node.startNS || 0) < startNS) { 158 x1 = 0; 159 } else { 160 x1 = ns2x((node.startNS || 0), startNS, endNS, totalNS, frame); 161 } 162 if ((node.startNS || 0) + (node.dur || 0) > endNS) { 163 x2 = frame.width; 164 } else { 165 x2 = ns2x((node.startNS || 0) + (node.dur || 0), startNS, endNS, totalNS, frame); 166 } 167 let getV: number = x2 - x1 <= 1 ? 1 : x2 - x1; 168 if (!node.frame) { 169 node.frame = {}; 170 } 171 node.frame.x = Math.floor(x1); 172 node.frame.y = frame.y + padding; 173 node.frame.width = Math.ceil(getV); 174 node.frame.height = Math.floor(frame.height - padding * 2); 175 } 176} 177 178const textPadding = 2;