FrameTracker.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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 <android-base/stringprintf.h>
  20. #include <android/log.h>
  21. #include <utils/String8.h>
  22. #include <ui/FrameStats.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(
  41. std::shared_ptr<FenceTime>&& readyFence) {
  42. Mutex::Autolock lock(mMutex);
  43. mFrameRecords[mOffset].frameReadyFence = std::move(readyFence);
  44. mNumFences++;
  45. }
  46. void FrameTracker::setActualPresentTime(nsecs_t presentTime) {
  47. Mutex::Autolock lock(mMutex);
  48. mFrameRecords[mOffset].actualPresentTime = presentTime;
  49. }
  50. void FrameTracker::setActualPresentFence(
  51. std::shared_ptr<FenceTime>&& readyFence) {
  52. Mutex::Autolock lock(mMutex);
  53. mFrameRecords[mOffset].actualPresentFence = std::move(readyFence);
  54. mNumFences++;
  55. }
  56. void FrameTracker::setDisplayRefreshPeriod(nsecs_t displayPeriod) {
  57. Mutex::Autolock lock(mMutex);
  58. mDisplayPeriod = displayPeriod;
  59. }
  60. void FrameTracker::advanceFrame() {
  61. Mutex::Autolock lock(mMutex);
  62. // Update the statistic to include the frame we just finished.
  63. updateStatsLocked(mOffset);
  64. // Advance to the next frame.
  65. mOffset = (mOffset+1) % NUM_FRAME_RECORDS;
  66. mFrameRecords[mOffset].desiredPresentTime = INT64_MAX;
  67. mFrameRecords[mOffset].frameReadyTime = INT64_MAX;
  68. mFrameRecords[mOffset].actualPresentTime = INT64_MAX;
  69. if (mFrameRecords[mOffset].frameReadyFence != nullptr) {
  70. // We're clobbering an unsignaled fence, so we need to decrement the
  71. // fence count.
  72. mFrameRecords[mOffset].frameReadyFence = nullptr;
  73. mNumFences--;
  74. }
  75. if (mFrameRecords[mOffset].actualPresentFence != nullptr) {
  76. // We're clobbering an unsignaled fence, so we need to decrement the
  77. // fence count.
  78. mFrameRecords[mOffset].actualPresentFence = nullptr;
  79. mNumFences--;
  80. }
  81. }
  82. void FrameTracker::clearStats() {
  83. Mutex::Autolock lock(mMutex);
  84. for (size_t i = 0; i < NUM_FRAME_RECORDS; i++) {
  85. mFrameRecords[i].desiredPresentTime = 0;
  86. mFrameRecords[i].frameReadyTime = 0;
  87. mFrameRecords[i].actualPresentTime = 0;
  88. mFrameRecords[i].frameReadyFence.reset();
  89. mFrameRecords[i].actualPresentFence.reset();
  90. }
  91. mNumFences = 0;
  92. mFrameRecords[mOffset].desiredPresentTime = INT64_MAX;
  93. mFrameRecords[mOffset].frameReadyTime = INT64_MAX;
  94. mFrameRecords[mOffset].actualPresentTime = INT64_MAX;
  95. }
  96. void FrameTracker::getStats(FrameStats* outStats) const {
  97. Mutex::Autolock lock(mMutex);
  98. processFencesLocked();
  99. outStats->refreshPeriodNano = mDisplayPeriod;
  100. const size_t offset = mOffset;
  101. for (size_t i = 1; i < NUM_FRAME_RECORDS; i++) {
  102. const size_t index = (offset + i) % NUM_FRAME_RECORDS;
  103. // Skip frame records with no data (if buffer not yet full).
  104. if (mFrameRecords[index].desiredPresentTime == 0) {
  105. continue;
  106. }
  107. nsecs_t desiredPresentTimeNano = mFrameRecords[index].desiredPresentTime;
  108. outStats->desiredPresentTimesNano.push_back(desiredPresentTimeNano);
  109. nsecs_t actualPresentTimeNano = mFrameRecords[index].actualPresentTime;
  110. outStats->actualPresentTimesNano.push_back(actualPresentTimeNano);
  111. nsecs_t frameReadyTimeNano = mFrameRecords[index].frameReadyTime;
  112. outStats->frameReadyTimesNano.push_back(frameReadyTimeNano);
  113. }
  114. }
  115. void FrameTracker::logAndResetStats(const String8& name) {
  116. Mutex::Autolock lock(mMutex);
  117. logStatsLocked(name);
  118. resetFrameCountersLocked();
  119. }
  120. void FrameTracker::processFencesLocked() const {
  121. FrameRecord* records = const_cast<FrameRecord*>(mFrameRecords);
  122. int& numFences = const_cast<int&>(mNumFences);
  123. for (int i = 1; i < NUM_FRAME_RECORDS && numFences > 0; i++) {
  124. size_t idx = (mOffset+NUM_FRAME_RECORDS-i) % NUM_FRAME_RECORDS;
  125. bool updated = false;
  126. const std::shared_ptr<FenceTime>& rfence = records[idx].frameReadyFence;
  127. if (rfence != nullptr) {
  128. records[idx].frameReadyTime = rfence->getSignalTime();
  129. if (records[idx].frameReadyTime < INT64_MAX) {
  130. records[idx].frameReadyFence = nullptr;
  131. numFences--;
  132. updated = true;
  133. }
  134. }
  135. const std::shared_ptr<FenceTime>& pfence =
  136. records[idx].actualPresentFence;
  137. if (pfence != nullptr) {
  138. records[idx].actualPresentTime = pfence->getSignalTime();
  139. if (records[idx].actualPresentTime < INT64_MAX) {
  140. records[idx].actualPresentFence = nullptr;
  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(std::string& 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. base::StringAppendF(&result, "%" 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