[compiler-rt] 7c1128f - [NFC][sanitizer] Return StackDepotStats by value

Vitaly Buka via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 28 15:42:30 PDT 2021


Author: Vitaly Buka
Date: 2021-09-28T15:42:21-07:00
New Revision: 7c1128f3bb6451e2bdced8f3f83f571c52e2a849

URL: https://github.com/llvm/llvm-project/commit/7c1128f3bb6451e2bdced8f3f83f571c52e2a849
DIFF: https://github.com/llvm/llvm-project/commit/7c1128f3bb6451e2bdced8f3f83f571c52e2a849.diff

LOG: [NFC][sanitizer] Return StackDepotStats by value

Differential Revision: https://reviews.llvm.org/D110644

Added: 
    

Modified: 
    compiler-rt/lib/asan/asan_stats.cpp
    compiler-rt/lib/hwasan/hwasan.cpp
    compiler-rt/lib/memprof/memprof_stats.cpp
    compiler-rt/lib/msan/msan_chained_origin_depot.cpp
    compiler-rt/lib/msan/msan_chained_origin_depot.h
    compiler-rt/lib/msan/msan_report.cpp
    compiler-rt/lib/sanitizer_common/sanitizer_chained_origin_depot.cpp
    compiler-rt/lib/sanitizer_common/sanitizer_chained_origin_depot.h
    compiler-rt/lib/sanitizer_common/sanitizer_common_libcdep.cpp
    compiler-rt/lib/sanitizer_common/sanitizer_stackdepot.cpp
    compiler-rt/lib/sanitizer_common/sanitizer_stackdepot.h
    compiler-rt/lib/sanitizer_common/sanitizer_stackdepotbase.h
    compiler-rt/lib/sanitizer_common/tests/sanitizer_chained_origin_depot_test.cpp
    compiler-rt/lib/tsan/rtl/tsan_platform_linux.cpp
    compiler-rt/lib/tsan/rtl/tsan_platform_mac.cpp

Removed: 
    


################################################################################
diff  --git a/compiler-rt/lib/asan/asan_stats.cpp b/compiler-rt/lib/asan/asan_stats.cpp
index 4182761083337..9a715ea76fee7 100644
--- a/compiler-rt/lib/asan/asan_stats.cpp
+++ b/compiler-rt/lib/asan/asan_stats.cpp
@@ -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();
 }
 

diff  --git a/compiler-rt/lib/hwasan/hwasan.cpp b/compiler-rt/lib/hwasan/hwasan.cpp
index 465419022123f..e8ffbbd6f48de 100644
--- a/compiler-rt/lib/hwasan/hwasan.cpp
+++ b/compiler-rt/lib/hwasan/hwasan.cpp
@@ -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

diff  --git a/compiler-rt/lib/memprof/memprof_stats.cpp b/compiler-rt/lib/memprof/memprof_stats.cpp
index a23e69baa6e25..c8faebfa12de1 100644
--- a/compiler-rt/lib/memprof/memprof_stats.cpp
+++ b/compiler-rt/lib/memprof/memprof_stats.cpp
@@ -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();
 }
 

diff  --git a/compiler-rt/lib/msan/msan_chained_origin_depot.cpp b/compiler-rt/lib/msan/msan_chained_origin_depot.cpp
index 5dee80fd4692f..49b14131a89be 100644
--- a/compiler-rt/lib/msan/msan_chained_origin_depot.cpp
+++ b/compiler-rt/lib/msan/msan_chained_origin_depot.cpp
@@ -19,7 +19,7 @@ namespace __msan {
 
 static ChainedOriginDepot chainedOriginDepot;
 
-StackDepotStats *ChainedOriginDepotGetStats() {
+StackDepotStats ChainedOriginDepotGetStats() {
   return chainedOriginDepot.GetStats();
 }
 

diff  --git a/compiler-rt/lib/msan/msan_chained_origin_depot.h b/compiler-rt/lib/msan/msan_chained_origin_depot.h
index 60ab182fa4c86..ea51c77a905b5 100644
--- a/compiler-rt/lib/msan/msan_chained_origin_depot.h
+++ b/compiler-rt/lib/msan/msan_chained_origin_depot.h
@@ -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.

diff  --git a/compiler-rt/lib/msan/msan_report.cpp b/compiler-rt/lib/msan/msan_report.cpp
index ea8608e569e79..3a7a237dea070 100644
--- a/compiler-rt/lib/msan/msan_report.cpp
+++ b/compiler-rt/lib/msan/msan_report.cpp
@@ -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);
   }
 }
 

diff  --git a/compiler-rt/lib/sanitizer_common/sanitizer_chained_origin_depot.cpp b/compiler-rt/lib/sanitizer_common/sanitizer_chained_origin_depot.cpp
index 250ac39e13016..57e0a6e27cd7d 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_chained_origin_depot.cpp
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_chained_origin_depot.cpp
@@ -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};

diff  --git a/compiler-rt/lib/sanitizer_common/sanitizer_chained_origin_depot.h b/compiler-rt/lib/sanitizer_common/sanitizer_chained_origin_depot.h
index 453cdf6b54498..c1302a0abb8ce 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_chained_origin_depot.h
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_chained_origin_depot.h
@@ -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.

diff  --git a/compiler-rt/lib/sanitizer_common/sanitizer_common_libcdep.cpp b/compiler-rt/lib/sanitizer_common/sanitizer_common_libcdep.cpp
index 881652558a867..bc4b477e350ff 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_common_libcdep.cpp
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_common_libcdep.cpp
@@ -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.

diff  --git a/compiler-rt/lib/sanitizer_common/sanitizer_stackdepot.cpp b/compiler-rt/lib/sanitizer_common/sanitizer_stackdepot.cpp
index 4f8c1d9147e67..e47127e04a7a1 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_stackdepot.cpp
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_stackdepot.cpp
@@ -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);

diff  --git a/compiler-rt/lib/sanitizer_common/sanitizer_stackdepot.h b/compiler-rt/lib/sanitizer_common/sanitizer_stackdepot.h
index 0e26c1fc37c49..937c1a446b1ea 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_stackdepot.h
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_stackdepot.h
@@ -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.

diff  --git a/compiler-rt/lib/sanitizer_common/sanitizer_stackdepotbase.h b/compiler-rt/lib/sanitizer_common/sanitizer_stackdepotbase.h
index 1af2c1892eff7..592df4b5d47f6 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_stackdepotbase.h
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_stackdepotbase.h
@@ -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();

diff  --git a/compiler-rt/lib/sanitizer_common/tests/sanitizer_chained_origin_depot_test.cpp b/compiler-rt/lib/sanitizer_common/tests/sanitizer_chained_origin_depot_test.cpp
index df3c9db828064..f4951c677f905 100644
--- a/compiler-rt/lib/sanitizer_common/tests/sanitizer_chained_origin_depot_test.cpp
+++ b/compiler-rt/lib/sanitizer_common/tests/sanitizer_chained_origin_depot_test.cpp
@@ -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);
 }

diff  --git a/compiler-rt/lib/tsan/rtl/tsan_platform_linux.cpp b/compiler-rt/lib/tsan/rtl/tsan_platform_linux.cpp
index c4545130d3cad..2fb753dd080c8 100644
--- a/compiler-rt/lib/tsan/rtl/tsan_platform_linux.cpp
+++ b/compiler-rt/lib/tsan/rtl/tsan_platform_linux.cpp
@@ -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

diff  --git a/compiler-rt/lib/tsan/rtl/tsan_platform_mac.cpp b/compiler-rt/lib/tsan/rtl/tsan_platform_mac.cpp
index 1fe646fa7bcbf..388b3836d7d14 100644
--- a/compiler-rt/lib/tsan/rtl/tsan_platform_mac.cpp
+++ b/compiler-rt/lib/tsan/rtl/tsan_platform_mac.cpp
@@ -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


        


More information about the llvm-commits mailing list