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 RENDER_PERF_CPU_PERF_SCOPE_H
16 #define RENDER_PERF_CPU_PERF_SCOPE_H
17
18 #if (RENDER_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/perf/intf_performance_data_manager.h>
25 #include <render/namespace.h>
26
27 #include "perf/cpu_timer.h"
28
RENDER_BEGIN_NAMESPACE()29 RENDER_BEGIN_NAMESPACE()
30 class CpuPerfScopeInternal final {
31 public:
32 inline CpuPerfScopeInternal(
33 const BASE_NS::string_view category, const BASE_NS::string_view subCategory, const BASE_NS::string_view name);
34 inline ~CpuPerfScopeInternal();
35
36 inline void Stop();
37
38 private:
39 CORE_NS::IPerformanceDataManager* manager_ { nullptr };
40 CORE_NS::IPerformanceDataManager::TimerHandle timerName_;
41 BASE_NS::string subCategory_;
42 BASE_NS::string name_;
43 };
44
CpuPerfScopeInternal(const BASE_NS::string_view category,const BASE_NS::string_view subCategory,const BASE_NS::string_view name)45 inline CpuPerfScopeInternal::CpuPerfScopeInternal(
46 const BASE_NS::string_view category, const BASE_NS::string_view subCategory, const BASE_NS::string_view name)
47 : subCategory_(subCategory), name_(name)
48 {
49 if (auto* inst = CORE_NS::GetInstance<CORE_NS::IPerformanceDataManagerFactory>(CORE_NS::UID_PERFORMANCE_FACTORY);
50 inst) {
51 manager_ = inst->Get(category);
52 }
53 if (manager_) {
54 timerName_ = manager_->BeginTimer();
55 }
56 }
57
~CpuPerfScopeInternal()58 inline CpuPerfScopeInternal::~CpuPerfScopeInternal()
59 {
60 Stop();
61 }
62
Stop()63 inline void CpuPerfScopeInternal::Stop()
64 {
65 if (manager_) {
66 manager_->UpdateData(subCategory_, name_, manager_->EndTimer(timerName_));
67 manager_ = nullptr;
68 }
69 }
70 RENDER_END_NAMESPACE()
71
72 // Helper to concatenate macro values.
73 #define RENDER_CONCAT_NOEXP(value0, value1) value0##value1
74 #define RENDER_CONCAT(value0, value1) RENDER_CONCAT_NOEXP(value0, value1)
75
76 #define RENDER_CPU_PERF_BEGIN(timerName, category, subCategory, name) \
77 RENDER_NS::CpuPerfScopeInternal timerName(category, subCategory, name);
78 #define RENDER_CPU_PERF_END(timerName) timerName.Stop();
79 #define RENDER_CPU_PERF_SCOPE(category, subCategory, name) \
80 RENDER_NS::CpuPerfScopeInternal RENDER_CONCAT(cpuPerfScope_, __LINE__)(category, subCategory, name)
81
82 #else
83 #define RENDER_CPU_PERF_BEGIN(timerName, category, subCategory, name)
84 #define RENDER_CPU_PERF_END(timerName)
85 #define RENDER_CPU_PERF_SCOPE(category, subCategory, name)
86 #endif
87
88 #endif // RENDER_PERF_CPU_PERF_SCOPE_H
89