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 { Args } from '../CommonArgs';
15import { TraficEnum } from '../utils/QueryEnum';
16
17export const chartProcessDeliverInputEventDataSql = (args: Args): string => {
18  return `select  
19      c.ts-${args.recordStartNS} as startTs,
20      c.dur,
21      c.argsetid,
22      tid,
23      P.pid,
24      is_main_thread as isMainThread,
25      c.callid as trackId,
26      c.parent_id as parentId,
27      c.id,
28      c.cookie,
29      c.depth,
30      ((c.ts - ${args.recordStartNS}) / (${Math.floor((args.endNS - args.startNS) / args.width)})) AS px,
31      c.name as funName,
32      A.name as threadName
33  from thread A
34  left join process P on P.id = A.ipid
35  left join callstack C on A.id = C.callid
36  where startTs not null and cookie not null
37  and c.name ='deliverInputEvent'
38  and tid = ${args.tid}
39  and startTs + dur >= ${Math.floor(args.startNS)}
40  and startTs <= ${Math.floor(args.endNS)}
41    group by px;
42  `;
43};
44
45export function processDeliverInputEventDataReceiver(data: unknown, proc: Function): void {
46  //@ts-ignore
47  let sql = chartProcessDeliverInputEventDataSql(data.params);
48  let res = proc(sql); //@ts-ignore
49  arrayBufferHandler(data, res, data.params.trafic !== TraficEnum.SharedArrayBuffer);
50}
51
52function arrayBufferHandler(data: unknown, res: unknown[], transfer: boolean): void {
53  let processDeliverInputEvent = new ProcessDeliverInputEvent(data, transfer, res.length);
54  res.forEach((it, i) => {
55    //@ts-ignore
56    data.params.trafic === TraficEnum.ProtoBuffer && (it = it.processInputEventData); //@ts-ignore
57    processDeliverInputEvent.tid[i] = it.tid; //@ts-ignore
58    processDeliverInputEvent.dur[i] = it.dur; //@ts-ignore
59    processDeliverInputEvent.is_main_thread[i] = it.isMainThread; //@ts-ignore
60    processDeliverInputEvent.track_id[i] = it.trackId; //@ts-ignore
61    processDeliverInputEvent.startTs[i] = it.startTs; //@ts-ignore
62    processDeliverInputEvent.pid[i] = it.pid; //@ts-ignore
63    processDeliverInputEvent.parent_id[i] = it.parentId; //@ts-ignore
64    processDeliverInputEvent.id[i] = it.id; //@ts-ignore
65    processDeliverInputEvent.cookie[i] = it.cookie; //@ts-ignore
66    processDeliverInputEvent.depth[i] = it.depth; //@ts-ignore
67    processDeliverInputEvent.argsetid[i] = it.argsetid;
68  });
69  postMessage(data, transfer, processDeliverInputEvent, res.length);
70}
71
72function postMessage(
73  data: unknown,
74  transfer: boolean,
75  processDeliverInputEvent: ProcessDeliverInputEvent,
76  len: number
77): void {
78  (self as unknown as Worker).postMessage(
79    {
80      transfer: transfer, //@ts-ignore
81      id: data.id, //@ts-ignore
82      action: data.action,
83      results: transfer
84        ? {
85            tid: processDeliverInputEvent.tid.buffer,
86            dur: processDeliverInputEvent.dur.buffer,
87            is_main_thread: processDeliverInputEvent.is_main_thread.buffer,
88            track_id: processDeliverInputEvent.track_id.buffer,
89            startTs: processDeliverInputEvent.startTs.buffer,
90            pid: processDeliverInputEvent.pid.buffer,
91            parent_id: processDeliverInputEvent.parent_id.buffer,
92            id: processDeliverInputEvent.id.buffer,
93            cookie: processDeliverInputEvent.cookie.buffer,
94            depth: processDeliverInputEvent.depth.buffer,
95            argsetid: processDeliverInputEvent.argsetid.buffer,
96          }
97        : {},
98      len: len,
99    },
100    transfer
101      ? [
102          processDeliverInputEvent.tid.buffer,
103          processDeliverInputEvent.dur.buffer,
104          processDeliverInputEvent.is_main_thread.buffer,
105          processDeliverInputEvent.track_id.buffer,
106          processDeliverInputEvent.startTs.buffer,
107          processDeliverInputEvent.pid.buffer,
108          processDeliverInputEvent.parent_id.buffer,
109          processDeliverInputEvent.id.buffer,
110          processDeliverInputEvent.cookie.buffer,
111          processDeliverInputEvent.depth.buffer,
112          processDeliverInputEvent.argsetid.buffer,
113        ]
114      : []
115  );
116}
117class ProcessDeliverInputEvent {
118  tid: Int32Array;
119  pid: Int32Array;
120  is_main_thread: Int8Array;
121  track_id: Int32Array;
122  startTs: Float64Array;
123  dur: Float64Array;
124  parent_id: Int32Array;
125  id: Int32Array;
126  cookie: Int32Array;
127  depth: Int32Array;
128  argsetid: Int32Array;
129  constructor(data: unknown, transfer: boolean, len: number) {
130    //@ts-ignore
131    this.tid = new Int32Array(transfer ? len : data.params.sharedArrayBuffers.tid); //@ts-ignore
132    this.pid = new Int32Array(transfer ? len : data.params.sharedArrayBuffers.pid); //@ts-ignore
133    this.is_main_thread = new Int8Array(transfer ? len : data.params.sharedArrayBuffers.is_main_thread); //@ts-ignore
134    this.track_id = new Int32Array(transfer ? len : data.params.sharedArrayBuffers.track_id); //@ts-ignore
135    this.startTs = new Float64Array(transfer ? len : data.params.sharedArrayBuffers.startTs); //@ts-ignore
136    this.dur = new Float64Array(transfer ? len : data.params.sharedArrayBuffers.dur); //@ts-ignore
137    this.parent_id = new Int32Array(transfer ? len : data.params.sharedArrayBuffers.parent_id); //@ts-ignore
138    this.id = new Int32Array(transfer ? len : data.params.sharedArrayBuffers.id); //@ts-ignore
139    this.cookie = new Int32Array(transfer ? len : data.params.sharedArrayBuffers.cookie); //@ts-ignore
140    this.depth = new Int32Array(transfer ? len : data.params.sharedArrayBuffers.depth); //@ts-ignore
141    this.argsetid = new Int32Array(transfer ? len : data.params.sharedArrayBuffers.argsetid);
142  }
143}
144