]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
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/utime.h" | |
19 | ||
20 | #include <math.h> | |
21 | ||
22 | /** | |
23 | * | |
24 | * TODO: normalize value based on some fucntion of half_life, | |
25 | * so that it can be interpreted as an approximation of a | |
26 | * moving average of N seconds. currently, changing half-life | |
27 | * skews the scale of the value, even at steady state. | |
28 | * | |
29 | */ | |
30 | ||
31 | class DecayRate { | |
32 | double k; // k = ln(.5)/half_life | |
33 | ||
34 | friend class DecayCounter; | |
35 | ||
36 | public: | |
37 | DecayRate() : k(0) {} | |
38 | DecayRate(const DecayRate &dr) : k(dr.k) {} | |
39 | ||
40 | // cppcheck-suppress noExplicitConstructor | |
41 | DecayRate(double hl) { set_halflife(hl); } | |
42 | void set_halflife(double hl) { | |
43 | k = ::log(.5) / hl; | |
44 | } | |
45 | }; | |
46 | ||
47 | class DecayCounter { | |
48 | public: | |
49 | double val; // value | |
50 | double delta; // delta since last decay | |
51 | double vel; // recent velocity | |
52 | utime_t last_decay; // time of last decay | |
53 | DecayRate rate; | |
54 | ||
55 | void encode(bufferlist& bl) const; | |
56 | void decode(const utime_t &t, bufferlist::iterator& p); | |
57 | void dump(Formatter *f) const; | |
58 | static void generate_test_instances(list<DecayCounter*>& ls); | |
59 | ||
60 | explicit DecayCounter(const utime_t &now) | |
61 | : val(0), delta(0), vel(0), last_decay(now) | |
62 | { | |
63 | } | |
64 | ||
65 | explicit DecayCounter(const utime_t &now, const DecayRate &rate) | |
66 | : val(0), delta(0), vel(0), last_decay(now), rate(rate) | |
67 | { | |
68 | } | |
69 | ||
70 | // these two functions are for the use of our dencoder testing infrastructure | |
71 | DecayCounter() : val(0), delta(0), vel(0), last_decay() {} | |
72 | ||
73 | void decode(bufferlist::iterator& p) { | |
74 | utime_t fake_time; | |
75 | decode(fake_time, p); | |
76 | } | |
77 | ||
78 | /** | |
79 | * reading | |
80 | */ | |
81 | ||
82 | double get(utime_t now, const DecayRate& rate) { | |
83 | decay(now, rate); | |
84 | return val+delta; | |
85 | } | |
86 | double get(utime_t now) { | |
87 | decay(now, rate); | |
88 | return val+delta; | |
89 | } | |
90 | ||
91 | double get_last() { | |
92 | return val; | |
93 | } | |
94 | ||
95 | double get_last_vel() { | |
96 | return vel; | |
97 | } | |
98 | ||
99 | utime_t get_last_decay() { | |
100 | return last_decay; | |
101 | } | |
102 | ||
103 | /** | |
104 | * adjusting | |
105 | */ | |
106 | ||
107 | double hit(utime_t now, const DecayRate& rate, double v = 1.0) { | |
108 | decay(now, rate); | |
109 | delta += v; | |
110 | return val+delta; | |
111 | } | |
112 | double hit(utime_t now, double v = 1.0) { | |
113 | decay(now, rate); | |
114 | delta += v; | |
115 | return val+delta; | |
116 | } | |
117 | ||
118 | void adjust(double a) { | |
119 | val += a; | |
120 | } | |
121 | void adjust(utime_t now, const DecayRate& rate, double a) { | |
122 | decay(now, rate); | |
123 | val += a; | |
124 | } | |
125 | void scale(double f) { | |
126 | val *= f; | |
127 | delta *= f; | |
128 | vel *= f; | |
129 | } | |
130 | ||
131 | /** | |
132 | * decay etc. | |
133 | */ | |
134 | ||
135 | void reset(utime_t now) { | |
136 | last_decay = now; | |
137 | val = delta = 0; | |
138 | } | |
139 | ||
140 | void decay(utime_t now, const DecayRate &rate); | |
141 | }; | |
142 | ||
143 | inline void encode(const DecayCounter &c, bufferlist &bl) { c.encode(bl); } | |
144 | inline void decode(DecayCounter &c, const utime_t &t, bufferlist::iterator &p) { | |
145 | c.decode(t, p); | |
146 | } | |
147 | // for dencoder | |
148 | inline void decode(DecayCounter &c, bufferlist::iterator &p) { | |
149 | utime_t t; | |
150 | c.decode(t, p); | |
151 | } | |
152 | ||
153 | ||
154 | #endif |