Gazebo Common

API Reference

5.7.0
gz/common/Profiler.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2018 Open Source Robotics Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17 
18 #ifndef GZ_COMMON_PROFILER_HH_
19 #define GZ_COMMON_PROFILER_HH_
20 
21 #include <memory>
22 #include <string>
23 
24 #include <gz/common/config.hh>
25 #include <gz/common/profiler/Export.hh>
26 #include <gz/common/SingletonT.hh>
27 
28 namespace gz
29 {
30  namespace common
31  {
32  class ProfilerImpl;
33 
56  class GZ_COMMON_PROFILER_VISIBLE Profiler
57  : public virtual SingletonT<Profiler>
58  {
60  protected: Profiler();
61 
63  protected: ~Profiler();
64 
67  public: void SetThreadName(const char *_name);
68 
75  public: void LogText(const char *_text);
76 
84  public: void BeginSample(const char *_name, uint32_t *_hash = nullptr);
85 
87  public: void EndSample();
88 
91 
93  public: bool Valid() const;
94 
96  private: ProfilerImpl *impl;
97 
99  private: friend class SingletonT<Profiler>;
100  };
101 
105  class GZ_COMMON_PROFILER_VISIBLE ScopedProfile
106  {
111  public: ScopedProfile(const char *_name, uint32_t *_hash)
112  {
113  Profiler::Instance()->BeginSample(_name, _hash);
114  }
115 
117  public: ~ScopedProfile()
118  {
120  }
121  };
122  }
123 }
124 
125 #ifndef GZ_PROFILER_ENABLE
127 #define GZ_PROFILER_ENABLE 0
128 #endif
129 
130 #if GZ_PROFILER_ENABLE
132 #define GZ_PROFILE_THREAD_NAME(name) \
133  gz::common::Profiler::Instance()->SetThreadName(name);
135 #define GZ_PROFILE_LOG_TEXT(name) \
136  gz::common::Profiler::Instance()->LogText(name);
138 #define GZ_PROFILE_BEGIN(name) \
139  gz::common::Profiler::Instance()->BeginSample(name)
141 #define GZ_PROFILE_END() \
142  gz::common::Profiler::Instance()->EndSample()
143 
145 #define GZ_PROFILE_L(name, line) \
146 static uint32_t __hash##line = 0; \
147 gz::common::ScopedProfile __profile##line(name, &__hash##line);
149 #define GZ_PROFILE(name) GZ_PROFILE_L(name, __LINE__);
150 
151 #else
152 
153 #define GZ_PROFILE_THREAD_NAME(name) ((void) name)
154 #define GZ_PROFILE_LOG_TEXT(name) ((void) name)
155 #define GZ_PROFILE_BEGIN(name) ((void) name)
156 #define GZ_PROFILE_END() ((void) 0)
157 #define GZ_PROFILE_L(name, line) ((void) name)
158 #define GZ_PROFILE(name) ((void) name)
159 #endif // GZ_PROFILER_ENABLE
160 
162 #define GZ_PROFILER_VALID \
163  GZ_PROFILER_ENABLE && gz::common::Profiler::Instance()->Valid()
164 
165 #endif // GZ_COMMON_PROFILER_HH_