ABC: A System for Sequential Synthesis and Verification
 
Loading...
Searching...
No Matches
cadical_resources.cpp
Go to the documentation of this file.
1#include "global.h"
2
3#include "internal.hpp"
4
5/*------------------------------------------------------------------------*/
6
7// This is operating system specific code. We mostly develop on Linux and
8// there it should be fine and mostly works out-of-the-box on MacOS too but
9// Windows needs special treatment (as probably other operating systems
10// too).
11
12extern "C" {
13
14#ifdef WIN32
15
16#ifndef __WIN32_WINNT
17#define __WIN32_WINNT 0x0600
18#endif
19
20// Clang-format would reorder the includes which breaks the Windows code
21// as it expects 'windows.h' to be included first. So disable it here.
22
23// clang-format off
24
25#include <windows.h>
26#include <psapi.h>
27
28// clang-format on
29
30#else
31
32#include <sys/resource.h>
33#include <sys/time.h>
34#include <sys/types.h>
35#include <unistd.h>
36
37#endif
38
39#include <string.h>
40}
41
43
44namespace CaDiCaL {
45
46/*------------------------------------------------------------------------*/
47
48#ifdef WIN32
49
50double absolute_real_time () {
51 FILETIME f;
52 GetSystemTimeAsFileTime (&f);
53 ULARGE_INTEGER t;
54 t.LowPart = f.dwLowDateTime;
55 t.HighPart = f.dwHighDateTime;
56 double res = (__int64) t.QuadPart;
57 res *= 1e-7;
58 return res;
59}
60
61double absolute_process_time () {
62 double res = 0;
63 FILETIME fc, fe, fu, fs;
64 if (GetProcessTimes (GetCurrentProcess (), &fc, &fe, &fu, &fs)) {
65 ULARGE_INTEGER u, s;
66 u.LowPart = fu.dwLowDateTime;
67 u.HighPart = fu.dwHighDateTime;
68 s.LowPart = fs.dwLowDateTime;
69 s.HighPart = fs.dwHighDateTime;
70 res = (__int64) u.QuadPart + (__int64) s.QuadPart;
71 res *= 1e-7;
72 }
73 return res;
74}
75
76#else
77
79 struct timeval tv;
80 if (gettimeofday (&tv, 0))
81 return 0;
82 return 1e-6 * tv.tv_usec + tv.tv_sec;
83}
84
85// We use 'getrusage' for 'process_time' and 'maximum_resident_set_size'
86// which is pretty standard on Unix but probably not available on Windows
87// etc. For different variants of Unix not all fields are meaningful.
88
90 double res;
91 struct rusage u;
92 if (getrusage (RUSAGE_SELF, &u))
93 return 0;
94 res = u.ru_utime.tv_sec + 1e-6 * u.ru_utime.tv_usec; // user time
95 res += u.ru_stime.tv_sec + 1e-6 * u.ru_stime.tv_usec; // + system time
96 return res;
97}
98
99#endif
100
101double Internal::real_time () const {
102 return absolute_real_time () - stats.time.real;
103}
104
105double Internal::process_time () const {
106 return absolute_process_time () - stats.time.process;
107}
108
109/*------------------------------------------------------------------------*/
110
111#ifdef WIN32
112
113uint64_t current_resident_set_size () {
114 PROCESS_MEMORY_COUNTERS pmc;
115 if (GetProcessMemoryInfo (GetCurrentProcess (), &pmc, sizeof (pmc))) {
116 return pmc.WorkingSetSize;
117 } else
118 return 0;
119}
120
121uint64_t maximum_resident_set_size () {
122 PROCESS_MEMORY_COUNTERS pmc;
123 if (GetProcessMemoryInfo (GetCurrentProcess (), &pmc, sizeof (pmc))) {
124 return pmc.PeakWorkingSetSize;
125 } else
126 return 0;
127}
128
129#else
130
131// This seems to work on Linux (man page says since Linux 2.6.32).
132
134 struct rusage u;
135 if (getrusage (RUSAGE_SELF, &u))
136 return 0;
137 return ((uint64_t) u.ru_maxrss) << 10;
138}
139
140// Unfortunately 'getrusage' on Linux does not support current resident set
141// size (the field 'ru_ixrss' is there but according to the man page
142// 'unused'). Thus we fall back to use the '/proc' file system instead. So
143// this is not portable at all and needs to be replaced on other systems
144// The code would still compile though (assuming 'sysconf' and
145// '_SC_PAGESIZE' are available).
146
148 char path[64];
149 snprintf (path, sizeof path, "/proc/%" PRId64 "/statm",
150 (int64_t) getpid ());
151 FILE *file = fopen (path, "r");
152 if (!file)
153 return 0;
154 uint64_t dummy, rss;
155 int scanned = fscanf (file, "%" PRIu64 " %" PRIu64 "", &dummy, &rss);
156 fclose (file);
157 return scanned == 2 ? rss * sysconf (_SC_PAGESIZE) : 0;
158}
159
160#endif
161
162/*------------------------------------------------------------------------*/
163
164} // namespace CaDiCaL
165
#define ABC_NAMESPACE_IMPL_START
#define ABC_NAMESPACE_IMPL_END
uint64_t current_resident_set_size()
double absolute_process_time()
uint64_t maximum_resident_set_size()
double absolute_real_time()
double process_time() const
Definition file.h:23