Initial checkin of SAT solvers
[satlib.git] / glucose-syrup / utils / System.cc
1 /***************************************************************************************[System.cc]
2 Copyright (c) 2003-2006, Niklas Een, Niklas Sorensson
3 Copyright (c) 2007-2010, Niklas Sorensson
4
5 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
6 associated documentation files (the "Software"), to deal in the Software without restriction,
7 including without limitation the rights to use, copy, modify, merge, publish, distribute,
8 sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included in all copies or
12 substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
15 NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
16 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
17 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
18 OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19 **************************************************************************************************/
20
21 #include "utils/System.h"
22
23 #if defined(__linux__)
24
25 #include <stdio.h>
26 #include <stdlib.h>
27
28 using namespace Glucose;
29
30 // TODO: split the memory reading functions into two: one for reading high-watermark of RSS, and
31 // one for reading the current virtual memory size.
32
33 static inline int memReadStat(int field)
34 {
35     char  name[256];
36     pid_t pid = getpid();
37     int   value;
38
39     sprintf(name, "/proc/%d/statm", pid);
40     FILE* in = fopen(name, "rb");
41     if (in == NULL) return 0;
42
43     for (; field >= 0; field--)
44         if (fscanf(in, "%d", &value) != 1)
45             printf("ERROR! Failed to parse memory statistics from \"/proc\".\n"), exit(1);
46     fclose(in);
47     return value;
48 }
49
50
51 static inline int memReadPeak(void)
52 {
53     char  name[256];
54     pid_t pid = getpid();
55
56     sprintf(name, "/proc/%d/status", pid);
57     FILE* in = fopen(name, "rb");
58     if (in == NULL) return 0;
59
60     // Find the correct line, beginning with "VmPeak:":
61     int peak_kb = 0;
62     while (!feof(in) && fscanf(in, "VmPeak: %d kB", &peak_kb) != 1)
63         while (!feof(in) && fgetc(in) != '\n')
64             ;
65     fclose(in);
66
67     return peak_kb;
68 }
69
70 double Glucose::memUsed() { return (double)memReadStat(0) * (double)getpagesize() / (1024*1024); }
71 double Glucose::memUsedPeak() { 
72     double peak = memReadPeak() / 1024;
73     return peak == 0 ? memUsed() : peak; }
74
75 #elif defined(__FreeBSD__)
76
77 double Glucose::memUsed(void) {
78     struct rusage ru;
79     getrusage(RUSAGE_SELF, &ru);
80     return (double)ru.ru_maxrss / 1024; }
81 double MiniSat::memUsedPeak(void) { return memUsed(); }
82
83
84 #elif defined(__APPLE__)
85 #include <malloc/malloc.h>
86
87 double Glucose::memUsed(void) {
88     malloc_statistics_t t;
89     malloc_zone_statistics(NULL, &t);
90     return (double)t.max_size_in_use / (1024*1024); }
91
92 #else
93 double Glucose::memUsed() { 
94     return 0; }
95 #endif