CernVM-FS  2.9.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 "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 
76  private:
77  Statistics(const Statistics &other);
78  Statistics& operator=(const Statistics &other);
79  struct CounterInfo {
80  explicit CounterInfo(const std::string &desc) : desc(desc) {
81  atomic_init32(&refcnt);
82  atomic_inc32(&refcnt);
83  }
86  std::string desc;
87  };
88  std::map<std::string, CounterInfo *> counters_;
89  mutable pthread_mutex_t *lock_;
90 };
91 
92 
99  public:
100  StatisticsTemplate(const std::string &name_major, Statistics *statistics)
101  : name_major_(name_major), statistics_(statistics)
102  { }
103  StatisticsTemplate(const std::string &name_sub,
104  const StatisticsTemplate &statistics)
105  : name_major_(statistics.name_major_ + "." + name_sub)
106  , statistics_(statistics.statistics_)
107  { }
108 
109  Counter *RegisterTemplated(const std::string &name_minor,
110  const std::string &desc)
111  {
112  return statistics_->Register(name_major_ + "." + name_minor, desc);
113  }
114 
115  Counter *RegisterOrLookupTemplated(const std::string &name_minor,
116  const std::string &desc)
117  {
118  Counter *result = statistics_->Lookup(name_major_ + "." + name_minor);
119  if (result == NULL) {
120  return RegisterTemplated(name_minor, desc);
121  }
122  return result;
123  }
124 
126 
127  private:
128  std::string name_major_;
130 };
131 
132 
133 struct FsCounters {
145 
146  explicit FsCounters(StatisticsTemplate statistics) {
147  n_files_added = statistics.RegisterTemplated("n_files_added",
148  "Number of files added");
149  n_files_removed = statistics.RegisterTemplated("n_files_removed",
150  "Number of files removed");
151  n_files_changed = statistics.RegisterTemplated("n_files_changed",
152  "Number of files changed");
153  n_directories_added = statistics.RegisterTemplated("n_directories_added",
154  "Number of directories added");
155  n_directories_removed =
156  statistics.RegisterTemplated("n_directories_removed",
157  "Number of directories removed");
158  n_directories_changed =
159  statistics.RegisterTemplated("n_directories_changed",
160  "Number of directories changed");
161  n_symlinks_added = statistics.RegisterTemplated("n_symlinks_added",
162  "Number of symlinks added");
163  n_symlinks_removed = statistics.RegisterTemplated("n_symlinks_removed",
164  "Number of symlinks removed");
165  n_symlinks_changed = statistics.RegisterTemplated("n_symlinks_changed",
166  "Number of symlinks changed");
167  sz_added_bytes = statistics.RegisterTemplated("sz_added_bytes",
168  "Number of bytes added");
169  sz_removed_bytes = statistics.RegisterTemplated("sz_removed_bytes",
170  "Number of bytes removed");
171  }
172 }; // FsCounters
173 
174 
180 class Recorder {
181  public:
182  Recorder(uint32_t resolution_s, uint32_t capacity_s);
183 
184  void Tick();
185  void TickAt(uint64_t timestamp);
186  uint64_t GetNoTicks(uint32_t retrospect_s) const;
187 
188  uint32_t capacity_s() const { return capacity_s_; }
189  uint32_t resolution_s() const { return resolution_s_; }
190 
191  private:
196  std::vector<uint32_t> bins_;
197 
201  uint64_t last_timestamp_;
202 
206  uint32_t capacity_s_;
207 
211  uint32_t resolution_s_;
212 
216  unsigned no_bins_;
217 };
218 
219 
227  public:
228  void Tick();
229  void TickAt(uint64_t timestamp);
230  uint64_t GetNoTicks(uint32_t retrospect_s) const;
231 
232  void AddRecorder(uint32_t resolution_s, uint32_t capacity_s);
233 
234  private:
235  std::vector<Recorder> recorders_;
236 };
237 
238 } // namespace perf
239 
240 #ifdef CVMFS_NAMESPACE_GUARD
241 } // namespace CVMFS_NAMESPACE_GUARD
242 #endif
243 
244 #endif // CVMFS_STATISTICS_H_
std::map< std::string, CounterInfo * > counters_
Definition: statistics.h:88
perf::Counter * n_symlinks_added
Definition: statistics.h:140
void Dec(class Counter *counter)
Definition: statistics.h:49
Counter * Register(const std::string &name, const std::string &desc)
Definition: statistics.cc:139
FsCounters(StatisticsTemplate statistics)
Definition: statistics.h:146
int64_t Xadd(class Counter *counter, const int64_t delta)
Definition: statistics.h:51
perf::Counter * n_symlinks_removed
Definition: statistics.h:141
int64_t atomic_int64
Definition: atomic.h:18
CounterInfo(const std::string &desc)
Definition: statistics.h:80
perf::Counter * n_directories_removed
Definition: statistics.h:138
atomic_int64 counter_
Definition: statistics.h:45
Counter * RegisterOrLookupTemplated(const std::string &name_minor, const std::string &desc)
Definition: statistics.h:115
uint64_t last_timestamp_
Definition: statistics.h:201
perf::Statistics * statistics_
Definition: repository.h:138
StatisticsTemplate(const std::string &name_sub, const StatisticsTemplate &statistics)
Definition: statistics.h:103
perf::Counter * sz_added_bytes
Definition: statistics.h:143
perf::Counter * n_symlinks_changed
Definition: statistics.h:142
perf::Counter * sz_removed_bytes
Definition: statistics.h:144
int32_t atomic_int32
Definition: atomic.h:17
perf::Counter * n_directories_added
Definition: statistics.h:137
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:62
pthread_mutex_t * lock_
Definition: statistics.h:89
void Set(const int64_t val)
Definition: statistics.h:33
perf::Counter * n_directories_changed
Definition: statistics.h:139
std::vector< uint32_t > bins_
Definition: statistics.h:196
std::vector< Recorder > recorders_
Definition: statistics.h:235
Statistics * statistics()
Definition: statistics.h:125
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:211
uint32_t capacity_s() const
Definition: statistics.h:188
perf::Counter * n_files_added
Definition: statistics.h:134
int64_t Xadd(const int64_t delta)
Definition: statistics.h:34
perf::Counter * n_files_removed
Definition: statistics.h:135
uint32_t capacity_s_
Definition: statistics.h:206
uint32_t resolution_s() const
Definition: statistics.h:189
unsigned no_bins_
Definition: statistics.h:216
StatisticsTemplate(const std::string &name_major, Statistics *statistics)
Definition: statistics.h:100
Statistics * statistics_
Definition: statistics.h:129
void Dec()
Definition: statistics.h:31
perf::Counter * n_files_changed
Definition: statistics.h:136