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 DiskIoAbilityRender extends Render{ 28 render(req: RequestMessage, list: Array<any>, filter: Array<any>) { 29 if (req.lazyRefresh) { 30 diskIoAbility(list, filter, req.startNS, req.endNS, req.totalNS, req.frame, req.useCache || !req.range.refresh); 31 } else { 32 if (!req.useCache) { 33 diskIoAbility(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 let maxDiskRate = req.params.maxDiskRate; 44 let maxDiskRateName = req.params.maxDiskRateName; 45 drawLines(req.context, req.xs, req.frame.height, req.lineColor) 46 DiskAbilityMonitorStruct.hoverDiskAbilityStruct = 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 DiskAbilityMonitorStruct.hoverDiskAbilityStruct = re; 51 break; 52 } 53 } 54 } 55 DiskAbilityMonitorStruct.selectDiskAbilityStruct = req.params.selectDiskAbilityStruct; 56 for (let re of filter) { 57 DiskAbilityMonitorStruct.draw(req.context, re, maxDiskRate) 58 } 59 drawSelection(req.context, req.params); 60 req.context.closePath(); 61 let textMetrics = req.context.measureText(maxDiskRateName); 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(maxDiskRateName, 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: DiskAbilityMonitorStruct.hoverDiskAbilityStruct 78 }); 79 } 80} 81 82export function diskIoAbility(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 i = 0; i < res.length; i++) { 85 let item = res[i]; 86 if ((item.startNS || 0) + (item.dur || 0) > (startNS || 0) && (item.startNS || 0) < (endNS || 0)) { 87 DiskAbilityMonitorStruct.setDiskIOFrame(item, 5, startNS || 0, endNS || 0, totalNS || 0, frame) 88 } else { 89 item.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 DiskAbilityMonitorStruct.setDiskIOFrame(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} 115 116export class DiskAbilityMonitorStruct extends BaseStruct { 117 static maxDiskRate: number = 0 118 static maxDiskRateName: string = "0 KB/S" 119 static hoverDiskAbilityStruct: DiskAbilityMonitorStruct | undefined; 120 static selectDiskAbilityStruct: DiskAbilityMonitorStruct | undefined; 121 value: number | undefined 122 startNS: number | undefined 123 dur: number | undefined //自补充,数据库没有返回 124 125 static draw(context2D: CanvasRenderingContext2D, data: DiskAbilityMonitorStruct, maxDiskRate: number) { 126 if (data.frame) { 127 let width = data.frame.width || 0; 128 let index = 2; 129 context2D.fillStyle = ColorUtils.colorForTid(index) 130 context2D.strokeStyle = ColorUtils.colorForTid(index) 131 if (data.startNS === DiskAbilityMonitorStruct.hoverDiskAbilityStruct?.startNS) { 132 context2D.lineWidth = 1; 133 context2D.globalAlpha = 0.6; 134 let drawHeight: number = Math.floor(((data.value || 0) * (data.frame.height || 0) * 1.0) / maxDiskRate); 135 context2D.fillRect(data.frame.x, data.frame.y + data.frame.height - drawHeight + 4, width, drawHeight) 136 context2D.beginPath() 137 context2D.arc(data.frame.x, data.frame.y + data.frame.height - drawHeight + 4, 3, 0, 2 * Math.PI, true) 138 context2D.fill() 139 context2D.globalAlpha = 1.0; 140 context2D.stroke(); 141 context2D.beginPath() 142 context2D.moveTo(data.frame.x + 3, data.frame.y + data.frame.height - drawHeight + 4); 143 context2D.lineWidth = 3; 144 context2D.lineTo(data.frame.x + width, data.frame.y + data.frame.height - drawHeight + 4) 145 context2D.stroke(); 146 } else { 147 context2D.globalAlpha = 0.6; 148 context2D.lineWidth = 1; 149 let drawHeight: number = Math.floor(((data.value || 0) * (data.frame.height || 0)) / maxDiskRate); 150 context2D.fillRect(data.frame.x, data.frame.y + data.frame.height - drawHeight + 4, width, drawHeight) 151 } 152 } 153 context2D.globalAlpha = 1.0; 154 context2D.lineWidth = 1; 155 } 156 157 static setDiskIOFrame(node: any, padding: number, startNS: number, endNS: number, totalNS: number, frame: any) { 158 let startPointX: number, endPointX: number 159 160 if ((node.startNS || 0) < startNS) { 161 startPointX = 0 162 } else { 163 startPointX = ns2x((node.startNS || 0), startNS, endNS, totalNS, frame); 164 } 165 if ((node.startNS || 0) + (node.dur || 0) > endNS) { 166 endPointX = frame.width; 167 } else { 168 endPointX = ns2x((node.startNS || 0) + (node.dur || 0), startNS, endNS, totalNS, frame); 169 } 170 let frameWidth: number = endPointX - startPointX <= 1 ? 1 : endPointX - startPointX; 171 if (!node.frame) { 172 node.frame = {}; 173 } 174 node.frame.x = Math.floor(startPointX); 175 node.frame.y = frame.y + padding; 176 node.frame.width = Math.ceil(frameWidth); 177 node.frame.height = Math.floor(frame.height - padding * 2); 178 } 179} 180