• 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 { TraficEnum } from '../utils/QueryEnum';
15import { processFrameList } from '../utils/AllMemoryCache';
16import { Args } from '../CommonArgs';
17
18export const chartProcessActualDataSql = (args: Args): string => {
19  return `
20  SELECT
21               (a.ts - ${args.recordStartNS}) AS ts,
22               a.dur,
23               ${args.pid} as pid,
24               a.id,
25               a.vsync AS name,
26               a.type,
27               a.flag AS jankTag,
28               a.dst AS dstSlice,
29               a.depth
30        FROM frame_slice AS a
31        WHERE a.type = 0
32          AND a.flag <> 2
33          AND a.ipid in (select p.ipid from process AS p where p.pid = ${args.pid})
34        ORDER BY a.ipid;`;
35};
36
37export const chartProcessActualProtoDataSql = (args: Args): string => {
38  return `
39  SELECT
40               (a.ts - ${args.recordStartNS}) AS ts,
41               a.dur,
42               ${args.pid} as pid,
43               a.id,
44               a.vsync AS name,
45               a.type,
46               a.flag AS jankTag,
47               a.dst AS dstSlice,
48               a.depth,
49               (a.ts - ${args.recordStartNS}) / (${Math.floor(
50    (args.endNS - args.startNS) / args.width
51  )}) + (a.depth * ${args.width})  AS px
52        FROM frame_slice AS a
53        WHERE a.type = 0
54          AND a.flag <> 2
55          AND a.ipid in (select p.ipid from process AS p where p.pid = ${args.pid})
56          AND (a.ts - ${args.recordStartNS}) + dur >= ${Math.floor(args.startNS)}
57
58          AND (a.ts - ${args.recordStartNS}) <= ${Math.floor(args.endNS)}
59        group by px
60        ORDER BY a.ipid;`;
61};
62
63export function processActualDataReceiver(data: unknown, proc: Function): void {
64  //@ts-ignore
65  if (data.params.trafic === TraficEnum.Memory) {
66    //@ts-ignore
67    if (!processFrameList.has(`${data.params.pid}_actual`)) {
68      //@ts-ignore
69      let sql = chartProcessActualDataSql(data.params); //@ts-ignore
70      processFrameList.set(`${data.params.pid}_actual`, proc(sql));
71    } //@ts-ignore
72    arrayBufferHandler(data, processFrameList.get(`${data.params.pid}_actual`)!, true);
73  } else {
74    //@ts-ignore
75    let sql = chartProcessActualProtoDataSql(data.params);
76    let res = proc(sql); //@ts-ignore
77    arrayBufferHandler(data, res, data.params.trafic !== TraficEnum.SharedArrayBuffer);
78  }
79}
80
81function arrayBufferHandler(data: unknown, res: unknown[], transfer: boolean): void {
82  let processActual = new ProcessActual(data, transfer, res.length);
83  for (let index = 0; index < res.length; index++) {
84    let itemData = res[index]; //@ts-ignore
85    data.params.trafic === TraficEnum.ProtoBuffer && (itemData = itemData.processJanksActualData); //@ts-ignore
86    if (!itemData.dur || itemData.dur < 0) {
87      continue;
88    } //@ts-ignore
89    processActual.dur[index] = itemData.dur; //@ts-ignore
90    processActual.ts[index] = itemData.ts; //@ts-ignore
91    processActual.pid[index] = itemData.pid; //@ts-ignore
92    processActual.id[index] = itemData.id; //@ts-ignore
93    processActual.name[index] = itemData.name; //@ts-ignore
94    processActual.type[index] = itemData.type; //@ts-ignore
95    processActual.jank_tag[index] = itemData.jankTag; //@ts-ignore
96    processActual.dst_slice[index] = itemData.dstSlice; //@ts-ignore
97    processActual.depth[index] = itemData.depth;
98  }
99  postProcessActualMessage(data, transfer, processActual, res.length);
100}
101function postProcessActualMessage(data: unknown, transfer: boolean, processActual: ProcessActual, len: number): void {
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: processActual.dur.buffer,
110            ts: processActual.ts.buffer,
111            pid: processActual.pid.buffer,
112            id: processActual.id.buffer,
113            name: processActual.name.buffer,
114            type: processActual.type.buffer,
115            jank_tag: processActual.jank_tag.buffer,
116            dst_slice: processActual.dst_slice.buffer,
117            depth: processActual.depth.buffer,
118          }
119        : {},
120      len: len,
121      transfer: transfer,
122    },
123    transfer
124      ? [
125          processActual.dur.buffer,
126          processActual.ts.buffer,
127          processActual.pid.buffer,
128          processActual.type.buffer,
129          processActual.id.buffer,
130          processActual.name.buffer,
131          processActual.jank_tag.buffer,
132          processActual.dst_slice.buffer,
133          processActual.depth.buffer,
134        ]
135      : []
136  );
137}
138class ProcessActual {
139  ts: Float64Array;
140  dur: Float64Array;
141  pid: Int32Array;
142  id: Int32Array;
143  name: Int32Array;
144  type: Int32Array;
145  jank_tag: Int32Array;
146  dst_slice: Int32Array;
147  depth: Uint16Array;
148  constructor(data: unknown, transfer: boolean, len: number) {
149    //@ts-ignore
150    this.ts = new Float64Array(transfer ? len : data.params.sharedArrayBuffers.ts); //@ts-ignore
151    this.dur = new Float64Array(transfer ? len : data.params.sharedArrayBuffers.dur); //@ts-ignore
152    this.pid = new Int32Array(transfer ? len : data.params.sharedArrayBuffers.pid); //@ts-ignore
153    this.id = new Int32Array(transfer ? len : data.params.sharedArrayBuffers.id); //@ts-ignore
154    this.name = new Int32Array(transfer ? len : data.params.sharedArrayBuffers.name); //@ts-ignore
155    this.type = new Int32Array(transfer ? len : data.params.sharedArrayBuffers.type); //@ts-ignore
156    this.jank_tag = new Int32Array(transfer ? len : data.params.sharedArrayBuffers.jank_tag); //@ts-ignore
157    this.dst_slice = new Int32Array(transfer ? len : data.params.sharedArrayBuffers.dst_slice); //@ts-ignore
158    this.depth = new Uint16Array(transfer ? len : data.params.sharedArrayBuffers.depth);
159  }
160}
161