blob: f9671ed437710f2647074bcffd811ee860f385e2 [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>
30
31#include "Properties.h"
32#include "utils/TimeUtils.h"
33
John Reckba6adf62015-02-19 14:36:50 -080034namespace android {
35namespace uirenderer {
36
John Reckba6adf62015-02-19 14:36:50 -080037struct Comparison {
John Reckc87be992015-02-20 10:57:22 -080038 FrameInfoIndex start;
39 FrameInfoIndex end;
John Reckba6adf62015-02-19 14:36:50 -080040};
41
42static const Comparison COMPARISONS[] = {
Chris Craik1b54fb22015-06-02 17:40:58 -070043 {FrameInfoIndex::IntendedVsync, FrameInfoIndex::Vsync},
44 {FrameInfoIndex::OldestInputEvent, FrameInfoIndex::Vsync},
45 {FrameInfoIndex::Vsync, FrameInfoIndex::SyncStart},
46 {FrameInfoIndex::SyncStart, FrameInfoIndex::IssueDrawCommandsStart},
47 {FrameInfoIndex::IssueDrawCommandsStart, FrameInfoIndex::FrameCompleted},
John Reckba6adf62015-02-19 14:36:50 -080048};
49
50// If the event exceeds 10 seconds throw it away, this isn't a jank event
51// it's an ANR and will be handled as such
52static const int64_t IGNORE_EXCEEDING = seconds_to_nanoseconds(10);
53
54/*
John Reck66010802016-03-30 14:19:44 -070055 * We don't track direct-drawing via Surface:lockHardwareCanvas()
John Reckba6adf62015-02-19 14:36:50 -080056 * for now
57 *
58 * TODO: kSurfaceCanvas can negatively impact other drawing by using up
59 * time on the RenderThread, figure out how to attribute that as a jank-causer
60 */
John Reck66010802016-03-30 14:19:44 -070061static const int64_t EXEMPT_FRAMES_FLAGS = FrameInfoFlags::SurfaceCanvas;
John Reckba6adf62015-02-19 14:36:50 -080062
John Reckc7cd9cf2016-03-28 10:38:19 -070063// For testing purposes to try and eliminate test infra overhead we will
64// consider any unknown delay of frame start as part of the test infrastructure
65// and filter it out of the frame profile data
66static FrameInfoIndex sFrameStart = FrameInfoIndex::IntendedVsync;
67
John Reck2d5b8d72016-07-28 15:36:11 -070068JankTracker::JankTracker(const DisplayInfo& displayInfo) {
John Reckedc524c2015-03-18 15:24:33 -070069 // By default this will use malloc memory. It may be moved later to ashmem
70 // if there is shared space for it and a request comes in to do that.
71 mData = new ProfileData;
John Reckba6adf62015-02-19 14:36:50 -080072 reset();
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
John Reckedc524c2015-03-18 15:24:33 -070092JankTracker::~JankTracker() {
93 freeData();
94}
95
96void JankTracker::freeData() {
97 if (mIsMapped) {
98 munmap(mData, sizeof(ProfileData));
99 } else {
100 delete mData;
101 }
102 mIsMapped = false;
103 mData = nullptr;
104}
105
John Reckdf1742e2017-01-19 15:56:21 -0800106void JankTracker::rotateStorage() {
107 // If we are mapped we want to stop using the ashmem backend and switch to malloc
108 // We are expecting a switchStorageToAshmem call to follow this, but it's not guaranteed
109 // If we aren't sitting on top of ashmem then just do a reset() as it's functionally
110 // equivalent do a free, malloc, reset.
111 if (mIsMapped) {
112 freeData();
113 mData = new ProfileData;
114 }
115 reset();
116}
117
John Reckedc524c2015-03-18 15:24:33 -0700118void JankTracker::switchStorageToAshmem(int ashmemfd) {
119 int regionSize = ashmem_get_size_region(ashmemfd);
John Reckdf1742e2017-01-19 15:56:21 -0800120 if (regionSize < 0) {
121 int err = errno;
122 ALOGW("Failed to get ashmem region size from fd %d, err %d %s", ashmemfd, err, strerror(err));
123 return;
124 }
John Reckedc524c2015-03-18 15:24:33 -0700125 if (regionSize < static_cast<int>(sizeof(ProfileData))) {
126 ALOGW("Ashmem region is too small! Received %d, required %u",
John Reck98fa0a32015-03-31 12:03:51 -0700127 regionSize, static_cast<unsigned int>(sizeof(ProfileData)));
John Reckedc524c2015-03-18 15:24:33 -0700128 return;
129 }
130 ProfileData* newData = reinterpret_cast<ProfileData*>(
131 mmap(NULL, sizeof(ProfileData), PROT_READ | PROT_WRITE,
132 MAP_SHARED, ashmemfd, 0));
133 if (newData == MAP_FAILED) {
134 int err = errno;
135 ALOGW("Failed to move profile data to ashmem fd %d, error = %d",
136 ashmemfd, err);
137 return;
138 }
139
John Reck7075c792017-07-05 14:03:43 -0700140 newData->mergeWith(*mData);
John Reckedc524c2015-03-18 15:24:33 -0700141 freeData();
142 mData = newData;
143 mIsMapped = true;
144}
145
John Reckba6adf62015-02-19 14:36:50 -0800146void JankTracker::setFrameInterval(nsecs_t frameInterval) {
147 mFrameInterval = frameInterval;
148 mThresholds[kMissedVsync] = 1;
149 /*
150 * Due to interpolation and sample rate differences between the touch
151 * panel and the display (example, 85hz touch panel driving a 60hz display)
152 * we call high latency 1.5 * frameinterval
153 *
154 * NOTE: Be careful when tuning this! A theoretical 1,000hz touch panel
155 * on a 60hz display will show kOldestInputEvent - kIntendedVsync of being 15ms
156 * Thus this must always be larger than frameInterval, or it will fail
157 */
158 mThresholds[kHighInputLatency] = static_cast<int64_t>(1.5 * frameInterval);
159
160 // Note that these do not add up to 1. This is intentional. It's to deal
161 // with variance in values, and should be sort of an upper-bound on what
162 // is reasonable to expect.
163 mThresholds[kSlowUI] = static_cast<int64_t>(.5 * frameInterval);
164 mThresholds[kSlowSync] = static_cast<int64_t>(.2 * frameInterval);
165 mThresholds[kSlowRT] = static_cast<int64_t>(.75 * frameInterval);
166
167}
168
169void JankTracker::addFrame(const FrameInfo& frame) {
John Reckba6adf62015-02-19 14:36:50 -0800170 // Fast-path for jank-free frames
John Reck126720a2016-04-15 15:16:38 -0700171 int64_t totalDuration = frame.duration(sFrameStart, FrameInfoIndex::FrameCompleted);
John Reck2d5b8d72016-07-28 15:36:11 -0700172 if (mDequeueTimeForgiveness
173 && frame[FrameInfoIndex::DequeueBufferDuration] > 500_us) {
174 nsecs_t expectedDequeueDuration =
175 mDequeueTimeForgiveness + frame[FrameInfoIndex::Vsync]
176 - frame[FrameInfoIndex::IssueDrawCommandsStart];
177 if (expectedDequeueDuration > 0) {
178 // Forgive only up to the expected amount, but not more than
179 // the actual time spent blocked.
180 nsecs_t forgiveAmount = std::min(expectedDequeueDuration,
181 frame[FrameInfoIndex::DequeueBufferDuration]);
John Reck1b7184f2017-03-27 14:47:46 -0700182 LOG_ALWAYS_FATAL_IF(forgiveAmount >= totalDuration,
183 "Impossible dequeue duration! dequeue duration reported %" PRId64
184 ", total duration %" PRId64, forgiveAmount, totalDuration);
John Reck2d5b8d72016-07-28 15:36:11 -0700185 totalDuration -= forgiveAmount;
186 }
187 }
John Reck1b7184f2017-03-27 14:47:46 -0700188 LOG_ALWAYS_FATAL_IF(totalDuration <= 0, "Impossible totalDuration %" PRId64, totalDuration);
John Reck7075c792017-07-05 14:03:43 -0700189 mData->reportFrame(totalDuration);
190
John Recke70c5752015-03-06 14:40:50 -0800191 // Keep the fast path as fast as possible.
John Reckba6adf62015-02-19 14:36:50 -0800192 if (CC_LIKELY(totalDuration < mFrameInterval)) {
193 return;
194 }
195
John Reck66010802016-03-30 14:19:44 -0700196 // Only things like Surface.lockHardwareCanvas() are exempt from tracking
Chris Craik1b54fb22015-06-02 17:40:58 -0700197 if (frame[FrameInfoIndex::Flags] & EXEMPT_FRAMES_FLAGS) {
John Reckba6adf62015-02-19 14:36:50 -0800198 return;
199 }
200
John Reck7075c792017-07-05 14:03:43 -0700201 mData->reportJank();
John Reckba6adf62015-02-19 14:36:50 -0800202
203 for (int i = 0; i < NUM_BUCKETS; i++) {
John Reckbe3fba02015-07-06 13:49:58 -0700204 int64_t delta = frame.duration(COMPARISONS[i].start, COMPARISONS[i].end);
John Reckba6adf62015-02-19 14:36:50 -0800205 if (delta >= mThresholds[i] && delta < IGNORE_EXCEEDING) {
John Reck7075c792017-07-05 14:03:43 -0700206 mData->reportJankType((JankType) i);
John Reckba6adf62015-02-19 14:36:50 -0800207 }
208 }
209}
210
John Reckdf1742e2017-01-19 15:56:21 -0800211void JankTracker::dumpData(int fd, const ProfileDataDescription* description, const ProfileData* data) {
212 if (description) {
213 switch (description->type) {
214 case JankTrackerType::Generic:
215 break;
216 case JankTrackerType::Package:
217 dprintf(fd, "\nPackage: %s", description->name.c_str());
218 break;
219 case JankTrackerType::Window:
220 dprintf(fd, "\nWindow: %s", description->name.c_str());
221 break;
222 }
John Reckba6adf62015-02-19 14:36:50 -0800223 }
John Reckc7cd9cf2016-03-28 10:38:19 -0700224 if (sFrameStart != FrameInfoIndex::IntendedVsync) {
225 dprintf(fd, "\nNote: Data has been filtered!");
226 }
John Reck7075c792017-07-05 14:03:43 -0700227 data->dump(fd);
John Reckedc524c2015-03-18 15:24:33 -0700228 dprintf(fd, "\n");
John Reckba6adf62015-02-19 14:36:50 -0800229}
230
231void JankTracker::reset() {
John Reck7075c792017-07-05 14:03:43 -0700232 mData->reset();
John Reckc7cd9cf2016-03-28 10:38:19 -0700233 sFrameStart = Properties::filterOutTestOverhead
234 ? FrameInfoIndex::HandleInputStart
235 : FrameInfoIndex::IntendedVsync;
John Reckba6adf62015-02-19 14:36:50 -0800236}
237
John Reckba6adf62015-02-19 14:36:50 -0800238} /* namespace uirenderer */
239} /* namespace android */