CernVM-FS  2.13.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 "util/concurrency.h"
12 #include "util/platform.h"
13 #include "util/smalloc.h"
14 #include "util/string.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();
53  i != iEnd;
54  ++i) {
55  atomic_inc32(&i->second->refcnt);
56  }
57  child->counters_ = counters_;
58 
59  return child;
60 }
61 
62 
63 Counter *Statistics::Lookup(const std::string &name) const {
64  MutexLockGuard lock_guard(lock_);
65  map<string, CounterInfo *>::const_iterator i = counters_.find(name);
66  if (i != counters_.end())
67  return &i->second->counter;
68  return NULL;
69 }
70 
71 
72 string Statistics::LookupDesc(const std::string &name) {
73  MutexLockGuard lock_guard(lock_);
74  map<string, CounterInfo *>::const_iterator i = counters_.find(name);
75  if (i != counters_.end())
76  return i->second->desc;
77  return "";
78 }
79 
80 string Statistics::PrintList(const PrintOptions print_options) {
81  string result;
82  if (print_options == kPrintHeader)
83  result += "Name|Value|Description\n";
84 
85  MutexLockGuard lock_guard(lock_);
86  for (map<string, CounterInfo *>::const_iterator i = counters_.begin(),
87  iEnd = counters_.end();
88  i != iEnd;
89  ++i) {
90  result += i->first + "|" + i->second->counter.ToString() + "|"
91  + i->second->desc + "\n";
92  }
93  return result;
94 }
95 
108 string Statistics::PrintJSON() {
109  MutexLockGuard lock_guard(lock_);
110 
111  JsonStringGenerator json_statistics;
112 
113  // Make use of std::map key ordering and add counters namespace by namespace
114  JsonStringGenerator json_statistics_namespace;
115  std::string last_namespace = "";
116  for (map<string, CounterInfo *>::const_iterator i = counters_.begin(),
117  iEnd = counters_.end();
118  i != iEnd;
119  ++i) {
120  std::vector<std::string> tokens = SplitString(i->first, '.');
121 
122  if (tokens[0] != last_namespace) {
123  if (last_namespace != "") {
124  json_statistics.AddJsonObject(
125  last_namespace, json_statistics_namespace.GenerateString());
126  }
127  json_statistics_namespace.Clear();
128  }
129 
130  json_statistics_namespace.Add(tokens[1], i->second->counter.Get());
131 
132  last_namespace = tokens[0];
133  }
134  if (last_namespace != "") {
135  json_statistics.AddJsonObject(last_namespace,
136  json_statistics_namespace.GenerateString());
137  }
138 
139  return json_statistics.GenerateString();
140 }
141 
150 void Statistics::SnapshotCounters(std::map<std::string, int64_t> *counters,
151  uint64_t *timestamp_ns) {
152  MutexLockGuard lock_guard(lock_);
153  *timestamp_ns = platform_realtime_ns();
154  for (map<string, CounterInfo *>::const_iterator i = counters_.begin(),
155  iEnd = counters_.end();
156  i != iEnd;
157  ++i) {
158  // modify or insert
159  (*counters)[i->first] = (*i->second).counter.Get();
160  }
161 }
162 
163 Counter *Statistics::Register(const string &name, const string &desc) {
164  MutexLockGuard lock_guard(lock_);
165  assert(counters_.find(name) == counters_.end());
166  CounterInfo *counter_info = new CounterInfo(desc);
167  counters_[name] = counter_info;
168  return &counter_info->counter;
169 }
170 
171 
172 Statistics::Statistics() {
173  lock_ = reinterpret_cast<pthread_mutex_t *>(smalloc(sizeof(pthread_mutex_t)));
174  int retval = pthread_mutex_init(lock_, NULL);
175  assert(retval == 0);
176 }
177 
178 
179 Statistics::~Statistics() {
180  for (map<string, CounterInfo *>::iterator i = counters_.begin(),
181  iEnd = counters_.end();
182  i != iEnd;
183  ++i) {
184  int32_t old_value = atomic_xadd32(&i->second->refcnt, -1);
185  if (old_value == 1)
186  delete i->second;
187  }
188  pthread_mutex_destroy(lock_);
189  free(lock_);
190 }
191 
192 
193 //------------------------------------------------------------------------------
194 
195 
199 Recorder::Recorder(uint32_t resolution_s, uint32_t capacity_s)
200  : last_timestamp_(0), capacity_s_(capacity_s), resolution_s_(resolution_s) {
201  assert((resolution_s > 0) && (capacity_s > resolution_s));
202  bool has_remainder = (capacity_s_ % resolution_s_) != 0;
203  if (has_remainder) {
205  }
207  bins_.reserve(no_bins_);
208  for (unsigned i = 0; i < no_bins_; ++i)
209  bins_.push_back(0);
210 }
211 
212 
214 
215 
216 void Recorder::TickAt(uint64_t timestamp) {
217  uint64_t bin_abs = timestamp / resolution_s_;
218  uint64_t last_bin_abs = last_timestamp_ / resolution_s_;
219 
220  // timestamp in the past: don't update last_timestamp_
221  if (bin_abs < last_bin_abs) {
222  // Do we still remember this event?
223  if ((last_bin_abs - bin_abs) < no_bins_)
224  bins_[bin_abs % no_bins_]++;
225  return;
226  }
227 
228  if (last_bin_abs == bin_abs) {
229  bins_[bin_abs % no_bins_]++;
230  } else {
231  // When clearing bins between last_timestamp_ and now, avoid cycling the
232  // ring buffer multiple times.
233  unsigned max_bins_clear = std::min(bin_abs, last_bin_abs + no_bins_ + 1);
234  for (uint64_t i = last_bin_abs + 1; i < max_bins_clear; ++i)
235  bins_[i % no_bins_] = 0;
236  bins_[bin_abs % no_bins_] = 1;
237  }
238 
239  last_timestamp_ = timestamp;
240 }
241 
242 
243 uint64_t Recorder::GetNoTicks(uint32_t retrospect_s) const {
244  uint64_t now = platform_monotonic_time();
245  if (retrospect_s > now)
246  retrospect_s = now;
247 
248  uint64_t last_bin_abs = last_timestamp_ / resolution_s_;
249  uint64_t past_bin_abs = (now - retrospect_s) / resolution_s_;
250  int64_t min_bin_abs = std::max(
251  past_bin_abs,
252  (last_bin_abs < no_bins_) ? 0 : (last_bin_abs - (no_bins_ - 1)));
253  uint64_t result = 0;
254  for (int64_t i = last_bin_abs; i >= min_bin_abs; --i) {
255  result += bins_[i % no_bins_];
256  }
257 
258  return result;
259 }
260 
261 
262 //------------------------------------------------------------------------------
263 
264 
265 void MultiRecorder::AddRecorder(uint32_t resolution_s, uint32_t capacity_s) {
266  recorders_.push_back(Recorder(resolution_s, capacity_s));
267 }
268 
269 
270 uint64_t MultiRecorder::GetNoTicks(uint32_t retrospect_s) const {
271  unsigned N = recorders_.size();
272  for (unsigned i = 0; i < N; ++i) {
273  if ((recorders_[i].capacity_s() >= retrospect_s) || (i == (N - 1))) {
274  return recorders_[i].GetNoTicks(retrospect_s);
275  }
276  }
277  return 0;
278 }
279 
280 
282  uint64_t now = platform_monotonic_time();
283  for (unsigned i = 0; i < recorders_.size(); ++i)
284  recorders_[i].TickAt(now);
285 }
286 
287 
288 void MultiRecorder::TickAt(uint64_t timestamp) {
289  for (unsigned i = 0; i < recorders_.size(); ++i)
290  recorders_[i].TickAt(timestamp);
291 }
292 
293 } // namespace perf
294 
295 
296 #ifdef CVMFS_NAMESPACE_GUARD
297 } // namespace CVMFS_NAMESPACE_GUARD
298 #endif
std::map< std::string, CounterInfo * > counters_
Definition: statistics.h:90
void AddRecorder(uint32_t resolution_s, uint32_t capacity_s)
Definition: statistics.cc:265
void Add(const std::string &key, const std::string &val)
void TickAt(uint64_t timestamp)
Definition: statistics.cc:216
uint64_t last_timestamp_
Definition: statistics.h:197
assert((mem||(size==0))&&"Out Of Memory")
string StringifyDouble(const double value)
Definition: string.cc:95
vector< string > SplitString(const string &str, char delim)
Definition: string.cc:306
std::vector< uint32_t > bins_
Definition: statistics.h:192
std::vector< Recorder > recorders_
Definition: statistics.h:231
uint64_t platform_realtime_ns()
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:207
uint64_t GetNoTicks(uint32_t retrospect_s) const
Definition: statistics.cc:243
uint32_t capacity_s_
Definition: statistics.h:202
Definition: mutex.h:42
uint64_t GetNoTicks(uint32_t retrospect_s) const
Definition: statistics.cc:270
unsigned no_bins_
Definition: statistics.h:212
void TickAt(uint64_t timestamp)
Definition: statistics.cc:288
std::string GenerateString() const
void AddJsonObject(const std::string &key, const std::string &json)