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