blob: 8ad805b1d874dee60d311d3c1723c4c371486852 [file] [log] [blame]
Jamie Gennis82dbc742012-11-08 19:23:28 -08001/*
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
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -080017// TODO(b/129481165): remove the #pragma below and fix conversion issues
18#pragma clang diagnostic push
19#pragma clang diagnostic ignored "-Wconversion"
20
Jamie Gennis82dbc742012-11-08 19:23:28 -080021// This is needed for stdint.h to define INT64_MAX in C++
22#define __STDC_LIMIT_MACROS
23
Greg Hackmann86efcc02014-03-07 12:44:02 -080024#include <inttypes.h>
25
Yiwei Zhang5434a782018-12-05 18:06:32 -080026#include <android-base/stringprintf.h>
Mark Salyzyna5e161b2016-09-29 08:08:05 -070027#include <android/log.h>
Jamie Gennis6547ff42013-07-16 20:12:42 -070028
Svetoslavd85084b2014-03-20 10:28:31 -070029#include <ui/FrameStats.h>
Jamie Gennis82dbc742012-11-08 19:23:28 -080030
Jamie Gennis82dbc742012-11-08 19:23:28 -080031#include "FrameTracker.h"
Jamie Gennis6547ff42013-07-16 20:12:42 -070032#include "EventLog/EventLog.h"
Jamie Gennis82dbc742012-11-08 19:23:28 -080033
34namespace android {
35
36FrameTracker::FrameTracker() :
37 mOffset(0),
Jamie Gennis6547ff42013-07-16 20:12:42 -070038 mNumFences(0),
39 mDisplayPeriod(0) {
40 resetFrameCountersLocked();
Jamie Gennis82dbc742012-11-08 19:23:28 -080041}
42
43void FrameTracker::setDesiredPresentTime(nsecs_t presentTime) {
Jamie Gennis4b0eba92013-02-05 13:30:24 -080044 Mutex::Autolock lock(mMutex);
Jamie Gennis82dbc742012-11-08 19:23:28 -080045 mFrameRecords[mOffset].desiredPresentTime = presentTime;
46}
47
48void FrameTracker::setFrameReadyTime(nsecs_t readyTime) {
Jamie Gennis4b0eba92013-02-05 13:30:24 -080049 Mutex::Autolock lock(mMutex);
Jamie Gennis82dbc742012-11-08 19:23:28 -080050 mFrameRecords[mOffset].frameReadyTime = readyTime;
51}
52
Brian Anderson3d4039d2016-09-23 16:31:30 -070053void FrameTracker::setFrameReadyFence(
54 std::shared_ptr<FenceTime>&& readyFence) {
Jamie Gennis4b0eba92013-02-05 13:30:24 -080055 Mutex::Autolock lock(mMutex);
Brian Anderson3d4039d2016-09-23 16:31:30 -070056 mFrameRecords[mOffset].frameReadyFence = std::move(readyFence);
Jamie Gennis82dbc742012-11-08 19:23:28 -080057 mNumFences++;
58}
59
60void FrameTracker::setActualPresentTime(nsecs_t presentTime) {
Jamie Gennis4b0eba92013-02-05 13:30:24 -080061 Mutex::Autolock lock(mMutex);
Jamie Gennis82dbc742012-11-08 19:23:28 -080062 mFrameRecords[mOffset].actualPresentTime = presentTime;
63}
64
Brian Anderson3d4039d2016-09-23 16:31:30 -070065void FrameTracker::setActualPresentFence(
66 std::shared_ptr<FenceTime>&& readyFence) {
Jamie Gennis4b0eba92013-02-05 13:30:24 -080067 Mutex::Autolock lock(mMutex);
Brian Anderson3d4039d2016-09-23 16:31:30 -070068 mFrameRecords[mOffset].actualPresentFence = std::move(readyFence);
Jamie Gennis82dbc742012-11-08 19:23:28 -080069 mNumFences++;
70}
71
Jamie Gennis6547ff42013-07-16 20:12:42 -070072void FrameTracker::setDisplayRefreshPeriod(nsecs_t displayPeriod) {
73 Mutex::Autolock lock(mMutex);
74 mDisplayPeriod = displayPeriod;
75}
76
Jamie Gennis82dbc742012-11-08 19:23:28 -080077void FrameTracker::advanceFrame() {
Jamie Gennis4b0eba92013-02-05 13:30:24 -080078 Mutex::Autolock lock(mMutex);
Jamie Gennis6547ff42013-07-16 20:12:42 -070079
80 // Update the statistic to include the frame we just finished.
81 updateStatsLocked(mOffset);
82
83 // Advance to the next frame.
Jamie Gennis82dbc742012-11-08 19:23:28 -080084 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
Peiyong Lin566a3b42018-01-09 18:22:43 -080089 if (mFrameRecords[mOffset].frameReadyFence != nullptr) {
Jamie Gennis82dbc742012-11-08 19:23:28 -080090 // We're clobbering an unsignaled fence, so we need to decrement the
91 // fence count.
Peiyong Lin566a3b42018-01-09 18:22:43 -080092 mFrameRecords[mOffset].frameReadyFence = nullptr;
Jamie Gennis82dbc742012-11-08 19:23:28 -080093 mNumFences--;
94 }
95
Peiyong Lin566a3b42018-01-09 18:22:43 -080096 if (mFrameRecords[mOffset].actualPresentFence != nullptr) {
Jamie Gennis82dbc742012-11-08 19:23:28 -080097 // We're clobbering an unsignaled fence, so we need to decrement the
98 // fence count.
Peiyong Lin566a3b42018-01-09 18:22:43 -080099 mFrameRecords[mOffset].actualPresentFence = nullptr;
Jamie Gennis82dbc742012-11-08 19:23:28 -0800100 mNumFences--;
101 }
Jamie Gennis82dbc742012-11-08 19:23:28 -0800102}
103
Svetoslavd85084b2014-03-20 10:28:31 -0700104void FrameTracker::clearStats() {
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800105 Mutex::Autolock lock(mMutex);
Jamie Gennis82dbc742012-11-08 19:23:28 -0800106 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;
Brian Anderson3d4039d2016-09-23 16:31:30 -0700110 mFrameRecords[i].frameReadyFence.reset();
111 mFrameRecords[i].actualPresentFence.reset();
Jamie Gennis82dbc742012-11-08 19:23:28 -0800112 }
113 mNumFences = 0;
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800114 mFrameRecords[mOffset].desiredPresentTime = INT64_MAX;
115 mFrameRecords[mOffset].frameReadyTime = INT64_MAX;
116 mFrameRecords[mOffset].actualPresentTime = INT64_MAX;
Jamie Gennis82dbc742012-11-08 19:23:28 -0800117}
118
Svetoslavd85084b2014-03-20 10:28:31 -0700119void 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
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700145void FrameTracker::logAndResetStats(const std::string_view& name) {
Jamie Gennis6547ff42013-07-16 20:12:42 -0700146 Mutex::Autolock lock(mMutex);
147 logStatsLocked(name);
148 resetFrameCountersLocked();
149}
150
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800151void FrameTracker::processFencesLocked() const {
Jamie Gennis82dbc742012-11-08 19:23:28 -0800152 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;
Jamie Gennis6547ff42013-07-16 20:12:42 -0700157 bool updated = false;
Jamie Gennis82dbc742012-11-08 19:23:28 -0800158
Brian Anderson3d4039d2016-09-23 16:31:30 -0700159 const std::shared_ptr<FenceTime>& rfence = records[idx].frameReadyFence;
Peiyong Lin566a3b42018-01-09 18:22:43 -0800160 if (rfence != nullptr) {
Jamie Gennis82dbc742012-11-08 19:23:28 -0800161 records[idx].frameReadyTime = rfence->getSignalTime();
162 if (records[idx].frameReadyTime < INT64_MAX) {
Peiyong Lin566a3b42018-01-09 18:22:43 -0800163 records[idx].frameReadyFence = nullptr;
Jamie Gennis82dbc742012-11-08 19:23:28 -0800164 numFences--;
Jamie Gennis6547ff42013-07-16 20:12:42 -0700165 updated = true;
Jamie Gennis82dbc742012-11-08 19:23:28 -0800166 }
167 }
168
Brian Anderson3d4039d2016-09-23 16:31:30 -0700169 const std::shared_ptr<FenceTime>& pfence =
170 records[idx].actualPresentFence;
Peiyong Lin566a3b42018-01-09 18:22:43 -0800171 if (pfence != nullptr) {
Jamie Gennis82dbc742012-11-08 19:23:28 -0800172 records[idx].actualPresentTime = pfence->getSignalTime();
173 if (records[idx].actualPresentTime < INT64_MAX) {
Peiyong Lin566a3b42018-01-09 18:22:43 -0800174 records[idx].actualPresentFence = nullptr;
Jamie Gennis82dbc742012-11-08 19:23:28 -0800175 numFences--;
Jamie Gennis6547ff42013-07-16 20:12:42 -0700176 updated = true;
Jamie Gennis82dbc742012-11-08 19:23:28 -0800177 }
178 }
Jamie Gennis6547ff42013-07-16 20:12:42 -0700179
180 if (updated) {
181 updateStatsLocked(idx);
182 }
Jamie Gennis82dbc742012-11-08 19:23:28 -0800183 }
184}
185
Jamie Gennis6547ff42013-07-16 20:12:42 -0700186void 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
217void FrameTracker::resetFrameCountersLocked() {
218 for (int i = 0; i < NUM_FRAME_BUCKETS; i++) {
219 mNumFrames[i] = 0;
220 }
221}
222
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700223void FrameTracker::logStatsLocked(const std::string_view& name) const {
Jamie Gennis6547ff42013-07-16 20:12:42 -0700224 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
232bool FrameTracker::isFrameValidLocked(size_t idx) const {
233 return mFrameRecords[idx].actualPresentTime > 0 &&
234 mFrameRecords[idx].actualPresentTime < INT64_MAX;
235}
236
Yiwei Zhang5434a782018-12-05 18:06:32 -0800237void FrameTracker::dumpStats(std::string& result) const {
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800238 Mutex::Autolock lock(mMutex);
239 processFencesLocked();
Jamie Gennis82dbc742012-11-08 19:23:28 -0800240
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;
Yiwei Zhang5434a782018-12-05 18:06:32 -0800244 base::StringAppendF(&result, "%" PRId64 "\t%" PRId64 "\t%" PRId64 "\n",
245 mFrameRecords[index].desiredPresentTime,
246 mFrameRecords[index].actualPresentTime,
247 mFrameRecords[index].frameReadyTime);
Jamie Gennis82dbc742012-11-08 19:23:28 -0800248 }
249 result.append("\n");
250}
251
252} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800253
254// TODO(b/129481165): remove the #pragma below and fix conversion issues
255#pragma clang diagnostic pop // ignored "-Wconversion"