• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2015 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/framework/log_memory.h"
17 
18 #include "tensorflow/core/framework/log_memory.pb_text.h"
19 #include "tensorflow/core/framework/log_memory.pb.h"
20 
21 namespace tensorflow {
22 
23 const string LogMemory::kLogMemoryLabel = "__LOG_MEMORY__";
24 
IsEnabled()25 bool LogMemory::IsEnabled() { return VLOG_IS_ON(1); }
26 
27 namespace {
28 
29 // Write the proto entry to LOG(INFO).
30 template <typename T>
OutputToLog(const T & proto)31 void OutputToLog(const T& proto) {
32   string type_name = proto.GetTypeName();
33   const size_t index = type_name.find_last_of(".");
34   if (index != string::npos) type_name = type_name.substr(index + 1);
35   LOG(INFO) << LogMemory::kLogMemoryLabel << " " << type_name << " { "
36             << ProtoShortDebugString(proto) << " }";
37 }
38 
39 }  // namespace
40 
RecordStep(const int64 step_id,const string & handle)41 void LogMemory::RecordStep(const int64 step_id, const string& handle) {
42   MemoryLogStep step;
43   step.set_step_id(step_id);
44   step.set_handle(handle);
45   OutputToLog(step);
46 }
47 
RecordTensorAllocation(const string & kernel_name,const int64 step_id,const Tensor & tensor)48 void LogMemory::RecordTensorAllocation(const string& kernel_name,
49                                        const int64 step_id,
50                                        const Tensor& tensor) {
51   MemoryLogTensorAllocation allocation;
52   allocation.set_step_id(step_id);
53   allocation.set_kernel_name(kernel_name);
54   tensor.FillDescription(allocation.mutable_tensor());
55   OutputToLog(allocation);
56 }
57 
RecordTensorDeallocation(const int64 allocation_id,const string & allocator_name)58 void LogMemory::RecordTensorDeallocation(const int64 allocation_id,
59                                          const string& allocator_name) {
60   MemoryLogTensorDeallocation deallocation;
61   deallocation.set_allocation_id(allocation_id);
62   deallocation.set_allocator_name(allocator_name);
63   OutputToLog(deallocation);
64 }
65 
RecordTensorOutput(const string & kernel_name,const int64 step_id,const int index,const Tensor & tensor)66 void LogMemory::RecordTensorOutput(const string& kernel_name,
67                                    const int64 step_id, const int index,
68                                    const Tensor& tensor) {
69   MemoryLogTensorOutput output;
70   output.set_step_id(step_id);
71   output.set_kernel_name(kernel_name);
72   output.set_index(index);
73   tensor.FillDescription(output.mutable_tensor());
74   OutputToLog(output);
75 }
76 
RecordRawAllocation(const string & operation,const int64 step_id,size_t num_bytes,void * ptr,Allocator * allocator)77 void LogMemory::RecordRawAllocation(const string& operation,
78                                     const int64 step_id, size_t num_bytes,
79                                     void* ptr, Allocator* allocator) {
80   MemoryLogRawAllocation allocation;
81   allocation.set_step_id(step_id);
82   allocation.set_operation(operation);
83   allocation.set_num_bytes(static_cast<int64>(num_bytes));
84   allocation.set_ptr(reinterpret_cast<uintptr_t>(ptr));
85   allocation.set_allocation_id(allocator->AllocationId(ptr));
86   allocation.set_allocator_name(allocator->Name());
87   OutputToLog(allocation);
88 }
89 
RecordRawDeallocation(const string & operation,const int64 step_id,void * ptr,Allocator * allocator,bool deferred)90 void LogMemory::RecordRawDeallocation(const string& operation,
91                                       const int64 step_id, void* ptr,
92                                       Allocator* allocator, bool deferred) {
93   MemoryLogRawDeallocation deallocation;
94   deallocation.set_step_id(step_id);
95   deallocation.set_operation(operation);
96   deallocation.set_allocation_id(allocator->AllocationId(ptr));
97   deallocation.set_allocator_name(allocator->Name());
98   deallocation.set_deferred(deferred);
99   OutputToLog(deallocation);
100 }
101 
102 }  // namespace tensorflow
103