FrameTracker.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. // This is needed for stdint.h to define INT64_MAX in C++
  17. #define __STDC_LIMIT_MACROS
  18. #include <inttypes.h>
  19. #include <cutils/log.h>
  20. #include <ui/Fence.h>
  21. #include <ui/FrameStats.h>
  22. #include <utils/String8.h>
  23. #include "FrameTracker.h"
  24. #include "EventLog/EventLog.h"
  25. namespace android {
  26. FrameTracker::FrameTracker() :
  27. mOffset(0),
  28. mNumFences(0),
  29. mDisplayPeriod(0) {
  30. resetFrameCountersLocked();
  31. }
  32. void FrameTracker::setDesiredPresentTime(nsecs_t presentTime) {
  33. Mutex::Autolock lock(mMutex);
  34. mFrameRecords[mOffset].desiredPresentTime = presentTime;
  35. }
  36. void FrameTracker::setFrameReadyTime(nsecs_t readyTime) {
  37. Mutex::Autolock lock(mMutex);
  38. mFrameRecords[mOffset].frameReadyTime = readyTime;
  39. }
  40. void FrameTracker::setFrameReadyFence(const sp<Fence>& readyFence) {
  41. Mutex::Autolock lock(mMutex);
  42. mFrameRecords[mOffset].frameReadyFence = readyFence;
  43. mNumFences++;
  44. }
  45. void FrameTracker::setActualPresentTime(nsecs_t presentTime) {
  46. Mutex::Autolock lock(mMutex);
  47. mFrameRecords[mOffset].actualPresentTime = presentTime;
  48. }
  49. void FrameTracker::setActualPresentFence(const sp<Fence>& readyFence) {
  50. Mutex::Autolock lock(mMutex);
  51. mFrameRecords[mOffset].actualPresentFence = readyFence;
  52. mNumFences++;
  53. }
  54. void FrameTracker::setDisplayRefreshPeriod(nsecs_t displayPeriod) {
  55. Mutex::Autolock lock(mMutex);
  56. mDisplayPeriod = displayPeriod;
  57. }
  58. void FrameTracker::advanceFrame() {
  59. Mutex::Autolock lock(mMutex);
  60. // Update the statistic to include the frame we just finished.
  61. updateStatsLocked(mOffset);
  62. // Advance to the next frame.
  63. mOffset = (mOffset+1) % NUM_FRAME_RECORDS;
  64. mFrameRecords[mOffset].desiredPresentTime = INT64_MAX;
  65. mFrameRecords[mOffset].frameReadyTime = INT64_MAX;
  66. mFrameRecords[mOffset].actualPresentTime = INT64_MAX;
  67. if (mFrameRecords[mOffset].frameReadyFence != NULL) {
  68. // We're clobbering an unsignaled fence, so we need to decrement the
  69. // fence count.
  70. mFrameRecords[mOffset].frameReadyFence = NULL;
  71. mNumFences--;
  72. }
  73. if (mFrameRecords[mOffset].actualPresentFence != NULL) {
  74. // We're clobbering an unsignaled fence, so we need to decrement the
  75. // fence count.
  76. mFrameRecords[mOffset].actualPresentFence = NULL;
  77. mNumFences--;
  78. }
  79. // Clean up the signaled fences to keep the number of open fence FDs in
  80. // this process reasonable.
  81. processFencesLocked();
  82. }
  83. void FrameTracker::clearStats() {
  84. Mutex::Autolock lock(mMutex);
  85. for (size_t i = 0; i < NUM_FRAME_RECORDS; i++) {
  86. mFrameRecords[i].desiredPresentTime = 0;
  87. mFrameRecords[i].frameReadyTime = 0;
  88. mFrameRecords[i].actualPresentTime = 0;
  89. mFrameRecords[i].frameReadyFence.clear();
  90. mFrameRecords[i].actualPresentFence.clear();
  91. }
  92. mNumFences = 0;
  93. mFrameRecords[mOffset].desiredPresentTime = INT64_MAX;
  94. mFrameRecords[mOffset].frameReadyTime = INT64_MAX;
  95. mFrameRecords[mOffset].actualPresentTime = INT64_MAX;
  96. }
  97. void FrameTracker::getStats(FrameStats* outStats) const {
  98. Mutex::Autolock lock(mMutex);
  99. processFencesLocked();
  100. outStats->refreshPeriodNano = mDisplayPeriod;
  101. const size_t offset = mOffset;
  102. for (size_t i = 1; i < NUM_FRAME_RECORDS; i++) {
  103. const size_t index = (offset + i) % NUM_FRAME_RECORDS;
  104. // Skip frame records with no data (if buffer not yet full).
  105. if (mFrameRecords[index].desiredPresentTime == 0) {
  106. continue;
  107. }
  108. nsecs_t desiredPresentTimeNano = mFrameRecords[index].desiredPresentTime;
  109. outStats->desiredPresentTimesNano.push_back(desiredPresentTimeNano);
  110. nsecs_t actualPresentTimeNano = mFrameRecords[index].actualPresentTime;
  111. outStats->actualPresentTimesNano.push_back(actualPresentTimeNano);
  112. nsecs_t frameReadyTimeNano = mFrameRecords[index].frameReadyTime;
  113. outStats->frameReadyTimesNano.push_back(frameReadyTimeNano);
  114. }
  115. }
  116. void FrameTracker::logAndResetStats(const String8& name) {
  117. Mutex::Autolock lock(mMutex);
  118. logStatsLocked(name);
  119. resetFrameCountersLocked();
  120. }
  121. void FrameTracker::processFencesLocked() const {
  122. FrameRecord* records = const_cast<FrameRecord*>(mFrameRecords);
  123. int& numFences = const_cast<int&>(mNumFences);
  124. for (int i = 1; i < NUM_FRAME_RECORDS && numFences > 0; i++) {
  125. size_t idx = (mOffset+NUM_FRAME_RECORDS-i) % NUM_FRAME_RECORDS;
  126. bool updated = false;
  127. const sp<Fence>& rfence = records[idx].frameReadyFence;
  128. if (rfence != NULL) {
  129. records[idx].frameReadyTime = rfence->getSignalTime();
  130. if (records[idx].frameReadyTime < INT64_MAX) {
  131. records[idx].frameReadyFence = NULL;
  132. numFences--;
  133. updated = true;
  134. }
  135. }
  136. const sp<Fence>& pfence = records[idx].actualPresentFence;
  137. if (pfence != NULL) {
  138. records[idx].actualPresentTime = pfence->getSignalTime();
  139. if (records[idx].actualPresentTime < INT64_MAX) {
  140. records[idx].actualPresentFence = NULL;
  141. numFences--;
  142. updated = true;
  143. }
  144. }
  145. if (updated) {
  146. updateStatsLocked(idx);
  147. }
  148. }
  149. }
  150. void FrameTracker::updateStatsLocked(size_t newFrameIdx) const {
  151. int* numFrames = const_cast<int*>(mNumFrames);
  152. if (mDisplayPeriod > 0 && isFrameValidLocked(newFrameIdx)) {
  153. size_t prevFrameIdx = (newFrameIdx+NUM_FRAME_RECORDS-1) %
  154. NUM_FRAME_RECORDS;
  155. if (isFrameValidLocked(prevFrameIdx)) {
  156. nsecs_t newPresentTime =
  157. mFrameRecords[newFrameIdx].actualPresentTime;
  158. nsecs_t prevPresentTime =
  159. mFrameRecords[prevFrameIdx].actualPresentTime;
  160. nsecs_t duration = newPresentTime - prevPresentTime;
  161. int numPeriods = int((duration + mDisplayPeriod/2) /
  162. mDisplayPeriod);
  163. for (int i = 0; i < NUM_FRAME_BUCKETS-1; i++) {
  164. int nextBucket = 1 << (i+1);
  165. if (numPeriods < nextBucket) {
  166. numFrames[i]++;
  167. return;
  168. }
  169. }
  170. // The last duration bucket is a catch-all.
  171. numFrames[NUM_FRAME_BUCKETS-1]++;
  172. }
  173. }
  174. }
  175. void FrameTracker::resetFrameCountersLocked() {
  176. for (int i = 0; i < NUM_FRAME_BUCKETS; i++) {
  177. mNumFrames[i] = 0;
  178. }
  179. }
  180. void FrameTracker::logStatsLocked(const String8& name) const {
  181. for (int i = 0; i < NUM_FRAME_BUCKETS; i++) {
  182. if (mNumFrames[i] > 0) {
  183. EventLog::logFrameDurations(name, mNumFrames, NUM_FRAME_BUCKETS);
  184. return;
  185. }
  186. }
  187. }
  188. bool FrameTracker::isFrameValidLocked(size_t idx) const {
  189. return mFrameRecords[idx].actualPresentTime > 0 &&
  190. mFrameRecords[idx].actualPresentTime < INT64_MAX;
  191. }
  192. void FrameTracker::dumpStats(String8& result) const {
  193. Mutex::Autolock lock(mMutex);
  194. processFencesLocked();
  195. const size_t o = mOffset;
  196. for (size_t i = 1; i < NUM_FRAME_RECORDS; i++) {
  197. const size_t index = (o+i) % NUM_FRAME_RECORDS;
  198. result.appendFormat("%" PRId64 "\t%" PRId64 "\t%" PRId64 "\n",
  199. mFrameRecords[index].desiredPresentTime,
  200. mFrameRecords[index].actualPresentTime,
  201. mFrameRecords[index].frameReadyTime);
  202. }
  203. result.append("\n");
  204. }
  205. } // namespace android