blob: eb9b55f196bbcc5227eb709df136eba6674cbb25 [file] [log] [blame]
John Reckba6adf62015-02-19 14:36:50 -08001/*
2 * Copyright (C) 2015 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#include "JankTracker.h"
17
John Recke70c5752015-03-06 14:40:50 -080018#include <algorithm>
John Reckedc524c2015-03-18 15:24:33 -070019#include <cutils/ashmem.h>
20#include <cutils/log.h>
John Reckba6adf62015-02-19 14:36:50 -080021#include <cstdio>
John Reckedc524c2015-03-18 15:24:33 -070022#include <errno.h>
John Reckba6adf62015-02-19 14:36:50 -080023#include <inttypes.h>
John Reckedc524c2015-03-18 15:24:33 -070024#include <sys/mman.h>
John Reckba6adf62015-02-19 14:36:50 -080025
26namespace android {
27namespace uirenderer {
28
29static const char* JANK_TYPE_NAMES[] = {
30 "Missed Vsync",
31 "High input latency",
32 "Slow UI thread",
33 "Slow bitmap uploads",
John Reckbe3fba02015-07-06 13:49:58 -070034 "Slow issue draw commands",
John Reckba6adf62015-02-19 14:36:50 -080035};
36
37struct Comparison {
John Reckc87be992015-02-20 10:57:22 -080038 FrameInfoIndex start;
39 FrameInfoIndex end;
John Reckba6adf62015-02-19 14:36:50 -080040};
41
42static const Comparison COMPARISONS[] = {
Chris Craik1b54fb22015-06-02 17:40:58 -070043 {FrameInfoIndex::IntendedVsync, FrameInfoIndex::Vsync},
44 {FrameInfoIndex::OldestInputEvent, FrameInfoIndex::Vsync},
45 {FrameInfoIndex::Vsync, FrameInfoIndex::SyncStart},
46 {FrameInfoIndex::SyncStart, FrameInfoIndex::IssueDrawCommandsStart},
47 {FrameInfoIndex::IssueDrawCommandsStart, FrameInfoIndex::FrameCompleted},
John Reckba6adf62015-02-19 14:36:50 -080048};
49
50// If the event exceeds 10 seconds throw it away, this isn't a jank event
51// it's an ANR and will be handled as such
52static const int64_t IGNORE_EXCEEDING = seconds_to_nanoseconds(10);
53
54/*
55 * Frames that are exempt from jank metrics.
56 * First-draw frames, for example, are expected to
57 * be slow, this is hidden from the user with window animations and
58 * other tricks
59 *
60 * Similarly, we don't track direct-drawing via Surface:lockHardwareCanvas()
61 * for now
62 *
63 * TODO: kSurfaceCanvas can negatively impact other drawing by using up
64 * time on the RenderThread, figure out how to attribute that as a jank-causer
65 */
66static const int64_t EXEMPT_FRAMES_FLAGS
Chris Craik1b54fb22015-06-02 17:40:58 -070067 = FrameInfoFlags::WindowLayoutChanged
68 | FrameInfoFlags::SurfaceCanvas;
John Reckba6adf62015-02-19 14:36:50 -080069
John Reckedc524c2015-03-18 15:24:33 -070070// The bucketing algorithm controls so to speak
71// If a frame is <= to this it goes in bucket 0
72static const uint32_t kBucketMinThreshold = 7;
73// If a frame is > this, start counting in increments of 2ms
74static const uint32_t kBucket2msIntervals = 32;
75// If a frame is > this, start counting in increments of 4ms
76static const uint32_t kBucket4msIntervals = 48;
77
78// This will be called every frame, performance sensitive
79// Uses bit twiddling to avoid branching while achieving the packing desired
80static uint32_t frameCountIndexForFrameTime(nsecs_t frameTime, uint32_t max) {
81 uint32_t index = static_cast<uint32_t>(ns2ms(frameTime));
82 // If index > kBucketMinThreshold mask will be 0xFFFFFFFF as a result
83 // of negating 1 (twos compliment, yaay) else mask will be 0
84 uint32_t mask = -(index > kBucketMinThreshold);
85 // If index > threshold, this will essentially perform:
86 // amountAboveThreshold = index - threshold;
87 // index = threshold + (amountAboveThreshold / 2)
88 // However if index is <= this will do nothing. It will underflow, do
89 // a right shift by 0 (no-op), then overflow back to the original value
90 index = ((index - kBucket4msIntervals) >> (index > kBucket4msIntervals))
91 + kBucket4msIntervals;
92 index = ((index - kBucket2msIntervals) >> (index > kBucket2msIntervals))
93 + kBucket2msIntervals;
94 // If index was < minThreshold at the start of all this it's going to
95 // be a pretty garbage value right now. However, mask is 0 so we'll end
96 // up with the desired result of 0.
97 index = (index - kBucketMinThreshold) & mask;
98 return index < max ? index : max;
99}
100
101// Only called when dumping stats, less performance sensitive
102static uint32_t frameTimeForFrameCountIndex(uint32_t index) {
103 index = index + kBucketMinThreshold;
104 if (index > kBucket2msIntervals) {
105 index += (index - kBucket2msIntervals);
106 }
107 if (index > kBucket4msIntervals) {
108 // This works because it was already doubled by the above if
109 // 1 is added to shift slightly more towards the middle of the bucket
110 index += (index - kBucket4msIntervals) + 1;
111 }
112 return index;
113}
114
John Reckba6adf62015-02-19 14:36:50 -0800115JankTracker::JankTracker(nsecs_t frameIntervalNanos) {
John Reckedc524c2015-03-18 15:24:33 -0700116 // By default this will use malloc memory. It may be moved later to ashmem
117 // if there is shared space for it and a request comes in to do that.
118 mData = new ProfileData;
John Reckba6adf62015-02-19 14:36:50 -0800119 reset();
120 setFrameInterval(frameIntervalNanos);
121}
122
John Reckedc524c2015-03-18 15:24:33 -0700123JankTracker::~JankTracker() {
124 freeData();
125}
126
127void JankTracker::freeData() {
128 if (mIsMapped) {
129 munmap(mData, sizeof(ProfileData));
130 } else {
131 delete mData;
132 }
133 mIsMapped = false;
134 mData = nullptr;
135}
136
137void JankTracker::switchStorageToAshmem(int ashmemfd) {
138 int regionSize = ashmem_get_size_region(ashmemfd);
139 if (regionSize < static_cast<int>(sizeof(ProfileData))) {
140 ALOGW("Ashmem region is too small! Received %d, required %u",
John Reck98fa0a32015-03-31 12:03:51 -0700141 regionSize, static_cast<unsigned int>(sizeof(ProfileData)));
John Reckedc524c2015-03-18 15:24:33 -0700142 return;
143 }
144 ProfileData* newData = reinterpret_cast<ProfileData*>(
145 mmap(NULL, sizeof(ProfileData), PROT_READ | PROT_WRITE,
146 MAP_SHARED, ashmemfd, 0));
147 if (newData == MAP_FAILED) {
148 int err = errno;
149 ALOGW("Failed to move profile data to ashmem fd %d, error = %d",
150 ashmemfd, err);
151 return;
152 }
153
154 // The new buffer may have historical data that we want to build on top of
155 // But let's make sure we don't overflow Just In Case
156 uint32_t divider = 0;
157 if (newData->totalFrameCount > (1 << 24)) {
158 divider = 4;
159 }
160 for (size_t i = 0; i < mData->jankTypeCounts.size(); i++) {
161 newData->jankTypeCounts[i] >>= divider;
162 newData->jankTypeCounts[i] += mData->jankTypeCounts[i];
163 }
164 for (size_t i = 0; i < mData->frameCounts.size(); i++) {
165 newData->frameCounts[i] >>= divider;
166 newData->frameCounts[i] += mData->frameCounts[i];
167 }
168 newData->jankFrameCount >>= divider;
169 newData->jankFrameCount += mData->jankFrameCount;
170 newData->totalFrameCount >>= divider;
171 newData->totalFrameCount += mData->totalFrameCount;
John Reck379f2642015-04-06 13:29:25 -0700172 if (newData->statStartTime > mData->statStartTime
173 || newData->statStartTime == 0) {
174 newData->statStartTime = mData->statStartTime;
175 }
John Reckedc524c2015-03-18 15:24:33 -0700176
177 freeData();
178 mData = newData;
179 mIsMapped = true;
180}
181
John Reckba6adf62015-02-19 14:36:50 -0800182void JankTracker::setFrameInterval(nsecs_t frameInterval) {
183 mFrameInterval = frameInterval;
184 mThresholds[kMissedVsync] = 1;
185 /*
186 * Due to interpolation and sample rate differences between the touch
187 * panel and the display (example, 85hz touch panel driving a 60hz display)
188 * we call high latency 1.5 * frameinterval
189 *
190 * NOTE: Be careful when tuning this! A theoretical 1,000hz touch panel
191 * on a 60hz display will show kOldestInputEvent - kIntendedVsync of being 15ms
192 * Thus this must always be larger than frameInterval, or it will fail
193 */
194 mThresholds[kHighInputLatency] = static_cast<int64_t>(1.5 * frameInterval);
195
196 // Note that these do not add up to 1. This is intentional. It's to deal
197 // with variance in values, and should be sort of an upper-bound on what
198 // is reasonable to expect.
199 mThresholds[kSlowUI] = static_cast<int64_t>(.5 * frameInterval);
200 mThresholds[kSlowSync] = static_cast<int64_t>(.2 * frameInterval);
201 mThresholds[kSlowRT] = static_cast<int64_t>(.75 * frameInterval);
202
203}
204
205void JankTracker::addFrame(const FrameInfo& frame) {
John Reckedc524c2015-03-18 15:24:33 -0700206 mData->totalFrameCount++;
John Reckba6adf62015-02-19 14:36:50 -0800207 // Fast-path for jank-free frames
John Reckc87be992015-02-20 10:57:22 -0800208 int64_t totalDuration =
Chris Craik1b54fb22015-06-02 17:40:58 -0700209 frame[FrameInfoIndex::FrameCompleted] - frame[FrameInfoIndex::IntendedVsync];
John Reckedc524c2015-03-18 15:24:33 -0700210 uint32_t framebucket = frameCountIndexForFrameTime(
211 totalDuration, mData->frameCounts.size());
John Recke70c5752015-03-06 14:40:50 -0800212 // Keep the fast path as fast as possible.
John Reckba6adf62015-02-19 14:36:50 -0800213 if (CC_LIKELY(totalDuration < mFrameInterval)) {
John Reckedc524c2015-03-18 15:24:33 -0700214 mData->frameCounts[framebucket]++;
John Reckba6adf62015-02-19 14:36:50 -0800215 return;
216 }
217
Chris Craik1b54fb22015-06-02 17:40:58 -0700218 if (frame[FrameInfoIndex::Flags] & EXEMPT_FRAMES_FLAGS) {
John Reckba6adf62015-02-19 14:36:50 -0800219 return;
220 }
221
John Reckedc524c2015-03-18 15:24:33 -0700222 mData->frameCounts[framebucket]++;
223 mData->jankFrameCount++;
John Reckba6adf62015-02-19 14:36:50 -0800224
225 for (int i = 0; i < NUM_BUCKETS; i++) {
John Reckbe3fba02015-07-06 13:49:58 -0700226 int64_t delta = frame.duration(COMPARISONS[i].start, COMPARISONS[i].end);
John Reckba6adf62015-02-19 14:36:50 -0800227 if (delta >= mThresholds[i] && delta < IGNORE_EXCEEDING) {
John Reckedc524c2015-03-18 15:24:33 -0700228 mData->jankTypeCounts[i]++;
John Reckba6adf62015-02-19 14:36:50 -0800229 }
230 }
231}
232
John Reckedc524c2015-03-18 15:24:33 -0700233void JankTracker::dumpBuffer(const void* buffer, size_t bufsize, int fd) {
234 if (bufsize < sizeof(ProfileData)) {
235 return;
John Reckba6adf62015-02-19 14:36:50 -0800236 }
John Reckedc524c2015-03-18 15:24:33 -0700237 const ProfileData* data = reinterpret_cast<const ProfileData*>(buffer);
238 dumpData(data, fd);
239}
240
241void JankTracker::dumpData(const ProfileData* data, int fd) {
Ying Wang05f56742015-04-07 18:03:31 -0700242 dprintf(fd, "\nStats since: %" PRIu64 "ns", data->statStartTime);
John Reckedc524c2015-03-18 15:24:33 -0700243 dprintf(fd, "\nTotal frames rendered: %u", data->totalFrameCount);
244 dprintf(fd, "\nJanky frames: %u (%.2f%%)", data->jankFrameCount,
245 (float) data->jankFrameCount / (float) data->totalFrameCount * 100.0f);
246 dprintf(fd, "\n90th percentile: %ums", findPercentile(data, 90));
247 dprintf(fd, "\n95th percentile: %ums", findPercentile(data, 95));
248 dprintf(fd, "\n99th percentile: %ums", findPercentile(data, 99));
249 for (int i = 0; i < NUM_BUCKETS; i++) {
250 dprintf(fd, "\nNumber %s: %u", JANK_TYPE_NAMES[i], data->jankTypeCounts[i]);
251 }
252 dprintf(fd, "\n");
John Reckba6adf62015-02-19 14:36:50 -0800253}
254
255void JankTracker::reset() {
John Reckedc524c2015-03-18 15:24:33 -0700256 mData->jankTypeCounts.fill(0);
257 mData->frameCounts.fill(0);
258 mData->totalFrameCount = 0;
259 mData->jankFrameCount = 0;
John Reck379f2642015-04-06 13:29:25 -0700260 mData->statStartTime = systemTime(CLOCK_MONOTONIC);
John Reckba6adf62015-02-19 14:36:50 -0800261}
262
John Reckedc524c2015-03-18 15:24:33 -0700263uint32_t JankTracker::findPercentile(const ProfileData* data, int percentile) {
264 int pos = percentile * data->totalFrameCount / 100;
265 int remaining = data->totalFrameCount - pos;
266 for (int i = data->frameCounts.size() - 1; i >= 0; i--) {
267 remaining -= data->frameCounts[i];
John Recke70c5752015-03-06 14:40:50 -0800268 if (remaining <= 0) {
John Reckedc524c2015-03-18 15:24:33 -0700269 return frameTimeForFrameCountIndex(i);
John Recke70c5752015-03-06 14:40:50 -0800270 }
271 }
272 return 0;
273}
274
John Reckba6adf62015-02-19 14:36:50 -0800275} /* namespace uirenderer */
276} /* namespace android */