1 /* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
2
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15
16 #ifndef TENSORFLOW_CORE_LIB_MONITORING_GAUGE_H_
17 #define TENSORFLOW_CORE_LIB_MONITORING_GAUGE_H_
18
19 // clang-format off
20 // Required for IS_MOBILE_PLATFORM
21 #include "tensorflow/core/platform/platform.h"
22 // clang-format on
23
24 // We replace this implementation with a null implementation for mobile
25 // platforms.
26 #ifdef IS_MOBILE_PLATFORM
27 #define TENSORFLOW_INCLUDED_FROM_GAUGE_H // prevent accidental use of
28 // mobile_gauge.h
29 #include "tensorflow/core/lib/monitoring/mobile_gauge.h"
30 #undef TENSORFLOW_INCLUDED_FROM_GAUGE_H
31 #else
32
33 #include <array>
34 #include <atomic>
35 #include <map>
36
37 #include "tensorflow/core/lib/core/status.h"
38 #include "tensorflow/core/lib/monitoring/collection_registry.h"
39 #include "tensorflow/core/lib/monitoring/metric_def.h"
40 #include "tensorflow/core/platform/macros.h"
41 #include "tensorflow/core/platform/mutex.h"
42 #include "tensorflow/core/platform/thread_annotations.h"
43 #include "tensorflow/core/platform/types.h"
44
45 namespace tensorflow {
46 namespace monitoring {
47
48 // GaugeCell stores each value of a gauge.
49 //
50 // A cell can be passed off to a module which may repeatedly update it without
51 // needing further map-indexing computations. This improves both encapsulation
52 // (separate modules can own a cell each, without needing to know about the map
53 // to which both cells belong) and performance (since map indexing and
54 // associated locking are both avoided).
55 //
56 // This class is thread-safe.
57 template <typename T>
58 class GaugeCell {
59 public:
GaugeCell(const T & value)60 explicit GaugeCell(const T& value) : value_(value) {}
~GaugeCell()61 ~GaugeCell() {}
62
63 // Atomically sets the value.
64 void Set(const T& value) TF_LOCKS_EXCLUDED(mu_);
65
66 // Retrieves the current value.
67 T value() const TF_LOCKS_EXCLUDED(mu_);
68
69 private:
70 T value_ TF_GUARDED_BY(mu_);
71 mutable mutex mu_;
72
73 TF_DISALLOW_COPY_AND_ASSIGN(GaugeCell);
74 };
75
76 // Explicit specialization of GaugeCell<int64>. Compared to the primary
77 // template, it uses atomic values as opposed to mutex. This class is
78 // thread-safe.
79 template <>
80 class GaugeCell<int64> {
81 public:
GaugeCell(int64 value)82 explicit GaugeCell(int64 value) : value_(value) {}
~GaugeCell()83 ~GaugeCell() {}
84
85 // Atomically sets the value.
86 void Set(int64 value);
87
88 // Retrieves the current value.
89 int64 value() const;
90
91 private:
92 std::atomic<int64> value_;
93
94 TF_DISALLOW_COPY_AND_ASSIGN(GaugeCell);
95 };
96
97 // Explicit specialization of GaugeCell<bool>. Compared to the primary
98 // template, it uses atomic values as opposed to mutex. This class is
99 // thread-safe.
100 template <>
101 class GaugeCell<bool> {
102 public:
GaugeCell(bool value)103 explicit GaugeCell(bool value) : value_(value) {}
~GaugeCell()104 ~GaugeCell() {}
105
106 // Atomically sets the value.
107 void Set(bool value);
108
109 // Retrieves the current value.
110 bool value() const;
111
112 private:
113 std::atomic<bool> value_;
114
115 TF_DISALLOW_COPY_AND_ASSIGN(GaugeCell);
116 };
117
118 // A stateful class for updating a gauge-like metric. Allowed ValueType are
119 // int64, string and bool.
120 //
121 // This class encapsulates a set of values (or a single value for a label-less
122 // metric). Each value is identified by a tuple of labels. The class allows the
123 // user to set each value.
124 //
125 // Gauge allocates storage and maintains a cell for each value. You can
126 // retrieve an individual cell using a label-tuple and update it separately.
127 // This improves performance since operations related to retrieval, like
128 // map-indexing and locking, are avoided.
129 //
130 // This class is thread-safe.
131 template <typename ValueType, int NumLabels>
132 class Gauge {
133 public:
~Gauge()134 ~Gauge() {
135 // Deleted here, before the metric_def is destroyed.
136 registration_handle_.reset();
137 }
138
139 // Creates the metric based on the metric-definition arguments.
140 //
141 // Example:
142 //
143 // auto* string_gauge_with_label = Gauge<string,1>::New(
144 // "/tensorflow/string_gauge_with_label",
145 // "String gauge with one label.", "MyLabelName");
146 //
147 // auto* integer_gauge = Gauge<int64, 0>::New("/tensorflow/integer_gauge",
148 // "Integer gauge")
149 //
150 // auto* bool_gauge = Gauge<bool, 0>::New("/tensorflow/bool_gauge",
151 // "Bool gauge")
152 template <typename... MetricDefArgs>
153 static Gauge* New(MetricDefArgs&&... metric_def_args);
154
155 // Retrieves the cell for the specified labels, creating it on demand if not
156 // already present.
157 template <typename... Labels>
158 GaugeCell<ValueType>* GetCell(const Labels&... labels) TF_LOCKS_EXCLUDED(mu_);
159
GetStatus()160 Status GetStatus() { return status_; }
161
162 private:
Gauge(const MetricDef<MetricKind::kGauge,ValueType,NumLabels> & metric_def)163 explicit Gauge(
164 const MetricDef<MetricKind::kGauge, ValueType, NumLabels>& metric_def)
165 : metric_def_(metric_def),
166 registration_handle_(CollectionRegistry::Default()->Register(
167 &metric_def_, [&](MetricCollectorGetter getter) {
168 auto metric_collector = getter.Get(&metric_def_);
169
170 mutex_lock l(mu_);
171 for (const auto& cell : cells_) {
172 metric_collector.CollectValue(cell.first, cell.second.value());
173 }
174 })) {
175 if (registration_handle_) {
176 status_ = Status::OK();
177 } else {
178 status_ = Status(tensorflow::error::Code::ALREADY_EXISTS,
179 "Another metric with the same name already exists.");
180 }
181 }
182
183 mutable mutex mu_;
184
185 Status status_;
186
187 // The metric definition. This will be used to identify the metric when we
188 // register it for collection.
189 const MetricDef<MetricKind::kGauge, ValueType, NumLabels> metric_def_;
190
191 std::unique_ptr<CollectionRegistry::RegistrationHandle> registration_handle_;
192
193 using LabelArray = std::array<string, NumLabels>;
194 std::map<LabelArray, GaugeCell<ValueType> > cells_ TF_GUARDED_BY(mu_);
195
196 TF_DISALLOW_COPY_AND_ASSIGN(Gauge);
197 };
198
199 ////
200 // Implementation details follow. API readers may skip.
201 ////
202 template <typename T>
Set(const T & value)203 void GaugeCell<T>::Set(const T& value) {
204 mutex_lock l(mu_);
205 value_ = value;
206 }
207
208 template <typename T>
value()209 T GaugeCell<T>::value() const {
210 mutex_lock l(mu_);
211 return value_;
212 }
213
Set(int64 value)214 inline void GaugeCell<int64>::Set(int64 value) { value_ = value; }
215
value()216 inline int64 GaugeCell<int64>::value() const { return value_; }
217
Set(bool value)218 inline void GaugeCell<bool>::Set(bool value) { value_ = value; }
219
value()220 inline bool GaugeCell<bool>::value() const { return value_; }
221
222 template <typename ValueType, int NumLabels>
223 template <typename... MetricDefArgs>
New(MetricDefArgs &&...metric_def_args)224 Gauge<ValueType, NumLabels>* Gauge<ValueType, NumLabels>::New(
225 MetricDefArgs&&... metric_def_args) {
226 static_assert(std::is_same<ValueType, int64>::value ||
227 std::is_same<ValueType, string>::value ||
228 std::is_same<ValueType, bool>::value,
229 "Gauge only allows bool, int64, and string types.");
230 return new Gauge<ValueType, NumLabels>(
231 MetricDef<MetricKind::kGauge, ValueType, NumLabels>(
232 std::forward<MetricDefArgs>(metric_def_args)...));
233 }
234
235 template <typename ValueType, int NumLabels>
236 template <typename... Labels>
GetCell(const Labels &...labels)237 GaugeCell<ValueType>* Gauge<ValueType, NumLabels>::GetCell(
238 const Labels&... labels) TF_LOCKS_EXCLUDED(mu_) {
239 // Provides a more informative error message than the one during array
240 // construction below.
241 static_assert(
242 sizeof...(Labels) == NumLabels,
243 "Mismatch between Gauge<ValueType, NumLabels> and number of labels "
244 "provided in GetCell(...).");
245
246 const LabelArray& label_array = {{labels...}};
247 mutex_lock l(mu_);
248 const auto found_it = cells_.find(label_array);
249 if (found_it != cells_.end()) {
250 return &(found_it->second);
251 }
252 return &(cells_
253 .emplace(std::piecewise_construct,
254 std::forward_as_tuple(label_array),
255 std::forward_as_tuple(ValueType()))
256 .first->second);
257 }
258
259 } // namespace monitoring
260 } // namespace tensorflow
261
262 #endif // IS_MOBILE_PLATFORM
263 #endif // TENSORFLOW_CORE_LIB_MONITORING_GAUGE_H_
264