1fb726d48Sopenharmony_ci// Copyright (c) 2021 Huawei Device Co., Ltd. 2fb726d48Sopenharmony_ci// Licensed under the Apache License, Version 2.0 (the "License"); 3fb726d48Sopenharmony_ci// you may not use this file except in compliance with the License. 4fb726d48Sopenharmony_ci// You may obtain a copy of the License at 5fb726d48Sopenharmony_ci// 6fb726d48Sopenharmony_ci// http://www.apache.org/licenses/LICENSE-2.0 7fb726d48Sopenharmony_ci// 8fb726d48Sopenharmony_ci// Unless required by applicable law or agreed to in writing, software 9fb726d48Sopenharmony_ci// distributed under the License is distributed on an "AS IS" BASIS, 10fb726d48Sopenharmony_ci// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11fb726d48Sopenharmony_ci// See the License for the specific language governing permissions and 12fb726d48Sopenharmony_ci// limitations under the License. 13fb726d48Sopenharmony_ci 14fb726d48Sopenharmony_ciimport { Args } from '../CommonArgs'; 15fb726d48Sopenharmony_ciimport { threadStateList } from '../utils/AllMemoryCache'; 16fb726d48Sopenharmony_ciimport { filterDataByGroup } from '../utils/DataFilter'; 17fb726d48Sopenharmony_ciimport { TraficEnum, threadStateToNumber } from '../utils/QueryEnum'; 18fb726d48Sopenharmony_ci 19fb726d48Sopenharmony_ciexport const chartThreadDataSql = (args: Args):unknown => { 20fb726d48Sopenharmony_ci return `select B.cpu, max(B.dur) AS dur, B.itid AS id, B.tid AS tid, B.state, B.pid, 21fb726d48Sopenharmony_ci B.ts - ${args.recordStartNS} AS startTime, ifnull(B.arg_setid, -1) AS argSetId, 22fb726d48Sopenharmony_ci ((B.ts - ${args.recordStartNS}) / (${Math.floor((args.endNS - args.startNS) / args.width)})) AS px 23fb726d48Sopenharmony_ci from thread_state AS B 24fb726d48Sopenharmony_ci where B.tid = ${args.tid} 25fb726d48Sopenharmony_ci and B.pid = ${args.pid} 26fb726d48Sopenharmony_ci and B.state != 'Running' 27fb726d48Sopenharmony_ci and startTime + dur >= ${Math.floor(args.startNS)} 28fb726d48Sopenharmony_ci and startTime <= ${Math.floor(args.endNS)} 29fb726d48Sopenharmony_ci group by px 30fb726d48Sopenharmony_ci union all 31fb726d48Sopenharmony_ci select B.cpu, max(B.dur) AS dur, B.itid AS id, B.tid AS tid, B.state, 32fb726d48Sopenharmony_ci B.pid, B.ts - ${args.recordStartNS} AS startTime, ifnull(B.arg_setid, -1) AS argSetId, 33fb726d48Sopenharmony_ci ((B.ts - ${args.recordStartNS}) / (${Math.floor((args.endNS - args.startNS) / args.width)})) AS px 34fb726d48Sopenharmony_ci from thread_state AS B 35fb726d48Sopenharmony_ci where B.tid = ${args.tid} 36fb726d48Sopenharmony_ci and B.pid = ${args.pid} 37fb726d48Sopenharmony_ci and B.state = 'Running' 38fb726d48Sopenharmony_ci and startTime + dur >= ${Math.floor(args.startNS)} 39fb726d48Sopenharmony_ci and startTime <= ${Math.floor(args.endNS)} 40fb726d48Sopenharmony_ci group by px; 41fb726d48Sopenharmony_ci ;`; 42fb726d48Sopenharmony_ci}; 43fb726d48Sopenharmony_ci 44fb726d48Sopenharmony_ciexport const sqlMem = (args: Args): string => { 45fb726d48Sopenharmony_ci return `select B.cpu, B.dur AS dur, B.itid AS id, B.tid AS tid, B.state, B.pid, B.ts - ${args.recordStartNS} AS startTime, 46fb726d48Sopenharmony_ci ifnull(B.arg_setid, -1) AS argSetId 47fb726d48Sopenharmony_ci from thread_state AS B 48fb726d48Sopenharmony_ci where B.tid = ${args.tid} 49fb726d48Sopenharmony_ci and B.pid = ${args.pid};`; 50fb726d48Sopenharmony_ci}; 51fb726d48Sopenharmony_ci 52fb726d48Sopenharmony_ciexport function threadDataReceiver(data: unknown, proc: Function): void { 53fb726d48Sopenharmony_ci //@ts-ignore 54fb726d48Sopenharmony_ci if (data.params.trafic === TraficEnum.Memory) { 55fb726d48Sopenharmony_ci //@ts-ignore 56fb726d48Sopenharmony_ci let key = `${data.params.pid}-${data.params.tid}`; 57fb726d48Sopenharmony_ci if (!threadStateList.has(key)) { 58fb726d48Sopenharmony_ci //@ts-ignore 59fb726d48Sopenharmony_ci threadStateList.set(key, proc(sqlMem(data.params))); 60fb726d48Sopenharmony_ci } 61fb726d48Sopenharmony_ci let array = threadStateList.get(key) || []; 62fb726d48Sopenharmony_ci let res = filterDataByGroup( 63fb726d48Sopenharmony_ci array, 64fb726d48Sopenharmony_ci 'startTime', 65fb726d48Sopenharmony_ci 'dur', //@ts-ignore 66fb726d48Sopenharmony_ci data.params.startNS, //@ts-ignore 67fb726d48Sopenharmony_ci data.params.endNS, //@ts-ignore 68fb726d48Sopenharmony_ci data.params.width, 69fb726d48Sopenharmony_ci undefined, 70fb726d48Sopenharmony_ci //@ts-ignore 71fb726d48Sopenharmony_ci (a) => a.state === 'Running', 72fb726d48Sopenharmony_ci false 73fb726d48Sopenharmony_ci ); 74fb726d48Sopenharmony_ci arrayBufferHandler(data, res, true, array.length === 0); 75fb726d48Sopenharmony_ci return; 76fb726d48Sopenharmony_ci } else { 77fb726d48Sopenharmony_ci //@ts-ignore 78fb726d48Sopenharmony_ci let sql = chartThreadDataSql(data.params); 79fb726d48Sopenharmony_ci let res = proc(sql); //@ts-ignore 80fb726d48Sopenharmony_ci arrayBufferHandler(data, res, data.params.trafic !== TraficEnum.SharedArrayBuffer, false); 81fb726d48Sopenharmony_ci } 82fb726d48Sopenharmony_ci} 83fb726d48Sopenharmony_ci 84fb726d48Sopenharmony_cifunction arrayBufferHandler(data: unknown, res: unknown[], transfer: boolean, isEmpty: boolean): void { 85fb726d48Sopenharmony_ci //@ts-ignore 86fb726d48Sopenharmony_ci let startTime = new Float64Array(transfer ? res.length : data.params.sharedArrayBuffers.startTime); //@ts-ignore 87fb726d48Sopenharmony_ci let dur = new Float64Array(transfer ? res.length : data.params.sharedArrayBuffers.dur); //@ts-ignore 88fb726d48Sopenharmony_ci let cpu = new Int8Array(transfer ? res.length : data.params.sharedArrayBuffers.cpu); //@ts-ignore 89fb726d48Sopenharmony_ci let id = new Int32Array(transfer ? res.length : data.params.sharedArrayBuffers.id); //@ts-ignore 90fb726d48Sopenharmony_ci let tid = new Int32Array(transfer ? res.length : data.params.sharedArrayBuffers.tid); //@ts-ignore 91fb726d48Sopenharmony_ci let state = new Int32Array(transfer ? res.length : data.params.sharedArrayBuffers.state); //@ts-ignore 92fb726d48Sopenharmony_ci let pid = new Int32Array(transfer ? res.length : data.params.sharedArrayBuffers.pid); //@ts-ignore 93fb726d48Sopenharmony_ci let argSetID = new Int32Array(transfer ? res.length : data.params.sharedArrayBuffers.argSetID); 94fb726d48Sopenharmony_ci res.forEach((it, i) => { 95fb726d48Sopenharmony_ci //@ts-ignore 96fb726d48Sopenharmony_ci data.params.trafic === TraficEnum.ProtoBuffer && (it = it.processThreadData); //@ts-ignore 97fb726d48Sopenharmony_ci startTime[i] = it.startTime; //@ts-ignore 98fb726d48Sopenharmony_ci dur[i] = it.dur; //@ts-ignore 99fb726d48Sopenharmony_ci cpu[i] = it.cpu; //@ts-ignore 100fb726d48Sopenharmony_ci id[i] = it.id; //@ts-ignore 101fb726d48Sopenharmony_ci tid[i] = it.tid; //@ts-ignore 102fb726d48Sopenharmony_ci state[i] = threadStateToNumber(it.state); //@ts-ignore 103fb726d48Sopenharmony_ci pid[i] = it.pid; //@ts-ignore 104fb726d48Sopenharmony_ci argSetID[i] = it.argSetId; 105fb726d48Sopenharmony_ci }); 106fb726d48Sopenharmony_ci (self as unknown as Worker).postMessage( 107fb726d48Sopenharmony_ci { 108fb726d48Sopenharmony_ci //@ts-ignore 109fb726d48Sopenharmony_ci id: data.id, //@ts-ignore 110fb726d48Sopenharmony_ci action: data.action, 111fb726d48Sopenharmony_ci results: transfer 112fb726d48Sopenharmony_ci ? { 113fb726d48Sopenharmony_ci id: id.buffer, 114fb726d48Sopenharmony_ci tid: tid.buffer, 115fb726d48Sopenharmony_ci state: state.buffer, 116fb726d48Sopenharmony_ci startTime: startTime.buffer, 117fb726d48Sopenharmony_ci dur: dur.buffer, 118fb726d48Sopenharmony_ci cpu: cpu.buffer, 119fb726d48Sopenharmony_ci pid: pid.buffer, 120fb726d48Sopenharmony_ci argSetID: argSetID.buffer, 121fb726d48Sopenharmony_ci } 122fb726d48Sopenharmony_ci : {}, 123fb726d48Sopenharmony_ci len: res.length, 124fb726d48Sopenharmony_ci transfer: transfer, 125fb726d48Sopenharmony_ci isEmpty: isEmpty, 126fb726d48Sopenharmony_ci }, 127fb726d48Sopenharmony_ci transfer 128fb726d48Sopenharmony_ci ? [startTime.buffer, dur.buffer, cpu.buffer, id.buffer, tid.buffer, state.buffer, pid.buffer, argSetID.buffer] 129fb726d48Sopenharmony_ci : [] 130fb726d48Sopenharmony_ci ); 131fb726d48Sopenharmony_ci} 132