CernVM-FS  2.12.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(); 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 
147 void Statistics::SnapshotCounters(
148  std::map<std::string, int64_t> *counters,
149  uint64_t *timestamp_ns) {
150  MutexLockGuard lock_guard(lock_);
151  *timestamp_ns = platform_realtime_ns();
152  for (map<string, CounterInfo *>::const_iterator i = counters_.begin(),
153  iEnd = counters_.end(); i != iEnd; ++i)
154  {
155  // modify or insert
156  (*counters)[i->first] = (*i->second).counter.Get();
157  }
158 }
159 
160 Counter *Statistics::Register(const string &name, const string &desc) {
161  MutexLockGuard lock_guard(lock_);
162  assert(counters_.find(name) == counters_.end());
163  CounterInfo *counter_info = new CounterInfo(desc);
164  counters_[name] = counter_info;
165  return &counter_info->counter;
166 }
167 
168 
169 Statistics::Statistics() {
170  lock_ =
171  reinterpret_cast<pthread_mutex_t *>(smalloc(sizeof(pthread_mutex_t)));
172  int retval = pthread_mutex_init(lock_, NULL);
173  assert(retval == 0);
174 }
175 
176 
177 Statistics::~Statistics() {
178  for (map<string, CounterInfo *>::iterator i = counters_.begin(),
179  iEnd = counters_.end(); i != iEnd; ++i)
180  {
181  int32_t old_value = atomic_xadd32(&i->second->refcnt, -1);
182  if (old_value == 1)
183  delete i->second;
184  }
185  pthread_mutex_destroy(lock_);
186  free(lock_);
187 }
188 
189 
190 //------------------------------------------------------------------------------
191 
192 
196 Recorder::Recorder(uint32_t resolution_s, uint32_t capacity_s)
197  : last_timestamp_(0)
198  , capacity_s_(capacity_s)
199  , resolution_s_(resolution_s)
200 {
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 
215 }
216 
217 
218 void Recorder::TickAt(uint64_t timestamp) {
219  uint64_t bin_abs = timestamp / resolution_s_;
220  uint64_t last_bin_abs = last_timestamp_ / resolution_s_;
221 
222  // timestamp in the past: don't update last_timestamp_
223  if (bin_abs < last_bin_abs) {
224  // Do we still remember this event?
225  if ((last_bin_abs - bin_abs) < no_bins_)
226  bins_[bin_abs % no_bins_]++;
227  return;
228  }
229 
230  if (last_bin_abs == bin_abs) {
231  bins_[bin_abs % no_bins_]++;
232  } else {
233  // When clearing bins between last_timestamp_ and now, avoid cycling the
234  // ring buffer multiple times.
235  unsigned max_bins_clear = std::min(bin_abs, last_bin_abs + no_bins_ + 1);
236  for (uint64_t i = last_bin_abs + 1; i < max_bins_clear; ++i)
237  bins_[i % no_bins_] = 0;
238  bins_[bin_abs % no_bins_] = 1;
239  }
240 
241  last_timestamp_ = timestamp;
242 }
243 
244 
245 uint64_t Recorder::GetNoTicks(uint32_t retrospect_s) const {
246  uint64_t now = platform_monotonic_time();
247  if (retrospect_s > now)
248  retrospect_s = now;
249 
250  uint64_t last_bin_abs = last_timestamp_ / resolution_s_;
251  uint64_t past_bin_abs = (now - retrospect_s) / resolution_s_;
252  int64_t min_bin_abs =
253  std::max(past_bin_abs,
254  (last_bin_abs < no_bins_) ? 0 : (last_bin_abs - (no_bins_ - 1)));
255  uint64_t result = 0;
256  for (int64_t i = last_bin_abs; i >= min_bin_abs; --i) {
257  result += bins_[i % no_bins_];
258  }
259 
260  return result;
261 }
262 
263 
264 //------------------------------------------------------------------------------
265 
266 
267 void MultiRecorder::AddRecorder(uint32_t resolution_s, uint32_t capacity_s) {
268  recorders_.push_back(Recorder(resolution_s, capacity_s));
269 }
270 
271 
272 uint64_t MultiRecorder::GetNoTicks(uint32_t retrospect_s) const {
273  unsigned N = recorders_.size();
274  for (unsigned i = 0; i < N; ++i) {
275  if ( (recorders_[i].capacity_s() >= retrospect_s) ||
276  (i == (N - 1)) )
277  {
278  return recorders_[i].GetNoTicks(retrospect_s);
279  }
280  }
281  return 0;
282 }
283 
284 
286  uint64_t now = platform_monotonic_time();
287  for (unsigned i = 0; i < recorders_.size(); ++i)
288  recorders_[i].TickAt(now);
289 }
290 
291 
292 void MultiRecorder::TickAt(uint64_t timestamp) {
293  for (unsigned i = 0; i < recorders_.size(); ++i)
294  recorders_[i].TickAt(timestamp);
295 }
296 
297 } // namespace perf
298 
299 
300 #ifdef CVMFS_NAMESPACE_GUARD
301 } // namespace CVMFS_NAMESPACE_GUARD
302 #endif
std::map< std::string, CounterInfo * > counters_
Definition: statistics.h:90
void AddRecorder(uint32_t resolution_s, uint32_t capacity_s)
Definition: statistics.cc:267
void Add(const std::string &key, const std::string &val)
void TickAt(uint64_t timestamp)
Definition: statistics.cc:218
uint64_t last_timestamp_
Definition: statistics.h:203
assert((mem||(size==0))&&"Out Of Memory")
string StringifyDouble(const double value)
Definition: string.cc:96
vector< string > SplitString(const string &str, char delim)
Definition: string.cc:290
std::vector< uint32_t > bins_
Definition: statistics.h:198
std::vector< Recorder > recorders_
Definition: statistics.h:237
uint64_t platform_realtime_ns()
string StringifyInt(const int64_t value)
Definition: string.cc:78
uint64_t platform_monotonic_time()
int64_t Get()
Definition: statistics.h:32
uint32_t resolution_s_
Definition: statistics.h:213
uint64_t GetNoTicks(uint32_t retrospect_s) const
Definition: statistics.cc:245
uint32_t capacity_s_
Definition: statistics.h:208
Definition: mutex.h:42
uint64_t GetNoTicks(uint32_t retrospect_s) const
Definition: statistics.cc:272
unsigned no_bins_
Definition: statistics.h:218
void TickAt(uint64_t timestamp)
Definition: statistics.cc:292
std::string GenerateString() const
void AddJsonObject(const std::string &key, const std::string &json)