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