Gazebo Common

API Reference

4.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 namespace ignition
30 {
31  namespace common
32  {
33  class ProfilerImpl;
34 
57  class IGNITION_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 
91  public: std::string ImplementationName() const;
92 
94  public: bool Valid() const;
95 
97  private: ProfilerImpl *impl;
98 
100  private: friend class SingletonT<Profiler>;
101  };
102 
106  class IGNITION_COMMON_PROFILER_VISIBLE ScopedProfile
107  {
112  public: ScopedProfile(const char *_name, uint32_t *_hash)
113  {
114  Profiler::Instance()->BeginSample(_name, _hash);
115  }
116 
118  public: ~ScopedProfile()
119  {
121  }
122  };
123  }
124 }
125 
126 #ifndef IGN_PROFILER_ENABLE
127 #define IGN_PROFILER_ENABLE 0
129 #endif
130 
131 #if IGN_PROFILER_ENABLE
132 #define IGN_PROFILE_THREAD_NAME(name) \
134  ignition::common::Profiler::Instance()->SetThreadName(name);
135 #define IGN_PROFILE_LOG_TEXT(name) \
137  ignition::common::Profiler::Instance()->LogText(name);
138 #define IGN_PROFILE_BEGIN(name) \
140  ignition::common::Profiler::Instance()->BeginSample(name)
141 #define IGN_PROFILE_END() \
143  ignition::common::Profiler::Instance()->EndSample()
144 
146 #define IGN_PROFILE_L(name, line) \
147 static uint32_t __hash##line = 0; \
148 ignition::common::ScopedProfile __profile##line(name, &__hash##line);
149 #define IGN_PROFILE(name) IGN_PROFILE_L(name, __LINE__);
151 
152 #else
153 
154 #define IGN_PROFILE_THREAD_NAME(name) ((void) name)
155 #define IGN_PROFILE_LOG_TEXT(name) ((void) name)
156 #define IGN_PROFILE_BEGIN(name) ((void) name)
157 #define IGN_PROFILE_END() ((void) 0)
158 #define IGN_PROFILE_L(name, line) ((void) name)
159 #define IGN_PROFILE(name) ((void) name)
160 #endif // IGN_PROFILER_ENABLE
161 
163 #define IGN_PROFILER_VALID \
164  IGN_PROFILER_ENABLE && ignition::common::Profiler::Instance()->Valid()
165 
166 #endif // GZ_COMMON_PROFILER_HH_
Forward declarations for the common classes.
STL class.
void BeginSample(const char *_name, uint32_t *_hash=nullptr)
Begin a named profiling sample. Begins a CPU profiler sample with a given name. Can optionally take a...
ScopedProfile(const char *_name, uint32_t *_hash)
Constructor. Starts profile sample.
Definition: gz/common/Profiler.hh:112
static Profiler * Instance()
Get an instance of the singleton.
Definition: gz/common/SingletonT.hh:32
void EndSample()
End a profiling sample.
~ScopedProfile()
Destructor. Stops profile sample.
Definition: gz/common/Profiler.hh:118
Singleton template class.
Definition: gz/common/SingletonT.hh:29
Used to provide C++ RAII-style profiling sample. The sample will start on the construction of the Sco...
Definition: gz/common/Profiler.hh:106
Used to perform application-wide performance profiling.
Definition: gz/common/Profiler.hh:57