xappy.memutils

Package xappy :: Module memutils |
[frames] | no frames]

# Source Code for Module xappy.memutils

     1  #!/usr/bin/env python
     2  #
     3  # Copyright (C) 2007 Lemur Consulting Ltd
     4  #
     5  # This program is free software; you can redistribute it and/or modify
     6  # it under the terms of the GNU General Public License as published by
     7  # the Free Software Foundation; either version 2 of the License, or
     8  # (at your option) any later version.
     9  #
    10  # This program is distributed in the hope that it will be useful,
    11  # but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  # GNU General Public License for more details.
    14  #
    15  # You should have received a copy of the GNU General Public License along
    16  # with this program; if not, write to the Free Software Foundation, Inc.,
    17  # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
    18  r"""memutils.py: Memory handling utilities.
    19
    20  """
    21  __docformat__ = "restructuredtext en"
    22
    23  import os
    24
    25 -def _get_physical_mem_sysconf():
    26      """Try getting a value for the physical memory using os.sysconf().
    27
    28      Returns None if no value can be obtained - otherwise, returns a value in
    29      bytes.
    30
    31      """
    32      if getattr(os, 'sysconf', None) is None:
    33          return None
    34
    35      try:
    36          pagesize = os.sysconf('SC_PAGESIZE')
    37      except ValueError:
    38          try:
    39              pagesize = os.sysconf('SC_PAGE_SIZE')
    40          except ValueError:
    41              return None
    42
    43      try:
    44          pagecount = os.sysconf('SC_PHYS_PAGES')
    45      except ValueError:
    46          return None
    47
    48      return pagesize * pagecount
    49
    50 -def _get_physical_mem_win32():
    51      """Try getting a value for the physical memory using GlobalMemoryStatus.
    52
    53      This is a windows specific method.  Returns None if no value can be
    54      obtained (eg, not running on windows) - otherwise, returns a value in
    55      bytes.
    56
    57      """
    58      try:
    59          import ctypes
    60          import ctypes.wintypes as wintypes
    61      except ValueError:
    62          return None
    63
    64      class MEMORYSTATUS(wintypes.Structure):
    65          _fields_ = [
    66              ('dwLength', wintypes.DWORD),
    67              ('dwMemoryLoad', wintypes.DWORD),
    68              ('dwTotalPhys', wintypes.DWORD),
    69              ('dwAvailPhys', wintypes.DWORD),
    70              ('dwTotalPageFile', wintypes.DWORD),
    71              ('dwAvailPageFile', wintypes.DWORD),
    72              ('dwTotalVirtual', wintypes.DWORD),
    73              ('dwAvailVirtual', wintypes.DWORD),
    74          ]
    75
    76      m = MEMORYSTATUS()
    77      wintypes.windll.kernel32.GlobalMemoryStatus(wintypes.byref(m))
    78      return m.dwTotalPhys
    79
    80 -def get_physical_memory():
    81      """Get the amount of physical memory in the system, in bytes.
    82
    83      If this can't be obtained, returns None.
    84
    85      """
    86      result = _get_physical_mem_sysconf()
    87      if result is not None:
    88          return result
    89      return _get_physical_mem_win32()
    90