]> git.proxmox.com Git - ceph.git/blob - ceph/src/common/DecayCounter.cc
bump version to 18.2.2-pve1
[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 #include "include/encoding.h"
19
20 void DecayCounter::encode(ceph::buffer::list& bl) const
21 {
22 decay();
23 ENCODE_START(5, 4, bl);
24 encode(val, bl);
25 ENCODE_FINISH(bl);
26 }
27
28 void DecayCounter::decode(ceph::buffer::list::const_iterator &p)
29 {
30 DECODE_START_LEGACY_COMPAT_LEN(5, 4, 4, p);
31 if (struct_v < 2) {
32 double k = 0.0;
33 decode(k, p);
34 }
35 if (struct_v < 3) {
36 double k = 0.0;
37 decode(k, p);
38 }
39 decode(val, p);
40 if (struct_v < 5) {
41 double delta, _;
42 decode(delta, p);
43 val += delta;
44 decode(_, p); /* velocity */
45 }
46 last_decay = clock::now();
47 DECODE_FINISH(p);
48 }
49
50 void DecayCounter::dump(ceph::Formatter *f) const
51 {
52 decay();
53 f->dump_float("value", val);
54 f->dump_float("halflife", rate.get_halflife());
55 }
56
57 void DecayCounter::generate_test_instances(std::list<DecayCounter*>& ls)
58 {
59 DecayCounter *counter = new DecayCounter();
60 counter->val = 3.0;
61 ls.push_back(counter);
62 counter = new DecayCounter();
63 ls.push_back(counter);
64 }
65
66 void DecayCounter::decay(double delta) const
67 {
68 auto now = clock::now();
69 double el = std::chrono::duration<double>(now - last_decay).count();
70
71 // calculate new value
72 double newval = val * exp(el * rate.k) + delta;
73 if (newval < .01) {
74 newval = 0.0;
75 }
76
77 val = newval;
78 last_decay = now;
79 }