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 { Args } from './CommonArgs'; 16import { TraficEnum } from './utils/QueryEnum'; 17 18export const chartSMapsDataSqlMem = (args: Args): string => { 19 return `SELECT (A.timestamp - ${args.recordStartNS}) as startNs, 20 sum(${args.name}) * 1024 as value 21 FROM smaps A 22 WHERE A.timestamp < ${args.recordEndNS} 23 GROUP by A.timestamp`; 24}; 25export const chartDmaDataSqlMem = (args: Args): string => { 26 return `SELECT (A.ts - ${args.recordStartNS}) as startNs, 27 sum(A.size) as value 28 FROM memory_dma A 29 WHERE A.flag = 0 30 AND ${args.ipid} = A.ipid 31 AND A.ts < ${args.recordEndNS} 32 GROUP by A.ts`; 33}; 34export const chartGpuMemoryDataSqlMem = (args: Args): string => { 35 return ` SELECT (A.ts - ${args.recordStartNS}) as startNs, 36 sum(A.used_gpu_size) as value 37 FROM memory_process_gpu A 38 WHERE ${args.ipid} = A.ipid 39 AND A.ts < ${args.recordEndNS} 40 GROUP by A.ts`; 41}; 42export const chartGpuResourceDataSqlMem = (args: Args): string => { 43 return `SELECT subquery1.startNs, 44 (IFNULL(subquery1.totalSize, 0) - IFNULL(subquery2.size, 0)) AS value 45 FROM 46 (SELECT (ts - ${args.recordStartNS}) AS startNs,SUM(total_size) AS totalSize 47 FROM memory_profile 48 WHERE ts between ${args.recordStartNS} and ${args.recordEndNS} 49 GROUP BY ts) AS subquery1 50 LEFT JOIN 51 (SELECT (ts - ${args.recordStartNS}) AS startNs, SUM(size) AS size 52 FROM memory_window_gpu 53 WHERE ts between ${args.recordStartNS} and ${args.recordEndNS} 54 AND category_name_id = ${args.scratchId} 55 GROUP BY ts) AS subquery2 56 ON subquery1.startNs = subquery2.startNs`; 57}; 58export const chartGpuDataSqlMem = (args: Args): string => { 59 return `select (ts - ${args.recordStartNS}) startNs, 60 sum(value) * 1024 value 61 from process_measure 62 where filter_id = ( 63 select id 64 from process_measure_filter 65 where name = ${args.name} and ipid = ${args.ipid} 66 ) 67 and ts between ${args.recordStartNS} AND ${args.recordEndNS} 68 group by ts;`; 69}; 70export const chartGpuTotalDataSqlMem = (args: Args): string => { 71 let moduleCondition = args.moduleId === null ? '' : `and module_name_id = ${args.moduleId}`; 72 return `select (ts - ${args.recordStartNS}) startNs, 73 sum(size) value 74 from memory_window_gpu 75 where window_name_id = 0 ${moduleCondition} 76 and ts < ${args.recordEndNS} 77 group by ts;`; 78}; 79export const chartGpuWindowDataSqlMem = (args: Args): string => { 80 let moduleCondition = args.moduleId === null ? '' : `and module_name_id = ${args.moduleId}`; 81 return `select (ts - ${args.recordStartNS}) startNs, 82 sum(size) value 83 from memory_window_gpu 84 where window_name_id = ${args.windowId} ${moduleCondition} 85 and ts < ${args.recordEndNS} 86 group by ts`; 87}; 88export const chartShmDataSqlMem = (args: Args): string => { 89 return `SELECT (A.ts - ${args.recordStartNS}) as startNs, 90 sum(A.size) as value 91 FROM memory_ashmem A 92 where A.ipid = ${args.ipid} 93 AND A.ts < ${args.recordEndNS} 94 AND flag = 0 95 GROUP by A.ts`; 96}; 97export const chartPurgeableDataSqlMem = (args: Args): string => { 98 const pinSql = args.isPin ? ' AND a.ref_count > 0' : ''; 99 const names = args.isPin ? " ('mem.purg_pin')" : "('mem.purg_sum')"; 100 return `SELECT startNs, 101 sum( value ) AS value 102 FROM (SELECT m.ts - ${args.recordStartNS} AS startNs, 103 sum(m.value) AS value 104 FROM process_measure m 105 LEFT JOIN process_measure_filter f ON f.id = m.filter_id 106 WHERE m.ts < ${args.recordEndNS} 107 AND f.name = ${names} 108 AND f.ipid = ${args.ipid} 109 GROUP BY m.ts 110 UNION ALL 111 SELECT a.ts - ${args.recordStartNS} AS startNs, 112 sum( a.pss ) AS value 113 FROM memory_ashmem a 114 WHERE a.ts < ${args.recordEndNS} 115 AND a.flag = 0 116 AND a.ipid = ${args.ipid} 117 ${pinSql} 118 GROUP BY a.ts) 119 GROUP BY startNs`; 120}; 121export const abilityPurgeablelDataSqlMem = (args: Args): string => { 122 const pinCondition = args.isPin ? ' AND a.ref_count > 0' : ''; 123 const names = args.isPin ? " ('sys.mem.pined.purg')" : "('sys.mem.active.purg','sys.mem.inactive.purg')"; 124 return `SELECT startNs, 125 sum( value ) AS value 126 FROM (SELECT m.ts - ${args.recordStartNS} AS startNs, 127 sum( m.value ) AS value 128 FROM sys_mem_measure m 129 LEFT JOIN sys_event_filter f ON f.id = m.filter_id 130 WHERE m.ts < ${args.recordEndNS} 131 AND f.name IN ${names} 132 GROUP BY m.ts 133 UNION ALL 134 SELECT a.ts - ${args.recordStartNS} AS startNs, 135 sum( a.size ) AS value 136 FROM memory_ashmem a 137 WHERE a.ts < ${args.recordEndNS} 138 AND a.flag = 0 139 ${pinCondition} 140 GROUP BY a.ts ) 141 GROUP BY startNs`; 142}; 143export const abilityDmaDataSqlMem = (args: Args): string => { 144 return `SELECT (A.ts - ${args.recordStartNS}) as startNs, 145 sum(A.size) as value, 146 E.data as expTaskComm, 147 A.flag as flag 148 FROM memory_dma A 149 left join data_dict as E on E.id=A.exp_task_comm_id 150 WHERE A.flag = 0 151 AND A.ts < ${args.recordEndNS} 152 GROUP by A.ts;`; 153}; 154export const abilityGpuMemoryDataSqlMem = (args: Args): string => { 155 return `SELECT 156 (A.ts - ${args.recordStartNS}) as startNs, 157 sum(A.used_gpu_size) as value 158 FROM memory_process_gpu A 159 WHERE A.ts < ${args.recordEndNS} 160 GROUP by A.ts;`; 161}; 162// @ts-ignore 163let sMapsList: Array<unknown> = []; // @ts-ignore 164let dmaList: Array<unknown> = []; // @ts-ignore 165let gpuMemoryList: Array<unknown> = []; // @ts-ignore 166let gpuList: Array<unknown> = []; // @ts-ignore 167let gpuResourceList: Array<unknown> = []; // @ts-ignore 168let gpuTotalList: Array<unknown> = []; // @ts-ignore 169let gpuWindowList: Array<unknown> = []; // @ts-ignore 170let shmList: Array<unknown> = []; // @ts-ignore 171let purgeableList: Array<unknown> = []; // @ts-ignore 172let sMapsMap = new Map<string, Array<unknown>>(); // @ts-ignore 173let purgeableMap = new Map<string, Array<unknown>>(); // @ts-ignore 174let gpuMap = new Map<string, Array<unknown>>(); // @ts-ignore 175let abilityPurgeableMap: Map<string, Array<unknown>> = new Map(); // @ts-ignore 176let abilityPurgeableList: Array<unknown> = []; // @ts-ignore 177let abilityDmaList: Array<unknown> = []; // @ts-ignore 178let abilityGpuMemoryList: Array<unknown> = []; 179 180export function resetVmTracker(): void { 181 sMapsList = []; 182 dmaList = []; 183 gpuMemoryList = []; 184 gpuList = []; 185 gpuResourceList = []; 186 gpuTotalList = []; 187 gpuWindowList = []; 188 shmList = []; 189 purgeableList = []; 190 sMapsMap.clear(); 191 purgeableMap.clear(); 192 gpuMap.clear(); 193} 194 195export function resetAbility(): void { 196 abilityPurgeableList = []; 197 abilityDmaList = []; 198 abilityGpuMemoryList = []; 199 abilityPurgeableMap.clear(); 200} 201 202export function sMapsDataReceiver(data: unknown, proc: Function): void { 203 // @ts-ignore 204 if (!sMapsMap.has(data.params.name)) { 205 // @ts-ignore 206 sMapsList = proc(chartSMapsDataSqlMem(data.params)); // @ts-ignore 207 sMapsMap.set(data.params.name, sMapsList); 208 } // @ts-ignore 209 let list = sMapsMap.get(data.params.name) || []; 210 arrayBufferHandler(data, list, true); 211} 212 213export function dmaDataReceiver(data: unknown, proc: Function): void { 214 if (dmaList.length === 0) { 215 // @ts-ignore 216 dmaList = proc(chartDmaDataSqlMem(data.params)); 217 } 218 arrayBufferHandler(data, dmaList, true); 219} 220 221export function gpuMemoryDataReceiver(data: unknown, proc: Function): void { 222 if (gpuMemoryList.length === 0) { 223 // @ts-ignore 224 gpuMemoryList = proc(chartGpuMemoryDataSqlMem(data.params)); 225 } 226 arrayBufferHandler(data, gpuMemoryList, true); 227} 228 229export function gpuDataReceiver(data: unknown, proc: Function): void { 230 // @ts-ignore 231 if (!gpuMap.has(data.params.name)) { 232 // @ts-ignore 233 gpuList = proc(chartGpuDataSqlMem(data.params)); // @ts-ignore 234 gpuMap.set(data.params.name, gpuList); 235 } // @ts-ignore 236 let list = gpuMap.get(data.params.name) || []; 237 arrayBufferHandler(data, list, true); 238} 239 240export function gpuResourceDataReceiver(data: unknown, proc: Function): void { 241 if (gpuResourceList.length === 0) { 242 // @ts-ignore 243 gpuResourceList = proc(chartGpuResourceDataSqlMem(data.params)); 244 } 245 arrayBufferHandler(data, gpuResourceList, true); 246} 247 248export function gpuTotalDataReceiver(data: unknown, proc: Function): void { 249 // @ts-ignore 250 if (gpuTotalList.length === 0 || data.params.moduleId) { 251 // @ts-ignore 252 gpuTotalList = proc(chartGpuTotalDataSqlMem(data.params)); 253 } 254 arrayBufferHandler(data, gpuTotalList, true); 255} 256 257export function gpuWindowDataReceiver(data: unknown, proc: Function): void { 258 // @ts-ignore 259 if (gpuWindowList.length === 0 || data.params.moduleId) { 260 // @ts-ignore 261 gpuWindowList = proc(chartGpuWindowDataSqlMem(data.params)); 262 } 263 arrayBufferHandler(data, gpuWindowList, true); 264} 265 266export function shmDataReceiver(data: unknown, proc: Function): void { 267 if (shmList.length === 0) { 268 // @ts-ignore 269 shmList = proc(chartShmDataSqlMem(data.params)); 270 } 271 arrayBufferHandler(data, shmList, true); 272} 273 274export function purgeableDataReceiver(data: unknown, proc: Function): void { 275 let key: string = ''; 276 if ( 277 // @ts-ignore 278 data.params.isPin 279 ) { 280 key = 'pin'; 281 } else { 282 key = 'total'; 283 } 284 if ( 285 !purgeableMap.has( 286 // @ts-ignore 287 data.params.isPin 288 ) 289 ) { 290 purgeableList = proc( 291 chartPurgeableDataSqlMem( 292 // @ts-ignore 293 data.params 294 ) 295 ); 296 purgeableMap.set(key, purgeableList); 297 } 298 let list = purgeableMap.get(key) || []; 299 arrayBufferHandler(data, list, true); 300} 301 302export function abilityPurgeableDataReceiver(data: unknown, proc: Function): void { 303 let key = ''; 304 if ( 305 // @ts-ignore 306 data.params.isPin 307 ) { 308 key = 'pin'; 309 } else { 310 key = 'total'; 311 } 312 if (!abilityPurgeableMap.has(key)) { 313 abilityPurgeableList = proc( 314 abilityPurgeablelDataSqlMem( 315 // @ts-ignore 316 data.params 317 ) 318 ); 319 abilityPurgeableMap.set(key, abilityPurgeableList); 320 } 321 let abilityList = abilityPurgeableMap.get(key) || []; 322 arrayBufferHandler(data, abilityList, true); 323} 324 325export function abilityDmaDataReceiver(data: unknown, proc: Function): void { 326 if (abilityDmaList.length === 0) { 327 // @ts-ignore 328 abilityDmaList = proc(abilityDmaDataSqlMem(data.params)); 329 } 330 arrayBufferHandler(data, abilityDmaList, true); 331} 332 333export function abilityGpuMemoryDataReceiver(data: unknown, proc: Function): void { 334 if (abilityGpuMemoryList.length === 0) { 335 // @ts-ignore 336 abilityGpuMemoryList = proc(abilityGpuMemoryDataSqlMem(data.params)); 337 } 338 arrayBufferHandler(data, abilityGpuMemoryList, true); 339} 340 341function arrayBufferHandler(data: unknown, res: unknown[], transfer: boolean): void { 342 // @ts-ignore 343 let startNs = new Float64Array(transfer ? res.length : data.params.sharedArrayBuffers.startNs); // @ts-ignore 344 let value = new Int32Array(transfer ? res.length : data.params.sharedArrayBuffers.value); 345 res.forEach((it, i) => { 346 // @ts-ignore 347 data.params.trafic === TraficEnum.ProtoBuffer && (it = it.trackerData); // @ts-ignore 348 startNs[i] = it.startNs; // @ts-ignore 349 value[i] = it.value; 350 }); 351 (self as unknown as Worker).postMessage( 352 { 353 // @ts-ignore 354 id: data.id, // @ts-ignore 355 action: data.action, 356 results: transfer 357 ? { 358 startNs: startNs.buffer, 359 value: value.buffer, 360 } 361 : {}, 362 len: res.length, 363 transfer: transfer, 364 }, 365 transfer ? [startNs.buffer, value.buffer] : [] 366 ); 367} 368