blob: 2fb665e47ca17ee822f29f535e037cc0522ddaeb [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
17// This is needed for stdint.h to define INT64_MAX in C++
18#define __STDC_LIMIT_MACROS
19
Greg Hackmann86efcc02014-03-07 12:44:02 -080020#include <inttypes.h>
21
Jamie Gennis6547ff42013-07-16 20:12:42 -070022#include <cutils/log.h>
23
Jamie Gennis82dbc742012-11-08 19:23:28 -080024#include <ui/Fence.h>
25
26#include <utils/String8.h>
27
28#include "FrameTracker.h"
Jamie Gennis6547ff42013-07-16 20:12:42 -070029#include "EventLog/EventLog.h"
Jamie Gennis82dbc742012-11-08 19:23:28 -080030
31namespace android {
32
33FrameTracker::FrameTracker() :
34 mOffset(0),
Jamie Gennis6547ff42013-07-16 20:12:42 -070035 mNumFences(0),
36 mDisplayPeriod(0) {
37 resetFrameCountersLocked();
Jamie Gennis82dbc742012-11-08 19:23:28 -080038}
39
40void FrameTracker::setDesiredPresentTime(nsecs_t presentTime) {
Jamie Gennis4b0eba92013-02-05 13:30:24 -080041 Mutex::Autolock lock(mMutex);
Jamie Gennis82dbc742012-11-08 19:23:28 -080042 mFrameRecords[mOffset].desiredPresentTime = presentTime;
43}
44
45void FrameTracker::setFrameReadyTime(nsecs_t readyTime) {
Jamie Gennis4b0eba92013-02-05 13:30:24 -080046 Mutex::Autolock lock(mMutex);
Jamie Gennis82dbc742012-11-08 19:23:28 -080047 mFrameRecords[mOffset].frameReadyTime = readyTime;
48}
49
50void FrameTracker::setFrameReadyFence(const sp<Fence>& readyFence) {
Jamie Gennis4b0eba92013-02-05 13:30:24 -080051 Mutex::Autolock lock(mMutex);
Jamie Gennis82dbc742012-11-08 19:23:28 -080052 mFrameRecords[mOffset].frameReadyFence = readyFence;
53 mNumFences++;
54}
55
56void FrameTracker::setActualPresentTime(nsecs_t presentTime) {
Jamie Gennis4b0eba92013-02-05 13:30:24 -080057 Mutex::Autolock lock(mMutex);
Jamie Gennis82dbc742012-11-08 19:23:28 -080058 mFrameRecords[mOffset].actualPresentTime = presentTime;
59}
60
61void FrameTracker::setActualPresentFence(const sp<Fence>& readyFence) {
Jamie Gennis4b0eba92013-02-05 13:30:24 -080062 Mutex::Autolock lock(mMutex);
Jamie Gennis82dbc742012-11-08 19:23:28 -080063 mFrameRecords[mOffset].actualPresentFence = readyFence;
64 mNumFences++;
65}
66
Jamie Gennis6547ff42013-07-16 20:12:42 -070067void FrameTracker::setDisplayRefreshPeriod(nsecs_t displayPeriod) {
68 Mutex::Autolock lock(mMutex);
69 mDisplayPeriod = displayPeriod;
70}
71
Jamie Gennis82dbc742012-11-08 19:23:28 -080072void FrameTracker::advanceFrame() {
Jamie Gennis4b0eba92013-02-05 13:30:24 -080073 Mutex::Autolock lock(mMutex);
Jamie Gennis6547ff42013-07-16 20:12:42 -070074
75 // Update the statistic to include the frame we just finished.
76 updateStatsLocked(mOffset);
77
78 // Advance to the next frame.
Jamie Gennis82dbc742012-11-08 19:23:28 -080079 mOffset = (mOffset+1) % NUM_FRAME_RECORDS;
80 mFrameRecords[mOffset].desiredPresentTime = INT64_MAX;
81 mFrameRecords[mOffset].frameReadyTime = INT64_MAX;
82 mFrameRecords[mOffset].actualPresentTime = INT64_MAX;
83
84 if (mFrameRecords[mOffset].frameReadyFence != NULL) {
85 // We're clobbering an unsignaled fence, so we need to decrement the
86 // fence count.
87 mFrameRecords[mOffset].frameReadyFence = NULL;
88 mNumFences--;
89 }
90
91 if (mFrameRecords[mOffset].actualPresentFence != NULL) {
92 // We're clobbering an unsignaled fence, so we need to decrement the
93 // fence count.
94 mFrameRecords[mOffset].actualPresentFence = NULL;
95 mNumFences--;
96 }
97
98 // Clean up the signaled fences to keep the number of open fence FDs in
99 // this process reasonable.
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800100 processFencesLocked();
Jamie Gennis82dbc742012-11-08 19:23:28 -0800101}
102
103void FrameTracker::clear() {
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800104 Mutex::Autolock lock(mMutex);
Jamie Gennis82dbc742012-11-08 19:23:28 -0800105 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.clear();
110 mFrameRecords[i].actualPresentFence.clear();
111 }
112 mNumFences = 0;
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800113 mFrameRecords[mOffset].desiredPresentTime = INT64_MAX;
114 mFrameRecords[mOffset].frameReadyTime = INT64_MAX;
115 mFrameRecords[mOffset].actualPresentTime = INT64_MAX;
Jamie Gennis82dbc742012-11-08 19:23:28 -0800116}
117
Jamie Gennis6547ff42013-07-16 20:12:42 -0700118void FrameTracker::logAndResetStats(const String8& name) {
119 Mutex::Autolock lock(mMutex);
120 logStatsLocked(name);
121 resetFrameCountersLocked();
122}
123
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800124void FrameTracker::processFencesLocked() const {
Jamie Gennis82dbc742012-11-08 19:23:28 -0800125 FrameRecord* records = const_cast<FrameRecord*>(mFrameRecords);
126 int& numFences = const_cast<int&>(mNumFences);
127
128 for (int i = 1; i < NUM_FRAME_RECORDS && numFences > 0; i++) {
129 size_t idx = (mOffset+NUM_FRAME_RECORDS-i) % NUM_FRAME_RECORDS;
Jamie Gennis6547ff42013-07-16 20:12:42 -0700130 bool updated = false;
Jamie Gennis82dbc742012-11-08 19:23:28 -0800131
132 const sp<Fence>& rfence = records[idx].frameReadyFence;
133 if (rfence != NULL) {
134 records[idx].frameReadyTime = rfence->getSignalTime();
135 if (records[idx].frameReadyTime < INT64_MAX) {
136 records[idx].frameReadyFence = NULL;
137 numFences--;
Jamie Gennis6547ff42013-07-16 20:12:42 -0700138 updated = true;
Jamie Gennis82dbc742012-11-08 19:23:28 -0800139 }
140 }
141
142 const sp<Fence>& pfence = records[idx].actualPresentFence;
143 if (pfence != NULL) {
144 records[idx].actualPresentTime = pfence->getSignalTime();
145 if (records[idx].actualPresentTime < INT64_MAX) {
146 records[idx].actualPresentFence = NULL;
147 numFences--;
Jamie Gennis6547ff42013-07-16 20:12:42 -0700148 updated = true;
Jamie Gennis82dbc742012-11-08 19:23:28 -0800149 }
150 }
Jamie Gennis6547ff42013-07-16 20:12:42 -0700151
152 if (updated) {
153 updateStatsLocked(idx);
154 }
Jamie Gennis82dbc742012-11-08 19:23:28 -0800155 }
156}
157
Jamie Gennis6547ff42013-07-16 20:12:42 -0700158void FrameTracker::updateStatsLocked(size_t newFrameIdx) const {
159 int* numFrames = const_cast<int*>(mNumFrames);
160
161 if (mDisplayPeriod > 0 && isFrameValidLocked(newFrameIdx)) {
162 size_t prevFrameIdx = (newFrameIdx+NUM_FRAME_RECORDS-1) %
163 NUM_FRAME_RECORDS;
164
165 if (isFrameValidLocked(prevFrameIdx)) {
166 nsecs_t newPresentTime =
167 mFrameRecords[newFrameIdx].actualPresentTime;
168 nsecs_t prevPresentTime =
169 mFrameRecords[prevFrameIdx].actualPresentTime;
170
171 nsecs_t duration = newPresentTime - prevPresentTime;
172 int numPeriods = int((duration + mDisplayPeriod/2) /
173 mDisplayPeriod);
174
175 for (int i = 0; i < NUM_FRAME_BUCKETS-1; i++) {
176 int nextBucket = 1 << (i+1);
177 if (numPeriods < nextBucket) {
178 numFrames[i]++;
179 return;
180 }
181 }
182
183 // The last duration bucket is a catch-all.
184 numFrames[NUM_FRAME_BUCKETS-1]++;
185 }
186 }
187}
188
189void FrameTracker::resetFrameCountersLocked() {
190 for (int i = 0; i < NUM_FRAME_BUCKETS; i++) {
191 mNumFrames[i] = 0;
192 }
193}
194
195void FrameTracker::logStatsLocked(const String8& name) const {
196 for (int i = 0; i < NUM_FRAME_BUCKETS; i++) {
197 if (mNumFrames[i] > 0) {
198 EventLog::logFrameDurations(name, mNumFrames, NUM_FRAME_BUCKETS);
199 return;
200 }
201 }
202}
203
204bool FrameTracker::isFrameValidLocked(size_t idx) const {
205 return mFrameRecords[idx].actualPresentTime > 0 &&
206 mFrameRecords[idx].actualPresentTime < INT64_MAX;
207}
208
Jamie Gennis82dbc742012-11-08 19:23:28 -0800209void FrameTracker::dump(String8& result) const {
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800210 Mutex::Autolock lock(mMutex);
211 processFencesLocked();
Jamie Gennis82dbc742012-11-08 19:23:28 -0800212
213 const size_t o = mOffset;
214 for (size_t i = 1; i < NUM_FRAME_RECORDS; i++) {
215 const size_t index = (o+i) % NUM_FRAME_RECORDS;
Greg Hackmann86efcc02014-03-07 12:44:02 -0800216 result.appendFormat("%" PRId64 "\t%" PRId64 "\t%" PRId64 "\n",
Jamie Gennis82dbc742012-11-08 19:23:28 -0800217 mFrameRecords[index].desiredPresentTime,
218 mFrameRecords[index].actualPresentTime,
219 mFrameRecords[index].frameReadyTime);
220 }
221 result.append("\n");
222}
223
224} // namespace android