CUBRID Engine  latest
monitor_collect.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2008 Search Solution Corporation
3  * Copyright 2016 CUBRID Corporation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 //
20 // monitor_collect.hpp - interface for collecting statistics
21 //
22 
23 #if !defined _MONITOR_COLLECT_HPP_
24 #define _MONITOR_COLLECT_HPP_
25 
26 #include "monitor_registration.hpp"
27 #include "monitor_statistic.hpp"
28 #include "monitor_transaction.hpp"
29 
30 namespace cubmonitor
31 {
32 
34  // grouped statistics
36 
37  class timer
38  {
39  public:
40  inline timer (void);
41 
42  inline void reset (void);
43  inline duration time (void);
44 
45  private:
47  };
48 
49 
51  // name array builder
52  //
53  // populate on basename and variadic list of prefixes.
55 
56  void
57  build_name_vector (std::vector<std::string> &names, const char *basename, const char *prefix);
58 
59  template <typename ... Args>
60  void
61  build_name_vector (std::vector<std::string> &names, const char *basename, const char *prefix, Args &&... args)
62  {
63  names.push_back (std::string (prefix) + basename);
64  build_name_vector (names, basename, args...);
65  }
66 
68  // Multi-statistics
69  //
70  // Group statistics together to provide detailed information about events
72 
74  // Timer statistics - one statistic based on time_rep
75  //
76  // T is template for time_rep based accumulator statistic
77  //
79  template <typename T = time_accumulator_statistic>
81  {
82  public:
83  class autotimer
84  {
85  public:
86  autotimer () = delete;
87  inline autotimer (timer_statistic &timer_stat, bool active = true);
88  inline ~autotimer ();
89  private:
91  bool m_active;
92  };
93 
94  timer_statistic (void);
95 
96  inline void time (const time_rep &d); // add duration to timer
97  inline void time (void); // add duration since last time () call to timer
98 
99  // fetching interface
100  inline void fetch (statistic_value *destination, fetch_mode mode = FETCH_GLOBAL) const;
101  inline std::size_t get_statistics_count (void) const;
102 
103  // get time
104  inline time_rep get_time (fetch_mode mode = FETCH_GLOBAL) const;
105 
106  inline void reset_timer ();
107 
108  private:
109  timer m_timer; // internal timer
110  T m_statistic; // time statistic
111  };
112  // explicit instantiations as time_accumulator[_atomic]_statistic with or without transaction sheets
117 
118  // aliases
123 
125  // Counter/timer statistic - two statistics that count and time events
126  //
127  // A is template for amount_rep based accumulator statistic
128  // T is template for time_rep based accumulator statistic
129  //
131  template <class A = amount_accumulator_statistic, class T = time_accumulator_statistic>
133  {
134  public:
135  // autotimer times and increments statistic on destructor
136  class autotimer
137  {
138  public:
139  autotimer () = delete;
140  inline autotimer (counter_timer_statistic &cts, bool active = true);
141  inline ~autotimer ();
142  private:
144  bool m_active;
145  };
146 
147  inline counter_timer_statistic (void);
148 
149  inline void time_and_increment (const time_rep &d, const amount_rep &a = 1); // add time and amount
150  inline void time_and_increment (const amount_rep &a = 1); // add internal time and amount
151 
152  inline void reset_timer ();
153 
154  // fetch interface
155  inline std::size_t get_statistics_count (void) const;
156  inline void fetch (statistic_value *destination, fetch_mode mode = FETCH_GLOBAL) const;
157 
158  // getters
159  inline amount_rep get_count (fetch_mode mode = FETCH_GLOBAL) const;
160  inline time_rep get_time (fetch_mode mode = FETCH_GLOBAL) const;
161  inline time_rep get_average_time (fetch_mode mode = FETCH_GLOBAL) const;
162 
163  // register statistic to monitor
164  // three statistics are registers: counter, total time and average time (total / count)
165  void register_to_monitor (monitor &mon, const char *basename) const;
166 
167  private:
168  timer m_timer; // internal timer
169  A m_amount_statistic; // amount accumulator
170  T m_time_statistic; // time accumulator
171  };
172  // explicit instantiations of counter_timer_statistic with atomic/non-atomic, with/without transactions
175 
180 
181  // aliases
185 
187  // Counter/timer/max statistic - three statistics that count, time and save events longest duration
188  //
189  // A is template for amount_rep based accumulator statistic
190  // T is template for time_rep based accumulator statistic
191  // M is template for time_rep based max statistic
193  template <class A = amount_accumulator_statistic, class T = time_accumulator_statistic, class M = time_max_statistic>
195  {
196  public:
197  inline counter_timer_max_statistic (void);
198 
199  inline void time_and_increment (const time_rep &d, const amount_rep &a = 1); // add time and amount
200  inline void time_and_increment (const amount_rep &a = 1); // add internal time and amount
201 
202  // fetch interface
203  inline std::size_t get_statistics_count (void) const;
204  inline void fetch (statistic_value *destination, fetch_mode mode = FETCH_GLOBAL) const;
205 
206  // getters
207  inline amount_rep get_count (fetch_mode mode = FETCH_GLOBAL) const;
208  inline time_rep get_time (fetch_mode mode = FETCH_GLOBAL) const;
209  inline time_rep get_average_time (fetch_mode mode = FETCH_GLOBAL) const;
210  inline time_rep get_max_time (fetch_mode mode = FETCH_GLOBAL) const;
211 
212  // register statistic to monitor
213  // three statistics are registers: counter, total time, max per unit time and average time (total / count)
214  void register_to_monitor (monitor &mon, const char *basename) const;
215 
216  private:
221  };
222  // explicit instantiations
228  transaction_statistic<time_accumulator_statistic>, transaction_statistic<time_max_statistic>>;
230  transaction_statistic<time_accumulator_atomic_statistic>, transaction_statistic<time_max_atomic_statistic>>;
231 
233  // template and inline implementation
235 
237  // timer_statistic
239 
240  template <typename T>
242  : m_timer ()
243  , m_statistic ()
244  {
245  //
246  }
247 
248  template <class T>
249  void
251  {
252  m_statistic.collect (d);
253  }
254 
255  template <class T>
256  void
258  {
259  m_statistic.collect (m_timer.time ()); // use internal timer
260  }
261 
262  template <class T>
263  std::size_t
265  {
266  return m_statistic.get_statistics_count ();
267  }
268 
269  template <class T>
270  void
271  timer_statistic<T>::fetch (statistic_value *destination, fetch_mode mode /* = FETCH_GLOBAL */) const
272  {
273  m_statistic.fetch (destination, mode);
274  }
275 
276  template <class T>
277  time_rep
278  timer_statistic<T>::get_time (fetch_mode mode /* = FETCH_GLOBAL */) const
279  {
280  return m_statistic.get_value (mode);
281  }
282 
283  template <class T>
284  void
286  {
287  m_timer.reset ();
288  }
289 
290  template <class T>
292  : m_stat (timer_stat)
293  , m_active (active)
294  {
295  if (m_active)
296  {
297  m_stat.reset_timer ();
298  }
299  }
300 
301  template <class T>
303  {
304  if (m_active)
305  {
306  m_stat.time ();
307  }
308  }
309 
311  // counter_timer_statistic
313 
314  template <class A, class T>
316  : m_timer ()
317  , m_amount_statistic ()
318  , m_time_statistic ()
319  {
320  //
321  }
322 
323  template <class A, class T>
324  void
326  {
327  m_amount_statistic.collect (a);
328  m_time_statistic.collect (d);
329  }
330 
331  template <class A, class T>
332  void
334  {
335  time_and_increment (m_timer.time (), a); // use internal timer
336  }
337 
338  template <class A, class T>
339  void
341  {
342  m_timer.reset ();
343  }
344 
345  template <class A, class T>
346  std::size_t
348  {
349  return m_amount_statistic.get_statistics_count () + m_time_statistic.get_statistics_count ();
350  }
351 
352  template <class A, class T>
353  void
354  counter_timer_statistic<A, T>::fetch (statistic_value *destination, fetch_mode mode /* = FETCH_GLOBAL */) const
355  {
356  std::size_t index = 0;
357 
358  m_amount_statistic.fetch (destination + index, mode);
359  index += m_amount_statistic.get_statistics_count ();
360 
361  m_time_statistic.fetch (destination + index, mode);
362  index += m_time_statistic.get_statistics_count ();
363 
364  assert (index == get_statistics_count ());
365  }
366 
367  template <class A, class T>
368  amount_rep
370  {
371  return m_amount_statistic.get_value (mode);
372  }
373 
374  template <class A, class T>
375  time_rep
377  {
378  return m_time_statistic.get_value (mode);
379  }
380 
381  template <class A, class T>
382  time_rep
384  {
385  return get_time (mode) / get_count (mode);
386  }
387 
388  template <class A, class T>
389  void
391  {
392  // we register counter, timer and average
393 
394  const char *count_prefix = "Num_";
395  const char *total_time_prefix = "Total_time_";
396  const char *average_time_prefix = "Avg_time_";
397  std::vector<std::string> names;
398  build_name_vector (names, basename, count_prefix, total_time_prefix, average_time_prefix);
399 
400  std::size_t stat_count = get_statistics_count () + 1;
401  assert (stat_count == names.size ());
402 
403  auto fetch_func = [&] (statistic_value * destination, fetch_mode mode)
404  {
405  this->fetch (destination, mode);
407  };
408  mon.register_statistics (stat_count, fetch_func, names);
409  }
410 
411  template <class A, class T>
413  : m_stat (cts)
414  , m_active (active)
415  {
416  if (m_active)
417  {
418  m_stat.reset_timer ();
419  }
420  }
421 
422  template <class A, class T>
424  {
425  if (m_active)
426  {
427  // will time duration from construction and increment once
429  }
430  }
431 
433  // counter_timer_max_statistic
435 
436  template <class A, class T, class M>
438  : m_timer ()
439  , m_amount_statistic ()
440  , m_total_time_statistic ()
441  , m_max_time_statistic ()
442  {
443  //
444  }
445 
446  template <class A, class T, class M>
447  void
449  {
450  m_amount_statistic.collect (a);
451  m_total_time_statistic.collect (d);
452  m_max_time_statistic.collect (d / a);
453  }
454 
455  template <class A, class T, class M>
456  void
458  {
460  }
461 
462  template <class A, class T, class M>
463  std::size_t
465  {
466  return m_amount_statistic.get_statistics_count ()
467  + m_total_time_statistic.get_statistics_count ()
468  + m_max_time_statistic.get_statistics_count ();
469  }
470 
471  template <class A, class T, class M>
472  void
474  fetch_mode mode /* = FETCH_GLOBAL */) const
475  {
476  std::size_t index = 0;
477 
478  m_amount_statistic.fetch (destination + index, mode);
479  index += m_amount_statistic.get_statistics_count ();
480 
481  m_total_time_statistic.fetch (destination + index, mode);
482  index += m_total_time_statistic.get_statistics_count ();
483 
484  m_max_time_statistic.fetch (destination + index, mode);
485  index += m_max_time_statistic.get_statistics_count ();
486 
487  assert (index == get_statistics_count ());
488  }
489 
490  template <class A, class T, class M>
491  amount_rep
493  {
494  return m_amount_statistic.get_value (mode);
495  }
496 
497  template <class A, class T, class M>
498  time_rep
500  {
501  return m_total_time_statistic.get_value (mode);
502  }
503 
504  template <class A, class T, class M>
505  time_rep
507  {
508  return m_max_time_statistic.get_value (mode);
509  }
510 
511  template <class A, class T, class M>
512  time_rep
514  {
515  return get_time (mode) / get_count (mode);
516  }
517 
518  template <class A, class T, class M>
519  void
521  {
522  // we register counter, timer, max and average
523 
524  const char *count_prefix = "Num_";
525  const char *total_time_prefix = "Total_time_";
526  const char *max_time_prefix = "Max_time_";
527  const char *average_time_prefix = "Avg_time_";
528  std::vector<std::string> names;
529  build_name_vector (names, basename, count_prefix, total_time_prefix, max_time_prefix, average_time_prefix);
530 
531  std::size_t stat_count = get_statistics_count () + 1;
532  assert (stat_count == names.size ());
533 
534  auto fetch_func = [&] (statistic_value * destination, fetch_mode mode)
535  {
536  this->fetch (destination, mode);
538  };
539  mon.register_statistics (stat_count, fetch_func, names);
540  }
541 
542  //
543  // timer
544  //
546  : m_timept (clock_type::now ())
547  {
548  //
549  }
550 
551  void
553  {
554  m_timept = clock_type::now ();
555  }
556 
557  duration
558  timer::time (void)
559  {
560  time_point start_pt = m_timept;
561  m_timept = clock_type::now ();
562  return m_timept - start_pt;
563  }
564 
565 } // namespace cubmonitor
566 
567 #endif // _MONITOR_COLLECT_HPP_
void time_and_increment(const time_rep &d, const amount_rep &a=1)
void fetch(statistic_value *destination, fetch_mode mode=FETCH_GLOBAL) const
accumulator_statistic< time_rep > time_accumulator_statistic
const fetch_mode FETCH_GLOBAL
time_rep get_time(fetch_mode mode=FETCH_GLOBAL) const
std::size_t get_statistics_count(void) const
std::uint64_t statistic_value
clock_type::duration duration
accumulator_atomic_statistic< time_rep > time_accumulator_atomic_statistic
std::chrono::high_resolution_clock clock_type
void fetch(statistic_value *destination, fetch_mode mode=FETCH_GLOBAL) const
amount_rep get_count(fetch_mode mode=FETCH_GLOBAL) const
void register_to_monitor(monitor &mon, const char *basename) const
static T_FETCH_FUNC fetch_func[]
Definition: cas_execute.c:364
#define assert(x)
void time(const time_rep &d)
time_rep get_average_time(fetch_mode mode=FETCH_GLOBAL) const
clock_type::time_point time_point
accumulator_atomic_statistic< amount_rep > amount_accumulator_atomic_statistic
static enum scanner_mode mode
void fetch(statistic_value *destination, fetch_mode mode=FETCH_GLOBAL) const
std::size_t get_statistics_count(void) const
static void get_time(struct timeval *start_time, char *time, int buf_len)
amount_rep get_count(fetch_mode mode=FETCH_GLOBAL) const
std::size_t get_statistics_count(void) const
time_rep get_max_time(fetch_mode mode=FETCH_GLOBAL) const
time_rep get_time(fetch_mode mode=FETCH_GLOBAL) const
statistic_value statistic_value_cast(const amount_rep &rep)
char * basename(const char *path)
Definition: porting.c:1132
void register_to_monitor(monitor &mon, const char *basename) const
duration time(void)
void build_name_vector(std::vector< std::string > &names, const char *basename, const char *prefix)
std::uint64_t amount_rep
time_rep get_average_time(fetch_mode mode=FETCH_GLOBAL) const
accumulator_statistic< amount_rep > amount_accumulator_statistic
void time_and_increment(const time_rep &d, const amount_rep &a=1)
void register_statistics(std::size_t statistics_count, const fetch_function &fetch_f, const std::vector< std::string > &names)
time_rep get_time(fetch_mode mode=FETCH_GLOBAL) const