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