• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2019 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 #include "tensorflow/core/profiler/lib/profiler_factory.h"
16 
17 #include <memory>
18 #include <utility>
19 #include <vector>
20 
21 #include "tensorflow/core/platform/mutex.h"
22 #include "tensorflow/core/platform/thread_annotations.h"
23 #include "tensorflow/core/profiler/lib/profiler_controller.h"
24 #include "tensorflow/core/profiler/lib/profiler_interface.h"
25 #include "tensorflow/core/profiler/profiler_options.pb.h"
26 
27 namespace tensorflow {
28 namespace profiler {
29 namespace {
30 
31 mutex mu(LINKER_INITIALIZED);
32 
GetFactories()33 std::vector<ProfilerFactory>* GetFactories() TF_EXCLUSIVE_LOCKS_REQUIRED(mu) {
34   static auto factories = new std::vector<ProfilerFactory>();
35   return factories;
36 }
37 
38 }  // namespace
39 
RegisterProfilerFactory(ProfilerFactory factory)40 void RegisterProfilerFactory(ProfilerFactory factory) {
41   mutex_lock lock(mu);
42   GetFactories()->push_back(std::move(factory));
43 }
44 
CreateProfilers(const ProfileOptions & options)45 std::vector<std::unique_ptr<profiler::ProfilerInterface>> CreateProfilers(
46     const ProfileOptions& options) {
47   std::vector<std::unique_ptr<profiler::ProfilerInterface>> result;
48   mutex_lock lock(mu);
49   for (const auto& factory : *GetFactories()) {
50     auto profiler = factory(options);
51     // A factory might return nullptr based on options.
52     if (profiler == nullptr) continue;
53     result.emplace_back(
54         absl::make_unique<ProfilerController>(std::move(profiler)));
55   }
56   return result;
57 }
58 
ClearRegisteredProfilersForTest()59 void ClearRegisteredProfilersForTest() {
60   mutex_lock lock(mu);
61   GetFactories()->clear();
62 }
63 
64 }  // namespace profiler
65 }  // namespace tensorflow
66