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 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 UpdateData(const BASE_NS::string_view subCategory, const BASE_NS::string_view name, const int64_t value, 60 PerformanceTimingData::DataType type) override; 61 62 void ResetData() override; 63 BASE_NS::vector<PerformanceData> GetData() const override; 64 65 void DumpToLog() const; 66 67 void RemoveData(const BASE_NS::string_view subCategory) override; 68 69 // IInterface 70 const IInterface* GetInterface(const BASE_NS::Uid& uid) const override; 71 IInterface* GetInterface(const BASE_NS::Uid& uid) override; 72 void Ref() override; 73 void Unref() override; 74 75 private: 76 const BASE_NS::string category_; 77 PerformanceDataManagerFactory& factory_; 78 #if (CORE_PERF_ENABLED == 1) 79 mutable std::mutex dataMutex_; 80 81 TypeDataSet data_; 82 #endif 83 }; 84 85 class PerformanceTraceLogger final : public ILogger::IOutput { 86 PerformanceDataManagerFactory* factory_; 87 void Write(ILogger::LogLevel logLevel, BASE_NS::string_view filename, int lineNumber, 88 BASE_NS::string_view message) override; 89 90 private: 91 friend class PerformanceDataManagerFactory; PerformanceTraceLogger(PerformanceDataManagerFactory * factory)92 explicit PerformanceTraceLogger(PerformanceDataManagerFactory* factory) : factory_(factory) {} 93 ~PerformanceTraceLogger() override = default; Destroy()94 void Destroy() override 95 { 96 delete this; 97 } 98 }; 99 100 class PerformanceDataManagerFactory final : public IPerformanceDataManagerFactory { 101 public: 102 explicit PerformanceDataManagerFactory(IPluginRegister& registry); 103 ~PerformanceDataManagerFactory() override; 104 105 IPerformanceDataManager* Get(const BASE_NS::string_view category) override; 106 BASE_NS::vector<IPerformanceDataManager*> GetAllCategories() const override; 107 108 // IInterface 109 const IInterface* GetInterface(const BASE_NS::Uid& uid) const override; 110 IInterface* GetInterface(const BASE_NS::Uid& uid) override; 111 void Ref() override; 112 void Unref() override; 113 struct RegisteredPerformanceTrace { 114 BASE_NS::Uid uid; 115 IPerformanceTrace::Ptr instance; 116 }; 117 118 BASE_NS::array_view<const RegisteredPerformanceTrace> GetPerformanceTraces() const; 119 IPerformanceTrace* GetFirstPerformanceTrace() const override; 120 void SetPerformanceTrace(const BASE_NS::Uid& uid, IPerformanceTrace::Ptr&& trace); 121 void RemovePerformanceTrace(const BASE_NS::Uid& uid); 122 ILogger::IOutput::Ptr GetLogger(); 123 124 private: 125 #if (CORE_PERF_ENABLED == 1) 126 mutable std::mutex mutex_; 127 BASE_NS::unordered_map<BASE_NS::string, BASE_NS::unique_ptr<PerformanceDataManager>> managers_; 128 #endif 129 BASE_NS::vector<RegisteredPerformanceTrace> perfTraces_; 130 }; 131 132 IPerformanceTrace::Ptr CreatePerfTraceTracy(PluginToken); 133 CORE_END_NAMESPACE() 134 135 #endif // CORE_UTIL_PERFORMANCE_DATA_MANAGER_H 136