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 { getThreadPool } from '../../SqlLite';
16import { TraceRow } from '../../../component/trace/base/TraceRow';
17import { FuncStruct } from '../../ui-worker/ProcedureWorkerFunc';
18import { Utils } from '../../../component/trace/base/Utils';
19
20export function funcDataSender(tid: number, ipid: number, row: TraceRow<FuncStruct>, traceId?: string):
21  Promise<FuncStruct[] | boolean>
22{
23  let trafic: number = TraficEnum.Memory;
24  let width = row.clientWidth || row.parentRowEl!.clientWidth - CHART_OFFSET_LEFT;
25  if (trafic === TraficEnum.SharedArrayBuffer && !row.sharedArrayBuffers) {
26    row.sharedArrayBuffers = {
27      startTs: new SharedArrayBuffer(Float64Array.BYTES_PER_ELEMENT * MAX_COUNT),
28      dur: new SharedArrayBuffer(Float64Array.BYTES_PER_ELEMENT * MAX_COUNT),
29      argsetid: new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT * MAX_COUNT),
30      depth: new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT * MAX_COUNT),
31      id: new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT * MAX_COUNT),
32      nofinish: new SharedArrayBuffer(Uint8Array.BYTES_PER_ELEMENT * MAX_COUNT),
33    };
34  }
35  return new Promise((resolve, reject): void => {
36    getThreadPool(traceId).submitProto(
37      QueryEnum.FuncData,
38      {
39        tid: tid,
40        ipid: ipid,
41        startNS: TraceRow.range?.startNS || 0,
42        endNS: TraceRow.range?.endNS || 0,
43        recordStartNS: Utils.getInstance().getRecordStartNS(traceId),
44        recordEndNS: Utils.getInstance().getRecordEndNS(traceId),
45        width: width,
46        expand: row.funcExpand,
47        trafic: trafic,
48        sharedArrayBuffers: row.sharedArrayBuffers,
49      },
50      (res: unknown, len: number, transfer: boolean, isEmpty: boolean) => {
51        if (isEmpty) {
52          resolve(true);
53        } else {
54          resolve(arrayBufferHandler(transfer ? res : row.sharedArrayBuffers, len));
55        }
56      }
57    );
58  });
59}
60
61function arrayBufferHandler(buffers: unknown, len: number): FuncStruct[] {
62  let outArr: unknown[] = []; //@ts-ignore
63  let startTs = new Float64Array(buffers.startTs); //@ts-ignore
64  let dur = new Float64Array(buffers.dur); //@ts-ignore
65  let argsetid = new Int32Array(buffers.argsetid); //@ts-ignore
66  let depth = new Int32Array(buffers.depth); //@ts-ignore
67  let id = new Int32Array(buffers.id); //@ts-ignore
68  let nofinish = new Uint8Array(buffers.nofinish);
69  for (let i = 0; i < len; i++) {
70    outArr.push({
71      startTs: startTs[i],
72      dur: dur[i],
73      argsetid: argsetid[i],
74      depth: depth[i],
75      id: id[i],
76      nofinish: nofinish[i] === 1,
77    });
78  } //@ts-ignore
79  return outArr;
80}
81