"""
:filename: MemoryMonitor.py
:author: roar@tordivel.no
:requirements: Scorpion 9.1 or higher
Scorpion virtual memory monitoring, monitoring VM usage after grab and
after inspection, calculating bpi - "bytes per image", consumption
::
1.0.0.1, 11nov2015, RL: modified for autodoc
1.0.0.0, 04dec2012, RL: created
"""
__version__ = '1.0.0.1'
from Scorpion import GetControlByHandle,GetFloatValue,GetBoolValue,RegisterCallback,UnregisterCallback
[docs]class VirtualMemoryMonitor(object):
def __init__(self,hWnd,histlen,limit,delay):
self.cntr=GetControlByHandle(hWnd)
if self.cntr:
self.cntr.deleteControls()
self.bReset=self.cntr.addControl('Button')
self.bReset.caption='Reset'
self.bReset.height=20
self.bReset.width=40
self.bReset.top=(self.cntr.height-self.bReset.height)/2
self.bReset.onClick=self.resetClick
self.initmem=self.getmem()
self.limit=limit
self.histlen=histlen
self.delay=delay
self.verbose=1
self.reset(self.delay)
RegisterCallback('Actions.BeforeStart',self.beforeStart)
RegisterCallback('Actions.AfterStop',self.afterStop)
RegisterCallback('Actions.AfterGrab',self.afterGrab)
RegisterCallback('Actions.AfterInspect',self.afterInspect)
self.update()
def __del___(self):
if self.cntr:self.cntr.deleteControls()
UnregisterCallback('Actions.BeforeStart',self.beforeStart)
UnregisterCallback('Actions.AfterStop',self.afterStop)
UnregisterCallback('Actions.AfterGrab',afterGrab)
UnregisterCallback('Actions.AfterInspect',afterInspect)
def __str__(self):
stat=self.stat()
return 'Count %i/%i Used %.1f/%.1f KB bpi %i/%i bytes (i-g) %d bytes' %(max(self.cnt[0],0),max(self.cnt[1],0),stat[0]/1000,stat[1]/1000,stat[2],stat[3],stat[4])
[docs] def getmem(self):
return int(GetFloatValue('System.VirtualMemory')*1000000) #return bytes
[docs] def reset(self,delay):
mem=self.getmem()
self.orig=mem
self.cnt=[-delay,-delay]
self.mem=[mem,mem]
self.hist=[[(0,mem)]*self.histlen,[(0,mem)]*self.histlen]
self.update()
[docs] def beforeStart(self):
self.update()
if self.verbose:print '%-8s %s'%('Start',self)
[docs] def afterStop(self):
self.update()
if self.verbose:print '%-8s %s'%('Stop',self)
[docs] def inc(self,idx):
self.cnt[idx]+=1
self.mem[idx]=self.getmem()
if self.cnt[idx]>0:
if abs(self.mem[idx]-self.hist[idx][-1][1])>self.limit:
self.hist[idx].pop(0)
self.hist[idx].append((self.cnt[idx],self.mem[idx]))
if idx==1 and self.verbose>1:print '%-8s %s'%('Inspect',self)
elif self.cnt[idx]==0:
if idx==1:self.reset(0)
self.update()
[docs] def afterGrab(self):
self.inc(0)
[docs] def afterInspect(self):
self.inc(1)
[docs] def stat(self):
gdiff=self.mem[0]-self.orig
idiff=self.mem[1]-self.orig
if self.cnt[0]>0:gbpi=(self.mem[0]-self.orig)/self.cnt[0]
else:gbpi=0
if self.cnt[1]>0:ibpi=(self.mem[1]-self.orig)/self.cnt[1]
else:ibpi=0
diff=self.hist[1][-1][1]-self.hist[0][-1][1] #last mem in hist
return (gdiff,idiff,gbpi,ibpi,diff)
[docs] def bpi(self,idx):
lst=[]
for i in self.hist[idx]:
if i[0]>0:
lst.append((i[0],(i[1]-self.orig)/i[0]))
else:
lst.append((i[0],0))
return lst
[docs] def update(self):
if self.cntr:
if GetBoolValue('System.Running'):
stat=self.stat()
self.cntr.caption='Count %i Used %.1f KB bpi %i'%(max(self.cnt[0],0),stat[0]/1000,stat[2]) #show grab values
else:
self.cntr.caption=str(self)
[docs] def resetClick(self,sender,args):
self.reset(0)
[docs]def CreatePlugin(hWnd,name=''):
'''
Scorpion Plugin Stub - Required
'''
return VirtualMemoryMonitor(hWnd,100,50,50)