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'; 17import { BaseStruct } from './BaseStruct'; 18import { WakeupBean } from './WakeupBean'; 19 20export class CpuStruct extends BaseStruct { 21 static cpuCount: number; //最大cpu数量 22 static hoverCpuStruct: CpuStruct | undefined; 23 static selectCpuStruct: CpuStruct | undefined; 24 static wakeupBean: WakeupBean | null | undefined; 25 cpu: number | undefined; 26 dur: number | undefined; 27 end_state: string | undefined; 28 id: number | undefined; 29 name: string | undefined; 30 priority: number | undefined; 31 processCmdLine: string | undefined; 32 processId: number | undefined; 33 processName: string | undefined; 34 schedId: number | undefined; 35 startTime: number | undefined; 36 tid: number | undefined; 37 type: string | undefined; 38 39 static draw(cpuCtx: CanvasRenderingContext2D, cpuStruct: CpuStruct): void { 40 if (cpuStruct.frame) { 41 let cpuWidth = cpuStruct.frame.width || 0; 42 if (cpuStruct.processId === CpuStruct.hoverCpuStruct?.processId || !CpuStruct.hoverCpuStruct) { 43 cpuCtx.fillStyle = ColorUtils.colorForTid( 44 (cpuStruct.processId || 0) > 0 ? cpuStruct.processId || 0 : cpuStruct.tid || 0 45 ); 46 } else { 47 cpuCtx.fillStyle = '#e0e0e0'; 48 } 49 cpuCtx.fillRect(cpuStruct.frame.x, cpuStruct.frame.y, cpuWidth, cpuStruct.frame.height); 50 if (cpuWidth > textPadding * 2) { 51 let cpuProcess = `${cpuStruct.processName || 'Process'} [${cpuStruct.processId}]`; 52 let cpuThread = `${cpuStruct.name || 'Thread'} [${cpuStruct.tid}]`; 53 let processMeasure = cpuCtx.measureText(cpuProcess); 54 let threadMeasure = cpuCtx.measureText(cpuThread); 55 let pChartWidth = Math.round(processMeasure.width / cpuProcess.length); 56 let tChartWidth = Math.round(threadMeasure.width / cpuThread.length); 57 cpuCtx.fillStyle = '#ffffff'; 58 let y = cpuStruct.frame.height / 2 + cpuStruct.frame.y; 59 if (processMeasure.width < cpuWidth - textPadding * 2) { 60 let x1 = Math.floor(cpuWidth / 2 - processMeasure.width / 2 + cpuStruct.frame.x + textPadding); 61 cpuCtx.textBaseline = 'bottom'; 62 cpuCtx.fillText(cpuProcess, x1, y, cpuWidth - textPadding * 2); 63 } else if (cpuWidth - textPadding * 2 > pChartWidth * 4) { 64 let chatNum = (cpuWidth - textPadding * 2) / pChartWidth; 65 let x1 = cpuStruct.frame.x + textPadding; 66 cpuCtx.textBaseline = 'bottom'; 67 cpuCtx.fillText(`${cpuProcess.substring(0, chatNum - 4)}...`, x1, y, cpuWidth - textPadding * 2); 68 } 69 if (threadMeasure.width < cpuWidth - textPadding * 2) { 70 cpuCtx.textBaseline = 'top'; 71 let x2 = Math.floor(cpuWidth / 2 - threadMeasure.width / 2 + cpuStruct.frame.x + textPadding); 72 cpuCtx.fillText(cpuThread, x2, y + 2, cpuWidth - textPadding * 2); 73 } else if (cpuWidth - textPadding * 2 > tChartWidth * 4) { 74 let chatNum = (cpuWidth - textPadding * 2) / tChartWidth; 75 let x1 = cpuStruct.frame.x + textPadding; 76 cpuCtx.textBaseline = 'top'; 77 cpuCtx.fillText(`${cpuThread.substring(0, chatNum - 4)}...`, x1, y + 2, cpuWidth - textPadding * 2); 78 } 79 } 80 if (CpuStruct.selectCpuStruct && CpuStruct.equals(CpuStruct.selectCpuStruct, cpuStruct)) { 81 cpuCtx.strokeStyle = '#232c5d'; 82 cpuCtx.lineWidth = 2; 83 cpuCtx.strokeRect(cpuStruct.frame.x, cpuStruct.frame.y, cpuWidth - 2, cpuStruct.frame.height); 84 } 85 } 86 } 87 88 static equals(d1: CpuStruct, d2: CpuStruct): boolean { 89 if ( 90 d1 && 91 d2 && 92 d1.cpu === d2.cpu && 93 d1.tid === d2.tid && 94 d1.processId === d2.processId && 95 d1.startTime === d2.startTime && 96 d1.dur === d2.dur 97 ) { 98 return true; 99 } else { 100 return false; 101 } 102 } 103} 104 105const textPadding = 2; 106