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