• 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 { threadPool } from '../SqlLite';
16import { TraceRow } from '../../component/trace/base/TraceRow';
17import { ClockStruct } from '../ui-worker/ProcedureWorkerClock';
18
19export function clockDataSender(
20  clockName: string = '',
21  sqlType: string,
22  row: TraceRow<ClockStruct>,
23  args?: any
24): Promise<ClockStruct[]> {
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(Int32Array.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    threadPool.submitProto(
37      QueryEnum.ClockData,
38      {
39        clockName: clockName,
40        sqlType: sqlType,
41        startNS: args ? args.startNS : (TraceRow.range?.startNS || 0),
42        endNS: args ? args.endNS : (TraceRow.range?.endNS || 0),
43        totalNS: args ? (args.endNS - args.startNS) : (TraceRow.range?.totalNS || 0),
44        recordStartNS: window.recordStartNS,
45        recordEndNS: window.recordEndNS,
46        queryAll: args && args.queryAll,
47        t: Date.now(),
48        width: width,
49        trafic: trafic,
50        sharedArrayBuffers: row.sharedArrayBuffers,
51      },
52      (res: any, len: number, transfer: boolean): void => {
53        resolve(arrayBufferHandler(transfer ? res : row.sharedArrayBuffers, len));
54      }
55    );
56  });
57}
58
59function arrayBufferHandler(buffers: any, len: number): ClockStruct[] {
60  let outArr: ClockStruct[] = [];
61  let filterId = new Int32Array(buffers.filterId);
62  let value = new Int32Array(buffers.value);
63  let startNS = new Float64Array(buffers.startNS);
64  let dur = new Float64Array(buffers.dur);
65  for (let i = 0; i < len; i++) {
66    outArr.push({
67      filterId: filterId[i],
68      value: value[i],
69      startNS: startNS[i],
70      dur: dur[i],
71    } as unknown as ClockStruct);
72  }
73  return outArr;
74}
75