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