From cd1c5a30ddacc140cde64c4ca4d4deaa7f345595 Mon Sep 17 00:00:00 2001 From: Joe Loser Date: Wed, 18 Jul 2018 08:11:01 -0400 Subject: [PATCH] Add user defined literals for megabytes and kilobytes --- src/ripple/app/main/Application.cpp | 11 +++--- src/ripple/basics/ByteUtilities.h | 38 +++++++++++++++++++ src/ripple/basics/qalloc.h | 6 +-- src/ripple/core/Coro.ipp | 4 +- src/ripple/core/impl/SociDB.cpp | 6 +-- src/ripple/crypto/impl/csprng.cpp | 3 +- src/ripple/net/HTTPClient.h | 6 +-- src/ripple/net/impl/RPCCall.cpp | 3 +- src/ripple/nodestore/backend/NuDBFactory.cpp | 12 ++---- .../nodestore/backend/RocksDBFactory.cpp | 5 ++- .../nodestore/backend/RocksDBQuickFactory.cpp | 5 ++- src/ripple/protocol/Protocol.h | 3 +- 12 files changed, 69 insertions(+), 33 deletions(-) create mode 100644 src/ripple/basics/ByteUtilities.h diff --git a/src/ripple/app/main/Application.cpp b/src/ripple/app/main/Application.cpp index 3766787ca69..dd6f93857b5 100644 --- a/src/ripple/app/main/Application.cpp +++ b/src/ripple/app/main/Application.cpp @@ -44,6 +44,7 @@ #include #include #include +#include #include #include #include @@ -609,7 +610,6 @@ class ApplicationImp return nodeIdentity_; } - PublicKey const & getValidationPublicKey() const override { @@ -1029,8 +1029,7 @@ class ApplicationImp boost::filesystem::space_info space = boost::filesystem::space (config_->legacy ("database_path")); - constexpr std::uintmax_t bytes512M = 512 * 1024 * 1024; - if (space.available < (bytes512M)) + if (space.available < megabytes(512)) { JLOG(m_journal.fatal()) << "Remaining free disk space is less than 512MB"; @@ -1074,7 +1073,7 @@ class ApplicationImp << "Note that this does not take into account available disk " "space."; - if (freeSpace < bytes512M) + if (freeSpace < megabytes(512)) { JLOG(m_journal.fatal()) << "Free SQLite space for transaction db is less than " @@ -1194,11 +1193,11 @@ bool ApplicationImp::setup() getLedgerDB ().getSession () << boost::str (boost::format ("PRAGMA cache_size=-%d;") % - (config_->getSize (siLgrDBCache) * 1024)); + (config_->getSize (siLgrDBCache) * kilobytes(1))); getTxnDB ().getSession () << boost::str (boost::format ("PRAGMA cache_size=-%d;") % - (config_->getSize (siTxnDBCache) * 1024)); + (config_->getSize (siTxnDBCache) * kilobytes(1))); mTxnDB->setupCheckpointing (m_jobQueue.get(), logs()); mLedgerDB->setupCheckpointing (m_jobQueue.get(), logs()); diff --git a/src/ripple/basics/ByteUtilities.h b/src/ripple/basics/ByteUtilities.h new file mode 100644 index 00000000000..85de89c078e --- /dev/null +++ b/src/ripple/basics/ByteUtilities.h @@ -0,0 +1,38 @@ +//------------------------------------------------------------------------------ +/* + This file is part of rippled: https://github.com/ripple/rippled + Copyright (c) 2018 Ripple Labs Inc. + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ +//============================================================================== + +#ifndef RIPPLE_BASICS_BYTEUTILITIES_H_INCLUDED +#define RIPPLE_BASICS_BYTEUTILITIES_H_INCLUDED + +namespace ripple { + + template + constexpr auto kilobytes(T value) noexcept + { + return value * 1024; + } + + template + constexpr auto megabytes(T value) noexcept + { + return kilobytes(1) * kilobytes(1) * value; + } +} + +#endif diff --git a/src/ripple/basics/qalloc.h b/src/ripple/basics/qalloc.h index 4d8a0f1c144..271f3cc63c7 100644 --- a/src/ripple/basics/qalloc.h +++ b/src/ripple/basics/qalloc.h @@ -21,6 +21,7 @@ #define RIPPLE_BASICS_QALLOC_H_INCLUDED #include +#include #include #include #include @@ -67,10 +68,7 @@ class qalloc_impl block* free_ = nullptr; public: - enum - { - block_size = 256 * 1024 - }; + static constexpr auto block_size = kilobytes(256); qalloc_impl() = default; qalloc_impl (qalloc_impl const&) = delete; diff --git a/src/ripple/core/Coro.ipp b/src/ripple/core/Coro.ipp index 73198e922b4..e259e2d293c 100644 --- a/src/ripple/core/Coro.ipp +++ b/src/ripple/core/Coro.ipp @@ -20,6 +20,8 @@ #ifndef RIPPLE_CORE_COROINL_H_INCLUDED #define RIPPLE_CORE_COROINL_H_INCLUDED +#include + namespace ripple { template @@ -40,7 +42,7 @@ Coro(Coro_create_t, JobQueue& jq, JobType type, #ifndef NDEBUG finished_ = true; #endif - }, boost::coroutines::attributes (1024 * 1024)) + }, boost::coroutines::attributes (megabytes(1))) { } diff --git a/src/ripple/core/impl/SociDB.cpp b/src/ripple/core/impl/SociDB.cpp index 386ebf0d0b1..bac44be410a 100644 --- a/src/ripple/core/impl/SociDB.cpp +++ b/src/ripple/core/impl/SociDB.cpp @@ -24,6 +24,7 @@ #include +#include #include #include #include @@ -129,7 +130,7 @@ size_t getKBUsedAll (soci::session& s) { if (! getConnection (s)) Throw ("No connection found."); - return static_cast (sqlite_api::sqlite3_memory_used () / 1024); + return static_cast (sqlite_api::sqlite3_memory_used () / kilobytes(1)); } size_t getKBUsedDB (soci::session& s) @@ -140,7 +141,7 @@ size_t getKBUsedDB (soci::session& s) int cur = 0, hiw = 0; sqlite_api::sqlite3_db_status ( conn, SQLITE_DBSTATUS_CACHE_USED, &cur, &hiw, 0); - return cur / 1024; + return cur / kilobytes(1); } Throw (""); return 0; // Silence compiler warning. @@ -159,7 +160,6 @@ void convert (soci::blob& from, std::string& to) std::vector tmp; convert (from, tmp); to.assign (tmp.begin (), tmp.end()); - } void convert (std::vector const& from, soci::blob& to) diff --git a/src/ripple/crypto/impl/csprng.cpp b/src/ripple/crypto/impl/csprng.cpp index c7bf1d11ebe..1e3ce92a56a 100644 --- a/src/ripple/crypto/impl/csprng.cpp +++ b/src/ripple/crypto/impl/csprng.cpp @@ -18,6 +18,7 @@ //============================================================================== #include +#include #include #include #include @@ -55,7 +56,7 @@ csprng_engine::load_state (std::string const& file) if (!file.empty()) { std::lock_guard lock (mutex_); - RAND_load_file (file.c_str (), 1024); + RAND_load_file (file.c_str (), kilobytes(1)); RAND_write_file (file.c_str ()); } } diff --git a/src/ripple/net/HTTPClient.h b/src/ripple/net/HTTPClient.h index 85fee29c388..30ed29da54e 100644 --- a/src/ripple/net/HTTPClient.h +++ b/src/ripple/net/HTTPClient.h @@ -20,6 +20,7 @@ #ifndef RIPPLE_NET_HTTPCLIENT_H_INCLUDED #define RIPPLE_NET_HTTPCLIENT_H_INCLUDED +#include #include #include #include @@ -34,10 +35,7 @@ class HTTPClient public: explicit HTTPClient() = default; - enum - { - maxClientHeaderBytes = 32 * 1024 - }; + static constexpr auto maxClientHeaderBytes = kilobytes(32); static void initializeSSLContext (Config const& config, beast::Journal j); diff --git a/src/ripple/net/impl/RPCCall.cpp b/src/ripple/net/impl/RPCCall.cpp index 82fa88893a8..05eac41f61e 100644 --- a/src/ripple/net/impl/RPCCall.cpp +++ b/src/ripple/net/impl/RPCCall.cpp @@ -19,6 +19,7 @@ #include #include +#include #include #include #include @@ -1534,7 +1535,7 @@ void fromNetwork ( // Number of bytes to try to receive if no // Content-Length header received - const int RPC_REPLY_MAX_BYTES (256*1024*1024); + constexpr auto RPC_REPLY_MAX_BYTES = megabytes(256); using namespace std::chrono_literals; auto constexpr RPC_NOTIFY = 10min; diff --git a/src/ripple/nodestore/backend/NuDBFactory.cpp b/src/ripple/nodestore/backend/NuDBFactory.cpp index 19f8eae3f71..3f77f011d5a 100644 --- a/src/ripple/nodestore/backend/NuDBFactory.cpp +++ b/src/ripple/nodestore/backend/NuDBFactory.cpp @@ -40,14 +40,10 @@ class NuDBBackend : public Backend { public: - enum - { - // This needs to be tuned for the - // distribution of data sizes. - arena_alloc_size = 16 * 1024 * 1024, - - currentType = 1 - }; + // This needs to be tuned for the + // distribution of data sizes. + static constexpr std::size_t arena_alloc_size = megabytes(16); + static constexpr std::size_t currentType = 1; beast::Journal j_; size_t const keyBytes_; diff --git a/src/ripple/nodestore/backend/RocksDBFactory.cpp b/src/ripple/nodestore/backend/RocksDBFactory.cpp index 04c4fe6f48f..2315ca30fcd 100644 --- a/src/ripple/nodestore/backend/RocksDBFactory.cpp +++ b/src/ripple/nodestore/backend/RocksDBFactory.cpp @@ -23,6 +23,7 @@ #if RIPPLE_ROCKSDB_AVAILABLE #include +#include #include // VFALCO Bad dependency #include #include @@ -117,7 +118,7 @@ class RocksDBBackend if (keyValues.exists ("cache_mb")) table_options.block_cache = rocksdb::NewLRUCache ( - get(keyValues, "cache_mb") * 1024L * 1024L); + get(keyValues, "cache_mb") * megabytes(1)); if (auto const v = get(keyValues, "filter_bits")) { @@ -131,7 +132,7 @@ class RocksDBBackend if (keyValues.exists ("file_size_mb")) { - m_options.target_file_size_base = 1024 * 1024 * get(keyValues,"file_size_mb"); + m_options.target_file_size_base = megabytes(1) * get(keyValues,"file_size_mb"); m_options.max_bytes_for_level_base = 5 * m_options.target_file_size_base; m_options.write_buffer_size = 2 * m_options.target_file_size_base; } diff --git a/src/ripple/nodestore/backend/RocksDBQuickFactory.cpp b/src/ripple/nodestore/backend/RocksDBQuickFactory.cpp index a15e3b1d70a..a8d1f1cf3da 100644 --- a/src/ripple/nodestore/backend/RocksDBQuickFactory.cpp +++ b/src/ripple/nodestore/backend/RocksDBQuickFactory.cpp @@ -23,6 +23,7 @@ #if RIPPLE_ROCKSDB_AVAILABLE #include +#include #include // VFALCO Bad dependency #include #include @@ -109,7 +110,7 @@ class RocksDBQuickBackend "Missing path in RocksDBQuickFactory backend"); // Defaults - std::uint64_t budget = 512 * 1024 * 1024; // 512MB + std::uint64_t budget = megabytes(512); std::string style("level"); std::uint64_t threads=4; @@ -128,7 +129,7 @@ class RocksDBQuickBackend m_options.OptimizeUniversalStyleCompaction(budget); if (style == "point") - m_options.OptimizeForPointLookup(budget / 1024 / 1024); // In MB + m_options.OptimizeForPointLookup(budget / megabytes(1)); // In MB m_options.IncreaseParallelism(threads); diff --git a/src/ripple/protocol/Protocol.h b/src/ripple/protocol/Protocol.h index e3d4ce81ca3..f3238a9b1b1 100644 --- a/src/ripple/protocol/Protocol.h +++ b/src/ripple/protocol/Protocol.h @@ -21,6 +21,7 @@ #define RIPPLE_PROTOCOL_PROTOCOL_H_INCLUDED #include +#include #include namespace ripple { @@ -38,7 +39,7 @@ namespace ripple { std::size_t constexpr txMinSizeBytes = 32; /** Largest legal byte size of a transaction. */ -std::size_t constexpr txMaxSizeBytes = 1024 * 1024; +std::size_t constexpr txMaxSizeBytes = megabytes(1); /** The maximum number of unfunded offers to delete at once */ std::size_t constexpr unfundedOfferRemoveLimit = 1000;