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
18 #include <android-base/stringprintf.h>
19 #include <inttypes.h>
20
21 #include <array>
22
23 #define HISTOGRAM_SIZE 85
24
25 using android::base::StringAppendF;
26 using android::base::StringPrintf;
27
28 namespace android {
29 namespace surfaceflinger {
30
31 // Time buckets for histogram, the calculated time deltas will be lower bounded
32 // to the buckets in this array.
33 static 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
insert(int32_t delta)40 void 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]) {
44 hist[histogramConfig[HISTOGRAM_SIZE - 1]]++;
45 return;
46 }
47 auto iter = std::lower_bound(histogramConfig.begin(), histogramConfig.end(), delta);
48 hist[*iter]++;
49 }
50
totalTime() const51 int64_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
averageTime() const59 float TimeStatsHelper::Histogram::averageTime() const {
60 int64_t ret = 0;
61 int64_t count = 0;
62 for (const auto& ele : hist) {
63 count += ele.second;
64 ret += ele.first * ele.second;
65 }
66 return static_cast<float>(ret) / count;
67 }
68
toString() const69 std::string TimeStatsHelper::Histogram::toString() const {
70 std::string result;
71 for (int32_t i = 0; i < HISTOGRAM_SIZE; ++i) {
72 int32_t bucket = histogramConfig[i];
73 int32_t count = (hist.count(bucket) == 0) ? 0 : hist.at(bucket);
74 StringAppendF(&result, "%dms=%d ", bucket, count);
75 }
76 result.back() = '\n';
77 return result;
78 }
79
toString() const80 std::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);
86 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);
91 return result;
92 }
93
toString(FrameRateCompatibility compatibility)94 std::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
toString(Seamlessness seamlessness)105 std::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
toString() const116 std::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
toString(int32_t gameMode) const125 std::string TimeStatsHelper::TimeStatsLayer::toString(int32_t gameMode) const {
126 switch (gameMode) {
127 case TimeStatsHelper::GameModeUnsupported:
128 return "GameModeUnsupported";
129 case TimeStatsHelper::GameModeStandard:
130 return "GameModeStandard";
131 case TimeStatsHelper::GameModePerformance:
132 return "GameModePerformance";
133 case TimeStatsHelper::GameModeBattery:
134 return "GameModeBattery";
135 default:
136 return "GameModeUnspecified";
137 }
138 }
toString() const139 std::string TimeStatsHelper::TimeStatsLayer::toString() const {
140 std::string result = "\n";
141 StringAppendF(&result, "displayRefreshRate = %d fps\n", displayRefreshRateBucket);
142 StringAppendF(&result, "renderRate = %d fps\n", renderRateBucket);
143 StringAppendF(&result, "uid = %d\n", uid);
144 StringAppendF(&result, "layerName = %s\n", layerName.c_str());
145 StringAppendF(&result, "packageName = %s\n", packageName.c_str());
146 StringAppendF(&result, "gameMode = %s\n", toString(gameMode).c_str());
147 StringAppendF(&result, "totalFrames = %d\n", totalFrames);
148 StringAppendF(&result, "droppedFrames = %d\n", droppedFrames);
149 StringAppendF(&result, "lateAcquireFrames = %d\n", lateAcquireFrames);
150 StringAppendF(&result, "badDesiredPresentFrames = %d\n", badDesiredPresentFrames);
151 result.append("Jank payload for this layer:\n");
152 result.append(jankPayload.toString());
153 result.append("SetFrateRate vote for this layer:\n");
154 result.append(setFrameRateVote.toString());
155 const auto iter = deltas.find("present2present");
156 if (iter != deltas.end()) {
157 const float averageTime = iter->second.averageTime();
158 const float averageFPS = averageTime < 1.0f ? 0.0f : 1000.0f / averageTime;
159 StringAppendF(&result, "averageFPS = %.3f\n", averageFPS);
160 }
161 for (const auto& ele : deltas) {
162 StringAppendF(&result, "%s histogram is as below:\n", ele.first.c_str());
163 result.append(ele.second.toString());
164 }
165
166 return result;
167 }
168
toString(std::optional<uint32_t> maxLayers) const169 std::string TimeStatsHelper::TimeStatsGlobal::toString(std::optional<uint32_t> maxLayers) const {
170 std::string result = "SurfaceFlinger TimeStats:\n";
171 result.append("Legacy stats are as follows:\n");
172 StringAppendF(&result, "statsStart = %" PRId64 "\n", statsStartLegacy);
173 StringAppendF(&result, "statsEnd = %" PRId64 "\n", statsEndLegacy);
174 StringAppendF(&result, "totalFrames = %d\n", totalFramesLegacy);
175 StringAppendF(&result, "missedFrames = %d\n", missedFramesLegacy);
176 StringAppendF(&result, "clientCompositionFrames = %d\n", clientCompositionFramesLegacy);
177 StringAppendF(&result, "clientCompositionReusedFrames = %d\n",
178 clientCompositionReusedFramesLegacy);
179 StringAppendF(&result, "refreshRateSwitches = %d\n", refreshRateSwitchesLegacy);
180 StringAppendF(&result, "compositionStrategyChanges = %d\n", compositionStrategyChangesLegacy);
181 StringAppendF(&result, "displayOnTime = %" PRId64 " ms\n", displayOnTimeLegacy);
182 StringAppendF(&result, "displayConfigStats is as below:\n");
183 for (const auto& [fps, duration] : refreshRateStatsLegacy) {
184 StringAppendF(&result, "%dfps = %ldms\n", fps, ns2ms(duration));
185 }
186 result.back() = '\n';
187 StringAppendF(&result, "totalP2PTime = %" PRId64 " ms\n", presentToPresentLegacy.totalTime());
188 StringAppendF(&result, "presentToPresent histogram is as below:\n");
189 result.append(presentToPresentLegacy.toString());
190 const float averageFrameDuration = frameDurationLegacy.averageTime();
191 StringAppendF(&result, "averageFrameDuration = %.3f ms\n",
192 std::isnan(averageFrameDuration) ? 0.0f : averageFrameDuration);
193 StringAppendF(&result, "frameDuration histogram is as below:\n");
194 result.append(frameDurationLegacy.toString());
195 const float averageRenderEngineTiming = renderEngineTimingLegacy.averageTime();
196 StringAppendF(&result, "averageRenderEngineTiming = %.3f ms\n",
197 std::isnan(averageRenderEngineTiming) ? 0.0f : averageRenderEngineTiming);
198 StringAppendF(&result, "renderEngineTiming histogram is as below:\n");
199 result.append(renderEngineTimingLegacy.toString());
200
201 result.append("\nGlobal aggregated jank payload (Timeline stats):");
202 for (const auto& ele : stats) {
203 result.append("\n");
204 StringAppendF(&result, "displayRefreshRate = %d fps\n",
205 ele.second.key.displayRefreshRateBucket);
206 StringAppendF(&result, "renderRate = %d fps\n", ele.second.key.renderRateBucket);
207 result.append(ele.second.jankPayload.toString());
208 StringAppendF(&result, "sfDeadlineMisses histogram is as below:\n");
209 result.append(ele.second.displayDeadlineDeltas.toString());
210 StringAppendF(&result, "sfPredictionErrors histogram is as below:\n");
211 result.append(ele.second.displayPresentDeltas.toString());
212 }
213
214 const auto dumpStats = generateDumpStats(maxLayers);
215 for (const auto& ele : dumpStats) {
216 result.append(ele->toString());
217 }
218
219 return result;
220 }
221
toProto() const222 SFTimeStatsLayerProto TimeStatsHelper::TimeStatsLayer::toProto() const {
223 SFTimeStatsLayerProto layerProto;
224 layerProto.set_layer_name(layerName);
225 layerProto.set_package_name(packageName);
226 layerProto.set_total_frames(totalFrames);
227 layerProto.set_dropped_frames(droppedFrames);
228 for (const auto& ele : deltas) {
229 SFTimeStatsDeltaProto* deltaProto = layerProto.add_deltas();
230 deltaProto->set_delta_name(ele.first);
231 for (const auto& histEle : ele.second.hist) {
232 SFTimeStatsHistogramBucketProto* histProto = deltaProto->add_histograms();
233 histProto->set_time_millis(histEle.first);
234 histProto->set_frame_count(histEle.second);
235 }
236 }
237 return layerProto;
238 }
239
toProto(std::optional<uint32_t> maxLayers) const240 SFTimeStatsGlobalProto TimeStatsHelper::TimeStatsGlobal::toProto(
241 std::optional<uint32_t> maxLayers) const {
242 SFTimeStatsGlobalProto globalProto;
243 globalProto.set_stats_start(statsStartLegacy);
244 globalProto.set_stats_end(statsEndLegacy);
245 globalProto.set_total_frames(totalFramesLegacy);
246 globalProto.set_missed_frames(missedFramesLegacy);
247 globalProto.set_client_composition_frames(clientCompositionFramesLegacy);
248 globalProto.set_display_on_time(displayOnTimeLegacy);
249 for (const auto& ele : refreshRateStatsLegacy) {
250 SFTimeStatsDisplayConfigBucketProto* configBucketProto =
251 globalProto.add_display_config_stats();
252 SFTimeStatsDisplayConfigProto* configProto = configBucketProto->mutable_config();
253 configProto->set_fps(ele.first);
254 configBucketProto->set_duration_millis(ns2ms(ele.second));
255 }
256 for (const auto& histEle : presentToPresentLegacy.hist) {
257 SFTimeStatsHistogramBucketProto* histProto = globalProto.add_present_to_present();
258 histProto->set_time_millis(histEle.first);
259 histProto->set_frame_count(histEle.second);
260 }
261 for (const auto& histEle : frameDurationLegacy.hist) {
262 SFTimeStatsHistogramBucketProto* histProto = globalProto.add_frame_duration();
263 histProto->set_time_millis(histEle.first);
264 histProto->set_frame_count(histEle.second);
265 }
266 for (const auto& histEle : renderEngineTimingLegacy.hist) {
267 SFTimeStatsHistogramBucketProto* histProto = globalProto.add_render_engine_timing();
268 histProto->set_time_millis(histEle.first);
269 histProto->set_frame_count(histEle.second);
270 }
271 const auto dumpStats = generateDumpStats(maxLayers);
272 for (const auto& ele : dumpStats) {
273 SFTimeStatsLayerProto* layerProto = globalProto.add_stats();
274 layerProto->CopyFrom(ele->toProto());
275 }
276 return globalProto;
277 }
278
279 std::vector<TimeStatsHelper::TimeStatsLayer const*>
generateDumpStats(std::optional<uint32_t> maxLayers) const280 TimeStatsHelper::TimeStatsGlobal::generateDumpStats(std::optional<uint32_t> maxLayers) const {
281 std::vector<TimeStatsLayer const*> dumpStats;
282
283 int numLayers = 0;
284 for (const auto& ele : stats) {
285 numLayers += ele.second.stats.size();
286 }
287
288 dumpStats.reserve(numLayers);
289
290 for (const auto& ele : stats) {
291 for (const auto& layerEle : ele.second.stats) {
292 dumpStats.push_back(&layerEle.second);
293 }
294 }
295
296 std::sort(dumpStats.begin(), dumpStats.end(),
297 [](TimeStatsHelper::TimeStatsLayer const* l,
298 TimeStatsHelper::TimeStatsLayer const* r) {
299 return l->totalFrames > r->totalFrames;
300 });
301
302 if (maxLayers && (*maxLayers < dumpStats.size())) {
303 dumpStats.resize(*maxLayers);
304 }
305 return dumpStats;
306 }
307
308 } // namespace surfaceflinger
309 } // namespace android
310