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 { TraficEnum } from '../utils/QueryEnum';
15import { processFrameList } from '../utils/AllMemoryCache';
16import { BaseStruct } from '../../ui-worker/ProcedureWorkerCommon';
17import { Args } from '../CommonArgs';
18
19export const chartProcessExpectedDataSql = (args: Args): string => {
20  const recordStartNS = args.recordStartNS;
21  const pid = args.pid;
22  return `
23  SELECT 
24         (a.ts - ${recordStartNS}) AS ts,
25         a.dur,
26         ${pid} as pid,
27         a.id,
28         a.vsync              as name,
29         a.type,
30         a.depth
31  FROM frame_slice AS a
32  WHERE a.type = 1
33    and (a.flag <> 2 or a.flag is null)
34    and a.ipid in (select p.ipid from process AS p where p.pid = ${pid})
35  ORDER BY a.ipid`;
36};
37
38export const chartProcessExpectedProtoDataSql = (args: Args): string => {
39  const endNS = args.endNS;
40  const startNS = args.startNS;
41  const recordStartNS = args.recordStartNS;
42  const pid = args.pid;
43  const width = args.width;
44  return `
45  SELECT 
46         (a.ts - ${startNS}) AS ts,
47         a.dur,
48         ${pid} as pid,
49         a.id,
50         a.vsync              as name,
51         a.type,
52         a.depth,
53         (a.ts - ${recordStartNS}) / (${Math.floor((endNS - startNS) / width)}) + (a.depth * ${width})  AS px
54  FROM frame_slice AS a
55  WHERE a.type = 1
56    and (a.flag <> 2 or a.flag is null)
57    and a.ipid in (select p.ipid from process AS p where p.pid = ${pid})
58    and (a.ts - ${recordStartNS} + a.dur) >= ${Math.floor(startNS)}
59    and (a.ts - ${recordStartNS}) <= ${Math.floor(endNS)}
60  group by px
61  ORDER BY a.ipid;`;
62};
63
64export function processExpectedDataReceiver(data: unknown, proc: Function): void {
65  //@ts-ignore
66  if (data.params.trafic === TraficEnum.Memory) {
67    //@ts-ignore
68    if (!processFrameList.has(`${data.params.pid}_expected`)) {
69      //@ts-ignore
70      let sql = chartProcessExpectedDataSql(data.params); //@ts-ignore
71      processFrameList.set(`${data.params.pid}_expected`, proc(sql));
72    } //@ts-ignore
73    arrayBufferHandler(data, processFrameList.get(`${data.params.pid}_expected`)!, true);
74  } else {
75    //@ts-ignore
76    let sql = chartProcessExpectedProtoDataSql(data.params as BaseStruct);
77    let res = proc(sql); //@ts-ignore
78    arrayBufferHandler(data, res, data.params.trafic !== TraficEnum.SharedArrayBuffer);
79  }
80}
81
82function arrayBufferHandler(data: unknown, res: unknown[], transfer: boolean): void {
83  //@ts-ignore
84  let ts = new Float64Array(transfer ? res.length : data.params.sharedArrayBuffers.ts); //@ts-ignore
85  let dur = new Float64Array(transfer ? res.length : data.params.sharedArrayBuffers.dur); //@ts-ignore
86  let pid = new Int32Array(transfer ? res.length : data.params.sharedArrayBuffers.pid); //@ts-ignore
87  let id = new Int32Array(transfer ? res.length : data.params.sharedArrayBuffers.id); //@ts-ignore
88  let name = new Int32Array(transfer ? res.length : data.params.sharedArrayBuffers.name); //@ts-ignore
89  let type = new Int32Array(transfer ? res.length : data.params.sharedArrayBuffers.type); //@ts-ignore
90  let depth = new Uint16Array(transfer ? res.length : data.params.sharedArrayBuffers.depth);
91  for (let index = 0; index < res.length; index++) {
92    let itemData = res[index]; //@ts-ignore
93    data.params.trafic === TraficEnum.ProtoBuffer && (itemData = itemData.processJanksFramesData); //@ts-ignore
94    dur[index] = itemData.dur; //@ts-ignore
95    ts[index] = itemData.ts; //@ts-ignore
96    pid[index] = itemData.pid; //@ts-ignore
97    id[index] = itemData.id; //@ts-ignore
98    name[index] = itemData.name; //@ts-ignore
99    type[index] = itemData.type; //@ts-ignore
100    depth[index] = itemData.depth;
101  }
102  (self as unknown as Worker).postMessage(
103    {
104      //@ts-ignore
105      id: data.id, //@ts-ignore
106      action: data.action,
107      results: transfer
108        ? {
109            dur: dur.buffer,
110            ts: ts.buffer,
111            pid: pid.buffer,
112            id: id.buffer,
113            name: name.buffer,
114            type: name.buffer,
115            depth: depth.buffer,
116          }
117        : {},
118      len: res.length,
119      transfer: transfer,
120    },
121    transfer ? [dur.buffer, ts.buffer, pid.buffer, type.buffer, id.buffer, name.buffer, depth.buffer] : []
122  );
123}
124