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