how can I get the CPU Usage which is the percentage of a process used CPU in Task Manager? use UserModeTime and KernelModeTime ?

That's much harder to do. At any given moment, on a single cpu machine, only one process is running, and using 100% of the cpu, whilst all other processes are idle.

Therefore, to get a %cpu usage figure you need to

  1. gather two sets of statistics, S[1] & S[2], some time apart.
  2. Sum the User and Kernel mode times for each proces, for both sets.
  3. Subtract the second combined time from the first for each process to give the delta time used for the elapsed period.
  4. Sum the deltas to give a total cpu time used within that period.
  5. The %cpu for each process is then the elapsed delta for that process divided by the deltas total * 100.

As you can see, not hugely difficult, but avoiding the stats gathering process itself using a large percentage of the cpu takes care.

If the machine has multiple cpus or cores, life gets harder again.

Here's some code that works for a single cpu, though it consumes 15-20% cpu to produce figures once per second and there is not much that you can do about it. It will give distorted numbers during periods in which a new process starts or an old process finishes. It will also give erroneous results if you have multiple cpus.

#! perl -slw use strict; use warnings; use Win32::Process::Info; use List::Util qw[ sum ]; my $pi = Win32::Process::Info->new; my @pids = $pi->ListPids; my @proc_info; my @fields = qw[ Name WorkingSetSize UserModeTime KernelModeTime ]; my %byPid; while( 1 ) { my @pids = $pi->ListPids; for my $pid ( @pids ) { my @info = $pi->GetProcInfo( { no_user_info => 1 }, $pid ); for my $info ( @info ) { $byPid{ $pid }{ last } = [ @{ $info }{ @fields } ]; } } sleep 1; for my $pid ( @pids ) { my @info = $pi->GetProcInfo( { no_user_info => 1 }, $pid ); for my $info ( @info ) { $byPid{ $pid }{ current } = [ @{ $info }{ @fields } ]; } } my $totalDelta = 0; for my $pid ( @pids ) { $totalDelta += $byPid{ $pid }{ delta } = sum( @{ $byPid{ $pid }{ current } }[ 2, 3 ] ) - sum( @{ $byPid{ $pid }{ last } }[ 2, 3 ] ); } system 'cls'; print "\n pid Name memory %cpu"; for my $pid ( @pids ) { printf "%5d %20s %10u %.1f%%\n", $pid, @{ $byPid{ $pid }{ last } }[ 0, 1 ], $byPid{ $pid }{ delta } * 100 / ($totalDelta||1); } }

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.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."

In reply to Re^5: How to get the Memory usage of specified Process on Windows ? by BrowserUk
in thread How to get the Memory usage of specified Process on Windows ? by ilovit

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.