OccupancyTracker.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Copyright 2016 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. #undef LOG_TAG
  17. #define LOG_TAG "OccupancyTracker"
  18. #include <gui/OccupancyTracker.h>
  19. #include <binder/Parcel.h>
  20. #include <utils/String8.h>
  21. #include <utils/Trace.h>
  22. #include <inttypes.h>
  23. namespace android {
  24. status_t OccupancyTracker::Segment::writeToParcel(Parcel* parcel) const {
  25. status_t result = parcel->writeInt64(totalTime);
  26. if (result != OK) {
  27. return result;
  28. }
  29. result = parcel->writeUint64(static_cast<uint64_t>(numFrames));
  30. if (result != OK) {
  31. return result;
  32. }
  33. result = parcel->writeFloat(occupancyAverage);
  34. if (result != OK) {
  35. return result;
  36. }
  37. return parcel->writeBool(usedThirdBuffer);
  38. }
  39. status_t OccupancyTracker::Segment::readFromParcel(const Parcel* parcel) {
  40. status_t result = parcel->readInt64(&totalTime);
  41. if (result != OK) {
  42. return result;
  43. }
  44. uint64_t uintNumFrames = 0;
  45. result = parcel->readUint64(&uintNumFrames);
  46. if (result != OK) {
  47. return result;
  48. }
  49. numFrames = static_cast<size_t>(uintNumFrames);
  50. result = parcel->readFloat(&occupancyAverage);
  51. if (result != OK) {
  52. return result;
  53. }
  54. return parcel->readBool(&usedThirdBuffer);
  55. }
  56. void OccupancyTracker::registerOccupancyChange(size_t occupancy) {
  57. ATRACE_CALL();
  58. nsecs_t now = systemTime();
  59. nsecs_t delta = now - mLastOccupancyChangeTime;
  60. if (delta > NEW_SEGMENT_DELAY) {
  61. recordPendingSegment();
  62. } else {
  63. mPendingSegment.totalTime += delta;
  64. if (mPendingSegment.mOccupancyTimes.count(mLastOccupancy)) {
  65. mPendingSegment.mOccupancyTimes[mLastOccupancy] += delta;
  66. } else {
  67. mPendingSegment.mOccupancyTimes[mLastOccupancy] = delta;
  68. }
  69. }
  70. if (occupancy > mLastOccupancy) {
  71. ++mPendingSegment.numFrames;
  72. }
  73. mLastOccupancyChangeTime = now;
  74. mLastOccupancy = occupancy;
  75. }
  76. std::vector<OccupancyTracker::Segment> OccupancyTracker::getSegmentHistory(
  77. bool forceFlush) {
  78. if (forceFlush) {
  79. recordPendingSegment();
  80. }
  81. std::vector<Segment> segments(mSegmentHistory.cbegin(),
  82. mSegmentHistory.cend());
  83. mSegmentHistory.clear();
  84. return segments;
  85. }
  86. void OccupancyTracker::recordPendingSegment() {
  87. // Only record longer segments to get a better measurement of actual double-
  88. // vs. triple-buffered time
  89. if (mPendingSegment.numFrames > LONG_SEGMENT_THRESHOLD) {
  90. float occupancyAverage = 0.0f;
  91. bool usedThirdBuffer = false;
  92. for (const auto& timePair : mPendingSegment.mOccupancyTimes) {
  93. size_t occupancy = timePair.first;
  94. float timeRatio = static_cast<float>(timePair.second) /
  95. mPendingSegment.totalTime;
  96. occupancyAverage += timeRatio * occupancy;
  97. usedThirdBuffer = usedThirdBuffer || (occupancy > 1);
  98. }
  99. mSegmentHistory.push_front({mPendingSegment.totalTime,
  100. mPendingSegment.numFrames, occupancyAverage, usedThirdBuffer});
  101. if (mSegmentHistory.size() > MAX_HISTORY_SIZE) {
  102. mSegmentHistory.pop_back();
  103. }
  104. }
  105. mPendingSegment.clear();
  106. }
  107. } // namespace android