diff options
author | A. Unique TensorFlower <gardener@tensorflow.org> | 2017-11-28 15:02:11 -0800 |
---|---|---|
committer | TensorFlower Gardener <gardener@tensorflow.org> | 2017-11-28 15:05:33 -0800 |
commit | 9306dd922fde7b739c5a4230fdc6d9bd646fb71c (patch) | |
tree | 7278ace07ca1d41704851a6d5a82accdc15cba6e /tensorflow/core/lib/monitoring | |
parent | efe5658aaa6f1666d4967880311430a70bdb23b9 (diff) |
Add bool value type support for gauge metrics.
PiperOrigin-RevId: 177223509
Diffstat (limited to 'tensorflow/core/lib/monitoring')
-rw-r--r-- | tensorflow/core/lib/monitoring/collected_metrics.h | 1 | ||||
-rw-r--r-- | tensorflow/core/lib/monitoring/collection_registry.h | 6 | ||||
-rw-r--r-- | tensorflow/core/lib/monitoring/gauge.h | 33 | ||||
-rw-r--r-- | tensorflow/core/lib/monitoring/gauge_test.cc | 22 | ||||
-rw-r--r-- | tensorflow/core/lib/monitoring/metric_def.h | 13 |
5 files changed, 69 insertions, 6 deletions
diff --git a/tensorflow/core/lib/monitoring/collected_metrics.h b/tensorflow/core/lib/monitoring/collected_metrics.h index fbef25619f..acdb0d86ed 100644 --- a/tensorflow/core/lib/monitoring/collected_metrics.h +++ b/tensorflow/core/lib/monitoring/collected_metrics.h @@ -88,6 +88,7 @@ struct Point { ValueType value_type; int64 int64_value; string string_value; + bool bool_value; HistogramProto histogram_value; // start_timestamp and end_timestamp indicate the time period over which this diff --git a/tensorflow/core/lib/monitoring/collection_registry.h b/tensorflow/core/lib/monitoring/collection_registry.h index 113d37e07d..2c8e250c56 100644 --- a/tensorflow/core/lib/monitoring/collection_registry.h +++ b/tensorflow/core/lib/monitoring/collection_registry.h @@ -225,6 +225,12 @@ inline void CollectValue(const string& value, Point* const point) { } template <> +inline void CollectValue(const bool& value, Point* const point) { + point->value_type = ValueType::kBool; + point->bool_value = value; +} + +template <> inline void CollectValue(const HistogramProto& value, Point* const point) { point->value_type = ValueType::kHistogram; // This is inefficient. If and when we hit snags, we can change the API to do diff --git a/tensorflow/core/lib/monitoring/gauge.h b/tensorflow/core/lib/monitoring/gauge.h index 75471cfb22..ec978a9193 100644 --- a/tensorflow/core/lib/monitoring/gauge.h +++ b/tensorflow/core/lib/monitoring/gauge.h @@ -86,8 +86,29 @@ class GaugeCell<int64> { TF_DISALLOW_COPY_AND_ASSIGN(GaugeCell); }; +// Explicit specialization of GaugeCell<bool>. Compared to the primary +// template, it uses atomic values as opposed to mutex. This class is +// thread-safe. +template <> +class GaugeCell<bool> { + public: + explicit GaugeCell(bool value) : value_(value) {} + ~GaugeCell() {} + + // Atomically sets the value. + void Set(bool value); + + // Retrieves the current value. + bool value() const; + + private: + std::atomic<bool> value_; + + TF_DISALLOW_COPY_AND_ASSIGN(GaugeCell); +}; + // A stateful class for updating a gauge-like metric. Allowed ValueType are -// int64 and string. +// int64, string and bool. // // This class encapsulates a set of values (or a single value for a label-less // metric). Each value is identified by a tuple of labels. The class allows the @@ -117,6 +138,9 @@ class Gauge { // // auto* integer_gauge = Gauge<int64, 0>::New("/tensorflow/integer_gauge", // "Integer gauge") + // + // auto* bool_gauge = Gauge<bool, 0>::New("/tensorflow/bool_gauge", + // "Bool gauge") template <typename... MetricDefArgs> static Gauge* New(MetricDefArgs&&... metric_def_args); @@ -172,12 +196,17 @@ inline void GaugeCell<int64>::Set(int64 value) { value_ = value; } inline int64 GaugeCell<int64>::value() const { return value_; } +inline void GaugeCell<bool>::Set(bool value) { value_ = value; } + +inline bool GaugeCell<bool>::value() const { return value_; } + template <typename ValueType, int NumLabels> template <typename... MetricDefArgs> Gauge<ValueType, NumLabels>* Gauge<ValueType, NumLabels>::New( MetricDefArgs&&... metric_def_args) { static_assert(std::is_same<ValueType, int64>::value || - std::is_same<ValueType, string>::value, + std::is_same<ValueType, string>::value || + std::is_same<ValueType, bool>::value, "Gauge only allows int64 and string types."); return new Gauge<ValueType, NumLabels>( MetricDef<MetricKind::kGauge, ValueType, NumLabels>( diff --git a/tensorflow/core/lib/monitoring/gauge_test.cc b/tensorflow/core/lib/monitoring/gauge_test.cc index f98cfe2a3b..c8f673db38 100644 --- a/tensorflow/core/lib/monitoring/gauge_test.cc +++ b/tensorflow/core/lib/monitoring/gauge_test.cc @@ -87,6 +87,28 @@ TEST(GaugeOfStringValue, GetCell) { EXPECT_EQ("bar", same_cell->value()); } +auto* bool_gauge = + Gauge<bool, 0>::New("/tensorflow/test/bool_gauge", "Gauge of bool value."); + +TEST(GaugeOfBoolValue, InitializedWithFalseValue) { + EXPECT_EQ(false, bool_gauge->GetCell()->value()); +} + +TEST(GaugeOfBoolValue, GetCell) { + auto* cell = bool_gauge->GetCell(); + EXPECT_EQ(false, cell->value()); + + cell->Set(true); + EXPECT_EQ(true, cell->value()); + + auto* same_cell = bool_gauge->GetCell(); + EXPECT_EQ(true, cell->value()); + + same_cell->Set(false); + EXPECT_EQ(false, cell->value()); + EXPECT_EQ(false, same_cell->value()); +} + } // namespace } // namespace monitoring } // namespace tensorflow diff --git a/tensorflow/core/lib/monitoring/metric_def.h b/tensorflow/core/lib/monitoring/metric_def.h index a7f14f9c94..f046842618 100644 --- a/tensorflow/core/lib/monitoring/metric_def.h +++ b/tensorflow/core/lib/monitoring/metric_def.h @@ -28,16 +28,16 @@ namespace monitoring { // The different metric kinds available. // // Gauge indicates that the metric's values are instantaneous measurements of a -// (typically) continuously varying quantity or a string value. Examples: a -// process's current heap size, a queue's current length, the name of the binary -// used by a process. +// (typically) continuously varying value. Examples: a process's current heap +// size, a queue's current length, the name of the binary used by a process, +// whether a task is complete. // // Cumulative indicates that the metric's values represent non-negative changes // over specified time periods. Example: the number of rpc calls to a service. enum class MetricKind : int { kGauge = 0, kCumulative }; // The type of the metric values. -enum class ValueType : int { kInt64 = 0, kHistogram, kString }; +enum class ValueType : int { kInt64 = 0, kHistogram, kString, kBool }; // Everything in the internal namespace is implementation details. Do not depend // on this. @@ -61,6 +61,11 @@ inline ValueType GetValueType<string>() { return ValueType::kString; } +template <> +inline ValueType GetValueType<bool>() { + return ValueType::kBool; +} + } // namespace internal // Abstract base class for a metric definition. |