CUBRID Engine  latest
perf.cpp
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.cpp - implementation of performance statistics basic utilities
21  */
22 
23 #include "perf.hpp"
24 
25 #include "error_manager.h"
26 
27 #include <stdexcept>
28 
29 #include <cstring>
30 
31 namespace cubperf
32 {
34  // stat_definition
37  {
38  // nothing
39  }
40 
41  stat_definition::stat_definition (const stat_id idref, type stat_type, const char *first_name,
42  const char *second_name /* = NULL */)
43  : m_id (idref)
44  , m_type (stat_type)
45  , m_names { first_name, second_name }
46  , m_offset (0)
47  {
48  //
49  }
50 
52  : m_id (other.m_id)
53  , m_type (other.m_type)
54  , m_names { other.m_names[0], other.m_names[1] }
55  , m_offset (0)
56  {
57  //
58  }
59 
62  {
63  m_id = other.m_id;
64  m_type = other.m_type;
65  for (std::size_t i = 0; i < MAX_VALUE_COUNT; ++i)
66  {
67  m_names[i] = other.m_names[i];
68  }
69  m_offset = 0;
70 
71  return *this;
72  }
73 
74  std::size_t
76  {
77  return m_type == type::COUNTER_AND_TIMER ? 2 : 1;
78  }
79 
81  // statset_definition
83 
84  statset_definition::statset_definition (std::initializer_list<stat_definition> defs)
85  : m_stat_count (defs.size ())
86  , m_value_count (0)
87  , m_stat_defs (NULL)
88  , m_value_names (NULL)
89  {
90  // copy definitions
91  m_stat_defs = new stat_definition[defs.size ()];
92  std::size_t stat_index = 0;
93  for (auto &def_it : defs)
94  {
95  if (def_it.m_id != stat_index)
96  {
97  // statset_definition is bad; crash program
98  throw std::runtime_error ("statset_definition is bad");
99  }
100  m_stat_defs[stat_index] = def_it; // copy definitions
101 
102  // set offset and increment value count
103  m_stat_defs[stat_index].m_offset = m_value_count;
104  m_value_count += def_it.get_value_count ();
105 
106  // increment index
107  stat_index++;
108  }
109 
110  // names for all values
111  m_value_names = new std::string[m_value_count];
112  std::size_t value_index = 0;
113  for (stat_index = 0; stat_index < m_stat_count; stat_index++)
114  {
115  assert (value_index == m_stat_defs[stat_index].m_offset);
116  for (std::size_t def_name_index = 0; def_name_index < m_stat_defs[stat_index].get_value_count ();
117  def_name_index++)
118  {
119  m_value_names[value_index++] = m_stat_defs[stat_index].m_names[def_name_index];
120  }
121  }
122  }
123 
125  {
126  delete [] m_stat_defs;
127  delete [] m_value_names;
128  }
129 
130  statset *
132  {
133  return new statset (get_value_count ());
134  }
135 
138  {
139  return new atomic_statset (get_value_count ());
140  }
141 
142  std::size_t
144  {
145  return m_stat_count;
146  }
147 
148  std::size_t
150  {
151  return m_value_count;
152  }
153 
154  const char *
155  statset_definition::get_value_name (std::size_t value_index) const
156  {
157  return m_value_names[value_index].c_str ();
158  }
159 
160  std::size_t
162  {
163  return get_value_count () * sizeof (stat_value);
164  }
165 
166  void
167  cubperf::statset_definition::get_stat_values (const statset &statsetr, stat_value *output_stats) const
168  {
169  std::memcpy (output_stats, statsetr.m_values, get_values_memsize ());
170  }
171 
172  void
174  {
175  for (std::size_t it = 0; it < get_value_count (); it++)
176  {
177  output_stats[it] = statsetr.m_values[it];
178  }
179  }
180 
181  void
182  cubperf::statset_definition::add_stat_values (const statset &statsetr, stat_value *output_stats) const
183  {
184  for (std::size_t it = 0; it < get_value_count (); it++)
185  {
186  output_stats[it] += statsetr.m_values[it];
187  }
188  }
189 
190  void
192  {
193  for (std::size_t it = 0; it < get_value_count (); it++)
194  {
195  output_stats[it] += statsetr.m_values[it];
196  }
197  }
198 
199 } // namespace cubperf
generic_value< false > stat_value
Definition: perf_def.hpp:46
stat_definition * m_stat_defs
Definition: perf.hpp:231
generic_statset< true > atomic_statset
Definition: perf_def.hpp:74
std::size_t stat_id
Definition: perf_def.hpp:50
std::size_t m_value_count
Definition: perf.hpp:230
static const std::size_t MAX_VALUE_COUNT
Definition: perf.hpp:131
stat_definition & operator=(const stat_definition &other)
Definition: perf.cpp:61
std::string * m_value_names
Definition: perf.hpp:232
atomic_statset * create_atomic_statset(void) const
Definition: perf.cpp:137
std::size_t get_value_count() const
Definition: perf.cpp:149
#define assert(x)
statset * create_statset(void) const
Definition: perf.cpp:131
std::size_t get_value_count(void) const
Definition: perf.cpp:75
Definition: perf.cpp:31
void add_stat_values(const statset &statsetr, stat_value *output_stats) const
Definition: perf.cpp:182
std::size_t get_stat_count() const
Definition: perf.cpp:143
#define NULL
Definition: freelistheap.h:34
std::size_t m_offset
Definition: perf.hpp:136
void get_stat_values(const statset &statsetr, stat_value *output_stats) const
Definition: perf.cpp:167
generic_value< IsAtomic > * m_values
Definition: perf_def.hpp:58
std::size_t m_stat_count
Definition: perf.hpp:229
const char * get_value_name(std::size_t value_index) const
Definition: perf.cpp:155
generic_statset< false > statset
Definition: perf_def.hpp:73
int i
Definition: dynamic_load.c:954
std::size_t get_values_memsize(void) const
Definition: perf.cpp:161
const char * m_names[MAX_VALUE_COUNT]
Definition: perf.hpp:135