NdmcClient.h
Go to the documentation of this file.
1 /**
2  * NdmcClient header file
3  * $Id: NdmcClient.h,v 1.28.10.1 2019/09/27 00:07:18 slin Exp $
4  */
5 #ifndef __NDMC_CLIENT_H_
6 #define __NDMC_CLIENT_H_
7 
8 #include <map>
9 #include <typeinfo>
10 #include <sys/times.h>
11 #include "boost/thread.hpp"
12 #include "NovaDAQMonitorClient/Ndmc.h"
13 #include "NovaDAQMonitorClient/NdmcMetricABC.h"
14 #include "NovaDAQMonitorClient/NdmcMetric.h"
15 #include "NovaDAQUtilities/Runnable.h" // base class
16 #include "ganglia.h"
17 
18 class NdmcClientTest;
19 
21 
22 /**
23  * NdmcClient
24  *
25  * This class implements a NdmcClient client interface for a single process to
26  * the daq monitor.
27  *
28  * @author Susan Kasahara
29  * @author <a href="mailto:schubert@physics.umn.edu">schubert@physics.umn.edu</a>
30  * @date May 6 2010
31  */
32 
33 class NdmcClient : public novadaq::Runnable {
34 
35  friend class ::NdmcClientTest;
36 
37  public:
38 
39  // Constructor/Destructor
40  NdmcClient(int32_t partition = -1, std::string appName = "", std::string groupName = "",
41  uint32_t sendTimeInterval=15);
42  virtual ~NdmcClient();
43 
44  void run(); /* required to implement Runnable. Execute in separate thread. */
45 
46  // State testing methods
47  uint32_t getNumMetric();
48  int32_t getPartition() const { return _partition; }
50  std::string getGroupName() const { return _groupName; }
51  uint32_t getSendTimeInterval() const { return _sendTimeInterval; }
52 
53  // State changing methods
54  template<class T>
55  int32_t createMetric(std::string metricName, std::string units, MetricType_t metricType=VALUE,
57  template<class T>
58  int32_t createMetric(std::string metricName, std::string units, MetricType_t metricType,
59  NdmcMetric<T>*& metricPtr, std::string title = "",
61 
62  int32_t send(); // will send all owned metrics
63  void destroyMetric(int32_t metricID); // remove from map and delete metric with metricID
64  void destroyMetrics(); // remove all managed metrics from map and delete
65  bool setPartition(int32_t partition); // partition number (default -1)
66  void setApplicationName(std::string appName); // application name (default "")
67  void setGroupName(std::string groupName); // group (A:<appname> P<partition> will be appended)
68  void setSendTimeInterval(uint32_t sendTimeInterval);
69 
70  template<class T>
71  bool setValue(int32_t metricId, T value, double weight = 1., int npar=0, double* par=0);
72  // use this method if cpu is tight to avoid look-up of metric by id
73  template<class T>
74  bool setValue(NdmcMetric<T>* metric, T value, double weight = 1., int npar=0, double* par=0);
75 
76  template<class T>
77  bool getLastValueSent(int32_t metricId, T& lastValueSent);
78  // use this method if cpu is tight to avoid look-up of metric by id
79  template<class T>
80  bool getLastValueSent(NdmcMetric<T>* metric, T& lastValueSent);
81 
82  private:
83 
84  int32_t _partition; /**< partition number (0,1,2, or 3) */
85  std::string _applicationName; /**< application name, e.g. dcmapp */
86  std::string _groupName; /**< group name, defaults to "appName + Ppartition" */
87  std::string _userGroupName; /**< user-specified group name */
88  uint32_t _sendTimeInterval; /**< time interval at which to send custom metrics */
89  std::map<int32_t, NdmcMetricABC*> _metricMap; /**< map by ID/Metric */
90  clock_t _lastSendTime; /**< last send time */
91  boost::mutex _mutex; /**< mutex used to control set/send of values */
92  boost::thread *_clientThread; /**< thread for this client */
93 
94  // Private methods
95  bool _timeToSend();
96  int32_t _adoptMetric(NdmcMetricABC* metric);
97  NdmcMetricABC* _getMetric(int32_t metricId) const;
98  void _updateGroupName();
99 
100 };
101 
102 /**
103  * Create new metric of name metricName.
104  * @param metricName - name of this metric.
105  * @param units - units to be displayed on axis of plot, e.g. "Bytes", "Hz"
106  * @param metricType - VALUE,COUNTER,RATE,AVERAGE,MAXIMUM,MINIMUM,STDEV,HEARTBEAT
107  * @return -1 if error, id number of this metric.
108  */
109 template<class T>
111  MetricType_t metricType, std::string title,
113 
114  boost::mutex::scoped_lock lock(_mutex);
115 
116  // tmax is the max time in seconds expected between metric sends (appears to be ignored)
117  int32_t tmax = _sendTimeInterval;
118  // dmax is the lifetime in seconds of this metric before it expires in ganglia plots
119  int32_t dmax = _sendTimeInterval*2;
120  NdmcMetric<T>* metric = new NdmcMetric<T>(metricName, units, metricType,
121  tmax, dmax, GANGLIA_SLOPE_BOTH,
123  title, description);
124 
125  int32_t metricID = _adoptMetric(metric);
126 
127  if ( metricID < 0 ) {
128  delete metric; metric = 0; // adoptMetric failed
129  }
130 
131  return metricID;
132 
133 }
134 
135 /**
136  * Create new metric of name metricName.
137  * @param metricName - name of this metric.
138  * @param units - units to be displayed on axis of plot, e.g. "Bytes", "Hz"
139  * @param metricType - VALUE,COUNTER,RATE,AVERAGE,MAXIMUM,MINIMUM,STDEV,HEARTBEAT
140  * @return -1 if error, id number of this metric.
141  */
142 template<class T>
144  MetricType_t metricType, NdmcMetric<T>*& metricPtr,
146 
147  int32_t metricID = createMetric<T>(metricName,units,metricType,title,description);
148  metricPtr = NULL;
149  if ( metricID >= 0 ) metricPtr = dynamic_cast<NdmcMetric<T>*>(_getMetric(metricID));
150 
151  return metricID;
152 
153 }
154 
155 /**
156  * Set metric value by metricID
157  * @param metricID - ID number of this metric
158  * @param value - value to set of type T
159  * @param weight - optional value (def = 1) only used with metrics of type AVERAGE
160  * @return true if successful, else false. Will fail if metric of ID of type T has not
161  * been created.
162  */
163 template<class T>
164 bool NdmcClient::setValue(int32_t metricID, T value, double weight, int npar, double* par) {
165 
166  boost::mutex::scoped_lock lock(_mutex);
167 
168  NdmcMetric<T>* metric = dynamic_cast<NdmcMetric<T>*>(_getMetric(metricID));
169  if ( !metric ) {
170  ::mf::LogWarning("NdmcClient") << "NdmcClient::setValue - failed. No metric of id "
171  << metricID << " of type " << typeid(T).name() << "."
172  << std::endl;
173  return false;
174  }
175 
176  metric -> setValue(value, weight);
177 
178  return true;
179 
180 }
181 
182 /**
183  * Set metric value by metric ptr. Recommend the user use setValue by metric id unless
184  * performance is an issue.
185  *
186  * @param metric - ptr of type NdmcMetric<T>* previously created by NdmcClient::createMetric
187  * @param value - value to set of type T
188  * @param weight - optional value (def = 1) only used with metrics of type AVERAGE
189  * @return true if successful, else false.
190  */
191 template<class T>
192 bool NdmcClient::setValue(NdmcMetric<T>* metric, T value, double weight, int npar, double* par) {
193 
194  boost::mutex::scoped_lock lock(_mutex);
195 
196  if ( !metric) {
197  ::mf::LogWarning("NdmcClient") << "NdmcClient::setValue - failed. Passed NULL metric ptr."
198  << std::endl;
199  return false;
200  }
201 
202  return metric -> setValue(value, weight, npar, par);
203 
204 }
205 
206 /**
207  * Get last metric value sent, by metricID
208  * @param metricID - ID number of this metric
209  * @param lastValueSent - reference of type T to return value
210  * @return true if successful, else false. Will fail if metric of ID of type T has not
211  * been created.
212  */
213 template<class T>
214 bool NdmcClient::getLastValueSent(int32_t metricID, T& lastValueSent) {
215 
216  boost::mutex::scoped_lock lock(_mutex);
217 
218  NdmcMetric<T>* metric = dynamic_cast<NdmcMetric<T>*>(_getMetric(metricID));
219  if ( !metric ) {
220  ::mf::LogWarning("NdmcClient") << "NdmcClient::getLastValueSent - failed. No metric of id "
221  << metricID << " of type " << typeid(T).name() << "."
222  << std::endl;
223  return false;
224  }
225 
226  return metric -> getLastValueSent(lastValueSent);
227 
228 }
229 
230 /**
231  * Get last metric value sent, by metric ptr. Recommend the user use getLastValueSent, by
232  * metric id unless performance is an issue.
233  *
234  * @param metric - ptr of type NdmcMetric<T>* previously created by NdmcClient::createMetric
235  * @param lastValueSent - reference of type T to return value
236  * @return true if successful, else false.
237  */
238 template<class T>
240 
241  boost::mutex::scoped_lock lock(_mutex);
242 
243  if ( !metric ) {
244  ::mf::LogWarning("NdmcClient") << "NdmcClient::getLastValueSent - failed."
245  << " Passed NULL metric ptr." << std::endl;
246  return false;
247  }
248 
249  return metric -> getLastValueSent(lastValueSent);
250 
251 }
252 
254 
255 #endif
256 
NdmcMetricABC * _getMetric(int32_t metricId) const
Definition: NdmcClient.cpp:232
uint32_t _sendTimeInterval
Definition: NdmcClient.h:88
bool _timeToSend()
Definition: NdmcClient.cpp:274
const XML_Char * name
Definition: expat.h:151
int32_t _adoptMetric(NdmcMetricABC *metric)
Definition: NdmcClient.cpp:167
std::string _applicationName
Definition: NdmcClient.h:85
NdmcClient(int32_t partition=-1, std::string appName="", std::string groupName="", uint32_t sendTimeInterval=15)
Definition: NdmcClient.cpp:16
void setSendTimeInterval(uint32_t sendTimeInterval)
Definition: NdmcClient.cpp:155
#define NDMC_NAMESPACE_BEGIN
Definition: Ndmc.h:23
const Var weight
Int_t par
Definition: SimpleIterate.C:24
void setGroupName(std::string groupName)
Definition: NdmcClient.cpp:85
bool setValue(int32_t metricId, T value, double weight=1., int npar=0, double *par=0)
Definition: NdmcClient.h:164
virtual ~NdmcClient()
Definition: NdmcClient.cpp:36
void destroyMetrics()
Definition: NdmcClient.cpp:211
std::map< int32_t, NdmcMetricABC * > _metricMap
Definition: NdmcClient.h:89
clock_t _lastSendTime
Definition: NdmcClient.h:90
enum MetricType MetricType_t
void _updateGroupName()
Definition: NdmcClient.cpp:99
#define NDMC_NAMESPACE_END
Definition: Ndmc.h:26
int32_t createMetric(std::string metricName, std::string units, MetricType_t metricType=VALUE, std::string title="", std::string description="")
Definition: NdmcClient.h:110
const XML_Char int const XML_Char * value
Definition: expat.h:331
void destroyMetric(int32_t metricID)
Definition: NdmcClient.cpp:193
std::string _userGroupName
Definition: NdmcClient.h:87
uint32_t getNumMetric()
Definition: NdmcClient.cpp:245
uint32_t getSendTimeInterval() const
Definition: NdmcClient.h:51
std::string _groupName
Definition: NdmcClient.h:86
boost::mutex _mutex
Definition: NdmcClient.h:91
bool getLastValueSent(int32_t metricId, T &lastValueSent)
Definition: NdmcClient.h:214
::xsd::cxx::tree::string< char, simple_type > string
Definition: Database.h:154
void setApplicationName(std::string appName)
Definition: NdmcClient.cpp:73
bool setPartition(int32_t partition)
Definition: NdmcClient.cpp:60
int32_t getPartition() const
Definition: NdmcClient.h:48
void run()
Definition: NdmcClient.cpp:134
std::string getApplicationName() const
Definition: NdmcClient.h:49
std::string getGroupName() const
Definition: NdmcClient.h:50
int32_t _partition
Definition: NdmcClient.h:84
boost::thread * _clientThread
Definition: NdmcClient.h:92
double T
Definition: Xdiff_gwt.C:5
int32_t send()
Definition: NdmcClient.cpp:257
Definition: Ndmc.h:35