Skip to content

Commit c780fc2

Browse files
committed
Improve MEMPROF size tracking.
GitOrigin-RevId: 3f7a82afbc464edb0dc3ad7b27bdd7e4e1de25cc
1 parent b6c915c commit c780fc2

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

‎memprof/memprof.cpp

+15-8
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
#if (TD_DARWIN || TD_LINUX) && defined(USE_MEMPROF)
1212
#include <algorithm>
1313
#include <atomic>
14-
#include <cassert>
1514
#include <cstddef>
1615
#include <cstdint>
1716
#include <cstdlib>
@@ -34,6 +33,11 @@ double get_fast_backtrace_success_rate() {
3433
}
3534
#else
3635

36+
#define my_assert(f) \
37+
if (!(f)) { \
38+
std::abort(); \
39+
}
40+
3741
#if TD_LINUX
3842
extern void *__libc_stack_end;
3943
#endif
@@ -156,7 +160,7 @@ std::int32_t get_ht_pos(const Backtrace &bt, bool force = false) {
156160
if (pos_hash == 0) {
157161
if (ht_size > HT_MAX_SIZE / 2) {
158162
if (force) {
159-
assert(ht_size * 10 < HT_MAX_SIZE * 7);
163+
my_assert(ht_size * 10 < HT_MAX_SIZE * 7);
160164
} else {
161165
Backtrace unknown_bt{{nullptr}};
162166
unknown_bt[0] = reinterpret_cast<void *>(1);
@@ -188,18 +192,21 @@ std::int32_t get_ht_pos(const Backtrace &bt, bool force = false) {
188192

189193
void dump_alloc(const std::function<void(const AllocInfo &)> &func) {
190194
for (auto &node : ht) {
191-
if (node.size == 0) {
195+
auto size = node.size.load(std::memory_order_relaxed);
196+
if (size == 0) {
192197
continue;
193198
}
194-
func(AllocInfo{node.backtrace, node.size.load()});
199+
func(AllocInfo{node.backtrace, size});
195200
}
196201
}
197202

198203
void register_xalloc(malloc_info *info, std::int32_t diff) {
204+
my_assert(info->size >= 0);
199205
if (diff > 0) {
200-
ht[info->ht_pos].size += info->size;
206+
ht[info->ht_pos].size.fetch_add(info->size, std::memory_order_relaxed);
201207
} else {
202-
ht[info->ht_pos].size -= info->size;
208+
auto old_value = ht[info->ht_pos].size.fetch_sub(info->size, std::memory_order_relaxed);
209+
my_assert(old_value >= static_cast<std::size_t>(info->size));
203210
}
204211
}
205212

@@ -234,7 +241,7 @@ static malloc_info *get_info(void *data_void) {
234241
auto *buf = data - RESERVED_SIZE;
235242

236243
auto *info = reinterpret_cast<malloc_info *>(buf);
237-
assert(info->magic == MALLOC_INFO_MAGIC);
244+
my_assert(info->magic == MALLOC_INFO_MAGIC);
238245
return info;
239246
}
240247

@@ -276,7 +283,7 @@ void *realloc(void *ptr, std::size_t size) {
276283
return new_ptr;
277284
}
278285
void *memalign(std::size_t aligment, std::size_t size) {
279-
assert(false && "Memalign is unsupported");
286+
my_assert(false && "Memalign is unsupported");
280287
return nullptr;
281288
}
282289
}

0 commit comments

Comments
 (0)