1*288bf522SAndroid Build Coastguard Worker# 2*288bf522SAndroid Build Coastguard Worker# Copyright (C) 2024 The Android Open Source Project 3*288bf522SAndroid Build Coastguard Worker# 4*288bf522SAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License"); 5*288bf522SAndroid Build Coastguard Worker# you may not use this file except in compliance with the License. 6*288bf522SAndroid Build Coastguard Worker# You may obtain a copy of the License at 7*288bf522SAndroid Build Coastguard Worker# 8*288bf522SAndroid Build Coastguard Worker# http://www.apache.org/licenses/LICENSE-2.0 9*288bf522SAndroid Build Coastguard Worker# 10*288bf522SAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software 11*288bf522SAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS, 12*288bf522SAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13*288bf522SAndroid Build Coastguard Worker# See the License for the specific language governing permissions and 14*288bf522SAndroid Build Coastguard Worker# limitations under the License. 15*288bf522SAndroid Build Coastguard Worker# 16*288bf522SAndroid Build Coastguard Worker 17*288bf522SAndroid Build Coastguard Workerimport textwrap 18*288bf522SAndroid Build Coastguard Workerfrom validation_error import ValidationError 19*288bf522SAndroid Build Coastguard Worker 20*288bf522SAndroid Build Coastguard WorkerANDROID_SDK_VERSION_T = 33 21*288bf522SAndroid Build Coastguard Worker 22*288bf522SAndroid Build Coastguard Worker 23*288bf522SAndroid Build Coastguard Workerdef create_ftrace_events_string(predefined_ftrace_events, 24*288bf522SAndroid Build Coastguard Worker excluded_ftrace_events, included_ftrace_events): 25*288bf522SAndroid Build Coastguard Worker if excluded_ftrace_events is not None: 26*288bf522SAndroid Build Coastguard Worker for event in excluded_ftrace_events: 27*288bf522SAndroid Build Coastguard Worker if event in predefined_ftrace_events: 28*288bf522SAndroid Build Coastguard Worker predefined_ftrace_events.remove(event) 29*288bf522SAndroid Build Coastguard Worker else: 30*288bf522SAndroid Build Coastguard Worker return None, ValidationError(("Cannot remove ftrace event %s from" 31*288bf522SAndroid Build Coastguard Worker " config because it is not one" 32*288bf522SAndroid Build Coastguard Worker " of the config's ftrace events." 33*288bf522SAndroid Build Coastguard Worker % event), 34*288bf522SAndroid Build Coastguard Worker ("Please specify one of the following" 35*288bf522SAndroid Build Coastguard Worker " possible ftrace events:\n\t %s" 36*288bf522SAndroid Build Coastguard Worker % "\n\t ".join(predefined_ftrace_events))) 37*288bf522SAndroid Build Coastguard Worker 38*288bf522SAndroid Build Coastguard Worker if included_ftrace_events is not None: 39*288bf522SAndroid Build Coastguard Worker for event in included_ftrace_events: 40*288bf522SAndroid Build Coastguard Worker if event not in predefined_ftrace_events: 41*288bf522SAndroid Build Coastguard Worker predefined_ftrace_events.append(event) 42*288bf522SAndroid Build Coastguard Worker else: 43*288bf522SAndroid Build Coastguard Worker return None, ValidationError(("Cannot add ftrace event %s to config" 44*288bf522SAndroid Build Coastguard Worker " because it is already one of the" 45*288bf522SAndroid Build Coastguard Worker " config's ftrace events." % event), 46*288bf522SAndroid Build Coastguard Worker ("Please do not specify any of the" 47*288bf522SAndroid Build Coastguard Worker " following ftrace events that are" 48*288bf522SAndroid Build Coastguard Worker " already included:\n\t %s" 49*288bf522SAndroid Build Coastguard Worker % "\n\t ".join(predefined_ftrace_events))) 50*288bf522SAndroid Build Coastguard Worker 51*288bf522SAndroid Build Coastguard Worker ftrace_events_string = ("ftrace_events: \"%s\"" % ("""\" 52*288bf522SAndroid Build Coastguard Worker ftrace_events: \"""".join(predefined_ftrace_events))) 53*288bf522SAndroid Build Coastguard Worker return ftrace_events_string, None 54*288bf522SAndroid Build Coastguard Worker 55*288bf522SAndroid Build Coastguard Worker 56*288bf522SAndroid Build Coastguard Workerdef build_default_config(command, android_sdk_version): 57*288bf522SAndroid Build Coastguard Worker if command.dur_ms is None: 58*288bf522SAndroid Build Coastguard Worker # This is always defined because it has a default value that is always 59*288bf522SAndroid Build Coastguard Worker # set in torq.py. 60*288bf522SAndroid Build Coastguard Worker raise ValueError("Cannot create config because a valid dur_ms was not set.") 61*288bf522SAndroid Build Coastguard Worker predefined_ftrace_events = [ 62*288bf522SAndroid Build Coastguard Worker "dmabuf_heap/dma_heap_stat", 63*288bf522SAndroid Build Coastguard Worker "ftrace/print", 64*288bf522SAndroid Build Coastguard Worker "gpu_mem/gpu_mem_total", 65*288bf522SAndroid Build Coastguard Worker "ion/ion_stat", 66*288bf522SAndroid Build Coastguard Worker "kmem/ion_heap_grow", 67*288bf522SAndroid Build Coastguard Worker "kmem/ion_heap_shrink", 68*288bf522SAndroid Build Coastguard Worker "kmem/rss_stat", 69*288bf522SAndroid Build Coastguard Worker "lowmemorykiller/lowmemory_kill", 70*288bf522SAndroid Build Coastguard Worker "mm_event/mm_event_record", 71*288bf522SAndroid Build Coastguard Worker "oom/mark_victim", 72*288bf522SAndroid Build Coastguard Worker "oom/oom_score_adj_update", 73*288bf522SAndroid Build Coastguard Worker "power/cpu_frequency", 74*288bf522SAndroid Build Coastguard Worker "power/cpu_idle", 75*288bf522SAndroid Build Coastguard Worker "power/gpu_frequency", 76*288bf522SAndroid Build Coastguard Worker "power/suspend_resume", 77*288bf522SAndroid Build Coastguard Worker "power/wakeup_source_activate", 78*288bf522SAndroid Build Coastguard Worker "power/wakeup_source_deactivate", 79*288bf522SAndroid Build Coastguard Worker "sched/sched_blocked_reason", 80*288bf522SAndroid Build Coastguard Worker "sched/sched_process_exit", 81*288bf522SAndroid Build Coastguard Worker "sched/sched_process_free", 82*288bf522SAndroid Build Coastguard Worker "sched/sched_switch", 83*288bf522SAndroid Build Coastguard Worker "sched/sched_wakeup", 84*288bf522SAndroid Build Coastguard Worker "sched/sched_wakeup_new", 85*288bf522SAndroid Build Coastguard Worker "sched/sched_waking", 86*288bf522SAndroid Build Coastguard Worker "task/task_newtask", 87*288bf522SAndroid Build Coastguard Worker "task/task_rename", 88*288bf522SAndroid Build Coastguard Worker "vmscan/*", 89*288bf522SAndroid Build Coastguard Worker "workqueue/*", 90*288bf522SAndroid Build Coastguard Worker ] 91*288bf522SAndroid Build Coastguard Worker ftrace_events_string, error = create_ftrace_events_string( 92*288bf522SAndroid Build Coastguard Worker predefined_ftrace_events, command.excluded_ftrace_events, 93*288bf522SAndroid Build Coastguard Worker command.included_ftrace_events) 94*288bf522SAndroid Build Coastguard Worker if error is not None: 95*288bf522SAndroid Build Coastguard Worker return None, error 96*288bf522SAndroid Build Coastguard Worker cpufreq_period_string = "cpufreq_period_ms: 500" 97*288bf522SAndroid Build Coastguard Worker if android_sdk_version < ANDROID_SDK_VERSION_T: 98*288bf522SAndroid Build Coastguard Worker cpufreq_period_string = "" 99*288bf522SAndroid Build Coastguard Worker config = f'''\ 100*288bf522SAndroid Build Coastguard Worker <<EOF 101*288bf522SAndroid Build Coastguard Worker 102*288bf522SAndroid Build Coastguard Worker buffers: {{ 103*288bf522SAndroid Build Coastguard Worker size_kb: 4096 104*288bf522SAndroid Build Coastguard Worker fill_policy: RING_BUFFER 105*288bf522SAndroid Build Coastguard Worker }} 106*288bf522SAndroid Build Coastguard Worker buffers {{ 107*288bf522SAndroid Build Coastguard Worker size_kb: 4096 108*288bf522SAndroid Build Coastguard Worker fill_policy: RING_BUFFER 109*288bf522SAndroid Build Coastguard Worker }} 110*288bf522SAndroid Build Coastguard Worker buffers: {{ 111*288bf522SAndroid Build Coastguard Worker size_kb: 260096 112*288bf522SAndroid Build Coastguard Worker fill_policy: RING_BUFFER 113*288bf522SAndroid Build Coastguard Worker }} 114*288bf522SAndroid Build Coastguard Worker 115*288bf522SAndroid Build Coastguard Worker data_sources: {{ 116*288bf522SAndroid Build Coastguard Worker config {{ 117*288bf522SAndroid Build Coastguard Worker name: "linux.process_stats" 118*288bf522SAndroid Build Coastguard Worker process_stats_config {{ 119*288bf522SAndroid Build Coastguard Worker scan_all_processes_on_start: true 120*288bf522SAndroid Build Coastguard Worker }} 121*288bf522SAndroid Build Coastguard Worker }} 122*288bf522SAndroid Build Coastguard Worker }} 123*288bf522SAndroid Build Coastguard Worker 124*288bf522SAndroid Build Coastguard Worker data_sources: {{ 125*288bf522SAndroid Build Coastguard Worker config {{ 126*288bf522SAndroid Build Coastguard Worker name: "android.log" 127*288bf522SAndroid Build Coastguard Worker android_log_config {{ 128*288bf522SAndroid Build Coastguard Worker }} 129*288bf522SAndroid Build Coastguard Worker }} 130*288bf522SAndroid Build Coastguard Worker }} 131*288bf522SAndroid Build Coastguard Worker 132*288bf522SAndroid Build Coastguard Worker data_sources {{ 133*288bf522SAndroid Build Coastguard Worker config {{ 134*288bf522SAndroid Build Coastguard Worker name: "android.packages_list" 135*288bf522SAndroid Build Coastguard Worker }} 136*288bf522SAndroid Build Coastguard Worker }} 137*288bf522SAndroid Build Coastguard Worker 138*288bf522SAndroid Build Coastguard Worker data_sources: {{ 139*288bf522SAndroid Build Coastguard Worker config {{ 140*288bf522SAndroid Build Coastguard Worker name: "linux.sys_stats" 141*288bf522SAndroid Build Coastguard Worker target_buffer: 1 142*288bf522SAndroid Build Coastguard Worker sys_stats_config {{ 143*288bf522SAndroid Build Coastguard Worker stat_period_ms: 500 144*288bf522SAndroid Build Coastguard Worker stat_counters: STAT_CPU_TIMES 145*288bf522SAndroid Build Coastguard Worker stat_counters: STAT_FORK_COUNT 146*288bf522SAndroid Build Coastguard Worker meminfo_period_ms: 1000 147*288bf522SAndroid Build Coastguard Worker meminfo_counters: MEMINFO_ACTIVE_ANON 148*288bf522SAndroid Build Coastguard Worker meminfo_counters: MEMINFO_ACTIVE_FILE 149*288bf522SAndroid Build Coastguard Worker meminfo_counters: MEMINFO_INACTIVE_ANON 150*288bf522SAndroid Build Coastguard Worker meminfo_counters: MEMINFO_INACTIVE_FILE 151*288bf522SAndroid Build Coastguard Worker meminfo_counters: MEMINFO_KERNEL_STACK 152*288bf522SAndroid Build Coastguard Worker meminfo_counters: MEMINFO_MLOCKED 153*288bf522SAndroid Build Coastguard Worker meminfo_counters: MEMINFO_SHMEM 154*288bf522SAndroid Build Coastguard Worker meminfo_counters: MEMINFO_SLAB 155*288bf522SAndroid Build Coastguard Worker meminfo_counters: MEMINFO_SLAB_UNRECLAIMABLE 156*288bf522SAndroid Build Coastguard Worker meminfo_counters: MEMINFO_VMALLOC_USED 157*288bf522SAndroid Build Coastguard Worker meminfo_counters: MEMINFO_MEM_FREE 158*288bf522SAndroid Build Coastguard Worker meminfo_counters: MEMINFO_SWAP_FREE 159*288bf522SAndroid Build Coastguard Worker vmstat_period_ms: 1000 160*288bf522SAndroid Build Coastguard Worker vmstat_counters: VMSTAT_PGFAULT 161*288bf522SAndroid Build Coastguard Worker vmstat_counters: VMSTAT_PGMAJFAULT 162*288bf522SAndroid Build Coastguard Worker vmstat_counters: VMSTAT_PGFREE 163*288bf522SAndroid Build Coastguard Worker vmstat_counters: VMSTAT_PGPGIN 164*288bf522SAndroid Build Coastguard Worker vmstat_counters: VMSTAT_PGPGOUT 165*288bf522SAndroid Build Coastguard Worker vmstat_counters: VMSTAT_PSWPIN 166*288bf522SAndroid Build Coastguard Worker vmstat_counters: VMSTAT_PSWPOUT 167*288bf522SAndroid Build Coastguard Worker vmstat_counters: VMSTAT_PGSCAN_DIRECT 168*288bf522SAndroid Build Coastguard Worker vmstat_counters: VMSTAT_PGSTEAL_DIRECT 169*288bf522SAndroid Build Coastguard Worker vmstat_counters: VMSTAT_PGSCAN_KSWAPD 170*288bf522SAndroid Build Coastguard Worker vmstat_counters: VMSTAT_PGSTEAL_KSWAPD 171*288bf522SAndroid Build Coastguard Worker vmstat_counters: VMSTAT_WORKINGSET_REFAULT 172*288bf522SAndroid Build Coastguard Worker {cpufreq_period_string} 173*288bf522SAndroid Build Coastguard Worker }} 174*288bf522SAndroid Build Coastguard Worker }} 175*288bf522SAndroid Build Coastguard Worker }} 176*288bf522SAndroid Build Coastguard Worker 177*288bf522SAndroid Build Coastguard Worker data_sources: {{ 178*288bf522SAndroid Build Coastguard Worker config {{ 179*288bf522SAndroid Build Coastguard Worker name: "android.surfaceflinger.frametimeline" 180*288bf522SAndroid Build Coastguard Worker target_buffer: 2 181*288bf522SAndroid Build Coastguard Worker }} 182*288bf522SAndroid Build Coastguard Worker }} 183*288bf522SAndroid Build Coastguard Worker 184*288bf522SAndroid Build Coastguard Worker data_sources: {{ 185*288bf522SAndroid Build Coastguard Worker config {{ 186*288bf522SAndroid Build Coastguard Worker name: "linux.ftrace" 187*288bf522SAndroid Build Coastguard Worker target_buffer: 2 188*288bf522SAndroid Build Coastguard Worker ftrace_config {{ 189*288bf522SAndroid Build Coastguard Worker {ftrace_events_string} 190*288bf522SAndroid Build Coastguard Worker atrace_categories: "aidl" 191*288bf522SAndroid Build Coastguard Worker atrace_categories: "am" 192*288bf522SAndroid Build Coastguard Worker atrace_categories: "dalvik" 193*288bf522SAndroid Build Coastguard Worker atrace_categories: "binder_lock" 194*288bf522SAndroid Build Coastguard Worker atrace_categories: "binder_driver" 195*288bf522SAndroid Build Coastguard Worker atrace_categories: "bionic" 196*288bf522SAndroid Build Coastguard Worker atrace_categories: "camera" 197*288bf522SAndroid Build Coastguard Worker atrace_categories: "disk" 198*288bf522SAndroid Build Coastguard Worker atrace_categories: "freq" 199*288bf522SAndroid Build Coastguard Worker atrace_categories: "idle" 200*288bf522SAndroid Build Coastguard Worker atrace_categories: "gfx" 201*288bf522SAndroid Build Coastguard Worker atrace_categories: "hal" 202*288bf522SAndroid Build Coastguard Worker atrace_categories: "input" 203*288bf522SAndroid Build Coastguard Worker atrace_categories: "pm" 204*288bf522SAndroid Build Coastguard Worker atrace_categories: "power" 205*288bf522SAndroid Build Coastguard Worker atrace_categories: "res" 206*288bf522SAndroid Build Coastguard Worker atrace_categories: "rro" 207*288bf522SAndroid Build Coastguard Worker atrace_categories: "sched" 208*288bf522SAndroid Build Coastguard Worker atrace_categories: "sm" 209*288bf522SAndroid Build Coastguard Worker atrace_categories: "ss" 210*288bf522SAndroid Build Coastguard Worker atrace_categories: "thermal" 211*288bf522SAndroid Build Coastguard Worker atrace_categories: "video" 212*288bf522SAndroid Build Coastguard Worker atrace_categories: "view" 213*288bf522SAndroid Build Coastguard Worker atrace_categories: "wm" 214*288bf522SAndroid Build Coastguard Worker atrace_apps: "lmkd" 215*288bf522SAndroid Build Coastguard Worker atrace_apps: "system_server" 216*288bf522SAndroid Build Coastguard Worker atrace_apps: "com.android.systemui" 217*288bf522SAndroid Build Coastguard Worker atrace_apps: "com.google.android.gms" 218*288bf522SAndroid Build Coastguard Worker atrace_apps: "com.google.android.gms.persistent" 219*288bf522SAndroid Build Coastguard Worker atrace_apps: "android:ui" 220*288bf522SAndroid Build Coastguard Worker atrace_apps: "com.google.android.apps.maps" 221*288bf522SAndroid Build Coastguard Worker atrace_apps: "*" 222*288bf522SAndroid Build Coastguard Worker buffer_size_kb: 16384 223*288bf522SAndroid Build Coastguard Worker drain_period_ms: 150 224*288bf522SAndroid Build Coastguard Worker symbolize_ksyms: true 225*288bf522SAndroid Build Coastguard Worker }} 226*288bf522SAndroid Build Coastguard Worker }} 227*288bf522SAndroid Build Coastguard Worker }} 228*288bf522SAndroid Build Coastguard Worker duration_ms: {command.dur_ms} 229*288bf522SAndroid Build Coastguard Worker write_into_file: true 230*288bf522SAndroid Build Coastguard Worker file_write_period_ms: 5000 231*288bf522SAndroid Build Coastguard Worker max_file_size_bytes: 100000000000 232*288bf522SAndroid Build Coastguard Worker flush_period_ms: 5000 233*288bf522SAndroid Build Coastguard Worker incremental_state_config {{ 234*288bf522SAndroid Build Coastguard Worker clear_period_ms: 5000 235*288bf522SAndroid Build Coastguard Worker }} 236*288bf522SAndroid Build Coastguard Worker 237*288bf522SAndroid Build Coastguard Worker EOF''' 238*288bf522SAndroid Build Coastguard Worker return textwrap.dedent(config), None 239*288bf522SAndroid Build Coastguard Worker 240*288bf522SAndroid Build Coastguard Worker 241*288bf522SAndroid Build Coastguard Workerdef build_lightweight_config(command, android_sdk_version): 242*288bf522SAndroid Build Coastguard Worker raise NotImplementedError 243*288bf522SAndroid Build Coastguard Worker 244*288bf522SAndroid Build Coastguard Worker 245*288bf522SAndroid Build Coastguard Workerdef build_memory_config(command, android_sdk_version): 246*288bf522SAndroid Build Coastguard Worker raise NotImplementedError 247*288bf522SAndroid Build Coastguard Worker 248*288bf522SAndroid Build Coastguard Worker 249*288bf522SAndroid Build Coastguard WorkerPREDEFINED_PERFETTO_CONFIGS = { 250*288bf522SAndroid Build Coastguard Worker 'default': build_default_config, 251*288bf522SAndroid Build Coastguard Worker 'lightweight': build_lightweight_config, 252*288bf522SAndroid Build Coastguard Worker 'memory': build_memory_config 253*288bf522SAndroid Build Coastguard Worker} 254*288bf522SAndroid Build Coastguard Worker 255*288bf522SAndroid Build Coastguard Worker 256*288bf522SAndroid Build Coastguard Workerdef build_custom_config(command): 257*288bf522SAndroid Build Coastguard Worker file_content = "" 258*288bf522SAndroid Build Coastguard Worker duration_prefix = "duration_ms:" 259*288bf522SAndroid Build Coastguard Worker appended_duration = duration_prefix + " " + str(command.dur_ms) 260*288bf522SAndroid Build Coastguard Worker try: 261*288bf522SAndroid Build Coastguard Worker with open(command.perfetto_config, "r") as file: 262*288bf522SAndroid Build Coastguard Worker for line in file: 263*288bf522SAndroid Build Coastguard Worker stripped_line = line.strip() 264*288bf522SAndroid Build Coastguard Worker if stripped_line.startswith(duration_prefix): 265*288bf522SAndroid Build Coastguard Worker duration = stripped_line[len(duration_prefix):].strip() 266*288bf522SAndroid Build Coastguard Worker appended_duration = "" 267*288bf522SAndroid Build Coastguard Worker command.dur_ms = int(duration) 268*288bf522SAndroid Build Coastguard Worker file_content += line 269*288bf522SAndroid Build Coastguard Worker except ValueError: 270*288bf522SAndroid Build Coastguard Worker return None, ValidationError(("Failed to parse custom perfetto-config on" 271*288bf522SAndroid Build Coastguard Worker " local file path: %s. Invalid duration_ms" 272*288bf522SAndroid Build Coastguard Worker " field in config." 273*288bf522SAndroid Build Coastguard Worker % command.perfetto_config), 274*288bf522SAndroid Build Coastguard Worker ("Make sure the perfetto config passed via" 275*288bf522SAndroid Build Coastguard Worker " arguments has a valid duration_ms value.")) 276*288bf522SAndroid Build Coastguard Worker except Exception as e: 277*288bf522SAndroid Build Coastguard Worker return None, ValidationError(("Failed to parse custom perfetto-config on" 278*288bf522SAndroid Build Coastguard Worker " local file path: %s. %s" 279*288bf522SAndroid Build Coastguard Worker % (command.perfetto_config, str(e))), None) 280*288bf522SAndroid Build Coastguard Worker config_string = f"<<EOF\n\n{file_content}\n{appended_duration}\n\nEOF" 281*288bf522SAndroid Build Coastguard Worker return config_string, None 282