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 { SpSystemTrace } from '../SpSystemTrace.js'; 17import { SpHiPerf } from './SpHiPerf.js'; 18import { SpCpuChart } from './SpCpuChart.js'; 19import { SpFreqChart } from './SpFreqChart.js'; 20import { SpFpsChart } from './SpFpsChart.js'; 21import { 22 getCpuUtilizationRate, queryAppStartupProcessIds, 23 queryDataDICT, 24 queryMemoryConfig, 25 queryTaskPoolCallStack, 26 queryThreadAndProcessName, 27 queryTotalTime, 28} from '../../database/SqlLite.js'; 29import { info } from '../../../log/Log.js'; 30import { SpNativeMemoryChart } from './SpNativeMemoryChart.js'; 31import { SpAbilityMonitorChart } from './SpAbilityMonitorChart.js'; 32import { SpProcessChart } from './SpProcessChart.js'; 33import { perfDataQuery } from './PerfDataQuery.js'; 34import { SpVirtualMemChart } from './SpVirtualMemChart.js'; 35import { SpFileSystemChart } from './SpFileSystemChart.js'; 36import { SpSdkChart } from './SpSdkChart.js'; 37import { SpHiSysEventChart } from './SpHiSysEventChart.js'; 38import { VmTrackerChart } from './SpVmTrackerChart.js'; 39import { SpClockChart } from './SpClockChart.js'; 40import { SpIrqChart } from './SpIrqChart.js'; 41import { renders } from '../../database/ui-worker/ProcedureWorker.js'; 42import { EmptyRender } from '../../database/ui-worker/ProcedureWorkerCPU.js'; 43import { TraceRow } from '../trace/base/TraceRow.js'; 44import { SpFrameTimeChart } from './SpFrameTimeChart.js'; 45import { Utils } from '../trace/base/Utils.js'; 46import { SpArkTsChart } from './SpArkTsChart.js'; 47import { MemoryConfig } from '../../bean/MemoryConfig.js'; 48import { FlagsConfig } from '../SpFlags.js'; 49 50export class SpChartManager { 51 static APP_STARTUP_PID_ARR: Array<number> = []; 52 53 private trace: SpSystemTrace; 54 public perf: SpHiPerf; 55 private cpu: SpCpuChart; 56 private freq: SpFreqChart; 57 private virtualMemChart: SpVirtualMemChart; 58 private fps: SpFpsChart; 59 private nativeMemory: SpNativeMemoryChart; 60 private abilityMonitor: SpAbilityMonitorChart; 61 private process: SpProcessChart; 62 private fileSystem: SpFileSystemChart; 63 private sdkChart: SpSdkChart; 64 private hiSyseventChart: SpHiSysEventChart; 65 private smapsChart: VmTrackerChart; 66 private clockChart: SpClockChart; 67 private irqChart: SpIrqChart; 68 frameTimeChart: SpFrameTimeChart; 69 public arkTsChart: SpArkTsChart; 70 71 constructor(trace: SpSystemTrace) { 72 this.trace = trace; 73 this.perf = new SpHiPerf(trace); 74 this.fileSystem = new SpFileSystemChart(trace); 75 this.cpu = new SpCpuChart(trace); 76 this.freq = new SpFreqChart(trace); 77 this.virtualMemChart = new SpVirtualMemChart(trace); 78 this.fps = new SpFpsChart(trace); 79 this.nativeMemory = new SpNativeMemoryChart(trace); 80 this.abilityMonitor = new SpAbilityMonitorChart(trace); 81 this.process = new SpProcessChart(trace); 82 this.sdkChart = new SpSdkChart(trace); 83 this.hiSyseventChart = new SpHiSysEventChart(trace); 84 this.smapsChart = new VmTrackerChart(trace); 85 this.clockChart = new SpClockChart(trace); 86 this.irqChart = new SpIrqChart(trace); 87 this.frameTimeChart = new SpFrameTimeChart(trace); 88 this.arkTsChart = new SpArkTsChart(trace); 89 } 90 91 async init(progress: Function) { 92 progress('load data dict', 50); 93 SpSystemTrace.DATA_DICT.clear(); 94 SpChartManager.APP_STARTUP_PID_ARR = []; 95 let dict = await queryDataDICT(); 96 if (FlagsConfig.getFlagsConfigEnableStatus('AppStartup')) { 97 let appStartUpPids = await queryAppStartupProcessIds(); 98 appStartUpPids.forEach(it => SpChartManager.APP_STARTUP_PID_ARR.push(it.pid)); 99 } 100 await this.initTraceConfig(); 101 dict.map((d) => SpSystemTrace.DATA_DICT.set(d['id'], d['data'])); 102 SpSystemTrace.DATA_TASK_POOL_CALLSTACK.clear(); 103 let taskPoolCallStack = await queryTaskPoolCallStack(); 104 taskPoolCallStack.map((d) => SpSystemTrace.DATA_TASK_POOL_CALLSTACK.set(d.id, d)); 105 progress('time range', 65); 106 await this.initTotalTime(); 107 let ptArr = await queryThreadAndProcessName(); 108 this.handleProcessThread(ptArr); 109 info('timerShaftEL Data initialized'); 110 progress('cpu', 70); 111 await this.cpu.init(); 112 info('cpu Data initialized'); 113 progress('process/thread state', 73); 114 await this.cpu.initProcessThreadStateData(progress); 115 if (FlagsConfig.getFlagsConfigEnableStatus('SchedulingAnalysis')) { 116 await this.cpu.initCpuIdle0Data(progress); 117 await this.cpu.initSchedulingPTData(progress); 118 await this.cpu.initSchedulingFreqData(progress); 119 } 120 info('ProcessThreadState Data initialized'); 121 progress('cpu rate', 75); 122 await this.initCpuRate(); 123 info('Cpu Rate Data initialized'); 124 progress('cpu freq', 80); 125 await this.freq.init(); 126 progress('Clock init', 82); 127 await this.clockChart.init(); 128 progress('Irq init', 84); 129 await this.irqChart.init(); 130 info('Cpu Freq Data initialized'); 131 await this.virtualMemChart.init(); 132 progress('fps', 85); 133 await this.fps.init(); 134 info('FPS Data initialized'); 135 progress('native memory', 87); 136 await this.nativeMemory.initChart(); 137 info('Native Memory Data initialized'); 138 progress('ability monitor', 88); 139 await this.abilityMonitor.init(); 140 progress('hiSysevent', 88.2); 141 await this.hiSyseventChart.init(); 142 info('Perf Files Data initialized'); 143 progress('vm tracker', 88.4); 144 await this.smapsChart.init(); 145 progress('sdk', 88.6); 146 await this.sdkChart.init(); 147 progress('perf', 88.8); 148 await this.perf!.init(); 149 progress('file system', 89); 150 await this.fileSystem!.init(); 151 info('Ability Monitor Data initialized'); 152 await perfDataQuery.initPerfCache(); 153 info('HiPerf Data initialized'); 154 progress('ark ts', 90); 155 await this.arkTsChart.initFolder(); 156 info('ark ts initialized'); 157 await this.frameTimeChart.init(); 158 progress('process', 92); 159 await this.process.initAsyncFuncData(); 160 await this.process.initDeliverInputEvent(); 161 await this.process.init(); 162 info('Process Data initialized'); 163 progress('display', 95); 164 } 165 166 async importSoFileUpdate() { 167 SpSystemTrace.DATA_DICT.clear(); 168 let dict = await queryDataDICT(); 169 dict.map((d) => SpSystemTrace.DATA_DICT.set(d['id'], d['data'])); 170 await perfDataQuery.initPerfCache(); 171 await this.nativeMemory.initNativeMemory(); 172 await this.fileSystem.initFileCallchain(); 173 this.perf.updateChartData(); 174 } 175 176 handleProcessThread(arr: { id: number; name: string; type: string }[]) { 177 Utils.PROCESS_MAP.clear(); 178 Utils.THREAD_MAP.clear(); 179 for (let pt of arr) { 180 if (pt.type === 'p') { 181 Utils.PROCESS_MAP.set(pt.id, pt.name); 182 } else { 183 Utils.THREAD_MAP.set(pt.id, pt.name); 184 } 185 } 186 } 187 188 initTotalTime = async () => { 189 let res = await queryTotalTime(); 190 if (this.trace.timerShaftEL) { 191 let total = res[0].total; 192 let startNS = res[0].recordStartNS; 193 let endNS = res[0].recordEndNS; 194 if (total === 0 && startNS === endNS) { 195 total = 1; 196 endNS = startNS + 1; 197 } 198 this.trace.timerShaftEL.totalNS = total; 199 this.trace.timerShaftEL.getRangeRuler()!.drawMark = true; 200 this.trace.timerShaftEL.setRangeNS(0, total); 201 (window as any).recordStartNS = startNS; 202 (window as any).recordEndNS = endNS; 203 (window as any).totalNS = total; 204 this.trace.timerShaftEL.loadComplete = true; 205 } 206 }; 207 208 initCpuRate = async () => { 209 let rates = await getCpuUtilizationRate(0, this.trace.timerShaftEL?.totalNS || 0); 210 if (this.trace.timerShaftEL) this.trace.timerShaftEL.cpuUsage = rates; 211 info('Cpu UtilizationRate data size is: ', rates.length); 212 }; 213 214 initTraceConfig = async (): Promise<void> => { 215 queryMemoryConfig().then((result) => { 216 if (result && result.length > 0) { 217 const config = result[0]; 218 MemoryConfig.getInstance().updateConfig(config.pid, config.iPid, config.processName, config.interval); 219 } 220 }); 221 }; 222} 223 224export const FolderSupplier = () => { 225 return () => new Promise<Array<any>>((resolve) => resolve([])); 226}; 227export const FolderThreadHandler = (row: TraceRow<any>, trace: SpSystemTrace) => { 228 return (useCache: boolean) => { 229 row.canvasSave(trace.canvasPanelCtx!); 230 if (row.expansion) { 231 trace.canvasPanelCtx?.clearRect(0, 0, row.frame.width, row.frame.height); 232 } else { 233 (renders['empty'] as EmptyRender).renderMainThread( 234 { 235 context: trace.canvasPanelCtx, 236 useCache: useCache, 237 type: ``, 238 }, 239 row 240 ); 241 } 242 row.canvasRestore(trace.canvasPanelCtx!); 243 }; 244}; 245