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 { CpuStruct } from '../ui-worker/cpu/ProcedureWorkerCPU';
15import { CHART_OFFSET_LEFT, MAX_COUNT, QueryEnum, TraficEnum } from './utils/QueryEnum';
16import { getThreadPool } from '../SqlLite';
17import { TraceRow } from '../../component/trace/base/TraceRow';
18import { Utils } from '../../component/trace/base/Utils';
19
20export function cpuDataSender(cpu: number, row: TraceRow<CpuStruct>, traceId?: string): Promise<CpuStruct[]> {
21  let trafic: number = TraficEnum.Memory;
22  let width = row.clientWidth - CHART_OFFSET_LEFT;
23  if (trafic === TraficEnum.SharedArrayBuffer && !row.sharedArrayBuffers) {
24    row.sharedArrayBuffers = {
25      processId: new SharedArrayBuffer(Uint16Array.BYTES_PER_ELEMENT * MAX_COUNT),
26      id: new SharedArrayBuffer(Uint16Array.BYTES_PER_ELEMENT * MAX_COUNT),
27      tid: new SharedArrayBuffer(Uint16Array.BYTES_PER_ELEMENT * MAX_COUNT),
28      cpu: new SharedArrayBuffer(Uint8Array.BYTES_PER_ELEMENT * MAX_COUNT),
29      dur: new SharedArrayBuffer(Float64Array.BYTES_PER_ELEMENT * MAX_COUNT),
30      startTime: new SharedArrayBuffer(Float64Array.BYTES_PER_ELEMENT * MAX_COUNT),
31      argSetId: new SharedArrayBuffer(Int8Array.BYTES_PER_ELEMENT * MAX_COUNT),
32      nofinish: new SharedArrayBuffer(Uint8Array.BYTES_PER_ELEMENT * MAX_COUNT),
33    };
34  }
35  return new Promise((resolve): void => {
36    getThreadPool(traceId).submitProto(
37      QueryEnum.CpuData,
38      {
39        cpu: cpu,
40        startNS: TraceRow.range?.startNS || 0,
41        endNS: TraceRow.range?.endNS || 0,
42        recordStartNS: Utils.getInstance().getRecordStartNS(traceId),
43        recordEndNS: Utils.getInstance().getRecordEndNS(traceId),
44        width: width,
45        t: new Date().getTime(),
46        trafic: trafic,
47        sharedArrayBuffers: row.sharedArrayBuffers,
48      },
49      (res: unknown, len: number, transfer: boolean): void => {
50        resolve(arrayBufferHandler(transfer ? res : row.sharedArrayBuffers, len));
51      }
52    );
53  });
54}
55
56export function searchCpuDataSender(pidArr: Array<number>, tidArr: Array<number>,
57  traceId?: string | null): Promise<unknown[]> {
58  return new Promise((resolve): void => {
59    getThreadPool(traceId).submitProto(
60      QueryEnum.SearchCpuData,
61      {
62        tidArr: tidArr,
63        pidArr: pidArr,
64        trafic: TraficEnum.SharedArrayBuffer,
65      },
66      (res: unknown, len: number, transfer: boolean): void => {
67        resolve(searchArrayBufferHandler(res, len));
68      }
69    );
70  });
71}
72
73function arrayBufferHandler(res: unknown, len: number): CpuStruct[] {
74  let outArr: CpuStruct[] = [];
75  // @ts-ignore
76  let startTime = new Float64Array(res.startTime);
77  // @ts-ignore
78  let dur = new Float64Array(res.dur);
79  // @ts-ignore
80  let id = new Uint16Array(res.id);
81  // @ts-ignore
82  let processId = new Uint16Array(res.processId);
83  // @ts-ignore
84  let tid = new Uint16Array(res.tid);
85  // @ts-ignore
86  let cpu = new Uint8Array(res.cpu);
87  // @ts-ignore
88  let argSetID = new Int32Array(res.argSetID);
89  // @ts-ignore
90  let nofinish = new Uint8Array(res.nofinish);
91  for (let i = 0; i < len; i++) {
92    outArr.push({
93      processId: processId[i],
94      cpu: cpu[i],
95      tid: tid[i],
96      id: id[i],
97      dur: dur[i],
98      startTime: startTime[i],
99      argSetID: argSetID[i],
100      nofinish: nofinish[i] === 1 ? true : false,
101    } as CpuStruct);
102  }
103  return outArr;
104}
105
106function searchArrayBufferHandler(res: unknown, len: number): CpuStruct[] {
107  let outArr: CpuStruct[] = [];
108  // @ts-ignore
109  let startTime = new Float64Array(res.startTime);
110  // @ts-ignore
111  let dur = new Float64Array(res.dur);
112  // @ts-ignore
113  let id = new Uint16Array(res.id);
114  // @ts-ignore
115  let processId = new Uint16Array(res.processId);
116  // @ts-ignore
117  let tid = new Uint16Array(res.tid);
118  // @ts-ignore
119  let cpu = new Uint8Array(res.cpu);
120  // @ts-ignore
121  let argSetID = new Int32Array(res.argSetID);
122  // @ts-ignore
123  let nofinish = new Uint8Array(res.nofinish);
124  for (let i = 0; i < len; i++) {
125    outArr.push({
126      processId: processId[i],
127      cpu: cpu[i],
128      tid: tid[i],
129      id: id[i],
130      dur: dur[i],
131      startTime: startTime[i],
132      type: 'cpu',
133      argSetID: -1,
134      nofinish: nofinish[i] === 1 ? true : false,
135    } as CpuStruct);
136  }
137  return outArr;
138}
139