blob: 39ed9a0478b671cb8117ed86e58c86a0586ad26c [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
John Reckedc524c2015-03-18 15:24:33 -070019#include <errno.h>
John Reckba6adf62015-02-19 14:36:50 -080020#include <inttypes.h>
Mark Salyzyn96bf5982016-09-28 16:15:30 -070021#include <sys/mman.h>
Mark Salyzyn52eb4e02016-09-28 16:15:30 -070022
23#include <algorithm>
John Reck5ed587f2016-03-24 15:57:01 -070024#include <cmath>
Mark Salyzyn52eb4e02016-09-28 16:15:30 -070025#include <cstdio>
26#include <limits>
John Reckba6adf62015-02-19 14:36:50 -080027
Mark Salyzyn52eb4e02016-09-28 16:15:30 -070028#include <cutils/ashmem.h>
29#include <log/log.h>
John Reck0e89ca22017-12-15 16:00:48 -080030#include <sstream>
Mark Salyzyn52eb4e02016-09-28 16:15:30 -070031
32#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
John Reck34781b22017-07-05 16:39:36 -070081JankTracker::JankTracker(ProfileDataContainer* globalData, const DisplayInfo& displayInfo) {
82 mGlobalData = globalData;
John Reck2d5b8d72016-07-28 15:36:11 -070083 nsecs_t frameIntervalNanos = static_cast<nsecs_t>(1_s / displayInfo.fps);
John Reck2d5b8d72016-07-28 15:36:11 -070084 nsecs_t sfOffset = frameIntervalNanos - (displayInfo.presentationDeadline - 1_ms);
85 nsecs_t offsetDelta = sfOffset - displayInfo.appVsyncOffset;
86 // There are two different offset cases. If the offsetDelta is positive
87 // and small, then the intention is to give apps extra time by leveraging
88 // pipelining between the UI & RT threads. If the offsetDelta is large or
89 // negative, the intention is to subtract time from the total duration
90 // in which case we can't afford to wait for dequeueBuffer blockage.
91 if (offsetDelta <= 4_ms && offsetDelta >= 0) {
92 // SF will begin composition at VSYNC-app + offsetDelta. If we are triple
93 // buffered, this is the expected time at which dequeueBuffer will
94 // return due to the staggering of VSYNC-app & VSYNC-sf.
95 mDequeueTimeForgiveness = offsetDelta + 4_ms;
96 }
John Reckba6adf62015-02-19 14:36:50 -080097 setFrameInterval(frameIntervalNanos);
98}
99
100void JankTracker::setFrameInterval(nsecs_t frameInterval) {
101 mFrameInterval = frameInterval;
John Reckba6adf62015-02-19 14:36:50 -0800102
John Reck0e486472018-03-19 14:06:16 -0700103 for (auto& comparison : COMPARISONS) {
104 mThresholds[comparison.type] = comparison.computeThreadshold(frameInterval);
105 }
John Reckba6adf62015-02-19 14:36:50 -0800106}
107
John Reck34781b22017-07-05 16:39:36 -0700108void JankTracker::finishFrame(const FrameInfo& frame) {
John Reckba6adf62015-02-19 14:36:50 -0800109 // Fast-path for jank-free frames
John Reck126720a2016-04-15 15:16:38 -0700110 int64_t totalDuration = frame.duration(sFrameStart, FrameInfoIndex::FrameCompleted);
John Reck1bcacfd2017-11-03 10:12:19 -0700111 if (mDequeueTimeForgiveness && frame[FrameInfoIndex::DequeueBufferDuration] > 500_us) {
112 nsecs_t expectedDequeueDuration = mDequeueTimeForgiveness + frame[FrameInfoIndex::Vsync] -
113 frame[FrameInfoIndex::IssueDrawCommandsStart];
John Reck2d5b8d72016-07-28 15:36:11 -0700114 if (expectedDequeueDuration > 0) {
115 // Forgive only up to the expected amount, but not more than
116 // the actual time spent blocked.
John Reck1bcacfd2017-11-03 10:12:19 -0700117 nsecs_t forgiveAmount =
118 std::min(expectedDequeueDuration, frame[FrameInfoIndex::DequeueBufferDuration]);
John Reck1b7184f2017-03-27 14:47:46 -0700119 LOG_ALWAYS_FATAL_IF(forgiveAmount >= totalDuration,
John Reck1bcacfd2017-11-03 10:12:19 -0700120 "Impossible dequeue duration! dequeue duration reported %" PRId64
121 ", total duration %" PRId64,
122 forgiveAmount, totalDuration);
John Reck2d5b8d72016-07-28 15:36:11 -0700123 totalDuration -= forgiveAmount;
124 }
125 }
John Reck0e486472018-03-19 14:06:16 -0700126
John Reck1b7184f2017-03-27 14:47:46 -0700127 LOG_ALWAYS_FATAL_IF(totalDuration <= 0, "Impossible totalDuration %" PRId64, totalDuration);
John Reck7075c792017-07-05 14:03:43 -0700128 mData->reportFrame(totalDuration);
John Reck34781b22017-07-05 16:39:36 -0700129 (*mGlobalData)->reportFrame(totalDuration);
John Reck7075c792017-07-05 14:03:43 -0700130
John Reck66010802016-03-30 14:19:44 -0700131 // Only things like Surface.lockHardwareCanvas() are exempt from tracking
John Reck0e486472018-03-19 14:06:16 -0700132 if (CC_UNLIKELY(frame[FrameInfoIndex::Flags] & EXEMPT_FRAMES_FLAGS)) {
John Reckba6adf62015-02-19 14:36:50 -0800133 return;
134 }
135
John Reck0e486472018-03-19 14:06:16 -0700136 if (totalDuration > mFrameInterval) {
137 mData->reportJank();
138 (*mGlobalData)->reportJank();
139 }
John Reckba6adf62015-02-19 14:36:50 -0800140
John Reck0e486472018-03-19 14:06:16 -0700141 bool isTripleBuffered = mSwapDeadline > frame[FrameInfoIndex::IntendedVsync];
142
143 mSwapDeadline = std::max(mSwapDeadline + mFrameInterval,
144 frame[FrameInfoIndex::IntendedVsync] + mFrameInterval);
145
146 // If we hit the deadline, cool!
John Reck50be3b82018-09-14 11:25:58 -0700147 if (frame[FrameInfoIndex::FrameCompleted] < mSwapDeadline || totalDuration < mFrameInterval) {
John Reck0e486472018-03-19 14:06:16 -0700148 if (isTripleBuffered) {
149 mData->reportJankType(JankType::kHighInputLatency);
150 (*mGlobalData)->reportJankType(JankType::kHighInputLatency);
151 }
152 return;
153 }
154
155 mData->reportJankType(JankType::kMissedDeadline);
156 (*mGlobalData)->reportJankType(JankType::kMissedDeadline);
157
158 // Janked, reset the swap deadline
159 nsecs_t jitterNanos = frame[FrameInfoIndex::FrameCompleted] - frame[FrameInfoIndex::Vsync];
160 nsecs_t lastFrameOffset = jitterNanos % mFrameInterval;
161 mSwapDeadline = frame[FrameInfoIndex::FrameCompleted] - lastFrameOffset + mFrameInterval;
162
163 for (auto& comparison : COMPARISONS) {
164 int64_t delta = frame.duration(comparison.start, comparison.end);
165 if (delta >= mThresholds[comparison.type] && delta < IGNORE_EXCEEDING) {
166 mData->reportJankType(comparison.type);
167 (*mGlobalData)->reportJankType(comparison.type);
John Reckba6adf62015-02-19 14:36:50 -0800168 }
169 }
John Reck0e89ca22017-12-15 16:00:48 -0800170
171 // Log daveys since they are weird and we don't know what they are (b/70339576)
172 if (totalDuration >= 700_ms) {
173 static int sDaveyCount = 0;
174 std::stringstream ss;
175 ss << "Davey! duration=" << ns2ms(totalDuration) << "ms; ";
176 for (size_t i = 0; i < static_cast<size_t>(FrameInfoIndex::NumIndexes); i++) {
177 ss << FrameInfoNames[i] << "=" << frame[i] << ", ";
178 }
179 ALOGI("%s", ss.str().c_str());
180 // Just so we have something that counts up, the value is largely irrelevant
181 ATRACE_INT(ss.str().c_str(), ++sDaveyCount);
182 }
John Reckba6adf62015-02-19 14:36:50 -0800183}
184
John Reck1bcacfd2017-11-03 10:12:19 -0700185void JankTracker::dumpData(int fd, const ProfileDataDescription* description,
186 const ProfileData* data) {
John Reckdf1742e2017-01-19 15:56:21 -0800187 if (description) {
188 switch (description->type) {
189 case JankTrackerType::Generic:
190 break;
191 case JankTrackerType::Package:
192 dprintf(fd, "\nPackage: %s", description->name.c_str());
193 break;
194 case JankTrackerType::Window:
195 dprintf(fd, "\nWindow: %s", description->name.c_str());
196 break;
197 }
John Reckba6adf62015-02-19 14:36:50 -0800198 }
John Reckc7cd9cf2016-03-28 10:38:19 -0700199 if (sFrameStart != FrameInfoIndex::IntendedVsync) {
200 dprintf(fd, "\nNote: Data has been filtered!");
201 }
John Reck7075c792017-07-05 14:03:43 -0700202 data->dump(fd);
John Reckedc524c2015-03-18 15:24:33 -0700203 dprintf(fd, "\n");
John Reckba6adf62015-02-19 14:36:50 -0800204}
205
John Reck34781b22017-07-05 16:39:36 -0700206void JankTracker::dumpFrames(int fd) {
John Reck47f5c3a2017-11-13 11:32:39 -0800207 dprintf(fd, "\n\n---PROFILEDATA---\n");
John Reck34781b22017-07-05 16:39:36 -0700208 for (size_t i = 0; i < static_cast<size_t>(FrameInfoIndex::NumIndexes); i++) {
John Reck47f5c3a2017-11-13 11:32:39 -0800209 dprintf(fd, "%s", FrameInfoNames[i].c_str());
210 dprintf(fd, ",");
John Reck34781b22017-07-05 16:39:36 -0700211 }
212 for (size_t i = 0; i < mFrames.size(); i++) {
213 FrameInfo& frame = mFrames[i];
214 if (frame[FrameInfoIndex::SyncStart] == 0) {
215 continue;
216 }
John Reck47f5c3a2017-11-13 11:32:39 -0800217 dprintf(fd, "\n");
John Reck34781b22017-07-05 16:39:36 -0700218 for (int i = 0; i < static_cast<int>(FrameInfoIndex::NumIndexes); i++) {
John Reck47f5c3a2017-11-13 11:32:39 -0800219 dprintf(fd, "%" PRId64 ",", frame[i]);
John Reck34781b22017-07-05 16:39:36 -0700220 }
221 }
John Reck47f5c3a2017-11-13 11:32:39 -0800222 dprintf(fd, "\n---PROFILEDATA---\n\n");
John Reck34781b22017-07-05 16:39:36 -0700223}
224
John Reckba6adf62015-02-19 14:36:50 -0800225void JankTracker::reset() {
John Reck34781b22017-07-05 16:39:36 -0700226 mFrames.clear();
John Reck7075c792017-07-05 14:03:43 -0700227 mData->reset();
John Reck34781b22017-07-05 16:39:36 -0700228 (*mGlobalData)->reset();
John Reck1bcacfd2017-11-03 10:12:19 -0700229 sFrameStart = Properties::filterOutTestOverhead ? FrameInfoIndex::HandleInputStart
230 : FrameInfoIndex::IntendedVsync;
John Reckba6adf62015-02-19 14:36:50 -0800231}
232
John Reckba6adf62015-02-19 14:36:50 -0800233} /* namespace uirenderer */
234} /* namespace android */