1fb726d48Sopenharmony_ci// Copyright (c) 2021 Huawei Device Co., Ltd.
2fb726d48Sopenharmony_ci// Licensed under the Apache License, Version 2.0 (the "License");
3fb726d48Sopenharmony_ci// you may not use this file except in compliance with the License.
4fb726d48Sopenharmony_ci// You may obtain a copy of the License at
5fb726d48Sopenharmony_ci//
6fb726d48Sopenharmony_ci//     http://www.apache.org/licenses/LICENSE-2.0
7fb726d48Sopenharmony_ci//
8fb726d48Sopenharmony_ci// Unless required by applicable law or agreed to in writing, software
9fb726d48Sopenharmony_ci// distributed under the License is distributed on an "AS IS" BASIS,
10fb726d48Sopenharmony_ci// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11fb726d48Sopenharmony_ci// See the License for the specific language governing permissions and
12fb726d48Sopenharmony_ci// limitations under the License.
13fb726d48Sopenharmony_ci
14fb726d48Sopenharmony_ciimport { CHART_OFFSET_LEFT, MAX_COUNT, QueryEnum, TraficEnum } from './utils/QueryEnum';
15fb726d48Sopenharmony_ciimport { threadPool } from '../SqlLite';
16fb726d48Sopenharmony_ciimport { TraceRow } from '../../component/trace/base/TraceRow';
17fb726d48Sopenharmony_ciimport { DiskAbilityMonitorStruct } from '../ui-worker/ProcedureWorkerDiskIoAbility';
18fb726d48Sopenharmony_ciimport { NetworkAbilityMonitorStruct } from '../ui-worker/ProcedureWorkerNetworkAbility';
19fb726d48Sopenharmony_ciimport { CpuAbilityMonitorStruct } from '../ui-worker/ProcedureWorkerCpuAbility';
20fb726d48Sopenharmony_ciimport { MemoryAbilityMonitorStruct } from '../ui-worker/ProcedureWorkerMemoryAbility';
21fb726d48Sopenharmony_ciimport { SnapshotStruct } from '../ui-worker/ProcedureWorkerSnapshot';
22fb726d48Sopenharmony_ci
23fb726d48Sopenharmony_ciexport function cpuAbilityUserDataSender(
24fb726d48Sopenharmony_ci  row: TraceRow<CpuAbilityMonitorStruct>,
25fb726d48Sopenharmony_ci  type: string
26fb726d48Sopenharmony_ci): Promise<CpuAbilityMonitorStruct[]> {
27fb726d48Sopenharmony_ci  let trafic: number = TraficEnum.Memory;
28fb726d48Sopenharmony_ci  let width = row.clientWidth - CHART_OFFSET_LEFT;
29fb726d48Sopenharmony_ci  let QueryEnumber: number = -1;
30fb726d48Sopenharmony_ci  if (trafic === TraficEnum.SharedArrayBuffer && !row.sharedArrayBuffers) {
31fb726d48Sopenharmony_ci    row.sharedArrayBuffers = {
32fb726d48Sopenharmony_ci      value: new SharedArrayBuffer(Float64Array.BYTES_PER_ELEMENT * MAX_COUNT),
33fb726d48Sopenharmony_ci      startNS: new SharedArrayBuffer(Float64Array.BYTES_PER_ELEMENT * MAX_COUNT),
34fb726d48Sopenharmony_ci      dur: new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT * MAX_COUNT),
35fb726d48Sopenharmony_ci    };
36fb726d48Sopenharmony_ci  }
37fb726d48Sopenharmony_ci  if (type === 'CpuAbilityUserData') {
38fb726d48Sopenharmony_ci    QueryEnumber = QueryEnum.CpuAbilityUserData;
39fb726d48Sopenharmony_ci  } else if (type === 'CpuAbilitySystemData') {
40fb726d48Sopenharmony_ci    QueryEnumber = QueryEnum.CpuAbilitySystemData;
41fb726d48Sopenharmony_ci  } else if (type === 'CpuAbilityMonitorData') {
42fb726d48Sopenharmony_ci    QueryEnumber = QueryEnum.CpuAbilityMonitorData;
43fb726d48Sopenharmony_ci  }
44fb726d48Sopenharmony_ci  return new Promise((resolve, reject) => {
45fb726d48Sopenharmony_ci    threadPool.submitProto(
46fb726d48Sopenharmony_ci      QueryEnumber,
47fb726d48Sopenharmony_ci      {
48fb726d48Sopenharmony_ci        width: width,
49fb726d48Sopenharmony_ci        trafic: trafic,
50fb726d48Sopenharmony_ci        recordStartNS: window.recordStartNS,
51fb726d48Sopenharmony_ci        recordEndNS: window.recordEndNS,
52fb726d48Sopenharmony_ci        startNS: TraceRow.range?.startNS || 0,
53fb726d48Sopenharmony_ci        endNS: TraceRow.range?.endNS || 0,
54fb726d48Sopenharmony_ci        sharedArrayBuffers: row.sharedArrayBuffers,
55fb726d48Sopenharmony_ci      },
56fb726d48Sopenharmony_ci      (res: unknown, len: number, transfer: boolean) => {
57fb726d48Sopenharmony_ci        resolve(cpuAbilityMonitorArrayBufferHandler(transfer ? res : row.sharedArrayBuffers, len));
58fb726d48Sopenharmony_ci      }
59fb726d48Sopenharmony_ci    );
60fb726d48Sopenharmony_ci  });
61fb726d48Sopenharmony_ci}
62fb726d48Sopenharmony_ciexport function abilityMemoryUsedDataSender(
63fb726d48Sopenharmony_ci  id: string = '',
64fb726d48Sopenharmony_ci  row: TraceRow<MemoryAbilityMonitorStruct>
65fb726d48Sopenharmony_ci): Promise<MemoryAbilityMonitorStruct[]> {
66fb726d48Sopenharmony_ci  let trafic: number = TraficEnum.Memory;
67fb726d48Sopenharmony_ci  let width = row.clientWidth - CHART_OFFSET_LEFT;
68fb726d48Sopenharmony_ci  if (trafic === TraficEnum.SharedArrayBuffer && !row.sharedArrayBuffers) {
69fb726d48Sopenharmony_ci    row.sharedArrayBuffers = {
70fb726d48Sopenharmony_ci      value: new SharedArrayBuffer(Float64Array.BYTES_PER_ELEMENT * MAX_COUNT),
71fb726d48Sopenharmony_ci      startNS: new SharedArrayBuffer(Float64Array.BYTES_PER_ELEMENT * MAX_COUNT),
72fb726d48Sopenharmony_ci      dur: new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT * MAX_COUNT),
73fb726d48Sopenharmony_ci    };
74fb726d48Sopenharmony_ci  }
75fb726d48Sopenharmony_ci  return new Promise((resolve, reject) => {
76fb726d48Sopenharmony_ci    threadPool.submitProto(
77fb726d48Sopenharmony_ci      QueryEnum.AbilityMemoryUsedData,
78fb726d48Sopenharmony_ci      {
79fb726d48Sopenharmony_ci        id: id,
80fb726d48Sopenharmony_ci        startNS: TraceRow.range?.startNS || 0,
81fb726d48Sopenharmony_ci        endNS: TraceRow.range?.endNS || 0,
82fb726d48Sopenharmony_ci        recordStartNS: window.recordStartNS,
83fb726d48Sopenharmony_ci        recordEndNS: window.recordEndNS,
84fb726d48Sopenharmony_ci        width: width,
85fb726d48Sopenharmony_ci        trafic: trafic,
86fb726d48Sopenharmony_ci        sharedArrayBuffers: row.sharedArrayBuffers,
87fb726d48Sopenharmony_ci      },
88fb726d48Sopenharmony_ci      (res: unknown, len: number, transfer: boolean) => {
89fb726d48Sopenharmony_ci        resolve(memoryArrayBufferHandler(transfer ? res : row.sharedArrayBuffers, len));
90fb726d48Sopenharmony_ci      }
91fb726d48Sopenharmony_ci    );
92fb726d48Sopenharmony_ci  });
93fb726d48Sopenharmony_ci}
94fb726d48Sopenharmony_ciexport function abilityBytesReadDataSender(
95fb726d48Sopenharmony_ci  row: TraceRow<DiskAbilityMonitorStruct>,
96fb726d48Sopenharmony_ci  type: string
97fb726d48Sopenharmony_ci): Promise<DiskAbilityMonitorStruct[]> {
98fb726d48Sopenharmony_ci  let trafic: number = TraficEnum.Memory;
99fb726d48Sopenharmony_ci  let width = row.clientWidth - CHART_OFFSET_LEFT;
100fb726d48Sopenharmony_ci  if (trafic === TraficEnum.SharedArrayBuffer && !row.sharedArrayBuffers) {
101fb726d48Sopenharmony_ci    row.sharedArrayBuffers = {
102fb726d48Sopenharmony_ci      value: new SharedArrayBuffer(Float64Array.BYTES_PER_ELEMENT * MAX_COUNT),
103fb726d48Sopenharmony_ci      startNS: new SharedArrayBuffer(Float64Array.BYTES_PER_ELEMENT * MAX_COUNT),
104fb726d48Sopenharmony_ci      dur: new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT * MAX_COUNT),
105fb726d48Sopenharmony_ci    };
106fb726d48Sopenharmony_ci  }
107fb726d48Sopenharmony_ci  let QueryEnumber = -1;
108fb726d48Sopenharmony_ci  if (type === 'AbilityBytesReadData') {
109fb726d48Sopenharmony_ci    QueryEnumber = QueryEnum.AbilityBytesReadData;
110fb726d48Sopenharmony_ci  } else if (type === 'AbilityBytesWrittenData') {
111fb726d48Sopenharmony_ci    QueryEnumber = QueryEnum.AbilityBytesWrittenData;
112fb726d48Sopenharmony_ci  } else if (type === 'AbilityReadOpsData') {
113fb726d48Sopenharmony_ci    QueryEnumber = QueryEnum.AbilityReadOpsData;
114fb726d48Sopenharmony_ci  } else if (type === 'AbilityWrittenOpsData') {
115fb726d48Sopenharmony_ci    QueryEnumber = QueryEnum.AbilityWrittenOpsData;
116fb726d48Sopenharmony_ci  }
117fb726d48Sopenharmony_ci  return new Promise((resolve, reject) => {
118fb726d48Sopenharmony_ci    threadPool.submitProto(
119fb726d48Sopenharmony_ci      QueryEnumber,
120fb726d48Sopenharmony_ci      {
121fb726d48Sopenharmony_ci        recordStartNS: window.recordStartNS,
122fb726d48Sopenharmony_ci        recordEndNS: window.recordEndNS,
123fb726d48Sopenharmony_ci        startNS: TraceRow.range?.startNS || 0,
124fb726d48Sopenharmony_ci        endNS: TraceRow.range?.endNS || 0,
125fb726d48Sopenharmony_ci        width: width,
126fb726d48Sopenharmony_ci        trafic: trafic,
127fb726d48Sopenharmony_ci        sharedArrayBuffers: row.sharedArrayBuffers,
128fb726d48Sopenharmony_ci      },
129fb726d48Sopenharmony_ci      (res: unknown, len: number, transfer: boolean) => {
130fb726d48Sopenharmony_ci        resolve(arrayBufferHandler(transfer ? res : row.sharedArrayBuffers, len));
131fb726d48Sopenharmony_ci      }
132fb726d48Sopenharmony_ci    );
133fb726d48Sopenharmony_ci  });
134fb726d48Sopenharmony_ci}
135fb726d48Sopenharmony_ciexport function abilityBytesInTraceDataSender(
136fb726d48Sopenharmony_ci  row: TraceRow<NetworkAbilityMonitorStruct>,
137fb726d48Sopenharmony_ci  type: string
138fb726d48Sopenharmony_ci): Promise<NetworkAbilityMonitorStruct[]> {
139fb726d48Sopenharmony_ci  let trafic: number = TraficEnum.Memory;
140fb726d48Sopenharmony_ci  let width = row.clientWidth - CHART_OFFSET_LEFT;
141fb726d48Sopenharmony_ci  if (trafic === TraficEnum.SharedArrayBuffer && !row.sharedArrayBuffers) {
142fb726d48Sopenharmony_ci    row.sharedArrayBuffers = {
143fb726d48Sopenharmony_ci      value: new SharedArrayBuffer(Float64Array.BYTES_PER_ELEMENT * MAX_COUNT),
144fb726d48Sopenharmony_ci      startNS: new SharedArrayBuffer(Float64Array.BYTES_PER_ELEMENT * MAX_COUNT),
145fb726d48Sopenharmony_ci      dur: new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT * MAX_COUNT),
146fb726d48Sopenharmony_ci    };
147fb726d48Sopenharmony_ci  }
148fb726d48Sopenharmony_ci  let QueryEnumber: number = -1;
149fb726d48Sopenharmony_ci  if (type === 'AbilityBytesInTraceData') {
150fb726d48Sopenharmony_ci    QueryEnumber = QueryEnum.AbilityBytesInTraceData;
151fb726d48Sopenharmony_ci  } else if (type === 'AbilityBytesOutTraceData') {
152fb726d48Sopenharmony_ci    QueryEnumber = QueryEnum.AbilityBytesOutTraceData;
153fb726d48Sopenharmony_ci  } else if (type === 'AbilityPacketInTraceData') {
154fb726d48Sopenharmony_ci    QueryEnumber = QueryEnum.AbilityPacketInTraceData;
155fb726d48Sopenharmony_ci  } else if (type === 'AbilityPacketsOutTraceData') {
156fb726d48Sopenharmony_ci    QueryEnumber = QueryEnum.AbilityPacketsOutTraceData;
157fb726d48Sopenharmony_ci  }
158fb726d48Sopenharmony_ci  return new Promise((resolve, reject) => {
159fb726d48Sopenharmony_ci    threadPool.submitProto(
160fb726d48Sopenharmony_ci      QueryEnumber,
161fb726d48Sopenharmony_ci      {
162fb726d48Sopenharmony_ci        startNS: TraceRow.range?.startNS || 0,
163fb726d48Sopenharmony_ci        endNS: TraceRow.range?.endNS || 0,
164fb726d48Sopenharmony_ci        recordStartNS: window.recordStartNS,
165fb726d48Sopenharmony_ci        recordEndNS: window.recordEndNS,
166fb726d48Sopenharmony_ci        width: width,
167fb726d48Sopenharmony_ci        trafic: trafic,
168fb726d48Sopenharmony_ci        sharedArrayBuffers: row.sharedArrayBuffers,
169fb726d48Sopenharmony_ci      },
170fb726d48Sopenharmony_ci      (res: unknown, len: number, transfer: boolean) => {
171fb726d48Sopenharmony_ci        resolve(networkArrayBufferHandler(transfer ? res : row.sharedArrayBuffers, len));
172fb726d48Sopenharmony_ci      }
173fb726d48Sopenharmony_ci    );
174fb726d48Sopenharmony_ci  });
175fb726d48Sopenharmony_ci}
176fb726d48Sopenharmony_cifunction memoryArrayBufferHandler(buffers: unknown, len: number): MemoryAbilityMonitorStruct[] {
177fb726d48Sopenharmony_ci  let outArr: MemoryAbilityMonitorStruct[] = [];
178fb726d48Sopenharmony_ci  // @ts-ignore
179fb726d48Sopenharmony_ci  let value = new Float64Array(buffers.value);
180fb726d48Sopenharmony_ci  // @ts-ignore
181fb726d48Sopenharmony_ci  let startNS = new Float64Array(buffers.startNS);
182fb726d48Sopenharmony_ci  // @ts-ignore
183fb726d48Sopenharmony_ci  let dur = new Int32Array(buffers.dur);
184fb726d48Sopenharmony_ci  for (let i = 0; i < len; i++) {
185fb726d48Sopenharmony_ci    outArr.push({
186fb726d48Sopenharmony_ci      dur: dur[i],
187fb726d48Sopenharmony_ci      value: value[i],
188fb726d48Sopenharmony_ci      startNS: startNS[i],
189fb726d48Sopenharmony_ci    } as unknown as MemoryAbilityMonitorStruct);
190fb726d48Sopenharmony_ci  }
191fb726d48Sopenharmony_ci  return outArr;
192fb726d48Sopenharmony_ci}
193fb726d48Sopenharmony_cifunction cpuAbilityMonitorArrayBufferHandler(buffers: unknown, len: number): CpuAbilityMonitorStruct[] {
194fb726d48Sopenharmony_ci  let outArr: CpuAbilityMonitorStruct[] = [];
195fb726d48Sopenharmony_ci  // @ts-ignore
196fb726d48Sopenharmony_ci  let value = new Float64Array(buffers.value);
197fb726d48Sopenharmony_ci  // @ts-ignore
198fb726d48Sopenharmony_ci  let startNS = new Float64Array(buffers.startNS);
199fb726d48Sopenharmony_ci  // @ts-ignore
200fb726d48Sopenharmony_ci  let dur = new Int32Array(buffers.dur);
201fb726d48Sopenharmony_ci  for (let i = 0; i < len; i++) {
202fb726d48Sopenharmony_ci    outArr.push({
203fb726d48Sopenharmony_ci      dur: dur[i],
204fb726d48Sopenharmony_ci      value: value[i],
205fb726d48Sopenharmony_ci      startNS: startNS[i],
206fb726d48Sopenharmony_ci    } as unknown as CpuAbilityMonitorStruct);
207fb726d48Sopenharmony_ci  }
208fb726d48Sopenharmony_ci  return outArr;
209fb726d48Sopenharmony_ci}
210fb726d48Sopenharmony_cifunction arrayBufferHandler(buffers: unknown, len: number): DiskAbilityMonitorStruct[] {
211fb726d48Sopenharmony_ci  let outArr: DiskAbilityMonitorStruct[] = [];
212fb726d48Sopenharmony_ci  // @ts-ignore
213fb726d48Sopenharmony_ci  let value = new Float64Array(buffers.value);
214fb726d48Sopenharmony_ci  // @ts-ignore
215fb726d48Sopenharmony_ci  let startNS = new Float64Array(buffers.startNS);
216fb726d48Sopenharmony_ci  // @ts-ignore
217fb726d48Sopenharmony_ci  let dur = new Int32Array(buffers.dur);
218fb726d48Sopenharmony_ci  for (let i = 0; i < len; i++) {
219fb726d48Sopenharmony_ci    outArr.push({
220fb726d48Sopenharmony_ci      dur: dur[i],
221fb726d48Sopenharmony_ci      value: value[i],
222fb726d48Sopenharmony_ci      startNS: startNS[i],
223fb726d48Sopenharmony_ci    } as unknown as DiskAbilityMonitorStruct);
224fb726d48Sopenharmony_ci  }
225fb726d48Sopenharmony_ci  return outArr;
226fb726d48Sopenharmony_ci}
227fb726d48Sopenharmony_cifunction networkArrayBufferHandler(buffers: unknown, len: number): NetworkAbilityMonitorStruct[] {
228fb726d48Sopenharmony_ci  let outArr: NetworkAbilityMonitorStruct[] = [];
229fb726d48Sopenharmony_ci  // @ts-ignore
230fb726d48Sopenharmony_ci  let value = new Float64Array(buffers.value);
231fb726d48Sopenharmony_ci  // @ts-ignore
232fb726d48Sopenharmony_ci  let startNS = new Float64Array(buffers.startNS);
233fb726d48Sopenharmony_ci  // @ts-ignore
234fb726d48Sopenharmony_ci  let dur = new Int32Array(buffers.dur);
235fb726d48Sopenharmony_ci  for (let i = 0; i < len; i++) {
236fb726d48Sopenharmony_ci    outArr.push({
237fb726d48Sopenharmony_ci      dur: dur[i],
238fb726d48Sopenharmony_ci      value: value[i],
239fb726d48Sopenharmony_ci      startNS: startNS[i],
240fb726d48Sopenharmony_ci    } as unknown as NetworkAbilityMonitorStruct);
241fb726d48Sopenharmony_ci  }
242fb726d48Sopenharmony_ci  return outArr;
243fb726d48Sopenharmony_ci}
244