Skip to content

Commit

Permalink
Issue #296 Fix util/system/unaligned_mem_ut (#309)
Browse files Browse the repository at this point in the history
  • Loading branch information
tsayukov authored Sep 2, 2024
1 parent 1c413d2 commit e111838
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 6 deletions.
4 changes: 4 additions & 0 deletions src/library/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
if (YDB_SDK_TESTS)
add_subdirectory(benchmark)
endif()

add_subdirectory(grpc/client)
add_subdirectory(json_value)
add_subdirectory(jwt)
Expand Down
1 change: 1 addition & 0 deletions src/library/benchmark/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
_ydb_sdk_add_library(benchmark-clobber INTERFACE)
26 changes: 26 additions & 0 deletions src/library/benchmark/clobber.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#pragma once

#include <util/system/compiler.h>

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
18 changes: 13 additions & 5 deletions util/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion util/system/unaligned_mem_ut.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "unaligned_mem.h"

#include <library/cpp/testing/benchmark/bench.h>
#include <src/library/benchmark/clobber.h>
#include <library/cpp/testing/unittest/registar.h>

#include <util/system/compiler.h>
Expand Down

0 comments on commit e111838

Please sign in to comment.