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 { Rect, Render, isFrameContainPoint, ns2x, drawLoadingFrame } from './ProcedureWorkerCommon'; 17import { TraceRow } from '../../component/trace/base/TraceRow'; 18import { HeapStruct as BaseHeapStruct } from '../../bean/HeapStruct'; 19import {SpSystemTrace} from "../../component/SpSystemTrace"; 20export class NativeMemoryRender extends Render { 21 renderMainThread(req: any, row: TraceRow<any>) {} 22} 23export class HeapRender { 24 renderMainThread( 25 req: { 26 context: CanvasRenderingContext2D; 27 useCache: boolean; 28 type: string; 29 }, 30 row: TraceRow<HeapStruct> 31 ) { 32 let heapList = row.dataList; 33 let heapFilter = row.dataListCache; 34 heap( 35 heapList, 36 heapFilter, 37 TraceRow.range?.startNS ?? 0, 38 TraceRow.range?.endNS ?? 0, 39 TraceRow.range?.totalNS ?? 0, 40 row.frame, 41 req.useCache || (TraceRow.range?.refresh ?? false) 42 ); 43 drawLoadingFrame(req.context, heapFilter, row); 44 setRenderHeapFrame(heapFilter, row); 45 drawHeap(req, heapFilter, row); 46 } 47} 48 49function setRenderHeapFrame(heapFilter: HeapStruct[], row: TraceRow<HeapStruct>): void { 50 // 多条数据,最后一条数据在结束点也需要绘制 51 if (heapFilter.length >= 2 && heapFilter[heapFilter.length - 1].dur === 0) { 52 if (heapFilter[heapFilter.length - 2].frame && heapFilter[heapFilter.length - 1].frame) { 53 heapFilter[heapFilter.length - 2].frame!.width = heapFilter[heapFilter.length - 2].frame!.width - 1; 54 heapFilter[heapFilter.length - 1].frame!.width = 1; 55 heapFilter[heapFilter.length - 1].frame!.x -= 1; 56 } 57 } 58 // 只有一条数据并且数据在结束点 59 if (heapFilter.length === 1 && row.frame.width === heapFilter[0].frame?.x) { 60 heapFilter[0].frame!.x -= 1; 61 } 62} 63 64function drawHeap( 65 req: { 66 context: CanvasRenderingContext2D; 67 useCache: boolean; 68 type: string; 69 }, 70 heapFilter: HeapStruct[], 71 row: TraceRow<HeapStruct> 72) { 73 req.context.beginPath(); 74 let find = false; 75 for (let re of heapFilter) { 76 if (row.isHover && re.frame && !find && isFrameContainPoint(re.frame, row.hoverX, row.hoverY)) { 77 HeapStruct.hoverHeapStruct = re; 78 find = true; 79 } 80 } 81 for (let re of heapFilter) { 82 HeapStruct.drawHeap(req.context, re, row.drawType); 83 } 84 if (!find && row.isHover) HeapStruct.hoverHeapStruct = undefined; 85 req.context.closePath(); 86} 87 88export function heap( 89 heapList: Array<any>, 90 res: Array<any>, 91 startNS: number, 92 endNS: number, 93 totalNS: number, 94 frame: any, 95 use: boolean 96) { 97 if (use && res.length > 0) { 98 setHeapFrameIfUse(res, startNS, endNS, totalNS, frame); 99 return; 100 } 101 res.length = 0; 102 for (let i = 0, len = heapList.length; i < len; i++) { 103 let it = heapList[i]; 104 if ((it.startTime || 0) + (it.dur || 0) > startNS && (it.startTime || 0) <= endNS) { 105 HeapStruct.setFrame(it, 5, startNS, endNS, totalNS, frame); 106 if (i > 0) { 107 let last = heapList[i - 1]; 108 if (last.frame?.x != it.frame.x || last.frame.width != it.frame.width) { 109 res.push(it); 110 } 111 } else { 112 res.push(it); 113 } 114 } 115 } 116} 117 118function setHeapFrameIfUse(res: Array<any>, startNS: number, endNS: number, totalNS: number, frame: any) { 119 for (let i = 0; i < res.length; i++) { 120 let it = res[i]; 121 if ((it.startTime || 0) + (it.dur || 0) > startNS && (it.startTime || 0) <= endNS) { 122 HeapStruct.setFrame(res[i], 5, startNS, endNS, totalNS, frame); 123 } else { 124 res[i].frame = null; 125 } 126 } 127} 128 129export function HeapStructOnClick(clickRowType: string, sp: SpSystemTrace, row: undefined | TraceRow<any>) { 130 return new Promise((resolve,reject) => { 131 if ( 132 clickRowType === TraceRow.ROW_TYPE_HEAP && 133 row && 134 row.getAttribute('heap-type') === 'native_hook_statistic' && 135 HeapStruct.hoverHeapStruct 136 ) { 137 HeapStruct.selectHeapStruct = HeapStruct.hoverHeapStruct; 138 const key = row.rowParentId!.split(' '); 139 let ipid = 1; 140 if (key.length > 0) { 141 ipid = Number(key[key.length - 1]); 142 } 143 sp.traceSheetEL?.displayNativeHookData(HeapStruct.selectHeapStruct, row.rowId!, ipid); 144 sp.timerShaftEL?.modifyFlagList(undefined); 145 reject(); 146 }else{ 147 resolve(null); 148 } 149 }); 150} 151export class HeapStruct extends BaseHeapStruct { 152 static hoverHeapStruct: HeapStruct | undefined; 153 static selectHeapStruct: HeapStruct | undefined; 154 maxDensity: number = 0; 155 minDensity: number = 0; 156 157 static setFrame(node: HeapStruct, padding: number, startNS: number, endNS: number, totalNS: number, frame: Rect) { 158 let x1: number, x2: number; 159 if ((node.startTime || 0) < startNS) { 160 x1 = 0; 161 } else { 162 x1 = ns2x(node.startTime || 0, startNS, endNS, totalNS, frame); 163 } 164 if ((node.startTime || 0) + (node.dur || 0) > endNS) { 165 x2 = frame.width; 166 } else { 167 x2 = ns2x( 168 // @ts-ignore 169 node.startTime + node.dur, 170 startNS, 171 endNS, 172 totalNS, 173 frame 174 ); 175 } 176 let getV: number = x2 - x1 <= 1 ? 1 : x2 - x1; 177 let rectangle: Rect = new Rect( 178 Math.floor(x1), 179 Math.ceil(frame.y + padding), 180 Math.ceil(getV), 181 Math.floor(frame.height - padding * 2) 182 ); 183 node.frame = rectangle; 184 } 185 186 static drawHeap(heapContext: CanvasRenderingContext2D, data: HeapStruct, drawType: number) { 187 if (data.frame) { 188 let width = data.frame.width || 0; 189 heapContext.fillStyle = '#2db3aa'; 190 heapContext.strokeStyle = '#2db3aa'; 191 let drawHeight: number = 0; 192 if (drawType == 0) { 193 if (data.minHeapSize < 0) { 194 drawHeight = Math.ceil( 195 (((data.heapsize || 0) - data.minHeapSize) * (data.frame.height || 0)) / 196 (data.maxHeapSize - data.minHeapSize) 197 ); 198 } else { 199 drawHeight = Math.ceil(((data.heapsize || 0) * (data.frame.height || 0)) / data.maxHeapSize); 200 } 201 } else { 202 if (data.minDensity < 0) { 203 drawHeight = Math.ceil( 204 (((data.density || 0) - data.minDensity) * (data.frame.height || 0)) / (data.maxDensity - data.minDensity) 205 ); 206 } else { 207 drawHeight = Math.ceil(((data.density || 0) * (data.frame.height || 0)) / data.maxDensity); 208 } 209 } 210 if (data == HeapStruct.hoverHeapStruct || data == HeapStruct.selectHeapStruct) { 211 heapContext.lineWidth = 1; 212 heapContext.globalAlpha = 0.6; 213 heapContext.fillRect(data.frame.x, data.frame.y + data.frame.height - drawHeight, width, drawHeight); 214 heapContext.beginPath(); 215 heapContext.arc(data.frame.x, data.frame.y + data.frame.height - drawHeight, 3, 0, 2 * Math.PI, true); 216 heapContext.fill(); 217 heapContext.globalAlpha = 1.0; 218 heapContext.stroke(); 219 heapContext.beginPath(); 220 heapContext.moveTo(data.frame.x + 3, data.frame.y + data.frame.height - drawHeight); 221 heapContext.lineWidth = 3; 222 heapContext.lineTo(data.frame.x + width, data.frame.y + data.frame.height - drawHeight); 223 heapContext.stroke(); 224 } else { 225 heapContext.globalAlpha = 0.6; 226 heapContext.lineWidth = 1; 227 heapContext.fillRect(data.frame.x, data.frame.y + data.frame.height - drawHeight, width, drawHeight); 228 } 229 } 230 heapContext.globalAlpha = 1.0; 231 heapContext.lineWidth = 1; 232 } 233} 234