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(const std::shared_ptr<FenceTime> & readyFence)65 void FrameTracker::setActualPresentFence(const std::shared_ptr<FenceTime>& readyFence) {
66 Mutex::Autolock lock(mMutex);
67 mFrameRecords[mOffset].actualPresentFence = readyFence;
68 mNumFences++;
69 }
70
setDisplayRefreshPeriod(nsecs_t displayPeriod)71 void FrameTracker::setDisplayRefreshPeriod(nsecs_t displayPeriod) {
72 Mutex::Autolock lock(mMutex);
73 mDisplayPeriod = displayPeriod;
74 }
75
advanceFrame()76 void FrameTracker::advanceFrame() {
77 Mutex::Autolock lock(mMutex);
78
79 // Update the statistic to include the frame we just finished.
80 updateStatsLocked(mOffset);
81
82 // Advance to the next frame.
83 mOffset = (mOffset+1) % NUM_FRAME_RECORDS;
84 mFrameRecords[mOffset].desiredPresentTime = INT64_MAX;
85 mFrameRecords[mOffset].frameReadyTime = INT64_MAX;
86 mFrameRecords[mOffset].actualPresentTime = INT64_MAX;
87
88 if (mFrameRecords[mOffset].frameReadyFence != nullptr) {
89 // We're clobbering an unsignaled fence, so we need to decrement the
90 // fence count.
91 mFrameRecords[mOffset].frameReadyFence = nullptr;
92 mNumFences--;
93 }
94
95 if (mFrameRecords[mOffset].actualPresentFence != nullptr) {
96 // We're clobbering an unsignaled fence, so we need to decrement the
97 // fence count.
98 mFrameRecords[mOffset].actualPresentFence = nullptr;
99 mNumFences--;
100 }
101 }
102
clearStats()103 void FrameTracker::clearStats() {
104 Mutex::Autolock lock(mMutex);
105 for (size_t i = 0; i < NUM_FRAME_RECORDS; i++) {
106 mFrameRecords[i].desiredPresentTime = 0;
107 mFrameRecords[i].frameReadyTime = 0;
108 mFrameRecords[i].actualPresentTime = 0;
109 mFrameRecords[i].frameReadyFence.reset();
110 mFrameRecords[i].actualPresentFence.reset();
111 }
112 mNumFences = 0;
113 mFrameRecords[mOffset].desiredPresentTime = INT64_MAX;
114 mFrameRecords[mOffset].frameReadyTime = INT64_MAX;
115 mFrameRecords[mOffset].actualPresentTime = INT64_MAX;
116 }
117
getStats(FrameStats * outStats) const118 void FrameTracker::getStats(FrameStats* outStats) const {
119 Mutex::Autolock lock(mMutex);
120 processFencesLocked();
121
122 outStats->refreshPeriodNano = mDisplayPeriod;
123
124 const size_t offset = mOffset;
125 for (size_t i = 1; i < NUM_FRAME_RECORDS; i++) {
126 const size_t index = (offset + i) % NUM_FRAME_RECORDS;
127
128 // Skip frame records with no data (if buffer not yet full).
129 if (mFrameRecords[index].desiredPresentTime == 0) {
130 continue;
131 }
132
133 nsecs_t desiredPresentTimeNano = mFrameRecords[index].desiredPresentTime;
134 outStats->desiredPresentTimesNano.push_back(desiredPresentTimeNano);
135
136 nsecs_t actualPresentTimeNano = mFrameRecords[index].actualPresentTime;
137 outStats->actualPresentTimesNano.push_back(actualPresentTimeNano);
138
139 nsecs_t frameReadyTimeNano = mFrameRecords[index].frameReadyTime;
140 outStats->frameReadyTimesNano.push_back(frameReadyTimeNano);
141 }
142 }
143
logAndResetStats(const std::string_view & name)144 void FrameTracker::logAndResetStats(const std::string_view& name) {
145 Mutex::Autolock lock(mMutex);
146 logStatsLocked(name);
147 resetFrameCountersLocked();
148 }
149
processFencesLocked() const150 void FrameTracker::processFencesLocked() const {
151 FrameRecord* records = const_cast<FrameRecord*>(mFrameRecords);
152 int& numFences = const_cast<int&>(mNumFences);
153
154 for (int i = 1; i < NUM_FRAME_RECORDS && numFences > 0; i++) {
155 size_t idx = (mOffset+NUM_FRAME_RECORDS-i) % NUM_FRAME_RECORDS;
156 bool updated = false;
157
158 const std::shared_ptr<FenceTime>& rfence = records[idx].frameReadyFence;
159 if (rfence != nullptr) {
160 records[idx].frameReadyTime = rfence->getSignalTime();
161 if (records[idx].frameReadyTime < INT64_MAX) {
162 records[idx].frameReadyFence = nullptr;
163 numFences--;
164 updated = true;
165 }
166 }
167
168 const std::shared_ptr<FenceTime>& pfence =
169 records[idx].actualPresentFence;
170 if (pfence != nullptr) {
171 records[idx].actualPresentTime = pfence->getSignalTime();
172 if (records[idx].actualPresentTime < INT64_MAX) {
173 records[idx].actualPresentFence = nullptr;
174 numFences--;
175 updated = true;
176 }
177 }
178
179 if (updated) {
180 updateStatsLocked(idx);
181 }
182 }
183 }
184
updateStatsLocked(size_t newFrameIdx) const185 void FrameTracker::updateStatsLocked(size_t newFrameIdx) const {
186 int* numFrames = const_cast<int*>(mNumFrames);
187
188 if (mDisplayPeriod > 0 && isFrameValidLocked(newFrameIdx)) {
189 size_t prevFrameIdx = (newFrameIdx+NUM_FRAME_RECORDS-1) %
190 NUM_FRAME_RECORDS;
191
192 if (isFrameValidLocked(prevFrameIdx)) {
193 nsecs_t newPresentTime =
194 mFrameRecords[newFrameIdx].actualPresentTime;
195 nsecs_t prevPresentTime =
196 mFrameRecords[prevFrameIdx].actualPresentTime;
197
198 nsecs_t duration = newPresentTime - prevPresentTime;
199 int numPeriods = int((duration + mDisplayPeriod/2) /
200 mDisplayPeriod);
201
202 for (int i = 0; i < NUM_FRAME_BUCKETS-1; i++) {
203 int nextBucket = 1 << (i+1);
204 if (numPeriods < nextBucket) {
205 numFrames[i]++;
206 return;
207 }
208 }
209
210 // The last duration bucket is a catch-all.
211 numFrames[NUM_FRAME_BUCKETS-1]++;
212 }
213 }
214 }
215
resetFrameCountersLocked()216 void FrameTracker::resetFrameCountersLocked() {
217 for (int i = 0; i < NUM_FRAME_BUCKETS; i++) {
218 mNumFrames[i] = 0;
219 }
220 }
221
logStatsLocked(const std::string_view & name) const222 void FrameTracker::logStatsLocked(const std::string_view& name) const {
223 for (int i = 0; i < NUM_FRAME_BUCKETS; i++) {
224 if (mNumFrames[i] > 0) {
225 EventLog::logFrameDurations(name, mNumFrames, NUM_FRAME_BUCKETS);
226 return;
227 }
228 }
229 }
230
isFrameValidLocked(size_t idx) const231 bool FrameTracker::isFrameValidLocked(size_t idx) const {
232 return mFrameRecords[idx].actualPresentTime > 0 &&
233 mFrameRecords[idx].actualPresentTime < INT64_MAX;
234 }
235
dumpStats(std::string & result) const236 void FrameTracker::dumpStats(std::string& result) const {
237 Mutex::Autolock lock(mMutex);
238 processFencesLocked();
239
240 const size_t o = mOffset;
241 for (size_t i = 1; i < NUM_FRAME_RECORDS; i++) {
242 const size_t index = (o+i) % NUM_FRAME_RECORDS;
243 base::StringAppendF(&result, "%" PRId64 "\t%" PRId64 "\t%" PRId64 "\n",
244 mFrameRecords[index].desiredPresentTime,
245 mFrameRecords[index].actualPresentTime,
246 mFrameRecords[index].frameReadyTime);
247 }
248 result.append("\n");
249 }
250
251 } // namespace android
252
253 // TODO(b/129481165): remove the #pragma below and fix conversion issues
254 #pragma clang diagnostic pop // ignored "-Wconversion"
255