18 #ifdef CVMFS_NAMESPACE_GUARD
19 namespace CVMFS_NAMESPACE_GUARD {
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();
51 for (map<string, CounterInfo *>::iterator i = counters_.begin(),
52 iEnd = counters_.end(); i != iEnd; ++i)
54 atomic_inc32(&i->second->refcnt);
62 Counter *Statistics::Lookup(
const std::string &name)
const {
64 map<string, CounterInfo *>::const_iterator i = counters_.find(name);
65 if (i != counters_.end())
66 return &i->second->counter;
71 string Statistics::LookupDesc(
const std::string &name) {
73 map<string, CounterInfo *>::const_iterator i = counters_.find(name);
74 if (i != counters_.end())
75 return i->second->desc;
81 if (print_options == kPrintHeader)
82 result +=
"Name|Value|Description\n";
85 for (map<string, CounterInfo *>::const_iterator i = counters_.begin(),
86 iEnd = counters_.end(); i != iEnd; ++i)
88 result += i->first +
"|" + i->second->counter.ToString() +
89 "|" + i->second->desc +
"\n";
106 string Statistics::PrintJSON() {
113 std::string last_namespace =
"";
114 for (map<string, CounterInfo *>::const_iterator i = counters_.begin(),
115 iEnd = counters_.end();
117 std::vector<std::string> tokens =
SplitString(i->first,
'.');
119 if (tokens[0] != last_namespace) {
120 if (last_namespace !=
"") {
124 json_statistics_namespace.
Clear();
127 json_statistics_namespace.
Add(tokens[1], i->second->counter.Get());
129 last_namespace = tokens[0];
131 if (last_namespace !=
"") {
147 void Statistics::SnapshotCounters(
148 std::map<std::string, int64_t> *counters,
149 uint64_t *timestamp_ns) {
152 for (map<string, CounterInfo *>::const_iterator i = counters_.begin(),
153 iEnd = counters_.end(); i != iEnd; ++i)
156 (*counters)[i->first] = (*i->second).counter.Get();
160 Counter *Statistics::Register(
const string &name,
const string &desc) {
162 assert(counters_.find(name) == counters_.end());
164 counters_[name] = counter_info;
165 return &counter_info->counter;
169 Statistics::Statistics() {
171 reinterpret_cast<pthread_mutex_t *
>(smalloc(
sizeof(pthread_mutex_t)));
172 int retval = pthread_mutex_init(lock_, NULL);
177 Statistics::~Statistics() {
178 for (map<string, CounterInfo *>::iterator i = counters_.begin(),
179 iEnd = counters_.end(); i != iEnd; ++i)
181 int32_t old_value = atomic_xadd32(&i->second->refcnt, -1);
185 pthread_mutex_destroy(lock_);
196 Recorder::Recorder(uint32_t resolution_s, uint32_t capacity_s)
198 , capacity_s_(capacity_s)
199 , resolution_s_(resolution_s)
201 assert((resolution_s > 0) && (capacity_s > resolution_s));
208 for (
unsigned i = 0; i <
no_bins_; ++i)
223 if (bin_abs < last_bin_abs) {
225 if ((last_bin_abs - bin_abs) <
no_bins_)
230 if (last_bin_abs == bin_abs) {
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)
247 if (retrospect_s > now)
251 uint64_t past_bin_abs = (now - retrospect_s) /
resolution_s_;
252 int64_t min_bin_abs =
253 std::max(past_bin_abs,
256 for (int64_t i = last_bin_abs; i >= min_bin_abs; --i) {
274 for (
unsigned i = 0; i < N; ++i) {
275 if ( (
recorders_[i].capacity_s() >= retrospect_s) ||
278 return recorders_[i].GetNoTicks(retrospect_s);
287 for (
unsigned i = 0; i <
recorders_.size(); ++i)
293 for (
unsigned i = 0; i <
recorders_.size(); ++i)
300 #ifdef CVMFS_NAMESPACE_GUARD
std::map< std::string, CounterInfo * > counters_
void AddRecorder(uint32_t resolution_s, uint32_t capacity_s)
void Add(const std::string &key, const std::string &val)
void TickAt(uint64_t timestamp)
assert((mem||(size==0))&&"Out Of Memory")
string StringifyDouble(const double value)
vector< string > SplitString(const string &str, char delim)
std::vector< uint32_t > bins_
std::vector< Recorder > recorders_
string StringifyInt(const int64_t value)
uint64_t GetNoTicks(uint32_t retrospect_s) const
uint64_t GetNoTicks(uint32_t retrospect_s) const
void TickAt(uint64_t timestamp)
std::string GenerateString() const
void AddJsonObject(const std::string &key, const std::string &json)