util/Diagnostics.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012-2015 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 _DIAGNOSTICMANAGER_HH_
19 #define _DIAGNOSTICMANAGER_HH_
20 
21 #include <boost/unordered_map.hpp>
22 #include <string>
23 #include <boost/filesystem.hpp>
24 
25 #include "gazebo/gazebo_config.h"
26 
28 
29 #include "gazebo/msgs/msgs.hh"
30 
33 #include "gazebo/common/Timer.hh"
34 
35 #include "gazebo/util/UtilTypes.hh"
36 #include "gazebo/util/system.hh"
37 
38 namespace gazebo
39 {
40  namespace util
41  {
44 
45 #ifdef ENABLE_DIAGNOSTICS
46  #define DIAG_TIMER_START(_name) \
50  gazebo::util::DiagnosticManager::Instance()->StartTimer(_name);
51 
57  #define DIAG_TIMER_LAP(_name, _prefix) \
58  gazebo::util::DiagnosticManager::Instance()->Lap(_name, _prefix);
59 
62  #define DIAG_TIMER_STOP(_name) \
63  gazebo::util::DiagnosticManager::Instance()->StopTimer(_name);
64 #else
65  #define DIAG_TIMER_START(_name) ((void) 0)
66  #define DIAG_TIMER_LAP(_name, _prefix) ((void)0)
67  #define DIAG_TIMER_STOP(_name) ((void) 0)
68 #endif
69 
73  public SingletonT<DiagnosticManager>
74  {
76  private: DiagnosticManager();
77 
79  private: virtual ~DiagnosticManager();
80 
83  public: void Init(const std::string &_worldName);
84 
88  public: void StartTimer(const std::string &_name);
89 
92  public: void StopTimer(const std::string &_name);
93 
99  public: void Lap(const std::string &_name, const std::string &_prefix);
100 
103  public: int GetTimerCount() const;
104 
108  public: common::Time GetTime(int _index) const;
109 
113  public: common::Time GetTime(const std::string &_label) const;
114 
118  public: std::string GetLabel(int _index) const;
119 
122  public: boost::filesystem::path GetLogPath() const;
123 
126  private: void Update(const common::UpdateInfo &_info);
127 
133  private: void AddTime(const std::string &_name, common::Time &_wallTime,
134  common::Time &_elapsedtime);
135 
137  private: typedef boost::unordered_map<std::string, DiagnosticTimerPtr>
138  TimerMap;
139 
141  private: TimerMap timers;
142 
144  private: boost::filesystem::path logPath;
145 
147  private: transport::NodePtr node;
148 
150  private: transport::PublisherPtr pub;
151 
153  private: msgs::Diagnostics msg;
154 
156  private: event::ConnectionPtr updateConnection;
157 
158  // Singleton implementation
159  private: friend class SingletonT<DiagnosticManager>;
160 
162  private: friend class DiagnosticTimer;
163  };
164 
168  {
171  public: DiagnosticTimer(const std::string &_name);
172 
174  public: virtual ~DiagnosticTimer();
175 
178  public: void Lap(const std::string &_prefix);
179 
180  // Documentation inherited
181  public: virtual void Start();
182 
183  // Documentation inherited
184  public: virtual void Stop();
185 
188  public: inline const std::string GetName() const
189  { return this->name; }
190 
192  private: std::string name;
193 
195  private: std::ofstream log;
196 
198  private: common::Time prevLap;
199  };
201  }
202 }
203 #endif
boost::shared_ptr< Connection > ConnectionPtr
Definition: CommonTypes.hh:147
Forward declarations for transport.
Singleton template class.
Definition: SingletonT.hh:33
A diagnostic manager class.
Definition: util/Diagnostics.hh:72
const std::string GetName() const
Get the name of the timer.
Definition: util/Diagnostics.hh:188
Information for use in an update event.
Definition: UpdateInfo.hh:30
#define GZ_UTIL_VISIBLE
Definition: system.hh:266
A timer class, used to time things in real world walltime.
Definition: Timer.hh:38
boost::shared_ptr< Node > NodePtr
Definition: TransportTypes.hh:57
A timer designed for diagnostics.
Definition: util/Diagnostics.hh:167
GAZEBO_VISIBLE void Init(google::protobuf::Message &_message, const std::string &_id="")
Initialize a message.
boost::shared_ptr< Publisher > PublisherPtr
Definition: TransportTypes.hh:49
A Time class, can be used to hold wall- or sim-time.
Definition: Time.hh:39