Skip to content

Commit

Permalink
Add PCU support for BSD mallctl and enable pcu/reel under BSD
Browse files Browse the repository at this point in the history
Add CMake check for mallctl and use it in PCU_GetMem if available.
Add private PCU link to libexecinfo on BSD.
Enable pcu/reel/reel.c functions on BSD.

Signed-off-by: Aiden Woodruff <[email protected]>
  • Loading branch information
bobpaw committed Jan 4, 2024
1 parent 50567e5 commit 388c71d
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 3 deletions.
10 changes: 10 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -193,11 +193,21 @@ scorec_export_library(core)
#check for mallinfo2
if(${CMAKE_CXX_COMPILER_ID} STREQUAL "GNU" OR ${CMAKE_CXX_COMPILER_ID} STREQUAL "Clang")
include(CheckCXXSymbolExists)
check_cxx_symbol_exists(mallinfo "malloc.h" PUMI_HAS_MALLINFO)
check_cxx_symbol_exists(mallinfo2 "malloc.h" PUMI_HAS_MALLINFO2)
check_cxx_symbol_exists(mallctl "malloc_np.h" PUMI_HAS_MALLCTL)
if(PUMI_HAS_MALLINFO)
target_compile_definitions(core INTERFACE -DPUMI_HAS_MALLINFO)
target_compile_definitions(pcu PRIVATE -DPUMI_HAS_MALLINFO)
endif()
if(PUMI_HAS_MALLINFO2)
target_compile_definitions(core INTERFACE -DPUMI_HAS_MALLINFO2)
target_compile_definitions(pcu PRIVATE -DPUMI_HAS_MALLINFO2)
endif()
if(PUMI_HAS_MALLCTL)
target_compile_definitions(core INTERFACE -DPUMI_HAS_MALLCTL)
target_compile_definitions(pcu PRIVATE -DPUMI_HAS_MALLCTL)
endif()
endif()

if(BUILD_EXES)
Expand Down
6 changes: 6 additions & 0 deletions pcu/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ add_library(pcu ${SOURCES})
# see: https://github.com/open-mpi/ompi/issues/5157
target_compile_definitions(pcu PUBLIC OMPI_SKIP_MPICXX)

execute_process(COMMAND uname OUTPUT_VARIABLE UNAME_STR)
string(STRIP "${UNAME_STR}" UNAME_STR)
if(UNAME_STR STREQUAL "FreeBSD" OR UNAME_STR STREQUAL "NetBSD" OR UNAME_STR STREQUAL "OpenBSD" OR UNAME_STR STREQUAL "DragonFly")
target_link_libraries(pcu PRIVATE execinfo)
endif()

# Include directories
target_include_directories(pcu
PUBLIC
Expand Down
10 changes: 9 additions & 1 deletion pcu/pcu_mem.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
#pragma GCC diagnostic pop
#endif

#elif defined(PUMI_HAVE_MALLCTL)

#include <malloc_np.h>

#else

#include <malloc.h> //warning - this is GNU-specific
Expand All @@ -48,10 +52,14 @@ double PCU_GetMem() {
size_t heap;
Kernel_GetMemorySize(KERNEL_MEMSIZE_HEAP, &heap);
return (double)heap/M;
#elif defined(PUMI_HAS_MALLCTL)
size_t size = 0, sizelen = sizeof(size_t);
mallctl("stats.allocated", &size, &sizelen, NULL, 0);
return (double)size/M;
#elif defined(__GNUC__) && defined(PUMI_HAS_MALLINFO2)
struct mallinfo2 meminfo_now = mallinfo2();
return ((double)meminfo_now.arena)/M;
#elif defined(__GNUC__)
#elif defined(__GNUC__) && defined(PUMI_HAS_MALLINFO)
struct mallinfo meminfo_now = mallinfo();
return ((double)meminfo_now.arena)/M;
#endif
Expand Down
4 changes: 2 additions & 2 deletions pcu/reel/reel.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ void reel_fail(const char* format, ...)
abort();
}

#if defined(__linux__) || defined(__APPLE__)
#if defined(__linux__) || defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
#include <execinfo.h> /* backtrace for pcu_trace */
#include <signal.h> /* signal for pcu_protect */

Expand Down Expand Up @@ -59,6 +59,6 @@ void reel_protect(void)
#else
void reel_protect(void)
{
reel_fail("reel_protect only supported on Linux and OS X");
reel_fail("reel_protect only supported on Linux, BSD, and OS X");
}
#endif

0 comments on commit 388c71d

Please sign in to comment.