home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume26 / cputt-2.3 / part01 / routines.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-05  |  2.2 KB  |  104 lines

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