CPUModesExample

Processes I/O

Source code:

   1 # make available the necessary modules
   2 import warnings
   3 warnings.filterwarnings(action='ignore',
   4                         message='tempnam is a potential security risk.*',
   5                         category=RuntimeWarning)
   6 import sys,posix,pprint,re,os
   7 from vms.starlet import getjpiw
   8 from vms.ssdef import SS__NOMOREPROC
   9 from vms.jpidef import JPI__PID, JPI__PRCNAM
  10 from vms.itemList import *
  11 from vms.jpidef import *
  12 from vms.crtl import to_vms
  13 
  14 # list which processes exist
  15 def show_processes(f):
  16     it = [itemList(code = JPI__PID, dtype = il_signedLong),
  17           itemList(code = JPI__PRCNAM, dtype = il_string)]
  18     prcnam = {}
  19     ctx = -1
  20     try:
  21         while(1):     # let loop terminate by exception
  22             s, ctx, dic = getjpiw(pid = ctx, itmlst = it)
  23             prcnam[dic[JPI__PID]] = dic[JPI__PRCNAM]
  24             print >> f, 'set proc/id= %X' % dic[JPI__PID]
  25             print >> f, 'exam pcb+pcb$l_kernel_counter'
  26             print >> f, 'exam pcb+pcb$l_exec_counter'
  27             print >> f, 'exam pcb+pcb$l_super_counter'
  28             print >> f, 'exam pcb+pcb$l_user_counter'
  29     except VMSError, e: # any exception aborts the loop
  30         if e.errno != SS__NOMOREPROC: raise
  31     return prcnam
  32 
  33 def modes(fncmd, fnlst):
  34     f = open(fncmd, 'w')
  35 #   to avoid extension while writing fnlst
  36     print >> f, '$ set rms/ext=1000'
  37     print >> f, '$ ana/sys'
  38     print >> f, 'read sysdef'
  39     print >> f, 'set log', to_vms(fnlst)+'.'
  40 
  41     prcnam = show_processes(f)
  42 
  43     f.close()
  44     s=os.popen('@'+to_vms(fncmd)+'.').read()
  45     f=open(fnlst, 'r')
  46 
  47     # regular expression to search for the 2 strings
  48     # "set proc/id=" and "SDA>"
  49 
  50     m=re.compile('^(SDA> set proc|.*:  (?P<v1>(\d|[A-F]){8,8})(\.(?P<v2>(\d|[A-F]){8,8}))?)')
  51 
  52     # define an array containing the pid of all the processes
  53 
  54     processes = []
  55 
  56     # the array modes is populated with
  57     # pid, modes kernel, modes executive, modes supervisor, mode user
  58     # total modes
  59     # percent mode kernel, percent mode exec, percent mode supervisor
  60     # percent mode user
  61 
  62     for line in f:
  63          mo = m.match(line)
  64          if mo:
  65              if line[:13] == 'SDA> set proc':
  66                  # process id is the first element
  67                  modes = [int(line[18:-1], 16)]
  68              else:
  69                  # append modes KESU ticks
  70                  v = mo.group('v2')
  71                  if v == None:
  72                      v = mo.group('v1')
  73                  modes.append(float(long(v,16)))
  74                  if len(modes) == 5:
  75                      sum = modes[1] + modes[2] + modes[3] + modes[4]
  76                      modes.append(sum)
  77                      if sum == 0:
  78                          sum = 1
  79                      modes.append(float(modes[1] * 100 / sum))
  80                      modes.append(float(modes[2] * 100 / sum))
  81                      modes.append(float(modes[3] * 100 / sum))
  82                      modes.append(float(modes[4] * 100 / sum))
  83                      processes.append(modes)
  84 
  85     f.close()
  86 
  87     lib = ['By PID', 'By Kernel', 'By Exec', 'By Super', 'By User', 'By Total',
  88             'By % Kernel', 'By % Exec', 'By % Super', 'By % User']
  89 
  90     #   format the results
  91     fmt = "%-16s %8X %10.0f %10.0f %10.0f %10.0f %10.0f"
  92     fmt += "       %6.2f  %6.2f  %6.2f  %6.2f"
  93     head = "proc. name            pid     kernel       exec      super       "
  94     head += "user      total"
  95     head += "           %K      %E      %S      %U"
  96     for v in range(min_stat,max_stat):
  97          print lib[v]
  98          print head
  99     # sort the array
 100          processes.sort((lambda x,y: cmp(y[v], x[v])))
 101 
 102     # print the results
 103          for x in processes[:max_process]:
 104              y = [prcnam[x[0]]] + x
 105              print fmt % tuple(y)
 106          print
 107 
 108 
 109 
 110 # if no parameter is given, the default of 4 processes will be displayed
 111 max_process = 4
 112 if len(sys.argv) > 1:
 113      max_process = int(sys.argv[1])
 114 
 115 # up to 10 criteria are displayed, see lib at the end
 116 min_stat = 1
 117 max_stat = 10
 118 if len(sys.argv) > 2:
 119      min_stat = int(sys.argv[2])
 120      max_stat = min_stat + 1
 121 
 122      if min_stat > 10:
 123          min_stat = 10
 124 
 125 # we create 2 temporary files in sys$scratch
 126 # because we should always be allowed to write in sys$scratch
 127 
 128 # command file
 129 fncmd = os.tempnam('/sys$scratch')
 130 
 131 # listing file
 132 fnlst  = os.tempnam('/sys$scratch')
 133 
 134 try:
 135     modes(fncmd,fnlst)
 136 except Exception, e:
 137     print e
 138 
 139 # delete the 2 temporary files
 140 os.unlink(fncmd)
 141 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

$
Recent