From: dodd@mycenae.cchem.berkeley.edu (Lawrence R. Dodd)
Newsgroups: comp.unix.misc
Subject: Re: how much memory do i have??
Followup-To: comp.unix.misc
Date: 20 Nov 92 06:21:46
Organization: Dept of Chemical Engineering, Polytechnic Univ, NY, USA
	<1992Nov20.000058.13035@megatek.com> <15578@auspex-gw.auspex.com>
Reply-To: dodd@roebling.poly.edu
NNTP-Posting-Host: mycenae.cchem.berkeley.edu
In-reply-to: guy@Auspex.COM's message of 20 Nov 92 07:56:45 GMT


This was posted to the net a while back.  I find it most useful.

..............................................................
/*
 * From jdd@db.toronto.edu Wed Feb 12 09:45:23 1992
 * Newsgroups: comp.sys.sun.admin
 * From: jdd@db.toronto.edu (John DiMarco)
 * Subject: Re: Boot messages, how to find out Memory
 * Organization: Department of Computer Science, University of Toronto
 * Date: 11 Feb 92 18:45:27 GMT
 * 
 * jpd@tardis.cl.msu.edu (Joe P. DeCello) writes:
 * 
 * >mem = 49152K (0x3000000)
 * 
 * >Telling me of course, that there is 48meg installed.
 * >How can I find this out from a command line prompt?
 * 
 * Here's a little program that'll do the trick. It's short, so I'll include it
 * here. Thanks to Dave Curry and Larry Breniser. I've hacked it a little.
 * 
 * John
 * --
 * John DiMarco                                              jdd@cdf.toronto.edu
 * Computing Disciplines Facility Systems Manager            jdd@cdf.utoronto.ca
 * University of Toronto                                     EA201B,(416)978-1928
 *
 * ----------- cut here -----------------
 * Figure out how much memory there is on a machine.  This works under
 * Berkeley UNIX.  It should work under System V if you change the
 * UNIX define to "/unix" instead of "/vmunix".
 *
 * Of course, if you don't have read permission on the kernel and
 * kernel memory, this won't work.
 *
 * Dave Curry
 * Purdue University
 * Engineering Computer Network
 *
 * Hacked by Larry Breniser 10/1/91
 * Pagesize is now determied dynamically, not at compile
 * time, as it varies from host to host (server vs workstation)
 *
 * davy@intrepid.ecn.purdue.edu
 * $Header: /src/mem/RCS/mem.c,v 1.4 1991/12/20 00:22:53 jdd Exp $
 * $Log: mem.c,v $
 * Revision 1.4  1991/12/20  00:22:53  jdd
 * Added a verbose option which behaves like the previous revision.
 * Changed the physmem figure to report memory in KBs
 * Added a usage message
 * Made the non -verbose default behave like the revision before the last.
 *
 * Revision 1.3  1991/10/15  19:52:26  jdd
 * *** empty log message ***
 *
 * Revision 1.2  1991/10/15  19:51:11  jdd
 * New version which works on machines with different page sizes.
 *
 * Revision 1.3  91/10/02  17:51:10  larryb
 * Added Version string to executable for ident.
 *
 * Revision 1.2  91/10/02  17:46:21  larryb
 * Added RCS log comments.
 *
 */
#include <sys/param.h>
#include <sys/types.h>
#include <nlist.h>
#include <stdio.h>

char version[] = "$Header: /src/mem/RCS/mem.c,v 1.4 1991/12/20 00:22:53 jdd Exp$";

#define UNIX    "/vmunix"
#define KMEM    "/dev/kmem"

struct nlist nl[] = {
#define X_PHYSMEM       0
        { "_physmem" },
#define X_MAXMEM        1
        { "_maxmem" },
        { NULL }
};

main(argc, argv)
int argc;
char **argv;
{
        int kmem;
        int maxmem, physmem;
        int pagesize;
        int verbose = 0;

        if(argc>1){
                if(0==strcmp("-v", argv[1])) {
                        verbose=1;
                } else {
                        fprintf(stderr, "Usage: %s [-v]\n", argv[0]);
                        exit(1);
                }
        }

        /*
         * Look up addresses of variables.
         */
        if ((nlist(UNIX, nl) < 0) || (nl[0].n_type == 0)) {
                fprintf(stderr, "%s: no namelist.\n", UNIX);
                exit(1);
        }

        /*
         * Open kernel memory.
         */
        if ((kmem = open(KMEM, 0)) < 0) {
                perror(KMEM);
                exit(1);
        }

        /*
         * Read variables.
         */
        lseek(kmem, (long) nl[X_PHYSMEM].n_value, 0);
        read(kmem, (char *) &physmem, sizeof(int));

        lseek(kmem, (long) nl[X_MAXMEM].n_value, 0);
        read(kmem, (char *) &maxmem, sizeof(int));

        close(kmem);


        pagesize=getpagesize();

        /*
         * Print the numbers.  The internal representation is
         * in units of core clicks; convert to bytes.
         */

        if(verbose) printf("Pagesize: %d\n", pagesize);

        if(verbose) {
                printf("Physical machine memory (KB): %d\n", (pagesize*physmem)/
1024);
        } else {
                printf("%d\n", (pagesize * physmem)/1024);
        }
        if(verbose) printf("Max memory available to a process: %d\n", pagesize *
 maxmem);

        exit(0);
}


Xref: sparky comp.unix.misc:4176 comp.unix.questions:13450 comp.sys.sgi:16457 comp.graphics:11900