Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

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:

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

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


Comment on Re: Windows 7 CPU usage
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (7)
As of 2014-07-10 06:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (200 votes), past polls