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]] += delta / 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::TimeStatsLayer::toString() const {
81     std::string result = "\n";
82     StringAppendF(&result, "layerName = %s\n", layerName.c_str());
83     StringAppendF(&result, "packageName = %s\n", packageName.c_str());
84     StringAppendF(&result, "totalFrames = %d\n", totalFrames);
85     StringAppendF(&result, "droppedFrames = %d\n", droppedFrames);
86     StringAppendF(&result, "lateAcquireFrames = %d\n", lateAcquireFrames);
87     StringAppendF(&result, "badDesiredPresentFrames = %d\n", badDesiredPresentFrames);
88     const auto iter = deltas.find("present2present");
89     if (iter != deltas.end()) {
90         const float averageTime = iter->second.averageTime();
91         const float averageFPS = averageTime < 1.0f ? 0.0f : 1000.0f / averageTime;
92         StringAppendF(&result, "averageFPS = %.3f\n", averageFPS);
93     }
94     for (const auto& ele : deltas) {
95         StringAppendF(&result, "%s histogram is as below:\n", ele.first.c_str());
96         result.append(ele.second.toString());
97     }
98 
99     return result;
100 }
101 
toString(std::optional<uint32_t> maxLayers) const102 std::string TimeStatsHelper::TimeStatsGlobal::toString(std::optional<uint32_t> maxLayers) const {
103     std::string result = "SurfaceFlinger TimeStats:\n";
104     StringAppendF(&result, "statsStart = %" PRId64 "\n", statsStart);
105     StringAppendF(&result, "statsEnd = %" PRId64 "\n", statsEnd);
106     StringAppendF(&result, "totalFrames = %d\n", totalFrames);
107     StringAppendF(&result, "missedFrames = %d\n", missedFrames);
108     StringAppendF(&result, "clientCompositionFrames = %d\n", clientCompositionFrames);
109     StringAppendF(&result, "clientCompositionReusedFrames = %d\n", clientCompositionReusedFrames);
110     StringAppendF(&result, "refreshRateSwitches = %d\n", refreshRateSwitches);
111     StringAppendF(&result, "compositionStrategyChanges = %d\n", compositionStrategyChanges);
112     StringAppendF(&result, "displayOnTime = %" PRId64 " ms\n", displayOnTime);
113     StringAppendF(&result, "displayConfigStats is as below:\n");
114     for (const auto& [fps, duration] : refreshRateStats) {
115         StringAppendF(&result, "%dfps=%ldms ", fps, ns2ms(duration));
116     }
117     result.back() = '\n';
118     StringAppendF(&result, "totalP2PTime = %" PRId64 " ms\n", presentToPresent.totalTime());
119     StringAppendF(&result, "presentToPresent histogram is as below:\n");
120     result.append(presentToPresent.toString());
121     const float averageFrameDuration = frameDuration.averageTime();
122     StringAppendF(&result, "averageFrameDuration = %.3f ms\n",
123                   std::isnan(averageFrameDuration) ? 0.0f : averageFrameDuration);
124     StringAppendF(&result, "frameDuration histogram is as below:\n");
125     result.append(frameDuration.toString());
126     const float averageRenderEngineTiming = renderEngineTiming.averageTime();
127     StringAppendF(&result, "averageRenderEngineTiming = %.3f ms\n",
128                   std::isnan(averageRenderEngineTiming) ? 0.0f : averageRenderEngineTiming);
129     StringAppendF(&result, "renderEngineTiming histogram is as below:\n");
130     result.append(renderEngineTiming.toString());
131     const auto dumpStats = generateDumpStats(maxLayers);
132     for (const auto& ele : dumpStats) {
133         result.append(ele->toString());
134     }
135 
136     return result;
137 }
138 
toProto() const139 SFTimeStatsLayerProto TimeStatsHelper::TimeStatsLayer::toProto() const {
140     SFTimeStatsLayerProto layerProto;
141     layerProto.set_layer_name(layerName);
142     layerProto.set_package_name(packageName);
143     layerProto.set_total_frames(totalFrames);
144     layerProto.set_dropped_frames(droppedFrames);
145     for (const auto& ele : deltas) {
146         SFTimeStatsDeltaProto* deltaProto = layerProto.add_deltas();
147         deltaProto->set_delta_name(ele.first);
148         for (const auto& histEle : ele.second.hist) {
149             SFTimeStatsHistogramBucketProto* histProto = deltaProto->add_histograms();
150             histProto->set_time_millis(histEle.first);
151             histProto->set_frame_count(histEle.second);
152         }
153     }
154     return layerProto;
155 }
156 
toProto(std::optional<uint32_t> maxLayers) const157 SFTimeStatsGlobalProto TimeStatsHelper::TimeStatsGlobal::toProto(
158         std::optional<uint32_t> maxLayers) const {
159     SFTimeStatsGlobalProto globalProto;
160     globalProto.set_stats_start(statsStart);
161     globalProto.set_stats_end(statsEnd);
162     globalProto.set_total_frames(totalFrames);
163     globalProto.set_missed_frames(missedFrames);
164     globalProto.set_client_composition_frames(clientCompositionFrames);
165     globalProto.set_display_on_time(displayOnTime);
166     for (const auto& ele : refreshRateStats) {
167         SFTimeStatsDisplayConfigBucketProto* configBucketProto =
168                 globalProto.add_display_config_stats();
169         SFTimeStatsDisplayConfigProto* configProto = configBucketProto->mutable_config();
170         configProto->set_fps(ele.first);
171         configBucketProto->set_duration_millis(ns2ms(ele.second));
172     }
173     for (const auto& histEle : presentToPresent.hist) {
174         SFTimeStatsHistogramBucketProto* histProto = globalProto.add_present_to_present();
175         histProto->set_time_millis(histEle.first);
176         histProto->set_frame_count(histEle.second);
177     }
178     for (const auto& histEle : frameDuration.hist) {
179         SFTimeStatsHistogramBucketProto* histProto = globalProto.add_frame_duration();
180         histProto->set_time_millis(histEle.first);
181         histProto->set_frame_count(histEle.second);
182     }
183     for (const auto& histEle : renderEngineTiming.hist) {
184         SFTimeStatsHistogramBucketProto* histProto = globalProto.add_render_engine_timing();
185         histProto->set_time_millis(histEle.first);
186         histProto->set_frame_count(histEle.second);
187     }
188     const auto dumpStats = generateDumpStats(maxLayers);
189     for (const auto& ele : dumpStats) {
190         SFTimeStatsLayerProto* layerProto = globalProto.add_stats();
191         layerProto->CopyFrom(ele->toProto());
192     }
193     return globalProto;
194 }
195 
196 std::vector<TimeStatsHelper::TimeStatsLayer const*>
generateDumpStats(std::optional<uint32_t> maxLayers) const197 TimeStatsHelper::TimeStatsGlobal::generateDumpStats(std::optional<uint32_t> maxLayers) const {
198     std::vector<TimeStatsLayer const*> dumpStats;
199     for (const auto& ele : stats) {
200         dumpStats.push_back(&ele.second);
201     }
202 
203     std::sort(dumpStats.begin(), dumpStats.end(),
204               [](TimeStatsHelper::TimeStatsLayer const* l,
205                  TimeStatsHelper::TimeStatsLayer const* r) {
206                   return l->totalFrames > r->totalFrames;
207               });
208 
209     if (maxLayers && (*maxLayers < dumpStats.size())) {
210         dumpStats.resize(*maxLayers);
211     }
212     return dumpStats;
213 }
214 
215 } // namespace surfaceflinger
216 } // namespace android
217