Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / common / DecayCounter.cc
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 #include "DecayCounter.h"
16 #include "Formatter.h"
17
18 void DecayCounter::encode(bufferlist& bl) const
19 {
20   ENCODE_START(4, 4, bl);
21   ::encode(val, bl);
22   ::encode(delta, bl);
23   ::encode(vel, bl);
24   ENCODE_FINISH(bl);
25 }
26
27 void DecayCounter::decode(const utime_t &t, bufferlist::iterator &p)
28 {
29   DECODE_START_LEGACY_COMPAT_LEN(4, 4, 4, p);
30   if (struct_v < 2) {
31     double half_life;
32     ::decode(half_life, p);
33   }
34   if (struct_v < 3) {
35     double k;
36     ::decode(k, p);
37   }
38   ::decode(val, p);
39   ::decode(delta, p);
40   ::decode(vel, p);
41   DECODE_FINISH(p);
42 }
43
44 void DecayCounter::dump(Formatter *f) const
45 {
46   f->dump_float("value", val);
47   f->dump_float("delta", delta);
48   f->dump_float("velocity", vel);
49 }
50
51 void DecayCounter::generate_test_instances(list<DecayCounter*>& ls)
52 {
53   utime_t fake_time;
54   DecayCounter *counter = new DecayCounter(fake_time);
55   counter->val = 3.0;
56   counter->delta = 2.0;
57   counter->vel = 1.0;
58   ls.push_back(counter);
59   counter = new DecayCounter(fake_time);
60   ls.push_back(counter);
61 }
62
63 void DecayCounter::decay(utime_t now, const DecayRate &rate)
64 {
65   utime_t el = now;
66   el -= last_decay;
67
68   if (el.sec() >= 1) {
69     // calculate new value
70     double newval = (val+delta) * exp((double)el * rate.k);
71     if (newval < .01)
72       newval = 0.0;
73
74     // calculate velocity approx
75     vel += (newval - val) * (double)el;
76     vel *= exp((double)el * rate.k);
77
78     val = newval;
79     delta = 0;
80     last_decay = now;
81   }
82 }