lsf_report.pl 8.42 KB
Newer Older
1 2
#!/usr/bin/env perl

3
# Obtain bacct data for your pipeline from the LSF and store it in 'worker_resource_usage' table
4 5 6

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
    my $this_lsf_farm = Bio::EnsEMBL::Hive::Meadow::LSF::name();
    die "Cannot find the name of the current farm.\n" unless $this_lsf_farm;

66
    if( $bacct_source_line && -r $bacct_source_line ) {
67

68
        warn "Parsing given bacct file '$bacct_source_line'...\n";
69

70
    } else {
71

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

74
        my $offset_died_expression = ($dbc->driver eq 'sqlite')
75 76
                        ? "datetime(max(died), '+2 minutes')"
                        : "FROM_UNIXTIME(UNIX_TIMESTAMP(max(died))+120)";
77

78
        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'" );
79 80 81 82
        $sth_times->execute();
        my ($from_time, $to_time) = $sth_times->fetchrow_array();
        $sth_times->finish();

83 84 85 86
        unless(defined($from_time) and defined($to_time)) {
            die "There seems to be no information on workers, exiting...\n";
        }

87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
        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$//;
        }
102 103 104 105 106 107 108 109 110

        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";
    }
111

112
    my $report_entries = Bio::EnsEMBL::Hive::Meadow::LSF::parse_report_source_line( $bacct_source_line );
113

114
    my $processid_2_workerid = $hive_dba->get_WorkerAdaptor()->fetch_by_meadow_type_AND_meadow_name_HASHED_FROM_process_id_TO_worker_id( 'LSF', $this_lsf_farm );
115

116
    my $sth_replace = $dbc->prepare( 'REPLACE INTO worker_resource_usage (worker_id, exit_status, mem_megs, swap_megs, pending_sec, cpu_sec, lifespan_sec, exception_status) VALUES (?, ?, ?, ?, ?, ?, ?, ?)' );
117

118
    while( my ($process_id, $report_entry) = each %$report_entries ) {
119

120 121 122 123
        if( my $worker_id = $processid_2_workerid->{$process_id} ) {
            $sth_replace->execute( $worker_id, @$report_entry{'exit_status', 'mem_megs', 'swap_megs', 'pending_sec', 'cpu_sec', 'lifespan_sec', 'exception_status'} );  # slicing hashref
        } else {
            warn "\tDiscarding process_id=$process_id as probably not ours because it could not be mapped to a Worker\n";
124 125
        }
    }
126
    $sth_replace->finish();
127
    warn "\nReport has been loaded into pipeline's 'worker_resource_usage' table. Enjoy.\n";
128
}
129 130 131 132 133 134 135 136 137 138 139 140 141 142

__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).

143 144
    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
145
    of LSF's 'bacct' database, parses it and stores in 'worker_resource_usage' table.
146
    You can join this table to 'worker' table USING(meadow_name,process_id) in the usual MySQL way
147 148 149
    to filter by analysis_id, do various stats, etc.

    You can optionally ask the script to dump the 'bacct' database in a dump file,
150
    or fill in the 'worker_resource_usage' table from an existing dump file (most time is taken by querying bacct).
151 152 153 154 155 156

    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

157
        # Just run it the usual way: query 'bacct' and load the relevant data into 'worker_resource_usage' table:
158 159 160 161 162
    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

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

166
        # Assuming the dump file did not exist. Query 'bacct', dump the data into a file and load it into 'worker_resource_usage' table:
167 168 169 170 171 172 173 174
    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
175 176
    -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')
177

178 179
=head1 LICENSE

180
    Copyright [1999-2014] Wellcome Trust Sanger Institute and the EMBL-European Bioinformatics Institute
181 182 183 184 185 186 187 188 189 190

    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.

191 192
=head1 CONTACT

193
    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
194 195

=cut
196