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 19from python.generators.diff_tests.testing import TestSuite 20 21 22class ChromeProcesses(TestSuite): 23 24 def test_chrome_processes(self): 25 return DiffTestBlueprint( 26 trace=DataPath('chrome_scroll_without_vsync.pftrace'), 27 query=""" 28 SELECT RUN_METRIC('chrome/chrome_processes.sql'); 29 SELECT pid, name, process_type FROM chrome_process; 30 """, 31 out=Csv(""" 32 "pid","name","process_type" 33 18250,"Renderer","Renderer" 34 17547,"Browser","Browser" 35 18277,"GPU Process","Gpu" 36 17578,"Browser","Browser" 37 """)) 38 39 def test_chrome_processes_android_systrace(self): 40 return DiffTestBlueprint( 41 trace=DataPath('chrome_android_systrace.pftrace'), 42 query=""" 43 SELECT RUN_METRIC('chrome/chrome_processes.sql'); 44 SELECT pid, name, process_type FROM chrome_process; 45 """, 46 out=Path('chrome_processes_android_systrace.out')) 47 48 def test_chrome_threads(self): 49 return DiffTestBlueprint( 50 trace=DataPath('chrome_scroll_without_vsync.pftrace'), 51 query=""" 52 SELECT RUN_METRIC('chrome/chrome_processes.sql'); 53 SELECT tid, name, is_main_thread, canonical_name 54 FROM chrome_thread 55 ORDER BY tid, name; 56 """, 57 out=Path('chrome_threads.out')) 58 59 def test_chrome_threads_android_systrace(self): 60 return DiffTestBlueprint( 61 trace=DataPath('chrome_android_systrace.pftrace'), 62 query=""" 63 SELECT RUN_METRIC('chrome/chrome_processes.sql'); 64 SELECT tid, name, is_main_thread, canonical_name 65 FROM chrome_thread 66 ORDER BY tid, name; 67 """, 68 out=Path('chrome_threads_android_systrace.out')) 69 70 def test_chrome_processes_type(self): 71 return DiffTestBlueprint( 72 trace=DataPath('chrome_scroll_without_vsync.pftrace'), 73 query=""" 74 SELECT pid, name, string_value AS chrome_process_type 75 FROM 76 process 77 JOIN 78 (SELECT * FROM args WHERE key = "chrome.process_type") chrome_process_args 79 ON 80 process.arg_set_id = chrome_process_args.arg_set_id 81 ORDER BY pid; 82 """, 83 out=Csv(""" 84 "pid","name","chrome_process_type" 85 17547,"Browser","Browser" 86 17578,"Browser","Browser" 87 18250,"Renderer","Renderer" 88 18277,"GPU Process","Gpu" 89 """)) 90 91 def test_chrome_processes_type_android_systrace(self): 92 return DiffTestBlueprint( 93 trace=DataPath('chrome_android_systrace.pftrace'), 94 query=""" 95 SELECT pid, name, string_value AS chrome_process_type 96 FROM 97 process 98 JOIN 99 (SELECT * FROM args WHERE key = "chrome.process_type") chrome_process_args 100 ON 101 process.arg_set_id = chrome_process_args.arg_set_id 102 ORDER BY pid; 103 """, 104 out=Path('chrome_processes_type_android_systrace.out')) 105 106 def test_track_with_chrome_process(self): 107 return DiffTestBlueprint( 108 trace=TextProto(r""" 109 packet { 110 trusted_packet_sequence_id: 1 111 incremental_state_cleared: true 112 timestamp: 0 113 track_descriptor { 114 uuid: 10 115 process { 116 pid: 5 117 process_name: "p5" 118 } 119 # Empty Chrome process. This is similar to a process descriptor emitted by 120 # Chrome for a process with an unknown Chrome process_type. This process 121 # should still receive a "chrome_process_type" arg in the args table, but 122 # with a NULL value. 123 chrome_process {} 124 } 125 } 126 """), 127 query=""" 128 SELECT pid, name, string_value AS chrome_process_type 129 FROM 130 process 131 JOIN 132 (SELECT * FROM args WHERE key = "chrome.process_type") chrome_process_args 133 ON 134 process.arg_set_id = chrome_process_args.arg_set_id 135 ORDER BY pid; 136 """, 137 out=Csv(""" 138 "pid","name","chrome_process_type" 139 5,"p5","[NULL]" 140 """)) 141 142 # Missing processes. 143 def test_chrome_missing_processes_default_trace(self): 144 return DiffTestBlueprint( 145 trace=DataPath('chrome_scroll_without_vsync.pftrace'), 146 query=""" 147 SELECT pid, reliable_from 148 FROM 149 experimental_missing_chrome_processes 150 JOIN 151 process 152 USING(upid) 153 ORDER BY upid; 154 """, 155 out=Csv(""" 156 "pid","reliable_from" 157 """)) 158 159 def test_chrome_missing_processes(self): 160 return DiffTestBlueprint( 161 trace=TextProto(r""" 162 packet { 163 timestamp: 1 164 incremental_state_cleared: true 165 trusted_packet_sequence_id: 1 166 track_event { 167 type: TYPE_INSTANT 168 name: "ActiveProcesses" 169 chrome_active_processes { 170 pid: 10 171 pid: 100 172 pid: 1000 173 } 174 } 175 } 176 packet { 177 timestamp: 1 178 trusted_packet_sequence_id: 2 179 track_descriptor { 180 uuid: 1 181 process { 182 pid: 10 183 } 184 parent_uuid: 0 185 } 186 } 187 packet { 188 timestamp: 1000000000 189 trusted_packet_sequence_id: 3 190 track_descriptor { 191 uuid: 2 192 process { 193 pid: 100 194 } 195 parent_uuid: 0 196 } 197 } 198 """), 199 query=""" 200 SELECT pid, reliable_from 201 FROM 202 experimental_missing_chrome_processes 203 JOIN 204 process 205 USING(upid) 206 ORDER BY upid; 207 """, 208 out=Csv(""" 209 "pid","reliable_from" 210 100,1000000000 211 1000,"[NULL]" 212 """)) 213 214 def test_chrome_missing_processes_args(self): 215 return DiffTestBlueprint( 216 trace=TextProto(r""" 217 packet { 218 timestamp: 1 219 incremental_state_cleared: true 220 trusted_packet_sequence_id: 1 221 track_event { 222 type: TYPE_INSTANT 223 name: "ActiveProcesses" 224 chrome_active_processes { 225 pid: 10 226 pid: 100 227 pid: 1000 228 } 229 } 230 } 231 packet { 232 timestamp: 1 233 trusted_packet_sequence_id: 2 234 track_descriptor { 235 uuid: 1 236 process { 237 pid: 10 238 } 239 parent_uuid: 0 240 } 241 } 242 packet { 243 timestamp: 1000000000 244 trusted_packet_sequence_id: 3 245 track_descriptor { 246 uuid: 2 247 process { 248 pid: 100 249 } 250 parent_uuid: 0 251 } 252 } 253 """), 254 query=""" 255 SELECT slice.name, key, int_value 256 FROM 257 slice 258 JOIN 259 args 260 USING(arg_set_id) 261 ORDER BY arg_set_id, key; 262 """, 263 out=Csv(""" 264 "name","key","int_value" 265 "ActiveProcesses","chrome_active_processes.pid[0]",10 266 "ActiveProcesses","chrome_active_processes.pid[1]",100 267 "ActiveProcesses","chrome_active_processes.pid[2]",1000 268 """)) 269 270 def test_chrome_missing_processes_2(self): 271 return DiffTestBlueprint( 272 trace=TextProto(r""" 273 packet { 274 timestamp: 1 275 incremental_state_cleared: true 276 trusted_packet_sequence_id: 1 277 track_event { 278 type: TYPE_INSTANT 279 name: "ActiveProcesses" 280 [perfetto.protos.ChromeTrackEvent.active_processes]: { 281 pid: 10 282 pid: 100 283 pid: 1000 284 } 285 } 286 } 287 packet { 288 timestamp: 1 289 trusted_packet_sequence_id: 2 290 track_descriptor { 291 uuid: 1 292 process { 293 pid: 10 294 } 295 parent_uuid: 0 296 } 297 } 298 packet { 299 timestamp: 1000000000 300 trusted_packet_sequence_id: 3 301 track_descriptor { 302 uuid: 2 303 process { 304 pid: 100 305 } 306 parent_uuid: 0 307 } 308 } 309 """), 310 query=""" 311 SELECT pid, reliable_from 312 FROM 313 experimental_missing_chrome_processes 314 JOIN 315 process 316 USING(upid) 317 ORDER BY upid; 318 """, 319 out=Csv(""" 320 "pid","reliable_from" 321 100,1000000000 322 1000,"[NULL]" 323 """)) 324 325 def test_chrome_missing_processes_extension_args(self): 326 return DiffTestBlueprint( 327 trace=TextProto(r""" 328 packet { 329 timestamp: 1 330 incremental_state_cleared: true 331 trusted_packet_sequence_id: 1 332 track_event { 333 type: TYPE_INSTANT 334 name: "ActiveProcesses" 335 [perfetto.protos.ChromeTrackEvent.active_processes]: { 336 pid: 10 337 pid: 100 338 pid: 1000 339 } 340 } 341 } 342 packet { 343 timestamp: 1 344 trusted_packet_sequence_id: 2 345 track_descriptor { 346 uuid: 1 347 process { 348 pid: 10 349 } 350 parent_uuid: 0 351 } 352 } 353 packet { 354 timestamp: 1000000000 355 trusted_packet_sequence_id: 3 356 track_descriptor { 357 uuid: 2 358 process { 359 pid: 100 360 } 361 parent_uuid: 0 362 } 363 } 364 """), 365 query=""" 366 SELECT slice.name, key, int_value 367 FROM 368 slice 369 JOIN 370 args 371 USING(arg_set_id) 372 ORDER BY arg_set_id, key; 373 """, 374 out=Csv(""" 375 "name","key","int_value" 376 "ActiveProcesses","active_processes.pid[0]",10 377 "ActiveProcesses","active_processes.pid[1]",100 378 "ActiveProcesses","active_processes.pid[2]",1000 379 """)) 380