1 /* Copyright 2021 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_DEVICE_PROFILER_SESSION_H_ 16 #define TENSORFLOW_CORE_PROFILER_LIB_DEVICE_PROFILER_SESSION_H_ 17 18 #include "tensorflow/core/framework/step_stats.pb.h" 19 #include "tensorflow/core/platform/errors.h" 20 #include "tensorflow/core/platform/platform.h" 21 #include "tensorflow/core/platform/status.h" 22 23 #if !defined(IS_MOBILE_PLATFORM) 24 #include "tensorflow/core/profiler/convert/xplane_to_step_stats.h" 25 #include "tensorflow/core/profiler/lib/profiler_session.h" 26 #include "tensorflow/core/profiler/protobuf/xplane.pb.h" 27 #endif 28 #include "tensorflow/core/profiler/profiler_options.pb.h" 29 30 namespace tensorflow { 31 32 // Wraps a ProfilerSession configured to collect only device traces. 33 // Returns data in StepStats format. 34 class DeviceProfilerSession { 35 public: 36 // Creates a DeviceProfilerSession and starts tracing. 37 // Traces GPU devices if present. 38 // Does not trace TPU devices (not supported). Create()39 static std::unique_ptr<DeviceProfilerSession> Create() { 40 #if !defined(IS_MOBILE_PLATFORM) 41 ProfileOptions options = ProfilerSession::DefaultOptions(); 42 options.set_host_tracer_level(0); 43 options.set_device_type(ProfileOptions::GPU); 44 return absl::WrapUnique(new DeviceProfilerSession(options)); 45 #else 46 return nullptr; 47 #endif 48 } 49 50 // Stops tracing and converts the data to StepStats format. 51 // Should be called at most once. CollectData(StepStats * step_stats)52 Status CollectData(StepStats* step_stats) { 53 #if defined(IS_MOBILE_PLATFORM) 54 return errors::Unimplemented("Profiling not supported on mobile platform."); 55 #else 56 profiler::XSpace space; 57 TF_RETURN_IF_ERROR(profiler_session_.CollectDataInternal(&space)); 58 profiler::ConvertGpuXSpaceToStepStats(space, step_stats); 59 return OkStatus(); 60 #endif 61 } 62 63 private: 64 // Constructs an instance of the class and starts profiling DeviceProfilerSession(const ProfileOptions & options)65 explicit DeviceProfilerSession(const ProfileOptions& options) 66 #if !defined(IS_MOBILE_PLATFORM) 67 : profiler_session_(options) 68 #endif 69 { 70 } 71 72 // DeviceProfilerSession is neither copyable or movable. 73 DeviceProfilerSession(const DeviceProfilerSession&) = delete; 74 DeviceProfilerSession& operator=(const DeviceProfilerSession&) = delete; 75 76 #if !defined(IS_MOBILE_PLATFORM) 77 ProfilerSession profiler_session_; 78 #endif 79 }; 80 81 } // namespace tensorflow 82 #endif // TENSORFLOW_CORE_PROFILER_LIB_DEVICE_PROFILER_SESSION_H_ 83