summaryrefslogtreecommitdiff
path: root/src/common/DecayCounter.h
blob: fa6f85f49b0f6ee6f8147d6794ea2b68d5c40d8a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- 
// vim: ts=8 sw=2 smarttab
/*
 * Ceph - scalable distributed file system
 *
 * Copyright (C) 2004-2006 Sage Weil <sage@newdream.net>
 *
 * This is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License version 2.1, as published by the Free Software 
 * Foundation.  See file COPYING.
 * 
 */

#ifndef CEPH_DECAYCOUNTER_H
#define CEPH_DECAYCOUNTER_H

#include "include/utime.h"

#include <math.h>

/**
 *
 * TODO: normalize value based on some fucntion of half_life, 
 *  so that it can be interpreted as an approximation of a
 *  moving average of N seconds.  currently, changing half-life
 *  skews the scale of the value, even at steady state.  
 *
 */

class DecayRate {
  double k;             // k = ln(.5)/half_life

  friend class DecayCounter;

public:
  DecayRate() : k(0) {}
  DecayRate(double hl) { set_halflife(hl); }
  void set_halflife(double hl) {
    k = ::log(.5) / hl;
  }    
};

class DecayCounter {
 protected:
public:
  double val;           // value
  double delta;         // delta since last decay
  double vel;           // recent velocity
  utime_t last_decay;   // time of last decay

 public:

  void encode(bufferlist& bl) const {
    __u8 struct_v = 3;
    ::encode(struct_v, bl);
    ::encode(val, bl);
    ::encode(delta, bl);
    ::encode(vel, bl);
  }
  void decode(const utime_t &t, bufferlist::iterator &p) {
    __u8 struct_v;
    ::decode(struct_v, p);
    if (struct_v < 2) {
      double half_life;
      ::decode(half_life, p);
    }
    if (struct_v < 3) {
      double k;
      ::decode(k, p);
    }
    ::decode(val, p);
    ::decode(delta, p);
    ::decode(vel, p);
  }

  DecayCounter(const utime_t &now)
    : val(0), delta(0), vel(0), last_decay(now)
  {
  }

  /**
   * reading
   */

  double get(utime_t now, const DecayRate& rate) {
    decay(now, rate);
    return val;
  }

  double get_last() {
    return val;
  }
  
  double get_last_vel() {
    return vel;
  }

  utime_t get_last_decay() { 
    return last_decay; 
  }

  /**
   * adjusting
   */

  double hit(utime_t now, const DecayRate& rate, double v = 1.0) {
    decay(now, rate);
    delta += v;
    return val+delta;
  }

  void adjust(double a) {
    val += a;
  }
  void adjust(utime_t now, const DecayRate& rate, double a) {
    decay(now, rate);
    val += a;
  }
  void scale(double f) {
    val *= f;
    delta *= f;    
    vel *= f;
  }

  /**
   * decay etc.
   */

  void reset(utime_t now) {
    last_decay = now;
    val = delta = 0;
  }
  
  void decay(utime_t now, const DecayRate &rate) {
    utime_t el = now;
    el -= last_decay;

    if (el.sec() >= 1) {
      // calculate new value
      double newval = (val+delta) * exp((double)el * rate.k);
      if (newval < .01) newval = 0.0;
      
      // calculate velocity approx
      vel += (newval - val) * (double)el;
      vel *= exp((double)el * rate.k);
      
      val = newval;
      delta = 0;
      last_decay = now;
    }
  }
};

inline void encode(const DecayCounter &c, bufferlist &bl) { c.encode(bl); }
inline void decode(DecayCounter &c, const utime_t &t, bufferlist::iterator &p) {
  c.decode(t, p);
}


#endif