Gazebo Common

API Reference

3.17.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/profiler/Export.hh>
25 #include <gz/common/SingletonT.hh>
26 #include <gz/common/config.hh>
27 
28 namespace ignition
29 {
30  namespace common
31  {
32  class ProfilerImpl;
33 
56  class IGNITION_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 
90  public: std::string ImplementationName() const;
91 
93  public: bool Valid() const;
94 
96  private: ProfilerImpl *impl;
97 
99  private: friend class SingletonT<Profiler>;
100  };
101 
105  class IGNITION_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 IGN_PROFILER_ENABLE
126 #define IGN_PROFILER_ENABLE 0
128 #endif
129 
130 #if IGN_PROFILER_ENABLE
131 #define IGN_PROFILE_THREAD_NAME(name) \
133  ignition::common::Profiler::Instance()->SetThreadName(name);
134 #define IGN_PROFILE_LOG_TEXT(name) \
136  ignition::common::Profiler::Instance()->LogText(name);
137 #define IGN_PROFILE_BEGIN(name) \
139  ignition::common::Profiler::Instance()->BeginSample(name)
140 #define IGN_PROFILE_END() \
142  ignition::common::Profiler::Instance()->EndSample()
143 
145 #define IGN_PROFILE_L(name, line) \
146 static uint32_t __hash##line = 0; \
147 gz::common::ScopedProfile __profile##line(name, &__hash##line);
148 #define IGN_PROFILE(name) IGN_PROFILE_L(name, __LINE__);
150 
151 #else
152 
153 #define IGN_PROFILE_THREAD_NAME(name) ((void) name)
154 #define IGN_PROFILE_LOG_TEXT(name) ((void) name)
155 #define IGN_PROFILE_BEGIN(name) ((void) name)
156 #define IGN_PROFILE_END() ((void) 0)
157 #define IGN_PROFILE_L(name, line) ((void) name)
158 #define IGN_PROFILE(name) ((void) name)
159 #endif // IGN_PROFILER_ENABLE
160 
162 #define IGN_PROFILER_VALID \
163  IGN_PROFILER_ENABLE && ignition::common::Profiler::Instance()->Valid()
164 
165 #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:111
static Profiler * Instance()
Get an instance of the singleton.
Definition: gz/common/SingletonT.hh:30
void EndSample()
End a profiling sample.
~ScopedProfile()
Destructor. Stops profile sample.
Definition: gz/common/Profiler.hh:117
Singleton template class.
Definition: gz/common/SingletonT.hh:27
Used to provide C++ RAII-style profiling sample. The sample will start on the construction of the Sco...
Definition: gz/common/Profiler.hh:105
Used to perform application-wide performance profiling.
Definition: gz/common/Profiler.hh:56