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