Gazebo Common

API Reference

5.8.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 <cstdint>
22 #include <memory>
23 #include <string>
24 
25 #include <gz/common/config.hh>
26 #include <gz/common/profiler/Export.hh>
27 #include <gz/common/SingletonT.hh>
28 
29 #include "ProfilerImpl.hh"
30 
31 namespace gz
32 {
33  namespace common
34  {
57  class GZ_COMMON_PROFILER_VISIBLE Profiler
58  : public virtual SingletonT<Profiler>
59  {
61  protected: Profiler();
62 
64  protected: ~Profiler();
65 
68  public: void SetThreadName(const char *_name);
69 
76  public: void LogText(const char *_text);
77 
85  public: void BeginSample(const char *_name, uint32_t *_hash = nullptr);
86 
88  public: void EndSample();
89 
97 
100 
102  public: bool Valid() const;
103 
105  private: ProfilerImpl *impl;
106 
108  private: friend class SingletonT<Profiler>;
109  };
110 
114  class GZ_COMMON_PROFILER_VISIBLE ScopedProfile
115  {
120  public: ScopedProfile(const char *_name, uint32_t *_hash)
121  {
122  Profiler::Instance()->BeginSample(_name, _hash);
123  }
124 
126  public: ~ScopedProfile()
127  {
129  }
130  };
131  }
132 }
133 
134 #ifndef GZ_PROFILER_ENABLE
136 #define GZ_PROFILER_ENABLE 0
137 #endif
138 
139 #if GZ_PROFILER_ENABLE
141 #define GZ_PROFILE_THREAD_NAME(name) \
142  gz::common::Profiler::Instance()->SetThreadName(name);
144 #define GZ_PROFILE_LOG_TEXT(name) \
145  gz::common::Profiler::Instance()->LogText(name);
147 #define GZ_PROFILE_BEGIN(name) \
148  gz::common::Profiler::Instance()->BeginSample(name)
150 #define GZ_PROFILE_END() \
151  gz::common::Profiler::Instance()->EndSample()
152 
154 #define GZ_PROFILE_L(name, line) \
155 static uint32_t __hash##line = 0; \
156 gz::common::ScopedProfile __profile##line(name, &__hash##line);
158 #define GZ_PROFILE(name) GZ_PROFILE_L(name, __LINE__);
159 
160 #else
161 
162 #define GZ_PROFILE_THREAD_NAME(name) ((void) name)
163 #define GZ_PROFILE_LOG_TEXT(name) ((void) name)
164 #define GZ_PROFILE_BEGIN(name) ((void) name)
165 #define GZ_PROFILE_END() ((void) 0)
166 #define GZ_PROFILE_L(name, line) ((void) name)
167 #define GZ_PROFILE(name) ((void) name)
168 #endif // GZ_PROFILER_ENABLE
169 
171 #define GZ_PROFILER_VALID \
172  GZ_PROFILER_ENABLE && gz::common::Profiler::Instance()->Valid()
173 
174 #endif // GZ_COMMON_PROFILER_HH_