John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 1 | /* |
| 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 Salyzyn | 96bf598 | 2016-09-28 16:15:30 -0700 | [diff] [blame] | 16 | |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 17 | #include "JankTracker.h" |
| 18 | |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 19 | #include <errno.h> |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 20 | #include <inttypes.h> |
Tej Singh | bb8554a | 2018-01-26 11:59:14 -0800 | [diff] [blame] | 21 | #include <statslog.h> |
Mark Salyzyn | 96bf598 | 2016-09-28 16:15:30 -0700 | [diff] [blame] | 22 | #include <sys/mman.h> |
Mark Salyzyn | 52eb4e0 | 2016-09-28 16:15:30 -0700 | [diff] [blame] | 23 | |
| 24 | #include <algorithm> |
John Reck | 5ed587f | 2016-03-24 15:57:01 -0700 | [diff] [blame] | 25 | #include <cmath> |
Mark Salyzyn | 52eb4e0 | 2016-09-28 16:15:30 -0700 | [diff] [blame] | 26 | #include <cstdio> |
| 27 | #include <limits> |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 28 | |
Mark Salyzyn | 52eb4e0 | 2016-09-28 16:15:30 -0700 | [diff] [blame] | 29 | #include <cutils/ashmem.h> |
| 30 | #include <log/log.h> |
John Reck | 0e89ca2 | 2017-12-15 16:00:48 -0800 | [diff] [blame] | 31 | #include <sstream> |
Mark Salyzyn | 52eb4e0 | 2016-09-28 16:15:30 -0700 | [diff] [blame] | 32 | |
| 33 | #include "Properties.h" |
| 34 | #include "utils/TimeUtils.h" |
John Reck | 0e89ca2 | 2017-12-15 16:00:48 -0800 | [diff] [blame] | 35 | #include "utils/Trace.h" |
Mark Salyzyn | 52eb4e0 | 2016-09-28 16:15:30 -0700 | [diff] [blame] | 36 | |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 37 | namespace android { |
| 38 | namespace uirenderer { |
| 39 | |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 40 | struct Comparison { |
John Reck | 0e48647 | 2018-03-19 14:06:16 -0700 | [diff] [blame] | 41 | JankType type; |
| 42 | std::function<int64_t(nsecs_t)> computeThreadshold; |
John Reck | c87be99 | 2015-02-20 10:57:22 -0800 | [diff] [blame] | 43 | FrameInfoIndex start; |
| 44 | FrameInfoIndex end; |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 45 | }; |
| 46 | |
John Reck | 0e48647 | 2018-03-19 14:06:16 -0700 | [diff] [blame] | 47 | static const std::array<Comparison, 4> COMPARISONS{ |
| 48 | Comparison{JankType::kMissedVsync, [](nsecs_t) { return 1; }, FrameInfoIndex::IntendedVsync, |
| 49 | FrameInfoIndex::Vsync}, |
| 50 | |
| 51 | Comparison{JankType::kSlowUI, |
| 52 | [](nsecs_t frameInterval) { return static_cast<int64_t>(.5 * frameInterval); }, |
| 53 | FrameInfoIndex::Vsync, FrameInfoIndex::SyncStart}, |
| 54 | |
| 55 | Comparison{JankType::kSlowSync, |
| 56 | [](nsecs_t frameInterval) { return static_cast<int64_t>(.2 * frameInterval); }, |
| 57 | FrameInfoIndex::SyncStart, FrameInfoIndex::IssueDrawCommandsStart}, |
| 58 | |
| 59 | Comparison{JankType::kSlowRT, |
| 60 | [](nsecs_t frameInterval) { return static_cast<int64_t>(.75 * frameInterval); }, |
| 61 | FrameInfoIndex::IssueDrawCommandsStart, FrameInfoIndex::FrameCompleted}, |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 62 | }; |
| 63 | |
| 64 | // If the event exceeds 10 seconds throw it away, this isn't a jank event |
| 65 | // it's an ANR and will be handled as such |
| 66 | static const int64_t IGNORE_EXCEEDING = seconds_to_nanoseconds(10); |
| 67 | |
| 68 | /* |
John Reck | 6601080 | 2016-03-30 14:19:44 -0700 | [diff] [blame] | 69 | * We don't track direct-drawing via Surface:lockHardwareCanvas() |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 70 | * for now |
| 71 | * |
| 72 | * TODO: kSurfaceCanvas can negatively impact other drawing by using up |
| 73 | * time on the RenderThread, figure out how to attribute that as a jank-causer |
| 74 | */ |
John Reck | 6601080 | 2016-03-30 14:19:44 -0700 | [diff] [blame] | 75 | static const int64_t EXEMPT_FRAMES_FLAGS = FrameInfoFlags::SurfaceCanvas; |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 76 | |
John Reck | c7cd9cf | 2016-03-28 10:38:19 -0700 | [diff] [blame] | 77 | // For testing purposes to try and eliminate test infra overhead we will |
| 78 | // consider any unknown delay of frame start as part of the test infrastructure |
| 79 | // and filter it out of the frame profile data |
| 80 | static FrameInfoIndex sFrameStart = FrameInfoIndex::IntendedVsync; |
| 81 | |
John Reck | 34781b2 | 2017-07-05 16:39:36 -0700 | [diff] [blame] | 82 | JankTracker::JankTracker(ProfileDataContainer* globalData, const DisplayInfo& displayInfo) { |
| 83 | mGlobalData = globalData; |
John Reck | 2d5b8d7 | 2016-07-28 15:36:11 -0700 | [diff] [blame] | 84 | nsecs_t frameIntervalNanos = static_cast<nsecs_t>(1_s / displayInfo.fps); |
| 85 | #if USE_HWC2 |
| 86 | nsecs_t sfOffset = frameIntervalNanos - (displayInfo.presentationDeadline - 1_ms); |
| 87 | nsecs_t offsetDelta = sfOffset - displayInfo.appVsyncOffset; |
| 88 | // There are two different offset cases. If the offsetDelta is positive |
| 89 | // and small, then the intention is to give apps extra time by leveraging |
| 90 | // pipelining between the UI & RT threads. If the offsetDelta is large or |
| 91 | // negative, the intention is to subtract time from the total duration |
| 92 | // in which case we can't afford to wait for dequeueBuffer blockage. |
| 93 | if (offsetDelta <= 4_ms && offsetDelta >= 0) { |
| 94 | // SF will begin composition at VSYNC-app + offsetDelta. If we are triple |
| 95 | // buffered, this is the expected time at which dequeueBuffer will |
| 96 | // return due to the staggering of VSYNC-app & VSYNC-sf. |
| 97 | mDequeueTimeForgiveness = offsetDelta + 4_ms; |
| 98 | } |
| 99 | #endif |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 100 | setFrameInterval(frameIntervalNanos); |
| 101 | } |
| 102 | |
| 103 | void JankTracker::setFrameInterval(nsecs_t frameInterval) { |
| 104 | mFrameInterval = frameInterval; |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 105 | |
John Reck | 0e48647 | 2018-03-19 14:06:16 -0700 | [diff] [blame] | 106 | for (auto& comparison : COMPARISONS) { |
| 107 | mThresholds[comparison.type] = comparison.computeThreadshold(frameInterval); |
| 108 | } |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 109 | } |
| 110 | |
John Reck | 34781b2 | 2017-07-05 16:39:36 -0700 | [diff] [blame] | 111 | void JankTracker::finishFrame(const FrameInfo& frame) { |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 112 | // Fast-path for jank-free frames |
John Reck | 126720a | 2016-04-15 15:16:38 -0700 | [diff] [blame] | 113 | int64_t totalDuration = frame.duration(sFrameStart, FrameInfoIndex::FrameCompleted); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 114 | if (mDequeueTimeForgiveness && frame[FrameInfoIndex::DequeueBufferDuration] > 500_us) { |
| 115 | nsecs_t expectedDequeueDuration = mDequeueTimeForgiveness + frame[FrameInfoIndex::Vsync] - |
| 116 | frame[FrameInfoIndex::IssueDrawCommandsStart]; |
John Reck | 2d5b8d7 | 2016-07-28 15:36:11 -0700 | [diff] [blame] | 117 | if (expectedDequeueDuration > 0) { |
| 118 | // Forgive only up to the expected amount, but not more than |
| 119 | // the actual time spent blocked. |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 120 | nsecs_t forgiveAmount = |
| 121 | std::min(expectedDequeueDuration, frame[FrameInfoIndex::DequeueBufferDuration]); |
John Reck | 1b7184f | 2017-03-27 14:47:46 -0700 | [diff] [blame] | 122 | LOG_ALWAYS_FATAL_IF(forgiveAmount >= totalDuration, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 123 | "Impossible dequeue duration! dequeue duration reported %" PRId64 |
| 124 | ", total duration %" PRId64, |
| 125 | forgiveAmount, totalDuration); |
John Reck | 2d5b8d7 | 2016-07-28 15:36:11 -0700 | [diff] [blame] | 126 | totalDuration -= forgiveAmount; |
| 127 | } |
| 128 | } |
John Reck | 0e48647 | 2018-03-19 14:06:16 -0700 | [diff] [blame] | 129 | |
John Reck | 1b7184f | 2017-03-27 14:47:46 -0700 | [diff] [blame] | 130 | LOG_ALWAYS_FATAL_IF(totalDuration <= 0, "Impossible totalDuration %" PRId64, totalDuration); |
John Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 131 | mData->reportFrame(totalDuration); |
John Reck | 34781b2 | 2017-07-05 16:39:36 -0700 | [diff] [blame] | 132 | (*mGlobalData)->reportFrame(totalDuration); |
John Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 133 | |
John Reck | 6601080 | 2016-03-30 14:19:44 -0700 | [diff] [blame] | 134 | // Only things like Surface.lockHardwareCanvas() are exempt from tracking |
John Reck | 0e48647 | 2018-03-19 14:06:16 -0700 | [diff] [blame] | 135 | if (CC_UNLIKELY(frame[FrameInfoIndex::Flags] & EXEMPT_FRAMES_FLAGS)) { |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 136 | return; |
| 137 | } |
| 138 | |
John Reck | 0e48647 | 2018-03-19 14:06:16 -0700 | [diff] [blame] | 139 | if (totalDuration > mFrameInterval) { |
| 140 | mData->reportJank(); |
| 141 | (*mGlobalData)->reportJank(); |
| 142 | } |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 143 | |
John Reck | 0e48647 | 2018-03-19 14:06:16 -0700 | [diff] [blame] | 144 | bool isTripleBuffered = mSwapDeadline > frame[FrameInfoIndex::IntendedVsync]; |
| 145 | |
| 146 | mSwapDeadline = std::max(mSwapDeadline + mFrameInterval, |
| 147 | frame[FrameInfoIndex::IntendedVsync] + mFrameInterval); |
| 148 | |
| 149 | // If we hit the deadline, cool! |
| 150 | if (frame[FrameInfoIndex::FrameCompleted] < mSwapDeadline) { |
| 151 | if (isTripleBuffered) { |
| 152 | mData->reportJankType(JankType::kHighInputLatency); |
| 153 | (*mGlobalData)->reportJankType(JankType::kHighInputLatency); |
| 154 | } |
| 155 | return; |
| 156 | } |
| 157 | |
| 158 | mData->reportJankType(JankType::kMissedDeadline); |
| 159 | (*mGlobalData)->reportJankType(JankType::kMissedDeadline); |
| 160 | |
| 161 | // Janked, reset the swap deadline |
| 162 | nsecs_t jitterNanos = frame[FrameInfoIndex::FrameCompleted] - frame[FrameInfoIndex::Vsync]; |
| 163 | nsecs_t lastFrameOffset = jitterNanos % mFrameInterval; |
| 164 | mSwapDeadline = frame[FrameInfoIndex::FrameCompleted] - lastFrameOffset + mFrameInterval; |
| 165 | |
| 166 | for (auto& comparison : COMPARISONS) { |
| 167 | int64_t delta = frame.duration(comparison.start, comparison.end); |
| 168 | if (delta >= mThresholds[comparison.type] && delta < IGNORE_EXCEEDING) { |
| 169 | mData->reportJankType(comparison.type); |
| 170 | (*mGlobalData)->reportJankType(comparison.type); |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 171 | } |
| 172 | } |
John Reck | 0e89ca2 | 2017-12-15 16:00:48 -0800 | [diff] [blame] | 173 | |
| 174 | // Log daveys since they are weird and we don't know what they are (b/70339576) |
| 175 | if (totalDuration >= 700_ms) { |
| 176 | static int sDaveyCount = 0; |
| 177 | std::stringstream ss; |
| 178 | ss << "Davey! duration=" << ns2ms(totalDuration) << "ms; "; |
| 179 | for (size_t i = 0; i < static_cast<size_t>(FrameInfoIndex::NumIndexes); i++) { |
| 180 | ss << FrameInfoNames[i] << "=" << frame[i] << ", "; |
| 181 | } |
| 182 | ALOGI("%s", ss.str().c_str()); |
| 183 | // Just so we have something that counts up, the value is largely irrelevant |
| 184 | ATRACE_INT(ss.str().c_str(), ++sDaveyCount); |
David Chen | 77ef671 | 2018-02-23 18:23:42 -0800 | [diff] [blame] | 185 | android::util::stats_write(android::util::DAVEY_OCCURRED, getuid(), ns2ms(totalDuration)); |
John Reck | 0e89ca2 | 2017-12-15 16:00:48 -0800 | [diff] [blame] | 186 | } |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 187 | } |
| 188 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 189 | void JankTracker::dumpData(int fd, const ProfileDataDescription* description, |
| 190 | const ProfileData* data) { |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 191 | if (description) { |
| 192 | switch (description->type) { |
| 193 | case JankTrackerType::Generic: |
| 194 | break; |
| 195 | case JankTrackerType::Package: |
| 196 | dprintf(fd, "\nPackage: %s", description->name.c_str()); |
| 197 | break; |
| 198 | case JankTrackerType::Window: |
| 199 | dprintf(fd, "\nWindow: %s", description->name.c_str()); |
| 200 | break; |
| 201 | } |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 202 | } |
John Reck | c7cd9cf | 2016-03-28 10:38:19 -0700 | [diff] [blame] | 203 | if (sFrameStart != FrameInfoIndex::IntendedVsync) { |
| 204 | dprintf(fd, "\nNote: Data has been filtered!"); |
| 205 | } |
John Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 206 | data->dump(fd); |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 207 | dprintf(fd, "\n"); |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 208 | } |
| 209 | |
John Reck | 34781b2 | 2017-07-05 16:39:36 -0700 | [diff] [blame] | 210 | void JankTracker::dumpFrames(int fd) { |
John Reck | 47f5c3a | 2017-11-13 11:32:39 -0800 | [diff] [blame] | 211 | dprintf(fd, "\n\n---PROFILEDATA---\n"); |
John Reck | 34781b2 | 2017-07-05 16:39:36 -0700 | [diff] [blame] | 212 | for (size_t i = 0; i < static_cast<size_t>(FrameInfoIndex::NumIndexes); i++) { |
John Reck | 47f5c3a | 2017-11-13 11:32:39 -0800 | [diff] [blame] | 213 | dprintf(fd, "%s", FrameInfoNames[i].c_str()); |
| 214 | dprintf(fd, ","); |
John Reck | 34781b2 | 2017-07-05 16:39:36 -0700 | [diff] [blame] | 215 | } |
| 216 | for (size_t i = 0; i < mFrames.size(); i++) { |
| 217 | FrameInfo& frame = mFrames[i]; |
| 218 | if (frame[FrameInfoIndex::SyncStart] == 0) { |
| 219 | continue; |
| 220 | } |
John Reck | 47f5c3a | 2017-11-13 11:32:39 -0800 | [diff] [blame] | 221 | dprintf(fd, "\n"); |
John Reck | 34781b2 | 2017-07-05 16:39:36 -0700 | [diff] [blame] | 222 | for (int i = 0; i < static_cast<int>(FrameInfoIndex::NumIndexes); i++) { |
John Reck | 47f5c3a | 2017-11-13 11:32:39 -0800 | [diff] [blame] | 223 | dprintf(fd, "%" PRId64 ",", frame[i]); |
John Reck | 34781b2 | 2017-07-05 16:39:36 -0700 | [diff] [blame] | 224 | } |
| 225 | } |
John Reck | 47f5c3a | 2017-11-13 11:32:39 -0800 | [diff] [blame] | 226 | dprintf(fd, "\n---PROFILEDATA---\n\n"); |
John Reck | 34781b2 | 2017-07-05 16:39:36 -0700 | [diff] [blame] | 227 | } |
| 228 | |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 229 | void JankTracker::reset() { |
John Reck | 34781b2 | 2017-07-05 16:39:36 -0700 | [diff] [blame] | 230 | mFrames.clear(); |
John Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 231 | mData->reset(); |
John Reck | 34781b2 | 2017-07-05 16:39:36 -0700 | [diff] [blame] | 232 | (*mGlobalData)->reset(); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 233 | sFrameStart = Properties::filterOutTestOverhead ? FrameInfoIndex::HandleInputStart |
| 234 | : FrameInfoIndex::IntendedVsync; |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 235 | } |
| 236 | |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 237 | } /* namespace uirenderer */ |
| 238 | } /* namespace android */ |