Skip to content

Commit

Permalink
Add metric to cacth the double free error in mmap allocator
Browse files Browse the repository at this point in the history
Currently we only have a warning log for double free in mmap allocator
and we shall also add a metric for production alert.
  • Loading branch information
xiaoxmeng committed Apr 9, 2024
1 parent e29cde7 commit eaf3afe
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
4 changes: 4 additions & 0 deletions velox/common/base/Counters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@ void registerVeloxMetrics() {
DEFINE_METRIC(
kMetricMemoryPoolReservationLeakBytes, facebook::velox::StatType::SUM);

// Tracks the count of double frees in memory allocator.
DEFINE_METRIC(
kMetricMemoryAllocatorDoubleFreeCount, facebook::velox::StatType::COUNT);

/// ================== Spill related Counters =================

// The number of bytes in memory to spill.
Expand Down
3 changes: 3 additions & 0 deletions velox/common/base/Counters.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ constexpr folly::StringPiece kMetricMemoryPoolUsageLeakBytes{
constexpr folly::StringPiece kMetricMemoryPoolReservationLeakBytes{
"velox.memory_pool_reservation_leak_bytes"};

constexpr folly::StringPiece kMetricMemoryAllocatorDoubleFreeCount{
"velox.memory_allocator_double_free_count"};

constexpr folly::StringPiece kMetricArbitratorRequestsCount{
"velox.arbitrator_requests_count"};

Expand Down
6 changes: 4 additions & 2 deletions velox/common/memory/MmapAllocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@

#include <sys/mman.h>

#include "velox/common/base/Counters.h"
#include "velox/common/base/Portability.h"
#include "velox/common/base/StatsReporter.h"
#include "velox/common/memory/Memory.h"

namespace facebook::velox::memory {
Expand Down Expand Up @@ -877,10 +879,10 @@ MachinePageCount MmapAllocator::SizeClass::free(Allocation& allocation) {
const int firstBit =
(runAddress - address_) / (AllocationTraits::kPageSize * unitSize_);
for (auto page = firstBit; page < firstBit + numPages; ++page) {
if (!bits::isBitSet(pageAllocated_.data(), page)) {
// TODO: change this to a velox failure to catch the bug.
if (FOLLY_UNLIKELY(!bits::isBitSet(pageAllocated_.data(), page))) {
VELOX_MEM_LOG(ERROR)
<< "Double free: page = " << page << " sizeclass = " << unitSize_;
RECORD_METRIC_VALUE(kMetricMemoryAllocatorDoubleFreeCount);
continue;
}
if (bits::isBitSet(pageMapped_.data(), page)) {
Expand Down

0 comments on commit eaf3afe

Please sign in to comment.