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.
13import { QueryEnum, TraficEnum } from './utils/QueryEnum';
14import { threadPool } from '../SqlLite';
15import { TraceRow } from '../../component/trace/base/TraceRow';
16
17export function nativeMemoryChartDataSender( //@ts-ignore
18  row: TraceRow<unknown>,
19  setting: {
20    eventType: number;
21    ipid: number;
22    model: string;
23    drawType: number;
24  }
25): Promise<unknown> {
26  return new Promise((resolve, reject) => {
27    threadPool.submitProto(
28      QueryEnum.NativeMemoryChartData,
29      {
30        startNS: TraceRow.range?.startNS || 0,
31        endNS: TraceRow.range?.endNS || 0,
32        totalNS: (TraceRow.range?.endNS || 0) - (TraceRow.range?.startNS || 0),
33        frame: row.frame,
34        eventType: setting.eventType,
35        drawType: setting.drawType,
36        model: setting.model,
37        ipid: setting.ipid,
38        trafic: TraficEnum.SharedArrayBuffer,
39      },
40      (res: unknown, len: number): void => {
41        resolve(arrayBufferHandler(res, len));
42      }
43    );
44  });
45}
46
47export function nativeMemoryChartDataCacheSender(processes: Array<number>, model: string): Promise<unknown> {
48  return new Promise((resolve, reject) => {
49    threadPool.submitProto(
50      model === 'native_hook' ? QueryEnum.NativeMemoryChartCacheNormal : QueryEnum.NativeMemoryChartCacheStatistic,
51      {
52        totalNS: window.totalNS,
53        recordStartNS: window.recordStartNS,
54        recordEndNS: window.recordEndNS,
55        model: model,
56        processes: processes,
57        trafic: TraficEnum.SharedArrayBuffer,
58        isCache: true,
59      },
60      (res: unknown, len: number): void => {
61        resolve('ok');
62      }
63    );
64  });
65}
66
67function arrayBufferHandler(res: unknown, len: number): unknown[] {
68  let outArr: unknown[] = []; //@ts-ignore
69  let startTime = new Float64Array(res.startTime); //@ts-ignore
70  let dur = new Float64Array(res.dur); //@ts-ignore
71  let density = new Int32Array(res.density); //@ts-ignore
72  let heapSize = new Float64Array(res.heapSize);
73  for (let i = 0; i < len; i++) {
74    outArr.push({
75      startTime: startTime[i],
76      dur: dur[i],
77      heapsize: heapSize[i],
78      density: density[i], //@ts-ignore
79      maxHeapSize: res.maxSize, //@ts-ignore
80      maxDensity: res.maxDensity, //@ts-ignore
81      minHeapSize: res.minSize, //@ts-ignore
82      minDensity: res.minDensity,
83    } as unknown);
84  }
85  return outArr;
86}
87