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