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"; 26import {CpuStruct} from "./ProcedureWorkerCPU.js"; 27 28export class ProcessRender extends Render{ 29 render(req: RequestMessage, list: Array<any>, filter: Array<any>) { 30 if (req.lazyRefresh) { 31 proc(list, filter, req.startNS, req.endNS, req.totalNS, req.frame, req.useCache || !req.range.refresh); 32 } else { 33 if (!req.useCache) { 34 proc(list, filter, req.startNS, req.endNS, req.totalNS, req.frame, false); 35 } 36 } 37 if (req.canvas) { 38 req.context.clearRect(0, 0, req.frame.width, req.frame.height); 39 let arr = filter; 40 if (arr.length > 0 && !req.range.refresh && !req.useCache && req.lazyRefresh) { 41 drawLoading(req.context, req.startNS, req.endNS, req.totalNS, req.frame, arr[0].startTime, arr[arr.length - 1].startTime + arr[arr.length - 1].dur) 42 } 43 req.context.beginPath(); 44 CpuStruct.cpuCount = req.params.cpuCount; 45 drawLines(req.context, req.xs, req.frame.height, req.lineColor) 46 let path = new Path2D(); 47 let miniHeight: number = 0; 48 miniHeight = Math.round((req.frame.height - (CpuStruct.cpuCount * 2)) / CpuStruct.cpuCount) 49 req.context.fillStyle = ColorUtils.colorForTid(req.params.pid || 0) 50 for (let re of filter) { 51 ProcessStruct.draw(req.context, path, re, miniHeight); 52 } 53 req.context.fill(path); 54 drawSelection(req.context, req.params); 55 drawWakeUp(req.context, req.wakeupBean, req.startNS, req.endNS, req.totalNS, req.frame); 56 req.context.closePath(); 57 drawFlagLine(req.context, req.flagMoveInfo, req.flagSelectedInfo, req.startNS, req.endNS, req.totalNS, req.frame, req.slicesTime); 58 } 59 // @ts-ignore 60 self.postMessage({ 61 id: req.id, 62 type: req.type, 63 results: req.canvas ? undefined : filter, 64 hover: undefined 65 }); 66 } 67} 68export function proc(list: Array<any>, res: Array<any>, startNS: number, endNS: number, totalNS: number, frame: any,use:boolean) { 69 if(use && res.length > 0){ 70 res.forEach(it=>ProcessStruct.setProcessFrame(it, 5, startNS || 0, endNS || 0, totalNS || 0, frame)) 71 return; 72 } 73 res.length = 0 ; 74 if (list) { 75 for (let i = 0, len = list.length; i < len; i++) { 76 let it = list[i]; 77 if ((it.startTime || 0) + (it.dur || 0) > (startNS || 0) && (it.startTime || 0) < (endNS || 0)) { 78 ProcessStruct.setProcessFrame(list[i], 5, startNS || 0, endNS || 0, totalNS || 0, frame) 79 if (i > 0 && ((list[i - 1].frame?.x || 0) == (list[i].frame?.x || 0) && (list[i - 1].frame?.width || 0) == (list[i].frame?.width || 0))) { 80 81 } else { 82 res.push(list[i]) 83 } 84 } 85 } 86 } 87} 88 89const padding = 1; 90 91export class ProcessStruct extends BaseStruct { 92 cpu: number | undefined 93 dur: number | undefined 94 id: number | undefined 95 pid: number | undefined 96 process: string | undefined 97 startTime: number | undefined 98 state: string | undefined 99 thread: string | undefined 100 tid: number | undefined 101 ts: number | undefined 102 type: string | undefined 103 utid: number | undefined 104 105 static draw(ctx: CanvasRenderingContext2D,path:Path2D, data: ProcessStruct,miniHeight:number) { 106 if (data.frame) { 107 path.rect(data.frame.x, data.frame.y + (data.cpu || 0) * miniHeight + padding, data.frame.width, miniHeight) 108 } 109 } 110 111 static setFrame(node: any, pns: number, startNS: number, endNS: number, frame: any) { 112 if ((node.startTime || 0) < startNS) { 113 node.frame.x = 0; 114 } else { 115 node.frame.x = Math.floor(((node.startTime || 0) - startNS) / pns); 116 } 117 if ((node.startTime || 0) + (node.dur || 0) > endNS) { 118 node.frame.width = frame.width - node.frame.x; 119 } else { 120 node.frame.width = Math.ceil(((node.startTime || 0) + (node.dur || 0) - startNS) / pns - node.frame.x); 121 } 122 if (node.frame.width < 1) { 123 node.frame.width = 1; 124 } 125 } 126 127 static setProcessFrame(node: any, padding: number, startNS: number, endNS: number, totalNS: number, frame: any) { 128 let x1: number; 129 let x2: number; 130 if ((node.startTime || 0) < startNS) { 131 x1 = 0; 132 } else { 133 x1 = ns2x((node.startTime || 0), startNS, endNS, totalNS, frame); 134 } 135 if ((node.startTime || 0) + (node.dur || 0) > endNS) { 136 x2 = frame.width; 137 } else { 138 x2 = ns2x((node.startTime || 0) + (node.dur || 0), startNS, endNS, totalNS, frame); 139 } 140 let getV: number = x2 - x1 <= 1 ? 1 : x2 - x1; 141 if (!node.frame) { 142 node.frame = {}; 143 } 144 node.frame.x = Math.floor(x1); 145 node.frame.y = Math.floor(frame.y + 2); 146 node.frame.width = Math.ceil(getV); 147 node.frame.height = Math.floor(frame.height - padding * 2); 148 } 149} 150