Repository: mesos Updated Branches: refs/heads/master 617d55e24 -> ed830653a
Introduced a push-based gauge metric. A push-based gauge differs from a pull-based gauge in that the client is responsible for pushing the latest value into the gauge whenever it changes. This can be challenging in some cases as it requires the client to have a good handle on when the gauge value changes (rather than just computing the current value when asked). It is highly recommended to use push-based gauges if possible as they provide significant performance benefits over pull-based gauges. Pull-based gauge suffer from delays getting processed on the event queue of a `Process`, as well as incur computation cost on the `Process` each time the metrics are collected. Push-based gauges, on the other hand, incur no cost to the owning `Process` when metrics are collected, and instead incur a trivial cost when the `Process` pushes new values in. Review: https://reviews.apache.org/r/66828/ Project: http://git-wip-us.apache.org/repos/asf/mesos/repo Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/6707d39d Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/6707d39d Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/6707d39d Branch: refs/heads/master Commit: 6707d39d2ee2e1ccb62e4998ef57d4db8cda5c96 Parents: 617d55e Author: Benjamin Mahler <[email protected]> Authored: Fri Apr 27 16:06:53 2018 -0700 Committer: Gilbert Song <[email protected]> Committed: Fri Apr 27 16:06:53 2018 -0700 ---------------------------------------------------------------------- 3rdparty/libprocess/include/Makefile.am | 1 + .../include/process/metrics/push_gauge.hpp | 100 +++++++++++++++++++ 2 files changed, 101 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/mesos/blob/6707d39d/3rdparty/libprocess/include/Makefile.am ---------------------------------------------------------------------- diff --git a/3rdparty/libprocess/include/Makefile.am b/3rdparty/libprocess/include/Makefile.am index cd2c3bc..25520c2 100644 --- a/3rdparty/libprocess/include/Makefile.am +++ b/3rdparty/libprocess/include/Makefile.am @@ -47,6 +47,7 @@ nobase_include_HEADERS = \ process/mutex.hpp \ process/metrics/counter.hpp \ process/metrics/gauge.hpp \ + process/metrics/push_gauge.hpp \ process/metrics/metric.hpp \ process/metrics/metrics.hpp \ process/metrics/timer.hpp \ http://git-wip-us.apache.org/repos/asf/mesos/blob/6707d39d/3rdparty/libprocess/include/process/metrics/push_gauge.hpp ---------------------------------------------------------------------- diff --git a/3rdparty/libprocess/include/process/metrics/push_gauge.hpp b/3rdparty/libprocess/include/process/metrics/push_gauge.hpp new file mode 100644 index 0000000..5c39846 --- /dev/null +++ b/3rdparty/libprocess/include/process/metrics/push_gauge.hpp @@ -0,0 +1,100 @@ +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License + +#ifndef __PROCESS_METRICS_PUSH_GAUGE_HPP__ +#define __PROCESS_METRICS_PUSH_GAUGE_HPP__ + +#include <memory> +#include <string> + +#include <process/metrics/metric.hpp> + +namespace process { +namespace metrics { + +// A Metric that represents an instantaneous measurement of a value +// (e.g. number of items in a queue). +// +// A push-based gauge differs from a pull-based gauge in that the +// client is responsible for pushing the latest value into the gauge +// whenever it changes. This can be challenging in some cases as it +// requires the client to have a good handle on when the gauge value +// changes (rather than just computing the current value when asked). +// +// NOTE: It is highly recommended to use push-based gauges if +// possible as they provide significant performance benefits over +// pull-based gauges. Pull-based gauge suffer from delays getting +// processed on the event queue of a `Process`, as well as incur +// computation cost on the `Process` each time the metrics are +// collected. Push-based gauges, on the other hand, incur no cost +// to the owning `Process` when metrics are collected, and instead +// incur a trivial cost when the `Process` pushes new values in. +class PushGauge : 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 function that is called when the Metric value is requested. + // The user of `Gauge` must ensure that `f` is safe to execute up until + // the removal of the `Gauge` (via `process::metrics::remove(...)`) is + // complete. + explicit PushGauge(const std::string& name) + : Metric(name, None()), data(new Data()) {} + + virtual ~PushGauge() {} + + virtual Future<double> value() const + { + return static_cast<double>(data->value.load()); + } + + PushGauge& operator=(int64_t v) + { + data->value.store(v); + push(v); + return *this; + } + + PushGauge& operator++() { return *this += 1; } + + PushGauge& operator+=(int64_t v) + { + int64_t prev = data->value.fetch_add(v); + push(static_cast<double>(prev + v)); + return *this; + } + + PushGauge& operator--() { return *this -= 1; } + + PushGauge& operator-=(int64_t v) + { + int64_t prev = data->value.fetch_sub(v); + push(static_cast<double>(prev - v)); + return *this; + } + +private: + struct Data + { + explicit Data() : value(0) {} + + std::atomic<int64_t> value; + }; + + std::shared_ptr<Data> data; +}; + +} // namespace metrics { +} // namespace process { + +#endif // __PROCESS_METRICS_PUSH_GAUGE_HPP__
