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 Memory(TestSuite): 23 # Contains test for Android memory metrics. ION metric 24 def test_android_ion(self): 25 return DiffTestBlueprint( 26 trace=Path('android_ion.py'), 27 query=Metric('android_ion'), 28 out=TextProto(r""" 29 android_ion { 30 buffer { 31 name: "adsp" 32 avg_size_bytes: 1000.0 33 min_size_bytes: 1000.0 34 max_size_bytes: 1100.0 35 total_alloc_size_bytes: 1100.0 36 } 37 buffer { 38 name: "system" 39 avg_size_bytes: 1497.4874371859296 40 min_size_bytes: 1000.0 41 max_size_bytes: 2000.0 42 total_alloc_size_bytes: 2000.0 43 } 44 } 45 """)) 46 47 def test_android_ion_stat(self): 48 return DiffTestBlueprint( 49 trace=TextProto(r""" 50 packet { 51 ftrace_events { 52 cpu: 0 53 event { 54 timestamp: 100 55 pid: 1 56 ion_stat { 57 buffer_id: 123 58 len: 1000 59 total_allocated: 2000 60 } 61 } 62 } 63 } 64 packet { 65 ftrace_events { 66 cpu: 0 67 event { 68 timestamp: 200 69 pid: 1 70 ion_stat { 71 buffer_id: 123 72 len: -1000 73 total_allocated: 1000 74 } 75 } 76 } 77 } 78 """), 79 query=Metric('android_ion'), 80 out=TextProto(r""" 81 android_ion { 82 buffer { 83 name: "all" 84 avg_size_bytes: 2000.0 85 min_size_bytes: 1000.0 86 max_size_bytes: 2000.0 87 total_alloc_size_bytes: 1000.0 88 } 89 } 90 """)) 91 92 # DMA-BUF heap Metric 93 def test_android_dma_heap_stat(self): 94 return DiffTestBlueprint( 95 trace=TextProto(r""" 96 packet { 97 ftrace_events { 98 cpu: 0 99 event { 100 timestamp: 100 101 pid: 1 102 dma_heap_stat { 103 inode: 123 104 len: 1024 105 total_allocated: 2048 106 } 107 } 108 } 109 } 110 packet { 111 ftrace_events { 112 cpu: 0 113 event { 114 timestamp: 200 115 pid: 1 116 dma_heap_stat { 117 inode: 123 118 len: -1024 119 total_allocated: 1024 120 } 121 } 122 } 123 } 124 """), 125 query=Metric('android_dma_heap'), 126 out=TextProto(r""" 127 android_dma_heap { 128 avg_size_bytes: 2048.0 129 min_size_bytes: 1024.0 130 max_size_bytes: 2048.0 131 total_alloc_size_bytes: 1024.0 132 } 133 """)) 134 135 def test_android_dma_buffer_tracks(self): 136 return DiffTestBlueprint( 137 trace=TextProto(r""" 138 packet { 139 ftrace_events { 140 cpu: 0 141 event { 142 timestamp: 100 143 pid: 1 144 dma_heap_stat { 145 inode: 123 146 len: 1024 147 total_allocated: 2048 148 } 149 } 150 } 151 } 152 packet { 153 ftrace_events { 154 cpu: 0 155 event { 156 timestamp: 200 157 pid: 1 158 dma_heap_stat { 159 inode: 123 160 len: -1024 161 total_allocated: 1024 162 } 163 } 164 } 165 } 166 """), 167 query=""" 168 SELECT track.name, slice.ts, slice.dur, slice.name 169 FROM slice JOIN track ON slice.track_id = track.id 170 WHERE track.name = 'mem.dma_buffer'; 171 """, 172 out=Csv(""" 173 "name","ts","dur","name" 174 "mem.dma_buffer",100,100,"1 kB" 175 """)) 176 177 # fastrpc metric 178 def test_android_fastrpc_dma_stat(self): 179 return DiffTestBlueprint( 180 trace=TextProto(r""" 181 packet { 182 ftrace_events { 183 cpu: 0 184 event { 185 timestamp: 100 186 pid: 1 187 fastrpc_dma_stat { 188 cid: 1 189 len: 1000 190 total_allocated: 2000 191 } 192 } 193 } 194 } 195 packet { 196 ftrace_events { 197 cpu: 0 198 event { 199 timestamp: 200 200 pid: 1 201 fastrpc_dma_stat { 202 cid: 1 203 len: -1000 204 total_allocated: 1000 205 } 206 } 207 } 208 } 209 """), 210 query=Metric('android_fastrpc'), 211 out=TextProto(r""" 212 android_fastrpc { 213 subsystem { 214 name: "MDSP" 215 avg_size_bytes: 2000.0 216 min_size_bytes: 1000.0 217 max_size_bytes: 2000.0 218 total_alloc_size_bytes: 1000.0 219 } 220 } 221 """)) 222 223 # shrink slab 224 def test_shrink_slab(self): 225 return DiffTestBlueprint( 226 trace=TextProto(r""" 227 packet { 228 ftrace_events { 229 cpu: 7 230 event { 231 timestamp: 36448185787847 232 pid: 156 233 mm_shrink_slab_start { 234 cache_items: 1 235 delta: 0 236 gfp_flags: 3264 237 nr_objects_to_shrink: 0 238 shr: 18446743882920355600 239 shrink: 90 240 total_scan: 0 241 nid: 0 242 priority: 12 243 } 244 } 245 } 246 } 247 packet { 248 ftrace_events { 249 cpu: 7 250 event { 251 timestamp: 36448185788539 252 pid: 156 253 mm_shrink_slab_end { 254 new_scan: 0 255 retval: 0 256 shr: 18446743882920355600 257 shrink: 90 258 total_scan: 0 259 unused_scan: 0 260 nid: 0 261 } 262 } 263 } 264 } 265 """), 266 query=""" 267 SELECT ts, dur, name FROM slice WHERE name = 'mm_vmscan_shrink_slab'; 268 """, 269 out=Csv(""" 270 "ts","dur","name" 271 36448185787847,692,"mm_vmscan_shrink_slab" 272 """)) 273 274 # cma alloc 275 def test_cma(self): 276 return DiffTestBlueprint( 277 trace=TextProto(r""" 278 packet { 279 system_info { 280 utsname { 281 sysname: "Linux" 282 release: "5.10.0" 283 } 284 } 285 } 286 packet { 287 ftrace_events { 288 cpu: 4 289 event { 290 timestamp: 74288080958099 291 pid: 537 292 cma_alloc_start { 293 align: 4 294 count: 6592 295 name: "farawimg" 296 } 297 } 298 event { 299 timestamp: 74288191109751 300 pid: 537 301 cma_alloc_info { 302 align: 4 303 count: 6592 304 err_iso: 0 305 err_mig: 0 306 err_test: 0 307 name: "farawimg" 308 nr_mapped: 832596 309 nr_migrated: 6365 310 nr_reclaimed: 7 311 pfn: 10365824 312 } 313 } 314 } 315 } 316 """), 317 query=""" 318 SELECT ts, dur, name FROM slice WHERE name = 'mm_cma_alloc'; 319 """, 320 out=Csv(""" 321 "ts","dur","name" 322 74288080958099,110151652,"mm_cma_alloc" 323 """)) 324