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