OLD | NEW |
| (Empty) |
1 #!/usr/bin/env python | |
2 # | |
3 # Copyright (c) 2013 The Chromium Authors. All rights reserved. | |
4 # Use of this source code is governed by a BSD-style license that can be | |
5 # found in the LICENSE file. | |
6 | |
7 """Command line tool for continuously printing Android graphics surface | |
8 statistics on the console. | |
9 """ | |
10 | |
11 import collections | |
12 import optparse | |
13 import sys | |
14 import time | |
15 | |
16 from pylib.device import adb_wrapper | |
17 from pylib.device import device_errors | |
18 from pylib.device import device_utils | |
19 from pylib.perf import surface_stats_collector | |
20 from pylib.utils import run_tests_helper | |
21 | |
22 | |
23 _FIELD_FORMAT = { | |
24 'jank_count (janks)': '%d', | |
25 'max_frame_delay (vsyncs)': '%d', | |
26 'avg_surface_fps (fps)': '%.2f', | |
27 'frame_lengths (vsyncs)': '%.3f', | |
28 'refresh_period (seconds)': '%.6f', | |
29 } | |
30 | |
31 | |
32 def _MergeResults(results, fields): | |
33 merged_results = collections.defaultdict(list) | |
34 for result in results: | |
35 if ((fields != ['all'] and not result.name in fields) or | |
36 result.value is None): | |
37 continue | |
38 name = '%s (%s)' % (result.name, result.unit) | |
39 if isinstance(result.value, list): | |
40 value = result.value | |
41 else: | |
42 value = [result.value] | |
43 merged_results[name] += value | |
44 for name, values in merged_results.iteritems(): | |
45 merged_results[name] = sum(values) / float(len(values)) | |
46 return merged_results | |
47 | |
48 | |
49 def _GetTerminalHeight(): | |
50 try: | |
51 import fcntl, termios, struct | |
52 except ImportError: | |
53 return 0, 0 | |
54 height, _, _, _ = struct.unpack('HHHH', | |
55 fcntl.ioctl(0, termios.TIOCGWINSZ, | |
56 struct.pack('HHHH', 0, 0, 0, 0))) | |
57 return height | |
58 | |
59 | |
60 def _PrintColumnTitles(results): | |
61 for name in results.keys(): | |
62 print '%s ' % name, | |
63 print | |
64 for name in results.keys(): | |
65 print '%s ' % ('-' * len(name)), | |
66 print | |
67 | |
68 | |
69 def _PrintResults(results): | |
70 for name, value in results.iteritems(): | |
71 value = _FIELD_FORMAT.get(name, '%s') % value | |
72 print value.rjust(len(name)) + ' ', | |
73 print | |
74 | |
75 | |
76 def main(argv): | |
77 parser = optparse.OptionParser(usage='Usage: %prog [options]', | |
78 description=__doc__) | |
79 parser.add_option('-v', | |
80 '--verbose', | |
81 dest='verbose_count', | |
82 default=0, | |
83 action='count', | |
84 help='Verbose level (multiple times for more)') | |
85 parser.add_option('--device', | |
86 help='Serial number of device we should use.') | |
87 parser.add_option('-f', | |
88 '--fields', | |
89 dest='fields', | |
90 default='jank_count,max_frame_delay,avg_surface_fps,' | |
91 'frame_lengths', | |
92 help='Comma separated list of fields to display or "all".') | |
93 parser.add_option('-d', | |
94 '--delay', | |
95 dest='delay', | |
96 default=1, | |
97 type='float', | |
98 help='Time in seconds to sleep between updates.') | |
99 | |
100 options, _ = parser.parse_args(argv) | |
101 run_tests_helper.SetLogLevel(options.verbose_count) | |
102 | |
103 if options.device: | |
104 device = device_utils.DeviceUtils(options.device) | |
105 else: | |
106 devices = adb_wrapper.AdbWrapper.GetDevices() | |
107 if not devices: | |
108 raise device_errors.NoDevicesError | |
109 device = device_utils.DeviceUtils(devices[0]) | |
110 | |
111 collector = surface_stats_collector.SurfaceStatsCollector(device) | |
112 collector.DisableWarningAboutEmptyData() | |
113 | |
114 fields = options.fields.split(',') | |
115 row_count = None | |
116 | |
117 try: | |
118 collector.Start() | |
119 while True: | |
120 time.sleep(options.delay) | |
121 results = collector.SampleResults() | |
122 results = _MergeResults(results, fields) | |
123 | |
124 if not results: | |
125 continue | |
126 | |
127 terminal_height = _GetTerminalHeight() | |
128 if row_count is None or (terminal_height and | |
129 row_count >= terminal_height - 3): | |
130 _PrintColumnTitles(results) | |
131 row_count = 0 | |
132 | |
133 _PrintResults(results) | |
134 row_count += 1 | |
135 except KeyboardInterrupt: | |
136 sys.exit(0) | |
137 finally: | |
138 collector.Stop() | |
139 | |
140 | |
141 if __name__ == '__main__': | |
142 main(sys.argv) | |
OLD | NEW |