Source code for bronx.system.memory
"""
This module is in charge of getting informations on Memory.
Various concrete implementations may be provided since the mechanism to retrieve
information on Memory may not be portable across platforms.
At the present time, the only concrete implementation is
the :class:`LinuxMemInfo`.
"""
import abc
import os
import resource
DEFAULT_MEM_UNIT = 'MiB'
[docs]def convert_bytes_in_unit(mem_b, unit):
"""Convert bytes to **unit** (among KB, MB, GB, ... or KiB, MiB, GiB).
Note: KB, MB, ... are powers of 1000 whereas KiB, MiB, ... are powers of 1024
(KiB are often mistaken for KB).
"""
unit_power = {'B': 0,
'KB': 1, 'MB': 2, 'GB': 3, 'TB': 4, 'PB': 5, 'EB': 6,
'KiB': 1, 'MiB': 2, 'GiB': 3, 'TiB': 4, 'PiB': 5, 'EiB': 6, }
if unit not in unit_power:
raise ValueError('Unknown unit {!s}'.format(unit))
if unit != 'B':
mem_b = float(mem_b)
return mem_b / ((1024 if 'i' in unit else 1000) ** unit_power[unit])
[docs]class MemInfo(metaclass=abc.ABCMeta):
"""Provide various informations about Memory (abstract class)."""
@abc.abstractmethod
def __init__(self):
self._system_RAM = None
[docs] def system_RAM(self, unit=DEFAULT_MEM_UNIT):
"""Get total RAM memory available in the system."""
return convert_bytes_in_unit(self._system_RAM, unit)
[docs]class LinuxMemInfo(MemInfo):
"""Provide various informations about Memory."""
def __init__(self):
# The RAM size in bytes
self._system_RAM = os.sysconf('SC_PAGE_SIZE') * os.sysconf('SC_PHYS_PAGES')