Skip to content

Commit

Permalink
precompile session params
Browse files Browse the repository at this point in the history
Signed-off-by: turuslan <[email protected]>
  • Loading branch information
turuslan committed Mar 8, 2024
1 parent ab4c7c4 commit 51e5406
Show file tree
Hide file tree
Showing 17 changed files with 151 additions and 132 deletions.
10 changes: 8 additions & 2 deletions core/parachain/pvf/module_precompiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <future>
#include <ranges>

#include "parachain/pvf/session_params.hpp"
#include "runtime/common/runtime_execution_error.hpp"
#include "runtime/common/runtime_instances_pool.hpp"
#include "runtime/runtime_api/parachain_host.hpp"
Expand Down Expand Up @@ -61,6 +62,8 @@ namespace kagome::parachain {

outcome::result<void> ModulePrecompiler::precompileModulesAt(
const primitives::BlockHash &last_finalized) {
OUTCOME_TRY(executor_params,
sessionParams(*parachain_api_, last_finalized));
auto cores_res = parachain_api_->availability_cores(last_finalized);
if (cores_res.has_error()
&& cores_res.error()
Expand All @@ -84,6 +87,7 @@ namespace kagome::parachain {
std::vector<std::thread> threads;
for (size_t i = 0; i < config_.precompile_threads_num; i++) {
auto compilation_worker = [self = shared_from_this(),
&executor_params,
&cores_queue_mutex,
&cores,
&stats,
Expand All @@ -101,7 +105,7 @@ namespace kagome::parachain {
cores.pop_back();
}
auto res = self->precompileModulesForCore(
stats, last_finalized, ParachainCore{core});
stats, last_finalized, executor_params, ParachainCore{core});
if (!res) {
using namespace std::string_literals;
auto id = get_para_id(core);
Expand Down Expand Up @@ -141,6 +145,7 @@ namespace kagome::parachain {
outcome::result<void> ModulePrecompiler::precompileModulesForCore(
PrecompilationStats &stats,
const primitives::BlockHash &last_finalized,
const runtime::RuntimeContext::ContextParams &executor_params,
const ParachainCore &_core) {
auto &core = _core.state;
if (std::holds_alternative<runtime::FreeCore>(core)) {
Expand Down Expand Up @@ -176,7 +181,8 @@ namespace kagome::parachain {
hash);
stats.total_code_size += code.size();

OUTCOME_TRY(runtime_cache_->instantiateFromCode(hash, code, {}));
OUTCOME_TRY(
runtime_cache_->instantiateFromCode(hash, code, executor_params));
SL_DEBUG(log_,
"Instantiated runtime instance with code hash {} for parachain "
"{}, {} left",
Expand Down
2 changes: 2 additions & 0 deletions core/parachain/pvf/module_precompiler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "log/logger.hpp"
#include "outcome/outcome.hpp"
#include "primitives/block_id.hpp"
#include "runtime/runtime_context.hpp"

namespace kagome::crypto {
class Hasher;
Expand Down Expand Up @@ -47,6 +48,7 @@ namespace kagome::parachain {
outcome::result<void> precompileModulesForCore(
PrecompilationStats &stats,
const primitives::BlockHash &last_finalized,
const runtime::RuntimeContext::ContextParams &executor_params,
const ParachainCore &core);

Config config_;
Expand Down
31 changes: 7 additions & 24 deletions core/parachain/pvf/pvf_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
#include "parachain/pvf/module_precompiler.hpp"
#include "parachain/pvf/pvf_worker_types.hpp"
#include "parachain/pvf/run_worker.hpp"
#include "parachain/pvf/session_params.hpp"
#include "runtime/common/runtime_execution_error.hpp"
#include "runtime/runtime_instances_pool.hpp"
#include "runtime/common/runtime_instances_pool.hpp"
#include "runtime/common/uncompress_code_if_needed.hpp"
#include "runtime/executor.hpp"
#include "runtime/module.hpp"
Expand Down Expand Up @@ -136,8 +137,7 @@ namespace kagome::parachain {
std::shared_ptr<boost::asio::io_context> io_context,
std::shared_ptr<libp2p::basic::Scheduler> scheduler,
std::shared_ptr<crypto::Hasher> hasher,
std::unique_ptr<runtime::RuntimeInstancesPool> instance_pool,
std::shared_ptr<runtime::RuntimePropertiesCache> runtime_properties_cache,
std::shared_ptr<runtime::ModuleFactory> module_factory,
std::shared_ptr<blockchain::BlockTree> block_tree,
std::shared_ptr<crypto::Sr25519Provider> sr25519_provider,
std::shared_ptr<runtime::ParachainHost> parachain_api,
Expand All @@ -149,14 +149,14 @@ namespace kagome::parachain {
io_context_{std::move(io_context)},
scheduler_{std::move(scheduler)},
hasher_{std::move(hasher)},
runtime_properties_cache_{std::move(runtime_properties_cache)},
block_tree_{std::move(block_tree)},
sr25519_provider_{std::move(sr25519_provider)},
parachain_api_{std::move(parachain_api)},
executor_{std::move(executor)},
ctx_factory_{std::move(ctx_factory)},
log_{log::createLogger("PVF Executor", "pvf_executor")},
runtime_cache_{std::move(instance_pool)},
runtime_cache_{std::make_shared<runtime::RuntimeInstancesPoolImpl>(
std::move(module_factory), config_.runtime_instance_cache_size)},
precompiler_{std::make_shared<ModulePrecompiler>(
ModulePrecompiler::Config{config_.precompile_threads_num},
parachain_api_,
Expand Down Expand Up @@ -290,26 +290,9 @@ namespace kagome::parachain {
const common::Hash256 &code_hash,
const ParachainRuntime &code_zstd,
const ValidationParams &params) const {
auto &parent_hash = receipt.descriptor.relay_parent;
OUTCOME_TRY(session_index,
parachain_api_->session_index_for_child(parent_hash));
OUTCOME_TRY(
session_params,
parachain_api_->session_executor_params(parent_hash, session_index));

runtime::RuntimeContext::ContextParams executor_params{};
if (session_params) {
for (auto &param : *session_params) {
if (auto *stack_max = get_if<runtime::StackLogicalMax>(&param)) {
executor_params.memory_limits.max_stack_values_num =
stack_max->max_values_num;
} else if (auto *pages_max =
get_if<runtime::MaxMemoryPages>(&param)) {
executor_params.memory_limits.max_memory_pages_num =
pages_max->limit;
}
}
}
executor_params,
sessionParams(*parachain_api_, receipt.descriptor.relay_parent));

constexpr auto name = "validate_block";
if (not app_configuration_->usePvfSubprocess()) {
Expand Down
11 changes: 3 additions & 8 deletions core/parachain/pvf/pvf_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#include "crypto/sr25519_provider.hpp"
#include "log/logger.hpp"
#include "runtime/runtime_api/parachain_host.hpp"
#include "runtime/runtime_properties_cache.hpp"

namespace boost::asio {
class io_context;
Expand Down Expand Up @@ -70,26 +69,23 @@ namespace kagome::parachain {
std::optional<ParachainRuntime> new_validation_code;
std::vector<UpwardMessage> upward_messages;
std::vector<network::OutboundHorizontal> horizontal_messages;
uint32_t processed_downward_messages;
BlockNumber hrmp_watermark;
uint32_t processed_downward_messages{};
BlockNumber hrmp_watermark{};
};

class PvfImpl : public Pvf, public std::enable_shared_from_this<PvfImpl> {
public:
struct Config {
bool precompile_modules;
size_t runtime_instance_cache_size{16};
size_t max_stack_depth{};
unsigned precompile_threads_num{1};
};

PvfImpl(const Config &config,
std::shared_ptr<boost::asio::io_context> io_context,
std::shared_ptr<libp2p::basic::Scheduler> scheduler,
std::shared_ptr<crypto::Hasher> hasher,
std::unique_ptr<runtime::RuntimeInstancesPool> instance_pool,
std::shared_ptr<runtime::RuntimePropertiesCache>
runtime_properties_cache,
std::shared_ptr<runtime::ModuleFactory> module_factory,
std::shared_ptr<blockchain::BlockTree> block_tree,
std::shared_ptr<crypto::Sr25519Provider> sr25519_provider,
std::shared_ptr<runtime::ParachainHost> parachain_api,
Expand Down Expand Up @@ -131,7 +127,6 @@ namespace kagome::parachain {
std::shared_ptr<boost::asio::io_context> io_context_;
std::shared_ptr<libp2p::basic::Scheduler> scheduler_;
std::shared_ptr<crypto::Hasher> hasher_;
std::shared_ptr<runtime::RuntimePropertiesCache> runtime_properties_cache_;
std::shared_ptr<blockchain::BlockTree> block_tree_;
std::shared_ptr<crypto::Sr25519Provider> sr25519_provider_;
std::shared_ptr<runtime::ParachainHost> parachain_api_;
Expand Down
32 changes: 32 additions & 0 deletions core/parachain/pvf/session_params.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* Copyright Quadrivium LLC
* All Rights Reserved
* SPDX-License-Identifier: Apache-2.0
*/

#pragma once

#include "runtime/runtime_api/parachain_host.hpp"
#include "runtime/runtime_context.hpp"

namespace kagome::parachain {
inline outcome::result<runtime::RuntimeContext::ContextParams> sessionParams(
runtime::ParachainHost &api, const primitives::BlockHash &relay_parent) {
OUTCOME_TRY(session_index, api.session_index_for_child(relay_parent));
OUTCOME_TRY(session_params,
api.session_executor_params(relay_parent, session_index));
runtime::RuntimeContext::ContextParams config;
config.memory_limits.max_stack_values_num =
runtime::RuntimeContext::DEFAULT_STACK_MAX;
if (session_params) {
for (auto &param : *session_params) {
if (auto *stack_max = get_if<runtime::StackLogicalMax>(&param)) {
config.memory_limits.max_stack_values_num = stack_max->max_values_num;
} else if (auto *pages_max = get_if<runtime::MaxMemoryPages>(&param)) {
config.memory_limits.max_memory_pages_num = pages_max->limit;
}
}
}
return config;
}
} // namespace kagome::parachain
2 changes: 1 addition & 1 deletion core/runtime/binaryen/module/module_instance_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ namespace kagome::runtime::binaryen {
std::shared_ptr<RuntimeExternalInterface> rei,
const common::Hash256 &code_hash);

const common::Hash256 &getCodeHash() const override {
common::Hash256 getCodeHash() const override {
return code_hash_;
}

Expand Down
18 changes: 9 additions & 9 deletions core/runtime/common/runtime_instances_pool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ namespace kagome::runtime {
}
}

const common::Hash256 &getCodeHash() const override {
common::Hash256 getCodeHash() const override {
return instance_->getCodeHash();
}

Expand Down Expand Up @@ -71,10 +71,8 @@ namespace kagome::runtime {
};

RuntimeInstancesPoolImpl::RuntimeInstancesPoolImpl(
std::shared_ptr<ModuleFactory> module_factory,
size_t capacity)
: module_factory_{std::move(module_factory)},
pools_{capacity} {
std::shared_ptr<ModuleFactory> module_factory, size_t capacity)
: module_factory_{std::move(module_factory)}, pools_{capacity} {
BOOST_ASSERT(module_factory_);
}

Expand Down Expand Up @@ -126,7 +124,8 @@ namespace kagome::runtime {
if (!uncompressCodeIfNeeded(code_zstd, code)) {
res = CompilationError{"Failed to uncompress code"};
} else {
if (config.memory_limits.max_stack_values_num) {
if (not module_factory_->testDontInstrument()
and config.memory_limits.max_stack_values_num) {
auto instr_res = instrumentWithStackLimiter(
code, *config.memory_limits.max_stack_values_num);
if (!instr_res) {
Expand All @@ -137,9 +136,10 @@ namespace kagome::runtime {
}
}
if (!res) {
res = common::map_result(module_factory_->make(code), [](auto &&module) {
return std::shared_ptr<const Module>(module);
});
res =
common::map_result(module_factory_->make(code), [](auto &&module) {
return std::shared_ptr<const Module>(module);
});
}
}
l.lock();
Expand Down
6 changes: 5 additions & 1 deletion core/runtime/module_factory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace kagome::runtime {
class Module;

struct CompilationError : std::runtime_error {
CompilationError(const std::string& message)
CompilationError(const std::string &message)
: std::runtime_error(message.c_str()) {}

std::string_view message() const {
Expand All @@ -40,6 +40,10 @@ namespace kagome::runtime {

virtual outcome::result<std::shared_ptr<Module>, CompilationError> make(
common::BufferView code) const = 0;

virtual bool testDontInstrument() const {
return false;
}
};

} // namespace kagome::runtime
2 changes: 1 addition & 1 deletion core/runtime/module_instance.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ namespace kagome::runtime {

virtual ~ModuleInstance() = default;

virtual const common::Hash256 &getCodeHash() const = 0;
virtual common::Hash256 getCodeHash() const = 0;

virtual std::shared_ptr<const Module> getModule() const = 0;

Expand Down
3 changes: 1 addition & 2 deletions core/runtime/runtime_context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ namespace kagome::runtime {

struct ContextParams {
SCALE_TIE(1);
MemoryLimits memory_limits{.max_stack_values_num = DEFAULT_STACK_MAX,
.max_memory_pages_num = {}};
MemoryLimits memory_limits;
};

const std::shared_ptr<ModuleInstance> module_instance;
Expand Down
2 changes: 1 addition & 1 deletion core/runtime/wasm_edge/module_factory_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ namespace kagome::runtime::wasm_edge {
BOOST_ASSERT(executor_ != nullptr);
}

const common::Hash256 &getCodeHash() const override {
common::Hash256 getCodeHash() const override {
return code_hash_;
}

Expand Down
2 changes: 1 addition & 1 deletion core/runtime/wavm/module_instance.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ namespace kagome::runtime::wavm {
std::shared_ptr<const CompartmentWrapper> compartment,
const common::Hash256 &code_hash);

const common::Hash256 &getCodeHash() const override {
common::Hash256 getCodeHash() const override {
return code_hash_;
}

Expand Down
Loading

0 comments on commit 51e5406

Please sign in to comment.