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();
55 atomic_inc32(&i->second->refcnt);
63 Counter *Statistics::Lookup(
const std::string &name)
const {
65 map<string, CounterInfo *>::const_iterator i = counters_.find(name);
66 if (i != counters_.end())
67 return &i->second->counter;
72 string Statistics::LookupDesc(
const std::string &name) {
74 map<string, CounterInfo *>::const_iterator i = counters_.find(name);
75 if (i != counters_.end())
76 return i->second->desc;
82 if (print_options == kPrintHeader)
83 result +=
"Name|Value|Description\n";
86 for (map<string, CounterInfo *>::const_iterator i = counters_.begin(),
87 iEnd = counters_.end();
90 result += i->first +
"|" + i->second->counter.ToString() +
"|"
91 + i->second->desc +
"\n";
108 string Statistics::PrintJSON() {
115 std::string last_namespace =
"";
116 for (map<string, CounterInfo *>::const_iterator i = counters_.begin(),
117 iEnd = counters_.end();
120 std::vector<std::string> tokens =
SplitString(i->first,
'.');
122 if (tokens[0] != last_namespace) {
123 if (last_namespace !=
"") {
127 json_statistics_namespace.
Clear();
130 json_statistics_namespace.
Add(tokens[1], i->second->counter.Get());
132 last_namespace = tokens[0];
134 if (last_namespace !=
"") {
150 void Statistics::SnapshotCounters(std::map<std::string, int64_t> *counters,
151 uint64_t *timestamp_ns) {
154 for (map<string, CounterInfo *>::const_iterator i = counters_.begin(),
155 iEnd = counters_.end();
159 (*counters)[i->first] = (*i->second).counter.Get();
163 Counter *Statistics::Register(
const string &name,
const string &desc) {
165 assert(counters_.find(name) == counters_.end());
167 counters_[name] = counter_info;
168 return &counter_info->counter;
172 Statistics::Statistics() {
173 lock_ =
reinterpret_cast<pthread_mutex_t *
>(smalloc(
sizeof(pthread_mutex_t)));
174 int retval = pthread_mutex_init(lock_, NULL);
179 Statistics::~Statistics() {
180 for (map<string, CounterInfo *>::iterator i = counters_.begin(),
181 iEnd = counters_.end();
184 int32_t old_value = atomic_xadd32(&i->second->refcnt, -1);
188 pthread_mutex_destroy(lock_);
199 Recorder::Recorder(uint32_t resolution_s, uint32_t capacity_s)
200 : last_timestamp_(0), capacity_s_(capacity_s), resolution_s_(resolution_s) {
201 assert((resolution_s > 0) && (capacity_s > resolution_s));
208 for (
unsigned i = 0; i <
no_bins_; ++i)
221 if (bin_abs < last_bin_abs) {
223 if ((last_bin_abs - bin_abs) <
no_bins_)
228 if (last_bin_abs == bin_abs) {
233 unsigned max_bins_clear = std::min(bin_abs, last_bin_abs +
no_bins_ + 1);
234 for (uint64_t i = last_bin_abs + 1; i < max_bins_clear; ++i)
245 if (retrospect_s > now)
249 uint64_t past_bin_abs = (now - retrospect_s) /
resolution_s_;
250 int64_t min_bin_abs = std::max(
254 for (int64_t i = last_bin_abs; i >= min_bin_abs; --i) {
272 for (
unsigned i = 0; i < N; ++i) {
273 if ((
recorders_[i].capacity_s() >= retrospect_s) || (i == (N - 1))) {
274 return recorders_[i].GetNoTicks(retrospect_s);
283 for (
unsigned i = 0; i <
recorders_.size(); ++i)
289 for (
unsigned i = 0; i <
recorders_.size(); ++i)
296 #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)