SMT-RAT  24.02
Toolbox for Strategic and Parallel Satisfiability-Modulo-Theories Solving
ResourceLimitation.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "ExitCodes.h"
4 
5 #include "ResourceLimitation.h"
6 
7 #include <carl-common/meta/platform.h>
8 #include <carl-common/memory/Singleton.h>
9 
10 #include <csignal>
11 #include <sys/time.h>
12 #include <sys/times.h>
13 #include <sys/resource.h>
14 
15 namespace smtrat {
16 namespace resource {
17 
18 #ifdef __LINUX
19 inline void setCPULimit(std::size_t seconds) {
20  rlimit rl;
21  getrlimit(RLIMIT_CPU, &rl);
22  rl.rlim_cur = seconds;
23  setrlimit(RLIMIT_CPU, &rl);
24 }
25 inline std::size_t getCPULimit() {
26  rlimit rl;
27  getrlimit(RLIMIT_CPU, &rl);
28  return rl.rlim_cur;
29 }
30 inline std::size_t usedCPU() {
31  return std::size_t(clock()) / CLOCKS_PER_SEC;
32 }
33 inline void setMemoryLimit(std::size_t megabytes) {
34  rlimit rl;
35  getrlimit(RLIMIT_AS, &rl);
36  rl.rlim_cur = megabytes * 1024 * 1024;
37  setrlimit(RLIMIT_AS, &rl);
38 }
39 
40 inline std::size_t getMemoryLimit() {
41  rlimit rl;
42  getrlimit(RLIMIT_AS, &rl);
43  return rl.rlim_cur;
44 }
45 void signalHandler(int signal);
46 inline void installSignalHandler() {
47  std::signal(SIGXCPU, signalHandler);
48  std::signal(ENOMEM, signalHandler);
49 }
50 #else
51 inline void setCPULimit(std::size_t) {}
52 inline std::size_t getCPULimit() { return 0; }
53 inline std::size_t usedCPU() { return 0; }
54 inline void setMemoryLimit(std::size_t) {}
55 inline std::size_t getMemoryLimit() { return 0; }
56 inline void installSignalHandler() {}
57 #endif
58 
59 class Limiter: public carl::Singleton<Limiter> {
60 private:
61  std::size_t mMemout;
62  std::size_t mTimeout;
63  std::size_t mOriginalMemout;
64  std::size_t mOriginalTimeout;
65  std::function<void()> mTimeoutHandler;
66 public:
67  void initialize() {
69  mMemout = 0;
70  mTimeout = 0;
73  }
74  void reset() {
75  if (mMemout != 0) {
76  mMemout = 0;
78  }
79  if (mTimeout != 0) {
80  mTimeout = 0;
82  }
83  }
84  void setMemout(std::size_t megabytes) {
85  mMemout = megabytes;
87  }
88  void setTimeout(std::size_t seconds) {
89  mTimeout = seconds;
91  }
92  void resetTimeout() const {
93  if (mTimeout != 0) {
95  }
96  }
97  void setTimeoutHandler(std::function<void()> f) {
98  mTimeoutHandler = f;
99  }
100  std::function<void()> timeoutHandler() const {
101  return mTimeoutHandler;
102  }
103 };
104 
105 inline void signalHandler(int signal) {
106  if (signal == SIGXCPU) {
107  std::cerr << "(error \"CPU resource out\")" << std::endl;
108  smtrat::resource::Limiter::getInstance().timeoutHandler()();
109  std::quick_exit(SMTRAT_EXIT_TIMEOUT);
110  } else if (signal == ENOMEM) {
111  std::cerr << "(error \"Memory resource out\")" << std::endl;
112  std::quick_exit(SMTRAT_EXIT_MEMOUT);
113  } else {
114  std::cerr << "(error \"Unknown abort in resource limitation module\")" << std::endl;
115  std::quick_exit(SMTRAT_EXIT_GENERALERROR);
116  }
117 }
118 
119 }
120 }
constexpr int SMTRAT_EXIT_TIMEOUT
Definition: ExitCodes.h:22
constexpr int SMTRAT_EXIT_GENERALERROR
Definition: ExitCodes.h:17
constexpr int SMTRAT_EXIT_MEMOUT
Definition: ExitCodes.h:23
void setMemout(std::size_t megabytes)
std::function< void()> timeoutHandler() const
std::function< void()> mTimeoutHandler
void setTimeout(std::size_t seconds)
void setTimeoutHandler(std::function< void()> f)
std::size_t usedCPU()
void setMemoryLimit(std::size_t)
std::size_t getMemoryLimit()
void setCPULimit(std::size_t)
std::size_t getCPULimit()
void signalHandler(int signal)
Class to create the formulas for axioms.