in reply to Re^2: Win32... CPU/RAM usage
in thread Win32... CPU/RAM usage
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?
|
|---|