• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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/string_view.h>
23 #include <base/containers/unordered_map.h>
24 #include <base/util/uid.h>
25 #include <core/namespace.h>
26 #include <core/plugin/intf_interface.h>
27 
CORE_BEGIN_NAMESPACE()28 CORE_BEGIN_NAMESPACE()
29 /** PerformanceDataManager for timings.
30  * Internally synchronized.
31  */
32 class IPerformanceDataManager : public IInterface {
33 public:
34     using Ptr = BASE_NS::refcnt_ptr<IPerformanceDataManager>;
35     static constexpr auto UID = BASE_NS::Uid { "9ce7d517-4b7d-400b-bdc5-f449d93479d8" };
36 
37     static constexpr uint32_t TIMING_DATA_POOL_SIZE { 64u };
38     static constexpr uint32_t TIMING_DATA_NAME_LENGTH { 64u };
39 
40     struct PerformanceTimingData {
41         /** Latest value. */
42         int64_t currentTime { 0 };
43         /** Largest value. */
44         int64_t maxTime { 0 };
45         /** Smallest value. */
46         int64_t minTime { 0 };
47         /** Average of the last TIMING_DATA_POOL_SIZE values. */
48         int64_t averageTime { 0 };
49         /** Sum of the valus. */
50         int64_t totalTime { 0 };
51         /** Max sum of total time values. (Might be used with negative values) */
52         int64_t maxTotalTime { 0 };
53         /** Number of times the data has been updated. */
54         int64_t counter { 0 };
55         /** Array containing the last TIMING_DATA_POOL_SIZE values. */
56         int64_t timings[TIMING_DATA_POOL_SIZE] { 0 };
57     };
58 
59     using NameToPerformanceMap =
60         BASE_NS::unordered_map<BASE_NS::fixed_string<TIMING_DATA_NAME_LENGTH>, PerformanceTimingData>;
61 
62     struct PerformanceData {
63         /** Name of the subcategory. */
64         BASE_NS::fixed_string<TIMING_DATA_NAME_LENGTH> subCategory;
65         /** Data so far gathered for the subcatagory. */
66         NameToPerformanceMap timings;
67     };
68 
69     IPerformanceDataManager(const IPerformanceDataManager&) = delete;
70     IPerformanceDataManager& operator=(const IPerformanceDataManager&) = delete;
71 
72     /** Returns the category to which this performance data belongs to.
73      * @return Name of the category given when calling IPerformanceDataManagerFactory::Get.
74      */
75     virtual BASE_NS::string_view GetCategory() const = 0;
76 
77     using TimerHandle = int64_t;
78 
79     /** Starts measuring time.
80      * @return Handle which is used to stop the measurement.
81      */
82     virtual TimerHandle BeginTimer() = 0;
83 
84     /** Stops measuring time.
85      * @param handle A handle previously returned from BeginTimer.
86      * @return Time elapsed between Begin/EndTimer calls in microseconds.
87      */
88     virtual int64_t EndTimer(TimerHandle handle) = 0;
89 
90     /** Updates performance timing data.
91      * @param subCategory Name of the subcategory.
92      * @param name Name of the data entry.
93      * @param microSeconds Time in microseconds.
94      */
95     virtual void UpdateData(
96         const BASE_NS::string_view subCategory, const BASE_NS::string_view name, const int64_t microSeconds) = 0;
97 
98     /** Resets all performance data gathered for this category. */
99     virtual void ResetData() = 0;
100 
101     /** Remove subcategory data.
102      * @param subCategory A sub category to be removed.
103      */
104     virtual void RemoveData(const BASE_NS::string_view subCategory) = 0;
105 
106     /** Returns the performance data gathered for this category.
107      * @return A list contains a name-timings lookuptable for each subcategory.
108      */
109     virtual BASE_NS::vector<PerformanceData> GetData() const = 0;
110 
111 protected:
112     IPerformanceDataManager() = default;
113     virtual ~IPerformanceDataManager() = default;
114 };
115 
GetName(const IPerformanceDataManager *)116 inline constexpr BASE_NS::string_view GetName(const IPerformanceDataManager*)
117 {
118     return "IPerformanceDataManager";
119 }
120 
121 class IPerformanceDataManagerFactory : public IInterface {
122 public:
123     static constexpr auto UID = BASE_NS::Uid { "58d60acd-a2d2-4f76-a9bb-5cf0a82ccd4f" };
124 
125     /** Get a performance data manager instance for a named category. The category can be any freeformed string.
126      * @param category Name of the category.
127      * @return Pointer to the instance grouping statistics for the given category.
128      */
129     virtual IPerformanceDataManager* Get(const BASE_NS::string_view category) = 0;
130 
131     /** Get performance data managers for all categories.
132      * @return Array of instances.
133      */
134     virtual BASE_NS::vector<IPerformanceDataManager*> GetAllCategories() const = 0;
135 };
136 
GetName(const IPerformanceDataManagerFactory *)137 inline constexpr BASE_NS::string_view GetName(const IPerformanceDataManagerFactory*)
138 {
139     return "IPerformanceDataManagerFactory";
140 }
141 CORE_END_NAMESPACE()
142 
143 #endif // API_CORE_PERF_INTF_PERFORMANCE_DATA_MANAGER_H
144