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