blob: dd8439647fd32f6b758e27074ea729ddd8817a6c [file] [log] [blame]
John Reck7075c792017-07-05 14:03:43 -07001/*
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 Iliev637ba5e2019-08-16 13:43:08 -040018#include "Properties.h"
John Reck7075c792017-07-05 14:03:43 -070019
20#include <cinttypes>
21
22namespace android {
23namespace uirenderer {
24
25static const char* JANK_TYPE_NAMES[] = {
John Reck1bcacfd2017-11-03 10:12:19 -070026 "Missed Vsync", "High input latency", "Slow UI thread",
Jorim Jaggi10f328c2021-01-19 00:08:02 +010027 "Slow bitmap uploads", "Slow issue draw commands", "Frame deadline missed",
28 "Frame deadline missed (legacy)"};
John Reck7075c792017-07-05 14:03:43 -070029
30// The bucketing algorithm controls so to speak
31// If a frame is <= to this it goes in bucket 0
32static const uint32_t kBucketMinThreshold = 5;
33// If a frame is > this, start counting in increments of 2ms
34static const uint32_t kBucket2msIntervals = 32;
35// If a frame is > this, start counting in increments of 4ms
36static const uint32_t kBucket4msIntervals = 48;
37
38// The interval of the slow frame histogram
39static const uint32_t kSlowFrameBucketIntervalMs = 50;
40// The start point of the slow frame bucket in ms
41static 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
45static 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 Reck1bcacfd2017-11-03 10:12:19 -070055 index = ((index - kBucket4msIntervals) >> (index > kBucket4msIntervals)) + kBucket4msIntervals;
56 index = ((index - kBucket2msIntervals) >> (index > kBucket2msIntervals)) + kBucket2msIntervals;
John Reck7075c792017-07-05 14:03:43 -070057 // 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
65uint32_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
78uint32_t ProfileData::frameTimeForSlowFrameCountIndex(uint32_t index) {
79 return (index * kSlowFrameBucketIntervalMs) + kSlowFrameBucketStartMs;
80}
81
82void 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 Jaggi10f328c2021-01-19 00:08:02 +010098 mJankLegacyFrameCount >>= divider;
99 mJankLegacyFrameCount += other.mJankLegacyFrameCount;
John Reck7075c792017-07-05 14:03:43 -0700100 mTotalFrameCount >>= divider;
101 mTotalFrameCount += other.mTotalFrameCount;
John Reck1bcacfd2017-11-03 10:12:19 -0700102 if (mStatStartTime > other.mStatStartTime || mStatStartTime == 0) {
John Reck7075c792017-07-05 14:03:43 -0700103 mStatStartTime = other.mStatStartTime;
104 }
Stan Iliev7203e1f2019-07-25 13:12:02 -0400105 for (size_t i = 0; i < other.mGPUFrameCounts.size(); i++) {
106 mGPUFrameCounts[i] >>= divider;
107 mGPUFrameCounts[i] += other.mGPUFrameCounts[i];
108 }
Stan Iliev637ba5e2019-08-16 13:43:08 -0400109 mPipelineType = other.mPipelineType;
John Reck7075c792017-07-05 14:03:43 -0700110}
111
112void ProfileData::dump(int fd) const {
113 dprintf(fd, "\nStats since: %" PRIu64 "ns", mStatStartTime);
114 dprintf(fd, "\nTotal frames rendered: %u", mTotalFrameCount);
115 dprintf(fd, "\nJanky frames: %u (%.2f%%)", mJankFrameCount,
John Recke170fb62018-05-07 08:12:07 -0700116 mTotalFrameCount == 0 ? 0.0f
117 : (float)mJankFrameCount / (float)mTotalFrameCount * 100.0f);
Jorim Jaggi10f328c2021-01-19 00:08:02 +0100118 dprintf(fd, "\nJanky frames (legacy): %u (%.2f%%)", mJankLegacyFrameCount, mTotalFrameCount == 0
119 ? 0.0f
120 : (float)mJankLegacyFrameCount / (float)mTotalFrameCount * 100.0f);
John Reck7075c792017-07-05 14:03:43 -0700121 dprintf(fd, "\n50th percentile: %ums", findPercentile(50));
122 dprintf(fd, "\n90th percentile: %ums", findPercentile(90));
123 dprintf(fd, "\n95th percentile: %ums", findPercentile(95));
124 dprintf(fd, "\n99th percentile: %ums", findPercentile(99));
125 for (int i = 0; i < NUM_BUCKETS; i++) {
126 dprintf(fd, "\nNumber %s: %u", JANK_TYPE_NAMES[i], mJankTypeCounts[i]);
127 }
128 dprintf(fd, "\nHISTOGRAM:");
129 histogramForEach([fd](HistogramEntry entry) {
130 dprintf(fd, " %ums=%u", entry.renderTimeMs, entry.frameCount);
131 });
Stan Iliev7203e1f2019-07-25 13:12:02 -0400132 dprintf(fd, "\n50th gpu percentile: %ums", findGPUPercentile(50));
133 dprintf(fd, "\n90th gpu percentile: %ums", findGPUPercentile(90));
134 dprintf(fd, "\n95th gpu percentile: %ums", findGPUPercentile(95));
135 dprintf(fd, "\n99th gpu percentile: %ums", findGPUPercentile(99));
136 dprintf(fd, "\nGPU HISTOGRAM:");
137 histogramGPUForEach([fd](HistogramEntry entry) {
138 dprintf(fd, " %ums=%u", entry.renderTimeMs, entry.frameCount);
139 });
John Reck7075c792017-07-05 14:03:43 -0700140}
141
142uint32_t ProfileData::findPercentile(int percentile) const {
143 int pos = percentile * mTotalFrameCount / 100;
144 int remaining = mTotalFrameCount - pos;
145 for (int i = mSlowFrameCounts.size() - 1; i >= 0; i--) {
146 remaining -= mSlowFrameCounts[i];
147 if (remaining <= 0) {
148 return (i * kSlowFrameBucketIntervalMs) + kSlowFrameBucketStartMs;
149 }
150 }
151 for (int i = mFrameCounts.size() - 1; i >= 0; i--) {
152 remaining -= mFrameCounts[i];
153 if (remaining <= 0) {
154 return frameTimeForFrameCountIndex(i);
155 }
156 }
157 return 0;
158}
159
160void ProfileData::reset() {
161 mJankTypeCounts.fill(0);
162 mFrameCounts.fill(0);
Stan Iliev7203e1f2019-07-25 13:12:02 -0400163 mGPUFrameCounts.fill(0);
John Reck7075c792017-07-05 14:03:43 -0700164 mSlowFrameCounts.fill(0);
165 mTotalFrameCount = 0;
166 mJankFrameCount = 0;
Jorim Jaggi10f328c2021-01-19 00:08:02 +0100167 mJankLegacyFrameCount = 0;
Jerome Gaillarde218c692019-06-14 12:58:57 +0100168 mStatStartTime = systemTime(SYSTEM_TIME_MONOTONIC);
Stan Iliev637ba5e2019-08-16 13:43:08 -0400169 mPipelineType = Properties::getRenderPipelineType();
John Reck7075c792017-07-05 14:03:43 -0700170}
171
172void ProfileData::reportFrame(int64_t duration) {
173 mTotalFrameCount++;
174 uint32_t framebucket = frameCountIndexForFrameTime(duration);
175 if (framebucket <= mFrameCounts.size()) {
176 mFrameCounts[framebucket]++;
177 } else {
178 framebucket = (ns2ms(duration) - kSlowFrameBucketStartMs) / kSlowFrameBucketIntervalMs;
179 framebucket = std::min(framebucket, static_cast<uint32_t>(mSlowFrameCounts.size() - 1));
180 mSlowFrameCounts[framebucket]++;
181 }
182}
183
184void ProfileData::histogramForEach(const std::function<void(HistogramEntry)>& callback) const {
185 for (size_t i = 0; i < mFrameCounts.size(); i++) {
186 callback(HistogramEntry{frameTimeForFrameCountIndex(i), mFrameCounts[i]});
187 }
188 for (size_t i = 0; i < mSlowFrameCounts.size(); i++) {
189 callback(HistogramEntry{frameTimeForSlowFrameCountIndex(i), mSlowFrameCounts[i]});
190 }
191}
192
Stan Iliev7203e1f2019-07-25 13:12:02 -0400193uint32_t ProfileData::findGPUPercentile(int percentile) const {
194 uint32_t totalGPUFrameCount = 0; // this is usually mTotalFrameCount - 3.
195 for (int i = mGPUFrameCounts.size() - 1; i >= 0; i--) {
196 totalGPUFrameCount += mGPUFrameCounts[i];
197 }
198 int pos = percentile * totalGPUFrameCount / 100;
199 int remaining = totalGPUFrameCount - pos;
200 for (int i = mGPUFrameCounts.size() - 1; i >= 0; i--) {
201 remaining -= mGPUFrameCounts[i];
202 if (remaining <= 0) {
203 return GPUFrameTimeForFrameCountIndex(i);
204 }
205 }
206 return 0;
207}
208
209uint32_t ProfileData::GPUFrameTimeForFrameCountIndex(uint32_t index) {
210 return index != 25 ? index + 1 : 4950;
211}
212
213void ProfileData::reportGPUFrame(int64_t duration) {
214 uint32_t index = static_cast<uint32_t>(ns2ms(duration));
215 if (index > 25) {
216 index = 25;
217 }
218
219 mGPUFrameCounts[index]++;
220}
221
222void ProfileData::histogramGPUForEach(const std::function<void(HistogramEntry)>& callback) const {
223 for (size_t i = 0; i < mGPUFrameCounts.size(); i++) {
224 callback(HistogramEntry{GPUFrameTimeForFrameCountIndex(i), mGPUFrameCounts[i]});
225 }
226}
227
John Reck7075c792017-07-05 14:03:43 -0700228} /* namespace uirenderer */
229} /* namespace android */