forum.vmspython.org Forum Index forum.vmspython.org
Forum system
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

using trace.py

 
Post new topic   Reply to topic    forum.vmspython.org Forum Index -> Python for OpenVMS
View previous topic :: View next topic  
Author Message
bisonmalembouche



Joined: 01 Jun 2005
Posts: 74

PostPosted: Fri Aug 18, 2006 10:00 am    Post subject: using trace.py Reply with quote

Hello

Just a short note to inform those who didn't know, trace.py is handy for checking some code.

I just used it
$ python python_root:[lib]trace.py --count myprog.py
and I have a new file myprog.cover, with at the beggining of each line of code, the number of times it has executed.

For example, with modes.py,

http://vmspython.dyndns.org/cgiplus-bin/pythoncolor?script=/demosrc/modes.py

it gives

# make available the necessary modules
1: import warnings
1: warnings.filterwarnings(action='ignore',
1: message='tempnam is a potential security risk.*',
1: category=RuntimeWarning)
1: import sys,posix,pprint,re,os
1: from vms.starlet import getjpiw
1: from vms.ssdef import SS__NOMOREPROC
1: from vms.jpidef import JPI__PID, JPI__PRCNAM
1: from vms.itemList import *
1: from vms.jpidef import *
1: from vms.crtl import to_vms

# list which processes exist
1: def show_processes(f):
1: it = [itemList(code = JPI__PID, dtype = il_signedLong),
1: itemList(code = JPI__PRCNAM, dtype = il_string)]
1: prcnam = {}
1: ctx = -1
1: try:
1: while(1): # let loop terminate by exception
84: s, ctx, dic = getjpiw(pid = ctx, itmlst = it)
83: prcnam[dic[JPI__PID]] = dic[JPI__PRCNAM]
83: print >> f, 'set proc/id= %X' % dic[JPI__PID]
83: print >> f, 'exam pcb+pcb$l_kernel_counter'
83: print >> f, 'exam pcb+pcb$l_exec_counter'
83: print >> f, 'exam pcb+pcb$l_super_counter'
83: print >> f, 'exam pcb+pcb$l_user_counter'
1: except VMSError, e: # any exception aborts the loop
1: if e.errno != SS__NOMOREPROC: raise
1: return prcnam

1: def modes(fncmd, fnlst):
1: f = open(fncmd, 'w')
# to avoid extension while writing fnlst
1: print >> f, '$ set rms/ext=1000'
1: print >> f, '$ ana/sys'
1: print >> f, 'read sysdef'
1: print >> f, 'set log', to_vms(fnlst)+'.'

1: prcnam = show_processes(f)

1: f.close()
1: s=os.popen('@'+to_vms(fncmd)+'.').read()
1: f=open(fnlst, 'r')

# regular expression to search for the 2 strings
# "set proc/id=" and "SDA>"

1: m=re.compile('^(SDA> set proc|.*: (?P<v1>(\d|[A-F]){8,8})(\.(?P<v2>(
\d|[A-F]){8,8}))?)')

# define an array containing the pid of all the processes

1: processes = []

# the array modes is populated with
# pid, modes kernel, modes executive, modes supervisor, mode user
# total modes
# percent mode kernel, percent mode exec, percent mode supervisor
# percent mode user

1: for line in f:
747: mo = m.match(line)
747: if mo:
415: if line[:13] == 'SDA> set proc':
# process id is the first element
83: modes = [int(line[18:-1], 16)]
else:
# append modes KESU ticks
332: v = mo.group('v2')
332: if v == None:
166: v = mo.group('v1')
332: modes.append(float(long(v,16)))
332: if len(modes) == 5:
83: sum = modes[1] + modes[2] + modes[3] + modes[4]
83: modes.append(sum)
83: if sum == 0:
8: sum = 1
83: modes.append(float(modes[1] * 100 / sum))
83: modes.append(float(modes[2] * 100 / sum))
83: modes.append(float(modes[3] * 100 / sum))
83: modes.append(float(modes[4] * 100 / sum))
83: processes.append(modes)

1: f.close()

1: lib = ['By PID', 'By Kernel', 'By Exec', 'By Super', 'By User', 'By T
otal', 'By % Kernel', 'By % Exec', 'By % Super', 'By % User']

# format the results
1: fmt = "%-16s %8X %10.0f %10.0f %10.0f %10.0f %10.0f"
1: fmt += " %6.2f %6.2f %6.2f %6.2f"
1: head = "proc. name pid kernel exec super
"
1: head += "user total"
1: head += " %K %E %S %U"
1: for v in range(min_stat,max_stat):
9: print lib[v]
9: print head
# sort the array
3581: processes.sort((lambda x,y: cmp(y[v], x[v])))

# print the results
9: for x in processes[:max_process]:
36: y = [prcnam[x[0]]] + x
36: print fmt % tuple(y)
9: print



# if no parameter is given, the default of 4 processes will be displayed
1: max_process = 4
1: if len(sys.argv) > 1:
max_process = int(sys.argv[1])

# up to 10 criteria are displayed, see lib at the end
1: min_stat = 1
1: max_stat = 10
1: if len(sys.argv) > 2:
min_stat = int(sys.argv[2])
max_stat = min_stat + 1

if min_stat > 10:
min_stat = 10

# we create 2 temporary files in sys$scratch
# because we should always be allowed to write in sys$scratch

# command file
1: fncmd = os.tempnam('/sys$scratch')

# listing file
1: fnlst = os.tempnam('/sys$\scratch')

1: try:
1: modes(fncmd,fnlst)
except Exception, e:
print e

# delete the 2 temporary files
1: os.unlink(fncmd)
1: os.unlink(fnlst)
Back to top
View user's profile Send private message Send e-mail
bisonmalembouche



Joined: 01 Jun 2005
Posts: 74

PostPosted: Fri Aug 18, 2006 11:11 am    Post subject: trace.py Reply with quote

A small update: JFP tells me that

We should always use the Unix-style syntax, so

$ python /python_root/lib/trace.py ...


Various problems are related to the Vms syntax.
Back to top
View user's profile Send private message Send e-mail
Display posts from previous:   
Post new topic   Reply to topic    forum.vmspython.org Forum Index -> Python for OpenVMS All times are GMT + 2 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group