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