1 /*
2 * Copyright (C) 2012 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 // TODO(b/129481165): remove the #pragma below and fix conversion issues
18 #pragma clang diagnostic push
19 #pragma clang diagnostic ignored "-Wconversion"
20
21 // This is needed for stdint.h to define INT64_MAX in C++
22 #define __STDC_LIMIT_MACROS
23
24 #include <inttypes.h>
25
26 #include <android-base/stringprintf.h>
27 #include <android/log.h>
28
29 #include <ui/FrameStats.h>
30
31 #include "FrameTracker.h"
32 #include "EventLog/EventLog.h"
33
34 namespace android {
35
FrameTracker()36 FrameTracker::FrameTracker() :
37 mOffset(0),
38 mNumFences(0),
39 mDisplayPeriod(0) {
40 resetFrameCountersLocked();
41 }
42
setDesiredPresentTime(nsecs_t presentTime)43 void FrameTracker::setDesiredPresentTime(nsecs_t presentTime) {
44 Mutex::Autolock lock(mMutex);
45 mFrameRecords[mOffset].desiredPresentTime = presentTime;
46 }
47
setFrameReadyTime(nsecs_t readyTime)48 void FrameTracker::setFrameReadyTime(nsecs_t readyTime) {
49 Mutex::Autolock lock(mMutex);
50 mFrameRecords[mOffset].frameReadyTime = readyTime;
51 }
52
setFrameReadyFence(std::shared_ptr<FenceTime> && readyFence)53 void FrameTracker::setFrameReadyFence(
54 std::shared_ptr<FenceTime>&& readyFence) {
55 Mutex::Autolock lock(mMutex);
56 mFrameRecords[mOffset].frameReadyFence = std::move(readyFence);
57 mNumFences++;
58 }
59
setActualPresentTime(nsecs_t presentTime)60 void FrameTracker::setActualPresentTime(nsecs_t presentTime) {
61 Mutex::Autolock lock(mMutex);
62 mFrameRecords[mOffset].actualPresentTime = presentTime;
63 }
64
setActualPresentFence(std::shared_ptr<FenceTime> && readyFence)65 void FrameTracker::setActualPresentFence(
66 std::shared_ptr<FenceTime>&& readyFence) {
67 Mutex::Autolock lock(mMutex);
68 mFrameRecords[mOffset].actualPresentFence = std::move(readyFence);
69 mNumFences++;
70 }
71
setDisplayRefreshPeriod(nsecs_t displayPeriod)72 void FrameTracker::setDisplayRefreshPeriod(nsecs_t displayPeriod) {
73 Mutex::Autolock lock(mMutex);
74 mDisplayPeriod = displayPeriod;
75 }
76
advanceFrame()77 void FrameTracker::advanceFrame() {
78 Mutex::Autolock lock(mMutex);
79
80 // Update the statistic to include the frame we just finished.
81 updateStatsLocked(mOffset);
82
83 // Advance to the next frame.
84 mOffset = (mOffset+1) % NUM_FRAME_RECORDS;
85 mFrameRecords[mOffset].desiredPresentTime = INT64_MAX;
86 mFrameRecords[mOffset].frameReadyTime = INT64_MAX;
87 mFrameRecords[mOffset].actualPresentTime = INT64_MAX;
88
89 if (mFrameRecords[mOffset].frameReadyFence != nullptr) {
90 // We're clobbering an unsignaled fence, so we need to decrement the
91 // fence count.
92 mFrameRecords[mOffset].frameReadyFence = nullptr;
93 mNumFences--;
94 }
95
96 if (mFrameRecords[mOffset].actualPresentFence != nullptr) {
97 // We're clobbering an unsignaled fence, so we need to decrement the
98 // fence count.
99 mFrameRecords[mOffset].actualPresentFence = nullptr;
100 mNumFences--;
101 }
102 }
103
clearStats()104 void FrameTracker::clearStats() {
105 Mutex::Autolock lock(mMutex);
106 for (size_t i = 0; i < NUM_FRAME_RECORDS; i++) {
107 mFrameRecords[i].desiredPresentTime = 0;
108 mFrameRecords[i].frameReadyTime = 0;
109 mFrameRecords[i].actualPresentTime = 0;
110 mFrameRecords[i].frameReadyFence.reset();
111 mFrameRecords[i].actualPresentFence.reset();
112 }
113 mNumFences = 0;
114 mFrameRecords[mOffset].desiredPresentTime = INT64_MAX;
115 mFrameRecords[mOffset].frameReadyTime = INT64_MAX;
116 mFrameRecords[mOffset].actualPresentTime = INT64_MAX;
117 }
118
getStats(FrameStats * outStats) const119 void FrameTracker::getStats(FrameStats* outStats) const {
120 Mutex::Autolock lock(mMutex);
121 processFencesLocked();
122
123 outStats->refreshPeriodNano = mDisplayPeriod;
124
125 const size_t offset = mOffset;
126 for (size_t i = 1; i < NUM_FRAME_RECORDS; i++) {
127 const size_t index = (offset + i) % NUM_FRAME_RECORDS;
128
129 // Skip frame records with no data (if buffer not yet full).
130 if (mFrameRecords[index].desiredPresentTime == 0) {
131 continue;
132 }
133
134 nsecs_t desiredPresentTimeNano = mFrameRecords[index].desiredPresentTime;
135 outStats->desiredPresentTimesNano.push_back(desiredPresentTimeNano);
136
137 nsecs_t actualPresentTimeNano = mFrameRecords[index].actualPresentTime;
138 outStats->actualPresentTimesNano.push_back(actualPresentTimeNano);
139
140 nsecs_t frameReadyTimeNano = mFrameRecords[index].frameReadyTime;
141 outStats->frameReadyTimesNano.push_back(frameReadyTimeNano);
142 }
143 }
144
logAndResetStats(const std::string_view & name)145 void FrameTracker::logAndResetStats(const std::string_view& name) {
146 Mutex::Autolock lock(mMutex);
147 logStatsLocked(name);
148 resetFrameCountersLocked();
149 }
150
processFencesLocked() const151 void FrameTracker::processFencesLocked() const {
152 FrameRecord* records = const_cast<FrameRecord*>(mFrameRecords);
153 int& numFences = const_cast<int&>(mNumFences);
154
155 for (int i = 1; i < NUM_FRAME_RECORDS && numFences > 0; i++) {
156 size_t idx = (mOffset+NUM_FRAME_RECORDS-i) % NUM_FRAME_RECORDS;
157 bool updated = false;
158
159 const std::shared_ptr<FenceTime>& rfence = records[idx].frameReadyFence;
160 if (rfence != nullptr) {
161 records[idx].frameReadyTime = rfence->getSignalTime();
162 if (records[idx].frameReadyTime < INT64_MAX) {
163 records[idx].frameReadyFence = nullptr;
164 numFences--;
165 updated = true;
166 }
167 }
168
169 const std::shared_ptr<FenceTime>& pfence =
170 records[idx].actualPresentFence;
171 if (pfence != nullptr) {
172 records[idx].actualPresentTime = pfence->getSignalTime();
173 if (records[idx].actualPresentTime < INT64_MAX) {
174 records[idx].actualPresentFence = nullptr;
175 numFences--;
176 updated = true;
177 }
178 }
179
180 if (updated) {
181 updateStatsLocked(idx);
182 }
183 }
184 }
185
updateStatsLocked(size_t newFrameIdx) const186 void FrameTracker::updateStatsLocked(size_t newFrameIdx) const {
187 int* numFrames = const_cast<int*>(mNumFrames);
188
189 if (mDisplayPeriod > 0 && isFrameValidLocked(newFrameIdx)) {
190 size_t prevFrameIdx = (newFrameIdx+NUM_FRAME_RECORDS-1) %
191 NUM_FRAME_RECORDS;
192
193 if (isFrameValidLocked(prevFrameIdx)) {
194 nsecs_t newPresentTime =
195 mFrameRecords[newFrameIdx].actualPresentTime;
196 nsecs_t prevPresentTime =
197 mFrameRecords[prevFrameIdx].actualPresentTime;
198
199 nsecs_t duration = newPresentTime - prevPresentTime;
200 int numPeriods = int((duration + mDisplayPeriod/2) /
201 mDisplayPeriod);
202
203 for (int i = 0; i < NUM_FRAME_BUCKETS-1; i++) {
204 int nextBucket = 1 << (i+1);
205 if (numPeriods < nextBucket) {
206 numFrames[i]++;
207 return;
208 }
209 }
210
211 // The last duration bucket is a catch-all.
212 numFrames[NUM_FRAME_BUCKETS-1]++;
213 }
214 }
215 }
216
resetFrameCountersLocked()217 void FrameTracker::resetFrameCountersLocked() {
218 for (int i = 0; i < NUM_FRAME_BUCKETS; i++) {
219 mNumFrames[i] = 0;
220 }
221 }
222
logStatsLocked(const std::string_view & name) const223 void FrameTracker::logStatsLocked(const std::string_view& name) const {
224 for (int i = 0; i < NUM_FRAME_BUCKETS; i++) {
225 if (mNumFrames[i] > 0) {
226 EventLog::logFrameDurations(name, mNumFrames, NUM_FRAME_BUCKETS);
227 return;
228 }
229 }
230 }
231
isFrameValidLocked(size_t idx) const232 bool FrameTracker::isFrameValidLocked(size_t idx) const {
233 return mFrameRecords[idx].actualPresentTime > 0 &&
234 mFrameRecords[idx].actualPresentTime < INT64_MAX;
235 }
236
dumpStats(std::string & result) const237 void FrameTracker::dumpStats(std::string& result) const {
238 Mutex::Autolock lock(mMutex);
239 processFencesLocked();
240
241 const size_t o = mOffset;
242 for (size_t i = 1; i < NUM_FRAME_RECORDS; i++) {
243 const size_t index = (o+i) % NUM_FRAME_RECORDS;
244 base::StringAppendF(&result, "%" PRId64 "\t%" PRId64 "\t%" PRId64 "\n",
245 mFrameRecords[index].desiredPresentTime,
246 mFrameRecords[index].actualPresentTime,
247 mFrameRecords[index].frameReadyTime);
248 }
249 result.append("\n");
250 }
251
252 } // namespace android
253
254 // TODO(b/129481165): remove the #pragma below and fix conversion issues
255 #pragma clang diagnostic pop // ignored "-Wconversion"
256