[comp.unix.questions] determining size of physical memory

hitt@Neon.Stanford.EDU (Daniel Hitt) (12/28/90)

Is there a standard UNIX program or system call that determines
the size of the physical memory of the machine on which it is
running?

What manual page should i be looking at?

I'd like to be able to do this on Ultrix, SunOS, and the NeXT OS,
and possibly HP-UX.

dan

paul@prcrs.UUCP (Paul Hite) (12/29/90)

In article <1990Dec27.202715.27688@Neon.Stanford.EDU>, hitt@Neon.Stanford.EDU (Daniel Hitt) writes:
> Is there a standard UNIX program or system call that determines
> the size of the physical memory of the machine on which it is
> running?
> 
> I'd like to be able to do this on Ultrix, SunOS, and the NeXT OS,
> and possibly HP-UX.

Well, I don't know of any "standard" way to do this.  But here are some HP-UX
solutions that you may be able to adapt to those other OS'es.  These have been
tested under HP-UX 3.1 and 7.0 on 9000 800's.

1)	HP-UX displays the physical memory at boot time.   Simply look at
	message.  These boot messages are saved in /usr/adm/messages.
	"grep mem /usr/adm/messages" yields:
		real mem = 67108864
		lockable mem = 51251200
		avail mem = 55265280
		using 1601 buffers containing 6709248 bytes of memory

2)	The support tape contains an unsupported program called "monitor".
	It can display memory size as well as many other useful data.

3)	The kernel has a value which indicates memory size.  You can display
	this by using something like:
		#include<stdio.h>
		#include<nlist.h>
		#include<fcntl.h>
		#define KERNEL "/hp-ux"
		main()
		{
		int fdkern;
		static struct nlist nl[2] = { { "physmem" } , { NULL } };
		int physmem;
		int  *address;

			nlist(KERNEL, nl);
			address = (int *)nl[0].n_value;
			fdkern = open("/dev/kmem", O_RDONLY);
			lseek(fdkern,(long) address,0);
			read(fdkern, (char *) &physmem, sizeof(physmem));
			printf("physmem = %d\n", physmem);
			physmem = physmem * 2 / 1024;
			printf("physical memory = %d Meg\n", physmem);
			exit(0);
		}


I hope this helps.

Paul Hite   PRC Realty Systems  McLean,Va   uunet!prcrs!paul    (703) 556-2243
        You can't tell which way the train went by studying its tracks.

bakke@plains.NoDak.edu (Jeffrey P. Bakke) (12/29/90)

In article <1990Dec27.202715.27688@Neon.Stanford.EDU>, hitt@Neon.Stanford.EDU (Daniel Hitt) writes:
> Is there a standard UNIX program or system call that determines
> the size of the physical memory of the machine on which it is
> running?
> 
> I'd like to be able to do this on Ultrix, SunOS, and the NeXT OS,
> and possibly HP-UX.

Well, I'm not positive that this is the best way or the that it will work
correctly under all instances but if you have read access to the /dev/mem
(or /dev/kmem) you might just try
'wc -c < /dev/mem' using the word count program to count the number of
bytes.  I've found on most systems that it will return the correct physical
memory size (not swap space though).  


-- 
Jeffrey P. Bakke                      |   There are a finite number of
  INTERNET:   bakke@plains.NoDak.edu  |   jokes in the world...         
  UUCP    : ...!uunet!plains!bakke    |     The overflow began 
  BITNET  : bakke@plains.bitnet       |   decades ago. 
"I am not a number, I am a free man!" - The Prisoner

kohli@gemed (Jim Kohli) (12/29/90)

In article <7325@plains.NoDak.edu>, bakke@plains.NoDak.edu (Jeffrey P. Bakke)
writes:
 >In article <1990Dec27.202715.27688@Neon.Stanford.EDU>, hitt@Neon.Stanford.EDU (Daniel Hitt) writes:
 >> Is there a standard UNIX program or system call that determines
 >> the size of the physical memory of the machine on which it is
 >> running?
 >> 
 >> I'd like to be able to do this on Ultrix, SunOS, and the NeXT OS,
 >> and possibly HP-UX.
 >
 >Well, I'm not positive that this is the best way or the that it will work
 >correctly under all instances but if you have read access to the /dev/mem
 >(or /dev/kmem) you might just try
 >'wc -c < /dev/mem' using the word count program to count the number of
 >bytes.  I've found on most systems that it will return the correct physical
 >memory size (not swap space though).  
 >
 While in theory this should work, there seems
 to be a problem with SunOS 4.01-- the /dev/mem
 driver gives you an early EOF indicating fewer
 bytes than there really are.

 I don't know if it's fixed in SunOS 4.1.

 There's approach also presents the problem of
 needing to run as root, since /dev/mem is (or SHOULD BE)
 read-protected.  It would be nice if there was a way to
 find out how much physical memory there is in a Sun without
 rebooting.

 Jim Kohli

wrwalke@prcrs.UUCP (William Walker) (01/01/91)

In article <7325@plains.NoDak.edu>, bakke@plains.NoDak.edu (Jeffrey P. Bakke) writes:
> In article <1990Dec27.202715.27688@Neon.Stanford.EDU>, hitt@Neon.Stanford.EDU (Daniel Hitt) writes:
> > Is there a standard UNIX program or system call that determines
> > the size of the physical memory of the machine on which it is
> > running?
> > I'd like to be able to do this on Ultrix, SunOS, and the NeXT OS,
> > and possibly HP-UX.
> 
> 'wc -c < /dev/mem' using the word count program to count the number of
> bytes.  I've found on most systems that it will return the correct physical
> memory size (not swap space though).  

or how about ...

dd if=/dev/mem of=/dev/null bs=1024 

this one is a try-at-you-own-risc solution, but it
works on vax ultrix 3.1 and s800 hp-ux 7.0.

bill.

src@scuzzy.in-berlin.de (Heiko Blume) (01/02/91)

>In article <1990Dec27.202715.27688@Neon.Stanford.EDU>, hitt@Neon.Stanford.EDU (Daniel Hitt) writes:
> Is there a standard UNIX program or system call that determines
> the size of the physical memory of the machine on which it is
> running?
> 
> I'd like to be able to do this on Ultrix, SunOS, and the NeXT OS,
> and possibly HP-UX.

sounds like you'll have to use #ifdef :-) anyway, you might look for
'machine specific' system calls, for example sys V/386 (interactive
in my case) lets you do 

#include <sys/sysi86.h>
long bytes;
bytes=sysi86(SI86MEM);
-- 
      Heiko Blume <-+-> src@scuzzy.in-berlin.de <-+-> (+49 30) 691 88 93
                    public source archive [HST V.42bis]:
        scuzzy Any ACU,f 38400 6919520 gin:--gin: nuucp sword: nuucp
                     uucp scuzzy!/src/README /your/home

lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) (01/03/91)

In article <1369@prcrs.UUCP> wrwalke@prcrs.UUCP (William Walker) writes:
: In article <7325@plains.NoDak.edu>, bakke@plains.NoDak.edu (Jeffrey P. Bakke) writes:
: > In article <1990Dec27.202715.27688@Neon.Stanford.EDU>, hitt@Neon.Stanford.EDU (Daniel Hitt) writes:
: > > Is there a standard UNIX program or system call that determines
: > > the size of the physical memory of the machine on which it is
: > > running?
: > > I'd like to be able to do this on Ultrix, SunOS, and the NeXT OS,
: > > and possibly HP-UX.
: > 
: > 'wc -c < /dev/mem' using the word count program to count the number of
: > bytes.  I've found on most systems that it will return the correct physical
: > memory size (not swap space though).  
: 
: or how about ...
: 
: dd if=/dev/mem of=/dev/null bs=1024 
: 
: this one is a try-at-you-own-risc solution, but it
: works on vax ultrix 3.1 and s800 hp-ux 7.0.

On a Sun you can derive it from the stuff the adb -k command prints out.
The following Perl script does this:

#!/usr/bin/perl

$ENV{'PATH'} = '/bin:/usr/bin';
unless (open(ADB,"-|")) {
    open(STDIN,'/dev/null');
    exec 'adb', '-k', '/vmunix', '/dev/mem';
    exit 1;
}

$_ = <ADB>;
$_ = <ADB> unless /^phys/;
close ADB;
($foo,$size) = split;

$size = (int(hex($size) / 0x100) + 1) * 2;
print $size,"\n";

Larry Wall
lwall@jpl-devvax.jpl.nasa.gov

chris@mimsy.umd.edu (Chris Torek) (01/07/91)

>In article <7325@plains.NoDak.edu>
(which, for some reason, is not included in the references line; apparently
the news software at mrsvr is broken)
>bakke@plains.NoDak.edu (Jeffrey P. Bakke) writes:
>>'wc -c < /dev/mem' [will often produce the answer]

In article <3318@mrsvr.UUCP> kohli@gemed (Jim Kohli) writes:
>While in theory this should work, there seems
>to be a problem with SunOS 4.01-- the /dev/mem
>driver gives you an early EOF indicating fewer
>bytes than there really are.

This is not a bug.  /dev/mem gives access to physical bus addresses.
The physical addresses of the memory in Sun 4s are not always
contiguous.  (That is why you can mix different capacity SIMMs on
these.) Note that on some machines physical memory does not start at
address 0; on these also straightforward reading of /dev/mem with `wc'
or `dd' will not work.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris@cs.umd.edu	Path:	uunet!mimsy!chris