Skip to content

Commit a0178a7

Browse files
committed
Make memory_info portable across all BSD derivatives
We can always fall back to `getrusage` for minimal memory usage information.
1 parent 56a3ee3 commit a0178a7

File tree

1 file changed

+29
-20
lines changed

1 file changed

+29
-20
lines changed

src/util/memory_info.cpp

+29-20
Original file line numberDiff line numberDiff line change
@@ -8,34 +8,37 @@ Author: Daniel Kroening, [email protected]
88

99
#include "memory_info.h"
1010

11-
#ifdef __APPLE__
12-
#include <mach/task.h>
13-
#include <mach/mach_init.h>
14-
#include <malloc/malloc.h>
15-
#endif
16-
17-
#ifdef __linux__
18-
#include <malloc.h>
19-
#endif
11+
#include "invariant.h"
2012

21-
#ifdef _WIN32
22-
#include <util/pragma_push.def>
23-
#ifdef _MSC_VER
24-
#pragma warning(disable:4668)
25-
// using #if/#elif on undefined macro
26-
#pragma warning(disable : 5039)
13+
#ifdef __GLIBC__
14+
# include <malloc.h>
15+
#elif defined(_WIN32)
16+
# include <util/pragma_push.def>
17+
# ifdef _MSC_VER
18+
# pragma warning(disable : 4668)
19+
// using #if/#elif on undefined macro
20+
# pragma warning(disable : 5039)
2721
// pointer or reference to potentially throwing function passed to extern C
28-
#endif
29-
#include <windows.h>
30-
#include <psapi.h>
31-
#include <util/pragma_pop.def>
22+
# endif
23+
# include <util/pragma_pop.def>
24+
// windows.h must be included before psapi.h
25+
// clang-format off
26+
# include <windows.h>
27+
# include <psapi.h>
28+
// clang-format on
29+
#elif defined(__APPLE__)
30+
# include <mach/mach_init.h>
31+
# include <mach/task.h>
32+
# include <malloc/malloc.h>
33+
#else
34+
# include <sys/resource.h>
3235
#endif
3336

3437
#include <ostream>
3538

3639
void memory_info(std::ostream &out)
3740
{
38-
#if defined(__linux__) && defined(__GLIBC__)
41+
#ifdef __GLIBC__
3942
// NOLINTNEXTLINE(readability/identifiers)
4043
struct mallinfo m = mallinfo();
4144
out << " non-mmapped space allocated from system: " << m.arena << "\n";
@@ -70,5 +73,11 @@ void memory_info(std::ostream &out)
7073
<< static_cast<double>(t.max_size_in_use)/1000000 << "m\n";
7174
out << " size_allocated: "
7275
<< static_cast<double>(t.size_allocated)/1000000 << "m\n";
76+
#else
77+
// NOLINTNEXTLINE(readability/identifiers)
78+
struct rusage r_usage;
79+
int result = getrusage(RUSAGE_SELF, &r_usage);
80+
CHECK_RETURN(result == 0);
81+
out << " maximum resident set size [bytes]: " << r_usage.ru_maxrss << '\n';
7382
#endif
7483
}

0 commit comments

Comments
 (0)