blob: 1e5be6c3eed7a1d49180c3804e1bf0c4c1619df3 [file] [log] [blame]
John Reckba6adf62015-02-19 14:36:50 -08001/*
2 * Copyright (C) 2015 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 at
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 */
Mark Salyzyn96bf5982016-09-28 16:15:30 -070016
John Reckba6adf62015-02-19 14:36:50 -080017#include "JankTracker.h"
18
Alec Mouri22d753f2019-09-05 17:11:45 -070019#include <cutils/ashmem.h>
John Reckedc524c2015-03-18 15:24:33 -070020#include <errno.h>
John Reckba6adf62015-02-19 14:36:50 -080021#include <inttypes.h>
Alec Mouri22d753f2019-09-05 17:11:45 -070022#include <log/log.h>
Mark Salyzyn96bf5982016-09-28 16:15:30 -070023#include <sys/mman.h>
Mark Salyzyn52eb4e02016-09-28 16:15:30 -070024
25#include <algorithm>
John Reck5ed587f2016-03-24 15:57:01 -070026#include <cmath>
Mark Salyzyn52eb4e02016-09-28 16:15:30 -070027#include <cstdio>
28#include <limits>
John Reck0e89ca22017-12-15 16:00:48 -080029#include <sstream>
Mark Salyzyn52eb4e02016-09-28 16:15:30 -070030
Alec Mouri22d753f2019-09-05 17:11:45 -070031#include "DeviceInfo.h"
Mark Salyzyn52eb4e02016-09-28 16:15:30 -070032#include "Properties.h"
33#include "utils/TimeUtils.h"
John Reck0e89ca22017-12-15 16:00:48 -080034#include "utils/Trace.h"
Mark Salyzyn52eb4e02016-09-28 16:15:30 -070035
John Reckba6adf62015-02-19 14:36:50 -080036namespace android {
37namespace uirenderer {
38
John Reckba6adf62015-02-19 14:36:50 -080039struct Comparison {
John Reck0e486472018-03-19 14:06:16 -070040 JankType type;
41 std::function<int64_t(nsecs_t)> computeThreadshold;
John Reckc87be992015-02-20 10:57:22 -080042 FrameInfoIndex start;
43 FrameInfoIndex end;
John Reckba6adf62015-02-19 14:36:50 -080044};
45
John Reck0e486472018-03-19 14:06:16 -070046static const std::array<Comparison, 4> COMPARISONS{
47 Comparison{JankType::kMissedVsync, [](nsecs_t) { return 1; }, FrameInfoIndex::IntendedVsync,
48 FrameInfoIndex::Vsync},
49
50 Comparison{JankType::kSlowUI,
51 [](nsecs_t frameInterval) { return static_cast<int64_t>(.5 * frameInterval); },
52 FrameInfoIndex::Vsync, FrameInfoIndex::SyncStart},
53
54 Comparison{JankType::kSlowSync,
55 [](nsecs_t frameInterval) { return static_cast<int64_t>(.2 * frameInterval); },
56 FrameInfoIndex::SyncStart, FrameInfoIndex::IssueDrawCommandsStart},
57
58 Comparison{JankType::kSlowRT,
59 [](nsecs_t frameInterval) { return static_cast<int64_t>(.75 * frameInterval); },
60 FrameInfoIndex::IssueDrawCommandsStart, FrameInfoIndex::FrameCompleted},
John Reckba6adf62015-02-19 14:36:50 -080061};
62
63// If the event exceeds 10 seconds throw it away, this isn't a jank event
64// it's an ANR and will be handled as such
65static const int64_t IGNORE_EXCEEDING = seconds_to_nanoseconds(10);
66
67/*
John Reck66010802016-03-30 14:19:44 -070068 * We don't track direct-drawing via Surface:lockHardwareCanvas()
John Reckba6adf62015-02-19 14:36:50 -080069 * for now
70 *
71 * TODO: kSurfaceCanvas can negatively impact other drawing by using up
72 * time on the RenderThread, figure out how to attribute that as a jank-causer
73 */
John Reck66010802016-03-30 14:19:44 -070074static const int64_t EXEMPT_FRAMES_FLAGS = FrameInfoFlags::SurfaceCanvas;
John Reckba6adf62015-02-19 14:36:50 -080075
John Reckc7cd9cf2016-03-28 10:38:19 -070076// For testing purposes to try and eliminate test infra overhead we will
77// consider any unknown delay of frame start as part of the test infrastructure
78// and filter it out of the frame profile data
79static FrameInfoIndex sFrameStart = FrameInfoIndex::IntendedVsync;
80
Jorim Jaggi71db8892021-02-03 23:19:29 +010081JankTracker::JankTracker(ProfileDataContainer* globalData)
82 : mData(globalData->getDataMutex())
83 , mDataMutex(globalData->getDataMutex()) {
John Reck34781b22017-07-05 16:39:36 -070084 mGlobalData = globalData;
Alec Mouri4a818f12019-11-19 16:17:53 -080085 nsecs_t frameIntervalNanos = DeviceInfo::getVsyncPeriod();
Alec Mouri22d753f2019-09-05 17:11:45 -070086 nsecs_t sfOffset = DeviceInfo::getCompositorOffset();
87 nsecs_t offsetDelta = sfOffset - DeviceInfo::getAppOffset();
John Reck2d5b8d72016-07-28 15:36:11 -070088 // There are two different offset cases. If the offsetDelta is positive
89 // and small, then the intention is to give apps extra time by leveraging
90 // pipelining between the UI & RT threads. If the offsetDelta is large or
91 // negative, the intention is to subtract time from the total duration
92 // in which case we can't afford to wait for dequeueBuffer blockage.
93 if (offsetDelta <= 4_ms && offsetDelta >= 0) {
94 // SF will begin composition at VSYNC-app + offsetDelta. If we are triple
95 // buffered, this is the expected time at which dequeueBuffer will
96 // return due to the staggering of VSYNC-app & VSYNC-sf.
Jorim Jaggi10f328c2021-01-19 00:08:02 +010097 mDequeueTimeForgivenessLegacy = offsetDelta + 4_ms;
John Reck2d5b8d72016-07-28 15:36:11 -070098 }
Jorim Jaggi10f328c2021-01-19 00:08:02 +010099 mFrameIntervalLegacy = frameIntervalNanos;
John Reckba6adf62015-02-19 14:36:50 -0800100}
101
Siarhei Vishniakou07d35cb2021-07-03 02:22:12 +0000102void JankTracker::calculateLegacyJank(FrameInfo& frame) REQUIRES(mDataMutex) {
John Reckba6adf62015-02-19 14:36:50 -0800103 // Fast-path for jank-free frames
Jorim Jaggi0ac066d2021-02-24 16:28:47 +0100104 int64_t totalDuration = frame.duration(sFrameStart, FrameInfoIndex::SwapBuffersCompleted);
Jorim Jaggi10f328c2021-01-19 00:08:02 +0100105 if (mDequeueTimeForgivenessLegacy && frame[FrameInfoIndex::DequeueBufferDuration] > 500_us) {
106 nsecs_t expectedDequeueDuration = mDequeueTimeForgivenessLegacy
107 + frame[FrameInfoIndex::Vsync]
108 - frame[FrameInfoIndex::IssueDrawCommandsStart];
John Reck2d5b8d72016-07-28 15:36:11 -0700109 if (expectedDequeueDuration > 0) {
110 // Forgive only up to the expected amount, but not more than
111 // the actual time spent blocked.
John Reck1bcacfd2017-11-03 10:12:19 -0700112 nsecs_t forgiveAmount =
113 std::min(expectedDequeueDuration, frame[FrameInfoIndex::DequeueBufferDuration]);
John Reck00665ae2021-10-04 14:35:03 -0400114 if (forgiveAmount >= totalDuration) {
115 ALOGV("Impossible dequeue duration! dequeue duration reported %" PRId64
116 ", total duration %" PRId64,
117 forgiveAmount, totalDuration);
118 return;
119 }
John Reck2d5b8d72016-07-28 15:36:11 -0700120 totalDuration -= forgiveAmount;
121 }
122 }
John Reck0e486472018-03-19 14:06:16 -0700123
John Reck00665ae2021-10-04 14:35:03 -0400124 if (totalDuration <= 0) {
125 ALOGV("Impossible totalDuration %" PRId64 " start=%" PRIi64 " gpuComplete=%" PRIi64,
126 totalDuration, frame[FrameInfoIndex::IntendedVsync],
127 frame[FrameInfoIndex::GpuCompleted]);
128 return;
129 }
Jorim Jaggi10f328c2021-01-19 00:08:02 +0100130
131 // Only things like Surface.lockHardwareCanvas() are exempt from tracking
132 if (CC_UNLIKELY(frame[FrameInfoIndex::Flags] & EXEMPT_FRAMES_FLAGS)) {
133 return;
134 }
135
136 if (totalDuration > mFrameIntervalLegacy) {
137 mData->reportJankLegacy();
138 (*mGlobalData)->reportJankLegacy();
139 }
140
141 if (mSwapDeadlineLegacy < 0) {
142 mSwapDeadlineLegacy = frame[FrameInfoIndex::IntendedVsync] + mFrameIntervalLegacy;
143 }
144 bool isTripleBuffered = (mSwapDeadlineLegacy - frame[FrameInfoIndex::IntendedVsync])
145 > (mFrameIntervalLegacy * 0.1);
146
147 mSwapDeadlineLegacy = std::max(mSwapDeadlineLegacy + mFrameIntervalLegacy,
148 frame[FrameInfoIndex::IntendedVsync] + mFrameIntervalLegacy);
149
150 // If we hit the deadline, cool!
151 if (frame[FrameInfoIndex::FrameCompleted] < mSwapDeadlineLegacy
152 || totalDuration < mFrameIntervalLegacy) {
153 if (isTripleBuffered) {
154 mData->reportJankType(JankType::kHighInputLatency);
155 (*mGlobalData)->reportJankType(JankType::kHighInputLatency);
156 }
157 return;
158 }
159
160 mData->reportJankType(JankType::kMissedDeadlineLegacy);
161 (*mGlobalData)->reportJankType(JankType::kMissedDeadlineLegacy);
162
163 // Janked, reset the swap deadline
164 nsecs_t jitterNanos = frame[FrameInfoIndex::FrameCompleted] - frame[FrameInfoIndex::Vsync];
165 nsecs_t lastFrameOffset = jitterNanos % mFrameIntervalLegacy;
166 mSwapDeadlineLegacy = frame[FrameInfoIndex::FrameCompleted]
167 - lastFrameOffset + mFrameIntervalLegacy;
168}
169
Pablo Gamito88660d72021-08-09 14:37:56 +0000170void JankTracker::finishFrame(FrameInfo& frame, std::unique_ptr<FrameMetricsReporter>& reporter,
171 int64_t frameNumber, int32_t surfaceControlId) {
Jorim Jaggi10f328c2021-01-19 00:08:02 +0100172 std::lock_guard lock(mDataMutex);
173
174 calculateLegacyJank(frame);
175
176 // Fast-path for jank-free frames
177 int64_t totalDuration = frame.duration(FrameInfoIndex::IntendedVsync,
178 FrameInfoIndex::FrameCompleted);
179
John Reck00665ae2021-10-04 14:35:03 -0400180 if (totalDuration <= 0) {
181 ALOGV("Impossible totalDuration %" PRId64, totalDuration);
182 return;
183 }
John Reck7075c792017-07-05 14:03:43 -0700184 mData->reportFrame(totalDuration);
John Reck34781b22017-07-05 16:39:36 -0700185 (*mGlobalData)->reportFrame(totalDuration);
John Reck7075c792017-07-05 14:03:43 -0700186
John Reck66010802016-03-30 14:19:44 -0700187 // Only things like Surface.lockHardwareCanvas() are exempt from tracking
John Reck0e486472018-03-19 14:06:16 -0700188 if (CC_UNLIKELY(frame[FrameInfoIndex::Flags] & EXEMPT_FRAMES_FLAGS)) {
John Reckba6adf62015-02-19 14:36:50 -0800189 return;
190 }
191
Jorim Jaggi10f328c2021-01-19 00:08:02 +0100192 int64_t frameInterval = frame[FrameInfoIndex::FrameInterval];
John Reckba6adf62015-02-19 14:36:50 -0800193
Jorim Jaggi10f328c2021-01-19 00:08:02 +0100194 // If we starter earlier than the intended frame start assuming an unstuffed scenario, it means
195 // that we are in a triple buffering situation.
196 bool isTripleBuffered = (mNextFrameStartUnstuffed - frame[FrameInfoIndex::IntendedVsync])
197 > (frameInterval * 0.1);
John Reck0e486472018-03-19 14:06:16 -0700198
Jorim Jaggi10f328c2021-01-19 00:08:02 +0100199 int64_t deadline = frame[FrameInfoIndex::FrameDeadline];
200
201 // If we are in triple buffering, we have enough buffers in queue to sustain a single frame
202 // drop without jank, so adjust the frame interval to the deadline.
203 if (isTripleBuffered) {
204 deadline += frameInterval;
205 frame.set(FrameInfoIndex::FrameDeadline) += frameInterval;
206 }
John Reck0e486472018-03-19 14:06:16 -0700207
208 // If we hit the deadline, cool!
Jorim Jaggi10f328c2021-01-19 00:08:02 +0100209 if (frame[FrameInfoIndex::GpuCompleted] < deadline) {
John Reck0e486472018-03-19 14:06:16 -0700210 if (isTripleBuffered) {
211 mData->reportJankType(JankType::kHighInputLatency);
212 (*mGlobalData)->reportJankType(JankType::kHighInputLatency);
Jorim Jaggi10f328c2021-01-19 00:08:02 +0100213
214 // Buffer stuffing state gets carried over to next frame, unless there is a "pause"
215 mNextFrameStartUnstuffed += frameInterval;
John Reck0e486472018-03-19 14:06:16 -0700216 }
Jorim Jaggi10f328c2021-01-19 00:08:02 +0100217 } else {
218 mData->reportJankType(JankType::kMissedDeadline);
219 (*mGlobalData)->reportJankType(JankType::kMissedDeadline);
220 mData->reportJank();
221 (*mGlobalData)->reportJank();
222
223 // Janked, store the adjust deadline to detect triple buffering in next frame correctly.
224 nsecs_t jitterNanos = frame[FrameInfoIndex::GpuCompleted]
225 - frame[FrameInfoIndex::Vsync];
226 nsecs_t lastFrameOffset = jitterNanos % frameInterval;
227
228 // Note the time when the next frame would start in an unstuffed situation. If it starts
229 // earlier, we are in a stuffed situation.
230 mNextFrameStartUnstuffed = frame[FrameInfoIndex::GpuCompleted]
231 - lastFrameOffset + frameInterval;
232
233 recomputeThresholds(frameInterval);
234 for (auto& comparison : COMPARISONS) {
235 int64_t delta = frame.duration(comparison.start, comparison.end);
236 if (delta >= mThresholds[comparison.type] && delta < IGNORE_EXCEEDING) {
237 mData->reportJankType(comparison.type);
238 (*mGlobalData)->reportJankType(comparison.type);
239 }
240 }
241
242 // Log daveys since they are weird and we don't know what they are (b/70339576)
243 if (totalDuration >= 700_ms) {
244 static int sDaveyCount = 0;
245 std::stringstream ss;
246 ss << "Davey! duration=" << ns2ms(totalDuration) << "ms; ";
247 for (size_t i = 0; i < static_cast<size_t>(FrameInfoIndex::NumIndexes); i++) {
248 ss << FrameInfoNames[i] << "=" << frame[i] << ", ";
249 }
250 ALOGI("%s", ss.str().c_str());
251 // Just so we have something that counts up, the value is largely irrelevant
252 ATRACE_INT(ss.str().c_str(), ++sDaveyCount);
253 }
254 }
255
256 int64_t totalGPUDrawTime = frame.gpuDrawTime();
257 if (totalGPUDrawTime >= 0) {
258 mData->reportGPUFrame(totalGPUDrawTime);
259 (*mGlobalData)->reportGPUFrame(totalGPUDrawTime);
260 }
261
262 if (CC_UNLIKELY(reporter.get() != nullptr)) {
Pablo Gamito88660d72021-08-09 14:37:56 +0000263 reporter->reportFrameMetrics(frame.data(), false /* hasPresentTime */, frameNumber,
264 surfaceControlId);
Jorim Jaggi10f328c2021-01-19 00:08:02 +0100265 }
266}
267
Siarhei Vishniakou07d35cb2021-07-03 02:22:12 +0000268void JankTracker::recomputeThresholds(int64_t frameBudget) REQUIRES(mDataMutex) {
Jorim Jaggi10f328c2021-01-19 00:08:02 +0100269 if (mThresholdsFrameBudget == frameBudget) {
John Reck0e486472018-03-19 14:06:16 -0700270 return;
271 }
Jorim Jaggi10f328c2021-01-19 00:08:02 +0100272 mThresholdsFrameBudget = frameBudget;
John Reck0e486472018-03-19 14:06:16 -0700273 for (auto& comparison : COMPARISONS) {
Jorim Jaggi10f328c2021-01-19 00:08:02 +0100274 mThresholds[comparison.type] = comparison.computeThreadshold(frameBudget);
John Reck0e89ca22017-12-15 16:00:48 -0800275 }
John Reckba6adf62015-02-19 14:36:50 -0800276}
277
John Reck1bcacfd2017-11-03 10:12:19 -0700278void JankTracker::dumpData(int fd, const ProfileDataDescription* description,
279 const ProfileData* data) {
Jorim Jaggi71db8892021-02-03 23:19:29 +0100280
John Reckdf1742e2017-01-19 15:56:21 -0800281 if (description) {
282 switch (description->type) {
283 case JankTrackerType::Generic:
284 break;
285 case JankTrackerType::Package:
286 dprintf(fd, "\nPackage: %s", description->name.c_str());
287 break;
288 case JankTrackerType::Window:
289 dprintf(fd, "\nWindow: %s", description->name.c_str());
290 break;
291 }
John Reckba6adf62015-02-19 14:36:50 -0800292 }
John Reckc7cd9cf2016-03-28 10:38:19 -0700293 if (sFrameStart != FrameInfoIndex::IntendedVsync) {
294 dprintf(fd, "\nNote: Data has been filtered!");
295 }
John Reck7075c792017-07-05 14:03:43 -0700296 data->dump(fd);
John Reckedc524c2015-03-18 15:24:33 -0700297 dprintf(fd, "\n");
John Reckba6adf62015-02-19 14:36:50 -0800298}
299
John Reck34781b22017-07-05 16:39:36 -0700300void JankTracker::dumpFrames(int fd) {
John Reck47f5c3a2017-11-13 11:32:39 -0800301 dprintf(fd, "\n\n---PROFILEDATA---\n");
John Reck34781b22017-07-05 16:39:36 -0700302 for (size_t i = 0; i < static_cast<size_t>(FrameInfoIndex::NumIndexes); i++) {
Siarhei Vishniakou4bcbffd2021-02-17 06:19:36 +0000303 dprintf(fd, "%s", FrameInfoNames[i]);
John Reck47f5c3a2017-11-13 11:32:39 -0800304 dprintf(fd, ",");
John Reck34781b22017-07-05 16:39:36 -0700305 }
306 for (size_t i = 0; i < mFrames.size(); i++) {
307 FrameInfo& frame = mFrames[i];
308 if (frame[FrameInfoIndex::SyncStart] == 0) {
309 continue;
310 }
John Reck47f5c3a2017-11-13 11:32:39 -0800311 dprintf(fd, "\n");
John Reck34781b22017-07-05 16:39:36 -0700312 for (int i = 0; i < static_cast<int>(FrameInfoIndex::NumIndexes); i++) {
John Reck47f5c3a2017-11-13 11:32:39 -0800313 dprintf(fd, "%" PRId64 ",", frame[i]);
John Reck34781b22017-07-05 16:39:36 -0700314 }
315 }
John Reck47f5c3a2017-11-13 11:32:39 -0800316 dprintf(fd, "\n---PROFILEDATA---\n\n");
John Reck34781b22017-07-05 16:39:36 -0700317}
318
Siarhei Vishniakou07d35cb2021-07-03 02:22:12 +0000319void JankTracker::reset() REQUIRES(mDataMutex) {
John Reck34781b22017-07-05 16:39:36 -0700320 mFrames.clear();
John Reck7075c792017-07-05 14:03:43 -0700321 mData->reset();
John Reck34781b22017-07-05 16:39:36 -0700322 (*mGlobalData)->reset();
John Reck1bcacfd2017-11-03 10:12:19 -0700323 sFrameStart = Properties::filterOutTestOverhead ? FrameInfoIndex::HandleInputStart
324 : FrameInfoIndex::IntendedVsync;
John Reckba6adf62015-02-19 14:36:50 -0800325}
326
John Reckba6adf62015-02-19 14:36:50 -0800327} /* namespace uirenderer */
328} /* namespace android */