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 const auto iter = deltas.find("present2present");
87 if (iter != deltas.end()) {
88 StringAppendF(&result, "averageFPS = %.3f\n", 1000.0 / iter->second.averageTime());
89 }
90 for (const auto& ele : deltas) {
91 StringAppendF(&result, "%s histogram is as below:\n", ele.first.c_str());
92 result.append(ele.second.toString());
93 }
94
95 return result;
96 }
97
toString(std::optional<uint32_t> maxLayers) const98 std::string TimeStatsHelper::TimeStatsGlobal::toString(std::optional<uint32_t> maxLayers) const {
99 std::string result = "SurfaceFlinger TimeStats:\n";
100 StringAppendF(&result, "statsStart = %" PRId64 "\n", statsStart);
101 StringAppendF(&result, "statsEnd = %" PRId64 "\n", statsEnd);
102 StringAppendF(&result, "totalFrames = %d\n", totalFrames);
103 StringAppendF(&result, "missedFrames = %d\n", missedFrames);
104 StringAppendF(&result, "clientCompositionFrames = %d\n", clientCompositionFrames);
105 StringAppendF(&result, "displayOnTime = %" PRId64 " ms\n", displayOnTime);
106 StringAppendF(&result, "displayConfigStats is as below:\n");
107 for (const auto& [fps, duration] : refreshRateStats) {
108 StringAppendF(&result, "%dfps=%ldms ", fps, ns2ms(duration));
109 }
110 result.back() = '\n';
111 StringAppendF(&result, "totalP2PTime = %" PRId64 " ms\n", presentToPresent.totalTime());
112 StringAppendF(&result, "presentToPresent histogram is as below:\n");
113 result.append(presentToPresent.toString());
114 const auto dumpStats = generateDumpStats(maxLayers);
115 for (const auto& ele : dumpStats) {
116 result.append(ele->toString());
117 }
118
119 return result;
120 }
121
toProto() const122 SFTimeStatsLayerProto TimeStatsHelper::TimeStatsLayer::toProto() const {
123 SFTimeStatsLayerProto layerProto;
124 layerProto.set_layer_name(layerName);
125 layerProto.set_package_name(packageName);
126 layerProto.set_total_frames(totalFrames);
127 layerProto.set_dropped_frames(droppedFrames);
128 for (const auto& ele : deltas) {
129 SFTimeStatsDeltaProto* deltaProto = layerProto.add_deltas();
130 deltaProto->set_delta_name(ele.first);
131 for (const auto& histEle : ele.second.hist) {
132 SFTimeStatsHistogramBucketProto* histProto = deltaProto->add_histograms();
133 histProto->set_time_millis(histEle.first);
134 histProto->set_frame_count(histEle.second);
135 }
136 }
137 return layerProto;
138 }
139
toProto(std::optional<uint32_t> maxLayers) const140 SFTimeStatsGlobalProto TimeStatsHelper::TimeStatsGlobal::toProto(
141 std::optional<uint32_t> maxLayers) const {
142 SFTimeStatsGlobalProto globalProto;
143 globalProto.set_stats_start(statsStart);
144 globalProto.set_stats_end(statsEnd);
145 globalProto.set_total_frames(totalFrames);
146 globalProto.set_missed_frames(missedFrames);
147 globalProto.set_client_composition_frames(clientCompositionFrames);
148 globalProto.set_display_on_time(displayOnTime);
149 for (const auto& ele : refreshRateStats) {
150 SFTimeStatsDisplayConfigBucketProto* configBucketProto =
151 globalProto.add_display_config_stats();
152 SFTimeStatsDisplayConfigProto* configProto = configBucketProto->mutable_config();
153 configProto->set_fps(ele.first);
154 configBucketProto->set_duration_millis(ns2ms(ele.second));
155 }
156 for (const auto& histEle : presentToPresent.hist) {
157 SFTimeStatsHistogramBucketProto* histProto = globalProto.add_present_to_present();
158 histProto->set_time_millis(histEle.first);
159 histProto->set_frame_count(histEle.second);
160 }
161 const auto dumpStats = generateDumpStats(maxLayers);
162 for (const auto& ele : dumpStats) {
163 SFTimeStatsLayerProto* layerProto = globalProto.add_stats();
164 layerProto->CopyFrom(ele->toProto());
165 }
166 return globalProto;
167 }
168
169 std::vector<TimeStatsHelper::TimeStatsLayer const*>
generateDumpStats(std::optional<uint32_t> maxLayers) const170 TimeStatsHelper::TimeStatsGlobal::generateDumpStats(std::optional<uint32_t> maxLayers) const {
171 std::vector<TimeStatsLayer const*> dumpStats;
172 for (const auto& ele : stats) {
173 dumpStats.push_back(&ele.second);
174 }
175
176 std::sort(dumpStats.begin(), dumpStats.end(),
177 [](TimeStatsHelper::TimeStatsLayer const* l,
178 TimeStatsHelper::TimeStatsLayer const* r) {
179 return l->totalFrames > r->totalFrames;
180 });
181
182 if (maxLayers && (*maxLayers < dumpStats.size())) {
183 dumpStats.resize(*maxLayers);
184 }
185 return dumpStats;
186 }
187
188 } // namespace surfaceflinger
189 } // namespace android
190