1 #define __STDC_FORMAT_MACROS
2 #define __STDC_LIMIT_MACROS
3 /***************************************************************************************[System.cc]
4 Copyright (c) 2003-2006, Niklas Een, Niklas Sorensson
5 Copyright (c) 2007-2010, Niklas Sorensson
7 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
8 associated documentation files (the "Software"), to deal in the Software without restriction,
9 including without limitation the rights to use, copy, modify, merge, publish, distribute,
10 sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
13 The above copyright notice and this permission notice shall be included in all copies or
14 substantial portions of the Software.
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
17 NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
19 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
20 OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 **************************************************************************************************/
26 #include "libs/minisat/System.h"
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;
61 // Find the correct line, beginning with "VmPeak:":
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); }
72 double Minisat::memUsedPeak(bool strictlyPeak
) {
73 double peak
= memReadPeak() / (double)1024;
74 return peak
== 0 && !strictlyPeak
? memUsed() : peak
; }
76 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__gnu_hurd__)
78 double Minisat::memUsed() {
80 getrusage(RUSAGE_SELF
, &ru
);
81 return (double)ru
.ru_maxrss
/ 1024; }
82 double Minisat::memUsedPeak(bool strictlyPeak
) { (void) strictlyPeak
; return memUsed(); }
85 #elif defined(__APPLE__)
86 #include <malloc/malloc.h>
88 double Minisat::memUsed() {
89 malloc_statistics_t t
;
90 malloc_zone_statistics(NULL
, &t
);
91 return (double)t
.max_size_in_use
/ (1024*1024); }
92 double Minisat::memUsedPeak(bool strictlyPeak
) { (void) strictlyPeak
; return memUsed(); }
95 double Minisat::memUsed() { return 0; }
96 double Minisat::memUsedPeak(bool strictlyPeak
) { (void) strictlyPeak
; return 0; }
100 void Minisat::setX86FPUPrecision()
102 #if defined(__linux__) && defined(_FPU_EXTENDED) && defined(_FPU_DOUBLE) && defined(_FPU_GETCW)
103 // Only correct FPU precision on Linux architectures that needs and supports it:
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__)
112 void Minisat::limitMemory(uint64_t max_mem_mb
)
114 // FIXME: OpenBSD does not support RLIMIT_AS. Not sure how well RLIMIT_DATA works instead.
115 #if defined(__OpenBSD__)
116 #define RLIMIT_AS RLIMIT_DATA
119 // Set limit on virtual memory:
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__)
136 void Minisat::limitMemory(uint64_t /*max_mem_mb*/)
138 printf("WARNING! Memory limit not supported on this architecture.\n");
143 #if !defined(_MSC_VER) && !defined(__MINGW32__)
144 void Minisat::limitTime(uint32_t max_cpu_time
)
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");
157 void Minisat::limitTime(uint32_t /*max_cpu_time*/)
159 printf("WARNING! CPU-time limit not supported on this architecture.\n");
164 void Minisat::sigTerm(void handler(int))
166 signal(SIGINT
, handler
);
167 signal(SIGTERM
,handler
);
169 signal(SIGXCPU
,handler
);