home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume26 / cputt / routines.c < prev   
Encoding:
C/C++ Source or Header  |  1992-04-18  |  2.7 KB  |  118 lines

  1. # include       "cputt.h"
  2. # include       <varargs.h>
  3. # include       <kvm.h>
  4. # include       <stdio.h>
  5.  
  6. /*
  7.    GETUPAGE - Reads the upage for the specified process as well as sufficient
  8.    page tables entries for reading the command arguments. The pte's are read
  9.    into the argument `p'. The upage is read into the external variable
  10.    `User'. Returns 1 if the upage was successfully read.
  11. */
  12.  
  13. getupage ( p )
  14.  
  15. register struct proc        *p ;
  16.  
  17. {
  18.      struct user            *upage ;
  19.      extern union userstate  User ;
  20.      extern kvm_t           *Flkvm ;
  21.  
  22.      if (upage = kvm_getu( Flkvm, p ))
  23.      {
  24.           bcopy( (char *)upage, User.u_pg[0], sizeof( struct user ) ) ;
  25.           return ( 1 ) ;
  26.      }
  27. /*
  28.      fprintf( stderr, "cputt - Can't read upage of process %d s= %o f= %o\n",
  29.               p->p_pid, p->p_stat, p->p_flag ) ;
  30. */
  31.      return ( 0 ) ;
  32. }
  33.  
  34. /* READSTATUS - Reads process table from kernel memory */
  35.  
  36. readstatus ( ptable )
  37.  
  38. struct proc *ptable;
  39.  
  40. {
  41.      extern struct info      Info ;
  42.  
  43.      if (getkmem((long)Info.i_proc0,(char*)ptable,Info.i_sproc) != Info.i_sproc)
  44.      {
  45.           fprintf(stderr,"cputt - Can't read system process table\n" ) ;
  46.           exit();
  47.      }
  48. }
  49.  
  50. /* PERCENTMEM - Returns the percentage of real memory used by this process */
  51.  
  52. double  percentmem ( p )
  53.  
  54. struct proc         *p ;
  55.  
  56. {
  57.      double                  fracmem ;
  58.      extern struct info      Info ;
  59.  
  60.      if ( !(p->p_flag & SLOAD) )
  61.           return ( 0.0 ) ;
  62.      fracmem = ( (double)p->p_rssize + UPAGES ) ;
  63.      return ( 100.0 * fracmem / (double)Info.i_ecmx ) ;
  64. }
  65.  
  66. /* GETKMEM - read kernel memory */
  67.  
  68. getkmem ( addr, buf, bufsize )
  69.  
  70. long                            addr ;
  71. char                            *buf ;
  72. int                             bufsize ;
  73. {
  74.      extern kvm_t            *Flkvm ;
  75.  
  76.      return( kvm_read( Flkvm, (long)addr, buf, bufsize ) ) ;
  77. }
  78.  
  79. /* SYSPERROR - Reports a system defined error msg and then exits gracefully */
  80.  
  81. sysperror ()
  82. {
  83.      extern int              errno ;
  84.      extern int              sys_nerr ;
  85.      extern char             *sys_errlist[] ;
  86.  
  87.      if ( 0 < errno && errno < sys_nerr )
  88.           fprintf( stderr, " : %s", sys_errlist[errno] ) ;
  89.      (void)fputc( '\n', stderr ) ;
  90.      exit( 1 ) ;
  91. }
  92.  
  93. /* GETCORE - Allocate and return a pointer to the asked for amount of core */
  94.  
  95. char    *getcore ( size )
  96.  
  97. register int                    size ;
  98.  
  99. {
  100.      register char           *chp ;
  101.      char                    *malloc() ;
  102.  
  103.      if ( chp = malloc( (unsigned)size ) )
  104.           return ( chp ) ;
  105.      fprintf( stderr, "cputt - Out of core" ) ;
  106.      sysperror() ;
  107. }
  108.  
  109. /* CMP - used by qsort() to perform comparisons */
  110.  
  111. int cmp(p1,p2)
  112.  
  113. register struct procdata *p1, *p2;
  114.  
  115. {
  116.      return (p1->pctcpu - p2->pctcpu);
  117. }   
  118.