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 | c87be99 | 2015-02-20 10:57:22 -0800 | [diff] [blame] | 41 | FrameInfoIndex start; |
| 42 | FrameInfoIndex end; |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 43 | }; |
| 44 | |
| 45 | static const Comparison COMPARISONS[] = { |
Chris Craik | 1b54fb2 | 2015-06-02 17:40:58 -0700 | [diff] [blame] | 46 | {FrameInfoIndex::IntendedVsync, FrameInfoIndex::Vsync}, |
| 47 | {FrameInfoIndex::OldestInputEvent, FrameInfoIndex::Vsync}, |
| 48 | {FrameInfoIndex::Vsync, FrameInfoIndex::SyncStart}, |
| 49 | {FrameInfoIndex::SyncStart, FrameInfoIndex::IssueDrawCommandsStart}, |
| 50 | {FrameInfoIndex::IssueDrawCommandsStart, FrameInfoIndex::FrameCompleted}, |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 51 | }; |
| 52 | |
| 53 | // If the event exceeds 10 seconds throw it away, this isn't a jank event |
| 54 | // it's an ANR and will be handled as such |
| 55 | static const int64_t IGNORE_EXCEEDING = seconds_to_nanoseconds(10); |
| 56 | |
| 57 | /* |
John Reck | 6601080 | 2016-03-30 14:19:44 -0700 | [diff] [blame] | 58 | * We don't track direct-drawing via Surface:lockHardwareCanvas() |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 59 | * for now |
| 60 | * |
| 61 | * TODO: kSurfaceCanvas can negatively impact other drawing by using up |
| 62 | * time on the RenderThread, figure out how to attribute that as a jank-causer |
| 63 | */ |
John Reck | 6601080 | 2016-03-30 14:19:44 -0700 | [diff] [blame] | 64 | static const int64_t EXEMPT_FRAMES_FLAGS = FrameInfoFlags::SurfaceCanvas; |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 65 | |
John Reck | c7cd9cf | 2016-03-28 10:38:19 -0700 | [diff] [blame] | 66 | // For testing purposes to try and eliminate test infra overhead we will |
| 67 | // consider any unknown delay of frame start as part of the test infrastructure |
| 68 | // and filter it out of the frame profile data |
| 69 | static FrameInfoIndex sFrameStart = FrameInfoIndex::IntendedVsync; |
| 70 | |
John Reck | 34781b2 | 2017-07-05 16:39:36 -0700 | [diff] [blame] | 71 | JankTracker::JankTracker(ProfileDataContainer* globalData, const DisplayInfo& displayInfo) { |
| 72 | mGlobalData = globalData; |
John Reck | 2d5b8d7 | 2016-07-28 15:36:11 -0700 | [diff] [blame] | 73 | 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 Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 89 | setFrameInterval(frameIntervalNanos); |
| 90 | } |
| 91 | |
| 92 | void JankTracker::setFrameInterval(nsecs_t frameInterval) { |
| 93 | mFrameInterval = frameInterval; |
| 94 | mThresholds[kMissedVsync] = 1; |
| 95 | /* |
| 96 | * Due to interpolation and sample rate differences between the touch |
| 97 | * panel and the display (example, 85hz touch panel driving a 60hz display) |
| 98 | * we call high latency 1.5 * frameinterval |
| 99 | * |
| 100 | * NOTE: Be careful when tuning this! A theoretical 1,000hz touch panel |
| 101 | * on a 60hz display will show kOldestInputEvent - kIntendedVsync of being 15ms |
| 102 | * Thus this must always be larger than frameInterval, or it will fail |
| 103 | */ |
| 104 | mThresholds[kHighInputLatency] = static_cast<int64_t>(1.5 * frameInterval); |
| 105 | |
| 106 | // Note that these do not add up to 1. This is intentional. It's to deal |
| 107 | // with variance in values, and should be sort of an upper-bound on what |
| 108 | // is reasonable to expect. |
| 109 | mThresholds[kSlowUI] = static_cast<int64_t>(.5 * frameInterval); |
| 110 | mThresholds[kSlowSync] = static_cast<int64_t>(.2 * frameInterval); |
| 111 | mThresholds[kSlowRT] = static_cast<int64_t>(.75 * frameInterval); |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 112 | } |
| 113 | |
John Reck | 34781b2 | 2017-07-05 16:39:36 -0700 | [diff] [blame] | 114 | void JankTracker::finishFrame(const FrameInfo& frame) { |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 115 | // Fast-path for jank-free frames |
John Reck | 126720a | 2016-04-15 15:16:38 -0700 | [diff] [blame] | 116 | int64_t totalDuration = frame.duration(sFrameStart, FrameInfoIndex::FrameCompleted); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 117 | if (mDequeueTimeForgiveness && frame[FrameInfoIndex::DequeueBufferDuration] > 500_us) { |
| 118 | nsecs_t expectedDequeueDuration = mDequeueTimeForgiveness + frame[FrameInfoIndex::Vsync] - |
| 119 | frame[FrameInfoIndex::IssueDrawCommandsStart]; |
John Reck | 2d5b8d7 | 2016-07-28 15:36:11 -0700 | [diff] [blame] | 120 | if (expectedDequeueDuration > 0) { |
| 121 | // Forgive only up to the expected amount, but not more than |
| 122 | // the actual time spent blocked. |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 123 | nsecs_t forgiveAmount = |
| 124 | std::min(expectedDequeueDuration, frame[FrameInfoIndex::DequeueBufferDuration]); |
John Reck | 1b7184f | 2017-03-27 14:47:46 -0700 | [diff] [blame] | 125 | LOG_ALWAYS_FATAL_IF(forgiveAmount >= totalDuration, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 126 | "Impossible dequeue duration! dequeue duration reported %" PRId64 |
| 127 | ", total duration %" PRId64, |
| 128 | forgiveAmount, totalDuration); |
John Reck | 2d5b8d7 | 2016-07-28 15:36:11 -0700 | [diff] [blame] | 129 | totalDuration -= forgiveAmount; |
| 130 | } |
| 131 | } |
John Reck | 1b7184f | 2017-03-27 14:47:46 -0700 | [diff] [blame] | 132 | LOG_ALWAYS_FATAL_IF(totalDuration <= 0, "Impossible totalDuration %" PRId64, totalDuration); |
John Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 133 | mData->reportFrame(totalDuration); |
John Reck | 34781b2 | 2017-07-05 16:39:36 -0700 | [diff] [blame] | 134 | (*mGlobalData)->reportFrame(totalDuration); |
John Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 135 | |
John Reck | e70c575 | 2015-03-06 14:40:50 -0800 | [diff] [blame] | 136 | // Keep the fast path as fast as possible. |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 137 | if (CC_LIKELY(totalDuration < mFrameInterval)) { |
| 138 | return; |
| 139 | } |
| 140 | |
John Reck | 6601080 | 2016-03-30 14:19:44 -0700 | [diff] [blame] | 141 | // Only things like Surface.lockHardwareCanvas() are exempt from tracking |
Chris Craik | 1b54fb2 | 2015-06-02 17:40:58 -0700 | [diff] [blame] | 142 | if (frame[FrameInfoIndex::Flags] & EXEMPT_FRAMES_FLAGS) { |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 143 | return; |
| 144 | } |
| 145 | |
John Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 146 | mData->reportJank(); |
John Reck | 34781b2 | 2017-07-05 16:39:36 -0700 | [diff] [blame] | 147 | (*mGlobalData)->reportJank(); |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 148 | |
| 149 | for (int i = 0; i < NUM_BUCKETS; i++) { |
John Reck | be3fba0 | 2015-07-06 13:49:58 -0700 | [diff] [blame] | 150 | int64_t delta = frame.duration(COMPARISONS[i].start, COMPARISONS[i].end); |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 151 | if (delta >= mThresholds[i] && delta < IGNORE_EXCEEDING) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 152 | mData->reportJankType((JankType)i); |
| 153 | (*mGlobalData)->reportJankType((JankType)i); |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 154 | } |
| 155 | } |
John Reck | 0e89ca2 | 2017-12-15 16:00:48 -0800 | [diff] [blame] | 156 | |
| 157 | // Log daveys since they are weird and we don't know what they are (b/70339576) |
| 158 | if (totalDuration >= 700_ms) { |
| 159 | static int sDaveyCount = 0; |
| 160 | std::stringstream ss; |
| 161 | ss << "Davey! duration=" << ns2ms(totalDuration) << "ms; "; |
| 162 | for (size_t i = 0; i < static_cast<size_t>(FrameInfoIndex::NumIndexes); i++) { |
| 163 | ss << FrameInfoNames[i] << "=" << frame[i] << ", "; |
| 164 | } |
| 165 | ALOGI("%s", ss.str().c_str()); |
| 166 | // Just so we have something that counts up, the value is largely irrelevant |
| 167 | ATRACE_INT(ss.str().c_str(), ++sDaveyCount); |
David Chen | 77ef671 | 2018-02-23 18:23:42 -0800 | [diff] [blame] | 168 | android::util::stats_write(android::util::DAVEY_OCCURRED, getuid(), ns2ms(totalDuration)); |
John Reck | 0e89ca2 | 2017-12-15 16:00:48 -0800 | [diff] [blame] | 169 | } |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 170 | } |
| 171 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 172 | void JankTracker::dumpData(int fd, const ProfileDataDescription* description, |
| 173 | const ProfileData* data) { |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 174 | if (description) { |
| 175 | switch (description->type) { |
| 176 | case JankTrackerType::Generic: |
| 177 | break; |
| 178 | case JankTrackerType::Package: |
| 179 | dprintf(fd, "\nPackage: %s", description->name.c_str()); |
| 180 | break; |
| 181 | case JankTrackerType::Window: |
| 182 | dprintf(fd, "\nWindow: %s", description->name.c_str()); |
| 183 | break; |
| 184 | } |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 185 | } |
John Reck | c7cd9cf | 2016-03-28 10:38:19 -0700 | [diff] [blame] | 186 | if (sFrameStart != FrameInfoIndex::IntendedVsync) { |
| 187 | dprintf(fd, "\nNote: Data has been filtered!"); |
| 188 | } |
John Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 189 | data->dump(fd); |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 190 | dprintf(fd, "\n"); |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 191 | } |
| 192 | |
John Reck | 34781b2 | 2017-07-05 16:39:36 -0700 | [diff] [blame] | 193 | void JankTracker::dumpFrames(int fd) { |
John Reck | 47f5c3a | 2017-11-13 11:32:39 -0800 | [diff] [blame] | 194 | dprintf(fd, "\n\n---PROFILEDATA---\n"); |
John Reck | 34781b2 | 2017-07-05 16:39:36 -0700 | [diff] [blame] | 195 | 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] | 196 | dprintf(fd, "%s", FrameInfoNames[i].c_str()); |
| 197 | dprintf(fd, ","); |
John Reck | 34781b2 | 2017-07-05 16:39:36 -0700 | [diff] [blame] | 198 | } |
| 199 | for (size_t i = 0; i < mFrames.size(); i++) { |
| 200 | FrameInfo& frame = mFrames[i]; |
| 201 | if (frame[FrameInfoIndex::SyncStart] == 0) { |
| 202 | continue; |
| 203 | } |
John Reck | 47f5c3a | 2017-11-13 11:32:39 -0800 | [diff] [blame] | 204 | dprintf(fd, "\n"); |
John Reck | 34781b2 | 2017-07-05 16:39:36 -0700 | [diff] [blame] | 205 | for (int i = 0; i < static_cast<int>(FrameInfoIndex::NumIndexes); i++) { |
John Reck | 47f5c3a | 2017-11-13 11:32:39 -0800 | [diff] [blame] | 206 | dprintf(fd, "%" PRId64 ",", frame[i]); |
John Reck | 34781b2 | 2017-07-05 16:39:36 -0700 | [diff] [blame] | 207 | } |
| 208 | } |
John Reck | 47f5c3a | 2017-11-13 11:32:39 -0800 | [diff] [blame] | 209 | dprintf(fd, "\n---PROFILEDATA---\n\n"); |
John Reck | 34781b2 | 2017-07-05 16:39:36 -0700 | [diff] [blame] | 210 | } |
| 211 | |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 212 | void JankTracker::reset() { |
John Reck | 34781b2 | 2017-07-05 16:39:36 -0700 | [diff] [blame] | 213 | mFrames.clear(); |
John Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 214 | mData->reset(); |
John Reck | 34781b2 | 2017-07-05 16:39:36 -0700 | [diff] [blame] | 215 | (*mGlobalData)->reset(); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 216 | sFrameStart = Properties::filterOutTestOverhead ? FrameInfoIndex::HandleInputStart |
| 217 | : FrameInfoIndex::IntendedVsync; |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 218 | } |
| 219 | |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 220 | } /* namespace uirenderer */ |
| 221 | } /* namespace android */ |