Summary of determining memory size

From: Daryl Crandall (daryl@dash.mitre.org)
Date: Thu Mar 29 1990 - 11:18:47 CST


There are two popular approaches to determining memory size. I've included
examples of each:

FIRST APPROACH:
        Examine the file /var/adm/messages that should have recorded the
        memory size information as the workstation was booting up.

        commands such as the following can be used:

                grep mem /var/adm/messages
                dmesg | grep mem
                dmesg | grep mem | uniq

SECOND APPROACH:
        Create a compiled program and examine /dev/kmem
        I've included two programs to do this.

Thanks for the quick responses. I needed to run a quick survey of each
of 40 clients to determine our memory status. I used the "dmesg | grep mem"
method.

        Daryl Crandall
        The MITRE Corporation
        7525 Colshire, Drive
        McLean, VA 22102
        daryl@mitre.org
        (703) 883-7278

###########################################################################
###########################################################################
From: kensmith@cs.Buffalo.EDU (Ken Smith)
Message-Id: <9003291602.AA09453@electra.cs.Buffalo.EDU>
To: daryl@dash.mitre.org
Subject: Re: determining memory size
Status: R

        From sun-managers-relay@eecs.nwu.edu Thu Mar 29 10:46:38 1990
        To: sun-managers@dash.mitre.org
        Subject: determining memory size

        Is there a command that returns the memory size of a workstation?

I posted this to sun-spots a couple weeks ago. It's also available in
the archives at titan.rice.edu. Needs access to kmem (setgid or run
as root).

        ken

/*
 * mem.c - print physical memory installed in a machine
 *
 * $Author: kensmith $
 *
 * $Date: 90/03/12 14:18:18 $
 *
 * $Log: mem.c,v $
 * Revision 1.2 90/03/12 14:18:18 kensmith
 * misc. cleanup
 *
 * Revision 1.1 90/02/09 21:43:41 kensmith
 * Initial revision
 *
 */

static char rcsid[] = "$Id: mem.c,v 1.2 90/03/12 14:18:18 kensmith Exp Locker: kensmith $";

#include <stdio.h>
#include <sys/param.h>
#include <sys/types.h>
#include <sys/file.h>
#include <nlist.h>

struct nlist nls[] = {
        { "_physmem" },
#define PHYSMEM 0
        { "" },
};

int fd, physmem;

char *name; /* program name */
char *rindex();

main(argc, argv)
        int argc;
        char *argv[];
{
        if ((name = rindex(argv[0], '/')) == NULL)
                name = argv[0];
        else
                name++;
        if (nlist("/vmunix", nls) < 0) {
                perror("nlist");
                exit(1);
        }
        if ((fd = open("/dev/kmem", O_RDONLY)) <= 0) {
                perror("/dev/kmem");
                exit(1);
        }
        if (nls[PHYSMEM].n_type == 0) {
                fprintf(stderr, "%s: physmem symbol not found\n", name);
                exit(1);
        }
        lseek(fd, nls[PHYSMEM].n_value, L_SET);
        read(fd, &physmem, sizeof(physmem));
        printf("%dK bytes of memory\n", ctob(physmem) / 1024);
}

###########################################################################
###########################################################################
>From pratt@coraki.stanford.edu Thu Mar 29 11:15:13 1990
Received: from mwunix.mitre.org by dash.mitre.org (4.0/SMI-4.0)
        id AA11824; Thu, 29 Mar 90 11:15:10 EST
Received: from gateway.mitre.org by mwunix.mitre.org (5.61/SMI-2.2)
        id AA25059; Thu, 29 Mar 90 11:16:01 -0500
Received: by gateway.mitre.org (5.54/SMI-2.2)
        id AA04641; Thu, 29 Mar 90 11:19:31 EST
Return-Path: <pratt@coraki.stanford.edu>
Received: by mwunix.mitre.org (5.61/SMI-2.2)
        id AA25054; Thu, 29 Mar 90 11:15:55 -0500
Received: from Coraki.Stanford.EDU by mwunix.mitre.org (5.61/SMI-2.2)
        id AA25046; Thu, 29 Mar 90 11:15:49 -0500
Organization: The MITRE Corp.
Received: by coraki.stanford.edu (4.0/25-eef) id AA15347; Thu, 29 Mar 90 08:16:56 PST
Date: Thu, 29 Mar 90 08:16:56 PST
From: Vaughan Pratt <pratt@cs.stanford.edu>
Message-Id: <9003291616.AA15347@coraki.stanford.edu>
To: daryl%mdf@mwunix.mitre.org
Subject: determining memory size
X-Mdf: daryl <Crandall, Daryl O> re-routed to "daryl@gateway.mitre.org"
Status: R

You need to run the following as root, or much better, setgid kmem.
        Vaughan Pratt

#include <stdio.h>
#include <nlist.h>
#include <sys/file.h>

#define BLOCKS_PER_MBYTE 128

char *kernel_mem = "/dev/kmem";
char *kernel = "/vmunix";

int kmem;

struct nlist nl[] = {
#define PHYSMEM 0
        { "_physmem" },
        { "" }
};

main(argc, argv)
int argc;
char **argv;
{
        long physmem;

        if ((kmem = open(kernel_mem, O_RDONLY, 0)) < 0) {
                fprintf(stderr, "Can't open %s\n", kernel_mem);
                exit(-1);
        }
        nlist("/vmunix", nl);
        physmem = getword(nl[PHYSMEM].n_value) + BLOCKS_PER_MBYTE/2;
        printf("%d\n", physmem/BLOCKS_PER_MBYTE);
        close(kmem);
}

long
getword(loc)
unsigned long loc;
{
        long word;
        if (lseek(kmem, (long)loc, 0) == -1) {
                perror("lseek: ");
                exit(1);
        }
        if (read(kmem, (char *)&word, sizeof(word)) != sizeof (word))
                printf("error reading %s at %x\n", kernel_mem, loc);
        return (word);
}
###########################################################################
###########################################################################



This archive was generated by hypermail 2.1.2 : Fri Sep 28 2001 - 23:05:56 CDT