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