Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Finding CPU stats on Win32?

by guice (Scribe)
on Apr 15, 2005 at 17:43 UTC ( #448275=perlquestion: print w/replies, xml ) Need Help??
guice has asked for the wisdom of the Perl Monks concerning the following question:

I need to used perl to gather CPU counts and stats of each processor (speed, RAM cache, hyperthreaded or not, etc).

Anybody know of a way, or a script, to gather this information? ASPN was of little help. Currently running ASPerl 5.8.3 on Win2k Systems.

-- philip
We put the 'K' in kwality!

Replies are listed 'Best First'.
Re: Finding CPU stats on Win32?
by NateTut (Deacon) on Apr 15, 2005 at 18:43 UTC
    Check out Win32::SystemInfo. Here's a little code to get you started:
    use Win32::SystemInfo; # Get Memory Information print("Memory Info:\n"); my %mHash; Win32::SystemInfo::MemoryStatus(%mHash,"MB"); foreach (sort(keys(%mHash))) { print("$_\[$mHash{$_}M\]\n"); } print("\n"); # Get Processor Information my %pHash; my $proc = Win32::SystemInfo::ProcessorInfo(%pHash); print("Number of Processors[$pHash{NumProcessors}]\n"); foreach my $Processor (keys(%pHash)) { if($Processor ne 'NumProcessors') { print("Processor[$Processor]\n"); foreach (sort(keys(%{$pHash{$Processor}}))) { print("$_\[" . $pHash{$Processor}->{$_} . "\]\n"); } print("\n"); } } }
      hi, can u pls explain the code to me as i'm a beginner in perl. thanx.
      I've just tried this code (minus the extra closing bracket). It presents me with the print of AvailPage3185.33984375M AvailPhys1187.1953125M AvailVirtual2018.03125M MemLoad42M TotalPage3940.31640625M TotalPhys2047.00390625M TotalVirtual2047.875M but also with the following error message (many times).
      Use of uninitialized value in string eq at c:/Perl/site/lib/Win32/API/ line 156, <DATA> line 164.
      Does anyone please have an explanation for this?

      Readmore for full output:

        I'm not sure of the exact cause but there is something undefined where a value is expected. Unfortunately the problem is in a module and not your code so it might be tougher to track down. I would first try to track it down to the line in my code that is causing the problem. Also make sure you are using strict & warnings.
Re: Finding CPU stats on Win32?
by Joost (Canon) on Apr 15, 2005 at 18:23 UTC
Re: Finding CPU stats on Win32?
by sh1tn (Priest) on Apr 15, 2005 at 18:47 UTC
      Wow nice! Now if I can only find out if there's a way to view if Hyperthreading it turned on/off or possible...

      -- philip
      We put the 'K' in kwality!

        Detecting hyperthreading has to be done from assembler, and I'm not aware of any Win32 API that provides that information. There is an article on the Intel website that explains the intracacies.

        Update: Here's an implementation in D. It should be trivial to convert to C.

        import; import std.stdio; void main ( ) { int ht_supported = 0; int cores_per_socket = 1; asm{ mov EAX, 0; cpuid; test EAX, 0x10000000; jz NO_HYPER; mov ht_supported, 1; mov EAX, 4; cpuid; and EAX, 0xfc000000; shr EAX, 26; add EAX, 1; mov cores_per_socket, EAX; NO_HYPER:; }; if( ht_supported ) { printf( "Hyperthreading is supported and there are %d cores pe +r cpu\n", cores_per_socket ); } else { printf( "Hyperthreading is not supported on this processor\n" +); } }

        Update2: I also turned up this quite comprehensive cpu features detector sample on MSDN

        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        Lingua non convalesco, consenesco et abolesco.
        Rule 1 has a caveat! -- Who broke the cabal?

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://448275]
Approved by Joost
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2017-07-21 05:48 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (319 votes). Check out past polls.