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

Re: CPU usage via Win32

by myocom (Deacon)
on Aug 08, 2002 at 19:55 UTC ( #188723=note: print w/ replies, xml ) Need Help??


in reply to CPU usage via Win32

The module you're looking for is Win32::PerfLib. With that module, you can get a snapshot of processes and their associated CPU times (along with a whole lot of other useful information). Get several snapshots throughout the day and then you can average them out.

"One word of warning: if you meet a bunch of Perl programmers on the bus or something, don't look them in the eye. They've been known to try to convert the young into Perl monks." - Frank Willison


Comment on Re: CPU usage via Win32
Replies are listed 'Best First'.
Re: Re: CPU usage via Win32
by softworkz (Monk) on Sep 25, 2002 at 18:33 UTC
    So far I've adapted this but I can't seem to get it to work while strict or warnings are in place. I get undeclared varible on GetObjectList, can someone help with this a little further?

    thanks!
    #!/usr/bin/perl use Win32::PerfLib; use Win32; use Cwd; my $node = Win32::NodeName(); ($server) = @ARGV; Win32::PerfLib::GetCounterNames($server, \%counter); %r_counter = map { $counter{$_} => $_ } keys %counter; $process_obj = $r_counter{Process}; $process_id = $r_counter{'ID Process'}; #$processor_time = $r_counter{'% Processor Time'}; $process_obj = 230; $process_id = 784; $processor_time = 6; #$elapsed = 684; $memory = 180; $page_faults = 28; $virtual_memory = 186; $priority = 682; $threads = 680; $perflib = new Win32::PerfLib($server); $proc_ref0 = {}; $proc_ref1 = {}; $perflib->GetObjectList($process_obj, $proc_ref0); sleep 5; $perflib->GetObjectList($process_obj, $proc_ref1); $perflib->Close(); $instance_ref0 = $proc_ref0->{Objects}->{$process_obj}->{Instances}; $instance_ref1 = $proc_ref1->{Objects}->{$process_obj}->{Instances}; foreach $p (keys %{$instance_ref0}) { $counter_ref0 = $instance_ref0->{$p}->{Counters}; $counter_ref1 = $instance_ref1->{$p}->{Counters}; foreach $i (keys %{$counter_ref0}) { # next if $instance_ref0->{$p}->{Name} eq "_Total"; if($counter_ref0->{$i}->{CounterNameTitleIndex} == $process_id +) { $process{$counter_ref0->{$i}->{Counter}} = $instance_ref0->{$p}->{Name}; $id{$counter_ref0->{$i}->{Counter}} = $p; } elsif($counter_ref0->{$i}->{CounterNameTitleIndex} == $process +or_time) { $Numerator0 = $counter_ref0->{$i}->{Counter}; $Denominator0 = $proc_ref0->{PerfTime100nSec}; $Numerator1 = $counter_ref1->{$i}->{Counter}; $Denominator1 = $proc_ref1->{PerfTime100nSec}; $proc_time{$p} = ($Numerator1 - $Numerator0) / ($Denominator1 - $Denominator0 ) * 100; $cputime{$p} = int($counter_ref1->{$i}->{Counter} / 100000 +00); } elsif($counter_ref0->{$i}->{CounterNameTitleIndex} == $memory) { $memory{$p} = int($counter_ref0->{$i}->{Counter} / 1024); } elsif($counter_ref0->{$i}->{CounterNameTitleIndex} == $page_fa +ults) { $page_faults{$p} = $counter_ref1->{$i}->{Counter}; } elsif($counter_ref0->{$i}->{CounterNameTitleIndex} == $virtual +_memory) { $virtual_memory{$p} = int($counter_ref0->{$i}->{Counter} / + 1024); } elsif($counter_ref0->{$i}->{CounterNameTitleIndex} == $priorit +y) { $priority{$p} = $counter_ref0->{$i}->{Counter}; } elsif($counter_ref0->{$i}->{CounterNameTitleIndex} == $threads +) { $threads{$p} = $counter_ref0->{$i}->{Counter}; } } } #print " PID Process CPU CPU-Time Memory PF Virt +.Mem Priority Thr\n"; # 0 Idle 93.73 20:51:40 16 K 1 0 K Un +known 1 my $dump = "proctime.txt"; open(OUTPUT,">$dump") or die "592.pl can't open $dump for writing: +$!"; # print OUTPUT "$node\n"; foreach $p (sort { $a <=> $b } keys %process) { $id = $id{$p}; $seconds = $cputime{$id}; $hour = int($seconds / 3600); $seconds -= $hour * 3600; $minute = int($seconds / 60); $seconds -= $minute * 60; next unless ( !($process{$p} =~ /idle/i) && !($process{$p} =~ /total/ +i) ); printf (OUTPUT "%3.f\n",$proc_time{$id}); } close (OUTPUT); #--------------------------------------------------------------------- +--------- # # This module parses the file that now holds all of the processes runn +ing on # a given node. # #--------------------------------------------------------------------- +--------- my @now = localtime(); my $timeStamp = sprintf("%02d/%02d/%04d %02d:%02d", $now[4]+1, $now[3] +, $now[5]+1900,$now[2],$now[1]); my $file = $node."_proctime.txt"; my $result; open(LOG,"$dump") || die "can't open $dump for reading: $!"; open(OUTPUT,">>$file") or die "can't open $dump for writing: $!"; while (<LOG>) { chomp; my @nums; push @nums, $_; $result += (shift @nums); } $result = $result/4; my $fmt = sprintf "%.0f", $result; print OUTPUT "$node $timeStamp $fmt\n"; close(LOG); unlink ("$dump");

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (9)
As of 2015-07-29 10:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (263 votes), past polls