Loading [MathJax]/extensions/TeX/AMSsymbols.js
Open3D (C++ API)  0.14.1
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
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 
36 namespace open3d {
37 namespace core {
38 
40 public:
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 
87 private:
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
static MemoryManagerStatistic & GetInstance()
Definition: MemoryManagerStatistic.cpp:38
void SetPrintLevel(PrintLevel level)
Sets the level of provided information for printing.
Definition: MemoryManagerStatistic.cpp:65
Statistics for all used devices are printed.
MemoryManagerStatistic & operator=(MemoryManagerStatistic &)=delete
~MemoryManagerStatistic()
Definition: MemoryManagerStatistic.cpp:49
void CountFree(void *ptr, const Device &device)
Definition: MemoryManagerStatistic.cpp:162
void Reset()
Resets the statistics.
Definition: MemoryManagerStatistic.cpp:191
Definition: Device.h:39
PrintLevel
Definition: MemoryManagerStatistic.h:41
Definition: MemoryManagerStatistic.h:39
void CountMalloc(void *ptr, size_t byte_size, const Device &device)
Adds the given allocation to the statistics.
Definition: MemoryManagerStatistic.cpp:137
void SetPrintAtProgramEnd(bool print)
Definition: MemoryManagerStatistic.cpp:67
Definition: PinholeCameraIntrinsic.cpp:35
void SetPrintAtMallocFree(bool print)
Enables or disables printing at each malloc and free.
Definition: MemoryManagerStatistic.cpp:71
bool HasLeaks() const
Definition: MemoryManagerStatistic.cpp:130
void Print() const
Prints statistics for all recorded devices depending on the print level.
Definition: MemoryManagerStatistic.cpp:75