CernVM-FS  2.13.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
statistics.h
Go to the documentation of this file.
1 
5 #ifndef CVMFS_STATISTICS_H_
6 #define CVMFS_STATISTICS_H_
7 
8 #include <pthread.h>
9 #include <stdint.h>
10 
11 #include <map>
12 #include <string>
13 #include <vector>
14 
15 #include "util/atomic.h"
16 
17 #ifdef CVMFS_NAMESPACE_GUARD
18 namespace CVMFS_NAMESPACE_GUARD {
19 #endif
20 
21 
22 namespace perf {
23 
27 class Counter {
28  public:
29  Counter() { atomic_init64(&counter_); }
30  void Inc() { atomic_inc64(&counter_); }
31  void Dec() { atomic_dec64(&counter_); }
32  int64_t Get() { return atomic_read64(&counter_); }
33  void Set(const int64_t val) { atomic_write64(&counter_, val); }
34  int64_t Xadd(const int64_t delta) { return atomic_xadd64(&counter_, delta); }
35 
36  std::string Print();
37  std::string PrintK();
38  std::string PrintKi();
39  std::string PrintM();
40  std::string PrintMi();
41  std::string PrintRatio(Counter divider);
42  std::string ToString();
43 
44  private:
46 };
47 
48 // perf::Func(Counter) is more clear to read in the code
49 inline void Dec(class Counter *counter) { counter->Dec(); }
50 inline void Inc(class Counter *counter) { counter->Inc(); }
51 inline int64_t Xadd(class Counter *counter, const int64_t delta) {
52  return counter->Xadd(delta);
53 }
54 
55 
60 class Statistics {
61  public:
62  enum PrintOptions {
63  kPrintSimple = 0,
64  kPrintHeader
65  };
66 
67  Statistics();
68  ~Statistics();
69  Statistics *Fork();
70  Counter *Register(const std::string &name, const std::string &desc);
71  Counter *Lookup(const std::string &name) const;
72  std::string LookupDesc(const std::string &name);
73  std::string PrintList(const PrintOptions print_options);
74  std::string PrintJSON();
75  void SnapshotCounters(std::map<std::string, int64_t> *counters,
76  uint64_t *timestamp_ns);
77 
78  private:
79  Statistics(const Statistics &other);
80  Statistics &operator=(const Statistics &other);
81  struct CounterInfo {
82  explicit CounterInfo(const std::string &desc) : desc(desc) {
83  atomic_init32(&refcnt);
84  atomic_inc32(&refcnt);
85  }
88  std::string desc;
89  };
90  std::map<std::string, CounterInfo *> counters_;
91  mutable pthread_mutex_t *lock_;
92 };
93 
94 
101  public:
102  StatisticsTemplate(const std::string &name_major, Statistics *statistics)
103  : name_major_(name_major), statistics_(statistics) { }
104  StatisticsTemplate(const std::string &name_sub,
105  const StatisticsTemplate &statistics)
106  : name_major_(statistics.name_major_ + "." + name_sub)
107  , statistics_(statistics.statistics_) { }
108 
109  Counter *RegisterTemplated(const std::string &name_minor,
110  const std::string &desc) {
111  return statistics_->Register(name_major_ + "." + name_minor, desc);
112  }
113 
114  Counter *RegisterOrLookupTemplated(const std::string &name_minor,
115  const std::string &desc) {
116  Counter *result = statistics_->Lookup(name_major_ + "." + name_minor);
117  if (result == NULL) {
118  return RegisterTemplated(name_minor, desc);
119  }
120  return result;
121  }
122 
124 
125  private:
126  std::string name_major_;
128 };
129 
130 
131 struct FsCounters {
143 
144  explicit FsCounters(StatisticsTemplate statistics) {
145  n_files_added = statistics.RegisterTemplated("n_files_added",
146  "Number of files added");
147  n_files_removed = statistics.RegisterTemplated("n_files_removed",
148  "Number of files removed");
149  n_files_changed = statistics.RegisterTemplated("n_files_changed",
150  "Number of files changed");
151  n_directories_added = statistics.RegisterTemplated(
152  "n_directories_added", "Number of directories added");
153  n_directories_removed = statistics.RegisterTemplated(
154  "n_directories_removed", "Number of directories removed");
155  n_directories_changed = statistics.RegisterTemplated(
156  "n_directories_changed", "Number of directories changed");
157  n_symlinks_added = statistics.RegisterTemplated("n_symlinks_added",
158  "Number of symlinks added");
159  n_symlinks_removed = statistics.RegisterTemplated(
160  "n_symlinks_removed", "Number of symlinks removed");
161  n_symlinks_changed = statistics.RegisterTemplated(
162  "n_symlinks_changed", "Number of symlinks changed");
163  sz_added_bytes = statistics.RegisterTemplated("sz_added_bytes",
164  "Number of bytes added");
165  sz_removed_bytes = statistics.RegisterTemplated("sz_removed_bytes",
166  "Number of bytes removed");
167  }
168 }; // FsCounters
169 
170 
176 class Recorder {
177  public:
178  Recorder(uint32_t resolution_s, uint32_t capacity_s);
179 
180  void Tick();
181  void TickAt(uint64_t timestamp);
182  uint64_t GetNoTicks(uint32_t retrospect_s) const;
183 
184  uint32_t capacity_s() const { return capacity_s_; }
185  uint32_t resolution_s() const { return resolution_s_; }
186 
187  private:
192  std::vector<uint32_t> bins_;
193 
197  uint64_t last_timestamp_;
198 
202  uint32_t capacity_s_;
203 
207  uint32_t resolution_s_;
208 
212  unsigned no_bins_;
213 };
214 
215 
223  public:
224  void Tick();
225  void TickAt(uint64_t timestamp);
226  uint64_t GetNoTicks(uint32_t retrospect_s) const;
227 
228  void AddRecorder(uint32_t resolution_s, uint32_t capacity_s);
229 
230  private:
231  std::vector<Recorder> recorders_;
232 };
233 
234 } // namespace perf
235 
236 #ifdef CVMFS_NAMESPACE_GUARD
237 } // namespace CVMFS_NAMESPACE_GUARD
238 #endif
239 
240 #endif // CVMFS_STATISTICS_H_
std::map< std::string, CounterInfo * > counters_
Definition: statistics.h:90
perf::Counter * n_symlinks_added
Definition: statistics.h:138
void Dec(class Counter *counter)
Definition: statistics.h:49
Counter * Register(const std::string &name, const std::string &desc)
Definition: statistics.cc:163
FsCounters(StatisticsTemplate statistics)
Definition: statistics.h:144
int64_t Xadd(class Counter *counter, const int64_t delta)
Definition: statistics.h:51
perf::Counter * n_symlinks_removed
Definition: statistics.h:139
int64_t atomic_int64
Definition: atomic.h:18
CounterInfo(const std::string &desc)
Definition: statistics.h:82
perf::Counter * n_directories_removed
Definition: statistics.h:136
atomic_int64 counter_
Definition: statistics.h:45
Counter * RegisterOrLookupTemplated(const std::string &name_minor, const std::string &desc)
Definition: statistics.h:114
uint64_t last_timestamp_
Definition: statistics.h:197
perf::Statistics * statistics_
Definition: repository.h:138
StatisticsTemplate(const std::string &name_sub, const StatisticsTemplate &statistics)
Definition: statistics.h:104
perf::Counter * sz_added_bytes
Definition: statistics.h:141
perf::Counter * n_symlinks_changed
Definition: statistics.h:140
perf::Counter * sz_removed_bytes
Definition: statistics.h:142
int32_t atomic_int32
Definition: atomic.h:17
perf::Counter * n_directories_added
Definition: statistics.h:135
Counter * RegisterTemplated(const std::string &name_minor, const std::string &desc)
Definition: statistics.h:109
Counter * Lookup(const std::string &name) const
Definition: statistics.cc:63
pthread_mutex_t * lock_
Definition: statistics.h:91
void Set(const int64_t val)
Definition: statistics.h:33
perf::Counter * n_directories_changed
Definition: statistics.h:137
std::vector< uint32_t > bins_
Definition: statistics.h:192
std::vector< Recorder > recorders_
Definition: statistics.h:231
Statistics * statistics()
Definition: statistics.h:123
void Inc(class Counter *counter)
Definition: statistics.h:50
void Inc()
Definition: statistics.h:30
int64_t Get()
Definition: statistics.h:32
uint32_t resolution_s_
Definition: statistics.h:207
uint32_t capacity_s() const
Definition: statistics.h:184
perf::Counter * n_files_added
Definition: statistics.h:132
int64_t Xadd(const int64_t delta)
Definition: statistics.h:34
perf::Counter * n_files_removed
Definition: statistics.h:133
uint32_t capacity_s_
Definition: statistics.h:202
uint32_t resolution_s() const
Definition: statistics.h:185
unsigned no_bins_
Definition: statistics.h:212
StatisticsTemplate(const std::string &name_major, Statistics *statistics)
Definition: statistics.h:102
Statistics * statistics_
Definition: statistics.h:127
void Dec()
Definition: statistics.h:31
perf::Counter * n_files_changed
Definition: statistics.h:134