home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume3 / sps / part2 / needed.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-11-30  |  4.4 KB  |  150 lines

  1. # include       "sps.h"
  2. # include       "flags.h"
  3. # include       <h/text.h>
  4. # include       <stdio.h>
  5.  
  6. /*
  7. ** NEEDED - Determine which processes are needed for the printout
  8. ** and add these to a list of needed processes.
  9. */
  10. struct process  *needed ( process, text )
  11.  
  12. register struct process         *process ;
  13. struct text                     *text ;
  14.  
  15. {
  16.     register struct process *p ;
  17.     register struct process *plist ;
  18.     struct process          *lastp ;
  19.     int                     uid ;
  20.     extern struct flags     Flg ;
  21.     extern union userstate  User ;
  22.     extern struct info      Info ;
  23.     extern struct ttyline   Notty ;
  24.     struct ttyline          *findtty() ;
  25.     char                    *getcmd() ;
  26.  
  27.     plist = (struct process*)0 ;
  28.     lastp = &process[ Info.i_nproc ] ;
  29.     /* Normalise internal pointers from kernel addresses. For each kmem
  30.        address in the `proc' and `text' structures, we convert that
  31.        address for our own internal use. */
  32.     for ( p = process ; p < lastp ; p++ )
  33.     {                               
  34.         if ( !p->pr_p.p_stat )  
  35.             continue ;
  36.         /* Normalise internal text pointers */
  37.         if ( p->pr_p.p_textp )
  38.             p->pr_p.p_textp = &text[p->pr_p.p_textp - Info.i_text0];
  39.         /* Normalise internal linked list of processes */
  40.         p->pr_plink = p->pr_p.p_link ?
  41.             &process[ p->pr_p.p_link  - Info.i_proc0 ] :
  42.             (struct process*)0 ;
  43.         /* Normalise internal parent pointers */
  44.         p->pr_pptr = p->pr_p.p_pptr ?
  45.             &process[ p->pr_p.p_pptr - Info.i_proc0 ] :
  46.             (struct process*)0 ;
  47.         /* Check for valid parent pointers */
  48.         if ( !p->pr_pptr )
  49.         {
  50.             p->pr_pptr = process ;
  51.             continue ;
  52.         }
  53.         if ( p->pr_pptr < process || p->pr_pptr >= lastp )
  54.         {
  55.             fprintf( stderr, "sps - process %d has bad pptr\n",
  56.                 p->pr_p.p_pid ) ;
  57.             p->pr_pptr = process ;
  58.         }
  59.     }
  60.     /* For each process, see if it is a candidate for selection.
  61.        If so, retrieve its command arguments and upage information. */
  62.     uid = getuid() ;
  63.     for ( p = process ; p < lastp ; p++ )
  64.     {                               
  65.         if ( !p->pr_p.p_stat )
  66.             continue ;
  67.         /* Count processes and sizes */
  68.         summarise( p ) ;
  69.         /* Select the given processes. Bear in mind that selection
  70.            of processes based on the `F' and `T' flags must be
  71.            postponed until the upage is accessed. */
  72.         if ( !Flg.flg_F && !Flg.flg_T && !selectproc( p, process, uid ))
  73.             continue ;
  74.         /* Try to find the process' command arguments. Accessing the
  75.            arguments also involves retrieving the upage. */
  76.         p->pr_cmd = getcmd( p ) ;
  77.         /* If the upage was found successfully, use this information */
  78.         if ( p->pr_upag )       
  79.         {
  80. # ifdef BSD42
  81.             p->pr_rself = User.u_us.u_ru ;
  82.             p->pr_rchild = User.u_us.u_cru ;
  83. # else
  84.             p->pr_vself = User.u_us.u_vm ;
  85.             p->pr_vchild = User.u_us.u_cvm ;
  86. # endif
  87.             p->pr_tty = findtty( p ) ;
  88.             p->pr_files = filecount() ;
  89.         }
  90.         else
  91.             p->pr_tty = &Notty ;
  92.         /* Select on the basis of the `F' and `T' flags */
  93.         if ( Flg.flg_F          
  94.         && !(p->pr_p.p_pgrp && p->pr_p.p_pgrp == p->pr_tty->l_pgrp) )
  95.             continue ;
  96.         if ( Flg.flg_T && !selecttty( p ) )
  97.             continue ;
  98.         /* Arrive here with a selected process. Add this to the
  99.            linked list of needed processes. */
  100.         p->pr_plink = plist ;   
  101.         plist = p ;
  102.         p->pr_child = (struct process*)0 ;
  103.         p->pr_sibling = (struct process*)0 ;
  104.     }
  105.     return ( plist ) ;
  106. }
  107.  
  108. /* SUMMARISE - Summarises the given process into the `Summary' structure */
  109. /*
  110. ** SHOULD ACCOUNT HERE FOR THE SIZE OF LOADED PAGE TABLES, BUT WE DON'T REALLY
  111. ** KNOW THEIR RESIDENT SIZES.
  112. */
  113. summarise ( p )
  114.  
  115. register struct process         *p ;
  116.  
  117. {
  118.     register struct text    *tp ;
  119.     int                     busy ;
  120.     extern struct summary   Summary ;
  121.  
  122.     Summary.sm_ntotal++ ;
  123.     if ( p->pr_p.p_stat == SZOMB )
  124.         return ;
  125.     /* Firstly, account for processes */
  126.     Summary.sm_ktotal += p->pr_p.p_dsize + p->pr_p.p_ssize ;
  127.     Summary.sm_kloaded += p->pr_p.p_rssize ;
  128.     Summary.sm_kswapped += p->pr_p.p_swrss ;
  129.     if ( p->pr_p.p_flag & SLOAD )
  130.         Summary.sm_nloaded++ ;
  131.     else
  132.         Summary.sm_nswapped++ ;
  133.     busy = (p->pr_p.p_stat == SRUN) || (p->pr_p.p_stat==SSLEEP
  134.          && (p->pr_p.p_pri<PZERO && p->pr_p.p_pid > MSPID) ) ;
  135.     if ( busy )
  136.     {
  137.         Summary.sm_nbusy++ ;
  138.         Summary.sm_kbusy += p->pr_p.p_dsize + p->pr_p.p_ssize ;
  139.     }
  140.     /* Now account for their texts */
  141.     if ( !(tp = p->pr_p.p_textp) || !tp->x_count )
  142.         return ;                
  143.     Summary.sm_ktotal += tp->x_size ;
  144.     Summary.sm_kloaded += tp->x_rssize ;
  145.     Summary.sm_kswapped += tp->x_swrss ;
  146.     if ( busy )
  147.         Summary.sm_kbusy += tp->x_size ;
  148.     tp->x_count = 0 ;
  149. }
  150.