From 5f47aa61407aeef4652e7e85c93d3415a36a7c4e Mon Sep 17 00:00:00 2001 From: Davide Pesavento Date: Fri, 7 Oct 2016 22:09:09 +0200 Subject: [PATCH] core: use C++11 instead of Boost.Random Change-Id: I8f22965b86c681581762a47995f29f888421a558 Refs: #3599 --- core/random.cpp | 20 +++++-------- core/random.hpp | 30 ++++++++++--------- daemon/fw/asf-probing-module.cpp | 7 ++--- daemon/fw/forwarder.cpp | 3 +- daemon/fw/ncc-strategy.cpp | 29 +++++++++--------- tests/core/random.t.cpp | 16 +++++----- tests/daemon/fw/asf-strategy.t.cpp | 4 +-- tests/daemon/mgmt/face-manager.t.cpp | 5 ++-- .../daemon/mgmt/strategy-choice-manager.t.cpp | 5 ++-- tests/rib/rib-manager.t.cpp | 7 +++-- wscript | 2 +- 11 files changed, 61 insertions(+), 67 deletions(-) diff --git a/core/random.cpp b/core/random.cpp index c398f8df..9c5de408 100644 --- a/core/random.cpp +++ b/core/random.cpp @@ -1,6 +1,6 @@ /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ /** - * Copyright (c) 2014-2015, Regents of the University of California, + * Copyright (c) 2014-2016, Regents of the University of California, * Arizona Board of Regents, * Colorado State University, * University Pierre & Marie Curie, Sorbonne University, @@ -28,21 +28,15 @@ namespace nfd { -static boost::thread_specific_ptr g_rng; - -boost::random::mt19937& +std::mt19937& getGlobalRng() { - if (g_rng.get() == nullptr) { - g_rng.reset(new boost::random::mt19937()); + static boost::thread_specific_ptr rng; + if (rng.get() == nullptr) { + std::random_device rd; + rng.reset(new std::mt19937(rd())); } - return *g_rng; -} - -void -resetGlobalRng() -{ - g_rng.reset(); + return *rng; } } // namespace nfd diff --git a/core/random.hpp b/core/random.hpp index 1515b724..03ae9d33 100644 --- a/core/random.hpp +++ b/core/random.hpp @@ -1,12 +1,12 @@ /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ /** - * Copyright (c) 2014, Regents of the University of California, - * Arizona Board of Regents, - * Colorado State University, - * University Pierre & Marie Curie, Sorbonne University, - * Washington University in St. Louis, - * Beijing Institute of Technology, - * The University of Memphis + * Copyright (c) 2014-2016, Regents of the University of California, + * Arizona Board of Regents, + * Colorado State University, + * University Pierre & Marie Curie, Sorbonne University, + * Washington University in St. Louis, + * Beijing Institute of Technology, + * The University of Memphis. * * This file is part of NFD (Named Data Networking Forwarding Daemon). * See AUTHORS.md for complete list of NFD authors and contributors. @@ -24,24 +24,26 @@ */ /** - * This file declares the global random number generator - * All random numbers in NFD should use this global random number generator, - * so that the generator can be properly seeded when necessary. + * This file declares the global pseudorandom number generator (PRNG). * - * This random number generator is not suitable for security purposes; - * security-critical code should use CryptoPP random number generator instead. + * All random numbers generated by NFD should use this global generator, + * so that it can be properly seeded when necessary. + * + * This PRNG is not suitable for security purposes; security-critical + * code must use a cryptographically secure PRNG, such as those provided + * by OpenSSL and Crypto++. */ #ifndef NFD_CORE_RANDOM_HPP #define NFD_CORE_RANDOM_HPP -#include +#include namespace nfd { /** \return the global random number generator instance */ -boost::random::mt19937& +std::mt19937& getGlobalRng(); } // namespace nfd diff --git a/daemon/fw/asf-probing-module.cpp b/daemon/fw/asf-probing-module.cpp index 99449cda..485a3e99 100644 --- a/daemon/fw/asf-probing-module.cpp +++ b/daemon/fw/asf-probing-module.cpp @@ -24,11 +24,8 @@ */ #include "asf-probing-module.hpp" - #include "core/random.hpp" -#include - namespace nfd { namespace fw { namespace asf { @@ -186,8 +183,8 @@ ProbingModule::getProbingProbability(uint64_t rank, uint64_t rankSum, uint64_t n double ProbingModule::getRandomNumber(double start, double end) { - boost::random::uniform_real_distribution distribution(start, end); - return distribution(getGlobalRng()); + std::uniform_real_distribution dist(start, end); + return dist(getGlobalRng()); } } // namespace asf diff --git a/daemon/fw/forwarder.cpp b/daemon/fw/forwarder.cpp index 9ef3c758..b8bd076b 100644 --- a/daemon/fw/forwarder.cpp +++ b/daemon/fw/forwarder.cpp @@ -30,7 +30,6 @@ #include "strategy.hpp" #include "table/cleanup.hpp" #include -#include namespace nfd { @@ -270,7 +269,7 @@ Forwarder::onOutgoingInterest(const shared_ptr& pitEntry, Face& outF if (wantNewNonce) { interest = make_shared(*interest); - static boost::random::uniform_int_distribution dist; + static std::uniform_int_distribution dist; interest->setNonce(dist(getGlobalRng())); } diff --git a/daemon/fw/ncc-strategy.cpp b/daemon/fw/ncc-strategy.cpp index f1a888a4..d191326a 100644 --- a/daemon/fw/ncc-strategy.cpp +++ b/daemon/fw/ncc-strategy.cpp @@ -26,7 +26,6 @@ #include "ncc-strategy.hpp" #include "pit-algorithm.hpp" #include "core/random.hpp" -#include namespace nfd { namespace fw { @@ -143,8 +142,7 @@ NccStrategy::doPropagate(weak_ptr pitEntryWeak) } if (isForwarded) { - boost::random::uniform_int_distribution dist(0, - pitEntryInfo->maxInterval.count() - 1); + std::uniform_int_distribution dist(0, pitEntryInfo->maxInterval.count() - 1); time::nanoseconds deferNext = time::nanoseconds(dist(getGlobalRng())); pitEntryInfo->propagateTimer = scheduler::schedule(deferNext, bind(&NccStrategy::doPropagate, this, weak_ptr(pitEntry))); @@ -239,13 +237,9 @@ NccStrategy::getMeasurementsEntryInfo(measurements::Entry* entry) return *info; } - -const time::microseconds NccStrategy::MeasurementsEntryInfo::INITIAL_PREDICTION = - time::microseconds(8192); -const time::microseconds NccStrategy::MeasurementsEntryInfo::MIN_PREDICTION = - time::microseconds(127); -const time::microseconds NccStrategy::MeasurementsEntryInfo::MAX_PREDICTION = - time::microseconds(160000); +const time::microseconds NccStrategy::MeasurementsEntryInfo::INITIAL_PREDICTION = time::microseconds(8192); +const time::microseconds NccStrategy::MeasurementsEntryInfo::MIN_PREDICTION = time::microseconds(127); +const time::microseconds NccStrategy::MeasurementsEntryInfo::MAX_PREDICTION = time::milliseconds(160); NccStrategy::MeasurementsEntryInfo::MeasurementsEntryInfo() : prediction(INITIAL_PREDICTION) @@ -259,7 +253,8 @@ NccStrategy::MeasurementsEntryInfo::inheritFrom(const MeasurementsEntryInfo& oth } shared_ptr -NccStrategy::MeasurementsEntryInfo::getBestFace(void) { +NccStrategy::MeasurementsEntryInfo::getBestFace() +{ shared_ptr best = this->bestFace.lock(); if (best != nullptr) { return best; @@ -269,7 +264,8 @@ NccStrategy::MeasurementsEntryInfo::getBestFace(void) { } void -NccStrategy::MeasurementsEntryInfo::updateBestFace(const Face& face) { +NccStrategy::MeasurementsEntryInfo::updateBestFace(const Face& face) +{ if (this->bestFace.expired()) { this->bestFace = const_cast(face).shared_from_this(); return; @@ -285,19 +281,22 @@ NccStrategy::MeasurementsEntryInfo::updateBestFace(const Face& face) { } void -NccStrategy::MeasurementsEntryInfo::adjustPredictDown() { +NccStrategy::MeasurementsEntryInfo::adjustPredictDown() +{ prediction = std::max(MIN_PREDICTION, time::microseconds(prediction.count() - (prediction.count() >> ADJUST_PREDICT_DOWN_SHIFT))); } void -NccStrategy::MeasurementsEntryInfo::adjustPredictUp() { +NccStrategy::MeasurementsEntryInfo::adjustPredictUp() +{ prediction = std::min(MAX_PREDICTION, time::microseconds(prediction.count() + (prediction.count() >> ADJUST_PREDICT_UP_SHIFT))); } void -NccStrategy::MeasurementsEntryInfo::ageBestFace() { +NccStrategy::MeasurementsEntryInfo::ageBestFace() +{ this->previousFace = this->bestFace; this->bestFace.reset(); } diff --git a/tests/core/random.t.cpp b/tests/core/random.t.cpp index fa9c4a7b..dcd15769 100644 --- a/tests/core/random.t.cpp +++ b/tests/core/random.t.cpp @@ -1,6 +1,6 @@ /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ /** - * Copyright (c) 2014-2015, Regents of the University of California, + * Copyright (c) 2014-2016, Regents of the University of California, * Arizona Board of Regents, * Colorado State University, * University Pierre & Marie Curie, Sorbonne University, @@ -32,15 +32,15 @@ namespace nfd { namespace tests { -BOOST_FIXTURE_TEST_SUITE(TestRandom, BaseFixture) +BOOST_AUTO_TEST_SUITE(TestRandom) -BOOST_AUTO_TEST_CASE(ThreadLocalRandon) +BOOST_AUTO_TEST_CASE(ThreadLocalRng) { - boost::random::mt19937* s1 = &getGlobalRng(); - boost::random::mt19937* s2 = nullptr; + std::mt19937* s1 = &getGlobalRng(); + std::mt19937* s2 = nullptr; boost::thread t([&s2] { - s2 = &getGlobalRng(); - }); + s2 = &getGlobalRng(); + }); t.join(); @@ -49,7 +49,7 @@ BOOST_AUTO_TEST_CASE(ThreadLocalRandon) BOOST_CHECK(s1 != s2); } -BOOST_AUTO_TEST_SUITE_END() +BOOST_AUTO_TEST_SUITE_END() // TestRandom } // namespace tests } // namespace nfd diff --git a/tests/daemon/fw/asf-strategy.t.cpp b/tests/daemon/fw/asf-strategy.t.cpp index af584cfe..601083d2 100644 --- a/tests/daemon/fw/asf-strategy.t.cpp +++ b/tests/daemon/fw/asf-strategy.t.cpp @@ -158,8 +158,8 @@ BOOST_FIXTURE_TEST_CASE(Basic, AsfGridFixture) runConsumer(); BOOST_CHECK_EQUAL(consumer->getForwarderFace().getCounters().nOutData, 89); - BOOST_CHECK_LE(linkAB->getFace(nodeA).getCounters().nOutInterests, 60); - BOOST_CHECK_GE(linkAD->getFace(nodeA).getCounters().nOutInterests, 60); + BOOST_CHECK_LE(linkAB->getFace(nodeA).getCounters().nOutInterests, 61); // FIXME #3830 + BOOST_CHECK_GE(linkAD->getFace(nodeA).getCounters().nOutInterests, 59); // FIXME #3830 } BOOST_FIXTURE_TEST_CASE(Nack, AsfGridFixture) diff --git a/tests/daemon/mgmt/face-manager.t.cpp b/tests/daemon/mgmt/face-manager.t.cpp index c1898941..325b3442 100644 --- a/tests/daemon/mgmt/face-manager.t.cpp +++ b/tests/daemon/mgmt/face-manager.t.cpp @@ -24,13 +24,13 @@ */ #include "mgmt/face-manager.hpp" +#include "core/random.hpp" #include "face/tcp-factory.hpp" #include "face/udp-factory.hpp" #include "nfd-manager-common-fixture.hpp" #include "../face/dummy-face.hpp" -#include #include #include #include @@ -103,7 +103,8 @@ private: static typename std::enable_if::value>::type randomizeCounter(const T& counter) { - const_cast(counter).set(ndn::random::generateWord64()); + static std::uniform_int_distribution dist; + const_cast(counter).set(dist(getGlobalRng())); } protected: diff --git a/tests/daemon/mgmt/strategy-choice-manager.t.cpp b/tests/daemon/mgmt/strategy-choice-manager.t.cpp index 56f22a75..7aa54ac5 100644 --- a/tests/daemon/mgmt/strategy-choice-manager.t.cpp +++ b/tests/daemon/mgmt/strategy-choice-manager.t.cpp @@ -25,6 +25,7 @@ #include "mgmt/strategy-choice-manager.hpp" +#include "core/random.hpp" #include "face/face.hpp" #include "face/internal-face.hpp" #include "fw/strategy.hpp" @@ -36,7 +37,6 @@ #include "tests/daemon/fw/dummy-strategy.hpp" #include "tests/daemon/fw/install-strategy.hpp" -#include #include namespace nfd { @@ -200,10 +200,11 @@ BOOST_AUTO_TEST_CASE(StrategyChoiceDataset) actualStrategies.insert(entry.getStrategyName()); } + std::uniform_int_distribution dist; size_t nEntries = 1024; for (size_t i = 0 ; i < nEntries ; i++) { auto name = Name("test-name").appendSegment(i); - auto strategy = Name("test-strategy").appendSegment(ndn::random::generateWord64()); + auto strategy = Name("test-strategy").appendSegment(dist(getGlobalRng())); auto entry = ndn::nfd::StrategyChoice().setName(name).setStrategy(strategy); actualNames.insert(name); actualStrategies.insert(strategy); diff --git a/tests/rib/rib-manager.t.cpp b/tests/rib/rib-manager.t.cpp index dfbeb29c..8cbc5fb1 100644 --- a/tests/rib/rib-manager.t.cpp +++ b/tests/rib/rib-manager.t.cpp @@ -25,12 +25,12 @@ #include "rib/rib-manager.hpp" #include "manager-common-fixture.hpp" +#include "core/random.hpp" #include #include #include #include -#include namespace nfd { namespace rib { @@ -482,11 +482,12 @@ operator!=(const RibEntry& left, const RibEntry& right) BOOST_FIXTURE_TEST_CASE(RibDataset, UnauthorizedRibManagerFixture) { + std::uniform_int_distribution dist; uint64_t faceId = 0; - auto generateRoute = [&faceId] () -> Route { + auto generateRoute = [&dist, &faceId] () -> Route { Route route; route.faceId = ++faceId; - route.cost = ndn::random::generateWord64(); + route.cost = dist(getGlobalRng()); route.expires = time::steady_clock::TimePoint::max(); return route; }; diff --git a/wscript b/wscript index 265f93ba..f3052b9f 100644 --- a/wscript +++ b/wscript @@ -110,7 +110,7 @@ main(int, char**) conf.check_cxx(header_name='ifaddrs.h', mandatory=False) conf.check_cxx(header_name='valgrind/valgrind.h', define_name='HAVE_VALGRIND', mandatory=False) - boost_libs = 'system chrono program_options random thread log log_setup' + boost_libs = 'system chrono program_options thread log log_setup' if conf.options.with_tests: conf.env['WITH_TESTS'] = 1 conf.define('WITH_TESTS', 1)