[commit: ghc] master: rts: Add getPhysicalMemorySize (72f8b8d)

git at git.haskell.org git at git.haskell.org
Fri Oct 25 14:22:20 UTC 2013


Repository : ssh://git@git.haskell.org/ghc

On branch  : master
Link       : http://ghc.haskell.org/trac/ghc/changeset/72f8b8d694362890a424f5fa21fef37cee921ecf/ghc

>---------------------------------------------------------------

commit 72f8b8d694362890a424f5fa21fef37cee921ecf
Author: Ben Gamari <bgamari.foss at gmail.com>
Date:   Thu Sep 12 23:04:10 2013 -0400

    rts: Add getPhysicalMemorySize


>---------------------------------------------------------------

72f8b8d694362890a424f5fa21fef37cee921ecf
 rts/posix/OSMem.c |   20 ++++++++++++++++++++
 rts/sm/OSMem.h    |    1 +
 rts/win32/OSMem.c |   18 ++++++++++++++++++
 3 files changed, 39 insertions(+)

diff --git a/rts/posix/OSMem.c b/rts/posix/OSMem.c
index 4328d9d..21d4e54 100644
--- a/rts/posix/OSMem.c
+++ b/rts/posix/OSMem.c
@@ -263,6 +263,26 @@ W_ getPageSize (void)
     }
 }
 
+/* Returns 0 if physical memory size cannot be identified */
+StgWord64 getPhysicalMemorySize (void)
+{
+    static StgWord64 physMemSize = 0;
+    if (!physMemSize) {
+        long ret;
+        W_ pageSize = getPageSize();
+
+        ret = sysconf(_SC_PHYS_PAGES);
+        if (ret == -1) {
+#if defined(DEBUG)
+            errorBelch("warning: getPhysicsMemorySize: cannot get physical memory size");
+#endif
+            return 0;
+        }
+        physMemSize = ret * pageSize;
+    }
+    return physMemSize;
+}
+
 void setExecutable (void *p, W_ len, rtsBool exec)
 {
     StgWord pageSize = getPageSize();
diff --git a/rts/sm/OSMem.h b/rts/sm/OSMem.h
index a0d615b..db704fc 100644
--- a/rts/sm/OSMem.h
+++ b/rts/sm/OSMem.h
@@ -17,6 +17,7 @@ void osFreeMBlocks(char *addr, nat n);
 void osReleaseFreeMemory(void);
 void osFreeAllMBlocks(void);
 W_ getPageSize (void);
+StgWord64 getPhysicalMemorySize (void);
 void setExecutable (void *p, W_ len, rtsBool exec);
 
 #include "EndPrivate.h"
diff --git a/rts/win32/OSMem.c b/rts/win32/OSMem.c
index 218b25d..f350076 100644
--- a/rts/win32/OSMem.c
+++ b/rts/win32/OSMem.c
@@ -378,6 +378,24 @@ W_ getPageSize (void)
     }
 }
 
+/* Returns 0 if physical memory size cannot be identified */
+StgWord64 getPhysicalMemorySize (void)
+{
+    static StgWord64 physMemSize = 0;
+    if (!physMemSize) {
+        MEMORYSTATUSEX status;
+        status.dwLength = sizeof(status);
+        if (!GlobalMemoryStatusEx(&status)) {
+#if defined(DEBUG)
+            errorBelch("warning: getPhysicalMemorySize: cannot get physical memory size");
+#endif
+            return 0;
+        }
+        physMemSize = status.ullTotalPhys;
+    }
+    return physMemSize;
+}
+
 void setExecutable (void *p, W_ len, rtsBool exec)
 {
     DWORD dwOldProtect = 0;



More information about the ghc-commits mailing list