Ignore:
Timestamp:
Jan 17, 2012, 1:05:00 PM (12 years ago)
Author:
pittj
Message:

Adding memory functions

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/anuga_core/source/anuga/utilities/system_tools.py

    r8145 r8299  
    1212import tarfile
    1313import warnings
     14import pdb
    1415
    1516try:
     
    567568
    568569
     570#### Memory functions
     571_proc_status = '/proc/%d/status' % os.getpid()
     572_scale = {'kB': 1024.0, 'mB': 1024.0*1024.0,
     573          'KB': 1024.0, 'MB': 1024.0*1024.0}
     574_total_memory = 0.0
     575_last_memory = 0.0
     576
     577def _VmB(VmKey):
     578    '''private method'''
     579    global _proc_status, _scale
     580     # get pseudo file  /proc/<pid>/status
     581    try:
     582        t = open(_proc_status)
     583        v = t.read()
     584        t.close()
     585    except:
     586        return 0.0  # non-Linux?
     587     # get VmKey line e.g. 'VmRSS:  9999  kB\n ...'
     588    i = v.index(VmKey)
     589    v = v[i:].split(None, 3)  # whitespace
     590    if len(v) < 3:
     591        return 0.0  # invalid format?
     592     # convert Vm value to MB
     593    return (float(v[1]) * _scale[v[2]]) / (1024.0*1024.0)
     594
     595
     596def MemoryUpdate(print_msg=None,str_return=False):
     597    '''print memory usage stats in MB.
     598    '''
     599    global _total_memory, _last_memory
     600
     601    _last_memory = _total_memory
     602    _total_memory = _VmB('VmSize:')
     603
     604    #if print_msg is not None:
     605    mem_diff = _total_memory - _last_memory
     606    return (mem_diff,_total_memory)
     607   
     608
Note: See TracChangeset for help on using the changeset viewer.