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 { PerfCall, PerfFile } from '../../bean/PerfProfile';
17import { info } from '../../../log/Log';
18import { SpHiPerf } from './SpHiPerf';
19import { procedurePool } from '../../database/Procedure';
20import { SpSystemTrace } from '../SpSystemTrace';
21import { queryPerfFiles } from '../../database/sql/Perf.sql';
22
23export class PerfDataQuery {
24  filesData: unknown = {};
25  callChainMap: Map<number, PerfCall> = new Map<number, PerfCall>();
26
27  async initPerfCache(): Promise<void> {
28    await this.initPerfCallChainMap();
29    await this.initPerfFiles();
30  }
31
32  async initPerfCallChainMap(): Promise<void> {
33    this.callChainMap.clear();
34  }
35
36  async initPerfFiles(): Promise<void> {
37    let files = await queryPerfFiles();
38    info('PerfFiles Data size is: ', files!.length);
39    files.forEach((file) => {
40      // @ts-ignore
41      this.filesData[file.fileId] = this.filesData[file.fileId] || [];
42      PerfFile.setFileName(file); // @ts-ignore
43      this.filesData[file.fileId].push(file);
44    });
45    const data = {
46      fValue: SpHiPerf.stringResult?.fValue,
47    };
48    let results = await new Promise<unknown>((resolve, reject) => {
49      procedurePool.submitWithName('logic0', 'perf-init', data, undefined, (res: unknown) => {
50        resolve(res);
51      });
52    }); // @ts-ignore
53    this.callChainMap = results as unknown;
54    info('Perf Files Data initialized');
55  }
56
57  getLibName(fileId: number, symbolId: number): string {
58    let name = 'unknown';
59    if (symbolId === -1) {
60      // @ts-ignore
61      if (this.filesData[fileId] && this.filesData[fileId].length > 0) {
62        // @ts-ignore
63        name = this.filesData[fileId][0].fileName;
64      }
65    } else {
66      // @ts-ignore
67      if (this.filesData[fileId] && this.filesData[fileId].length > symbolId) {
68        // @ts-ignore
69        name = this.filesData[fileId][symbolId].fileName;
70      }
71    }
72    return name.replace(/</g, '&lt;').replace(/>/g, '&gt;');
73  }
74}
75
76export const perfDataQuery = new PerfDataQuery();
77