Robust Distributed System Nucleus (rDSN)  ver 1.0.0
perf_counter.h
1 /*
2  * The MIT License (MIT)
3  *
4  * Copyright (c) 2015 Microsoft Corporation
5  *
6  * -=- Robust Distributed System Nucleus (rDSN) -=-
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a copy
9  * of this software and associated documentation files (the "Software"), to deal
10  * in the Software without restriction, including without limitation the rights
11  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12  * copies of the Software, and to permit persons to whom the Software is
13  * furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24  * THE SOFTWARE.
25  */
26 
27 /*
28  * Description:
29  * What is this file about?
30  *
31  * Revision history:
32  * xxxx-xx-xx, author, first version
33  * xxxx-xx-xx, author, fix bug about xxx
34  */
35 
36 # pragma once
37 
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>
42 # include <memory>
43 # include <sstream>
44 # include <vector>
45 
46 namespace dsn {
47 
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)
57 
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)
65 
66 class perf_counter;
67 typedef ref_ptr<perf_counter> perf_counter_ptr;
68 
69 class perf_counter : public ref_counter
70 {
71 public:
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)
73  {
74  return new T(app, section, name, type, dsptr);
75  }
76 
77  typedef perf_counter* (*factory)(const char*, const char *, const char *, dsn_perf_counter_type_t, const char *);
78 
79 public:
80  DSN_API static perf_counter_ptr get_counter(
81  const char* app,
82  const char *section,
83  const char *name,
84  dsn_perf_counter_type_t flags,
85  const char *dsptr,
86  bool create_if_not_exist = false
87  );
88 
89  DSN_API static bool remove_counter(const char* full_name);
90 
91 public:
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)
94  {
95  build_full_name(app, section, name, _full_name);
96  }
97 
98  virtual ~perf_counter(void) {}
99 
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;
107 
108  typedef std::vector<std::pair<uint64_t*, int> > samples_t;
109 
110  // return actual sample count, must <= required_sample_count
111  virtual int get_latest_samples(int required_sample_count, /*out*/ samples_t& samples) const { return 0; }
112 
113  // return the latest sample value
114  virtual uint64_t get_latest_sample() const { return 0; }
115 
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; } // index << 32 | version
123 
124 public:
125  static void build_full_name(const char* app, const char* section, const char* name, /*out*/ std::string& counter_name)
126  {
127  std::stringstream ss;
128  ss << app << "*" << section << "*" << name;
129  counter_name = std::move(ss.str());
130  }
131 
132 private:
133  std::string _app;
134  std::string _section;
135  std::string _name;
136  std::string _dsptr;
137  dsn_perf_counter_type_t _type;
138 
139  uint64_t _index; // for quick query in perf_counters: index << 32 | version
140  std::string _full_name;
141  friend class perf_counters;
142 };
144 } // end namespace
Definition: address.h:52