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

Conversation

stotko
Copy link
Contributor

@stotko stotko commented Jun 24, 2021

Features:

  • Thread-safe MemoryManagerStatistic instance implemented as a singleton
  • Per Device logging of memory allocations and frees
  • Configurable print modes: All, Unbalanced, None
  • Automatic print at the end of the program, defaults to Unbalanced to minimize noise

Detected memory leaks in unit tests:

  • Skipped Free in LU decomposition when the operation fails

This change is Reviewable

@stotko stotko requested a review from theNded June 24, 2021 13:28
@update-docs
Copy link

update-docs bot commented Jun 24, 2021

Thanks for submitting this pull request! The maintainers of this repository would appreciate if you could update the CHANGELOG.md based on your changes.

Copy link
Contributor

@theNded theNded left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 4 of 5 files at r1.
Reviewable status: 4 of 5 files reviewed, 2 unresolved discussions (waiting on @stotko)


cpp/open3d/core/MemoryManager.cpp, line 42 at r1 (raw file):

void* MemoryManager::Malloc(size_t byte_size, const Device& device) {
    MemoryManagerStatistic::getInstance().IncrementCountMalloc(device);

Shall we count byte_size as well? That might be helpful for more advanced checks.


cpp/open3d/core/MemoryManagerStatistic.cpp, line 36 at r1 (raw file):

namespace core {

MemoryManagerStatistic& MemoryManagerStatistic::getInstance() {

Should we use GetInstance to keep consistency?

Copy link
Contributor Author

@stotko stotko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 2 of 5 files reviewed, 2 unresolved discussions (waiting on @theNded)


cpp/open3d/core/MemoryManager.cpp, line 42 at r1 (raw file):

Previously, theNded (Wei Dong) wrote…

Shall we count byte_size as well? That might be helpful for more advanced checks.

Done.


cpp/open3d/core/MemoryManagerStatistic.cpp, line 36 at r1 (raw file):

Previously, theNded (Wei Dong) wrote…

Should we use GetInstance to keep consistency?

Done.

Copy link
Contributor

@theNded theNded left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 3 of 3 files at r2.
Reviewable status: all files reviewed, 2 unresolved discussions (waiting on @theNded)

Copy link
Collaborator

@yxlao yxlao left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 2 of 5 files at r1, 2 of 3 files at r2.
Reviewable status: all files reviewed, 4 unresolved discussions (waiting on @stotko and @theNded)


cpp/open3d/core/MemoryManager.cpp, line 50 at r2 (raw file):

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

nit: shall we change the order to be consistent with MemoryManager::Free(ptr, device)?
i.e.

// We have:
MemoryManager::Free(ptr, device);
// Change IncrementCountFree to:
IncrementCountFree(ptr, device);

Similarly for IncrementCountMalloc:

// We have:
MemoryManager::Malloc(byte_size, device);
// Change IncrementCountMalloc to:
IncrementCountMalloc(ptr, byte_size, device);

cpp/open3d/core/MemoryManagerStatistic.h, line 76 at r2 (raw file):

    };

    struct DeviceComparator {

This could be useful on other occasions if we'd like to use Device as the key in a map/unordered_map.

Shall we consider moving this to the Device class itself? For the unordered_map version, this would look like:

class Device {
public:
    struct Hash {
        std::size_t operator()(const open3d::core::Device& device) const {
            return std::hash<std::string>{}(device.ToString());
        }
    };
};

and, we can use the unordered_map as:

static std::unordered_map<Device, MemoryStatistics, Device::Hash>;

Copy link
Contributor Author

@stotko stotko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 2 of 6 files reviewed, 3 unresolved discussions (waiting on @theNded and @yxlao)


cpp/open3d/core/MemoryManager.cpp, line 50 at r2 (raw file):

Previously, yxlao (Yixing Lao) wrote…

nit: shall we change the order to be consistent with MemoryManager::Free(ptr, device)?
i.e.

// We have:
MemoryManager::Free(ptr, device);
// Change IncrementCountFree to:
IncrementCountFree(ptr, device);

Similarly for IncrementCountMalloc:

// We have:
MemoryManager::Malloc(byte_size, device);
// Change IncrementCountMalloc to:
IncrementCountMalloc(ptr, byte_size, device);

Done.


cpp/open3d/core/MemoryManagerStatistic.h, line 76 at r2 (raw file):

Previously, yxlao (Yixing Lao) wrote…

This could be useful on other occasions if we'd like to use Device as the key in a map/unordered_map.

Shall we consider moving this to the Device class itself? For the unordered_map version, this would look like:

class Device {
public:
    struct Hash {
        std::size_t operator()(const open3d::core::Device& device) const {
            return std::hash<std::string>{}(device.ToString());
        }
    };
};

and, we can use the unordered_map as:

static std::unordered_map<Device, MemoryStatistics, Device::Hash>;

Done. FYI I used std::map<Device, MemoryStatistics> to have a consistent ordering during Print().

@yxlao yxlao merged commit b917b07 into master Jun 25, 2021
@yxlao yxlao deleted the stotko/memory-manager-statistics branch June 25, 2021 09:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants