| 1 | package System; | ||
| 2 | use warnings; | ||
| 3 | use strict; | ||
| 4 | use autodie; | ||
| 5 | use base 'Object'; | ||
| 6 | use Array; | ||
| 7 | use IPC::Open3; | ||
| 8 | use Symbol; | ||
| 9 | use Sys::CpuAffinity; | ||
| 10 | |||
| 11 | sub new { | ||
| 12 | my $pkg = shift; | ||
| 13 | return bless {}, $pkg; | ||
| 14 | } | ||
| 15 | |||
| 16 | =head2 local_run | ||
| 17 | local_run "ls", sub { | ||
| 18 | my ($out, $err) = @_; | ||
| 19 | $out->each(sub{ | ||
| 20 | ... | ||
| 21 | }) | ||
| 22 | ... | ||
| 23 | } | ||
| 24 | =cut | ||
| 25 | |||
| 26 | sub local_run { | ||
| 27 | my $inh = gensym; | ||
| 28 | my $outh = gensym; | ||
| 29 | my $errh = gensym; | ||
| 30 | my $cb; | ||
| 31 | if ( ref($cb) ne 'CODE' ) { | ||
| 32 | $cb = pop @_; | ||
| 33 | } | ||
| 34 | my @commands = @_; | ||
| 35 | |||
| 36 | my $ret = open3( $inh, $outh, $errh, @commands ); | ||
| 37 | |||
| 38 | my $stdout = Array->new; | ||
| 39 | my $stderr = Array->new; | ||
| 40 | while (<$outh>) { | ||
| 41 | chomp; | ||
| 42 | $stdout->push($_); | ||
| 43 | } | ||
| 44 | while (<$errh>) { | ||
| 45 | chomp; | ||
| 46 | $stderr->push($_); | ||
| 47 | } | ||
| 48 | if ( defined $cb ) { | ||
| 49 | $cb->( $stdout, $stderr ); | ||
| 50 | return $ret; | ||
| 51 | } | ||
| 52 | else { | ||
| 53 | return $stdout; | ||
| 54 | } | ||
| 55 | } | ||
| 56 | |||
| 57 | =head2 remote_run | ||
| 58 | TODO: async ssh command | ||
| 59 | remote_run @hosts, $cmd, sub { | ||
| 60 | my ($out, $err) = @_; | ||
| 61 | } | ||
| 62 | =cut | ||
| 63 | |||
| 64 | sub remote_run { | ||
| 65 | my $cb = pop @_; | ||
| 66 | my $cmd = pop @_; | ||
| 67 | my @hosts = @_; | ||
| 68 | ...; | ||
| 69 | } | ||
| 70 | |||
| 71 | =head2 taskset | ||
| 72 | taskset($subpid, [0, 2]) | ||
| 73 | =cut | ||
| 74 | |||
| 75 | sub taskset { | ||
| 76 | my ( $pid, @cpus ) = @_; | ||
| 77 | Sys::CpuAffinity::setAffinity( $pid, \@cpus ); | ||
| 78 | } | ||
| 79 | |||
| 80 | 1; |