Skip to content

Commit

Permalink
Add Darwin
Browse files Browse the repository at this point in the history
Add new Process class with some changes to structure
  • Loading branch information
SineSwiper committed Oct 12, 2012
1 parent c6018ca commit 90bc9c9
Show file tree
Hide file tree
Showing 10 changed files with 436 additions and 50 deletions.
23 changes: 20 additions & 3 deletions lib/P9Y/ProcessTable.pm
Expand Up @@ -32,11 +32,28 @@ BEGIN {
require P9Y::ProcessTable::ProcFS;
}
else {
die "No idea how to handle $^O processes. Email me with more information!";
die "No idea how to handle $^O processes. Email me with more information!";
}
}
}
}

#############################################################################
# Common Methods (may potentially be redefined with OS-specific ones)

sub table {
my $self = shift;
return map { $self->process($_) } ($self->list);
}

sub process {
my ($self, $pid) = @_;
$pid = $$ if (@_ == 1);
my $hash = $self->_process_hash($pid);
return unless $hash;

$hash->{_pt_obj} = $self;
return P9Y::ProcessTable::Process->new($hash);
}

42;
Expand All @@ -46,9 +63,9 @@ __END__
=begin wikidoc
= SYNOPSIS
# code
= DESCRIPTION
### Ruler ##################################################################################################################################12345
Expand Down
14 changes: 7 additions & 7 deletions lib/P9Y/ProcessTable/BSD.pm
@@ -1,4 +1,5 @@
package P9Y::ProcessTable;
package # hide from PAUSE
P9Y::ProcessTable;

# VERSION
# ABSTRACT: BSD process table
Expand All @@ -8,6 +9,7 @@ package P9Y::ProcessTable;

use sanity;
use Moo;
use P9Y::ProcessTable::Process;

use BSD::Process;

Expand All @@ -17,17 +19,12 @@ no warnings 'uninitialized';
#############################################################################
# Methods

sub table {
my $self = shift;
return map { $self->process($_) } ($self->list);
}

sub list {
my $self = shift;
return sort { $a <=> $b } (BSD::Process::list);
}

sub process {
sub _process_hash {
my ($self, $pid) = @_;
my $info = BSD::Process::info($pid);
return unless $info;
Expand Down Expand Up @@ -69,6 +66,9 @@ sub process {
my $item = $info->{ $stat_loc->{$key} };
$hash->{$key} = $item if defined $item;
}

$hash->{ ttlflt} = $hash->{ minflt} + $hash->{ majflt};
$hash->{cttlflt} = $hash->{cminflt} + $hash->{cmajflt};

state $states = {
stat_1 => 'forking',
Expand Down
133 changes: 133 additions & 0 deletions lib/P9Y/ProcessTable/Darwin.pm
@@ -0,0 +1,133 @@
package # hide from PAUSE
P9Y::ProcessTable;

# VERSION
# ABSTRACT: Darwin/OSX process table

#############################################################################
# Modules

use sanity;
use Moo;
use P9Y::ProcessTable::Process;

use Proc::ProcessTable;
use List::AllUtils 'first';

use namespace::clean;
no warnings 'uninitialized';

my $pt = Proc::ProcessTable->new();

#############################################################################
# Methods

# Unfortunately, P:PT has no concept of anything except "grab everything at once". So, we need to run
# through these wasteful cycles just to get one process, one list of PIDs, etc.

no warnings 'redefine';

sub table {
my $self = shift;
return map {
my $hash = $self->_convert_hash($_);
$hash->{_pt_obj} = $self;
P9Y::ProcessTable::Process->new($hash);
} ($pt->table);
}

sub list {
my $self = shift;
return sort { $a <=> $b } map { $_->pid } @{ $pt->table };
}

sub _process_hash {
my ($self, $pid) = @_;
my $info = first { $_->pid == $pid } @{ $pt->table };
return unless $info;
return $self->_convert_hash;
}

sub _convert_hash {
my ($self, $info) = @_;
return unless $info;

my $hash = {};
state $stat_loc = { qw/
pid pid
ppid ppid
pgrp pgrp
uid uid
gid gid
euid euid
egid egid
suid suid
sgid sgid
priority priority
size size
rss rss
flags flags
nice nice
sess sess
time time
stime stime
utime utime
start start
wchan wchan
ttydev ttydev
ttynum ttynum
pctcpu pctcpu
pctmem pctmem
state state
cmdline cmndline
fname fname
/ };

foreach my $key (keys %$stat_loc) {
no strict 'refs';
my $old = $stat_loc->{$key};
my $item = $info->$old();
$hash->{$key} = $item if defined $item;
}

$hash->{ ttlflt} = $hash->{ minflt} + $hash->{ majflt};
$hash->{cttlflt} = $hash->{cminflt} + $hash->{cmajflt};

return $hash;
}

42;

__END__
=begin wikidoc
= SYNOPSIS
# code
= DESCRIPTION
### Ruler ##################################################################################################################################12345
Insert description here...
= CAVEATS
### Ruler ##################################################################################################################################12345
Bad stuff...
= SEE ALSO
### Ruler ##################################################################################################################################12345
Other modules...
= ACKNOWLEDGEMENTS
### Ruler ##################################################################################################################################12345
Thanks and stuff...
=end wikidoc
18 changes: 13 additions & 5 deletions lib/P9Y/ProcessTable/OS2.pm
@@ -1,4 +1,5 @@
package P9Y::ProcessTable;
package # hide from PAUSE
P9Y::ProcessTable;

# VERSION
# ABSTRACT: OS/2 process table
Expand All @@ -8,6 +9,7 @@ package P9Y::ProcessTable;

use sanity;
use Moo;
use P9Y::ProcessTable::Process;

use OS2::Process;

Expand All @@ -17,24 +19,30 @@ no warnings 'uninitialized';
#############################################################################
# Methods

no warnings 'redefine';

sub table {
my $self = shift;
return map { $self->_process_hash($_) } (process_hentries);
return map {
my $hash = $self->_convert_hash($_);
$hash->{_pt_obj} = $self;
P9Y::ProcessTable::Process->new($hash);
} (process_hentries);
}

sub list {
my $self = shift;
return sort { $a <=> $b } map { $_->{owner_pid} } (process_hentries);
}

sub process {
sub _process_hash {
my ($self, $pid) = @_;
my $info = process_hentry($pid);
return unless $info;
return $self->_process_hash($info);
return $self->_convert_hash;
}

sub _process_hash {
sub _convert_hash {
my ($self, $info) = @_;
return unless $info;

Expand Down
20 changes: 10 additions & 10 deletions lib/P9Y/ProcessTable/ProcFS.pm
@@ -1,4 +1,5 @@
package P9Y::ProcessTable;
package # hide from PAUSE
P9Y::ProcessTable;

# VERSION
# ABSTRACT: /proc FS process table
Expand All @@ -8,6 +9,7 @@ package P9Y::ProcessTable;

use sanity;
use Moo;
use P9Y::ProcessTable::Process;

use Path::Class;
use File::Slurp 'read_file';
Expand All @@ -19,11 +21,6 @@ no warnings 'uninitialized';
#############################################################################
# Methods

sub table {
my $self = shift;
return map { $self->process($_) } ($self->list);
}

sub list {
my $self = shift;

Expand All @@ -40,7 +37,7 @@ sub list {
return sort { $a <=> $b } @list;
}

sub process {
sub _process_hash {
my ($self, $pid) = @_;

my $pdir = dir('', 'proc', $pid);
Expand Down Expand Up @@ -95,7 +92,7 @@ sub process {
};

state $stat_loc = [ qw(
pid fname state ppid pgrp sess ttynum . flags minflt cminflt cmajflt utime stime cutime cstime priority . threads . .
pid fname state ppid pgrp sess ttynum . flags minflt cminflt majflt cmajflt utime stime cutime cstime priority . threads . .
size . rss . . . . . . . . . wchan . . . cpuid . . . . .
) ];

Expand All @@ -107,8 +104,11 @@ sub process {

$hash->{fname} =~ s/^\((.+)\)$/$1/;
$hash->{state} = $states->{ $hash->{state} };
$hash->{time} = $hash->{utime} + $hash->{stime};
$hash->{ctime} = $hash->{cutime} + $hash->{stime};
$hash->{ time} = $hash->{ utime} + $hash->{ stime};
$hash->{ctime} = $hash->{cutime} + $hash->{cstime};

$hash->{ ttlflt} = $hash->{ minflt} + $hash->{ majflt};
$hash->{cttlflt} = $hash->{cminflt} + $hash->{cmajflt};
}
elsif ($^O eq /solaris|sunos/i) {
### Solaris ###
Expand Down

0 comments on commit 90bc9c9

Please sign in to comment.