blob: c70122434e1982e95216f61a3198f957fd9967b4 [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 Zhang3a226d22018-10-16 09:23:03 -070052 for (const 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 Zhangeaeea062018-06-28 14:46:51 -070071 std::string result = "\n";
Yiwei Zhang0102ad22018-05-02 17:37:17 -070072 StringAppendF(&result, "layerName = %s\n", layerName.c_str());
Yiwei Zhang0102ad22018-05-02 17:37:17 -070073 StringAppendF(&result, "packageName = %s\n", packageName.c_str());
Yiwei Zhangeaeea062018-06-28 14:46:51 -070074 StringAppendF(&result, "totalFrames = %d\n", totalFrames);
75 StringAppendF(&result, "droppedFrames = %d\n", droppedFrames);
Yiwei Zhang3a226d22018-10-16 09:23:03 -070076 const auto iter = deltas.find("present2present");
Yiwei Zhang8a4015c2018-05-08 16:03:47 -070077 if (iter != deltas.end()) {
78 StringAppendF(&result, "averageFPS = %.3f\n", 1000.0 / iter->second.averageTime());
Yiwei Zhang0102ad22018-05-02 17:37:17 -070079 }
Yiwei Zhang3a226d22018-10-16 09:23:03 -070080 for (const auto& ele : deltas) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -070081 StringAppendF(&result, "%s histogram is as below:\n", ele.first.c_str());
Yiwei Zhang3a226d22018-10-16 09:23:03 -070082 result.append(ele.second.toString());
Yiwei Zhang0102ad22018-05-02 17:37:17 -070083 }
84
85 return result;
86}
87
Yiwei Zhang8a4015c2018-05-08 16:03:47 -070088std::string TimeStatsHelper::TimeStatsGlobal::toString(std::optional<uint32_t> maxLayers) const {
Yiwei Zhang0102ad22018-05-02 17:37:17 -070089 std::string result = "SurfaceFlinger TimeStats:\n";
90 StringAppendF(&result, "statsStart = %lld\n", static_cast<long long int>(statsStart));
91 StringAppendF(&result, "statsEnd = %lld\n", static_cast<long long int>(statsEnd));
Yiwei Zhangeaeea062018-06-28 14:46:51 -070092 StringAppendF(&result, "totalFrames = %d\n", totalFrames);
93 StringAppendF(&result, "missedFrames = %d\n", missedFrames);
94 StringAppendF(&result, "clientCompositionFrames = %d\n", clientCompositionFrames);
Yiwei Zhang3a226d22018-10-16 09:23:03 -070095 StringAppendF(&result, "displayOnTime = %lld ms\n", static_cast<long long int>(displayOnTime));
Yiwei Zhangce6ebc02018-10-20 12:42:38 -070096 StringAppendF(&result, "presentToPresent histogram is as below:\n");
97 result.append(presentToPresent.toString());
Yiwei Zhang8a4015c2018-05-08 16:03:47 -070098 const auto dumpStats = generateDumpStats(maxLayers);
Yiwei Zhang3a226d22018-10-16 09:23:03 -070099 for (const auto& ele : dumpStats) {
100 result.append(ele->toString());
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700101 }
102
103 return result;
104}
105
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700106SFTimeStatsLayerProto TimeStatsHelper::TimeStatsLayer::toProto() const {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700107 SFTimeStatsLayerProto layerProto;
108 layerProto.set_layer_name(layerName);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700109 layerProto.set_package_name(packageName);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700110 layerProto.set_total_frames(totalFrames);
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700111 layerProto.set_dropped_frames(droppedFrames);
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700112 for (const auto& ele : deltas) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700113 SFTimeStatsDeltaProto* deltaProto = layerProto.add_deltas();
114 deltaProto->set_delta_name(ele.first);
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700115 for (const auto& histEle : ele.second.hist) {
Yiwei Zhang3bef3952018-05-04 14:08:01 -0700116 SFTimeStatsHistogramBucketProto* histProto = deltaProto->add_histograms();
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700117 histProto->set_time_millis(histEle.first);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700118 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 Zhang3a226d22018-10-16 09:23:03 -0700132 globalProto.set_display_on_time(displayOnTime);
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700133 for (const auto& histEle : presentToPresent.hist) {
134 SFTimeStatsHistogramBucketProto* histProto = globalProto.add_present_to_present();
135 histProto->set_time_millis(histEle.first);
136 histProto->set_frame_count(histEle.second);
137 }
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700138 const auto dumpStats = generateDumpStats(maxLayers);
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700139 for (const auto& ele : dumpStats) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700140 SFTimeStatsLayerProto* layerProto = globalProto.add_stats();
141 layerProto->CopyFrom(ele->toProto());
142 }
143 return globalProto;
144}
145
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700146std::vector<TimeStatsHelper::TimeStatsLayer const*>
147TimeStatsHelper::TimeStatsGlobal::generateDumpStats(std::optional<uint32_t> maxLayers) const {
148 std::vector<TimeStatsLayer const*> dumpStats;
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700149 for (const auto& ele : stats) {
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700150 dumpStats.push_back(&ele.second);
151 }
152
153 std::sort(dumpStats.begin(), dumpStats.end(),
154 [](TimeStatsHelper::TimeStatsLayer const* l,
155 TimeStatsHelper::TimeStatsLayer const* r) {
156 return l->totalFrames > r->totalFrames;
157 });
158
159 if (maxLayers && (*maxLayers < dumpStats.size())) {
160 dumpStats.resize(*maxLayers);
161 }
162 return dumpStats;
163}
164
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700165} // namespace surfaceflinger
166} // namespace android