345be8c4c75a54515552f1fd88a30b0f74840e49
[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 #include <signal.h>
28 #include <stdio.h>
29
30 #include "System.h"
31
32 #if defined(__linux__)
33
34 #include <stdlib.h>
35
36 using namespace Minisat;
37
38 static inline int memReadStat(int field)
39 {
40 char name[256];
41 pid_t pid = getpid();
42 int value;
43
44 sprintf(name, "/proc/%d/statm", pid);
45 FILE* in = fopen(name, "rb");
46 if (in == NULL) return 0;
47
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);
51 fclose(in);
52 return value;
53 }
54
55
56 static inline int memReadPeak(void)
57 {
58 char name[256];
59 pid_t pid = getpid();
60
61 sprintf(name, "/proc/%d/status", pid);
62 FILE* in = fopen(name, "rb");
63 if (in == NULL) return 0;
64
65 // Find the correct line, beginning with "VmPeak:":
66 int peak_kb = 0;
67 while (!feof(in) && fscanf(in, "VmPeak: %d kB", &peak_kb) != 1)
68 while (!feof(in) && fgetc(in) != '\n')
69 ;
70 fclose(in);
71
72 return peak_kb;
73 }
74
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; }
79
80 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__gnu_hurd__)
81
82 double Minisat::memUsed() {
83 struct rusage ru;
84 getrusage(RUSAGE_SELF, &ru);
85 return (double)ru.ru_maxrss / 1024; }
86 double Minisat::memUsedPeak(bool) { return memUsed(); }
87
88
89 #elif defined(__APPLE__)
90 #include <malloc/malloc.h>
91
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(); }
97
98 #else
99 double Minisat::memUsed() { return 0; }
100 double Minisat::memUsedPeak(bool) { return 0; }
101 #endif
102
103
104 #if !defined(_MSC_VER) && !defined(__MINGW32__) && !defined(__wasm)
105 void Minisat::limitMemory(uint64_t max_mem_mb)
106 {
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
110 #endif
111
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;
115 rlimit rl;
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");
121 }
122 }
123
124 #if defined(__OpenBSD__)
125 #undef RLIMIT_AS
126 #endif
127 }
128 #else
129 void Minisat::limitMemory(uint64_t /*max_mem_mb*/)
130 {
131 printf("WARNING! Memory limit not supported on this architecture.\n");
132 }
133 #endif
134
135
136 #if !defined(_MSC_VER) && !defined(__MINGW32__) && !defined(__wasm)
137 void Minisat::limitTime(uint32_t max_cpu_time)
138 {
139 if (max_cpu_time != 0){
140 rlimit rl;
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");
146 }
147 }
148 }
149 #else
150 void Minisat::limitTime(uint32_t /*max_cpu_time*/)
151 {
152 printf("WARNING! CPU-time limit not supported on this architecture.\n");
153 }
154 #endif
155
156
157 void Minisat::sigTerm(void handler(int))
158 {
159 #if defined(__wasm)
160 (void)handler;
161 #else
162 signal(SIGINT, handler);
163 signal(SIGTERM,handler);
164 #ifdef SIGXCPU
165 signal(SIGXCPU,handler);
166 #endif
167 #endif
168 }