xref: /aosp_15_r20/external/tensorflow/tensorflow/core/profiler/lib/profiler_session.cc (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 
16 #include "tensorflow/core/profiler/lib/profiler_session.h"
17 
18 #include <memory>
19 #include <utility>
20 
21 #include "absl/memory/memory.h"
22 #include "tensorflow/core/platform/errors.h"
23 #include "tensorflow/core/platform/logging.h"
24 #include "tensorflow/core/platform/mutex.h"
25 #include "tensorflow/core/platform/platform.h"
26 #include "tensorflow/core/platform/status.h"
27 #include "tensorflow/core/platform/types.h"
28 #include "tensorflow/core/profiler/profiler_options.pb.h"
29 #include "tensorflow/core/profiler/protobuf/xplane.pb.h"
30 
31 #if !defined(IS_MOBILE_PLATFORM)
32 #include "tensorflow/core/platform/host_info.h"
33 #include "tensorflow/core/profiler/convert/post_process_single_host_xplane.h"
34 #include "tensorflow/core/profiler/lib/profiler_collection.h"
35 #include "tensorflow/core/profiler/lib/profiler_factory.h"
36 #include "tensorflow/core/profiler/lib/profiler_interface.h"
37 #include "tensorflow/core/profiler/lib/profiler_lock.h"
38 #include "tensorflow/core/profiler/utils/time_utils.h"
39 #endif
40 
41 namespace tensorflow {
42 namespace {
43 
GetOptions(const ProfileOptions & opts)44 ProfileOptions GetOptions(const ProfileOptions& opts) {
45   if (opts.version()) return opts;
46   ProfileOptions options = ProfilerSession::DefaultOptions();
47   options.set_include_dataset_ops(opts.include_dataset_ops());
48   return options;
49 }
50 
51 };  // namespace
52 
Create(const ProfileOptions & options)53 /*static*/ std::unique_ptr<ProfilerSession> ProfilerSession::Create(
54     const ProfileOptions& options) {
55   return absl::WrapUnique(new ProfilerSession(options));
56 }
57 
Status()58 tensorflow::Status ProfilerSession::Status() {
59   mutex_lock l(mutex_);
60   return status_;
61 }
62 
63 #if !defined(IS_MOBILE_PLATFORM)
CollectDataInternal(profiler::XSpace * space)64 Status ProfilerSession::CollectDataInternal(profiler::XSpace* space) {
65   mutex_lock l(mutex_);
66   TF_RETURN_IF_ERROR(status_);
67   LOG(INFO) << "Profiler session collecting data.";
68   if (profilers_ != nullptr) {
69     profilers_->Stop().IgnoreError();
70     profilers_->CollectData(space).IgnoreError();
71     profilers_.reset();  // data has been collected.
72   }
73   // Allow another session to start.
74   profiler_lock_.ReleaseIfActive();
75   return OkStatus();
76 }
77 #endif
78 
CollectData(profiler::XSpace * space)79 Status ProfilerSession::CollectData(profiler::XSpace* space) {
80 #if !defined(IS_MOBILE_PLATFORM)
81   space->add_hostnames(port::Hostname());
82   TF_RETURN_IF_ERROR(CollectDataInternal(space));
83   PostProcessSingleHostXSpace(space, start_time_ns_);
84 #endif
85   return OkStatus();
86 }
87 
ProfilerSession(const ProfileOptions & options)88 ProfilerSession::ProfilerSession(const ProfileOptions& options)
89 #if defined(IS_MOBILE_PLATFORM)
90     : status_(errors::Unimplemented(
91           "Profiler is unimplemented for mobile platforms.")) {
92 #else
93     : options_(GetOptions(options)) {
94   auto profiler_lock = profiler::ProfilerLock::Acquire();
95   if (!profiler_lock.ok()) {
96     status_ = profiler_lock.status();
97     return;
98   }
99   profiler_lock_ = *std::move(profiler_lock);
100 
101   LOG(INFO) << "Profiler session initializing.";
102   // Sleep until it is time to start profiling.
103   if (options_.start_timestamp_ns() > 0) {
104     int64_t sleep_duration_ns =
105         options_.start_timestamp_ns() - profiler::GetCurrentTimeNanos();
106     if (sleep_duration_ns < 0) {
107       LOG(WARNING) << "Profiling is late by " << -sleep_duration_ns
108                    << " nanoseconds and will start immediately.";
109     } else {
110       LOG(INFO) << "Delaying start of profiler session by "
111                 << sleep_duration_ns;
112       profiler::SleepForNanos(sleep_duration_ns);
113     }
114   }
115 
116   LOG(INFO) << "Profiler session started.";
117   start_time_ns_ = profiler::GetCurrentTimeNanos();
118 
119   DCHECK(profiler_lock_.Active());
120   profilers_ = absl::make_unique<profiler::ProfilerCollection>(
121       profiler::CreateProfilers(options_));
122   profilers_->Start().IgnoreError();
123 #endif
124 }
125 
126 ProfilerSession::~ProfilerSession() {
127 #if !defined(IS_MOBILE_PLATFORM)
128   LOG(INFO) << "Profiler session tear down.";
129 #endif
130 }
131 
132 }  // namespace tensorflow
133