1fb726d48Sopenharmony_ci/* 2fb726d48Sopenharmony_ci * Copyright (C) 2022 Huawei Device Co., Ltd. 3fb726d48Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4fb726d48Sopenharmony_ci * you may not use this file except in compliance with the License. 5fb726d48Sopenharmony_ci * You may obtain a copy of the License at 6fb726d48Sopenharmony_ci * 7fb726d48Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8fb726d48Sopenharmony_ci * 9fb726d48Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10fb726d48Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11fb726d48Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12fb726d48Sopenharmony_ci * See the License for the specific language governing permissions and 13fb726d48Sopenharmony_ci * limitations under the License. 14fb726d48Sopenharmony_ci */ 15fb726d48Sopenharmony_ci 16fb726d48Sopenharmony_ciimport { ColorUtils } from '../component/trace/base/ColorUtils'; 17fb726d48Sopenharmony_ciimport { BaseStruct } from './BaseStruct'; 18fb726d48Sopenharmony_ciimport { WakeupBean } from './WakeupBean'; 19fb726d48Sopenharmony_ci 20fb726d48Sopenharmony_ciexport class CpuStruct extends BaseStruct { 21fb726d48Sopenharmony_ci static cpuCount: number; //最大cpu数量 22fb726d48Sopenharmony_ci static hoverCpuStruct: CpuStruct | undefined; 23fb726d48Sopenharmony_ci static selectCpuStruct: CpuStruct | undefined; 24fb726d48Sopenharmony_ci static wakeupBean: WakeupBean | null | undefined; 25fb726d48Sopenharmony_ci cpu: number | undefined; 26fb726d48Sopenharmony_ci dur: number | undefined; 27fb726d48Sopenharmony_ci end_state: string | undefined; 28fb726d48Sopenharmony_ci id: number | undefined; 29fb726d48Sopenharmony_ci name: string | undefined; 30fb726d48Sopenharmony_ci priority: number | undefined; 31fb726d48Sopenharmony_ci processCmdLine: string | undefined; 32fb726d48Sopenharmony_ci processId: number | undefined; 33fb726d48Sopenharmony_ci processName: string | undefined; 34fb726d48Sopenharmony_ci schedId: number | undefined; 35fb726d48Sopenharmony_ci startTime: number | undefined; 36fb726d48Sopenharmony_ci tid: number | undefined; 37fb726d48Sopenharmony_ci type: string | undefined; 38fb726d48Sopenharmony_ci 39fb726d48Sopenharmony_ci static draw(cpuCtx: CanvasRenderingContext2D, cpuStruct: CpuStruct): void { 40fb726d48Sopenharmony_ci if (cpuStruct.frame) { 41fb726d48Sopenharmony_ci let cpuWidth = cpuStruct.frame.width || 0; 42fb726d48Sopenharmony_ci if (cpuStruct.processId === CpuStruct.hoverCpuStruct?.processId || !CpuStruct.hoverCpuStruct) { 43fb726d48Sopenharmony_ci cpuCtx.fillStyle = ColorUtils.colorForTid( 44fb726d48Sopenharmony_ci (cpuStruct.processId || 0) > 0 ? cpuStruct.processId || 0 : cpuStruct.tid || 0 45fb726d48Sopenharmony_ci ); 46fb726d48Sopenharmony_ci } else { 47fb726d48Sopenharmony_ci cpuCtx.fillStyle = '#e0e0e0'; 48fb726d48Sopenharmony_ci } 49fb726d48Sopenharmony_ci cpuCtx.fillRect(cpuStruct.frame.x, cpuStruct.frame.y, cpuWidth, cpuStruct.frame.height); 50fb726d48Sopenharmony_ci if (cpuWidth > textPadding * 2) { 51fb726d48Sopenharmony_ci let cpuProcess = `${cpuStruct.processName || 'Process'} [${cpuStruct.processId}]`; 52fb726d48Sopenharmony_ci let cpuThread = `${cpuStruct.name || 'Thread'} [${cpuStruct.tid}]`; 53fb726d48Sopenharmony_ci let processMeasure = cpuCtx.measureText(cpuProcess); 54fb726d48Sopenharmony_ci let threadMeasure = cpuCtx.measureText(cpuThread); 55fb726d48Sopenharmony_ci let pChartWidth = Math.round(processMeasure.width / cpuProcess.length); 56fb726d48Sopenharmony_ci let tChartWidth = Math.round(threadMeasure.width / cpuThread.length); 57fb726d48Sopenharmony_ci cpuCtx.fillStyle = '#ffffff'; 58fb726d48Sopenharmony_ci let y = cpuStruct.frame.height / 2 + cpuStruct.frame.y; 59fb726d48Sopenharmony_ci if (processMeasure.width < cpuWidth - textPadding * 2) { 60fb726d48Sopenharmony_ci let x1 = Math.floor(cpuWidth / 2 - processMeasure.width / 2 + cpuStruct.frame.x + textPadding); 61fb726d48Sopenharmony_ci cpuCtx.textBaseline = 'bottom'; 62fb726d48Sopenharmony_ci cpuCtx.fillText(cpuProcess, x1, y, cpuWidth - textPadding * 2); 63fb726d48Sopenharmony_ci } else if (cpuWidth - textPadding * 2 > pChartWidth * 4) { 64fb726d48Sopenharmony_ci let chatNum = (cpuWidth - textPadding * 2) / pChartWidth; 65fb726d48Sopenharmony_ci let x1 = cpuStruct.frame.x + textPadding; 66fb726d48Sopenharmony_ci cpuCtx.textBaseline = 'bottom'; 67fb726d48Sopenharmony_ci cpuCtx.fillText(`${cpuProcess.substring(0, chatNum - 4)}...`, x1, y, cpuWidth - textPadding * 2); 68fb726d48Sopenharmony_ci } 69fb726d48Sopenharmony_ci if (threadMeasure.width < cpuWidth - textPadding * 2) { 70fb726d48Sopenharmony_ci cpuCtx.textBaseline = 'top'; 71fb726d48Sopenharmony_ci let x2 = Math.floor(cpuWidth / 2 - threadMeasure.width / 2 + cpuStruct.frame.x + textPadding); 72fb726d48Sopenharmony_ci cpuCtx.fillText(cpuThread, x2, y + 2, cpuWidth - textPadding * 2); 73fb726d48Sopenharmony_ci } else if (cpuWidth - textPadding * 2 > tChartWidth * 4) { 74fb726d48Sopenharmony_ci let chatNum = (cpuWidth - textPadding * 2) / tChartWidth; 75fb726d48Sopenharmony_ci let x1 = cpuStruct.frame.x + textPadding; 76fb726d48Sopenharmony_ci cpuCtx.textBaseline = 'top'; 77fb726d48Sopenharmony_ci cpuCtx.fillText(`${cpuThread.substring(0, chatNum - 4)}...`, x1, y + 2, cpuWidth - textPadding * 2); 78fb726d48Sopenharmony_ci } 79fb726d48Sopenharmony_ci } 80fb726d48Sopenharmony_ci if (CpuStruct.selectCpuStruct && CpuStruct.equals(CpuStruct.selectCpuStruct, cpuStruct)) { 81fb726d48Sopenharmony_ci cpuCtx.strokeStyle = '#232c5d'; 82fb726d48Sopenharmony_ci cpuCtx.lineWidth = 2; 83fb726d48Sopenharmony_ci cpuCtx.strokeRect(cpuStruct.frame.x, cpuStruct.frame.y, cpuWidth - 2, cpuStruct.frame.height); 84fb726d48Sopenharmony_ci } 85fb726d48Sopenharmony_ci } 86fb726d48Sopenharmony_ci } 87fb726d48Sopenharmony_ci 88fb726d48Sopenharmony_ci static equals(d1: CpuStruct, d2: CpuStruct): boolean { 89fb726d48Sopenharmony_ci if ( 90fb726d48Sopenharmony_ci d1 && 91fb726d48Sopenharmony_ci d2 && 92fb726d48Sopenharmony_ci d1.cpu === d2.cpu && 93fb726d48Sopenharmony_ci d1.tid === d2.tid && 94fb726d48Sopenharmony_ci d1.processId === d2.processId && 95fb726d48Sopenharmony_ci d1.startTime === d2.startTime && 96fb726d48Sopenharmony_ci d1.dur === d2.dur 97fb726d48Sopenharmony_ci ) { 98fb726d48Sopenharmony_ci return true; 99fb726d48Sopenharmony_ci } else { 100fb726d48Sopenharmony_ci return false; 101fb726d48Sopenharmony_ci } 102fb726d48Sopenharmony_ci } 103fb726d48Sopenharmony_ci} 104fb726d48Sopenharmony_ci 105fb726d48Sopenharmony_ciconst textPadding = 2; 106