home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright (c) 1992 Michael A. Cooper.
- * This software may be freely distributed provided it is not sold for
- * profit and the author is credited appropriately.
- */
-
- #ifndef lint
- static char *RCSid = "$Header: /src/common/usc/bin/sysinfo/RCS/memory.c,v 1.9 1992/04/26 23:32:06 mcooper Exp $";
- #endif
-
- /*
- * $Log: memory.c,v $
- * Revision 1.9 1992/04/26 23:32:06 mcooper
- * Add Copyright notice
- *
- * Revision 1.8 1992/04/17 01:07:59 mcooper
- * More de-linting
- *
- * Revision 1.7 1992/04/16 02:25:39 mcooper
- * Bug fixes, de-linting, and other changes found with CodeCenter.
- *
- * Revision 1.6 1992/03/31 02:35:53 mcooper
- * Change "nl" to "PhysmemNL".
- *
- * Revision 1.5 1992/03/31 01:55:17 mcooper
- * Use new CheckNlist to check nlist success.
- *
- * Revision 1.4 1992/03/31 00:22:54 mcooper
- * Add ctob() comment.
- *
- * Revision 1.3 1992/03/31 00:15:09 mcooper
- * Add error check for nlist.n_type.
- *
- * Revision 1.2 1992/03/28 23:13:07 mcooper
- * Don't set memory if == 0.
- *
- * Revision 1.1 1992/03/22 00:20:10 mcooper
- * Initial revision
- *
- */
-
-
- /*
- * Memory related functions.
- */
-
- #include <stdio.h>
- #include "system.h"
- #include "defs.h"
-
- /*
- * Divide and Round Up
- */
- static int DivRndUp(Num, Div)
- unsigned long Num;
- unsigned long Div;
- {
- int i;
-
- i = Num / Div;
-
- return((Num % Div) ? i+1 : i);
- }
-
- #if defined(HAVE_KVM) && defined(HAVE_NLIST)
-
- #include <fcntl.h>
- #include <nlist.h>
-
- static struct nlist PhysmemNL[] = {
- #if defined(COFF)
- { "physmem" },
- #else
- { "_physmem" },
- #endif /* COFF */
- { 0 },
- };
-
- /*
- * Common method of determining amount of physical memory in a
- * BSD Unix machine.
- *
- * Get memory by reading the variable "physmem" from the kernel
- * and the system page size.
- */
- extern char *GetMemoryFromPhysmem()
- {
- static char Buf[BUFSIZ];
- unsigned long NPages, Bytes;
- int Amount = -1;
- kvm_t *kd;
-
- if (kd = KVM_open(PhysmemNL)) {
- /*
- * See if we got a valid entry
- */
- if (CheckNlist(&PhysmemNL[0]))
- return((char *) NULL);
-
- if (KVM_read(kd, PhysmemNL[0].n_value, (char *)&NPages,
- sizeof(NPages)) >= 0) {
- /*
- * Could use ctob() instead of "Page Size * Num Pages",
- * but this is more portable.
- */
- Bytes = (long) (getpagesize() * NPages);
- if (Debug)
- printf("Bytes = %d NPages = %d pagesize = %d\n",
- Bytes, NPages, getpagesize());
- Amount = DivRndUp(Bytes, MBYTES);
- }
- }
-
- if (kd)
- KVM_close(kd);
-
- if (Amount > 0) {
- (void) sprintf(Buf, "%d MB", Amount);
- return(Buf);
- } else
- return((char *) NULL);
- }
- #endif /* HAVE_KVM && HAVE_NLIST */
-