]> git.donarmstrong.com Git - biopieces.git/blob - bp_bin/biostat
fixed up assemble_seq_velvet
[biopieces.git] / bp_bin / biostat
1 #!/usr/bin/env perl
2
3 # Copyright (C) 2007-2009 Martin A. Hansen.
4
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.
9
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.
14
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.
18
19 # http://www.gnu.org/copyleft/gpl.html
20
21
22 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DESCRIPTION <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
23
24 # Monitor status running Biopieces.
25
26 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
27
28
29 use warnings;
30 use strict;
31 use Maasha::Biopieces;
32 use Maasha::Common;
33 use Maasha::Filesys;
34
35
36 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
37
38
39 my ( $options, $allowed, @files, $file, $user, $pid, $script, $info, $time0, $time1, $args, $du, @table );
40
41 $allowed = "USER,PID,%CPU,%MEM,TMP_DIR,START_TIME,RUN_TIME,COMMAND";
42
43 $options = Maasha::Biopieces::parse_options(
44     [
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 },
48     ]   
49 );
50
51 while ( 1 )
52 {
53     system( "clear" );
54
55     undef @table;
56
57     @files = Maasha::Filesys::ls_files( $ENV{ 'BP_TMP' } );
58     @files = grep /\.status$/, @files;
59
60     if ( $options->{ 'user' } ) {
61         @files = grep /$options->{ 'user' }/, @files;
62     }
63
64     foreach $file ( @files )
65     {
66         ( $user, $script, $pid ) = split /\./, $file;
67         
68         if ( $info = process_info_mac( $pid ) )
69         {
70             ( $time0, $args, $du ) = get_status_info( $file );
71
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";
76
77             push @table, $info;
78         }
79     }
80
81     if ( $options->{ 'sort' } =~ /^(USER|TMP_DIR|START_TIME|RUN_TIME|COMMAND)$/ ) {
82         @table = sort { $a->{ $options->{ 'sort' } } cmp $b->{ $options->{ 'sort' } } } @table;
83     } else {
84         @table = sort { $a->{ $options->{ 'sort' } } <=> $b->{ $options->{ 'sort' } } } @table;
85     }
86     
87     print join( "\t", 'USER', 'PID', '%CPU', '%MEM', 'TMP_DIR', 'START_TIME           ', 'RUN_TIME', 'COMMAND' ), "\n";
88     print join( "\t", $_->{ 'USER' }, $_->{ 'PID' }, $_->{ '%CPU' }, $_->{ '%MEM' }, $_->{ 'TMP_DIR' }, $_->{ 'START_TIME' }, $_->{ 'RUN_TIME' }, $_->{ 'COMMAND' } ), "\n" foreach @table;
89
90     sleep $options->{ 'time' };
91 }
92
93
94 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SUBROUTINES <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
95
96
97 sub process_info_mac
98 {
99     # Martin A. Hansen, June 2009.
100
101     # Get process info for a given process given a PID by
102     # calling ps aux on a Mac.
103
104     # ps aux:
105     # USER       PID %CPU %MEM      VSZ    RSS   TT  STAT STARTED      TIME COMMAND
106
107     my ( $pid,   # process id
108        ) = @_;
109
110     # Returns a hashref
111
112     my ( @processes, $process, @fields, %info_hash, $hour, $min, $ampm );
113
114     @processes = `ps aux`;
115
116     shift @processes;
117
118     foreach $process ( @processes )
119     {
120         @fields = split " ", $process;
121
122         if ( $fields[ 1 ] == $pid )
123         {
124             %info_hash = (
125                 "USER"    => $fields[ 0 ],
126                 "PID"     => $fields[ 1 ],
127                 "%CPU"    => $fields[ 2 ],
128                 "%MEM"    => $fields[ 3 ],
129                 "VSZ"     => $fields[ 4 ],
130                 "RSS"     => $fields[ 5 ],
131                 "TT"      => $fields[ 6 ],
132                 "STAT"    => $fields[ 7 ],
133                 "STARTED" => $fields[ 8 ],
134                 "TIME"    => $fields[ 9 ],
135                 "COMMAND" => $fields[ 10 ],
136             );
137
138             if ( $info_hash{ 'STARTED' } =~ /^(\d+):(\d+)(AM|PM)$/ )
139             {
140                 $hour = $1;
141                 $min  = $2;
142                 $ampm = $3;
143
144                 if ( $ampm eq "PM" ) {
145                     $hour += 12;
146                 }
147
148                 $info_hash{ 'STARTED' } = "$hour:$min";
149             }
150
151             return wantarray ? %info_hash : \%info_hash;
152         }
153     }
154
155     return;
156 }
157
158
159 sub get_status_info
160 {
161     my ( $file,   #
162        ) = @_;
163
164     my ( $fh, $line, $time, $args, $tmp_dir, $du );
165
166     if (-e $file)
167     {
168         $fh = Maasha::Filesys::file_read_open( $file );
169         flock($fh, 1);
170         $line = <$fh>;
171         close $fh;
172     }
173
174     chomp $line;
175
176     ( $time, $args, $tmp_dir ) = split ";", $line;
177
178     if ( $tmp_dir and -d $tmp_dir )
179     {
180         $du = `du -sh $tmp_dir`;
181
182         chomp $du;
183
184         $du =~ s/\s.*//;
185     }
186
187     return wantarray ? ( $time, $args, $du ) : [ $time, $args, $du ];
188 }
189
190
191 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
192
193
194 BEGIN
195 {
196     Maasha::Biopieces::status_set();
197 }
198
199
200 END
201 {
202     Maasha::Biopieces::status_log();
203 }
204
205
206 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
207
208
209 __END__