]> git.proxmox.com Git - ceph.git/blob - ceph/src/common/DecayCounter.cc
update sources to 12.2.7
[ceph.git] / 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 if (now >= last_decay) {
66 double el = (double)(now - last_decay);
67 if (el >= 1.0) {
68 // calculate new value
69 double newval = (val+delta) * exp(el * rate.k);
70 if (newval < .01)
71 newval = 0.0;
72
73 // calculate velocity approx
74 vel += (newval - val) * el;
75 vel *= exp(el * rate.k);
76
77 val = newval;
78 delta = 0;
79 last_decay = now;
80 }
81 } else {
82 last_decay = now;
83 }
84 }