Skip to content

Commit

Permalink
Merge pull request #629 from GolosChain/616-ram-bandwidth
Browse files Browse the repository at this point in the history
RAM (caches) bandwidth #616
  • Loading branch information
afalaleev committed May 8, 2019
2 parents e27564c + 2844572 commit 5596bc8
Show file tree
Hide file tree
Showing 23 changed files with 212 additions and 66 deletions.
1 change: 1 addition & 0 deletions contracts/eosio.bios/eosio.bios.abi
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
{"name":"target_block_cpu_usage_pct", "type":"uint32"},
{"name":"max_transaction_cpu_usage", "type":"uint32"},
{"name":"min_transaction_cpu_usage", "type":"uint32"},
{"name":"min_transaction_ram_usage", "type":"uint64"},
{"name":"max_transaction_lifetime", "type":"uint32"},
{"name":"deferred_trx_expiration_window", "type":"uint32"},
{"name":"max_transaction_delay", "type":"uint32"},
Expand Down
1 change: 1 addition & 0 deletions contracts/eosio.system/eosio.system.abi
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@
{"name":"target_block_cpu_usage_pct", "type":"uint32"},
{"name":"max_transaction_cpu_usage", "type":"uint32"},
{"name":"min_transaction_cpu_usage", "type":"uint32"},
{"name":"min_transaction_ram_usage", "type":"uint64"},
{"name":"max_transaction_lifetime", "type":"uint32"},
{"name":"deferred_trx_expiration_window", "type":"uint32"},
{"name":"max_transaction_delay", "type":"uint32"},
Expand Down
4 changes: 4 additions & 0 deletions contracts/eosiolib/privileged.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ namespace eosio {

uint32_t min_transaction_cpu_usage;

uint64_t min_transaction_ram_usage;


/**
* The numerator for the discount on cpu usage for CFA's
Expand Down Expand Up @@ -103,6 +105,8 @@ namespace eosio {
(max_block_cpu_usage)(target_block_cpu_usage_pct)
(max_transaction_cpu_usage)(min_transaction_cpu_usage)

(min_transaction_ram_usage)

(max_transaction_lifetime)(deferred_trx_expiration_window)(max_transaction_delay)
(max_inline_action_size)(max_inline_action_depth)(max_authority_depth)
)
Expand Down
82 changes: 64 additions & 18 deletions libraries/chain/chaindb/cache_map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,13 +225,55 @@ namespace cyberway { namespace chaindb {
}; // struct pending_cache_object_state

struct pending_cache_cell final: public cache_cell {
pending_cache_cell(cache_map_impl& m, const revision_t r)
pending_cache_cell(cache_map_impl& m, const revision_t r, const uint64_t d)
: cache_cell(m, cache_cell::Pending),
revision(r) {
revision_(r),
max_distance_(d) {
assert(revision_ >= start_revision);
assert(max_distance_ > 0);
}

void emplace(cache_object_ptr obj_ptr, const bool is_deleted) {
size += obj_ptr->service().size;
assert(obj_ptr);
assert(!obj_ptr->has_cell() || obj_ptr->cell().kind == cache_cell::LRU);

auto& state = emplace_impl(std::move(obj_ptr), is_deleted);
add_ram_usage(state);
}

void emplace(cache_object_ptr obj_ptr, const bool is_deleted, cache_object_state* prev_state) {
assert(obj_ptr);
assert(obj_ptr->cell().kind == cache_cell::Pending);

// squash case - don't add ram usage
auto& state = emplace_impl(std::move(obj_ptr), is_deleted);
state.prev_state = prev_state;
}

void squash_revision() {
revision_--;
assert(revision_ >= start_revision);
}

revision_t revision() {
return revision_;
}

std::deque<pending_cache_object_state> state_list; // Expansion of a std::deque is cheaper than the expansion of a std::vector

private:
void add_ram_usage(const pending_cache_object_state& state) {
auto delta = max_distance_;
if (state.prev_state) {
delta = pos - state.prev_state->cell->pos;
}

CYBERWAY_CACHE_ASSERT(UINT64_MAX - size >= delta, "Pending delta would overflow UINT64_MAX");
size += delta;
}

pending_cache_object_state& emplace_impl(cache_object_ptr obj_ptr, const bool is_deleted) {
assert(obj_ptr);

state_list.emplace_back(*this, std::move(obj_ptr));

Expand All @@ -240,15 +282,12 @@ namespace cyberway { namespace chaindb {

state.prev_state = prev_state;
state.is_deleted = is_deleted;
}

void emplace(cache_object_ptr obj_ptr, const bool is_deleted, cache_object_state* prev_state) {
emplace(std::move(obj_ptr), is_deleted);
state_list.back().prev_state = prev_state;
return state;
}

revision_t revision = impossible_revision;
std::deque<pending_cache_object_state> state_list; // Expansion of a std::deque is cheaper than the expansion of a std::vector
revision_t revision_ = impossible_revision;
const uint64_t max_distance_ = 0;
}; // struct pending_cache_cell

//---------------------------------------
Expand Down Expand Up @@ -507,8 +546,12 @@ namespace cyberway { namespace chaindb {
}
}

uint64_t calc_ram_bytes(const revision_t revision) {
return get_pending_cell(revision).size;
}

void start_session(const revision_t revision) {
pending_cell_list_.emplace_back(*this, revision);
pending_cell_list_.emplace_back(*this, revision, ram_limit_);
auto& pending = pending_cell_list_.back();
if (!lru_cell_list_.empty()) {
auto& lru = lru_cell_list_.back();
Expand Down Expand Up @@ -546,13 +589,12 @@ namespace cyberway { namespace chaindb {

auto itr = pending_cell_list_.end();
--itr; auto& src_cell =*itr;
CYBERWAY_CACHE_ASSERT(revision == src_cell.revision, "Wrong revision of top pending caches");
CYBERWAY_CACHE_ASSERT(revision == src_cell.revision(), "Wrong revision of top pending caches");
--itr; auto& dst_cell =*itr;
CYBERWAY_CACHE_ASSERT(revision - 1 == dst_cell.revision, "Wrong revision of pending caches");
CYBERWAY_CACHE_ASSERT(revision - 1 == dst_cell.revision(), "Wrong revision of pending caches");

if (pending_cell_list_.size() == 2 && dst_cell.state_list.empty()) {
assert(pending_cell_list_.front().revision == revision -1);
src_cell.revision = dst_cell.revision;
if (pending_cell_list_.begin() == itr && dst_cell.state_list.empty()) {
src_cell.squash_revision();
pending_cell_list_.pop_front();
return;
}
Expand All @@ -570,7 +612,7 @@ namespace cyberway { namespace chaindb {

void undo_session(const revision_t revision) {
if (!has_pending_cell()) {
// pop_block()
// case of pop_block
return;
}

Expand Down Expand Up @@ -746,9 +788,9 @@ namespace cyberway { namespace chaindb {

pending_cache_cell& get_pending_cell(const revision_t revision) {
auto pending_ptr = find_pending_cell();
CYBERWAY_CACHE_ASSERT(pending_ptr && pending_ptr->revision == revision,
CYBERWAY_CACHE_ASSERT(pending_ptr && pending_ptr->revision() == revision,
"Wrong pending revision ${pending_revision} != ${revision}",
("pending_revision", pending_ptr ? pending_ptr->revision : impossible_revision)("revision", revision));
("pending_revision", pending_ptr ? pending_ptr->revision() : impossible_revision)("revision", revision));
return *pending_ptr;
}

Expand Down Expand Up @@ -1066,6 +1108,10 @@ namespace cyberway { namespace chaindb {
impl_->set_revision(obj.service, rev);
}

uint64_t cache_map::calc_ram_bytes(const revision_t revision) const {
return impl_->calc_ram_bytes(revision);
}

void cache_map::start_session(const revision_t revision) const {
impl_->start_session(revision);
}
Expand Down
4 changes: 4 additions & 0 deletions libraries/chain/chaindb/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1012,4 +1012,8 @@ namespace cyberway { namespace chaindb {
apply_ = false;
}

uint64_t chaindb_session::calc_ram_bytes() const {
return controller_.impl_->cache_.calc_ram_bytes(revision_);
}

} } // namespace cyberway::chaindb

0 comments on commit 5596bc8

Please sign in to comment.