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 */ 15// VM Tracker Gpu Memory泳道图 16import type {SnapshotStruct} from "../ui-worker/ProcedureWorkerSnapshot"; 17import {query} from "../SqlLite"; 18import {GpuMemory, GpuMemoryComparison} from "../../bean/AbilityMonitor"; 19import type {MemoryConfig} from "../../bean/MemoryConfig"; 20 21export const queryGpuMemoryData = (processId: number): Promise<Array<SnapshotStruct>> => 22 query( 23 'queryGpuMemorySampsData', 24 `SELECT 25 (A.ts - B.start_ts) as startNs, 26 sum(A.used_gpu_size) as value, 27 A.ipid as ipid 28 FROM memory_process_gpu A,trace_range B 29 WHERE 30 $pid = A.ipid 31 AND A.ts < B.end_ts 32 GROUP by A.ts;`, 33 { $pid: processId } 34 ); 35 36// 判断VM Tracker Gpu Memory泳道图是否有数据 37export const queryisExistsGpuMemoryData = (processId: number): Promise<Array<SnapshotStruct>> => 38 query( 39 'queryisExistsGpuMemoryData', 40 `SELECT EXISTS ( 41 SELECT 1 42 FROM memory_process_gpu A, trace_range B 43 WHERE $pid = A.ipid 44 AND A.ts < B.end_ts 45 GROUP BY A.ts 46 ) AS data_exists`, 47 { $pid: processId } 48 ); 49//VM Tracker SkiaGpuMemory 框选 50export const getTabGpuMemoryData = ( 51 leftNs: number, 52 rightNs: number, 53 processId: number, 54 dur: number 55): Promise<Array<GpuMemory>> => 56 query<GpuMemory>( 57 'getTabGpuMemoryData', 58 `SELECT 59 (S.ts-TR.start_ts) as startNs, 60 gpu_name_id as gpuNameId, 61 T.tid as threadId, 62 T.name as threadName, 63 MAX(S.used_gpu_size) as maxSize, 64 MIN(S.used_gpu_size) as minSize, 65 Avg(S.used_gpu_size) as avgSize 66 from trace_range as TR,memory_process_gpu as S 67 left join thread as T on T.itid=S.itid 68 where 69 $leftNS <= startNs + ${dur} 70 and 71 $rightNS >= startNs 72 and 73 $pid = S.ipid 74 group by gpu_name_id,threadId 75 `, 76 { $leftNS: leftNs, $rightNS: rightNs, $pid: processId } 77 ); 78//VM Tracker SkiaGpuMemory 点选 79export const getTabGpuMemoryVMTrackerClickData = (startNs: number, processId: number): Promise<Array<GpuMemory>> => 80 query<GpuMemory>( 81 'getTabGpuMemoryVMTrackerClickData', 82 `SELECT 83 (S.ts-TR.start_ts) as startNs, 84 S.used_gpu_size as size, 85 T.tid as threadId, 86 T.name as threadName, 87 A.data as gpuName 88 from trace_range as TR,memory_process_gpu as S 89 left join thread as T on T.itid=S.itid 90 left join data_dict as A on A.id=S.gpu_name_id 91 WHERE 92 startNs = ${startNs} 93 AND 94 $pid = S.ipid 95 `, 96 { $startNs: startNs, $pid: processId } 97 ); 98 99//VM Tracker Gpu Memory 点选比较 100export const getTabGpuMemoryVmTrackerComparisonData = ( 101 startNs: number, 102 processId: number 103): Promise<Array<GpuMemoryComparison>> => 104 query<GpuMemoryComparison>( 105 'getTabGpuMemoryVmTrackerComparisonData', 106 `SELECT 107 (S.ts-TR.start_ts) as startNs, 108 sum(S.used_gpu_size) as value, 109 T.tid as threadId, 110 T.name as threadName, 111 S.gpu_name_id as gpuNameId 112 from trace_range as TR,memory_process_gpu as S 113 left join thread as T on T.itid=S.itid 114 WHERE 115 startNs = ${startNs} 116 AND 117 $pid = S.ipid 118 `, 119 { $startNs: startNs, $pid: processId } 120 ); 121export const queryMemFilterIdMaxValue = (): Promise<Array<{ filterId: number; maxValue: number }>> => { 122 return query( 123 'queryMemFilterIdMaxValue', 124 `select filter_id as filterId,max(value) maxValue from process_measure group by filter_id;` 125 ); 126}; 127export const getTabVirtualMemoryType = (startTime: number, endTime: number): Promise<Array<string>> => 128 query( 129 'getTabVirtualMemoryType', 130 ` 131 SELECT type from paged_memory_sample s,trace_range t 132 WHERE s.end_ts between $startTime + t.start_ts and $endTime + t.start_ts group by type`, 133 { $startTime: startTime, $endTime: endTime }, 134 'exec' 135 ); 136export const queryNativeMemoryRealTime = (): Promise<Array<any>> => 137 query( 138 'queryNativeMemoryRealTime', 139 `select cs.ts,cs.clock_name from datasource_clockid dc left join clock_snapshot cs on dc.clock_id = cs.clock_id where data_source_name = 'memory-plugin' or data_source_name = 'nativehook' 140`, 141 {} 142 ); 143export const queryJsMemoryData = (): Promise<Array<any>> => 144 query('queryJsMemoryData', `SELECT 1 WHERE EXISTS(SELECT 1 FROM js_heap_nodes)`); 145 146export const queryVmTrackerShmData = (iPid: number): Promise<Array<any>> => 147 query( 148 'queryVmTrackerShmData', 149 `SELECT (A.ts - B.start_ts) as startNs, 150 sum(A.size) as value 151 FROM 152 memory_ashmem A,trace_range B 153 where 154 A.ipid = ${iPid} 155 AND A.ts < B.end_ts 156 and 157 flag = 0 158 GROUP by A.ts`, 159 {} 160 ); 161export const queryVmTrackerShmSelectionData = (startNs: number, ipid: number): Promise<Array<any>> => 162 query( 163 'queryVmTrackerShmSelectionData', 164 `SELECT (A.ts - B.start_ts) as startNS,A.ipid, 165 A.fd,A.size,A.adj,A.ashmem_name_id as name, 166 A.ashmem_id as id,A.time,A.purged,A.ref_count as count, 167 A.flag 168 FROM memory_ashmem A,trace_range B 169 where startNS = ${startNs} and ipid = ${ipid};`, 170 {} 171 ); 172export const queryMemoryConfig = (): Promise<Array<MemoryConfig>> => 173 query( 174 'queryMemoryConfiig', 175 `SELECT ipid as iPid, process.pid AS pid, 176 process.name AS processName, 177 (SELECT value FROM trace_config WHERE trace_source = 'memory_config' AND key = 'sample_interval') AS interval 178 FROM 179 trace_config 180 LEFT JOIN process ON value = ipid 181 WHERE 182 trace_source = 'memory_config' 183 AND key = 'ipid' 184 ;` 185 ); 186// VM Tracker Purgeable泳道图 187export const queryPurgeableProcessData = (ipid: number, isPin?: boolean): Promise<Array<any>> => { 188 const pinSql = isPin ? ' AND a.ref_count > 0' : ''; 189 const names = isPin ? " ('mem.purg_pin')" : "('mem.purg_sum')"; 190 return query( 191 'queryPurgeableProcessData', 192 `SELECT startNs, sum( value ) AS value 193 FROM 194 (SELECT 195 m.ts - tr.start_ts AS startNs, 196 sum(m.value) AS value 197 FROM 198 process_measure m, 199 trace_range tr 200 LEFT JOIN process_measure_filter f ON f.id = m.filter_id 201 WHERE 202 m.ts < tr.end_ts 203 AND f.name = ${names} 204 AND f.ipid = ${ipid} 205 GROUP BY m.ts 206 UNION ALL 207 SELECT 208 a.ts - tr.start_ts AS startNs, 209 sum( a.pss ) AS value 210 FROM 211 memory_ashmem a, 212 trace_range tr 213 WHERE 214 a.ts < tr.end_ts 215 AND a.flag = 0 216 AND a.ipid = ${ipid} 217 ${pinSql} 218 GROUP BY a.ts) 219 GROUP BY startNs` 220 ); 221}; 222export const queryVirtualMemory = (): Promise<Array<any>> => 223 query('queryVirtualMemory', `select id,name from sys_event_filter where type='sys_virtual_memory_filter'`); 224export const queryVirtualMemoryData = (filterId: number): Promise<Array<any>> => 225 query( 226 'queryVirtualMemoryData', 227 `select ts-${ 228 (window as any).recordStartNS 229 } as startTime,value,filter_id as filterID from sys_mem_measure where filter_id=$filter_id`, 230 { $filter_id: filterId } 231 ); 232export const queryTraceMemory = (): Promise< 233 Array<{ 234 maxNum: string; 235 minNum: string; 236 avgNum: string; 237 name: string; 238 processName: string; 239 }> 240> => 241 query( 242 'queryTraceMemory', 243 ` 244 select 245 max(value) as maxNum, 246 min(value) as minNum, 247 avg(value) as avgNum, 248 filter.name as name, 249 p.name as processName 250 from process_measure 251 left join process_measure_filter as filter on filter.id= filter_id 252 left join process as p on p.id = filter.ipid 253 where 254 filter_id > 0 255 and 256 filter.name = 'mem.rss.anon' 257 group by 258 filter_id 259 order by 260 avgNum desc` 261 ); 262 263export const queryTraceMemoryTop = (): Promise< 264 Array<{ 265 maxNum: string; 266 minNum: string; 267 avgNum: string; 268 name: string; 269 processName: string; 270 }> 271> => 272 query( 273 'queryTraceMemoryTop', 274 ` 275 select 276 max(value) as maxNum, 277 min(value) as minNum, 278 avg(value) as avgNum, 279 f.name as name, 280 p.name as processName 281 from process_measure 282 left join process_measure_filter as f on f.id= filter_id 283 left join process as p on p.id = f.ipid 284 where 285 filter_id > 0 286 and 287 f.name = 'mem.rss.anon' 288 group by 289 filter_id 290 order by 291 avgNum desc limit 10` 292 ); 293 294export const queryTraceMemoryUnAgg = (): Promise< 295 Array<{ 296 processName: string; 297 name: string; 298 value: string; 299 ts: string; 300 }> 301> => 302 query( 303 'queryTraceMemoryUnAgg', 304 ` 305 select 306 p.name as processName, 307 group_concat(filter.name) as name, 308 cast(group_concat(value) as varchar) as value, 309 cast(group_concat(ts) as varchar) as ts 310 from process_measure m 311 left join process_measure_filter as filter on filter.id= m.filter_id 312 left join process as p on p.id = filter.ipid 313 where 314 filter.name = 'mem.rss.anon' 315 or 316 filter.name = 'mem.rss.file' 317 or 318 filter.name = 'mem.swap' 319 or 320 filter.name = 'oom_score_adj' 321 group by 322 p.name,filter.ipid 323 order by 324 filter.ipid` 325 ); 326export const queryMemoryMaxData = (memoryName: string): Promise<Array<any>> => 327 query( 328 'queryMemoryMaxData', 329 `SELECT ifnull(max(m.value),0) as maxValue, 330 filter_id 331 from sys_mem_measure m 332 WHERE m.filter_id = 333 (SELECT id FROM sys_event_filter WHERE name = $memoryName) 334`, 335 { $memoryName: memoryName } 336 ); 337export const getTabPaneVirtualMemoryStatisticsData = (leftNs: number, rightNs: number): Promise<Array<any>> => 338 query( 339 'getTabPaneVirtualMemoryStatisticsData', 340 ` 341 select p.pid, 342 t.tid, 343 ifnull(p.name,'Process') as pname, 344 ifnull(t.name,'Thread') as tname, 345 f.type, 346 f.ipid, 347 f.itid, 348 count(f.ipid) as count, 349 sum(dur) as allDuration, 350 min(dur) as minDuration, 351 max(dur) as maxDuration, 352 avg(dur) as avgDuration 353 from paged_memory_sample as f left join process as p on f.ipid=p.ipid left join thread as t on f.itid=t.itid 354 where f.end_ts >= $leftNs 355 and f.end_ts <= $rightNs 356 group by f.type,f.ipid,f.itid 357 order by f.type; 358`, 359 { $leftNs: leftNs, $rightNs: rightNs } 360 ); 361export const getFileSysVirtualMemoryChartData = (): Promise<Array<any>> => 362 query( 363 'getFileSysVirtualMemoryChartData', 364 ` 365 select 366 (A.start_ts -B.start_ts) as startNS, 367 (A.end_ts - B.start_ts) as endNS, 368 dur as dur 369 from paged_memory_sample A,trace_range B 370 where startNS > 0 371 order by A.start_ts;`, 372 {}, 373 'exec' 374 ); 375export const hasFileSysData = (): Promise<Array<any>> => 376 query( 377 'hasFileSysData', 378 ` 379 select 380 fsCount, 381 vmCount, 382 ioCount from 383 (select count(1) as fsCount from file_system_sample s,trace_range t where (s.start_ts between t.start_ts and t.end_ts) or (s.end_ts between t.start_ts and t.end_ts) ) 384 ,(select count(1) as vmCount from paged_memory_sample s,trace_range t where (s.start_ts between t.start_ts and t.end_ts) or (s.end_ts between t.start_ts and t.end_ts) ) 385 ,(select count(1) as ioCount from bio_latency_sample s,trace_range t where (s.start_ts between t.start_ts and t.end_ts) or (s.end_ts between t.start_ts and t.end_ts) ); 386 `, 387 {} 388 ); 389export const queryEbpfSamplesCount = (startTime: number, endTime: number, ipids: number[]): Promise<Array<any>> => 390 query( 391 'queryEbpfSamplesCount', 392 ` 393 select 394fsCount, 395 vmCount from 396(select count(1) as fsCount from file_system_sample s,trace_range t where s.end_ts between $startTime + t.start_ts and $endTime + t.start_ts ${ 397 ipids.length > 0 ? `and s.ipid in (${ipids.join(',')})` : '' 398 }) 399,(select count(1) as vmCount from paged_memory_sample s,trace_range t where s.end_ts between $startTime + t.start_ts and $endTime + t.start_ts ${ 400 ipids.length > 0 ? `and s.ipid in (${ipids.join(',')})` : '' 401 }); 402`, 403 { $startTime: startTime, $endTime: endTime } 404 ); 405export const queryisExistsShmData = (iPid: number): Promise<Array<any>> => 406 query( 407 'queryisExistsShmData', 408 `SELECT EXISTS ( 409 SELECT 1 410 FROM memory_ashmem A,trace_range B 411 where A.ipid = ${iPid} 412 AND A.ts < B.end_ts 413 AND flag = 0 414 GROUP BY A.ts 415 ) AS data_exists`, 416 {} 417 ); 418export const queryVmTrackerShmSizeData = ( 419 leftNs: number, 420 rightNs: number, 421 iPid: number, 422 dur: number 423): Promise<Array<any>> => 424 query( 425 'queryVmTrackerShmSizeData', 426 `SELECT ( A.ts - B.start_ts ) AS startNS, 427 A.flag, 428 avg( A.size ) AS avg, 429 max( A.size ) AS max, 430 min( A.size ) AS min, 431 sum( A.size ) AS sum 432 FROM 433 memory_ashmem A, 434 trace_range B 435 WHERE 436 startNS <= ${rightNs} and (startNS+ ${dur}) >=${leftNs} 437 AND ipid = ${iPid}`, 438 {} 439 ); 440export const queryisExistsPurgeableData = (ipid: number, isPin?: boolean): Promise<Array<any>> => { 441 const pinSql = isPin ? ' AND a.ref_count > 0' : ''; 442 const names = isPin ? " ('mem.purg_pin')" : "('mem.purg_sum')"; 443 return query( 444 'queryisExistsPurgeableData', 445 `SELECT EXISTS ( 446 SELECT 1 447 FROM 448 (SELECT 1 449 FROM 450 process_measure m, 451 trace_range tr 452 LEFT JOIN process_measure_filter f ON f.id = m.filter_id 453 WHERE 454 m.ts < tr.end_ts 455 AND f.name = ${names} 456 AND f.ipid = ${ipid} 457 UNION ALL 458 SELECT 1 459 FROM 460 memory_ashmem a, 461 trace_range tr 462 WHERE 463 a.ts < tr.end_ts 464 AND a.flag = 0 465 AND a.ipid = ${ipid} 466 ${pinSql}) 467 ) AS data_exists` 468 ); 469};