Ticket #13880: trac_13880_gap_pool_RLIMIT_AS.patch

File trac_13880_gap_pool_RLIMIT_AS.patch, 6.6 KB (added by vbraun, 9 years ago)

Updated patch

  • sage/interfaces/gap.py

    # HG changeset patch
    # User Volker Braun <vbraun.name@gmail.com>
    # Date 1357315176 0
    # Node ID 12adbcefff2caa6315526b8e5f6c80e76b1f6a77
    # Parent  0002ff91d54a87602a0f94599a9632798b757b2d
    Respect ulimit -v when deciding GAP memory pool size.
    Fix doctest for amout of swap.
    
    diff --git a/sage/interfaces/gap.py b/sage/interfaces/gap.py
    a b  
    270270    from sage.misc.memory_info import MemoryInfo
    271271    mem = MemoryInfo()
    272272    suggested_size = max(int(mem.available_swap() / 10),
    273                          int(mem.available_ram()  / 50),   # in case you run without swap
    274                          250 * 1024**2 )      # ~220MB is the minimum for long doctests
     273                         int(mem.available_ram()  / 50))
     274    # Don't eat all address space if the user set ulimit -v
     275    suggested_size = min(suggested_size, int(mem.virtual_memory_limit()/10))
     276    # ~220MB is the minimum for long doctests
     277    suggested_size = max(suggested_size, 250 * 1024**2)
    275278    return suggested_size
    276279
    277280def _get_gap_memory_pool_size_MB():
  • sage/misc/memory_info.py

    diff --git a/sage/misc/memory_info.py b/sage/misc/memory_info.py
    a b  
    7777    return memory_info_instance
    7878
    7979
    80 class MemoryInfo_proc(SageObject):
     80class MemoryInfo_base(SageObject):
     81    """
     82    Base class for memory info objects.
     83    """
     84
     85    def rlimit_address_space(self):
     86        """
     87        Return ``RLIMIT_AS``.
     88       
     89        OUTPUT:
     90       
     91        Integer. The limit in bytes or `-1` if no limit is set or cannot
     92        be found out.
     93
     94        EXAMPLES::
     95       
     96            sage: from sage.misc.memory_info import MemoryInfo
     97            sage: mem = MemoryInfo()
     98            sage: mem.rlimit_address_space() in ZZ
     99            True
     100        """
     101        import resource
     102        try:
     103            return resource.getrlimit(resource.RLIMIT_AS)[1]
     104        except resource.error:
     105            return -1
     106
     107    def virtual_memory_limit(self):
     108        """
     109        Return the upper limit for virtual memory usage
     110       
     111        This is the value set by ``ulimit -v`` at the command line or
     112        a practical limit if no limit is set.
     113
     114        OUTPUT:
     115
     116        Integer. The virtual memory limit in bytes.
     117
     118        EXAMPLES::
     119
     120            sage: from sage.misc.memory_info import MemoryInfo
     121            sage: mem = MemoryInfo()
     122            sage: mem.virtual_memory_limit() > 0
     123            True
     124        """
     125        limit = self.rlimit_address_space()
     126        if limit >=0:
     127            return limit
     128        else:
     129            avail = self.total_swap() + self.total_ram()
     130            import platform
     131            if platform.architecture()[0] == '32bit':
     132                # 2GB is likely the single-process address space limit
     133                return min(avail, 2 * 1024**3)
     134            else:
     135                return avail
     136
     137
     138class MemoryInfo_proc(MemoryInfo_base):
    81139    """
    82140    Provide information from ``/proc/`` pseudo-filesystem on most UNIXes
    83141
     
    114172            sage: info   # random output
    115173            {'available_ram': 1749782528,
    116174             'total_swap': 15728635904,
    117              'available_swap': 15340572672,
     175             'free_swap': 15340572672,
    118176             'total_ram': 16708194304}
    119             sage: keys = set(['available_ram', 'total_swap', 'available_swap', 'total_ram'])
     177            sage: keys = set(['available_ram', 'total_swap', 'free_swap', 'total_ram'])
    120178            sage: (info is None) or keys.issubset(info.keys())
    121179            True
    122180        """
     
    132190            if line[0].startswith('SwapTotal') and line[2] == 'kB':
    133191                result['total_swap'] = int(line[1]) * kb
    134192            if line[0].startswith('SwapFree') and line[2] == 'kB':
    135                 result['available_swap'] = int(line[1]) * kb
     193                result['free_swap'] = int(line[1]) * kb
    136194            if line[0].startswith('Committed_AS') and line[2] == 'kB':
    137195                result['Committed_AS'] = int(line[1]) * kb
    138196        meminfo.close()
    139         required = set(['available_ram', 'total_swap', 'available_swap', 'total_ram'])
     197        required = set(['available_ram', 'total_swap', 'free_swap', 'total_ram'])
    140198        if not required.issubset(result.keys()):
    141199            raise OSError('failed to parse /proc/meminfo correctly')
    142200        return result
     
    184242        EXAMPLES::
    185243
    186244            sage: from sage.misc.memory_info import MemoryInfo
    187             sage: MemoryInfo().total_swap() > 0
     245            sage: MemoryInfo().total_swap() >= 0
    188246            True
    189247        """
    190248        return self._parse_proc_meminfo()['total_swap']
     
    195253       
    196254        OUTPUT:
    197255
    198         Integer. The free swap size in bytes.
     256        Integer. The free swap size in bytes, excluding reserved swap
     257        space. Can be negative if the system is overcommitting memory.
    199258
    200259        EXAMPLES::
    201260
    202261            sage: from sage.misc.memory_info import MemoryInfo
    203             sage: MemoryInfo().available_swap() > 0
     262            sage: MemoryInfo().available_swap() in ZZ
    204263            True
    205264        """
    206265        info = self._parse_proc_meminfo()
    207266        try:
    208267            return info['total_swap'] - info['Committed_AS']
    209268        except KeyError:
    210             return info['available_swap']
     269            return info['free_swap']
    211270
    212271
    213 class MemoryInfo_OSX(SageObject):
     272class MemoryInfo_OSX(MemoryInfo_base):
    214273    """
    215274    Memory info on OSX
    216275
     
    320379        EXAMPLES::
    321380
    322381            sage: from sage.misc.memory_info import MemoryInfo
    323             sage: MemoryInfo().total_swap() > 0
     382            sage: MemoryInfo().total_swap() >= 0
    324383            True
    325384        """
    326385        return 2*self.total_ram()
     
    339398        EXAMPLES::
    340399
    341400            sage: from sage.misc.memory_info import MemoryInfo
    342             sage: MemoryInfo().available_swap() > 0
     401            sage: MemoryInfo().available_swap() in ZZ
    343402            True
    344403        """
    345404        return 2*self.available_ram()
    346405
    347406
    348 class MemoryInfo_guess(SageObject):
     407class MemoryInfo_guess(MemoryInfo_base):
    349408    """
    350409    Guess memory as a fallback.
    351410
     
    401460        EXAMPLES::
    402461
    403462            sage: from sage.misc.memory_info import MemoryInfo
    404             sage: MemoryInfo().total_swap() > 0
     463            sage: MemoryInfo().total_swap() >= 0
    405464            True
    406465        """
    407466        GB = 1024 * 1024 * 1024
     
    418477        EXAMPLES::
    419478
    420479            sage: from sage.misc.memory_info import MemoryInfo
    421             sage: MemoryInfo().available_swap() > 0
     480            sage: MemoryInfo().available_swap() in ZZ
    422481            True
    423482        """
    424483        return self.total_swap()
    425        
    426