From: Sage Weil Date: Tue, 25 Sep 2012 17:48:09 +0000 (-0700) Subject: DecayCounter: use modern encoding X-Git-Tag: v0.58~100^2~46 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=d5a6a251ae2023349f786db2c4c4563d5170a0f4;p=ceph.git DecayCounter: use modern encoding Signed-off-by: Sage Weil Signed-off-by: Greg Farnum --- diff --git a/src/Makefile.am b/src/Makefile.am index b40ffff922b..142daea0725 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1229,6 +1229,7 @@ libcommon_files = \ auth/Crypto.cc \ auth/KeyRing.cc \ auth/RotatingKeyRing.cc \ + common/DecayCounter.cc \ common/LogClient.cc \ common/LogEntry.cc \ common/PrebufferedStreambuf.cc \ diff --git a/src/common/DecayCounter.cc b/src/common/DecayCounter.cc new file mode 100644 index 00000000000..67a129ccd09 --- /dev/null +++ b/src/common/DecayCounter.cc @@ -0,0 +1,82 @@ +// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- +// vim: ts=8 sw=2 smarttab +/* + * Ceph - scalable distributed file system + * + * Copyright (C) 2004-2006 Sage Weil + * + * This is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software + * Foundation. See file COPYING. + * + */ + +#include "DecayCounter.h" +#include "Formatter.h" + +void DecayCounter::encode(bufferlist& bl) const +{ + ENCODE_START(4, 4, bl); + ::encode(val, bl); + ::encode(delta, bl); + ::encode(vel, bl); + ENCODE_FINISH(bl); +} + +void DecayCounter::decode(const utime_t &t, bufferlist::iterator &p) +{ + DECODE_START_LEGACY_COMPAT_LEN(4, 4, 4, p); + if (struct_v < 2) { + double half_life; + ::decode(half_life, p); + } + if (struct_v < 3) { + double k; + ::decode(k, p); + } + ::decode(val, p); + ::decode(delta, p); + ::decode(vel, p); + DECODE_FINISH(p); +} + +void DecayCounter::dump(Formatter *f) const +{ + f->dump_float("value", val); + f->dump_float("delta", delta); + f->dump_float("velocity", vel); +} + +void DecayCounter::generate_test_instances(list& ls) +{ + utime_t fake_time; + DecayCounter *counter = new DecayCounter(fake_time); + counter->val = 3.0; + counter->delta = 2.0; + counter->vel = 1.0; + ls.push_back(counter); + counter = new DecayCounter(fake_time); + ls.push_back(counter); +} + +void DecayCounter::decay(utime_t now, const DecayRate &rate) +{ + utime_t el = now; + el -= last_decay; + + if (el.sec() >= 1) { + // calculate new value + double newval = (val+delta) * exp((double)el * rate.k); + if (newval < .01) + newval = 0.0; + + // calculate velocity approx + vel += (newval - val) * (double)el; + vel *= exp((double)el * rate.k); + + val = newval; + delta = 0; + last_decay = now; + } +} diff --git a/src/common/DecayCounter.h b/src/common/DecayCounter.h index fa6f85f49b0..4e69a886963 100644 --- a/src/common/DecayCounter.h +++ b/src/common/DecayCounter.h @@ -51,34 +51,24 @@ public: public: - void encode(bufferlist& bl) const { - __u8 struct_v = 3; - ::encode(struct_v, bl); - ::encode(val, bl); - ::encode(delta, bl); - ::encode(vel, bl); - } - void decode(const utime_t &t, bufferlist::iterator &p) { - __u8 struct_v; - ::decode(struct_v, p); - if (struct_v < 2) { - double half_life; - ::decode(half_life, p); - } - if (struct_v < 3) { - double k; - ::decode(k, p); - } - ::decode(val, p); - ::decode(delta, p); - ::decode(vel, p); - } + void encode(bufferlist& bl) const; + void decode(const utime_t &t, bufferlist::iterator& p); + void dump(Formatter *f) const; + static void generate_test_instances(list& ls); DecayCounter(const utime_t &now) : val(0), delta(0), vel(0), last_decay(now) { } + // these two functions are for the use of our dencoder testing infrastructure + DecayCounter() : val(0), delta(0), vel(0), last_decay() {} + + void decode(bufferlist::iterator& p) { + utime_t fake_time; + decode(fake_time, p); + } + /** * reading */ @@ -131,25 +121,8 @@ public: last_decay = now; val = delta = 0; } - - void decay(utime_t now, const DecayRate &rate) { - utime_t el = now; - el -= last_decay; - - if (el.sec() >= 1) { - // calculate new value - double newval = (val+delta) * exp((double)el * rate.k); - if (newval < .01) newval = 0.0; - - // calculate velocity approx - vel += (newval - val) * (double)el; - vel *= exp((double)el * rate.k); - - val = newval; - delta = 0; - last_decay = now; - } - } + + void decay(utime_t now, const DecayRate &rate); }; inline void encode(const DecayCounter &c, bufferlist &bl) { c.encode(bl); } diff --git a/src/test/encoding/types.h b/src/test/encoding/types.h index cc7e3ec862d..724b09daaa7 100644 --- a/src/test/encoding/types.h +++ b/src/test/encoding/types.h @@ -8,6 +8,9 @@ TYPE(filepath) TYPE(SnapContext) TYPE(SnapRealmInfo) +#include "common/DecayCounter.h" +TYPE(DecayCounter) + #include "common/LogEntry.h" TYPE(LogEntryKey) TYPE(LogEntry)