pal_statistics.h
Go to the documentation of this file.
1 
26 #ifndef _PAL_STATISTICS_H_
27 #define _PAL_STATISTICS_H_
28 
29 #include <ros/ros.h>
30 #include <boost/thread/lock_guard.hpp>
31 #include <boost/thread/mutex.hpp>
32 #include <boost/thread/condition_variable.hpp>
33 #include <boost/thread/thread.hpp>
34 #include <pal_statistics_msgs/Statistics.h>
36 #include <gtest/gtest_prod.h>
37 
38 namespace pal_statistics
39 {
53 class StatisticsRegistry : public boost::enable_shared_from_this<StatisticsRegistry>
54 {
55 public:
56  StatisticsRegistry(const std::string &topic);
57 
58  virtual ~StatisticsRegistry();
59 
64  IdType registerVariable(const std::string &name, const double * variable, RegistrationsRAII *bookkeeping = NULL,
65  bool enabled = true)
66  {
67  return registerInternal(name, VariableHolder(variable), bookkeeping, enabled);
68  }
69 
74  IdType registerFunction(const std::string &name, const boost::function<double()> &funct,
75  RegistrationsRAII *bookkeeping = NULL, bool enabled = true);
76 
77 
78  void unregisterVariable(const std::string &name, RegistrationsRAII *bookkeeping = NULL);
79  void unregisterVariable(IdType id, RegistrationsRAII *bookkeeping = NULL);
80 
87  void publish();
88 
96  bool publishAsync();
97 
102  void startPublishThread();
103 
109  pal_statistics_msgs::Statistics createMsg();
110 
114  template <typename T>
115  void publishCustomStatistic(const std::string &name, T value)
116  {
117  pal_statistics_msgs::Statistics msg;
118  pal_statistics_msgs::Statistic stat;
119  stat.name = name;
120  stat.value = static_cast<double>(value);
121  msg.statistics.push_back(stat);
122  msg.header.stamp = ros::Time::now();
123  pub_.publish(msg);
124  }
125 
129  void publishCustomStatistics(const pal_statistics_msgs::Statistics &msg)
130  {
131  pub_.publish(msg);
132  }
133 
147  bool enable(const IdType &id);
148  bool disable(const IdType &id);
149 
150 private:
156  void updateMsgUnsafe();
157 
162  bool updateMsg(pal_statistics_msgs::StatisticsNames &names,
163  pal_statistics_msgs::StatisticsValues &values, bool smart_fill = false);
164 
165  void publisherThreadCycle();
166 
167  void startPublishThreadImpl();
168 
169  IdType registerInternal(const std::string &name, VariableHolder &&variable, RegistrationsRAII *bookkeeping, bool enabled);
170 
171  bool setEnabledmpl(const IdType &id, bool enabled);
172 
176  void handlePendingDisables(const boost::unique_lock<boost::mutex> &data_lock);
177 
181  void doPublish(bool publish_names_msg = true);
182 
184 
185  boost::mutex data_mutex_;
187 
188  struct EnabledId
189  {
190  //Can't use a pair because it's not trivially copiable
192  bool enabled;
193  };
194 
206 
207 
208  // To avoid deadlocks, should always be acquired after data_mutex_
209  boost::mutex pub_mutex_;
213 
214  std::atomic<bool> is_data_ready_;
216 
218  {
220  : last_names_version_(-1)
221  {}
222  void update(const pal_statistics_msgs::StatisticsNames &names,
223  const pal_statistics_msgs::StatisticsValues &values);
224 
226  pal_statistics_msgs::Statistics msg_;
227  unsigned int last_names_version_;
228  };
229 
230  pal_statistics_msgs::StatisticsNames names_msg_;
231  pal_statistics_msgs::StatisticsValues values_msg_;
233  // Internal stats
238 
239  FRIEND_TEST(PalStatisticsTest, stressAsync);
240 };
241 } // namespace pal_statistics
242 #endif
bool publishAsync()
publishAsync Capture data and flag it to be published by the publisher thread. Real-Time safe...
IdType registerInternal(const std::string &name, VariableHolder &&variable, RegistrationsRAII *bookkeeping, bool enabled)
The RegistrationList class.
StatisticsRegistry(const std::string &topic)
FRIEND_TEST(PalStatisticsTest, stressAsync)
pal_statistics_msgs::StatisticsValues values_msg_
The StatisticsRegistry class reads the value of registered variables and publishes them on the specif...
void doPublish(bool publish_names_msg=true)
doPublish publishes the subscribed topics, requires mutex
bool updateMsg(pal_statistics_msgs::StatisticsNames &names, pal_statistics_msgs::StatisticsValues &values, bool smart_fill=false)
updateMsg update names and values, optionally using smartfill to minimize copying ...
GeneratedStatistics generated_statistics_
void publishCustomStatistic(const std::string &name, T value)
publishCustomStatistic publishes a one-time statistic
void startPublishThread()
startPublishThread creates and starts the publisherThread. The user still has to call publishAsync ea...
void publish()
publish Reads the values of all registered variables and publishes them to the topic associated to th...
void update(const std::string &key, const XmlRpc::XmlRpcValue &v)
void publish(const boost::shared_ptr< M > &message) const
bool setEnabledmpl(const IdType &id, bool enabled)
boost::shared_ptr< boost::thread > publisher_thread_
void updateMsgUnsafe()
updateMsgUnsafe Updates the internal message variable without acquiring the mutex Should only be used...
pal_statistics_msgs::Statistics msg_
This message is generated using an updated StatiticsNames and StatisticsValues.
pal_statistics_msgs::StatisticsNames names_msg_
LockFreeQueue< EnabledId > enabled_ids_
disabled_ids_ this is used to keep track of enabled/disabled variables in a lock free way ...
IdType registerVariable(const std::string &name, const double *variable, RegistrationsRAII *bookkeeping=NULL, bool enabled=true)
registerVariable Specialization for double*, the most common case, to avoid going through a boost fun...
The RegistrationsRAII class holds handles to registered variables and when it is destroyed, unregisters them automatically.
void publishCustomStatistics(const pal_statistics_msgs::Statistics &msg)
publishCustomStatistic publishes a one-time statistics msg
Simple wrapper around boost lockfree queue to keep track of the reserved memory Boost&#39;s implementatio...
static Time now()
pal_statistics_msgs::Statistics createMsg()
createMsg creates a Statistics message from the registered variables, useful for debugging ...
void unregisterVariable(const std::string &name, RegistrationsRAII *bookkeeping=NULL)
void handlePendingDisables(const boost::unique_lock< boost::mutex > &data_lock)
handlePendingDisables Empties by handling the queue of disabled/enabled ids.
IdType registerFunction(const std::string &name, const boost::function< double()> &funct, RegistrationsRAII *bookkeeping=NULL, bool enabled=true)
registerFunction Adds a function that returns double with the specified name


pal_statistics
Author(s):
autogenerated on Mon Feb 28 2022 23:04:05