blob: a7e7db25d7a20b976f71a70e92c2dbc7d9ecfe6f [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>
Yiwei Zhang91a86382018-11-15 14:44:40 -080019#include <inttypes.h>
Yiwei Zhang0102ad22018-05-02 17:37:17 -070020
21#include <array>
Yiwei Zhang0102ad22018-05-02 17:37:17 -070022
23#define HISTOGRAM_SIZE 85
24
25using android::base::StringAppendF;
26using android::base::StringPrintf;
27
28namespace android {
29namespace surfaceflinger {
30
31// Time buckets for histogram, the calculated time deltas will be lower bounded
32// to the buckets in this array.
33static const std::array<int32_t, HISTOGRAM_SIZE> histogramConfig =
34 {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
35 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33,
36 34, 36, 38, 40, 42, 44, 46, 48, 50, 54, 58, 62, 66, 70, 74, 78, 82,
37 86, 90, 94, 98, 102, 106, 110, 114, 118, 122, 126, 130, 134, 138, 142, 146, 150,
38 200, 250, 300, 350, 400, 450, 500, 550, 600, 650, 700, 750, 800, 850, 900, 950, 1000};
39
40void TimeStatsHelper::Histogram::insert(int32_t delta) {
41 if (delta < 0) return;
42 // std::lower_bound won't work on out of range values
43 if (delta > histogramConfig[HISTOGRAM_SIZE - 1]) {
Alec Mouri363faf02021-01-29 16:34:55 -080044 hist[histogramConfig[HISTOGRAM_SIZE - 1]]++;
Yiwei Zhang0102ad22018-05-02 17:37:17 -070045 return;
46 }
47 auto iter = std::lower_bound(histogramConfig.begin(), histogramConfig.end(), delta);
48 hist[*iter]++;
49}
50
Yiwei Zhang91a86382018-11-15 14:44:40 -080051int64_t TimeStatsHelper::Histogram::totalTime() const {
52 int64_t ret = 0;
53 for (const auto& ele : hist) {
54 ret += ele.first * ele.second;
55 }
56 return ret;
57}
58
Yiwei Zhang8a4015c2018-05-08 16:03:47 -070059float TimeStatsHelper::Histogram::averageTime() const {
Yiwei Zhang0102ad22018-05-02 17:37:17 -070060 int64_t ret = 0;
61 int64_t count = 0;
Yiwei Zhang3a226d22018-10-16 09:23:03 -070062 for (const auto& ele : hist) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -070063 count += ele.second;
64 ret += ele.first * ele.second;
65 }
66 return static_cast<float>(ret) / count;
67}
68
Yiwei Zhang8a4015c2018-05-08 16:03:47 -070069std::string TimeStatsHelper::Histogram::toString() const {
Yiwei Zhang0102ad22018-05-02 17:37:17 -070070 std::string result;
71 for (int32_t i = 0; i < HISTOGRAM_SIZE; ++i) {
72 int32_t bucket = histogramConfig[i];
Yiwei Zhang8a4015c2018-05-08 16:03:47 -070073 int32_t count = (hist.count(bucket) == 0) ? 0 : hist.at(bucket);
Yiwei Zhang0102ad22018-05-02 17:37:17 -070074 StringAppendF(&result, "%dms=%d ", bucket, count);
75 }
76 result.back() = '\n';
77 return result;
78}
79
Alec Mouri9a29e672020-09-14 12:39:14 -070080std::string TimeStatsHelper::JankPayload::toString() const {
81 std::string result;
82 StringAppendF(&result, "totalTimelineFrames = %d\n", totalFrames);
83 StringAppendF(&result, "jankyFrames = %d\n", totalJankyFrames);
84 StringAppendF(&result, "sfLongCpuJankyFrames = %d\n", totalSFLongCpu);
85 StringAppendF(&result, "sfLongGpuJankyFrames = %d\n", totalSFLongGpu);
Adithya Srinivasanead17162021-02-18 02:17:37 +000086 StringAppendF(&result, "sfUnattributedJankyFrames = %d\n", totalSFUnattributed);
87 StringAppendF(&result, "appUnattributedJankyFrames = %d\n", totalAppUnattributed);
88 StringAppendF(&result, "sfSchedulingJankyFrames = %d\n", totalSFScheduling);
89 StringAppendF(&result, "sfPredictionErrorJankyFrames = %d\n", totalSFPredictionError);
90 StringAppendF(&result, "appBufferStuffingJankyFrames = %d\n", totalAppBufferStuffing);
Alec Mouri9a29e672020-09-14 12:39:14 -070091 return result;
92}
93
Ady Abraham8b9e6122021-01-26 19:11:45 -080094std::string TimeStatsHelper::SetFrameRateVote::toString(FrameRateCompatibility compatibility) {
95 switch (compatibility) {
96 case FrameRateCompatibility::Undefined:
97 return "Undefined";
98 case FrameRateCompatibility::Default:
99 return "Default";
100 case FrameRateCompatibility::ExactOrMultiple:
101 return "ExactOrMultiple";
102 }
103}
104
105std::string TimeStatsHelper::SetFrameRateVote::toString(Seamlessness seamlessness) {
106 switch (seamlessness) {
107 case Seamlessness::Undefined:
108 return "Undefined";
109 case Seamlessness::ShouldBeSeamless:
110 return "ShouldBeSeamless";
111 case Seamlessness::NotRequired:
112 return "NotRequired";
113 }
114}
115
116std::string TimeStatsHelper::SetFrameRateVote::toString() const {
117 std::string result;
118 StringAppendF(&result, "frameRate = %.2f\n", frameRate);
119 StringAppendF(&result, "frameRateCompatibility = %s\n",
120 toString(frameRateCompatibility).c_str());
121 StringAppendF(&result, "seamlessness = %s\n", toString(seamlessness).c_str());
122 return result;
123}
124
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700125std::string TimeStatsHelper::TimeStatsLayer::toString() const {
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700126 std::string result = "\n";
Alec Mouri7d436ec2021-01-27 20:40:50 -0800127 StringAppendF(&result, "displayRefreshRate = %d fps\n", displayRefreshRateBucket);
128 StringAppendF(&result, "renderRate = %d fps\n", renderRateBucket);
Alec Mouri9a29e672020-09-14 12:39:14 -0700129 StringAppendF(&result, "uid = %d\n", uid);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700130 StringAppendF(&result, "layerName = %s\n", layerName.c_str());
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700131 StringAppendF(&result, "packageName = %s\n", packageName.c_str());
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700132 StringAppendF(&result, "totalFrames = %d\n", totalFrames);
133 StringAppendF(&result, "droppedFrames = %d\n", droppedFrames);
Alec Mouri91f6df32020-01-30 08:48:58 -0800134 StringAppendF(&result, "lateAcquireFrames = %d\n", lateAcquireFrames);
135 StringAppendF(&result, "badDesiredPresentFrames = %d\n", badDesiredPresentFrames);
Alec Mouri9a29e672020-09-14 12:39:14 -0700136 result.append("Jank payload for this layer:\n");
137 result.append(jankPayload.toString());
Ady Abraham8b9e6122021-01-26 19:11:45 -0800138 result.append("SetFrateRate vote for this layer:\n");
139 result.append(setFrameRateVote.toString());
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700140 const auto iter = deltas.find("present2present");
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700141 if (iter != deltas.end()) {
Yiwei Zhangdd221b22020-06-12 11:06:19 -0700142 const float averageTime = iter->second.averageTime();
143 const float averageFPS = averageTime < 1.0f ? 0.0f : 1000.0f / averageTime;
144 StringAppendF(&result, "averageFPS = %.3f\n", averageFPS);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700145 }
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700146 for (const auto& ele : deltas) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700147 StringAppendF(&result, "%s histogram is as below:\n", ele.first.c_str());
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700148 result.append(ele.second.toString());
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700149 }
150
151 return result;
152}
153
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700154std::string TimeStatsHelper::TimeStatsGlobal::toString(std::optional<uint32_t> maxLayers) const {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700155 std::string result = "SurfaceFlinger TimeStats:\n";
Alec Mouri7d436ec2021-01-27 20:40:50 -0800156 result.append("Legacy stats are as follows:\n");
157 StringAppendF(&result, "statsStart = %" PRId64 "\n", statsStartLegacy);
158 StringAppendF(&result, "statsEnd = %" PRId64 "\n", statsEndLegacy);
159 StringAppendF(&result, "totalFrames = %d\n", totalFramesLegacy);
160 StringAppendF(&result, "missedFrames = %d\n", missedFramesLegacy);
161 StringAppendF(&result, "clientCompositionFrames = %d\n", clientCompositionFramesLegacy);
162 StringAppendF(&result, "clientCompositionReusedFrames = %d\n",
163 clientCompositionReusedFramesLegacy);
164 StringAppendF(&result, "refreshRateSwitches = %d\n", refreshRateSwitchesLegacy);
165 StringAppendF(&result, "compositionStrategyChanges = %d\n", compositionStrategyChangesLegacy);
166 StringAppendF(&result, "displayOnTime = %" PRId64 " ms\n", displayOnTimeLegacy);
Alec Mourifb571ea2019-01-24 18:42:10 -0800167 StringAppendF(&result, "displayConfigStats is as below:\n");
Alec Mouri7d436ec2021-01-27 20:40:50 -0800168 for (const auto& [fps, duration] : refreshRateStatsLegacy) {
tangrobin8ef39762020-09-21 17:56:44 +0800169 StringAppendF(&result, "%dfps = %ldms\n", fps, ns2ms(duration));
Alec Mourifb571ea2019-01-24 18:42:10 -0800170 }
171 result.back() = '\n';
Alec Mouri7d436ec2021-01-27 20:40:50 -0800172 StringAppendF(&result, "totalP2PTime = %" PRId64 " ms\n", presentToPresentLegacy.totalTime());
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700173 StringAppendF(&result, "presentToPresent histogram is as below:\n");
Alec Mouri7d436ec2021-01-27 20:40:50 -0800174 result.append(presentToPresentLegacy.toString());
175 const float averageFrameDuration = frameDurationLegacy.averageTime();
Vishnu Nairabf97fd2020-02-03 13:51:16 -0800176 StringAppendF(&result, "averageFrameDuration = %.3f ms\n",
177 std::isnan(averageFrameDuration) ? 0.0f : averageFrameDuration);
Alec Mouri9519bf12019-11-15 16:54:44 -0800178 StringAppendF(&result, "frameDuration histogram is as below:\n");
Alec Mouri7d436ec2021-01-27 20:40:50 -0800179 result.append(frameDurationLegacy.toString());
180 const float averageRenderEngineTiming = renderEngineTimingLegacy.averageTime();
Vishnu Nairabf97fd2020-02-03 13:51:16 -0800181 StringAppendF(&result, "averageRenderEngineTiming = %.3f ms\n",
182 std::isnan(averageRenderEngineTiming) ? 0.0f : averageRenderEngineTiming);
Alec Mourie4034bb2019-11-19 12:45:54 -0800183 StringAppendF(&result, "renderEngineTiming histogram is as below:\n");
Alec Mouri7d436ec2021-01-27 20:40:50 -0800184 result.append(renderEngineTimingLegacy.toString());
185
186 result.append("\nGlobal aggregated jank payload (Timeline stats):");
187 for (const auto& ele : stats) {
188 result.append("\n");
189 StringAppendF(&result, "displayRefreshRate = %d fps\n",
190 ele.second.key.displayRefreshRateBucket);
191 StringAppendF(&result, "renderRate = %d fps\n", ele.second.key.renderRateBucket);
192 result.append(ele.second.jankPayload.toString());
Alec Mouri363faf02021-01-29 16:34:55 -0800193 StringAppendF(&result, "sfDeadlineMisses histogram is as below:\n");
194 result.append(ele.second.displayDeadlineDeltas.toString());
195 StringAppendF(&result, "sfPredictionErrors histogram is as below:\n");
196 result.append(ele.second.displayPresentDeltas.toString());
Alec Mouri7d436ec2021-01-27 20:40:50 -0800197 }
198
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700199 const auto dumpStats = generateDumpStats(maxLayers);
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700200 for (const auto& ele : dumpStats) {
201 result.append(ele->toString());
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700202 }
203
204 return result;
205}
206
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700207SFTimeStatsLayerProto TimeStatsHelper::TimeStatsLayer::toProto() const {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700208 SFTimeStatsLayerProto layerProto;
209 layerProto.set_layer_name(layerName);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700210 layerProto.set_package_name(packageName);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700211 layerProto.set_total_frames(totalFrames);
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700212 layerProto.set_dropped_frames(droppedFrames);
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700213 for (const auto& ele : deltas) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700214 SFTimeStatsDeltaProto* deltaProto = layerProto.add_deltas();
215 deltaProto->set_delta_name(ele.first);
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700216 for (const auto& histEle : ele.second.hist) {
Yiwei Zhang3bef3952018-05-04 14:08:01 -0700217 SFTimeStatsHistogramBucketProto* histProto = deltaProto->add_histograms();
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700218 histProto->set_time_millis(histEle.first);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700219 histProto->set_frame_count(histEle.second);
220 }
221 }
222 return layerProto;
223}
224
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700225SFTimeStatsGlobalProto TimeStatsHelper::TimeStatsGlobal::toProto(
226 std::optional<uint32_t> maxLayers) const {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700227 SFTimeStatsGlobalProto globalProto;
Alec Mouri7d436ec2021-01-27 20:40:50 -0800228 globalProto.set_stats_start(statsStartLegacy);
229 globalProto.set_stats_end(statsEndLegacy);
230 globalProto.set_total_frames(totalFramesLegacy);
231 globalProto.set_missed_frames(missedFramesLegacy);
232 globalProto.set_client_composition_frames(clientCompositionFramesLegacy);
233 globalProto.set_display_on_time(displayOnTimeLegacy);
234 for (const auto& ele : refreshRateStatsLegacy) {
Alec Mourifb571ea2019-01-24 18:42:10 -0800235 SFTimeStatsDisplayConfigBucketProto* configBucketProto =
236 globalProto.add_display_config_stats();
237 SFTimeStatsDisplayConfigProto* configProto = configBucketProto->mutable_config();
238 configProto->set_fps(ele.first);
239 configBucketProto->set_duration_millis(ns2ms(ele.second));
240 }
Alec Mouri7d436ec2021-01-27 20:40:50 -0800241 for (const auto& histEle : presentToPresentLegacy.hist) {
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700242 SFTimeStatsHistogramBucketProto* histProto = globalProto.add_present_to_present();
243 histProto->set_time_millis(histEle.first);
244 histProto->set_frame_count(histEle.second);
245 }
Alec Mouri7d436ec2021-01-27 20:40:50 -0800246 for (const auto& histEle : frameDurationLegacy.hist) {
Alec Mouri9519bf12019-11-15 16:54:44 -0800247 SFTimeStatsHistogramBucketProto* histProto = globalProto.add_frame_duration();
248 histProto->set_time_millis(histEle.first);
249 histProto->set_frame_count(histEle.second);
250 }
Alec Mouri7d436ec2021-01-27 20:40:50 -0800251 for (const auto& histEle : renderEngineTimingLegacy.hist) {
Alec Mourie4034bb2019-11-19 12:45:54 -0800252 SFTimeStatsHistogramBucketProto* histProto = globalProto.add_render_engine_timing();
253 histProto->set_time_millis(histEle.first);
254 histProto->set_frame_count(histEle.second);
255 }
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700256 const auto dumpStats = generateDumpStats(maxLayers);
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700257 for (const auto& ele : dumpStats) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700258 SFTimeStatsLayerProto* layerProto = globalProto.add_stats();
259 layerProto->CopyFrom(ele->toProto());
260 }
261 return globalProto;
262}
263
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700264std::vector<TimeStatsHelper::TimeStatsLayer const*>
265TimeStatsHelper::TimeStatsGlobal::generateDumpStats(std::optional<uint32_t> maxLayers) const {
266 std::vector<TimeStatsLayer const*> dumpStats;
Alec Mouri7d436ec2021-01-27 20:40:50 -0800267
268 int numLayers = 0;
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700269 for (const auto& ele : stats) {
Alec Mouri7d436ec2021-01-27 20:40:50 -0800270 numLayers += ele.second.stats.size();
271 }
272
273 dumpStats.reserve(numLayers);
274
275 for (const auto& ele : stats) {
276 for (const auto& layerEle : ele.second.stats) {
277 dumpStats.push_back(&layerEle.second);
278 }
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700279 }
280
281 std::sort(dumpStats.begin(), dumpStats.end(),
282 [](TimeStatsHelper::TimeStatsLayer const* l,
283 TimeStatsHelper::TimeStatsLayer const* r) {
284 return l->totalFrames > r->totalFrames;
285 });
286
287 if (maxLayers && (*maxLayers < dumpStats.size())) {
288 dumpStats.resize(*maxLayers);
289 }
290 return dumpStats;
291}
292
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700293} // namespace surfaceflinger
294} // namespace android