1// Copyright (c) 2021 Huawei Device Co., Ltd.
2// Licensed under the Apache License, Version 2.0 (the "License");
3// you may not use this file except in compliance with the License.
4// You may obtain a copy of the License at
5//
6//     http://www.apache.org/licenses/LICENSE-2.0
7//
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13import { QueryEnum, TraficEnum } from '../utils/QueryEnum';
14import { threadPool } from '../../SqlLite';
15import { TraceRow } from '../../../component/trace/base/TraceRow';
16import { SpSystemTrace } from '../../../component/SpSystemTrace';
17
18export function hiperfCallChartDataSender(
19  // @ts-ignore
20  row: TraceRow<unknown>,
21  setting: {
22    startTime: number;
23    eventTypeId: number;
24    type: number;
25    id: number;
26  }
27): Promise<unknown> {
28  return new Promise((resolve, reject) => {
29    threadPool.submitProto(
30      QueryEnum.HiperfCallChart,
31      {
32        startNS: TraceRow.range?.startNS || 0,
33        endNS: TraceRow.range?.endNS || 0,
34        totalNS: (TraceRow.range?.endNS || 0) - (TraceRow.range?.startNS || 0),
35        frame: row.frame,
36        expand: row.funcExpand,
37        isComplete: row.isComplete,
38        startTime: setting.startTime,
39        eventTypeId: setting.eventTypeId,
40        type: setting.type,
41        id: setting.id,
42      },
43      (res: unknown, len: number): void => {
44        resolve(arrayBufferHandler(res, len));
45      }
46    );
47  });
48}
49
50export function hiperfCallStackCacheSender(): Promise<unknown> {
51  return new Promise((resolve, reject) => {
52    threadPool.submitProto(
53      QueryEnum.HiperfCallStack,
54      {
55        recordStartNS: window.recordStartNS,
56        isCache: true,
57        trafic: TraficEnum.TransferArrayBuffer,
58      },
59      (res: unknown, len: number): void => {
60        resolve('ok');
61      }
62    );
63  });
64}
65
66export function hiperfCallChartDataCacheSender(): Promise<unknown> {
67  return new Promise((resolve, reject) => {
68    threadPool.submitProto(
69      QueryEnum.HiperfCallChart,
70      {
71        recordStartNS: window.recordStartNS,
72        recordEndNS: window.recordEndNS,
73        trafic: TraficEnum.TransferArrayBuffer,
74        isCache: true,
75      },
76      (res: unknown, len: number): void => {
77        resolve('ok');
78      }
79    );
80  });
81}
82
83function arrayBufferHandler(
84  res: unknown,
85  len: number
86): {
87  maxDepth: unknown;
88  dataList: unknown[];
89} {
90  // @ts-ignore
91  let startTs = new Float64Array(res.startTs);
92  // @ts-ignore
93  let dur = new Float64Array(res.dur);
94  // @ts-ignore
95  let depth = new Int32Array(res.depth);
96  // @ts-ignore
97  let eventCount = new Int32Array(res.eventCount);
98  // @ts-ignore
99  let symbolId = new Int32Array(res.symbolId);
100  // @ts-ignore
101  let fileId = new Int32Array(res.fileId);
102  // @ts-ignore
103  let callchainId = new Int32Array(res.callchainId);
104  // @ts-ignore
105  let selfDur = new Int32Array(res.selfDur);
106  // @ts-ignore
107  let name = new Int32Array(res.name);
108  let outArr: unknown[] = [];
109  for (let i = 0; i < len; i++) {
110    outArr.push({
111      startTime: startTs[i],
112      totalTime: dur[i],
113      endTime: startTs[i] + dur[i],
114      depth: depth[i],
115      eventCount: eventCount[i],
116      fileId: fileId[i],
117      symbolId: symbolId[i],
118      callchain_id: callchainId[i],
119      selfDur: selfDur[i],
120      name: SpSystemTrace.DATA_DICT.get(name[i]),
121    } as unknown);
122  }
123  return {
124    // @ts-ignore
125    maxDepth: res.maxDepth,
126    dataList: outArr,
127  };
128}
129