[compiler-rt] 25dade5 - [profile] Decommit memory after counter relocation

Petr Hosek via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 15 22:49:51 PDT 2021


Author: Petr Hosek
Date: 2021-07-15T22:49:21-07:00
New Revision: 25dade54d3ece60dc9fb8f9960e4a558e85f7ae9

URL: https://github.com/llvm/llvm-project/commit/25dade54d3ece60dc9fb8f9960e4a558e85f7ae9
DIFF: https://github.com/llvm/llvm-project/commit/25dade54d3ece60dc9fb8f9960e4a558e85f7ae9.diff

LOG: [profile] Decommit memory after counter relocation

After we relocate counters, we no longer need to keep the original copy
around so we can return the memory back to the operating system.

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

Added: 
    

Modified: 
    compiler-rt/lib/profile/InstrProfilingFile.c
    compiler-rt/lib/profile/InstrProfilingPlatformFuchsia.c
    compiler-rt/lib/profile/InstrProfilingUtil.c
    compiler-rt/lib/profile/InstrProfilingUtil.h
    compiler-rt/lib/profile/WindowsMMap.c
    compiler-rt/lib/profile/WindowsMMap.h

Removed: 
    


################################################################################
diff  --git a/compiler-rt/lib/profile/InstrProfilingFile.c b/compiler-rt/lib/profile/InstrProfilingFile.c
index 420e8246f4337..ec12f2ad9fb56 100644
--- a/compiler-rt/lib/profile/InstrProfilingFile.c
+++ b/compiler-rt/lib/profile/InstrProfilingFile.c
@@ -490,6 +490,8 @@ static void relocateCounters(void) {
    * __llvm_profile_get_size_for_buffer(). */
   const __llvm_profile_data *DataBegin = __llvm_profile_begin_data();
   const __llvm_profile_data *DataEnd = __llvm_profile_end_data();
+  const uint64_t *CountersBegin = __llvm_profile_begin_counters();
+  const uint64_t *CountersEnd = __llvm_profile_end_counters();
   uint64_t DataSize = __llvm_profile_get_data_size(DataBegin, DataEnd);
   const uint64_t CountersOffset = sizeof(__llvm_profile_header) +
       (DataSize * sizeof(__llvm_profile_data));
@@ -542,8 +544,11 @@ static void relocateCounters(void) {
   }
 
   /* Update the profile fields based on the current mapping. */
-  __llvm_profile_counter_bias = (intptr_t)Profile -
-      (uintptr_t)__llvm_profile_begin_counters() + CountersOffset;
+  __llvm_profile_counter_bias =
+      (intptr_t)Profile - (uintptr_t)CountersBegin + CountersOffset;
+
+  /* Return the memory allocated for counters to OS. */
+  lprofReleaseMemoryPagesToOS((uintptr_t)CountersBegin, (uintptr_t)CountersEnd);
 }
 
 static void initializeProfileForContinuousMode(void) {

diff  --git a/compiler-rt/lib/profile/InstrProfilingPlatformFuchsia.c b/compiler-rt/lib/profile/InstrProfilingPlatformFuchsia.c
index 8bd5e969aa50c..368c0f81d7f0a 100644
--- a/compiler-rt/lib/profile/InstrProfilingPlatformFuchsia.c
+++ b/compiler-rt/lib/profile/InstrProfilingPlatformFuchsia.c
@@ -192,9 +192,11 @@ void __llvm_profile_initialize(void) {
   lprofWrite("LLVM Profile: {{{dumpfile:%s:%s}}}\n", ProfileSinkName, VmoName);
 
   /* Update the profile fields based on the current mapping. */
-  __llvm_profile_counter_bias = (intptr_t)Mapping -
-                                (uintptr_t)__llvm_profile_begin_counters() +
-                                CountersOffset;
+  __llvm_profile_counter_bias =
+      (intptr_t)Mapping - (uintptr_t)CountersBegin + CountersOffset;
+
+  /* Return the memory allocated for counters to OS. */
+  lprofReleaseMemoryPagesToOS((uintptr_t)CountersBegin, (uintptr_t)CountersEnd);
 }
 
 #endif

diff  --git a/compiler-rt/lib/profile/InstrProfilingUtil.c b/compiler-rt/lib/profile/InstrProfilingUtil.c
index bf5a9670fe18c..4fa792b72eac8 100644
--- a/compiler-rt/lib/profile/InstrProfilingUtil.c
+++ b/compiler-rt/lib/profile/InstrProfilingUtil.c
@@ -12,12 +12,13 @@
 #include <windows.h>
 #include "WindowsMMap.h"
 #else
+#include <errno.h>
+#include <fcntl.h>
 #include <sys/file.h>
+#include <sys/mman.h>
 #include <sys/stat.h>
 #include <sys/types.h>
 #include <unistd.h>
-#include <fcntl.h>
-#include <errno.h>
 #endif
 
 #ifdef COMPILER_RT_HAS_UNAME
@@ -32,6 +33,10 @@
 #include <sys/prctl.h>
 #endif
 
+#if defined(__Fuchsia__)
+#include <zircon/syscalls.h>
+#endif
+
 #include "InstrProfiling.h"
 #include "InstrProfilingUtil.h"
 
@@ -330,3 +335,21 @@ COMPILER_RT_VISIBILITY void lprofRestoreSigKill() {
   prctl(PR_SET_PDEATHSIG, SIGKILL);
 #endif
 }
+
+COMPILER_RT_VISIBILITY int lprofReleaseMemoryPagesToOS(uintptr_t Begin,
+                                                       uintptr_t End) {
+  size_t PageSize = getpagesize();
+  uintptr_t BeginAligned = lprofRoundUpTo((uintptr_t)Begin, PageSize);
+  uintptr_t EndAligned = lprofRoundDownTo((uintptr_t)End, PageSize);
+  if (BeginAligned < EndAligned) {
+#if defined(__Fuchsia__)
+    return _zx_vmar_op_range(_zx_vmar_root_self(), ZX_VMAR_OP_DECOMMIT,
+                             (zx_vaddr_t)BeginAligned,
+                             EndAligned - BeginAligned, NULL, 0);
+#else
+    return madvise((void *)BeginAligned, EndAligned - BeginAligned,
+                   MADV_DONTNEED);
+#endif
+  }
+  return 0;
+}

diff  --git a/compiler-rt/lib/profile/InstrProfilingUtil.h b/compiler-rt/lib/profile/InstrProfilingUtil.h
index 5f5c85091fe87..183aff040f7e5 100644
--- a/compiler-rt/lib/profile/InstrProfilingUtil.h
+++ b/compiler-rt/lib/profile/InstrProfilingUtil.h
@@ -9,6 +9,7 @@
 #ifndef PROFILE_INSTRPROFILINGUTIL_H
 #define PROFILE_INSTRPROFILINGUTIL_H
 
+#include <inttypes.h>
 #include <stddef.h>
 #include <stdio.h>
 
@@ -73,4 +74,14 @@ int lprofSuspendSigKill();
 /* Restore previously suspended SIGKILL. */
 void lprofRestoreSigKill();
 
+inline size_t lprofRoundUpTo(size_t x, size_t boundary) {
+  return (x + boundary - 1) & ~(boundary - 1);
+}
+
+inline size_t lprofRoundDownTo(size_t x, size_t boundary) {
+  return x & ~(boundary - 1);
+}
+
+int lprofReleaseMemoryPagesToOS(uintptr_t Begin, uintptr_t End);
+
 #endif /* PROFILE_INSTRPROFILINGUTIL_H */

diff  --git a/compiler-rt/lib/profile/WindowsMMap.c b/compiler-rt/lib/profile/WindowsMMap.c
index 41cc67f41f1fd..07c0a689feaea 100644
--- a/compiler-rt/lib/profile/WindowsMMap.c
+++ b/compiler-rt/lib/profile/WindowsMMap.c
@@ -112,6 +112,18 @@ int msync(void *addr, size_t length, int flags)
   return 0;
 }
 
+COMPILER_RT_VISIBILITY
+int madvise(void *addr, size_t length, int advice)
+{
+  if (advice != MADV_DONTNEED)
+    return -1; /* Not supported. */
+
+  if (!VirtualUnlock(addr, length))
+    return -1;
+
+  return 0;
+}
+
 COMPILER_RT_VISIBILITY
 int lock(HANDLE handle, DWORD lockType, BOOL blocking) {
   DWORD flags = lockType;

diff  --git a/compiler-rt/lib/profile/WindowsMMap.h b/compiler-rt/lib/profile/WindowsMMap.h
index c8d6250f41c1a..68b8de2398d60 100644
--- a/compiler-rt/lib/profile/WindowsMMap.h
+++ b/compiler-rt/lib/profile/WindowsMMap.h
@@ -36,6 +36,14 @@
 #define MS_INVALIDATE   0x0002  /* invalidate all cached data */
 #define MS_SYNC         0x0010  /* msync synchronously */
 
+/*
+ * madvise() flags
+ */
+
+#define MADV_NORMAL     0   /* no special treatment */
+#define MADV_WILLNEED   3   /* expect access in the near future */
+#define MADV_DONTNEED   4   /* do not expect access in the near future */
+
 /*
  * flock() operations
  */
@@ -59,6 +67,8 @@ void munmap(void *addr, size_t length);
 
 int msync(void *addr, size_t length, int flags);
 
+int madvise(void *addr, size_t length, int advice);
+
 int flock(int fd, int operation);
 
 #endif /* _WIN32 */


        


More information about the llvm-commits mailing list