xref: /aosp_15_r20/external/tensorflow/tensorflow/core/lib/monitoring/cell_reader-inl.h (revision b6fb3261f9314811a0f4371741dbb8839866f948)
1 /* Copyright 2022 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 #ifndef TENSORFLOW_CORE_LIB_MONITORING_CELL_READER_INL_H_
16 #define TENSORFLOW_CORE_LIB_MONITORING_CELL_READER_INL_H_
17 
18 #include <cstdint>
19 #include <memory>
20 #include <string>
21 #include <utility>
22 #include <vector>
23 
24 #include "tensorflow/core/lib/monitoring/collected_metrics.h"
25 #include "tensorflow/core/lib/monitoring/metric_def.h"
26 #include "tensorflow/core/lib/monitoring/test_utils.h"
27 #include "tensorflow/core/platform/errors.h"
28 #include "tensorflow/core/platform/status.h"
29 #include "tensorflow/core/platform/statusor.h"
30 
31 namespace tensorflow {
32 namespace monitoring {
33 namespace testing {
34 namespace internal {
35 
36 // Returns a snapshot of the metrics collected at the time of calling.
37 std::unique_ptr<CollectedMetrics> CollectMetrics();
38 
39 // Returns whether this is a cumulative or gauge metric.
40 MetricKind GetMetricKind(const CollectedMetrics& metrics,
41                          const std::string& metric_name);
42 
43 // Returns the points collected for `metric_name` associated with the `labels`.
44 // A `Point` represents a data point collected for the metric. For example,
45 // suppose a counter is incremented 3 times, then its points are 1, 2, 3.
46 //
47 // If the metric does not exist, it returns a `NotFound` error. If the number of
48 // labels does not match the metric definition, it returns an `InvalidArgument`
49 // error.
50 StatusOr<std::vector<Point>> GetPoints(const CollectedMetrics& metrics,
51                                        const std::string& metric_name,
52                                        const std::vector<std::string>& labels);
53 
54 // Returns the `Point` that corresponds to the latest data point collected for
55 // `metric_name`, associated with the `labels`.
56 //
57 // If the metric does not exist, it returns a `NotFound` error. If the metric
58 // exists but no data is collected, it returns an `Unavailable` error. If the
59 // number of labels does not match the metric definition, it returns an
60 // `InvalidArgument` error.
61 StatusOr<Point> GetLatestPoint(const CollectedMetrics& metrics,
62                                const std::string& metric_name,
63                                const std::vector<std::string>& labels);
64 
65 // Returns the value of `point`. Currently, only int64_t (counter) values are
66 // supported.
67 template <typename ValueType>
GetValue(const Point & point)68 ValueType GetValue(const Point& point) {
69   LOG(FATAL) << "Invalid argument: Tensorflow CellReader does not support type "
70              << typeid(ValueType).name();
71 }
72 
73 template <>
74 int64_t GetValue(const Point& point);
75 
76 template <>
77 std::string GetValue(const Point& point);
78 
79 template <>
80 bool GetValue(const Point& point);
81 
82 template <>
83 Histogram GetValue(const Point& point);
84 
85 template <>
86 Percentiles GetValue(const Point& point);
87 
88 // Returns the latest value for `metric_name`, associated with the `labels`. If
89 // the metric has not collected any data, it returns a default value appropriate
90 // for `ValueType`. If the metric does not exist, or the wrong number of labels
91 // is provided, it will crash.
92 template <typename ValueType>
93 ValueType GetLatestValueOrDefault(const CollectedMetrics& metrics,
94                                   const std::string& metric_name,
95                                   const std::vector<std::string>& labels,
96                                   const ValueType default_value = ValueType()) {
97   StatusOr<Point> latest_point = GetLatestPoint(metrics, metric_name, labels);
98   if (errors::IsUnavailable(latest_point.status())) {
99     return std::move(default_value);
100   }
101   if (!latest_point.ok()) {
102     LOG(FATAL) << "Failed to read from tfstreamz: " << latest_point.status();
103   }
104   return GetValue<ValueType>(*latest_point);
105 }
106 
107 // Returns the difference between two values. Currently, only int64_t (counter)
108 // values are supported.
109 template <typename ValueType>
GetDelta(const ValueType & a,const ValueType & b)110 ValueType GetDelta(const ValueType& a, const ValueType& b) {
111   LOG(FATAL) << "Invalid argument: Tensorflow CellReader does not support type "
112              << typeid(ValueType).name();
113 }
114 
115 template <>
116 int64_t GetDelta(const int64_t& a, const int64_t& b);
117 
118 template <>
119 Histogram GetDelta(const Histogram& a, const Histogram& b);
120 
121 template <>
122 Percentiles GetDelta(const Percentiles& a, const Percentiles& b);
123 
124 template <>
125 std::string GetDelta(const std::string& a, const std::string& b);
126 
127 template <>
128 bool GetDelta(const bool& a, const bool& b);
129 
130 }  // namespace internal
131 }  // namespace testing
132 }  // namespace monitoring
133 }  // namespace tensorflow
134 
135 #endif  // TENSORFLOW_CORE_LIB_MONITORING_CELL_READER_INL_H_
136