1fb726d48Sopenharmony_ci// Copyright (c) 2021 Huawei Device Co., Ltd.
2fb726d48Sopenharmony_ci// Licensed under the Apache License, Version 2.0 (the "License");
3fb726d48Sopenharmony_ci// you may not use this file except in compliance with the License.
4fb726d48Sopenharmony_ci// You may obtain a copy of the License at
5fb726d48Sopenharmony_ci//
6fb726d48Sopenharmony_ci//     http://www.apache.org/licenses/LICENSE-2.0
7fb726d48Sopenharmony_ci//
8fb726d48Sopenharmony_ci// Unless required by applicable law or agreed to in writing, software
9fb726d48Sopenharmony_ci// distributed under the License is distributed on an "AS IS" BASIS,
10fb726d48Sopenharmony_ci// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11fb726d48Sopenharmony_ci// See the License for the specific language governing permissions and
12fb726d48Sopenharmony_ci// limitations under the License.
13fb726d48Sopenharmony_ci
14fb726d48Sopenharmony_ciimport { CpuStruct } from '../ui-worker/cpu/ProcedureWorkerCPU';
15fb726d48Sopenharmony_ciimport { CHART_OFFSET_LEFT, MAX_COUNT, QueryEnum, TraficEnum } from './utils/QueryEnum';
16fb726d48Sopenharmony_ciimport { getThreadPool } from '../SqlLite';
17fb726d48Sopenharmony_ciimport { TraceRow } from '../../component/trace/base/TraceRow';
18fb726d48Sopenharmony_ciimport { Utils } from '../../component/trace/base/Utils';
19fb726d48Sopenharmony_ci
20fb726d48Sopenharmony_ciexport function cpuDataSender(cpu: number, row: TraceRow<CpuStruct>, traceId?: string): Promise<CpuStruct[]> {
21fb726d48Sopenharmony_ci  let trafic: number = TraficEnum.Memory;
22fb726d48Sopenharmony_ci  let width = row.clientWidth - CHART_OFFSET_LEFT;
23fb726d48Sopenharmony_ci  if (trafic === TraficEnum.SharedArrayBuffer && !row.sharedArrayBuffers) {
24fb726d48Sopenharmony_ci    row.sharedArrayBuffers = {
25fb726d48Sopenharmony_ci      processId: new SharedArrayBuffer(Uint16Array.BYTES_PER_ELEMENT * MAX_COUNT),
26fb726d48Sopenharmony_ci      id: new SharedArrayBuffer(Uint16Array.BYTES_PER_ELEMENT * MAX_COUNT),
27fb726d48Sopenharmony_ci      tid: new SharedArrayBuffer(Uint16Array.BYTES_PER_ELEMENT * MAX_COUNT),
28fb726d48Sopenharmony_ci      cpu: new SharedArrayBuffer(Uint8Array.BYTES_PER_ELEMENT * MAX_COUNT),
29fb726d48Sopenharmony_ci      dur: new SharedArrayBuffer(Float64Array.BYTES_PER_ELEMENT * MAX_COUNT),
30fb726d48Sopenharmony_ci      startTime: new SharedArrayBuffer(Float64Array.BYTES_PER_ELEMENT * MAX_COUNT),
31fb726d48Sopenharmony_ci      argSetId: new SharedArrayBuffer(Int8Array.BYTES_PER_ELEMENT * MAX_COUNT),
32fb726d48Sopenharmony_ci      nofinish: new SharedArrayBuffer(Uint8Array.BYTES_PER_ELEMENT * MAX_COUNT),
33fb726d48Sopenharmony_ci    };
34fb726d48Sopenharmony_ci  }
35fb726d48Sopenharmony_ci  return new Promise((resolve): void => {
36fb726d48Sopenharmony_ci    getThreadPool(traceId).submitProto(
37fb726d48Sopenharmony_ci      QueryEnum.CpuData,
38fb726d48Sopenharmony_ci      {
39fb726d48Sopenharmony_ci        cpu: cpu,
40fb726d48Sopenharmony_ci        startNS: TraceRow.range?.startNS || 0,
41fb726d48Sopenharmony_ci        endNS: TraceRow.range?.endNS || 0,
42fb726d48Sopenharmony_ci        recordStartNS: Utils.getInstance().getRecordStartNS(traceId),
43fb726d48Sopenharmony_ci        recordEndNS: Utils.getInstance().getRecordEndNS(traceId),
44fb726d48Sopenharmony_ci        width: width,
45fb726d48Sopenharmony_ci        t: new Date().getTime(),
46fb726d48Sopenharmony_ci        trafic: trafic,
47fb726d48Sopenharmony_ci        sharedArrayBuffers: row.sharedArrayBuffers,
48fb726d48Sopenharmony_ci      },
49fb726d48Sopenharmony_ci      (res: unknown, len: number, transfer: boolean): void => {
50fb726d48Sopenharmony_ci        resolve(arrayBufferHandler(transfer ? res : row.sharedArrayBuffers, len));
51fb726d48Sopenharmony_ci      }
52fb726d48Sopenharmony_ci    );
53fb726d48Sopenharmony_ci  });
54fb726d48Sopenharmony_ci}
55fb726d48Sopenharmony_ci
56fb726d48Sopenharmony_ciexport function searchCpuDataSender(pidArr: Array<number>, tidArr: Array<number>,
57fb726d48Sopenharmony_ci  traceId?: string | null): Promise<unknown[]> {
58fb726d48Sopenharmony_ci  return new Promise((resolve): void => {
59fb726d48Sopenharmony_ci    getThreadPool(traceId).submitProto(
60fb726d48Sopenharmony_ci      QueryEnum.SearchCpuData,
61fb726d48Sopenharmony_ci      {
62fb726d48Sopenharmony_ci        tidArr: tidArr,
63fb726d48Sopenharmony_ci        pidArr: pidArr,
64fb726d48Sopenharmony_ci        trafic: TraficEnum.SharedArrayBuffer,
65fb726d48Sopenharmony_ci      },
66fb726d48Sopenharmony_ci      (res: unknown, len: number, transfer: boolean): void => {
67fb726d48Sopenharmony_ci        resolve(searchArrayBufferHandler(res, len));
68fb726d48Sopenharmony_ci      }
69fb726d48Sopenharmony_ci    );
70fb726d48Sopenharmony_ci  });
71fb726d48Sopenharmony_ci}
72fb726d48Sopenharmony_ci
73fb726d48Sopenharmony_cifunction arrayBufferHandler(res: unknown, len: number): CpuStruct[] {
74fb726d48Sopenharmony_ci  let outArr: CpuStruct[] = [];
75fb726d48Sopenharmony_ci  // @ts-ignore
76fb726d48Sopenharmony_ci  let startTime = new Float64Array(res.startTime);
77fb726d48Sopenharmony_ci  // @ts-ignore
78fb726d48Sopenharmony_ci  let dur = new Float64Array(res.dur);
79fb726d48Sopenharmony_ci  // @ts-ignore
80fb726d48Sopenharmony_ci  let id = new Uint16Array(res.id);
81fb726d48Sopenharmony_ci  // @ts-ignore
82fb726d48Sopenharmony_ci  let processId = new Uint16Array(res.processId);
83fb726d48Sopenharmony_ci  // @ts-ignore
84fb726d48Sopenharmony_ci  let tid = new Uint16Array(res.tid);
85fb726d48Sopenharmony_ci  // @ts-ignore
86fb726d48Sopenharmony_ci  let cpu = new Uint8Array(res.cpu);
87fb726d48Sopenharmony_ci  // @ts-ignore
88fb726d48Sopenharmony_ci  let argSetID = new Int32Array(res.argSetID);
89fb726d48Sopenharmony_ci  // @ts-ignore
90fb726d48Sopenharmony_ci  let nofinish = new Uint8Array(res.nofinish);
91fb726d48Sopenharmony_ci  for (let i = 0; i < len; i++) {
92fb726d48Sopenharmony_ci    outArr.push({
93fb726d48Sopenharmony_ci      processId: processId[i],
94fb726d48Sopenharmony_ci      cpu: cpu[i],
95fb726d48Sopenharmony_ci      tid: tid[i],
96fb726d48Sopenharmony_ci      id: id[i],
97fb726d48Sopenharmony_ci      dur: dur[i],
98fb726d48Sopenharmony_ci      startTime: startTime[i],
99fb726d48Sopenharmony_ci      argSetID: argSetID[i],
100fb726d48Sopenharmony_ci      nofinish: nofinish[i] === 1 ? true : false,
101fb726d48Sopenharmony_ci    } as CpuStruct);
102fb726d48Sopenharmony_ci  }
103fb726d48Sopenharmony_ci  return outArr;
104fb726d48Sopenharmony_ci}
105fb726d48Sopenharmony_ci
106fb726d48Sopenharmony_cifunction searchArrayBufferHandler(res: unknown, len: number): CpuStruct[] {
107fb726d48Sopenharmony_ci  let outArr: CpuStruct[] = [];
108fb726d48Sopenharmony_ci  // @ts-ignore
109fb726d48Sopenharmony_ci  let startTime = new Float64Array(res.startTime);
110fb726d48Sopenharmony_ci  // @ts-ignore
111fb726d48Sopenharmony_ci  let dur = new Float64Array(res.dur);
112fb726d48Sopenharmony_ci  // @ts-ignore
113fb726d48Sopenharmony_ci  let id = new Uint16Array(res.id);
114fb726d48Sopenharmony_ci  // @ts-ignore
115fb726d48Sopenharmony_ci  let processId = new Uint16Array(res.processId);
116fb726d48Sopenharmony_ci  // @ts-ignore
117fb726d48Sopenharmony_ci  let tid = new Uint16Array(res.tid);
118fb726d48Sopenharmony_ci  // @ts-ignore
119fb726d48Sopenharmony_ci  let cpu = new Uint8Array(res.cpu);
120fb726d48Sopenharmony_ci  // @ts-ignore
121fb726d48Sopenharmony_ci  let argSetID = new Int32Array(res.argSetID);
122fb726d48Sopenharmony_ci  // @ts-ignore
123fb726d48Sopenharmony_ci  let nofinish = new Uint8Array(res.nofinish);
124fb726d48Sopenharmony_ci  for (let i = 0; i < len; i++) {
125fb726d48Sopenharmony_ci    outArr.push({
126fb726d48Sopenharmony_ci      processId: processId[i],
127fb726d48Sopenharmony_ci      cpu: cpu[i],
128fb726d48Sopenharmony_ci      tid: tid[i],
129fb726d48Sopenharmony_ci      id: id[i],
130fb726d48Sopenharmony_ci      dur: dur[i],
131fb726d48Sopenharmony_ci      startTime: startTime[i],
132fb726d48Sopenharmony_ci      type: 'cpu',
133fb726d48Sopenharmony_ci      argSetID: -1,
134fb726d48Sopenharmony_ci      nofinish: nofinish[i] === 1 ? true : false,
135fb726d48Sopenharmony_ci    } as CpuStruct);
136fb726d48Sopenharmony_ci  }
137fb726d48Sopenharmony_ci  return outArr;
138fb726d48Sopenharmony_ci}
139