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 17from python.generators.diff_tests.testing import Csv 18from python.generators.diff_tests.testing import DiffTestBlueprint 19from python.generators.diff_tests.testing import TestSuite 20 21 22class StdlibSched(TestSuite): 23 24 def test_runnable_thread_count(self): 25 return DiffTestBlueprint( 26 trace=Path('../../common/synth_1.py'), 27 query=""" 28 INCLUDE PERFETTO MODULE sched.thread_level_parallelism; 29 SELECT * FROM sched_runnable_thread_count; 30 """, 31 out=Csv(""" 32 "ts","runnable_thread_count" 33 1,1 34 50,1 35 100,2 36 115,2 37 120,2 38 170,3 39 250,2 40 390,2 41 """)) 42 43 def test_active_cpu_count(self): 44 return DiffTestBlueprint( 45 trace=Path('../../common/synth_1.py'), 46 query=""" 47 INCLUDE PERFETTO MODULE sched.thread_level_parallelism; 48 49 SELECT * FROM sched_active_cpu_count; 50 """, 51 out=Csv(""" 52 "ts","active_cpu_count" 53 1,1 54 50,2 55 100,2 56 115,2 57 120,2 58 170,1 59 250,2 60 390,2 61 """)) 62 63 def test_sched_time_in_state_for_thread(self): 64 return DiffTestBlueprint( 65 trace=DataPath('example_android_trace_30s.pb'), 66 query=""" 67 INCLUDE PERFETTO MODULE sched.time_in_state; 68 69 SELECT * 70 FROM sched_time_in_state_for_thread 71 ORDER BY utid, state 72 LIMIT 10; 73 """, 74 out=Csv(""" 75 "utid","total_runtime","state","time_in_state","percentage_in_state" 76 1,27540674878,"D",596720,0 77 1,27540674878,"R",1988438,0 78 1,27540674878,"R+",2435415,0 79 1,27540674878,"Running",23098223,0 80 1,27540674878,"S",27512556082,99 81 2,27761417087,"D",833039830,3 82 2,27761417087,"R+",2931096,0 83 2,27761417087,"Running",92350845,0 84 2,27761417087,"S",26833095316,96 85 3,29374171050,"R",140800325,0 86 """)) 87 88 def test_sched_percentage_of_time_in_state(self): 89 return DiffTestBlueprint( 90 trace=DataPath('example_android_trace_30s.pb'), 91 query=""" 92 INCLUDE PERFETTO MODULE sched.time_in_state; 93 94 SELECT * 95 FROM sched_percentage_of_time_in_state 96 ORDER BY utid 97 LIMIT 10; 98 """, 99 out=Csv(""" 100 "utid","running","runnable","runnable_preempted","sleeping","uninterruptible_sleep","other" 101 1,0,0,0,99,0,"[NULL]" 102 2,0,"[NULL]",0,96,3,"[NULL]" 103 3,5,0,0,93,"[NULL]","[NULL]" 104 4,100,"[NULL]","[NULL]","[NULL]","[NULL]",0 105 5,0,0,0,99,0,"[NULL]" 106 6,0,"[NULL]",0,99,"[NULL]","[NULL]" 107 7,0,0,0,99,"[NULL]","[NULL]" 108 8,0,0,0,98,0,"[NULL]" 109 9,0,"[NULL]","[NULL]",99,"[NULL]","[NULL]" 110 10,0,"[NULL]",0,99,"[NULL]","[NULL]" 111 """)) 112 113 def test_sched_time_in_state_for_thread_in_interval(self): 114 return DiffTestBlueprint( 115 trace=DataPath('example_android_trace_30s.pb'), 116 query=""" 117 INCLUDE PERFETTO MODULE sched.time_in_state; 118 119 SELECT * 120 FROM sched_time_in_state_for_thread_in_interval(71039311397, 10000000000, 44); 121 """, 122 out=Csv(""" 123 "state","io_wait","blocked_function","dur" 124 "S","[NULL]","[NULL]",9994400675 125 "Running","[NULL]","[NULL]",4655524 126 "D","[NULL]","[NULL]",563645 127 "R+","[NULL]","[NULL]",380156 128 """)) 129 130 def test_sched_time_in_state_and_cpu_for_thread_in_interval(self): 131 return DiffTestBlueprint( 132 trace=DataPath('example_android_trace_30s.pb'), 133 query=""" 134 INCLUDE PERFETTO MODULE sched.time_in_state; 135 136 SELECT * 137 FROM sched_time_in_state_and_cpu_for_thread_in_interval(71039311397, 10000000000, 44); 138 """, 139 out=Csv(""" 140 "state","io_wait","cpu","blocked_function","dur" 141 "S","[NULL]","[NULL]","[NULL]",9994400675 142 "Running","[NULL]",2,"[NULL]",4655524 143 "D","[NULL]","[NULL]","[NULL]",563645 144 "R+","[NULL]","[NULL]","[NULL]",380156 145 """)) 146 147 def test_sched_time_in_state_for_cpu_in_interval(self): 148 return DiffTestBlueprint( 149 trace=DataPath('example_android_trace_30s.pb'), 150 query=""" 151 INCLUDE PERFETTO MODULE sched.time_in_state; 152 SELECT * FROM 153 sched_time_in_state_for_cpu_in_interval(1, TRACE_START(), TRACE_DUR()); 154 """, 155 out=Csv(""" 156 "end_state","dur" 157 "D",311982601 158 "DK",31103960 159 "R",23230879715 160 "R+",1148673560 161 "S",3868233011 162 "x",35240577 163 """)) 164 165 def test_sched_previous_runnable_on_thread(self): 166 return DiffTestBlueprint( 167 trace=DataPath('android_boot.pftrace'), 168 query=""" 169 INCLUDE PERFETTO MODULE sched.runnable; 170 171 SELECT * 172 FROM sched_previous_runnable_on_thread 173 WHERE prev_wakeup_runnable_id IS NOT NULL 174 ORDER BY id DESC 175 LIMIT 10; 176 """, 177 out=Csv(""" 178 "id","prev_runnable_id","prev_wakeup_runnable_id" 179 538199,538191,538191 180 538197,538191,538191 181 538195,538191,538191 182 538190,538136,538136 183 538188,538088,533235 184 538184,538176,524613 185 538181,538178,537492 186 538179,524619,524619 187 538177,537492,537492 188 538175,538174,524613 189 """)) 190 191 def test_sched_latency(self): 192 return DiffTestBlueprint( 193 trace=DataPath('android_boot.pftrace'), 194 query=""" 195 INCLUDE PERFETTO MODULE sched.latency; 196 197 SELECT 198 thread_state_id, 199 sched_id, 200 utid, 201 runnable_latency_id, 202 latency_dur 203 FROM sched_latency_for_running_interval 204 ORDER BY thread_state_id DESC 205 LIMIT 10; 206 """, 207 out=Csv(""" 208 "thread_state_id","sched_id","utid","runnable_latency_id","latency_dur" 209 538199,269427,2,538191,91919 210 538197,269425,2,538191,91919 211 538195,269423,2,538191,91919 212 538190,269422,1330,538136,1437215 213 538188,269420,2,538088,826823 214 538184,269419,91,538176,131388 215 538181,269418,319,538178,4883 216 538179,269417,1022,524619,469849 217 538177,269416,319,537492,670736 218 538175,269415,91,538174,12532 219 """)) 220