Back to index

moin  1.9.0~rc2
memutils.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 #
00003 # Copyright (C) 2007 Lemur Consulting Ltd
00004 #
00005 # This program is free software; you can redistribute it and/or modify
00006 # it under the terms of the GNU General Public License as published by
00007 # the Free Software Foundation; either version 2 of the License, or
00008 # (at your option) any later version.
00009 #
00010 # This program is distributed in the hope that it will be useful,
00011 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 # GNU General Public License for more details.
00014 # 
00015 # You should have received a copy of the GNU General Public License along
00016 # with this program; if not, write to the Free Software Foundation, Inc.,
00017 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
00018 r"""memutils.py: Memory handling utilities.
00019 
00020 """
00021 __docformat__ = "restructuredtext en"
00022 
00023 import os
00024 
00025 def _get_physical_mem_sysconf():
00026     """Try getting a value for the physical memory using os.sysconf().
00027 
00028     Returns None if no value can be obtained - otherwise, returns a value in
00029     bytes.
00030 
00031     """
00032     if getattr(os, 'sysconf', None) is None:
00033         return None
00034 
00035     try:
00036         pagesize = os.sysconf('SC_PAGESIZE')
00037     except ValueError:
00038         try:
00039             pagesize = os.sysconf('SC_PAGE_SIZE')
00040         except ValueError:
00041             return None
00042 
00043     try:
00044         pagecount = os.sysconf('SC_PHYS_PAGES')
00045     except ValueError:
00046         return None
00047 
00048     return pagesize * pagecount
00049 
00050 def _get_physical_mem_win32():
00051     """Try getting a value for the physical memory using GlobalMemoryStatus.
00052 
00053     This is a windows specific method.  Returns None if no value can be
00054     obtained (eg, not running on windows) - otherwise, returns a value in
00055     bytes.
00056 
00057     """
00058     try:
00059         import ctypes
00060         import ctypes.wintypes as wintypes
00061     except ValueError:
00062         return None
00063     
00064     class MEMORYSTATUS(wintypes.Structure):
00065         _fields_ = [
00066             ('dwLength', wintypes.DWORD),
00067             ('dwMemoryLoad', wintypes.DWORD),
00068             ('dwTotalPhys', wintypes.DWORD),
00069             ('dwAvailPhys', wintypes.DWORD),
00070             ('dwTotalPageFile', wintypes.DWORD),
00071             ('dwAvailPageFile', wintypes.DWORD),
00072             ('dwTotalVirtual', wintypes.DWORD),
00073             ('dwAvailVirtual', wintypes.DWORD),
00074         ]
00075 
00076     m = MEMORYSTATUS()
00077     wintypes.windll.kernel32.GlobalMemoryStatus(wintypes.byref(m))
00078     return m.dwTotalPhys
00079 
00080 def get_physical_memory():
00081     """Get the amount of physical memory in the system, in bytes.
00082 
00083     If this can't be obtained, returns None.
00084 
00085     """
00086     result = _get_physical_mem_sysconf()
00087     if result is not None:
00088         return result
00089     return _get_physical_mem_win32()