1 # Copyright 2013 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
10 from pylib
import android_commands
11 from pylib
.device
import device_utils
14 # Log marker containing SurfaceTexture timestamps.
15 _SURFACE_TEXTURE_TIMESTAMPS_MESSAGE
= 'SurfaceTexture update timestamps'
16 _SURFACE_TEXTURE_TIMESTAMP_RE
= r
'\d+'
19 class SurfaceStatsCollector(object):
20 """Collects surface stats for a SurfaceView from the output of SurfaceFlinger.
23 device: A DeviceUtils instance.
26 def __init__(self
, device
):
27 # TODO(jbudorick) Remove once telemetry gets switched over.
28 if isinstance(device
, android_commands
.AndroidCommands
):
29 device
= device_utils
.DeviceUtils(device
)
31 self
._collector
_thread
= None
32 self
._surface
_before
= None
33 self
._get
_data
_event
= None
34 self
._data
_queue
= None
35 self
._stop
_event
= None
36 self
._warn
_about
_empty
_data
= True
38 def DisableWarningAboutEmptyData(self
):
39 self
._warn
_about
_empty
_data
= False
42 assert not self
._collector
_thread
44 if self
._ClearSurfaceFlingerLatencyData
():
45 self
._get
_data
_event
= threading
.Event()
46 self
._stop
_event
= threading
.Event()
47 self
._data
_queue
= Queue
.Queue()
48 self
._collector
_thread
= threading
.Thread(target
=self
._CollectorThread
)
49 self
._collector
_thread
.start()
51 raise Exception('SurfaceFlinger not supported on this device.')
54 assert self
._collector
_thread
55 (refresh_period
, timestamps
) = self
._GetDataFromThread
()
56 if self
._collector
_thread
:
57 self
._stop
_event
.set()
58 self
._collector
_thread
.join()
59 self
._collector
_thread
= None
60 return (refresh_period
, timestamps
)
62 def _CollectorThread(self
):
67 while not self
._stop
_event
.is_set():
68 self
._get
_data
_event
.wait(1)
70 refresh_period
, new_timestamps
= self
._GetSurfaceFlingerFrameData
()
71 if refresh_period
is None or timestamps
is None:
76 # Some data has already been collected, but either the app
77 # was closed or there's no new data. Signal the main thread and
79 self
._data
_queue
.put((None, None))
80 self
._stop
_event
.wait()
82 raise Exception('Unable to get surface flinger latency data')
84 timestamps
+= [timestamp
for timestamp
in new_timestamps
85 if timestamp
> last_timestamp
]
87 last_timestamp
= timestamps
[-1]
89 if self
._get
_data
_event
.is_set():
90 self
._get
_data
_event
.clear()
91 self
._data
_queue
.put((refresh_period
, timestamps
))
93 except Exception as e
:
94 # On any error, before aborting, put the exception into _data_queue to
95 # prevent the main thread from waiting at _data_queue.get() infinitely.
96 self
._data
_queue
.put(e
)
99 def _GetDataFromThread(self
):
100 self
._get
_data
_event
.set()
101 ret
= self
._data
_queue
.get()
102 if isinstance(ret
, Exception):
106 def _ClearSurfaceFlingerLatencyData(self
):
107 """Clears the SurfaceFlinger latency data.
110 True if SurfaceFlinger latency is supported by the device, otherwise
113 # The command returns nothing if it is supported, otherwise returns many
114 # lines of result just like 'dumpsys SurfaceFlinger'.
115 results
= self
._device
.RunShellCommand(
116 'dumpsys SurfaceFlinger --latency-clear SurfaceView')
117 return not len(results
)
119 def GetSurfaceFlingerPid(self
):
120 results
= self
._device
.RunShellCommand('ps | grep surfaceflinger')
122 raise Exception('Unable to get surface flinger process id')
123 pid
= results
[0].split()[1]
126 def _GetSurfaceFlingerFrameData(self
):
127 """Returns collected SurfaceFlinger frame timing data.
131 - The display's nominal refresh period in milliseconds.
132 - A list of timestamps signifying frame presentation times in
134 The return value may be (None, None) if there was no data collected (for
135 example, if the app was closed before the collector thread has finished).
137 # adb shell dumpsys SurfaceFlinger --latency <window name>
138 # prints some information about the last 128 frames displayed in
140 # The data returned looks like this:
142 # 7657467895508 7657482691352 7657493499756
143 # 7657484466553 7657499645964 7657511077881
144 # 7657500793457 7657516600576 7657527404785
147 # The first line is the refresh period (here 16.95 ms), it is followed
148 # by 128 lines w/ 3 timestamps in nanosecond each:
149 # A) when the app started to draw
150 # B) the vsync immediately preceding SF submitting the frame to the h/w
151 # C) timestamp immediately after SF submitted that frame to the h/w
153 # The difference between the 1st and 3rd timestamp is the frame-latency.
154 # An interesting data is when the frame latency crosses a refresh period
155 # boundary, this can be calculated this way:
157 # ceil((C - A) / refresh-period)
159 # (each time the number above changes, we have a "jank").
160 # If this happens a lot during an animation, the animation appears
161 # janky, even if it runs at 60 fps in average.
163 # We use the special "SurfaceView" window name because the statistics for
164 # the activity's main window are not updated when the main web content is
165 # composited into a SurfaceView.
166 results
= self
._device
.RunShellCommand(
167 'dumpsys SurfaceFlinger --latency SurfaceView')
172 nanoseconds_per_millisecond
= 1e6
173 refresh_period
= long(results
[0]) / nanoseconds_per_millisecond
175 # If a fence associated with a frame is still pending when we query the
176 # latency data, SurfaceFlinger gives the frame a timestamp of INT64_MAX.
177 # Since we only care about completed frames, we will ignore any timestamps
179 pending_fence_timestamp
= (1 << 63) - 1
181 for line
in results
[1:]:
182 fields
= line
.split()
185 timestamp
= long(fields
[1])
186 if timestamp
== pending_fence_timestamp
:
188 timestamp
/= nanoseconds_per_millisecond
189 timestamps
.append(timestamp
)
191 return (refresh_period
, timestamps
)