TimeStatsHelper.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * Copyright (C) 2017 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. #include "timestatsproto/TimeStatsHelper.h"
  17. #include <android-base/stringprintf.h>
  18. #include <inttypes.h>
  19. #include <array>
  20. #define HISTOGRAM_SIZE 85
  21. using android::base::StringAppendF;
  22. using android::base::StringPrintf;
  23. namespace android {
  24. namespace surfaceflinger {
  25. // Time buckets for histogram, the calculated time deltas will be lower bounded
  26. // to the buckets in this array.
  27. static const std::array<int32_t, HISTOGRAM_SIZE> histogramConfig =
  28. {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
  29. 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33,
  30. 34, 36, 38, 40, 42, 44, 46, 48, 50, 54, 58, 62, 66, 70, 74, 78, 82,
  31. 86, 90, 94, 98, 102, 106, 110, 114, 118, 122, 126, 130, 134, 138, 142, 146, 150,
  32. 200, 250, 300, 350, 400, 450, 500, 550, 600, 650, 700, 750, 800, 850, 900, 950, 1000};
  33. void TimeStatsHelper::Histogram::insert(int32_t delta) {
  34. if (delta < 0) return;
  35. // std::lower_bound won't work on out of range values
  36. if (delta > histogramConfig[HISTOGRAM_SIZE - 1]) {
  37. hist[histogramConfig[HISTOGRAM_SIZE - 1]] += delta / histogramConfig[HISTOGRAM_SIZE - 1];
  38. return;
  39. }
  40. auto iter = std::lower_bound(histogramConfig.begin(), histogramConfig.end(), delta);
  41. hist[*iter]++;
  42. }
  43. int64_t TimeStatsHelper::Histogram::totalTime() const {
  44. int64_t ret = 0;
  45. for (const auto& ele : hist) {
  46. ret += ele.first * ele.second;
  47. }
  48. return ret;
  49. }
  50. float TimeStatsHelper::Histogram::averageTime() const {
  51. int64_t ret = 0;
  52. int64_t count = 0;
  53. for (const auto& ele : hist) {
  54. count += ele.second;
  55. ret += ele.first * ele.second;
  56. }
  57. return static_cast<float>(ret) / count;
  58. }
  59. std::string TimeStatsHelper::Histogram::toString() const {
  60. std::string result;
  61. for (int32_t i = 0; i < HISTOGRAM_SIZE; ++i) {
  62. int32_t bucket = histogramConfig[i];
  63. int32_t count = (hist.count(bucket) == 0) ? 0 : hist.at(bucket);
  64. StringAppendF(&result, "%dms=%d ", bucket, count);
  65. }
  66. result.back() = '\n';
  67. return result;
  68. }
  69. std::string TimeStatsHelper::TimeStatsLayer::toString() const {
  70. std::string result = "\n";
  71. StringAppendF(&result, "layerName = %s\n", layerName.c_str());
  72. StringAppendF(&result, "packageName = %s\n", packageName.c_str());
  73. StringAppendF(&result, "totalFrames = %d\n", totalFrames);
  74. StringAppendF(&result, "droppedFrames = %d\n", droppedFrames);
  75. const auto iter = deltas.find("present2present");
  76. if (iter != deltas.end()) {
  77. StringAppendF(&result, "averageFPS = %.3f\n", 1000.0 / iter->second.averageTime());
  78. }
  79. for (const auto& ele : deltas) {
  80. StringAppendF(&result, "%s histogram is as below:\n", ele.first.c_str());
  81. result.append(ele.second.toString());
  82. }
  83. return result;
  84. }
  85. std::string TimeStatsHelper::TimeStatsGlobal::toString(std::optional<uint32_t> maxLayers) const {
  86. std::string result = "SurfaceFlinger TimeStats:\n";
  87. StringAppendF(&result, "statsStart = %" PRId64 "\n", statsStart);
  88. StringAppendF(&result, "statsEnd = %" PRId64 "\n", statsEnd);
  89. StringAppendF(&result, "totalFrames = %d\n", totalFrames);
  90. StringAppendF(&result, "missedFrames = %d\n", missedFrames);
  91. StringAppendF(&result, "clientCompositionFrames = %d\n", clientCompositionFrames);
  92. StringAppendF(&result, "displayOnTime = %" PRId64 " ms\n", displayOnTime);
  93. StringAppendF(&result, "displayConfigStats is as below:\n");
  94. for (const auto& [fps, duration] : refreshRateStats) {
  95. StringAppendF(&result, "%dfps=%ldms ", fps, ns2ms(duration));
  96. }
  97. result.back() = '\n';
  98. StringAppendF(&result, "totalP2PTime = %" PRId64 " ms\n", presentToPresent.totalTime());
  99. StringAppendF(&result, "presentToPresent histogram is as below:\n");
  100. result.append(presentToPresent.toString());
  101. const auto dumpStats = generateDumpStats(maxLayers);
  102. for (const auto& ele : dumpStats) {
  103. result.append(ele->toString());
  104. }
  105. return result;
  106. }
  107. SFTimeStatsLayerProto TimeStatsHelper::TimeStatsLayer::toProto() const {
  108. SFTimeStatsLayerProto layerProto;
  109. layerProto.set_layer_name(layerName);
  110. layerProto.set_package_name(packageName);
  111. layerProto.set_total_frames(totalFrames);
  112. layerProto.set_dropped_frames(droppedFrames);
  113. for (const auto& ele : deltas) {
  114. SFTimeStatsDeltaProto* deltaProto = layerProto.add_deltas();
  115. deltaProto->set_delta_name(ele.first);
  116. for (const auto& histEle : ele.second.hist) {
  117. SFTimeStatsHistogramBucketProto* histProto = deltaProto->add_histograms();
  118. histProto->set_time_millis(histEle.first);
  119. histProto->set_frame_count(histEle.second);
  120. }
  121. }
  122. return layerProto;
  123. }
  124. SFTimeStatsGlobalProto TimeStatsHelper::TimeStatsGlobal::toProto(
  125. std::optional<uint32_t> maxLayers) const {
  126. SFTimeStatsGlobalProto globalProto;
  127. globalProto.set_stats_start(statsStart);
  128. globalProto.set_stats_end(statsEnd);
  129. globalProto.set_total_frames(totalFrames);
  130. globalProto.set_missed_frames(missedFrames);
  131. globalProto.set_client_composition_frames(clientCompositionFrames);
  132. globalProto.set_display_on_time(displayOnTime);
  133. for (const auto& ele : refreshRateStats) {
  134. SFTimeStatsDisplayConfigBucketProto* configBucketProto =
  135. globalProto.add_display_config_stats();
  136. SFTimeStatsDisplayConfigProto* configProto = configBucketProto->mutable_config();
  137. configProto->set_fps(ele.first);
  138. configBucketProto->set_duration_millis(ns2ms(ele.second));
  139. }
  140. for (const auto& histEle : presentToPresent.hist) {
  141. SFTimeStatsHistogramBucketProto* histProto = globalProto.add_present_to_present();
  142. histProto->set_time_millis(histEle.first);
  143. histProto->set_frame_count(histEle.second);
  144. }
  145. const auto dumpStats = generateDumpStats(maxLayers);
  146. for (const auto& ele : dumpStats) {
  147. SFTimeStatsLayerProto* layerProto = globalProto.add_stats();
  148. layerProto->CopyFrom(ele->toProto());
  149. }
  150. return globalProto;
  151. }
  152. std::vector<TimeStatsHelper::TimeStatsLayer const*>
  153. TimeStatsHelper::TimeStatsGlobal::generateDumpStats(std::optional<uint32_t> maxLayers) const {
  154. std::vector<TimeStatsLayer const*> dumpStats;
  155. for (const auto& ele : stats) {
  156. dumpStats.push_back(&ele.second);
  157. }
  158. std::sort(dumpStats.begin(), dumpStats.end(),
  159. [](TimeStatsHelper::TimeStatsLayer const* l,
  160. TimeStatsHelper::TimeStatsLayer const* r) {
  161. return l->totalFrames > r->totalFrames;
  162. });
  163. if (maxLayers && (*maxLayers < dumpStats.size())) {
  164. dumpStats.resize(*maxLayers);
  165. }
  166. return dumpStats;
  167. }
  168. } // namespace surfaceflinger
  169. } // namespace android