Skip to content

Commit

Permalink
Add user defined literals for megabytes and kilobytes
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeLoser authored and nbougalis committed Oct 1, 2018
1 parent 8dd8433 commit cd1c5a3
Show file tree
Hide file tree
Showing 12 changed files with 69 additions and 33 deletions.
11 changes: 5 additions & 6 deletions src/ripple/app/main/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
#include <ripple/app/misc/ValidatorKeys.h>
#include <ripple/app/paths/PathRequests.h>
#include <ripple/app/tx/apply.h>
#include <ripple/basics/ByteUtilities.h>
#include <ripple/basics/ResolverAsio.h>
#include <ripple/basics/Sustain.h>
#include <ripple/basics/PerfLog.h>
Expand Down Expand Up @@ -609,7 +610,6 @@ class ApplicationImp
return nodeIdentity_;
}


PublicKey const &
getValidationPublicKey() const override
{
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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 "
Expand Down Expand Up @@ -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());
Expand Down
38 changes: 38 additions & 0 deletions src/ripple/basics/ByteUtilities.h
Original file line number Diff line number Diff line change
@@ -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<class T>
constexpr auto kilobytes(T value) noexcept
{
return value * 1024;
}

template<class T>
constexpr auto megabytes(T value) noexcept
{
return kilobytes(1) * kilobytes(1) * value;
}
}

#endif
6 changes: 2 additions & 4 deletions src/ripple/basics/qalloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#define RIPPLE_BASICS_QALLOC_H_INCLUDED

#include <ripple/basics/contract.h>
#include <ripple/basics/ByteUtilities.h>
#include <boost/intrusive/list.hpp>
#include <cstddef>
#include <cstdint>
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion src/ripple/core/Coro.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
#ifndef RIPPLE_CORE_COROINL_H_INCLUDED
#define RIPPLE_CORE_COROINL_H_INCLUDED

#include <ripple/basics/ByteUtilities.h>

namespace ripple {

template <class F>
Expand All @@ -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)))
{
}

Expand Down
6 changes: 3 additions & 3 deletions src/ripple/core/impl/SociDB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@


#include <ripple/basics/contract.h>
#include <ripple/basics/ByteUtilities.h>
#include <ripple/core/ConfigSections.h>
#include <ripple/core/SociDB.h>
#include <ripple/core/Config.h>
Expand Down Expand Up @@ -129,7 +130,7 @@ size_t getKBUsedAll (soci::session& s)
{
if (! getConnection (s))
Throw<std::logic_error> ("No connection found.");
return static_cast <size_t> (sqlite_api::sqlite3_memory_used () / 1024);
return static_cast <size_t> (sqlite_api::sqlite3_memory_used () / kilobytes(1));
}

size_t getKBUsedDB (soci::session& s)
Expand All @@ -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<std::logic_error> ("");
return 0; // Silence compiler warning.
Expand All @@ -159,7 +160,6 @@ void convert (soci::blob& from, std::string& to)
std::vector<std::uint8_t> tmp;
convert (from, tmp);
to.assign (tmp.begin (), tmp.end());

}

void convert (std::vector<std::uint8_t> const& from, soci::blob& to)
Expand Down
3 changes: 2 additions & 1 deletion src/ripple/crypto/impl/csprng.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
//==============================================================================

#include <ripple/basics/contract.h>
#include <ripple/basics/ByteUtilities.h>
#include <ripple/crypto/csprng.h>
#include <openssl/rand.h>
#include <array>
Expand Down Expand Up @@ -55,7 +56,7 @@ csprng_engine::load_state (std::string const& file)
if (!file.empty())
{
std::lock_guard<std::mutex> lock (mutex_);
RAND_load_file (file.c_str (), 1024);
RAND_load_file (file.c_str (), kilobytes(1));
RAND_write_file (file.c_str ());
}
}
Expand Down
6 changes: 2 additions & 4 deletions src/ripple/net/HTTPClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#ifndef RIPPLE_NET_HTTPCLIENT_H_INCLUDED
#define RIPPLE_NET_HTTPCLIENT_H_INCLUDED

#include <ripple/basics/ByteUtilities.h>
#include <ripple/core/Config.h>
#include <boost/asio/io_service.hpp>
#include <boost/asio/streambuf.hpp>
Expand All @@ -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);

Expand Down
3 changes: 2 additions & 1 deletion src/ripple/net/impl/RPCCall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include <ripple/app/main/Application.h>
#include <ripple/basics/StringUtilities.h>
#include <ripple/basics/ByteUtilities.h>
#include <ripple/net/RPCCall.h>
#include <ripple/net/RPCErr.h>
#include <ripple/basics/base64.h>
Expand Down Expand Up @@ -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;
Expand Down
12 changes: 4 additions & 8 deletions src/ripple/nodestore/backend/NuDBFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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_;
Expand Down
5 changes: 3 additions & 2 deletions src/ripple/nodestore/backend/RocksDBFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#if RIPPLE_ROCKSDB_AVAILABLE

#include <ripple/basics/contract.h>
#include <ripple/basics/ByteUtilities.h>
#include <ripple/core/Config.h> // VFALCO Bad dependency
#include <ripple/nodestore/Factory.h>
#include <ripple/nodestore/Manager.h>
Expand Down Expand Up @@ -117,7 +118,7 @@ class RocksDBBackend

if (keyValues.exists ("cache_mb"))
table_options.block_cache = rocksdb::NewLRUCache (
get<int>(keyValues, "cache_mb") * 1024L * 1024L);
get<int>(keyValues, "cache_mb") * megabytes(1));

if (auto const v = get<int>(keyValues, "filter_bits"))
{
Expand All @@ -131,7 +132,7 @@ class RocksDBBackend

if (keyValues.exists ("file_size_mb"))
{
m_options.target_file_size_base = 1024 * 1024 * get<int>(keyValues,"file_size_mb");
m_options.target_file_size_base = megabytes(1) * get<int>(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;
}
Expand Down
5 changes: 3 additions & 2 deletions src/ripple/nodestore/backend/RocksDBQuickFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#if RIPPLE_ROCKSDB_AVAILABLE

#include <ripple/basics/contract.h>
#include <ripple/basics/ByteUtilities.h>
#include <ripple/core/Config.h> // VFALCO Bad dependency
#include <ripple/nodestore/Factory.h>
#include <ripple/nodestore/Manager.h>
Expand Down Expand Up @@ -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;

Expand All @@ -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);

Expand Down
3 changes: 2 additions & 1 deletion src/ripple/protocol/Protocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#define RIPPLE_PROTOCOL_PROTOCOL_H_INCLUDED

#include <ripple/basics/base_uint.h>
#include <ripple/basics/ByteUtilities.h>
#include <cstdint>

namespace ripple {
Expand All @@ -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;
Expand Down

0 comments on commit cd1c5a3

Please sign in to comment.