diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt index fff4edf5..55997163 100644 --- a/example/CMakeLists.txt +++ b/example/CMakeLists.txt @@ -36,6 +36,8 @@ add_metall_executable(allocator_aware_type allocator_aware_type.cpp) add_metall_executable(logger logger.cpp) +add_metall_executable(concurrent concurrent.cpp) + if (BUILD_C) add_c_executable(c_api c_api.c) target_link_libraries(c_api PRIVATE metall_c) diff --git a/example/concurrent.cpp b/example/concurrent.cpp new file mode 100644 index 00000000..765499f2 --- /dev/null +++ b/example/concurrent.cpp @@ -0,0 +1,47 @@ +// Copyright 2023 Lawrence Livermore National Security, LLC and other Metall +// Project Developers. See the top-level COPYRIGHT file for details. +// +// SPDX-License-Identifier: (Apache-2.0 OR MIT) + +/// \file concurrent.cpp +/// \brief This example demonstrates Metall's concurrency support. +/// Metall can be used in a multi-threaded environment. +/// Please see the API documentation of the manager class to find out which +/// functions are thread-safe. + +#include +#include + +#include + +void metall_alloc(metall::manager& manager, const int tid) { + for (int i = 0; i < 10; ++i) { + if (tid % 2 == 0) { + manager.deallocate(manager.allocate(10)); + } else { + manager.destroy_ptr(manager.construct(metall::anonymous_instance)()); + } + } +} + +int main() { + { + metall::manager manager(metall::create_only, "/tmp/datastore"); + + { + std::thread t1(metall_alloc, std::ref(manager), 1); + std::thread t2(metall_alloc, std::ref(manager), 2); + std::thread t3(metall_alloc, std::ref(manager), 3); + std::thread t4(metall_alloc, std::ref(manager), 4); + + t1.join(); + t2.join(); + t3.join(); + t4.join(); + } + assert(manager.check_sanity()); + assert(manager.all_memory_deallocated()); + } + + return 0; +} \ No newline at end of file diff --git a/example/concurrent_map.cpp b/example/concurrent_map.cpp index bbaa527c..383f66e5 100644 --- a/example/concurrent_map.cpp +++ b/example/concurrent_map.cpp @@ -1,6 +1,8 @@ +// Copyright 2023 Lawrence Livermore National Security, LLC and other Metall +// Project Developers. See the top-level COPYRIGHT file for details. // -// Created by Iwabuchi, Keita on 9/25/20. -// +// SPDX-License-Identifier: (Apache-2.0 OR MIT) + #include #include diff --git a/scripts/sanitizer/run_sanitizers.sh b/scripts/sanitizer/run_sanitizers.sh new file mode 100644 index 00000000..1a6acf12 --- /dev/null +++ b/scripts/sanitizer/run_sanitizers.sh @@ -0,0 +1,69 @@ +#!/bin/bash + +# This script builds and runs example programs with various sanitizers. +# Usage example: +# cd metall # Metall root directory +# sh scripts/sanitizer/run_sanitizers.sh + +# Change the following variables to your own settings +CC=clang +CXX=clang++ +BOOST_ROOT="${HOME}/local/boost" +METALL_ROOT=".." + +LOG_FILE=out-sanitizer.log + +exec_cmd () { + echo "" >> ${LOG_FILE} + echo "------------------------------------------------------------" >> ${LOG_FILE} + echo "Command: " "$@" | tee -a ${LOG_FILE} + "$@" >> ${LOG_FILE} 2>&1 + echo "------------------------------------------------------------" >> ${LOG_FILE} + echo "" >> ${LOG_FILE} +} + +build () { + local target=$1 + exec_cmd ${CXX} -I ${BOOST_ROOT} -I ${METALL_ROOT}/include -std=c++17 ${SANITIZER_BUILD_ARGS} ${target} -DMETALL_DEFAULT_CAPACITY=$((1024*1024*500)) +} + +run_sanitizer () { + SANITIZER_BUILD_ARGS=$1 + +# build "${METALL_ROOT}/example/simple.cpp" +# ./a.out 2>&1 | tee -a ${LOG_FILE} +# rm -f a.out + + build "${METALL_ROOT}/example/concurrent.cpp" + echo "Running concurrent program" + ./a.out 2>&1 | tee -a ${LOG_FILE} + rm -f a.out +} + +main() { + mkdir build + cd build + rm -rf ${LOG_FILE} + + echo "" + echo "=====================================================================" + echo "Build with address sanitizer and leak sanitizer" + run_sanitizer "-fsanitize=address -fno-omit-frame-pointer -O1 -g" + + echo "" + echo "=====================================================================" + echo "Build with thread sanitizer" + run_sanitizer "-fsanitize=thread -fPIE -pie -O1 -g" + + echo "" + echo "=====================================================================" + echo "Build with memory sanitizer" + run_sanitizer "-fsanitize=memory -fsanitize-memory-track-origins -fPIE -pie -fno-omit-frame-pointer -g -O2" + + echo "" + echo "=====================================================================" + echo "Build with undefined behavior sanitizer" + run_sanitizer "-fsanitize=undefined -O1 -g" +} + +main "$@" \ No newline at end of file