-
Notifications
You must be signed in to change notification settings - Fork 60
issue/513: 内存使用情况统计&&内存引用计数和内存池实现(WIP) #514
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
Draft
Ceng23333
wants to merge
2
commits into
main
Choose a base branch
from
issue/513
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,30 +1,5 @@ | ||
| #pragma once | ||
|
|
||
| #include "device.hpp" | ||
|
|
||
| #include <cstddef> | ||
| #include <functional> | ||
|
|
||
| namespace infinicore { | ||
|
|
||
| class Memory { | ||
| public: | ||
| using Deleter = std::function<void(std::byte *)>; | ||
|
|
||
| Memory(std::byte *data, size_t size, Device device, Deleter deleter, bool pin_memory = false); | ||
| ~Memory(); | ||
|
|
||
| std::byte *data(); | ||
| Device device() const; | ||
| size_t size() const; | ||
| bool is_pinned() const; | ||
|
|
||
| private: | ||
| std::byte *data_; | ||
| size_t size_; | ||
| Device device_; | ||
| Deleter deleter_; | ||
| bool is_pinned_; | ||
| }; | ||
|
|
||
| } // namespace infinicore | ||
| #include "memory/memory_block.hpp" | ||
| #include "memory/memory_pool.hpp" | ||
| #include "memory/stats.hpp" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| #pragma once | ||
|
|
||
| #include "../device.hpp" | ||
|
|
||
| #include <cstddef> | ||
| #include <functional> | ||
| #include <memory> | ||
|
|
||
| namespace infinicore { | ||
|
|
||
| class Memory { | ||
| public: | ||
| using Deleter = std::function<void(std::byte *)>; | ||
|
|
||
| Memory(std::byte *data, size_t size, Device device, Deleter deleter, bool pin_memory = false); | ||
| ~Memory(); | ||
|
|
||
| // Copy constructor and copy assignment with reference counting | ||
| Memory(const Memory& other); | ||
| Memory& operator=(const Memory& other); | ||
|
|
||
| // Move constructor and move assignment | ||
| Memory(Memory&& other) noexcept; | ||
| Memory& operator=(Memory&& other) noexcept; | ||
|
|
||
| std::byte *data() const; | ||
| Device device() const; | ||
| size_t size() const; | ||
| bool is_pinned() const; | ||
|
|
||
| private: | ||
| std::byte *data_; | ||
| size_t size_; | ||
| Device device_; | ||
| Deleter deleter_; | ||
| bool is_pinned_; | ||
| }; | ||
|
|
||
| } // namespace infinicore |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| #pragma once | ||
|
|
||
| #include <memory> | ||
| #include <unordered_map> | ||
| #include <mutex> | ||
| #include <atomic> | ||
| #include <cstddef> | ||
| #include <functional> | ||
|
|
||
| namespace infinicore { | ||
|
|
||
| struct MemoryInfo { | ||
| std::byte* ptr; | ||
| size_t size; | ||
| std::atomic<int> ref_count; | ||
| bool is_freed; | ||
|
|
||
| MemoryInfo(std::byte* p, size_t s) | ||
| : ptr(p), size(s), ref_count(1), is_freed(false) {} | ||
| }; | ||
|
|
||
| class MemoryPool { | ||
| public: | ||
| static MemoryPool& instance(); | ||
|
|
||
| // Register a memory allocation | ||
| void registerMemory(std::byte* ptr, size_t size); | ||
|
|
||
| // Increment reference count | ||
| void addRef(std::byte* ptr); | ||
|
|
||
| // Decrement reference count and potentially free memory | ||
| void releaseMemory(std::byte* ptr, std::function<void(std::byte*)> actual_deleter); | ||
|
|
||
| // Get reference count | ||
| int getRefCount(std::byte* ptr) const; | ||
|
|
||
| // Check if memory is registered | ||
| bool isRegistered(std::byte* ptr) const; | ||
|
|
||
| // Check if memory is already freed | ||
| bool isFreed(std::byte* ptr) const; | ||
|
|
||
| private: | ||
| MemoryPool() = default; | ||
| ~MemoryPool() = default; | ||
|
|
||
| mutable std::mutex mutex_; | ||
| std::unordered_map<std::byte*, std::shared_ptr<MemoryInfo>> memory_map_; | ||
| }; | ||
|
|
||
| } // namespace infinicore | ||
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,11 +9,40 @@ DeviceCachingAllocator::DeviceCachingAllocator(Device device) : MemoryAllocator( | |
|
|
||
| std::byte *DeviceCachingAllocator::allocate(size_t size) { | ||
| void *ptr = nullptr; | ||
| spdlog::debug("DeviceCachingAllocator::allocate() called for size={}", size); | ||
|
|
||
| // Update statistics before allocation | ||
| stats_.allocation[static_cast<size_t>(StatType::AGGREGATE)].increase(1); | ||
| stats_.requested_bytes[static_cast<size_t>(StatType::AGGREGATE)].increase(size); | ||
|
|
||
| INFINICORE_CHECK_ERROR(infinirtMallocAsync(&ptr, size, context::getStream())); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 你这不还是用的原生的mallocAsync接口,那你的内存池用哪了? |
||
|
|
||
| // Update statistics after successful allocation | ||
| stats_.segment[static_cast<size_t>(StatType::AGGREGATE)].increase(1); | ||
| stats_.allocated_bytes[static_cast<size_t>(StatType::AGGREGATE)].increase(size); | ||
| stats_.reserved_bytes[static_cast<size_t>(StatType::AGGREGATE)].increase(size); | ||
| stats_.active[static_cast<size_t>(StatType::AGGREGATE)].increase(1); | ||
| stats_.active_bytes[static_cast<size_t>(StatType::AGGREGATE)].increase(size); | ||
| stats_.num_device_alloc++; | ||
|
|
||
| spdlog::debug("DeviceCachingAllocator::allocate() returned memory={}", static_cast<void *>(ptr)); | ||
| return (std::byte *)ptr; | ||
| } | ||
|
|
||
| void DeviceCachingAllocator::deallocate(std::byte *ptr) { | ||
| spdlog::debug("DeviceCachingAllocator::deallocate() called for memory={}", static_cast<void *>(ptr)); | ||
|
|
||
| // Update statistics before deallocation | ||
| stats_.active[static_cast<size_t>(StatType::AGGREGATE)].decrease(1); | ||
| // Note: We don't know the exact size being deallocated here, so we can't update | ||
| // active_bytes, allocated_bytes, etc. This is a limitation of the current design. | ||
| // In a more sophisticated implementation, we would track the size of each allocation. | ||
|
|
||
| INFINICORE_CHECK_ERROR(infinirtFreeAsync(ptr, context::getStream())); | ||
|
|
||
| // Update statistics after successful deallocation | ||
| stats_.num_device_free++; | ||
|
|
||
| spdlog::debug("DeviceCachingAllocator::deallocate() returned"); | ||
| } | ||
| } // namespace infinicore | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
include目录下都是会暴露给外部的接口,这个设计是要把memory pool的接口暴露给外部吗?什么情况下用户或者上层框架需要直接和memory pool交互?