345be8c4c75a54515552f1fd88a30b0f74840e49
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 **************************************************************************************************/
32 #if defined(__linux__)
36 using namespace Minisat
;
38 static inline int memReadStat(int field
)
44 sprintf(name
, "/proc/%d/statm", pid
);
45 FILE* in
= fopen(name
, "rb");
46 if (in
== NULL
) return 0;
48 for (; field
>= 0; field
--)
49 if (fscanf(in
, "%d", &value
) != 1)
50 printf("ERROR! Failed to parse memory statistics from \"/proc\".\n"), exit(1);
56 static inline int memReadPeak(void)
61 sprintf(name
, "/proc/%d/status", pid
);
62 FILE* in
= fopen(name
, "rb");
63 if (in
== NULL
) return 0;
65 // Find the correct line, beginning with "VmPeak:":
67 while (!feof(in
) && fscanf(in
, "VmPeak: %d kB", &peak_kb
) != 1)
68 while (!feof(in
) && fgetc(in
) != '\n')
75 double Minisat::memUsed() { return (double)memReadStat(0) * (double)getpagesize() / (1024*1024); }
76 double Minisat::memUsedPeak(bool strictlyPeak
) {
77 double peak
= memReadPeak() / (double)1024;
78 return peak
== 0 && !strictlyPeak
? memUsed() : peak
; }
80 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__gnu_hurd__)
82 double Minisat::memUsed() {
84 getrusage(RUSAGE_SELF
, &ru
);
85 return (double)ru
.ru_maxrss
/ 1024; }
86 double Minisat::memUsedPeak(bool) { return memUsed(); }
89 #elif defined(__APPLE__)
90 #include <malloc/malloc.h>
92 double Minisat::memUsed() {
93 malloc_statistics_t t
;
94 malloc_zone_statistics(NULL
, &t
);
95 return (double)t
.max_size_in_use
/ (1024*1024); }
96 double Minisat::memUsedPeak(bool) { return memUsed(); }
99 double Minisat::memUsed() { return 0; }
100 double Minisat::memUsedPeak(bool) { return 0; }
104 #if !defined(_MSC_VER) && !defined(__MINGW32__) && !defined(__wasm)
105 void Minisat::limitMemory(uint64_t max_mem_mb
)
107 // FIXME: OpenBSD does not support RLIMIT_AS. Not sure how well RLIMIT_DATA works instead.
108 #if defined(__OpenBSD__)
109 #define RLIMIT_AS RLIMIT_DATA
112 // Set limit on virtual memory:
113 if (max_mem_mb
!= 0){
114 rlim_t new_mem_lim
= (rlim_t
)max_mem_mb
* 1024*1024;
116 getrlimit(RLIMIT_AS
, &rl
);
117 if (rl
.rlim_max
== RLIM_INFINITY
|| new_mem_lim
< rl
.rlim_max
){
118 rl
.rlim_cur
= new_mem_lim
;
119 if (setrlimit(RLIMIT_AS
, &rl
) == -1)
120 printf("WARNING! Could not set resource limit: Virtual memory.\n");
124 #if defined(__OpenBSD__)
129 void Minisat::limitMemory(uint64_t /*max_mem_mb*/)
131 printf("WARNING! Memory limit not supported on this architecture.\n");
136 #if !defined(_MSC_VER) && !defined(__MINGW32__) && !defined(__wasm)
137 void Minisat::limitTime(uint32_t max_cpu_time
)
139 if (max_cpu_time
!= 0){
141 getrlimit(RLIMIT_CPU
, &rl
);
142 if (rl
.rlim_max
== RLIM_INFINITY
|| (rlim_t
)max_cpu_time
< rl
.rlim_max
){
143 rl
.rlim_cur
= max_cpu_time
;
144 if (setrlimit(RLIMIT_CPU
, &rl
) == -1)
145 printf("WARNING! Could not set resource limit: CPU-time.\n");
150 void Minisat::limitTime(uint32_t /*max_cpu_time*/)
152 printf("WARNING! CPU-time limit not supported on this architecture.\n");
157 void Minisat::sigTerm(void handler(int))
162 signal(SIGINT
, handler
);
163 signal(SIGTERM
,handler
);
165 signal(SIGXCPU
,handler
);