Profiling

Finding memory leakages in PythonAPI

Easy way

See https://stackoverflow.com/questions/20112989/how-to-use-valgrind-with-python

/usr/bin/valgrind --log-file=output.out --leak-check=full --track-origins=yes --show-leak-kinds=all --suppressions=valgrind-python.supp python3 /home/pospelov/development/machine-learning/ba-learning/rotated_lattices/gen_lattice.py . 2 2

Output will be huge (Mbytes) and will contains lots of stuff about Py* API functions. With some luck one can find among it calls related to BornAgain.

Hard way

One can decrease amount of false Python-related warnings produces by valgrind, if one compile Python from scratch.
The problem is that valgrind will be still generating a lot of warnings and I didn't find the way to suppress it. Anyway, here is recipe.

  • Install Python dependencies

In my OpenSuse case libopenssl-1_1-devel, libffi-devel, tcl-devel, dk-devel, valgrind-devel

  • Download Python source, build Python
export MYPYTHON=<place-for-python>
./configure --enable-shared --with-valgrind --without-pymalloc --with-pydebug --prefix=$MYPYTHON
make; make install

# OpenSuse specific (will cure not found readline, broken pip, etc)
ln -s $MYPYTHON/lib64/python3.6/lib-dynload/ $MYPYTHON/lib/python3.6/lib-dynload
  • Take care of Python environment
export LD_LIBRARY_PATH=$MYPYTHON/lib64:$LD_LIBRARY_PATH
export PATH=$MYPYTHON/bin:$PATH
export PYTHONHOME=$MYPYTHON
  • Make sure that Python is working
  • Install numpy and matplotlib with pip.
  • Compile BornAgain against new Python
cmake  -DCMAKE_BUILD_TYPE=Debug -DCMAKE_INSTALL_PREFIX=../installed -DPYTHON_LIBRARY=$MYPYTHON/lib64/libpython3.6d.so -DPYTHON_EXECUTABLE=$MYPYTHON/bin/python3.6 -DPYTHON_INCLUDE_DIRS=$MYPYTHON/include/python3.6d <source>
  • Run valgrind
/usr/bin/valgrind --log-file=output.out --leak-check=full --track-origins=yes --show-leak-kinds=all --suppressions=valgrind-python.supp $MYPYTHON/bin/python3 share/BornAgain-1.14/Examples/python/simulation/ex01_BasicParticles/CylindersAndPrisms.py

Obsolete

  • KDAB apparently uses the following tools (among many other tools, I presume) for analyzing Qt applications:

https://github.com/KDAB/GammaRay/wiki/Getting-GammaRay
https://github.com/KDE/heaptrack