38 # include <dsn/utility/enum_helper.h> 39 # include <dsn/utility/autoref_ptr.h> 40 # include <dsn/utility/dlib.h> 41 # include <dsn/service_api_c.h> 52 ENUM_BEGIN(dsn_perf_counter_type_t, COUNTER_TYPE_INVALID)
53 ENUM_REG(COUNTER_TYPE_NUMBER)
54 ENUM_REG(COUNTER_TYPE_RATE)
55 ENUM_REG(COUNTER_TYPE_NUMBER_PERCENTILES)
56 ENUM_END(dsn_perf_counter_type_t)
58 ENUM_BEGIN(dsn_perf_counter_percentile_type_t, COUNTER_PERCENTILE_INVALID)
59 ENUM_REG(COUNTER_PERCENTILE_50)
60 ENUM_REG(COUNTER_PERCENTILE_90)
61 ENUM_REG(COUNTER_PERCENTILE_95)
62 ENUM_REG(COUNTER_PERCENTILE_99)
63 ENUM_REG(COUNTER_PERCENTILE_999)
64 ENUM_END(dsn_perf_counter_percentile_type_t)
67 typedef ref_ptr<perf_counter> perf_counter_ptr;
69 class perf_counter : public ref_counter
72 template <
typename T>
static perf_counter* create(
const char* app,
const char *section,
const char *name, dsn_perf_counter_type_t type,
const char *dsptr)
74 return new T(app, section, name, type, dsptr);
77 typedef perf_counter* (*factory)(
const char*,
const char *,
const char *, dsn_perf_counter_type_t,
const char *);
80 DSN_API
static perf_counter_ptr get_counter(
84 dsn_perf_counter_type_t flags,
86 bool create_if_not_exist =
false 89 DSN_API
static bool remove_counter(
const char* full_name);
92 perf_counter(
const char* app,
const char *section,
const char *name, dsn_perf_counter_type_t type,
const char *dsptr)
93 : _app(app), _section(section), _name(name), _dsptr(dsptr), _type(type), _index(0)
95 build_full_name(app, section, name, _full_name);
98 virtual ~perf_counter(
void) {}
100 virtual void increment() = 0;
101 virtual void decrement() = 0;
102 virtual void add(uint64_t val) = 0;
103 virtual void set(uint64_t val) = 0;
104 virtual double get_value() = 0;
105 virtual uint64_t get_integer_value() = 0;
106 virtual double get_percentile(dsn_perf_counter_percentile_type_t type) = 0;
108 typedef std::vector<std::pair<uint64_t*, int> > samples_t;
111 virtual int get_latest_samples(
int required_sample_count, samples_t& samples)
const {
return 0; }
114 virtual uint64_t get_latest_sample()
const {
return 0; }
116 const char* full_name()
const {
return _full_name.c_str(); }
117 const char* app()
const {
return _app.c_str(); }
118 const char* section()
const {
return _section.c_str(); }
119 const char* name()
const {
return _name.c_str(); }
120 const char* dsptr()
const {
return _dsptr.c_str(); }
121 dsn_perf_counter_type_t type()
const {
return _type; }
122 uint64_t index()
const {
return _index; }
125 static void build_full_name(
const char* app,
const char* section,
const char* name, std::string& counter_name)
127 std::stringstream ss;
128 ss << app <<
"*" << section <<
"*" << name;
129 counter_name = std::move(ss.str());
134 std::string _section;
137 dsn_perf_counter_type_t _type;
140 std::string _full_name;
141 friend class perf_counters;