Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce memory manager statistics #3639

Merged
merged 3 commits into from
Jun 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cpp/open3d/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ target_sources(core PRIVATE
Indexer.cpp
MemoryManager.cpp
MemoryManagerCPU.cpp
MemoryManagerStatistic.cpp
NumpyIO.cpp
ShapeUtil.cpp
Tensor.cpp
Expand Down
13 changes: 13 additions & 0 deletions cpp/open3d/core/Device.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ class Device {

bool operator!=(const Device& other) const { return !operator==(other); }

bool operator<(const Device& other) const {
return ToString() < other.ToString();
}

std::string ToString() const {
std::string str = "";
switch (device_type_) {
Expand Down Expand Up @@ -132,3 +136,12 @@ class Device {

} // namespace core
} // namespace open3d

namespace std {
template <>
struct hash<open3d::core::Device> {
std::size_t operator()(const open3d::core::Device& device) const {
return std::hash<std::string>{}(device.ToString());
}
};
} // namespace std
9 changes: 7 additions & 2 deletions cpp/open3d/core/MemoryManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,23 @@

#include "open3d/core/Blob.h"
#include "open3d/core/Device.h"
#include "open3d/core/MemoryManagerStatistic.h"
#include "open3d/utility/Helper.h"
#include "open3d/utility/Logging.h"

namespace open3d {
namespace core {

void* MemoryManager::Malloc(size_t byte_size, const Device& device) {
return GetDeviceMemoryManager(device)->Malloc(byte_size, device);
void* ptr = GetDeviceMemoryManager(device)->Malloc(byte_size, device);
MemoryManagerStatistic::GetInstance().IncrementCountMalloc(ptr, byte_size,
device);
return ptr;
}

void MemoryManager::Free(void* ptr, const Device& device) {
return GetDeviceMemoryManager(device)->Free(ptr, device);
GetDeviceMemoryManager(device)->Free(ptr, device);
MemoryManagerStatistic::GetInstance().IncrementCountFree(ptr, device);
}

void MemoryManager::Memcpy(void* dst_ptr,
Expand Down
127 changes: 127 additions & 0 deletions cpp/open3d/core/MemoryManagerStatistic.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
// ----------------------------------------------------------------------------
// - Open3D: www.open3d.org -
// ----------------------------------------------------------------------------
// The MIT License (MIT)
//
// Copyright (c) 2018-2021 www.open3d.org
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
// ----------------------------------------------------------------------------

#include "open3d/core/MemoryManagerStatistic.h"

#include <algorithm>
#include <numeric>

#include "open3d/utility/Logging.h"

namespace open3d {
namespace core {

MemoryManagerStatistic& MemoryManagerStatistic::GetInstance() {
// Ensure the static Logger instance is instantiated before the
// MemoryManagerStatistic instance.
// Since destruction of static instances happens in reverse order,
// this guarantees that the Logger can be used at any point in time.
utility::Logger::GetInstance();

static MemoryManagerStatistic instance;
return instance;
}

MemoryManagerStatistic::~MemoryManagerStatistic() {
if (print_at_program_end_) {
// Always use the default print function (print to the console).
// Custom print functions like py::print may not work reliably
// at this point in time.
utility::Logger::GetInstance().ResetPrintFunction();
Print();
}
}

void MemoryManagerStatistic::SetPrintLevel(PrintLevel level) { level_ = level; }

void MemoryManagerStatistic::SetPrintAtProgramEnd(bool print) {
print_at_program_end_ = print;
}

void MemoryManagerStatistic::Print() const {
if (level_ == PrintLevel::None) {
return;
}

auto is_unbalanced = [](const auto& value_pair) -> bool {
return value_pair.second.count_malloc_ != value_pair.second.count_free_;
};

if (level_ == PrintLevel::Unbalanced &&
std::count_if(statistics_.begin(), statistics_.end(), is_unbalanced) ==
0) {
return;
}

utility::LogInfo("Memory Statistics: (Device) (#Malloc) (#Free)");
utility::LogInfo("---------------------------------------------");
for (const auto& value_pair : statistics_) {
if (level_ == PrintLevel::Unbalanced && !is_unbalanced(value_pair)) {
continue;
}

if (is_unbalanced(value_pair)) {
size_t count_leaking = value_pair.second.count_malloc_ -
value_pair.second.count_free_;

size_t leaking_byte_size = std::accumulate(
value_pair.second.active_allocations_.begin(),
value_pair.second.active_allocations_.end(), 0,
[](size_t count, auto ptr_byte_size) -> size_t {
return count + ptr_byte_size.second;
});

utility::LogWarning("{}: {} {} --> {} with {} bytes",
value_pair.first.ToString(),
value_pair.second.count_malloc_,
value_pair.second.count_free_, count_leaking,
leaking_byte_size);
} else {
utility::LogInfo("{}: {} {}", value_pair.first.ToString(),
value_pair.second.count_malloc_,
value_pair.second.count_free_);
}
}
utility::LogInfo("---------------------------------------------");
}

void MemoryManagerStatistic::IncrementCountMalloc(void* ptr,
size_t byte_size,
const Device& device) {
std::lock_guard<std::mutex> lock(statistics_mutex_);
statistics_[device].count_malloc_++;
statistics_[device].active_allocations_.emplace(ptr, byte_size);
}

void MemoryManagerStatistic::IncrementCountFree(void* ptr,
const Device& device) {
std::lock_guard<std::mutex> lock(statistics_mutex_);
statistics_[device].count_free_++;
statistics_[device].active_allocations_.erase(ptr);
}

} // namespace core
} // namespace open3d
85 changes: 85 additions & 0 deletions cpp/open3d/core/MemoryManagerStatistic.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// ----------------------------------------------------------------------------
// - Open3D: www.open3d.org -
// ----------------------------------------------------------------------------
// The MIT License (MIT)
//
// Copyright (c) 2018-2021 www.open3d.org
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
// ----------------------------------------------------------------------------

#pragma once

#include <cstddef>
#include <map>
#include <mutex>
#include <unordered_map>

#include "open3d/core/Device.h"

namespace open3d {
namespace core {

class MemoryManagerStatistic {
public:
enum class PrintLevel {
/// Statistics for all used devices are printed.
All = 0,
/// Only devices with unbalanced counts are printed.
/// This is typically an indicator for memory leaks.
Unbalanced = 1,
/// No statistics are printed.
None = 2,
};

static MemoryManagerStatistic& GetInstance();

MemoryManagerStatistic(const MemoryManagerStatistic&) = delete;
MemoryManagerStatistic& operator=(MemoryManagerStatistic&) = delete;

~MemoryManagerStatistic();

void SetPrintLevel(PrintLevel level);
void SetPrintAtProgramEnd(bool print);
void Print() const;

void IncrementCountMalloc(void* ptr,
size_t byte_size,
const Device& device);
void IncrementCountFree(void* ptr, const Device& device);

private:
MemoryManagerStatistic() = default;

struct MemoryStatistics {
size_t count_malloc_ = 0;
size_t count_free_ = 0;
std::unordered_map<void*, size_t> active_allocations_;
};

/// Only print unbalanced statistics by default.
PrintLevel level_ = PrintLevel::Unbalanced;
bool print_at_program_end_ = true;

std::mutex statistics_mutex_;
std::map<Device, MemoryStatistics> statistics_;
};

} // namespace core
} // namespace open3d
17 changes: 9 additions & 8 deletions cpp/open3d/core/linalg/LUCUDA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,24 @@ void LUCUDA(void* A_data,
cusolverDnHandle_t handle = CuSolverContext::GetInstance()->GetHandle();
DISPATCH_LINALG_DTYPE_TO_TEMPLATE(dtype, [&]() {
int len;
int* dinfo =
static_cast<int*>(MemoryManager::Malloc(sizeof(int), device));
OPEN3D_CUSOLVER_CHECK(
getrf_cuda_buffersize<scalar_t>(handle, rows, cols, rows, &len),
"getrf_buffersize failed in LUCUDA");

int* dinfo =
static_cast<int*>(MemoryManager::Malloc(sizeof(int), device));
void* workspace = MemoryManager::Malloc(len * sizeof(scalar_t), device);

OPEN3D_CUSOLVER_CHECK_WITH_DINFO(
getrf_cuda<scalar_t>(handle, rows, cols,
static_cast<scalar_t*>(A_data), rows,
static_cast<scalar_t*>(workspace),
static_cast<int*>(ipiv_data), dinfo),
"getrf failed in LUCUDA", dinfo, device);
cusolverStatus_t getrf_status = getrf_cuda<scalar_t>(
handle, rows, cols, static_cast<scalar_t*>(A_data), rows,
static_cast<scalar_t*>(workspace), static_cast<int*>(ipiv_data),
dinfo);

MemoryManager::Free(workspace, device);
MemoryManager::Free(dinfo, device);

OPEN3D_CUSOLVER_CHECK_WITH_DINFO(getrf_status, "getrf failed in LUCUDA",
dinfo, device);
});
}

Expand Down