CUBRID Engine  latest
perf_def.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  * perf_def.hpp - types and class definitions used perf module.
21  */
22 
23 #ifndef _PERF_DEF_HPP_
24 #define _PERF_DEF_HPP_
25 
26 #include <atomic>
27 #include <chrono>
28 
29 #include <cinttypes>
30 
31 // perf module basic types and classes
32 //
33 // read details in perf.hpp description comment
34 //
35 
36 namespace cubperf
37 {
38  // clocking
39  using clock = std::chrono::high_resolution_clock; // default clock
40  using time_point = clock::time_point; // default time point
41  using duration = clock::duration; // default duration
42 
43  // statistics value types
44  template <bool IsAtomic>
45  using generic_value = typename std::conditional<IsAtomic, std::atomic<std::uint64_t>, std::uint64_t>::type;
46  using stat_value = generic_value<false>; // non-atomic value
47  using atomic_stat_value = generic_value<true>; // atomic value
48 
49  // alias for std::size_t used in the context of statistic id
50  using stat_id = std::size_t;
51 
52  // a set of statistics values. can be atomic (that stores atomic_stat_value) or non-atomic (that stores stat_value).
53  template<bool IsAtomic>
55  {
56  public:
57  std::size_t m_value_count; // value count
58  generic_value<IsAtomic> *m_values; // statistics values
59  time_point m_timept; // internal time point; used as default starting time point for time and
60  // time_and_increment functions
61 
62  ~generic_statset (void); // destroy
63 
64  private:
65 
66  // classes that can construct me
67  friend class statset_definition;
68 
69  generic_statset (void) = delete; // no default constructor
70  generic_statset (std::size_t value_count); // construct with value count. it is private and statset_definition
71  // behaves like a factory for stat sets
72  };
73  using statset = generic_statset<false>; // non-atomic set of statistics values
74  using atomic_statset = generic_statset<true>; // atomic set of statistics values
75 
76  // generic_stat_counter - specialized one counter statistic
77  //
78  template <bool IsAtomic>
80  {
81  public:
82  generic_stat_counter (const char *name = NULL); // constructor with name
83 
84  inline void increment (stat_value incr = 1); // increment counter; default value of 1
85  stat_value get_count (void); // get current count
86  const char *get_name (void); // get statistic name
87 
88  private:
90  const char *m_stat_name; // statistic name
91  };
92 
93  // generic_stat_timer - specialized one timer statistic
94  //
95  template <bool IsAtomic>
97  {
98  public:
99  generic_stat_timer (const char *name = NULL); // constructor with name
100 
101  inline void time (duration d); // add duration to timer
102  inline void time (void); // add duration since last time () call to timer
103  stat_value get_time (void); // get current timer value
104  const char *get_name (void); // get statistic name
105 
106  private:
107  generic_value<IsAtomic> m_stat_value; // timer statistic value
108  const char *m_stat_name; // statistic name
109  time_point m_timept; // internal time point used and modified by time (void)
110  // can be manually reset by reset_timept.
111  };
112 
113  // generic_stat_counter_and_timer - specialized statistic to track a counter and timer
114  //
115  template <bool IsAtomic>
117  {
118  public:
119  // constructor with statistic names
120  generic_stat_counter_and_timer (const char *stat_counter_name, const char *stat_timer_name);
121  generic_stat_counter_and_timer (void); // default constructor with NULL names
122 
123  inline void time_and_increment (duration d, stat_value incr = 1); // add counter & duration
124  inline void time_and_increment (stat_value incr = 1); // add counter & duration since last
125  // time_and_increment (counter_val) call
126  stat_value get_count (void); // get current counter value
127  stat_value get_time (void); // get current timer value
128  const char *get_count_name (void); // get counter statistic name
129  const char *get_time_name (void); // get timer statistic name
130 
131  private:
134  };
135 
136  // non-atomic and atomic specialization of generic counter, timer and counter/timer
143 
144 } // namespace cubperf
145 
146 #endif // _PERF_DEF_HPP_
generic_value< IsAtomic > m_stat_value
Definition: perf_def.hpp:89
generic_value< false > stat_value
Definition: perf_def.hpp:46
typename std::conditional< IsAtomic, std::atomic< std::uint64_t >, std::uint64_t >::type generic_value
Definition: perf_def.hpp:45
std::size_t m_value_count
Definition: perf_def.hpp:57
std::size_t stat_id
Definition: perf_def.hpp:50
generic_value< true > atomic_stat_value
Definition: perf_def.hpp:47
generic_statset(void)=delete
clock::time_point time_point
Definition: perf_def.hpp:40
std::chrono::high_resolution_clock clock
Definition: perf_def.hpp:39
Definition: perf.cpp:31
#define NULL
Definition: freelistheap.h:34
static void get_time(struct timeval *start_time, char *time, int buf_len)
generic_value< IsAtomic > * m_values
Definition: perf_def.hpp:58
generic_stat_timer< IsAtomic > m_stat_timer
Definition: perf_def.hpp:133
clock::duration duration
Definition: perf_def.hpp:41
generic_stat_counter< IsAtomic > m_stat_counter
Definition: perf_def.hpp:132
generic_value< IsAtomic > m_stat_value
Definition: perf_def.hpp:107