blob: 1c910aa6a178fa694044eb5a63bbda4320582284 [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 */
Yiwei Zhang91a86382018-11-15 14:44:40 -080016#include "timestatsproto/TimeStatsHelper.h"
17
Yiwei Zhang0102ad22018-05-02 17:37:17 -070018#include <android-base/stringprintf.h>
Yiwei Zhang91a86382018-11-15 14:44:40 -080019#include <inttypes.h>
Yiwei Zhang0102ad22018-05-02 17:37:17 -070020
21#include <array>
Yiwei Zhang0102ad22018-05-02 17:37:17 -070022
23#define HISTOGRAM_SIZE 85
24
25using android::base::StringAppendF;
26using android::base::StringPrintf;
27
28namespace android {
29namespace surfaceflinger {
30
31// Time buckets for histogram, the calculated time deltas will be lower bounded
32// to the buckets in this array.
33static 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
40void 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]) {
Yiwei Zhange5c49d52018-10-29 00:15:31 -070044 hist[histogramConfig[HISTOGRAM_SIZE - 1]] += delta / histogramConfig[HISTOGRAM_SIZE - 1];
Yiwei Zhang0102ad22018-05-02 17:37:17 -070045 return;
46 }
47 auto iter = std::lower_bound(histogramConfig.begin(), histogramConfig.end(), delta);
48 hist[*iter]++;
49}
50
Yiwei Zhang91a86382018-11-15 14:44:40 -080051int64_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
Yiwei Zhang8a4015c2018-05-08 16:03:47 -070059float TimeStatsHelper::Histogram::averageTime() const {
Yiwei Zhang0102ad22018-05-02 17:37:17 -070060 int64_t ret = 0;
61 int64_t count = 0;
Yiwei Zhang3a226d22018-10-16 09:23:03 -070062 for (const auto& ele : hist) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -070063 count += ele.second;
64 ret += ele.first * ele.second;
65 }
66 return static_cast<float>(ret) / count;
67}
68
Yiwei Zhang8a4015c2018-05-08 16:03:47 -070069std::string TimeStatsHelper::Histogram::toString() const {
Yiwei Zhang0102ad22018-05-02 17:37:17 -070070 std::string result;
71 for (int32_t i = 0; i < HISTOGRAM_SIZE; ++i) {
72 int32_t bucket = histogramConfig[i];
Yiwei Zhang8a4015c2018-05-08 16:03:47 -070073 int32_t count = (hist.count(bucket) == 0) ? 0 : hist.at(bucket);
Yiwei Zhang0102ad22018-05-02 17:37:17 -070074 StringAppendF(&result, "%dms=%d ", bucket, count);
75 }
76 result.back() = '\n';
77 return result;
78}
79
Yiwei Zhang8a4015c2018-05-08 16:03:47 -070080std::string TimeStatsHelper::TimeStatsLayer::toString() const {
Yiwei Zhangeaeea062018-06-28 14:46:51 -070081 std::string result = "\n";
Yiwei Zhang0102ad22018-05-02 17:37:17 -070082 StringAppendF(&result, "layerName = %s\n", layerName.c_str());
Yiwei Zhang0102ad22018-05-02 17:37:17 -070083 StringAppendF(&result, "packageName = %s\n", packageName.c_str());
Yiwei Zhangeaeea062018-06-28 14:46:51 -070084 StringAppendF(&result, "totalFrames = %d\n", totalFrames);
85 StringAppendF(&result, "droppedFrames = %d\n", droppedFrames);
Alec Mouri91f6df32020-01-30 08:48:58 -080086 StringAppendF(&result, "lateAcquireFrames = %d\n", lateAcquireFrames);
87 StringAppendF(&result, "badDesiredPresentFrames = %d\n", badDesiredPresentFrames);
Yiwei Zhang3a226d22018-10-16 09:23:03 -070088 const auto iter = deltas.find("present2present");
Yiwei Zhang8a4015c2018-05-08 16:03:47 -070089 if (iter != deltas.end()) {
90 StringAppendF(&result, "averageFPS = %.3f\n", 1000.0 / iter->second.averageTime());
Yiwei Zhang0102ad22018-05-02 17:37:17 -070091 }
Yiwei Zhang3a226d22018-10-16 09:23:03 -070092 for (const auto& ele : deltas) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -070093 StringAppendF(&result, "%s histogram is as below:\n", ele.first.c_str());
Yiwei Zhang3a226d22018-10-16 09:23:03 -070094 result.append(ele.second.toString());
Yiwei Zhang0102ad22018-05-02 17:37:17 -070095 }
96
97 return result;
98}
99
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700100std::string TimeStatsHelper::TimeStatsGlobal::toString(std::optional<uint32_t> maxLayers) const {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700101 std::string result = "SurfaceFlinger TimeStats:\n";
Yiwei Zhang91a86382018-11-15 14:44:40 -0800102 StringAppendF(&result, "statsStart = %" PRId64 "\n", statsStart);
103 StringAppendF(&result, "statsEnd = %" PRId64 "\n", statsEnd);
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700104 StringAppendF(&result, "totalFrames = %d\n", totalFrames);
105 StringAppendF(&result, "missedFrames = %d\n", missedFrames);
106 StringAppendF(&result, "clientCompositionFrames = %d\n", clientCompositionFrames);
Vishnu Nair9b079a22020-01-21 14:36:08 -0800107 StringAppendF(&result, "clientCompositionReusedFrames = %d\n", clientCompositionReusedFrames);
Yiwei Zhang91a86382018-11-15 14:44:40 -0800108 StringAppendF(&result, "displayOnTime = %" PRId64 " ms\n", displayOnTime);
Alec Mourifb571ea2019-01-24 18:42:10 -0800109 StringAppendF(&result, "displayConfigStats is as below:\n");
110 for (const auto& [fps, duration] : refreshRateStats) {
111 StringAppendF(&result, "%dfps=%ldms ", fps, ns2ms(duration));
112 }
113 result.back() = '\n';
Yiwei Zhang91a86382018-11-15 14:44:40 -0800114 StringAppendF(&result, "totalP2PTime = %" PRId64 " ms\n", presentToPresent.totalTime());
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700115 StringAppendF(&result, "presentToPresent histogram is as below:\n");
116 result.append(presentToPresent.toString());
Alec Mouri9519bf12019-11-15 16:54:44 -0800117 StringAppendF(&result, "frameDuration histogram is as below:\n");
118 result.append(frameDuration.toString());
Alec Mourie4034bb2019-11-19 12:45:54 -0800119 StringAppendF(&result, "renderEngineTiming histogram is as below:\n");
120 result.append(renderEngineTiming.toString());
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700121 const auto dumpStats = generateDumpStats(maxLayers);
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700122 for (const auto& ele : dumpStats) {
123 result.append(ele->toString());
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700124 }
125
126 return result;
127}
128
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700129SFTimeStatsLayerProto TimeStatsHelper::TimeStatsLayer::toProto() const {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700130 SFTimeStatsLayerProto layerProto;
131 layerProto.set_layer_name(layerName);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700132 layerProto.set_package_name(packageName);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700133 layerProto.set_total_frames(totalFrames);
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700134 layerProto.set_dropped_frames(droppedFrames);
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700135 for (const auto& ele : deltas) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700136 SFTimeStatsDeltaProto* deltaProto = layerProto.add_deltas();
137 deltaProto->set_delta_name(ele.first);
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700138 for (const auto& histEle : ele.second.hist) {
Yiwei Zhang3bef3952018-05-04 14:08:01 -0700139 SFTimeStatsHistogramBucketProto* histProto = deltaProto->add_histograms();
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700140 histProto->set_time_millis(histEle.first);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700141 histProto->set_frame_count(histEle.second);
142 }
143 }
144 return layerProto;
145}
146
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700147SFTimeStatsGlobalProto TimeStatsHelper::TimeStatsGlobal::toProto(
148 std::optional<uint32_t> maxLayers) const {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700149 SFTimeStatsGlobalProto globalProto;
150 globalProto.set_stats_start(statsStart);
151 globalProto.set_stats_end(statsEnd);
152 globalProto.set_total_frames(totalFrames);
153 globalProto.set_missed_frames(missedFrames);
154 globalProto.set_client_composition_frames(clientCompositionFrames);
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700155 globalProto.set_display_on_time(displayOnTime);
Alec Mourifb571ea2019-01-24 18:42:10 -0800156 for (const auto& ele : refreshRateStats) {
157 SFTimeStatsDisplayConfigBucketProto* configBucketProto =
158 globalProto.add_display_config_stats();
159 SFTimeStatsDisplayConfigProto* configProto = configBucketProto->mutable_config();
160 configProto->set_fps(ele.first);
161 configBucketProto->set_duration_millis(ns2ms(ele.second));
162 }
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700163 for (const auto& histEle : presentToPresent.hist) {
164 SFTimeStatsHistogramBucketProto* histProto = globalProto.add_present_to_present();
165 histProto->set_time_millis(histEle.first);
166 histProto->set_frame_count(histEle.second);
167 }
Alec Mouri9519bf12019-11-15 16:54:44 -0800168 for (const auto& histEle : frameDuration.hist) {
169 SFTimeStatsHistogramBucketProto* histProto = globalProto.add_frame_duration();
170 histProto->set_time_millis(histEle.first);
171 histProto->set_frame_count(histEle.second);
172 }
Alec Mourie4034bb2019-11-19 12:45:54 -0800173 for (const auto& histEle : renderEngineTiming.hist) {
174 SFTimeStatsHistogramBucketProto* histProto = globalProto.add_render_engine_timing();
175 histProto->set_time_millis(histEle.first);
176 histProto->set_frame_count(histEle.second);
177 }
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700178 const auto dumpStats = generateDumpStats(maxLayers);
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700179 for (const auto& ele : dumpStats) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700180 SFTimeStatsLayerProto* layerProto = globalProto.add_stats();
181 layerProto->CopyFrom(ele->toProto());
182 }
183 return globalProto;
184}
185
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700186std::vector<TimeStatsHelper::TimeStatsLayer const*>
187TimeStatsHelper::TimeStatsGlobal::generateDumpStats(std::optional<uint32_t> maxLayers) const {
188 std::vector<TimeStatsLayer const*> dumpStats;
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700189 for (const auto& ele : stats) {
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700190 dumpStats.push_back(&ele.second);
191 }
192
193 std::sort(dumpStats.begin(), dumpStats.end(),
194 [](TimeStatsHelper::TimeStatsLayer const* l,
195 TimeStatsHelper::TimeStatsLayer const* r) {
196 return l->totalFrames > r->totalFrames;
197 });
198
199 if (maxLayers && (*maxLayers < dumpStats.size())) {
200 dumpStats.resize(*maxLayers);
201 }
202 return dumpStats;
203}
204
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700205} // namespace surfaceflinger
206} // namespace android