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