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