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 #ifndef API_CORE_PERF_CPU_PERF_SCOPE_H
16 #define API_CORE_PERF_CPU_PERF_SCOPE_H
17
18 #if (CORE_PERF_ENABLED == 1)
19
20 #include <base/containers/string.h>
21 #include <base/containers/string_view.h>
22 #include <base/namespace.h>
23 #include <core/implementation_uids.h>
24 #include <core/namespace.h>
25 #include <core/perf/intf_performance_data_manager.h>
26 #include <core/plugin/intf_class_register.h>
27
CORE_BEGIN_NAMESPACE()28 CORE_BEGIN_NAMESPACE()
29 class CpuPerfScope final {
30 public:
31 inline CpuPerfScope(
32 const BASE_NS::string_view category, const BASE_NS::string_view subCategory, const BASE_NS::string_view name);
33 inline ~CpuPerfScope();
34 inline void Stop();
35
36 protected:
37 IPerformanceDataManager* manager_ { nullptr };
38 IPerformanceDataManager::TimerHandle timerName_;
39 BASE_NS::string subCategory_;
40 BASE_NS::string name_;
41 };
42
CpuPerfScope(const BASE_NS::string_view category,const BASE_NS::string_view subCategory,const BASE_NS::string_view name)43 inline CpuPerfScope::CpuPerfScope(
44 const BASE_NS::string_view category, const BASE_NS::string_view subCategory, const BASE_NS::string_view name)
45 : subCategory_(subCategory), name_(name)
46 {
47 if (auto* inst = GetInstance<IPerformanceDataManagerFactory>(UID_PERFORMANCE_FACTORY); inst) {
48 manager_ = inst->Get(category);
49 }
50 if (manager_) {
51 timerName_ = manager_->BeginTimer();
52 }
53 }
54
~CpuPerfScope()55 inline CpuPerfScope::~CpuPerfScope()
56 {
57 Stop();
58 }
59
Stop()60 inline void CpuPerfScope::Stop()
61 {
62 if (manager_) {
63 manager_->UpdateData(subCategory_, name_, manager_->EndTimer(timerName_));
64 manager_ = nullptr;
65 }
66 }
67 CORE_END_NAMESPACE()
68
69 // Helper to concatenate macro values.
70 #define CORE_CONCAT_NOEXP(value0, value1) value0##value1
71 #define CORE_CONCAT(value0, value1) CORE_CONCAT_NOEXP(value0, value1)
72
73 #define CORE_CPU_PERF_BEGIN(timerName, category, subCategory, name) \
74 CORE_NS::CpuPerfScope timerName(category, subCategory, name);
75 #define CORE_CPU_PERF_END(timerName) timerName.Stop();
76 #define CORE_CPU_PERF_SCOPE(category, subCategory, name) \
77 CORE_NS::CpuPerfScope CORE_CONCAT(cpuPerfScope_, __LINE__)(category, subCategory, name)
78
79 #else
80 #define CORE_CPU_PERF_BEGIN(timerName, category, subCategory, name)
81 #define CORE_CPU_PERF_END(timerName)
82 #define CORE_CPU_PERF_SCOPE(category, subCategory, name)
83 #endif
84
85 #endif // API_CORE_PERF_CPU_PERF_SCOPE_H
86