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.
13
14import { TraceRow } from '../../../component/trace/base/TraceRow';
15import { CHART_OFFSET_LEFT, MAX_COUNT, QueryEnum, TraficEnum } from '../utils/QueryEnum';
16import { threadPool } from '../../SqlLite';
17import { HiPerfProcessStruct } from '../../ui-worker/hiperf/ProcedureWorkerHiPerfProcess2';
18
19export function hiperfProcessDataSender(
20  pid: number,
21  drawType: number,
22  maxCpu: number,
23  intervalPerf: number,
24  scale: number,
25  // @ts-ignore
26  row: TraceRow<unknown>
27  // @ts-ignore
28): Promise<unknown[]> {
29  let trafic: number = TraficEnum.ProtoBuffer;
30  let width = row.clientWidth - CHART_OFFSET_LEFT;
31  if (trafic === TraficEnum.SharedArrayBuffer && !row.sharedArrayBuffers) {
32    row.sharedArrayBuffers = {
33      startNS: new SharedArrayBuffer(Float64Array.BYTES_PER_ELEMENT * MAX_COUNT),
34      eventCount: new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT * MAX_COUNT),
35      sampleCount: new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT * MAX_COUNT),
36      eventTypeId: new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT * MAX_COUNT),
37      callChainId: new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT * MAX_COUNT),
38      height: new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT * MAX_COUNT),
39    };
40  }
41  return new Promise((resolve): void => {
42    threadPool.submitProto(
43      QueryEnum.HiperfProcessData,
44      {
45        intervalPerf: intervalPerf,
46        startNS: TraceRow.range?.startNS || 0,
47        endNS: TraceRow.range?.endNS || 0,
48        recordStartNS: window.recordStartNS,
49        recordEndNS: window.recordEndNS,
50        width: width,
51        trafic: trafic,
52        sharedArrayBuffers: row.sharedArrayBuffers,
53        pid: pid,
54        maxCpuCount: maxCpu,
55        scale: scale,
56        drawType: drawType,
57      },
58      // @ts-ignore
59      (res: unknown, len: number, transfer: boolean): void => {
60        resolve(arrayBufferHandler(transfer ? res : row.sharedArrayBuffers, len));
61      }
62    );
63  });
64}
65
66function arrayBufferHandler(buffers: unknown, len: number): HiPerfProcessStruct[] {
67  // @ts-ignore
68  let outArr: HiPerfProcessStruct[] = [];
69  // @ts-ignore
70  let startNS = new Float64Array(buffers.startNS);
71  // @ts-ignore
72  let eventCount = new Int32Array(buffers.eventCount);
73  // @ts-ignore
74  let sampleCount = new Int32Array(buffers.sampleCount);
75  // @ts-ignore
76  let eventTypeId = new Int32Array(buffers.eventTypeId);
77  // @ts-ignore
78  let callChainId = new Int32Array(buffers.callChainId);
79  // @ts-ignore
80  let height = new Int32Array(buffers.height);
81  for (let i = 0; i < len; i++) {
82    outArr.push({
83      startNS: startNS[i],
84      event_count: eventCount[i],
85      sampleCount: sampleCount[i],
86      event_type_id: eventTypeId[i],
87      callchain_id: callChainId[i],
88      height: height[i],
89      dur: 10_000_000,
90    } as unknown as HiPerfProcessStruct);
91  }
92  return outArr;
93}
94