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