[Git][ghc/ghc][wip/hugepages] rts: Add support for hugepages

Ben Gamari gitlab at gitlab.haskell.org
Fri Nov 27 17:09:24 UTC 2020



Ben Gamari pushed to branch wip/hugepages at Glasgow Haskell Compiler / GHC


Commits:
ea17a1f1 by Ben Gamari at 2020-11-27T12:09:17-05:00
rts: Add support for hugepages

- - - - -


3 changed files:

- includes/rts/Flags.h
- rts/RtsFlags.c
- rts/posix/OSMem.c


Changes:

=====================================
includes/rts/Flags.h
=====================================
@@ -89,6 +89,7 @@ typedef struct _GC_FLAGS {
 
     bool numa;                   /* Use NUMA */
     StgWord numaMask;
+    bool hugepages;              /* Enable hugepages support */
 } GC_FLAGS;
 
 /* See Note [Synchronization of flags and base APIs] */


=====================================
rts/RtsFlags.c
=====================================
@@ -181,6 +181,7 @@ void initRtsFlagsDefaults(void)
     RtsFlags.GcFlags.allocLimitGrace    = (100*1024) / BLOCK_SIZE;
     RtsFlags.GcFlags.numa               = false;
     RtsFlags.GcFlags.numaMask           = 1;
+    RtsFlags.GcFlags.hugepages          = false;
     RtsFlags.GcFlags.ringBell           = false;
     RtsFlags.GcFlags.longGCSync         = 0; /* detection turned off */
 
@@ -516,6 +517,7 @@ usage_text[] = {
 #endif
 "  -xq       The allocation limit given to a thread after it receives",
 "            an AllocationLimitExceeded exception. (default: 100k)",
+"  -xH       Use hugepages to allocate huge",
 "",
 "  -Mgrace=<n>",
 "            The amount of allocation after the program receives a",
@@ -1694,13 +1696,18 @@ error = true;
                    */
 
                 case 'q':
-                  OPTION_UNSAFE;
-                  RtsFlags.GcFlags.allocLimitGrace
-                      = decodeSize(rts_argv[arg], 3, BLOCK_SIZE, HS_INT_MAX)
-                          / BLOCK_SIZE;
-                  break;
+                    OPTION_UNSAFE;
+                    RtsFlags.GcFlags.allocLimitGrace
+                        = decodeSize(rts_argv[arg], 3, BLOCK_SIZE, HS_INT_MAX)
+                            / BLOCK_SIZE;
+                    break;
 
-                  default:
+                case 'H':
+                    OPTION_SAFE;
+                    RtsFlags.GcFlags.hugepages = true;
+                    break;
+
+                default:
                     OPTION_SAFE;
                     errorBelch("unknown RTS option: %s",rts_argv[arg]);
                     error = true;


=====================================
rts/posix/OSMem.c
=====================================
@@ -472,6 +472,11 @@ void setExecutable (void *p, W_ len, bool exec)
 
 #if defined(USE_LARGE_ADDRESS_SPACE)
 
+#if defined(MAP_HUGETLB) && defined(MAP_HUGE_2MB)
+#define HUGEPAGE_SIZE (2*1024*1024)
+#define HUGEPAGE_FLAGS (MAP_HUGETLB | MAP_HUGE_2MB)
+#endif
+
 static void *
 osTryReserveHeapMemory (W_ len, void *hint)
 {
@@ -484,7 +489,16 @@ osTryReserveHeapMemory (W_ len, void *hint)
        because we need memory which is MBLOCK_SIZE aligned,
        and then we discard what we don't need */
 
-    base = my_mmap(hint, len + MBLOCK_SIZE, MEM_RESERVE);
+#if defined(HUGEPAGE_SIZE)
+    const bool hugepages = RtsFlags.GcFlags.hugepages ? HUGEPAGE_FLAGS : 0;
+#else
+    const bool hugepages = 0;
+#endif
+    base = my_mmap(hint, len + MBLOCK_SIZE, MEM_RESERVE | hugepages);
+
+    // If failed then try again without hugepages
+    if (base == NULL && hugepages)
+        base = my_mmap(hint, len + MBLOCK_SIZE, MEM_RESERVE);
     if (base == NULL)
         return NULL;
 
@@ -643,6 +657,18 @@ void *osReserveHeapMemory(void *startAddressPtr, W_ *len)
 
 void osCommitMemory(void *at, W_ size)
 {
+#if defined(HUGEPAGE_SIZE)
+    // Try committing with hugepages, if available.
+    if (RtsFlags.GcFlags.hugepages
+        && ((uintptr_t) at & (HUGEPAGE_SIZE - 1) == 0)
+        && (size & (HUGEPAGE_SIZE - 1) == 0)) {
+        void *r = my_mmap(at, size, MEM_COMMIT | HUGEPAGE_FLAGS);
+        if (r != NULL) {
+            return;
+        }
+    }
+#endif
+
     void *r = my_mmap(at, size, MEM_COMMIT);
     if (r == NULL) {
         barf("Unable to commit %" FMT_Word " bytes of memory", size);



View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/ea17a1f1dc628da0a85fec8774b2060f364a1c06

-- 
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/ea17a1f1dc628da0a85fec8774b2060f364a1c06
You're receiving this email because of your account on gitlab.haskell.org.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/ghc-commits/attachments/20201127/181d8588/attachment-0001.html>


More information about the ghc-commits mailing list