1#!/usr/bin/env python3 2# Copyright (C) 2023 The Android Open Source Project 3# 4# Licensed under the Apache License, Version 2.0 (the "License"); 5# you may not use this file except in compliance with the License. 6# You may obtain a copy of the License a 7# 8# http://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, 12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13# See the License for the specific language governing permissions and 14# limitations under the License. 15 16from python.generators.diff_tests.testing import Path, DataPath, Metric 17from python.generators.diff_tests.testing import Csv, Json, TextProto 18from python.generators.diff_tests.testing import DiffTestBlueprint, TraceInjector 19from python.generators.diff_tests.testing import TestSuite 20 21 22class TablesCounters(TestSuite): 23 # Counters table 24 def test_synth_1_filter_counter(self): 25 return DiffTestBlueprint( 26 trace=Path('../common/synth_1.py'), 27 query=""" 28 SELECT COUNT(*) 29 FROM counter 30 WHERE 31 track_id = 0; 32 """, 33 out=Csv(""" 34 "COUNT(*)" 35 2 36 """)) 37 38 def test_memory_counters_b120278869_neg_ts_end(self): 39 return DiffTestBlueprint( 40 trace=DataPath('memory_counters.pb'), 41 query=""" 42 SELECT count(*) FROM counters WHERE -1 < ts; 43 """, 44 out=Csv(""" 45 "count(*)" 46 98688 47 """)) 48 49 def test_counters_where_cpu_counters_where_cpu(self): 50 return DiffTestBlueprint( 51 trace=Path('counters_where_cpu.py'), 52 query=""" 53 SELECT 54 ts, 55 lead(ts, 1, ts) OVER (PARTITION BY name ORDER BY ts) - ts AS dur, 56 value 57 FROM counter c 58 JOIN cpu_counter_track t ON t.id = c.track_id 59 WHERE cpu = 1; 60 """, 61 out=Csv(""" 62 "ts","dur","value" 63 1000,1,3000.000000 64 1001,0,4000.000000 65 """)) 66 67 def test_counters_group_by_freq_counters_group_by_freq(self): 68 return DiffTestBlueprint( 69 trace=Path('counters_group_by_freq.py'), 70 query=""" 71 SELECT 72 value, 73 sum(dur) AS dur_sum 74 FROM ( 75 SELECT value, 76 lead(ts) OVER (PARTITION BY name, track_id ORDER BY ts) - ts AS dur 77 FROM counter 78 JOIN counter_track ON counter.track_id = counter_track.id 79 ) 80 WHERE value > 0 81 GROUP BY value 82 ORDER BY dur_sum DESC; 83 """, 84 out=Csv(""" 85 "value","dur_sum" 86 4000.000000,2 87 3000.000000,1 88 """)) 89 90 def test_filter_row_vector_example_android_trace_30s(self): 91 return DiffTestBlueprint( 92 trace=DataPath('example_android_trace_30s.pb'), 93 query=""" 94 SELECT ts 95 FROM counter 96 WHERE 97 ts > 72563651549 98 AND track_id = ( 99 SELECT t.id 100 FROM process_counter_track t 101 JOIN process p USING (upid) 102 WHERE 103 t.name = 'Heap size (KB)' 104 AND p.pid = 1204 105 ) 106 AND value != 17952.000000 107 LIMIT 20; 108 """, 109 out=Path('filter_row_vector_example_android_trace_30s.out')) 110 111 def test_counter_dur_example_android_trace_30s(self): 112 return DiffTestBlueprint( 113 trace=DataPath('example_android_trace_30s.pb'), 114 query=Path('counter_dur_test.sql'), 115 out=Csv(""" 116 "ts","dur" 117 100351738640,-1 118 100351738640,-1 119 100351738640,-1 120 70731059648,19510835 121 70731059648,19510835 122 70731059648,19510835 123 73727335051,23522762 124 73727335051,23522762 125 73727335051,23522762 126 86726132752,24487554 127 """)) 128 129 def test_counter_dur_example_android_trace_30s_machine_id(self): 130 return DiffTestBlueprint( 131 trace=DataPath('example_android_trace_30s.pb'), 132 trace_modifier=TraceInjector( 133 ['ftrace_events', 'sys_stats', 'process_stats', 'process_tree'], 134 {'machine_id': 1001}), 135 query=""" 136 SELECT ts, dur, m.raw_id as raw_machine_id 137 FROM experimental_counter_dur c 138 JOIN counter_track t on c.track_id = t.id 139 JOIN machine m on t.machine_id = m.id 140 WHERE track_id IN (1, 2, 3) 141 ORDER BY dur LIMIT 10; 142 """, 143 out=Csv(""" 144 "ts","dur","raw_machine_id" 145 100351738640,-1,1001 146 100351738640,-1,1001 147 100351738640,-1,1001 148 70731059648,19510835,1001 149 70731059648,19510835,1001 150 70731059648,19510835,1001 151 73727335051,23522762,1001 152 73727335051,23522762,1001 153 73727335051,23522762,1001 154 86726132752,24487554,1001 155 """)) 156 157 # Tests counter.machine_id and process_counter_track.machine. 158 def test_filter_row_vector_example_android_trace_30s_machine_id(self): 159 return DiffTestBlueprint( 160 trace=DataPath('example_android_trace_30s.pb'), 161 trace_modifier=TraceInjector( 162 ['ftrace_events', 'sys_stats', 'process_stats', 'process_tree'], 163 {'machine_id': 1001}), 164 query=""" 165 SELECT ts 166 FROM counter 167 WHERE 168 ts > 72563651549 169 AND track_id = ( 170 SELECT t.id 171 FROM process_counter_track t 172 JOIN process p USING (upid) 173 WHERE 174 t.name = 'Heap size (KB)' 175 AND p.pid = 1204 176 AND t.machine_id is not NULL 177 ) 178 AND value != 17952.000000 179 LIMIT 20; 180 """, 181 out=Path('filter_row_vector_example_android_trace_30s.out')) 182 183 def test_counters_where_cpu_counters_where_cpu_machine_id(self): 184 return DiffTestBlueprint( 185 trace=Path('counters_where_cpu.py'), 186 trace_modifier=TraceInjector(['ftrace_events'], {'machine_id': 1001}), 187 query=""" 188 SELECT 189 ts, 190 lead(ts, 1, ts) OVER (PARTITION BY track_id ORDER BY ts) - ts AS dur, 191 value 192 FROM counter c 193 JOIN cpu_counter_track t on c.track_id = t.id 194 WHERE cpu = 1; 195 """, 196 out=Csv(""" 197 "ts","dur","value" 198 1000,1,3000.000000 199 1001,0,4000.000000 200 """)) 201 202 def test_synth_1_filter_counter_machine_id(self): 203 return DiffTestBlueprint( 204 trace=Path('../common/synth_1.py'), 205 trace_modifier=TraceInjector( 206 ['ftrace_events', 'process_stats', 'process_tree'], 207 {'machine_id': 1001}), 208 query=""" 209 SELECT COUNT(*) 210 FROM counter 211 WHERE 212 track_id = 0; 213 """, 214 out=Csv(""" 215 "COUNT(*)" 216 2 217 """)) 218 219 def test_memory_counters_machine_id(self): 220 return DiffTestBlueprint( 221 trace=DataPath('memory_counters.pb'), 222 trace_modifier=TraceInjector( 223 ['ftrace_events', 'sys_stats', 'process_stats', 'process_tree'], 224 {'machine_id': 1001}), 225 query=""" 226 SELECT count(*) 227 FROM counters 228 WHERE -1 < ts group by machine_id; 229 """, 230 out=Csv(""" 231 "count(*)" 232 98688 233 """)) 234