in reply to Win32... CPU/RAM usage

Something like this?

See the { no_user_info => 1 } parmeter on the GetProcInfo() call. If you don't need user information, gathering it slows things down horribly. (Also, notice the lc inside the grep.)

#! perl -slw use strict; use warnings; use Win32::Process::Info; use Data::Dumper; my $pio_2 = Win32::Process::Info->new (); my @pids = $pio_2->ListPids (); my @proc_info; my %ofInterest = map { $_ => 1 } qw[ perl.exe cmd.exe textpad.exe ]; my @fields = qw[ Name WorkingSetSize UserModeTime KernelModeTime ]; printf "%-25s %-14s %-12s %-14s\n", @fields; for my $pid (@pids) { @proc_info = $pio_2->GetProcInfo( { no_user_info => 1 }, $pid); for my $info ( grep{ exists $ofInterest{ lc $_->{ Name } } } @proc_info ) { printf "%-25s %14d %12.6f %14.7f\n", @{ $info }{ @fields }; } } __END__ C:\test>junk4 Name WorkingSetSize UserModeTime KernelModeTime CMD.EXE 1605632 2.328125 5.5156250 CMD.EXE 36864 0.015625 0.4218750 TextPad.exe 6189056 215.453125 108.9843750 CMD.EXE 65536 0.015625 0.2812500 perl.exe 7122944 0.375000 0.2187500

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."

Replies are listed 'Best First'.
Re^2: Win32... CPU/RAM usage
by mikejones (Scribe) on Jul 06, 2007 at 19:51 UTC
    Will you explain the line b/c I get lost at the lc part
    grep{ exists $ofInterest{ lc $_->{ Name } } } @proc_info ## : )
    Ahh heck just explain the entire embedded for loop:
    for my $info ( grep{ exists $ProcsOfInterest{ lc $_->{ Name } } } @proc_info ) { printf "%-25s %14d %12.6f %14.7f\n", @{ $info }{ @fields }; }

      Sure, NP.

      As you know, GetProcInfo() returns an array of hashes--1 hash per running process. One of the keys in those hashes is the Name of the executable. To avoid processing those processes you are not interest in, I used grep to check each of the return process names against the hash %ofInterest and only let it through if it exists.

      The list of processes that you are interested in is in that hash:

      my %ofInterest = map { $_ => 1 } qw[ perl.exe cmd.exe textpad.exe ];

      Rather than try and get the case of the program names correct--for the three in my example that would be perl.exe, CMD.EXE, TextPad.exe--, I typed them all in lower case and the used lc to lowercase the names return from the system to ensure they matched those I put in the hash.

      So now the for loop body will only be entered for those processes you are interest in. All that is left is to extract the four fields of each of those hashes that you are interested in. I placed the names of those four fields in an array:

      my @fields = qw[ Name WorkingSetSize UserModeTime KernelModeTime ];

      So now I can use a hash slice (see perlreftut for info on hash references and hash slices and much more), to extract the values you want and supply them to printf for display:

      for my $info ( # This ^^^^^ var will be set to the hashref each of the ... grep{ exists $ProcsOfInterest{ lc $_->{ Name } } } @proc_info # ...............^^^^^^^^^^^^^^^^ processes of interest ) { # ........................and is used here vvvvv printf "%-25s %14d %12.6f %14.7f\n", @{ $info }{ @fields }; # .......................in a hash slice ^^ ^^ ^ # ............................with the field names ^^^^^^^ # ..to produce the wanted 4 values and supply them to printf }

      The hash slice

      my @fields = qw[ Name WorkingSetSize UserModeTime KernelModeTime ]; ... printf ... @{ $info }{ @fields };

      is equivalent to:

      printf ... @{ $info }{'Name','WorkingSetSize','UserModeTime','KernelMo +deTime'};

      is equivalent to:

      printf ... $info->{ Name }, $info->{ WorkingSetSize }, $info->{ UserModeTime }, $info->{ KernelModeTime };

      Does that help?


      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.