-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathperf
executable file
·699 lines (583 loc) · 19.6 KB
/
perf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
#!/usr/bin/env python3
import subprocess
import os
import time
import argparse
import re
import signal
import threading
from enum import Enum
from typing import Callable
config_path = "/data/misc/perfetto-traces/perf.proto"
perf_command = [
"adb",
"shell",
"perfetto",
"--txt",
"-o",
"/data/misc/perfetto-traces/trace",
]
basic_ftrace_events = [
'sched/*',
'task/task_newtask',
'task/task_rename',
'power/suspend_resume',
'ftrace/print'
]
basic_atrace_categories = [
'am',
'audio',
'binder_lock',
'binder_driver',
'bionic',
'gfx',
'hal',
'input',
'pm',
'video',
'view',
'webview',
'wm',
]
class DataSourceType(Enum):
PACKAGE_LIST = 1
PROCESS_STATS = 2
SYS_STATS = 3
LOG = 4
FTRACE = 5
class DataSourceBuilder:
def __init__(self, data_source_type, params):
self.data_source_type = data_source_type
self.data_source = ""
self.params = params
self.builder_map = {
DataSourceType.PACKAGE_LIST: self._build_package_list,
DataSourceType.PROCESS_STATS: self._build_package_stats,
DataSourceType.SYS_STATS: self._build_sys_stats,
DataSourceType.LOG: self._build_log,
DataSourceType.FTRACE: self._build_ftrace,
}
def _build_package_list(self):
return """data_sources: {
config {
name: "android.packages_list"
target_buffer: 1
}
}
"""
def _build_package_stats(self):
return """data_sources: {
config {
name: "linux.process_stats"
target_buffer: 1
process_stats_config {
scan_all_processes_on_start: true
}
}
}
"""
def _build_sys_stats(self):
return """data_sources: {
config {
name: "linux.sys_stats"
sys_stats_config {
stat_period_ms: 500
meminfo_period_ms: 500
}
}
}
"""
def _build_log(self):
return """data_sources: {
config {
name: "android.log"
android_log_config {
log_ids: LID_DEFAULT
log_ids: LID_SYSTEM
log_ids: LID_KERNEL
}
}
}
"""
def _build_ftrace(self):
ftrace_events = self.params.get("ftrace_events", [])
atrace_categories = self.params.get("atrace_categories", [])
app_name = self.params.get("app_name", "")
ftrace_string = ""
for event in ftrace_events:
ftrace_string += " " * 12 + 'ftrace_events: "%s"\n' % event
for category in atrace_categories:
ftrace_string += " " * 12 + 'atrace_categories: "%s"\n' % category
ftrace_string += " " * 12 + 'atrace_apps: "%s"\n' % app_name
return (
"""data_sources {
config {
name: "linux.ftrace"
ftrace_config {
%s
}
}
}
"""
% ftrace_string
)
def build(self):
return self.builder_map[self.data_source_type]()
class ConfigComposer:
main_buffer_size = 0
app_name = ""
enable_logcat = True
enable_memory_profiling = False
ftrace_events = basic_ftrace_events
atrace_categories = basic_atrace_categories
duration_ms = None
file_write_period_ms = None
flush_period_ms = 5000
def __init__(self):
pass
def set_main_buffer_size(self, size):
self.main_buffer_size = size
return self
def set_app_name(self, name):
self.app_name = name
return self
def set_enable_logcat(self, enable):
self.enable_logcat = enable
return self
def set_flush_period_ms(self, period):
self.flush_period_ms = period
return self
def set_duration_ms(self, duration):
self.duration_ms = duration
return self
def set_file_write_period_ms(self, period):
self.file_write_period_ms = period
return self
def add_ftrace_event(self, event):
self.ftrace_events.append(event)
return self
def add_atrace_category(self, category):
self.atrace_categories.append(category)
return self
def set_enable_memory_profiling(self, enable):
self.ftrace_events.extend([
'kmem/rss_stat',
'mm_event/mm_event_record'
])
self.enable_memory_profiling = enable
def _build_buffers_block(self, buffer_size):
return (
"""buffers {
size_kb: %d
fill_policy: RING_BUFFER
}
"""
% buffer_size
)
def _build_java_heap_block(self):
return (
"""data_sources: {
config {
name: "android.java_hprof
java_hprof_config {
process_cmdline: "%s"
dump_smaps: true
}
}
}""" % self.app_name
)
def build(self):
config = ""
self.main_buffer_size = self.main_buffer_size or 1024
config += self._build_buffers_block(self.main_buffer_size)
config += self._build_buffers_block(4096) # store package stats information
# default data sources
config += DataSourceBuilder(DataSourceType.PACKAGE_LIST, {}).build()
config += DataSourceBuilder(DataSourceType.PROCESS_STATS, {}).build()
config += DataSourceBuilder(DataSourceType.SYS_STATS, {}).build()
if self.enable_logcat:
config += DataSourceBuilder(DataSourceType.LOG, {}).build()
if self.ftrace_events or self.atrace_categories:
config += DataSourceBuilder(
DataSourceType.FTRACE,
{
"ftrace_events": self.ftrace_events,
"atrace_categories": self.atrace_categories,
"app_name": self.app_name,
},
).build()
config += "\nwrite_into_file: true"
config += "\nflush_period_ms: %d" % self.flush_period_ms
if self.duration_ms:
config += "\nduration_ms: %d" % self.duration_ms
if self.file_write_period_ms:
config += "\nfile_write_period_ms: %d" % self.file_write_period_ms
if self.enable_memory_profiling:
config += self._build_java_heap_block()
return config
def get_next_file_name(prefix):
i = 0
while True:
file_name = f"{prefix}{i}"
if not os.path.exists(file_name):
return file_name
i += 1
class MediaCodecLoggerInfo:
def __init__(
self, codec: str, frames_dropped: int, resolution: tuple, framerate: float
):
self.codec_type = codec
self.resolution = resolution
self.stream_framerate = framerate
self.frames_dropped = frames_dropped
def __str__(self) -> str:
return f"Codec Type: {self.codec_type}\nResolution: {self.resolution}\nStream Framerate: {self.stream_framerate}\nFramesDropped: {self.frames_dropped}"
def diff_str(self, info) -> str:
diff = ""
if self.codec_type != info.codec_type:
diff += f"Codec change: {self.codec_type} -> {info.codec_type}\n"
if self.resolution != info.resolution:
diff += f"Resolution change: {self.resolution} -> {info.resolution}\n"
if self.stream_framerate != info.stream_framerate:
diff += f"Stream Framerate change: {self.stream_framerate} -> {info.stream_framerate}\n"
if self.frames_dropped != info.frames_dropped:
diff += f"FramesDropped change: {self.frames_dropped} -> {info.frames_dropped}\n"
return diff
def valid(self) -> bool:
return not self.frames_dropped is None # The least information to be valid
def getMediaCodecInfo() -> MediaCodecLoggerInfo:
dump = subprocess.run(
"adb shell dumpsys amazon_media_codec_logger",
shell=True,
text=True,
capture_output=True,
)
info = MediaCodecLoggerInfo(None, None, None, None)
if dump.returncode != 0:
print("[FrameDrop] Failed to get frame drop info")
return info
clients = dump.stdout.split("Client [")
for out in clients:
if "Codec Type: VIDEO" not in out:
continue
m = re.search(r"FramesDropped: (\d+)", out)
if m:
info.frames_dropped = int(m.group(1))
m = re.search(r"Resolution: (\d+)x(\d+)", out)
if m:
info.resolution = (int(m.group(1)), int(m.group(2)))
m = re.search(r"Stream Framerate \(fps\): ([\d\.]+)", out)
if m:
info.stream_framerate = float(m.group(1))
m = re.search(r"Format: (\S+)", out)
if m:
info.codec_type = m.group(1)
return info
def add_common_parser(parser: argparse.ArgumentParser) -> None:
parser.add_argument(
"-a",
"--app_name",
type=str,
default="mediaserver",
help="The name of the app to profile",
)
parser.add_argument(
"-p",
"--prefix",
type=str,
default="trace_",
help="Prefix of the output file, the output file will be {prefix}_{i}",
)
parser.add_argument(
"-f",
"--flush_period_ms",
type=int,
default=5000,
help="The flush period of the perfetto",
)
parser.add_argument(
"-o",
"--open_in_browser",
action="store_true",
default=False,
help="Open the trace in browser, open_trace_in_ui must be in the PATH",
)
parser.add_argument(
"-m",
"--memory",
action="store_true",
default=False,
help="Enable memory profiling",
)
def add_root_parser(parser: argparse.ArgumentParser) -> None:
parser.add_argument(
"-b",
"--buffer_size_kb",
type=int,
default=1000000,
help="The buffer size of the perfetto",
)
parser.add_argument(
"-d",
"--duration",
type=float,
default=10.0,
help="The time to run the perfetto, in seconds",
)
parser.add_argument(
"-ws",
"--file_write_period_ms",
type=int,
default=5000,
help="The file write period of the perfetto",
)
def add_async_mode_parser(parser: argparse.ArgumentParser) -> None:
parser.add_argument(
"-e",
"--event",
type=str,
default="",
help="The event to trigger stopping the perfetto. Available events: frame_drop, log_keyword. Default value will be guessed based on other arguments",
)
parser.add_argument(
"-b",
"--buffer_size_kb",
type=int,
default=100000, # 100MB, no need to be that large in detach mode
help="The buffer size of the perfetto. This will be size of the utimate output file, so keep it small. According to experience, 1 second of trace is about 2-3MB",
)
parser.add_argument(
"-dl",
"--delay_seconds",
type=float,
default=3.0,
help="Seconds to delay after the event is triggered and before stopping the perfetto",
)
# Frame drop
parser.add_argument(
"-w",
"--window-size",
type=int,
default=10,
help="The window size to detect frame drop",
)
parser.add_argument(
"-t",
"--threshold",
type=int,
default=5,
help="The threshold to detect frame drop",
)
# Log keyword
parser.add_argument(
"-k",
"--keyword",
type=str,
help="The keyword to trigger stopping the perfetto",
)
def push_config_file(config: str) -> bool:
with open("/tmp/tmp_config", "w") as f:
f.write(config)
command = f"adb push /tmp/tmp_config {config_path}"
proc = subprocess.run(command, shell=True, capture_output=True)
if proc.returncode != 0:
print(f"Failed to push config file: {proc.stderr}")
return False
return True
def start_regular_profiling(args: argparse.Namespace):
duration_ms = int(args.duration * 1000)
prefix = args.prefix
config = (
ConfigComposer()
.set_main_buffer_size(args.buffer_size_kb)
.set_app_name(args.app_name)
.set_flush_period_ms(args.flush_period_ms)
.set_duration_ms(duration_ms)
.set_file_write_period_ms(args.file_write_period_ms)
.build()
)
if not push_config_file(config):
return
output = get_next_file_name(prefix)
print(
f"Profiling {args.app_name} for {duration_ms/1000} seconds, output to {output}"
)
perf_command.extend(["-c", config_path])
perfetto = subprocess.Popen(perf_command, text=True)
time.sleep(1) # Wait for perfetto to start
for i in range(duration_ms // 1000):
print(f"{duration_ms//1000 - i} seconds left")
time.sleep(1)
print("Waiting for perfetto to finish")
# wait for perfetto to finish
perfetto.wait()
subprocess.run(f"adb pull /data/misc/perfetto-traces/trace {output}", shell=True)
print(f"Trace saved to {output}")
if args.open_in_browser:
subprocess.run(f"open_trace_in_ui {output}", shell=True)
class AsyncPerfettoResult:
should_stop_hit = False
user_cancelled = False
error = ''
def __init__(self, should_stop_hit=False, user_cancelled=False, error=''):
self.should_stop_hit = should_stop_hit
self.user_cancelled = user_cancelled
self.error = error
def run_perfetto_asyncly(config: str, should_stop: Callable[[], bool]) -> AsyncPerfettoResult:
session_id = "frame_drop"
if not push_config_file(config):
return AsyncPerfettoResult(error="Failed to push config file")
perf_command.extend(["-c", config_path, f"--detach={session_id}"])
print(f'perf_command: {perf_command}')
perfetto = subprocess.Popen(perf_command, text=True)
perfetto.wait() # detach mode should exit immediately
def end_perfetto():
stop_perf_cmd = ["adb", "shell", "perfetto", f"--attach={session_id}", "--stop"]
stop_process = subprocess.Popen(stop_perf_cmd)
stop_process.wait()
def signal_handler(sig, frame):
print("Manual cancellation detected. Stopping profiling...")
end_perfetto()
print("Profiling stopped")
exit(0)
signal.signal(signal.SIGINT, signal_handler)
while True:
print('')
print('---')
print(f'Running perfetto in detach mode, current time: {time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())}')
if should_stop():
print('Stop condition hit, stopping perfetto...')
end_perfetto()
return AsyncPerfettoResult(should_stop_hit=True)
time.sleep(1)
class DetachPerfettoHandler:
def __init__(self):
pass
def should_stop(self):
raise NotImplementedError()
def on_stopped(self):
pass
class FrameDropHandler(DetachPerfettoHandler):
def __init__(self, window_size, threshold):
self.window_size = window_size
self.threshold = threshold
self.last_codec_info = getMediaCodecInfo()
self.last_check_time = time.time()
if not self.last_codec_info.valid():
print("[FrameDrop] Failed to get initial codec info")
raise Exception("Failed to get initial codec info")
print(
f"Profiling frame drop, detect drop every {window_size}s, threshold: {threshold}"
)
def should_stop(self):
if time.time() - self.last_check_time < self.window_size:
return False
codec_info = getMediaCodecInfo()
if not codec_info.valid():
print("[FrameDrop] Failed to get codec info")
return True
print(f"New codec status:\n{codec_info}")
diff = self.last_codec_info.diff_str(codec_info)
if diff:
print(f"Codec status changed:\n{diff}")
if codec_info.frames_dropped - self.last_codec_info.frames_dropped >= self.threshold:
print(
f"Frame drop detected, diff: {codec_info.frames_dropped - self.last_codec_info.frames_dropped}"
)
return True
self.last_codec_info = codec_info
return False
def on_stopped(self):
print("Frame drop handler stopped")
class LogcatKeywordHandler(DetachPerfettoHandler):
def __init__(self, keyword):
self.keyword = keyword
self.keyword_hit = False
self.lock = threading.Lock()
self.logcat_thread = threading.Thread(target=self.logcat_thread_fun)
self.logcat_thread.start()
def logcat_thread_fun(self):
checked_lines = 0
subprocess.run(["adb", "logcat", "-c"], text=True)
logcat = subprocess.Popen(
["adb", "logcat"],
stdout=subprocess.PIPE,
text=True,
)
for line in logcat.stdout:
checked_lines += 1
if checked_lines % 5000 == 0:
print(f"Checked {checked_lines} lines")
if self.keyword in line:
print(f"Keyword hit: {self.keyword}, line: {line}")
with self.lock:
self.keyword_hit = True
break
logcat.kill()
def should_stop(self):
with self.lock:
return self.keyword_hit
def on_stopped(self):
print("Logcat keyword handler stopped")
if self.logcat_thread.is_alive():
self.logcat_thread.join()
def make_handler(args: argparse.Namespace) -> DetachPerfettoHandler:
if args.event == "frame_drop":
return FrameDropHandler(args.window_size, args.threshold)
elif args.event == "log_keyword":
return LogcatKeywordHandler(args.keyword)
else:
raise Exception(f"Unknown event: {args.event}")
def start_detach_profiling(args: argparse.Namespace) -> None:
handler = make_handler(args)
config = (
ConfigComposer()
.set_main_buffer_size(args.buffer_size_kb)
.set_app_name(args.app_name)
.set_flush_period_ms(args.flush_period_ms)
.set_file_write_period_ms(1000000000)
.build()
)
def should_stop():
return handler.should_stop()
result = run_perfetto_asyncly(config, should_stop)
if result.should_stop_hit:
output = get_next_file_name(args.prefix)
time.sleep(args.delay_seconds)
subprocess.run(
f"adb pull /data/misc/perfetto-traces/trace {output}", shell=True
)
print(f"Trace saved to {output}")
if args.open_in_browser:
subprocess.run(f"open_trace_in_ui {output}", shell=True)
elif result.error:
print(f"Error: {result.error}")
handler.on_stopped()
def main():
parser = argparse.ArgumentParser(description="Run perfetto on Android devices")
subcommands = parser.add_subparsers(dest="subcommand")
add_common_parser(parser)
add_root_parser(parser)
async_cmd = subcommands.add_parser(
"detach",
help="Run in detach mode, trigger stopping by certain events",
)
add_common_parser(async_cmd)
add_async_mode_parser(async_cmd)
args = parser.parse_args()
if args.subcommand == "detach":
print(f"Running detach command")
if not args.event:
print("Event not specified, guessing based on other arguments")
if args.keyword:
args.event = "log_keyword"
elif args.window_size and args.threshold:
args.event = "frame_drop"
else:
raise Exception("Event not specified and cannot be guessed. '-k' for log_keyword, '-w' and '-t' for frame_drop")
start_detach_profiling(args)
else:
print(f"Running regular command")
start_regular_profiling(args)
if __name__ == "__main__":
main()