home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / unix / bsd / 10981 < prev    next >
Encoding:
Internet Message Format  |  1993-01-06  |  6.6 KB

  1. Path: sparky!uunet!olivea!mintaka.lcs.mit.edu!ai-lab!hal.gnu.ai.mit.edu!mycroft
  2. From: mycroft@hal.gnu.ai.mit.edu (Charles Hannum)
  3. Newsgroups: comp.unix.bsd
  4. Subject: Memory leak in kvm_getprocs() and top 3.0
  5. Message-ID: <1ig4u5INNstu@life.ai.mit.edu>
  6. Date: 7 Jan 93 02:35:49 GMT
  7. Organization: MIT Artificial Intelligence Lab
  8. Lines: 253
  9. NNTP-Posting-Host: hal.gnu.ai.mit.edu
  10.  
  11.  
  12. Here are two sets of patches.  The first fixes a memory leak in
  13. kvm_getprocs(), and the second is to convent m_bsd44.c to m_386bsd.c,
  14. in the top 3.0 distribution.
  15.  
  16. Note that while I've tried to take advantage of virtual memory
  17. metering, this is not yet implemented in 386BSD.  I'm also not entirely
  18. certain why the resident size always shows up as `0'.  I'll post a
  19. patch when I decide to think about it again.
  20.  
  21. -----8<-----snip-----8<-----snip-----8<-----snip-----8<-----snip-----8<-----
  22. *** /usr/src/lib/libutil/kvm.c~    Fri Nov 27 21:16:35 1992
  23. --- /usr/src/lib/libutil/kvm.c    Wed Jan  6 20:32:34 1993
  24. ***************
  25. *** 99,105 ****
  26.   /*
  27.    * state
  28.    */
  29. ! static    struct kinfo_proc *kvmprocbase, *kvmprocptr;
  30.   static    int kvmnprocs;
  31.   /*
  32.    * u. buffer
  33. --- 99,105 ----
  34.   /*
  35.    * state
  36.    */
  37. ! static    struct kinfo_proc *kvmprocbase = NULL, *kvmprocptr;
  38.   static    int kvmnprocs;
  39.   /*
  40.    * u. buffer
  41. ***************
  42. *** 406,411 ****
  43. --- 406,413 ----
  44.               return (-1);
  45.           }
  46.           copysize = ret;
  47. +         if (kvmprocbase)
  48. +             free(kvmprocbase);
  49.           if ((kvmprocbase = (struct kinfo_proc *)malloc(copysize)) 
  50.                == NULL) {
  51.               seterr("out of memory");
  52. -----8<-----snip-----8<-----snip-----8<-----snip-----8<-----snip-----8<-----
  53.  
  54. -----8<-----snip-----8<-----snip-----8<-----snip-----8<-----snip-----8<-----
  55. *** m_bsd44.c    Sat May  9 15:09:54 1992
  56. --- m_386bsd.c    Wed Jan  6 21:28:02 1993
  57. ***************
  58. *** 2,17 ****
  59.    * top - a top users display for Unix
  60.    *
  61. !  * SYNOPSIS:  For a pre-release 4.4BSD system
  62. !  *          Note memory statistisc and process sizes could be wrong,
  63. !  *          by ps gets them wrong too...
  64.    *
  65.    * DESCRIPTION:
  66. !  * This is the machine-dependent module for BSD4.4 
  67. !  * Works for:
  68. !  *    hp300
  69.    *
  70. !  * LIBS: -lkvm
  71. !  *
  72. !  * AUTHOR:  Christos Zoulas <christos@ee.cornell.edu>
  73.    */
  74.   
  75. --- 2,13 ----
  76.    * top - a top users display for Unix
  77.    *
  78. !  * SYNOPSIS:  For 386BSD 0.1
  79.    *
  80.    * DESCRIPTION:
  81. !  * This is the machine-dependent module for 386BSD
  82.    *
  83. !  * AUTHOR:  Charles Hannum <mycroft@ai.mit.edu>
  84. !  *        based on m_bsd44.c, by
  85. !  *        Christos Zoulas <christos@ee.cornell.edu>
  86.    */
  87.   
  88. ***************
  89. *** 27,30 ****
  90. --- 23,27 ----
  91.   #include <sys/kinfo.h>
  92.   #include <sys/kinfo_proc.h>
  93. + #include <sys/vmmeter.h>
  94.   #ifdef notyet
  95.   #define time __time
  96. ***************
  97. *** 45,50 ****
  98.   #include "machine.h"
  99.   
  100. ! #define VMUNIX    "/vmunix"
  101. ! #define KMEM    "/dev/kmem"
  102.   #define MEM    "/dev/mem"
  103.   #ifdef DOSWAP
  104. --- 42,47 ----
  105.   #include "machine.h"
  106.   
  107. ! #define VMUNIX    "/386bsd"
  108. ! #define KMEM    NULL
  109.   #define MEM    "/dev/mem"
  110.   #ifdef DOSWAP
  111. ***************
  112. *** 79,82 ****
  113. --- 76,80 ----
  114.   #define X_HZ        2
  115.   #define X_AVENRUN    3
  116. + #define X_TOTAL        4
  117.   
  118.   static struct nlist nlst[] = {
  119. ***************
  120. *** 85,88 ****
  121. --- 83,87 ----
  122.       { "_hz" },            /* 2 */
  123.       { "_averunnable" },        /* 3 */
  124. +     { "_total" },        /* 4 */
  125.       { 0 }
  126.   };
  127. ***************
  128. *** 111,116 ****
  129.   
  130.   
  131. - static kvm_t *kd;
  132.   /* values that we stash away in _init and use in later routines */
  133.   
  134. --- 110,113 ----
  135. ***************
  136. *** 185,194 ****
  137.       register int pagesize;
  138.   
  139. !     if ((kd = kvm_open(VMUNIX, MEM, SWAP, O_RDONLY, "kvm_open")) == NULL)
  140.       return -1;
  141.   
  142.       /* get the list of symbols we want to access in the kernel */
  143. !     (void) kvm_nlist(kd, nlst);
  144.       if (nlst[0].n_type == 0)
  145.       {
  146. --- 182,190 ----
  147.       register int pagesize;
  148.   
  149. !     if (kvm_openfiles(VMUNIX, KMEM, SWAP) == -1)
  150.       return -1;
  151.   
  152.       /* get the list of symbols we want to access in the kernel */
  153. !     (void) kvm_nlist(nlst);
  154.       if (nlst[0].n_type == 0)
  155.       {
  156. ***************
  157. *** 302,307 ****
  158.       int size;
  159.   
  160. !     /* get total -- systemwide main memory usage structure */
  161. !     getkerninfo(KINFO_METER, &total, &size, 0);
  162.       /* convert memory stats to Kbytes */
  163.       memory_stats[0] = -1;
  164. --- 298,303 ----
  165.       int size;
  166.   
  167. !         (void) getkval(nlst[X_TOTAL].n_value, (int *)(&total), sizeof(total),
  168. !                nlst[X_TOTAL].n_name);
  169.       /* convert memory stats to Kbytes */
  170.       memory_stats[0] = -1;
  171. ***************
  172. *** 343,354 ****
  173.   
  174.       
  175. !     pbase = kvm_getprocs(kd, KINFO_PROC_ALL, 0, &nproc);
  176. !     if (nproc > onproc)
  177. !     pref = (struct kinfo_proc **) realloc(pref, sizeof(struct kinfo_proc *)
  178. !         * (onproc = nproc));
  179. !     if (pref == NULL || pbase == NULL) {
  180. !     (void) fprintf(stderr, "top: Out of memory.\n");
  181.       quit(23);
  182.       }
  183.       /* get a pointer to the states summary array */
  184.       si->procstates = process_states;
  185. --- 339,355 ----
  186.   
  187.       
  188. !     if ((nproc = kvm_getprocs(KINFO_PROC_ALL, 0)) == -1) {
  189. !         (void) fprintf(stderr, "top: kvm_getprocs() failed: %s\n", kvm_geterr());
  190. !     quit(23);
  191. !     }
  192. !     if (pref)
  193. !         free(pref);
  194. !     if ((pref = (struct kinfo_proc **) malloc(sizeof(struct kinfo_proc *) * nproc)) ==  NULL) {
  195. !         (void) fprintf(stderr, "top: out of memory\n");
  196.       quit(23);
  197.       }
  198.       /* get a pointer to the states summary array */
  199.       si->procstates = process_states;
  200. ***************
  201. *** 365,369 ****
  202.       memset((char *)process_states, 0, sizeof(process_states));
  203.       prefp = pref;
  204. !     for (pp = pbase, i = 0; i < nproc; pp++, i++)
  205.       {
  206.       /*
  207. --- 366,370 ----
  208.       memset((char *)process_states, 0, sizeof(process_states));
  209.       prefp = pref;
  210. !     for (kvm_setproc(); pp = (struct kinfo_proc *) kvm_nextproc();)
  211.       {
  212.       /*
  213. ***************
  214. *** 520,524 ****
  215.   
  216.   {
  217. !     if (kvm_read(kd, offset, (char *) ptr, size) != size)
  218.       {
  219.       if (*refstr == '!')
  220. --- 521,525 ----
  221.   
  222.   {
  223. !     if (kvm_read((void *) offset, (char *) ptr, size) != size)
  224.       {
  225.       if (*refstr == '!')
  226. ***************
  227. *** 562,575 ****
  228.   proc_compare(pp1, pp2)
  229.   
  230. ! struct kinfo_proc **pp1;
  231. ! struct kinfo_proc **pp2;
  232.   
  233.   {
  234. !     register struct kinfo_proc *p1;
  235. !     register struct kinfo_proc *p2;
  236.       register int result;
  237.       register pctcpu lresult;
  238.   
  239. -     /* remove one level of indirection */
  240.       p1 = *pp1;
  241.       p2 = *pp2;
  242. --- 563,573 ----
  243.   proc_compare(pp1, pp2)
  244.   
  245. ! struct kinfo_proc **pp1, **pp2;
  246.   
  247.   {
  248. !     register struct kinfo_proc *p1, *p2;
  249.       register int result;
  250.       register pctcpu lresult;
  251.   
  252.       p1 = *pp1;
  253.       p2 = *pp2;
  254. -----8<-----snip-----8<-----snip-----8<-----snip-----8<-----snip-----8<-----
  255. -- 
  256.  \  /   Charles Hannum, mycroft@ai.mit.edu
  257.  /\ \   PGP public key available on request.  MIME, AMS, NextMail accepted.
  258. Scheme  White heterosexual atheist male (WHAM) pride!
  259.