1 #ifndef __STDC_FORMAT_MACROS
2 #define __STDC_FORMAT_MACROS
4 #ifndef __STDC_LIMIT_MACROS
5 #define __STDC_LIMIT_MACROS
7 /***************************************************************************************[System.cc]
8 Copyright (c) 2003-2006, Niklas Een, Niklas Sorensson
9 Copyright (c) 2007-2010, Niklas Sorensson
11 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
12 associated documentation files (the "Software"), to deal in the Software without restriction,
13 including without limitation the rights to use, copy, modify, merge, publish, distribute,
14 sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
15 furnished to do so, subject to the following conditions:
17 The above copyright notice and this permission notice shall be included in all copies or
18 substantial portions of the Software.
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
21 NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
23 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
24 OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 **************************************************************************************************/
34 #if defined(__linux__)
38 using namespace Minisat
;
40 static inline int memReadStat(int field
)
46 sprintf(name
, "/proc/%d/statm", pid
);
47 FILE* in
= fopen(name
, "rb");
48 if (in
== NULL
) return 0;
50 for (; field
>= 0; field
--)
51 if (fscanf(in
, "%d", &value
) != 1)
52 printf("ERROR! Failed to parse memory statistics from \"/proc\".\n"), exit(1);
58 static inline int memReadPeak(void)
63 sprintf(name
, "/proc/%d/status", pid
);
64 FILE* in
= fopen(name
, "rb");
65 if (in
== NULL
) return 0;
67 // Find the correct line, beginning with "VmPeak:":
69 while (!feof(in
) && fscanf(in
, "VmPeak: %d kB", &peak_kb
) != 1)
70 while (!feof(in
) && fgetc(in
) != '\n')
77 double Minisat::memUsed() { return (double)memReadStat(0) * (double)getpagesize() / (1024*1024); }
78 double Minisat::memUsedPeak(bool strictlyPeak
) {
79 double peak
= memReadPeak() / (double)1024;
80 return peak
== 0 && !strictlyPeak
? memUsed() : peak
; }
82 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__gnu_hurd__)
84 double Minisat::memUsed() {
86 getrusage(RUSAGE_SELF
, &ru
);
87 return (double)ru
.ru_maxrss
/ 1024; }
88 double Minisat::memUsedPeak(bool) { return memUsed(); }
91 #elif defined(__APPLE__)
92 #include <malloc/malloc.h>
94 double Minisat::memUsed() {
95 malloc_statistics_t t
;
96 malloc_zone_statistics(NULL
, &t
);
97 return (double)t
.max_size_in_use
/ (1024*1024); }
98 double Minisat::memUsedPeak(bool) { return memUsed(); }
101 double Minisat::memUsed() { return 0; }
102 double Minisat::memUsedPeak(bool) { return 0; }
106 #if !defined(_MSC_VER) && !defined(__MINGW32__) && !defined(__wasm)
107 void Minisat::limitMemory(uint64_t max_mem_mb
)
109 // FIXME: OpenBSD does not support RLIMIT_AS. Not sure how well RLIMIT_DATA works instead.
110 #if defined(__OpenBSD__)
111 #define RLIMIT_AS RLIMIT_DATA
114 // Set limit on virtual memory:
115 if (max_mem_mb
!= 0){
116 rlim_t new_mem_lim
= (rlim_t
)max_mem_mb
* 1024*1024;
118 getrlimit(RLIMIT_AS
, &rl
);
119 if (rl
.rlim_max
== RLIM_INFINITY
|| new_mem_lim
< rl
.rlim_max
){
120 rl
.rlim_cur
= new_mem_lim
;
121 if (setrlimit(RLIMIT_AS
, &rl
) == -1)
122 printf("WARNING! Could not set resource limit: Virtual memory.\n");
126 #if defined(__OpenBSD__)
131 void Minisat::limitMemory(uint64_t /*max_mem_mb*/)
133 printf("WARNING! Memory limit not supported on this architecture.\n");
138 #if !defined(_MSC_VER) && !defined(__MINGW32__) && !defined(__wasm)
139 void Minisat::limitTime(uint32_t max_cpu_time
)
141 if (max_cpu_time
!= 0){
143 getrlimit(RLIMIT_CPU
, &rl
);
144 if (rl
.rlim_max
== RLIM_INFINITY
|| (rlim_t
)max_cpu_time
< rl
.rlim_max
){
145 rl
.rlim_cur
= max_cpu_time
;
146 if (setrlimit(RLIMIT_CPU
, &rl
) == -1)
147 printf("WARNING! Could not set resource limit: CPU-time.\n");
152 void Minisat::limitTime(uint32_t /*max_cpu_time*/)
154 printf("WARNING! CPU-time limit not supported on this architecture.\n");
159 void Minisat::sigTerm(void handler(int))
164 signal(SIGINT
, handler
);
165 signal(SIGTERM
,handler
);
167 signal(SIGXCPU
,handler
);