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 { CHART_OFFSET_LEFT, MAX_COUNT, QueryEnum, TraficEnum } from '../utils/QueryEnum';
15import { threadPool } from '../../SqlLite';
16import { TraceRow } from '../../../component/trace/base/TraceRow';
17import { SoStruct } from '../../ui-worker/ProcedureWorkerSoInit';
18
19export function processSoInitDataSender(pid: number, row: TraceRow<SoStruct>): Promise<SoStruct[]> {
20  let trafic: number = TraficEnum.ProtoBuffer;
21  let width = row.clientWidth - CHART_OFFSET_LEFT;
22  if (trafic === TraficEnum.SharedArrayBuffer && !row.sharedArrayBuffers) {
23    row.sharedArrayBuffers = {
24      id: new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT * MAX_COUNT),
25      depth: new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT * MAX_COUNT),
26      pid: new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT * MAX_COUNT),
27      tid: new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT * MAX_COUNT),
28      itid: new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT * MAX_COUNT),
29      startTs: new SharedArrayBuffer(Float64Array.BYTES_PER_ELEMENT * MAX_COUNT),
30      dur: new SharedArrayBuffer(Float64Array.BYTES_PER_ELEMENT * MAX_COUNT),
31    };
32  }
33  return new Promise((resolve): void => {
34    threadPool.submitProto(
35      QueryEnum.ProcessSoInitData,
36      {
37        startNS: TraceRow.range?.startNS || 0,
38        endNS: TraceRow.range?.endNS || 0,
39        recordStartNS: window.recordStartNS,
40        recordEndNS: window.recordEndNS,
41        t: Date.now(),
42        pid: pid,
43        width: width,
44        trafic: trafic,
45        sharedArrayBuffers: row.sharedArrayBuffers,
46      },
47      (res: unknown, len: number, transfer: boolean): void => {
48        resolve(arrayBufferHandler(transfer ? res : row.sharedArrayBuffers, len));
49      }
50    );
51  });
52}
53
54function arrayBufferHandler(buffers: unknown, len: number): SoStruct[] {
55  let outArr: SoStruct[] = []; //@ts-ignore
56  let id = new Int32Array(buffers.id); //@ts-ignore
57  let depth = new Int32Array(buffers.depth); //@ts-ignore
58  let pid = new Int32Array(buffers.pid); //@ts-ignore
59  let tid = new Int32Array(buffers.tid); //@ts-ignore
60  let itid = new Int32Array(buffers.itid); //@ts-ignore
61  let startTs = new Float64Array(buffers.startTs); //@ts-ignore
62  let dur = new Float64Array(buffers.dur);
63  for (let i = 0; i < len; i++) {
64    outArr.push({
65      id: id[i],
66      depth: depth[i],
67      pid: pid[i],
68      tid: tid[i],
69      itid: itid[i],
70      startTs: startTs[i],
71      dur: dur[i],
72    } as unknown as SoStruct);
73  }
74  return outArr;
75}
76