blob: cf1ca659726054cd8581a632349e9896b0180ba8 [file] [log] [blame]
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001/*
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 */
Yiwei Zhang91a86382018-11-15 14:44:40 -080016#include "timestatsproto/TimeStatsHelper.h"
17
Yiwei Zhang0102ad22018-05-02 17:37:17 -070018#include <android-base/stringprintf.h>
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -070019#include <ftl/enum.h>
Yiwei Zhang0102ad22018-05-02 17:37:17 -070020
21#include <array>
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -070022#include <cinttypes>
Yiwei Zhang0102ad22018-05-02 17:37:17 -070023
24#define HISTOGRAM_SIZE 85
25
26using android::base::StringAppendF;
27using android::base::StringPrintf;
28
29namespace android {
30namespace surfaceflinger {
31
32// Time buckets for histogram, the calculated time deltas will be lower bounded
33// to the buckets in this array.
34static const std::array<int32_t, HISTOGRAM_SIZE> histogramConfig =
35 {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
36 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33,
37 34, 36, 38, 40, 42, 44, 46, 48, 50, 54, 58, 62, 66, 70, 74, 78, 82,
38 86, 90, 94, 98, 102, 106, 110, 114, 118, 122, 126, 130, 134, 138, 142, 146, 150,
39 200, 250, 300, 350, 400, 450, 500, 550, 600, 650, 700, 750, 800, 850, 900, 950, 1000};
40
41void TimeStatsHelper::Histogram::insert(int32_t delta) {
42 if (delta < 0) return;
43 // std::lower_bound won't work on out of range values
44 if (delta > histogramConfig[HISTOGRAM_SIZE - 1]) {
Alec Mouri363faf02021-01-29 16:34:55 -080045 hist[histogramConfig[HISTOGRAM_SIZE - 1]]++;
Yiwei Zhang0102ad22018-05-02 17:37:17 -070046 return;
47 }
48 auto iter = std::lower_bound(histogramConfig.begin(), histogramConfig.end(), delta);
49 hist[*iter]++;
50}
51
Yiwei Zhang91a86382018-11-15 14:44:40 -080052int64_t TimeStatsHelper::Histogram::totalTime() const {
53 int64_t ret = 0;
54 for (const auto& ele : hist) {
55 ret += ele.first * ele.second;
56 }
57 return ret;
58}
59
Yiwei Zhang8a4015c2018-05-08 16:03:47 -070060float TimeStatsHelper::Histogram::averageTime() const {
Yiwei Zhang0102ad22018-05-02 17:37:17 -070061 int64_t ret = 0;
62 int64_t count = 0;
Yiwei Zhang3a226d22018-10-16 09:23:03 -070063 for (const auto& ele : hist) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -070064 count += ele.second;
65 ret += ele.first * ele.second;
66 }
67 return static_cast<float>(ret) / count;
68}
69
Yiwei Zhang8a4015c2018-05-08 16:03:47 -070070std::string TimeStatsHelper::Histogram::toString() const {
Yiwei Zhang0102ad22018-05-02 17:37:17 -070071 std::string result;
72 for (int32_t i = 0; i < HISTOGRAM_SIZE; ++i) {
73 int32_t bucket = histogramConfig[i];
Yiwei Zhang8a4015c2018-05-08 16:03:47 -070074 int32_t count = (hist.count(bucket) == 0) ? 0 : hist.at(bucket);
Yiwei Zhang0102ad22018-05-02 17:37:17 -070075 StringAppendF(&result, "%dms=%d ", bucket, count);
76 }
77 result.back() = '\n';
78 return result;
79}
80
Alec Mouri9a29e672020-09-14 12:39:14 -070081std::string TimeStatsHelper::JankPayload::toString() const {
82 std::string result;
83 StringAppendF(&result, "totalTimelineFrames = %d\n", totalFrames);
84 StringAppendF(&result, "jankyFrames = %d\n", totalJankyFrames);
85 StringAppendF(&result, "sfLongCpuJankyFrames = %d\n", totalSFLongCpu);
86 StringAppendF(&result, "sfLongGpuJankyFrames = %d\n", totalSFLongGpu);
Adithya Srinivasanead17162021-02-18 02:17:37 +000087 StringAppendF(&result, "sfUnattributedJankyFrames = %d\n", totalSFUnattributed);
88 StringAppendF(&result, "appUnattributedJankyFrames = %d\n", totalAppUnattributed);
89 StringAppendF(&result, "sfSchedulingJankyFrames = %d\n", totalSFScheduling);
90 StringAppendF(&result, "sfPredictionErrorJankyFrames = %d\n", totalSFPredictionError);
91 StringAppendF(&result, "appBufferStuffingJankyFrames = %d\n", totalAppBufferStuffing);
Alec Mouri9a29e672020-09-14 12:39:14 -070092 return result;
93}
94
Ady Abraham8b9e6122021-01-26 19:11:45 -080095std::string TimeStatsHelper::SetFrameRateVote::toString() const {
96 std::string result;
97 StringAppendF(&result, "frameRate = %.2f\n", frameRate);
98 StringAppendF(&result, "frameRateCompatibility = %s\n",
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -070099 ftl::enum_string(frameRateCompatibility).c_str());
100 StringAppendF(&result, "seamlessness = %s\n", ftl::enum_string(seamlessness).c_str());
Ady Abraham8b9e6122021-01-26 19:11:45 -0800101 return result;
102}
103
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700104std::string TimeStatsHelper::TimeStatsLayer::toString() const {
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700105 std::string result = "\n";
Alec Mouri7d436ec2021-01-27 20:40:50 -0800106 StringAppendF(&result, "displayRefreshRate = %d fps\n", displayRefreshRateBucket);
107 StringAppendF(&result, "renderRate = %d fps\n", renderRateBucket);
Alec Mouri9a29e672020-09-14 12:39:14 -0700108 StringAppendF(&result, "uid = %d\n", uid);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700109 StringAppendF(&result, "layerName = %s\n", layerName.c_str());
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700110 StringAppendF(&result, "packageName = %s\n", packageName.c_str());
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700111 StringAppendF(&result, "gameMode = %s\n", ftl::enum_string(gameMode).c_str());
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700112 StringAppendF(&result, "totalFrames = %d\n", totalFrames);
113 StringAppendF(&result, "droppedFrames = %d\n", droppedFrames);
Alec Mouri91f6df32020-01-30 08:48:58 -0800114 StringAppendF(&result, "lateAcquireFrames = %d\n", lateAcquireFrames);
115 StringAppendF(&result, "badDesiredPresentFrames = %d\n", badDesiredPresentFrames);
Alec Mouri9a29e672020-09-14 12:39:14 -0700116 result.append("Jank payload for this layer:\n");
117 result.append(jankPayload.toString());
Ady Abraham8b9e6122021-01-26 19:11:45 -0800118 result.append("SetFrateRate vote for this layer:\n");
119 result.append(setFrameRateVote.toString());
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700120 const auto iter = deltas.find("present2present");
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700121 if (iter != deltas.end()) {
Yiwei Zhangdd221b22020-06-12 11:06:19 -0700122 const float averageTime = iter->second.averageTime();
123 const float averageFPS = averageTime < 1.0f ? 0.0f : 1000.0f / averageTime;
124 StringAppendF(&result, "averageFPS = %.3f\n", averageFPS);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700125 }
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700126 for (const auto& ele : deltas) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700127 StringAppendF(&result, "%s histogram is as below:\n", ele.first.c_str());
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700128 result.append(ele.second.toString());
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700129 }
130
131 return result;
132}
133
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700134std::string TimeStatsHelper::TimeStatsGlobal::toString(std::optional<uint32_t> maxLayers) const {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700135 std::string result = "SurfaceFlinger TimeStats:\n";
Alec Mouri7d436ec2021-01-27 20:40:50 -0800136 result.append("Legacy stats are as follows:\n");
137 StringAppendF(&result, "statsStart = %" PRId64 "\n", statsStartLegacy);
138 StringAppendF(&result, "statsEnd = %" PRId64 "\n", statsEndLegacy);
139 StringAppendF(&result, "totalFrames = %d\n", totalFramesLegacy);
140 StringAppendF(&result, "missedFrames = %d\n", missedFramesLegacy);
141 StringAppendF(&result, "clientCompositionFrames = %d\n", clientCompositionFramesLegacy);
142 StringAppendF(&result, "clientCompositionReusedFrames = %d\n",
143 clientCompositionReusedFramesLegacy);
144 StringAppendF(&result, "refreshRateSwitches = %d\n", refreshRateSwitchesLegacy);
145 StringAppendF(&result, "compositionStrategyChanges = %d\n", compositionStrategyChangesLegacy);
Vishnu Nair9cf89262022-02-26 09:17:49 -0800146 StringAppendF(&result, "compositionStrategyPredicted = %d\n",
147 compositionStrategyPredictedLegacy);
148 StringAppendF(&result, "compositionStrategyPredictionSucceeded = %d\n",
149 compositionStrategyPredictionSucceededLegacy);
150 StringAppendF(&result, "compositionStrategyPredictionFailed = %d\n",
151 compositionStrategyPredictedLegacy -
152 compositionStrategyPredictionSucceededLegacy);
153
Alec Mouri7d436ec2021-01-27 20:40:50 -0800154 StringAppendF(&result, "displayOnTime = %" PRId64 " ms\n", displayOnTimeLegacy);
Alec Mourifb571ea2019-01-24 18:42:10 -0800155 StringAppendF(&result, "displayConfigStats is as below:\n");
Alec Mouri7d436ec2021-01-27 20:40:50 -0800156 for (const auto& [fps, duration] : refreshRateStatsLegacy) {
tangrobin8ef39762020-09-21 17:56:44 +0800157 StringAppendF(&result, "%dfps = %ldms\n", fps, ns2ms(duration));
Alec Mourifb571ea2019-01-24 18:42:10 -0800158 }
159 result.back() = '\n';
Alec Mouri7d436ec2021-01-27 20:40:50 -0800160 StringAppendF(&result, "totalP2PTime = %" PRId64 " ms\n", presentToPresentLegacy.totalTime());
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700161 StringAppendF(&result, "presentToPresent histogram is as below:\n");
Alec Mouri7d436ec2021-01-27 20:40:50 -0800162 result.append(presentToPresentLegacy.toString());
163 const float averageFrameDuration = frameDurationLegacy.averageTime();
Vishnu Nairabf97fd2020-02-03 13:51:16 -0800164 StringAppendF(&result, "averageFrameDuration = %.3f ms\n",
165 std::isnan(averageFrameDuration) ? 0.0f : averageFrameDuration);
Alec Mouri9519bf12019-11-15 16:54:44 -0800166 StringAppendF(&result, "frameDuration histogram is as below:\n");
Alec Mouri7d436ec2021-01-27 20:40:50 -0800167 result.append(frameDurationLegacy.toString());
168 const float averageRenderEngineTiming = renderEngineTimingLegacy.averageTime();
Vishnu Nairabf97fd2020-02-03 13:51:16 -0800169 StringAppendF(&result, "averageRenderEngineTiming = %.3f ms\n",
170 std::isnan(averageRenderEngineTiming) ? 0.0f : averageRenderEngineTiming);
Alec Mourie4034bb2019-11-19 12:45:54 -0800171 StringAppendF(&result, "renderEngineTiming histogram is as below:\n");
Alec Mouri7d436ec2021-01-27 20:40:50 -0800172 result.append(renderEngineTimingLegacy.toString());
173
174 result.append("\nGlobal aggregated jank payload (Timeline stats):");
175 for (const auto& ele : stats) {
176 result.append("\n");
177 StringAppendF(&result, "displayRefreshRate = %d fps\n",
178 ele.second.key.displayRefreshRateBucket);
179 StringAppendF(&result, "renderRate = %d fps\n", ele.second.key.renderRateBucket);
180 result.append(ele.second.jankPayload.toString());
Alec Mouri363faf02021-01-29 16:34:55 -0800181 StringAppendF(&result, "sfDeadlineMisses histogram is as below:\n");
182 result.append(ele.second.displayDeadlineDeltas.toString());
183 StringAppendF(&result, "sfPredictionErrors histogram is as below:\n");
184 result.append(ele.second.displayPresentDeltas.toString());
Alec Mouri7d436ec2021-01-27 20:40:50 -0800185 }
186
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700187 const auto dumpStats = generateDumpStats(maxLayers);
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700188 for (const auto& ele : dumpStats) {
189 result.append(ele->toString());
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700190 }
191
192 return result;
193}
194
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700195SFTimeStatsLayerProto TimeStatsHelper::TimeStatsLayer::toProto() const {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700196 SFTimeStatsLayerProto layerProto;
197 layerProto.set_layer_name(layerName);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700198 layerProto.set_package_name(packageName);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700199 layerProto.set_total_frames(totalFrames);
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700200 layerProto.set_dropped_frames(droppedFrames);
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700201 for (const auto& ele : deltas) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700202 SFTimeStatsDeltaProto* deltaProto = layerProto.add_deltas();
203 deltaProto->set_delta_name(ele.first);
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700204 for (const auto& histEle : ele.second.hist) {
Yiwei Zhang3bef3952018-05-04 14:08:01 -0700205 SFTimeStatsHistogramBucketProto* histProto = deltaProto->add_histograms();
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700206 histProto->set_time_millis(histEle.first);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700207 histProto->set_frame_count(histEle.second);
208 }
209 }
210 return layerProto;
211}
212
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700213SFTimeStatsGlobalProto TimeStatsHelper::TimeStatsGlobal::toProto(
214 std::optional<uint32_t> maxLayers) const {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700215 SFTimeStatsGlobalProto globalProto;
Alec Mouri7d436ec2021-01-27 20:40:50 -0800216 globalProto.set_stats_start(statsStartLegacy);
217 globalProto.set_stats_end(statsEndLegacy);
218 globalProto.set_total_frames(totalFramesLegacy);
219 globalProto.set_missed_frames(missedFramesLegacy);
220 globalProto.set_client_composition_frames(clientCompositionFramesLegacy);
221 globalProto.set_display_on_time(displayOnTimeLegacy);
222 for (const auto& ele : refreshRateStatsLegacy) {
Alec Mourifb571ea2019-01-24 18:42:10 -0800223 SFTimeStatsDisplayConfigBucketProto* configBucketProto =
224 globalProto.add_display_config_stats();
225 SFTimeStatsDisplayConfigProto* configProto = configBucketProto->mutable_config();
226 configProto->set_fps(ele.first);
227 configBucketProto->set_duration_millis(ns2ms(ele.second));
228 }
Alec Mouri7d436ec2021-01-27 20:40:50 -0800229 for (const auto& histEle : presentToPresentLegacy.hist) {
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700230 SFTimeStatsHistogramBucketProto* histProto = globalProto.add_present_to_present();
231 histProto->set_time_millis(histEle.first);
232 histProto->set_frame_count(histEle.second);
233 }
Alec Mouri7d436ec2021-01-27 20:40:50 -0800234 for (const auto& histEle : frameDurationLegacy.hist) {
Alec Mouri9519bf12019-11-15 16:54:44 -0800235 SFTimeStatsHistogramBucketProto* histProto = globalProto.add_frame_duration();
236 histProto->set_time_millis(histEle.first);
237 histProto->set_frame_count(histEle.second);
238 }
Alec Mouri7d436ec2021-01-27 20:40:50 -0800239 for (const auto& histEle : renderEngineTimingLegacy.hist) {
Alec Mourie4034bb2019-11-19 12:45:54 -0800240 SFTimeStatsHistogramBucketProto* histProto = globalProto.add_render_engine_timing();
241 histProto->set_time_millis(histEle.first);
242 histProto->set_frame_count(histEle.second);
243 }
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700244 const auto dumpStats = generateDumpStats(maxLayers);
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700245 for (const auto& ele : dumpStats) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700246 SFTimeStatsLayerProto* layerProto = globalProto.add_stats();
247 layerProto->CopyFrom(ele->toProto());
248 }
249 return globalProto;
250}
251
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700252std::vector<TimeStatsHelper::TimeStatsLayer const*>
253TimeStatsHelper::TimeStatsGlobal::generateDumpStats(std::optional<uint32_t> maxLayers) const {
254 std::vector<TimeStatsLayer const*> dumpStats;
Alec Mouri7d436ec2021-01-27 20:40:50 -0800255
256 int numLayers = 0;
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700257 for (const auto& ele : stats) {
Alec Mouri7d436ec2021-01-27 20:40:50 -0800258 numLayers += ele.second.stats.size();
259 }
260
261 dumpStats.reserve(numLayers);
262
263 for (const auto& ele : stats) {
264 for (const auto& layerEle : ele.second.stats) {
265 dumpStats.push_back(&layerEle.second);
266 }
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700267 }
268
269 std::sort(dumpStats.begin(), dumpStats.end(),
270 [](TimeStatsHelper::TimeStatsLayer const* l,
271 TimeStatsHelper::TimeStatsLayer const* r) {
272 return l->totalFrames > r->totalFrames;
273 });
274
275 if (maxLayers && (*maxLayers < dumpStats.size())) {
276 dumpStats.resize(*maxLayers);
277 }
278 return dumpStats;
279}
280
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700281} // namespace surfaceflinger
282} // namespace android