1 #define __STDC_FORMAT_MACROS
2 #define __STDC_LIMIT_MACROS
28 #if defined(__linux__)
32 using namespace Minisat;
34 static inline int memReadStat(
int field)
40 sprintf(name,
"/proc/%d/statm", pid);
41 FILE* in = fopen(name,
"rb");
42 if (in ==
NULL)
return 0;
44 for (; field >= 0; field--)
45 if (fscanf(in,
"%d", &value) != 1)
46 printf(
"ERROR! Failed to parse memory statistics from \"/proc\".\n"), exit(1);
52 static inline int memReadPeak(
void)
57 sprintf(name,
"/proc/%d/status", pid);
58 FILE* in = fopen(name,
"rb");
59 if (in ==
NULL)
return 0;
63 while (!feof(in) && fscanf(in,
"VmPeak: %d kB", &peak_kb) != 1)
64 while (!feof(in) && fgetc(in) !=
'\n')
71 double Minisat::memUsed() {
return (
double)memReadStat(0) * (double)getpagesize() / (1024*1024); }
73 double peak = memReadPeak() / (double)1024;
74 return peak == 0 && !strictlyPeak ?
memUsed() : peak; }
76 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__gnu_hurd__)
80 getrusage(RUSAGE_SELF, &ru);
81 return (
double)ru.ru_maxrss / 1024; }
85 #elif defined(__APPLE__)
86 #include <malloc/malloc.h>
89 malloc_statistics_t t;
90 malloc_zone_statistics(
NULL, &t);
91 return (
double)t.max_size_in_use / (1024*1024); }
102 #if defined(__linux__) && defined(_FPU_EXTENDED) && defined(_FPU_DOUBLE) && defined(_FPU_GETCW)
104 fpu_control_t oldcw, newcw;
105 _FPU_GETCW(oldcw); newcw = (oldcw & ~_FPU_EXTENDED) | _FPU_DOUBLE; _FPU_SETCW(newcw);
106 printf(
"WARNING: for repeatability, setting FPU to use double precision\n");
111 #if !defined(_MSC_VER) && !defined(__MINGW32__)
115 #if defined(__OpenBSD__)
116 #define RLIMIT_AS RLIMIT_DATA
120 if (max_mem_mb != 0){
121 rlim_t new_mem_lim = (rlim_t)max_mem_mb * 1024*1024;
123 getrlimit(RLIMIT_AS, &rl);
124 if (rl.rlim_max == RLIM_INFINITY || new_mem_lim < rl.rlim_max){
125 rl.rlim_cur = new_mem_lim;
126 if (setrlimit(RLIMIT_AS, &rl) == -1)
127 printf(
"WARNING! Could not set resource limit: Virtual memory.\n");
131 #if defined(__OpenBSD__)
138 printf(
"WARNING! Memory limit not supported on this architecture.\n");
143 #if !defined(_MSC_VER) && !defined(__MINGW32__)
146 if (max_cpu_time != 0){
148 getrlimit(RLIMIT_CPU, &rl);
149 if (rl.rlim_max == RLIM_INFINITY || (rlim_t)max_cpu_time < rl.rlim_max){
150 rl.rlim_cur = max_cpu_time;
151 if (setrlimit(RLIMIT_CPU, &rl) == -1)
152 printf(
"WARNING! Could not set resource limit: CPU-time.\n");
159 printf(
"WARNING! CPU-time limit not supported on this architecture.\n");
166 signal(SIGINT, handler);
167 signal(SIGTERM,handler);
169 signal(SIGXCPU,handler);
void setX86FPUPrecision()
void limitTime(uint32_t max_cpu_time)
void sigTerm(void handler(int))
void limitMemory(uint64_t max_mem_mb)
double memUsedPeak(bool strictlyPeak=false)