]> git.donarmstrong.com Git - biopieces.git/blob - bp_bin/biostat
clearning up kmer_stats and kmer_freq
[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         else
80         {
81             print STDERR "Removing stale file: $file\n";
82             unlink $file;
83         }
84     }
85
86     if ( $options->{ 'sort' } =~ /^(USER|TMP_DIR|START_TIME|RUN_TIME|COMMAND)$/ ) {
87         @table = sort { $a->{ $options->{ 'sort' } } cmp $b->{ $options->{ 'sort' } } } @table;
88     } else {
89         @table = sort { $a->{ $options->{ 'sort' } } <=> $b->{ $options->{ 'sort' } } } @table;
90     }
91     
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;
94
95     sleep $options->{ 'time' };
96 }
97
98
99 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SUBROUTINES <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
100
101
102 sub process_info_mac
103 {
104     # Martin A. Hansen, June 2009.
105
106     # Get process info for a given process given a PID by
107     # calling ps aux on a Mac.
108
109     # ps aux:
110     # USER       PID %CPU %MEM      VSZ    RSS   TT  STAT STARTED      TIME COMMAND
111
112     my ( $pid,   # process id
113        ) = @_;
114
115     # Returns a hashref
116
117     my ( @processes, $process, @fields, %info_hash, $hour, $min, $ampm );
118
119     @processes = `ps aux`;
120
121     shift @processes;
122
123     foreach $process ( @processes )
124     {
125         @fields = split " ", $process;
126
127         if ( $fields[ 1 ] == $pid )
128         {
129             %info_hash = (
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 ],
141             );
142
143             if ( $info_hash{ 'STARTED' } =~ /^(\d+):(\d+)(AM|PM)$/ )
144             {
145                 $hour = $1;
146                 $min  = $2;
147                 $ampm = $3;
148
149                 if ( $ampm eq "PM" ) {
150                     $hour += 12;
151                 }
152
153                 $info_hash{ 'STARTED' } = "$hour:$min";
154             }
155
156             return wantarray ? %info_hash : \%info_hash;
157         }
158     }
159
160     return;
161 }
162
163
164 sub get_status_info
165 {
166     my ( $file,   #
167        ) = @_;
168
169     my ( $fh, $line, $time, $args, $tmp_dir, $du );
170
171     $fh = Maasha::Filesys::file_read_open( $file );
172     $line = <$fh>;
173     close $fh;
174
175     chomp $line;
176
177     ( $time, $args, $tmp_dir ) = split ";", $line;
178
179     if ( $tmp_dir and -d $tmp_dir )
180     {
181         $du = `du -sh $tmp_dir`;
182
183         chomp $du;
184
185         $du =~ s/\s.*//;
186     }
187
188     return wantarray ? ( $time, $args, $du ) : [ $time, $args, $du ];
189 }
190
191
192 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
193
194
195 BEGIN
196 {
197     Maasha::Biopieces::status_set();
198 }
199
200
201 END
202 {
203     Maasha::Biopieces::status_log();
204 }
205
206
207 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
208
209
210 __END__