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