From f5d9ed02c839f67d13ea846b88fc5fc8b250ac5a Mon Sep 17 00:00:00 2001 From: Pavel Tsayukov Date: Mon, 2 Sep 2024 14:53:27 +0300 Subject: [PATCH 1/2] Add benchmark-clobber library (#296) --- src/library/CMakeLists.txt | 4 ++++ src/library/benchmark/CMakeLists.txt | 1 + src/library/benchmark/clobber.h | 26 ++++++++++++++++++++++++++ util/CMakeLists.txt | 18 +++++++++++++----- 4 files changed, 44 insertions(+), 5 deletions(-) create mode 100644 src/library/benchmark/CMakeLists.txt create mode 100644 src/library/benchmark/clobber.h diff --git a/src/library/CMakeLists.txt b/src/library/CMakeLists.txt index 96fed21fca..a1f8aeb527 100644 --- a/src/library/CMakeLists.txt +++ b/src/library/CMakeLists.txt @@ -1,3 +1,7 @@ +if (YDB_SDK_TESTS) + add_subdirectory(benchmark) +endif() + add_subdirectory(grpc/client) add_subdirectory(json_value) add_subdirectory(jwt) diff --git a/src/library/benchmark/CMakeLists.txt b/src/library/benchmark/CMakeLists.txt new file mode 100644 index 0000000000..90a15166bf --- /dev/null +++ b/src/library/benchmark/CMakeLists.txt @@ -0,0 +1 @@ +_ydb_sdk_add_library(benchmark-clobber INTERFACE) diff --git a/src/library/benchmark/clobber.h b/src/library/benchmark/clobber.h new file mode 100644 index 0000000000..f8ce28a3f1 --- /dev/null +++ b/src/library/benchmark/clobber.h @@ -0,0 +1,26 @@ +#pragma once + +#include + +namespace NBench { + +/** + * Functions that states "I can read and write everywhere in memory". + * + * Use it to prevent optimizer from reordering or discarding memory writes prior + * to it's call, and force memory reads after it's call. + */ +Y_FORCE_INLINE void Clobber() { +#if defined(__GNUC__) + asm volatile("" + : + : + : "memory"); +#elif defined(_MSC_VER) + _ReadWriteBarrier(); +#else + // Otherwise, do nothing +#endif +} + +} // namespace NBench diff --git a/util/CMakeLists.txt b/util/CMakeLists.txt index a4bddaca3f..994e5163a2 100644 --- a/util/CMakeLists.txt +++ b/util/CMakeLists.txt @@ -310,12 +310,7 @@ if (YDB_SDK_TESTS) system/thread_ut.cpp system/types_ut.cpp - - # TODO: add library/cpp/testing/benchmark - # depends only on NBench::Clobber, that's a memory optimization barrier - # system/unaligned_mem_ut.cpp system/user_ut.cpp - system/yassert_ut.cpp LINK_LIBRARIES yutil @@ -324,6 +319,19 @@ if (YDB_SDK_TESTS) unit ) + add_ydb_test(NAME util-system-unaligned_mem_ut + WORKING_DIRECTORY + ${CMAKE_CURRENT_BINARY_DIR}/system + SOURCES + system/unaligned_mem_ut.cpp + LINK_LIBRARIES + yutil + benchmark-clobber + cpp-testing-unittest_main + LABELS + unit + ) + add_ydb_test(NAME util-system-fstat_ut WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/system From 745637686a8e0fe6acff38a3fe2acc471bed9cdc Mon Sep 17 00:00:00 2001 From: Pavel Tsayukov Date: Mon, 2 Sep 2024 14:53:53 +0300 Subject: [PATCH 2/2] Replace include (#296) --- util/system/unaligned_mem_ut.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/util/system/unaligned_mem_ut.cpp b/util/system/unaligned_mem_ut.cpp index 9de3f3e931..ac135752aa 100644 --- a/util/system/unaligned_mem_ut.cpp +++ b/util/system/unaligned_mem_ut.cpp @@ -1,6 +1,6 @@ #include "unaligned_mem.h" -#include +#include #include #include