summaryrefslogtreecommitdiff
path: root/runtime/contrib/heap_trace/src/trace.cc
blob: 82f2915cb65e52b06ff8e37def10c816b045d255 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
 * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "trace.h"

#include <memory>

std::unique_ptr<Trace> GlobalTrace(new Trace);

bool Trace::Guard::_is_trace_not_available = true;
thread_local bool Trace::Guard::_is_recursion_detected = false;

Trace::Trace()
{
  if (!_out.is_open())
  {
    _out.open(getLogFileNameFromEnvVariable("HEAP_TRACE_LOG"));
  }

  Guard{}.markTraceAsReady();
}

const char *Trace::getLogFileNameFromEnvVariable(const char *env_variable_name)
{
  return getenv(env_variable_name);
}

void Trace::logAllocationEvent(void *memory_ptr, size_t size_of_allocated_space_in_bytes)
{
  Guard{}.signalizeAboutPossibleRecursion();
  std::lock_guard<std::mutex> guard(_lock);
  _total_allocated_bytes_on_cpu += size_of_allocated_space_in_bytes;
  if (_peak_heap_usage_on_cpu < _total_allocated_bytes_on_cpu - _total_deallocated_bytes_on_cpu)
  {
    _peak_heap_usage_on_cpu = _total_allocated_bytes_on_cpu - _total_deallocated_bytes_on_cpu;
  }
  _memory_in_use_on_cpu[memory_ptr] = size_of_allocated_space_in_bytes;
  Guard{}.signalizeThatDangerOfRecursionHAsPassed();
}

void Trace::logDeallocationEvent(void *memory_ptr)
{
  Guard{}.signalizeAboutPossibleRecursion();
  std::lock_guard<std::mutex> guard(_lock);
  auto found_memory_space_description = _memory_in_use_on_cpu.find(memory_ptr);
  if (found_memory_space_description != _memory_in_use_on_cpu.end())
  {
    _total_deallocated_bytes_on_cpu += found_memory_space_description->second;
    _memory_in_use_on_cpu.erase(found_memory_space_description);
  }
  Guard{}.signalizeThatDangerOfRecursionHAsPassed();
}

void Trace::logAllocationEvent(cl_mem memory_ptr, size_t size_of_allocated_space_in_bytes)
{
  Guard{}.signalizeAboutPossibleRecursion();
  std::lock_guard<std::mutex> guard(_lock);
  _total_allocated_bytes_on_gpu += size_of_allocated_space_in_bytes;
  if (_peak_heap_usage_on_gpu < _total_allocated_bytes_on_gpu - _total_deallocated_bytes_on_gpu)
  {
    _peak_heap_usage_on_gpu = _total_allocated_bytes_on_gpu - _total_deallocated_bytes_on_gpu;
  }
  _memory_in_use_on_gpu[memory_ptr] = size_of_allocated_space_in_bytes;
  Guard{}.signalizeThatDangerOfRecursionHAsPassed();
}

void Trace::logDeallocationEvent(cl_mem memory_ptr)
{
  Guard{}.signalizeAboutPossibleRecursion();
  std::lock_guard<std::mutex> guard(_lock);
  auto found_memory_space_description = _memory_in_use_on_gpu.find(memory_ptr);
  if (found_memory_space_description != _memory_in_use_on_gpu.end())
  {
    _total_deallocated_bytes_on_gpu += found_memory_space_description->second;
    _memory_in_use_on_gpu.erase(found_memory_space_description);
  }
  Guard{}.signalizeThatDangerOfRecursionHAsPassed();
}

Trace::~Trace()
{
  Guard{}.markTraceAsNotReady();

  _out << "On CPU - Peak heap usage: " << _peak_heap_usage_on_cpu
       << " B, Total allocated: " << _total_allocated_bytes_on_cpu
       << " B, Total deallocated: " << _total_deallocated_bytes_on_cpu << " B\n";
  _out << "On GPU - Peak mem usage: " << _peak_heap_usage_on_gpu
       << " B, Total allocated: " << _total_allocated_bytes_on_gpu
       << " B, Total deallocated: " << _total_deallocated_bytes_on_gpu << " B\n";
}