[Updated] [+++ ] D8644: util: provide a helper function to estimate RAM size

joerg.sonnenberger (Joerg Sonnenberger) phabricator at mercurial-scm.org
Tue Jun 30 21:26:12 UTC 2020


joerg.sonnenberger updated this revision to Diff 21735.

REPOSITORY
  rHG Mercurial

CHANGES SINCE LAST UPDATE
  https://phab.mercurial-scm.org/D8644?vs=21672&id=21735

BRANCH
  stable

CHANGES SINCE LAST ACTION
  https://phab.mercurial-scm.org/D8644/new/

REVISION DETAIL
  https://phab.mercurial-scm.org/D8644

AFFECTED FILES
  mercurial/configitems.py
  mercurial/ui.py
  mercurial/util.py

CHANGE DETAILS

diff --git a/mercurial/util.py b/mercurial/util.py
--- a/mercurial/util.py
+++ b/mercurial/util.py
@@ -3626,3 +3626,39 @@
         if not (byte & 0x80):
             return result
         shift += 7
+
+
+def _estimatememory():
+    """Provide an estimate for the available system memory in Bytes.
+
+    If no estimate can be provided on the platform, returns None.
+    """
+    try:
+        pagesize = os.sysconf(os.sysconf_names['SC_PAGE_SIZE'])
+        pages = os.sysconf(os.sysconf_names['SC_PHYS_PAGES'])
+        return pagesize * pages
+    except OSError:
+        pass
+    except KeyError:
+        pass
+    if pycompat.sysplatform.startswith(b'win'):
+        from ctypes import c_long as DWORD, c_ulonglong as DWORDLONG
+        from ctypes.wintypes import Structure, byref, sizeof, windll
+
+        class MEMORYSTATUSEX(Structure):
+            _fields_ = [
+                ('dwLength', DWORD),
+                ('dwMemoryLoad', DWORD),
+                ('ullTotalPhys', DWORDLONG),
+                ('ullAvailPhys', DWORDLONG),
+                ('ullTotalPageFile', DWORDLONG),
+                ('ullAvailPageFile', DWORDLONG),
+                ('ullTotalVirtual', DWORDLONG),
+                ('ullAvailVirtual', DWORDLONG),
+                ('ullExtendedVirtual', DWORDLONG),
+            ]
+
+        x = MEMORYSTATUSEX()
+        x.dwLength = sizeof(x)
+        windll.kernel32.GlobalMemoryStatusEx(byref(x))
+        return x.ullAvailPhys
diff --git a/mercurial/ui.py b/mercurial/ui.py
--- a/mercurial/ui.py
+++ b/mercurial/ui.py
@@ -2102,6 +2102,22 @@
             if (b'ui', b'quiet') in overrides:
                 self.fixconfig(section=b'ui')
 
+    def estimatememory(self):
+        """Provide an estimate for the available system memory in Bytes.
+
+        This can be overriden via ui.available-memory and can fail by
+        returning None.
+        """
+        value = self.config(b'ui', b'available-memory')
+        if value is not None:
+            try:
+                return util.sizetoint(value)
+            except error.ParseError:
+                raise error.ConfigError(
+                    _(b"ui.available-memory value is invalid ('%s')") % value
+                )
+        return util._estimatememory()
+
 
 class paths(dict):
     """Represents a collection of paths and their configs.
diff --git a/mercurial/configitems.py b/mercurial/configitems.py
--- a/mercurial/configitems.py
+++ b/mercurial/configitems.py
@@ -1231,6 +1231,10 @@
     b'ui', b'askusername', default=False,
 )
 coreconfigitem(
+    b'ui', b'available-memory', default=None,
+)
+
+coreconfigitem(
     b'ui', b'clonebundlefallback', default=False,
 )
 coreconfigitem(



To: joerg.sonnenberger, #hg-reviewers
Cc: mercurial-patches
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mercurial-scm.org/pipermail/mercurial-patches/attachments/20200630/264e5a80/attachment-0002.html>


More information about the Mercurial-patches mailing list