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 | */ |
| 16 | #include "JankTracker.h" |
| 17 | |
John Reck | c7cd9cf | 2016-03-28 10:38:19 -0700 | [diff] [blame] | 18 | #include "Properties.h" |
| 19 | |
John Reck | e70c575 | 2015-03-06 14:40:50 -0800 | [diff] [blame] | 20 | #include <algorithm> |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 21 | #include <cutils/ashmem.h> |
| 22 | #include <cutils/log.h> |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 23 | #include <cstdio> |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 24 | #include <errno.h> |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 25 | #include <inttypes.h> |
John Reck | 5ed587f | 2016-03-24 15:57:01 -0700 | [diff] [blame] | 26 | #include <limits> |
| 27 | #include <cmath> |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 28 | #include <sys/mman.h> |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 29 | |
| 30 | namespace android { |
| 31 | namespace uirenderer { |
| 32 | |
| 33 | static const char* JANK_TYPE_NAMES[] = { |
| 34 | "Missed Vsync", |
| 35 | "High input latency", |
| 36 | "Slow UI thread", |
| 37 | "Slow bitmap uploads", |
John Reck | be3fba0 | 2015-07-06 13:49:58 -0700 | [diff] [blame] | 38 | "Slow issue draw commands", |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 39 | }; |
| 40 | |
| 41 | struct Comparison { |
John Reck | c87be99 | 2015-02-20 10:57:22 -0800 | [diff] [blame] | 42 | FrameInfoIndex start; |
| 43 | FrameInfoIndex end; |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 44 | }; |
| 45 | |
| 46 | static const Comparison COMPARISONS[] = { |
Chris Craik | 1b54fb2 | 2015-06-02 17:40:58 -0700 | [diff] [blame] | 47 | {FrameInfoIndex::IntendedVsync, FrameInfoIndex::Vsync}, |
| 48 | {FrameInfoIndex::OldestInputEvent, FrameInfoIndex::Vsync}, |
| 49 | {FrameInfoIndex::Vsync, FrameInfoIndex::SyncStart}, |
| 50 | {FrameInfoIndex::SyncStart, FrameInfoIndex::IssueDrawCommandsStart}, |
| 51 | {FrameInfoIndex::IssueDrawCommandsStart, FrameInfoIndex::FrameCompleted}, |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 52 | }; |
| 53 | |
| 54 | // If the event exceeds 10 seconds throw it away, this isn't a jank event |
| 55 | // it's an ANR and will be handled as such |
| 56 | static const int64_t IGNORE_EXCEEDING = seconds_to_nanoseconds(10); |
| 57 | |
| 58 | /* |
John Reck | 6601080 | 2016-03-30 14:19:44 -0700 | [diff] [blame] | 59 | * We don't track direct-drawing via Surface:lockHardwareCanvas() |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 60 | * for now |
| 61 | * |
| 62 | * TODO: kSurfaceCanvas can negatively impact other drawing by using up |
| 63 | * time on the RenderThread, figure out how to attribute that as a jank-causer |
| 64 | */ |
John Reck | 6601080 | 2016-03-30 14:19:44 -0700 | [diff] [blame] | 65 | static const int64_t EXEMPT_FRAMES_FLAGS = FrameInfoFlags::SurfaceCanvas; |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 66 | |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 67 | // The bucketing algorithm controls so to speak |
| 68 | // If a frame is <= to this it goes in bucket 0 |
John Reck | 6601080 | 2016-03-30 14:19:44 -0700 | [diff] [blame] | 69 | static const uint32_t kBucketMinThreshold = 5; |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 70 | // If a frame is > this, start counting in increments of 2ms |
| 71 | static const uint32_t kBucket2msIntervals = 32; |
| 72 | // If a frame is > this, start counting in increments of 4ms |
| 73 | static const uint32_t kBucket4msIntervals = 48; |
| 74 | |
John Reck | c7cd9cf | 2016-03-28 10:38:19 -0700 | [diff] [blame] | 75 | // For testing purposes to try and eliminate test infra overhead we will |
| 76 | // consider any unknown delay of frame start as part of the test infrastructure |
| 77 | // and filter it out of the frame profile data |
| 78 | static FrameInfoIndex sFrameStart = FrameInfoIndex::IntendedVsync; |
| 79 | |
John Reck | 6601080 | 2016-03-30 14:19:44 -0700 | [diff] [blame] | 80 | // The interval of the slow frame histogram |
| 81 | static const uint32_t kSlowFrameBucketIntervalMs = 50; |
| 82 | // The start point of the slow frame bucket in ms |
| 83 | static const uint32_t kSlowFrameBucketStartMs = 150; |
| 84 | |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 85 | // This will be called every frame, performance sensitive |
| 86 | // Uses bit twiddling to avoid branching while achieving the packing desired |
John Reck | 6601080 | 2016-03-30 14:19:44 -0700 | [diff] [blame] | 87 | static uint32_t frameCountIndexForFrameTime(nsecs_t frameTime) { |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 88 | uint32_t index = static_cast<uint32_t>(ns2ms(frameTime)); |
| 89 | // If index > kBucketMinThreshold mask will be 0xFFFFFFFF as a result |
| 90 | // of negating 1 (twos compliment, yaay) else mask will be 0 |
| 91 | uint32_t mask = -(index > kBucketMinThreshold); |
| 92 | // If index > threshold, this will essentially perform: |
| 93 | // amountAboveThreshold = index - threshold; |
| 94 | // index = threshold + (amountAboveThreshold / 2) |
| 95 | // However if index is <= this will do nothing. It will underflow, do |
| 96 | // a right shift by 0 (no-op), then overflow back to the original value |
| 97 | index = ((index - kBucket4msIntervals) >> (index > kBucket4msIntervals)) |
| 98 | + kBucket4msIntervals; |
| 99 | index = ((index - kBucket2msIntervals) >> (index > kBucket2msIntervals)) |
| 100 | + kBucket2msIntervals; |
| 101 | // If index was < minThreshold at the start of all this it's going to |
| 102 | // be a pretty garbage value right now. However, mask is 0 so we'll end |
| 103 | // up with the desired result of 0. |
| 104 | index = (index - kBucketMinThreshold) & mask; |
John Reck | 6601080 | 2016-03-30 14:19:44 -0700 | [diff] [blame] | 105 | return index; |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | // Only called when dumping stats, less performance sensitive |
| 109 | static uint32_t frameTimeForFrameCountIndex(uint32_t index) { |
| 110 | index = index + kBucketMinThreshold; |
| 111 | if (index > kBucket2msIntervals) { |
| 112 | index += (index - kBucket2msIntervals); |
| 113 | } |
| 114 | if (index > kBucket4msIntervals) { |
| 115 | // This works because it was already doubled by the above if |
| 116 | // 1 is added to shift slightly more towards the middle of the bucket |
| 117 | index += (index - kBucket4msIntervals) + 1; |
| 118 | } |
| 119 | return index; |
| 120 | } |
| 121 | |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 122 | JankTracker::JankTracker(nsecs_t frameIntervalNanos) { |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 123 | // By default this will use malloc memory. It may be moved later to ashmem |
| 124 | // if there is shared space for it and a request comes in to do that. |
| 125 | mData = new ProfileData; |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 126 | reset(); |
| 127 | setFrameInterval(frameIntervalNanos); |
| 128 | } |
| 129 | |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 130 | JankTracker::~JankTracker() { |
| 131 | freeData(); |
| 132 | } |
| 133 | |
| 134 | void JankTracker::freeData() { |
| 135 | if (mIsMapped) { |
| 136 | munmap(mData, sizeof(ProfileData)); |
| 137 | } else { |
| 138 | delete mData; |
| 139 | } |
| 140 | mIsMapped = false; |
| 141 | mData = nullptr; |
| 142 | } |
| 143 | |
| 144 | void JankTracker::switchStorageToAshmem(int ashmemfd) { |
| 145 | int regionSize = ashmem_get_size_region(ashmemfd); |
| 146 | if (regionSize < static_cast<int>(sizeof(ProfileData))) { |
| 147 | ALOGW("Ashmem region is too small! Received %d, required %u", |
John Reck | 98fa0a3 | 2015-03-31 12:03:51 -0700 | [diff] [blame] | 148 | regionSize, static_cast<unsigned int>(sizeof(ProfileData))); |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 149 | return; |
| 150 | } |
| 151 | ProfileData* newData = reinterpret_cast<ProfileData*>( |
| 152 | mmap(NULL, sizeof(ProfileData), PROT_READ | PROT_WRITE, |
| 153 | MAP_SHARED, ashmemfd, 0)); |
| 154 | if (newData == MAP_FAILED) { |
| 155 | int err = errno; |
| 156 | ALOGW("Failed to move profile data to ashmem fd %d, error = %d", |
| 157 | ashmemfd, err); |
| 158 | return; |
| 159 | } |
| 160 | |
| 161 | // The new buffer may have historical data that we want to build on top of |
| 162 | // But let's make sure we don't overflow Just In Case |
| 163 | uint32_t divider = 0; |
| 164 | if (newData->totalFrameCount > (1 << 24)) { |
| 165 | divider = 4; |
| 166 | } |
| 167 | for (size_t i = 0; i < mData->jankTypeCounts.size(); i++) { |
| 168 | newData->jankTypeCounts[i] >>= divider; |
| 169 | newData->jankTypeCounts[i] += mData->jankTypeCounts[i]; |
| 170 | } |
| 171 | for (size_t i = 0; i < mData->frameCounts.size(); i++) { |
| 172 | newData->frameCounts[i] >>= divider; |
| 173 | newData->frameCounts[i] += mData->frameCounts[i]; |
| 174 | } |
| 175 | newData->jankFrameCount >>= divider; |
| 176 | newData->jankFrameCount += mData->jankFrameCount; |
| 177 | newData->totalFrameCount >>= divider; |
| 178 | newData->totalFrameCount += mData->totalFrameCount; |
John Reck | 379f264 | 2015-04-06 13:29:25 -0700 | [diff] [blame] | 179 | if (newData->statStartTime > mData->statStartTime |
| 180 | || newData->statStartTime == 0) { |
| 181 | newData->statStartTime = mData->statStartTime; |
| 182 | } |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 183 | |
| 184 | freeData(); |
| 185 | mData = newData; |
| 186 | mIsMapped = true; |
| 187 | } |
| 188 | |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 189 | void JankTracker::setFrameInterval(nsecs_t frameInterval) { |
| 190 | mFrameInterval = frameInterval; |
| 191 | mThresholds[kMissedVsync] = 1; |
| 192 | /* |
| 193 | * Due to interpolation and sample rate differences between the touch |
| 194 | * panel and the display (example, 85hz touch panel driving a 60hz display) |
| 195 | * we call high latency 1.5 * frameinterval |
| 196 | * |
| 197 | * NOTE: Be careful when tuning this! A theoretical 1,000hz touch panel |
| 198 | * on a 60hz display will show kOldestInputEvent - kIntendedVsync of being 15ms |
| 199 | * Thus this must always be larger than frameInterval, or it will fail |
| 200 | */ |
| 201 | mThresholds[kHighInputLatency] = static_cast<int64_t>(1.5 * frameInterval); |
| 202 | |
| 203 | // Note that these do not add up to 1. This is intentional. It's to deal |
| 204 | // with variance in values, and should be sort of an upper-bound on what |
| 205 | // is reasonable to expect. |
| 206 | mThresholds[kSlowUI] = static_cast<int64_t>(.5 * frameInterval); |
| 207 | mThresholds[kSlowSync] = static_cast<int64_t>(.2 * frameInterval); |
| 208 | mThresholds[kSlowRT] = static_cast<int64_t>(.75 * frameInterval); |
| 209 | |
| 210 | } |
| 211 | |
| 212 | void JankTracker::addFrame(const FrameInfo& frame) { |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 213 | mData->totalFrameCount++; |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 214 | // Fast-path for jank-free frames |
John Reck | 126720a | 2016-04-15 15:16:38 -0700 | [diff] [blame] | 215 | int64_t totalDuration = frame.duration(sFrameStart, FrameInfoIndex::FrameCompleted); |
John Reck | 6601080 | 2016-03-30 14:19:44 -0700 | [diff] [blame] | 216 | uint32_t framebucket = frameCountIndexForFrameTime(totalDuration); |
John Reck | e70c575 | 2015-03-06 14:40:50 -0800 | [diff] [blame] | 217 | // Keep the fast path as fast as possible. |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 218 | if (CC_LIKELY(totalDuration < mFrameInterval)) { |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 219 | mData->frameCounts[framebucket]++; |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 220 | return; |
| 221 | } |
| 222 | |
John Reck | 6601080 | 2016-03-30 14:19:44 -0700 | [diff] [blame] | 223 | // Only things like Surface.lockHardwareCanvas() are exempt from tracking |
Chris Craik | 1b54fb2 | 2015-06-02 17:40:58 -0700 | [diff] [blame] | 224 | if (frame[FrameInfoIndex::Flags] & EXEMPT_FRAMES_FLAGS) { |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 225 | return; |
| 226 | } |
| 227 | |
John Reck | 6601080 | 2016-03-30 14:19:44 -0700 | [diff] [blame] | 228 | if (framebucket <= mData->frameCounts.size()) { |
| 229 | mData->frameCounts[framebucket]++; |
| 230 | } else { |
| 231 | framebucket = (ns2ms(totalDuration) - kSlowFrameBucketStartMs) |
| 232 | / kSlowFrameBucketIntervalMs; |
| 233 | framebucket = std::min(framebucket, |
| 234 | static_cast<uint32_t>(mData->slowFrameCounts.size() - 1)); |
| 235 | framebucket = std::max(framebucket, 0u); |
| 236 | mData->slowFrameCounts[framebucket]++; |
| 237 | } |
| 238 | |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 239 | mData->jankFrameCount++; |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 240 | |
| 241 | for (int i = 0; i < NUM_BUCKETS; i++) { |
John Reck | be3fba0 | 2015-07-06 13:49:58 -0700 | [diff] [blame] | 242 | int64_t delta = frame.duration(COMPARISONS[i].start, COMPARISONS[i].end); |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 243 | if (delta >= mThresholds[i] && delta < IGNORE_EXCEEDING) { |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 244 | mData->jankTypeCounts[i]++; |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 245 | } |
| 246 | } |
| 247 | } |
| 248 | |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 249 | void JankTracker::dumpBuffer(const void* buffer, size_t bufsize, int fd) { |
| 250 | if (bufsize < sizeof(ProfileData)) { |
| 251 | return; |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 252 | } |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 253 | const ProfileData* data = reinterpret_cast<const ProfileData*>(buffer); |
| 254 | dumpData(data, fd); |
| 255 | } |
| 256 | |
| 257 | void JankTracker::dumpData(const ProfileData* data, int fd) { |
John Reck | c7cd9cf | 2016-03-28 10:38:19 -0700 | [diff] [blame] | 258 | if (sFrameStart != FrameInfoIndex::IntendedVsync) { |
| 259 | dprintf(fd, "\nNote: Data has been filtered!"); |
| 260 | } |
Ying Wang | 05f5674 | 2015-04-07 18:03:31 -0700 | [diff] [blame] | 261 | dprintf(fd, "\nStats since: %" PRIu64 "ns", data->statStartTime); |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 262 | dprintf(fd, "\nTotal frames rendered: %u", data->totalFrameCount); |
| 263 | dprintf(fd, "\nJanky frames: %u (%.2f%%)", data->jankFrameCount, |
| 264 | (float) data->jankFrameCount / (float) data->totalFrameCount * 100.0f); |
John Reck | 682573c | 2015-10-30 10:37:35 -0700 | [diff] [blame] | 265 | dprintf(fd, "\n50th percentile: %ums", findPercentile(data, 50)); |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 266 | dprintf(fd, "\n90th percentile: %ums", findPercentile(data, 90)); |
| 267 | dprintf(fd, "\n95th percentile: %ums", findPercentile(data, 95)); |
| 268 | dprintf(fd, "\n99th percentile: %ums", findPercentile(data, 99)); |
| 269 | for (int i = 0; i < NUM_BUCKETS; i++) { |
| 270 | dprintf(fd, "\nNumber %s: %u", JANK_TYPE_NAMES[i], data->jankTypeCounts[i]); |
| 271 | } |
John Reck | 6601080 | 2016-03-30 14:19:44 -0700 | [diff] [blame] | 272 | dprintf(fd, "\nHISTOGRAM:"); |
| 273 | for (size_t i = 0; i < data->frameCounts.size(); i++) { |
| 274 | dprintf(fd, " %ums=%u", frameTimeForFrameCountIndex(i), |
| 275 | data->frameCounts[i]); |
| 276 | } |
| 277 | for (size_t i = 0; i < data->slowFrameCounts.size(); i++) { |
| 278 | dprintf(fd, " %zums=%u", (i * kSlowFrameBucketIntervalMs) + kSlowFrameBucketStartMs, |
| 279 | data->slowFrameCounts[i]); |
| 280 | } |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 281 | dprintf(fd, "\n"); |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | void JankTracker::reset() { |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 285 | mData->jankTypeCounts.fill(0); |
| 286 | mData->frameCounts.fill(0); |
John Reck | 8f55d00 | 2016-04-12 13:10:19 -0700 | [diff] [blame] | 287 | mData->slowFrameCounts.fill(0); |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 288 | mData->totalFrameCount = 0; |
| 289 | mData->jankFrameCount = 0; |
John Reck | 379f264 | 2015-04-06 13:29:25 -0700 | [diff] [blame] | 290 | mData->statStartTime = systemTime(CLOCK_MONOTONIC); |
John Reck | c7cd9cf | 2016-03-28 10:38:19 -0700 | [diff] [blame] | 291 | sFrameStart = Properties::filterOutTestOverhead |
| 292 | ? FrameInfoIndex::HandleInputStart |
| 293 | : FrameInfoIndex::IntendedVsync; |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 294 | } |
| 295 | |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 296 | uint32_t JankTracker::findPercentile(const ProfileData* data, int percentile) { |
| 297 | int pos = percentile * data->totalFrameCount / 100; |
| 298 | int remaining = data->totalFrameCount - pos; |
John Reck | 6601080 | 2016-03-30 14:19:44 -0700 | [diff] [blame] | 299 | for (int i = data->slowFrameCounts.size() - 1; i >= 0; i--) { |
| 300 | remaining -= data->slowFrameCounts[i]; |
| 301 | if (remaining <= 0) { |
| 302 | return (i * kSlowFrameBucketIntervalMs) + kSlowFrameBucketStartMs; |
| 303 | } |
| 304 | } |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 305 | for (int i = data->frameCounts.size() - 1; i >= 0; i--) { |
| 306 | remaining -= data->frameCounts[i]; |
John Reck | e70c575 | 2015-03-06 14:40:50 -0800 | [diff] [blame] | 307 | if (remaining <= 0) { |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 308 | return frameTimeForFrameCountIndex(i); |
John Reck | e70c575 | 2015-03-06 14:40:50 -0800 | [diff] [blame] | 309 | } |
| 310 | } |
| 311 | return 0; |
| 312 | } |
| 313 | |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 314 | } /* namespace uirenderer */ |
| 315 | } /* namespace android */ |