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

[fix] unmap reserved address #962

Merged
merged 1 commit into from
Aug 8, 2024
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
19 changes: 12 additions & 7 deletions src/ppl/nn/utils/buffered_cpu_allocator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,22 @@ namespace ppl { namespace nn { namespace utils {
static constexpr uint32_t g_max_msg_buf_size = 1024;
#endif

BufferedCpuAllocator::BufferedCpuAllocator() {
#ifdef _MSC_VER
base_ = nullptr;
#else
base_ = MAP_FAILED;
#endif
}

BufferedCpuAllocator::~BufferedCpuAllocator() {
#ifdef _MSC_VER
if (base_) {
VirtualFree(base_, 0, MEM_RELEASE);
}
#else
if (base_ != MAP_FAILED) {
munmap(base_, (char*)cursor_ - (char*)base_);
munmap(base_, addr_len_);
}
#endif
}
Expand Down Expand Up @@ -75,12 +83,9 @@ RetCode BufferedCpuAllocator::Init(uint64_t max_mem_bytes) {
LOG(ERROR) << "VirtualAlloc reserve [" << max_mem_bytes << "] bytes failed: " << errmsg;
return RC_OTHER_ERROR;
}

addr_len_ = max_mem_bytes;
LOG(DEBUG) << "reserved [" << max_mem_bytes << "] bytes of virtual address from [" << base_ << "].";
#else
{
auto totalram = sysconf(_SC_PAGE_SIZE) * sysconf(_SC_PHYS_PAGES);
uint64_t totalram = sysconf(_SC_PAGE_SIZE) * sysconf(_SC_PHYS_PAGES);
if (max_mem_bytes > totalram) {
max_mem_bytes = totalram;
}
Expand All @@ -91,12 +96,12 @@ RetCode BufferedCpuAllocator::Init(uint64_t max_mem_bytes) {
LOG(ERROR) << "mmap reserve [" << max_mem_bytes << "] bytes failed: " << strerror(errno);
return RC_OTHER_ERROR;
}
#endif

cursor_ = base_;
addr_len_ = max_mem_bytes;
LOG(DEBUG) << "reserved [" << max_mem_bytes << "] bytes of virtual address from [" << base_ << "].";
#endif

cursor_ = base_;
return RC_SUCCESS;
}

Expand Down
2 changes: 1 addition & 1 deletion src/ppl/nn/utils/buffered_cpu_allocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace ppl { namespace nn { namespace utils {

class BufferedCpuAllocator final : public ppl::common::CompactAddrManager::VMAllocator {
public:
BufferedCpuAllocator() {}
BufferedCpuAllocator();
~BufferedCpuAllocator();
ppl::common::RetCode Init(uint64_t max_mem_bytes = UINT64_MAX);
uint64_t Extend(uint64_t bytes) override;
Expand Down
Loading