Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Re: Windows 7 CPU usage

by gulden (Monk)
on Feb 02, 2012 at 16:09 UTC ( #951475=note: print w/replies, xml ) Need Help??

in reply to Windows 7 CPU usage

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:

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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://951475]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2018-01-22 08:41 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (233 votes). Check out past polls.