blob: afdd339ba0a21b259f5760e8d681cb967c99c94e [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 Reck34781b22017-07-05 16:39:36 -070068JankTracker::JankTracker(ProfileDataContainer* globalData, const DisplayInfo& displayInfo) {
69 mGlobalData = globalData;
John Reck2d5b8d72016-07-28 15:36:11 -070070 nsecs_t frameIntervalNanos = static_cast<nsecs_t>(1_s / displayInfo.fps);
71#if USE_HWC2
72 nsecs_t sfOffset = frameIntervalNanos - (displayInfo.presentationDeadline - 1_ms);
73 nsecs_t offsetDelta = sfOffset - displayInfo.appVsyncOffset;
74 // There are two different offset cases. If the offsetDelta is positive
75 // and small, then the intention is to give apps extra time by leveraging
76 // pipelining between the UI & RT threads. If the offsetDelta is large or
77 // negative, the intention is to subtract time from the total duration
78 // in which case we can't afford to wait for dequeueBuffer blockage.
79 if (offsetDelta <= 4_ms && offsetDelta >= 0) {
80 // SF will begin composition at VSYNC-app + offsetDelta. If we are triple
81 // buffered, this is the expected time at which dequeueBuffer will
82 // return due to the staggering of VSYNC-app & VSYNC-sf.
83 mDequeueTimeForgiveness = offsetDelta + 4_ms;
84 }
85#endif
John Reckba6adf62015-02-19 14:36:50 -080086 setFrameInterval(frameIntervalNanos);
87}
88
89void JankTracker::setFrameInterval(nsecs_t frameInterval) {
90 mFrameInterval = frameInterval;
91 mThresholds[kMissedVsync] = 1;
92 /*
93 * Due to interpolation and sample rate differences between the touch
94 * panel and the display (example, 85hz touch panel driving a 60hz display)
95 * we call high latency 1.5 * frameinterval
96 *
97 * NOTE: Be careful when tuning this! A theoretical 1,000hz touch panel
98 * on a 60hz display will show kOldestInputEvent - kIntendedVsync of being 15ms
99 * Thus this must always be larger than frameInterval, or it will fail
100 */
101 mThresholds[kHighInputLatency] = static_cast<int64_t>(1.5 * frameInterval);
102
103 // Note that these do not add up to 1. This is intentional. It's to deal
104 // with variance in values, and should be sort of an upper-bound on what
105 // is reasonable to expect.
106 mThresholds[kSlowUI] = static_cast<int64_t>(.5 * frameInterval);
107 mThresholds[kSlowSync] = static_cast<int64_t>(.2 * frameInterval);
108 mThresholds[kSlowRT] = static_cast<int64_t>(.75 * frameInterval);
John Reckba6adf62015-02-19 14:36:50 -0800109}
110
John Reck34781b22017-07-05 16:39:36 -0700111void JankTracker::finishFrame(const FrameInfo& frame) {
John Reckba6adf62015-02-19 14:36:50 -0800112 // Fast-path for jank-free frames
John Reck126720a2016-04-15 15:16:38 -0700113 int64_t totalDuration = frame.duration(sFrameStart, FrameInfoIndex::FrameCompleted);
John Reck1bcacfd2017-11-03 10:12:19 -0700114 if (mDequeueTimeForgiveness && frame[FrameInfoIndex::DequeueBufferDuration] > 500_us) {
115 nsecs_t expectedDequeueDuration = mDequeueTimeForgiveness + frame[FrameInfoIndex::Vsync] -
116 frame[FrameInfoIndex::IssueDrawCommandsStart];
John Reck2d5b8d72016-07-28 15:36:11 -0700117 if (expectedDequeueDuration > 0) {
118 // Forgive only up to the expected amount, but not more than
119 // the actual time spent blocked.
John Reck1bcacfd2017-11-03 10:12:19 -0700120 nsecs_t forgiveAmount =
121 std::min(expectedDequeueDuration, frame[FrameInfoIndex::DequeueBufferDuration]);
John Reck1b7184f2017-03-27 14:47:46 -0700122 LOG_ALWAYS_FATAL_IF(forgiveAmount >= totalDuration,
John Reck1bcacfd2017-11-03 10:12:19 -0700123 "Impossible dequeue duration! dequeue duration reported %" PRId64
124 ", total duration %" PRId64,
125 forgiveAmount, totalDuration);
John Reck2d5b8d72016-07-28 15:36:11 -0700126 totalDuration -= forgiveAmount;
127 }
128 }
John Reck1b7184f2017-03-27 14:47:46 -0700129 LOG_ALWAYS_FATAL_IF(totalDuration <= 0, "Impossible totalDuration %" PRId64, totalDuration);
John Reck7075c792017-07-05 14:03:43 -0700130 mData->reportFrame(totalDuration);
John Reck34781b22017-07-05 16:39:36 -0700131 (*mGlobalData)->reportFrame(totalDuration);
John Reck7075c792017-07-05 14:03:43 -0700132
John Recke70c5752015-03-06 14:40:50 -0800133 // Keep the fast path as fast as possible.
John Reckba6adf62015-02-19 14:36:50 -0800134 if (CC_LIKELY(totalDuration < mFrameInterval)) {
135 return;
136 }
137
John Reck66010802016-03-30 14:19:44 -0700138 // Only things like Surface.lockHardwareCanvas() are exempt from tracking
Chris Craik1b54fb22015-06-02 17:40:58 -0700139 if (frame[FrameInfoIndex::Flags] & EXEMPT_FRAMES_FLAGS) {
John Reckba6adf62015-02-19 14:36:50 -0800140 return;
141 }
142
John Reck7075c792017-07-05 14:03:43 -0700143 mData->reportJank();
John Reck34781b22017-07-05 16:39:36 -0700144 (*mGlobalData)->reportJank();
John Reckba6adf62015-02-19 14:36:50 -0800145
146 for (int i = 0; i < NUM_BUCKETS; i++) {
John Reckbe3fba02015-07-06 13:49:58 -0700147 int64_t delta = frame.duration(COMPARISONS[i].start, COMPARISONS[i].end);
John Reckba6adf62015-02-19 14:36:50 -0800148 if (delta >= mThresholds[i] && delta < IGNORE_EXCEEDING) {
John Reck1bcacfd2017-11-03 10:12:19 -0700149 mData->reportJankType((JankType)i);
150 (*mGlobalData)->reportJankType((JankType)i);
John Reckba6adf62015-02-19 14:36:50 -0800151 }
152 }
153}
154
John Reck1bcacfd2017-11-03 10:12:19 -0700155void JankTracker::dumpData(int fd, const ProfileDataDescription* description,
156 const ProfileData* data) {
John Reckdf1742e2017-01-19 15:56:21 -0800157 if (description) {
158 switch (description->type) {
159 case JankTrackerType::Generic:
160 break;
161 case JankTrackerType::Package:
162 dprintf(fd, "\nPackage: %s", description->name.c_str());
163 break;
164 case JankTrackerType::Window:
165 dprintf(fd, "\nWindow: %s", description->name.c_str());
166 break;
167 }
John Reckba6adf62015-02-19 14:36:50 -0800168 }
John Reckc7cd9cf2016-03-28 10:38:19 -0700169 if (sFrameStart != FrameInfoIndex::IntendedVsync) {
170 dprintf(fd, "\nNote: Data has been filtered!");
171 }
John Reck7075c792017-07-05 14:03:43 -0700172 data->dump(fd);
John Reckedc524c2015-03-18 15:24:33 -0700173 dprintf(fd, "\n");
John Reckba6adf62015-02-19 14:36:50 -0800174}
175
John Reck34781b22017-07-05 16:39:36 -0700176void JankTracker::dumpFrames(int fd) {
John Reck47f5c3a2017-11-13 11:32:39 -0800177 dprintf(fd, "\n\n---PROFILEDATA---\n");
John Reck34781b22017-07-05 16:39:36 -0700178 for (size_t i = 0; i < static_cast<size_t>(FrameInfoIndex::NumIndexes); i++) {
John Reck47f5c3a2017-11-13 11:32:39 -0800179 dprintf(fd, "%s", FrameInfoNames[i].c_str());
180 dprintf(fd, ",");
John Reck34781b22017-07-05 16:39:36 -0700181 }
182 for (size_t i = 0; i < mFrames.size(); i++) {
183 FrameInfo& frame = mFrames[i];
184 if (frame[FrameInfoIndex::SyncStart] == 0) {
185 continue;
186 }
John Reck47f5c3a2017-11-13 11:32:39 -0800187 dprintf(fd, "\n");
John Reck34781b22017-07-05 16:39:36 -0700188 for (int i = 0; i < static_cast<int>(FrameInfoIndex::NumIndexes); i++) {
John Reck47f5c3a2017-11-13 11:32:39 -0800189 dprintf(fd, "%" PRId64 ",", frame[i]);
John Reck34781b22017-07-05 16:39:36 -0700190 }
191 }
John Reck47f5c3a2017-11-13 11:32:39 -0800192 dprintf(fd, "\n---PROFILEDATA---\n\n");
John Reck34781b22017-07-05 16:39:36 -0700193}
194
John Reckba6adf62015-02-19 14:36:50 -0800195void JankTracker::reset() {
John Reck34781b22017-07-05 16:39:36 -0700196 mFrames.clear();
John Reck7075c792017-07-05 14:03:43 -0700197 mData->reset();
John Reck34781b22017-07-05 16:39:36 -0700198 (*mGlobalData)->reset();
John Reck1bcacfd2017-11-03 10:12:19 -0700199 sFrameStart = Properties::filterOutTestOverhead ? FrameInfoIndex::HandleInputStart
200 : FrameInfoIndex::IntendedVsync;
John Reckba6adf62015-02-19 14:36:50 -0800201}
202
John Reckba6adf62015-02-19 14:36:50 -0800203} /* namespace uirenderer */
204} /* namespace android */