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 { 17 BaseStruct, 18 drawFlagLine, 19 drawLines, 20 drawSelection, 21 ns2x, 22 Render, 23 RequestMessage 24} from "./ProcedureWorkerCommon.js"; 25 26export class SmapsRender extends Render { 27 render(req: RequestMessage, list: Array<any>, filter: Array<any>) { 28 if (req.lazyRefresh) { 29 smaps(list, filter, req.startNS, req.endNS, req.totalNS, req.frame, req.useCache || !req.range.refresh); 30 } else { 31 if (!req.useCache) { 32 smaps(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 req.context.beginPath(); 38 let maxValue = 0; 39 let maxValueName = ""; 40 if (req.params.maxValue != undefined || req.params.maxValueName != undefined) { 41 maxValue = req.params.maxValue; 42 maxValueName = req.params.maxValueName; 43 } 44 drawLines(req.context, req.xs, req.frame.height, req.lineColor) 45 SmapsStruct.hoverSmapsStruct = 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 SmapsStruct.hoverSmapsStruct = re; 50 break; 51 } 52 } 53 } 54 let drawColor = "#0A59F7" 55 if (req.params.rowName != undefined) { 56 switch (req.params.rowName) { 57 case "dirty": 58 drawColor = "#0A59F7"; 59 break; 60 case "swapper": 61 drawColor = "#46B1E3"; 62 break; 63 case "resident_size": 64 drawColor = "#564AF7"; 65 break; 66 } 67 } 68 SmapsStruct.selectSmapsStruct = req.params.selectSmapsStruct; 69 for (let re of filter) { 70 SmapsStruct.draw(req.context, re, maxValue, drawColor) 71 } 72 drawSelection(req.context, req.params); 73 req.context.closePath(); 74 drawFlagLine(req.context, req.flagMoveInfo, req.flagSelectedInfo, req.startNS, req.endNS, req.totalNS, req.frame, req.slicesTime); 75 } 76 // @ts-ignore 77 self.postMessage({ 78 id: req.id, 79 type: req.type, 80 results: req.canvas ? undefined : filter, 81 hover: SmapsStruct.hoverSmapsStruct 82 }); 83 } 84} 85 86export function smaps(list: Array<any>, res: Array<any>, startNS: number, endNS: number, totalNS: number, frame: any, use: boolean) { 87 if (use && res.length > 0) { 88 for (let i = 0; i < res.length; i++) { 89 let item = res[i]; 90 if ((item.startNS || 0) + (item.dur || 0) > (startNS || 0) && (item.startNS || 0) < (endNS || 0)) { 91 SmapsStruct.setSmapsFrame(item, 5, startNS || 0, endNS || 0, totalNS || 0, frame) 92 } else { 93 item.frame = null; 94 } 95 } 96 return; 97 } 98 res.length = 0; 99 if (list) { 100 for (let index = 0; index < list.length; index++) { 101 let item = list[index]; 102 if (index === list.length - 1) { 103 item.dur = (endNS || 0) - (item.startNS || 0) 104 } else { 105 item.dur = (list[index + 1].startNS || 0) - (item.startNS || 0) 106 } 107 if ((item.startNS || 0) + (item.dur || 0) > (startNS || 0) && (item.startNS || 0) < (endNS || 0)) { 108 SmapsStruct.setSmapsFrame(list[index], 5, startNS || 0, endNS || 0, totalNS || 0, frame) 109 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))) { 110 111 } else { 112 res.push(item) 113 } 114 } 115 } 116 } 117} 118 119export class SmapsStruct extends BaseStruct { 120 static maxValue: number = 0 121 static maxValueName: string = "0 KB/S" 122 static hoverSmapsStruct: SmapsStruct | undefined; 123 static selectSmapsStruct: SmapsStruct | undefined; 124 value: number | undefined 125 startNS: number | undefined 126 dur: number | undefined 127 128 static draw(context2D: CanvasRenderingContext2D, data: SmapsStruct, maxValue: number, drawColor: string) { 129 if (data.frame) { 130 let width = data.frame.width || 0; 131 context2D.fillStyle = drawColor 132 context2D.strokeStyle = drawColor 133 if (data.startNS === SmapsStruct.hoverSmapsStruct?.startNS) { 134 context2D.lineWidth = 1; 135 let drawHeight: number = Math.floor(((data.value || 0) * (data.frame.height || 0) * 1.0) / maxValue); 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.lineWidth = 1; 149 let drawHeight: number = Math.floor(((data.value || 0) * (data.frame.height || 0)) / maxValue); 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 setSmapsFrame(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}