John Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | |
| 17 | #include "ProfileData.h" |
| 18 | |
| 19 | #include <cinttypes> |
| 20 | |
| 21 | namespace android { |
| 22 | namespace uirenderer { |
| 23 | |
| 24 | static const char* JANK_TYPE_NAMES[] = { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 25 | "Missed Vsync", "High input latency", "Slow UI thread", |
| 26 | "Slow bitmap uploads", "Slow issue draw commands", |
John Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 27 | }; |
| 28 | |
| 29 | // The bucketing algorithm controls so to speak |
| 30 | // If a frame is <= to this it goes in bucket 0 |
| 31 | static const uint32_t kBucketMinThreshold = 5; |
| 32 | // If a frame is > this, start counting in increments of 2ms |
| 33 | static const uint32_t kBucket2msIntervals = 32; |
| 34 | // If a frame is > this, start counting in increments of 4ms |
| 35 | static const uint32_t kBucket4msIntervals = 48; |
| 36 | |
| 37 | // The interval of the slow frame histogram |
| 38 | static const uint32_t kSlowFrameBucketIntervalMs = 50; |
| 39 | // The start point of the slow frame bucket in ms |
| 40 | static const uint32_t kSlowFrameBucketStartMs = 150; |
| 41 | |
| 42 | // This will be called every frame, performance sensitive |
| 43 | // Uses bit twiddling to avoid branching while achieving the packing desired |
| 44 | static uint32_t frameCountIndexForFrameTime(nsecs_t frameTime) { |
| 45 | uint32_t index = static_cast<uint32_t>(ns2ms(frameTime)); |
| 46 | // If index > kBucketMinThreshold mask will be 0xFFFFFFFF as a result |
| 47 | // of negating 1 (twos compliment, yaay) else mask will be 0 |
| 48 | uint32_t mask = -(index > kBucketMinThreshold); |
| 49 | // If index > threshold, this will essentially perform: |
| 50 | // amountAboveThreshold = index - threshold; |
| 51 | // index = threshold + (amountAboveThreshold / 2) |
| 52 | // However if index is <= this will do nothing. It will underflow, do |
| 53 | // a right shift by 0 (no-op), then overflow back to the original value |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 54 | index = ((index - kBucket4msIntervals) >> (index > kBucket4msIntervals)) + kBucket4msIntervals; |
| 55 | index = ((index - kBucket2msIntervals) >> (index > kBucket2msIntervals)) + kBucket2msIntervals; |
John Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 56 | // If index was < minThreshold at the start of all this it's going to |
| 57 | // be a pretty garbage value right now. However, mask is 0 so we'll end |
| 58 | // up with the desired result of 0. |
| 59 | index = (index - kBucketMinThreshold) & mask; |
| 60 | return index; |
| 61 | } |
| 62 | |
| 63 | // Only called when dumping stats, less performance sensitive |
| 64 | uint32_t ProfileData::frameTimeForFrameCountIndex(uint32_t index) { |
| 65 | index = index + kBucketMinThreshold; |
| 66 | if (index > kBucket2msIntervals) { |
| 67 | index += (index - kBucket2msIntervals); |
| 68 | } |
| 69 | if (index > kBucket4msIntervals) { |
| 70 | // This works because it was already doubled by the above if |
| 71 | // 1 is added to shift slightly more towards the middle of the bucket |
| 72 | index += (index - kBucket4msIntervals) + 1; |
| 73 | } |
| 74 | return index; |
| 75 | } |
| 76 | |
| 77 | uint32_t ProfileData::frameTimeForSlowFrameCountIndex(uint32_t index) { |
| 78 | return (index * kSlowFrameBucketIntervalMs) + kSlowFrameBucketStartMs; |
| 79 | } |
| 80 | |
| 81 | void ProfileData::mergeWith(const ProfileData& other) { |
| 82 | // Make sure we don't overflow Just In Case |
| 83 | uint32_t divider = 0; |
| 84 | if (mTotalFrameCount > (1 << 24)) { |
| 85 | divider = 4; |
| 86 | } |
| 87 | for (size_t i = 0; i < other.mJankTypeCounts.size(); i++) { |
| 88 | mJankTypeCounts[i] >>= divider; |
| 89 | mJankTypeCounts[i] += other.mJankTypeCounts[i]; |
| 90 | } |
| 91 | for (size_t i = 0; i < other.mFrameCounts.size(); i++) { |
| 92 | mFrameCounts[i] >>= divider; |
| 93 | mFrameCounts[i] += other.mFrameCounts[i]; |
| 94 | } |
| 95 | mJankFrameCount >>= divider; |
| 96 | mJankFrameCount += other.mJankFrameCount; |
| 97 | mTotalFrameCount >>= divider; |
| 98 | mTotalFrameCount += other.mTotalFrameCount; |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 99 | if (mStatStartTime > other.mStatStartTime || mStatStartTime == 0) { |
John Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 100 | mStatStartTime = other.mStatStartTime; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | void ProfileData::dump(int fd) const { |
| 105 | dprintf(fd, "\nStats since: %" PRIu64 "ns", mStatStartTime); |
| 106 | dprintf(fd, "\nTotal frames rendered: %u", mTotalFrameCount); |
| 107 | dprintf(fd, "\nJanky frames: %u (%.2f%%)", mJankFrameCount, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 108 | (float)mJankFrameCount / (float)mTotalFrameCount * 100.0f); |
John Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 109 | dprintf(fd, "\n50th percentile: %ums", findPercentile(50)); |
| 110 | dprintf(fd, "\n90th percentile: %ums", findPercentile(90)); |
| 111 | dprintf(fd, "\n95th percentile: %ums", findPercentile(95)); |
| 112 | dprintf(fd, "\n99th percentile: %ums", findPercentile(99)); |
| 113 | for (int i = 0; i < NUM_BUCKETS; i++) { |
| 114 | dprintf(fd, "\nNumber %s: %u", JANK_TYPE_NAMES[i], mJankTypeCounts[i]); |
| 115 | } |
| 116 | dprintf(fd, "\nHISTOGRAM:"); |
| 117 | histogramForEach([fd](HistogramEntry entry) { |
| 118 | dprintf(fd, " %ums=%u", entry.renderTimeMs, entry.frameCount); |
| 119 | }); |
| 120 | } |
| 121 | |
| 122 | uint32_t ProfileData::findPercentile(int percentile) const { |
| 123 | int pos = percentile * mTotalFrameCount / 100; |
| 124 | int remaining = mTotalFrameCount - pos; |
| 125 | for (int i = mSlowFrameCounts.size() - 1; i >= 0; i--) { |
| 126 | remaining -= mSlowFrameCounts[i]; |
| 127 | if (remaining <= 0) { |
| 128 | return (i * kSlowFrameBucketIntervalMs) + kSlowFrameBucketStartMs; |
| 129 | } |
| 130 | } |
| 131 | for (int i = mFrameCounts.size() - 1; i >= 0; i--) { |
| 132 | remaining -= mFrameCounts[i]; |
| 133 | if (remaining <= 0) { |
| 134 | return frameTimeForFrameCountIndex(i); |
| 135 | } |
| 136 | } |
| 137 | return 0; |
| 138 | } |
| 139 | |
| 140 | void ProfileData::reset() { |
| 141 | mJankTypeCounts.fill(0); |
| 142 | mFrameCounts.fill(0); |
| 143 | mSlowFrameCounts.fill(0); |
| 144 | mTotalFrameCount = 0; |
| 145 | mJankFrameCount = 0; |
| 146 | mStatStartTime = systemTime(CLOCK_MONOTONIC); |
| 147 | } |
| 148 | |
| 149 | void ProfileData::reportFrame(int64_t duration) { |
| 150 | mTotalFrameCount++; |
| 151 | uint32_t framebucket = frameCountIndexForFrameTime(duration); |
| 152 | if (framebucket <= mFrameCounts.size()) { |
| 153 | mFrameCounts[framebucket]++; |
| 154 | } else { |
| 155 | framebucket = (ns2ms(duration) - kSlowFrameBucketStartMs) / kSlowFrameBucketIntervalMs; |
| 156 | framebucket = std::min(framebucket, static_cast<uint32_t>(mSlowFrameCounts.size() - 1)); |
| 157 | mSlowFrameCounts[framebucket]++; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | void ProfileData::histogramForEach(const std::function<void(HistogramEntry)>& callback) const { |
| 162 | for (size_t i = 0; i < mFrameCounts.size(); i++) { |
| 163 | callback(HistogramEntry{frameTimeForFrameCountIndex(i), mFrameCounts[i]}); |
| 164 | } |
| 165 | for (size_t i = 0; i < mSlowFrameCounts.size(); i++) { |
| 166 | callback(HistogramEntry{frameTimeForSlowFrameCountIndex(i), mSlowFrameCounts[i]}); |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | } /* namespace uirenderer */ |
| 171 | } /* namespace android */ |