PMDA++  0.4.4
Header-only C++ library for writing PCP PMDAs
metric_description.hpp
Go to the documentation of this file.
1 // Copyright Paul Colby 2013 - 2015.
2 // Distributed under the Boost Software License, Version 1.0.
3 // (See accompanying file LICENSE.md or copy at
4 // http://www.boost.org/LICENSE_1_0.txt)
5 
6 /**
7  * @file
8  * @brief Defines various metric-description related classes.
9  */
10 
11 #ifndef __PCP_CPP_METRIC_DESCRIPTION_HPP__
12 #define __PCP_CPP_METRIC_DESCRIPTION_HPP__
13 
14 #include "config.hpp"
15 #include "exception.hpp"
16 #include "types.hpp"
17 
18 #include <assert.h>
19 #include <string>
20 
21 PCP_CPP_BEGIN_NAMESPACE
22 
23 namespace pcp {
24 
25 class instance_domain;
26 
27 /**
28  * @brief Flags that may be applied to metric descriptions.
29  */
31  storable_metric = 0x1 ///< Metric supports pmstore operations.
32 };
33 
34 /**
35  * @brief Pipe operator for combining metric_flags values.
36  *
37  * This function performs a logical OR of two metric_flags sets. This is a
38  * convenience function, allowing metric_flags enum values to be used both
39  * standalone, and in combination.
40  *
41  * @param a First set of flags.
42  * @param b Second set of flags.
43  *
44  * @return Combined set of both \a a and \a b flags.
45  */
47 {
48  return static_cast<metric_flags>(
49  static_cast<int>(a) | static_cast<int>(b)
50  );
51 }
52 
53 /**
54  * @brief Individual metric description.
55  */
57  std::string metric_name; ///< This metric's name.
58  atom_type_type type; ///< This metric's atom type.
59  semantic_type semantic; ///< This metric's PCP semantic.
60  pmUnits units; ///< This metric's PCP units.
61  instance_domain * domain; ///< Optional instance domain for this metric.
62  std::string short_description; ///< This metric's short description.
63  std::string verbose_description; ///< This metric's verbose description.
64  void * const opaque; ///< Opaque value to track with this metric.
65  metric_flags flags; ///< Optional flags for this metric.
66 
67  /**
68  * @brief Constructor
69  *
70  * @param metric_name Metric name.
71  * @param type Atom type.
72  * @param semantic PCP semantic.
73  * @param units PCP units.
74  * @param domain Optional instance domain.
75  * @param short_description Short description.
76  * @param verbose_description Verbose description.
77  * @param opaque Opaque value to track.
78  * @param flags Optional metric flags.
79  */
80  metric_description(const std::string &metric_name,
81  const atom_type_type type,
82  const semantic_type semantic,
83  const pmUnits &units,
84  instance_domain * const domain = NULL,
85  const std::string &short_description = std::string(),
86  const std::string &verbose_description = std::string(),
87  void * const opaque = NULL,
88  const metric_flags flags = static_cast<metric_flags>(0))
89  : metric_name(metric_name),
90  type(type),
91  semantic(semantic),
92  units(units),
93  domain(domain),
94  short_description(short_description),
95  verbose_description(verbose_description),
96  opaque(opaque),
97  flags(flags)
98  {
99 
100  }
101 
102  /**
103  * @brief Operator for implicit cast to pmDesc.
104  *
105  * This operator allows this object to be used wherever PCP functions
106  * require a pmDesc instance.
107  *
108  * @return A pmDesc struct representing this metric description.
109  */
110  inline operator pmDesc() const
111  {
112  pmDesc description;
113  description.indom = PM_INDOM_NULL;
114  description.pmid = 0;
115  description.sem = semantic;
116  description.type = type;
117  description.units = units;
118  return description;
119  }
120 
121 };
122 
123 /**
124  * @brief A cluster of metric descriptions.
125  */
126 class metric_cluster : public std::map<item_id_type, metric_description> {
127 
128 public:
129 
130  /**
131  * @brief Constructor.
132  *
133  * @param id ID for this cluster.
134  * @param name Name for this cluster.
135  */
137  const std::string &name)
138  : cluster_id(id),
139  cluster_name(name)
140  {
141 
142  }
143 
144  /**
145  * @brief Get this cluster's ID.
146  *
147  * @return This cluster's ID.
148  */
150  {
151  return cluster_id;
152  }
153 
154  /**
155  * @brief Get this cluster's name.
156  *
157  * @return This cluster's name.
158  */
159  std::string get_cluster_name() const
160  {
161  return cluster_name;
162  }
163 
164  /**
165  * @brief Metric insertion functor.
166  *
167  * This functor allows for chained insertion of metrics into this cluster.
168  *
169  * @param item_id ID for the metric being inserted.
170  * @param metric_name Metric name.
171  * @param type Atom type.
172  * @param semantic PCP semantic.
173  * @param units PCP units.
174  * @param flags Optional metric flags.
175  * @param domain Optional instance domain.
176  * @param short_description Short description.
177  * @param verbose_description Verbose description.
178  * @param opaque Opaque value to track.
179  *
180  * @return A reference to this metric cluster.
181  */
183  const std::string &metric_name,
184  const atom_type_type type,
185  const semantic_type semantic,
186  const pmUnits &units,
187  const metric_flags flags = static_cast<metric_flags>(0),
188  instance_domain * const domain = NULL,
189  const std::string &short_description = std::string(),
190  const std::string &verbose_description = std::string(),
191  void * const opaque = NULL)
192  {
193  insert(value_type(item_id, metric_description(metric_name, type, semantic,
194  units, domain, short_description, verbose_description, opaque, flags)));
195  return *this;
196  }
197 
198  /**
199  * @brief Metric insertion functor.
200  *
201  * This functor allows for chained insertion of metrics into this cluster.
202  *
203  * @param item_id ID for the metric being inserted.
204  * @param metric_name Metric name.
205  * @param type Atom type.
206  * @param semantic PCP semantic.
207  * @param units PCP units.
208  * @param domain Optional instance domain.
209  * @param flags Optional metric flags.
210  * @param short_description Short description.
211  * @param verbose_description Verbose description.
212  * @param opaque Opaque value to track.
213  *
214  * @return A reference to this metric cluster.
215  */
217  const std::string &metric_name,
218  const atom_type_type type,
219  const semantic_type semantic,
220  const pmUnits &units,
221  instance_domain * const domain,
222  const metric_flags flags = static_cast<metric_flags>(0),
223  const std::string &short_description = std::string(),
224  const std::string &verbose_description = std::string(),
225  void * const opaque = NULL)
226  {
227  insert(value_type(item_id, metric_description(metric_name, type, semantic,
228  units, domain, short_description, verbose_description, opaque, flags)));
229  return *this;
230  }
231 
232  /**
233  * @brief Metric insertion functor.
234  *
235  * This functor allows for chained insertion of metrics into this cluster.
236  *
237  * @param item_id ID for the metric being inserted.
238  * @param metric_name Metric name.
239  * @param type Atom type.
240  * @param semantic PCP semantic.
241  * @param units PCP units.
242  * @param domain Optional instance domain.
243  * @param short_description Short description.
244  * @param verbose_description Verbose description.
245  * @param opaque Opaque value to track.
246  * @param flags Optional metric flags.
247  *
248  * @return A reference to this metric cluster.
249  */
251  const std::string &metric_name,
252  const atom_type_type type,
253  const semantic_type semantic,
254  const pmUnits &units,
255  instance_domain * const domain = NULL,
256  const std::string &short_description = std::string(),
257  const std::string &verbose_description = std::string(),
258  void * const opaque = NULL,
259  const metric_flags flags = static_cast<metric_flags>(0))
260  {
261  insert(value_type(item_id, metric_description(metric_name, type, semantic,
262  units, domain, short_description, verbose_description, opaque, flags)));
263  return *this;
264  }
265 
266 private:
267  const cluster_id_type cluster_id; ///< The ID of this cluster.
268  const std::string cluster_name; ///< The name of this cluster.
269 
270 };
271 
272 /**
273  * @brief Collection of clusters of metric descriptions.
274  */
275 class metrics_description : public std::map<cluster_id_type, metric_cluster> {
276 
277 public:
278 
279  /**
280  * @brief Default constructor.
281  *
282  * Constructs an empty metrics_description class - ie one with no metric
283  * clusters yet.
284  */
285  explicit metrics_description() :
287  most_recent_cluster(end())
288  {
289 
290  }
291 
292  /**
293  * @brief Cluster insertion functor.
294  *
295  * This functor inserts a new empty metric cluster into this object. It also
296  * records the insertion iterator so that future calls to any of the metric
297  * insertion functors add metrics to the new cluster inserted by this
298  * function.
299  *
300  * @param cluster_id ID of the new cluster to insert.
301  * @param cluster_name Optional name of the cluster to insert.
302  *
303  * @return A reference to this metrics_description object.
304  */
306  const std::string &cluster_name = std::string())
307  {
308  most_recent_cluster = insert(value_type(cluster_id,
309  metric_cluster(cluster_id, cluster_name))).first;
310  return *this;
311  }
312 
313  /**
314  * @brief Metric description insertion functor.
315  *
316  * This functor inserts a metric description in the most recently inserted
317  * cluster.
318  *
319  * The cluster insertion function must be called at least once prior to
320  * calling this, or any of the other metric description insertion functors,
321  * otherwise an exception will be throw.
322  *
323  * @param item_id Metric ID.
324  * @param metric_name Metric name.
325  * @param type Metric atom type.
326  * @param semantic PCP metric semantic.
327  * @param units PCP metric units.
328  * @param flags Optional metric flags.
329  * @param domain Optional metric instance domain.
330  * @param short_description Short metric description.
331  * @param verbose_description Verbose metric description.
332  * @param opaque Optional opaque pointer to track.
333  *
334  * @throw pcp::exception If no metric cluster has been inserted yet.
335  *
336  * @return A reference to this metrics_description object.
337  */
339  const std::string &metric_name,
340  const atom_type_type type,
341  const semantic_type semantic,
342  const pmUnits &units,
343  const metric_flags flags,
344  instance_domain * const domain = NULL,
345  const std::string &short_description = std::string(),
346  const std::string &verbose_description = std::string(),
347  void * const opaque = NULL)
348  {
349  if (most_recent_cluster == end()) {
350  throw pcp::exception(PM_ERR_GENERIC, "no cluster to add metric to");
351  }
352  most_recent_cluster->second(item_id, metric_name, type, semantic,
353  units, domain, short_description,
354  verbose_description, opaque, flags);
355  return *this;
356  }
357 
358  /**
359  * @brief Metric description insertion functor.
360  *
361  * This functor inserts a metric description in the most recently inserted
362  * cluster.
363  *
364  * The cluster insertion function must be called at least once prior to
365  * calling this, or any of the other metric description insertion functors,
366  * otherwise an exception will be throw.
367  *
368  * @param item_id Metric ID.
369  * @param metric_name Metric name.
370  * @param type Metric atom type.
371  * @param semantic PCP metric semantic.
372  * @param units PCP metric units.
373  * @param domain Optional metric instance domain.
374  * @param flags Optional metric flags.
375  * @param short_description Short metric description.
376  * @param verbose_description Verbose metric description.
377  * @param opaque Optional opaque pointer to track.
378  *
379  * @throw pcp::exception If no metric cluster has been inserted yet.
380  *
381  * @return A reference to this metrics_description object.
382  */
384  const std::string &metric_name,
385  const atom_type_type type,
386  const semantic_type semantic,
387  const pmUnits &units,
388  instance_domain * const domain,
389  const metric_flags flags,
390  const std::string &short_description = std::string(),
391  const std::string &verbose_description = std::string(),
392  void * const opaque = NULL)
393  {
394  if (most_recent_cluster == end()) {
395  throw pcp::exception(PM_ERR_GENERIC, "no cluster to add metric to");
396  }
397  most_recent_cluster->second(item_id, metric_name, type, semantic,
398  units, domain, short_description,
399  verbose_description, opaque, flags);
400  return *this;
401  }
402 
403  /**
404  * @brief Metric description insertion functor.
405  *
406  * This functor inserts a metric description in the most recently inserted
407  * cluster.
408  *
409  * The cluster insertion function must be called at least once prior to
410  * calling this, or any of the other metric description insertion functors,
411  * otherwise an exception will be throw.
412  *
413  * @param item_id Metric ID.
414  * @param metric_name Metric name.
415  * @param type Metric atom type.
416  * @param semantic PCP metric semantic.
417  * @param units PCP metric units.
418  * @param domain Optional metric instance domain.
419  * @param short_description Short metric description.
420  * @param verbose_description Verbose metric description.
421  * @param opaque Optional opaque pointer to track.
422  * @param flags Optional metric flags.
423  *
424  * @throw pcp::exception If no metric cluster has been inserted yet.
425  *
426  * @return A reference to this metrics_description object.
427  */
429  const std::string &metric_name,
430  const atom_type_type type,
431  const semantic_type semantic,
432  const pmUnits &units,
433  instance_domain * const domain = NULL,
434  const std::string &short_description = std::string(),
435  const std::string &verbose_description = std::string(),
436  void * const opaque = NULL,
437  const metric_flags flags = static_cast<metric_flags>(0))
438  {
439  if (most_recent_cluster == end()) {
440  throw pcp::exception(PM_ERR_GENERIC, "no cluster to add metric to");
441  }
442  most_recent_cluster->second(item_id, metric_name, type, semantic,
443  units, domain, short_description,
444  verbose_description, opaque, flags);
445  return *this;
446  }
447 
448 private:
449  iterator most_recent_cluster; ///< The most-recently inserted cluster.
450 };
451 
452 } // pcp namespace.
453 
454 PCP_CPP_END_NAMESPACE
455 
456 #endif
cluster_id_type get_cluster_id() const
Get this cluster&#39;s ID.
uint_fast8_t semantic_type
PM_SEM_* (0 - 4)
Definition: types.hpp:27
metrics_description & operator()(const item_id_type item_id, const std::string &metric_name, const atom_type_type type, const semantic_type semantic, const pmUnits &units, instance_domain *const domain=NULL, const std::string &short_description=std::string(), const std::string &verbose_description=std::string(), void *const opaque=NULL, const metric_flags flags=static_cast< metric_flags >(0))
Metric description insertion functor.
Definition: atom.hpp:20
std::string get_cluster_name() const
Get this cluster&#39;s name.
metrics_description & operator()(const item_id_type item_id, const std::string &metric_name, const atom_type_type type, const semantic_type semantic, const pmUnits &units, const metric_flags flags, instance_domain *const domain=NULL, const std::string &short_description=std::string(), const std::string &verbose_description=std::string(), void *const opaque=NULL)
Metric description insertion functor.
std::string metric_name
This metric&#39;s name.
metrics_description & operator()(const item_id_type item_id, const std::string &metric_name, const atom_type_type type, const semantic_type semantic, const pmUnits &units, instance_domain *const domain, const metric_flags flags, const std::string &short_description=std::string(), const std::string &verbose_description=std::string(), void *const opaque=NULL)
Metric description insertion functor.
metric_flags operator|(metric_flags a, metric_flags b)
Pipe operator for combining metric_flags values.
A cluster of metric descriptions.
Individual metric description.
STL namespace.
metric_flags flags
Optional flags for this metric.
uint_fast8_t atom_type_type
PM_TYPE_* (0 - 9)
Definition: types.hpp:22
void *const opaque
Opaque value to track with this metric.
Sets up common PMDA++ library macros.
Declares various types used throughout the PMDA++ library.
metric_flags
Flags that may be applied to metric descriptions.
metric_cluster & operator()(const item_id_type item_id, const std::string &metric_name, const atom_type_type type, const semantic_type semantic, const pmUnits &units, const metric_flags flags=static_cast< metric_flags >(0), instance_domain *const domain=NULL, const std::string &short_description=std::string(), const std::string &verbose_description=std::string(), void *const opaque=NULL)
Metric insertion functor.
std::string verbose_description
This metric&#39;s verbose description.
Base class for all PMDA++ exceptions.
Definition: exception.hpp:25
Metric supports pmstore operations.
metric_description(const std::string &metric_name, const atom_type_type type, const semantic_type semantic, const pmUnits &units, instance_domain *const domain=NULL, const std::string &short_description=std::string(), const std::string &verbose_description=std::string(), void *const opaque=NULL, const metric_flags flags=static_cast< metric_flags >(0))
Constructor.
metric_cluster & operator()(const item_id_type item_id, const std::string &metric_name, const atom_type_type type, const semantic_type semantic, const pmUnits &units, instance_domain *const domain=NULL, const std::string &short_description=std::string(), const std::string &verbose_description=std::string(), void *const opaque=NULL, const metric_flags flags=static_cast< metric_flags >(0))
Metric insertion functor.
instance_domain * domain
Optional instance domain for this metric.
metric_cluster(const cluster_id_type id, const std::string &name)
Constructor.
atom_type_type type
This metric&#39;s atom type.
Performance metric instance domain.
metric_cluster & operator()(const item_id_type item_id, const std::string &metric_name, const atom_type_type type, const semantic_type semantic, const pmUnits &units, instance_domain *const domain, const metric_flags flags=static_cast< metric_flags >(0), const std::string &short_description=std::string(), const std::string &verbose_description=std::string(), void *const opaque=NULL)
Metric insertion functor.
metrics_description & operator()(const cluster_id_type cluster_id, const std::string &cluster_name=std::string())
Cluster insertion functor.
uint_fast16_t item_id_type
__pmID_int::item (10-bits)
Definition: types.hpp:26
std::string short_description
This metric&#39;s short description.
metrics_description()
Default constructor.
Defines the pcp::exception class.
Collection of clusters of metric descriptions.
pmUnits units
This metric&#39;s PCP units.
uint_fast16_t cluster_id_type
__pmID_int::cluster (12-bits)
Definition: types.hpp:23
semantic_type semantic
This metric&#39;s PCP semantic.