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 
20 #include <optional>
21 #include <string>
22 #include <unordered_map>
23 #include <vector>
24 
25 namespace android {
26 namespace surfaceflinger {
27 
28 class TimeStatsHelper {
29 public:
30     class Histogram {
31     public:
32         // Key is the delta time between timestamps
33         // Value is the number of appearances of that delta
34         std::unordered_map<int32_t, int32_t> hist;
35 
36         void insert(int32_t delta);
37         float averageTime() const;
38         std::string toString() const;
39     };
40 
41     class TimeStatsLayer {
42     public:
43         std::string layerName;
44         std::string packageName;
45         int64_t statsStart = 0;
46         int64_t statsEnd = 0;
47         int32_t totalFrames = 0;
48         std::unordered_map<std::string, Histogram> deltas;
49 
50         std::string toString() const;
51         SFTimeStatsLayerProto toProto() const;
52     };
53 
54     class TimeStatsGlobal {
55     public:
56         int64_t statsStart = 0;
57         int64_t statsEnd = 0;
58         int32_t totalFrames = 0;
59         int32_t missedFrames = 0;
60         int32_t clientCompositionFrames = 0;
61         std::unordered_map<std::string, TimeStatsLayer> stats;
62 
63         std::string toString(std::optional<uint32_t> maxLayers) const;
64         SFTimeStatsGlobalProto toProto(std::optional<uint32_t> maxLayers) const;
65 
66     private:
67         std::vector<TimeStatsLayer const*> generateDumpStats(
68                 std::optional<uint32_t> maxLayers) const;
69     };
70 };
71 
72 } // namespace surfaceflinger
73 } // namespace android
74