2 * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
21 std::unique_ptr<Trace> GlobalTrace(new Trace);
23 bool Trace::Guard::_is_trace_not_available = true;
24 thread_local bool Trace::Guard::_is_recursion_detected = false;
30 _out.open(getLogFileNameFromEnvVariable("HEAP_TRACE_LOG"));
33 Guard{}.markTraceAsReady();
36 const char *Trace::getLogFileNameFromEnvVariable(const char *env_variable_name)
38 return getenv(env_variable_name);
41 void Trace::logAllocationEvent(void *memory_ptr, size_t size_of_allocated_space_in_bytes)
43 Guard{}.signalizeAboutPossibleRecursion();
44 std::lock_guard<std::mutex> guard(_lock);
45 _total_allocated_bytes_on_cpu += size_of_allocated_space_in_bytes;
46 if (_peak_heap_usage_on_cpu < _total_allocated_bytes_on_cpu - _total_deallocated_bytes_on_cpu)
48 _peak_heap_usage_on_cpu = _total_allocated_bytes_on_cpu - _total_deallocated_bytes_on_cpu;
50 _memory_in_use_on_cpu[memory_ptr] = size_of_allocated_space_in_bytes;
51 Guard{}.signalizeThatDangerOfRecursionHasPassed();
54 void Trace::logDeallocationEvent(void *memory_ptr)
56 Guard{}.signalizeAboutPossibleRecursion();
57 std::lock_guard<std::mutex> guard(_lock);
58 auto found_memory_space_description = _memory_in_use_on_cpu.find(memory_ptr);
59 if (found_memory_space_description != _memory_in_use_on_cpu.end())
61 _total_deallocated_bytes_on_cpu += found_memory_space_description->second;
62 _memory_in_use_on_cpu.erase(found_memory_space_description);
64 Guard{}.signalizeThatDangerOfRecursionHasPassed();
67 void Trace::logAllocationEvent(cl_mem memory_ptr, size_t size_of_allocated_space_in_bytes)
69 Guard{}.signalizeAboutPossibleRecursion();
70 std::lock_guard<std::mutex> guard(_lock);
71 auto found_memory_space_description = _memory_in_use_on_gpu.find(memory_ptr);
72 if (found_memory_space_description == _memory_in_use_on_gpu.end())
74 _memory_in_use_on_gpu.insert(
75 std::make_pair(memory_ptr, MemoryTraits(1, size_of_allocated_space_in_bytes)));
76 _total_allocated_bytes_on_gpu += size_of_allocated_space_in_bytes;
77 if (_peak_heap_usage_on_gpu < _total_allocated_bytes_on_gpu - _total_deallocated_bytes_on_gpu)
79 _peak_heap_usage_on_gpu = _total_allocated_bytes_on_gpu - _total_deallocated_bytes_on_gpu;
84 ++found_memory_space_description->second.ref_counter;
86 Guard{}.signalizeThatDangerOfRecursionHasPassed();
89 void Trace::logDeallocationEvent(cl_mem memory_ptr)
91 Guard{}.signalizeAboutPossibleRecursion();
92 std::lock_guard<std::mutex> guard(_lock);
93 auto found_memory_space_description = _memory_in_use_on_gpu.find(memory_ptr);
94 if (found_memory_space_description != _memory_in_use_on_gpu.end())
96 if (--found_memory_space_description->second.ref_counter == 0)
98 _total_deallocated_bytes_on_gpu += found_memory_space_description->second.size;
99 _memory_in_use_on_gpu.erase(found_memory_space_description);
102 Guard{}.signalizeThatDangerOfRecursionHasPassed();
107 Guard{}.markTraceAsNotReady();
109 _out << "On CPU - Peak heap usage: " << _peak_heap_usage_on_cpu
110 << " B, Total allocated: " << _total_allocated_bytes_on_cpu
111 << " B, Total deallocated: " << _total_deallocated_bytes_on_cpu << " B\n";
112 _out << "On GPU - Peak mem usage: " << _peak_heap_usage_on_gpu
113 << " B, Total allocated: " << _total_allocated_bytes_on_gpu
114 << " B, Total deallocated: " << _total_deallocated_bytes_on_gpu << " B\n";