yosys-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
System.cc
Go to the documentation of this file.
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 
101 {
102 #if defined(__linux__) && defined(_FPU_EXTENDED) && defined(_FPU_DOUBLE) && defined(_FPU_GETCW)
103  // Only correct FPU precision on Linux architectures that needs and supports it:
104  fpu_control_t oldcw, newcw;
105  _FPU_GETCW(oldcw); newcw = (oldcw & ~_FPU_EXTENDED) | _FPU_DOUBLE; _FPU_SETCW(newcw);
106  printf("WARNING: for repeatability, setting FPU to use double precision\n");
107 #endif
108 }
109 
110 
111 #if !defined(_MSC_VER) && !defined(__MINGW32__)
112 void Minisat::limitMemory(uint64_t max_mem_mb)
113 {
114 // FIXME: OpenBSD does not support RLIMIT_AS. Not sure how well RLIMIT_DATA works instead.
115 #if defined(__OpenBSD__)
116 #define RLIMIT_AS RLIMIT_DATA
117 #endif
118 
119  // Set limit on virtual memory:
120  if (max_mem_mb != 0){
121  rlim_t new_mem_lim = (rlim_t)max_mem_mb * 1024*1024;
122  rlimit rl;
123  getrlimit(RLIMIT_AS, &rl);
124  if (rl.rlim_max == RLIM_INFINITY || new_mem_lim < rl.rlim_max){
125  rl.rlim_cur = new_mem_lim;
126  if (setrlimit(RLIMIT_AS, &rl) == -1)
127  printf("WARNING! Could not set resource limit: Virtual memory.\n");
128  }
129  }
130 
131 #if defined(__OpenBSD__)
132 #undef RLIMIT_AS
133 #endif
134 }
135 #else
136 void Minisat::limitMemory(uint64_t /*max_mem_mb*/)
137 {
138  printf("WARNING! Memory limit not supported on this architecture.\n");
139 }
140 #endif
141 
142 
143 #if !defined(_MSC_VER) && !defined(__MINGW32__)
144 void Minisat::limitTime(uint32_t max_cpu_time)
145 {
146  if (max_cpu_time != 0){
147  rlimit rl;
148  getrlimit(RLIMIT_CPU, &rl);
149  if (rl.rlim_max == RLIM_INFINITY || (rlim_t)max_cpu_time < rl.rlim_max){
150  rl.rlim_cur = max_cpu_time;
151  if (setrlimit(RLIMIT_CPU, &rl) == -1)
152  printf("WARNING! Could not set resource limit: CPU-time.\n");
153  }
154  }
155 }
156 #else
157 void Minisat::limitTime(uint32_t /*max_cpu_time*/)
158 {
159  printf("WARNING! CPU-time limit not supported on this architecture.\n");
160 }
161 #endif
162 
163 
164 void Minisat::sigTerm(void handler(int))
165 {
166  signal(SIGINT, handler);
167  signal(SIGTERM,handler);
168 #ifdef SIGXCPU
169  signal(SIGXCPU,handler);
170 #endif
171 }
void setX86FPUPrecision()
Definition: System.cc:100
#define NULL
double memUsed()
Definition: System.cc:95
void limitTime(uint32_t max_cpu_time)
Definition: System.cc:144
void sigTerm(void handler(int))
Definition: System.cc:164
void limitMemory(uint64_t max_mem_mb)
Definition: System.cc:112
double memUsedPeak(bool strictlyPeak=false)
Definition: System.cc:96