• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef API_CORE_PERF_INTF_PERFORMANCE_DATA_MANAGER_H
17 #define API_CORE_PERF_INTF_PERFORMANCE_DATA_MANAGER_H
18 
19 #include <cstdint>
20 
21 #include <base/containers/fixed_string.h>
22 #include <base/containers/refcnt_ptr.h>
23 #include <base/containers/string_view.h>
24 #include <base/containers/unordered_map.h>
25 #include <base/containers/vector.h>
26 #include <base/namespace.h>
27 #include <base/util/uid.h>
28 #include <core/namespace.h>
29 #include <core/plugin/intf_interface.h>
30 
31 CORE_BEGIN_NAMESPACE()
32 /** PerformanceDataManager for timings.
33  * Internally synchronized.
34  */
35 class IPerformanceTrace;
36 class IPerformanceDataManager : public IInterface {
37 public:
38     using Ptr = BASE_NS::refcnt_ptr<IPerformanceDataManager>;
39     static constexpr auto UID = BASE_NS::Uid { "aedc8e81-10ce-4c77-8fde-c238100a36ec" };
40 
41     static constexpr uint32_t TIMING_DATA_POOL_SIZE { 64u };
42     static constexpr uint32_t TIMING_DATA_NAME_LENGTH { 64u };
43 
44     struct PerformanceTimingData {
45         enum class DataType : uint8_t {
46             /** Time in microseconds. */
47             MICROSECONDS,
48             /** Bytes, e.g. memory. */
49             BYTES,
50             /** Number of items, e.g. triangle count. */
51             COUNT,
52         };
53 
54         /** Latest value. */
55         int64_t currentTime { 0 };
56         /** Largest value. */
57         int64_t maxTime { 0 };
58         /** Smallest value. */
59         int64_t minTime { 0 };
60         /** Average of the last TIMING_DATA_POOL_SIZE values. */
61         int64_t averageTime { 0 };
62         /** Sum of the valus. */
63         int64_t totalTime { 0 };
64         /** Max sum of total time values. (Might be used with negative values) */
65         int64_t maxTotalTime { 0 };
66         /** Number of times the data has been updated. */
67         int64_t counter { 0 };
68         /** Array containing the last TIMING_DATA_POOL_SIZE values. */
69         int64_t timings[TIMING_DATA_POOL_SIZE] { 0 };
70         /** Array containing the last TIMING_DATA_POOL_SIZE average values. */
71         int64_t averageTimings[TIMING_DATA_POOL_SIZE] { 0 };
72         /** Type of the values. */
73         DataType type { DataType::MICROSECONDS };
74     };
75 
76     using NameToPerformanceMap =
77         BASE_NS::unordered_map<BASE_NS::fixed_string<TIMING_DATA_NAME_LENGTH>, PerformanceTimingData>;
78 
79     struct PerformanceData {
80         /** Name of the subcategory. */
81         BASE_NS::fixed_string<TIMING_DATA_NAME_LENGTH> subCategory;
82         /** Data so far gathered for the subcatagory. */
83         NameToPerformanceMap timings;
84     };
85 
86     IPerformanceDataManager(const IPerformanceDataManager&) = delete;
87     IPerformanceDataManager& operator=(const IPerformanceDataManager&) = delete;
88 
89     /** Returns the category to which this performance data belongs to.
90      * @return Name of the category given when calling IPerformanceDataManagerFactory::Get.
91      */
92     virtual BASE_NS::string_view GetCategory() const = 0;
93 
94     using TimerHandle = int64_t;
95 
96     /** Starts measuring time.
97      * @return Handle which is used to stop the measurement.
98      */
99     virtual TimerHandle BeginTimer() = 0;
100 
101     /** Stops measuring time.
102      * @param handle A handle previously returned from BeginTimer.
103      * @return Time elapsed between Begin/EndTimer calls in microseconds.
104      */
105     virtual int64_t EndTimer(TimerHandle handle) = 0;
106 
107     /** Updates performance timing data.
108      * @param subCategory Name of the subcategory.
109      * @param name Name of the data entry.
110      * @param microSeconds Time in microseconds.
111      */
112     virtual void UpdateData(
113         const BASE_NS::string_view subCategory, const BASE_NS::string_view name, const int64_t microSeconds) = 0;
114 
115     /** Updates performance timing data.
116      * @param subCategory Name of the subcategory.
117      * @param name Name of the data entry.
118      * @param value Current value.
119      * @param type Type of the current value.
120      */
121     virtual void UpdateData(const BASE_NS::string_view subCategory, const BASE_NS::string_view name,
122         const int64_t value, PerformanceTimingData::DataType type) = 0;
123 
124     /** Resets all performance data gathered for this category. */
125     virtual void ResetData() = 0;
126 
127     /** Remove subcategory data.
128      * @param subCategory A sub category to be removed.
129      */
130     virtual void RemoveData(const BASE_NS::string_view subCategory) = 0;
131 
132     /** Returns the performance data gathered for this category.
133      * @return A list contains a name-timings lookuptable for each subcategory.
134      */
135     virtual BASE_NS::vector<PerformanceData> GetData() const = 0;
136 
137     /***/
138     struct Event {
139         /***/
140         const char* name;
141         /** Funtion name. */
142         const char* function;
143         /** Source file. */
144         const char* file;
145         /** Line number in source file. */
146         uint32_t line;
147         /** Event color. 0xRRGGBB */
148         uint32_t color;
149     };
150 
151 protected:
152     IPerformanceDataManager() = default;
153     virtual ~IPerformanceDataManager() = default;
154 };
155 
GetName(const IPerformanceDataManager *)156 inline constexpr BASE_NS::string_view GetName(const IPerformanceDataManager*)
157 {
158     return "IPerformanceDataManager";
159 }
160 
161 class IPerformanceDataManagerFactory : public IInterface {
162 public:
163     static constexpr auto UID = BASE_NS::Uid { "58d60acd-a2d2-4f76-a9bb-5cf0a82ccd4f" };
164 
165     /** Get a performance data manager instance for a named category. The category can be any freeformed string.
166      * @param category Name of the category.
167      * @return Pointer to the instance grouping statistics for the given category.
168      */
169     virtual IPerformanceDataManager* Get(const BASE_NS::string_view category) = 0;
170 
171     /** Get performance data managers for all categories.
172      * @return Array of instances.
173      */
174     virtual BASE_NS::vector<IPerformanceDataManager*> GetAllCategories() const = 0;
175 
176     virtual IPerformanceTrace* GetFirstPerformanceTrace() const = 0;
177 };
178 
GetName(const IPerformanceDataManagerFactory *)179 inline constexpr BASE_NS::string_view GetName(const IPerformanceDataManagerFactory*)
180 {
181     return "IPerformanceDataManagerFactory";
182 }
183 CORE_END_NAMESPACE()
184 
185 #endif // API_CORE_PERF_INTF_PERFORMANCE_DATA_MANAGER_H
186