Bump version
[yosys.git] / libs / minisat / System.cc
1 #ifndef __STDC_FORMAT_MACROS
2 #define __STDC_FORMAT_MACROS
3 #endif
4 #ifndef __STDC_LIMIT_MACROS
5 #define __STDC_LIMIT_MACROS
6 #endif
7 /***************************************************************************************[System.cc]
8 Copyright (c) 2003-2006, Niklas Een, Niklas Sorensson
9 Copyright (c) 2007-2010, Niklas Sorensson
10
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:
16
17 The above copyright notice and this permission notice shall be included in all copies or
18 substantial portions of the Software.
19
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 **************************************************************************************************/
26
27 #if !defined(__wasm)
28 #include <signal.h>
29 #endif
30 #include <stdio.h>
31
32 #include "System.h"
33
34 #if defined(__linux__)
35
36 #include <stdlib.h>
37
38 using namespace Minisat;
39
40 static inline int memReadStat(int field)
41 {
42 char name[256];
43 pid_t pid = getpid();
44 int value;
45
46 sprintf(name, "/proc/%d/statm", pid);
47 FILE* in = fopen(name, "rb");
48 if (in == NULL) return 0;
49
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);
53 fclose(in);
54 return value;
55 }
56
57
58 static inline int memReadPeak(void)
59 {
60 char name[256];
61 pid_t pid = getpid();
62
63 sprintf(name, "/proc/%d/status", pid);
64 FILE* in = fopen(name, "rb");
65 if (in == NULL) return 0;
66
67 // Find the correct line, beginning with "VmPeak:":
68 int peak_kb = 0;
69 while (!feof(in) && fscanf(in, "VmPeak: %d kB", &peak_kb) != 1)
70 while (!feof(in) && fgetc(in) != '\n')
71 ;
72 fclose(in);
73
74 return peak_kb;
75 }
76
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; }
81
82 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__gnu_hurd__)
83
84 double Minisat::memUsed() {
85 struct rusage ru;
86 getrusage(RUSAGE_SELF, &ru);
87 return (double)ru.ru_maxrss / 1024; }
88 double Minisat::memUsedPeak(bool) { return memUsed(); }
89
90
91 #elif defined(__APPLE__)
92 #include <malloc/malloc.h>
93
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(); }
99
100 #else
101 double Minisat::memUsed() { return 0; }
102 double Minisat::memUsedPeak(bool) { return 0; }
103 #endif
104
105
106 #if !defined(_MSC_VER) && !defined(__MINGW32__) && !defined(__wasm)
107 void Minisat::limitMemory(uint64_t max_mem_mb)
108 {
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
112 #endif
113
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;
117 rlimit rl;
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");
123 }
124 }
125
126 #if defined(__OpenBSD__)
127 #undef RLIMIT_AS
128 #endif
129 }
130 #else
131 void Minisat::limitMemory(uint64_t /*max_mem_mb*/)
132 {
133 printf("WARNING! Memory limit not supported on this architecture.\n");
134 }
135 #endif
136
137
138 #if !defined(_MSC_VER) && !defined(__MINGW32__) && !defined(__wasm)
139 void Minisat::limitTime(uint32_t max_cpu_time)
140 {
141 if (max_cpu_time != 0){
142 rlimit rl;
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");
148 }
149 }
150 }
151 #else
152 void Minisat::limitTime(uint32_t /*max_cpu_time*/)
153 {
154 printf("WARNING! CPU-time limit not supported on this architecture.\n");
155 }
156 #endif
157
158
159 void Minisat::sigTerm(void handler(int))
160 {
161 #if defined(__wasm)
162 (void)handler;
163 #else
164 signal(SIGINT, handler);
165 signal(SIGTERM,handler);
166 #ifdef SIGXCPU
167 signal(SIGXCPU,handler);
168 #endif
169 #endif
170 }