1fb726d48Sopenharmony_ci// Copyright (c) 2021 Huawei Device Co., Ltd.
2fb726d48Sopenharmony_ci// Licensed under the Apache License, Version 2.0 (the "License");
3fb726d48Sopenharmony_ci// you may not use this file except in compliance with the License.
4fb726d48Sopenharmony_ci// You may obtain a copy of the License at
5fb726d48Sopenharmony_ci//
6fb726d48Sopenharmony_ci//     http://www.apache.org/licenses/LICENSE-2.0
7fb726d48Sopenharmony_ci//
8fb726d48Sopenharmony_ci// Unless required by applicable law or agreed to in writing, software
9fb726d48Sopenharmony_ci// distributed under the License is distributed on an "AS IS" BASIS,
10fb726d48Sopenharmony_ci// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11fb726d48Sopenharmony_ci// See the License for the specific language governing permissions and
12fb726d48Sopenharmony_ci// limitations under the License.
13fb726d48Sopenharmony_ci
14fb726d48Sopenharmony_ciimport { TraceRow } from '../../../component/trace/base/TraceRow';
15fb726d48Sopenharmony_ciimport { CHART_OFFSET_LEFT, MAX_COUNT, QueryEnum, TraficEnum } from '../utils/QueryEnum';
16fb726d48Sopenharmony_ciimport { threadPool } from '../../SqlLite';
17fb726d48Sopenharmony_ciimport { HiPerfProcessStruct } from '../../ui-worker/hiperf/ProcedureWorkerHiPerfProcess2';
18fb726d48Sopenharmony_ci
19fb726d48Sopenharmony_ciexport function hiperfProcessDataSender(
20fb726d48Sopenharmony_ci  pid: number,
21fb726d48Sopenharmony_ci  drawType: number,
22fb726d48Sopenharmony_ci  maxCpu: number,
23fb726d48Sopenharmony_ci  intervalPerf: number,
24fb726d48Sopenharmony_ci  scale: number,
25fb726d48Sopenharmony_ci  // @ts-ignore
26fb726d48Sopenharmony_ci  row: TraceRow<unknown>
27fb726d48Sopenharmony_ci  // @ts-ignore
28fb726d48Sopenharmony_ci): Promise<unknown[]> {
29fb726d48Sopenharmony_ci  let trafic: number = TraficEnum.ProtoBuffer;
30fb726d48Sopenharmony_ci  let width = row.clientWidth - CHART_OFFSET_LEFT;
31fb726d48Sopenharmony_ci  if (trafic === TraficEnum.SharedArrayBuffer && !row.sharedArrayBuffers) {
32fb726d48Sopenharmony_ci    row.sharedArrayBuffers = {
33fb726d48Sopenharmony_ci      startNS: new SharedArrayBuffer(Float64Array.BYTES_PER_ELEMENT * MAX_COUNT),
34fb726d48Sopenharmony_ci      eventCount: new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT * MAX_COUNT),
35fb726d48Sopenharmony_ci      sampleCount: new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT * MAX_COUNT),
36fb726d48Sopenharmony_ci      eventTypeId: new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT * MAX_COUNT),
37fb726d48Sopenharmony_ci      callChainId: new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT * MAX_COUNT),
38fb726d48Sopenharmony_ci      height: new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT * MAX_COUNT),
39fb726d48Sopenharmony_ci    };
40fb726d48Sopenharmony_ci  }
41fb726d48Sopenharmony_ci  return new Promise((resolve): void => {
42fb726d48Sopenharmony_ci    threadPool.submitProto(
43fb726d48Sopenharmony_ci      QueryEnum.HiperfProcessData,
44fb726d48Sopenharmony_ci      {
45fb726d48Sopenharmony_ci        intervalPerf: intervalPerf,
46fb726d48Sopenharmony_ci        startNS: TraceRow.range?.startNS || 0,
47fb726d48Sopenharmony_ci        endNS: TraceRow.range?.endNS || 0,
48fb726d48Sopenharmony_ci        recordStartNS: window.recordStartNS,
49fb726d48Sopenharmony_ci        recordEndNS: window.recordEndNS,
50fb726d48Sopenharmony_ci        width: width,
51fb726d48Sopenharmony_ci        trafic: trafic,
52fb726d48Sopenharmony_ci        sharedArrayBuffers: row.sharedArrayBuffers,
53fb726d48Sopenharmony_ci        pid: pid,
54fb726d48Sopenharmony_ci        maxCpuCount: maxCpu,
55fb726d48Sopenharmony_ci        scale: scale,
56fb726d48Sopenharmony_ci        drawType: drawType,
57fb726d48Sopenharmony_ci      },
58fb726d48Sopenharmony_ci      // @ts-ignore
59fb726d48Sopenharmony_ci      (res: unknown, len: number, transfer: boolean): void => {
60fb726d48Sopenharmony_ci        resolve(arrayBufferHandler(transfer ? res : row.sharedArrayBuffers, len));
61fb726d48Sopenharmony_ci      }
62fb726d48Sopenharmony_ci    );
63fb726d48Sopenharmony_ci  });
64fb726d48Sopenharmony_ci}
65fb726d48Sopenharmony_ci
66fb726d48Sopenharmony_cifunction arrayBufferHandler(buffers: unknown, len: number): HiPerfProcessStruct[] {
67fb726d48Sopenharmony_ci  // @ts-ignore
68fb726d48Sopenharmony_ci  let outArr: HiPerfProcessStruct[] = [];
69fb726d48Sopenharmony_ci  // @ts-ignore
70fb726d48Sopenharmony_ci  let startNS = new Float64Array(buffers.startNS);
71fb726d48Sopenharmony_ci  // @ts-ignore
72fb726d48Sopenharmony_ci  let eventCount = new Int32Array(buffers.eventCount);
73fb726d48Sopenharmony_ci  // @ts-ignore
74fb726d48Sopenharmony_ci  let sampleCount = new Int32Array(buffers.sampleCount);
75fb726d48Sopenharmony_ci  // @ts-ignore
76fb726d48Sopenharmony_ci  let eventTypeId = new Int32Array(buffers.eventTypeId);
77fb726d48Sopenharmony_ci  // @ts-ignore
78fb726d48Sopenharmony_ci  let callChainId = new Int32Array(buffers.callChainId);
79fb726d48Sopenharmony_ci  // @ts-ignore
80fb726d48Sopenharmony_ci  let height = new Int32Array(buffers.height);
81fb726d48Sopenharmony_ci  for (let i = 0; i < len; i++) {
82fb726d48Sopenharmony_ci    outArr.push({
83fb726d48Sopenharmony_ci      startNS: startNS[i],
84fb726d48Sopenharmony_ci      event_count: eventCount[i],
85fb726d48Sopenharmony_ci      sampleCount: sampleCount[i],
86fb726d48Sopenharmony_ci      event_type_id: eventTypeId[i],
87fb726d48Sopenharmony_ci      callchain_id: callChainId[i],
88fb726d48Sopenharmony_ci      height: height[i],
89fb726d48Sopenharmony_ci      dur: 10_000_000,
90fb726d48Sopenharmony_ci    } as unknown as HiPerfProcessStruct);
91fb726d48Sopenharmony_ci  }
92fb726d48Sopenharmony_ci  return outArr;
93fb726d48Sopenharmony_ci}
94