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 // Proto GetTypeName requires full proto. We remove the usage here
32 // to stick with lite proto which is prefered on mobile version.
33 // string type_name = proto.GetTypeName();
34 // const size_t index = type_name.find_last_of('.');
35 // if (index != string::npos) type_name = type_name.substr(index + 1);
36 // LOG(INFO) << LogMemory::kLogMemoryLabel << " " << type_name << " { "
37 // << proto.ShortDebugString() << " }";
38 LOG(INFO) << LogMemory::kLogMemoryLabel << " { "
39 << proto.ShortDebugString() << " }";
40 }
41
42 } // namespace
43
RecordStep(const int64_t step_id,const string & handle)44 void LogMemory::RecordStep(const int64_t step_id, const string& handle) {
45 MemoryLogStep step;
46 step.set_step_id(step_id);
47 step.set_handle(handle);
48 OutputToLog(step);
49 }
50
RecordTensorAllocation(const string & kernel_name,const int64_t step_id,const Tensor & tensor)51 void LogMemory::RecordTensorAllocation(const string& kernel_name,
52 const int64_t step_id,
53 const Tensor& tensor) {
54 MemoryLogTensorAllocation allocation;
55 allocation.set_step_id(step_id);
56 allocation.set_kernel_name(kernel_name);
57 tensor.FillDescription(allocation.mutable_tensor());
58 OutputToLog(allocation);
59 }
60
RecordTensorDeallocation(const int64_t allocation_id,const string & allocator_name)61 void LogMemory::RecordTensorDeallocation(const int64_t allocation_id,
62 const string& allocator_name) {
63 MemoryLogTensorDeallocation deallocation;
64 deallocation.set_allocation_id(allocation_id);
65 deallocation.set_allocator_name(allocator_name);
66 OutputToLog(deallocation);
67 }
68
RecordTensorOutput(const string & kernel_name,const int64_t step_id,const int index,const Tensor & tensor)69 void LogMemory::RecordTensorOutput(const string& kernel_name,
70 const int64_t step_id, const int index,
71 const Tensor& tensor) {
72 MemoryLogTensorOutput output;
73 output.set_step_id(step_id);
74 output.set_kernel_name(kernel_name);
75 output.set_index(index);
76 tensor.FillDescription(output.mutable_tensor());
77 OutputToLog(output);
78 }
79
RecordRawAllocation(const string & operation,const int64_t step_id,size_t num_bytes,void * ptr,Allocator * allocator)80 void LogMemory::RecordRawAllocation(const string& operation,
81 const int64_t step_id, size_t num_bytes,
82 void* ptr, Allocator* allocator) {
83 MemoryLogRawAllocation allocation;
84 allocation.set_step_id(step_id);
85 allocation.set_operation(operation);
86 allocation.set_num_bytes(static_cast<int64_t>(num_bytes));
87 allocation.set_ptr(reinterpret_cast<uintptr_t>(ptr));
88 allocation.set_allocation_id(allocator->AllocationId(ptr));
89 allocation.set_allocator_name(allocator->Name());
90 OutputToLog(allocation);
91 }
92
RecordRawDeallocation(const string & operation,const int64_t step_id,void * ptr,Allocator * allocator,bool deferred)93 void LogMemory::RecordRawDeallocation(const string& operation,
94 const int64_t step_id, void* ptr,
95 Allocator* allocator, bool deferred) {
96 MemoryLogRawDeallocation deallocation;
97 deallocation.set_step_id(step_id);
98 deallocation.set_operation(operation);
99 deallocation.set_allocation_id(allocator->AllocationId(ptr));
100 deallocation.set_allocator_name(allocator->Name());
101 deallocation.set_deferred(deferred);
102 OutputToLog(deallocation);
103 }
104
105 } // namespace tensorflow
106