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 #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 #include <cstdint>
20
21 #include <base/containers/string.h>
22 #include <base/containers/string_view.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 #include <core/plugin/intf_plugin_register.h>
28
CORE_BEGIN_NAMESPACE()29 CORE_BEGIN_NAMESPACE()
30 class CpuPerfScope final {
31 public:
32 inline CpuPerfScope(
33 const BASE_NS::string_view category, const BASE_NS::string_view subCategory, const BASE_NS::string_view name);
34 inline ~CpuPerfScope();
35 inline void Stop();
36
37 protected:
38 IPerformanceDataManager* manager_ { nullptr };
39 IPerformanceDataManager::TimerHandle timerName_;
40 BASE_NS::string subCategory_;
41 BASE_NS::string name_;
42 };
43
CpuPerfScope(const BASE_NS::string_view category,const BASE_NS::string_view subCategory,const BASE_NS::string_view name)44 inline CpuPerfScope::CpuPerfScope(
45 const BASE_NS::string_view category, const BASE_NS::string_view subCategory, const BASE_NS::string_view name)
46 : subCategory_(subCategory), name_(name)
47 {
48 if (auto* inst = GetInstance<IPerformanceDataManagerFactory>(UID_PERFORMANCE_FACTORY); inst) {
49 manager_ = inst->Get(category);
50 }
51 if (manager_) {
52 timerName_ = manager_->BeginTimer();
53 }
54 }
55
~CpuPerfScope()56 inline CpuPerfScope::~CpuPerfScope()
57 {
58 Stop();
59 }
60
Stop()61 inline void CpuPerfScope::Stop()
62 {
63 if (manager_) {
64 manager_->UpdateData(subCategory_, name_, manager_->EndTimer(timerName_));
65 manager_ = nullptr;
66 }
67 }
68 CORE_END_NAMESPACE()
69
70 // Helper to concatenate macro values.
71 #define CORE_CONCAT_NOEXP(value0, value1) value0##value1
72 #define CORE_CONCAT(value0, value1) CORE_CONCAT_NOEXP(value0, value1)
73
74 #define CORE_CPU_PERF_BEGIN(timerName, category, subCategory, name) 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