]> git.proxmox.com Git - ceph.git/blob - ceph/src/common/DecayCounter.h
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / common / DecayCounter.h
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3 /*
4 * Ceph - scalable distributed file system
5 *
6 * Copyright (C) 2004-2006 Sage Weil <sage@newdream.net>
7 *
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License version 2.1, as published by the Free Software
11 * Foundation. See file COPYING.
12 *
13 */
14
15 #ifndef CEPH_DECAYCOUNTER_H
16 #define CEPH_DECAYCOUNTER_H
17
18 #include "include/buffer.h"
19 #include "common/Formatter.h"
20 #include "common/ceph_time.h"
21
22 #include <cmath>
23 #include <list>
24 #include <sstream>
25
26 /**
27 *
28 * TODO: normalize value based on some function of half_life,
29 * so that it can be interpreted as an approximation of a
30 * moving average of N seconds. currently, changing half-life
31 * skews the scale of the value, even at steady state.
32 *
33 */
34
35 class DecayRate {
36 public:
37 friend class DecayCounter;
38
39 DecayRate() {}
40 // cppcheck-suppress noExplicitConstructor
41 DecayRate(double hl) { set_halflife(hl); }
42 DecayRate(const DecayRate &dr) : k(dr.k) {}
43
44 void set_halflife(double hl) {
45 k = log(.5) / hl;
46 }
47
48 private:
49 double k = 0; // k = ln(.5)/half_life
50 };
51
52 class DecayCounter {
53 public:
54 using time = ceph::coarse_mono_time;
55 using clock = ceph::coarse_mono_clock;
56
57 DecayCounter() : DecayCounter(DecayRate()) {}
58 explicit DecayCounter(const DecayRate &rate) : last_decay(clock::now()), rate(rate) {}
59
60 void encode(bufferlist& bl) const;
61 void decode(bufferlist::const_iterator& p);
62 void dump(Formatter *f) const;
63 static void generate_test_instances(std::list<DecayCounter*>& ls);
64
65 /**
66 * reading
67 */
68
69 double get() const {
70 decay();
71 return val;
72 }
73
74 double get_last() const {
75 return val;
76 }
77
78 time get_last_decay() const {
79 return last_decay;
80 }
81
82 /**
83 * adjusting
84 */
85
86 double hit(double v = 1.0) {
87 decay(v);
88 return val;
89 }
90 void adjust(double v = 1.0) {
91 decay(v);
92 }
93
94 void scale(double f) {
95 val *= f;
96 }
97
98 /**
99 * decay etc.
100 */
101
102 void reset() {
103 last_decay = clock::now();
104 val = 0;
105 }
106
107 protected:
108 void decay(double delta) const;
109 void decay() const {decay(0.0);}
110
111 private:
112 mutable double val = 0.0; // value
113 mutable time last_decay = clock::zero(); // time of last decay
114 DecayRate rate;
115 };
116
117 inline void encode(const DecayCounter &c, bufferlist &bl) {
118 c.encode(bl);
119 }
120 inline void decode(DecayCounter &c, bufferlist::const_iterator &p) {
121 c.decode(p);
122 }
123
124 inline std::ostream& operator<<(std::ostream& out, const DecayCounter& d) {
125 std::ostringstream oss;
126 oss.precision(2);
127 double val = d.get();
128 oss << "[C " << std::scientific << val << "]";
129 return out << oss.str();
130 }
131
132 #endif