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" |
Stan Iliev | 637ba5e | 2019-08-16 13:43:08 -0400 | [diff] [blame] | 18 | #include "Properties.h" |
John Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 19 | |
| 20 | #include <cinttypes> |
| 21 | |
| 22 | namespace android { |
| 23 | namespace uirenderer { |
| 24 | |
| 25 | static const char* JANK_TYPE_NAMES[] = { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 26 | "Missed Vsync", "High input latency", "Slow UI thread", |
Jorim Jaggi | 10f328c | 2021-01-19 00:08:02 +0100 | [diff] [blame] | 27 | "Slow bitmap uploads", "Slow issue draw commands", "Frame deadline missed", |
| 28 | "Frame deadline missed (legacy)"}; |
John Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 29 | |
| 30 | // The bucketing algorithm controls so to speak |
| 31 | // If a frame is <= to this it goes in bucket 0 |
| 32 | static const uint32_t kBucketMinThreshold = 5; |
| 33 | // If a frame is > this, start counting in increments of 2ms |
| 34 | static const uint32_t kBucket2msIntervals = 32; |
| 35 | // If a frame is > this, start counting in increments of 4ms |
| 36 | static const uint32_t kBucket4msIntervals = 48; |
| 37 | |
| 38 | // The interval of the slow frame histogram |
| 39 | static const uint32_t kSlowFrameBucketIntervalMs = 50; |
| 40 | // The start point of the slow frame bucket in ms |
| 41 | static const uint32_t kSlowFrameBucketStartMs = 150; |
| 42 | |
| 43 | // This will be called every frame, performance sensitive |
| 44 | // Uses bit twiddling to avoid branching while achieving the packing desired |
| 45 | static uint32_t frameCountIndexForFrameTime(nsecs_t frameTime) { |
| 46 | uint32_t index = static_cast<uint32_t>(ns2ms(frameTime)); |
| 47 | // If index > kBucketMinThreshold mask will be 0xFFFFFFFF as a result |
| 48 | // of negating 1 (twos compliment, yaay) else mask will be 0 |
| 49 | uint32_t mask = -(index > kBucketMinThreshold); |
| 50 | // If index > threshold, this will essentially perform: |
| 51 | // amountAboveThreshold = index - threshold; |
| 52 | // index = threshold + (amountAboveThreshold / 2) |
| 53 | // However if index is <= this will do nothing. It will underflow, do |
| 54 | // 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] | 55 | index = ((index - kBucket4msIntervals) >> (index > kBucket4msIntervals)) + kBucket4msIntervals; |
| 56 | index = ((index - kBucket2msIntervals) >> (index > kBucket2msIntervals)) + kBucket2msIntervals; |
John Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 57 | // If index was < minThreshold at the start of all this it's going to |
| 58 | // be a pretty garbage value right now. However, mask is 0 so we'll end |
| 59 | // up with the desired result of 0. |
| 60 | index = (index - kBucketMinThreshold) & mask; |
| 61 | return index; |
| 62 | } |
| 63 | |
| 64 | // Only called when dumping stats, less performance sensitive |
| 65 | uint32_t ProfileData::frameTimeForFrameCountIndex(uint32_t index) { |
| 66 | index = index + kBucketMinThreshold; |
| 67 | if (index > kBucket2msIntervals) { |
| 68 | index += (index - kBucket2msIntervals); |
| 69 | } |
| 70 | if (index > kBucket4msIntervals) { |
| 71 | // This works because it was already doubled by the above if |
| 72 | // 1 is added to shift slightly more towards the middle of the bucket |
| 73 | index += (index - kBucket4msIntervals) + 1; |
| 74 | } |
| 75 | return index; |
| 76 | } |
| 77 | |
| 78 | uint32_t ProfileData::frameTimeForSlowFrameCountIndex(uint32_t index) { |
| 79 | return (index * kSlowFrameBucketIntervalMs) + kSlowFrameBucketStartMs; |
| 80 | } |
| 81 | |
| 82 | void ProfileData::mergeWith(const ProfileData& other) { |
| 83 | // Make sure we don't overflow Just In Case |
| 84 | uint32_t divider = 0; |
| 85 | if (mTotalFrameCount > (1 << 24)) { |
| 86 | divider = 4; |
| 87 | } |
| 88 | for (size_t i = 0; i < other.mJankTypeCounts.size(); i++) { |
| 89 | mJankTypeCounts[i] >>= divider; |
| 90 | mJankTypeCounts[i] += other.mJankTypeCounts[i]; |
| 91 | } |
| 92 | for (size_t i = 0; i < other.mFrameCounts.size(); i++) { |
| 93 | mFrameCounts[i] >>= divider; |
| 94 | mFrameCounts[i] += other.mFrameCounts[i]; |
| 95 | } |
| 96 | mJankFrameCount >>= divider; |
| 97 | mJankFrameCount += other.mJankFrameCount; |
Jorim Jaggi | 10f328c | 2021-01-19 00:08:02 +0100 | [diff] [blame] | 98 | mJankLegacyFrameCount >>= divider; |
| 99 | mJankLegacyFrameCount += other.mJankLegacyFrameCount; |
John Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 100 | mTotalFrameCount >>= divider; |
| 101 | mTotalFrameCount += other.mTotalFrameCount; |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 102 | if (mStatStartTime > other.mStatStartTime || mStatStartTime == 0) { |
John Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 103 | mStatStartTime = other.mStatStartTime; |
| 104 | } |
Stan Iliev | 7203e1f | 2019-07-25 13:12:02 -0400 | [diff] [blame] | 105 | for (size_t i = 0; i < other.mGPUFrameCounts.size(); i++) { |
| 106 | mGPUFrameCounts[i] >>= divider; |
| 107 | mGPUFrameCounts[i] += other.mGPUFrameCounts[i]; |
| 108 | } |
Stan Iliev | 637ba5e | 2019-08-16 13:43:08 -0400 | [diff] [blame] | 109 | mPipelineType = other.mPipelineType; |
John Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | void ProfileData::dump(int fd) const { |
Jerome Gaillard | 87ef524 | 2024-02-13 16:33:00 +0000 | [diff] [blame] | 113 | #ifdef __ANDROID__ |
John Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 114 | dprintf(fd, "\nStats since: %" PRIu64 "ns", mStatStartTime); |
| 115 | dprintf(fd, "\nTotal frames rendered: %u", mTotalFrameCount); |
| 116 | dprintf(fd, "\nJanky frames: %u (%.2f%%)", mJankFrameCount, |
John Reck | e170fb6 | 2018-05-07 08:12:07 -0700 | [diff] [blame] | 117 | mTotalFrameCount == 0 ? 0.0f |
| 118 | : (float)mJankFrameCount / (float)mTotalFrameCount * 100.0f); |
Jorim Jaggi | 10f328c | 2021-01-19 00:08:02 +0100 | [diff] [blame] | 119 | dprintf(fd, "\nJanky frames (legacy): %u (%.2f%%)", mJankLegacyFrameCount, mTotalFrameCount == 0 |
| 120 | ? 0.0f |
| 121 | : (float)mJankLegacyFrameCount / (float)mTotalFrameCount * 100.0f); |
John Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 122 | dprintf(fd, "\n50th percentile: %ums", findPercentile(50)); |
| 123 | dprintf(fd, "\n90th percentile: %ums", findPercentile(90)); |
| 124 | dprintf(fd, "\n95th percentile: %ums", findPercentile(95)); |
| 125 | dprintf(fd, "\n99th percentile: %ums", findPercentile(99)); |
| 126 | for (int i = 0; i < NUM_BUCKETS; i++) { |
| 127 | dprintf(fd, "\nNumber %s: %u", JANK_TYPE_NAMES[i], mJankTypeCounts[i]); |
| 128 | } |
| 129 | dprintf(fd, "\nHISTOGRAM:"); |
| 130 | histogramForEach([fd](HistogramEntry entry) { |
| 131 | dprintf(fd, " %ums=%u", entry.renderTimeMs, entry.frameCount); |
| 132 | }); |
Stan Iliev | 7203e1f | 2019-07-25 13:12:02 -0400 | [diff] [blame] | 133 | dprintf(fd, "\n50th gpu percentile: %ums", findGPUPercentile(50)); |
| 134 | dprintf(fd, "\n90th gpu percentile: %ums", findGPUPercentile(90)); |
| 135 | dprintf(fd, "\n95th gpu percentile: %ums", findGPUPercentile(95)); |
| 136 | dprintf(fd, "\n99th gpu percentile: %ums", findGPUPercentile(99)); |
| 137 | dprintf(fd, "\nGPU HISTOGRAM:"); |
| 138 | histogramGPUForEach([fd](HistogramEntry entry) { |
| 139 | dprintf(fd, " %ums=%u", entry.renderTimeMs, entry.frameCount); |
| 140 | }); |
Chet Haase | 33c1ea7 | 2021-09-27 20:56:08 +0000 | [diff] [blame] | 141 | dprintf(fd, "\n"); |
Jerome Gaillard | 87ef524 | 2024-02-13 16:33:00 +0000 | [diff] [blame] | 142 | #endif |
John Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | uint32_t ProfileData::findPercentile(int percentile) const { |
| 146 | int pos = percentile * mTotalFrameCount / 100; |
| 147 | int remaining = mTotalFrameCount - pos; |
| 148 | for (int i = mSlowFrameCounts.size() - 1; i >= 0; i--) { |
| 149 | remaining -= mSlowFrameCounts[i]; |
| 150 | if (remaining <= 0) { |
| 151 | return (i * kSlowFrameBucketIntervalMs) + kSlowFrameBucketStartMs; |
| 152 | } |
| 153 | } |
| 154 | for (int i = mFrameCounts.size() - 1; i >= 0; i--) { |
| 155 | remaining -= mFrameCounts[i]; |
| 156 | if (remaining <= 0) { |
| 157 | return frameTimeForFrameCountIndex(i); |
| 158 | } |
| 159 | } |
| 160 | return 0; |
| 161 | } |
| 162 | |
| 163 | void ProfileData::reset() { |
| 164 | mJankTypeCounts.fill(0); |
| 165 | mFrameCounts.fill(0); |
Stan Iliev | 7203e1f | 2019-07-25 13:12:02 -0400 | [diff] [blame] | 166 | mGPUFrameCounts.fill(0); |
John Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 167 | mSlowFrameCounts.fill(0); |
| 168 | mTotalFrameCount = 0; |
| 169 | mJankFrameCount = 0; |
Jorim Jaggi | 10f328c | 2021-01-19 00:08:02 +0100 | [diff] [blame] | 170 | mJankLegacyFrameCount = 0; |
Jerome Gaillard | e218c69 | 2019-06-14 12:58:57 +0100 | [diff] [blame] | 171 | mStatStartTime = systemTime(SYSTEM_TIME_MONOTONIC); |
Stan Iliev | 637ba5e | 2019-08-16 13:43:08 -0400 | [diff] [blame] | 172 | mPipelineType = Properties::getRenderPipelineType(); |
John Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | void ProfileData::reportFrame(int64_t duration) { |
| 176 | mTotalFrameCount++; |
| 177 | uint32_t framebucket = frameCountIndexForFrameTime(duration); |
| 178 | if (framebucket <= mFrameCounts.size()) { |
| 179 | mFrameCounts[framebucket]++; |
| 180 | } else { |
| 181 | framebucket = (ns2ms(duration) - kSlowFrameBucketStartMs) / kSlowFrameBucketIntervalMs; |
| 182 | framebucket = std::min(framebucket, static_cast<uint32_t>(mSlowFrameCounts.size() - 1)); |
| 183 | mSlowFrameCounts[framebucket]++; |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | void ProfileData::histogramForEach(const std::function<void(HistogramEntry)>& callback) const { |
| 188 | for (size_t i = 0; i < mFrameCounts.size(); i++) { |
| 189 | callback(HistogramEntry{frameTimeForFrameCountIndex(i), mFrameCounts[i]}); |
| 190 | } |
| 191 | for (size_t i = 0; i < mSlowFrameCounts.size(); i++) { |
| 192 | callback(HistogramEntry{frameTimeForSlowFrameCountIndex(i), mSlowFrameCounts[i]}); |
| 193 | } |
| 194 | } |
| 195 | |
Stan Iliev | 7203e1f | 2019-07-25 13:12:02 -0400 | [diff] [blame] | 196 | uint32_t ProfileData::findGPUPercentile(int percentile) const { |
| 197 | uint32_t totalGPUFrameCount = 0; // this is usually mTotalFrameCount - 3. |
| 198 | for (int i = mGPUFrameCounts.size() - 1; i >= 0; i--) { |
| 199 | totalGPUFrameCount += mGPUFrameCounts[i]; |
| 200 | } |
| 201 | int pos = percentile * totalGPUFrameCount / 100; |
| 202 | int remaining = totalGPUFrameCount - pos; |
| 203 | for (int i = mGPUFrameCounts.size() - 1; i >= 0; i--) { |
| 204 | remaining -= mGPUFrameCounts[i]; |
| 205 | if (remaining <= 0) { |
| 206 | return GPUFrameTimeForFrameCountIndex(i); |
| 207 | } |
| 208 | } |
| 209 | return 0; |
| 210 | } |
| 211 | |
| 212 | uint32_t ProfileData::GPUFrameTimeForFrameCountIndex(uint32_t index) { |
| 213 | return index != 25 ? index + 1 : 4950; |
| 214 | } |
| 215 | |
| 216 | void ProfileData::reportGPUFrame(int64_t duration) { |
| 217 | uint32_t index = static_cast<uint32_t>(ns2ms(duration)); |
| 218 | if (index > 25) { |
| 219 | index = 25; |
| 220 | } |
| 221 | |
| 222 | mGPUFrameCounts[index]++; |
| 223 | } |
| 224 | |
| 225 | void ProfileData::histogramGPUForEach(const std::function<void(HistogramEntry)>& callback) const { |
| 226 | for (size_t i = 0; i < mGPUFrameCounts.size(); i++) { |
| 227 | callback(HistogramEntry{GPUFrameTimeForFrameCountIndex(i), mGPUFrameCounts[i]}); |
| 228 | } |
| 229 | } |
| 230 | |
John Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 231 | } /* namespace uirenderer */ |
| 232 | } /* namespace android */ |