From: Matty Williams Date: Wed, 15 Oct 2025 15:58:15 +0000 (+0100) Subject: test: Change random number generator to be complr independent X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=862d1de72847010c6f4d7b4afc24a7243a6ee8a3;p=ceph.git test: Change random number generator to be complr independent Changed from using ceph util random_number_generator class to using std::mt19937_64 as it is compiler independent. Fixes: https://tracker.ceph.com/issues/73553 Signed-off-by: Matty Williams --- diff --git a/src/common/io_exerciser/ObjectModel.cc b/src/common/io_exerciser/ObjectModel.cc index 7f155d122203..92cc8f9fdf6b 100644 --- a/src/common/io_exerciser/ObjectModel.cc +++ b/src/common/io_exerciser/ObjectModel.cc @@ -48,7 +48,8 @@ bool ObjectModel::readyForIoOp(IoOp& op) { return true; } void ObjectModel::applyIoOp(IoOp& op) { auto generate_random = [&rng = rng]() { - return rng(1, std::numeric_limits::max()); + std::uniform_int_distribution distMax(1, std::numeric_limits::max()); + return distMax(rng); }; auto verify_and_record_read_op = diff --git a/src/common/io_exerciser/ObjectModel.h b/src/common/io_exerciser/ObjectModel.h index e16658085b6e..f14bfd0c64b8 100644 --- a/src/common/io_exerciser/ObjectModel.h +++ b/src/common/io_exerciser/ObjectModel.h @@ -29,8 +29,8 @@ class ObjectModel : public Model { bool secondary_created; std::vector primary_contents; std::vector secondary_contents; - ceph::util::random_number_generator rng = - ceph::util::random_number_generator(); + std::mt19937_64 rng = + std::mt19937_64(); // Track read and write I/Os that can be submitted in // parallel to detect violations: diff --git a/src/test/osd/ceph_test_rados_io_sequence/ProgramOptionReader.h b/src/test/osd/ceph_test_rados_io_sequence/ProgramOptionReader.h index cf56134d447c..ebfdd1a31cda 100644 --- a/src/test/osd/ceph_test_rados_io_sequence/ProgramOptionReader.h +++ b/src/test/osd/ceph_test_rados_io_sequence/ProgramOptionReader.h @@ -2,6 +2,7 @@ #include #include +#include #include #include "include/ceph_assert.h" @@ -86,7 +87,7 @@ template & selections_array> class ProgramOptionSelector : public ProgramOptionReader { public: - ProgramOptionSelector(ceph::util::random_number_generator& rng, + ProgramOptionSelector(std::mt19937_64& rng, po::variables_map& vm, const std::string& option_name, bool select_first) @@ -105,12 +106,13 @@ class ProgramOptionSelector : public ProgramOptionReader { } else if (first_value.has_value()) { return *std::exchange(first_value, std::nullopt); } else { - return selections_array[rng(num_selections - 1)]; + std::uniform_int_distribution dist_selections(0, num_selections - 1); + return selections_array[dist_selections(rng)]; } } protected: - ceph::util::random_number_generator& rng; + std::mt19937_64& rng; std::optional first_value; }; @@ -124,7 +126,7 @@ template & elections_array_stable> class StableOptionSelector : public ProgramOptionReader { public: - StableOptionSelector(ceph::util::random_number_generator& rng, + StableOptionSelector(std::mt19937_64& rng, po::variables_map& vm, const std::string& option_name, bool select_first) @@ -150,14 +152,16 @@ public: } else if (first_value.has_value()) { return *std::exchange(first_value, std::nullopt); } else if (stable) { - return elections_array_stable[rng(num_selections_stable - 1)]; + std::uniform_int_distribution dist_selections_stable(0, num_selections_stable - 1); + return elections_array_stable[dist_selections_stable(rng)]; } else { - return selections_array[rng(num_selections - 1)]; + std::uniform_int_distribution dist_selections(0, num_selections - 1); + return selections_array[dist_selections(rng)]; } } protected: - ceph::util::random_number_generator& rng; + std::mt19937_64& rng; std::optional first_value; bool stable; }; @@ -166,7 +170,7 @@ template class ProgramOptionGeneratedSelector : public OptionalProgramOptionReader { public: - ProgramOptionGeneratedSelector(ceph::util::random_number_generator& rng, + ProgramOptionGeneratedSelector(std::mt19937_64& rng, po::variables_map& vm, const std::string& option_name, bool first_use) @@ -196,16 +200,18 @@ class ProgramOptionGeneratedSelector virtual const std::optional selectRandom() { std::vector selection = generate_selections(); - if (selection.size() > 0) - return selection[rng(selection.size() - 1)]; - else + if (selection.size() > 0) { + std::uniform_int_distribution dist_selection(0, selection.size() - 1); + return selection[dist_selection(rng)]; + } else { return std::nullopt; + } } bool is_first_use() { return first_use; } private: - ceph::util::random_number_generator& rng; + std::mt19937_64& rng; bool first_use; }; diff --git a/src/test/osd/ceph_test_rados_io_sequence/ceph_test_rados_io_sequence.cc b/src/test/osd/ceph_test_rados_io_sequence/ceph_test_rados_io_sequence.cc index 709f6e5e6932..0c932f1a90a4 100644 --- a/src/test/osd/ceph_test_rados_io_sequence/ceph_test_rados_io_sequence.cc +++ b/src/test/osd/ceph_test_rados_io_sequence/ceph_test_rados_io_sequence.cc @@ -2,6 +2,7 @@ #include #include +#include #include #include "common/Formatter.h" @@ -301,7 +302,7 @@ ceph::io_sequence::tester::SelectSeqRange::select() { } ceph::io_sequence::tester::SelectErasureTechnique::SelectErasureTechnique( - ceph::util::random_number_generator& rng, + std::mt19937_64& rng, po::variables_map& vm, std::string_view plugin, bool first_use) @@ -339,7 +340,7 @@ ceph::io_sequence::tester::SelectErasureTechnique::generate_selections() { } ceph::io_sequence::tester::lrc::SelectMappingAndLayers::SelectMappingAndLayers( - ceph::util::random_number_generator& rng, + std::mt19937_64& rng, po::variables_map& vm, bool first_use) : rng_seed(rng()), @@ -366,7 +367,7 @@ ceph::io_sequence::tester::lrc::SelectMappingAndLayers::select() { } ceph::io_sequence::tester::SelectErasureKM::SelectErasureKM( - ceph::util::random_number_generator& rng, + std::mt19937_64& rng, po::variables_map& vm, std::string_view plugin, const std::optional& technique, @@ -416,7 +417,7 @@ ceph::io_sequence::tester::SelectErasureKM::generate_selections() { } ceph::io_sequence::tester::jerasure::SelectErasureW::SelectErasureW( - ceph::util::random_number_generator& rng, + std::mt19937_64& rng, po::variables_map& vm, std::string_view plugin, const std::optional& technique, @@ -457,7 +458,7 @@ ceph::io_sequence::tester::jerasure::SelectErasureW::generate_selections() { } ceph::io_sequence::tester::shec::SelectErasureC::SelectErasureC( - ceph::util::random_number_generator& rng, + std::mt19937_64& rng, po::variables_map& vm, std::string_view plugin, const std::optional>& km, @@ -484,7 +485,7 @@ ceph::io_sequence::tester::shec::SelectErasureC::generate_selections() { } ceph::io_sequence::tester::jerasure::SelectErasurePacketSize:: - SelectErasurePacketSize(ceph::util::random_number_generator& rng, + SelectErasurePacketSize(std::mt19937_64& rng, po::variables_map& vm, std::string_view plugin, const std::optional& technique, @@ -532,7 +533,7 @@ const std::vector ceph::io_sequence::tester::jerasure:: } ceph::io_sequence::tester::SelectErasureChunkSize::SelectErasureChunkSize( - ceph::util::random_number_generator& rng, + std::mt19937_64& rng, po::variables_map& vm, ErasureCodeInterfaceRef ec_impl, bool first_use) @@ -552,19 +553,22 @@ ceph::io_sequence::tester::SelectErasureChunkSize::generate_selections() { if (4096 % minimum_chunksize == 0) { choices.push_back(4096); } else { - choices.push_back(minimum_chunksize * (rng(4) + 1)); + std::uniform_int_distribution dist4(0, 3); + choices.push_back(minimum_chunksize * (dist4(rng) + 1)); } if ((64 * 1024) % minimum_chunksize == 0) { choices.push_back(64 * 1024); } else { - choices.push_back(minimum_chunksize * (rng(64) + 1)); + std::uniform_int_distribution dist64(0, 63); + choices.push_back(minimum_chunksize * (dist64(rng) + 1)); } if ((256 * 1024) % minimum_chunksize == 0) { choices.push_back(256 * 1024); } else { - choices.push_back(minimum_chunksize * (rng(256) + 1)); + std::uniform_int_distribution dist256(0, 255); + choices.push_back(minimum_chunksize * (dist256(rng) + 1)); } return choices; @@ -572,7 +576,7 @@ ceph::io_sequence::tester::SelectErasureChunkSize::generate_selections() { ceph::io_sequence::tester::SelectErasureProfile::SelectErasureProfile( boost::intrusive_ptr cct, - ceph::util::random_number_generator& rng, + std::mt19937_64& rng, po::variables_map& vm, librados::Rados& rados, bool dry_run, @@ -795,7 +799,7 @@ ceph::io_sequence::tester::SelectErasureProfile::selectExistingProfile( ceph::io_sequence::tester::SelectErasurePool::SelectErasurePool( boost::intrusive_ptr cct, - ceph::util::random_number_generator& rng, + std::mt19937_64& rng, po::variables_map& vm, librados::Rados& rados, bool dry_run, @@ -1024,7 +1028,7 @@ ceph::io_sequence::tester::TestObject::TestObject( const std::string primary_oid, const std::string secondary_oid, librados::Rados& rados, boost::asio::io_context& asio, SelectBlockSize& sbs, SelectErasurePool& spo, SelectObjectSize& sos, SelectNumThreads& snt, SelectSeqRange& ssr, - ceph::util::random_number_generator& rng, ceph::mutex& lock, + std::mt19937_64& rng, ceph::mutex& lock, ceph::condition_variable& cond, bool dryrun, bool verbose, std::optional seqseed, bool testrecovery, bool checkconsistency, bool delete_objects) : rng(rng), verbose(verbose), seqseed(seqseed), testrecovery(testrecovery), checkconsistency(checkconsistency), @@ -1133,7 +1137,7 @@ ceph::io_sequence::tester::TestRunner::TestRunner( librados::Rados& rados) : rados(rados), seed(vm.contains("seed") ? vm["seed"].as() : time(nullptr)), - rng(ceph::util::random_number_generator(seed)), + rng(seed), sbs{rng, vm, "blocksize", true}, sos{rng, vm, "objectsize", true}, spo{cct, @@ -1316,6 +1320,7 @@ bool ceph::io_sequence::tester::TestRunner::run_interactive_test() { primary_object_name, secondary_object_name, sbs.select(), rng()); } else { const std::string pool = spo.select(); + dout(0) << "Pool name: " << pool << dendl; model = std::make_unique( rados, asio, pool, primary_object_name, secondary_object_name, sbs.select(), rng(), diff --git a/src/test/osd/ceph_test_rados_io_sequence/ceph_test_rados_io_sequence.h b/src/test/osd/ceph_test_rados_io_sequence/ceph_test_rados_io_sequence.h index a48564f5c6df..a0940d3a7c13 100644 --- a/src/test/osd/ceph_test_rados_io_sequence/ceph_test_rados_io_sequence.h +++ b/src/test/osd/ceph_test_rados_io_sequence/ceph_test_rados_io_sequence.h @@ -1,6 +1,7 @@ #include #include #include +#include #include #include @@ -161,7 +162,7 @@ using SelectErasurePlugin = class SelectErasureKM : public ProgramOptionGeneratedSelector> { public: - SelectErasureKM(ceph::util::random_number_generator& rng, + SelectErasureKM(std::mt19937_64& rng, po::variables_map& vm, std::string_view plugin, const std::optional& technique, @@ -170,7 +171,7 @@ class SelectErasureKM const std::vector> generate_selections() override; private: - ceph::util::random_number_generator& rng; + std::mt19937_64& rng; std::string_view plugin; std::optional technique; @@ -179,7 +180,7 @@ class SelectErasureKM namespace shec { class SelectErasureC : public ProgramOptionGeneratedSelector { public: - SelectErasureC(ceph::util::random_number_generator& rng, + SelectErasureC(std::mt19937_64& rng, po::variables_map& vm, std::string_view plugin, const std::optional>& km, @@ -188,7 +189,7 @@ class SelectErasureC : public ProgramOptionGeneratedSelector { const std::vector generate_selections() override; private: - ceph::util::random_number_generator& rng; + std::mt19937_64& rng; std::string_view plugin; std::optional> km; @@ -198,7 +199,7 @@ class SelectErasureC : public ProgramOptionGeneratedSelector { namespace jerasure { class SelectErasureW : public ProgramOptionGeneratedSelector { public: - SelectErasureW(ceph::util::random_number_generator& rng, + SelectErasureW(std::mt19937_64& rng, po::variables_map& vm, std::string_view plugin, const std::optional& technique, @@ -209,7 +210,7 @@ class SelectErasureW : public ProgramOptionGeneratedSelector { const std::vector generate_selections() override; private: - ceph::util::random_number_generator& rng; + std::mt19937_64& rng; std::string_view plugin; std::optional technique; @@ -220,7 +221,7 @@ class SelectErasureW : public ProgramOptionGeneratedSelector { class SelectErasurePacketSize : public ProgramOptionGeneratedSelector { public: - SelectErasurePacketSize(ceph::util::random_number_generator& rng, + SelectErasurePacketSize(std::mt19937_64& rng, po::variables_map& vm, std::string_view plugin, const std::optional& technique, @@ -230,7 +231,7 @@ class SelectErasurePacketSize const std::vector generate_selections() override; private: - ceph::util::random_number_generator& rng; + std::mt19937_64& rng; std::string_view plugin; std::optional technique; @@ -291,7 +292,7 @@ using SelectLayers = class SelectMappingAndLayers { public: - SelectMappingAndLayers(ceph::util::random_number_generator& rng, + SelectMappingAndLayers(std::mt19937_64& rng, po::variables_map& vm, bool first_use); const std::pair select(); @@ -299,8 +300,8 @@ class SelectMappingAndLayers { private: uint64_t rng_seed; - ceph::util::random_number_generator mapping_rng; - ceph::util::random_number_generator layers_rng; + std::mt19937_64 mapping_rng; + std::mt19937_64 layers_rng; SelectMapping sma; SelectLayers sly; @@ -310,7 +311,7 @@ class SelectMappingAndLayers { class SelectErasureTechnique : public ProgramOptionGeneratedSelector { public: - SelectErasureTechnique(ceph::util::random_number_generator& rng, + SelectErasureTechnique(std::mt19937_64& rng, po::variables_map& vm, std::string_view plugin, bool first_use); @@ -318,7 +319,7 @@ class SelectErasureTechnique const std::vector generate_selections() override; private: - ceph::util::random_number_generator& rng; + std::mt19937_64& rng; std::string_view plugin; bool stable; @@ -326,14 +327,14 @@ class SelectErasureTechnique class SelectErasureChunkSize : public ProgramOptionGeneratedSelector { public: - SelectErasureChunkSize(ceph::util::random_number_generator& rng, + SelectErasureChunkSize(std::mt19937_64& rng, po::variables_map& vm, ErasureCodeInterfaceRef ec_impl, bool first_use); const std::vector generate_selections() override; private: - ceph::util::random_number_generator& rng; + std::mt19937_64& rng; ErasureCodeInterfaceRef ec_impl; }; @@ -355,7 +356,7 @@ struct Profile { class SelectErasureProfile : public ProgramOptionReader { public: SelectErasureProfile(boost::intrusive_ptr cct, - ceph::util::random_number_generator& rng, + std::mt19937_64& rng, po::variables_map& vm, librados::Rados& rados, bool dry_run, bool first_use); const Profile select() override; @@ -366,7 +367,7 @@ class SelectErasureProfile : public ProgramOptionReader { boost::intrusive_ptr cct; librados::Rados& rados; bool dry_run; - ceph::util::random_number_generator& rng; + std::mt19937_64& rng; po::variables_map& vm; bool first_use; @@ -380,7 +381,7 @@ class SelectErasureProfile : public ProgramOptionReader { class SelectErasurePool : public ProgramOptionReader { public: SelectErasurePool(boost::intrusive_ptr cct, - ceph::util::random_number_generator& rng, + std::mt19937_64& rng, po::variables_map& vm, librados::Rados& rados, bool dry_run, @@ -453,7 +454,7 @@ class TestObject { ceph::io_sequence::tester::SelectObjectSize& sos, ceph::io_sequence::tester::SelectNumThreads& snt, ceph::io_sequence::tester::SelectSeqRange& ssr, - ceph::util::random_number_generator& rng, + std::mt19937_64& rng, ceph::mutex& lock, ceph::condition_variable& cond, bool dryrun, @@ -477,7 +478,7 @@ class TestObject { std::unique_ptr seq; std::unique_ptr op; bool done; - ceph::util::random_number_generator& rng; + std::mt19937_64& rng; bool verbose; std::optional seqseed; std::optional> pool_km; @@ -500,7 +501,7 @@ class TestRunner { private: librados::Rados& rados; int seed; - ceph::util::random_number_generator rng; + std::mt19937_64 rng; ceph::io_sequence::tester::SelectBlockSize sbs; ceph::io_sequence::tester::SelectObjectSize sos;