Skip to content

Commit

Permalink
2
Browse files Browse the repository at this point in the history
  • Loading branch information
xinyiZzz committed Aug 28, 2024
1 parent 0659aab commit 197ea53
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 26 deletions.
53 changes: 29 additions & 24 deletions be/src/runtime/memory/mem_tracker_limiter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ MemTrackerLimiter::~MemTrackerLimiter() {
fmt::format("mem tracker label: {}, consumption: {}, peak consumption: {}, {}.",
label(), _consumption->current_value(), _consumption->peak_value(),
mem_tracker_inaccurate_msg);
LOG(FATAL) << err_msg << print_address_sanitizers();
LOG(INFO) << err_msg << print_address_sanitizers();
}
#endif
if (ExecEnv::tracking_memory()) {
Expand All @@ -141,10 +141,10 @@ MemTrackerLimiter::~MemTrackerLimiter() {
_consumption->set(0);
#ifndef NDEBUG
} else if (!_address_sanitizers.empty()) {
LOG(INFO) << "[Address Sanitizer] consumption is 0, but address sanitizers not empty. "
<< ", mem tracker label: " << _label
<< ", peak consumption: " << _consumption->peak_value()
<< print_address_sanitizers();
LOG(FATAL) << "[Address Sanitizer] consumption is 0, but address sanitizers not empty. "
<< ", mem tracker label: " << _label
<< ", peak consumption: " << _consumption->peak_value()
<< print_address_sanitizers();
#endif
}
memory_memtrackerlimiter_cnt << -1;
Expand All @@ -156,13 +156,13 @@ void MemTrackerLimiter::add_address_sanitizers(void* buf, size_t size) {
std::lock_guard<std::mutex> l(_address_sanitizers_mtx);
auto it = _address_sanitizers.find(buf);
if (it != _address_sanitizers.end()) {
LOG(INFO) << "[Address Sanitizer] memory buf repeat add, mem tracker label: " << _label
<< ", consumption: " << _consumption->current_value()
<< ", peak consumption: " << _consumption->peak_value() << ", buf: " << buf
<< ", size: " << size << ", old buf: " << it->first
<< ", old size: " << it->second.size
<< ", new stack_trace: " << get_stack_trace(1, "DISABLED")
<< ", old stack_trace: " << it->second.stack_trace;
_error_address_sanitizers.emplace_back(
fmt::format("[Address Sanitizer] memory buf repeat add, mem tracker label: {}, "
"consumption: {}, peak consumption: {}, buf: {}, size: {}, old "
"buf: {}, old size: {}, new stack_trace: {}, old stack_trace: {}.",
_label, _consumption->current_value(), _consumption->peak_value(),
buf, size, it->first, it->second.size,
get_stack_trace(1, "FULL_WITH_INLINE"), it->second.stack_trace););
}

// if alignment not equal to 0, maybe usable_size > size.
Expand All @@ -179,32 +179,37 @@ void MemTrackerLimiter::remove_address_sanitizers(void* buf, size_t size) {
auto it = _address_sanitizers.find(buf);
if (it != _address_sanitizers.end()) {
if (it->second.size != size) {
LOG(INFO) << "[Address Sanitizer] free memory buf size inaccurate, mem tracker "
"label: "
<< _label << ", consumption: " << _consumption->current_value()
<< ", peak consumption: " << _consumption->peak_value()
<< ", buf: " << buf << ", size: " << size << ", old buf: " << it->first
<< ", old size: " << it->second.size
<< ", new stack_trace: " << get_stack_trace(1, "DISABLED")
<< ", old stack_trace: " << it->second.stack_trace;
_error_address_sanitizers.emplace_back(fmt::format(
"[Address Sanitizer] free memory buf size inaccurate, mem tracker label: "
"{}, consumption: {}, peak consumption: {}, buf: {}, size: {}, old buf: "
"{}, old size: {}, new stack_trace: {}, old stack_trace: {}.",
_label, _consumption->current_value(), _consumption->peak_value(), buf,
size, it->first, it->second.size, get_stack_trace(1, "FULL_WITH_INLINE"),
it->second.stack_trace));
}
_address_sanitizers.erase(buf);
} else {
LOG(INFO) << "[Address Sanitizer] memory buf not exist, mem tracker label: " << _label
<< ", consumption: " << _consumption->current_value()
<< ", peak consumption: " << _consumption->peak_value() << ", buf: " << buf
<< ", size: " << size << ", stack_trace: " << get_stack_trace(1, "DISABLED");
_error_address_sanitizers.emplace_back(fmt::format(
"[Address Sanitizer] memory buf not exist, mem tracker label: {}, consumption: "
"{}, peak consumption: {}, buf: {}, size: {}, stack_trace: {}.",
_label, _consumption->current_value(), _consumption->peak_value(), buf, size,
get_stack_trace(1, "FULL_WITH_INLINE")));
}
}
}

std::string MemTrackerLimiter::print_address_sanitizers() {
std::lock_guard<std::mutex> l(_address_sanitizers_mtx);
std::string detail = "[Address Sanitizer]:";
detail += "\n memory not be freed:";
for (const auto& it : _address_sanitizers) {
detail += fmt::format("\n {}, size {}, strack trace: {}", it.first, it.second.size,
it.second.stack_trace);
}
detail += "\n incorrect memory alloc and free:";
for (const auto& err_msg : _error_address_sanitizers) {
detail += fmt::format("\n {}", err_msg);
}
return detail;
}
#endif
Expand Down
1 change: 1 addition & 0 deletions be/src/runtime/memory/mem_tracker_limiter.h
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ class MemTrackerLimiter final : public MemTracker {

std::mutex _address_sanitizers_mtx;
std::unordered_map<void*, AddressSanitizer> _address_sanitizers;
std::vector<std::string> _error_address_sanitizers;
#endif
};

Expand Down
3 changes: 2 additions & 1 deletion be/src/util/faststring.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ class faststring : private Allocator<false, false, false, DefaultMemoryAllocator
OwnedSlice build() {
uint8_t* ret = data_;
if (ret == initial_data_) {
ret = reinterpret_cast<uint8_t*>(Allocator::alloc(len_));
ret = reinterpret_cast<uint8_t*>(Allocator::alloc(capacity_));
DCHECK(len_ <= capacity_);
memcpy(ret, data_, len_);
}
OwnedSlice result(ret, len_, capacity_);
Expand Down
9 changes: 8 additions & 1 deletion be/src/util/slice.h
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,14 @@ class OwnedSlice : private Allocator<false, false, false, DefaultMemoryAllocator
OwnedSlice(const OwnedSlice&) = delete;
void operator=(const OwnedSlice&) = delete;

~OwnedSlice() { Allocator::free(_slice.data, _capacity); }
~OwnedSlice() {
if (_slice.data != nullptr) {
DCHECK(_capacity != 0);
Allocator::free(_slice.data, _capacity);
} else {
DCHECK(_capacity == 0);
}
}

const Slice& slice() const { return _slice; }

Expand Down

0 comments on commit 197ea53

Please sign in to comment.