3 # Copyright (C) 2007-2009 Martin A. Hansen.
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 # http://www.gnu.org/copyleft/gpl.html
22 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DESCRIPTION <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
24 # Monitor status running Biopieces.
26 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
31 use Maasha::Biopieces;
36 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
39 my ( $options, $allowed, @files, $file, $user, $pid, $script, $info, $time0, $time1, $args, $du, @table );
41 $allowed = "USER,PID,%CPU,%MEM,TMP_DIR,START_TIME,RUN_TIME,COMMAND";
43 $options = Maasha::Biopieces::parse_options(
45 { long => 'time', short => 't', type => 'uint', mandatory => 'no', default => 5, allowed => undef, disallowed => 0 },
46 { long => 'sort', short => 's', type => 'string', mandatory => 'no', default => '%CPU', allowed => $allowed, disallowed => undef },
47 { long => 'user', short => 'u', type => 'string', mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
57 @files = Maasha::Filesys::ls_files( $ENV{ 'BP_TMP' } );
58 @files = grep /\.status$/, @files;
60 if ( $options->{ 'user' } ) {
61 @files = grep /$options->{ 'user' }/, @files;
64 foreach $file ( @files )
66 ( $user, $script, $pid ) = split /\./, $file;
68 if ( $info = process_info_mac( $pid ) )
70 ( $time0, $args, $du ) = get_status_info( $file );
72 $info->{ 'START_TIME' } = $time0;
73 $info->{ 'RUN_TIME' } = Maasha::Common::time_stamp_diff( $time0, Maasha::Common::time_stamp() );
74 $info->{ 'TMP_DIR' } = $du || "N/A";
75 $info->{ 'COMMAND' } = "$script $args";
81 print STDERR "Removing stale file: $file\n";
86 if ( $options->{ 'sort' } =~ /^(USER|TMP_DIR|START_TIME|RUN_TIME|COMMAND)$/ ) {
87 @table = sort { $a->{ $options->{ 'sort' } } cmp $b->{ $options->{ 'sort' } } } @table;
89 @table = sort { $a->{ $options->{ 'sort' } } <=> $b->{ $options->{ 'sort' } } } @table;
92 print join( "\t", 'USER', 'PID', '%CPU', '%MEM', 'TMP_DIR', 'START_TIME ', 'RUN_TIME', 'COMMAND' ), "\n";
93 print join( "\t", $_->{ 'USER' }, $_->{ 'PID' }, $_->{ '%CPU' }, $_->{ '%MEM' }, $_->{ 'TMP_DIR' }, $_->{ 'START_TIME' }, $_->{ 'RUN_TIME' }, $_->{ 'COMMAND' } ), "\n" foreach @table;
95 sleep $options->{ 'time' };
99 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SUBROUTINES <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
104 # Martin A. Hansen, June 2009.
106 # Get process info for a given process given a PID by
107 # calling ps aux on a Mac.
110 # USER PID %CPU %MEM VSZ RSS TT STAT STARTED TIME COMMAND
112 my ( $pid, # process id
117 my ( @processes, $process, @fields, %info_hash, $hour, $min, $ampm );
119 @processes = `ps aux`;
123 foreach $process ( @processes )
125 @fields = split " ", $process;
127 if ( $fields[ 1 ] == $pid )
130 "USER" => $fields[ 0 ],
131 "PID" => $fields[ 1 ],
132 "%CPU" => $fields[ 2 ],
133 "%MEM" => $fields[ 3 ],
134 "VSZ" => $fields[ 4 ],
135 "RSS" => $fields[ 5 ],
136 "TT" => $fields[ 6 ],
137 "STAT" => $fields[ 7 ],
138 "STARTED" => $fields[ 8 ],
139 "TIME" => $fields[ 9 ],
140 "COMMAND" => $fields[ 10 ],
143 if ( $info_hash{ 'STARTED' } =~ /^(\d+):(\d+)(AM|PM)$/ )
149 if ( $ampm eq "PM" ) {
153 $info_hash{ 'STARTED' } = "$hour:$min";
156 return wantarray ? %info_hash : \%info_hash;
169 my ( $fh, $line, $time, $args, $tmp_dir, $du );
171 $fh = Maasha::Filesys::file_read_open( $file );
177 ( $time, $args, $tmp_dir ) = split ";", $line;
179 if ( $tmp_dir and -d $tmp_dir )
181 $du = `du -sh $tmp_dir`;
188 return wantarray ? ( $time, $args, $du ) : [ $time, $args, $du ];
192 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
197 Maasha::Biopieces::status_set();
203 Maasha::Biopieces::status_log();
207 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<