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 { getThreadPool } from '../../SqlLite';
16import { TraceRow } from '../../../component/trace/base/TraceRow';
17import { CpuFreqLimitsStruct } from '../../ui-worker/cpu/ProcedureWorkerCpuFreqLimits';
18import { Utils } from '../../../component/trace/base/Utils';
19
20export function cpuFreqLimitSender(
21  maxId: number,
22  minId: number,
23  cpu: number,
24  row: TraceRow<CpuFreqLimitsStruct>
25): Promise<CpuFreqLimitsStruct[]> {
26  let trafic: number = TraficEnum.Memory;
27  let width = row.clientWidth - CHART_OFFSET_LEFT;
28  if (trafic === TraficEnum.SharedArrayBuffer && !row.sharedArrayBuffers) {
29    row.sharedArrayBuffers = {
30      value: new SharedArrayBuffer(Uint32Array.BYTES_PER_ELEMENT * MAX_COUNT),
31      max: new SharedArrayBuffer(Uint32Array.BYTES_PER_ELEMENT * MAX_COUNT),
32      min: new SharedArrayBuffer(Uint32Array.BYTES_PER_ELEMENT * MAX_COUNT),
33      dur: new SharedArrayBuffer(Float64Array.BYTES_PER_ELEMENT * MAX_COUNT),
34      startNs: new SharedArrayBuffer(Float64Array.BYTES_PER_ELEMENT * MAX_COUNT),
35    };
36  }
37  return new Promise((resolve, reject): void => {
38    getThreadPool(row.traceId).submitProto(
39      QueryEnum.CpuFreqLimitData,
40      {
41        startNS: TraceRow.range?.startNS || 0,
42        endNS: TraceRow.range?.endNS || 0,
43        recordStartNS: Utils.getInstance().getRecordStartNS(row.traceId),
44        recordEndNS: Utils.getInstance().getRecordEndNS(row.traceId),
45        width: width,
46        trafic: trafic,
47        sharedArrayBuffers: row.sharedArrayBuffers,
48        maxId: maxId,
49        minId: minId,
50        cpu: cpu,
51      },
52      (res: unknown, len: number, transfer: boolean): void => {
53        resolve(arrayBufferHandler(transfer ? res : row.sharedArrayBuffers, len));
54      }
55    );
56  });
57}
58
59function arrayBufferHandler(res: unknown, len: number): CpuFreqLimitsStruct[] {
60  let outArr: CpuFreqLimitsStruct[] = [];
61  // @ts-ignore
62  let startNs = new Float64Array(res.startNs);
63  // @ts-ignore
64  let dur = new Float64Array(res.dur);
65  // @ts-ignore
66  let value = new Uint32Array(res.value);
67  // @ts-ignore
68  let max = new Uint32Array(res.max);
69  // @ts-ignore
70  let min = new Uint32Array(res.min);
71  for (let i = 0; i < len; i++) {
72    outArr.push({
73      value: value[i],
74      max: max[i],
75      min: min[i],
76      dur: dur[i],
77      startNs: startNs[i],
78    } as unknown as CpuFreqLimitsStruct);
79  }
80  return outArr;
81}
82