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 {BaseStruct} from "./ProcedureWorkerCommon.js"; 17 18export function HiPerfReport(arr: Array<any>, arr2: any, type: string, res: Array<any>, startNS: number, endNS: number, totalNS: number, frame: any, groupBy10MS: boolean, intervalPerf: number,use:boolean) { 19 if (use && res.length > 0 && !groupBy10MS) { 20 let pns = (endNS - startNS) / frame.width; 21 let y = frame.y; 22 for (let i = 0; i < res.length; i++) { 23 let it = res[i]; 24 if((it.startNS || 0) + (it.dur || 0) > startNS && (it.startNS || 0) < endNS){ 25 if (!it.frame) { 26 it.frame = {}; 27 it.frame.y = y; 28 } 29 it.frame.height = it.height; 30 HiPerfReportStruct.setFrame(it, pns, startNS, endNS, frame); 31 }else{ 32 it.frame = null; 33 } 34 } 35 return; 36 } 37 res.length = 0; 38 if (arr) { 39 let list: Array<any>; 40 if (groupBy10MS) { 41 if (arr2[type] && arr2[type].length > 0) { 42 list = arr2[type]; 43 } else { 44 list = HiPerfReportStruct.groupBy10MS(arr, intervalPerf); 45 arr2[type] = list; 46 } 47 } else { 48 list = arr; 49 } 50 let pns = (endNS - startNS) / frame.width; 51 let y = frame.y; 52 53 let groups = list.filter(it => (it.startNS || 0) + (it.dur || 0) > startNS && (it.startNS || 0) < endNS).map(it => { 54 if (!it.frame) { 55 it.frame = {}; 56 it.frame.y = y; 57 } 58 it.frame.height = it.height; 59 HiPerfReportStruct.setFrame(it, pns, startNS, endNS, frame); 60 return it; 61 }).reduce((pre, current, index, arr) => { 62 if (!pre[`${current.frame.x}`]) { 63 pre[`${current.frame.x}`] = []; 64 pre[`${current.frame.x}`].push(current); 65 if (groupBy10MS) { 66 res.push(current); 67 } else { 68 if (res.length == 0) { 69 res.push(current); 70 } 71 if (res[res.length - 1] && Math.abs(current.frame.x - res[res.length - 1].frame.x) > 4) { 72 res.push(current); 73 } 74 } 75 } 76 return pre; 77 }, {}); 78 } 79} 80 81export class HiPerfReportStruct extends BaseStruct { 82 static hoverStruct: HiPerfReportStruct | undefined; 83 static selectStruct: HiPerfReportStruct | undefined; 84 static path = new Path2D('M 100,100 h 50 v 50 h 50'); 85 id: number | undefined; 86 sample_id: number | undefined; 87 timestamp: number | undefined; 88 thread_id: number | undefined; 89 event_count: number | undefined; 90 event_type_id: number | undefined; 91 cpu_id: number | undefined; 92 thread_state: string | undefined; 93 startNS: number | undefined; 94 endNS: number | undefined; 95 dur: number | undefined; 96 height: number | undefined; 97 cpu: number | undefined; 98 99 static draw(ctx: CanvasRenderingContext2D, path: Path2D, data: HiPerfReportStruct, groupBy10MS: boolean) { 100 if (data.frame) { 101 if (groupBy10MS) { 102 let width = data.frame.width; 103 path.rect(data.frame.x, 40 - (data.height || 0), width, data.height || 0) 104 } else { 105 path.moveTo(data.frame.x + 7, 20); 106 HiPerfReportStruct.drawRoundRectPath(path, data.frame.x - 7, 20 - 7, 14, 14, 3) 107 path.moveTo(data.frame.x, 27); 108 path.lineTo(data.frame.x, 33); 109 } 110 } 111 } 112 113 static drawRoundRectPath(cxt: Path2D, x: number, y: number, width: number, height: number, radius: number) { 114 cxt.arc(x + width - radius, y + height - radius, radius, 0, Math.PI / 2); 115 cxt.lineTo(x + radius, y + height); 116 cxt.arc(x + radius, y + height - radius, radius, Math.PI / 2, Math.PI); 117 cxt.lineTo(x + 0, y + radius); 118 cxt.arc(x + radius, y + radius, radius, Math.PI, Math.PI * 3 / 2); 119 cxt.lineTo(x + width - radius, y + 0); 120 cxt.arc(x + width - radius, y + radius, radius, Math.PI * 3 / 2, Math.PI * 2); 121 cxt.lineTo(x + width, y + height - radius); 122 cxt.moveTo(x + width / 3, y + height / 5); 123 cxt.lineTo(x + width / 3, y + height / 5 * 4); 124 cxt.moveTo(x + width / 3, y + height / 5); 125 cxt.bezierCurveTo(x + width / 3 + 7, y + height / 5 - 2, x + width / 3 + 7, y + height / 5 + 6, x + width / 3, y + height / 5 + 4); 126 } 127 128 static setFrame(node: any, pns: number, startNS: number, endNS: number, frame: any) { 129 if ((node.startNS || 0) < startNS) { 130 node.frame.x = 0; 131 } else { 132 node.frame.x = Math.floor(((node.startNS || 0) - startNS) / pns); 133 } 134 if ((node.startNS || 0) + (node.dur || 0) > endNS) { 135 node.frame.width = frame.width - node.frame.x; 136 } else { 137 node.frame.width = Math.ceil(((node.startNS || 0) + (node.dur || 0) - startNS) / pns - node.frame.x); 138 } 139 if (node.frame.width < 1) { 140 node.frame.width = 1; 141 } 142 } 143 144 static groupBy10MS(array: Array<any>, intervalPerf: number): Array<any> { 145 let obj = array.map(it => { 146 it.timestamp_group = Math.trunc(it.startNS / 1_000_000_0) * 1_000_000_0; 147 return it; 148 }).reduce((pre, current) => { 149 (pre[current["timestamp_group"]] = pre[current["timestamp_group"]] || []).push(current); 150 return pre; 151 }, {}); 152 let arr: any[] = []; 153 let max = 0; 154 for (let aKey in obj) { 155 let sum = obj[aKey].reduce((pre: any, cur: any) => { 156 return pre + cur.event_count 157 }, 0) 158 if (sum > max) max = sum; 159 let ns = parseInt(aKey); 160 arr.push({ 161 startNS: ns, 162 dur: 1_000_000_0, 163 height: 0, 164 sum: sum, 165 }) 166 } 167 arr.map(it => { 168 it.height = Math.floor(40 * it.sum / max); 169 return it; 170 }) 171 return arr; 172 } 173} 174