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/platform/mutex.h" 22 #include "tensorflow/core/platform/status.h" 23 #include "tensorflow/core/platform/thread_annotations.h" 24 #include "tensorflow/core/platform/types.h" 25 #include "tensorflow/core/profiler/lib/profiler_interface.h" 26 #include "tensorflow/core/profiler/profiler_options.pb.h" 27 #include "tensorflow/core/profiler/protobuf/xplane.pb.h" 28 #include "tensorflow/core/protobuf/config.pb.h" 29 30 namespace tensorflow { 31 32 // A profiler which will start profiling when creating the object and will stop 33 // when either the object is destroyed or SerializedToString is called. It will 34 // profile all operations run under the given EagerContext. 35 // Multiple instances of it can be created, but at most one of them will profile 36 // for each EagerContext. Status() will return OK only for the instance that is 37 // profiling. 38 // Thread-safety: ProfilerSession is thread-safe. 39 class ProfilerSession { 40 public: 41 // Creates a ProfilerSession and starts profiling. 42 static std::unique_ptr<ProfilerSession> Create(const ProfileOptions& options); 43 DefaultOptions()44 static ProfileOptions DefaultOptions() { 45 ProfileOptions options; 46 options.set_version(1); 47 options.set_device_tracer_level(1); 48 options.set_host_tracer_level(2); 49 options.set_device_type(ProfileOptions::UNSPECIFIED); 50 options.set_python_tracer_level(0); 51 options.set_enable_hlo_proto(false); 52 options.set_include_dataset_ops(true); 53 return options; 54 } 55 56 // Deletes an existing Profiler and enables starting a new one. 57 ~ProfilerSession(); 58 59 tensorflow::Status Status() TF_LOCKS_EXCLUDED(mutex_); 60 61 tensorflow::Status CollectData(profiler::XSpace* space) 62 TF_LOCKS_EXCLUDED(mutex_); 63 64 tensorflow::Status CollectData(RunMetadata* run_metadata) 65 TF_LOCKS_EXCLUDED(mutex_); 66 67 private: 68 // Constructs an instance of the class and starts profiling 69 explicit ProfilerSession(ProfileOptions options); 70 71 // ProfilerSession is neither copyable or movable. 72 ProfilerSession(const ProfilerSession&) = delete; 73 ProfilerSession& operator=(const ProfilerSession&) = delete; 74 75 std::vector<std::unique_ptr<profiler::ProfilerInterface>> profilers_ 76 TF_GUARDED_BY(mutex_); 77 78 // True if the session is active. 79 bool active_ TF_GUARDED_BY(mutex_); 80 81 tensorflow::Status status_ TF_GUARDED_BY(mutex_); 82 uint64 start_time_ns_; 83 mutex mutex_; 84 ProfileOptions options_; 85 }; 86 87 } // namespace tensorflow 88 #endif // TENSORFLOW_CORE_PROFILER_LIB_PROFILER_SESSION_H_ 89