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 !=
"") {
139 Counter *Statistics::Register(
const string &name,
const string &desc) {
141 assert(counters_.find(name) == counters_.end());
143 counters_[name] = counter_info;
144 return &counter_info->counter;
148 Statistics::Statistics() {
150 reinterpret_cast<pthread_mutex_t *
>(smalloc(
sizeof(pthread_mutex_t)));
151 int retval = pthread_mutex_init(lock_, NULL);
156 Statistics::~Statistics() {
157 for (map<string, CounterInfo *>::iterator i = counters_.begin(),
158 iEnd = counters_.end(); i != iEnd; ++i)
160 int32_t old_value = atomic_xadd32(&i->second->refcnt, -1);
164 pthread_mutex_destroy(lock_);
175 Recorder::Recorder(uint32_t resolution_s, uint32_t capacity_s)
177 , capacity_s_(capacity_s)
178 , resolution_s_(resolution_s)
180 assert((resolution_s > 0) && (capacity_s > resolution_s));
187 for (
unsigned i = 0; i <
no_bins_; ++i)
202 if (bin_abs < last_bin_abs) {
204 if ((last_bin_abs - bin_abs) <
no_bins_)
209 if (last_bin_abs == bin_abs) {
214 unsigned max_bins_clear = std::min(bin_abs, last_bin_abs +
no_bins_ + 1);
215 for (uint64_t i = last_bin_abs + 1; i < max_bins_clear; ++i)
226 if (retrospect_s > now)
230 uint64_t past_bin_abs = (now - retrospect_s) /
resolution_s_;
231 int64_t min_bin_abs =
232 std::max(past_bin_abs,
235 for (int64_t i = last_bin_abs; i >= min_bin_abs; --i) {
253 for (
unsigned i = 0; i < N; ++i) {
254 if ( (
recorders_[i].capacity_s() >= retrospect_s) ||
257 return recorders_[i].GetNoTicks(retrospect_s);
266 for (
unsigned i = 0; i <
recorders_.size(); ++i)
272 for (
unsigned i = 0; i <
recorders_.size(); ++i)
279 #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)