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