1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2004-2006 Sage Weil <sage@newdream.net>
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.
15 #ifndef CEPH_DECAYCOUNTER_H
16 #define CEPH_DECAYCOUNTER_H
18 #include "include/buffer.h"
19 #include "common/Formatter.h"
20 #include "common/StackStringStream.h"
21 #include "common/ceph_time.h"
29 * TODO: normalize value based on some function of half_life,
30 * so that it can be interpreted as an approximation of a
31 * moving average of N seconds. currently, changing half-life
32 * skews the scale of the value, even at steady state.
38 friend class DecayCounter
;
41 // cppcheck-suppress noExplicitConstructor
42 DecayRate(double hl
) { set_halflife(hl
); }
43 DecayRate(const DecayRate
&dr
) : k(dr
.k
) {}
45 void set_halflife(double hl
) {
48 double get_halflife() const {
53 double k
= 0; // k = ln(.5)/half_life
58 using time
= ceph::coarse_mono_time
;
59 using clock
= ceph::coarse_mono_clock
;
61 DecayCounter() : DecayCounter(DecayRate()) {}
62 explicit DecayCounter(const DecayRate
&rate
) : last_decay(clock::now()), rate(rate
) {}
64 void encode(ceph::buffer::list
& bl
) const;
65 void decode(ceph::buffer::list::const_iterator
& p
);
66 void dump(ceph::Formatter
*f
) const;
67 static void generate_test_instances(std::list
<DecayCounter
*>& ls
);
78 double get_last() const {
82 time
get_last_decay() const {
90 double hit(double v
= 1.0) {
94 void adjust(double v
= 1.0) {
98 void scale(double f
) {
107 last_decay
= clock::now();
112 void decay(double delta
) const;
113 void decay() const {decay(0.0);}
116 mutable double val
= 0.0; // value
117 mutable time last_decay
= clock::zero(); // time of last decay
121 inline void encode(const DecayCounter
&c
, ceph::buffer::list
&bl
) {
124 inline void decode(DecayCounter
&c
, ceph::buffer::list::const_iterator
&p
) {
128 inline std::ostream
& operator<<(std::ostream
& out
, const DecayCounter
& d
) {
129 CachedStackStringStream css
;
131 double val
= d
.get();
132 *css
<< "[C " << std::scientific
<< val
<< "]";
133 return out
<< css
->strv();