minisat: Remove template with gzFile specialization.
[yosys.git] / libs / minisat / System.cc
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
6
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:
12
13 The above copyright notice and this permission notice shall be included in all copies or
14 substantial portions of the Software.
15
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 **************************************************************************************************/
22
23 #include <signal.h>
24 #include <stdio.h>
25
26 #include "System.h"
27
28 #if defined(__linux__)
29
30 #include <stdlib.h>
31
32 using namespace Minisat;
33
34 static inline int memReadStat(int field)
35 {
36 char name[256];
37 pid_t pid = getpid();
38 int value;
39
40 sprintf(name, "/proc/%d/statm", pid);
41 FILE* in = fopen(name, "rb");
42 if (in == NULL) return 0;
43
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);
47 fclose(in);
48 return value;
49 }
50
51
52 static inline int memReadPeak(void)
53 {
54 char name[256];
55 pid_t pid = getpid();
56
57 sprintf(name, "/proc/%d/status", pid);
58 FILE* in = fopen(name, "rb");
59 if (in == NULL) return 0;
60
61 // Find the correct line, beginning with "VmPeak:":
62 int peak_kb = 0;
63 while (!feof(in) && fscanf(in, "VmPeak: %d kB", &peak_kb) != 1)
64 while (!feof(in) && fgetc(in) != '\n')
65 ;
66 fclose(in);
67
68 return peak_kb;
69 }
70
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; }
75
76 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__gnu_hurd__)
77
78 double Minisat::memUsed() {
79 struct rusage ru;
80 getrusage(RUSAGE_SELF, &ru);
81 return (double)ru.ru_maxrss / 1024; }
82 double Minisat::memUsedPeak(bool) { return memUsed(); }
83
84
85 #elif defined(__APPLE__)
86 #include <malloc/malloc.h>
87
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(); }
93
94 #else
95 double Minisat::memUsed() { return 0; }
96 double Minisat::memUsedPeak(bool) { return 0; }
97 #endif
98
99
100 #if !defined(_MSC_VER) && !defined(__MINGW32__)
101 void Minisat::limitMemory(uint64_t max_mem_mb)
102 {
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
106 #endif
107
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;
111 rlimit rl;
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");
117 }
118 }
119
120 #if defined(__OpenBSD__)
121 #undef RLIMIT_AS
122 #endif
123 }
124 #else
125 void Minisat::limitMemory(uint64_t /*max_mem_mb*/)
126 {
127 printf("WARNING! Memory limit not supported on this architecture.\n");
128 }
129 #endif
130
131
132 #if !defined(_MSC_VER) && !defined(__MINGW32__)
133 void Minisat::limitTime(uint32_t max_cpu_time)
134 {
135 if (max_cpu_time != 0){
136 rlimit rl;
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");
142 }
143 }
144 }
145 #else
146 void Minisat::limitTime(uint32_t /*max_cpu_time*/)
147 {
148 printf("WARNING! CPU-time limit not supported on this architecture.\n");
149 }
150 #endif
151
152
153 void Minisat::sigTerm(void handler(int))
154 {
155 signal(SIGINT, handler);
156 signal(SIGTERM,handler);
157 #ifdef SIGXCPU
158 signal(SIGXCPU,handler);
159 #endif
160 }