Skip to content
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
6 changes: 4 additions & 2 deletions be/src/runtime/memory/system_allocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@ uint8_t* SystemAllocator::allocate_via_malloc(size_t length) {
int res = posix_memalign(&ptr, PAGE_SIZE, length);
if (res != 0) {
char buf[64];
LOG(ERROR) << "fail to allocate mem via posix_memalign, res=" << res
<< ", errmsg=" << strerror_r(res, buf, 64);
auto err = fmt::format("fail to allocate mem via posix_memalign, res={}, errmsg={}.", res,
strerror_r(res, buf, 64));
ExecEnv::GetInstance()->process_mem_tracker()->print_log_usage(err);
LOG(ERROR) << err;
return nullptr;
}
return (uint8_t*)ptr;
Expand Down
50 changes: 30 additions & 20 deletions be/src/vec/common/allocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,17 +129,20 @@ class Allocator {
buf = mmap(get_mmap_hint(), size, PROT_READ | PROT_WRITE, mmap_flags, -1, 0);
if (MAP_FAILED == buf) {
RELEASE_THREAD_MEM_TRACKER(size);
doris::vectorized::throwFromErrno(fmt::format("Allocator: Cannot mmap {}.", size),
auto err = fmt::format("Allocator: Cannot mmap {}.", size);
doris::ExecEnv::GetInstance()->process_mem_tracker()->print_log_usage(err);
doris::vectorized::throwFromErrno(err,
doris::TStatusCode::VEC_CANNOT_ALLOCATE_MEMORY);
}

/// No need for zero-fill, because mmap guarantees it.
} else if (!doris::config::disable_chunk_allocator_in_vec && size >= CHUNK_THRESHOLD) {
doris::Chunk chunk;
if (!doris::ChunkAllocator::instance()->allocate_align(size, &chunk)) {
doris::vectorized::throwFromErrno(
fmt::format("Allocator: Cannot allocate chunk {}.", size),
doris::TStatusCode::VEC_CANNOT_ALLOCATE_MEMORY);
auto err = fmt::format("Allocator: Cannot allocate chunk {}.", size);
doris::ExecEnv::GetInstance()->process_mem_tracker()->print_log_usage(err);
doris::vectorized::throwFromErrno(err,
doris::TStatusCode::VEC_CANNOT_ALLOCATE_MEMORY);
}
buf = chunk.data;
if constexpr (clear_memory) memset(buf, 0, chunk.size);
Expand All @@ -150,18 +153,22 @@ class Allocator {
else
buf = ::malloc(size);

if (nullptr == buf)
if (nullptr == buf) {
auto err = fmt::format("Allocator: Cannot malloc {}.", size);
doris::ExecEnv::GetInstance()->process_mem_tracker()->print_log_usage(err);
doris::vectorized::throwFromErrno(
fmt::format("Allocator: Cannot malloc {}.", size),
doris::TStatusCode::VEC_CANNOT_ALLOCATE_MEMORY);
err, doris::TStatusCode::VEC_CANNOT_ALLOCATE_MEMORY);
}
} else {
buf = nullptr;
int res = posix_memalign(&buf, alignment, size);

if (0 != res)
if (0 != res) {
auto err = fmt::format("Cannot allocate memory (posix_memalign) {}.", size);
doris::ExecEnv::GetInstance()->process_mem_tracker()->print_log_usage(err);
doris::vectorized::throwFromErrno(
fmt::format("Cannot allocate memory (posix_memalign) {}.", size),
doris::TStatusCode::VEC_CANNOT_ALLOCATE_MEMORY, res);
err, doris::TStatusCode::VEC_CANNOT_ALLOCATE_MEMORY, res);
}

if constexpr (clear_memory) memset(buf, 0, size);
}
Expand All @@ -173,8 +180,9 @@ class Allocator {
void free(void* buf, size_t size) {
if (size >= MMAP_THRESHOLD) {
if (0 != munmap(buf, size)) {
doris::vectorized::throwFromErrno(fmt::format("Allocator: Cannot munmap {}.", size),
doris::TStatusCode::VEC_CANNOT_MUNMAP);
auto err = fmt::format("Allocator: Cannot munmap {}.", size);
doris::ExecEnv::GetInstance()->process_mem_tracker()->print_log_usage(err);
doris::vectorized::throwFromErrno(err, doris::TStatusCode::VEC_CANNOT_MUNMAP);
} else {
RELEASE_THREAD_MEM_TRACKER(size);
}
Expand All @@ -199,11 +207,13 @@ class Allocator {
alignment <= MALLOC_MIN_ALIGNMENT) {
/// Resize malloc'd memory region with no special alignment requirement.
void* new_buf = ::realloc(buf, new_size);
if (nullptr == new_buf)
doris::vectorized::throwFromErrno("Allocator: Cannot realloc from " +
std::to_string(old_size) + " to " +
std::to_string(new_size) + ".",
if (nullptr == new_buf) {
auto err =
fmt::format("Allocator: Cannot realloc from {} to {}.", old_size, new_size);
doris::ExecEnv::GetInstance()->process_mem_tracker()->print_log_usage(err);
doris::vectorized::throwFromErrno(err,
doris::TStatusCode::VEC_CANNOT_ALLOCATE_MEMORY);
}

buf = new_buf;
if constexpr (clear_memory)
Expand All @@ -218,10 +228,10 @@ class Allocator {
mmap_flags, -1, 0);
if (MAP_FAILED == buf) {
RELEASE_THREAD_MEM_TRACKER(new_size - old_size);
doris::vectorized::throwFromErrno("Allocator: Cannot mremap memory chunk from " +
std::to_string(old_size) + " to " +
std::to_string(new_size) + ".",
doris::TStatusCode::VEC_CANNOT_MREMAP);
auto err = fmt::format("Allocator: Cannot mremap memory chunk from {} to {}.",
old_size, new_size);
doris::ExecEnv::GetInstance()->process_mem_tracker()->print_log_usage(err);
doris::vectorized::throwFromErrno(err, doris::TStatusCode::VEC_CANNOT_MREMAP);
}

/// No need for zero-fill, because mmap guarantees it.
Expand Down