comparison mercurial/util.py @ 45061:02b17231f6c3

util: provide a helper function to estimate RAM size For POSIX systems, it uses sysconf. For Windows, it uses the win32 API directly. Differential Revision: https://phab.mercurial-scm.org/D8644
author Joerg Sonnenberger <joerg@bec.de>
date Tue, 09 Jun 2020 11:22:31 +0200
parents 4a503c1b664a
children a0791bfd9cfa
comparison
equal deleted inserted replaced
45060:baffdfa5bd1a 45061:02b17231f6c3
3624 yield 3624 yield
3625 finally: 3625 finally:
3626 locale.setlocale(locale.LC_CTYPE, oldloc) 3626 locale.setlocale(locale.LC_CTYPE, oldloc)
3627 else: 3627 else:
3628 yield 3628 yield
3629
3630
3631 def _estimatememory():
3632 """Provide an estimate for the available system memory in Bytes.
3633
3634 If no estimate can be provided on the platform, returns None.
3635 """
3636 if pycompat.sysplatform.startswith(b'win'):
3637 # On Windows, use the GlobalMemoryStatusEx kernel function directly.
3638 from ctypes import c_long as DWORD, c_ulonglong as DWORDLONG
3639 from ctypes.wintypes import Structure, byref, sizeof, windll
3640
3641 class MEMORYSTATUSEX(Structure):
3642 _fields_ = [
3643 ('dwLength', DWORD),
3644 ('dwMemoryLoad', DWORD),
3645 ('ullTotalPhys', DWORDLONG),
3646 ('ullAvailPhys', DWORDLONG),
3647 ('ullTotalPageFile', DWORDLONG),
3648 ('ullAvailPageFile', DWORDLONG),
3649 ('ullTotalVirtual', DWORDLONG),
3650 ('ullAvailVirtual', DWORDLONG),
3651 ('ullExtendedVirtual', DWORDLONG),
3652 ]
3653
3654 x = MEMORYSTATUSEX()
3655 x.dwLength = sizeof(x)
3656 windll.kernel32.GlobalMemoryStatusEx(byref(x))
3657 return x.ullAvailPhys
3658
3659 # On newer Unix-like systems and Mac OSX, the sysconf interface
3660 # can be used. _SC_PAGE_SIZE is part of POSIX; _SC_PHYS_PAGES
3661 # seems to be implemented on most systems.
3662 try:
3663 pagesize = os.sysconf(os.sysconf_names['SC_PAGE_SIZE'])
3664 pages = os.sysconf(os.sysconf_names['SC_PHYS_PAGES'])
3665 return pagesize * pages
3666 except OSError: # sysconf can fail
3667 pass
3668 except KeyError: # unknown parameter
3669 pass