020aeb90e28f8472cafe80172c8626f3a3fe4a32
[platform/core/ml/nnfw.git] / runtime / contrib / heap_trace / src / trace.cc
1 /*
2  * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
3  *
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
7  *
8  *    http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include "trace.h"
18
19 #include <memory>
20
21 std::unique_ptr<Trace> GlobalTrace(new Trace);
22
23 bool Trace::Guard::_is_trace_not_available = true;
24 thread_local bool Trace::Guard::_is_recursion_detected = false;
25
26 Trace::Trace()
27 {
28   if (!_out.is_open())
29   {
30     _out.open(getLogFileNameFromEnvVariable("HEAP_TRACE_LOG"));
31   }
32
33   Guard{}.markTraceAsReady();
34 }
35
36 const char *Trace::getLogFileNameFromEnvVariable(const char *env_variable_name)
37 {
38   return getenv(env_variable_name);
39 }
40
41 void Trace::logAllocationEvent(void *memory_ptr, size_t size_of_allocated_space_in_bytes)
42 {
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)
47   {
48     _peak_heap_usage_on_cpu = _total_allocated_bytes_on_cpu - _total_deallocated_bytes_on_cpu;
49   }
50   _memory_in_use_on_cpu[memory_ptr] = size_of_allocated_space_in_bytes;
51   Guard{}.signalizeThatDangerOfRecursionHasPassed();
52 }
53
54 void Trace::logDeallocationEvent(void *memory_ptr)
55 {
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())
60   {
61     _total_deallocated_bytes_on_cpu += found_memory_space_description->second;
62     _memory_in_use_on_cpu.erase(found_memory_space_description);
63   }
64   Guard{}.signalizeThatDangerOfRecursionHasPassed();
65 }
66
67 void Trace::logAllocationEvent(cl_mem memory_ptr, size_t size_of_allocated_space_in_bytes)
68 {
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())
73   {
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)
78     {
79       _peak_heap_usage_on_gpu = _total_allocated_bytes_on_gpu - _total_deallocated_bytes_on_gpu;
80     }
81   }
82   else
83   {
84     ++found_memory_space_description->second.ref_counter;
85   }
86   Guard{}.signalizeThatDangerOfRecursionHasPassed();
87 }
88
89 void Trace::logDeallocationEvent(cl_mem memory_ptr)
90 {
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())
95   {
96     if (--found_memory_space_description->second.ref_counter == 0)
97     {
98       _total_deallocated_bytes_on_gpu += found_memory_space_description->second.size;
99       _memory_in_use_on_gpu.erase(found_memory_space_description);
100     }
101   }
102   Guard{}.signalizeThatDangerOfRecursionHasPassed();
103 }
104
105 Trace::~Trace()
106 {
107   Guard{}.markTraceAsNotReady();
108
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";
115 }