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