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 { AppStartupStruct } from '../../ui-worker/ProcedureWorkerAppStartup';
18
19export function processStartupDataSender(pid: number, row: TraceRow<AppStartupStruct>): Promise<AppStartupStruct[]> {
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      startName: new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT * MAX_COUNT),
25      pid: new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT * MAX_COUNT),
26      tid: new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT * MAX_COUNT),
27      itid: new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT * MAX_COUNT),
28      startTs: new SharedArrayBuffer(Float64Array.BYTES_PER_ELEMENT * MAX_COUNT),
29      dur: new SharedArrayBuffer(Float64Array.BYTES_PER_ELEMENT * MAX_COUNT),
30    };
31  }
32  return new Promise((resolve) => {
33    threadPool.submitProto(
34      QueryEnum.ProcessStartupData,
35      {
36        startNS: TraceRow.range?.startNS || 0,
37        endNS: TraceRow.range?.endNS || 0,
38        recordStartNS: window.recordStartNS,
39        recordEndNS: window.recordEndNS,
40        t: Date.now(),
41        width: width,
42        pid: pid,
43        trafic: trafic,
44        sharedArrayBuffers: row.sharedArrayBuffers,
45      },
46      (res: unknown, len: number, transfer: boolean) => {
47        resolve(arrayBufferHandler(transfer ? res : row.sharedArrayBuffers, len));
48      }
49    );
50  });
51}
52
53function arrayBufferHandler(buffers: unknown, len: number): AppStartupStruct[] {
54  //@ts-ignore
55  let startName = new Int32Array(buffers.startName); //@ts-ignore
56  let pid = new Int32Array(buffers.pid); //@ts-ignore
57  let tid = new Int32Array(buffers.tid); //@ts-ignore
58  let itid = new Int32Array(buffers.itid); //@ts-ignore
59  let startTs = new Float64Array(buffers.startTs); //@ts-ignore
60  let dur = new Float64Array(buffers.dur);
61  let outArr: AppStartupStruct[] = [];
62  for (let i = 0; i < len; i++) {
63    outArr.push({
64      startName: startName[i],
65      pid: pid[i],
66      tid: tid[i],
67      itid: itid[i],
68      startTs: startTs[i],
69      dur: dur[i],
70    } as unknown as AppStartupStruct);
71  }
72  return outArr;
73}
74
75function protoBufferHandler(res: ArrayBuffer, len: number): AppStartupStruct[] {
76  return [];
77}
78