Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Using Perl to detect RAM amount

by Tommy (Chaplain)
on Jan 06, 2004 at 00:14 UTC ( [id://318982]=perlquestion: print w/replies, xml ) Need Help??

Tommy has asked for the wisdom of the Perl Monks concerning the following question:

Detecting RAM

I need to detect how much RAM is installed on a given machine, independent of the operating system it runs and the type of architecture.

Why I ask

I work at a datacenter which also runs a web-hosting subsidiary. The web hosting side of the business sells or leases out machines as dedicated servers. I frequently need to detect the amount of RAM that is installed on a given machine before racking it and leasing it out, etc. The need to add/remove RAM is necessitated by the type of service package a customer signs up for.

Target Usage

I'd like to compile the resulting code as a stand-alone executable with PAR and see if I could fit it on a bootable floppy of some sort, like some linux flavor or DOS perhaps. That way I could run it on virtually any machine in here, except for those pesky cobalts.

--
Tommy Butler, a.k.a. TOMMY

Replies are listed 'Best First'.
Re: Using Perl to detect RAM amount
by BrowserUk (Patriarch) on Jan 06, 2004 at 02:37 UTC

    This script does the job on Win32.

    #! perl -slw use Win32::API::Prototype; my $memoryStatus = AllocMemory( 32 ); ApiLink( 'Kernel32', 'GlobalMemoryStatus', 'P', 'V' ); GlobalMemoryStatus( $memoryStatus ); my @keys = qw[ length PercentageLoad TotalPhysical AvailablePhysical TotalPaged AvailablePaged TotalVirtualMemory AvailableVirtualMemory ]; my %mStatus; @mStatus{ @keys } = unpack 'V8', $memoryStatus; printf '%30s : %.2f' . $/, $_, $mStatus{ $_ } / 2**20 for @keys[1 .. $#keys ]; __END__ P:\test>gmem PercentageLoad : 0.00 TotalPhysical : 223.24 AvailablePhysical : 54.38 TotalPaged : 408.66 AvailablePaged : 228.52 TotalVirtualMemory : 2047.88 AvailableVirtualMemory : 2019.03

    The TotalPhysical is the one you need. There have been several threads on doing this on various flavours of *nix.

    With respect to putting the two on a floppy disc, you might get away with miniperl.exe (5.6.2 ~= 700k) from the source distribution. You would have to copy a few dependancies Config, Dynaloader, Autoloader, Win32::API::Prototype etc. including their binary components. The list isn't huge, however, that doesn't solve the dual boot problem.

    But, given that the physical memory doesn't vary from OS to OS (unless you have RAM disks and stuff configured), you would probably be better off booting from a DOS floppy (assuming the hardware can run that) and use the DOS mem command (though that gives a weird mixture of real mode and protect mode numbers).

    Or maybe a freeware DOS utility from somewhere. There used to be loads of things like that kicking around. It wouldn't work on non-DOSable hardware, but not much will.

    Come to that, if you can boot into the bios configuration, the information ought to be available there.


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
    Hooray!

      WOW! Impressive! Problem is, I can never get Win32::API to install on my machine, because I run C:\cywin\bin\perl.exe from within the cygwin environment, and generally shun the less portable/compatible-with-my-linux-development ActiveState Perl.

      But it's worth a shot. I'll definitely give this one a try! Thanks again, ++BrowserUk

      --
      Tommy Butler, a.k.a. TOMMY
      
        No, ActivePerl is far more portable and compatible with your Linux environment than cygwin is.

        Much as I like cygwin.


        --
        Regards,
        Helgi Briem
        hbriem AT simnet DOT is

      This script does the job on Win32.

      So does this....

      C:\>perl -e "print `mem`" 655360 bytes total conventional memory 655360 bytes available to MS-DOS 633712 largest executable program size 1048576 bytes total contiguous extended memory 0 bytes available contiguous extended memory 941056 bytes available XMS memory MS-DOS resident in High Memory Area C:\>

      Actually the results or DOS mem are RUBBISH - Please Ignore this post...

      cheers

      tachyon

Re: Using Perl to detect RAM amount
by dominix (Deacon) on Jan 06, 2004 at 02:19 UTC
    Having read recently Determining memory usage of a process... which talk about Process Memory using Proc::ProcessTable, I thougth maybe making a test that assign a certain|determined amount of memory would be enoughh for what you want to achieve ? If not, I don't know any way of doing direclty such a scan. You could think about a full set of command, system specific :-( , that check that for you depending on system . eg netstat -k on Solaris, free on linux, ... wow there is a lot of system to scan. If so, Perlmonk will be a good place to ask for such a platform's specific command isn't it ?
    --
    dominix

      Well that's usefull information, thanks :-)

      I just might have to do some real hunting. I have to ask myself how is such a thing done in other programming languages. I might end up poking around some source code for linux's "free" (if I can get it) or see if there's some way to get access to the BIOS from Perl. Haven't searched the CPAN for "BIOS" yet.

      --
      Tommy Butler, a.k.a. TOMMY
      
        Look for "Firmware" as well if you intend to test memory on Macintosh or Sun machines. (BIOS is for x86 PC only)
        --
        dominix
Re: Using Perl to detect RAM amount
by Abigail-II (Bishop) on Jan 06, 2004 at 00:42 UTC
    I need to detect how much RAM is installed on a given machine, independent of the operating system it runs and the type of architecture.

    I'd like to compile the resulting code as a stand-alone executable with PAR and see if I could fit it on a bootable floppy of some sort, like some linux flavor or DOS perhaps. That way I could run it on virtually any machine in here, except for those pesky cobalts.

    If you are going to put the resulting program on a bootable floppy, does the solution have to be portable between OSses? Or are you going to cram multiple OSses on the same floppy? And the floppy is going to be run on say on both Intel x86 architecture and SUN Sparc architecture?

    However, I very much doubt you'll be able to put a kernel *and* a Perl runtime environment one a floppy. Regardless of the OS. (Even with PAR, you need a Perl runtime environment - it's there, even if it's in one huge binary).

    Abigail

      This is true, that the runtime must also be present. I've also considered using a business-card CD and running it from source which won't run into architecture problems like a PAR executable (at least I don't think so).

      Still you offer me no suggestions... Do you know of any code that could accomplish this design, if even on one architechture and not many?

      --
      Tommy Butler, a.k.a. TOMMY
      
        Linux: cat /proc/meminfo. On Solaris, one can dump the hardware device tree - this will include the memory. (I can't remember the command). IIRC, on HP-UX, one of the *scan commands will show the physical amount of memory. Typically, after a reboot, dmesg will show that info there somewhere as well. Of course, none of this is very platform independent.

        Abigail

For x86 machines...
by Anonymous Monk on Jan 06, 2004 at 13:38 UTC
    Why not use memtest86? You can write the memtest86 image to a floppy, and use that floppy to boot the x86 machines to get the memory amount (and run tests on the memory to make sure it's good, to boot! -- pun intended).

    The pesky cobalts will require another method, however.

    And yes, you may deduct points from this suggestion since no Perl was used.

      No point deductions. It's a good solution. Sometimes Perl isn't the best solution. I just find that most of the time it is for me, so I start with Perl first.

      --
      Tommy Butler, a.k.a. TOMMY
      
Re: Using Perl to detect RAM amount
by zentara (Archbishop) on Jan 06, 2004 at 17:25 UTC
    This works nice on linux:
    #!/usr/bin/perl use Linux::MemInfo; %hash = get_mem_info(); foreach(sort keys %hash) { print "$_ = $hash{$_} \n"; }
      I'm glad this topic came up, because I have a similar need.

      What I don't see yet though, is a Mac OS X-based solution. I'm not much of a Mac guy, so I'm still digging around, but if anyone has an idea, I'd love to hear it.

      Doug

      d'oh! one more day of digging and I found the system_profiler -detailLevel -2 command in OS X 10.3.

Re: Using Perl to detect RAM amount
by TomDLux (Vicar) on Jan 07, 2004 at 01:07 UTC

    If you're going to reboot, anyway, use a Knoppix boot CD.

    I don't have one handy to test with, but Linux normally displays the installed RAM during the boot process.

    If not, use dmesg, once the machine has started up, to see the installed RAM

    If that doesn't work for some reason, use the top command; available memory is discussed on the fourth line of the header.

    --
    TTTATCGGTCGTTATATAGATGTTTGCA

      That's true, http://knoppix.org/ is pretty cool. I just wanted to see if I could do it with Perl. Thanks for the tip. I forgot about using knoppix for that. Seems I only think of it when I need to reset somebody's root password on a linux box.

      --
      Tommy Butler, a.k.a. TOMMY
      

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://318982]
Approved by bassplayer
Front-paged by broquaint
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2024-04-19 19:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found