Skip to content
Browse files

Clean up some of the Win32 work

Fix ProcFS to not create hashes for non-existent files
  • Loading branch information...
1 parent 028a5d6 commit 8f23744a3c78027f11e0d1a085d16a25df296c96 @SineSwiper committed Feb 27, 2013
Showing with 63 additions and 67 deletions.
  1. +2 −1 lib/P9Y/ProcessTable/ProcFS.pm
  2. +4 −0 lib/P9Y/ProcessTable/Process.pm
  3. +57 −66 lib/P9Y/ProcessTable/Win32.pm
View
3 lib/P9Y/ProcessTable/ProcFS.pm
@@ -77,7 +77,8 @@ sub _process_hash {
# process simple cats
foreach my $fn (qw{cmdline}) {
my $file = $pdir->file($fn);
- $hash->{$fn} = $file->slurp if (-f $file);
+ next unless (-f $file);
+ $hash->{$fn} = $file->slurp;
$hash->{$fn} =~ s/\0/ /g;
$hash->{$fn} =~ s/^\s+|\s+$//g;
}
View
4 lib/P9Y/ProcessTable/Process.pm
@@ -83,6 +83,7 @@ has winexe => ( is => 'rwp', predicate => 1 );
# cwd exe root cmdline environ
# minflt cminflt majflt cmajflt ttlflt cttlflt utime stime cutime cstime start time ctime
# priority fname state ttynum ttydev flags threads size rss wchan cpuid pctcpu pctmem
+# winpid winexe
# / );
#}
@@ -185,6 +186,9 @@ Depending on the OS, the following methods are available. Also, all methods als
environ Environment variables for the process (as a HASHREF)
fname Filename (typically without a path)
+ winpid (Cygwin only) Windows PID
+ winexe (Cygwin only) Windows Executable path
+
minflt Minor page faults
cminflt Minor page faults of children
majflt Major page faults
View
123 lib/P9Y/ProcessTable/Win32.pm
@@ -30,23 +30,30 @@ my $IS_CYGWIN = ($^O =~ /cygwin/i) ? 1 : 0;
no warnings 'redefine';
-
sub list {
- my %winpids = map { $_ => 1 } _win32_list();
- my %cygpids = map { $_ => 1 } ($IS_CYGWIN ? _cyg_list() : ());
- my %pids = (%winpids, %cygpids,);
+ my ($self) = @_;
+ my %winpids = map { $_ => 1 } $self->_win32_list;
+ my %cygpids = map { $_ => 1 } ($IS_CYGWIN ? $self->_cyg_list : ());
+ my %pids = (%winpids, %cygpids);
return sort { $a <=> $b } keys %pids;
}
-
sub fields {
- my %winflds = map { $_ => 1 } _win32_fields();
- my %cygflds = map { $_ => 1 } ($IS_CYGWIN ? _cyg_fields() : ());
- my %fields = (%winflds, %cygflds,);
- return sort { lc $a cmp lc $b } keys %fields;
+ my ($self) = @_;
+ my %winflds = map { $_ => 1 } $self->_win32_fields;
+ my %cygflds = map { $_ => 1 } ($IS_CYGWIN ? $self->_cyg_fields : ());
+ my %fields = (%winflds, %cygflds);
+
+ # (keeps the order straight)
+ return grep { $fields{$_} } ( qw/
+ pid uid gid euid egid suid sgid ppid pgrp sess
+ cwd exe root cmdline environ
+ minflt cminflt majflt cmajflt ttlflt cttlflt utime stime cutime cstime start time ctime
+ priority fname state ttynum ttydev flags threads size rss wchan cpuid pctcpu pctmem
+ winpid winexe
+ / );
}
-
sub process {
my ($self, $pid) = @_;
$pid = $$ unless defined $pid;
@@ -57,69 +64,55 @@ sub process {
return P9Y::ProcessTable::Process->new($hash);
}
-
sub _process_hash {
my ($self, $pid) = @_;
-
- my $hash = {};
- if ($IS_CYGWIN and Cygwin::pid_to_winpid($pid)) {
- $hash = _cyg_process_hash($pid);
- }
- else {
- $hash = _win32_process_hash($pid);
- }
-
- return $hash;
+ return ($IS_CYGWIN and Cygwin::pid_to_winpid($pid)) ?
+ $self-> _cyg_process_hash($pid) :
+ $self->_win32_process_hash($pid)
+ ;
}
############################
## Win32 only methods
-
sub _win32_list {
return sort { $a <=> $b } ($pi->ListPids);
}
-
sub _win32_fields {
- return (
- qw/
- pid uid ppid sess
- exe root
- ttlflt utime stime start state time
- threads priority fname state size rss
- /
+ return qw(
+ pid uid ppid sess
+ exe root
+ ttlflt utime stime start state time
+ threads priority fname state size rss
);
}
-
sub _win32_process_hash {
- my ($pid) = @_;
+ my ($self, $pid) = @_;
my $info = $pi->GetProcInfo($pid);
return unless $info;
$info = $info->[0];
- my $hash = {};
- my $stat_loc = {
- qw/
- pid ProcessId
- uid Owner
- ppid ParentProcessId
- sess SessionId
- exe ExecutablePath
- threads ThreadCount
- priority Priority
- ttlflt PageFaults
- utime UserModeTime
- stime KernelModeTime
- size VirtualSize
- rss WorkingSetSize
- fname Caption
- start CreationDate
- state Status
- cmdline CommandLine
- /
- };
+ my $hash = {};
+ my $stat_loc = { qw/
+ pid ProcessId
+ uid Owner
+ ppid ParentProcessId
+ sess SessionId
+ exe ExecutablePath
+ threads ThreadCount
+ priority Priority
+ ttlflt PageFaults
+ utime UserModeTime
+ stime KernelModeTime
+ size VirtualSize
+ rss WorkingSetSize
+ fname Caption
+ start CreationDate
+ state Status
+ cmdline CommandLine
+ / };
foreach my $key (keys %$stat_loc) {
my $item = $info->{ $stat_loc->{$key} };
@@ -136,6 +129,7 @@ sub _win32_process_hash {
############################
## Cygwin only methods
+### TODO: Leverage ProcFS, instead of copying the same code ###
sub _cyg_list {
my @list;
@@ -152,7 +146,6 @@ sub _cyg_list {
return @list;
}
-
sub _cyg_fields {
return qw(
pid uid gid ppid pgrp sess
@@ -164,9 +157,8 @@ sub _cyg_fields {
);
}
-
sub _cyg_process_hash {
- my ($pid) = @_;
+ my ($self, $pid) = @_;
my $pdir = dir('', 'proc', $pid);
return unless (-d $pdir);
@@ -186,10 +178,11 @@ sub _cyg_process_hash {
# process simple cats
foreach my $fn (qw{cmdline winpid winexename}) {
my $file = $pdir->file($fn);
- $hash->{$fn} = $file->slurp if (-f $file);
+ next unless (-f $file);
+ $hash->{$fn} = $file->slurp;
$hash->{$fn} =~ s/\0/ /g;
$hash->{$fn} =~ s/^\s+|\s+$//g;
- $hash->{winexe} = $hash->{$fn} if ($fn eq 'winexename');
+ $hash->{winexe} = delete $hash->{$fn} if ($fn eq 'winexename');
}
# process main PID stats
@@ -209,12 +202,10 @@ sub _cyg_process_hash {
};
# See cygwin/fhandler_process.cc for the order
- my $stat_loc = [
- qw(
- pid fname state ppid pgrp sess ttynum . flags minflt cminflt majflt cmajflt
- utime stime cutime cstime priority . . . . size rss .
- )
- ];
+ my $stat_loc = [ qw(
+ pid fname state ppid pgrp sess ttynum . flags minflt cminflt majflt cmajflt
+ utime stime cutime cstime priority . . . . size rss .
+ ) ];
foreach my $i (0 .. @data - 1) {
next if $stat_loc->[$i] eq '.';
@@ -224,10 +215,10 @@ sub _cyg_process_hash {
$hash->{fname} =~ s/^\((.+)\)$/$1/;
$hash->{state} = $states->{ $hash->{state} };
- $hash->{time} = $hash->{utime} + $hash->{stime};
+ $hash->{ time} = $hash->{ utime} + $hash->{ stime};
$hash->{ctime} = $hash->{cutime} + $hash->{cstime};
- $hash->{ttlflt} = $hash->{minflt} + $hash->{majflt};
+ $hash->{ ttlflt} = $hash->{ minflt} + $hash->{ majflt};
$hash->{cttlflt} = $hash->{cminflt} + $hash->{cmajflt};
}

0 comments on commit 8f23744

Please sign in to comment.
Something went wrong with that request. Please try again.