Ignore:
Timestamp:
Dec 12, 2011, 2:36:51 PM (11 years ago)
Author:
neweyv
Message:

Additional information written to the log file. This information will assist in creating an algorithm to predict the runtime of ANUGA given various parameters.

File:
1 edited

Legend:

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

    r7854 r8279  
    3636import traceback
    3737import logging
     38import datetime
    3839
    3940DefaultConsoleLogLevel = logging.CRITICAL
    4041DefaultFileLogLevel = logging.INFO
    41 
     42TimingDelimiter ='#@# '
    4243
    4344################################################################################
     
    203204
    204205    log(msg, logging.CRITICAL)
     206
     207def timingInfo(msg=''):
     208    '''Shortcut for log(timingDelimiter, msg).'''
     209
     210    log(TimingDelimiter + msg, logging.INFO)
    205211
    206212
     
    286292        log(msg, level)
    287293
    288 
    289 ################################################################################
    290 
     294def CurrentDateTime():
     295    return datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
     296
     297
     298def resource_usage_timing(level=logging.INFO, prefix =""):
     299    '''Log memory usage at given log level.'''
     300
     301    _scale = {'KB': 1024, 'MB': 1024*1024, 'GB': 1024*1024*1024,
     302              'kB': 1024, 'mB': 1024*1024, 'gB': 1024*1024*1024}
     303
     304    if sys.platform != 'win32':
     305        _proc_status = '/proc/%d/status' % os.getpid()
     306       
     307        def _VmB(VmKey):
     308            '''Get number of virtual bytes used.'''
     309
     310            # get pseudo file /proc/<pid>/status
     311            try:
     312                t = open(_proc_status)
     313                v = t.read()
     314                t.close()
     315            except IOError:
     316                return 0.0
     317
     318            # get VmKey line, eg: 'VmRSS: 999 kB\n ...
     319            i = v.index(VmKey)
     320            v = v[i:].split(None, 3)
     321            if len(v) < 3:
     322                return 0.0
     323
     324            # convert Vm value to bytes
     325            return float(v[1]) * _scale[v[2]]
     326
     327        def memory(since=0.0):
     328            '''Get virtual memory usage in bytes.'''
     329
     330            return _VmB('VmSize:') - since
     331
     332        def resident(since=0.0):
     333            '''Get resident memory usage in bytes.'''
     334
     335            return _VmB('VmRSS:') - since
     336
     337        def stacksize(since=0.0):
     338            '''Get stack size in bytes.'''
     339
     340            return _VmB('VmStk:') - since
     341
     342        msg = ('Resource usage: memory=%.1fMB resident=%.1fMB stacksize=%.1fMB'
     343               % (memory()/_scale['MB'], resident()/_scale['MB'],
     344                  stacksize()/_scale['MB']))
     345        log(msg, level)
     346        timingInfo('sys_platform, ' + sys.platform)
     347        timingInfo(prefix + 'memory, ' + str(memory()/_scale['MB']))
     348        timingInfo(prefix + 'resident, ' + str(resident()/_scale['MB']))
     349        timingInfo(prefix + 'stacksize, ' + str(stacksize()/_scale['MB']))
     350    else:
     351        # Windows code from: http://code.activestate.com/recipes/511491/
     352        try:
     353            import ctypes
     354            import _winreg
     355        except:
     356            log(level, 'Windows resource usage not available')
     357            return
     358
     359        kernel32 = ctypes.windll.kernel32
     360        c_ulong = ctypes.c_ulong
     361        c_ulonglong = ctypes.c_ulonglong
     362        class MEMORYSTATUSEX(ctypes.Structure):
     363            _fields_ = [('dwLength', c_ulong),
     364                        ('dwMemoryLoad', c_ulong),
     365                        ('ullTotalPhys', c_ulonglong),
     366                        ('ullAvailPhys', c_ulonglong),
     367                        ('ullTotalPageFile', c_ulonglong),
     368                        ('ullAvailPageFile', c_ulonglong),
     369                        ('ullTotalVirtual', c_ulonglong),
     370                        ('ullAvailVirtual', c_ulonglong),
     371                        ('ullAvailExtendedVirtual', c_ulonglong)
     372                       ]
     373
     374        memoryStatusEx = MEMORYSTATUSEX()
     375        memoryStatusEx.dwLength = ctypes.sizeof(MEMORYSTATUSEX)
     376        kernel32.GlobalMemoryStatusEx(ctypes.byref(memoryStatusEx))
     377
     378        msg = ('Resource usage: total memory=%.1fMB free memory=%.1fMB'
     379               % (memoryStatusEx.ullTotalPhys/_scale['MB'],
     380                  memoryStatusEx.ullAvailPhys/_scale['MB']))
     381        log(msg, level)
     382        timingInfo('sys_platform, ' + sys.platform)
     383        timingInfo(prefix + 'total_memory, ' + str(memoryStatusEx.ullTotalPhys/_scale['MB']))
     384        timingInfo(prefix + 'free_memory, ' + str(memoryStatusEx.ullAvailPhys/_scale['MB']))
     385
     386   
     387################################################################################
    291388if __name__ == '__main__':
    292389    critical('#' * 80)
Note: See TracChangeset for help on using the changeset viewer.