blob: 4a2e30dd38f238e58e5c8318dca37fdc5f106c10 [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>
Yangster-macba5bf0d2018-10-09 20:48:23 -070023#include <statslog.h>
Mark Salyzyn96bf5982016-09-28 16:15:30 -070024#include <sys/mman.h>
Mark Salyzyn52eb4e02016-09-28 16:15:30 -070025
26#include <algorithm>
John Reck5ed587f2016-03-24 15:57:01 -070027#include <cmath>
Mark Salyzyn52eb4e02016-09-28 16:15:30 -070028#include <cstdio>
29#include <limits>
John Reck0e89ca22017-12-15 16:00:48 -080030#include <sstream>
Mark Salyzyn52eb4e02016-09-28 16:15:30 -070031
Alec Mouri22d753f2019-09-05 17:11:45 -070032#include "DeviceInfo.h"
Mark Salyzyn52eb4e02016-09-28 16:15:30 -070033#include "Properties.h"
34#include "utils/TimeUtils.h"
John Reck0e89ca22017-12-15 16:00:48 -080035#include "utils/Trace.h"
Mark Salyzyn52eb4e02016-09-28 16:15:30 -070036
John Reckba6adf62015-02-19 14:36:50 -080037namespace android {
38namespace uirenderer {
39
John Reckba6adf62015-02-19 14:36:50 -080040struct Comparison {
John Reck0e486472018-03-19 14:06:16 -070041 JankType type;
42 std::function<int64_t(nsecs_t)> computeThreadshold;
John Reckc87be992015-02-20 10:57:22 -080043 FrameInfoIndex start;
44 FrameInfoIndex end;
John Reckba6adf62015-02-19 14:36:50 -080045};
46
John Reck0e486472018-03-19 14:06:16 -070047static const std::array<Comparison, 4> COMPARISONS{
48 Comparison{JankType::kMissedVsync, [](nsecs_t) { return 1; }, FrameInfoIndex::IntendedVsync,
49 FrameInfoIndex::Vsync},
50
51 Comparison{JankType::kSlowUI,
52 [](nsecs_t frameInterval) { return static_cast<int64_t>(.5 * frameInterval); },
53 FrameInfoIndex::Vsync, FrameInfoIndex::SyncStart},
54
55 Comparison{JankType::kSlowSync,
56 [](nsecs_t frameInterval) { return static_cast<int64_t>(.2 * frameInterval); },
57 FrameInfoIndex::SyncStart, FrameInfoIndex::IssueDrawCommandsStart},
58
59 Comparison{JankType::kSlowRT,
60 [](nsecs_t frameInterval) { return static_cast<int64_t>(.75 * frameInterval); },
61 FrameInfoIndex::IssueDrawCommandsStart, FrameInfoIndex::FrameCompleted},
John Reckba6adf62015-02-19 14:36:50 -080062};
63
64// If the event exceeds 10 seconds throw it away, this isn't a jank event
65// it's an ANR and will be handled as such
66static const int64_t IGNORE_EXCEEDING = seconds_to_nanoseconds(10);
67
68/*
John Reck66010802016-03-30 14:19:44 -070069 * We don't track direct-drawing via Surface:lockHardwareCanvas()
John Reckba6adf62015-02-19 14:36:50 -080070 * for now
71 *
72 * TODO: kSurfaceCanvas can negatively impact other drawing by using up
73 * time on the RenderThread, figure out how to attribute that as a jank-causer
74 */
John Reck66010802016-03-30 14:19:44 -070075static const int64_t EXEMPT_FRAMES_FLAGS = FrameInfoFlags::SurfaceCanvas;
John Reckba6adf62015-02-19 14:36:50 -080076
John Reckc7cd9cf2016-03-28 10:38:19 -070077// For testing purposes to try and eliminate test infra overhead we will
78// consider any unknown delay of frame start as part of the test infrastructure
79// and filter it out of the frame profile data
80static FrameInfoIndex sFrameStart = FrameInfoIndex::IntendedVsync;
81
Jorim Jaggi71db8892021-02-03 23:19:29 +010082JankTracker::JankTracker(ProfileDataContainer* globalData)
83 : mData(globalData->getDataMutex())
84 , mDataMutex(globalData->getDataMutex()) {
John Reck34781b22017-07-05 16:39:36 -070085 mGlobalData = globalData;
Alec Mouri4a818f12019-11-19 16:17:53 -080086 nsecs_t frameIntervalNanos = DeviceInfo::getVsyncPeriod();
Alec Mouri22d753f2019-09-05 17:11:45 -070087 nsecs_t sfOffset = DeviceInfo::getCompositorOffset();
88 nsecs_t offsetDelta = sfOffset - DeviceInfo::getAppOffset();
John Reck2d5b8d72016-07-28 15:36:11 -070089 // There are two different offset cases. If the offsetDelta is positive
90 // and small, then the intention is to give apps extra time by leveraging
91 // pipelining between the UI & RT threads. If the offsetDelta is large or
92 // negative, the intention is to subtract time from the total duration
93 // in which case we can't afford to wait for dequeueBuffer blockage.
94 if (offsetDelta <= 4_ms && offsetDelta >= 0) {
95 // SF will begin composition at VSYNC-app + offsetDelta. If we are triple
96 // buffered, this is the expected time at which dequeueBuffer will
97 // return due to the staggering of VSYNC-app & VSYNC-sf.
98 mDequeueTimeForgiveness = offsetDelta + 4_ms;
99 }
John Reckba6adf62015-02-19 14:36:50 -0800100 setFrameInterval(frameIntervalNanos);
101}
102
103void JankTracker::setFrameInterval(nsecs_t frameInterval) {
104 mFrameInterval = frameInterval;
John Reckba6adf62015-02-19 14:36:50 -0800105
John Reck0e486472018-03-19 14:06:16 -0700106 for (auto& comparison : COMPARISONS) {
107 mThresholds[comparison.type] = comparison.computeThreadshold(frameInterval);
108 }
John Reckba6adf62015-02-19 14:36:50 -0800109}
110
John Reck34781b22017-07-05 16:39:36 -0700111void JankTracker::finishFrame(const FrameInfo& frame) {
Jorim Jaggi71db8892021-02-03 23:19:29 +0100112 std::lock_guard lock(mDataMutex);
113
John Reckba6adf62015-02-19 14:36:50 -0800114 // Fast-path for jank-free frames
John Reck126720a2016-04-15 15:16:38 -0700115 int64_t totalDuration = frame.duration(sFrameStart, FrameInfoIndex::FrameCompleted);
John Reck1bcacfd2017-11-03 10:12:19 -0700116 if (mDequeueTimeForgiveness && frame[FrameInfoIndex::DequeueBufferDuration] > 500_us) {
117 nsecs_t expectedDequeueDuration = mDequeueTimeForgiveness + frame[FrameInfoIndex::Vsync] -
118 frame[FrameInfoIndex::IssueDrawCommandsStart];
John Reck2d5b8d72016-07-28 15:36:11 -0700119 if (expectedDequeueDuration > 0) {
120 // Forgive only up to the expected amount, but not more than
121 // the actual time spent blocked.
John Reck1bcacfd2017-11-03 10:12:19 -0700122 nsecs_t forgiveAmount =
123 std::min(expectedDequeueDuration, frame[FrameInfoIndex::DequeueBufferDuration]);
John Reck1b7184f2017-03-27 14:47:46 -0700124 LOG_ALWAYS_FATAL_IF(forgiveAmount >= totalDuration,
John Reck1bcacfd2017-11-03 10:12:19 -0700125 "Impossible dequeue duration! dequeue duration reported %" PRId64
126 ", total duration %" PRId64,
127 forgiveAmount, totalDuration);
John Reck2d5b8d72016-07-28 15:36:11 -0700128 totalDuration -= forgiveAmount;
129 }
130 }
John Reck0e486472018-03-19 14:06:16 -0700131
Jorim Jaggi71db8892021-02-03 23:19:29 +0100132 LOG_ALWAYS_FATAL_IF(totalDuration <= 0, "Impossible totalDuration %" PRId64 " start=%" PRIi64
133 " gpuComplete=%" PRIi64, totalDuration,
134 frame[FrameInfoIndex::IntendedVsync],
135 frame[FrameInfoIndex::GpuCompleted]);
136
John Reck7075c792017-07-05 14:03:43 -0700137 mData->reportFrame(totalDuration);
John Reck34781b22017-07-05 16:39:36 -0700138 (*mGlobalData)->reportFrame(totalDuration);
John Reck7075c792017-07-05 14:03:43 -0700139
John Reck66010802016-03-30 14:19:44 -0700140 // Only things like Surface.lockHardwareCanvas() are exempt from tracking
John Reck0e486472018-03-19 14:06:16 -0700141 if (CC_UNLIKELY(frame[FrameInfoIndex::Flags] & EXEMPT_FRAMES_FLAGS)) {
John Reckba6adf62015-02-19 14:36:50 -0800142 return;
143 }
144
John Reck0e486472018-03-19 14:06:16 -0700145 if (totalDuration > mFrameInterval) {
146 mData->reportJank();
147 (*mGlobalData)->reportJank();
148 }
John Reckba6adf62015-02-19 14:36:50 -0800149
Zhiyin Luo (罗植尹)7a0d2242020-01-11 17:14:05 +0800150 if (mSwapDeadline < 0) {
151 mSwapDeadline = frame[FrameInfoIndex::IntendedVsync] + mFrameInterval;
152 }
Gao Shuo7e05ff32019-05-27 14:14:57 +0800153 bool isTripleBuffered = (mSwapDeadline - frame[FrameInfoIndex::IntendedVsync]) > (mFrameInterval * 0.1);
John Reck0e486472018-03-19 14:06:16 -0700154
155 mSwapDeadline = std::max(mSwapDeadline + mFrameInterval,
156 frame[FrameInfoIndex::IntendedVsync] + mFrameInterval);
157
158 // If we hit the deadline, cool!
John Reck50be3b82018-09-14 11:25:58 -0700159 if (frame[FrameInfoIndex::FrameCompleted] < mSwapDeadline || totalDuration < mFrameInterval) {
John Reck0e486472018-03-19 14:06:16 -0700160 if (isTripleBuffered) {
161 mData->reportJankType(JankType::kHighInputLatency);
162 (*mGlobalData)->reportJankType(JankType::kHighInputLatency);
163 }
164 return;
165 }
166
167 mData->reportJankType(JankType::kMissedDeadline);
168 (*mGlobalData)->reportJankType(JankType::kMissedDeadline);
169
170 // Janked, reset the swap deadline
171 nsecs_t jitterNanos = frame[FrameInfoIndex::FrameCompleted] - frame[FrameInfoIndex::Vsync];
172 nsecs_t lastFrameOffset = jitterNanos % mFrameInterval;
173 mSwapDeadline = frame[FrameInfoIndex::FrameCompleted] - lastFrameOffset + mFrameInterval;
174
175 for (auto& comparison : COMPARISONS) {
176 int64_t delta = frame.duration(comparison.start, comparison.end);
177 if (delta >= mThresholds[comparison.type] && delta < IGNORE_EXCEEDING) {
178 mData->reportJankType(comparison.type);
179 (*mGlobalData)->reportJankType(comparison.type);
John Reckba6adf62015-02-19 14:36:50 -0800180 }
181 }
John Reck0e89ca22017-12-15 16:00:48 -0800182
183 // Log daveys since they are weird and we don't know what they are (b/70339576)
184 if (totalDuration >= 700_ms) {
185 static int sDaveyCount = 0;
186 std::stringstream ss;
187 ss << "Davey! duration=" << ns2ms(totalDuration) << "ms; ";
188 for (size_t i = 0; i < static_cast<size_t>(FrameInfoIndex::NumIndexes); i++) {
189 ss << FrameInfoNames[i] << "=" << frame[i] << ", ";
190 }
191 ALOGI("%s", ss.str().c_str());
192 // Just so we have something that counts up, the value is largely irrelevant
193 ATRACE_INT(ss.str().c_str(), ++sDaveyCount);
194 }
John Reckba6adf62015-02-19 14:36:50 -0800195}
196
John Reck1bcacfd2017-11-03 10:12:19 -0700197void JankTracker::dumpData(int fd, const ProfileDataDescription* description,
198 const ProfileData* data) {
Jorim Jaggi71db8892021-02-03 23:19:29 +0100199
John Reckdf1742e2017-01-19 15:56:21 -0800200 if (description) {
201 switch (description->type) {
202 case JankTrackerType::Generic:
203 break;
204 case JankTrackerType::Package:
205 dprintf(fd, "\nPackage: %s", description->name.c_str());
206 break;
207 case JankTrackerType::Window:
208 dprintf(fd, "\nWindow: %s", description->name.c_str());
209 break;
210 }
John Reckba6adf62015-02-19 14:36:50 -0800211 }
John Reckc7cd9cf2016-03-28 10:38:19 -0700212 if (sFrameStart != FrameInfoIndex::IntendedVsync) {
213 dprintf(fd, "\nNote: Data has been filtered!");
214 }
John Reck7075c792017-07-05 14:03:43 -0700215 data->dump(fd);
John Reckedc524c2015-03-18 15:24:33 -0700216 dprintf(fd, "\n");
John Reckba6adf62015-02-19 14:36:50 -0800217}
218
John Reck34781b22017-07-05 16:39:36 -0700219void JankTracker::dumpFrames(int fd) {
John Reck47f5c3a2017-11-13 11:32:39 -0800220 dprintf(fd, "\n\n---PROFILEDATA---\n");
John Reck34781b22017-07-05 16:39:36 -0700221 for (size_t i = 0; i < static_cast<size_t>(FrameInfoIndex::NumIndexes); i++) {
John Reck47f5c3a2017-11-13 11:32:39 -0800222 dprintf(fd, "%s", FrameInfoNames[i].c_str());
223 dprintf(fd, ",");
John Reck34781b22017-07-05 16:39:36 -0700224 }
225 for (size_t i = 0; i < mFrames.size(); i++) {
226 FrameInfo& frame = mFrames[i];
227 if (frame[FrameInfoIndex::SyncStart] == 0) {
228 continue;
229 }
John Reck47f5c3a2017-11-13 11:32:39 -0800230 dprintf(fd, "\n");
John Reck34781b22017-07-05 16:39:36 -0700231 for (int i = 0; i < static_cast<int>(FrameInfoIndex::NumIndexes); i++) {
John Reck47f5c3a2017-11-13 11:32:39 -0800232 dprintf(fd, "%" PRId64 ",", frame[i]);
John Reck34781b22017-07-05 16:39:36 -0700233 }
234 }
John Reck47f5c3a2017-11-13 11:32:39 -0800235 dprintf(fd, "\n---PROFILEDATA---\n\n");
John Reck34781b22017-07-05 16:39:36 -0700236}
237
John Reckba6adf62015-02-19 14:36:50 -0800238void JankTracker::reset() {
Jorim Jaggi71db8892021-02-03 23:19:29 +0100239 std::lock_guard lock(mDataMutex);
John Reck34781b22017-07-05 16:39:36 -0700240 mFrames.clear();
John Reck7075c792017-07-05 14:03:43 -0700241 mData->reset();
John Reck34781b22017-07-05 16:39:36 -0700242 (*mGlobalData)->reset();
John Reck1bcacfd2017-11-03 10:12:19 -0700243 sFrameStart = Properties::filterOutTestOverhead ? FrameInfoIndex::HandleInputStart
244 : FrameInfoIndex::IntendedVsync;
John Reckba6adf62015-02-19 14:36:50 -0800245}
246
Stan Iliev7203e1f2019-07-25 13:12:02 -0400247void JankTracker::finishGpuDraw(const FrameInfo& frame) {
Jorim Jaggi71db8892021-02-03 23:19:29 +0100248 std::lock_guard lock(mDataMutex);
Stan Iliev7203e1f2019-07-25 13:12:02 -0400249 int64_t totalGPUDrawTime = frame.gpuDrawTime();
250 if (totalGPUDrawTime >= 0) {
251 mData->reportGPUFrame(totalGPUDrawTime);
252 (*mGlobalData)->reportGPUFrame(totalGPUDrawTime);
253 }
254}
255
John Reckba6adf62015-02-19 14:36:50 -0800256} /* namespace uirenderer */
257} /* namespace android */