<s> """<STR_LIT>""" <EOL> import logging <EOL> import threading <EOL> try : <EOL> from thread import get_ident <EOL> except ImportError : <EOL> from _thread import get_ident <EOL> from os import getpid <EOL> from subprocess import Popen , PIPE <EOL> from pympler . util . stringutils import pp <EOL> try : <EOL> from resource import getpagesize as _getpagesize <EOL> except ImportError : <EOL> _getpagesize = lambda : <NUM_LIT> <EOL> class _ProcessMemoryInfo ( object ) : <EOL> """<STR_LIT>""" <EOL> pagesize = _getpagesize ( ) <EOL> def __init__ ( self ) : <EOL> self . pid = getpid ( ) <EOL> self . rss = <NUM_LIT:0> <EOL> self . vsz = <NUM_LIT:0> <EOL> self . pagefaults = <NUM_LIT:0> <EOL> self . os_specific = [ ] <EOL> self . data_segment = <NUM_LIT:0> <EOL> self . code_segment = <NUM_LIT:0> <EOL> self . shared_segment = <NUM_LIT:0> <EOL> self . stack_segment = <NUM_LIT:0> <EOL> self . available = self . update ( ) <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % ( self . __class__ . __name__ , <EOL> self . vsz , self . rss ) <EOL> def update ( self ) : <EOL> """<STR_LIT>""" <EOL> return False <EOL> def __sub__ ( self , other ) : <EOL> diff = [ ( '<STR_LIT>' , self . rss - other . rss ) , <EOL> ( '<STR_LIT>' , self . vsz - other . vsz ) , <EOL> ] <EOL> return diff <EOL> ProcessMemoryInfo = _ProcessMemoryInfo <EOL> def is_available ( ) : <EOL> """<STR_LIT>""" <EOL> return ProcessMemoryInfo ( ) . update ( ) <EOL> class _ProcessMemoryInfoPS ( _ProcessMemoryInfo ) : <EOL> def update ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> p = Popen ( [ '<STR_LIT>' , '<STR_LIT>' % self . pid , '<STR_LIT>' , '<STR_LIT>' ] , <EOL> stdout = PIPE , stderr = PIPE ) <EOL> except OSError : <EOL> pass <EOL> else : <EOL> s = p . communicate ( ) [ <NUM_LIT:0> ] . split ( ) <EOL> if p . returncode == <NUM_LIT:0> and len ( s ) >= <NUM_LIT:2> : <EOL> self . vsz = int ( s [ - <NUM_LIT:1> ] ) * <NUM_LIT> <EOL> self . rss = int ( s [ - <NUM_LIT:2> ] ) * <NUM_LIT> <EOL> return True <EOL> return False <EOL> class _ProcessMemoryInfoProc ( _ProcessMemoryInfo ) : <EOL> key_map = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> def update ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> stat = open ( '<STR_LIT>' ) <EOL> status = open ( '<STR_LIT>' ) <EOL> except IOError : <EOL> return False <EOL> else : <EOL> stats = stat . read ( ) . split ( ) <EOL> self . vsz = int ( stats [ <NUM_LIT> ] ) <EOL> self . rss = int ( stats [ <NUM_LIT> ] ) * self . pagesize <EOL> self . pagefaults = int ( stats [ <NUM_LIT:11> ] ) <EOL> for entry in status . readlines ( ) : <EOL> key , value = entry . split ( '<STR_LIT::>' , <NUM_LIT:1> ) <EOL> size_in_bytes = lambda x : int ( x . split ( ) [ <NUM_LIT:0> ] ) * <NUM_LIT> <EOL> if key == '<STR_LIT>' : <EOL> self . data_segment = size_in_bytes ( value ) <EOL> elif key == '<STR_LIT>' : <EOL> self . code_segment = size_in_bytes ( value ) <EOL> elif key == '<STR_LIT>' : <EOL> self . shared_segment = size_in_bytes ( value ) <EOL> elif key == '<STR_LIT>' : <EOL> self . stack_segment = size_in_bytes ( value ) <EOL> key = self . key_map . get ( key ) <EOL> if key : <EOL> self . os_specific . append ( ( key , pp ( size_in_bytes ( value ) ) ) ) <EOL> stat . close ( ) <EOL> status . close ( ) <EOL> return True <EOL> try : <EOL> from resource import getrusage , RUSAGE_SELF <EOL> class _ProcessMemoryInfoResource ( _ProcessMemoryInfo ) : <EOL> def update ( self ) : <EOL> """<STR_LIT>""" <EOL> usage = getrusage ( RUSAGE_SELF ) <EOL> self . rss = usage . ru_maxrss * <NUM_LIT> <EOL> self . data_segment = usage . ru_idrss * <NUM_LIT> <EOL> self . shared_segment = usage . ru_ixrss * <NUM_LIT> <EOL> self . stack_segment = usage . ru_isrss * <NUM_LIT> <EOL> self . vsz = ( self . data_segment + self . shared_segment + <EOL> self . stack_segment ) <EOL> self . pagefaults = usage . ru_majflt <EOL> return self . rss != <NUM_LIT:0> <EOL> if _ProcessMemoryInfoProc ( ) . update ( ) : <EOL> ProcessMemoryInfo = _ProcessMemoryInfoProc <EOL> elif _ProcessMemoryInfoPS ( ) . update ( ) : <EOL> ProcessMemoryInfo = _ProcessMemoryInfoPS <EOL> elif _ProcessMemoryInfoResource ( ) . update ( ) : <EOL> ProcessMemoryInfo = _ProcessMemoryInfoResource <EOL> except ImportError : <EOL> try : <EOL> from win32process import GetProcessMemoryInfo <EOL> from win32api import GetCurrentProcess , GlobalMemoryStatusEx <EOL> except ImportError : <EOL> logging . warn ( "<STR_LIT>" ) <EOL> else : <EOL> class _ProcessMemoryInfoWin32 ( _ProcessMemoryInfo ) : <EOL> def update ( self ) : <EOL> process_handle = GetCurrentProcess ( ) <EOL> meminfo = GetProcessMemoryInfo ( process_handle ) <EOL> memstatus = GlobalMemoryStatusEx ( ) <EOL> self . vsz = ( memstatus [ '<STR_LIT>' ] - <EOL> memstatus [ '<STR_LIT>' ] ) <EOL> self . rss = meminfo [ '<STR_LIT>' ] <EOL> self . pagefaults = meminfo [ '<STR_LIT>' ] <EOL> return True <EOL> ProcessMemoryInfo = _ProcessMemoryInfoWin32 <EOL> class ThreadInfo ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , thread ) : <EOL> self . ident = <NUM_LIT:0> <EOL> try : <EOL> self . ident = thread . ident <EOL> except AttributeError : <EOL> pass <EOL> if not self . ident : <EOL> for tid , athread in threading . _active . items ( ) : <EOL> if athread is thread : <EOL> self . ident = tid <EOL> break <EOL> try : <EOL> self . name = thread . name <EOL> except AttributeError : <EOL> self . name = thread . getName ( ) <EOL> try : <EOL> self . daemon = thread . daemon <EOL> except AttributeError : <EOL> self . daemon = thread . isDaemon ( ) <EOL> def get_current_threads ( ) : <EOL> """<STR_LIT>""" <EOL> return [ ThreadInfo ( thread ) for thread in threading . enumerate ( ) ] <EOL> def get_current_thread_id ( ) : <EOL> """<STR_LIT>""" <EOL> return get_ident ( ) </s>
