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 { threadSysCallList } from '../utils/AllMemoryCache'; 16import { filterDataByGroup } from '../utils/DataFilter'; 17import { TraficEnum } from '../utils/QueryEnum'; 18 19export const chartThreadSysCallDataSql = (args: Args):unknown => { 20 return `SELECT syscall_number as id, itid, ts - ${args.recordStartNS} as startTs, max(dur) as dur, 21 ((ts - ${args.recordStartNS}) / (${Math.floor((args.endNS - args.startNS) / args.width)})) AS px 22 from syscall 23 where itid = ${args.itid} 24 and startTs + dur >= ${Math.floor(args.startNS)} 25 and startTs <= ${Math.floor(args.endNS)} 26 group by px 27 `; 28}; 29 30export const sysCallMemSql = (args: Args): unknown => { 31 return `select syscall_number as id, (ts - ${args.recordStartNS}) as startTs, dur from syscall where itid = ${args.itid}` 32} 33 34export function threadSysCallDataReceiver(data: unknown, proc: Function): void { 35 //@ts-ignore 36 let itid: number = data.params.itid; 37 let arr: unknown[] = []; 38 if (threadSysCallList.has(itid)) { 39 arr = threadSysCallList.get(itid) || []; 40 } else { 41 //@ts-ignore 42 let sql = sysCallMemSql(data.params); 43 arr = proc(sql); //@ts-ignore 44 threadSysCallList.set(itid, arr); 45 } 46 let res = filterDataByGroup( 47 arr, 48 'startTs', 49 'dur', //@ts-ignore 50 data.params.startNS, //@ts-ignore 51 data.params.endNS, //@ts-ignore 52 data.params.width 53 ); 54 arrayBufferHandler(data, res, true, false); 55} 56 57function arrayBufferHandler(data: unknown, res: unknown[], transfer: boolean, isEmpty: boolean): void { 58 //@ts-ignore 59 let startTs = new Float64Array(res.length); //@ts-ignore 60 let dur = new Float64Array(res.length); //@ts-ignore 61 let id = new Int32Array(res.length); //@ts-ignore 62 let itid = new Int32Array(res.length); //@ts-ignore 63 res.forEach((it, i) => { 64 //@ts-ignore 65 data.params.trafic === TraficEnum.ProtoBuffer && (it = it.processThreadSysCallData); //@ts-ignore 66 startTs[i] = it.startTs; //@ts-ignore 67 dur[i] = it.dur; //@ts-ignore 68 id[i] = it.id; //@ts-ignore 69 itid[i] = data.params.itid; //@ts-ignore 70 }); 71 (self as unknown as Worker).postMessage( 72 { 73 //@ts-ignore 74 id: data.id, //@ts-ignorew 75 action: data.action, 76 results: transfer 77 ? { 78 id: id.buffer, 79 itid: itid.buffer, 80 startTs: startTs.buffer, 81 dur: dur.buffer, 82 } 83 : {}, 84 len: res.length, 85 transfer: transfer, 86 isEmpty: isEmpty, 87 }, 88 transfer 89 ? [startTs.buffer, dur.buffer, id.buffer, itid.buffer] 90 : [] 91 ); 92} 93