home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 18 REXX / 18-REXX.zip / pyrxutil.zip / kill'em.cmd next >
OS/2 REXX Batch file  |  2001-10-19  |  13KB  |  431 lines

  1. /*PYRxUtil demo*/
  2. /* î«¡¿Γ«α »α«µÑßß«ó .
  3.    Kill 'em all ... */
  4. /*trace i*/
  5. SLICE = 32 ; SCAN_DT = 1000
  6. P_I_SIZE = 40
  7.  
  8. P_I_FG = 'a'x ; P_I_SEL = '3f'x
  9. ACT_BG = '50'x ; IDLE_BG = '10'x
  10. PATH_ATTR = '7'x ; HEAD_ATTR = '72'x
  11. SUM_ATTR = '80'x ; KEY_ATTR = '71'x
  12. REG_U_BG = '40'x ; REG_S_BG = '60'x
  13. IDL_U_BG = '50'x ; IDL_S_BG = '10'x
  14. CPU_B_BG = '40'x ; CPU_I_BG = '60'x ;
  15.  
  16. LEFT_HEAD = ' PID  PPID  Type   Time used     Peak'
  17. EXE_HEAD = 'Exe Name' ; WIN_HEAD = 'Window title' 
  18. THR_HEAD = ' TID Slot Priort State  SleepID   Peak'
  19. CPU_HEAD = '  N   Busy    Intr    Idle    Freq'
  20. PATH = 0 ; TITLE = 1 ; THREAD = 3 ; CPU = 4
  21. rt_view = PATH
  22. INFO.0 = "info_tx = '<P>path <W>title <T>threads <C>CPU  ('||'18'x||' '||'19'x||' <PgUp><PgDn><Home><End><Del>)+<Ctrl>'"
  23. INFO.1 = "info_tx = '  Kill process (ID='s_pid') Y/N ?'"
  24. INFO.2 = "info_tx = '  Kill -9 process (ID='s_pid') Y/N ?'"
  25. INFO.3 = "info_tx = '  <0>reboot  <1>standby  <2>suspend  <3>power_off'"
  26. F_coeff = 1000
  27.  
  28. signal on HALT name do_exit
  29. call RxFuncAdd 'PYRxInit', 'PYRxUtil', 'PYRxInit'
  30. call PYRxInit
  31. my_pid = PYRxGo("p","Kill 'em all... (c)PYzone 2001") ; s_pid = my_pid
  32. parse value PYRxShow(0,0,0) with . . ymx xmx .
  33. call PYRxShow ,,ymx*xmx,' ','7'x
  34. INF_LINES = ymx - 3
  35. call PYRxShow ,,0,left(left(LEFT_HEAD,P_I_SIZE)||EXE_HEAD,xmx),HEAD_ATTR
  36. sys_time = 0 ; s_off = 1 ; draw_time = 0 ; q_kill = 0
  37.  
  38. do forever
  39.   timer = info_tx
  40.   interpret INFO.q_kill
  41.   if info_tx \= timer then
  42.     call PYRxShow ymx-1,0,0,left(info_tx,xmx),KEY_ATTR
  43.   timer = PYRxGo('i','.............?')
  44.   s_thn = 0
  45.   if timer - sys_time >= SCAN_DT - draw_time then
  46.     do
  47.       drop pl. pid. ppid.    
  48.       sys_time = timer 
  49.       wait = SCAN_DT
  50.       call PYRxGo 'P','*','*','pl'
  51.       do i=1 to pl.0  
  52.         parse var pl.i pid.i ppid.i . . p_thn .
  53.         s_thn = s_thn + p_thn
  54.         if pid.i = s_pid then
  55.           s_idx = i
  56.        end
  57.       sum_txt = '  Summary: 'pl.0' processes with 's_thn' threads .  '
  58.       call PYRxShow ymx-2,0,0,sum_txt,SUM_ATTR
  59.       if rt_view = PATH then
  60.         do
  61.           attr = SUM_ATTR
  62.           if sys_time > 4208567295 then
  63.             attr = bitor(SUM_ATTR,'4'x)
  64.           if sys_time > 4291367295 then
  65.             attr = bitor(SUM_ATTR,'c'x)
  66.           call PYRxShow ymx-2,length(sum_txt),0,left('System timer:',
  67.                              ||t_conv(sys_time),xmx-length(sum_txt)),attr
  68.          end
  69.       if rt_view = TITLE then
  70.         call PYRxShow ymx-2,length(sum_txt),0,left('System time: '||date()||'  'time(),,
  71.                                xmx-length(sum_txt)),SUM_ATTR
  72.       call get_cpu
  73.       if rt_view = CPU then
  74.         call show_cpu_info
  75.      end
  76.   do while s_idx > pl.0
  77.     s_idx = pl.0 ; s_pid = pid.s_idx ; s_off = 0
  78.    end
  79.   y_prc = 1
  80.   do i=1 to pl.0 + INF_LINES while y_prc <= INF_LINES
  81.     mask = ' '||ppid.i||' '
  82.     parse var chain chain (mask) +1 alive .
  83.     if alive = '' then chain = ''
  84.     chain = ' 'chain' 'alive' 'pid.i' '
  85.     if i < s_idx - s_off then
  86.       iterate
  87.     if i <= pl.0 then
  88.       call show_p_info
  89.      else
  90.       call PYRxShow y_prc,0,P_I_SIZE+(xmx-P_I_SIZE)*(rt_view\=THREAD),' ','7'x
  91.     y_prc = y_prc + 1
  92.    end
  93.   if rt_view = THREAD then
  94.     do
  95.       y_thr = 1 ; chain = '' ; drop tl.    
  96.       call PYRxGo 'P',,s_pid,'tl'
  97.       do while s_pid \= t_pid
  98.         call drop_slots
  99.         t_pid = s_pid ; t_mem = 0
  100.        end
  101.       call PYRxShow ymx-2,length(sum_txt),0,left('Process 't_pid' have ',
  102.                              tl.0' thread(s) .',xmx-length(sum_txt)),SUM_ATTR
  103.       do i=1 to tl.0 + INF_LINES
  104.         if i <= tl.0 then
  105.           call show_t_info
  106.          else
  107.           if y_thr <= INF_LINES then
  108.             do
  109.               call PYRxShow y_thr,P_I_SIZE,(xmx-P_I_SIZE),' ','7'x
  110.               y_thr = y_thr + 1
  111.              end
  112.        end
  113.      end
  114.   
  115.   call drop_slots
  116.   draw_time = PYRxGo('i','.............?') - timer
  117.   wait = wait - draw_time
  118.   if wait <= draw_time then
  119.     iterate
  120.   parse value PYRxKey(wait) with char scan state
  121.   parse upper value x2c(char) with char
  122.   select
  123.     when scan = '00' & char = '0'x then
  124.       if q_kill \= 0 then
  125.         do
  126.           attr = bitor(KEY_ATTR,d2c(7*((timer%1000)//2)))
  127.           call PYRxShow ymx-1,2,length(info_tx),,attr
  128.          end
  129.     when scan='01' then    /*Esc*/
  130.       exit
  131.     when q_kill \= 0 then
  132.       do
  133.         if q_kill = 3 then
  134.           do
  135.             if pos(char,'0123') \= 0 then
  136.               call PYRxGo 'p','-',char
  137.            end
  138.          else
  139.           if char = 'y' | char = 'Y' then
  140.             if q_kill = 1 then
  141.               call PYRxGo 'p','-',s_pid
  142.              else
  143.               call PYRxGo 'P','-',s_pid
  144.         q_kill = 0
  145.        end
  146.     when scan='0F' then    /*Tab*/
  147.       call PYRxGo 't','.'
  148.     when scan='1C' then    /*Enter*/
  149.       call PYRxGo 'p','!',s_pid
  150.     when scan = 48 then    /*Up*/
  151.       if s_idx > 1 then
  152.         do
  153.       s_idx = s_idx-1
  154.       s_off = s_off - (s_off > 0)
  155.      end
  156.     when scan = 50 then    /*Down*/
  157.       if s_idx < pl.0 then
  158.         do
  159.           s_idx = s_idx+1
  160.       s_off = s_off + (s_off < INF_LINES-1)
  161.      end
  162.     when scan = 49 then    /*PgUp*/
  163.       do
  164.         s_idx = s_idx - INF_LINES
  165.         if s_idx < 1 then s_idx = 1
  166.         if s_idx - s_off < 1 then s_off = s_idx - 1
  167.        end
  168.     when scan = 51 then    /*PgDn*/
  169.       do
  170.         s_idx = s_idx + INF_LINES - 1
  171.         do while s_idx > pl.0
  172.           s_idx = pl.0
  173.           s_off = (INF_LINES-1)*(pl.0 > INF_LINES)
  174.          end
  175.         if s_idx - s_off < 1 then s_off = 0
  176.        end
  177.     when scan = 47 then /* Home */
  178.       do
  179.         s_idx = s_idx - s_off
  180.         s_off = 0
  181.        end
  182.     when scan = '4F' then /*End*/
  183.       do  
  184.         s_idx = s_idx + INF_LINES - s_off - 1
  185.         if s_idx > pl.0 then s_idx = pl.0
  186.         s_off = INF_LINES - 1
  187.         if s_off > pl.0 then s_off = pl.0 - 1
  188.        end
  189.     when scan = 53 then    /* Del */
  190.       if s_pid \= my_pid then
  191.         q_kill = 1 + 2*(s_pid <= 3)
  192.     when scan = 93 then    /* Ctrl-Del */
  193.       if s_pid \= my_pid then
  194.         q_kill = 2 + (s_pid <= 3)
  195.     when char = 'T' then /* Threads view */
  196.       do
  197.         rt_view = THREAD ; drop t_pid ; t_idx = 1
  198.         call PYRxShow 0,P_I_SIZE,0,left(THR_HEAD,xmx-P_I_SIZE),HEAD_ATTR
  199.        end
  200.     when char = 'C' then /* CPU view */
  201.       do
  202.         rt_view = CPU ; t_idx = 1
  203.         call PYRxShow 0,P_I_SIZE,0,left(CPU_HEAD,xmx-P_I_SIZE),HEAD_ATTR
  204.        end
  205.     when char = 'P' then /* Exe Path names */
  206.       do
  207.         rt_view = PATH
  208.         call PYRxShow 0,P_I_SIZE,0,left(EXE_HEAD,xmx-P_I_SIZE),HEAD_ATTR
  209.        end
  210.     when char = 'W' then /* Task titles */
  211.       do
  212.         rt_view = TITLE
  213.         call PYRxShow 0,P_I_SIZE,0,left(WIN_HEAD,xmx-P_I_SIZE),HEAD_ATTR
  214.        end
  215.     when rt_view \= THREAD & rt_view \= CPU then
  216.       nop
  217.     when scan = '8D' then /*Ctrl-Up*/
  218.       if t_idx > 1 then
  219.         t_idx = t_idx-1
  220.     when scan = 91 then    /*Ctrl-Down*/
  221.       if t_idx <= tl.0 - INF_LINES then
  222.         t_idx = t_idx+1
  223.     when scan = 84 then /*Ctrl-PgUp*/
  224.       do
  225.         t_idx = t_idx - INF_LINES
  226.         if t_idx < 1 then
  227.           t_idx = 1
  228.        end
  229.     when scan = 76 then /*Ctrl-PgDn*/
  230.       if t_idx <= tl.0 - INF_LINES then
  231.         t_idx = t_idx + INF_LINES
  232.     when scan = 77 then /* Ctrl-Home */
  233.       t_idx = 1
  234.     when scan = 75 then /*Ctrl-End*/
  235.       if tl.0 > INF_LINES then
  236.         t_idx = tl.0 - INF_LINES + 1
  237.     otherwise
  238.       nop
  239.    end /*select*/
  240.   s_pid = pid.s_idx
  241.  end
  242. do_exit:
  243.   exit
  244.  
  245. /*■■■■■■■■■■■■■■*/
  246.  
  247. show_p_info:
  248.   parse var pl.i . . p_typ p_ses p_thn p_st p_time ,
  249.                   '"'p_pth'" "'p_ttl '2201'x dt_sys dt_usr dt_idle p_dt
  250.   p_time = t_conv(p_time*SLICE)
  251.   peak = ((SLICE*(dt_sys+dt_usr))/p_dt)*100
  252.   if peak >= 100.0 then
  253.     peak = ' --.-- '
  254.    else
  255.     peak = format(peak,3,2)||'%' ; d_sign = '  '
  256.   if s_idx = i then 
  257.     if (y_prc = 1 & s_idx > 1) | s_idx = pl.0 then 
  258.       d_sign = '1818'x
  259.      else
  260.       if (s_off = INF_LINES-1 & s_idx < pl.0) | s_idx = 1 then d_sign = '1919'x
  261.   view_str = right(pid.i,5)||right(ppid.i,6)||right(p_typ,4)||p_time||peak||d_sign
  262.   act  = format(((dt_sys+dt_usr-dt_idle)*P_I_SIZE*SLICE)/p_dt,,0)
  263.   idle = format((dt_idle*P_I_SIZE*SLICE)/p_dt,,0)
  264.   attr = P_I_FG ; if i = s_idx then attr = P_I_SEL
  265.   lt_attr = ACT_BG ; dk_attr = IDLE_BG
  266.   y_view = y_prc ; x_view = 0 ; sz_view = P_I_SIZE
  267.   call right_view
  268.   select
  269.     when rt_view = PATH then
  270.       do
  271.         ch_x = 2*(words(chain)-1) ; v_text = strip(right(p_pth,xmx-ch_x-P_I_SIZE-2))
  272.         if v_text \= p_pth then v_text = '..'v_text
  273.         v_text = left(left("",ch_x)||v_text,xmx-P_I_SIZE)
  274.         call PYRxShow ,P_I_SIZE,0,v_text,PATH_ATTR
  275.        end
  276.     when rt_view = TITLE then
  277.       do
  278.         v_text = left(p_ttl,xmx - P_I_SIZE - 2)
  279.         if length(p_ttl) > xmx - P_I_SIZE - 2 then v_text = v_text'..'
  280.         call PYRxShow ,P_I_SIZE,0,left(v_text,xmx - P_I_SIZE),PATH_ATTR
  281.        end
  282.     otherwise
  283.       nop
  284.    end
  285.   return
  286.   
  287. t_conv: procedure
  288.   parse arg tim
  289.   rsec = tim%1000 ; rmsec = right(tim//1000,3,'0')
  290.   rmin = rsec % 60 ;  rsec = right(rsec//60,2,'0')
  291.   rhour = rmin % 60 ; rmin = right(rmin//60,2,'0')
  292.   rday = right(rhour%24,3) ; rhour = right(rhour//24,2,'0')
  293.   return rday':'rhour':'rmin':'rsec'.'rmsec
  294.   
  295. drop_slots:
  296.   do while t_slots \= ''
  297.     parse var t_slots slot t_slots
  298.     drop ut.slot st.slot
  299.    end
  300.   t_slots = chain
  301.   return
  302.   
  303. show_t_info:
  304.   parse var tl.i . tid slot sleep prio state t_st t_ut t_tim
  305.   mask = ' 'slot' ' ;  chain = ' 'chain||mask
  306.   parse var t_slots t_slots (mask) +1 alive mask ; t_slots = t_slots||mask
  307.   if alive \= '' then
  308.     do
  309.       dt_sys =  t_st - st.slot ; dt_usr = t_ut - ut.slot
  310.      end
  311.    else
  312.     do
  313.       dt_sys =  t_st ; dt_usr = t_ut
  314.      end
  315.   st.slot = t_st ; ut.slot = t_ut
  316.   if tid = 1 then
  317.     t_dt = t_tim - t_mem 
  318.   t_mem = t_tim ;
  319.   if i < t_idx | y_thr > INF_LINES then 
  320.     return
  321.   peak = ((SLICE*(dt_sys+dt_usr))/t_dt)*100
  322.   if peak >= 100.0 then
  323.     peak = ' --.-- '
  324.    else
  325.     peak = format(peak,3,2)||'%'
  326.   view_str = right(tid,4)||right(slot,6)||' '||left(prio,6)||right(state,4),
  327.         ||right(sleep,11)||peak
  328.   idle = format((dt_sys*(xmx-P_I_SIZE)*SLICE)/t_dt,,0)
  329.   act = format((dt_usr*(xmx-P_I_SIZE)*SLICE)/t_dt,,0)
  330.   lt_attr = REG_U_BG ; dk_attr = REG_S_BG
  331.   if pos("IDL",prio) = 1 then
  332.     do
  333.       lt_attr = IDL_U_BG ; dk_attr = IDL_S_BG
  334.      end
  335.   y_view = y_thr ; x_view = P_I_SIZE ; sz_view = xmx-P_I_SIZE ; attr = PATH_ATTR
  336.   call right_view
  337.   y_thr = y_thr + 1
  338.   return
  339.  
  340. get_cpu:
  341.   drop tl.    
  342.   call PYRxGo "i",,"tl"
  343.   if \datatype(tl.0,'w') then
  344.     do
  345.       CPU_TXT = "Not supported OS level"
  346.       tl.0 = 0
  347.      end
  348.    else 
  349.     if tl.0 = 0 then
  350.       CPU_TXT = "Not supported CPU type"
  351.      else
  352.       CPU_TXT = tl.0||" CPU(s) onboard"
  353.   CPU_TXT = "CPU info: "CPU_TXT
  354.   do i=1 to tl.0
  355.     parse var tl.i ts_l ts_h idle_l idle_h busy_l busy_h int_l int_h dt
  356.       ts_cur = 4294967296*ts_h   + ts_l
  357.     idle_cur = 4294967296*idle_h + idle_l
  358.     busy_cur = 4294967296*busy_h + busy_l
  359.      int_cur = 4294967296*int_h  + int_l
  360.     if ts_prev.i \= 'TS_PREV.'||i then
  361.       do
  362.     ts = ts_cur - ts_prev.i
  363.     busy = right(format(((busy_cur - busy_prev.i)*100)/ts,,2),7)
  364.     int = right(format(((int_cur - int_prev.i)*100)/ts,,2),7)
  365.     idle = right(format(((idle_cur - idle_prev.i)*100)/ts,,2),7)
  366.     freq = format(ts/(dt*F_coeff),,0)
  367.     tl.i = left('  'i||busy"%"int"%"idle"%  ~"freq" MGz.",xmx-P_I_SIZE)
  368.        end
  369.       else
  370.        tl.i = ""
  371.     ts_prev.i = ts_cur ;     idle_prev.i = idle_cur
  372.     busy_prev.i = busy_cur ; int_prev.i = int_cur
  373.    end
  374.   return
  375.  
  376. show_cpu_info:
  377.   y_thr = 1
  378.   do i=1 to tl.0 + INF_LINES while y_thr <= INF_LINES
  379.     if i <= tl.0 & tl.i \= '' then
  380.       do
  381.         if i < t_idx then
  382.           iterate
  383.         parse var tl.i view_str 1 . busy '%' int '%' .
  384.         act = format(busy*(xmx-P_I_SIZE)/100,,0)
  385.         idle = format(int*(xmx-P_I_SIZE)/100,,0)
  386.         lt_attr = CPU_B_BG ; dk_attr = CPU_I_BG
  387.         y_view = y_thr ; x_view = P_I_SIZE ; sz_view = xmx-P_I_SIZE 
  388.         attr = PATH_ATTR
  389.         call right_view
  390.         y_thr = y_thr + 1
  391.        end
  392.      else
  393.       do
  394.         call PYRxShow y_thr,P_I_SIZE,(xmx-P_I_SIZE),' ','7'x
  395.         y_thr = y_thr + 1
  396.        end
  397.    end
  398.   call PYRxShow ymx-2,length(sum_txt),0,left(CPU_TXT,,
  399.                               xmx-length(sum_txt)),SUM_ATTR
  400.   return
  401.  
  402. right_view:
  403.   attr_str = ""
  404.   if datatype(act,'w') then
  405.     do
  406.       if act > 0 then
  407.         attr_str = left("",act,bitor(lt_attr,bitand(attr,'f'x)))
  408.        else
  409.         act = 0
  410.      end
  411.    else
  412.     act = 0
  413.   if datatype(idle,'w') then
  414.     do
  415.       if idle > 0 then
  416.         attr_str = attr_str||left("",idle,bitor(dk_attr,bitand(attr,'f'x)))
  417.        else
  418.         idle = 0
  419.      end
  420.    else
  421.     idle = 0
  422.   sz_attr = sz_view-act-idle
  423.   if sz_attr > 0 then
  424.     attr_str = attr_str||left("",sz_attr,attr)
  425.   view_str = left(view_str,sz_view)
  426.   v_str = ''
  427.   do j=1 to sz_view
  428.     v_str = v_str||substr(view_str,j,1)||substr(attr_str,j,1)
  429.    end
  430.   call PYRxShow y_view,x_view,-1,v_str
  431.   return