Skip to content

Commit d101b22

Browse files
Set memory limit utility
1 parent 5bd5962 commit d101b22

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

src/util/Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ SRC = arith_tools.cpp \
4040
lispexpr.cpp \
4141
lispirep.cpp \
4242
memory_info.cpp \
43+
memory_limit.cpp \
4344
merge_irep.cpp \
4445
message.cpp \
4546
mp_arith.cpp \

src/util/memory_limit.cpp

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*******************************************************************\
2+
3+
Module:
4+
5+
Author: Peter Schrammel, [email protected]
6+
7+
\*******************************************************************/
8+
9+
#include "memory_limit.h"
10+
11+
#ifdef __linux__
12+
#include <sys/resource.h>
13+
#endif
14+
15+
#include <ostream>
16+
17+
/// Outputs the memory limits to the given output stream
18+
/// \param out: output stream
19+
void memory_limits(std::ostream &out)
20+
{
21+
#ifdef __linux__
22+
struct rlimit mem_limit;
23+
getrlimit(RLIMIT_AS, &mem_limit);
24+
25+
out << " soft limit: " << mem_limit.rlim_cur << '\n';
26+
out << " hard limit: " << mem_limit.rlim_max;
27+
#else
28+
out << " not supported";
29+
#endif
30+
}
31+
32+
/// Sets the soft memory limit of the current process
33+
/// \param soft_limit: the soft limit in bytes
34+
/// \return: true if setting the limit succeeded
35+
bool set_memory_limit(std::size_t soft_limit)
36+
{
37+
#ifdef __linux__
38+
struct rlimit mem_limit;
39+
getrlimit(RLIMIT_AS, &mem_limit);
40+
mem_limit.rlim_cur = soft_limit;
41+
setrlimit(RLIMIT_AS, &mem_limit);
42+
getrlimit(RLIMIT_AS, &mem_limit);
43+
return mem_limit.rlim_cur == soft_limit;
44+
#else
45+
// not supported
46+
return false;
47+
#endif
48+
}

src/util/memory_limit.h

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*******************************************************************\
2+
3+
Module:
4+
5+
Author: Peter Schrammel, [email protected]
6+
7+
\*******************************************************************/
8+
9+
#ifndef CPROVER_UTIL_MEMORY_LIMIT_H
10+
#define CPROVER_UTIL_MEMORY_LIMIT_H
11+
12+
#include <cstddef> // size_t
13+
#include <iosfwd>
14+
15+
void memory_limits(std::ostream &);
16+
bool set_memory_limit(std::size_t soft_limit);
17+
18+
#endif // CPROVER_UTIL_MEMORY_LIMIT_H

0 commit comments

Comments
 (0)