CernVM-FS  2.9.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
statistics.cc
Go to the documentation of this file.
1 
5 #include "statistics.h"
6 
7 #include <algorithm>
8 #include <cassert>
9 
10 #include "json_document_write.h"
11 #include "platform.h"
12 #include "smalloc.h"
13 #include "util/string.h"
14 #include "util_concurrency.h"
15 
16 using namespace std; // NOLINT
17 
18 #ifdef CVMFS_NAMESPACE_GUARD
19 namespace CVMFS_NAMESPACE_GUARD {
20 #endif
21 
22 namespace perf {
23 
24 std::string Counter::ToString() { return StringifyInt(Get()); }
25 std::string Counter::Print() { return StringifyInt(Get()); }
26 std::string Counter::PrintK() { return StringifyInt(Get() / 1000); }
27 std::string Counter::PrintKi() { return StringifyInt(Get() / 1024); }
28 std::string Counter::PrintM() { return StringifyInt(Get() / (1000 * 1000)); }
29 std::string Counter::PrintMi() { return StringifyInt(Get() / (1024 * 1024)); }
30 std::string Counter::PrintRatio(Counter divider) {
31  double enumerator_value = Get();
32  double divider_value = divider.Get();
33  return StringifyDouble(enumerator_value / divider_value);
34 }
35 
36 
37 //-----------------------------------------------------------------------------
38 
39 
47 Statistics *Statistics::Fork() {
48  Statistics *child = new Statistics();
49 
50  MutexLockGuard lock_guard(lock_);
51  for (map<string, CounterInfo *>::iterator i = counters_.begin(),
52  iEnd = counters_.end(); i != iEnd; ++i)
53  {
54  atomic_inc32(&i->second->refcnt);
55  }
56  child->counters_ = counters_;
57 
58  return child;
59 }
60 
61 
62 Counter *Statistics::Lookup(const std::string &name) const {
63  MutexLockGuard lock_guard(lock_);
64  map<string, CounterInfo *>::const_iterator i = counters_.find(name);
65  if (i != counters_.end())
66  return &i->second->counter;
67  return NULL;
68 }
69 
70 
71 string Statistics::LookupDesc(const std::string &name) {
72  MutexLockGuard lock_guard(lock_);
73  map<string, CounterInfo *>::const_iterator i = counters_.find(name);
74  if (i != counters_.end())
75  return i->second->desc;
76  return "";
77 }
78 
79 string Statistics::PrintList(const PrintOptions print_options) {
80  string result;
81  if (print_options == kPrintHeader)
82  result += "Name|Value|Description\n";
83 
84  MutexLockGuard lock_guard(lock_);
85  for (map<string, CounterInfo *>::const_iterator i = counters_.begin(),
86  iEnd = counters_.end(); i != iEnd; ++i)
87  {
88  result += i->first + "|" + i->second->counter.ToString() +
89  "|" + i->second->desc + "\n";
90  }
91  return result;
92 }
93 
106 string Statistics::PrintJSON() {
107  MutexLockGuard lock_guard(lock_);
108 
109  JsonStringGenerator json_statistics;
110 
111  // Make use of std::map key ordering and add counters namespace by namespace
112  JsonStringGenerator json_statistics_namespace;
113  std::string last_namespace = "";
114  for (map<string, CounterInfo *>::const_iterator i = counters_.begin(),
115  iEnd = counters_.end();
116  i != iEnd; ++i) {
117  std::vector<std::string> tokens = SplitString(i->first, '.');
118 
119  if (tokens[0] != last_namespace) {
120  if (last_namespace != "") {
121  json_statistics.AddJsonObject(last_namespace,
122  json_statistics_namespace.GenerateString());
123  }
124  json_statistics_namespace.Clear();
125  }
126 
127  json_statistics_namespace.Add(tokens[1], i->second->counter.Get());
128 
129  last_namespace = tokens[0];
130  }
131  if (last_namespace != "") {
132  json_statistics.AddJsonObject(last_namespace,
133  json_statistics_namespace.GenerateString());
134  }
135 
136  return json_statistics.GenerateString();
137 }
138 
139 Counter *Statistics::Register(const string &name, const string &desc) {
140  MutexLockGuard lock_guard(lock_);
141  assert(counters_.find(name) == counters_.end());
142  CounterInfo *counter_info = new CounterInfo(desc);
143  counters_[name] = counter_info;
144  return &counter_info->counter;
145 }
146 
147 
148 Statistics::Statistics() {
149  lock_ =
150  reinterpret_cast<pthread_mutex_t *>(smalloc(sizeof(pthread_mutex_t)));
151  int retval = pthread_mutex_init(lock_, NULL);
152  assert(retval == 0);
153 }
154 
155 
156 Statistics::~Statistics() {
157  for (map<string, CounterInfo *>::iterator i = counters_.begin(),
158  iEnd = counters_.end(); i != iEnd; ++i)
159  {
160  int32_t old_value = atomic_xadd32(&i->second->refcnt, -1);
161  if (old_value == 1)
162  delete i->second;
163  }
164  pthread_mutex_destroy(lock_);
165  free(lock_);
166 }
167 
168 
169 //------------------------------------------------------------------------------
170 
171 
175 Recorder::Recorder(uint32_t resolution_s, uint32_t capacity_s)
176  : last_timestamp_(0)
177  , capacity_s_(capacity_s)
178  , resolution_s_(resolution_s)
179 {
180  assert((resolution_s > 0) && (capacity_s > resolution_s));
181  bool has_remainder = (capacity_s_ % resolution_s_) != 0;
182  if (has_remainder) {
184  }
186  bins_.reserve(no_bins_);
187  for (unsigned i = 0; i < no_bins_; ++i)
188  bins_.push_back(0);
189 }
190 
191 
194 }
195 
196 
197 void Recorder::TickAt(uint64_t timestamp) {
198  uint64_t bin_abs = timestamp / resolution_s_;
199  uint64_t last_bin_abs = last_timestamp_ / resolution_s_;
200 
201  // timestamp in the past: don't update last_timestamp_
202  if (bin_abs < last_bin_abs) {
203  // Do we still remember this event?
204  if ((last_bin_abs - bin_abs) < no_bins_)
205  bins_[bin_abs % no_bins_]++;
206  return;
207  }
208 
209  if (last_bin_abs == bin_abs) {
210  bins_[bin_abs % no_bins_]++;
211  } else {
212  // When clearing bins between last_timestamp_ and now, avoid cycling the
213  // ring buffer multiple times.
214  unsigned max_bins_clear = std::min(bin_abs, last_bin_abs + no_bins_ + 1);
215  for (uint64_t i = last_bin_abs + 1; i < max_bins_clear; ++i)
216  bins_[i % no_bins_] = 0;
217  bins_[bin_abs % no_bins_] = 1;
218  }
219 
220  last_timestamp_ = timestamp;
221 }
222 
223 
224 uint64_t Recorder::GetNoTicks(uint32_t retrospect_s) const {
225  uint64_t now = platform_monotonic_time();
226  if (retrospect_s > now)
227  retrospect_s = now;
228 
229  uint64_t last_bin_abs = last_timestamp_ / resolution_s_;
230  uint64_t past_bin_abs = (now - retrospect_s) / resolution_s_;
231  int64_t min_bin_abs =
232  std::max(past_bin_abs,
233  (last_bin_abs < no_bins_) ? 0 : (last_bin_abs - (no_bins_ - 1)));
234  uint64_t result = 0;
235  for (int64_t i = last_bin_abs; i >= min_bin_abs; --i) {
236  result += bins_[i % no_bins_];
237  }
238 
239  return result;
240 }
241 
242 
243 //------------------------------------------------------------------------------
244 
245 
246 void MultiRecorder::AddRecorder(uint32_t resolution_s, uint32_t capacity_s) {
247  recorders_.push_back(Recorder(resolution_s, capacity_s));
248 }
249 
250 
251 uint64_t MultiRecorder::GetNoTicks(uint32_t retrospect_s) const {
252  unsigned N = recorders_.size();
253  for (unsigned i = 0; i < N; ++i) {
254  if ( (recorders_[i].capacity_s() >= retrospect_s) ||
255  (i == (N - 1)) )
256  {
257  return recorders_[i].GetNoTicks(retrospect_s);
258  }
259  }
260  return 0;
261 }
262 
263 
265  uint64_t now = platform_monotonic_time();
266  for (unsigned i = 0; i < recorders_.size(); ++i)
267  recorders_[i].TickAt(now);
268 }
269 
270 
271 void MultiRecorder::TickAt(uint64_t timestamp) {
272  for (unsigned i = 0; i < recorders_.size(); ++i)
273  recorders_[i].TickAt(timestamp);
274 }
275 
276 } // namespace perf
277 
278 
279 #ifdef CVMFS_NAMESPACE_GUARD
280 } // namespace CVMFS_NAMESPACE_GUARD
281 #endif
std::map< std::string, CounterInfo * > counters_
Definition: statistics.h:88
void AddRecorder(uint32_t resolution_s, uint32_t capacity_s)
Definition: statistics.cc:246
void Add(const std::string &key, const std::string &val)
void TickAt(uint64_t timestamp)
Definition: statistics.cc:197
vector< string > SplitString(const string &str, const char delim, const unsigned max_chunks)
Definition: string.cc:287
uint64_t last_timestamp_
Definition: statistics.h:201
assert((mem||(size==0))&&"Out Of Memory")
string StringifyDouble(const double value)
Definition: string.cc:95
std::vector< uint32_t > bins_
Definition: statistics.h:196
std::vector< Recorder > recorders_
Definition: statistics.h:235
string StringifyInt(const int64_t value)
Definition: string.cc:77
uint64_t platform_monotonic_time()
int64_t Get()
Definition: statistics.h:32
uint32_t resolution_s_
Definition: statistics.h:211
uint64_t GetNoTicks(uint32_t retrospect_s) const
Definition: statistics.cc:224
uint32_t capacity_s_
Definition: statistics.h:206
uint64_t GetNoTicks(uint32_t retrospect_s) const
Definition: statistics.cc:251
unsigned no_bins_
Definition: statistics.h:216
void TickAt(uint64_t timestamp)
Definition: statistics.cc:271
std::string GenerateString() const
void AddJsonObject(const std::string &key, const std::string &json)