• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "tensorflow/core/lib/core/errors.h"
19 #include "tensorflow/core/platform/env_time.h"
20 #include "tensorflow/core/platform/mutex.h"
21 #include "tensorflow/core/platform/platform.h"
22 #include "tensorflow/core/platform/types.h"
23 #include "tensorflow/core/protobuf/config.pb.h"
24 #include "tensorflow/core/protobuf/error_codes.pb.h"
25 #include "tensorflow/core/protobuf/trace_events.pb.h"
26 #include "tensorflow/core/util/env_var.h"
27 #include "tensorflow/core/util/ptr_util.h"
28 
29 #if !defined(IS_MOBILE_PLATFORM)
30 #include "tensorflow/core/profiler/convert/xplane_to_trace_events.h"
31 #include "tensorflow/core/profiler/internal/profiler_factory.h"
32 #include "tensorflow/core/profiler/lib/profiler_utils.h"
33 #include "tensorflow/core/profiler/utils/group_events.h"
34 #endif
35 
36 namespace tensorflow {
37 
Create(const profiler::ProfilerOptions & options)38 /*static*/ std::unique_ptr<ProfilerSession> ProfilerSession::Create(
39     const profiler::ProfilerOptions& options) {
40   return WrapUnique(new ProfilerSession(options));
41 }
42 
Create()43 /*static*/ std::unique_ptr<ProfilerSession> ProfilerSession::Create() {
44   int64 host_tracer_level = 2;
45   tensorflow::Status s = ReadInt64FromEnvVar("TF_PROFILER_HOST_TRACER_LEVEL", 2,
46                                              &host_tracer_level);
47   if (!s.ok()) {
48     LOG(WARNING) << "ProfilerSession: " << s.error_message();
49   }
50   profiler::ProfilerOptions options;
51   options.host_tracer_level = host_tracer_level;
52   return Create(options);
53 }
54 
Status()55 Status ProfilerSession::Status() {
56   mutex_lock l(mutex_);
57   return status_;
58 }
59 
CollectData(profiler::XSpace * space)60 Status ProfilerSession::CollectData(profiler::XSpace* space) {
61   mutex_lock l(mutex_);
62   if (!status_.ok()) return status_;
63   for (auto& profiler : profilers_) {
64     profiler->Stop().IgnoreError();
65   }
66 
67   for (auto& profiler : profilers_) {
68     profiler->CollectData(space).IgnoreError();
69   }
70 
71   if (active_) {
72     // Allow another session to start.
73 #if !defined(IS_MOBILE_PLATFORM)
74     profiler::ReleaseProfilerLock();
75 #endif
76     active_ = false;
77   }
78 
79   return Status::OK();
80 }
81 
CollectData(RunMetadata * run_metadata)82 Status ProfilerSession::CollectData(RunMetadata* run_metadata) {
83   mutex_lock l(mutex_);
84   if (!status_.ok()) return status_;
85   for (auto& profiler : profilers_) {
86     profiler->Stop().IgnoreError();
87   }
88 
89   for (auto& profiler : profilers_) {
90     profiler->CollectData(run_metadata).IgnoreError();
91   }
92 
93   if (active_) {
94     // Allow another session to start.
95 #if !defined(IS_MOBILE_PLATFORM)
96     profiler::ReleaseProfilerLock();
97 #endif
98     active_ = false;
99   }
100 
101   return Status::OK();
102 }
103 
SerializeToString(string * content)104 Status ProfilerSession::SerializeToString(string* content) {
105   profiler::XSpace xspace;
106   TF_RETURN_IF_ERROR(CollectData(&xspace));
107   profiler::Trace trace;
108 #if !defined(IS_MOBILE_PLATFORM)
109   uint64 end_time_ns = EnvTime::NowNanos();
110   profiler::GroupTfEvents(&xspace, /*event_group_name_map=*/nullptr);
111   profiler::ConvertXSpaceToTraceEvents(start_time_ns_, end_time_ns, xspace,
112                                        &trace);
113 #endif
114   trace.SerializeToString(content);
115   return Status::OK();
116 }
117 
ProfilerSession(const profiler::ProfilerOptions & options)118 ProfilerSession::ProfilerSession(const profiler::ProfilerOptions& options)
119 #if !defined(IS_MOBILE_PLATFORM)
120     : active_(profiler::AcquireProfilerLock()),
121 #else
122     : active_(false),
123 #endif
124       start_time_ns_(EnvTime::NowNanos()) {
125   if (!active_) {
126 #if !defined(IS_MOBILE_PLATFORM)
127     status_ = tensorflow::Status(error::UNAVAILABLE,
128                                  "Another profiler session is active.");
129 #else
130     status_ =
131         tensorflow::Status(error::UNIMPLEMENTED,
132                            "Profiler is unimplemented for mobile platforms.");
133 #endif
134     return;
135   }
136 
137   LOG(INFO) << "Profiler session started.";
138 
139 #if !defined(IS_MOBILE_PLATFORM)
140   CreateProfilers(options, &profilers_);
141 #endif
142   status_ = Status::OK();
143 
144   for (auto& profiler : profilers_) {
145     auto start_status = profiler->Start();
146     if (!start_status.ok()) {
147       LOG(WARNING) << "Encountered error while starting profiler: "
148                    << start_status.ToString();
149     }
150   }
151 }
152 
~ProfilerSession()153 ProfilerSession::~ProfilerSession() {
154   for (auto& profiler : profilers_) {
155     profiler->Stop().IgnoreError();
156   }
157 
158   if (active_) {
159     // Allow another session to start.
160 #if !defined(IS_MOBILE_PLATFORM)
161     profiler::ReleaseProfilerLock();
162 #endif
163   }
164 }
165 }  // namespace tensorflow
166