blob: f41956cdebeb4363f30c785a87c0bc08082c1055 [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 Reckc87be992015-02-20 10:57:22 -080040 FrameInfoIndex start;
41 FrameInfoIndex end;
John Reckba6adf62015-02-19 14:36:50 -080042};
43
44static const Comparison COMPARISONS[] = {
Chris Craik1b54fb22015-06-02 17:40:58 -070045 {FrameInfoIndex::IntendedVsync, FrameInfoIndex::Vsync},
46 {FrameInfoIndex::OldestInputEvent, FrameInfoIndex::Vsync},
47 {FrameInfoIndex::Vsync, FrameInfoIndex::SyncStart},
48 {FrameInfoIndex::SyncStart, FrameInfoIndex::IssueDrawCommandsStart},
49 {FrameInfoIndex::IssueDrawCommandsStart, FrameInfoIndex::FrameCompleted},
John Reckba6adf62015-02-19 14:36:50 -080050};
51
52// If the event exceeds 10 seconds throw it away, this isn't a jank event
53// it's an ANR and will be handled as such
54static const int64_t IGNORE_EXCEEDING = seconds_to_nanoseconds(10);
55
56/*
John Reck66010802016-03-30 14:19:44 -070057 * We don't track direct-drawing via Surface:lockHardwareCanvas()
John Reckba6adf62015-02-19 14:36:50 -080058 * for now
59 *
60 * TODO: kSurfaceCanvas can negatively impact other drawing by using up
61 * time on the RenderThread, figure out how to attribute that as a jank-causer
62 */
John Reck66010802016-03-30 14:19:44 -070063static const int64_t EXEMPT_FRAMES_FLAGS = FrameInfoFlags::SurfaceCanvas;
John Reckba6adf62015-02-19 14:36:50 -080064
John Reckc7cd9cf2016-03-28 10:38:19 -070065// For testing purposes to try and eliminate test infra overhead we will
66// consider any unknown delay of frame start as part of the test infrastructure
67// and filter it out of the frame profile data
68static FrameInfoIndex sFrameStart = FrameInfoIndex::IntendedVsync;
69
John Reck34781b22017-07-05 16:39:36 -070070JankTracker::JankTracker(ProfileDataContainer* globalData, const DisplayInfo& displayInfo) {
71 mGlobalData = globalData;
John Reck2d5b8d72016-07-28 15:36:11 -070072 nsecs_t frameIntervalNanos = static_cast<nsecs_t>(1_s / displayInfo.fps);
73#if USE_HWC2
74 nsecs_t sfOffset = frameIntervalNanos - (displayInfo.presentationDeadline - 1_ms);
75 nsecs_t offsetDelta = sfOffset - displayInfo.appVsyncOffset;
76 // There are two different offset cases. If the offsetDelta is positive
77 // and small, then the intention is to give apps extra time by leveraging
78 // pipelining between the UI & RT threads. If the offsetDelta is large or
79 // negative, the intention is to subtract time from the total duration
80 // in which case we can't afford to wait for dequeueBuffer blockage.
81 if (offsetDelta <= 4_ms && offsetDelta >= 0) {
82 // SF will begin composition at VSYNC-app + offsetDelta. If we are triple
83 // buffered, this is the expected time at which dequeueBuffer will
84 // return due to the staggering of VSYNC-app & VSYNC-sf.
85 mDequeueTimeForgiveness = offsetDelta + 4_ms;
86 }
87#endif
John Reckba6adf62015-02-19 14:36:50 -080088 setFrameInterval(frameIntervalNanos);
89}
90
91void JankTracker::setFrameInterval(nsecs_t frameInterval) {
92 mFrameInterval = frameInterval;
93 mThresholds[kMissedVsync] = 1;
94 /*
95 * Due to interpolation and sample rate differences between the touch
96 * panel and the display (example, 85hz touch panel driving a 60hz display)
97 * we call high latency 1.5 * frameinterval
98 *
99 * NOTE: Be careful when tuning this! A theoretical 1,000hz touch panel
100 * on a 60hz display will show kOldestInputEvent - kIntendedVsync of being 15ms
101 * Thus this must always be larger than frameInterval, or it will fail
102 */
103 mThresholds[kHighInputLatency] = static_cast<int64_t>(1.5 * frameInterval);
104
105 // Note that these do not add up to 1. This is intentional. It's to deal
106 // with variance in values, and should be sort of an upper-bound on what
107 // is reasonable to expect.
108 mThresholds[kSlowUI] = static_cast<int64_t>(.5 * frameInterval);
109 mThresholds[kSlowSync] = static_cast<int64_t>(.2 * frameInterval);
110 mThresholds[kSlowRT] = static_cast<int64_t>(.75 * frameInterval);
John Reckba6adf62015-02-19 14:36:50 -0800111}
112
John Reck34781b22017-07-05 16:39:36 -0700113void JankTracker::finishFrame(const FrameInfo& frame) {
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 Reck1b7184f2017-03-27 14:47:46 -0700131 LOG_ALWAYS_FATAL_IF(totalDuration <= 0, "Impossible totalDuration %" PRId64, totalDuration);
John Reck7075c792017-07-05 14:03:43 -0700132 mData->reportFrame(totalDuration);
John Reck34781b22017-07-05 16:39:36 -0700133 (*mGlobalData)->reportFrame(totalDuration);
John Reck7075c792017-07-05 14:03:43 -0700134
John Recke70c5752015-03-06 14:40:50 -0800135 // Keep the fast path as fast as possible.
John Reckba6adf62015-02-19 14:36:50 -0800136 if (CC_LIKELY(totalDuration < mFrameInterval)) {
137 return;
138 }
139
John Reck66010802016-03-30 14:19:44 -0700140 // Only things like Surface.lockHardwareCanvas() are exempt from tracking
Chris Craik1b54fb22015-06-02 17:40:58 -0700141 if (frame[FrameInfoIndex::Flags] & EXEMPT_FRAMES_FLAGS) {
John Reckba6adf62015-02-19 14:36:50 -0800142 return;
143 }
144
John Reck7075c792017-07-05 14:03:43 -0700145 mData->reportJank();
John Reck34781b22017-07-05 16:39:36 -0700146 (*mGlobalData)->reportJank();
John Reckba6adf62015-02-19 14:36:50 -0800147
148 for (int i = 0; i < NUM_BUCKETS; i++) {
John Reckbe3fba02015-07-06 13:49:58 -0700149 int64_t delta = frame.duration(COMPARISONS[i].start, COMPARISONS[i].end);
John Reckba6adf62015-02-19 14:36:50 -0800150 if (delta >= mThresholds[i] && delta < IGNORE_EXCEEDING) {
John Reck1bcacfd2017-11-03 10:12:19 -0700151 mData->reportJankType((JankType)i);
152 (*mGlobalData)->reportJankType((JankType)i);
John Reckba6adf62015-02-19 14:36:50 -0800153 }
154 }
John Reck0e89ca22017-12-15 16:00:48 -0800155
156 // Log daveys since they are weird and we don't know what they are (b/70339576)
157 if (totalDuration >= 700_ms) {
158 static int sDaveyCount = 0;
159 std::stringstream ss;
160 ss << "Davey! duration=" << ns2ms(totalDuration) << "ms; ";
161 for (size_t i = 0; i < static_cast<size_t>(FrameInfoIndex::NumIndexes); i++) {
162 ss << FrameInfoNames[i] << "=" << frame[i] << ", ";
163 }
164 ALOGI("%s", ss.str().c_str());
165 // Just so we have something that counts up, the value is largely irrelevant
166 ATRACE_INT(ss.str().c_str(), ++sDaveyCount);
167 }
John Reckba6adf62015-02-19 14:36:50 -0800168}
169
John Reck1bcacfd2017-11-03 10:12:19 -0700170void JankTracker::dumpData(int fd, const ProfileDataDescription* description,
171 const ProfileData* data) {
John Reckdf1742e2017-01-19 15:56:21 -0800172 if (description) {
173 switch (description->type) {
174 case JankTrackerType::Generic:
175 break;
176 case JankTrackerType::Package:
177 dprintf(fd, "\nPackage: %s", description->name.c_str());
178 break;
179 case JankTrackerType::Window:
180 dprintf(fd, "\nWindow: %s", description->name.c_str());
181 break;
182 }
John Reckba6adf62015-02-19 14:36:50 -0800183 }
John Reckc7cd9cf2016-03-28 10:38:19 -0700184 if (sFrameStart != FrameInfoIndex::IntendedVsync) {
185 dprintf(fd, "\nNote: Data has been filtered!");
186 }
John Reck7075c792017-07-05 14:03:43 -0700187 data->dump(fd);
John Reckedc524c2015-03-18 15:24:33 -0700188 dprintf(fd, "\n");
John Reckba6adf62015-02-19 14:36:50 -0800189}
190
John Reck34781b22017-07-05 16:39:36 -0700191void JankTracker::dumpFrames(int fd) {
John Reck47f5c3a2017-11-13 11:32:39 -0800192 dprintf(fd, "\n\n---PROFILEDATA---\n");
John Reck34781b22017-07-05 16:39:36 -0700193 for (size_t i = 0; i < static_cast<size_t>(FrameInfoIndex::NumIndexes); i++) {
John Reck47f5c3a2017-11-13 11:32:39 -0800194 dprintf(fd, "%s", FrameInfoNames[i].c_str());
195 dprintf(fd, ",");
John Reck34781b22017-07-05 16:39:36 -0700196 }
197 for (size_t i = 0; i < mFrames.size(); i++) {
198 FrameInfo& frame = mFrames[i];
199 if (frame[FrameInfoIndex::SyncStart] == 0) {
200 continue;
201 }
John Reck47f5c3a2017-11-13 11:32:39 -0800202 dprintf(fd, "\n");
John Reck34781b22017-07-05 16:39:36 -0700203 for (int i = 0; i < static_cast<int>(FrameInfoIndex::NumIndexes); i++) {
John Reck47f5c3a2017-11-13 11:32:39 -0800204 dprintf(fd, "%" PRId64 ",", frame[i]);
John Reck34781b22017-07-05 16:39:36 -0700205 }
206 }
John Reck47f5c3a2017-11-13 11:32:39 -0800207 dprintf(fd, "\n---PROFILEDATA---\n\n");
John Reck34781b22017-07-05 16:39:36 -0700208}
209
John Reckba6adf62015-02-19 14:36:50 -0800210void JankTracker::reset() {
John Reck34781b22017-07-05 16:39:36 -0700211 mFrames.clear();
John Reck7075c792017-07-05 14:03:43 -0700212 mData->reset();
John Reck34781b22017-07-05 16:39:36 -0700213 (*mGlobalData)->reset();
John Reck1bcacfd2017-11-03 10:12:19 -0700214 sFrameStart = Properties::filterOutTestOverhead ? FrameInfoIndex::HandleInputStart
215 : FrameInfoIndex::IntendedVsync;
John Reckba6adf62015-02-19 14:36:50 -0800216}
217
John Reckba6adf62015-02-19 14:36:50 -0800218} /* namespace uirenderer */
219} /* namespace android */