##// END OF EJS Templates
util: provide a helper function to estimate RAM size...
Joerg Sonnenberger -
r45607:cfe0f491 default draft
parent child Browse files
Show More
@@ -1231,6 +1231,10 b' coreconfigitem('
1231 b'ui', b'askusername', default=False,
1231 b'ui', b'askusername', default=False,
1232 )
1232 )
1233 coreconfigitem(
1233 coreconfigitem(
1234 b'ui', b'available-memory', default=None,
1235 )
1236
1237 coreconfigitem(
1234 b'ui', b'clonebundlefallback', default=False,
1238 b'ui', b'clonebundlefallback', default=False,
1235 )
1239 )
1236 coreconfigitem(
1240 coreconfigitem(
@@ -2119,6 +2119,22 b' class ui(object):'
2119 if (b'ui', b'quiet') in overrides:
2119 if (b'ui', b'quiet') in overrides:
2120 self.fixconfig(section=b'ui')
2120 self.fixconfig(section=b'ui')
2121
2121
2122 def estimatememory(self):
2123 """Provide an estimate for the available system memory in Bytes.
2124
2125 This can be overriden via ui.available-memory. It returns None, if
2126 no estimate can be computed.
2127 """
2128 value = self.config(b'ui', b'available-memory')
2129 if value is not None:
2130 try:
2131 return util.sizetoint(value)
2132 except error.ParseError:
2133 raise error.ConfigError(
2134 _(b"ui.available-memory value is invalid ('%s')") % value
2135 )
2136 return util._estimatememory()
2137
2122
2138
2123 class paths(dict):
2139 class paths(dict):
2124 """Represents a collection of paths and their configs.
2140 """Represents a collection of paths and their configs.
@@ -3626,3 +3626,44 b' def with_lc_ctype():'
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
General Comments 0
You need to be logged in to leave comments. Login now