Source

mana-core-athenacommon / python / ResourceLimits.py

# File: AthenaCommon/python/ResourceLimits.py
# Author: Grigori Rybkine (Grigori.Rybkine@rhul.ac.uk)
# Modified: Wim Lavrijsen (WLavrijsen@lbl.gov)

"""Helper to max soft resource limits to the hard ones."""

from Logging import logging
import resource


### data ---------------------------------------------------------------------
__version__ = '1.0.1'
__author__  = 'Grigori Rybkine (Grigori.Rybkine@rhul.ac.uk)'

__all__ = [ 'SetMaxLimits' ]

log = logging.getLogger( 'ResourceLimits' )


### private helper -----------------------------------------------------------
def _maxout( what, descr ):
   soft, hard = resource.getrlimit( what )
   if soft < hard or ( hard == -1L and soft != hard ):
      log.debug( 'setting soft %s limit to %s (was: %s)', descr,
                 hard == -1L and 'unlimited' or str(hard),
                 soft == -1L and 'unlimited' or str(soft), )
      resource.setrlimit( what, (hard,hard) )


### max out soft resource limits to hard ones --------------------------------
def SetMaxLimits():
    try:
       _maxout( resource.RLIMIT_AS,  'address space' )
       _maxout( resource.RLIMIT_RSS, 'resident state' )
    except (ValueError,resource.error), e:
       log.error( 'failed to set max resource limits (%s)', str(e) )