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> |
Mark Salyzyn | 96bf598 | 2016-09-28 16:15:30 -0700 | [diff] [blame] | 21 | #include <sys/mman.h> |
Mark Salyzyn | 52eb4e0 | 2016-09-28 16:15:30 -0700 | [diff] [blame] | 22 | |
| 23 | #include <algorithm> |
John Reck | 5ed587f | 2016-03-24 15:57:01 -0700 | [diff] [blame] | 24 | #include <cmath> |
Mark Salyzyn | 52eb4e0 | 2016-09-28 16:15:30 -0700 | [diff] [blame] | 25 | #include <cstdio> |
| 26 | #include <limits> |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 27 | |
Mark Salyzyn | 52eb4e0 | 2016-09-28 16:15:30 -0700 | [diff] [blame] | 28 | #include <cutils/ashmem.h> |
| 29 | #include <log/log.h> |
| 30 | |
| 31 | #include "Properties.h" |
| 32 | #include "utils/TimeUtils.h" |
| 33 | |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 34 | namespace android { |
| 35 | namespace uirenderer { |
| 36 | |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 37 | struct Comparison { |
John Reck | c87be99 | 2015-02-20 10:57:22 -0800 | [diff] [blame] | 38 | FrameInfoIndex start; |
| 39 | FrameInfoIndex end; |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 40 | }; |
| 41 | |
| 42 | static const Comparison COMPARISONS[] = { |
Chris Craik | 1b54fb2 | 2015-06-02 17:40:58 -0700 | [diff] [blame] | 43 | {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 Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 48 | }; |
| 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 |
| 52 | static const int64_t IGNORE_EXCEEDING = seconds_to_nanoseconds(10); |
| 53 | |
| 54 | /* |
John Reck | 6601080 | 2016-03-30 14:19:44 -0700 | [diff] [blame] | 55 | * We don't track direct-drawing via Surface:lockHardwareCanvas() |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 56 | * 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 Reck | 6601080 | 2016-03-30 14:19:44 -0700 | [diff] [blame] | 61 | static const int64_t EXEMPT_FRAMES_FLAGS = FrameInfoFlags::SurfaceCanvas; |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 62 | |
John Reck | c7cd9cf | 2016-03-28 10:38:19 -0700 | [diff] [blame] | 63 | // 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 |
| 66 | static FrameInfoIndex sFrameStart = FrameInfoIndex::IntendedVsync; |
| 67 | |
John Reck | 2d5b8d7 | 2016-07-28 15:36:11 -0700 | [diff] [blame] | 68 | JankTracker::JankTracker(const DisplayInfo& displayInfo) { |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 69 | // 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 Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 72 | reset(); |
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 | |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 92 | JankTracker::~JankTracker() { |
| 93 | freeData(); |
| 94 | } |
| 95 | |
| 96 | void 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 Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 106 | void 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 Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 118 | void JankTracker::switchStorageToAshmem(int ashmemfd) { |
| 119 | int regionSize = ashmem_get_size_region(ashmemfd); |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 120 | 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 Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 125 | if (regionSize < static_cast<int>(sizeof(ProfileData))) { |
| 126 | ALOGW("Ashmem region is too small! Received %d, required %u", |
John Reck | 98fa0a3 | 2015-03-31 12:03:51 -0700 | [diff] [blame] | 127 | regionSize, static_cast<unsigned int>(sizeof(ProfileData))); |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 128 | 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 Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 140 | newData->mergeWith(*mData); |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 141 | freeData(); |
| 142 | mData = newData; |
| 143 | mIsMapped = true; |
| 144 | } |
| 145 | |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 146 | void 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 | |
| 169 | void JankTracker::addFrame(const FrameInfo& frame) { |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 170 | // Fast-path for jank-free frames |
John Reck | 126720a | 2016-04-15 15:16:38 -0700 | [diff] [blame] | 171 | int64_t totalDuration = frame.duration(sFrameStart, FrameInfoIndex::FrameCompleted); |
John Reck | 2d5b8d7 | 2016-07-28 15:36:11 -0700 | [diff] [blame] | 172 | 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 Reck | 1b7184f | 2017-03-27 14:47:46 -0700 | [diff] [blame] | 182 | LOG_ALWAYS_FATAL_IF(forgiveAmount >= totalDuration, |
| 183 | "Impossible dequeue duration! dequeue duration reported %" PRId64 |
| 184 | ", total duration %" PRId64, forgiveAmount, totalDuration); |
John Reck | 2d5b8d7 | 2016-07-28 15:36:11 -0700 | [diff] [blame] | 185 | totalDuration -= forgiveAmount; |
| 186 | } |
| 187 | } |
John Reck | 1b7184f | 2017-03-27 14:47:46 -0700 | [diff] [blame] | 188 | LOG_ALWAYS_FATAL_IF(totalDuration <= 0, "Impossible totalDuration %" PRId64, totalDuration); |
John Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 189 | mData->reportFrame(totalDuration); |
| 190 | |
John Reck | e70c575 | 2015-03-06 14:40:50 -0800 | [diff] [blame] | 191 | // Keep the fast path as fast as possible. |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 192 | if (CC_LIKELY(totalDuration < mFrameInterval)) { |
| 193 | return; |
| 194 | } |
| 195 | |
John Reck | 6601080 | 2016-03-30 14:19:44 -0700 | [diff] [blame] | 196 | // Only things like Surface.lockHardwareCanvas() are exempt from tracking |
Chris Craik | 1b54fb2 | 2015-06-02 17:40:58 -0700 | [diff] [blame] | 197 | if (frame[FrameInfoIndex::Flags] & EXEMPT_FRAMES_FLAGS) { |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 198 | return; |
| 199 | } |
| 200 | |
John Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 201 | mData->reportJank(); |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 202 | |
| 203 | for (int i = 0; i < NUM_BUCKETS; i++) { |
John Reck | be3fba0 | 2015-07-06 13:49:58 -0700 | [diff] [blame] | 204 | int64_t delta = frame.duration(COMPARISONS[i].start, COMPARISONS[i].end); |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 205 | if (delta >= mThresholds[i] && delta < IGNORE_EXCEEDING) { |
John Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 206 | mData->reportJankType((JankType) i); |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 207 | } |
| 208 | } |
| 209 | } |
| 210 | |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 211 | void 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 Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 223 | } |
John Reck | c7cd9cf | 2016-03-28 10:38:19 -0700 | [diff] [blame] | 224 | if (sFrameStart != FrameInfoIndex::IntendedVsync) { |
| 225 | dprintf(fd, "\nNote: Data has been filtered!"); |
| 226 | } |
John Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 227 | data->dump(fd); |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 228 | dprintf(fd, "\n"); |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | void JankTracker::reset() { |
John Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 232 | mData->reset(); |
John Reck | c7cd9cf | 2016-03-28 10:38:19 -0700 | [diff] [blame] | 233 | sFrameStart = Properties::filterOutTestOverhead |
| 234 | ? FrameInfoIndex::HandleInputStart |
| 235 | : FrameInfoIndex::IntendedVsync; |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 236 | } |
| 237 | |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 238 | } /* namespace uirenderer */ |
| 239 | } /* namespace android */ |