blob: 69afa2a7a156123b05501f1f53bc578f057ae5a5 [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);
146 StringAppendF(&result, "displayOnTime = %" PRId64 " ms\n", displayOnTimeLegacy);
Alec Mourifb571ea2019-01-24 18:42:10 -0800147 StringAppendF(&result, "displayConfigStats is as below:\n");
Alec Mouri7d436ec2021-01-27 20:40:50 -0800148 for (const auto& [fps, duration] : refreshRateStatsLegacy) {
tangrobin8ef39762020-09-21 17:56:44 +0800149 StringAppendF(&result, "%dfps = %ldms\n", fps, ns2ms(duration));
Alec Mourifb571ea2019-01-24 18:42:10 -0800150 }
151 result.back() = '\n';
Alec Mouri7d436ec2021-01-27 20:40:50 -0800152 StringAppendF(&result, "totalP2PTime = %" PRId64 " ms\n", presentToPresentLegacy.totalTime());
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700153 StringAppendF(&result, "presentToPresent histogram is as below:\n");
Alec Mouri7d436ec2021-01-27 20:40:50 -0800154 result.append(presentToPresentLegacy.toString());
155 const float averageFrameDuration = frameDurationLegacy.averageTime();
Vishnu Nairabf97fd2020-02-03 13:51:16 -0800156 StringAppendF(&result, "averageFrameDuration = %.3f ms\n",
157 std::isnan(averageFrameDuration) ? 0.0f : averageFrameDuration);
Alec Mouri9519bf12019-11-15 16:54:44 -0800158 StringAppendF(&result, "frameDuration histogram is as below:\n");
Alec Mouri7d436ec2021-01-27 20:40:50 -0800159 result.append(frameDurationLegacy.toString());
160 const float averageRenderEngineTiming = renderEngineTimingLegacy.averageTime();
Vishnu Nairabf97fd2020-02-03 13:51:16 -0800161 StringAppendF(&result, "averageRenderEngineTiming = %.3f ms\n",
162 std::isnan(averageRenderEngineTiming) ? 0.0f : averageRenderEngineTiming);
Alec Mourie4034bb2019-11-19 12:45:54 -0800163 StringAppendF(&result, "renderEngineTiming histogram is as below:\n");
Alec Mouri7d436ec2021-01-27 20:40:50 -0800164 result.append(renderEngineTimingLegacy.toString());
165
166 result.append("\nGlobal aggregated jank payload (Timeline stats):");
167 for (const auto& ele : stats) {
168 result.append("\n");
169 StringAppendF(&result, "displayRefreshRate = %d fps\n",
170 ele.second.key.displayRefreshRateBucket);
171 StringAppendF(&result, "renderRate = %d fps\n", ele.second.key.renderRateBucket);
172 result.append(ele.second.jankPayload.toString());
Alec Mouri363faf02021-01-29 16:34:55 -0800173 StringAppendF(&result, "sfDeadlineMisses histogram is as below:\n");
174 result.append(ele.second.displayDeadlineDeltas.toString());
175 StringAppendF(&result, "sfPredictionErrors histogram is as below:\n");
176 result.append(ele.second.displayPresentDeltas.toString());
Alec Mouri7d436ec2021-01-27 20:40:50 -0800177 }
178
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700179 const auto dumpStats = generateDumpStats(maxLayers);
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700180 for (const auto& ele : dumpStats) {
181 result.append(ele->toString());
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700182 }
183
184 return result;
185}
186
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700187SFTimeStatsLayerProto TimeStatsHelper::TimeStatsLayer::toProto() const {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700188 SFTimeStatsLayerProto layerProto;
189 layerProto.set_layer_name(layerName);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700190 layerProto.set_package_name(packageName);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700191 layerProto.set_total_frames(totalFrames);
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700192 layerProto.set_dropped_frames(droppedFrames);
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700193 for (const auto& ele : deltas) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700194 SFTimeStatsDeltaProto* deltaProto = layerProto.add_deltas();
195 deltaProto->set_delta_name(ele.first);
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700196 for (const auto& histEle : ele.second.hist) {
Yiwei Zhang3bef3952018-05-04 14:08:01 -0700197 SFTimeStatsHistogramBucketProto* histProto = deltaProto->add_histograms();
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700198 histProto->set_time_millis(histEle.first);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700199 histProto->set_frame_count(histEle.second);
200 }
201 }
202 return layerProto;
203}
204
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700205SFTimeStatsGlobalProto TimeStatsHelper::TimeStatsGlobal::toProto(
206 std::optional<uint32_t> maxLayers) const {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700207 SFTimeStatsGlobalProto globalProto;
Alec Mouri7d436ec2021-01-27 20:40:50 -0800208 globalProto.set_stats_start(statsStartLegacy);
209 globalProto.set_stats_end(statsEndLegacy);
210 globalProto.set_total_frames(totalFramesLegacy);
211 globalProto.set_missed_frames(missedFramesLegacy);
212 globalProto.set_client_composition_frames(clientCompositionFramesLegacy);
213 globalProto.set_display_on_time(displayOnTimeLegacy);
214 for (const auto& ele : refreshRateStatsLegacy) {
Alec Mourifb571ea2019-01-24 18:42:10 -0800215 SFTimeStatsDisplayConfigBucketProto* configBucketProto =
216 globalProto.add_display_config_stats();
217 SFTimeStatsDisplayConfigProto* configProto = configBucketProto->mutable_config();
218 configProto->set_fps(ele.first);
219 configBucketProto->set_duration_millis(ns2ms(ele.second));
220 }
Alec Mouri7d436ec2021-01-27 20:40:50 -0800221 for (const auto& histEle : presentToPresentLegacy.hist) {
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700222 SFTimeStatsHistogramBucketProto* histProto = globalProto.add_present_to_present();
223 histProto->set_time_millis(histEle.first);
224 histProto->set_frame_count(histEle.second);
225 }
Alec Mouri7d436ec2021-01-27 20:40:50 -0800226 for (const auto& histEle : frameDurationLegacy.hist) {
Alec Mouri9519bf12019-11-15 16:54:44 -0800227 SFTimeStatsHistogramBucketProto* histProto = globalProto.add_frame_duration();
228 histProto->set_time_millis(histEle.first);
229 histProto->set_frame_count(histEle.second);
230 }
Alec Mouri7d436ec2021-01-27 20:40:50 -0800231 for (const auto& histEle : renderEngineTimingLegacy.hist) {
Alec Mourie4034bb2019-11-19 12:45:54 -0800232 SFTimeStatsHistogramBucketProto* histProto = globalProto.add_render_engine_timing();
233 histProto->set_time_millis(histEle.first);
234 histProto->set_frame_count(histEle.second);
235 }
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700236 const auto dumpStats = generateDumpStats(maxLayers);
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700237 for (const auto& ele : dumpStats) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700238 SFTimeStatsLayerProto* layerProto = globalProto.add_stats();
239 layerProto->CopyFrom(ele->toProto());
240 }
241 return globalProto;
242}
243
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700244std::vector<TimeStatsHelper::TimeStatsLayer const*>
245TimeStatsHelper::TimeStatsGlobal::generateDumpStats(std::optional<uint32_t> maxLayers) const {
246 std::vector<TimeStatsLayer const*> dumpStats;
Alec Mouri7d436ec2021-01-27 20:40:50 -0800247
248 int numLayers = 0;
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700249 for (const auto& ele : stats) {
Alec Mouri7d436ec2021-01-27 20:40:50 -0800250 numLayers += ele.second.stats.size();
251 }
252
253 dumpStats.reserve(numLayers);
254
255 for (const auto& ele : stats) {
256 for (const auto& layerEle : ele.second.stats) {
257 dumpStats.push_back(&layerEle.second);
258 }
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700259 }
260
261 std::sort(dumpStats.begin(), dumpStats.end(),
262 [](TimeStatsHelper::TimeStatsLayer const* l,
263 TimeStatsHelper::TimeStatsLayer const* r) {
264 return l->totalFrames > r->totalFrames;
265 });
266
267 if (maxLayers && (*maxLayers < dumpStats.size())) {
268 dumpStats.resize(*maxLayers);
269 }
270 return dumpStats;
271}
272
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700273} // namespace surfaceflinger
274} // namespace android