Open3D (C++ API)  0.18.0+5c982c7
MemoryManagerStatistic.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - Open3D: www.open3d.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2023 www.open3d.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #pragma once
9 
10 #include <cstddef>
11 #include <map>
12 #include <mutex>
13 #include <unordered_map>
14 
15 #include "open3d/core/Device.h"
16 
17 namespace open3d {
18 namespace core {
19 
21 public:
22  enum class PrintLevel {
24  All = 0,
27  Unbalanced = 1,
29  None = 2,
30  };
31 
33 
36 
38 
40  void SetPrintLevel(PrintLevel level);
41 
45  void SetPrintAtProgramEnd(bool print);
46 
48  void SetPrintAtMallocFree(bool print);
49 
51  void Print() const;
52 
55  bool HasLeaks() const;
56 
58  void CountMalloc(void* ptr, size_t byte_size, const Device& device);
59 
63  void CountFree(void* ptr, const Device& device);
64 
66  void Reset();
67 
68 private:
69  MemoryManagerStatistic() = default;
70 
71  struct MemoryStatistics {
72  bool IsBalanced() const;
73 
74  int64_t count_malloc_ = 0;
75  int64_t count_free_ = 0;
76  std::unordered_map<void*, size_t> active_allocations_;
77  };
78 
81 
84  bool print_at_program_end_ = true;
85 
87  bool print_at_malloc_free_ = false;
88 
89  std::mutex statistics_mutex_;
90  std::map<Device, MemoryStatistics> statistics_;
91 };
92 
93 } // namespace core
94 } // namespace open3d
Definition: Device.h:18
Definition: MemoryManagerStatistic.h:20
void CountFree(void *ptr, const Device &device)
Definition: MemoryManagerStatistic.cpp:143
static MemoryManagerStatistic & GetInstance()
Definition: MemoryManagerStatistic.cpp:19
bool HasLeaks() const
Definition: MemoryManagerStatistic.cpp:111
~MemoryManagerStatistic()
Definition: MemoryManagerStatistic.cpp:30
void SetPrintAtProgramEnd(bool print)
Definition: MemoryManagerStatistic.cpp:48
void Print() const
Prints statistics for all recorded devices depending on the print level.
Definition: MemoryManagerStatistic.cpp:56
void Reset()
Resets the statistics.
Definition: MemoryManagerStatistic.cpp:172
void SetPrintLevel(PrintLevel level)
Sets the level of provided information for printing.
Definition: MemoryManagerStatistic.cpp:46
MemoryManagerStatistic & operator=(MemoryManagerStatistic &)=delete
PrintLevel
Definition: MemoryManagerStatistic.h:22
@ All
Statistics for all used devices are printed.
void SetPrintAtMallocFree(bool print)
Enables or disables printing at each malloc and free.
Definition: MemoryManagerStatistic.cpp:52
void CountMalloc(void *ptr, size_t byte_size, const Device &device)
Adds the given allocation to the statistics.
Definition: MemoryManagerStatistic.cpp:118
MemoryManagerStatistic(const MemoryManagerStatistic &)=delete
Definition: PinholeCameraIntrinsic.cpp:16