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