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 50//VM Tracker SkiaGpuMemory 框选 51export const getTabGpuMemoryData = ( 52 leftNs: number, 53 rightNs: number, 54 processId: number, 55 dur: number 56): Promise<Array<GpuMemory>> => 57 query<GpuMemory>( 58 'getTabGpuMemoryData', 59 `SELECT 60 (S.ts-TR.start_ts) as startNs, 61 gpu_name_id as gpuNameId, 62 T.tid as threadId, 63 T.name as threadName, 64 MAX(S.used_gpu_size) as maxSize, 65 MIN(S.used_gpu_size) as minSize, 66 Avg(S.used_gpu_size) as avgSize 67 from trace_range as TR,memory_process_gpu as S 68 left join thread as T on T.itid=S.itid 69 where 70 $leftNS <= startNs + ${dur} 71 and 72 $rightNS >= startNs 73 and 74 $pid = S.ipid 75 group by gpu_name_id,threadId 76 `, 77 { $leftNS: leftNs, $rightNS: rightNs, $pid: processId } 78 ); 79 80//VM Tracker SkiaGpuMemory 点选 81export const getTabGpuMemoryVMTrackerClickData = (startNs: number, processId: number): 82 Promise<Array<GpuMemory>> => 83 query<GpuMemory>( 84 'getTabGpuMemoryVMTrackerClickData', 85 `SELECT 86 (S.ts-TR.start_ts) as startNs, 87 S.used_gpu_size as size, 88 T.tid as threadId, 89 T.name as threadName, 90 A.data as gpuName 91 from trace_range as TR,memory_process_gpu as S 92 left join thread as T on T.itid=S.itid 93 left join data_dict as A on A.id=S.gpu_name_id 94 WHERE 95 startNs = ${startNs} 96 AND 97 $pid = S.ipid 98 `, 99 { $startNs: startNs, $pid: processId } 100 ); 101 102//VM Tracker Gpu Memory 点选比较 103export const getTabGpuMemoryVmTrackerComparisonData = ( 104 startNs: number, 105 processId: number 106): Promise<Array<GpuMemoryComparison>> => 107 query<GpuMemoryComparison>( 108 'getTabGpuMemoryVmTrackerComparisonData', 109 `SELECT 110 (S.ts-TR.start_ts) as startNs, 111 sum(S.used_gpu_size) as value, 112 T.tid as threadId, 113 T.name as threadName, 114 S.gpu_name_id as gpuNameId 115 from trace_range as TR,memory_process_gpu as S 116 left join thread as T on T.itid=S.itid 117 WHERE 118 startNs = ${startNs} 119 AND 120 $pid = S.ipid 121 `, 122 { $startNs: startNs, $pid: processId } 123 ); 124 125export const queryMemFilterIdMaxValue = (): Promise<Array<{ filterId: number; maxValue: number }>> => { 126 return query( 127 'queryMemFilterIdMaxValue', 128 `select filter_id as filterId,max(value) maxValue from process_measure group by filter_id;` 129 ); 130}; 131 132export const queryMemFilterIdMinValue = (): Promise<Array<{ filterId: number; minValue: number }>> => { 133 return query( 134 'queryMemFilterIdMinValue', 135 `select filter_id as filterId,min(value) minValue from process_measure group by filter_id;` 136 ); 137}; 138 139export const getTabVirtualMemoryType = (startTime: number, endTime: number): Promise<Array<string>> => 140 query( 141 'getTabVirtualMemoryType', 142 ` 143 SELECT type from paged_memory_sample s,trace_range t 144 WHERE s.end_ts >= $startTime + t.start_ts 145 and s.start_ts <= $endTime + t.start_ts 146 group by type`, 147 { $startTime: startTime, $endTime: endTime } 148 ); 149 150export const queryNativeMemoryRealTime = (): //@ts-ignore 151 Promise<Array<unknown>> => 152 query( 153 'queryNativeMemoryRealTime', 154 `select cs.ts,cs.clock_name from datasource_clockid dc 155 left join clock_snapshot cs on dc.clock_id = cs.clock_id 156 where data_source_name = 'memory-plugin' or data_source_name = 'nativehook' 157`, 158 {} 159 ); 160 161export const queryJsMemoryData = (): //@ts-ignore 162 Promise<Array<unknown>> => query('queryJsMemoryData', 163 'SELECT 1 WHERE EXISTS(SELECT 1 FROM js_heap_nodes)'); 164 165export const queryVmTrackerShmData = ( 166 iPid: number 167): //@ts-ignore 168 Promise<Array<unknown>> => 169 query( 170 'queryVmTrackerShmData', 171 `SELECT (A.ts - B.start_ts) as startNs, 172 sum(A.size) as value 173 FROM 174 memory_ashmem A,trace_range B 175 where 176 A.ipid = ${iPid} 177 AND A.ts < B.end_ts 178 and 179 flag = 0 180 GROUP by A.ts`, 181 {} 182 ); 183export const queryVmTrackerShmSelectionData = ( 184 startNs: number, 185 ipid: number 186): //@ts-ignore 187 Promise<Array<unknown>> => 188 query( 189 'queryVmTrackerShmSelectionData', 190 `SELECT (A.ts - B.start_ts) as startNS,A.ipid, 191 A.fd,A.size,A.adj,A.ashmem_name_id as name, 192 A.ashmem_id as id,A.time,A.purged,A.ref_count as count, 193 A.flag 194 FROM memory_ashmem A,trace_range B 195 where startNS = ${startNs} and ipid = ${ipid};`, 196 {} 197 ); 198export const queryMemoryConfig = async (): Promise<Array<MemoryConfig>> => { 199 let keyList = await query( 200 'queryIsColorIndex', 201 `select 202 key 203 from 204 trace_config`, 205 {}, 206 ); 207 //@ts-ignore 208 let keySql = keyList && keyList.length > 0 && keyList.some(entry => entry.key === 'ipid') ? "AND key = 'ipid'" : ''; 209 return query( 210 'queryMemoryConfiig', 211 `SELECT ipid as iPid, process.pid AS pid, 212 process.name AS processName, 213 ( 214 SELECT value 215 FROM trace_config 216 WHERE trace_source = 'memory_config' AND key = 'sample_interval') AS interval 217 FROM 218 trace_config 219 LEFT JOIN process ON value = ipid 220 WHERE 221 trace_source = 'memory_config' 222 ${keySql} 223 ;` 224 ); 225}; 226 227// VM Tracker Purgeable泳道图 228export const queryPurgeableProcessData = ( 229 ipid: number, 230 isPin?: boolean 231): //@ts-ignore 232 Promise<Array<unknown>> => { 233 const pinSql = isPin ? ' AND a.ref_count > 0' : ''; 234 const names = isPin ? " ('mem.purg_pin')" : "('mem.purg_sum')"; 235 return query( 236 'queryPurgeableProcessData', 237 `SELECT startNs, sum( value ) AS value 238 FROM 239 (SELECT 240 m.ts - tr.start_ts AS startNs, 241 sum(m.value) AS value 242 FROM 243 process_measure m, 244 trace_range tr 245 LEFT JOIN process_measure_filter f ON f.id = m.filter_id 246 WHERE 247 m.ts < tr.end_ts 248 AND f.name = ${names} 249 AND f.ipid = ${ipid} 250 GROUP BY m.ts 251 UNION ALL 252 SELECT 253 a.ts - tr.start_ts AS startNs, 254 sum( a.pss ) AS value 255 FROM 256 memory_ashmem a, 257 trace_range tr 258 WHERE 259 a.ts < tr.end_ts 260 AND a.flag = 0 261 AND a.ipid = ${ipid} 262 ${pinSql} 263 GROUP BY a.ts) 264 GROUP BY startNs` 265 ); 266}; 267 268export const queryVirtualMemory = (): //@ts-ignore 269 Promise<Array<unknown>> => 270 query('queryVirtualMemory', 271 `select 272 id, 273 name 274 from sys_event_filter where type='sys_virtual_memory_filter'`); 275 276export const queryVirtualMemoryData = ( 277 filterId: number 278): //@ts-ignore 279 Promise<Array<unknown>> => 280 query( 281 'queryVirtualMemoryData', 282 `select ts-${window.recordStartNS} as startTime,value,filter_id as filterID 283 from sys_mem_measure where filter_id=$filter_id`, 284 { $filter_id: filterId } 285 ); 286 287export const queryTraceMemory = (): Promise< 288 Array<{ 289 maxNum: string; 290 minNum: string; 291 avgNum: string; 292 name: string; 293 processName: string; 294 }> 295> => 296 query( 297 'queryTraceMemory', 298 ` 299 select 300 max(value) as maxNum, 301 min(value) as minNum, 302 avg(value) as avgNum, 303 filter.name as name, 304 p.name as processName 305 from process_measure 306 left join process_measure_filter as filter on filter.id= filter_id 307 left join process as p on p.id = filter.ipid 308 where 309 filter_id > 0 310 and 311 filter.name = 'mem.rss.anon' 312 group by 313 filter_id 314 order by 315 avgNum desc` 316 ); 317 318export const queryTraceMemoryTop = (): Promise< 319 Array<{ 320 maxNum: string; 321 minNum: string; 322 avgNum: string; 323 name: string; 324 processName: string; 325 }> 326> => 327 query( 328 'queryTraceMemoryTop', 329 ` 330 select 331 max(value) as maxNum, 332 min(value) as minNum, 333 avg(value) as avgNum, 334 f.name as name, 335 p.name as processName 336 from process_measure 337 left join process_measure_filter as f on f.id= filter_id 338 left join process as p on p.id = f.ipid 339 where 340 filter_id > 0 341 and 342 f.name = 'mem.rss.anon' 343 group by 344 filter_id 345 order by 346 avgNum desc limit 10` 347 ); 348 349export const queryTraceMemoryUnAgg = (): Promise< 350 Array<{ 351 processName: string; 352 name: string; 353 value: string; 354 ts: string; 355 }> 356> => 357 query( 358 'queryTraceMemoryUnAgg', 359 ` 360 select 361 p.name as processName, 362 group_concat(filter.name) as name, 363 cast(group_concat(value) as varchar) as value, 364 cast(group_concat(ts) as varchar) as ts 365 from process_measure m 366 left join process_measure_filter as filter on filter.id= m.filter_id 367 left join process as p on p.id = filter.ipid 368 where 369 filter.name = 'mem.rss.anon' 370 or 371 filter.name = 'mem.rss.file' 372 or 373 filter.name = 'mem.swap' 374 or 375 filter.name = 'oom_score_adj' 376 group by 377 p.name,filter.ipid 378 order by 379 filter.ipid` 380 ); 381 382export const queryMemoryMaxData = ( 383 memoryName: string 384): //@ts-ignore 385 Promise<Array<unknown>> => 386 query( 387 'queryMemoryMaxData', 388 `SELECT ifnull(max(m.value),0) as maxValue, 389 filter_id 390 from sys_mem_measure m 391 WHERE m.filter_id = 392 (SELECT id FROM sys_event_filter WHERE name = $memoryName) 393`, 394 { $memoryName: memoryName } 395 ); 396 397export const getTabPaneVirtualMemoryStatisticsData = ( 398 leftNs: number, 399 rightNs: number 400): //@ts-ignore 401 Promise<Array<unknown>> => 402 query( 403 'getTabPaneVirtualMemoryStatisticsData', 404 ` 405 select p.pid, 406 t.tid, 407 ifnull(p.name,'Process') as pname, 408 ifnull(t.name,'Thread') as tname, 409 f.type, 410 f.ipid, 411 f.itid, 412 count(f.ipid) as count, 413 sum(dur) as allDuration, 414 min(dur) as minDuration, 415 max(dur) as maxDuration, 416 avg(dur) as avgDuration 417 from paged_memory_sample as f 418 left join process as p on f.ipid=p.ipid left join thread as t on f.itid=t.itid 419 where f.end_ts >= $leftNs 420 and f.start_ts <= $rightNs 421 group by f.type,f.ipid,f.itid 422 order by f.type; 423`, 424 { $leftNs: leftNs, $rightNs: rightNs } 425 ); 426 427export const getFileSysVirtualMemoryChartData = (): //@ts-ignore 428 Promise<Array<unknown>> => 429 query( 430 'getFileSysVirtualMemoryChartData', 431 ` 432 select 433 (A.start_ts -B.start_ts) as startNS, 434 (A.end_ts - B.start_ts) as endNS, 435 dur as dur 436 from paged_memory_sample A,trace_range B 437 where startNS > 0 438 order by A.start_ts;`, 439 {} 440 ); 441 442export const hasFileSysData = (): //@ts-ignore 443 Promise<Array<unknown>> => 444 query( 445 'hasFileSysData', 446 ` 447 select 448 fsCount, 449 vmCount, 450 ioCount from 451 (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) ) 452 ,(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) ) 453 ,(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) ); 454 `, 455 {} 456 ); 457 458export const queryEbpfSamplesCount = ( 459 startTime: number, 460 endTime: number, 461 ipids: number[] 462): //@ts-ignore 463 Promise<Array<unknown>> => 464 query( 465 'queryEbpfSamplesCount', 466 ` 467 select 468 fsCount, 469 vmCount 470 from 471 (select count(1) as fsCount from file_system_sample s,trace_range t 472 where s.end_ts between $startTime + t.start_ts and $endTime + t.start_ts ${ipids.length > 0 ? `and s.ipid in (${ipids.join(',')})` : '' 473 }) 474,(select count(1) as vmCount from paged_memory_sample s,trace_range t 475where s.end_ts between $startTime + t.start_ts and $endTime + t.start_ts ${ipids.length > 0 ? `and s.ipid in (${ipids.join(',')})` : '' 476 }); 477`, 478 { $startTime: startTime, $endTime: endTime } 479 ); 480 481// 查询是否使用插件插件 482export const queryPlugins = ( 483 table: string 484): Promise<Array<unknown>> => 485 query( 486 'queryPlugins', 487 `select 1 from ${table} limit 1`, 488 {} 489 ); 490 491export const queryisExistsShmData = ( 492 iPid: number 493): //@ts-ignore 494 Promise<Array<unknown>> => 495 query( 496 'queryisExistsShmData', 497 `SELECT EXISTS ( 498 SELECT 1 499 FROM memory_ashmem A,trace_range B 500 where A.ipid = ${iPid} 501 AND A.ts < B.end_ts 502 AND flag = 0 503 GROUP BY A.ts 504 ) AS data_exists`, 505 {} 506 ); 507 508export const queryVmTrackerShmSizeData = ( 509 leftNs: number, 510 rightNs: number, 511 iPid: number, 512 dur: number 513): //@ts-ignore 514 Promise<Array<unknown>> => 515 query( 516 'queryVmTrackerShmSizeData', 517 `SELECT ( A.ts - B.start_ts ) AS startNS, 518 A.flag, 519 avg( A.size ) AS avg, 520 max( A.size ) AS max, 521 min( A.size ) AS min, 522 sum( A.size ) AS sum 523 FROM 524 memory_ashmem A, 525 trace_range B 526 WHERE 527 startNS <= ${rightNs} and (startNS+ ${dur}) >=${leftNs} 528 AND ipid = ${iPid}`, 529 {} 530 ); 531 532export const queryisExistsPurgeableData = ( 533 ipid: number, 534 isPin?: boolean 535): //@ts-ignore 536 Promise<Array<unknown>> => { 537 const pinSql = isPin ? ' AND a.ref_count > 0' : ''; 538 const names = isPin ? " ('mem.purg_pin')" : "('mem.purg_sum')"; 539 return query( 540 'queryisExistsPurgeableData', 541 `SELECT EXISTS ( 542 SELECT 1 543 FROM 544 (SELECT 1 545 FROM 546 process_measure m, 547 trace_range tr 548 LEFT JOIN process_measure_filter f ON f.id = m.filter_id 549 WHERE 550 m.ts < tr.end_ts 551 AND f.name = ${names} 552 AND f.ipid = ${ipid} 553 UNION ALL 554 SELECT 1 555 FROM 556 memory_ashmem a, 557 trace_range tr 558 WHERE 559 a.ts < tr.end_ts 560 AND a.flag = 0 561 AND a.ipid = ${ipid} 562 ${pinSql}) 563 ) AS data_exists` 564 ); 565}; 566