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 **************************************************************************************************/
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) { 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) { return memUsed(); }
95 double Minisat::memUsed() { return 0; }
96 double Minisat::memUsedPeak(bool) { return 0; }
100 #if !defined(_MSC_VER) && !defined(__MINGW32__)
101 void Minisat::limitMemory(uint64_t max_mem_mb
)
103 // FIXME: OpenBSD does not support RLIMIT_AS. Not sure how well RLIMIT_DATA works instead.
104 #if defined(__OpenBSD__)
105 #define RLIMIT_AS RLIMIT_DATA
108 // Set limit on virtual memory:
109 if (max_mem_mb
!= 0){
110 rlim_t new_mem_lim
= (rlim_t
)max_mem_mb
* 1024*1024;
112 getrlimit(RLIMIT_AS
, &rl
);
113 if (rl
.rlim_max
== RLIM_INFINITY
|| new_mem_lim
< rl
.rlim_max
){
114 rl
.rlim_cur
= new_mem_lim
;
115 if (setrlimit(RLIMIT_AS
, &rl
) == -1)
116 printf("WARNING! Could not set resource limit: Virtual memory.\n");
120 #if defined(__OpenBSD__)
125 void Minisat::limitMemory(uint64_t /*max_mem_mb*/)
127 printf("WARNING! Memory limit not supported on this architecture.\n");
132 #if !defined(_MSC_VER) && !defined(__MINGW32__)
133 void Minisat::limitTime(uint32_t max_cpu_time
)
135 if (max_cpu_time
!= 0){
137 getrlimit(RLIMIT_CPU
, &rl
);
138 if (rl
.rlim_max
== RLIM_INFINITY
|| (rlim_t
)max_cpu_time
< rl
.rlim_max
){
139 rl
.rlim_cur
= max_cpu_time
;
140 if (setrlimit(RLIMIT_CPU
, &rl
) == -1)
141 printf("WARNING! Could not set resource limit: CPU-time.\n");
146 void Minisat::limitTime(uint32_t /*max_cpu_time*/)
148 printf("WARNING! CPU-time limit not supported on this architecture.\n");
153 void Minisat::sigTerm(void handler(int))
155 signal(SIGINT
, handler
);
156 signal(SIGTERM
,handler
);
158 signal(SIGXCPU
,handler
);