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