Skip to content

Commit 45f9c56

Browse files
authored
Merge pull request #115 from cindytsai/valgrind
Add method for Python to call valgrind massif detailed snapshot
2 parents 6e66b6e + 8381289 commit 45f9c56

File tree

3 files changed

+50
-3
lines changed

3 files changed

+50
-3
lines changed

CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@ option(INTERACTIVE_MODE "Use interactive mode"
1313
option(JUPYTER_KERNEL "Use Jupyter notebook interface" OFF)
1414
option(SUPPORT_TIMER "Support time profiling" OFF)
1515
option(USE_PYBIND11 "Use pybind11" OFF)
16+
option(SUPPORT_VALGRIND "Support valgrind" OFF)
1617

1718
## set paths ##
1819
# It is recommended that we always provide PYTHON_PATH and MPI_PATH.
1920
# libyt will also try to find the package if the paths weren't provided.
2021
set(PYTHON_PATH "" CACHE PATH "Path to Python installation prefix (Always)")
2122
set(MPI_PATH "" CACHE PATH "Path to MPI installation prefix (-DSERIAL_MODE=OFF)")
2223
set(READLINE_PATH "" CACHE PATH "Path to Readline installation prefix (-DINTERACTIVE_MODE=ON)")
24+
set(VALGRIND_PATH "" CACHE PATH "Path to valgrind installation prefix (-DSUPPORT_VALGRIND=ON / -DLIBYT_RUN_MEMORY_PROFILE=ON)")
2325

2426
## set paths (optional) ##
2527
# libyt will get the dependencies if it cannot find one when needed.
@@ -40,8 +42,6 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
4042
option(BUILD_SHARED_LIBS "Building using shared libraries" ON )
4143

4244
# run test ##
43-
set(VALGRIND_PATH "" CACHE PATH "Path to valgrind installation prefix (-DLIBYT_RUN_MEMORY_PROFILE=ON)")
44-
4545
option(LIBYT_RUN_TEST "Run unit test" OFF)
4646
option(LIBYT_RUN_MEMORY_PROFILE "Run memory profile" OFF)
4747

src/CMakeLists.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,17 @@ set_option(INTERACTIVE_MODE)
7373
set_option(JUPYTER_KERNEL)
7474
set_option(SUPPORT_TIMER)
7575
set_option(USE_PYBIND11)
76+
set_option(SUPPORT_VALGRIND)
7677

7778
# include dir
7879
target_include_directories(
7980
yt
8081
PRIVATE
8182
${PROJECT_SOURCE_DIR}/include
82-
$<$<NOT:$<BOOL:${SERIAL_MODE}>>:${MPI_CXX_INCLUDE_DIRS}> ${Python_INCLUDE_DIRS}
83+
$<$<NOT:$<BOOL:${SERIAL_MODE}>>:${MPI_CXX_INCLUDE_DIRS}>
84+
${Python_INCLUDE_DIRS}
8385
$<$<BOOL:${INTERACTIVE_MODE}>:${Readline_INCLUDE_DIR}>
86+
$<$<BOOL:${SUPPORT_VALGRIND}>:${VALGRIND_PATH}/include>
8487
)
8588

8689
# link lib

src/libyt_python_module.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
#include <pybind11/pytypes.h>
1616
#endif
1717

18+
#ifdef SUPPORT_VALGRIND
19+
#include <valgrind/valgrind.h>
20+
#endif
21+
1822
//-------------------------------------------------------------------------------------------------------
1923
// Description : List of libyt C extension python methods built using Pybind11 API
2024
//
@@ -514,6 +518,16 @@ pybind11::object GetParticleRemote(const pybind11::dict& py_ptf,
514518
#endif // #ifndef SERIAL_MODE
515519
}
516520

521+
#ifdef SUPPORT_VALGRIND
522+
pybind11::object DumpValgrindDetailedSnapshot(const char* filename) {
523+
std::string valgrind_cmd = "detailed_snapshot ";
524+
valgrind_cmd += filename;
525+
526+
VALGRIND_MONITOR_COMMAND(valgrind_cmd.c_str());
527+
return pybind11::none();
528+
}
529+
#endif
530+
517531
PYBIND11_EMBEDDED_MODULE(libyt, m) {
518532
SET_TIMER(__PRETTY_FUNCTION__);
519533

@@ -561,6 +575,11 @@ PYBIND11_EMBEDDED_MODULE(libyt, m) {
561575
m.def("get_particle_remote",
562576
&GetParticleRemote,
563577
pybind11::return_value_policy::take_ownership);
578+
#ifdef SUPPORT_VALGRIND
579+
m.def("dump_valgrind_detailed_snapshot",
580+
&DumpValgrindDetailedSnapshot,
581+
pybind11::return_value_policy::take_ownership);
582+
#endif
564583
}
565584

566585
#else // #ifdef USE_PYBIND11
@@ -1120,6 +1139,25 @@ static PyObject* LibytParticleGetParticleRemote(PyObject* self, PyObject* args)
11201139
#endif // #ifndef SERIAL_MODE
11211140
}
11221141

1142+
#ifdef SUPPORT_VALGRIND
1143+
static PyObject* LibytDumpValgrindDetailedSnapshot(PyObject* self, PyObject* args) {
1144+
char* filename;
1145+
if (!PyArg_ParseTuple(args, "s", &filename)) {
1146+
PyErr_SetString(
1147+
PyExc_TypeError,
1148+
"Wrong input type, expect to be libyt.dump_valgrind_detailed_snapshot(str).");
1149+
return NULL;
1150+
}
1151+
1152+
std::string valgrind_cmd = "detailed_snapshot ";
1153+
valgrind_cmd += filename;
1154+
1155+
VALGRIND_MONITOR_COMMAND(valgrind_cmd.c_str());
1156+
1157+
Py_RETURN_NONE;
1158+
}
1159+
#endif
1160+
11231161
//-------------------------------------------------------------------------------------------------------
11241162
// Description : Preparation for creating libyt python module
11251163
//
@@ -1152,6 +1190,12 @@ static PyMethodDef libyt_method_list[] = {
11521190
LibytParticleGetParticleRemote,
11531191
METH_VARARGS,
11541192
"Get remote particle attribute data."},
1193+
#ifdef SUPPORT_VALGRIND
1194+
{"dump_valgrind_detailed_snapshot",
1195+
LibytDumpValgrindDetailedSnapshot,
1196+
METH_VARARGS,
1197+
"Dump valgrind detailed snapshot."},
1198+
#endif
11551199
{NULL, NULL, 0, NULL} // sentinel
11561200
};
11571201

0 commit comments

Comments
 (0)