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