• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 select
17  distinct process_view.pid as pid,
18  process_view.name as processName
19from (
20  select ipid, itid from sched_slice join thread_view using(itid) group by itid
21) the_tracks
22left join (select ipid, sum(dur) as total_dur
23  from sched_view join thread_view using(itid)
24  group by ipid
25) using(ipid)
26left join process_view using(ipid)
27where pid is not null
28order by
29  total_dur desc,
30  the_tracks.ipid,
31 processName,
32  the_tracks.itid;
33`
34let temp_query_cpu_data = `create table temp_query_cpu_data as with list as (SELECT
35    IP.name as processName,
36    IP.name processCmdLine,
37    IP.pid as processId,B.cpu,
38    A.name,
39    C.id as schedId,
40    A.tid,
41    A.id,
42    A.type,
43    B.dur,
44    B.ts - TR.start_ts AS startTime,
45    C.priority,
46    C.end_state
47from thread_state AS B
48    left join  thread as A
49    left join sched_slice AS C
50    left join trace_section AS TR
51    left join process AS IP
52where B.itid = A.id
53    and B.itid = C.itid and B.ts = C.ts
54    and A.ipid = IP.id
55order by B.rowid)
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_section 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 select ta.id,type, ts, dur, ta.cpu, itid as utid, state
69     ,ts-tb.start_ts as startTime,tc.tid,tc.pid,tc.process,tc.thread
70from thread_state ta,trace_section tb
71left join (
72    select it.id,tid,pid,ip.name as process,it.name as thread from thread as it left join process ip on it.ipid = ip.id
73    ) 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 select tid,
79    A.name as threadName,
80    is_main_thread,
81    c.callid as track_id,
82    c.ts-D.start_ts as startTs,
83    c.dur,
84    c.name as funName,
85    c.parent_id,
86    c.id,
87    c.depth,
88    c.argsetid
89from thread A,trace_section D
90left join callstack C on A.id = C.callid
91where startTs not null and c.cookie is null;
92create index temp_query_thread_function_idx on temp_query_thread_function(tid);
93`;
94
95let 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
96     , B.cpu, B.ts-TR.start_ts AS startTime,B.dur,B.state,IP.pid,IP.name as processName
97                from thread_state AS B
98                left join thread as A
99                left join trace_section AS TR
100                left join process AS IP on IP.id=ipid
101                where A.id=B.itid;
102                create index temp_query_thread_data_idx on temp_query_thread_data(tid);`
103
104let temp_view = `CREATE VIEW IF NOT EXISTS thread_view AS SELECT id as itid, * FROM thread;
105CREATE VIEW IF NOT EXISTS process_view AS SELECT id as ipid, * FROM process;
106CREATE VIEW IF NOT EXISTS sched_view AS SELECT *, ts + dur as ts_end FROM sched_slice;
107CREATE VIEW IF NOT EXISTS instants_view AS SELECT *, 0.0 as value FROM instant;
108CREATE VIEW IF NOT EXISTS trace_section AS select start_ts, end_ts from trace_range;`
109
110let 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;`
111let temp_query_cpu_max_freq = `create table temp_query_cpu_max_freq as select max(value) as maxFreq
112from measure c
113inner join cpu_measure_filter t on c.filter_id = t.id
114where (name = 'cpufreq' or name='cpu_frequency');`
115
116let temp_get_tab_states_group_by_process = `create table temp_get_tab_states_group_by_process as
117select IP.name as process,IP.pid as processId,
118       dur,
119      Ip.id as id,
120    (ts - B.start_ts + dur) as end_ts,
121    (ts - B.start_ts) as start_ts
122from thread_state as A,trace_section as B
123    left join  thread as C on A.itid = C.id
124    left join process AS IP on C.ipid = IP.id
125where A.dur > 0 and processId not null and (ts - B.start_ts)>0;
126create index temp_get_tab_states_group_by_process_idx on temp_get_tab_states_group_by_process(end_ts,start_ts);
127`
128
129let temp_get_tab_states_group_by_state_pid_tid = ` create table temp_get_tab_states_group_by_state_pid_tid as
130select  IP.name as process,
131    IP.pid as processId,
132    A.name as thread,
133    B.state as state,
134    A.tid as threadId,
135    B.dur as dur,
136    A.tid as tid,
137    (B.ts - TR.start_ts + B.dur) as end_ts,
138    (B.ts - TR.start_ts) as start_ts
139from thread_state AS B
140    left join  thread as A on B.itid = A.id
141    left join process AS IP on A.ipid = IP.id
142    left join trace_section AS TR
143where
144    B.dur > 0 and IP.pid not null and (B.ts - TR.start_ts > 0);
145create 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);
146create 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);
147create 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);
148`
149let temp_get_tab_states_group_by_state_pid = `create table temp_get_tab_states_group_by_state_pid as
150select IP.name as process,
151       IP.pid as processId,
152       B.state as state,
153    B.dur as  dur,
154    A.tid as tid,
155    (ts - TR.start_ts + dur) as end_ts,
156    (ts - TR.start_ts) as start_ts
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_section AS TR
161where pid not null and
162    B.dur > 0 and (ts - TR.start_ts > 0);
163    create index temp_get_tab_states_group_by_state_pid_idx0 on temp_get_tab_states_group_by_state_pid(process,processId,state);
164    create index temp_get_tab_states_group_by_state_pid_idx1 on temp_get_tab_states_group_by_state_pid(start_ts,end_ts);
165`
166let temp_get_tab_states_group_by_state = `create table temp_get_tab_states_group_by_state as
167select state,
168        dur,
169        (ts - B.start_ts + dur) as end_ts,
170        (ts - B.start_ts) as start_ts
171 from thread_state as A,trace_section as B
172     left join  thread as C on A.itid = C.id
173     left join process AS IP on C.ipid = IP.id
174 where A.dur > 0 and IP.pid not null and (ts - B.start_ts > 0);
175 create index temp_get_tab_states_group_by_state_idx0 on temp_get_tab_states_group_by_state(state);
176 create index temp_get_tab_states_group_by_state_idx1 on temp_get_tab_states_group_by_state(start_ts,end_ts);
177 `
178let temp_get_tab_states_group_by_process_thread = `create table temp_get_tab_states_group_by_process_thread as
179select IP.name as process,
180        IP.pid as processId,
181        A.name as thread,
182        a.tid as threadId,
183        B.dur as dur,
184        A.tid as tid,
185        (ts - TR.start_ts + dur) as end_ts,
186        (ts - TR.start_ts) as start_ts
187 from thread_state AS B
188     left join  thread as A on B.itid = A.id
189     left join process AS IP on A.ipid = IP.id
190     left join trace_section AS TR
191 where pid not null and
192     B.dur > 0 and (ts - TR.start_ts)>0;
193 create index temp_get_tab_states_group_by_process_thread_idx0 on temp_get_tab_states_group_by_process_thread(process,processId,thread,threadId);
194 create index temp_get_tab_states_group_by_process_thread_idx1 on temp_get_tab_states_group_by_process_thread(start_ts,end_ts);
195`
196
197let temp_get_cpu_rate = `create table temp_get_cpu_rate as
198with cpu as (
199    select cpu,ts,dur,(case when ro < 99 then ro else 99 end) as ro ,
200           (case when ro < 99 then stime+ro*cell else stime + 99 * cell end) as st,
201           (case when ro < 99 then stime + (ro+1)*cell else etime end) as et
202    from (
203        select cpu,ts,A.dur,((ts+A.dur)-D.start_ts)/((D.end_ts-D.start_ts)/100) as ro,D.start_ts as stime,D.end_ts etime,(D.end_ts-D.start_ts)/100 as cell
204        from sched_slice A
205        left join trace_section D
206        left join thread B on A.itid = B.id
207        left join process C on B.ipid = C.id
208        where tid != 0 and (A.ts) between D.start_ts and D.end_ts))
209select cpu,ro,
210       sum(case
211               when ts <= st and ts + dur <= et then (ts + dur - st)
212               when ts <= st and ts + dur > et then et-st
213               when ts > st and ts + dur <= et then dur
214               when ts > st and ts + dur > et then et - ts end)/cast(et-st as float) as rate
215from cpu
216group by cpu,ro;
217`
218
219let temp_get_tab_thread_states= `create table temp_get_tab_thread_states as
220    select
221    IP.name as process,
222    IP.pid as pid,
223    A.name as thread,
224    A.tid as tid,
225    B.state as state,
226    B.dur as dur,
227    (B.ts - TR.start_ts + B.dur) as end_ts,
228    (B.ts - TR.start_ts) as start_ts
229from thread_state AS B
230left join thread as A on A.id = B.itid
231left join trace_section AS TR
232left join process AS IP on IP.id=ipid
233where (B.ts - TR.start_ts > 0);
234create index temp_get_tab_thread_states_idx0 on temp_get_tab_thread_states(process,pid,thread,tid,state);
235create index temp_get_tab_thread_states_idx1 on temp_get_tab_thread_states(start_ts,end_ts);
236`;
237
238let temp_get_tab_slices = `create table temp_get_tab_slices as
239    select
240      c.name as name,
241      c.dur as dur,
242      A.tid as tid,
243      (C.ts - D.start_ts + C.dur) as end_ts,
244      (C.ts - D.start_ts) as start_ts
245from thread A,trace_section D
246left join callstack C on A.id = C.callid
247where C.ts not null
248      and c.dur >= 0
249      and (C.ts - D.start_ts > 0);
250create index temp_get_tab_slices_idx0 on temp_get_tab_slices(name);
251`;
252
253let delete_callstack_binder_data = `DELETE FROM callstack WHERE dur<0 or name like 'binder%';`;
254let temp_init_sql_list = [
255    temp_view,
256    delete_callstack_binder_data,
257    temp_query_cpu_freq,
258    temp_query_cpu_max_freq,
259    temp_query_process,
260    temp_query_cpu_data,
261    temp_query_freq_data,
262    temp_query_process_data,
263    temp_query_thread_function,
264    temp_query_thread_data,
265    temp_get_cpu_rate,
266];
267let temp_init_sql = `
268${temp_view};
269${temp_query_cpu_freq};
270${temp_query_cpu_max_freq};
271${temp_query_process};
272${temp_query_cpu_data};
273${temp_query_freq_data};
274${temp_query_process_data};
275${temp_query_thread_function};
276${temp_query_thread_data};
277${temp_get_cpu_rate};
278${temp_get_tab_states_group_by_process};
279${temp_get_tab_states_group_by_process_thread};
280${temp_get_tab_states_group_by_state_pid};
281${temp_get_tab_states_group_by_state_pid_tid};
282${temp_get_tab_states_group_by_state};
283${temp_get_tab_thread_states};
284${temp_get_tab_slices};
285`