lsf_report.pl 10.8 KB
Newer Older
1 2 3 4 5 6
#!/usr/bin/env perl

# Obtain bacct data for your pipeline from the LSF and store it in lsf_report table

use strict;
use warnings;
7

8 9 10 11
    # Finding out own path in order to reference own components (including own modules):
use Cwd            ();
use File::Basename ();
BEGIN {
12
    $ENV{'EHIVE_ROOT_DIR'} ||= File::Basename::dirname( File::Basename::dirname( Cwd::realpath($0) ) );
13
    unshift @INC, $ENV{'EHIVE_ROOT_DIR'}.'/modules';
14 15
}

16

17
use Getopt::Long;
18
use Bio::EnsEMBL::Hive::DBSQL::DBAdaptor;
19
use Bio::EnsEMBL::Hive::Utils ('script_usage');
20
use Bio::EnsEMBL::Hive::Meadow::LSF;
21

22 23
main();
exit(0);
24 25


26
sub main {
27

28
    my ($url, $reg_conf, $reg_type, $reg_alias, $nosqlvc, $bacct_source_line, $lsf_user, $help, $start_date, $end_date);
29

30
    GetOptions(
31 32 33 34 35 36 37 38 39 40 41 42
                # connect to the database:
            'url=s'                      => \$url,
            'reg_conf|regfile=s'         => \$reg_conf,
            'reg_type=s'                 => \$reg_type,
            'reg_alias|regname=s'        => \$reg_alias,
            'nosqlvc=i'                  => \$nosqlvc,      # using "=i" instead of "!" for consistency with scripts where it is a propagated option

            'dump|file=s'                => \$bacct_source_line,
            'lu|lsf_user=s'              => \$lsf_user,
            'sd|start_date=s'            => \$start_date,
            'ed|end_date=s'              => \$end_date,
            'h|help'                     => \$help,
43
    );
44

45
    if ($help) { script_usage(0); }
46

47 48 49 50 51 52 53 54 55 56 57
    my $hive_dba;
    if($url or $reg_alias) {
        $hive_dba = Bio::EnsEMBL::Hive::DBSQL::DBAdaptor->new(
                -url                            => $url,
                -reg_conf                       => $reg_conf,
                -reg_type                       => $reg_type,
                -reg_alias                      => $reg_alias,
                -no_sql_schema_version_check    => $nosqlvc,
        );
    } else {
        warn "\nERROR: Connection parameters (url or reg_conf+reg_alias) need to be specified\n";
58 59
        script_usage(1);
    }
60

61
    my $dbc = $hive_dba->dbc();
62

63 64 65
    warn "Creating the 'lsf_report' table if it doesn't exist...\n";
    $dbc->do (qq{
        CREATE TABLE IF NOT EXISTS lsf_report (
66 67
            meadow_name      varchar(255) NOT NULL,
            process_id       varchar(255) NOT NULL,
68
            status           varchar(20) NOT NULL,
69 70
            mem_megs         float NOT NULL,
            swap_megs        float NOT NULL,
71 72 73
            pending_sec      integer NOT NULL,
            cpu_sec          float NOT NULL,
            lifespan_sec     integer NOT NULL,
74
            exception_status varchar(40) NOT NULL,
75

76 77
            PRIMARY KEY (meadow_name,process_id),
            KEY process_id_idx (process_id)
78

79 80
        ) ENGINE=InnoDB;
    });
81

82 83 84
    warn "Creating the 'lsf_usage' view if it doesn't exist...\n";
    $dbc->do (qq{
        CREATE OR REPLACE VIEW lsf_usage AS
Leo Gordon's avatar
Leo Gordon committed
85
            SELECT CONCAT(logic_name,'(',analysis_id,')') analysis,
86
                   CONCAT(rc.name,'(',rc.resource_class_id,')') resource_class,
Leo Gordon's avatar
Leo Gordon committed
87
                   count(*) workers,
88 89
                   min(mem_megs), avg(mem_megs), max(mem_megs),
                   min(swap_megs), avg(swap_megs), max(swap_megs)
90
            FROM analysis_base
Leo Gordon's avatar
Leo Gordon committed
91
            JOIN resource_class rc USING(resource_class_id)
92
            LEFT JOIN worker w USING(analysis_id)
93
            LEFT JOIN lsf_report USING (meadow_name, process_id)
94
            WHERE w.meadow_type='LSF'
95 96 97 98
            GROUP BY analysis_id
            ORDER BY analysis_id;
    });

99 100 101
    my $this_lsf_farm = Bio::EnsEMBL::Hive::Meadow::LSF::name();
    die "Cannot find the name of the current farm.\n" unless $this_lsf_farm;

102
    if( $bacct_source_line && -r $bacct_source_line ) {
103

104
        warn "Parsing given bacct file '$bacct_source_line'...\n";
105

106
    } else {
107

108
        warn "No bacct information given, finding out the time interval when the pipeline was run on '$this_lsf_farm' ...\n";
109

110 111 112 113
        my $offset_died_expression = ($dbc->driver eq 'sqlite')
                        ? "datetime(max(died), '+1 minute')"
                        : "FROM_UNIXTIME(UNIX_TIMESTAMP(max(died))+60)";

114
        my $sth_times = $dbc->prepare( "SELECT min(born), $offset_died_expression FROM worker WHERE meadow_type='LSF' AND meadow_name='$this_lsf_farm' AND status='DEAD'" );
115 116 117 118
        $sth_times->execute();
        my ($from_time, $to_time) = $sth_times->fetchrow_array();
        $sth_times->finish();

119 120 121 122
        unless(defined($from_time) and defined($to_time)) {
            die "There seems to be no information on workers, exiting...\n";
        }

123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
        if (defined $start_date) {
            die "start_date must be in a format like '2012/01/25/13:46'" unless $start_date =~ /^\d{4}\/\d{2}\/\d{2}\/\d{2}:\d{2}$/;
            $from_time = $start_date;
        } else {
            $from_time=~s/[- ]/\//g;
            $from_time=~s/:\d\d$//;
        }

        if (defined $end_date) {
            die "end_date must be in a format like '2012/01/25/13:46'" unless $end_date =~ /^\d{4}\/\d{2}\/\d{2}\/\d{2}:\d{2}$/;
            $to_time = $end_date;
        } else {
            $to_time=~s/[- ]/\//g;
            $to_time=~s/:\d\d$//;
        }
138 139 140 141 142 143 144 145 146

        warn "\tfrom=$from_time, to=$to_time\n";

        $lsf_user = $lsf_user           ? "-u $lsf_user"                : '';
        my $tee   = $bacct_source_line  ? "| tee $bacct_source_line"    : '';
        $bacct_source_line = "bacct -l -C $from_time,$to_time $lsf_user $tee |";

        warn 'Will run the following command to obtain '.($tee ? 'and dump ' : '')."bacct information: '$bacct_source_line' (may take a few minutes)\n";
    }
147

148
    my $sth_replace = $dbc->prepare( 'REPLACE INTO lsf_report (meadow_name, process_id, status, mem_megs, swap_megs, pending_sec, cpu_sec, lifespan_sec, exception_status) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)' );
149 150
    {
        local $/ = "------------------------------------------------------------------------------\n\n";
151 152
        my %units_converter = ( 'K' => 1.0/1024, 'M' => 1, 'G' => 1024, 'T' => 1024*1024 );

153 154
        open(my $bacct_fh, $bacct_source_line);
        my $record = <$bacct_fh>; # skip the header
155

156 157
        for my $record (<$bacct_fh>) {
            chomp $record;
158

159
            # warn "RECORD:\n$record";
160

161 162
            my @lines = split(/\n/, $record);
            if( my ($process_id) = $lines[0]=~/^Job <(\d+(?:\[\d+\])?)>/) {
163

164 165 166 167 168 169
                my $exception_status = '';
                foreach (@lines) {
                    if(/^\s*EXCEPTION STATUS:\s*(.*?)\s*$/) {
                        $exception_status = $1;
                        $exception_status =~s/\s+/;/g;
                    }
170 171
                }

172 173
                my (@keys)   = split(/\s+/, ' '.$lines[@lines-2]);
                my (@values) = split(/\s+/, ' '.$lines[@lines-1]);
174
                my %usage = map { ($keys[$_] => $values[$_]) } (0..@keys-1);
175

176 177
                my ($mem_in_units, $mem_unit)   = $usage{'MEM'}  =~ /^([\d\.]+)([KMGT])$/;
                my ($swap_in_units, $swap_unit) = $usage{'SWAP'} =~ /^([\d\.]+)([KMGT])$/;
178

179 180
                my $mem_megs    = $mem_in_units  * $units_converter{$mem_unit};
                my $swap_megs   = $swap_in_units * $units_converter{$swap_unit};
181

182 183
                #warn join(', ', map {sprintf('%s=%s', $_, $usage{$_})} (sort keys %usage)), "\n";
                $sth_replace->execute( $this_lsf_farm, $process_id, $usage{STATUS}, $mem_megs, $swap_megs, $usage{WAIT}, $usage{CPU_T}, $usage{TURNAROUND}, $exception_status );
184
            }
185
        }
186 187

        close $bacct_fh;
188
    }
189 190
    $sth_replace->finish();
    warn "\nReport has been loaded into pipeline's lsf_report table. Enjoy.\n";
191 192

}
193 194 195 196 197 198 199 200 201 202 203 204 205 206

__DATA__

=pod

=head1 NAME

    lsf_report.pl

=head1 DESCRIPTION

    This script is used for offline examination of resources used by a Hive pipeline running on LSF
    (the script is [Pp]latform-dependent).

207 208 209
    Based on the command-line parameters 'start_date' and 'end_date', or on the start time of the first
    worker and end time of the last worker (as recorded in pipeline DB), it pulls the relevant data out
    of LSF's 'bacct' database, parses it and stores in 'lsf_report' table.
210
    You can join this table to 'worker' table USING(meadow_name,process_id) in the usual MySQL way
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
    to filter by analysis_id, do various stats, etc.

    You can optionally ask the script to dump the 'bacct' database in a dump file,
    or fill in the 'lsf_report' table from an existing dump file (most time is taken by querying bacct).

    Please note the script may additionally pull information about LSF processes that you ran simultaneously
    with running the pipeline. It is easy to ignore them by joining into 'worker' table.

=head1 USAGE EXAMPLES

        # Just run it the usual way: query 'bacct' and load the relevant data into 'lsf_report' table:
    lsf_report.pl -url mysql://username:secret@hostname:port/long_mult_test

        # The same, but assuming LSF user someone_else ran the pipeline:
    lsf_report.pl -url mysql://username:secret@hostname:port/long_mult_test -lsf_user someone_else

        # Assuming the dump file existed. Load the dumped bacct data into 'lsf_report' table:
    lsf_report.pl -url mysql://username:secret@hostname:port/long_mult_test -dump long_mult.bacct

        # Assuming the dump file did not exist. Query 'bacct', dump the data into a file and load it into 'lsf_report':
    lsf_report.pl -url mysql://username:secret@hostname:port/long_mult_test -dump long_mult_again.bacct

=head1 OPTIONS

    -help                   : print this help
    -url <url string>       : url defining where hive database is located
    -dump <filename>        : a filename for bacct dump. It will be read from if the file exists, and written to otherwise.
    -lsf_user <username>    : if it wasn't you who ran the pipeline, LSF user name of that user can be provided
239 240
    -start_date <date>      : minimal start date of a job (the format is '2012/01/25/13:46')
    -end_date <date>        : maximal end date of a job (the format is '2012/01/25/13:46')
241

242 243
=head1 LICENSE

244
    Copyright [1999-2015] Wellcome Trust Sanger Institute and the EMBL-European Bioinformatics Institute
Matthieu Muffato's avatar
Matthieu Muffato committed
245
    Copyright [2016-2018] EMBL-European Bioinformatics Institute
246 247 248 249 250 251 252 253 254 255

    Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

         http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software distributed under the License
    is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and limitations under the License.

256 257
=head1 CONTACT

258
    Please subscribe to the Hive mailing list:  http://listserver.ebi.ac.uk/mailman/listinfo/ehive-users  to discuss Hive-related questions or to be notified of our updates
259 260

=cut
261