• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2016 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 #include "tensorflow/core/profiler/internal/runtime/eager_profiler.h"
16 
17 namespace tensorflow {
18 namespace profiler {
19 namespace runtime {
20 
TraceCollector(EagerContext * const eager_context)21 TraceCollector::TraceCollector(EagerContext* const eager_context)
22     : context_(eager_context) {}
23 
BeforeClearRunMetadata()24 void TraceCollector::BeforeClearRunMetadata() {
25   run_metadata_.MergeFrom(*context_->RunMetadataProto());
26 }
27 
CollectData(RunMetadata * run_metadata)28 Status TraceCollector::CollectData(RunMetadata* run_metadata) {
29   run_metadata->MergeFrom(run_metadata_);
30   return Status::OK();
31 }
32 
Create(EagerContext * const eager_context)33 /* static */ std::unique_ptr<ProfilerInterface> EagerProfiler::Create(
34     EagerContext* const eager_context) {
35   return absl::WrapUnique(new EagerProfiler(eager_context));
36 }
37 
Start()38 Status EagerProfiler::Start() {
39   if (context_ == nullptr) {
40     return Status(tensorflow::error::Code::FAILED_PRECONDITION,
41                   "No eager context attached.");
42   }
43   return context_->RegisterRunMetadataListener(&collector_);
44 }
45 
Stop()46 Status EagerProfiler::Stop() {
47   collector_.BeforeClearRunMetadata();
48   context_->ClearRunMetadataListener();
49   return Status::OK();
50 }
51 
CollectData(RunMetadata * run_metadata)52 Status EagerProfiler::CollectData(RunMetadata* run_metadata) {
53   return collector_.CollectData(run_metadata);
54 }
55 
EagerProfiler(EagerContext * const eager_context)56 EagerProfiler::EagerProfiler(EagerContext* const eager_context)
57     : context_(eager_context), collector_(eager_context) {}
58 
59 }  // namespace runtime
60 }  // namespace profiler
61 }  // namespace tensorflow
62