blob: 3e5007c0b888254aa15d2c3c2779eb16a8b1201b [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>
20#include <regex>
21
22#define HISTOGRAM_SIZE 85
23
24using android::base::StringAppendF;
25using android::base::StringPrintf;
26
27namespace android {
28namespace surfaceflinger {
29
30// Time buckets for histogram, the calculated time deltas will be lower bounded
31// to the buckets in this array.
32static const std::array<int32_t, HISTOGRAM_SIZE> histogramConfig =
33 {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
34 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33,
35 34, 36, 38, 40, 42, 44, 46, 48, 50, 54, 58, 62, 66, 70, 74, 78, 82,
36 86, 90, 94, 98, 102, 106, 110, 114, 118, 122, 126, 130, 134, 138, 142, 146, 150,
37 200, 250, 300, 350, 400, 450, 500, 550, 600, 650, 700, 750, 800, 850, 900, 950, 1000};
38
39void TimeStatsHelper::Histogram::insert(int32_t delta) {
40 if (delta < 0) return;
41 // std::lower_bound won't work on out of range values
42 if (delta > histogramConfig[HISTOGRAM_SIZE - 1]) {
43 hist[histogramConfig[HISTOGRAM_SIZE - 1]]++;
44 return;
45 }
46 auto iter = std::lower_bound(histogramConfig.begin(), histogramConfig.end(), delta);
47 hist[*iter]++;
48}
49
50float TimeStatsHelper::Histogram::averageTime() {
51 int64_t ret = 0;
52 int64_t count = 0;
53 for (auto ele : hist) {
54 count += ele.second;
55 ret += ele.first * ele.second;
56 }
57 return static_cast<float>(ret) / count;
58}
59
60std::string TimeStatsHelper::Histogram::toString() {
61 std::string result;
62 for (int32_t i = 0; i < HISTOGRAM_SIZE; ++i) {
63 int32_t bucket = histogramConfig[i];
64 int32_t count = (hist.count(bucket) == 0) ? 0 : hist[bucket];
65 StringAppendF(&result, "%dms=%d ", bucket, count);
66 }
67 result.back() = '\n';
68 return result;
69}
70
71static std::string getPackageName(const std::string& layerName) {
72 // This regular expression captures the following for instance:
73 // StatusBar in StatusBar#0
74 // com.appname in com.appname/com.appname.activity#0
75 // com.appname in SurfaceView - com.appname/com.appname.activity#0
76 const std::regex re("(?:SurfaceView[-\\s\\t]+)?([^/]+).*#\\d+");
77 std::smatch match;
78 if (std::regex_match(layerName.begin(), layerName.end(), match, re)) {
79 // There must be a match for group 1 otherwise the whole string is not
80 // matched and the above will return false
81 return match[1];
82 }
83 return "";
84}
85
86std::string TimeStatsHelper::TimeStatsLayer::toString() {
87 std::string result = "";
88 StringAppendF(&result, "layerName = %s\n", layerName.c_str());
89 packageName = getPackageName(layerName);
90 StringAppendF(&result, "packageName = %s\n", packageName.c_str());
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 if (deltas.find("present2present") != deltas.end()) {
95 StringAppendF(&result, "averageFPS = %.3f\n",
96 1000.0 / deltas["present2present"].averageTime());
97 }
98 for (auto ele : deltas) {
99 StringAppendF(&result, "%s histogram is as below:\n", ele.first.c_str());
100 StringAppendF(&result, "%s", ele.second.toString().c_str());
101 }
102
103 return result;
104}
105
106std::string TimeStatsHelper::TimeStatsGlobal::toString() {
107 std::string result = "SurfaceFlinger TimeStats:\n";
108 StringAppendF(&result, "statsStart = %lld\n", static_cast<long long int>(statsStart));
109 StringAppendF(&result, "statsEnd = %lld\n", static_cast<long long int>(statsEnd));
110 StringAppendF(&result, "totalFrames= %d\n", totalFrames);
111 StringAppendF(&result, "missedFrames= %d\n", missedFrames);
112 StringAppendF(&result, "clientCompositionFrames= %d\n", clientCompositionFrames);
113 StringAppendF(&result, "TimeStats for each layer is as below:\n");
114 for (auto ele : dumpStats) {
115 StringAppendF(&result, "%s", ele->toString().c_str());
116 }
117
118 return result;
119}
120
121SFTimeStatsLayerProto TimeStatsHelper::TimeStatsLayer::toProto() {
122 SFTimeStatsLayerProto layerProto;
123 layerProto.set_layer_name(layerName);
124 packageName = getPackageName(layerName);
125 layerProto.set_package_name(packageName);
126 layerProto.set_stats_start(statsStart);
127 layerProto.set_stats_end(statsEnd);
128 layerProto.set_total_frames(totalFrames);
129 for (auto ele : deltas) {
130 SFTimeStatsDeltaProto* deltaProto = layerProto.add_deltas();
131 deltaProto->set_delta_name(ele.first);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700132 for (auto histEle : ele.second.hist) {
Yiwei Zhang3bef3952018-05-04 14:08:01 -0700133 SFTimeStatsHistogramBucketProto* histProto = deltaProto->add_histograms();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700134 histProto->set_render_millis(histEle.first);
135 histProto->set_frame_count(histEle.second);
136 }
137 }
138 return layerProto;
139}
140
141SFTimeStatsGlobalProto TimeStatsHelper::TimeStatsGlobal::toProto() {
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 for (auto ele : dumpStats) {
149 SFTimeStatsLayerProto* layerProto = globalProto.add_stats();
150 layerProto->CopyFrom(ele->toProto());
151 }
152 return globalProto;
153}
154
155} // namespace surfaceflinger
156} // namespace android