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 { FuncStruct } from '../ui-worker/ProcedureWorkerFunc'; 17import { SearchFuncBean } from '../../bean/SearchFuncBean'; 18import { SelectionData } from '../../bean/BoxSelection'; 19import { HeapTraceFunctionInfo } from '../../../js-heap/model/DatabaseStruct'; 20 21export const queryAllFuncNames = (): Promise<Array<any>> => { 22 return query( 23 'queryAllFuncNames', 24 ` 25 select id,name from callstack;` 26 ); 27}; 28export const queryProcessAsyncFunc = (_funName?: string): Promise<Array<any>> => 29 query( 30 'queryProcessAsyncFunc', 31 ` 32select tid, 33 P.pid, 34 A.name as threadName, 35 is_main_thread, 36 c.callid as track_id, 37 c.ts-D.start_ts as startTs, 38 c.dur, 39 c.name as funName, 40 c.parent_id, 41 c.id, 42 c.cookie, 43 c.depth, 44 c.argsetid 45from thread A,trace_range D 46left join callstack C on A.id = C.callid 47left join process P on P.id = A.ipid 48where startTs not null and cookie not null ${_funName ? 'funName=$funName' : ''};`, 49 { 50 funName: _funName, 51 } 52 ); 53 54export const getFunDataByTid = (tid: number, ipid: number): Promise<Array<FuncStruct>> => 55 query( 56 'getFunDataByTid', 57 ` 58 select 59 c.ts-D.start_ts as startTs, 60 c.dur, 61 c.name as funName, 62 c.argsetid, 63 c.depth, 64 c.id as id, 65 A.itid as itid, 66 A.ipid as ipid 67from thread A,trace_range D 68left join callstack C on A.id = C.callid 69where startTs not null and c.cookie is null and tid = $tid and A.ipid = $ipid`, 70 { $tid: tid, $ipid: ipid } 71 ); 72export const getMaxDepthByTid = (): Promise<Array<any>> => 73 query( 74 'getMaxDepthByTid', 75 ` 76 select 77tid, 78ipid, 79 MAX(c.depth + 1) as maxDepth 80from thread A 81left join callstack C on A.id = C.callid 82where c.ts not null and c.cookie is null group by tid,ipid`, 83 {} 84 ); 85export const querySearchFuncData = ( 86 funcName: string, 87 tIds: number, 88 leftNS: number, 89 rightNS: number 90): Promise<Array<SearchFuncBean>> => 91 query( 92 'querySearchFuncData', 93 ` 94 select 95 c.ts - r.start_ts as startTime, 96 c.dur 97 from 98 callstack c 99 left join 100 thread t 101 on 102 c.callid = t.id 103 left join 104 process p 105 on 106 t.ipid = p.id 107 left join 108 trace_range r 109 where 110 c.name like '${funcName}%' 111 and 112 t.tid = ${tIds} 113 and 114 not ((startTime < ${leftNS}) or (startTime > ${rightNS})); 115 ` 116 ); 117export const querySearchRowFuncData = ( 118 funcName: string, 119 tIds: number, 120 leftNS: number, 121 rightNS: number 122): Promise<Array<SearchFuncBean>> => 123 query( 124 'querySearchRowFuncData', 125 ` 126 select 127 c.name as funName, 128 c.ts - r.start_ts as startTime, 129 t.tid, 130 t.name as threadName, 131 'func' as type 132 from 133 callstack c 134 left join 135 thread t 136 on 137 c.callid = t.id 138 left join 139 process p 140 on 141 t.ipid = p.id 142 left join 143 trace_range r 144 where 145 c.name like '${funcName}' 146 and 147 t.tid = ${tIds} 148 and 149 not ((startTime < ${leftNS}) or (startTime > ${rightNS})); 150 `, 151 { $search: funcName } 152 ); 153export const getTabSlicesAsyncFunc = ( 154 asyncNames: Array<string>, 155 asyncPid: Array<number>, 156 leftNS: number, 157 rightNS: number 158): Promise<Array<any>> => 159 query<SelectionData>( 160 'getTabSlicesAsyncFunc', 161 ` 162 select 163 c.name as name, 164 sum(c.dur) as wallDuration, 165 avg(c.dur) as avgDuration, 166 count(c.name) as occurrences 167 from 168 thread A, trace_range D 169 left join 170 callstack C 171 on 172 A.id = C.callid 173 left join process P on P.id = A.ipid 174 where 175 C.ts > 0 176 and 177 c.dur >= -1 178 and 179 c.cookie not null 180 and 181 P.pid in (${asyncPid.join(',')}) 182 and 183 c.name in (${asyncNames.map((it) => "'" + it + "'").join(',')}) 184 and 185 not ((C.ts - D.start_ts + C.dur < $leftNS) or (C.ts - D.start_ts > $rightNS)) 186 group by 187 c.name 188 order by 189 wallDuration desc;`, 190 { $leftNS: leftNS, $rightNS: rightNS } 191 ); 192export const querySearchFunc = (search: string): Promise<Array<SearchFuncBean>> => 193 query( 194 'querySearchFunc', 195 ` 196 select c.cookie, 197 c.id, 198 c.name as funName, 199 c.ts - r.start_ts as startTime, 200 c.dur, 201 c.depth, 202 t.tid, 203 t.name as threadName, 204 p.pid, 205 c.argsetid, 206 'func' as type 207 from callstack c left join thread t on c.callid = t.id left join process p on t.ipid = p.id 208 left join trace_range r 209 where c.name like '%${search}%' and startTime > 0; 210 `, 211 { $search: search } 212 ); 213 214export const querySceneSearchFunc = (search: string, processList: Array<string>): Promise<Array<SearchFuncBean>> => 215 query( 216 'querySearchFunc', 217 ` 218 select c.cookie, 219 c.id, 220 c.name as funName, 221 c.ts - r.start_ts as startTime, 222 c.dur, 223 c.depth, 224 t.tid, 225 t.name as threadName, 226 p.pid, 227 c.argsetid, 228 'func' as type 229 from callstack c left join thread t on c.callid = t.id left join process p on t.ipid = p.id 230 left join trace_range r 231 where c.name like '%${search}%' ESCAPE '\\' and startTime > 0 and p.pid in (${processList.join(',')}); 232 `, 233 { $search: search } 234 ); 235export const queryHeapFunction = (fileId: number): Promise<Array<HeapTraceFunctionInfo>> => 236 query( 237 'queryHeapFunction', 238 `SELECT function_index as index ,function_id as id ,name,script_name as scriptName,script_id as scriptId,line,column 239 FROM js_heap_trace_function_info WHERE file_id = ${fileId}` 240 ); 241export const queryHeapTraceNode = (fileId: number): Promise<Array<any>> => 242 query( 243 'queryHeapTraceNode', 244 `SELECT F.name, 245 F.script_name as scriptName, 246 F.script_id as scriptId, 247 F.column, 248 F.line, 249 N.id, 250 N.function_info_index as functionInfoIndex, 251 N.parent_id as parentId, 252 N.count, 253 N.size, 254 IFNULL( S.live_count, 0 ) AS liveCount, 255 IFNULL( S.live_size, 0 ) AS liveSize 256 FROM 257 js_heap_trace_node N 258 LEFT JOIN ( 259 SELECT 260 trace_node_id as traceNodeId, 261 SUM( self_size ) AS liveSize, 262 count( * ) AS liveCount 263 FROM 264 js_heap_nodes 265 WHERE 266 file_id = ${fileId} 267 AND trace_node_id != 0 268 GROUP BY 269 trace_node_id 270 ) S ON N.id = S.trace_node_id 271 LEFT JOIN js_heap_trace_function_info F ON (F.file_id = N.file_id 272 AND F.function_index = N.function_info_index) 273 WHERE 274 N.file_id = ${fileId} 275 ORDER BY 276 N.id` 277 ); 278export const queryTaskPoolOtherRelationData = (ids: Array<number>, tid: number): Promise<Array<FuncStruct>> => { 279 let sqlStr = `select 280 c.ts-D.start_ts as startTs, 281 c.dur, 282 c.name as funName, 283 c.argsetid, 284 c.depth, 285 c.id as id, 286 A.itid as itid, 287 A.ipid as ipid 288 from thread A,trace_range D 289 left join callstack C on A.id = C.callid 290 where startTs not null and c.cookie is null and tid = $tid and c.id in (${ids.join(',')})`; 291 return query('queryTaskPoolOtherRelationData', sqlStr, { $ids: ids, $tid: tid }); 292}; 293 294export const queryTaskPoolRelationData = (ids: Array<number>, tids: Array<number>): Promise<Array<FuncStruct>> => { 295 const sqlArray: Array<string> = []; 296 if (ids.length > 0) { 297 for (let index = 0; index < ids.length; index++) { 298 if (index !== 0) { 299 sqlArray.push(`or`); 300 } 301 sqlArray.push(`( tid = ${tids[index]} and c.id = ${ids[index]})`); 302 } 303 } 304 let sql = sqlArray.join(' '); 305 let sqlStr = `select 306 c.ts-D.start_ts as startTs, 307 c.dur, 308 c.name as funName, 309 c.argsetid, 310 c.depth, 311 c.id as id, 312 A.itid as itid, 313 A.ipid as ipid 314 from thread A,trace_range D 315 left join callstack C on A.id = C.callid 316 where startTs not null and c.cookie is null and (${sql})`; 317 return query('queryTaskPoolRelationData', sqlStr); 318}; 319