User Tools

Site Tools


cpumodesexample

Processes I/O

Source code:

#!python
# make available the necessary modules
import warnings
warnings.filterwarnings(action='ignore',
                        message='tempnam is a potential security risk.*',
                        category=RuntimeWarning)
import sys,posix,pprint,re,os
from vms.starlet import getjpiw
from vms.ssdef import SS__NOMOREPROC
from vms.jpidef import JPI__PID, JPI__PRCNAM
from vms.itemList import *
from vms.jpidef import *
from vms.crtl import to_vms
 
# list which processes exist
def show_processes(f):
    it = [itemList(code = JPI__PID, dtype = il_signedLong),
          itemList(code = JPI__PRCNAM, dtype = il_string)]
    prcnam = {}
    ctx = -1
    try:
        while(1):     # let loop terminate by exception
            s, ctx, dic = getjpiw(pid = ctx, itmlst = it)
            prcnam[dic[JPI__PID]] = dic[JPI__PRCNAM]
            print >> f, 'set proc/id= %X' % dic[JPI__PID]
            print >> f, 'exam pcb+pcb$l_kernel_counter'
            print >> f, 'exam pcb+pcb$l_exec_counter'
            print >> f, 'exam pcb+pcb$l_super_counter'
            print >> f, 'exam pcb+pcb$l_user_counter'
    except VMSError, e: # any exception aborts the loop
        if e.errno != SS__NOMOREPROC: raise
    return prcnam
 
def modes(fncmd, fnlst):
    f = open(fncmd, 'w')
#   to avoid extension while writing fnlst
    print >> f, '$ set rms/ext=1000'
    print >> f, '$ ana/sys'
    print >> f, 'read sysdef'
    print >> f, 'set log', to_vms(fnlst)+'.'
 
    prcnam = show_processes(f)
 
    f.close()
    s=os.popen('@'+to_vms(fncmd)+'.').read()
    f=open(fnlst, 'r')
 
    # regular expression to search for the 2 strings
    # "set proc/id=" and "SDA>"
 
    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
 
    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
 
    for line in f:
         mo = m.match(line)
         if mo:
             if line[:13] == 'SDA> set proc':
                 # process id is the first element
                 modes = [int(line[18:-1], 16)]
             else:
                 # append modes KESU ticks
                 v = mo.group('v2')
                 if v == None:
                     v = mo.group('v1')
                 modes.append(float(long(v,16)))
                 if len(modes) == 5:
                     sum = modes[1] + modes[2] + modes[3] + modes[4]
                     modes.append(sum)
                     if sum == 0:
                         sum = 1
                     modes.append(float(modes[1] * 100 / sum))
                     modes.append(float(modes[2] * 100 / sum))
                     modes.append(float(modes[3] * 100 / sum))
                     modes.append(float(modes[4] * 100 / sum))
                     processes.append(modes)
 
    f.close()
 
    lib = ['By PID', 'By Kernel', 'By Exec', 'By Super', 'By User', 'By Total',
            'By % Kernel', 'By % Exec', 'By % Super', 'By % User']
 
    #   format the results
    fmt = "%-16s %8X %10.0f %10.0f %10.0f %10.0f %10.0f"
    fmt += "       %6.2f  %6.2f  %6.2f  %6.2f"
    head = "proc. name            pid     kernel       exec      super       "
    head += "user      total"
    head += "           %K      %E      %S      %U"
    for v in range(min_stat,max_stat):
         print lib[v]
         print head
    # sort the array
         processes.sort((lambda x,y: cmp(y[v], x[v])))
 
    # print the results
         for x in processes[:max_process]:
             y = [prcnam[x[0]]] + x
             print fmt % tuple(y)
         print
 
 
 
# if no parameter is given, the default of 4 processes will be displayed
max_process = 4
if len(sys.argv) > 1:
     max_process = int(sys.argv[1])
 
# up to 10 criteria are displayed, see lib at the end
min_stat = 1
max_stat = 10
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
fncmd = os.tempnam('/sys$scratch')
 
# listing file
fnlst  = os.tempnam('/sys$scratch')
 
try:
    modes(fncmd,fnlst)
except Exception, e:
    print e
 
# delete the 2 temporary files
os.unlink(fncmd)
os.unlink(fnlst)

Result:

$ python modes.py
By Kernel
proc. name            pid     kernel       exec      super       user      total           %K      %E      %S      %U
BATCH_213        20200154     118965      64943       2471     136455     322834        36.85   20.12    0.77   42.27
AUDIT_SERVER     2020010E      17971      12092          0      27538      57601        31.20   20.99    0.00   47.81
MYSQL_SERVER     20200156      15018       1769        220      85675     102682        14.63    1.72    0.21   83.44
CONNECTION_UP    2020011B      11654       6297        444        749      19144        60.88   32.89    2.32    3.91

By Exec
proc. name            pid     kernel       exec      super       user      total           %K      %E      %S      %U
BATCH_213        20200154     118965      64943       2471     136455     322834        36.85   20.12    0.77   42.27
AUDIT_SERVER     2020010E      17971      12092          0      27538      57601        31.20   20.99    0.00   47.81
CONNECTION_UP    2020011B      11654       6297        444        749      19144        60.88   32.89    2.32    3.91
MYSQL_SERVER     20200156      15018       1769        220      85675     102682        14.63    1.72    0.21   83.44

By Super
proc. name            pid     kernel       exec      super       user      total           %K      %E      %S      %U
BATCH_213        20200154     118965      64943       2471     136455     322834        36.85   20.12    0.77   42.27
CONNECTION_UP    2020011B      11654       6297        444        749      19144        60.88   32.89    2.32    3.91
MYSQL_SERVER     20200156      15018       1769        220      85675     102682        14.63    1.72    0.21   83.44
SYSTEM_3         2020A4A7        240        200          7        326        773        31.05   25.87    0.91   42.17

By User
proc. name            pid     kernel       exec      super       user      total           %K      %E      %S      %U
BATCH_213        20200154     118965      64943       2471     136455     322834        36.85   20.12    0.77   42.27
MYSQL_SERVER     20200156      15018       1769        220      85675     102682        14.63    1.72    0.21   83.44
AUDIT_SERVER     2020010E      17971      12092          0      27538      57601        31.20   20.99    0.00   47.81
SMTP_BISOU_01    20200127       5295       1489          0       9471      16255        32.57    9.16    0.00   58.27

By Total
proc. name            pid     kernel       exec      super       user      total           %K      %E      %S      %U
BATCH_213        20200154     118965      64943       2471     136455     322834        36.85   20.12    0.77   42.27
MYSQL_SERVER     20200156      15018       1769        220      85675     102682        14.63    1.72    0.21   83.44
AUDIT_SERVER     2020010E      17971      12092          0      27538      57601        31.20   20.99    0.00   47.81
CONNECTION_UP    2020011B      11654       6297        444        749      19144        60.88   32.89    2.32    3.91

By % Kernel
proc. name            pid     kernel       exec      super       user      total           %K      %E      %S      %U
SWAPPER          20200101       1015          0          0          0       1015       100.00    0.00    0.00    0.00
LES$ACP_V30      20200116          4          0          0          0          4       100.00    0.00    0.00    0.00
TP_SERVER        2020011A          3          0          0          0          3       100.00    0.00    0.00    0.00
TCPIP$INETACP    20200121       6409          2          0          0       6411        99.97    0.03    0.00    0.00

By % Exec
proc. name            pid     kernel       exec      super       user      total           %K      %E      %S      %U
TCPIP$PWIP_ACP   20200126          1          2          0          0          3        33.33   66.67    0.00    0.00
HTTPd:80-76      2020BBA9          0          2          1          0          3         0.00   66.67   33.33    0.00
DTGREET          20200168          8         38          0         12         58        13.79   65.52    0.00   20.69
RDMS_MONITOR     2020013C          1          1          0          0          2        50.00   50.00    0.00    0.00

By % Super
proc. name            pid     kernel       exec      super       user      total           %K      %E      %S      %U
HTTPd:80-76      2020BBA9          0          2          1          0          3         0.00   66.67   33.33    0.00
GENERI004000271  20200148         12          7          3          1         23        52.17   30.43   13.04    4.35
sqlsrv_mon_0071  20200145          8          1          1          1         11        72.73    9.09    9.09    9.09
GENERI004000171  20200147         22          7          2          1         32        68.75   21.88    6.25    3.12

By % User
proc. name            pid     kernel       exec      super       user      total           %K      %E      %S      %U
HTTPd:80-82      2020BEAE         17          2          2        163        184         9.24    1.09    1.09   88.59
MYSQL_SERVER     20200156      15018       1769        220      85675     102682        14.63    1.72    0.21   83.44
SECURITY_SERVER  20200112        204         12          0        914       1130        18.05    1.06    0.00   80.88
REGISTRY_SERVER  20200139        148          3          0        617        768        19.27    0.39    0.00   80.34

$
cpumodesexample.txt · Last modified: 2014/09/16 19:09 by wikiadmin