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