1fb726d48Sopenharmony_ci/* 2fb726d48Sopenharmony_ci * Copyright (C) 2022 Huawei Device Co., Ltd. 3fb726d48Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4fb726d48Sopenharmony_ci * you may not use this file except in compliance with the License. 5fb726d48Sopenharmony_ci * You may obtain a copy of the License at 6fb726d48Sopenharmony_ci * 7fb726d48Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8fb726d48Sopenharmony_ci * 9fb726d48Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10fb726d48Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11fb726d48Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12fb726d48Sopenharmony_ci * See the License for the specific language governing permissions and 13fb726d48Sopenharmony_ci * limitations under the License. 14fb726d48Sopenharmony_ci */ 15fb726d48Sopenharmony_ciimport { query } from '../SqlLite'; 16fb726d48Sopenharmony_ciimport { NativeHookMalloc, NativeHookProcess, NativeHookSampleQueryInfo } from '../../bean/NativeHook'; 17fb726d48Sopenharmony_ci 18fb726d48Sopenharmony_ciexport const queryNativeHookResponseTypes = ( 19fb726d48Sopenharmony_ci leftNs: number, 20fb726d48Sopenharmony_ci rightNs: number, 21fb726d48Sopenharmony_ci types: Array<string | number>, 22fb726d48Sopenharmony_ci pid: number, 23fb726d48Sopenharmony_ci isStatistic: boolean 24fb726d48Sopenharmony_ci): //@ts-ignore 25fb726d48Sopenharmony_ci Promise<Array<unknown>> => { 26fb726d48Sopenharmony_ci const table = isStatistic ? 'native_hook_statistic' : 'native_hook'; 27fb726d48Sopenharmony_ci const tsKey = isStatistic ? 'ts' : 'start_ts'; 28fb726d48Sopenharmony_ci const type = isStatistic ? 'type' : 'event_type'; 29fb726d48Sopenharmony_ci return query( 30fb726d48Sopenharmony_ci 'queryNativeHookResponseTypes', 31fb726d48Sopenharmony_ci ` 32fb726d48Sopenharmony_ci select 33fb726d48Sopenharmony_ci distinct last_lib_id as lastLibId, 34fb726d48Sopenharmony_ci data_dict.data as value 35fb726d48Sopenharmony_ci from 36fb726d48Sopenharmony_ci ${table} A ,trace_range B 37fb726d48Sopenharmony_ci left join data_dict on A.last_lib_id = data_dict.id 38fb726d48Sopenharmony_ci where 39fb726d48Sopenharmony_ci A.${tsKey} - B.start_ts 40fb726d48Sopenharmony_ci between ${leftNs} and ${rightNs} and A.${type} in (${types.join(',')}) 41fb726d48Sopenharmony_ci and A.ipid = ${pid}; 42fb726d48Sopenharmony_ci `, 43fb726d48Sopenharmony_ci { $leftNs: leftNs, $rightNs: rightNs, $types: types, $pid: pid } 44fb726d48Sopenharmony_ci ); 45fb726d48Sopenharmony_ci}; 46fb726d48Sopenharmony_ciexport const queryNativeHookStatistics = ( 47fb726d48Sopenharmony_ci leftNs: number, 48fb726d48Sopenharmony_ci rightNs: number, 49fb726d48Sopenharmony_ci ipid: number 50fb726d48Sopenharmony_ci): Promise<Array<NativeHookMalloc>> => 51fb726d48Sopenharmony_ci query( 52fb726d48Sopenharmony_ci 'queryNativeHookStatistics', 53fb726d48Sopenharmony_ci ` 54fb726d48Sopenharmony_ci select 55fb726d48Sopenharmony_ci event_type as eventType, 56fb726d48Sopenharmony_ci sub_type_id as subTypeId, 57fb726d48Sopenharmony_ci max(heap_size) as max, 58fb726d48Sopenharmony_ci sum(case when ((A.start_ts - B.start_ts) between ${leftNs} and ${rightNs}) then heap_size else 0 end) as allocByte, 59fb726d48Sopenharmony_ci sum(case when ((A.start_ts - B.start_ts) between ${leftNs} and ${rightNs}) then 1 else 0 end) as allocCount, 60fb726d48Sopenharmony_ci sum(case when ((A.end_ts - B.start_ts) between ${leftNs} and ${rightNs} ) then heap_size else 0 end) as freeByte, 61fb726d48Sopenharmony_ci sum(case when ((A.end_ts - B.start_ts) between ${leftNs} and ${rightNs} ) then 1 else 0 end) as freeCount 62fb726d48Sopenharmony_ci from 63fb726d48Sopenharmony_ci native_hook A, 64fb726d48Sopenharmony_ci trace_range B 65fb726d48Sopenharmony_ci where 66fb726d48Sopenharmony_ci (A.start_ts - B.start_ts) between ${leftNs} and ${rightNs} 67fb726d48Sopenharmony_ci and (event_type = 'AllocEvent' or event_type = 'MmapEvent') 68fb726d48Sopenharmony_ci and ipid = ${ipid} 69fb726d48Sopenharmony_ci group by event_type;`, 70fb726d48Sopenharmony_ci { $leftNs: leftNs, $rightNs: rightNs } 71fb726d48Sopenharmony_ci ); 72fb726d48Sopenharmony_ci 73fb726d48Sopenharmony_ciexport const queryNativeHookStatisticsMalloc = ( 74fb726d48Sopenharmony_ci leftNs: number, 75fb726d48Sopenharmony_ci rightNs: number, 76fb726d48Sopenharmony_ci ipid: number 77fb726d48Sopenharmony_ci): Promise<Array<NativeHookMalloc>> => 78fb726d48Sopenharmony_ci query( 79fb726d48Sopenharmony_ci 'queryNativeHookStatisticsMalloc', 80fb726d48Sopenharmony_ci ` 81fb726d48Sopenharmony_ci select 82fb726d48Sopenharmony_ci event_type as eventType, 83fb726d48Sopenharmony_ci heap_size as heapSize, 84fb726d48Sopenharmony_ci sum(case when ((A.start_ts - B.start_ts) between ${leftNs} and ${rightNs}) then heap_size else 0 end) as allocByte, 85fb726d48Sopenharmony_ci sum(case when ((A.start_ts - B.start_ts) between ${leftNs} and ${rightNs}) then 1 else 0 end) as allocCount, 86fb726d48Sopenharmony_ci sum(case when ((A.end_ts - B.start_ts) between ${leftNs} and ${rightNs} ) then heap_size else 0 end) as freeByte, 87fb726d48Sopenharmony_ci sum(case when ((A.end_ts - B.start_ts) between ${leftNs} and ${rightNs} ) then 1 else 0 end) as freeCount 88fb726d48Sopenharmony_ci from 89fb726d48Sopenharmony_ci native_hook A, 90fb726d48Sopenharmony_ci trace_range B 91fb726d48Sopenharmony_ci where 92fb726d48Sopenharmony_ci (A.start_ts - B.start_ts) between ${leftNs} and ${rightNs} 93fb726d48Sopenharmony_ci and 94fb726d48Sopenharmony_ci (event_type = 'AllocEvent' or event_type = 'MmapEvent') 95fb726d48Sopenharmony_ci and 96fb726d48Sopenharmony_ci sub_type_id is null 97fb726d48Sopenharmony_ci and ipid = ${ipid} 98fb726d48Sopenharmony_ci group by 99fb726d48Sopenharmony_ci event_type, 100fb726d48Sopenharmony_ci heap_size 101fb726d48Sopenharmony_ci order by heap_size desc 102fb726d48Sopenharmony_ci `, 103fb726d48Sopenharmony_ci { $leftNs: leftNs, $rightNs: rightNs } 104fb726d48Sopenharmony_ci ); 105fb726d48Sopenharmony_ci 106fb726d48Sopenharmony_ciexport const queryNativeHookStatisticsSubType = ( 107fb726d48Sopenharmony_ci leftNs: number, 108fb726d48Sopenharmony_ci rightNs: number, 109fb726d48Sopenharmony_ci ipid: number 110fb726d48Sopenharmony_ci): Promise<Array<NativeHookMalloc>> => 111fb726d48Sopenharmony_ci query( 112fb726d48Sopenharmony_ci 'queryNativeHookStatisticsSubType', 113fb726d48Sopenharmony_ci ` 114fb726d48Sopenharmony_ci select 115fb726d48Sopenharmony_ci event_type as eventType, 116fb726d48Sopenharmony_ci sub_type_id as subTypeId, 117fb726d48Sopenharmony_ci max(heap_size) as max, 118fb726d48Sopenharmony_ci sum(case when ((NH.start_ts - TR.start_ts) between ${leftNs} and ${rightNs}) then heap_size else 0 end) as allocByte, 119fb726d48Sopenharmony_ci sum(case when ((NH.start_ts - TR.start_ts) between ${leftNs} and ${rightNs}) then 1 else 0 end) as allocCount, 120fb726d48Sopenharmony_ci sum(case when ((NH.end_ts - TR.start_ts) between ${leftNs} and ${rightNs} ) then heap_size else 0 end) as freeByte, 121fb726d48Sopenharmony_ci sum(case when ((NH.end_ts - TR.start_ts) between ${leftNs} and ${rightNs} ) then 1 else 0 end) as freeCount 122fb726d48Sopenharmony_ci from 123fb726d48Sopenharmony_ci native_hook NH, 124fb726d48Sopenharmony_ci trace_range TR 125fb726d48Sopenharmony_ci where 126fb726d48Sopenharmony_ci (NH.start_ts - TR.start_ts) between ${leftNs} and ${rightNs} 127fb726d48Sopenharmony_ci and 128fb726d48Sopenharmony_ci (event_type = 'MmapEvent') 129fb726d48Sopenharmony_ci and ipid = ${ipid} 130fb726d48Sopenharmony_ci group by 131fb726d48Sopenharmony_ci event_type,sub_type_id; 132fb726d48Sopenharmony_ci `, 133fb726d48Sopenharmony_ci { $leftNs: leftNs, $rightNs: rightNs } 134fb726d48Sopenharmony_ci ); 135fb726d48Sopenharmony_ci 136fb726d48Sopenharmony_ciexport const queryNativeHookSubType = ( 137fb726d48Sopenharmony_ci leftNs: number, 138fb726d48Sopenharmony_ci rightNs: number, 139fb726d48Sopenharmony_ci ipid: number 140fb726d48Sopenharmony_ci): //@ts-ignore 141fb726d48Sopenharmony_ci Promise<Array<unknown>> => 142fb726d48Sopenharmony_ci query( 143fb726d48Sopenharmony_ci 'queryNativeHookSubType', 144fb726d48Sopenharmony_ci `select distinct( 145fb726d48Sopenharmony_ci case when sub_type_id is null then -1 else sub_type_id end 146fb726d48Sopenharmony_ci) as subTypeId, 147fb726d48Sopenharmony_ci(case when sub_type_id is null then 'Other MmapEvent' else DD.data end) as subType 148fb726d48Sopenharmony_ci from 149fb726d48Sopenharmony_ci native_hook NH, 150fb726d48Sopenharmony_ci trace_range TR 151fb726d48Sopenharmony_ci left join data_dict DD on NH.sub_type_id = DD.id 152fb726d48Sopenharmony_ciwhere event_type = 'MmapEvent' and 153fb726d48Sopenharmony_ci (NH.start_ts - TR.start_ts) between ${leftNs} and ${rightNs} 154fb726d48Sopenharmony_ci and ipid = ${ipid} 155fb726d48Sopenharmony_ci `, 156fb726d48Sopenharmony_ci { $leftNs: leftNs, $rightNs: rightNs } 157fb726d48Sopenharmony_ci ); 158fb726d48Sopenharmony_ci 159fb726d48Sopenharmony_ciexport const queryNativeHookStatisticSubType = ( 160fb726d48Sopenharmony_ci leftNs: number, 161fb726d48Sopenharmony_ci rightNs: number, 162fb726d48Sopenharmony_ci ipid: number 163fb726d48Sopenharmony_ci): //@ts-ignore 164fb726d48Sopenharmony_ci Promise<Array<unknown>> => 165fb726d48Sopenharmony_ci query( 166fb726d48Sopenharmony_ci 'queryNativeHookStatisticSubType', 167fb726d48Sopenharmony_ci `SELECT DISTINCT 168fb726d48Sopenharmony_ci CASE 169fb726d48Sopenharmony_ci WHEN type = 3 AND sub_type_id NOT NULL THEN sub_type_id 170fb726d48Sopenharmony_ci ELSE type 171fb726d48Sopenharmony_ci END AS subTypeId, 172fb726d48Sopenharmony_ci CASE 173fb726d48Sopenharmony_ci WHEN type = 2 THEN 'FILE_PAGE_MSG' 174fb726d48Sopenharmony_ci WHEN type = 3 AND sub_type_id NOT NULL THEN D.data 175fb726d48Sopenharmony_ci WHEN type = 3 THEN 'MEMORY_USING_MSG' 176fb726d48Sopenharmony_ci ELSE 'Other MmapEvent' 177fb726d48Sopenharmony_ci END AS subType 178fb726d48Sopenharmony_ci FROM 179fb726d48Sopenharmony_ci native_hook_statistic NHS 180fb726d48Sopenharmony_ci LEFT JOIN data_dict D ON NHS.sub_type_id = D.id, 181fb726d48Sopenharmony_ci trace_range TR 182fb726d48Sopenharmony_ci WHERE 183fb726d48Sopenharmony_ci NHS.type >= 1 AND 184fb726d48Sopenharmony_ci (NHS.ts - TR.start_ts) between ${leftNs} and ${rightNs} 185fb726d48Sopenharmony_ci AND ipid = ${ipid} 186fb726d48Sopenharmony_ci `, 187fb726d48Sopenharmony_ci { $leftNs: leftNs, $rightNs: rightNs } 188fb726d48Sopenharmony_ci ); 189fb726d48Sopenharmony_ci 190fb726d48Sopenharmony_ciexport const queryNativeHookStatisticsCount = (): Promise<Array<{ num: number }>> => 191fb726d48Sopenharmony_ci query('queryNativeHookStatisticsCount', `select count(1) num from native_hook_statistic`, {}); 192fb726d48Sopenharmony_ci 193fb726d48Sopenharmony_ciexport const queryNativeHookProcess = (table: string): Promise<Array<NativeHookProcess>> => { 194fb726d48Sopenharmony_ci let sql = ` 195fb726d48Sopenharmony_ci select 196fb726d48Sopenharmony_ci distinct ${table}.ipid, 197fb726d48Sopenharmony_ci pid, 198fb726d48Sopenharmony_ci name 199fb726d48Sopenharmony_ci from 200fb726d48Sopenharmony_ci ${table} 201fb726d48Sopenharmony_ci left join 202fb726d48Sopenharmony_ci process p 203fb726d48Sopenharmony_ci on 204fb726d48Sopenharmony_ci ${table}.ipid = p.id 205fb726d48Sopenharmony_ci `; 206fb726d48Sopenharmony_ci return query('queryNativeHookProcess', sql, {}); 207fb726d48Sopenharmony_ci}; 208fb726d48Sopenharmony_ci 209fb726d48Sopenharmony_ciexport const queryNativeHookSnapshotTypes = (ipid: number): Promise<Array<NativeHookSampleQueryInfo>> => 210fb726d48Sopenharmony_ci query( 211fb726d48Sopenharmony_ci 'queryNativeHookSnapshotTypes', 212fb726d48Sopenharmony_ci ` 213fb726d48Sopenharmony_ciselect 214fb726d48Sopenharmony_ci event_type as eventType, 215fb726d48Sopenharmony_ci data as subType 216fb726d48Sopenharmony_ci from 217fb726d48Sopenharmony_ci native_hook left join data_dict on native_hook.sub_type_id = data_dict.id 218fb726d48Sopenharmony_ci where 219fb726d48Sopenharmony_ci (event_type = 'AllocEvent' or event_type = 'MmapEvent') 220fb726d48Sopenharmony_ci and ipid = ${ipid} 221fb726d48Sopenharmony_ci group by 222fb726d48Sopenharmony_ci event_type,data;`, 223fb726d48Sopenharmony_ci {} 224fb726d48Sopenharmony_ci ); 225fb726d48Sopenharmony_ci 226fb726d48Sopenharmony_ciexport const queryAllHookData = (rightNs: number, ipid: number): Promise<Array<NativeHookSampleQueryInfo>> => 227fb726d48Sopenharmony_ci query( 228fb726d48Sopenharmony_ci 'queryAllHookData', 229fb726d48Sopenharmony_ci ` 230fb726d48Sopenharmony_ci select 231fb726d48Sopenharmony_ci callchain_id as eventId, 232fb726d48Sopenharmony_ci event_type as eventType, 233fb726d48Sopenharmony_ci data as subType, 234fb726d48Sopenharmony_ci addr, 235fb726d48Sopenharmony_ci heap_size as growth, 236fb726d48Sopenharmony_ci (n.start_ts - t.start_ts) as startTs, 237fb726d48Sopenharmony_ci (n.end_ts - t.start_ts) as endTs 238fb726d48Sopenharmony_ci from 239fb726d48Sopenharmony_ci native_hook n left join data_dict on n.sub_type_id = data_dict.id, 240fb726d48Sopenharmony_ci trace_range t 241fb726d48Sopenharmony_ci where 242fb726d48Sopenharmony_ci (event_type = 'AllocEvent' or event_type = 'MmapEvent') 243fb726d48Sopenharmony_ci and ipid = ${ipid} 244fb726d48Sopenharmony_ci and 245fb726d48Sopenharmony_ci n.start_ts between t.start_ts and ${rightNs} + t.start_ts`, 246fb726d48Sopenharmony_ci { $rightNs: rightNs } 247fb726d48Sopenharmony_ci ); 248