]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
histogram: calculate bin position of a value in the histrogram
authorSage Weil <sage@inktank.com>
Fri, 24 Jan 2014 02:11:29 +0000 (18:11 -0800)
committerSage Weil <sage@inktank.com>
Sun, 16 Feb 2014 06:07:04 +0000 (22:07 -0800)
Generate a lower and upper bound.

Signed-off-by: Sage Weil <sage@inktank.com>
src/include/histogram.h

index c817b1ec175a72724dc6b780a329d527c2bc1a20..73ddd5d289b51916e963e0ae626517e35cdc49e0 100644 (file)
@@ -48,6 +48,39 @@ public:
     h[bin] = v;
     _contract();
   }
+  static int calc_bits_of(int t) {
+    int b = 0;
+    while (t > 0) {
+      t = t >> 1;
+      b++;
+    }
+    return b;
+  }
+
+  /// get a value's position in the histogram.
+  ///
+  /// positions are represented as values in the range [0..1000000]
+  /// (millionths on the unit interval).
+  ///
+  /// @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) {
+    if (v < 0)
+      return -ERANGE;
+    unsigned bin = calc_bits_of(v);
+    unsigned lower_sum = 0, upper_sum = 0, total = 0;
+    for (unsigned i=0; i<h.size(); ++i) {
+      if (i <= bin)
+       upper_sum += h[i];
+      if (i < bin)
+       lower_sum += h[i];
+      total += h[i];
+    }
+    *lower = lower_sum * 1000000 / total;
+    *upper = upper_sum * 1000000 / total;
+    return 0;
+  }
 
   void add(const pow2_hist_t& o) {
     _expand_to(o.h.size());