]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
DecayCounter: use modern encoding
authorSage Weil <sage@inktank.com>
Tue, 25 Sep 2012 17:48:09 +0000 (10:48 -0700)
committerGreg Farnum <greg@inktank.com>
Fri, 8 Feb 2013 21:17:51 +0000 (13:17 -0800)
Signed-off-by: Sage Weil <sage@inktank.com>
Signed-off-by: Greg Farnum <greg@inktank.com>
src/Makefile.am
src/common/DecayCounter.cc [new file with mode: 0644]
src/common/DecayCounter.h
src/test/encoding/types.h

index b40ffff922b9912d44b67c321044d9cb3988b64f..142daea072503e3f7b6e7acaf359b68cc0cd01dd 100644 (file)
@@ -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 (file)
index 0000000..67a129c
--- /dev/null
@@ -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 <sage@newdream.net>
+ *
+ * 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<DecayCounter*>& 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;
+  }
+}
index fa6f85f49b0f6ee6f8147d6794ea2b68d5c40d8a..4e69a88696308483242cff3e76978b097d4c436f 100644 (file)
@@ -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<DecayCounter*>& 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); }
index cc7e3ec862d8b6d5826c1f477d89e7b9a5d83376..724b09daaa7ec0e606be25305b9dfacf19a5fcf8 100644 (file)
@@ -8,6 +8,9 @@ TYPE(filepath)
 TYPE(SnapContext)
 TYPE(SnapRealmInfo)
 
+#include "common/DecayCounter.h"
+TYPE(DecayCounter)
+
 #include "common/LogEntry.h"
 TYPE(LogEntryKey)
 TYPE(LogEntry)