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 <map> 21 #include <mutex> 22 23 #include <base/containers/fixed_string.h> 24 #include <base/containers/string.h> 25 #include <base/containers/string_view.h> 26 #include <base/containers/unique_ptr.h> 27 #include <base/containers/unordered_map.h> 28 #include <base/containers/vector.h> 29 #include <base/namespace.h> 30 #include <core/namespace.h> 31 #include <core/perf/intf_performance_data_manager.h> 32 CORE_BEGIN_NAMESPACE()33CORE_BEGIN_NAMESPACE() 34 // if CORE_DEV_ENABLED defined the manager methods are empty 35 36 /** PerformanceDataManager. 37 * Internally synchronized global singleton for timings. 38 */ 39 class PerformanceDataManager final : public IPerformanceDataManager { 40 public: 41 struct InternalData; 42 using TypeDataSet = std::map<BASE_NS::fixed_string<TIMING_DATA_NAME_LENGTH>, InternalData>; 43 44 ~PerformanceDataManager(); 45 explicit PerformanceDataManager(const BASE_NS::string_view category); 46 47 PerformanceDataManager(const PerformanceDataManager&) = delete; 48 PerformanceDataManager& operator=(const PerformanceDataManager&) = delete; 49 50 BASE_NS::string_view GetCategory() const override; 51 52 TimerHandle BeginTimer() override; 53 int64_t EndTimer(TimerHandle handle) override; 54 55 void UpdateData( 56 const BASE_NS::string_view subCategory, const BASE_NS::string_view name, const int64_t microSeconds) override; 57 void ResetData() override; 58 BASE_NS::vector<PerformanceData> GetData() const override; 59 60 void DumpToLog() const; 61 62 void RemoveData(const BASE_NS::string_view subCategory) override; 63 64 // IInterface 65 const IInterface* GetInterface(const BASE_NS::Uid& uid) const override; 66 IInterface* GetInterface(const BASE_NS::Uid& uid) override; 67 void Ref() override; 68 void Unref() override; 69 70 private: 71 const BASE_NS::string category_; 72 73 #if (CORE_PERF_ENABLED == 1) 74 mutable std::mutex dataMutex_; 75 76 TypeDataSet data_; 77 #endif 78 }; 79 80 class PerformanceDataManagerFactory final : public IPerformanceDataManagerFactory { 81 public: 82 IPerformanceDataManager* Get(const BASE_NS::string_view category) override; 83 BASE_NS::vector<IPerformanceDataManager*> GetAllCategories() const override; 84 85 // IInterface 86 const IInterface* GetInterface(const BASE_NS::Uid& uid) const override; 87 IInterface* GetInterface(const BASE_NS::Uid& uid) override; 88 void Ref() override; 89 void Unref() override; 90 91 private: 92 #if (CORE_PERF_ENABLED == 1) 93 mutable std::mutex mutex_; 94 BASE_NS::unordered_map<BASE_NS::string, BASE_NS::unique_ptr<PerformanceDataManager>> managers_; 95 #endif 96 }; 97 CORE_END_NAMESPACE() 98 99 #endif // CORE__UTIL__PERFORMANCE_DATA_MANAGER_H 100