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