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         std::unordered_map<std::string, Histogram> deltas;
50 
51         std::string toString() const;
52         SFTimeStatsLayerProto toProto() const;
53     };
54 
55     class TimeStatsGlobal {
56     public:
57         int64_t statsStart = 0;
58         int64_t statsEnd = 0;
59         int32_t totalFrames = 0;
60         int32_t missedFrames = 0;
61         int32_t clientCompositionFrames = 0;
62         int64_t displayOnTime = 0;
63         Histogram presentToPresent;
64         std::unordered_map<std::string, TimeStatsLayer> stats;
65         std::unordered_map<uint32_t, nsecs_t> refreshRateStats;
66 
67         std::string toString(std::optional<uint32_t> maxLayers) const;
68         SFTimeStatsGlobalProto toProto(std::optional<uint32_t> maxLayers) const;
69 
70     private:
71         std::vector<TimeStatsLayer const*> generateDumpStats(
72                 std::optional<uint32_t> maxLayers) const;
73     };
74 };
75 
76 } // namespace surfaceflinger
77 } // namespace android
78