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 16let temp_query_cpu_data = `create table temp_query_cpu_data as 17with list as (SELECT IP.name as processName, 18 IP.name processCmdLine, 19 IP.pid as processId, 20 B.cpu, 21 A.name, 22 C.id as schedId, 23 A.tid, 24 A.id, 25 A.type, 26 B.dur, 27 B.ts - TR.start_ts AS startTime, 28 C.priority, 29 C.end_state 30 from thread_state AS B 31 left join thread as A on B.itid = A.id 32 left join sched_slice AS C on B.itid = C.itid and B.ts = C.ts 33 left join trace_range AS TR 34 left join process AS IP on A.ipid = IP.id 35 where C.itid is not null 36 order by B.id) 37select * 38from list; 39create index temp_query_cpu_data_idx on temp_query_cpu_data (cpu, startTime); 40`; 41 42let temp_query_freq_data = `create table temp_query_freq_data as 43select cpu, value, ts - tb.start_ts as startNS 44from measure c, 45 trace_range tb 46 inner join cpu_measure_filter t on c.filter_id = t.id 47where (name = 'cpufreq' or name = 'cpu_frequency') 48order by ts; 49create index temp_query_freq_data_idx on temp_query_freq_data (cpu); 50`; 51 52let temp_query_process_data = `create table temp_query_process_data as 53select ta.id, 54 type, 55 ts, 56 dur, 57 ta.cpu, 58 itid as utid, 59 state, 60 ts - tb.start_ts as startTime, 61 tc.tid, 62 tc.pid, 63 tc.process, 64 tc.thread 65from thread_state ta, 66 trace_range tb 67 left join (select it.id, 68 tid, 69 pid, 70 ip.name as process, 71 it.name as thread 72 from thread as it 73 left join process ip on it.ipid = ip.id) tc on ta.itid = tc.id 74where ta.cpu is not null 75order by startTime; 76create index temp_query_process_data_idx on temp_query_process_data (pid, startTime); 77`; 78let temp_query_thread_function = `create table temp_query_thread_function as 79select tid, 80 A.name as threadName, 81 is_main_thread, 82 c.callid as track_id, 83 c.ts - D.start_ts as startTs, 84 c.dur, 85 c.name as funName, 86 c.parent_id, 87 c.id, 88 c.depth, 89 c.argsetid 90from thread A, 91 trace_range D 92 left join callstack C on A.id = C.callid 93where startTs not null and c.cookie is null; 94create index temp_query_thread_function_idx on temp_query_thread_function (tid); 95`; 96 97let temp_query_thread_data = `create table temp_query_thread_data as 98select A.id 99 , A.type 100 , A.tid 101 , A.name 102 , A.start_ts 103 , A.end_ts 104 , A.ipid as upid 105 , A.is_main_thread 106 , B.cpu 107 , B.ts - TR.start_ts AS startTime 108 , B.dur 109 , B.state 110 , IP.pid 111 , IP.name as processName 112from thread_state AS B 113 left join thread as A on A.id = B.itid 114 left join trace_range AS TR 115 left join process AS IP on IP.id = A.ipid; 116create index temp_query_thread_data_idx on temp_query_thread_data (tid);`; 117 118let temp_view = `CREATE VIEW IF NOT EXISTS thread_view AS SELECT id as itid, * FROM thread; 119CREATE VIEW IF NOT EXISTS process_view AS SELECT id as ipid, * FROM process; 120CREATE VIEW IF NOT EXISTS sched_view AS SELECT *, ts + dur as ts_end FROM sched_slice; 121CREATE VIEW IF NOT EXISTS instants_view AS SELECT *, 0.0 as value FROM instant; 122CREATE VIEW IF NOT EXISTS trace_section AS select start_ts, end_ts from trace_range;`; 123 124let temp_query_cpu_freq = `create table temp_query_cpu_freq as 125select cpu 126from cpu_measure_filter 127where (name = 'cpufreq' or name = 'cpu_frequency') 128order by cpu;`; 129let temp_query_cpu_max_freq = `create table temp_query_cpu_max_freq as 130select max(value) as maxFreq 131from measure c 132 inner join cpu_measure_filter t on c.filter_id = t.id 133where (name = 'cpufreq' or name = 'cpu_frequency');`; 134 135let temp_get_tab_states_group_by_process = `create table temp_get_tab_states_group_by_process as 136select IP.name as process, 137 IP.pid as processId, 138 dur, 139 Ip.id as id, 140 (ts - B.start_ts + dur) as end_ts, 141 (ts - B.start_ts) as start_ts 142from thread_state as A, 143 trace_range as B 144 left join thread as C on A.itid = C.id 145 left join process AS IP on C.ipid = IP.id 146where A.dur > 0 147 and processId not null and (ts - B.start_ts)>0; 148create index temp_get_tab_states_group_by_process_idx on temp_get_tab_states_group_by_process (end_ts, start_ts); 149`; 150 151let temp_get_process_thread_state_data = ` create table temp_get_process_thread_state_data as 152select IP.name as process, 153 IP.pid as processId, 154 A.name as thread, 155 B.state as state, 156 A.tid as threadId, 157 B.dur, 158 (B.ts - TR.start_ts + B.dur) as end_ts, 159 (B.ts - TR.start_ts) as start_ts, 160 B.cpu, 161 C.priority, 162 '-' as note 163from thread_state as B 164 left join thread as A on B.itid = A.id 165 left join process as IP on A.ipid = IP.id 166 left join trace_range as TR 167 left join sched_slice as C on B.itid = C.itid and C.ts = B.ts 168where B.dur > 0 169 and IP.pid not null and (B.ts - TR.start_ts) >= 0; 170create index temp_get_process_thread_state_data_idx on temp_get_process_thread_state_data (end_ts, start_ts); 171`; 172 173let temp_get_tab_states_group_by_state_pid_tid = ` create table temp_get_tab_states_group_by_state_pid_tid as 174select IP.name as process, 175 IP.pid as processId, 176 A.name as thread, 177 B.state as state, 178 A.tid as threadId, 179 B.dur as dur, 180 A.tid as tid, 181 (B.ts - TR.start_ts + B.dur) as end_ts, 182 (B.ts - TR.start_ts) as start_ts 183from thread_state AS B 184 left join thread as A on B.itid = A.id 185 left join process AS IP on A.ipid = IP.id 186 left join trace_range AS TR 187where B.dur > 0 188 and IP.pid not null and (B.ts - TR.start_ts > 0); 189create index temp_get_tab_states_group_by_state_pid_tid_idx0 on temp_get_tab_states_group_by_state_pid_tid (process, processId, thread, threadId, state); 190create index temp_get_tab_states_group_by_state_pid_tid_idx1 on temp_get_tab_states_group_by_state_pid_tid (end_ts, start_ts); 191create index temp_get_tab_states_group_by_state_pid_tid_idx3 on temp_get_tab_states_group_by_state_pid_tid (end_ts, start_ts, process, processId, thread, threadId, state); 192`; 193let temp_get_tab_states_group_by_state_pid = `create table temp_get_tab_states_group_by_state_pid as 194select IP.name as process, 195 IP.pid as processId, 196 B.state as state, 197 B.dur as dur, 198 A.tid as tid, 199 (ts - TR.start_ts + dur) as end_ts, 200 (ts - TR.start_ts) as start_ts 201from thread_state AS B 202 left join thread as A on B.itid = A.id 203 left join process AS IP on A.ipid = IP.id 204 left join trace_range AS TR 205where pid not null and 206 B.dur > 0 and (ts - TR.start_ts > 0); 207create index temp_get_tab_states_group_by_state_pid_idx0 on temp_get_tab_states_group_by_state_pid (process, processId, state); 208create index temp_get_tab_states_group_by_state_pid_idx1 on temp_get_tab_states_group_by_state_pid (start_ts, end_ts); 209`; 210let temp_get_tab_states_group_by_state = `create table temp_get_tab_states_group_by_state as 211select state, 212 dur, 213 (ts - B.start_ts + dur) as end_ts, 214 (ts - B.start_ts) as start_ts 215from thread_state as A, 216 trace_range as B 217 left join thread as C on A.itid = C.id 218 left join process AS IP on C.ipid = IP.id 219where A.dur > 0 220 and IP.pid not null and (ts - B.start_ts > 0); 221create index temp_get_tab_states_group_by_state_idx0 on temp_get_tab_states_group_by_state (state); 222create index temp_get_tab_states_group_by_state_idx1 on temp_get_tab_states_group_by_state (start_ts, end_ts); 223`; 224let temp_get_tab_states_group_by_process_thread = `create table temp_get_tab_states_group_by_process_thread as 225select IP.name as process, 226 IP.pid as processId, 227 A.name as thread, 228 a.tid as threadId, 229 B.dur as dur, 230 A.tid as tid, 231 (ts - TR.start_ts + dur) as end_ts, 232 (ts - TR.start_ts) as start_ts 233from thread_state AS B 234 left join 235 thread as A on B.itid = A.id 236 left join 237 process AS IP on A.ipid = IP.id 238 left join 239 trace_range AS TR 240where pid not null 241 and 242 B.dur > 0 243 and 244 (ts - TR.start_ts)>0; 245create index temp_get_tab_states_group_by_process_thread_idx0 on temp_get_tab_states_group_by_process_thread (process, processId, thread, threadId); 246create index temp_get_tab_states_group_by_process_thread_idx1 on temp_get_tab_states_group_by_process_thread (start_ts, end_ts); 247`; 248 249let temp_get_cpu_rate = `create table temp_get_cpu_rate as 250with cpu as (select cpu, 251 ts, 252 dur, 253 (case when ro < 99 then ro else 99 end) as ro, 254 (case when ro < 99 then stime + ro * cell else stime + 99 * cell end) as st, 255 (case when ro < 99 then stime + (ro + 1) * cell else etime end) as et 256 from (select cpu, 257 ts, 258 A.dur, 259 ((ts + A.dur) - D.start_ts) / ((D.end_ts - D.start_ts) / 100) as ro, 260 D.start_ts as stime, 261 D.end_ts etime, 262 (D.end_ts - D.start_ts) / 100 as cell 263 from sched_slice A 264 left join 265 trace_range D 266 left join 267 thread B on A.itid = B.id 268 left join 269 process C on B.ipid = C.id 270 where tid != 0 271 and (A.ts) 272 between D.start_ts and D.end_ts)) 273select cpu, 274 ro, 275 sum(case 276 when ts <= st and ts + dur <= et then (ts + dur - st) 277 when ts <= st and ts + dur > et then et - st 278 when ts > st and ts + dur <= et then dur 279 when ts > st and ts + dur > et then et - ts end) / cast(et - st as float) as rate 280from cpu 281group by cpu, ro; 282`; 283 284let temp_get_tab_thread_states = `create table temp_get_tab_thread_states as 285select IP.name as process, 286 IP.pid as pid, 287 A.name as thread, 288 A.tid as tid, 289 B.state as state, 290 B.dur as dur, 291 (B.ts - TR.start_ts + ifnull(B.dur, 0)) as end_ts, 292 (B.ts - TR.start_ts) as start_ts 293from thread_state AS B 294 left join 295 thread as A 296 on 297 A.id = B.itid 298 left join 299 trace_range AS TR 300 left join 301 process AS IP 302 on 303 IP.id = A.ipid 304where (B.ts - TR.start_ts > 0); 305create index temp_get_tab_thread_states_idx0 on temp_get_tab_thread_states (process, pid, thread, tid, state); 306create index temp_get_tab_thread_states_idx1 on temp_get_tab_thread_states (start_ts, end_ts); 307`; 308 309let temp_get_tab_slices = `create table temp_get_tab_slices as 310select c.name as name, 311 c.dur as dur, 312 A.tid as tid, 313 (C.ts - D.start_ts + C.dur) as end_ts, 314 (C.ts - D.start_ts) as start_ts 315from thread A, 316 trace_range D 317 left join 318 callstack C on A.id = C.callid 319where C.ts not null 320 and c.dur >= 0 321 and (C.ts - D.start_ts > 0); 322create index temp_get_tab_slices_idx0 on temp_get_tab_slices (name); 323`; 324 325let createProcessNoId = ` 326 insert into process(id, ipid, type, pid, name, start_ts) 327 SELECT null, null, 'process' as type, tid as pid, t.name, t.start_ts 328 from thread t 329 where ipid is null 330 and tid != 0; 331 update process 332 set id = ROWID - 1, 333 ipid = ROWID - 1 334 where id is null; 335 update thread 336 set ipid = (select id from process where thread.tid = process.pid) 337 where thread.ipid is null; 338`; 339let temp_create_cpu_freq_view = `CREATE VIEW cpu_freq_view AS SELECT B.cpu, A.ts, LEAD(A.ts, 1, (SELECT end_ts FROM trace_range)) OVER (PARTITION BY A.filter_id ORDER BY ts) AS end_ts,LEAD(A.ts, 1, (SELECT end_ts FROM trace_range)) OVER (PARTITION BY A.filter_id ORDER BY ts) - ts AS dur,value AS freq FROM measure AS A, cpu_measure_filter AS B WHERE B.name = 'cpu_frequency' AND A.filter_id = B.id`; 340let temp_create_virtual_table = `CREATE VIRTUAL table result USING SPAN_JOIN(cpu_freq_view partitioned cpu, sched_slice partitioned cpu)`; 341 342let queryThreadWakeUpFrom = ` 343 select TB.tid, TB.name as thread, TA.cpu, (TA.ts - TR.start_ts) as ts, TC.pid, TC.name as process 344 from (select ts as wakeTs, wakeup_from as wakeupFromTid 345 from instant, 346 trace_range 347 where name = 'sched_wakeup' 348 and ref = $itid 349 and ts > start_ts + $startTime 350 and ts < start_ts + $startTime + $dur 351 order by ts) TW 352 left join thread_state TA 353 on TW.wakeupFromTid = TA.itid and TA.ts < TW.wakeTs and TA.ts + TA.dur >= TW.wakeTs 354 left join thread TB on TA.itid = TB.id 355 left join process TC on TB.ipid = TC.id 356 left join trace_range TR 357 where TB.ipid not null 358 limit 1; 359`; 360 361let queryThreadWakeUp = ` 362 select TB.tid, TB.name as thread, min(TA.ts - TR.start_ts) as ts, TC.pid, TC.name as process 363 from (select min(ts) as wakeTs, ref as itid 364 from instant, 365 trace_range 366 where name = 'sched_wakeup' 367 and wakeup_from = $itid 368 and ts > start_ts + $startTime 369 and ts < start_ts + $startTime + $dur 370 group by ref) TW 371 left join thread_state TA on TW.itid = TA.itid and TA.ts > TW.wakeTs 372 left join thread TB on TA.itid = TB.id 373 left join process TC on TB.ipid = TC.id 374 left join trace_range TR 375 where TB.ipid not null 376 group by TB.tid, TB.name, TC.pid, TC.name; 377`; 378 379let delete_callstack_binder_data = `DELETE 380 FROM callstack 381 WHERE dur < -1 382 or name = 'binder transaction async' 383 or name = 'binder async rcv';`; 384// @ts-ignore 385let temp_init_sql_list = []; 386// @ts-ignore 387export { temp_init_sql_list }; 388