Skip to content

Commit

Permalink
reHC*, implemented an internal time limit.
Browse files Browse the repository at this point in the history
  • Loading branch information
yp committed Sep 27, 2011
1 parent 6dd1e96 commit 6cf1306
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 0 deletions.
39 changes: 39 additions & 0 deletions include/timelimit.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
*
* reHC-*
* Haplotyping with Recombinations, Errors, and Missing Genotypes
*
* Copyright (C) 2010,2011 Yuri Pirola <yuri.pirola(-at-)gmail.com>
*
* Distributed under the terms of the GNU General Public License (GPL)
*
*
* This file is part of reHC-* (reHCstar),
* previously known as ZRHC-* (ZRHCstar).
*
* reHC-* is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* reHC-* is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with reHC-*. If not, see <http://www.gnu.org/licenses/>.
*
**/
#ifndef __TIMELIMIT_HPP__
#define __TIMELIMIT_HPP__

// Minimum number of CPU seconds that the process can use
#define MIN_TIME_LIMIT (0)


void register_time_limit(unsigned long int secs);



#endif //__TIMELIMIT_HPP__
15 changes: 15 additions & 0 deletions src/application/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "log.hpp"
#include "assertion.hpp"
#include "utility.hpp"
#include "timelimit.hpp"

#include <iostream>
#include <fstream>
Expand Down Expand Up @@ -200,9 +201,18 @@ class rehcstar_application_t: public application_t {
"Windows overlap each other by half their length.")
;

po::options_description exec_opt("Execution Management Options",
po::options_description::m_default_line_length,
po::options_description::m_default_line_length-16);
exec_opt.add_options()
("time-limit", po::value< unsigned int >()->default_value(0),
"Maximum (approximated) execution time in seconds (0=no limit).")
;

modes.add(files);
modes.add(errors);
modes.add(recombs);
modes.add(exec_opt);
return modes;
};

Expand Down Expand Up @@ -300,6 +310,11 @@ class rehcstar_application_t: public application_t {
rehcstar_t rehcstar;
rehcstar.prepare_program_options(vm);

// Check if a time-limit is specified
if (vm["time-limit"].as<unsigned int>()>0) {
register_time_limit(vm["time-limit"].as<unsigned int>());
}

// Dispatch the work depending on the program parameters
#ifndef ONLY_INTERNAL_SAT_SOLVER
if (vm["create"].as<bool>()) {
Expand Down
63 changes: 63 additions & 0 deletions src/lib/timelimit.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/**
*
* reHC-*
* Haplotyping with Recombinations, Errors, and Missing Genotypes
*
* Copyright (C) 2010,2011 Yuri Pirola <yuri.pirola(-at-)gmail.com>
*
* Distributed under the terms of the GNU General Public License (GPL)
*
*
* This file is part of reHC-* (reHCstar),
* previously known as ZRHC-* (ZRHCstar).
*
* reHC-* is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* reHC-* is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with reHC-*. If not, see <http://www.gnu.org/licenses/>.
*
**/

#include "timelimit.hpp"

#include "log.hpp"

#include <csignal>
#include <cstdlib>
#include <sys/resource.h>

void exceeded_time_limit(int sig, siginfo_t * info, void * context) {
ROOT_FATAL("Time limit reached. Exiting now...");
exit(EXIT_FAILURE);
};

void register_time_limit(unsigned long int secs) {
if (secs>0){
if (secs<=MIN_TIME_LIMIT) secs= MIN_TIME_LIMIT;
ROOT_INFO("Setting time-limit to " << secs << " seconds.");
// Setting time-limit
struct sigaction act;
act.sa_sigaction= exceeded_time_limit;
sigemptyset(&act.sa_mask);
act.sa_flags= SA_SIGINFO;
sigaction(SIGXCPU, &act, NULL);
struct rlimit cpu_limit;
getrlimit(RLIMIT_CPU, &cpu_limit);
cpu_limit.rlim_cur= secs;
cpu_limit.rlim_max= secs+10;
const int lim_res= setrlimit(RLIMIT_CPU, &cpu_limit);
if (lim_res!=0) {
ROOT_ERROR("Failed to set the \"hard\" time-limit! "
"Error: " << lim_res << ". Continuing anyway...");
}

}
};

0 comments on commit 6cf1306

Please sign in to comment.