Gazebo Common

API Reference

5.7.1
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 #include "ProfilerImpl.hh"
29 
30 namespace gz
31 {
32  namespace common
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 
96 
99 
101  public: bool Valid() const;
102 
104  private: ProfilerImpl *impl;
105 
107  private: friend class SingletonT<Profiler>;
108  };
109 
113  class GZ_COMMON_PROFILER_VISIBLE ScopedProfile
114  {
119  public: ScopedProfile(const char *_name, uint32_t *_hash)
120  {
121  Profiler::Instance()->BeginSample(_name, _hash);
122  }
123 
125  public: ~ScopedProfile()
126  {
128  }
129  };
130  }
131 }
132 
133 #ifndef GZ_PROFILER_ENABLE
135 #define GZ_PROFILER_ENABLE 0
136 #endif
137 
138 #if GZ_PROFILER_ENABLE
140 #define GZ_PROFILE_THREAD_NAME(name) \
141  gz::common::Profiler::Instance()->SetThreadName(name);
143 #define GZ_PROFILE_LOG_TEXT(name) \
144  gz::common::Profiler::Instance()->LogText(name);
146 #define GZ_PROFILE_BEGIN(name) \
147  gz::common::Profiler::Instance()->BeginSample(name)
149 #define GZ_PROFILE_END() \
150  gz::common::Profiler::Instance()->EndSample()
151 
153 #define GZ_PROFILE_L(name, line) \
154 static uint32_t __hash##line = 0; \
155 gz::common::ScopedProfile __profile##line(name, &__hash##line);
157 #define GZ_PROFILE(name) GZ_PROFILE_L(name, __LINE__);
158 
159 #else
160 
161 #define GZ_PROFILE_THREAD_NAME(name) ((void) name)
162 #define GZ_PROFILE_LOG_TEXT(name) ((void) name)
163 #define GZ_PROFILE_BEGIN(name) ((void) name)
164 #define GZ_PROFILE_END() ((void) 0)
165 #define GZ_PROFILE_L(name, line) ((void) name)
166 #define GZ_PROFILE(name) ((void) name)
167 #endif // GZ_PROFILER_ENABLE
168 
170 #define GZ_PROFILER_VALID \
171  GZ_PROFILER_ENABLE && gz::common::Profiler::Instance()->Valid()
172 
173 #endif // GZ_COMMON_PROFILER_HH_