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 #include "Properties.h"
19
20 #include <cinttypes>
21
22 namespace android {
23 namespace uirenderer {
24
25 static const char* JANK_TYPE_NAMES[] = {
26 "Missed Vsync", "High input latency", "Slow UI thread",
27 "Slow bitmap uploads", "Slow issue draw commands", "Frame deadline missed",
28 "Frame deadline missed (legacy)"};
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
frameCountIndexForFrameTime(nsecs_t frameTime)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
55 index = ((index - kBucket4msIntervals) >> (index > kBucket4msIntervals)) + kBucket4msIntervals;
56 index = ((index - kBucket2msIntervals) >> (index > kBucket2msIntervals)) + kBucket2msIntervals;
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
frameTimeForFrameCountIndex(uint32_t index)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
frameTimeForSlowFrameCountIndex(uint32_t index)78 uint32_t ProfileData::frameTimeForSlowFrameCountIndex(uint32_t index) {
79 return (index * kSlowFrameBucketIntervalMs) + kSlowFrameBucketStartMs;
80 }
81
mergeWith(const ProfileData & other)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;
98 mJankLegacyFrameCount >>= divider;
99 mJankLegacyFrameCount += other.mJankLegacyFrameCount;
100 mTotalFrameCount >>= divider;
101 mTotalFrameCount += other.mTotalFrameCount;
102 if (mStatStartTime > other.mStatStartTime || mStatStartTime == 0) {
103 mStatStartTime = other.mStatStartTime;
104 }
105 for (size_t i = 0; i < other.mGPUFrameCounts.size(); i++) {
106 mGPUFrameCounts[i] >>= divider;
107 mGPUFrameCounts[i] += other.mGPUFrameCounts[i];
108 }
109 mPipelineType = other.mPipelineType;
110 }
111
dump(int fd) const112 void 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,
116 mTotalFrameCount == 0 ? 0.0f
117 : (float)mJankFrameCount / (float)mTotalFrameCount * 100.0f);
118 dprintf(fd, "\nJanky frames (legacy): %u (%.2f%%)", mJankLegacyFrameCount, mTotalFrameCount == 0
119 ? 0.0f
120 : (float)mJankLegacyFrameCount / (float)mTotalFrameCount * 100.0f);
121 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 });
132 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 });
140 dprintf(fd, "\n");
141 }
142
findPercentile(int percentile) const143 uint32_t ProfileData::findPercentile(int percentile) const {
144 int pos = percentile * mTotalFrameCount / 100;
145 int remaining = mTotalFrameCount - pos;
146 for (int i = mSlowFrameCounts.size() - 1; i >= 0; i--) {
147 remaining -= mSlowFrameCounts[i];
148 if (remaining <= 0) {
149 return (i * kSlowFrameBucketIntervalMs) + kSlowFrameBucketStartMs;
150 }
151 }
152 for (int i = mFrameCounts.size() - 1; i >= 0; i--) {
153 remaining -= mFrameCounts[i];
154 if (remaining <= 0) {
155 return frameTimeForFrameCountIndex(i);
156 }
157 }
158 return 0;
159 }
160
reset()161 void ProfileData::reset() {
162 mJankTypeCounts.fill(0);
163 mFrameCounts.fill(0);
164 mGPUFrameCounts.fill(0);
165 mSlowFrameCounts.fill(0);
166 mTotalFrameCount = 0;
167 mJankFrameCount = 0;
168 mJankLegacyFrameCount = 0;
169 mStatStartTime = systemTime(SYSTEM_TIME_MONOTONIC);
170 mPipelineType = Properties::getRenderPipelineType();
171 }
172
reportFrame(int64_t duration)173 void ProfileData::reportFrame(int64_t duration) {
174 mTotalFrameCount++;
175 uint32_t framebucket = frameCountIndexForFrameTime(duration);
176 if (framebucket <= mFrameCounts.size()) {
177 mFrameCounts[framebucket]++;
178 } else {
179 framebucket = (ns2ms(duration) - kSlowFrameBucketStartMs) / kSlowFrameBucketIntervalMs;
180 framebucket = std::min(framebucket, static_cast<uint32_t>(mSlowFrameCounts.size() - 1));
181 mSlowFrameCounts[framebucket]++;
182 }
183 }
184
histogramForEach(const std::function<void (HistogramEntry)> & callback) const185 void ProfileData::histogramForEach(const std::function<void(HistogramEntry)>& callback) const {
186 for (size_t i = 0; i < mFrameCounts.size(); i++) {
187 callback(HistogramEntry{frameTimeForFrameCountIndex(i), mFrameCounts[i]});
188 }
189 for (size_t i = 0; i < mSlowFrameCounts.size(); i++) {
190 callback(HistogramEntry{frameTimeForSlowFrameCountIndex(i), mSlowFrameCounts[i]});
191 }
192 }
193
findGPUPercentile(int percentile) const194 uint32_t ProfileData::findGPUPercentile(int percentile) const {
195 uint32_t totalGPUFrameCount = 0; // this is usually mTotalFrameCount - 3.
196 for (int i = mGPUFrameCounts.size() - 1; i >= 0; i--) {
197 totalGPUFrameCount += mGPUFrameCounts[i];
198 }
199 int pos = percentile * totalGPUFrameCount / 100;
200 int remaining = totalGPUFrameCount - pos;
201 for (int i = mGPUFrameCounts.size() - 1; i >= 0; i--) {
202 remaining -= mGPUFrameCounts[i];
203 if (remaining <= 0) {
204 return GPUFrameTimeForFrameCountIndex(i);
205 }
206 }
207 return 0;
208 }
209
GPUFrameTimeForFrameCountIndex(uint32_t index)210 uint32_t ProfileData::GPUFrameTimeForFrameCountIndex(uint32_t index) {
211 return index != 25 ? index + 1 : 4950;
212 }
213
reportGPUFrame(int64_t duration)214 void ProfileData::reportGPUFrame(int64_t duration) {
215 uint32_t index = static_cast<uint32_t>(ns2ms(duration));
216 if (index > 25) {
217 index = 25;
218 }
219
220 mGPUFrameCounts[index]++;
221 }
222
histogramGPUForEach(const std::function<void (HistogramEntry)> & callback) const223 void ProfileData::histogramGPUForEach(const std::function<void(HistogramEntry)>& callback) const {
224 for (size_t i = 0; i < mGPUFrameCounts.size(); i++) {
225 callback(HistogramEntry{GPUFrameTimeForFrameCountIndex(i), mGPUFrameCounts[i]});
226 }
227 }
228
229 } /* namespace uirenderer */
230 } /* namespace android */