Skip to content

linem-davton/cpp-playground

Repository files navigation

CPP Playground

Getting Started

  • CMakePresets.json defines various debug and release configurations, including the compiler flags.
  • scripts directory contains scripts to build, test, and benchmark the project.
git clone [email protected]:linem-davton/cpp-playground.git
cd cpp-playground
mkdir build
./scripts/install_dep.sh # install dependencies
./scripts/build.sh # build the project
./scripts/test_all.sh # run all tests
./scripts/bench_all.sh # run all benchmarks , can be time consuming

The executables are generated in the build/{preset}/bin directory.

Requirements

Install Script installs the following requirements (apt package manager). Perf is not available on WSL2. Works on native Linux.

Adding Executables

Place the header files in the include directory. Place the source files in the src directory, or in a new subdirectory in the src directory. Configure CMake by Adding the following to the CMakeLists.txt file in src dir to build the executable.

add_executable(a.out a.cpp) # add executable
target_link_libraries(a.out PRIVATE <libs>) # link libraries

Adding Libraries

Add header files in the include directory. Place the source files in a new subdirectory in the src directory. Configure CMake by Adding the following to the CMakeLists.txt file in the new subdirectory to build the library.

add_library(lib_name STATIC lib.cpp <other.cpp>) # <other.cpp> is placeholder for other source files

Adding Tests

The project uses Google Test for testing. Place the test files in the tests directory. Add the following to the CMakeLists.txt file in the tests directory to build the test.

add_executable(test_name test.cpp) # add test
target_link_libraries(test_name PRIVATE <libs> GTest::gtest_main) # <libs> is place holder for other libs

Adding Benchmarks

The project uses Google Benchmark for benchmarking. Place the benchmark files in the benchmarks directory. Add the following to the CMakeLists.txt file in the benchmarks directory to build the benchmark.

add_executable(benchmark_name benchmark.cpp) # add benchmark
target_link_libraries(benchmark_name PRIVATE <libs> benchmark::benchmark) # <libs> is place holder for other libs
  • Add the benchmark to the list of benchmarks in bench_all.sh script.

Performance

Perf

I have not be able to get perf to work on WSL2. Works on native Linux.

perf stat ./matrices # get performance statistics
perf stat -e cache-references,cache-misses,LLC-loads,LLC-load-misses ./matrices # get cache statistics

Sample perf stat outputs for matrix multiplication, in the matrices directory.

Call Graph

Build with -fno-omit-frame-pointer to disable frame pointer optimization, to make call graphs more accurate.(Not tested if disabling frame pointer optimization affects the call graph accuracy.)

# Call graph
perf record --call-graph dwarf -F 99 ./a.out # record call graph
perf script | speedscope -  # Vizualize the call graph and flame graph

Perf benchmarks for matrix multiplication are available in the matrices directory.

Speedscope: https://www.speedscope.app/

Strace

strace ./a.out # trace system calls
strace -c ./a.out # get summary of system calls
strace -f ./a.out # trace child processes
strace -e trace=open,close ./a.out # trace only open and close system calls
strace -e trace=network ./a.out # trace only network system calls
strace -k ./a.out # print call stack

Heaptrack

sudo apt install heaptrack
heaptrack ./a.out # get heap memory statistics
heaptrack --analyze heaptrack*.zst # analyze the heap memory statistics

Heaptrack is a heap memory profiler that tracks all memory allocations and de-allocations in a program. Gui is available in the heaptrack-gui package.

lcov

To see how many times a line was executed in the program. Must compile with --coverage flag.

g++ a.cpp -o a.out --coverage
./a.out # generates coverage file , .gcda
lcov --capture --directory . --output-file coverage.info # Capture coverage data from curr and sub dirs
genhtml coverage.info --output-directory out # generate html report
google-chrome out/index.html # open the html repor

Google Benchmark

Google Benchmark is a microbenchmark library for C++. Test performance of functions.

./matrices_benchmark --benchmark_filter=BM_MatrixMul # run only the benchmark with the name BM_MatrixMul

Testing

Google Test

Setting up GTest with CMake

Configure CMakeLists.txt to use GTest.

include(FetchContent)
FetchContent_Declare(
  googletest
  GIT_REPOSITORY https://github.com/google/googletest.git
  GIT_TAG v1.15.2
)
# For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)

enable_testing() # set at the top level CMakeLists.txt to generate CTestTestfile.cmake in correct directory

To build tests, add the following to CMakeLists.txt.

# first test
add_executable(test_matrix test_matrix.cpp)
target_link_libraries(test_matrix PRIVATE matrices GTest::gtest_main)
# second test

include(GoogleTest)
gtest_discover_tests(test_matrix)

To run the tests, use the following command.

ctest --test-dir build/debug/tests # the location of CTestTestfile.cmake
ctest --preset=debug -V # run all tests

Documentation

Detailed documentation is available in the docs directory.

References

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published