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 { CHART_OFFSET_LEFT, MAX_COUNT, QueryEnum, TraficEnum } from '../utils/QueryEnum';
15fb726d48Sopenharmony_ciimport { getThreadPool } from '../../SqlLite';
16fb726d48Sopenharmony_ciimport { TraceRow } from '../../../component/trace/base/TraceRow';
17fb726d48Sopenharmony_ciimport { CpuStateStruct } from '../../ui-worker/cpu/ProcedureWorkerCpuState';
18fb726d48Sopenharmony_ciimport { Utils } from '../../../component/trace/base/Utils';
19fb726d48Sopenharmony_ci
20fb726d48Sopenharmony_ciexport function cpuStateSender(filterId: number, row: TraceRow<CpuStateStruct>): Promise<CpuStateStruct[]> {
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      value: new SharedArrayBuffer(Uint32Array.BYTES_PER_ELEMENT * MAX_COUNT),
26fb726d48Sopenharmony_ci      dur: new SharedArrayBuffer(Float64Array.BYTES_PER_ELEMENT * MAX_COUNT),
27fb726d48Sopenharmony_ci      startTs: new SharedArrayBuffer(Float64Array.BYTES_PER_ELEMENT * MAX_COUNT),
28fb726d48Sopenharmony_ci      height: new SharedArrayBuffer(Uint8Array.BYTES_PER_ELEMENT * MAX_COUNT),
29fb726d48Sopenharmony_ci    };
30fb726d48Sopenharmony_ci  }
31fb726d48Sopenharmony_ci  return new Promise((resolve, reject): void => {
32fb726d48Sopenharmony_ci    getThreadPool(row.traceId).submitProto(
33fb726d48Sopenharmony_ci      QueryEnum.CpuStateData,
34fb726d48Sopenharmony_ci      {
35fb726d48Sopenharmony_ci        startNS: TraceRow.range?.startNS || 0,
36fb726d48Sopenharmony_ci        endNS: TraceRow.range?.endNS || 0,
37fb726d48Sopenharmony_ci        recordStartNS: Utils.getInstance().getRecordStartNS(row.traceId),
38fb726d48Sopenharmony_ci        recordEndNS: Utils.getInstance().getRecordEndNS(row.traceId),
39fb726d48Sopenharmony_ci        width: width,
40fb726d48Sopenharmony_ci        trafic: trafic,
41fb726d48Sopenharmony_ci        sharedArrayBuffers: row.sharedArrayBuffers,
42fb726d48Sopenharmony_ci        filterId: filterId,
43fb726d48Sopenharmony_ci      },
44fb726d48Sopenharmony_ci      (res: unknown, len: number, transfer: boolean): void => {
45fb726d48Sopenharmony_ci        resolve(arrayBufferHandler(transfer ? res : row.sharedArrayBuffers, len));
46fb726d48Sopenharmony_ci      }
47fb726d48Sopenharmony_ci    );
48fb726d48Sopenharmony_ci  });
49fb726d48Sopenharmony_ci}
50fb726d48Sopenharmony_ci
51fb726d48Sopenharmony_cifunction arrayBufferHandler(res: unknown, len: number): CpuStateStruct[] {
52fb726d48Sopenharmony_ci  let outArr: CpuStateStruct[] = [];
53fb726d48Sopenharmony_ci  // @ts-ignore
54fb726d48Sopenharmony_ci  let startTs = new Float64Array(res.startTs);
55fb726d48Sopenharmony_ci  // @ts-ignore
56fb726d48Sopenharmony_ci  let dur = new Float64Array(res.dur);
57fb726d48Sopenharmony_ci  // @ts-ignore
58fb726d48Sopenharmony_ci  let value = new Uint32Array(res.value);
59fb726d48Sopenharmony_ci  // @ts-ignore
60fb726d48Sopenharmony_ci  let height = new Uint8Array(res.value);
61fb726d48Sopenharmony_ci  for (let i = 0; i < len; i++) {
62fb726d48Sopenharmony_ci    outArr.push({
63fb726d48Sopenharmony_ci      value: value[i],
64fb726d48Sopenharmony_ci      dur: dur[i],
65fb726d48Sopenharmony_ci      height: height[i],
66fb726d48Sopenharmony_ci      startTs: startTs[i],
67fb726d48Sopenharmony_ci    } as unknown as CpuStateStruct);
68fb726d48Sopenharmony_ci  }
69fb726d48Sopenharmony_ci  return outArr;
70fb726d48Sopenharmony_ci}
71