1 /*
2  * Copyright (C) 2018 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 #pragma once
17 
18 #include <timestatsproto/TimeStatsProtoHeader.h>
19 #include <utils/Timers.h>
20 
21 #include <optional>
22 #include <string>
23 #include <unordered_map>
24 #include <vector>
25 
26 namespace android {
27 namespace surfaceflinger {
28 
29 class TimeStatsHelper {
30 public:
31     class Histogram {
32     public:
33         // Key is the delta time between timestamps
34         // Value is the number of appearances of that delta
35         std::unordered_map<int32_t, int32_t> hist;
36 
37         void insert(int32_t delta);
38         int64_t totalTime() const;
39         float averageTime() const;
40         std::string toString() const;
41     };
42 
43     class TimeStatsLayer {
44     public:
45         std::string layerName;
46         std::string packageName;
47         int32_t totalFrames = 0;
48         int32_t droppedFrames = 0;
49         int32_t lateAcquireFrames = 0;
50         int32_t badDesiredPresentFrames = 0;
51         std::unordered_map<std::string, Histogram> deltas;
52 
53         std::string toString() const;
54         SFTimeStatsLayerProto toProto() const;
55     };
56 
57     class TimeStatsGlobal {
58     public:
59         int64_t statsStart = 0;
60         int64_t statsEnd = 0;
61         int32_t totalFrames = 0;
62         int32_t missedFrames = 0;
63         int32_t clientCompositionFrames = 0;
64         int32_t clientCompositionReusedFrames = 0;
65         int32_t refreshRateSwitches = 0;
66         int32_t compositionStrategyChanges = 0;
67         int32_t displayEventConnectionsCount = 0;
68         int64_t displayOnTime = 0;
69         Histogram presentToPresent;
70         Histogram frameDuration;
71         Histogram renderEngineTiming;
72         std::unordered_map<std::string, TimeStatsLayer> stats;
73         std::unordered_map<uint32_t, nsecs_t> refreshRateStats;
74 
75         std::string toString(std::optional<uint32_t> maxLayers) const;
76         SFTimeStatsGlobalProto toProto(std::optional<uint32_t> maxLayers) const;
77 
78     private:
79         std::vector<TimeStatsLayer const*> generateDumpStats(
80                 std::optional<uint32_t> maxLayers) const;
81     };
82 };
83 
84 } // namespace surfaceflinger
85 } // namespace android
86