]>
git.proxmox.com Git - ceph.git/blob - ceph/src/common/DecayCounter.h
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/ceph_time.h"
28 * TODO: normalize value based on some function of half_life,
29 * so that it can be interpreted as an approximation of a
30 * moving average of N seconds. currently, changing half-life
31 * skews the scale of the value, even at steady state.
37 friend class DecayCounter
;
40 // cppcheck-suppress noExplicitConstructor
41 DecayRate(double hl
) { set_halflife(hl
); }
42 DecayRate(const DecayRate
&dr
) : k(dr
.k
) {}
44 void set_halflife(double hl
) {
49 double k
= 0; // k = ln(.5)/half_life
54 using time
= ceph::coarse_mono_time
;
55 using clock
= ceph::coarse_mono_clock
;
57 DecayCounter() : DecayCounter(DecayRate()) {}
58 explicit DecayCounter(const DecayRate
&rate
) : last_decay(clock::now()), rate(rate
) {}
60 void encode(bufferlist
& bl
) const;
61 void decode(bufferlist::const_iterator
& p
);
62 void dump(Formatter
*f
) const;
63 static void generate_test_instances(std::list
<DecayCounter
*>& ls
);
74 double get_last() const {
78 time
get_last_decay() const {
86 double hit(double v
= 1.0) {
90 void adjust(double v
= 1.0) {
94 void scale(double f
) {
103 last_decay
= clock::now();
108 void decay(double delta
) const;
109 void decay() const {decay(0.0);}
112 mutable double val
= 0.0; // value
113 mutable time last_decay
= clock::zero(); // time of last decay
117 inline void encode(const DecayCounter
&c
, bufferlist
&bl
) {
120 inline void decode(DecayCounter
&c
, bufferlist::const_iterator
&p
) {
124 inline std::ostream
& operator<<(std::ostream
& out
, const DecayCounter
& d
) {
125 std::ostringstream oss
;
127 double val
= d
.get();
128 oss
<< "[C " << std::scientific
<< val
<< "]";
129 return out
<< oss
.str();