blob: 8110664ad44b0d27744b1c6bcf5d506d24896bc7 [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>
Tej Singhbb8554a2018-01-26 11:59:14 -080021#include <statslog.h>
Mark Salyzyn96bf5982016-09-28 16:15:30 -070022#include <sys/mman.h>
Mark Salyzyn52eb4e02016-09-28 16:15:30 -070023
24#include <algorithm>
John Reck5ed587f2016-03-24 15:57:01 -070025#include <cmath>
Mark Salyzyn52eb4e02016-09-28 16:15:30 -070026#include <cstdio>
27#include <limits>
John Reckba6adf62015-02-19 14:36:50 -080028
Mark Salyzyn52eb4e02016-09-28 16:15:30 -070029#include <cutils/ashmem.h>
30#include <log/log.h>
John Reck0e89ca22017-12-15 16:00:48 -080031#include <sstream>
Mark Salyzyn52eb4e02016-09-28 16:15:30 -070032
33#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 Reckc87be992015-02-20 10:57:22 -080041 FrameInfoIndex start;
42 FrameInfoIndex end;
John Reckba6adf62015-02-19 14:36:50 -080043};
44
45static const Comparison COMPARISONS[] = {
Chris Craik1b54fb22015-06-02 17:40:58 -070046 {FrameInfoIndex::IntendedVsync, FrameInfoIndex::Vsync},
47 {FrameInfoIndex::OldestInputEvent, FrameInfoIndex::Vsync},
48 {FrameInfoIndex::Vsync, FrameInfoIndex::SyncStart},
49 {FrameInfoIndex::SyncStart, FrameInfoIndex::IssueDrawCommandsStart},
50 {FrameInfoIndex::IssueDrawCommandsStart, FrameInfoIndex::FrameCompleted},
John Reckba6adf62015-02-19 14:36:50 -080051};
52
53// If the event exceeds 10 seconds throw it away, this isn't a jank event
54// it's an ANR and will be handled as such
55static const int64_t IGNORE_EXCEEDING = seconds_to_nanoseconds(10);
56
57/*
John Reck66010802016-03-30 14:19:44 -070058 * We don't track direct-drawing via Surface:lockHardwareCanvas()
John Reckba6adf62015-02-19 14:36:50 -080059 * for now
60 *
61 * TODO: kSurfaceCanvas can negatively impact other drawing by using up
62 * time on the RenderThread, figure out how to attribute that as a jank-causer
63 */
John Reck66010802016-03-30 14:19:44 -070064static const int64_t EXEMPT_FRAMES_FLAGS = FrameInfoFlags::SurfaceCanvas;
John Reckba6adf62015-02-19 14:36:50 -080065
John Reckc7cd9cf2016-03-28 10:38:19 -070066// For testing purposes to try and eliminate test infra overhead we will
67// consider any unknown delay of frame start as part of the test infrastructure
68// and filter it out of the frame profile data
69static FrameInfoIndex sFrameStart = FrameInfoIndex::IntendedVsync;
70
John Reck34781b22017-07-05 16:39:36 -070071JankTracker::JankTracker(ProfileDataContainer* globalData, const DisplayInfo& displayInfo) {
72 mGlobalData = globalData;
John Reck2d5b8d72016-07-28 15:36:11 -070073 nsecs_t frameIntervalNanos = static_cast<nsecs_t>(1_s / displayInfo.fps);
74#if USE_HWC2
75 nsecs_t sfOffset = frameIntervalNanos - (displayInfo.presentationDeadline - 1_ms);
76 nsecs_t offsetDelta = sfOffset - displayInfo.appVsyncOffset;
77 // There are two different offset cases. If the offsetDelta is positive
78 // and small, then the intention is to give apps extra time by leveraging
79 // pipelining between the UI & RT threads. If the offsetDelta is large or
80 // negative, the intention is to subtract time from the total duration
81 // in which case we can't afford to wait for dequeueBuffer blockage.
82 if (offsetDelta <= 4_ms && offsetDelta >= 0) {
83 // SF will begin composition at VSYNC-app + offsetDelta. If we are triple
84 // buffered, this is the expected time at which dequeueBuffer will
85 // return due to the staggering of VSYNC-app & VSYNC-sf.
86 mDequeueTimeForgiveness = offsetDelta + 4_ms;
87 }
88#endif
John Reckba6adf62015-02-19 14:36:50 -080089 setFrameInterval(frameIntervalNanos);
90}
91
92void JankTracker::setFrameInterval(nsecs_t frameInterval) {
93 mFrameInterval = frameInterval;
94 mThresholds[kMissedVsync] = 1;
95 /*
96 * Due to interpolation and sample rate differences between the touch
97 * panel and the display (example, 85hz touch panel driving a 60hz display)
98 * we call high latency 1.5 * frameinterval
99 *
100 * NOTE: Be careful when tuning this! A theoretical 1,000hz touch panel
101 * on a 60hz display will show kOldestInputEvent - kIntendedVsync of being 15ms
102 * Thus this must always be larger than frameInterval, or it will fail
103 */
104 mThresholds[kHighInputLatency] = static_cast<int64_t>(1.5 * frameInterval);
105
106 // Note that these do not add up to 1. This is intentional. It's to deal
107 // with variance in values, and should be sort of an upper-bound on what
108 // is reasonable to expect.
109 mThresholds[kSlowUI] = static_cast<int64_t>(.5 * frameInterval);
110 mThresholds[kSlowSync] = static_cast<int64_t>(.2 * frameInterval);
111 mThresholds[kSlowRT] = static_cast<int64_t>(.75 * frameInterval);
John Reckba6adf62015-02-19 14:36:50 -0800112}
113
John Reck34781b22017-07-05 16:39:36 -0700114void JankTracker::finishFrame(const FrameInfo& frame) {
John Reckba6adf62015-02-19 14:36:50 -0800115 // Fast-path for jank-free frames
John Reck126720a2016-04-15 15:16:38 -0700116 int64_t totalDuration = frame.duration(sFrameStart, FrameInfoIndex::FrameCompleted);
John Reck1bcacfd2017-11-03 10:12:19 -0700117 if (mDequeueTimeForgiveness && frame[FrameInfoIndex::DequeueBufferDuration] > 500_us) {
118 nsecs_t expectedDequeueDuration = mDequeueTimeForgiveness + frame[FrameInfoIndex::Vsync] -
119 frame[FrameInfoIndex::IssueDrawCommandsStart];
John Reck2d5b8d72016-07-28 15:36:11 -0700120 if (expectedDequeueDuration > 0) {
121 // Forgive only up to the expected amount, but not more than
122 // the actual time spent blocked.
John Reck1bcacfd2017-11-03 10:12:19 -0700123 nsecs_t forgiveAmount =
124 std::min(expectedDequeueDuration, frame[FrameInfoIndex::DequeueBufferDuration]);
John Reck1b7184f2017-03-27 14:47:46 -0700125 LOG_ALWAYS_FATAL_IF(forgiveAmount >= totalDuration,
John Reck1bcacfd2017-11-03 10:12:19 -0700126 "Impossible dequeue duration! dequeue duration reported %" PRId64
127 ", total duration %" PRId64,
128 forgiveAmount, totalDuration);
John Reck2d5b8d72016-07-28 15:36:11 -0700129 totalDuration -= forgiveAmount;
130 }
131 }
John Reck09979fb2018-03-12 17:14:53 -0700132
John Reck1b7184f2017-03-27 14:47:46 -0700133 LOG_ALWAYS_FATAL_IF(totalDuration <= 0, "Impossible totalDuration %" PRId64, totalDuration);
John Reck7075c792017-07-05 14:03:43 -0700134 mData->reportFrame(totalDuration);
John Reck34781b22017-07-05 16:39:36 -0700135 (*mGlobalData)->reportFrame(totalDuration);
John Reck7075c792017-07-05 14:03:43 -0700136
John Reck66010802016-03-30 14:19:44 -0700137 // Only things like Surface.lockHardwareCanvas() are exempt from tracking
John Reck09979fb2018-03-12 17:14:53 -0700138 if (CC_UNLIKELY(frame[FrameInfoIndex::Flags] & EXEMPT_FRAMES_FLAGS)) {
John Reckba6adf62015-02-19 14:36:50 -0800139 return;
140 }
141
John Reck09979fb2018-03-12 17:14:53 -0700142 if (totalDuration > mFrameInterval) {
143 mData->reportJank();
144 (*mGlobalData)->reportJank();
145 }
146
147 bool isTripleBuffered = mSwapDeadline > frame[FrameInfoIndex::IntendedVsync];
148
149 mSwapDeadline = std::max(mSwapDeadline + mFrameInterval,
150 frame[FrameInfoIndex::IntendedVsync] + mFrameInterval);
151
152 // If we hit the deadline, cool!
153 if (frame[FrameInfoIndex::FrameCompleted] < mSwapDeadline) {
154 if (isTripleBuffered) {
155 mData->reportJankType(JankType::kHighInputLatency);
156 (*mGlobalData)->reportJankType(JankType::kHighInputLatency);
157 }
158 return;
159 }
160
161 mData->reportJankType(JankType::kMissedDeadline);
162 (*mGlobalData)->reportJankType(JankType::kMissedDeadline);
163
164 // Janked, reset the swap deadline
165 nsecs_t jitterNanos = frame[FrameInfoIndex::FrameCompleted] - frame[FrameInfoIndex::Vsync];
166 nsecs_t lastFrameOffset = jitterNanos % mFrameInterval;
167 mSwapDeadline = frame[FrameInfoIndex::FrameCompleted] - lastFrameOffset + mFrameInterval;
John Reckba6adf62015-02-19 14:36:50 -0800168
169 for (int i = 0; i < NUM_BUCKETS; i++) {
John Reckbe3fba02015-07-06 13:49:58 -0700170 int64_t delta = frame.duration(COMPARISONS[i].start, COMPARISONS[i].end);
John Reckba6adf62015-02-19 14:36:50 -0800171 if (delta >= mThresholds[i] && delta < IGNORE_EXCEEDING) {
John Reck1bcacfd2017-11-03 10:12:19 -0700172 mData->reportJankType((JankType)i);
173 (*mGlobalData)->reportJankType((JankType)i);
John Reckba6adf62015-02-19 14:36:50 -0800174 }
175 }
John Reck0e89ca22017-12-15 16:00:48 -0800176
177 // Log daveys since they are weird and we don't know what they are (b/70339576)
178 if (totalDuration >= 700_ms) {
179 static int sDaveyCount = 0;
180 std::stringstream ss;
181 ss << "Davey! duration=" << ns2ms(totalDuration) << "ms; ";
182 for (size_t i = 0; i < static_cast<size_t>(FrameInfoIndex::NumIndexes); i++) {
183 ss << FrameInfoNames[i] << "=" << frame[i] << ", ";
184 }
185 ALOGI("%s", ss.str().c_str());
186 // Just so we have something that counts up, the value is largely irrelevant
187 ATRACE_INT(ss.str().c_str(), ++sDaveyCount);
David Chen77ef6712018-02-23 18:23:42 -0800188 android::util::stats_write(android::util::DAVEY_OCCURRED, getuid(), ns2ms(totalDuration));
John Reck0e89ca22017-12-15 16:00:48 -0800189 }
John Reckba6adf62015-02-19 14:36:50 -0800190}
191
John Reck1bcacfd2017-11-03 10:12:19 -0700192void JankTracker::dumpData(int fd, const ProfileDataDescription* description,
193 const ProfileData* data) {
John Reckdf1742e2017-01-19 15:56:21 -0800194 if (description) {
195 switch (description->type) {
196 case JankTrackerType::Generic:
197 break;
198 case JankTrackerType::Package:
199 dprintf(fd, "\nPackage: %s", description->name.c_str());
200 break;
201 case JankTrackerType::Window:
202 dprintf(fd, "\nWindow: %s", description->name.c_str());
203 break;
204 }
John Reckba6adf62015-02-19 14:36:50 -0800205 }
John Reckc7cd9cf2016-03-28 10:38:19 -0700206 if (sFrameStart != FrameInfoIndex::IntendedVsync) {
207 dprintf(fd, "\nNote: Data has been filtered!");
208 }
John Reck7075c792017-07-05 14:03:43 -0700209 data->dump(fd);
John Reckedc524c2015-03-18 15:24:33 -0700210 dprintf(fd, "\n");
John Reckba6adf62015-02-19 14:36:50 -0800211}
212
John Reck34781b22017-07-05 16:39:36 -0700213void JankTracker::dumpFrames(int fd) {
John Reck47f5c3a2017-11-13 11:32:39 -0800214 dprintf(fd, "\n\n---PROFILEDATA---\n");
John Reck34781b22017-07-05 16:39:36 -0700215 for (size_t i = 0; i < static_cast<size_t>(FrameInfoIndex::NumIndexes); i++) {
John Reck47f5c3a2017-11-13 11:32:39 -0800216 dprintf(fd, "%s", FrameInfoNames[i].c_str());
217 dprintf(fd, ",");
John Reck34781b22017-07-05 16:39:36 -0700218 }
219 for (size_t i = 0; i < mFrames.size(); i++) {
220 FrameInfo& frame = mFrames[i];
221 if (frame[FrameInfoIndex::SyncStart] == 0) {
222 continue;
223 }
John Reck47f5c3a2017-11-13 11:32:39 -0800224 dprintf(fd, "\n");
John Reck34781b22017-07-05 16:39:36 -0700225 for (int i = 0; i < static_cast<int>(FrameInfoIndex::NumIndexes); i++) {
John Reck47f5c3a2017-11-13 11:32:39 -0800226 dprintf(fd, "%" PRId64 ",", frame[i]);
John Reck34781b22017-07-05 16:39:36 -0700227 }
228 }
John Reck47f5c3a2017-11-13 11:32:39 -0800229 dprintf(fd, "\n---PROFILEDATA---\n\n");
John Reck34781b22017-07-05 16:39:36 -0700230}
231
John Reckba6adf62015-02-19 14:36:50 -0800232void JankTracker::reset() {
John Reck34781b22017-07-05 16:39:36 -0700233 mFrames.clear();
John Reck7075c792017-07-05 14:03:43 -0700234 mData->reset();
John Reck34781b22017-07-05 16:39:36 -0700235 (*mGlobalData)->reset();
John Reck1bcacfd2017-11-03 10:12:19 -0700236 sFrameStart = Properties::filterOutTestOverhead ? FrameInfoIndex::HandleInputStart
237 : FrameInfoIndex::IntendedVsync;
John Reckba6adf62015-02-19 14:36:50 -0800238}
239
John Reckba6adf62015-02-19 14:36:50 -0800240} /* namespace uirenderer */
241} /* namespace android */