blob: 21f3ef3d7e6d2f1412a512510871768c00d0e3b2 [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 */
16#include <android-base/stringprintf.h>
17#include <timestatsproto/TimeStatsHelper.h>
18
19#include <array>
Yiwei Zhang0102ad22018-05-02 17:37:17 -070020
21#define HISTOGRAM_SIZE 85
22
23using android::base::StringAppendF;
24using android::base::StringPrintf;
25
26namespace android {
27namespace surfaceflinger {
28
29// Time buckets for histogram, the calculated time deltas will be lower bounded
30// to the buckets in this array.
31static const std::array<int32_t, HISTOGRAM_SIZE> histogramConfig =
32 {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
33 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33,
34 34, 36, 38, 40, 42, 44, 46, 48, 50, 54, 58, 62, 66, 70, 74, 78, 82,
35 86, 90, 94, 98, 102, 106, 110, 114, 118, 122, 126, 130, 134, 138, 142, 146, 150,
36 200, 250, 300, 350, 400, 450, 500, 550, 600, 650, 700, 750, 800, 850, 900, 950, 1000};
37
38void TimeStatsHelper::Histogram::insert(int32_t delta) {
39 if (delta < 0) return;
40 // std::lower_bound won't work on out of range values
41 if (delta > histogramConfig[HISTOGRAM_SIZE - 1]) {
42 hist[histogramConfig[HISTOGRAM_SIZE - 1]]++;
43 return;
44 }
45 auto iter = std::lower_bound(histogramConfig.begin(), histogramConfig.end(), delta);
46 hist[*iter]++;
47}
48
Yiwei Zhang8a4015c2018-05-08 16:03:47 -070049float TimeStatsHelper::Histogram::averageTime() const {
Yiwei Zhang0102ad22018-05-02 17:37:17 -070050 int64_t ret = 0;
51 int64_t count = 0;
Yiwei Zhang8a4015c2018-05-08 16:03:47 -070052 for (auto& ele : hist) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -070053 count += ele.second;
54 ret += ele.first * ele.second;
55 }
56 return static_cast<float>(ret) / count;
57}
58
Yiwei Zhang8a4015c2018-05-08 16:03:47 -070059std::string TimeStatsHelper::Histogram::toString() const {
Yiwei Zhang0102ad22018-05-02 17:37:17 -070060 std::string result;
61 for (int32_t i = 0; i < HISTOGRAM_SIZE; ++i) {
62 int32_t bucket = histogramConfig[i];
Yiwei Zhang8a4015c2018-05-08 16:03:47 -070063 int32_t count = (hist.count(bucket) == 0) ? 0 : hist.at(bucket);
Yiwei Zhang0102ad22018-05-02 17:37:17 -070064 StringAppendF(&result, "%dms=%d ", bucket, count);
65 }
66 result.back() = '\n';
67 return result;
68}
69
Yiwei Zhang8a4015c2018-05-08 16:03:47 -070070std::string TimeStatsHelper::TimeStatsLayer::toString() const {
Yiwei Zhang0102ad22018-05-02 17:37:17 -070071 std::string result = "";
72 StringAppendF(&result, "layerName = %s\n", layerName.c_str());
Yiwei Zhang0102ad22018-05-02 17:37:17 -070073 StringAppendF(&result, "packageName = %s\n", packageName.c_str());
74 StringAppendF(&result, "statsStart = %lld\n", static_cast<long long int>(statsStart));
75 StringAppendF(&result, "statsEnd = %lld\n", static_cast<long long int>(statsEnd));
76 StringAppendF(&result, "totalFrames= %d\n", totalFrames);
Yiwei Zhang8a4015c2018-05-08 16:03:47 -070077 auto iter = deltas.find("present2present");
78 if (iter != deltas.end()) {
79 StringAppendF(&result, "averageFPS = %.3f\n", 1000.0 / iter->second.averageTime());
Yiwei Zhang0102ad22018-05-02 17:37:17 -070080 }
Yiwei Zhang8a4015c2018-05-08 16:03:47 -070081 for (auto& ele : deltas) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -070082 StringAppendF(&result, "%s histogram is as below:\n", ele.first.c_str());
83 StringAppendF(&result, "%s", ele.second.toString().c_str());
84 }
85
86 return result;
87}
88
Yiwei Zhang8a4015c2018-05-08 16:03:47 -070089std::string TimeStatsHelper::TimeStatsGlobal::toString(std::optional<uint32_t> maxLayers) const {
Yiwei Zhang0102ad22018-05-02 17:37:17 -070090 std::string result = "SurfaceFlinger TimeStats:\n";
91 StringAppendF(&result, "statsStart = %lld\n", static_cast<long long int>(statsStart));
92 StringAppendF(&result, "statsEnd = %lld\n", static_cast<long long int>(statsEnd));
93 StringAppendF(&result, "totalFrames= %d\n", totalFrames);
94 StringAppendF(&result, "missedFrames= %d\n", missedFrames);
95 StringAppendF(&result, "clientCompositionFrames= %d\n", clientCompositionFrames);
96 StringAppendF(&result, "TimeStats for each layer is as below:\n");
Yiwei Zhang8a4015c2018-05-08 16:03:47 -070097 const auto dumpStats = generateDumpStats(maxLayers);
98 for (auto& ele : dumpStats) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -070099 StringAppendF(&result, "%s", ele->toString().c_str());
100 }
101
102 return result;
103}
104
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700105SFTimeStatsLayerProto TimeStatsHelper::TimeStatsLayer::toProto() const {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700106 SFTimeStatsLayerProto layerProto;
107 layerProto.set_layer_name(layerName);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700108 layerProto.set_package_name(packageName);
109 layerProto.set_stats_start(statsStart);
110 layerProto.set_stats_end(statsEnd);
111 layerProto.set_total_frames(totalFrames);
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700112 for (auto& ele : deltas) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700113 SFTimeStatsDeltaProto* deltaProto = layerProto.add_deltas();
114 deltaProto->set_delta_name(ele.first);
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700115 for (auto& histEle : ele.second.hist) {
Yiwei Zhang3bef3952018-05-04 14:08:01 -0700116 SFTimeStatsHistogramBucketProto* histProto = deltaProto->add_histograms();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700117 histProto->set_render_millis(histEle.first);
118 histProto->set_frame_count(histEle.second);
119 }
120 }
121 return layerProto;
122}
123
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700124SFTimeStatsGlobalProto TimeStatsHelper::TimeStatsGlobal::toProto(
125 std::optional<uint32_t> maxLayers) const {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700126 SFTimeStatsGlobalProto globalProto;
127 globalProto.set_stats_start(statsStart);
128 globalProto.set_stats_end(statsEnd);
129 globalProto.set_total_frames(totalFrames);
130 globalProto.set_missed_frames(missedFrames);
131 globalProto.set_client_composition_frames(clientCompositionFrames);
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700132 const auto dumpStats = generateDumpStats(maxLayers);
133 for (auto& ele : dumpStats) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700134 SFTimeStatsLayerProto* layerProto = globalProto.add_stats();
135 layerProto->CopyFrom(ele->toProto());
136 }
137 return globalProto;
138}
139
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700140std::vector<TimeStatsHelper::TimeStatsLayer const*>
141TimeStatsHelper::TimeStatsGlobal::generateDumpStats(std::optional<uint32_t> maxLayers) const {
142 std::vector<TimeStatsLayer const*> dumpStats;
143 for (auto& ele : stats) {
144 dumpStats.push_back(&ele.second);
145 }
146
147 std::sort(dumpStats.begin(), dumpStats.end(),
148 [](TimeStatsHelper::TimeStatsLayer const* l,
149 TimeStatsHelper::TimeStatsLayer const* r) {
150 return l->totalFrames > r->totalFrames;
151 });
152
153 if (maxLayers && (*maxLayers < dumpStats.size())) {
154 dumpStats.resize(*maxLayers);
155 }
156 return dumpStats;
157}
158
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700159} // namespace surfaceflinger
160} // namespace android