#! perl
# ======================================================================
# $Author: Jmk $
# $Date: 19.05.98 7:12 $
# $Archive: /Jmk/scripts/saa/process.pl $
# $Revision: 2 $
# ======================================================================
# shows the task list like the task manager but of any computer
use Win32::PerfLib;
($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} == $processor_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} / 10000000);
}
elsif($counter_ref0->{$i}->{CounterNameTitleIndex} == $memory)
{
$memory{$p} = int($counter_ref0->{$i}->{Counter} / 1024);
}
elsif($counter_ref0->{$i}->{CounterNameTitleIndex} == $page_faults)
{
$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} == $priority)
{
$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 Unknown 1
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;
if ($priority{$id} > 15)
{
$prio = "Realtime";
}
elsif ($priority{$id} > 10 )
{
$prio = "High";
}
elsif ($priority{$id} > 5 )
{
$prio = "Normal";
}
elsif ($priority{$id} > 0 )
{
$prio = "Low";
}
else
{
$prio = "Unknown";
}
printf("% 4d %-14s%5.2f % 3d:%02d:%02d % 8d K % 8d % 8d K %8s % 3d\n",
$p, $process{$p}, $proc_time{$id}, $hour, $minute, $seconds,
$memory{$id}, $page_faults{$id}, $virtual_memory{$id},
$prio, $threads{$id});
}
# ======================================================================
# $History: process.pl $
#
# ***************** Version 2 *****************
# User: Jmk Date: 19.05.98 Time: 7:12
# Updated in $/Jmk/scripts/saa
# some comments added
# ======================================================================
> -----Original Message-----
> From: [EMAIL PROTECTED]
> [mailto:[EMAIL PROTECTED] On
> Behalf Of Veli-Pekka Tätilä
> Sent: Sunday, November 27, 2005 7:40 AM
> To: [email protected]
> Subject: CPU Usage in Percentages for Each Process?
>
>
> Hi,
> Is there a module or some sample code that will let one find
> out the CPU
> usage of a given process in percentages? This figure is
> highly useful and
> can be viewed in XP's task manager. However, none of the
> other tools I've
> seen, including XP's tasklist and Sysinternal's process
> tools, let you get
> at the CPU Usage as such. Many do offer the CPU time,
> however. I'd like to
> experiment with changing process priorities in case of serius
> peaks in CPU
> usage. Thus, a metric of how well the system performs and
> which processes
> hog the CPU the most would be just what I need, I guess.
>
> I've installed some related Win32 modules using PPM including
> perflib and process:: info, memory and perf.
>
> I've Googled and read related bits of MSDN with little luck
> so far. seems
> there are two performance interfaces an NT specific one and
> the other based
> on Windows management instrumentation. However, both seem
> highly abstract
> and complex to work with and might not even offer the CPU
> usage as such.
>
> To be sure, I launched the perfmon application in XP and went
> through the
> list of counters. You do get to user and kernel times on a
> per process basis
> as well as elapsed CPU time, but again I didn't see anything
> related to the
> CPU usage in the list. In Task manager's view/choose column
> dialog, the
> entry goes by the name CPU Usage and is shown by default.
>
> So, does either WMI or the NT performance counters let me
> access the CPU
> Usage directly? If this is not the case, what kind of
> workarounds or other
> means do I have? For example, what do the 3rd party task manager
> replacements use?
>
> I've checked the Win32 API functions in MSDN and as expected,
> did not find
> the CPu usage there, either. Even though I found references to some
> previously undocumented and interesting NT funcs like
> NtQuerySystemInformation. Another thought that struck me
> would be to monitor
> which Win32 API functions taskmgr.exe itself employs, using
> some free Win32
> API monitors out there. However, this job seems difficult as
> I really don't
> know what kind of activity I should be looking for, that is
> by which name
> the function might go by.
>
> Any help greatly appreciated as usual.
>
> --
> With kind regards Veli-Pekka Tätilä
> ([EMAIL PROTECTED]) Accessibility, game music,
> synthesizers and programming: http://www.student.oulu.fi/~vtatila/
>
> _______________________________________________
> Perl-Win32-Users mailing list
> [email protected]
> To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
>
_______________________________________________
Perl-Win32-Users mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs