diff --git a/be/src/runtime/memory/system_allocator.cpp b/be/src/runtime/memory/system_allocator.cpp index fe268ec8d383b8..78a9e7911ed1cb 100644 --- a/be/src/runtime/memory/system_allocator.cpp +++ b/be/src/runtime/memory/system_allocator.cpp @@ -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; diff --git a/be/src/vec/common/allocator.h b/be/src/vec/common/allocator.h index 5f2e642f26639a..31bbbe1cb235d8 100644 --- a/be/src/vec/common/allocator.h +++ b/be/src/vec/common/allocator.h @@ -129,7 +129,9 @@ 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); } @@ -137,9 +139,10 @@ class Allocator { } 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); @@ -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); } @@ -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); } @@ -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) @@ -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.