blob: 9d11828194441ae85cd2501f4fbaebe82d166a14 [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);
109
110}
111
John Reck34781b22017-07-05 16:39:36 -0700112void JankTracker::finishFrame(const FrameInfo& frame) {
John Reckba6adf62015-02-19 14:36:50 -0800113 // Fast-path for jank-free frames
John Reck126720a2016-04-15 15:16:38 -0700114 int64_t totalDuration = frame.duration(sFrameStart, FrameInfoIndex::FrameCompleted);
John Reck2d5b8d72016-07-28 15:36:11 -0700115 if (mDequeueTimeForgiveness
116 && frame[FrameInfoIndex::DequeueBufferDuration] > 500_us) {
117 nsecs_t expectedDequeueDuration =
118 mDequeueTimeForgiveness + frame[FrameInfoIndex::Vsync]
119 - frame[FrameInfoIndex::IssueDrawCommandsStart];
120 if (expectedDequeueDuration > 0) {
121 // Forgive only up to the expected amount, but not more than
122 // the actual time spent blocked.
123 nsecs_t forgiveAmount = std::min(expectedDequeueDuration,
124 frame[FrameInfoIndex::DequeueBufferDuration]);
John Reck1b7184f2017-03-27 14:47:46 -0700125 LOG_ALWAYS_FATAL_IF(forgiveAmount >= totalDuration,
126 "Impossible dequeue duration! dequeue duration reported %" PRId64
127 ", total duration %" PRId64, 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 Reck7075c792017-07-05 14:03:43 -0700151 mData->reportJankType((JankType) i);
John Reck34781b22017-07-05 16:39:36 -0700152 (*mGlobalData)->reportJankType((JankType) i);
John Reckba6adf62015-02-19 14:36:50 -0800153 }
154 }
155}
156
John Reckdf1742e2017-01-19 15:56:21 -0800157void JankTracker::dumpData(int fd, const ProfileDataDescription* description, const ProfileData* data) {
158 if (description) {
159 switch (description->type) {
160 case JankTrackerType::Generic:
161 break;
162 case JankTrackerType::Package:
163 dprintf(fd, "\nPackage: %s", description->name.c_str());
164 break;
165 case JankTrackerType::Window:
166 dprintf(fd, "\nWindow: %s", description->name.c_str());
167 break;
168 }
John Reckba6adf62015-02-19 14:36:50 -0800169 }
John Reckc7cd9cf2016-03-28 10:38:19 -0700170 if (sFrameStart != FrameInfoIndex::IntendedVsync) {
171 dprintf(fd, "\nNote: Data has been filtered!");
172 }
John Reck7075c792017-07-05 14:03:43 -0700173 data->dump(fd);
John Reckedc524c2015-03-18 15:24:33 -0700174 dprintf(fd, "\n");
John Reckba6adf62015-02-19 14:36:50 -0800175}
176
John Reck34781b22017-07-05 16:39:36 -0700177void JankTracker::dumpFrames(int fd) {
178 FILE* file = fdopen(fd, "a");
179 fprintf(file, "\n\n---PROFILEDATA---\n");
180 for (size_t i = 0; i < static_cast<size_t>(FrameInfoIndex::NumIndexes); i++) {
181 fprintf(file, "%s", FrameInfoNames[i].c_str());
182 fprintf(file, ",");
183 }
184 for (size_t i = 0; i < mFrames.size(); i++) {
185 FrameInfo& frame = mFrames[i];
186 if (frame[FrameInfoIndex::SyncStart] == 0) {
187 continue;
188 }
189 fprintf(file, "\n");
190 for (int i = 0; i < static_cast<int>(FrameInfoIndex::NumIndexes); i++) {
191 fprintf(file, "%" PRId64 ",", frame[i]);
192 }
193 }
194 fprintf(file, "\n---PROFILEDATA---\n\n");
195 fflush(file);
196}
197
John Reckba6adf62015-02-19 14:36:50 -0800198void JankTracker::reset() {
John Reck34781b22017-07-05 16:39:36 -0700199 mFrames.clear();
John Reck7075c792017-07-05 14:03:43 -0700200 mData->reset();
John Reck34781b22017-07-05 16:39:36 -0700201 (*mGlobalData)->reset();
John Reckc7cd9cf2016-03-28 10:38:19 -0700202 sFrameStart = Properties::filterOutTestOverhead
203 ? FrameInfoIndex::HandleInputStart
204 : FrameInfoIndex::IntendedVsync;
John Reckba6adf62015-02-19 14:36:50 -0800205}
206
John Reckba6adf62015-02-19 14:36:50 -0800207} /* namespace uirenderer */
208} /* namespace android */