[memprof] Simplify initialized flags.
authorSnehasish Kumar <snehasishk@google.com>
Tue, 7 Mar 2023 23:38:51 +0000 (23:38 +0000)
committerSnehasish Kumar <snehasishk@google.com>
Wed, 8 Mar 2023 18:02:23 +0000 (18:02 +0000)
As discussed in D145428, the memprof_init_is_running check can be moved
to the end of the initialization routine to avoid intercepting
allocations during initialization. Also, the memprof_init_done flag can
be removed and replaced with memprof_inited. Finally, memprof_inited can
also be moved to the end of the method.

Tested on the existing check-memprof tests; memprof profile collection
succeeded on a large internal workload.

Reviewed By: tejohnson

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

compiler-rt/lib/memprof/memprof_allocator.cpp
compiler-rt/lib/memprof/memprof_internal.h
compiler-rt/lib/memprof/memprof_rtl.cpp

index c21e4e8..6e3fa7f 100644 (file)
@@ -75,7 +75,7 @@ static int GetCpuId(void) {
   // _memprof_preinit is called via the preinit_array, which subsequently calls
   // malloc. Since this is before _dl_init calls VDSO_SETUP, sched_getcpu
   // will seg fault as the address of __vdso_getcpu will be null.
-  if (!memprof_init_done)
+  if (!memprof_inited)
     return -1;
   return sched_getcpu();
 }
@@ -445,8 +445,7 @@ struct Allocator {
 
     u64 user_requested_size =
         atomic_exchange(&m->user_requested_size, 0, memory_order_acquire);
-    if (memprof_inited && memprof_init_done &&
-        atomic_load_relaxed(&constructed) &&
+    if (memprof_inited && atomic_load_relaxed(&constructed) &&
         !atomic_load_relaxed(&destructing)) {
       u64 c = GetShadowCount(p, user_requested_size);
       long curtime = GetTimestamp();
index bba465e..990e62c 100644 (file)
@@ -76,7 +76,6 @@ void *MemprofDlSymNext(const char *sym);
 
 extern int memprof_inited;
 extern int memprof_timestamp_inited;
-extern int memprof_init_done;
 // Used to avoid infinite recursion in __memprof_init().
 extern bool memprof_init_is_running;
 extern void (*death_callback)(void);
index d30b803..5e2e7bc 100644 (file)
@@ -65,7 +65,6 @@ static void CheckUnwind() {
 
 // -------------------------- Globals --------------------- {{{1
 int memprof_inited;
-int memprof_init_done;
 bool memprof_init_is_running;
 int memprof_timestamp_inited;
 long memprof_init_timestamp_s;
@@ -195,11 +194,6 @@ static void MemprofInitInternal() {
 
   InitializeAllocator();
 
-  // On Linux MemprofThread::ThreadStart() calls malloc() that's why
-  // memprof_inited should be set to 1 prior to initializing the threads.
-  memprof_inited = 1;
-  memprof_init_is_running = false;
-
   if (flags()->atexit)
     Atexit(memprof_atexit);
 
@@ -218,7 +212,8 @@ static void MemprofInitInternal() {
 
   VReport(1, "MemProfiler Init done\n");
 
-  memprof_init_done = 1;
+  memprof_init_is_running = false;
+  memprof_inited = 1;
 }
 
 void MemprofInitTime() {