Apache Mesos
counter.hpp
Go to the documentation of this file.
1 // Licensed under the Apache License, Version 2.0 (the "License");
2 // you may not use this file except in compliance with the License.
3 // You may obtain a copy of the License at
4 //
5 // http://www.apache.org/licenses/LICENSE-2.0
6 //
7 // Unless required by applicable law or agreed to in writing, software
8 // distributed under the License is distributed on an "AS IS" BASIS,
9 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 // See the License for the specific language governing permissions and
11 // limitations under the License
12 
13 #ifndef __PROCESS_METRICS_COUNTER_HPP__
14 #define __PROCESS_METRICS_COUNTER_HPP__
15 
16 #include <memory>
17 #include <string>
18 
20 
21 namespace process {
22 namespace metrics {
23 
24 // A Metric that represents an integer value that can be incremented and
25 // decremented.
26 class Counter : public Metric
27 {
28 public:
29  // 'name' is the unique name for the instance of Counter being constructed.
30  // This is what will be used as the key in the JSON endpoint.
31  // 'window' is the amount of history to keep for this Metric.
32  Counter(const std::string& name, const Option<Duration>& window = None())
33  : Metric(name, window),
34  data(new Data())
35  {
36  push(static_cast<double>(data->value.load()));
37  }
38 
39  ~Counter() override {}
40 
41  Future<double> value() const override
42  {
43  return static_cast<double>(data->value.load());
44  }
45 
46  void reset()
47  {
48  data->value.store(0);
49  push(0);
50  }
51 
53  {
54  return *this += 1;
55  }
56 
58  {
59  Counter c(*this);
60  ++(*this);
61  return c;
62  }
63 
64  Counter& operator+=(int64_t v)
65  {
66  int64_t prev = data->value.fetch_add(v);
67  push(static_cast<double>(prev + v));
68  return *this;
69  }
70 
71 private:
72  struct Data
73  {
74  explicit Data() : value(0) {}
75 
76  std::atomic<int64_t> value;
77  };
78 
79  std::shared_ptr<Data> data;
80 };
81 
82 } // namespace metrics {
83 } // namespace process {
84 
85 #endif // __PROCESS_METRICS_COUNTER_HPP__
Definition: metric.hpp:33
Counter & operator++()
Definition: counter.hpp:52
Definition: counter.hpp:26
~Counter() override
Definition: counter.hpp:39
Counter operator++(int)
Definition: counter.hpp:57
Future< double > value() const override
Definition: counter.hpp:41
void reset()
Definition: counter.hpp:46
Counter & operator+=(int64_t v)
Definition: counter.hpp:64
Definition: none.hpp:27
const std::string & name() const
Definition: metric.hpp:39
Definition: executor.hpp:48
Counter(const std::string &name, const Option< Duration > &window=None())
Definition: counter.hpp:32
void push(double value)
Definition: metric.hpp:63
PID< MetricsProcess > metrics
Definition: future.hpp:58