Open3D (C++ API)  0.16.1
MemoryManagerStatistic.h
Go to the documentation of this file.
1// ----------------------------------------------------------------------------
2// - Open3D: www.open3d.org -
3// ----------------------------------------------------------------------------
4// The MIT License (MIT)
5//
6// Copyright (c) 2018-2021 www.open3d.org
7//
8// Permission is hereby granted, free of charge, to any person obtaining a copy
9// of this software and associated documentation files (the "Software"), to deal
10// in the Software without restriction, including without limitation the rights
11// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12// copies of the Software, and to permit persons to whom the Software is
13// furnished to do so, subject to the following conditions:
14//
15// The above copyright notice and this permission notice shall be included in
16// all copies or substantial portions of the Software.
17//
18// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24// IN THE SOFTWARE.
25// ----------------------------------------------------------------------------
26
27#pragma once
28
29#include <cstddef>
30#include <map>
31#include <mutex>
32#include <unordered_map>
33
34#include "open3d/core/Device.h"
35
36namespace open3d {
37namespace core {
38
40public:
41 enum class PrintLevel {
43 All = 0,
46 Unbalanced = 1,
48 None = 2,
49 };
50
52
55
57
59 void SetPrintLevel(PrintLevel level);
60
64 void SetPrintAtProgramEnd(bool print);
65
67 void SetPrintAtMallocFree(bool print);
68
70 void Print() const;
71
74 bool HasLeaks() const;
75
77 void CountMalloc(void* ptr, size_t byte_size, const Device& device);
78
82 void CountFree(void* ptr, const Device& device);
83
85 void Reset();
86
87private:
88 MemoryManagerStatistic() = default;
89
90 struct MemoryStatistics {
91 bool IsBalanced() const;
92
93 int64_t count_malloc_ = 0;
94 int64_t count_free_ = 0;
95 std::unordered_map<void*, size_t> active_allocations_;
96 };
97
100
103 bool print_at_program_end_ = true;
104
106 bool print_at_malloc_free_ = false;
107
108 std::mutex statistics_mutex_;
109 std::map<Device, MemoryStatistics> statistics_;
110};
111
112} // namespace core
113} // namespace open3d
Definition: Device.h:37
Definition: MemoryManagerStatistic.h:39
void CountFree(void *ptr, const Device &device)
Definition: MemoryManagerStatistic.cpp:162
static MemoryManagerStatistic & GetInstance()
Definition: MemoryManagerStatistic.cpp:38
bool HasLeaks() const
Definition: MemoryManagerStatistic.cpp:130
~MemoryManagerStatistic()
Definition: MemoryManagerStatistic.cpp:49
void SetPrintAtProgramEnd(bool print)
Definition: MemoryManagerStatistic.cpp:67
void Print() const
Prints statistics for all recorded devices depending on the print level.
Definition: MemoryManagerStatistic.cpp:75
MemoryManagerStatistic & operator=(MemoryManagerStatistic &)=delete
void Reset()
Resets the statistics.
Definition: MemoryManagerStatistic.cpp:191
void SetPrintLevel(PrintLevel level)
Sets the level of provided information for printing.
Definition: MemoryManagerStatistic.cpp:65
PrintLevel
Definition: MemoryManagerStatistic.h:41
void SetPrintAtMallocFree(bool print)
Enables or disables printing at each malloc and free.
Definition: MemoryManagerStatistic.cpp:71
void CountMalloc(void *ptr, size_t byte_size, const Device &device)
Adds the given allocation to the statistics.
Definition: MemoryManagerStatistic.cpp:137
MemoryManagerStatistic(const MemoryManagerStatistic &)=delete
constexpr utility::nullopt_t None
Definition: TensorKey.h:39
Definition: PinholeCameraIntrinsic.cpp:35