• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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, Systrace
17from python.generators.diff_tests.testing import Csv, Json, TextProto, BinaryProto
18from python.generators.diff_tests.testing import DiffTestBlueprint
19from python.generators.diff_tests.testing import TestSuite
20from python.generators.diff_tests.testing import PrintProfileProto
21
22
23class AndroidMetrics(TestSuite):
24
25  def test_android_network_activity(self):
26    # The following should have three activity regions:
27    # * uid=123 from 1000 to 2010 (note: end is max(ts)+idle_ns)
28    # * uid=456 from 1005 to 3115 (note: doesn't group with above due to name)
29    #   * Also tests that groups form based on (ts+dur), not just start ts.
30    # * uid=123 from 3000 to 5500 (note: gap between 1010 to 3000 > idle_ns)
31    # Note: packet_timestamps are delta encoded from the base timestamp.
32    return DiffTestBlueprint(
33        trace=TextProto(r"""
34        packet {
35          timestamp: 0
36          network_packet_bundle {
37            ctx {
38              direction: DIR_EGRESS
39              interface: "wlan"
40              uid: 123
41            }
42            packet_timestamps: [
43              1000, 1010,
44              3000, 3050, 4000, 4500
45            ],
46            packet_lengths: [
47              50, 50,
48              50, 50, 50, 50
49            ],
50          }
51        }
52        packet {
53          timestamp: 1005
54          network_packet_bundle {
55            ctx {
56              direction: DIR_EGRESS
57              interface: "wlan"
58              uid: 456
59            }
60            total_duration: 100
61            total_packets: 2
62            total_length: 300
63          }
64        }
65        packet {
66          timestamp: 2015
67          network_packet_bundle {
68            ctx {
69              direction: DIR_EGRESS
70              interface: "wlan"
71              uid: 456
72            }
73            total_duration: 100
74            total_packets: 1
75            total_length: 50
76          }
77        }
78        packet {
79          timestamp: 0
80          network_packet_bundle {
81            ctx {
82              direction: DIR_INGRESS
83              interface: "loopback"
84              uid: 123
85            }
86            packet_timestamps: [6000]
87            packet_lengths: [100]
88          }
89        }
90        """),
91        query="""
92        SELECT RUN_METRIC(
93          'android/network_activity_template.sql',
94          'view_name', 'android_network_activity',
95          'group_by',  'package_name',
96          'filter',    'iface = "wlan"',
97          'idle_ns',   '1000',
98          'quant_ns',  '100'
99        );
100
101        SELECT * FROM android_network_activity
102        ORDER BY package_name, ts;
103        """,
104        out=Path('android_network_activity.out'))
105
106  def test_anr_metric(self):
107    return DiffTestBlueprint(
108        trace=Path('android_anr_metric.py'),
109        query=Metric('android_anr'),
110        out=Path('android_anr_metric.out'))
111
112  def test_binder_metric(self):
113    return DiffTestBlueprint(
114        trace=DataPath('sched_wakeup_trace.atr'),
115        query=Metric('android_binder'),
116        out=Path('android_binder_metric.out'))
117
118  def test_android_blocking_calls_cuj(self):
119    return DiffTestBlueprint(
120        trace=Path('android_blocking_calls_cuj_metric.py'),
121        query=Metric('android_blocking_calls_cuj_metric'),
122        out=Path('android_blocking_calls_cuj_metric.out'))
123
124  def test_android_blocking_calls_unagg(self):
125    return DiffTestBlueprint(
126        trace=Path('android_blocking_calls_cuj_metric.py'),
127        query=Metric('android_blocking_calls_unagg'),
128        out=Path('android_blocking_calls_unagg.out'))
129
130  def test_android_blocking_calls_on_jank_cujs(self):
131    return DiffTestBlueprint(
132        trace=Path('../graphics/android_jank_cuj.py'),
133        query=Metric('android_blocking_calls_cuj_metric'),
134        out=Path('android_blocking_calls_on_jank_cuj_metric.out'))
135
136  def test_android_sysui_notifications_blocking_calls(self):
137    return DiffTestBlueprint(
138        trace=Path('android_sysui_notifications_blocking_calls_metric.py'),
139        query=Metric('android_sysui_notifications_blocking_calls_metric'),
140        out=Path('android_sysui_notifications_blocking_calls_metric.out'))
141
142  def test_sysui_notif_shade_list_builder(self):
143    return DiffTestBlueprint(
144        trace=Path('android_sysui_notif_shade_list_builder_metric.py'),
145        query=Metric('sysui_notif_shade_list_builder_metric'),
146        out=Path('sysui_notif_shade_list_builder_metric.out'))
147
148  def test_sysui_update_notif_on_ui_mode_changed(self):
149    return DiffTestBlueprint(
150        trace=Path('sysui_update_notif_on_ui_mode_changed_metric.py'),
151        query=Metric('sysui_update_notif_on_ui_mode_changed_metric'),
152        out=Path('sysui_update_notif_on_ui_mode_changed_metric.out'))
153
154  def test_monitor_contention_metric(self):
155    return DiffTestBlueprint(
156        trace=DataPath('android_monitor_contention_trace.atr'),
157        query=Metric('android_monitor_contention'),
158        out=Path('android_monitor_contention.out'))
159
160  def test_monitor_contention_agg_metric(self):
161    return DiffTestBlueprint(
162        trace=DataPath('android_monitor_contention_trace.atr'),
163        query=Metric('android_monitor_contention_agg'),
164        out=TextProto(r"""
165        android_monitor_contention_agg {
166          process_aggregation {
167            name: "android.process.media"
168            total_contention_count: 12
169            total_contention_dur: 12893198
170            main_thread_contention_count: 12
171            main_thread_contention_dur: 12893198
172          }
173          process_aggregation {
174            name: "com.android.providers.media.module"
175            total_contention_count: 7
176            total_contention_dur: 169793
177          }
178          process_aggregation {
179            name: "com.android.systemui"
180            total_contention_count: 8
181            total_contention_dur: 9445959
182            main_thread_contention_count: 5
183            main_thread_contention_dur: 9228582
184          }
185          process_aggregation {
186            name: "system_server"
187            total_contention_count: 354
188            total_contention_dur: 358898613
189            main_thread_contention_count: 27
190            main_thread_contention_dur: 36904702
191          }
192        }
193        """))
194
195  def test_android_boot(self):
196    return DiffTestBlueprint(
197        trace=DataPath('android_postboot_unlock.pftrace'),
198        query=Metric('android_boot'),
199        out=Path('android_boot.out'))
200
201  def test_ad_services_metric(self):
202    return DiffTestBlueprint(
203        trace=Path('ad_services_metric.py'),
204        query=Metric('ad_services_metric'),
205        out=TextProto(r"""
206         ad_services_metric {
207           ui_metric {
208             consent_manager_initialization_latency: 0.0003
209             consent_manager_read_latency: 0.00015
210           }
211           app_set_id_metric {
212             latency: 0.0001
213           }
214           ad_id_metric {
215             latency: 0.0003
216           }
217           odp_metric {
218             managing_service_initialization_latency: 0.00005
219             service_delegate_execute_flow_latency: 0.0001
220             service_delegate_request_surface_package_latency: 0.00015
221             service_delegate_register_web_trigger_latency: 0.0002
222           }
223         }
224        """))
225
226  def test_android_boot_unagg(self):
227    return DiffTestBlueprint(
228        trace=DataPath('android_postboot_unlock.pftrace'),
229        query=Metric("android_boot_unagg"),
230        out=Path('android_boot_unagg.out'))
231
232  def test_android_app_process_starts(self):
233    return DiffTestBlueprint(
234        trace=DataPath('android_postboot_unlock.pftrace'),
235        query=Metric("android_app_process_starts"),
236        out=Path('android_app_process_starts.out'))
237
238  def test_android_garbage_collection(self):
239    return DiffTestBlueprint(
240        trace=DataPath('android_postboot_unlock.pftrace'),
241        query=Metric('android_garbage_collection_unagg'),
242        out=Path('android_garbage_collection_unagg.out'))
243
244  def test_android_auto_multiuser_switch(self):
245    return DiffTestBlueprint(
246        trace=TextProto(r"""
247        packet {
248          ftrace_events {
249            cpu: 2
250            event {
251              timestamp: 1000000000
252              pid: 4032
253              print {
254                buf: "S|5993|UserController.startUser-10-fg-start-mode-1|0\n"
255              }
256            }
257          }
258        }
259        packet {
260          ftrace_events {
261            cpu: 2
262            event {
263              timestamp: 2000000000
264              pid: 4065
265              print {
266                buf: "S|2608|launching: com.android.car.carlauncher|0\n"
267              }
268            }
269          }
270        }
271        packet {
272          ftrace_events {
273            cpu: 2
274            event {
275              timestamp: 3000000000
276              pid: 4032
277              print {
278                buf: "S|5993|UserController.startUser-11-fg-start-mode-1|0\n"
279              }
280            }
281          }
282        }
283        packet {
284          ftrace_events {
285            cpu: 2
286            event {
287              timestamp: 6878000000
288              pid: 4065
289              print {
290                buf: "S|2609|launching: com.android.car.carlauncher|0\n"
291              }
292            }
293          }
294        }
295        """),
296        query=Metric('android_auto_multiuser'),
297        out=TextProto(r"""
298       android_auto_multiuser {
299         user_switch {
300            user_id: 11
301            start_event: "UserController.startUser-11-fg-start-mode-1"
302            end_event: "com.android.car.carlauncher"
303            duration_ms: 3877
304            previous_user_info {
305            }
306         }
307       }
308       """))
309
310  def test_android_auto_multiuser_switch_with_previous_user_data(self):
311    return DiffTestBlueprint(
312        trace=Path("android_auto_multiuser.textproto"),
313        query=Metric('android_auto_multiuser'),
314        out=TextProto(r"""
315       android_auto_multiuser {
316         user_switch {
317            user_id: 11
318            start_event: "UserController.startUser-11-fg-start-mode-1"
319            end_event: "com.android.car.carlauncher"
320            duration_ms: 999
321            previous_user_info {
322                user_id: 10
323                total_cpu_time_ms: 9
324                total_memory_usage_kb: 2048
325            }
326         }
327          user_switch {
328             user_id: 11
329             start_event: "UserController.startUser-11-fg-start-mode-1"
330             end_event: "finishUserStopped-10-[stopUser]"
331             duration_ms: 2100
332             previous_user_info {
333                 user_id: 10
334                 total_cpu_time_ms: 19
335                 total_memory_usage_kb: 3072
336             }
337          }
338       }
339       """))
340
341  def test_android_auto_multiuser_timing_table(self):
342    return DiffTestBlueprint(
343        trace=Path("android_auto_multiuser.textproto"),
344        query="""
345        INCLUDE PERFETTO MODULE android.auto.multiuser;
346        SELECT * FROM android_auto_multiuser_timing;
347        """,
348        out=Csv("""
349        "event_start_user_id","event_start_time","event_end_time","event_end_name","event_start_name","duration"
350        "11",3000000000,3999999999,"com.android.car.carlauncher","UserController.startUser-11-fg-start-mode-1",999999999
351        "11",3000000000,5100000000,"finishUserStopped-10-[stopUser]","UserController.startUser-11-fg-start-mode-1",2100000000
352        """))
353
354  def test_android_oom_adjuster(self):
355    return DiffTestBlueprint(
356        trace=DataPath('android_postboot_unlock.pftrace'),
357        query=Metric("android_oom_adjuster"),
358        out=Path('android_oom_adjuster.out'))
359
360  def test_android_broadcasts(self):
361    return DiffTestBlueprint(
362        trace=DataPath('android_postboot_unlock.pftrace'),
363        query=Metric("android_broadcasts"),
364        out=Path('android_broadcasts.out'))
365