2012-11-09 03:23:28 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2012 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// This is needed for stdint.h to define INT64_MAX in C++
|
|
|
|
#define __STDC_LIMIT_MACROS
|
|
|
|
|
2014-03-07 20:44:02 +00:00
|
|
|
#include <inttypes.h>
|
|
|
|
|
2013-07-17 03:12:42 +00:00
|
|
|
#include <cutils/log.h>
|
|
|
|
|
2012-11-09 03:23:28 +00:00
|
|
|
#include <ui/Fence.h>
|
2014-03-20 17:28:31 +00:00
|
|
|
#include <ui/FrameStats.h>
|
2012-11-09 03:23:28 +00:00
|
|
|
|
|
|
|
#include <utils/String8.h>
|
|
|
|
|
|
|
|
#include "FrameTracker.h"
|
2013-07-17 03:12:42 +00:00
|
|
|
#include "EventLog/EventLog.h"
|
2012-11-09 03:23:28 +00:00
|
|
|
|
|
|
|
namespace android {
|
|
|
|
|
|
|
|
FrameTracker::FrameTracker() :
|
|
|
|
mOffset(0),
|
2013-07-17 03:12:42 +00:00
|
|
|
mNumFences(0),
|
|
|
|
mDisplayPeriod(0) {
|
|
|
|
resetFrameCountersLocked();
|
2012-11-09 03:23:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FrameTracker::setDesiredPresentTime(nsecs_t presentTime) {
|
2013-02-05 21:30:24 +00:00
|
|
|
Mutex::Autolock lock(mMutex);
|
2012-11-09 03:23:28 +00:00
|
|
|
mFrameRecords[mOffset].desiredPresentTime = presentTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FrameTracker::setFrameReadyTime(nsecs_t readyTime) {
|
2013-02-05 21:30:24 +00:00
|
|
|
Mutex::Autolock lock(mMutex);
|
2012-11-09 03:23:28 +00:00
|
|
|
mFrameRecords[mOffset].frameReadyTime = readyTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FrameTracker::setFrameReadyFence(const sp<Fence>& readyFence) {
|
2013-02-05 21:30:24 +00:00
|
|
|
Mutex::Autolock lock(mMutex);
|
2012-11-09 03:23:28 +00:00
|
|
|
mFrameRecords[mOffset].frameReadyFence = readyFence;
|
|
|
|
mNumFences++;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FrameTracker::setActualPresentTime(nsecs_t presentTime) {
|
2013-02-05 21:30:24 +00:00
|
|
|
Mutex::Autolock lock(mMutex);
|
2012-11-09 03:23:28 +00:00
|
|
|
mFrameRecords[mOffset].actualPresentTime = presentTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FrameTracker::setActualPresentFence(const sp<Fence>& readyFence) {
|
2013-02-05 21:30:24 +00:00
|
|
|
Mutex::Autolock lock(mMutex);
|
2012-11-09 03:23:28 +00:00
|
|
|
mFrameRecords[mOffset].actualPresentFence = readyFence;
|
|
|
|
mNumFences++;
|
|
|
|
}
|
|
|
|
|
2013-07-17 03:12:42 +00:00
|
|
|
void FrameTracker::setDisplayRefreshPeriod(nsecs_t displayPeriod) {
|
|
|
|
Mutex::Autolock lock(mMutex);
|
|
|
|
mDisplayPeriod = displayPeriod;
|
|
|
|
}
|
|
|
|
|
2012-11-09 03:23:28 +00:00
|
|
|
void FrameTracker::advanceFrame() {
|
2013-02-05 21:30:24 +00:00
|
|
|
Mutex::Autolock lock(mMutex);
|
2013-07-17 03:12:42 +00:00
|
|
|
|
|
|
|
// Update the statistic to include the frame we just finished.
|
|
|
|
updateStatsLocked(mOffset);
|
|
|
|
|
|
|
|
// Advance to the next frame.
|
2012-11-09 03:23:28 +00:00
|
|
|
mOffset = (mOffset+1) % NUM_FRAME_RECORDS;
|
|
|
|
mFrameRecords[mOffset].desiredPresentTime = INT64_MAX;
|
|
|
|
mFrameRecords[mOffset].frameReadyTime = INT64_MAX;
|
|
|
|
mFrameRecords[mOffset].actualPresentTime = INT64_MAX;
|
|
|
|
|
|
|
|
if (mFrameRecords[mOffset].frameReadyFence != NULL) {
|
|
|
|
// We're clobbering an unsignaled fence, so we need to decrement the
|
|
|
|
// fence count.
|
|
|
|
mFrameRecords[mOffset].frameReadyFence = NULL;
|
|
|
|
mNumFences--;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mFrameRecords[mOffset].actualPresentFence != NULL) {
|
|
|
|
// We're clobbering an unsignaled fence, so we need to decrement the
|
|
|
|
// fence count.
|
|
|
|
mFrameRecords[mOffset].actualPresentFence = NULL;
|
|
|
|
mNumFences--;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clean up the signaled fences to keep the number of open fence FDs in
|
|
|
|
// this process reasonable.
|
2013-02-05 21:30:24 +00:00
|
|
|
processFencesLocked();
|
2012-11-09 03:23:28 +00:00
|
|
|
}
|
|
|
|
|
2014-03-20 17:28:31 +00:00
|
|
|
void FrameTracker::clearStats() {
|
2013-02-05 21:30:24 +00:00
|
|
|
Mutex::Autolock lock(mMutex);
|
2012-11-09 03:23:28 +00:00
|
|
|
for (size_t i = 0; i < NUM_FRAME_RECORDS; i++) {
|
|
|
|
mFrameRecords[i].desiredPresentTime = 0;
|
|
|
|
mFrameRecords[i].frameReadyTime = 0;
|
|
|
|
mFrameRecords[i].actualPresentTime = 0;
|
|
|
|
mFrameRecords[i].frameReadyFence.clear();
|
|
|
|
mFrameRecords[i].actualPresentFence.clear();
|
|
|
|
}
|
|
|
|
mNumFences = 0;
|
2013-02-05 21:30:24 +00:00
|
|
|
mFrameRecords[mOffset].desiredPresentTime = INT64_MAX;
|
|
|
|
mFrameRecords[mOffset].frameReadyTime = INT64_MAX;
|
|
|
|
mFrameRecords[mOffset].actualPresentTime = INT64_MAX;
|
2012-11-09 03:23:28 +00:00
|
|
|
}
|
|
|
|
|
2014-03-20 17:28:31 +00:00
|
|
|
void FrameTracker::getStats(FrameStats* outStats) const {
|
|
|
|
Mutex::Autolock lock(mMutex);
|
|
|
|
processFencesLocked();
|
|
|
|
|
|
|
|
outStats->refreshPeriodNano = mDisplayPeriod;
|
|
|
|
|
|
|
|
const size_t offset = mOffset;
|
|
|
|
for (size_t i = 1; i < NUM_FRAME_RECORDS; i++) {
|
|
|
|
const size_t index = (offset + i) % NUM_FRAME_RECORDS;
|
|
|
|
|
|
|
|
// Skip frame records with no data (if buffer not yet full).
|
|
|
|
if (mFrameRecords[index].desiredPresentTime == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsecs_t desiredPresentTimeNano = mFrameRecords[index].desiredPresentTime;
|
|
|
|
outStats->desiredPresentTimesNano.push_back(desiredPresentTimeNano);
|
|
|
|
|
|
|
|
nsecs_t actualPresentTimeNano = mFrameRecords[index].actualPresentTime;
|
|
|
|
outStats->actualPresentTimesNano.push_back(actualPresentTimeNano);
|
|
|
|
|
|
|
|
nsecs_t frameReadyTimeNano = mFrameRecords[index].frameReadyTime;
|
|
|
|
outStats->frameReadyTimesNano.push_back(frameReadyTimeNano);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-17 03:12:42 +00:00
|
|
|
void FrameTracker::logAndResetStats(const String8& name) {
|
|
|
|
Mutex::Autolock lock(mMutex);
|
|
|
|
logStatsLocked(name);
|
|
|
|
resetFrameCountersLocked();
|
|
|
|
}
|
|
|
|
|
2013-02-05 21:30:24 +00:00
|
|
|
void FrameTracker::processFencesLocked() const {
|
2012-11-09 03:23:28 +00:00
|
|
|
FrameRecord* records = const_cast<FrameRecord*>(mFrameRecords);
|
|
|
|
int& numFences = const_cast<int&>(mNumFences);
|
|
|
|
|
|
|
|
for (int i = 1; i < NUM_FRAME_RECORDS && numFences > 0; i++) {
|
|
|
|
size_t idx = (mOffset+NUM_FRAME_RECORDS-i) % NUM_FRAME_RECORDS;
|
2013-07-17 03:12:42 +00:00
|
|
|
bool updated = false;
|
2012-11-09 03:23:28 +00:00
|
|
|
|
|
|
|
const sp<Fence>& rfence = records[idx].frameReadyFence;
|
|
|
|
if (rfence != NULL) {
|
|
|
|
records[idx].frameReadyTime = rfence->getSignalTime();
|
|
|
|
if (records[idx].frameReadyTime < INT64_MAX) {
|
|
|
|
records[idx].frameReadyFence = NULL;
|
|
|
|
numFences--;
|
2013-07-17 03:12:42 +00:00
|
|
|
updated = true;
|
2012-11-09 03:23:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const sp<Fence>& pfence = records[idx].actualPresentFence;
|
|
|
|
if (pfence != NULL) {
|
|
|
|
records[idx].actualPresentTime = pfence->getSignalTime();
|
|
|
|
if (records[idx].actualPresentTime < INT64_MAX) {
|
|
|
|
records[idx].actualPresentFence = NULL;
|
|
|
|
numFences--;
|
2013-07-17 03:12:42 +00:00
|
|
|
updated = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (updated) {
|
|
|
|
updateStatsLocked(idx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FrameTracker::updateStatsLocked(size_t newFrameIdx) const {
|
|
|
|
int* numFrames = const_cast<int*>(mNumFrames);
|
|
|
|
|
|
|
|
if (mDisplayPeriod > 0 && isFrameValidLocked(newFrameIdx)) {
|
|
|
|
size_t prevFrameIdx = (newFrameIdx+NUM_FRAME_RECORDS-1) %
|
|
|
|
NUM_FRAME_RECORDS;
|
|
|
|
|
|
|
|
if (isFrameValidLocked(prevFrameIdx)) {
|
|
|
|
nsecs_t newPresentTime =
|
|
|
|
mFrameRecords[newFrameIdx].actualPresentTime;
|
|
|
|
nsecs_t prevPresentTime =
|
|
|
|
mFrameRecords[prevFrameIdx].actualPresentTime;
|
|
|
|
|
|
|
|
nsecs_t duration = newPresentTime - prevPresentTime;
|
|
|
|
int numPeriods = int((duration + mDisplayPeriod/2) /
|
|
|
|
mDisplayPeriod);
|
|
|
|
|
|
|
|
for (int i = 0; i < NUM_FRAME_BUCKETS-1; i++) {
|
|
|
|
int nextBucket = 1 << (i+1);
|
|
|
|
if (numPeriods < nextBucket) {
|
|
|
|
numFrames[i]++;
|
|
|
|
return;
|
|
|
|
}
|
2012-11-09 03:23:28 +00:00
|
|
|
}
|
2013-07-17 03:12:42 +00:00
|
|
|
|
|
|
|
// The last duration bucket is a catch-all.
|
|
|
|
numFrames[NUM_FRAME_BUCKETS-1]++;
|
2012-11-09 03:23:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-17 03:12:42 +00:00
|
|
|
void FrameTracker::resetFrameCountersLocked() {
|
|
|
|
for (int i = 0; i < NUM_FRAME_BUCKETS; i++) {
|
|
|
|
mNumFrames[i] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FrameTracker::logStatsLocked(const String8& name) const {
|
|
|
|
for (int i = 0; i < NUM_FRAME_BUCKETS; i++) {
|
|
|
|
if (mNumFrames[i] > 0) {
|
|
|
|
EventLog::logFrameDurations(name, mNumFrames, NUM_FRAME_BUCKETS);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FrameTracker::isFrameValidLocked(size_t idx) const {
|
|
|
|
return mFrameRecords[idx].actualPresentTime > 0 &&
|
|
|
|
mFrameRecords[idx].actualPresentTime < INT64_MAX;
|
|
|
|
}
|
|
|
|
|
2014-03-20 17:28:31 +00:00
|
|
|
void FrameTracker::dumpStats(String8& result) const {
|
2013-02-05 21:30:24 +00:00
|
|
|
Mutex::Autolock lock(mMutex);
|
|
|
|
processFencesLocked();
|
2012-11-09 03:23:28 +00:00
|
|
|
|
|
|
|
const size_t o = mOffset;
|
|
|
|
for (size_t i = 1; i < NUM_FRAME_RECORDS; i++) {
|
|
|
|
const size_t index = (o+i) % NUM_FRAME_RECORDS;
|
2014-03-07 20:44:02 +00:00
|
|
|
result.appendFormat("%" PRId64 "\t%" PRId64 "\t%" PRId64 "\n",
|
2012-11-09 03:23:28 +00:00
|
|
|
mFrameRecords[index].desiredPresentTime,
|
|
|
|
mFrameRecords[index].actualPresentTime,
|
|
|
|
mFrameRecords[index].frameReadyTime);
|
|
|
|
}
|
|
|
|
result.append("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace android
|