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'; 15 16export const chartProcessDeliverInputEventDataSql = (args: any): string => { 17 return ` 18 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 callstack C on A.id = C.callid 35 left join process P on P.id = A.ipid 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: any, proc: Function): void { 46 let sql = chartProcessDeliverInputEventDataSql(data.params); 47 let res = proc(sql); 48 arrayBufferHandler(data, res, data.params.trafic !== TraficEnum.SharedArrayBuffer); 49} 50 51function arrayBufferHandler(data: any, res: any[], transfer: boolean): void { 52 let processDeliverInputEvent = new ProcessDeliverInputEvent(data, transfer, res.length); 53 res.forEach((it, i) => { 54 data.params.trafic === TraficEnum.ProtoBuffer && (it = it.processInputEventData); 55 processDeliverInputEvent.tid[i] = it.tid; 56 processDeliverInputEvent.dur[i] = it.dur; 57 processDeliverInputEvent.is_main_thread[i] = it.isMainThread; 58 processDeliverInputEvent.track_id[i] = it.trackId; 59 processDeliverInputEvent.startTs[i] = it.startTs; 60 processDeliverInputEvent.pid[i] = it.pid; 61 processDeliverInputEvent.parent_id[i] = it.parentId; 62 processDeliverInputEvent.id[i] = it.id; 63 processDeliverInputEvent.cookie[i] = it.cookie; 64 processDeliverInputEvent.depth[i] = it.depth; 65 processDeliverInputEvent.argsetid[i] = it.argsetid; 66 }); 67 postMessage(data, transfer, processDeliverInputEvent, res.length); 68} 69function postMessage(data: any, transfer: boolean, processDeliverInputEvent: ProcessDeliverInputEvent, len: number) { 70 (self as unknown as Worker).postMessage( 71 { 72 transfer: transfer, 73 id: data.id, 74 action: data.action, 75 results: transfer 76 ? { 77 tid: processDeliverInputEvent.tid.buffer, 78 dur: processDeliverInputEvent.dur.buffer, 79 is_main_thread: processDeliverInputEvent.is_main_thread.buffer, 80 track_id: processDeliverInputEvent.track_id.buffer, 81 startTs: processDeliverInputEvent.startTs.buffer, 82 pid: processDeliverInputEvent.pid.buffer, 83 parent_id: processDeliverInputEvent.parent_id.buffer, 84 id: processDeliverInputEvent.id.buffer, 85 cookie: processDeliverInputEvent.cookie.buffer, 86 depth: processDeliverInputEvent.depth.buffer, 87 argsetid: processDeliverInputEvent.argsetid.buffer, 88 } 89 : {}, 90 len: len, 91 }, 92 transfer 93 ? [ 94 processDeliverInputEvent.tid.buffer, 95 processDeliverInputEvent.dur.buffer, 96 processDeliverInputEvent.is_main_thread.buffer, 97 processDeliverInputEvent.track_id.buffer, 98 processDeliverInputEvent.startTs.buffer, 99 processDeliverInputEvent.pid.buffer, 100 processDeliverInputEvent.parent_id.buffer, 101 processDeliverInputEvent.id.buffer, 102 processDeliverInputEvent.cookie.buffer, 103 processDeliverInputEvent.depth.buffer, 104 processDeliverInputEvent.argsetid.buffer, 105 ] 106 : [] 107 ); 108} 109class ProcessDeliverInputEvent { 110 tid: Int32Array; 111 pid: Int32Array; 112 is_main_thread: Int8Array; 113 track_id: Int32Array; 114 startTs: Float64Array; 115 dur: Float64Array; 116 parent_id: Int32Array; 117 id: Int32Array; 118 cookie: Int32Array; 119 depth: Int32Array; 120 argsetid: Int32Array; 121 constructor(data: any, transfer: boolean, len: number) { 122 this.tid = new Int32Array(transfer ? len : data.params.sharedArrayBuffers.tid); 123 this.pid = new Int32Array(transfer ? len : data.params.sharedArrayBuffers.pid); 124 this.is_main_thread = new Int8Array(transfer ? len : data.params.sharedArrayBuffers.is_main_thread); 125 this.track_id = new Int32Array(transfer ? len : data.params.sharedArrayBuffers.track_id); 126 this.startTs = new Float64Array(transfer ? len : data.params.sharedArrayBuffers.startTs); 127 this.dur = new Float64Array(transfer ? len : data.params.sharedArrayBuffers.dur); 128 this.parent_id = new Int32Array(transfer ? len : data.params.sharedArrayBuffers.parent_id); 129 this.id = new Int32Array(transfer ? len : data.params.sharedArrayBuffers.id); 130 this.cookie = new Int32Array(transfer ? len : data.params.sharedArrayBuffers.cookie); 131 this.depth = new Int32Array(transfer ? len : data.params.sharedArrayBuffers.depth); 132 this.argsetid = new Int32Array(transfer ? len : data.params.sharedArrayBuffers.argsetid); 133 } 134} 135