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 getTabVirtualMemoryType = (startTime: number, endTime: number): Promise<Array<string>> => 133 query( 134 'getTabVirtualMemoryType', 135 ` 136 SELECT type from paged_memory_sample s,trace_range t 137 WHERE s.end_ts >= $startTime + t.start_ts 138 and s.start_ts <= $endTime + t.start_ts 139 group by type`, 140 { $startTime: startTime, $endTime: endTime } 141 ); 142 143export const queryNativeMemoryRealTime = (): //@ts-ignore 144Promise<Array<unknown>> => 145 query( 146 'queryNativeMemoryRealTime', 147 `select cs.ts,cs.clock_name from datasource_clockid dc 148 left join clock_snapshot cs on dc.clock_id = cs.clock_id 149 where data_source_name = 'memory-plugin' or data_source_name = 'nativehook' 150`, 151 {} 152 ); 153 154export const queryJsMemoryData = (): //@ts-ignore 155Promise<Array<unknown>> => query('queryJsMemoryData', 156 'SELECT 1 WHERE EXISTS(SELECT 1 FROM js_heap_nodes)'); 157 158export const queryVmTrackerShmData = ( 159 iPid: number 160): //@ts-ignore 161Promise<Array<unknown>> => 162 query( 163 'queryVmTrackerShmData', 164 `SELECT (A.ts - B.start_ts) as startNs, 165 sum(A.size) as value 166 FROM 167 memory_ashmem A,trace_range B 168 where 169 A.ipid = ${iPid} 170 AND A.ts < B.end_ts 171 and 172 flag = 0 173 GROUP by A.ts`, 174 {} 175 ); 176export const queryVmTrackerShmSelectionData = ( 177 startNs: number, 178 ipid: number 179): //@ts-ignore 180Promise<Array<unknown>> => 181 query( 182 'queryVmTrackerShmSelectionData', 183 `SELECT (A.ts - B.start_ts) as startNS,A.ipid, 184 A.fd,A.size,A.adj,A.ashmem_name_id as name, 185 A.ashmem_id as id,A.time,A.purged,A.ref_count as count, 186 A.flag 187 FROM memory_ashmem A,trace_range B 188 where startNS = ${startNs} and ipid = ${ipid};`, 189 {} 190 ); 191export const queryMemoryConfig = (): Promise<Array<MemoryConfig>> => 192 query( 193 'queryMemoryConfiig', 194 `SELECT ipid as iPid, process.pid AS pid, 195 process.name AS processName, 196 ( 197 SELECT value 198 FROM trace_config 199 WHERE trace_source = 'memory_config' AND key = 'sample_interval') AS interval 200 FROM 201 trace_config 202 LEFT JOIN process ON value = ipid 203 WHERE 204 trace_source = 'memory_config' 205 AND key = 'ipid' 206 ;` 207 ); 208 209// VM Tracker Purgeable泳道图 210export const queryPurgeableProcessData = ( 211 ipid: number, 212 isPin?: boolean 213): //@ts-ignore 214Promise<Array<unknown>> => { 215 const pinSql = isPin ? ' AND a.ref_count > 0' : ''; 216 const names = isPin ? " ('mem.purg_pin')" : "('mem.purg_sum')"; 217 return query( 218 'queryPurgeableProcessData', 219 `SELECT startNs, sum( value ) AS value 220 FROM 221 (SELECT 222 m.ts - tr.start_ts AS startNs, 223 sum(m.value) AS value 224 FROM 225 process_measure m, 226 trace_range tr 227 LEFT JOIN process_measure_filter f ON f.id = m.filter_id 228 WHERE 229 m.ts < tr.end_ts 230 AND f.name = ${names} 231 AND f.ipid = ${ipid} 232 GROUP BY m.ts 233 UNION ALL 234 SELECT 235 a.ts - tr.start_ts AS startNs, 236 sum( a.pss ) AS value 237 FROM 238 memory_ashmem a, 239 trace_range tr 240 WHERE 241 a.ts < tr.end_ts 242 AND a.flag = 0 243 AND a.ipid = ${ipid} 244 ${pinSql} 245 GROUP BY a.ts) 246 GROUP BY startNs` 247 ); 248}; 249 250export const queryVirtualMemory = (): //@ts-ignore 251Promise<Array<unknown>> => 252 query('queryVirtualMemory', 253 `select 254 id, 255 name 256 from sys_event_filter where type='sys_virtual_memory_filter'`); 257 258export const queryVirtualMemoryData = ( 259 filterId: number 260): //@ts-ignore 261Promise<Array<unknown>> => 262 query( 263 'queryVirtualMemoryData', 264 `select ts-${window.recordStartNS} as startTime,value,filter_id as filterID 265 from sys_mem_measure where filter_id=$filter_id`, 266 { $filter_id: filterId } 267 ); 268 269export const queryTraceMemory = (): Promise< 270 Array<{ 271 maxNum: string; 272 minNum: string; 273 avgNum: string; 274 name: string; 275 processName: string; 276 }> 277> => 278 query( 279 'queryTraceMemory', 280 ` 281 select 282 max(value) as maxNum, 283 min(value) as minNum, 284 avg(value) as avgNum, 285 filter.name as name, 286 p.name as processName 287 from process_measure 288 left join process_measure_filter as filter on filter.id= filter_id 289 left join process as p on p.id = filter.ipid 290 where 291 filter_id > 0 292 and 293 filter.name = 'mem.rss.anon' 294 group by 295 filter_id 296 order by 297 avgNum desc` 298 ); 299 300export const queryTraceMemoryTop = (): Promise< 301 Array<{ 302 maxNum: string; 303 minNum: string; 304 avgNum: string; 305 name: string; 306 processName: string; 307 }> 308> => 309 query( 310 'queryTraceMemoryTop', 311 ` 312 select 313 max(value) as maxNum, 314 min(value) as minNum, 315 avg(value) as avgNum, 316 f.name as name, 317 p.name as processName 318 from process_measure 319 left join process_measure_filter as f on f.id= filter_id 320 left join process as p on p.id = f.ipid 321 where 322 filter_id > 0 323 and 324 f.name = 'mem.rss.anon' 325 group by 326 filter_id 327 order by 328 avgNum desc limit 10` 329 ); 330 331export const queryTraceMemoryUnAgg = (): Promise< 332 Array<{ 333 processName: string; 334 name: string; 335 value: string; 336 ts: string; 337 }> 338> => 339 query( 340 'queryTraceMemoryUnAgg', 341 ` 342 select 343 p.name as processName, 344 group_concat(filter.name) as name, 345 cast(group_concat(value) as varchar) as value, 346 cast(group_concat(ts) as varchar) as ts 347 from process_measure m 348 left join process_measure_filter as filter on filter.id= m.filter_id 349 left join process as p on p.id = filter.ipid 350 where 351 filter.name = 'mem.rss.anon' 352 or 353 filter.name = 'mem.rss.file' 354 or 355 filter.name = 'mem.swap' 356 or 357 filter.name = 'oom_score_adj' 358 group by 359 p.name,filter.ipid 360 order by 361 filter.ipid` 362 ); 363 364export const queryMemoryMaxData = ( 365 memoryName: string 366): //@ts-ignore 367Promise<Array<unknown>> => 368 query( 369 'queryMemoryMaxData', 370 `SELECT ifnull(max(m.value),0) as maxValue, 371 filter_id 372 from sys_mem_measure m 373 WHERE m.filter_id = 374 (SELECT id FROM sys_event_filter WHERE name = $memoryName) 375`, 376 { $memoryName: memoryName } 377 ); 378 379export const getTabPaneVirtualMemoryStatisticsData = ( 380 leftNs: number, 381 rightNs: number 382): //@ts-ignore 383Promise<Array<unknown>> => 384 query( 385 'getTabPaneVirtualMemoryStatisticsData', 386 ` 387 select p.pid, 388 t.tid, 389 ifnull(p.name,'Process') as pname, 390 ifnull(t.name,'Thread') as tname, 391 f.type, 392 f.ipid, 393 f.itid, 394 count(f.ipid) as count, 395 sum(dur) as allDuration, 396 min(dur) as minDuration, 397 max(dur) as maxDuration, 398 avg(dur) as avgDuration 399 from paged_memory_sample as f 400 left join process as p on f.ipid=p.ipid left join thread as t on f.itid=t.itid 401 where f.end_ts >= $leftNs 402 and f.start_ts <= $rightNs 403 group by f.type,f.ipid,f.itid 404 order by f.type; 405`, 406 { $leftNs: leftNs, $rightNs: rightNs } 407 ); 408 409export const getFileSysVirtualMemoryChartData = (): //@ts-ignore 410Promise<Array<unknown>> => 411 query( 412 'getFileSysVirtualMemoryChartData', 413 ` 414 select 415 (A.start_ts -B.start_ts) as startNS, 416 (A.end_ts - B.start_ts) as endNS, 417 dur as dur 418 from paged_memory_sample A,trace_range B 419 where startNS > 0 420 order by A.start_ts;`, 421 {} 422 ); 423 424export const hasFileSysData = (): //@ts-ignore 425Promise<Array<unknown>> => 426 query( 427 'hasFileSysData', 428 ` 429 select 430 fsCount, 431 vmCount, 432 ioCount from 433 (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) ) 434 ,(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) ) 435 ,(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) ); 436 `, 437 {} 438 ); 439 440export const queryEbpfSamplesCount = ( 441 startTime: number, 442 endTime: number, 443 ipids: number[] 444): //@ts-ignore 445Promise<Array<unknown>> => 446 query( 447 'queryEbpfSamplesCount', 448 ` 449 select 450 fsCount, 451 vmCount 452 from 453 (select count(1) as fsCount from file_system_sample s,trace_range t 454 where s.end_ts between $startTime + t.start_ts and $endTime + t.start_ts ${ 455 ipids.length > 0 ? `and s.ipid in (${ipids.join(',')})` : '' 456}) 457,(select count(1) as vmCount from paged_memory_sample s,trace_range t 458where s.end_ts between $startTime + t.start_ts and $endTime + t.start_ts ${ 459 ipids.length > 0 ? `and s.ipid in (${ipids.join(',')})` : '' 460}); 461`, 462 { $startTime: startTime, $endTime: endTime } 463); 464 465export const queryisExistsShmData = ( 466 iPid: number 467): //@ts-ignore 468Promise<Array<unknown>> => 469 query( 470 'queryisExistsShmData', 471 `SELECT EXISTS ( 472 SELECT 1 473 FROM memory_ashmem A,trace_range B 474 where A.ipid = ${iPid} 475 AND A.ts < B.end_ts 476 AND flag = 0 477 GROUP BY A.ts 478 ) AS data_exists`, 479 {} 480 ); 481 482export const queryVmTrackerShmSizeData = ( 483 leftNs: number, 484 rightNs: number, 485 iPid: number, 486 dur: number 487): //@ts-ignore 488Promise<Array<unknown>> => 489 query( 490 'queryVmTrackerShmSizeData', 491 `SELECT ( A.ts - B.start_ts ) AS startNS, 492 A.flag, 493 avg( A.size ) AS avg, 494 max( A.size ) AS max, 495 min( A.size ) AS min, 496 sum( A.size ) AS sum 497 FROM 498 memory_ashmem A, 499 trace_range B 500 WHERE 501 startNS <= ${rightNs} and (startNS+ ${dur}) >=${leftNs} 502 AND ipid = ${iPid}`, 503 {} 504 ); 505 506export const queryisExistsPurgeableData = ( 507 ipid: number, 508 isPin?: boolean 509): //@ts-ignore 510Promise<Array<unknown>> => { 511 const pinSql = isPin ? ' AND a.ref_count > 0' : ''; 512 const names = isPin ? " ('mem.purg_pin')" : "('mem.purg_sum')"; 513 return query( 514 'queryisExistsPurgeableData', 515 `SELECT EXISTS ( 516 SELECT 1 517 FROM 518 (SELECT 1 519 FROM 520 process_measure m, 521 trace_range tr 522 LEFT JOIN process_measure_filter f ON f.id = m.filter_id 523 WHERE 524 m.ts < tr.end_ts 525 AND f.name = ${names} 526 AND f.ipid = ${ipid} 527 UNION ALL 528 SELECT 1 529 FROM 530 memory_ashmem a, 531 trace_range tr 532 WHERE 533 a.ts < tr.end_ts 534 AND a.flag = 0 535 AND a.ipid = ${ipid} 536 ${pinSql}) 537 ) AS data_exists` 538 ); 539}; 540