• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 CORE_PERF_PERFORMANCE_DATA_MANAGER_H
17 #define CORE_PERF_PERFORMANCE_DATA_MANAGER_H
18 
19 #include <cstdint>
20 #include <mutex>
21 
22 #include <base/containers/fixed_string.h>
23 #include <base/containers/string.h>
24 #include <base/containers/string_view.h>
25 #include <base/containers/unique_ptr.h>
26 #include <base/containers/unordered_map.h>
27 #include <base/containers/vector.h>
28 #include <base/namespace.h>
29 #include <core/intf_logger.h>
30 #include <core/namespace.h>
31 #include <core/perf/intf_performance_data_manager.h>
32 #include <core/perf/intf_performance_trace.h>
33 #include <core/plugin/intf_plugin_register.h>
34 
35 CORE_BEGIN_NAMESPACE()
36 // if CORE_DEV_ENABLED defined the manager methods are empty
37 class PerformanceDataManagerFactory;
38 /** PerformanceDataManager.
39  * Internally synchronized global singleton for timings.
40  */
41 class PerformanceDataManager final : public IPerformanceDataManager {
42 public:
43     struct InternalData;
44     using TypeDataSet = BASE_NS::unordered_map<BASE_NS::fixed_string<TIMING_DATA_NAME_LENGTH>, InternalData>;
45 
46     ~PerformanceDataManager();
47     PerformanceDataManager(const BASE_NS::string_view category, PerformanceDataManagerFactory& factory);
48 
49     PerformanceDataManager(const PerformanceDataManager&) = delete;
50     PerformanceDataManager& operator=(const PerformanceDataManager&) = delete;
51 
52     BASE_NS::string_view GetCategory() const override;
53 
54     TimerHandle BeginTimer() override;
55     int64_t EndTimer(TimerHandle handle) override;
56 
57     void UpdateData(
58         const BASE_NS::string_view subCategory, const BASE_NS::string_view name, const int64_t microSeconds) override;
59     void ResetData() override;
60     BASE_NS::vector<PerformanceData> GetData() const override;
61 
62     void DumpToLog() const;
63 
64     void RemoveData(const BASE_NS::string_view subCategory) override;
65 
66     // IInterface
67     const IInterface* GetInterface(const BASE_NS::Uid& uid) const override;
68     IInterface* GetInterface(const BASE_NS::Uid& uid) override;
69     void Ref() override;
70     void Unref() override;
71 
72 private:
73     const BASE_NS::string category_;
74     PerformanceDataManagerFactory& factory_;
75 #if (CORE_PERF_ENABLED == 1)
76     mutable std::mutex dataMutex_;
77 
78     TypeDataSet data_;
79 #endif
80 };
81 
82 class PerformanceTraceLogger final : public ILogger::IOutput {
83     PerformanceDataManagerFactory* factory_;
84     void Write(ILogger::LogLevel logLevel, BASE_NS::string_view filename, int lineNumber,
85         BASE_NS::string_view message) override;
86 
87 private:
88     friend class PerformanceDataManagerFactory;
PerformanceTraceLogger(PerformanceDataManagerFactory * factory)89     explicit PerformanceTraceLogger(PerformanceDataManagerFactory* factory) : factory_(factory) {}
90     ~PerformanceTraceLogger() override = default;
Destroy()91     void Destroy() override
92     {
93         delete this;
94     }
95 };
96 
97 class PerformanceDataManagerFactory final : public IPerformanceDataManagerFactory {
98 public:
99     explicit PerformanceDataManagerFactory(IPluginRegister& registry);
100     ~PerformanceDataManagerFactory() override;
101 
102     IPerformanceDataManager* Get(const BASE_NS::string_view category) override;
103     BASE_NS::vector<IPerformanceDataManager*> GetAllCategories() const override;
104 
105     // IInterface
106     const IInterface* GetInterface(const BASE_NS::Uid& uid) const override;
107     IInterface* GetInterface(const BASE_NS::Uid& uid) override;
108     void Ref() override;
109     void Unref() override;
110     struct RegisteredPerformanceTrace {
111         BASE_NS::Uid uid;
112         IPerformanceTrace::Ptr instance;
113     };
114 
115     BASE_NS::array_view<const RegisteredPerformanceTrace> GetPerformanceTraces() const;
116     IPerformanceTrace* GetFirstPerformanceTrace() const override;
117     void SetPerformanceTrace(const BASE_NS::Uid& uid, IPerformanceTrace::Ptr&& trace);
118     void RemovePerformanceTrace(const BASE_NS::Uid& uid);
119     ILogger::IOutput::Ptr GetLogger();
120 
121 private:
122 #if (CORE_PERF_ENABLED == 1)
123     mutable std::mutex mutex_;
124     BASE_NS::unordered_map<BASE_NS::string, BASE_NS::unique_ptr<PerformanceDataManager>> managers_;
125 #endif
126     BASE_NS::vector<RegisteredPerformanceTrace> perfTraces_;
127 };
128 
129 IPerformanceTrace::Ptr CreatePerfTraceTracy(PluginToken);
130 CORE_END_NAMESPACE()
131 
132 #endif // CORE_UTIL_PERFORMANCE_DATA_MANAGER_H
133