From 88517230b6e5b8239a476096f290040d335e6dea Mon Sep 17 00:00:00 2001 From: Seth Hall Date: Mon, 4 Jan 2016 00:57:11 -0500 Subject: [PATCH] Fix memory usage collection on Mac OS X. - getrusage is broken on Mac OS X, but there is a Mach API available which can collect the same memory usage information. --- bro-config.h.in | 3 +++ src/util.cc | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/bro-config.h.in b/bro-config.h.in index 755a9eee98..0937950604 100644 --- a/bro-config.h.in +++ b/bro-config.h.in @@ -14,6 +14,9 @@ /* We are on a Linux system */ #cmakedefine HAVE_LINUX +/* We are on a Mac OS X (Darwin) system */ +#cmakedefine HAVE_DARWIN + /* Define if you have the `mallinfo' function. */ #cmakedefine HAVE_MALLINFO diff --git a/src/util.cc b/src/util.cc index 6a03859a3c..facbab295f 100644 --- a/src/util.cc +++ b/src/util.cc @@ -14,6 +14,11 @@ # endif #endif +#ifdef HAVE_DARWIN +#include +#include +#endif + #include #include #include @@ -1662,11 +1667,24 @@ void get_memory_usage(unsigned int* total, unsigned int* malloced) #endif +#ifdef HAVE_DARWIN + struct task_basic_info t_info; + mach_msg_type_number_t t_info_count = TASK_BASIC_INFO_COUNT; + + if ( KERN_SUCCESS != task_info(mach_task_self(), + TASK_BASIC_INFO, + (task_info_t)&t_info, + &t_info_count) ) + ret_total = 0; + else + ret_total = t_info.resident_size; +#else struct rusage r; getrusage(RUSAGE_SELF, &r); // In KB. ret_total = r.ru_maxrss * 1024; +#endif if ( total ) *total = ret_total;