diff --git a/CMakeLists.txt b/CMakeLists.txt index 5eefb8216d..b4c8915cf3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -169,6 +169,8 @@ if(MSVC) endif() endif() +option(ENABLE_PROFILING "Use libprofiler for profiling tests" OFF) + include(Dependencies) if(MUST_BUILD_TOXAV) diff --git a/auto_tests/CMakeLists.txt b/auto_tests/CMakeLists.txt index d891ba122b..14a151598f 100644 --- a/auto_tests/CMakeLists.txt +++ b/auto_tests/CMakeLists.txt @@ -28,6 +28,10 @@ function(auto_test target) elseif(TARGET Threads::Threads) target_link_libraries(auto_${target}_test PRIVATE Threads::Threads) endif() + if(ENABLE_PROFILING) + target_link_libraries(auto_${target}_test PRIVATE PkgConfig::LIBPROFILER) + target_compile_definitions(auto_${target}_test PRIVATE "ENABLE_PROFILING") + endif() add_test(NAME ${target} COMMAND ${CROSSCOMPILING_EMULATOR} auto_${target}_test) set_tests_properties(${target} PROPERTIES TIMEOUT "${TEST_TIMEOUT_SECONDS}") # add the source dir as environment variable, so the testdata can be found diff --git a/auto_tests/send_message_test.c b/auto_tests/send_message_test.c index 92079cb8f7..27c639bc37 100644 --- a/auto_tests/send_message_test.c +++ b/auto_tests/send_message_test.c @@ -5,6 +5,10 @@ #include #include +#ifdef ENABLE_PROFILING +#include +#endif + typedef struct State { bool message_received; } State; @@ -63,6 +67,10 @@ int main(void) { setvbuf(stdout, nullptr, _IONBF, 0); +#ifdef ENABLE_PROFILING + ProfilerStart("send_message_test.prof"); +#endif + struct Tox_Options *tox_options = tox_options_new(nullptr); ck_assert(tox_options != nullptr); @@ -76,5 +84,9 @@ int main(void) tox_options_free(tox_options); +#ifdef ENABLE_PROFILING + ProfilerStop(); +#endif + return 0; } diff --git a/cmake/Dependencies.cmake b/cmake/Dependencies.cmake index 900e9b185a..17e34e14f0 100644 --- a/cmake/Dependencies.cmake +++ b/cmake/Dependencies.cmake @@ -34,3 +34,8 @@ endif() # For tox-bootstrapd. pkg_search_module(LIBCONFIG libconfig IMPORTED_TARGET) + +# For profiling. +if(ENABLE_PROFILING) + pkg_search_module(LIBPROFILER libprofiler IMPORTED_TARGET REQUIRED) +endif() diff --git a/other/docker/prof/Dockerfile b/other/docker/prof/Dockerfile new file mode 100644 index 0000000000..97c1b23b9a --- /dev/null +++ b/other/docker/prof/Dockerfile @@ -0,0 +1,34 @@ +FROM toxchat/c-toxcore:sources AS src +FROM golang:1.18-alpine AS build + +ENV LANG=en_US.UTF-8 \ + LANGUAGE=en_US.UTF-8 \ + LC_CTYPE=en_US.UTF-8 \ + LC_ALL=en_US.UTF-8 + +RUN ["go", "install", "github.com/google/pprof@latest"] +RUN ["apk", "add", "--no-cache", \ + "--repository=http://dl-cdn.alpinelinux.org/alpine/edge/testing/", \ + "binutils", \ + "clang", \ + "cmake", \ + "gcc", \ + "gperftools-dev", \ + "graphviz", \ + "libsodium-dev", \ + "libvpx-dev", \ + "linux-headers", \ + "musl-dev", \ + "opus-dev", \ + "pkgconfig", \ + "samurai"] +WORKDIR /work +COPY --from=src /src/ /work/ +RUN cmake -B_build -H. -GNinja -DAUTOTEST=ON -DENABLE_SHARED=OFF -DENABLE_PROFILING=ON -DCMAKE_EXE_LINKER_FLAGS="-dynamic" +RUN cmake --build _build +RUN CPUPROFILE_FREQUENCY=4000 \ + CPUPROFILE=auto_test.prof \ + _build/auto_tests/auto_send_message_test + +ENV PPROF_BINARY_PATH=/work/_build/auto_tests +CMD ["pprof", "-http", "0.0.0.0:80", "_build/auto_tests/auto_send_message_test", "auto_test.prof"] diff --git a/other/docker/prof/run b/other/docker/prof/run new file mode 100755 index 0000000000..11c5eb920a --- /dev/null +++ b/other/docker/prof/run @@ -0,0 +1,7 @@ +#!/bin/sh + +set -eux + +docker build -t toxchat/c-toxcore:sources -f other/docker/sources/Dockerfile . +docker build -t toxchat/c-toxcore:prof -f other/docker/prof/Dockerfile . +docker run --name toxcore-prof --rm -it -p "28192:80" toxchat/c-toxcore:prof