[NFC][sanitizer] Return StackDepotStats by value

Differential Revision: https://reviews.llvm.org/D110644
This commit is contained in:
Vitaly Buka 2021-09-28 11:20:18 -07:00
parent 204d301bb1
commit 7c1128f3bb
15 changed files with 39 additions and 42 deletions

View File

@ -124,9 +124,9 @@ static void PrintAccumulatedStats() {
// Use lock to keep reports from mixing up.
Lock lock(&print_lock);
stats.Print();
StackDepotStats *stack_depot_stats = StackDepotGetStats();
StackDepotStats stack_depot_stats = StackDepotGetStats();
Printf("Stats: StackDepot: %zd ids; %zdM allocated\n",
stack_depot_stats->n_uniq_ids, stack_depot_stats->allocated >> 20);
stack_depot_stats.n_uniq_ids, stack_depot_stats.allocated >> 20);
PrintInternalAllocatorStats();
}

View File

@ -141,7 +141,7 @@ static void CheckUnwind() {
static void HwasanFormatMemoryUsage(InternalScopedString &s) {
HwasanThreadList &thread_list = hwasanThreadList();
auto thread_stats = thread_list.GetThreadStats();
auto *sds = StackDepotGetStats();
auto sds = StackDepotGetStats();
AllocatorStatCounters asc;
GetAllocatorStats(asc);
s.append(
@ -151,7 +151,7 @@ static void HwasanFormatMemoryUsage(InternalScopedString &s) {
internal_getpid(), GetRSS(), thread_stats.n_live_threads,
thread_stats.total_stack_size,
thread_stats.n_live_threads * thread_list.MemoryUsedPerThread(),
sds->allocated, sds->n_uniq_ids, asc[AllocatorStatMapped]);
sds.allocated, sds.n_uniq_ids, asc[AllocatorStatMapped]);
}
#if SANITIZER_ANDROID

View File

@ -115,9 +115,9 @@ static void PrintAccumulatedStats() {
// Use lock to keep reports from mixing up.
Lock lock(&print_lock);
stats.Print();
StackDepotStats *stack_depot_stats = StackDepotGetStats();
StackDepotStats stack_depot_stats = StackDepotGetStats();
Printf("Stats: StackDepot: %zd ids; %zdM allocated\n",
stack_depot_stats->n_uniq_ids, stack_depot_stats->allocated >> 20);
stack_depot_stats.n_uniq_ids, stack_depot_stats.allocated >> 20);
PrintInternalAllocatorStats();
}

View File

@ -19,7 +19,7 @@ namespace __msan {
static ChainedOriginDepot chainedOriginDepot;
StackDepotStats *ChainedOriginDepotGetStats() {
StackDepotStats ChainedOriginDepotGetStats() {
return chainedOriginDepot.GetStats();
}

View File

@ -19,7 +19,7 @@
namespace __msan {
// Gets the statistic of the origin chain storage.
StackDepotStats *ChainedOriginDepotGetStats();
StackDepotStats ChainedOriginDepotGetStats();
// Stores a chain with StackDepot ID here_id and previous chain ID prev_id.
// If successful, returns true and the new chain id new_id.

View File

@ -122,17 +122,17 @@ void ReportStats() {
ScopedErrorReportLock l;
if (__msan_get_track_origins() > 0) {
StackDepotStats *stack_depot_stats = StackDepotGetStats();
StackDepotStats stack_depot_stats = StackDepotGetStats();
// FIXME: we want this at normal exit, too!
// FIXME: but only with verbosity=1 or something
Printf("Unique heap origins: %zu\n", stack_depot_stats->n_uniq_ids);
Printf("Stack depot allocated bytes: %zu\n", stack_depot_stats->allocated);
Printf("Unique heap origins: %zu\n", stack_depot_stats.n_uniq_ids);
Printf("Stack depot allocated bytes: %zu\n", stack_depot_stats.allocated);
StackDepotStats *chained_origin_depot_stats = ChainedOriginDepotGetStats();
StackDepotStats chained_origin_depot_stats = ChainedOriginDepotGetStats();
Printf("Unique origin histories: %zu\n",
chained_origin_depot_stats->n_uniq_ids);
chained_origin_depot_stats.n_uniq_ids);
Printf("History depot allocated bytes: %zu\n",
chained_origin_depot_stats->allocated);
chained_origin_depot_stats.allocated);
}
}

View File

@ -85,7 +85,9 @@ ChainedOriginDepot::ChainedOriginDepotNode::get_handle() {
ChainedOriginDepot::ChainedOriginDepot() {}
StackDepotStats *ChainedOriginDepot::GetStats() { return depot.GetStats(); }
StackDepotStats ChainedOriginDepot::GetStats() const {
return depot.GetStats();
}
bool ChainedOriginDepot::Put(u32 here_id, u32 prev_id, u32 *new_id) {
ChainedOriginDepotDesc desc = {here_id, prev_id};

View File

@ -22,7 +22,7 @@ class ChainedOriginDepot {
ChainedOriginDepot();
// Gets the statistic of the origin chain storage.
StackDepotStats *GetStats();
StackDepotStats GetStats() const;
// Stores a chain with StackDepot ID here_id and previous chain ID prev_id.
// If successful, returns true and the new chain id new_id.

View File

@ -26,9 +26,7 @@ void SetSoftRssLimitExceededCallback(void (*Callback)(bool exceeded)) {
#if (SANITIZER_LINUX || SANITIZER_NETBSD) && !SANITIZER_GO
// Weak default implementation for when sanitizer_stackdepot is not linked in.
SANITIZER_WEAK_ATTRIBUTE StackDepotStats *StackDepotGetStats() {
return nullptr;
}
SANITIZER_WEAK_ATTRIBUTE StackDepotStats StackDepotGetStats() { return {}; }
void *BackgroundThread(void *arg) {
const uptr hard_rss_limit_mb = common_flags()->hard_rss_limit_mb;
@ -48,15 +46,12 @@ void *BackgroundThread(void *arg) {
prev_reported_rss = current_rss_mb;
}
// If stack depot has grown 10% since last time, print it too.
StackDepotStats *stack_depot_stats = StackDepotGetStats();
if (stack_depot_stats) {
if (prev_reported_stack_depot_size * 11 / 10 <
stack_depot_stats->allocated) {
Printf("%s: StackDepot: %zd ids; %zdM allocated\n", SanitizerToolName,
stack_depot_stats->n_uniq_ids,
stack_depot_stats->allocated >> 20);
prev_reported_stack_depot_size = stack_depot_stats->allocated;
}
StackDepotStats stack_depot_stats = StackDepotGetStats();
if (prev_reported_stack_depot_size * 11 / 10 <
stack_depot_stats.allocated) {
Printf("%s: StackDepot: %zd ids; %zdM allocated\n", SanitizerToolName,
stack_depot_stats.n_uniq_ids, stack_depot_stats.allocated >> 20);
prev_reported_stack_depot_size = stack_depot_stats.allocated;
}
}
// Check RSS against the limit.

View File

@ -90,7 +90,7 @@ typedef StackDepotBase<StackDepotNode, 1, StackDepotNode::kTabSizeLog>
StackDepot;
static StackDepot theDepot;
StackDepotStats *StackDepotGetStats() { return theDepot.GetStats(); }
StackDepotStats StackDepotGetStats() { return theDepot.GetStats(); }
u32 StackDepotPut(StackTrace stack) {
StackDepotHandle h = theDepot.Put(stack);
@ -126,7 +126,7 @@ bool StackDepotReverseMap::IdDescPair::IdComparator(
}
StackDepotReverseMap::StackDepotReverseMap() {
map_.reserve(StackDepotGetStats()->n_uniq_ids + 100);
map_.reserve(StackDepotGetStats().n_uniq_ids + 100);
for (int idx = 0; idx < StackDepot::kTabSize; idx++) {
atomic_uintptr_t *p = &theDepot.tab[idx];
uptr v = atomic_load(p, memory_order_consume);

View File

@ -33,7 +33,7 @@ struct StackDepotHandle {
const int kStackDepotMaxUseCount = 1U << (SANITIZER_ANDROID ? 16 : 20);
StackDepotStats *StackDepotGetStats();
StackDepotStats StackDepotGetStats();
u32 StackDepotPut(StackTrace stack);
StackDepotHandle StackDepotPut_WithHandle(StackTrace stack);
// Retrieves a stored stack trace by the id.

View File

@ -32,7 +32,7 @@ class StackDepotBase {
// Retrieves a stored stack trace by the id.
args_type Get(u32 id);
StackDepotStats *GetStats() { return &stats; }
StackDepotStats GetStats() const { return stats; }
void LockAll();
void UnlockAll();

View File

@ -68,21 +68,21 @@ TEST(SanitizerCommon, ChainedOriginDepotDifferent) {
}
TEST(SanitizerCommon, ChainedOriginDepotStats) {
StackDepotStats stats0 = *chainedOriginDepot.GetStats();
StackDepotStats stats0 = chainedOriginDepot.GetStats();
u32 new_id;
EXPECT_TRUE(chainedOriginDepot.Put(33, 34, &new_id));
StackDepotStats stats1 = *chainedOriginDepot.GetStats();
StackDepotStats stats1 = chainedOriginDepot.GetStats();
EXPECT_EQ(stats1.n_uniq_ids, stats0.n_uniq_ids + 1);
EXPECT_GT(stats1.allocated, stats0.allocated);
EXPECT_FALSE(chainedOriginDepot.Put(33, 34, &new_id));
StackDepotStats stats2 = *chainedOriginDepot.GetStats();
StackDepotStats stats2 = chainedOriginDepot.GetStats();
EXPECT_EQ(stats2.n_uniq_ids, stats1.n_uniq_ids);
EXPECT_EQ(stats2.allocated, stats1.allocated);
EXPECT_TRUE(chainedOriginDepot.Put(35, 36, &new_id));
StackDepotStats stats3 = *chainedOriginDepot.GetStats();
StackDepotStats stats3 = chainedOriginDepot.GetStats();
EXPECT_EQ(stats3.n_uniq_ids, stats2.n_uniq_ids + 1);
EXPECT_GT(stats3.allocated, stats2.allocated);
}

View File

@ -124,13 +124,13 @@ void WriteMemoryProfile(char *buf, uptr buf_size, u64 uptime_ns) {
internal_memset(mem, 0, sizeof(mem));
GetMemoryProfile(FillProfileCallback, mem, MemCount);
auto meta = ctx->metamap.GetMemoryStats();
StackDepotStats *stacks = StackDepotGetStats();
StackDepotStats stacks = StackDepotGetStats();
uptr nthread, nlive;
ctx->thread_registry.GetNumberOfThreads(&nthread, &nlive);
uptr internal_stats[AllocatorStatCount];
internal_allocator()->GetStats(internal_stats);
// All these are allocated from the common mmap region.
mem[MemMmap] -= meta.mem_block + meta.sync_obj + stacks->allocated +
mem[MemMmap] -= meta.mem_block + meta.sync_obj + stacks.allocated +
internal_stats[AllocatorStatMapped];
if (s64(mem[MemMmap]) < 0)
mem[MemMmap] = 0;
@ -143,8 +143,8 @@ void WriteMemoryProfile(char *buf, uptr buf_size, u64 uptime_ns) {
mem[MemShadow] >> 20, mem[MemMeta] >> 20, mem[MemFile] >> 20,
mem[MemMmap] >> 20, mem[MemTrace] >> 20, mem[MemHeap] >> 20,
mem[MemOther] >> 20, internal_stats[AllocatorStatMapped] >> 20,
meta.mem_block >> 20, meta.sync_obj >> 20, stacks->allocated >> 20,
stacks->n_uniq_ids, nlive, nthread);
meta.mem_block >> 20, meta.sync_obj >> 20, stacks.allocated >> 20,
stacks.n_uniq_ids, nlive, nthread);
}
# if SANITIZER_LINUX

View File

@ -159,7 +159,7 @@ void WriteMemoryProfile(char *buf, uptr buf_size, u64 uptime_ns) {
RegionMemUsage(LoAppMemBeg(), LoAppMemEnd(), &app_res, &app_dirty);
#endif
StackDepotStats *stacks = StackDepotGetStats();
StackDepotStats stacks = StackDepotGetStats();
uptr nthread, nlive;
ctx->thread_registry.GetNumberOfThreads(&nthread, &nlive);
internal_snprintf(
@ -187,7 +187,7 @@ void WriteMemoryProfile(char *buf, uptr buf_size, u64 uptime_ns) {
# else // !SANITIZER_GO
LoAppMemBeg(), LoAppMemEnd(), app_res / 1024, app_dirty / 1024,
# endif
stacks->n_uniq_ids, stacks->allocated / 1024, nthread, nlive);
stacks.n_uniq_ids, stacks.allocated / 1024, nthread, nlive);
}
# if !SANITIZER_GO