Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Windows 7 CPU usage

by gepebril69 (Beadle)
on Feb 02, 2012 at 12:14 UTC ( #951420=perlquestion: print w/ replies, xml ) Need Help??
gepebril69 has asked for the wisdom of the Perl Monks concerning the following question:

Hi there, I'm trying to get the CPU usage of a Windows 7 machine(x64), running on a Intel core i5. I've Googled my way around and first found this: Win32::SystemInfo::CpuUsage
use Win32::SystemInfo::CpuUsage; my $intvl = 1000; # in milliseconds my $usage = Win32::SystemInfo::CpuUsage::getCpuUsage($intvl); my $i = 0; while($i < 5) { #query 5 times $i++; $usage = Win32::SystemInfo::CpuUsage::getCpuUsage($intvl); print "$i: cpu usage $usage\n"; }
Which gave me following result
1: cpu usage -1 2: cpu usage -1 3: cpu usage -1 4: cpu usage -1 5: cpu usage -1
Then I found Sys::Info::Constants With following example:
use Sys::Info; use Sys::Info::Constants qw( :device_cpu ); my $Info = Sys::Info->new; my $Cpu = $Info->device( CPU =>%options ); printf "CPU Load: %s % (last minute)\n", ($Cpu->load(DCPU_LOAD_LAST_01 +)*100);
Wich provides me with output that is mostly 10% or more above what Window Task Manager provides me when I monitor CPU usage for a minute...

Three questions:
1) Why does example 1 output only -1 (I also ran as administrator)
2) Why higher than Window Task Manager
3) Is there a better way of doing so?

Thanks in advance

Comment on Windows 7 CPU usage
Select or Download Code
Re: Windows 7 CPU usage
by ww (Bishop) on Feb 02, 2012 at 13:08 UTC
    re Q2: possibly (maybe; not authoratative; but one might suspect that this is better than a WAG): perhaps because Task Manager calculates load using a different scheme or alternate data source.
Re: Windows 7 CPU usage
by BrowserUk (Pope) on Feb 02, 2012 at 13:40 UTC

    1. Because the author chose to use the 'undocumented' function NtQuerySystemInformation() instead of the documented function GetSystemInfo(), and it has either been withdrawn or changed such that his code no longer works. (Also true on Vista.)
      The NtQuerySystemInformation function and the structures that it returns are internal to the operating system and subject to change from one release of Windows to another. To maintain the compatibility of your application, it is better to use the alternate functions previously mentioned instead.
    2. Because the time being measured is different.

      For example, say the cpu usage over a 1 second period, but measured every 50th of a second, looked like this:

      # #### # ###### ## ######### ## ########### ## ############ ### ############# # ### ############### ## # ### ################# ## # ### # ################## ## # ### # ################### ## # ### # # #################### ## # ### ## # #################### ## # # ### ## ## #################### ## # # ### ## ## #################### ## # # ### ## ## #################### # ## # # ### ## ## #################### # ## # # ### ## ## #################### # ### # ## ### ## ## #################### ## ### #### # ### ## ### #################### ## ### #### # ### ## ### ##################### ### ### #### # ### ############################# ####### ########## 0 .1 .2 .3 .4 .5 .6 .7 .8 .9 1.0

      Say you measured the whole of that second and got 50% usage; but the system is measuring every half second and when you looked, it had just measured the first half of that second and found it to be 75%. But if you look half a second later, it would show 25%.

      So the average over the whole second, as measured by the system is 75+25/2 = 50%, which is the same 50% you measured, but it all depends upon the exact start and duration of the instrumented period as to what value you'll get.

      As measurements go, cpu usage is a pretty useless measurement of anything, unless you average it out over a longish period -- several seconds. And then it is only useful as a trend indicator, not something you can use for useful statistics or decision making.

    3. It very much depends upon what the "it" is that you are seeking to do?

      That is, why are you measuring cpu usage? And what do you intend to do with the information?


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    The start of some sanity?

Re: Windows 7 CPU usage
by gulden (Monk) on Feb 02, 2012 at 16:09 UTC
    Hi, I've implemented an example using WMI that gives you the Average CPU usage between samples.

    Here is the code:

    #!/usr/bin/perl -w use strict; use warnings; use Win32::OLE; # Init WMI my $wmi = Win32::OLE->GetObject("winmgmts://./root/cimv2") or die "Failed getobject\n"; # get WMI values sub get_wmi{ my $wmi = shift; my $list, my $v; my @properties = qw(PercentProcessorTime TimeStamp_Sys100NS); my $class = 'Win32_PerfRawData_PerfOS_Processor'; my $key = 'Name'; $list = $wmi->InstancesOf("$class") or die "Failed getobject\n"; my $hash; foreach $v (in $list) { $hash->{$v->{$key}}->{$_} = $v->{$_} for @properties; } $hash; } # CPU calculation my $cpu; my $hash_prev = get_wmi($wmi); while(1){ sleep 2; my $hash = get_wmi($wmi); $cpu = sprintf("%.2f", ( 1 - ( $hash->{'_Total'}->{'PercentProcessorTime'} - $has +h_prev->{'_Total'}->{'PercentProcessorTime'} ) / ( $hash->{'_Total'}->{'TimeStamp_Sys100NS'} - $hash_ +prev->{'_Total'}->{'TimeStamp_Sys100NS'} ) )* 100 ); print "CPU=$cpu\n"; $hash_prev = $hash; }

    Another similar solution in:

    http://stackoverflow.com/questions/4750582/get-wmi-cpu-values-using-perl

    A contentious debate is always associated with a lack of valid arguments.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://951420]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (13)
As of 2014-10-20 11:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (75 votes), past polls