]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
histogram: fix histogram::get_position_micro overflow
authorLoic Dachary <loic@dachary.org>
Mon, 27 Jan 2014 13:20:25 +0000 (14:20 +0100)
committerSage Weil <sage@inktank.com>
Sun, 16 Feb 2014 06:09:38 +0000 (22:09 -0800)
Convert the return values to uint64_t

Signed-off-by: Loic Dachary <loic@dachary.org>
src/common/histogram.h
src/osd/ReplicatedPG.cc
src/test/common/histogram.cc

index ceca6d90053ffdcf5b27e870f277fea1e0ae8905..8d1ad8ea29a3b702c5a378635525c78cf70a1924 100644 (file)
@@ -82,11 +82,11 @@ public:
   /// @param v [in] value (non-negative)
   /// @param lower [out] pointer to lower-bound (0..1000000)
   /// @param upper [out] pointer to the upper bound (0..1000000)
-  int get_position_micro(int32_t v, unsigned *lower, unsigned *upper) {
+  int get_position_micro(int32_t v, uint64_t *lower, uint64_t *upper) {
     if (v < 0)
       return -1;
     unsigned bin = calc_bits_of(v);
-    unsigned lower_sum = 0, upper_sum = 0, total = 0;
+    uint64_t lower_sum = 0, upper_sum = 0, total = 0;
     for (unsigned i=0; i<h.size(); ++i) {
       if (i <= bin)
        upper_sum += h[i];
index e2f6bec322554bc88633f3d0f2def316941d3e49..714cf38ab7a2dc543273ec5e1b4089221fb4e5d5 100644 (file)
@@ -10354,7 +10354,7 @@ bool ReplicatedPG::agent_maybe_evict(ObjectContextRef& obc)
     int atime = -1, temp = 0;
     agent_estimate_atime_temp(soid, &atime, NULL /*FIXME &temp*/);
 
-    unsigned atime_upper = 0, atime_lower = 0;
+    uint64_t atime_upper = 0, atime_lower = 0;
     if (atime < 0 && obc->obs.oi.mtime != utime_t())
       atime = ceph_clock_now(NULL).sec() - obc->obs.oi.mtime;
     if (atime < 0)
index 6cbf648534db6d42f1f86ccf2968da019d5959f6..2fd3cfec2a7af92f7d0d12f17cbca9f5bccb9fbb 100644 (file)
@@ -49,48 +49,58 @@ TEST(Histogram, Set) {
 TEST(Histogram, Position) {
   {
     pow2_hist_t h;
-    unsigned lb, ub;
+    uint64_t lb, ub;
     h.add(0);
     ASSERT_EQ(-1, h.get_position_micro(-20, &lb, &ub));
   }
   {
     pow2_hist_t h;
     h.add(0);
-    unsigned lb, ub;
+    uint64_t lb, ub;
     h.get_position_micro(0, &lb, &ub);
-    ASSERT_EQ(0, lb);
-    ASSERT_EQ(1000000, ub);
+    ASSERT_EQ(0u, lb);
+    ASSERT_EQ(1000000u, ub);
     h.add(0);
     h.add(0);
     h.add(0);
     h.get_position_micro(0, &lb, &ub);
-    ASSERT_EQ(0, lb);
-    ASSERT_EQ(1000000, ub);
+    ASSERT_EQ(0u, lb);
+    ASSERT_EQ(1000000u, ub);
   }
   {
     pow2_hist_t h;
     h.add(1);
     h.add(1);
-    unsigned lb, ub;
+    uint64_t lb, ub;
     h.get_position_micro(0, &lb, &ub);
-    ASSERT_EQ(0, lb);
-    ASSERT_EQ(0, ub);
+    ASSERT_EQ(0u, lb);
+    ASSERT_EQ(0u, ub);
     h.add(0);
     h.get_position_micro(0, &lb, &ub);
-    ASSERT_EQ(0, lb);
-    ASSERT_EQ(333333, ub);
+    ASSERT_EQ(0u, lb);
+    ASSERT_EQ(333333u, ub);
     h.get_position_micro(1, &lb, &ub);
-    ASSERT_EQ(333333, lb);
-    ASSERT_EQ(1000000, ub);
+    ASSERT_EQ(333333u, lb);
+    ASSERT_EQ(1000000u, ub);
   }
   {
     pow2_hist_t h;
-    h.add(1);
-    h.add(10);
-    unsigned lb, ub;
+    h.h.resize(10, 0);
+    h.h[0] = 1;
+    h.h[5] = 1;
+    uint64_t lb, ub;
     h.get_position_micro(4, &lb, &ub);
-    ASSERT_EQ(500000, lb);
-    ASSERT_EQ(500000, ub);
+    ASSERT_EQ(500000u, lb);
+    ASSERT_EQ(500000u, ub);
+  }
+  {
+    pow2_hist_t h;
+    h.h.resize(10, 0);
+    h.h[0] = UINT_MAX;
+    h.h[5] = UINT_MAX;
+    uint64_t lb, ub;
+    ASSERT_EQ(500000u, lb);
+    ASSERT_EQ(500000u, ub);
   }
 }
 
@@ -104,3 +114,13 @@ TEST(Histogram, Decay) {
   ASSERT_EQ(6, h.h[3]);
   ASSERT_EQ(4u, h.h.size());
 }
+
+/*
+ * Local Variables:
+ * compile-command: "cd ../.. ; make -j4 &&
+ *   make unittest_histogram &&
+ *   valgrind --tool=memcheck --leak-check=full \
+ *      ./unittest_histogram
+ *   "
+ * End:
+ */