http://qs1969.pair.com?node_id=448275

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.
    A reply falls below the community's threshold of quality. You may see it by logging in.
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 std.stream; 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?