1#!/usr/bin/env python3 2# Copyright (C) 2024 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 Csv, DataPath, TextProto 17from python.generators.diff_tests.testing import DiffTestBlueprint 18from python.generators.diff_tests.testing import TestSuite 19 20 21class LinuxCpu(TestSuite): 22 23 def test_cpu_utilization_per_second(self): 24 return DiffTestBlueprint( 25 trace=DataPath('example_android_trace_30s.pb'), 26 query=""" 27 INCLUDE PERFETTO MODULE linux.cpu.utilization.system; 28 29 SELECT * FROM cpu_utilization_per_second; 30 """, 31 out=Csv(""" 32 "ts","utilization","unnormalized_utilization" 33 70000000000,0.004545,0.036362 34 71000000000,0.022596,0.180764 35 72000000000,0.163393,1.307146 36 73000000000,0.452122,3.616972 37 74000000000,0.525557,4.204453 38 75000000000,0.388632,3.109057 39 76000000000,0.425447,3.403579 40 77000000000,0.201112,1.608896 41 78000000000,0.280247,2.241977 42 79000000000,0.345228,2.761827 43 80000000000,0.303258,2.426064 44 81000000000,0.487522,3.900172 45 82000000000,0.080542,0.644336 46 83000000000,0.362450,2.899601 47 84000000000,0.076438,0.611501 48 85000000000,0.110689,0.885514 49 86000000000,0.681488,5.451901 50 87000000000,0.808331,6.466652 51 88000000000,0.941768,7.534142 52 89000000000,0.480556,3.844446 53 90000000000,0.453268,3.626142 54 91000000000,0.280310,2.242478 55 92000000000,0.006381,0.051049 56 93000000000,0.030991,0.247932 57 94000000000,0.031981,0.255845 58 95000000000,0.027931,0.223446 59 96000000000,0.063066,0.504529 60 97000000000,0.023847,0.190773 61 98000000000,0.011291,0.090328 62 99000000000,0.024065,0.192518 63 100000000000,0.001964,0.015711 64 """)) 65 66 def test_cpu_process_utilization_per_second(self): 67 return DiffTestBlueprint( 68 trace=DataPath('example_android_trace_30s.pb'), 69 query=""" 70 INCLUDE PERFETTO MODULE linux.cpu.utilization.process; 71 72 SELECT * 73 FROM cpu_process_utilization_per_second(10); 74 """, 75 out=Csv(""" 76 "ts","utilization","unnormalized_utilization" 77 72000000000,0.000187,0.001495 78 73000000000,0.000182,0.001460 79 77000000000,0.000072,0.000579 80 78000000000,0.000275,0.002204 81 82000000000,0.000300,0.002404 82 83000000000,0.000004,0.000034 83 87000000000,0.000133,0.001065 84 88000000000,0.000052,0.000416 85 89000000000,0.000212,0.001697 86 92000000000,0.000207,0.001658 87 97000000000,0.000353,0.002823 88 """)) 89 90 def test_cpu_thread_utilization_per_second(self): 91 return DiffTestBlueprint( 92 trace=DataPath('example_android_trace_30s.pb'), 93 query=""" 94 INCLUDE PERFETTO MODULE linux.cpu.utilization.thread; 95 96 SELECT * 97 FROM cpu_thread_utilization_per_second(10); 98 """, 99 out=Csv(""" 100 "ts","utilization","unnormalized_utilization" 101 70000000000,0.000024,0.000195 102 72000000000,0.000025,0.000200 103 73000000000,0.000053,0.000420 104 74000000000,0.000044,0.000352 105 75000000000,0.000058,0.000461 106 76000000000,0.000075,0.000603 107 77000000000,0.000051,0.000407 108 78000000000,0.000047,0.000374 109 79000000000,0.000049,0.000396 110 80000000000,0.000084,0.000673 111 81000000000,0.000041,0.000329 112 82000000000,0.000048,0.000383 113 83000000000,0.000040,0.000323 114 84000000000,0.000018,0.000145 115 85000000000,0.000053,0.000421 116 86000000000,0.000121,0.000972 117 87000000000,0.000049,0.000392 118 88000000000,0.000036,0.000285 119 89000000000,0.000033,0.000266 120 90000000000,0.000050,0.000401 121 91000000000,0.000025,0.000201 122 92000000000,0.000009,0.000071 123 """)) 124 125 def test_cpu_cycles(self): 126 return DiffTestBlueprint( 127 trace=DataPath('android_postboot_unlock.pftrace'), 128 query=(""" 129 INCLUDE PERFETTO MODULE linux.cpu.utilization.system; 130 131 SELECT 132 millicycles, 133 megacycles, 134 runtime, 135 min_freq, 136 max_freq, 137 avg_freq 138 FROM cpu_cycles; 139 """), 140 out=Csv(""" 141 "millicycles","megacycles","runtime","min_freq","max_freq","avg_freq" 142 36093928491870,36093,17131594098,500000,2850000,2112132 143 """)) 144 145 def test_cpu_cycles_in_interval(self): 146 return DiffTestBlueprint( 147 trace=DataPath('android_cpu_eos.pb'), 148 query=(""" 149 INCLUDE PERFETTO MODULE linux.cpu.utilization.system; 150 151 SELECT 152 millicycles, 153 megacycles, 154 runtime, 155 min_freq, 156 max_freq, 157 avg_freq 158 FROM cpu_cycles_in_interval(TRACE_START(), TRACE_DUR() / 10); 159 """), 160 out=Csv(""" 161 "millicycles","megacycles","runtime","min_freq","max_freq","avg_freq" 162 31636287288,31,76193077,614400,1708800,415998 163 """)) 164 165 def test_cpu_cycles_per_cpu(self): 166 return DiffTestBlueprint( 167 trace=DataPath('android_postboot_unlock.pftrace'), 168 query=(""" 169 INCLUDE PERFETTO MODULE linux.cpu.utilization.system; 170 171 SELECT 172 cpu, 173 millicycles, 174 megacycles, 175 runtime, 176 min_freq, 177 max_freq, 178 avg_freq 179 FROM cpu_cycles_per_cpu; 180 """), 181 out=Csv(""" 182 "cpu","millicycles","megacycles","runtime","min_freq","max_freq","avg_freq" 183 0,4007488375822,4007,2260291804,930000,1803000,1775516 184 1,3985923237512,3985,2247149674,930000,1803000,1776869 185 2,4047926756581,4047,2276274170,930000,1803000,1781496 186 3,3992276081242,3992,2248956757,930000,1803000,1778975 187 4,5134318459625,5134,2203887266,553000,2348000,2335531 188 5,5615703220380,5615,2438499077,553000,2348000,2308698 189 6,4715590442538,4715,1737264802,500000,2850000,2725191 190 7,4594701918170,4594,1719270548,500000,2850000,2685290 191 """)) 192 193 def test_cpu_cycles_per_cpu_in_interval(self): 194 return DiffTestBlueprint( 195 trace=DataPath('android_cpu_eos.pb'), 196 query=(""" 197 INCLUDE PERFETTO MODULE linux.cpu.utilization.system; 198 199 SELECT 200 cpu, 201 millicycles, 202 megacycles, 203 runtime, 204 min_freq, 205 max_freq, 206 avg_freq 207 FROM cpu_cycles_per_cpu_in_interval(TRACE_START(), TRACE_DUR() / 10); 208 """), 209 out=Csv(""" 210 "cpu","millicycles","megacycles","runtime","min_freq","max_freq","avg_freq" 211 0,27811901835,27,50296201,614400,1708800,554220 212 1,2893791427,2,4709947,614400,614400,615831 213 2,177750720,0,3718178,864000,864000,47885 214 3,752843306,0,17468751,614400,864000,43128 215 """)) 216 217 def test_cpu_cycles_per_thread(self): 218 return DiffTestBlueprint( 219 trace=DataPath('android_cpu_eos.pb'), 220 query=(""" 221 INCLUDE PERFETTO MODULE linux.cpu.utilization.thread; 222 223 SELECT 224 utid, 225 millicycles, 226 megacycles, 227 runtime, 228 min_freq, 229 max_freq, 230 avg_freq 231 FROM cpu_cycles_per_thread 232 WHERE utid < 10 233 """), 234 out=Csv(""" 235 "utid","millicycles","megacycles","runtime","min_freq","max_freq","avg_freq" 236 1,39042295612,39,28747861,614400,1708800,1359695 237 2,286312857,0,167552,1708800,1708800,1714448 238 8,124651656403,124,99592232,614400,1708800,1255974 239 """)) 240 241 def test_cpu_cycles_per_thread_in_interval(self): 242 return DiffTestBlueprint( 243 trace=DataPath('android_cpu_eos.pb'), 244 query=(""" 245 INCLUDE PERFETTO MODULE linux.cpu.utilization.thread; 246 247 SELECT 248 utid, 249 millicycles, 250 megacycles, 251 runtime, 252 min_freq, 253 max_freq, 254 avg_freq 255 FROM cpu_cycles_per_thread_in_interval(TRACE_START(), TRACE_DUR() / 10) 256 WHERE utid < 100 257 """), 258 out=Csv(""" 259 "utid","millicycles","megacycles","runtime","min_freq","max_freq","avg_freq" 260 1,1226879384,1,1996874,614400,614400,614669 261 14,1247778191,1,2446930,614400,614400,513911 262 15,1407232193,1,2384063,614400,614400,593768 263 16,505278870,0,1142238,614400,614400,444396 264 30,29888102,0,48646,614400,614400,622668 265 37,"[NULL]","[NULL]",222814,"[NULL]","[NULL]","[NULL]" 266 38,"[NULL]","[NULL]",2915520,"[NULL]","[NULL]","[NULL]" 267 45,"[NULL]","[NULL]",2744688,"[NULL]","[NULL]","[NULL]" 268 54,"[NULL]","[NULL]",8614114,"[NULL]","[NULL]","[NULL]" 269 61,151616101,0,246771,614400,614400,618841 270 62,58740000,0,8307552,1708800,1708800,7071 271 92,243675648,0,962397,864000,864000,255157 272 """)) 273 274 def test_cpu_cycles_per_process(self): 275 return DiffTestBlueprint( 276 trace=DataPath('android_cpu_eos.pb'), 277 query=(""" 278 INCLUDE PERFETTO MODULE linux.cpu.utilization.process; 279 280 SELECT 281 upid, 282 millicycles, 283 megacycles, 284 runtime, 285 min_freq, 286 max_freq, 287 avg_freq 288 FROM cpu_cycles_per_process 289 WHERE upid < 10 290 """), 291 out=Csv(""" 292 "upid","millicycles","megacycles","runtime","min_freq","max_freq","avg_freq" 293 1,79550724630,79,56977346,614400,1708800,1398005 294 2,286312857,0,167552,1708800,1708800,1714448 295 8,124651656403,124,99592232,614400,1708800,1255974 296 """)) 297 298 def test_cpu_cycles_per_process_in_interval(self): 299 return DiffTestBlueprint( 300 trace=DataPath('android_cpu_eos.pb'), 301 query=(""" 302 INCLUDE PERFETTO MODULE linux.cpu.utilization.process; 303 304 SELECT 305 upid, 306 millicycles, 307 megacycles, 308 runtime, 309 min_freq, 310 max_freq, 311 avg_freq 312 FROM cpu_cycles_per_process_in_interval(TRACE_START(), TRACE_DUR() / 10) 313 WHERE upid < 30; 314 """), 315 out=Csv(""" 316 "upid","millicycles","megacycles","runtime","min_freq","max_freq","avg_freq" 317 1,2163648305,2,3521563,614400,614400,614672 318 14,1247778191,1,2446930,614400,614400,513911 319 15,1407232193,1,2384063,614400,614400,593768 320 16,505278870,0,1142238,614400,614400,444396 321 """)) 322 323 def test_cpu_cycles_per_thread_slice(self): 324 return DiffTestBlueprint( 325 trace=DataPath('android_postboot_unlock.pftrace'), 326 query=(""" 327 INCLUDE PERFETTO MODULE linux.cpu.utilization.slice; 328 329 SELECT 330 id, 331 utid, 332 millicycles, 333 megacycles 334 FROM cpu_cycles_per_thread_slice 335 WHERE millicycles IS NOT NULL 336 LIMIT 10 337 """), 338 out=Csv(""" 339 "id","utid","millicycles","megacycles" 340 125,6,6375728,0 341 126,6,8699728,0 342 128,6,5565648,0 343 129,6,5565648,0 344 214,6,7132688,0 345 270,6,662972400,0 346 271,6,58483872,0 347 274,6,571785696,0 348 277,6,206411922,0 349 278,6,190908162,0 350 """)) 351 352 def test_cpu_cycles_per_thread_slice(self): 353 return DiffTestBlueprint( 354 trace=DataPath('android_postboot_unlock.pftrace'), 355 query=(""" 356 INCLUDE PERFETTO MODULE linux.cpu.utilization.slice; 357 358 SELECT 359 id, 360 utid, 361 millicycles, 362 megacycles 363 FROM cpu_cycles_per_thread_slice_in_interval(TRACE_START(), TRACE_DUR() / 10) 364 WHERE millicycles IS NOT NULL 365 LIMIT 10 366 """), 367 out=Csv(""" 368 "id","utid","millicycles","megacycles" 369 110,17,13022368,0 370 121,17,9618704,0 371 125,6,6375728,0 372 126,6,8699728,0 373 128,6,5565648,0 374 129,6,5565648,0 375 146,24,6916224,0 376 151,26,5296064,0 377 203,17,150060016,0 378 214,6,7132688,0 379 """)) 380 381 # Test CPU frequency counter grouping. 382 def test_cpu_eos_counters_freq(self): 383 return DiffTestBlueprint( 384 trace=DataPath('android_cpu_eos.pb'), 385 query=(""" 386 INCLUDE PERFETTO MODULE linux.cpu.frequency; 387 select 388 freq, 389 cpu, 390 sum(dur) as dur 391 from cpu_frequency_counters 392 GROUP BY freq, cpu 393 """), 394 out=Csv(""" 395 "freq","cpu","dur" 396 614400,0,4755967239 397 614400,1,4755971561 398 614400,2,4755968228 399 614400,3,4755964320 400 864000,0,442371195 401 864000,1,442397134 402 864000,2,442417916 403 864000,3,442434530 404 1363200,0,897122398 405 1363200,1,897144167 406 1363200,2,897180154 407 1363200,3,897216772 408 1708800,0,2553979530 409 1708800,1,2553923073 410 1708800,2,2553866772 411 1708800,3,2553814688 412 """)) 413 414 # Test CPU idle state counter grouping. 415 def test_cpu_eos_counters_idle(self): 416 return DiffTestBlueprint( 417 trace=DataPath('android_cpu_eos.pb'), 418 query=(""" 419 INCLUDE PERFETTO MODULE linux.cpu.idle; 420 select 421 idle, 422 cpu, 423 sum(dur) as dur 424 from cpu_idle_counters 425 GROUP BY idle, cpu 426 """), 427 out=Csv(""" 428 "idle","cpu","dur" 429 -1,0,2839828332 430 -1,1,1977033843 431 -1,2,1800498713 432 -1,3,1884366297 433 0,0,1833971336 434 0,1,2285260950 435 0,2,1348416182 436 0,3,1338508968 437 1,0,4013820433 438 1,1,4386917600 439 1,2,5532102915 440 1,3,5462026920 441 """)) 442 443 def test_linux_cpu_idle_stats(self): 444 return DiffTestBlueprint( 445 trace=TextProto(r""" 446 packet { 447 ftrace_events { 448 cpu: 0 449 event: { 450 timestamp: 200000000000 451 pid: 2 452 cpu_frequency: { 453 state : 1704000 454 cpu_id: 0 455 } 456 } 457 event: { 458 timestamp: 200000000000 459 pid: 2 460 cpu_idle: { 461 state: 4294967295 462 cpu_id: 0 463 } 464 } 465 event { 466 timestamp: 200001000000 467 pid: 2 468 cpu_idle: { 469 state : 1 470 cpu_id: 0 471 } 472 } 473 event: { 474 timestamp: 200002000000 475 pid : 2 476 cpu_idle: { 477 state : 4294967295 478 cpu_id: 0 479 } 480 } 481 event { 482 timestamp: 200003000000 483 pid: 2 484 cpu_idle: { 485 state : 1 486 cpu_id: 0 487 } 488 } 489 event: { 490 timestamp: 200004000000 491 pid: 2 492 cpu_idle: { 493 state : 4294967295 494 cpu_id: 0 495 } 496 } 497 event: { 498 timestamp: 200005000000 499 pid: 2 500 cpu_frequency: { 501 state: 300000 502 cpu_id: 0 503 } 504 } 505 } 506 trusted_uid: 9999 507 trusted_packet_sequence_id: 2 508 } 509 """), 510 query=""" 511 INCLUDE PERFETTO MODULE linux.cpu.idle_stats; 512 SELECT * FROM cpu_idle_stats; 513 """, 514 out=Csv(""" 515 "cpu","state","count","dur","avg_dur","idle_percent" 516 0,2,2,2000000,1000000,40.000000 517 """)) 518 519 def test_linux_per_cpu_idle_time_in_state(self): 520 return DiffTestBlueprint( 521 trace=TextProto(r""" 522 packet { 523 sys_stats { 524 cpuidle_state { 525 cpu_id: 0 526 cpuidle_state_entry { 527 state: "C8" 528 duration_us: 1000000 529 } 530 } 531 } 532 timestamp: 200000000000 533 trusted_packet_sequence_id: 2 534 } 535 packet { 536 sys_stats { 537 cpuidle_state { 538 cpu_id: 1 539 cpuidle_state_entry { 540 state: "C8" 541 duration_us: 1000000 542 } 543 } 544 } 545 timestamp: 200000000000 546 trusted_packet_sequence_id: 2 547 } 548 packet { 549 sys_stats { 550 cpuidle_state { 551 cpu_id: 0 552 cpuidle_state_entry { 553 state: "C8" 554 duration_us: 1000100 555 } 556 } 557 } 558 timestamp: 200001000000 559 trusted_packet_sequence_id: 2 560 } 561 packet { 562 sys_stats { 563 cpuidle_state { 564 cpu_id: 1 565 cpuidle_state_entry { 566 state: "C8" 567 duration_us: 1000100 568 } 569 } 570 } 571 timestamp: 200001000000 572 trusted_packet_sequence_id: 2 573 } 574 packet { 575 sys_stats { 576 cpuidle_state { 577 cpu_id: 0 578 cpuidle_state_entry { 579 state: "C8" 580 duration_us: 1000200 581 } 582 } 583 } 584 timestamp: 200002000000 585 trusted_packet_sequence_id: 2 586 } 587 packet { 588 sys_stats { 589 cpuidle_state { 590 cpu_id: 1 591 cpuidle_state_entry { 592 state: "C8" 593 duration_us: 1000200 594 } 595 } 596 } 597 timestamp: 200002000000 598 trusted_packet_sequence_id: 2 599 } 600 """), 601 query=""" 602 INCLUDE PERFETTO MODULE linux.cpu.idle_time_in_state; 603 SELECT * FROM linux_per_cpu_idle_time_in_state_counters; 604 """, 605 out=Csv(""" 606 "ts","machine_id","state","cpu","idle_percentage","total_residency","time_slice" 607 200001000000,"[NULL]","C8",0,10.000000,100.000000,1000 608 200002000000,"[NULL]","C8",0,10.000000,100.000000,1000 609 200001000000,"[NULL]","C8",1,10.000000,100.000000,1000 610 200002000000,"[NULL]","C8",1,10.000000,100.000000,1000 611 200001000000,"[NULL]","C0",0,90.000000,900.000000,1000 612 200001000000,"[NULL]","C0",1,90.000000,900.000000,1000 613 200002000000,"[NULL]","C0",0,90.000000,900.000000,1000 614 200002000000,"[NULL]","C0",1,90.000000,900.000000,1000 615 """)) 616 617 def test_linux_cpu_idle_time_in_state(self): 618 return DiffTestBlueprint( 619 trace=TextProto(r""" 620 packet { 621 sys_stats { 622 cpuidle_state { 623 cpu_id: 0 624 cpuidle_state_entry { 625 state: "C8" 626 duration_us: 1000000 627 } 628 } 629 } 630 timestamp: 200000000000 631 trusted_packet_sequence_id: 2 632 } 633 packet { 634 sys_stats { 635 cpuidle_state { 636 cpu_id: 0 637 cpuidle_state_entry { 638 state: "C8" 639 duration_us: 1000100 640 } 641 } 642 } 643 timestamp: 200001000000 644 trusted_packet_sequence_id: 2 645 } 646 packet { 647 sys_stats { 648 cpuidle_state { 649 cpu_id: 0 650 cpuidle_state_entry { 651 state: "C8" 652 duration_us: 1000200 653 } 654 } 655 } 656 timestamp: 200002000000 657 trusted_packet_sequence_id: 2 658 } 659 """), 660 query=""" 661 INCLUDE PERFETTO MODULE linux.cpu.idle_time_in_state; 662 SELECT * FROM linux_cpu_idle_time_in_state_counters; 663 """, 664 out=Csv(""" 665 "ts","machine_id","state","idle_percentage","total_residency","time_slice" 666 200001000000,"[NULL]","C0",90.000000,900.000000,1000 667 200001000000,"[NULL]","C8",10.000000,100.000000,1000 668 200002000000,"[NULL]","C0",90.000000,900.000000,1000 669 200002000000,"[NULL]","C8",10.000000,100.000000,1000 670 """)) 671