1 /* Copyright 2016 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 #include "tensorflow/core/lib/monitoring/collection_registry.h"
17
18 #include "tensorflow/core/platform/logging.h"
19
20 namespace tensorflow {
21 namespace monitoring {
22 namespace internal {
23
CollectMetricValues(const CollectionRegistry::CollectionInfo & info)24 void Collector::CollectMetricValues(
25 const CollectionRegistry::CollectionInfo& info) {
26 info.collection_function(MetricCollectorGetter(
27 this, info.metric_def, info.registration_time_millis));
28 }
29
ConsumeCollectedMetrics()30 std::unique_ptr<CollectedMetrics> Collector::ConsumeCollectedMetrics() {
31 mutex_lock l(mu_);
32 return std::move(collected_metrics_);
33 }
34
CollectMetricDescriptor(const AbstractMetricDef * const metric_def)35 void Collector::CollectMetricDescriptor(
36 const AbstractMetricDef* const metric_def) {
37 auto* const metric_descriptor = [&]() {
38 mutex_lock l(mu_);
39 return collected_metrics_->metric_descriptor_map
40 .insert(std::make_pair(
41 string(metric_def->name()),
42 std::unique_ptr<MetricDescriptor>(new MetricDescriptor())))
43 .first->second.get();
44 }();
45 metric_descriptor->name = string(metric_def->name());
46 metric_descriptor->description = string(metric_def->description());
47
48 for (const StringPiece label_name : metric_def->label_descriptions()) {
49 metric_descriptor->label_names.emplace_back(label_name);
50 }
51
52 metric_descriptor->metric_kind = metric_def->kind();
53 metric_descriptor->value_type = metric_def->value_type();
54 }
55
56 } // namespace internal
57
58 // static
Default()59 CollectionRegistry* CollectionRegistry::Default() {
60 static CollectionRegistry* default_registry =
61 new CollectionRegistry(Env::Default());
62 return default_registry;
63 }
64
CollectionRegistry(Env * const env)65 CollectionRegistry::CollectionRegistry(Env* const env) : env_(env) {}
66
67 std::unique_ptr<CollectionRegistry::RegistrationHandle>
Register(const AbstractMetricDef * const metric_def,const CollectionFunction & collection_function)68 CollectionRegistry::Register(const AbstractMetricDef* const metric_def,
69 const CollectionFunction& collection_function) {
70 CHECK(collection_function)
71 << "Requires collection_function to contain an implementation.";
72
73 mutex_lock l(mu_);
74
75 const auto found_it = registry_.find(metric_def->name());
76 if (found_it != registry_.end()) {
77 LOG(FATAL) << "Cannot register 2 metrics with the same name: "
78 << metric_def->name();
79 }
80 registry_.insert(
81 {metric_def->name(),
82 {metric_def, collection_function, env_->NowMicros() / 1000}});
83
84 return std::unique_ptr<RegistrationHandle>(
85 new RegistrationHandle(this, metric_def));
86 }
87
Unregister(const AbstractMetricDef * const metric_def)88 void CollectionRegistry::Unregister(const AbstractMetricDef* const metric_def) {
89 mutex_lock l(mu_);
90 registry_.erase(metric_def->name());
91 }
92
CollectMetrics(const CollectMetricsOptions & options) const93 std::unique_ptr<CollectedMetrics> CollectionRegistry::CollectMetrics(
94 const CollectMetricsOptions& options) const {
95 internal::Collector collector(env_->NowMicros() / 1000);
96
97 mutex_lock l(mu_);
98 for (const auto& registration : registry_) {
99 if (options.collect_metric_descriptors) {
100 collector.CollectMetricDescriptor(registration.second.metric_def);
101 }
102
103 collector.CollectMetricValues(registration.second /* collection_info */);
104 }
105 return collector.ConsumeCollectedMetrics();
106 }
107
108 } // namespace monitoring
109 } // namespace tensorflow
110