home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume26 / sysinfo-1.0 / part01 / memory.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-10  |  2.6 KB  |  124 lines

  1. /*
  2.  * Copyright (c) 1992 Michael A. Cooper.
  3.  * This software may be freely distributed provided it is not sold for 
  4.  * profit and the author is credited appropriately.
  5.  */
  6.  
  7. #ifndef lint
  8. static char *RCSid = "$Header: /src/common/usc/bin/sysinfo/RCS/memory.c,v 1.9 1992/04/26 23:32:06 mcooper Exp $";
  9. #endif
  10.  
  11. /*
  12.  * $Log: memory.c,v $
  13.  * Revision 1.9  1992/04/26  23:32:06  mcooper
  14.  * Add Copyright notice
  15.  *
  16.  * Revision 1.8  1992/04/17  01:07:59  mcooper
  17.  * More de-linting
  18.  *
  19.  * Revision 1.7  1992/04/16  02:25:39  mcooper
  20.  * Bug fixes, de-linting, and other changes found with CodeCenter.
  21.  *
  22.  * Revision 1.6  1992/03/31  02:35:53  mcooper
  23.  * Change "nl" to "PhysmemNL".
  24.  *
  25.  * Revision 1.5  1992/03/31  01:55:17  mcooper
  26.  * Use new CheckNlist to check nlist success.
  27.  *
  28.  * Revision 1.4  1992/03/31  00:22:54  mcooper
  29.  * Add ctob() comment.
  30.  *
  31.  * Revision 1.3  1992/03/31  00:15:09  mcooper
  32.  * Add error check for nlist.n_type.
  33.  *
  34.  * Revision 1.2  1992/03/28  23:13:07  mcooper
  35.  * Don't set memory if == 0.
  36.  *
  37.  * Revision 1.1  1992/03/22  00:20:10  mcooper
  38.  * Initial revision
  39.  *
  40.  */
  41.  
  42.  
  43. /*
  44.  * Memory related functions.
  45.  */
  46.  
  47. #include <stdio.h>
  48. #include "system.h"
  49. #include "defs.h"
  50.  
  51. /*
  52.  * Divide and Round Up
  53.  */
  54. static int DivRndUp(Num, Div)
  55.     unsigned long         Num;
  56.     unsigned long         Div;
  57. {
  58.     int             i;
  59.  
  60.     i = Num / Div;
  61.  
  62.     return((Num % Div) ? i+1 : i);
  63. }
  64.  
  65. #if    defined(HAVE_KVM) && defined(HAVE_NLIST)
  66.  
  67. #include <fcntl.h>
  68. #include <nlist.h>
  69.  
  70. static struct nlist PhysmemNL[] = {
  71. #if         defined(COFF)
  72.     { "physmem" },
  73. #else
  74.     { "_physmem" },
  75. #endif        /* COFF */
  76.     { 0 },
  77. };
  78.  
  79. /*
  80.  * Common method of determining amount of physical memory in a
  81.  * BSD Unix machine.
  82.  *
  83.  * Get memory by reading the variable "physmem" from the kernel
  84.  * and the system page size.
  85.  */ 
  86. extern char *GetMemoryFromPhysmem()
  87. {
  88.     static char         Buf[BUFSIZ];
  89.     unsigned long         NPages, Bytes;
  90.     int                 Amount = -1;
  91.     kvm_t               *kd;
  92.  
  93.     if (kd = KVM_open(PhysmemNL)) {
  94.     /*
  95.      * See if we got a valid entry
  96.      */
  97.     if (CheckNlist(&PhysmemNL[0]))
  98.         return((char *) NULL);
  99.  
  100.     if (KVM_read(kd, PhysmemNL[0].n_value, (char *)&NPages,
  101.              sizeof(NPages)) >= 0) {
  102.         /*
  103.          * Could use ctob() instead of "Page Size * Num Pages",
  104.          * but this is more portable.
  105.          */
  106.         Bytes = (long) (getpagesize() * NPages);
  107.         if (Debug)
  108.         printf("Bytes = %d NPages = %d pagesize = %d\n", 
  109.                Bytes, NPages, getpagesize());
  110.         Amount = DivRndUp(Bytes, MBYTES);
  111.     }
  112.     }
  113.  
  114.     if (kd)
  115.     KVM_close(kd);
  116.  
  117.     if (Amount > 0) {
  118.     (void) sprintf(Buf, "%d MB", Amount);
  119.     return(Buf);
  120.     } else
  121.     return((char *) NULL);
  122. }
  123. #endif    /* HAVE_KVM && HAVE_NLIST */
  124.