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 { TraceRow } from '../../component/trace/base/TraceRow';
17import { threadPool } from '../SqlLite';
18import { JsCpuProfilerStruct } from '../ui-worker/ProcedureWorkerCpuProfiler';
19import { CHART_OFFSET_LEFT, QueryEnum, TraficEnum } from './utils/QueryEnum';
20
21export function cpuProfilerDataSender(row: TraceRow<JsCpuProfilerStruct>): Promise<unknown> {
22  let trafic: number = TraficEnum.ProtoBuffer;
23  let width = row.clientWidth - CHART_OFFSET_LEFT;
24  return new Promise((resolve, reject) => {
25    threadPool.submitProto(
26      QueryEnum.CpuProfilerData,
27      {
28        startNS: TraceRow.range?.startNS || 0,
29        endNS: TraceRow.range?.endNS || 0,
30        recordStartNS: window.recordStartNS,
31        recordEndNS: window.recordEndNS,
32        width: width,
33        trafic: trafic,
34      },
35      (res: unknown, len: number): void => {
36        resolve(arrayBufferHandler(res, len));
37      }
38    );
39  });
40}
41
42function arrayBufferHandler(
43  res: unknown,
44  len: number
45): {
46  maxDepth: unknown;
47  dataList: unknown[];
48} {
49  let outArr: unknown[] = [];
50  // @ts-ignore
51  let column = new Int32Array(res.column);
52  // @ts-ignore
53  let depth = new Int32Array(res.depth);
54  // @ts-ignore
55  let endTime = new Float64Array(res.endTime);
56  // @ts-ignore
57  let id = new Int32Array(res.id);
58  // @ts-ignore
59  let line = new Int32Array(res.line);
60  // @ts-ignore
61  let nameId = new Int32Array(res.nameId);
62  // @ts-ignore
63  let parentId = new Int32Array(res.parentId);
64  // @ts-ignore
65  let selfTime = new Float64Array(res.selfTime);
66  // @ts-ignore
67  let startTime = new Float64Array(res.startTime);
68  // @ts-ignore
69  let totalTime = new Float64Array(res.totalTime);
70  // @ts-ignore
71  let urlId = new Int32Array(res.urlId);
72  for (let i = 0; i < len; i++) {
73    outArr.push({
74      column: column[i],
75      depth: depth[i],
76      endTime: endTime[i],
77      id: id[i],
78      line: line[i],
79      nameId: nameId[i],
80      parentId: parentId[i],
81      // @ts-ignore
82      samplesIds: res.samplesIds[i],
83      selfTime: selfTime[i],
84      startTime: startTime[i],
85      totalTime: totalTime[i],
86      urlId: urlId[i],
87      // @ts-ignore
88      childrenIds: res.childrenIds[i],
89    } as unknown);
90  }
91  return {
92    // @ts-ignore
93    maxDepth: res.maxDepth,
94    dataList: outArr,
95  };
96}
97