C++20 compatibility (#6697)

Summary:
Based on https://github.com/facebook/rocksdb/issues/6648 (CLA Signed), but heavily modified / extended:

* Implicit capture of this via [=] deprecated in C++20, and [=,this] not standard before C++20 -> now using explicit capture lists
* Implicit copy operator deprecated in gcc 9 -> add explicit '= default' definition
* std::random_shuffle deprecated in C++17 and removed in C++20 -> migrated to a replacement in RocksDB random.h API
* Add the ability to build with different std version though -DCMAKE_CXX_STANDARD=11/14/17/20 on the cmake command line
* Minimal rebuild flag of MSVC is deprecated and is forbidden with /std:c++latest (C++20)
* Added MSVC 2019 C++11 & MSVC 2019 C++20 in AppVeyor
* Added GCC 9 C++11 & GCC9 C++20 in Travis
Pull Request resolved: https://github.com/facebook/rocksdb/pull/6697

Test Plan: make check and CI

Reviewed By: cheng-chang

Differential Revision: D21020318

Pulled By: pdillinger

fbshipit-source-id: 12311be5dbd8675a0e2c817f7ec50fa11c18ab91
main
Peter Dillinger 5 years ago committed by Facebook GitHub Bot
parent fe206f4f7c
commit 31da5e34c1
  1. 47
      .travis.yml
  2. 17
      CMakeLists.txt
  3. 17
      appveyor.yml
  4. 1
      build_tools/build_detect_platform
  5. 7
      cmake/modules/CxxFlags.cmake
  6. 3
      db/db_bloom_filter_test.cc
  7. 2
      db/db_compaction_test.cc
  8. 2
      db/db_dynamic_level_test.cc
  9. 6
      db/db_test.cc
  10. 2
      db/log_reader.cc
  11. 2
      db/memtable.cc
  12. 4
      db/perf_context_test.cc
  13. 2
      db/write_batch.cc
  14. 2
      include/rocksdb/file_system.h
  15. 9
      memory/concurrent_arena.h
  16. 5
      memtable/memtablerep_bench.cc
  17. 0
      port/lang.h
  18. 7
      table/block_based/block_based_table_builder.cc
  19. 2
      table/block_based/block_based_table_reader.cc
  20. 2
      test_util/fault_injection_test_fs.cc
  21. 4
      test_util/transaction_test_util.cc
  22. 5
      third-party/folly/folly/synchronization/Baton.h
  23. 5
      tools/block_cache_analyzer/block_cache_trace_analyzer.cc
  24. 5
      tools/db_bench_tool.cc
  25. 2
      util/crc32c.cc
  26. 4
      util/hash.cc
  27. 2
      util/murmurhash.cc
  28. 14
      util/random.h
  29. 2
      util/xxhash.cc
  30. 4
      utilities/blob_db/blob_db_impl.cc
  31. 5
      utilities/transactions/write_unprepared_transaction_test.cc

@ -50,12 +50,18 @@ env:
- JOB_NAME=examples # 5-7 minutes - JOB_NAME=examples # 5-7 minutes
- JOB_NAME=cmake # 3-5 minutes - JOB_NAME=cmake # 3-5 minutes
- JOB_NAME=cmake-gcc8 # 3-5 minutes - JOB_NAME=cmake-gcc8 # 3-5 minutes
- JOB_NAME=cmake-gcc9 # 3-5 minutes
- JOB_NAME=cmake-gcc9-c++20 # 3-5 minutes
- JOB_NAME=cmake-mingw # 3 minutes - JOB_NAME=cmake-mingw # 3 minutes
matrix: matrix:
exclude: exclude:
- os: osx - os: osx
env: JOB_NAME=cmake-gcc8 env: JOB_NAME=cmake-gcc8
- os: osx
env: JOB_NAME=cmake-gcc9
- os: osx
env: JOB_NAME=cmake-gcc9-c++20
- os: osx - os: osx
env: JOB_NAME=cmake-mingw env: JOB_NAME=cmake-mingw
- os: osx - os: osx
@ -70,6 +76,19 @@ matrix:
env: JOB_NAME=cmake-mingw env: JOB_NAME=cmake-mingw
- os: linux - os: linux
compiler: clang compiler: clang
# Exclude all but most unique cmake variants for pull requests, but build all in branches
- if: type = pull_request
os : linux
arch: amd64
env: JOB_NAME=cmake
- if: type = pull_request
os : linux
arch: amd64
env: JOB_NAME=cmake-gcc8
- if: type = pull_request
os : linux
arch: amd64
env: JOB_NAME=cmake-gcc9
# Exclude most osx, arm64 and ppc64le tests for pull requests, but build in branches # Exclude most osx, arm64 and ppc64le tests for pull requests, but build in branches
# Temporarily disable ppc64le unit tests in PRs until Travis gets its act together (#6653) # Temporarily disable ppc64le unit tests in PRs until Travis gets its act together (#6653)
- if: type = pull_request - if: type = pull_request
@ -162,6 +181,22 @@ matrix:
os: linux os: linux
arch: ppc64le arch: ppc64le
env: JOB_NAME=cmake-gcc8 env: JOB_NAME=cmake-gcc8
- if: type = pull_request
os : linux
arch: arm64
env: JOB_NAME=cmake-gcc9
- if: type = pull_request
os: linux
arch: ppc64le
env: JOB_NAME=cmake-gcc9
- if: type = pull_request
os : linux
arch: arm64
env: JOB_NAME=cmake-gcc9-c++20
- if: type = pull_request
os: linux
arch: ppc64le
env: JOB_NAME=cmake-gcc9-c++20
install: install:
- if [ "${TRAVIS_OS_NAME}" == osx ]; then - if [ "${TRAVIS_OS_NAME}" == osx ]; then
@ -171,6 +206,10 @@ install:
sudo apt-get install -y g++-8; sudo apt-get install -y g++-8;
CC=gcc-8 && CXX=g++-8; CC=gcc-8 && CXX=g++-8;
fi fi
- if [ "${JOB_NAME}" == cmake-gcc9 ] || [ "${JOB_NAME}" == cmake-gcc9-c++20 ]; then
sudo apt-get install -y g++-9;
CC=gcc-9 && CXX=g++-9;
fi
- if [ "${JOB_NAME}" == cmake-mingw ]; then - if [ "${JOB_NAME}" == cmake-mingw ]; then
sudo apt-get install -y mingw-w64 ; sudo apt-get install -y mingw-w64 ;
fi fi
@ -240,7 +279,13 @@ script:
mkdir build && cd build && cmake -DJNI=1 -DWITH_GFLAGS=OFF .. -DCMAKE_C_COMPILER=x86_64-w64-mingw32-gcc -DCMAKE_CXX_COMPILER=x86_64-w64-mingw32-g++ -DCMAKE_SYSTEM_NAME=Windows && make -j4 rocksdb rocksdbjni mkdir build && cd build && cmake -DJNI=1 -DWITH_GFLAGS=OFF .. -DCMAKE_C_COMPILER=x86_64-w64-mingw32-gcc -DCMAKE_CXX_COMPILER=x86_64-w64-mingw32-g++ -DCMAKE_SYSTEM_NAME=Windows && make -j4 rocksdb rocksdbjni
;; ;;
cmake*) cmake*)
mkdir build && cd build && cmake -DJNI=1 .. -DCMAKE_BUILD_TYPE=Release && make -j4 rocksdb rocksdbjni case $JOB_NAME in
*-c++20)
OPT=-DCMAKE_CXX_STANDARD=20
;;
esac
mkdir build && cd build && cmake -DJNI=1 .. -DCMAKE_BUILD_TYPE=Release $OPT && make -j4 rocksdb rocksdbjni
;; ;;
esac esac
notifications: notifications:

@ -83,6 +83,10 @@ else()
option(WITH_FOLLY_DISTRIBUTED_MUTEX "build with folly::DistributedMutex" OFF) option(WITH_FOLLY_DISTRIBUTED_MUTEX "build with folly::DistributedMutex" OFF)
endif() endif()
if( NOT DEFINED CMAKE_CXX_STANDARD )
set(CMAKE_CXX_STANDARD 11)
endif()
include(CMakeDependentOption) include(CMakeDependentOption)
CMAKE_DEPENDENT_OPTION(WITH_GFLAGS "build with GFlags" ON CMAKE_DEPENDENT_OPTION(WITH_GFLAGS "build with GFlags" ON
"NOT MSVC;NOT MINGW" OFF) "NOT MSVC;NOT MINGW" OFF)
@ -191,7 +195,6 @@ else()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-format -fno-asynchronous-unwind-tables") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-format -fno-asynchronous-unwind-tables")
add_definitions(-D_POSIX_C_SOURCE=1) add_definitions(-D_POSIX_C_SOURCE=1)
endif() endif()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
if(NOT CMAKE_BUILD_TYPE STREQUAL "Debug") if(NOT CMAKE_BUILD_TYPE STREQUAL "Debug")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-omit-frame-pointer") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-omit-frame-pointer")
include(CheckCXXCompilerFlag) include(CheckCXXCompilerFlag)
@ -254,6 +257,7 @@ include(CheckCXXSourceCompiles)
if(NOT MSVC) if(NOT MSVC)
set(CMAKE_REQUIRED_FLAGS "-msse4.2 -mpclmul") set(CMAKE_REQUIRED_FLAGS "-msse4.2 -mpclmul")
endif() endif()
CHECK_CXX_SOURCE_COMPILES(" CHECK_CXX_SOURCE_COMPILES("
#include <cstdint> #include <cstdint>
#include <nmmintrin.h> #include <nmmintrin.h>
@ -386,7 +390,15 @@ if(MSVC)
message(STATUS "Debug optimization is enabled") message(STATUS "Debug optimization is enabled")
set(CMAKE_CXX_FLAGS_DEBUG "/Oxt") set(CMAKE_CXX_FLAGS_DEBUG "/Oxt")
else() else()
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /Od /RTC1 /Gm") set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /Od /RTC1")
# Minimal Build is deprecated after MSVC 2015
if( MSVC_VERSION GREATER 1900 )
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /Gm-")
else()
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /Gm")
endif()
endif() endif()
if(WITH_RUNTIME_DEBUG) if(WITH_RUNTIME_DEBUG)
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /${RUNTIME_LIBRARY}d") set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /${RUNTIME_LIBRARY}d")
@ -849,7 +861,6 @@ if(ROCKSDB_BUILD_SHARED)
LINKER_LANGUAGE CXX LINKER_LANGUAGE CXX
VERSION ${rocksdb_VERSION} VERSION ${rocksdb_VERSION}
SOVERSION ${rocksdb_VERSION_MAJOR} SOVERSION ${rocksdb_VERSION_MAJOR}
CXX_STANDARD 11
OUTPUT_NAME "rocksdb") OUTPUT_NAME "rocksdb")
endif() endif()
endif() endif()

@ -1,6 +1,6 @@
version: 1.0.{build} version: 1.0.{build}
image: Visual Studio 2017 image: Visual Studio 2019
environment: environment:
JAVA_HOME: C:\Program Files\Java\jdk1.8.0 JAVA_HOME: C:\Program Files\Java\jdk1.8.0
@ -24,6 +24,15 @@ environment:
- APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2017 - APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2017
CMAKE_GENERATOR: Visual Studio 15 Win64 CMAKE_GENERATOR: Visual Studio 15 Win64
DEV_ENV: C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\devenv.com DEV_ENV: C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\devenv.com
- APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2019
CMAKE_GENERATOR: Visual Studio 16
CMAKE_PLATEFORM_NAME: x64
DEV_ENV: C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\IDE\devenv.com
- APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2019
CMAKE_GENERATOR: Visual Studio 16
CMAKE_PLATEFORM_NAME: x64
CMAKE_OPT: -DCMAKE_CXX_STANDARD=20
DEV_ENV: C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\IDE\devenv.com
install: install:
- md %THIRDPARTY_HOME% - md %THIRDPARTY_HOME%
@ -34,7 +43,8 @@ install:
- cd snappy-1.1.7 - cd snappy-1.1.7
- mkdir build - mkdir build
- cd build - cd build
- cmake -G "%CMAKE_GENERATOR%" .. - if DEFINED CMAKE_PLATEFORM_NAME (set "PLATEFORM_OPT=-A %CMAKE_PLATEFORM_NAME%")
- cmake .. -G "%CMAKE_GENERATOR%" %PLATEFORM_OPT%
- msbuild Snappy.sln /p:Configuration=Debug /p:Platform=x64 - msbuild Snappy.sln /p:Configuration=Debug /p:Platform=x64
- msbuild Snappy.sln /p:Configuration=Release /p:Platform=x64 - msbuild Snappy.sln /p:Configuration=Release /p:Platform=x64
- echo "Building LZ4 dependency..." - echo "Building LZ4 dependency..."
@ -57,7 +67,8 @@ install:
before_build: before_build:
- md %APPVEYOR_BUILD_FOLDER%\build - md %APPVEYOR_BUILD_FOLDER%\build
- cd %APPVEYOR_BUILD_FOLDER%\build - cd %APPVEYOR_BUILD_FOLDER%\build
- cmake -G "%CMAKE_GENERATOR%" -DCMAKE_BUILD_TYPE=Debug -DOPTDBG=1 -DPORTABLE=1 -DSNAPPY=1 -DLZ4=1 -DZSTD=1 -DXPRESS=1 -DJNI=1 .. - if DEFINED CMAKE_PLATEFORM_NAME (set "PLATEFORM_OPT=-A %CMAKE_PLATEFORM_NAME%")
- cmake .. -G "%CMAKE_GENERATOR%" %PLATEFORM_OPT% %CMAKE_OPT% -DCMAKE_BUILD_TYPE=Debug -DOPTDBG=1 -DPORTABLE=1 -DSNAPPY=1 -DLZ4=1 -DZSTD=1 -DXPRESS=1 -DJNI=1
- cd .. - cd ..
build: build:

@ -705,6 +705,7 @@ EOF
fi fi
fi fi
if [ "$FBCODE_BUILD" != "true" -a "$PLATFORM" = OS_LINUX ]; then if [ "$FBCODE_BUILD" != "true" -a "$PLATFORM" = OS_LINUX ]; then
$CXX $COMMON_FLAGS $PLATFORM_SHARED_CFLAGS -x c++ -c - -o test_dl.o 2>/dev/null <<EOF $CXX $COMMON_FLAGS $PLATFORM_SHARED_CFLAGS -x c++ -c - -o test_dl.o 2>/dev/null <<EOF
void dummy_func() {} void dummy_func() {}

@ -0,0 +1,7 @@
macro(get_cxx_std_flags FLAGS_VARIABLE)
if( CMAKE_CXX_STANDARD_REQUIRED )
set(${FLAGS_VARIABLE} ${CMAKE_CXX${CMAKE_CXX_STANDARD}_STANDARD_COMPILE_OPTION})
else()
set(${FLAGS_VARIABLE} ${CMAKE_CXX${CMAKE_CXX_STANDARD}_EXTENSION_COMPILE_OPTION})
endif()
endmacro()

@ -1343,8 +1343,7 @@ TEST_F(DBBloomFilterTest, OptimizeFiltersForHits) {
for (int i = 0; i < numkeys; i += 2) { for (int i = 0; i < numkeys; i += 2) {
keys.push_back(i); keys.push_back(i);
} }
std::random_shuffle(std::begin(keys), std::end(keys)); RandomShuffle(std::begin(keys), std::end(keys));
int num_inserted = 0; int num_inserted = 0;
for (int key : keys) { for (int key : keys) {
ASSERT_OK(Put(1, Key(key), "val")); ASSERT_OK(Put(1, Key(key), "val"));

@ -4762,7 +4762,7 @@ TEST_P(CompactionPriTest, Test) {
for (int i = 0; i < kNKeys; i++) { for (int i = 0; i < kNKeys; i++) {
keys[i] = i; keys[i] = i;
} }
std::random_shuffle(std::begin(keys), std::end(keys)); RandomShuffle(std::begin(keys), std::end(keys), rnd.Next());
for (int i = 0; i < kNKeys; i++) { for (int i = 0; i < kNKeys; i++) {
ASSERT_OK(Put(Key(keys[i]), RandomString(&rnd, 102))); ASSERT_OK(Put(Key(keys[i]), RandomString(&rnd, 102)));

@ -50,7 +50,7 @@ TEST_F(DBTestDynamicLevel, DynamicLevelMaxBytesBase) {
keys[i] = i; keys[i] = i;
} }
if (ordered_insert == 0) { if (ordered_insert == 0) {
std::random_shuffle(std::begin(keys), std::end(keys)); RandomShuffle(std::begin(keys), std::end(keys), rnd.Next());
} }
for (int max_background_compactions = 1; max_background_compactions < 4; for (int max_background_compactions = 1; max_background_compactions < 4;
max_background_compactions += 2) { max_background_compactions += 2) {

@ -1421,7 +1421,7 @@ TEST_F(DBTest, ApproximateSizesMemTable) {
keys[i * 3 + 1] = i * 5 + 1; keys[i * 3 + 1] = i * 5 + 1;
keys[i * 3 + 2] = i * 5 + 2; keys[i * 3 + 2] = i * 5 + 2;
} }
std::random_shuffle(std::begin(keys), std::end(keys)); RandomShuffle(std::begin(keys), std::end(keys));
for (int i = 0; i < N * 3; i++) { for (int i = 0; i < N * 3; i++) {
ASSERT_OK(Put(Key(keys[i] + 1000), RandomString(&rnd, 1024))); ASSERT_OK(Put(Key(keys[i] + 1000), RandomString(&rnd, 1024)));
@ -4530,7 +4530,7 @@ TEST_F(DBTest, DynamicLevelCompressionPerLevel) {
for (int i = 0; i < kNKeys; i++) { for (int i = 0; i < kNKeys; i++) {
keys[i] = i; keys[i] = i;
} }
std::random_shuffle(std::begin(keys), std::end(keys)); RandomShuffle(std::begin(keys), std::end(keys));
Random rnd(301); Random rnd(301);
Options options; Options options;
@ -4613,7 +4613,7 @@ TEST_F(DBTest, DynamicLevelCompressionPerLevel2) {
for (int i = 0; i < kNKeys; i++) { for (int i = 0; i < kNKeys; i++) {
keys[i] = i; keys[i] = i;
} }
std::random_shuffle(std::begin(keys), std::end(keys)); RandomShuffle(std::begin(keys), std::end(keys));
Random rnd(301); Random rnd(301);
Options options; Options options;

@ -11,11 +11,11 @@
#include <stdio.h> #include <stdio.h>
#include "file/sequence_file_reader.h" #include "file/sequence_file_reader.h"
#include "port/lang.h"
#include "rocksdb/env.h" #include "rocksdb/env.h"
#include "test_util/sync_point.h" #include "test_util/sync_point.h"
#include "util/coding.h" #include "util/coding.h"
#include "util/crc32c.h" #include "util/crc32c.h"
#include "util/util.h"
namespace ROCKSDB_NAMESPACE { namespace ROCKSDB_NAMESPACE {
namespace log { namespace log {

@ -23,6 +23,7 @@
#include "memory/memory_usage.h" #include "memory/memory_usage.h"
#include "monitoring/perf_context_imp.h" #include "monitoring/perf_context_imp.h"
#include "monitoring/statistics.h" #include "monitoring/statistics.h"
#include "port/lang.h"
#include "port/port.h" #include "port/port.h"
#include "rocksdb/comparator.h" #include "rocksdb/comparator.h"
#include "rocksdb/env.h" #include "rocksdb/env.h"
@ -36,7 +37,6 @@
#include "util/autovector.h" #include "util/autovector.h"
#include "util/coding.h" #include "util/coding.h"
#include "util/mutexlock.h" #include "util/mutexlock.h"
#include "util/util.h"
namespace ROCKSDB_NAMESPACE { namespace ROCKSDB_NAMESPACE {

@ -251,7 +251,7 @@ void ProfileQueries(bool enabled_time = false) {
} }
if (FLAGS_random_key) { if (FLAGS_random_key) {
std::random_shuffle(keys.begin(), keys.end()); RandomShuffle(std::begin(keys), std::end(keys));
} }
#ifndef NDEBUG #ifndef NDEBUG
ThreadStatusUtil::TEST_SetStateDelay(ThreadStatus::STATE_MUTEX_WAIT, 1U); ThreadStatusUtil::TEST_SetStateDelay(ThreadStatus::STATE_MUTEX_WAIT, 1U);
@ -524,7 +524,7 @@ TEST_F(PerfContextTest, SeekKeyComparison) {
} }
if (FLAGS_random_key) { if (FLAGS_random_key) {
std::random_shuffle(keys.begin(), keys.end()); RandomShuffle(std::begin(keys), std::end(keys));
} }
HistogramImpl hist_put_time; HistogramImpl hist_put_time;

@ -53,13 +53,13 @@
#include "db/write_batch_internal.h" #include "db/write_batch_internal.h"
#include "monitoring/perf_context_imp.h" #include "monitoring/perf_context_imp.h"
#include "monitoring/statistics.h" #include "monitoring/statistics.h"
#include "port/lang.h"
#include "rocksdb/merge_operator.h" #include "rocksdb/merge_operator.h"
#include "util/autovector.h" #include "util/autovector.h"
#include "util/cast_util.h" #include "util/cast_util.h"
#include "util/coding.h" #include "util/coding.h"
#include "util/duplicate_detector.h" #include "util/duplicate_detector.h"
#include "util/string_util.h" #include "util/string_util.h"
#include "util/util.h"
namespace ROCKSDB_NAMESPACE { namespace ROCKSDB_NAMESPACE {

@ -105,6 +105,8 @@ struct FileOptions : EnvOptions {
FileOptions(const FileOptions& opts) FileOptions(const FileOptions& opts)
: EnvOptions(opts), io_options(opts.io_options) {} : EnvOptions(opts), io_options(opts.io_options) {}
FileOptions& operator=(const FileOptions& opts) = default;
}; };
// A structure to pass back some debugging information from the FileSystem // A structure to pass back some debugging information from the FileSystem

@ -13,6 +13,7 @@
#include <utility> #include <utility>
#include "memory/allocator.h" #include "memory/allocator.h"
#include "memory/arena.h" #include "memory/arena.h"
#include "port/lang.h"
#include "port/likely.h" #include "port/likely.h"
#include "util/core_local.h" #include "util/core_local.h"
#include "util/mutexlock.h" #include "util/mutexlock.h"
@ -49,7 +50,7 @@ class ConcurrentArena : public Allocator {
char* Allocate(size_t bytes) override { char* Allocate(size_t bytes) override {
return AllocateImpl(bytes, false /*force_arena*/, return AllocateImpl(bytes, false /*force_arena*/,
[=]() { return arena_.Allocate(bytes); }); [this, bytes]() { return arena_.Allocate(bytes); });
} }
char* AllocateAligned(size_t bytes, size_t huge_page_size = 0, char* AllocateAligned(size_t bytes, size_t huge_page_size = 0,
@ -58,8 +59,10 @@ class ConcurrentArena : public Allocator {
assert(rounded_up >= bytes && rounded_up < bytes + sizeof(void*) && assert(rounded_up >= bytes && rounded_up < bytes + sizeof(void*) &&
(rounded_up % sizeof(void*)) == 0); (rounded_up % sizeof(void*)) == 0);
return AllocateImpl(rounded_up, huge_page_size != 0 /*force_arena*/, [=]() { return AllocateImpl(rounded_up, huge_page_size != 0 /*force_arena*/,
return arena_.AllocateAligned(rounded_up, huge_page_size, logger); [this, rounded_up, huge_page_size, logger]() {
return arena_.AllocateAligned(rounded_up,
huge_page_size, logger);
}); });
} }

@ -170,9 +170,8 @@ class KeyGenerator {
for (uint64_t i = 0; i < num_; ++i) { for (uint64_t i = 0; i < num_; ++i) {
values_[i] = i; values_[i] = i;
} }
std::shuffle( RandomShuffle(values_.begin(), values_.end(),
values_.begin(), values_.end(), static_cast<uint32_t>(FLAGS_seed));
std::default_random_engine(static_cast<unsigned int>(FLAGS_seed)));
} }
} }

@ -21,6 +21,7 @@
#include "db/dbformat.h" #include "db/dbformat.h"
#include "index_builder.h" #include "index_builder.h"
#include "port/lang.h"
#include "rocksdb/cache.h" #include "rocksdb/cache.h"
#include "rocksdb/comparator.h" #include "rocksdb/comparator.h"
@ -661,13 +662,13 @@ BlockBasedTableBuilder::BlockBasedTableBuilder(
rep_->pc_rep->compress_thread_pool.reserve( rep_->pc_rep->compress_thread_pool.reserve(
rep_->compression_opts.parallel_threads); rep_->compression_opts.parallel_threads);
for (uint32_t i = 0; i < rep_->compression_opts.parallel_threads; i++) { for (uint32_t i = 0; i < rep_->compression_opts.parallel_threads; i++) {
rep_->pc_rep->compress_thread_pool.emplace_back([=] { rep_->pc_rep->compress_thread_pool.emplace_back([this, i] {
BGWorkCompression(*(rep_->compression_ctxs[i]), BGWorkCompression(*(rep_->compression_ctxs[i]),
rep_->verify_ctxs[i].get()); rep_->verify_ctxs[i].get());
}); });
} }
rep_->pc_rep->write_thread.reset( rep_->pc_rep->write_thread.reset(
new port::Thread([=] { BGWorkWriteRawBlock(); })); new port::Thread([this] { BGWorkWriteRawBlock(); }));
} }
} }
@ -832,7 +833,7 @@ void BlockBasedTableBuilder::Flush() {
if (first_block) { if (first_block) {
std::unique_lock<std::mutex> lock(r->pc_rep->first_block_mutex); std::unique_lock<std::mutex> lock(r->pc_rep->first_block_mutex);
r->pc_rep->first_block_cond.wait(lock, r->pc_rep->first_block_cond.wait(lock,
[=] { return !r->pc_rep->first_block; }); [r] { return !r->pc_rep->first_block; });
} }
} else { } else {
WriteBlock(&r->data_block, &r->pending_handle, true /* is_data_block */); WriteBlock(&r->data_block, &r->pending_handle, true /* is_data_block */);

@ -53,12 +53,12 @@
#include "table/two_level_iterator.h" #include "table/two_level_iterator.h"
#include "monitoring/perf_context_imp.h" #include "monitoring/perf_context_imp.h"
#include "port/lang.h"
#include "test_util/sync_point.h" #include "test_util/sync_point.h"
#include "util/coding.h" #include "util/coding.h"
#include "util/crc32c.h" #include "util/crc32c.h"
#include "util/stop_watch.h" #include "util/stop_watch.h"
#include "util/string_util.h" #include "util/string_util.h"
#include "util/util.h"
#include "util/xxhash.h" #include "util/xxhash.h"
namespace ROCKSDB_NAMESPACE { namespace ROCKSDB_NAMESPACE {

@ -17,8 +17,8 @@
#include "test_util/fault_injection_test_fs.h" #include "test_util/fault_injection_test_fs.h"
#include <functional> #include <functional>
#include <utility> #include <utility>
#include "port/lang.h"
#include "port/stack_trace.h" #include "port/stack_trace.h"
#include "util/util.h"
namespace ROCKSDB_NAMESPACE { namespace ROCKSDB_NAMESPACE {

@ -139,7 +139,7 @@ bool RandomTransactionInserter::DoInsert(DB* db, Transaction* txn,
std::vector<uint16_t> set_vec(num_sets_); std::vector<uint16_t> set_vec(num_sets_);
std::iota(set_vec.begin(), set_vec.end(), static_cast<uint16_t>(0)); std::iota(set_vec.begin(), set_vec.end(), static_cast<uint16_t>(0));
std::shuffle(set_vec.begin(), set_vec.end(), std::random_device{}); RandomShuffle(set_vec.begin(), set_vec.end());
// For each set, pick a key at random and increment it // For each set, pick a key at random and increment it
for (uint16_t set_i : set_vec) { for (uint16_t set_i : set_vec) {
@ -296,7 +296,7 @@ Status RandomTransactionInserter::Verify(DB* db, uint16_t num_sets,
std::vector<uint16_t> set_vec(num_sets); std::vector<uint16_t> set_vec(num_sets);
std::iota(set_vec.begin(), set_vec.end(), static_cast<uint16_t>(0)); std::iota(set_vec.begin(), set_vec.end(), static_cast<uint16_t>(0));
std::shuffle(set_vec.begin(), set_vec.end(), std::random_device{}); RandomShuffle(set_vec.begin(), set_vec.end());
// For each set of keys with the same prefix, sum all the values // For each set of keys with the same prefix, sum all the values
for (uint16_t set_i : set_vec) { for (uint16_t set_i : set_vec) {

@ -249,7 +249,8 @@ class Baton {
bool tryWaitSlow( bool tryWaitSlow(
const std::chrono::time_point<Clock, Duration>& deadline, const std::chrono::time_point<Clock, Duration>& deadline,
const WaitOptions& opt) noexcept { const WaitOptions& opt) noexcept {
switch (detail::spin_pause_until(deadline, opt, [=] { return ready(); })) { switch (
detail::spin_pause_until(deadline, opt, [this] { return ready(); })) {
case detail::spin_result::success: case detail::spin_result::success:
return true; return true;
case detail::spin_result::timeout: case detail::spin_result::timeout:
@ -259,7 +260,7 @@ class Baton {
} }
if (!MayBlock) { if (!MayBlock) {
switch (detail::spin_yield_until(deadline, [=] { return ready(); })) { switch (detail::spin_yield_until(deadline, [this] { return ready(); })) {
case detail::spin_result::success: case detail::spin_result::success:
return true; return true;
case detail::spin_result::timeout: case detail::spin_result::timeout:

@ -578,7 +578,7 @@ void BlockCacheTraceAnalyzer::WriteSkewness(
} }
// Sort in descending order. // Sort in descending order.
sort(pairs.begin(), pairs.end(), sort(pairs.begin(), pairs.end(),
[=](const std::pair<std::string, uint64_t>& a, [](const std::pair<std::string, uint64_t>& a,
const std::pair<std::string, uint64_t>& b) { const std::pair<std::string, uint64_t>& b) {
return b.second < a.second; return b.second < a.second;
}); });
@ -652,7 +652,6 @@ void BlockCacheTraceAnalyzer::WriteCorrelationFeaturesToFile(
const std::map<std::string, Features>& label_features, const std::map<std::string, Features>& label_features,
const std::map<std::string, Predictions>& label_predictions, const std::map<std::string, Predictions>& label_predictions,
uint32_t max_number_of_values) const { uint32_t max_number_of_values) const {
std::default_random_engine rand_engine(static_cast<std::default_random_engine::result_type>(env_->NowMicros()));
for (auto const& label_feature_vectors : label_features) { for (auto const& label_feature_vectors : label_features) {
const Features& past = label_feature_vectors.second; const Features& past = label_feature_vectors.second;
auto it = label_predictions.find(label_feature_vectors.first); auto it = label_predictions.find(label_feature_vectors.first);
@ -674,7 +673,7 @@ void BlockCacheTraceAnalyzer::WriteCorrelationFeaturesToFile(
for (uint32_t i = 0; i < past.num_accesses_since_last_access.size(); i++) { for (uint32_t i = 0; i < past.num_accesses_since_last_access.size(); i++) {
indexes.push_back(i); indexes.push_back(i);
} }
std::shuffle(indexes.begin(), indexes.end(), rand_engine); RandomShuffle(indexes.begin(), indexes.end());
for (uint32_t i = 0; i < max_number_of_values && i < indexes.size(); i++) { for (uint32_t i = 0; i < max_number_of_values && i < indexes.size(); i++) {
uint32_t rand_index = indexes[i]; uint32_t rand_index = indexes[i];
out << std::to_string(past.num_accesses_since_last_access[rand_index]) out << std::to_string(past.num_accesses_since_last_access[rand_index])

@ -4313,9 +4313,8 @@ class Benchmark {
for (uint64_t i = 0; i < num_; ++i) { for (uint64_t i = 0; i < num_; ++i) {
values_[i] = i; values_[i] = i;
} }
std::shuffle( RandomShuffle(values_.begin(), values_.end(),
values_.begin(), values_.end(), static_cast<uint32_t>(FLAGS_seed));
std::default_random_engine(static_cast<unsigned int>(FLAGS_seed)));
} }
} }

@ -15,8 +15,8 @@
#include <nmmintrin.h> #include <nmmintrin.h>
#include <wmmintrin.h> #include <wmmintrin.h>
#endif #endif
#include "port/lang.h"
#include "util/coding.h" #include "util/coding.h"
#include "util/util.h"
#include "util/crc32c_arm64.h" #include "util/crc32c_arm64.h"

@ -7,10 +7,10 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. See the AUTHORS file for names of contributors. // found in the LICENSE file. See the AUTHORS file for names of contributors.
#include "util/hash.h"
#include <string.h> #include <string.h>
#include "port/lang.h"
#include "util/coding.h" #include "util/coding.h"
#include "util/hash.h"
#include "util/util.h"
#include "util/xxhash.h" #include "util/xxhash.h"
namespace ROCKSDB_NAMESPACE { namespace ROCKSDB_NAMESPACE {

@ -10,7 +10,7 @@
is under the MIT license. is under the MIT license.
*/ */
#include "murmurhash.h" #include "murmurhash.h"
#include "util/util.h" #include "port/lang.h"
#if defined(__x86_64__) #if defined(__x86_64__)

@ -9,6 +9,7 @@
#pragma once #pragma once
#include <stdint.h> #include <stdint.h>
#include <algorithm>
#include <random> #include <random>
#include "rocksdb/rocksdb_namespace.h" #include "rocksdb/rocksdb_namespace.h"
@ -163,4 +164,17 @@ class Random64 {
} }
}; };
// A seeded replacement for removed std::random_shuffle
template <class RandomIt>
void RandomShuffle(RandomIt first, RandomIt last, uint32_t seed) {
std::mt19937 rng(seed);
std::shuffle(first, last, rng);
}
// A replacement for removed std::random_shuffle
template <class RandomIt>
void RandomShuffle(RandomIt first, RandomIt last) {
RandomShuffle(first, last, std::random_device{}());
}
} // namespace ROCKSDB_NAMESPACE } // namespace ROCKSDB_NAMESPACE

@ -126,7 +126,7 @@ static void* XXH_memcpy(void* dest, const void* src, size_t size) { return memcp
#include "xxhash.h" #include "xxhash.h"
/* BEGIN RocksDB customizations */ /* BEGIN RocksDB customizations */
#include "util/util.h" /* for FALLTHROUGH_INTENDED, inserted as appropriate */ #include "port/lang.h" /* for FALLTHROUGH_INTENDED, inserted as appropriate */
/* END RocksDB customizations */ /* END RocksDB customizations */
/* ************************************* /* *************************************

@ -622,7 +622,7 @@ void BlobDBImpl::MarkUnreferencedBlobFilesObsolete() {
const SequenceNumber obsolete_seq = GetLatestSequenceNumber(); const SequenceNumber obsolete_seq = GetLatestSequenceNumber();
MarkUnreferencedBlobFilesObsoleteImpl( MarkUnreferencedBlobFilesObsoleteImpl(
[=](const std::shared_ptr<BlobFile>& blob_file) { [this, obsolete_seq](const std::shared_ptr<BlobFile>& blob_file) {
WriteLock file_lock(&blob_file->mutex_); WriteLock file_lock(&blob_file->mutex_);
return MarkBlobFileObsoleteIfNeeded(blob_file, obsolete_seq); return MarkBlobFileObsoleteIfNeeded(blob_file, obsolete_seq);
}); });
@ -630,7 +630,7 @@ void BlobDBImpl::MarkUnreferencedBlobFilesObsolete() {
void BlobDBImpl::MarkUnreferencedBlobFilesObsoleteDuringOpen() { void BlobDBImpl::MarkUnreferencedBlobFilesObsoleteDuringOpen() {
MarkUnreferencedBlobFilesObsoleteImpl( MarkUnreferencedBlobFilesObsoleteImpl(
[=](const std::shared_ptr<BlobFile>& blob_file) { [this](const std::shared_ptr<BlobFile>& blob_file) {
return MarkBlobFileObsoleteIfNeeded(blob_file, /* obsolete_seq */ 0); return MarkBlobFileObsoleteIfNeeded(blob_file, /* obsolete_seq */ 0);
}); });
} }

@ -147,9 +147,6 @@ TEST_P(WriteUnpreparedStressTest, ReadYourOwnWriteStress) {
const uint32_t kNumThreads = 10; const uint32_t kNumThreads = 10;
const uint32_t kNumKeys = 5; const uint32_t kNumKeys = 5;
std::default_random_engine rand(static_cast<uint32_t>(
std::hash<std::thread::id>()(std::this_thread::get_id())));
// Test with // Test with
// 1. no snapshots set // 1. no snapshots set
// 2. snapshot set on ReadOptions // 2. snapshot set on ReadOptions
@ -164,7 +161,7 @@ TEST_P(WriteUnpreparedStressTest, ReadYourOwnWriteStress) {
for (uint32_t k = 0; k < kNumKeys * kNumThreads; k++) { for (uint32_t k = 0; k < kNumKeys * kNumThreads; k++) {
keys.push_back("k" + ToString(k)); keys.push_back("k" + ToString(k));
} }
std::shuffle(keys.begin(), keys.end(), rand); RandomShuffle(keys.begin(), keys.end());
// This counter will act as a "sequence number" to help us validate // This counter will act as a "sequence number" to help us validate
// visibility logic with snapshots. If we had direct access to the seqno of // visibility logic with snapshots. If we had direct access to the seqno of

Loading…
Cancel
Save