• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 }
141 
findPercentile(int percentile) const142 uint32_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 
reset()160 void ProfileData::reset() {
161     mJankTypeCounts.fill(0);
162     mFrameCounts.fill(0);
163     mGPUFrameCounts.fill(0);
164     mSlowFrameCounts.fill(0);
165     mTotalFrameCount = 0;
166     mJankFrameCount = 0;
167     mJankLegacyFrameCount = 0;
168     mStatStartTime = systemTime(SYSTEM_TIME_MONOTONIC);
169     mPipelineType = Properties::getRenderPipelineType();
170 }
171 
reportFrame(int64_t duration)172 void 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 
histogramForEach(const std::function<void (HistogramEntry)> & callback) const184 void 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 
findGPUPercentile(int percentile) const193 uint32_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 
GPUFrameTimeForFrameCountIndex(uint32_t index)209 uint32_t ProfileData::GPUFrameTimeForFrameCountIndex(uint32_t index) {
210     return index != 25 ? index + 1 : 4950;
211 }
212 
reportGPUFrame(int64_t duration)213 void 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 
histogramGPUForEach(const std::function<void (HistogramEntry)> & callback) const222 void 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 
228 } /* namespace uirenderer */
229 } /* namespace android */