• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2018 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_PROFILER_LIB_PROFILER_SESSION_H_
16 #define TENSORFLOW_CORE_PROFILER_LIB_PROFILER_SESSION_H_
17 
18 #include <memory>
19 #include <vector>
20 
21 #include "tensorflow/core/lib/core/status.h"
22 #include "tensorflow/core/platform/mutex.h"
23 #include "tensorflow/core/platform/thread_annotations.h"
24 #include "tensorflow/core/profiler/internal/profiler_interface.h"
25 #include "tensorflow/core/profiler/protobuf/xplane.pb.h"
26 #include "tensorflow/core/protobuf/config.pb.h"
27 
28 namespace tensorflow {
29 
30 // A profiler which will start profiling when creating the object and will stop
31 // when either the object is destroyed or SerializedToString is called. It will
32 // profile all operations run under the given EagerContext.
33 // Multiple instances of it can be created, but at most one of them will profile
34 // for each EagerContext. Status() will return OK only for the instance that is
35 // profiling.
36 // Thread-safety: ProfilerSession is thread-safe.
37 class ProfilerSession {
38  public:
39   // Creates and ProfilerSession and starts profiling.
40   static std::unique_ptr<ProfilerSession> Create(
41       const profiler::ProfilerOptions& options);
42   static std::unique_ptr<ProfilerSession> Create();
43 
44   // Deletes an exsiting Profiler and enables starting a new one.
45   ~ProfilerSession();
46 
47   tensorflow::Status Status() LOCKS_EXCLUDED(mutex_);
48 
49   tensorflow::Status CollectData(profiler::XSpace* space)
50       LOCKS_EXCLUDED(mutex_);
51 
52   tensorflow::Status CollectData(RunMetadata* run_metadata)
53       LOCKS_EXCLUDED(mutex_);
54 
55   tensorflow::Status SerializeToString(string* content) LOCKS_EXCLUDED(mutex_);
56 
57  private:
58   // Constructs an instance of the class and starts profiling
59   explicit ProfilerSession(const profiler::ProfilerOptions& options);
60 
61   // ProfilerSession is neither copyable or movable.
62   ProfilerSession(const ProfilerSession&) = delete;
63   ProfilerSession& operator=(const ProfilerSession&) = delete;
64 
65   std::vector<std::unique_ptr<profiler::ProfilerInterface>> profilers_
66       GUARDED_BY(mutex_);
67 
68   // True if the session is active.
69   bool active_ GUARDED_BY(mutex_);
70 
71   tensorflow::Status status_ GUARDED_BY(mutex_);
72   const uint64 start_time_ns_;
73   mutex mutex_;
74 };
75 
76 }  // namespace tensorflow
77 #endif  // TENSORFLOW_CORE_PROFILER_LIB_PROFILER_SESSION_H_
78