You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mesos.apache.org by bm...@apache.org on 2014/04/18 20:56:56 UTC

git commit: Added history to metrics library.

Repository: mesos
Updated Branches:
  refs/heads/master 0d5c80386 -> eb34b222c


Added history to metrics library.

Review: https://reviews.apache.org/r/20015


Project: http://git-wip-us.apache.org/repos/asf/mesos/repo
Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/eb34b222
Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/eb34b222
Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/eb34b222

Branch: refs/heads/master
Commit: eb34b222c831628ac11162c20d5c0c1211fe121e
Parents: 0d5c803
Author: Dominic Hamon <dh...@twopensource.com>
Authored: Fri Apr 18 11:44:11 2014 -0700
Committer: Benjamin Mahler <bm...@twitter.com>
Committed: Fri Apr 18 11:56:51 2014 -0700

----------------------------------------------------------------------
 .../include/process/metrics/counter.hpp         | 26 ++++++----
 .../include/process/metrics/gauge.hpp           | 15 ++++--
 .../include/process/metrics/metric.hpp          | 47 +++++++++++++----
 3rdparty/libprocess/src/tests/metrics_tests.cpp | 53 ++++++++++++++++++++
 4 files changed, 118 insertions(+), 23 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/eb34b222/3rdparty/libprocess/include/process/metrics/counter.hpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/include/process/metrics/counter.hpp b/3rdparty/libprocess/include/process/metrics/counter.hpp
index f4774ad..6800e7a 100644
--- a/3rdparty/libprocess/include/process/metrics/counter.hpp
+++ b/3rdparty/libprocess/include/process/metrics/counter.hpp
@@ -5,6 +5,8 @@
 
 #include <process/metrics/metric.hpp>
 
+#include <stout/memory.hpp>
+
 namespace process {
 namespace metrics {
 
@@ -13,9 +15,15 @@ namespace metrics {
 class Counter : public Metric
 {
 public:
-  explicit Counter(const std::string& name)
-    : Metric(name),
-      data(new Data()) {}
+  // 'name' is the unique name for the instance of Counter being constructed.
+  // This is what will be used as the key in the JSON endpoint.
+  // 'window' is the amount of history to keep for this Metric.
+  Counter(const std::string& name, const Option<Duration>& window = None())
+    : Metric(name, window),
+      data(new Data())
+  {
+    push(data->v);
+  }
 
   virtual ~Counter() {}
 
@@ -26,7 +34,7 @@ public:
 
   void reset()
   {
-    __sync_fetch_and_and(&data->v, 0);
+    push(__sync_and_and_fetch(&data->v, 0));
   }
 
   Counter& operator ++ ()
@@ -43,7 +51,7 @@ public:
 
   Counter& operator += (int64_t v)
   {
-    __sync_fetch_and_add(&data->v, v);
+    push(__sync_add_and_fetch(&data->v, v));
     return *this;
   }
 
@@ -56,10 +64,10 @@ private:
     volatile int64_t v;
   };
 
-  boost::shared_ptr<Data> data;
+  memory::shared_ptr<Data> data;
 };
 
-}  // namespace metrics {
-}  // namespace process {
+} // namespace metrics {
+} // namespace process {
 
-#endif  // __PROCESS_METRICS_COUNTER_HPP__
+#endif // __PROCESS_METRICS_COUNTER_HPP__

http://git-wip-us.apache.org/repos/asf/mesos/blob/eb34b222/3rdparty/libprocess/include/process/metrics/gauge.hpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/include/process/metrics/gauge.hpp b/3rdparty/libprocess/include/process/metrics/gauge.hpp
index 4f5c108..e10ca94 100644
--- a/3rdparty/libprocess/include/process/metrics/gauge.hpp
+++ b/3rdparty/libprocess/include/process/metrics/gauge.hpp
@@ -8,6 +8,8 @@
 #include <process/metrics/metric.hpp>
 
 #include <stout/lambda.hpp>
+#include <stout/memory.hpp>
+#include <stout/option.hpp>
 
 namespace process {
 namespace metrics {
@@ -17,9 +19,12 @@ namespace metrics {
 class Gauge : public Metric
 {
 public:
+  // 'name' is the unique name for the instance of Gauge being constructed.
+  // It will be the key exposed in the JSON endpoint.
+  // 'f' is the deferred object called when the Metric value is requested.
   Gauge(const std::string& name,
         const Deferred<Future<double> (void)>& f)
-    : Metric(name),
+    : Metric(name, None()),
       data(new Data(f)) {}
 
   virtual ~Gauge() {}
@@ -35,10 +40,10 @@ private:
     const Deferred<Future<double> (void)> f;
   };
 
-  boost::shared_ptr<Data> data;
+  memory::shared_ptr<Data> data;
 };
 
-}  // namespace metrics {
-}  // namespace process {
+} // namespace metrics {
+} // namespace process {
 
-#endif  // __PROCESS_METRICS_GAUGE_HPP__
+#endif // __PROCESS_METRICS_GAUGE_HPP__

http://git-wip-us.apache.org/repos/asf/mesos/blob/eb34b222/3rdparty/libprocess/include/process/metrics/metric.hpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/include/process/metrics/metric.hpp b/3rdparty/libprocess/include/process/metrics/metric.hpp
index ea64f69..6c48357 100644
--- a/3rdparty/libprocess/include/process/metrics/metric.hpp
+++ b/3rdparty/libprocess/include/process/metrics/metric.hpp
@@ -3,9 +3,14 @@
 
 #include <string>
 
-#include <boost/smart_ptr/shared_ptr.hpp>
-
 #include <process/future.hpp>
+#include <process/internal.hpp>
+#include <process/owned.hpp>
+#include <process/timeseries.hpp>
+
+#include <stout/duration.hpp>
+#include <stout/memory.hpp>
+#include <stout/option.hpp>
 
 namespace process {
 namespace metrics {
@@ -21,20 +26,44 @@ public:
 
 protected:
   // Only derived classes can construct.
-  explicit Metric(const std::string& name)
-    : data(new Data(name)) {}
+  Metric(const std::string& name, const Option<Duration>& window)
+    : data(new Data(name, window)) {}
+
+  // Inserts 'value' into the history for this metric.
+  void push(double value) {
+    if (data->history.isSome()) {
+      Time now = Clock::now();
+
+      internal::acquire(&data->lock);
+      data->history.get()->set(value, now);
+      internal::release(&data->lock);
+    }
+  }
 
 private:
   struct Data {
-    explicit Data(const std::string& _name) : name(_name) {}
+    Data(const std::string& _name, const Option<Duration>& window)
+      : name(_name),
+        lock(0),
+        history(None())
+    {
+      if (window.isSome()) {
+        history =
+          Owned<TimeSeries<double> >(new TimeSeries<double>(window.get()));
+      }
+    }
 
     const std::string name;
+
+    int lock;
+
+    Option<Owned<TimeSeries<double> > > history;
   };
 
-  boost::shared_ptr<Data> data;
+  memory::shared_ptr<Data> data;
 };
 
-}  // namespace metrics {
-}  // namespace process {
+} // namespace metrics {
+} // namespace process {
 
-#endif  // __PROCESS_METRICS_METRIC_HPP__
+#endif // __PROCESS_METRICS_METRIC_HPP__

http://git-wip-us.apache.org/repos/asf/mesos/blob/eb34b222/3rdparty/libprocess/src/tests/metrics_tests.cpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/src/tests/metrics_tests.cpp b/3rdparty/libprocess/src/tests/metrics_tests.cpp
index 0cc9f4b..abe1588 100644
--- a/3rdparty/libprocess/src/tests/metrics_tests.cpp
+++ b/3rdparty/libprocess/src/tests/metrics_tests.cpp
@@ -2,20 +2,24 @@
 
 #include <stout/gtest.hpp>
 
+#include <process/clock.hpp>
 #include <process/future.hpp>
 #include <process/gtest.hpp>
 #include <process/process.hpp>
+#include <process/time.hpp>
 
 #include <process/metrics/counter.hpp>
 #include <process/metrics/gauge.hpp>
 #include <process/metrics/metrics.hpp>
 
 
+using process::Clock;
 using process::Deferred;
 using process::Failure;
 using process::Future;
 using process::PID;
 using process::Process;
+using process::Time;
 
 using process::metrics::add;
 using process::metrics::remove;
@@ -54,6 +58,55 @@ TEST(MetricsTest, Counter)
 }
 
 
+TEST(MetricsTest, CounterHistory)
+{
+  Clock::pause();
+  Time t0 = Clock::now();
+
+  Counter c("test/counter", process::TIME_SERIES_WINDOW);
+  AWAIT_READY(add(c));
+
+  Clock::advance(Seconds(1));
+  Time t1 = Clock::now();
+  ++c;
+
+  Clock::advance(Seconds(1));
+  Time t2 = Clock::now();
+  ++c;
+
+  // TODO(dhamon): get json/history from metrics process and check
+  // the history.
+
+  AWAIT_READY(remove(c));
+}
+
+
+// TODO(dhamon): Expand benchmarks and enable them.
+// TEST(MetricsTest, CounterBM)
+// {
+//   for (int i = 0; i < 10; ++i) {
+//     Counter c("test/counter", Seconds(1));
+//
+//     // Fill the history
+//     Time t0 = Clock::now();
+//     while (Clock::now() - t0 < Seconds(1)) {
+//       c++;
+//     }
+//
+//     // Run the benchmark
+//     t0 = Clock::now();
+//     int numInc = 0;
+//     while (Clock::now() - t0 < Seconds(1)) {
+//       c++;
+//       ++numInc;
+//     }
+//     std::cout << numInc << "\n";
+//   }
+//
+//   EXPECT_TRUE(true);
+// }
+
+
 TEST(MetricsTest, Gauge)
 {
   ASSERT_TRUE(GTEST_IS_THREADSAFE);