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