Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

metric prepare time #2097

Merged
merged 1 commit into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions core/injector/application_injector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@
#include "parachain/availability/store/store_impl.hpp"
#include "parachain/backing/store_impl.hpp"
#include "parachain/pvf/module_precompiler.hpp"
#include "parachain/pvf/pool.hpp"
#include "parachain/pvf/pvf_impl.hpp"
#include "parachain/pvf/pvf_thread_pool.hpp"
#include "parachain/validator/impl/parachain_observer_impl.hpp"
Expand Down Expand Up @@ -600,8 +601,6 @@ namespace {
config->isOffchainIndexingEnabled()};
parachain::PvfImpl::Config pvf_config{
.precompile_modules = config->shouldPrecompileParachainModules(),
.runtime_instance_cache_size =
config->parachainRuntimeInstanceCacheSize(),
.precompile_threads_num = config->parachainPrecompilationThreadNum(),
};
#if KAGOME_WASM_COMPILER_WASM_EDGE == 1
Expand Down
1 change: 1 addition & 0 deletions core/parachain/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ add_library(validator_parachain
availability/recovery/recovery_impl.cpp
availability/store/store_impl.cpp
backing/store_impl.cpp
pvf/pool.cpp
pvf/precheck.cpp
pvf/pvf_impl.cpp
pvf/module_precompiler.cpp
Expand Down
10 changes: 4 additions & 6 deletions core/parachain/pvf/module_precompiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
#include "parachain/pvf/module_precompiler.hpp"

#include <atomic>
#include <future>
#include <ranges>

#include "parachain/pvf/pool.hpp"
#include "parachain/pvf/session_params.hpp"
#include "runtime/common/runtime_execution_error.hpp"
#include "runtime/common/runtime_instances_pool.hpp"
Expand All @@ -24,11 +23,11 @@ namespace kagome::parachain {
ModulePrecompiler::ModulePrecompiler(
const kagome::parachain::ModulePrecompiler::Config &config,
std::shared_ptr<runtime::ParachainHost> parachain_api,
std::shared_ptr<runtime::RuntimeInstancesPool> runtime_cache,
std::shared_ptr<PvfPool> pvf_pool,
std::shared_ptr<crypto::Hasher> hasher)
: config_{config},
parachain_api_{parachain_api},
runtime_cache_{runtime_cache},
pvf_pool_{std::move(pvf_pool)},
hasher_{hasher} {
if (getThreadsNum() > std::thread::hardware_concurrency() - 1) {
SL_WARN(
Expand Down Expand Up @@ -181,8 +180,7 @@ namespace kagome::parachain {
hash);
stats.total_code_size += code.size();

OUTCOME_TRY(
runtime_cache_->instantiateFromCode(hash, code, executor_params));
OUTCOME_TRY(pvf_pool_->pool()->precompile(hash, code, executor_params));
SL_DEBUG(log_,
"Instantiated runtime instance with code hash {} for parachain "
"{}, {} left",
Expand Down
13 changes: 6 additions & 7 deletions core/parachain/pvf/module_precompiler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ namespace kagome::crypto {

namespace kagome::runtime {
class ParachainHost;
class RuntimeInstancesPool;
} // namespace kagome::runtime

namespace kagome::parachain {
struct ParachainCore;
class PvfPool;

class ModulePrecompiler
: public std::enable_shared_from_this<ModulePrecompiler> {
Expand All @@ -29,11 +29,10 @@ namespace kagome::parachain {
unsigned precompile_threads_num;
};

ModulePrecompiler(
const Config &config,
std::shared_ptr<runtime::ParachainHost> parachain_api,
std::shared_ptr<runtime::RuntimeInstancesPool> runtime_cache,
std::shared_ptr<crypto::Hasher> hasher);
ModulePrecompiler(const Config &config,
std::shared_ptr<runtime::ParachainHost> parachain_api,
std::shared_ptr<PvfPool> pvf_pool,
std::shared_ptr<crypto::Hasher> hasher);

outcome::result<void> precompileModulesAt(
const primitives::BlockHash &last_finalized);
Expand All @@ -53,7 +52,7 @@ namespace kagome::parachain {

Config config_;
std::shared_ptr<runtime::ParachainHost> parachain_api_;
std::shared_ptr<runtime::RuntimeInstancesPool> runtime_cache_;
std::shared_ptr<PvfPool> pvf_pool_;
std::shared_ptr<crypto::Hasher> hasher_;

log::Logger log_ = log::createLogger("ModulePrecompiler", "pvf_executor");
Expand Down
57 changes: 57 additions & 0 deletions core/parachain/pvf/pool.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/**
* Copyright Quadrivium LLC
* All Rights Reserved
* SPDX-License-Identifier: Apache-2.0
*/

#include "parachain/pvf/pool.hpp"

#include "application/app_configuration.hpp"
#include "metrics/histogram_timer.hpp"
#include "runtime/common/runtime_instances_pool.hpp"

namespace kagome::parachain {
inline auto &metric_pvf_preparation_time() {
static metrics::HistogramTimer metric{
"kagome_pvf_preparation_time",
"Time spent in preparing PVF artifacts in seconds",
{
0.1,
0.5,
1.0,
2.0,
3.0,
10.0,
20.0,
30.0,
60.0,
120.0,
240.0,
360.0,
480.0,
},
};
return metric;
}

struct PvfPoolWrapper : runtime::ModuleFactory {
PvfPoolWrapper(std::shared_ptr<runtime::ModuleFactory> inner)
: inner_{std::move(inner)} {}

outcome::result<std::shared_ptr<runtime::Module>, runtime::CompilationError>
make(common::BufferView code) const override {
auto timer = metric_pvf_preparation_time().timer();
return inner_->make(code);
}

std::shared_ptr<runtime::ModuleFactory> inner_;
};

PvfPool::PvfPool(const application::AppConfiguration &app_config,
std::shared_ptr<runtime::ModuleFactory> module_factory,
std::shared_ptr<runtime::InstrumentWasm> instrument)
: pool_{std::make_shared<runtime::RuntimeInstancesPoolImpl>(
std::make_shared<PvfPoolWrapper>(std::move(module_factory)),
std::move(instrument),
app_config.parachainRuntimeInstanceCacheSize())} {}
} // namespace kagome::parachain
38 changes: 38 additions & 0 deletions core/parachain/pvf/pool.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* Copyright Quadrivium LLC
* All Rights Reserved
* SPDX-License-Identifier: Apache-2.0
*/

#pragma once

#include <memory>

namespace kagome::application {
class AppConfiguration;
} // namespace kagome::application

namespace kagome::runtime {
class InstrumentWasm;
class ModuleFactory;
class RuntimeInstancesPoolImpl;
} // namespace kagome::runtime

namespace kagome::parachain {
/**
* Reused by `PvfPrecheck` and `PvfImpl` to measure pvf compile time metric.
*/
class PvfPool {
public:
PvfPool(const application::AppConfiguration &app_config,
std::shared_ptr<runtime::ModuleFactory> module_factory,
std::shared_ptr<runtime::InstrumentWasm> instrument);

auto &pool() const {
return pool_;
}

private:
std::shared_ptr<runtime::RuntimeInstancesPoolImpl> pool_;
};
} // namespace kagome::parachain
39 changes: 8 additions & 31 deletions core/parachain/pvf/precheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,43 +9,23 @@
#include <libp2p/common/final_action.hpp>

#include "blockchain/block_tree.hpp"
#include "metrics/histogram_timer.hpp"
#include "offchain/offchain_worker_factory.hpp"
#include "offchain/offchain_worker_pool.hpp"
#include "parachain/pvf/pool.hpp"
#include "parachain/pvf/pvf_thread_pool.hpp"
#include "runtime/common/uncompress_code_if_needed.hpp"
#include "parachain/pvf/session_params.hpp"
#include "runtime/common/runtime_instances_pool.hpp"
#include "runtime/module.hpp"
#include "runtime/module_factory.hpp"

namespace kagome::parachain {
constexpr size_t kSessions = 3;

metrics::HistogramTimer metric_pvf_preparation_time{
"kagome_pvf_preparation_time",
"Time spent in preparing PVF artifacts in seconds",
{
0.1,
0.5,
1.0,
2.0,
3.0,
10.0,
20.0,
30.0,
60.0,
120.0,
240.0,
360.0,
480.0,
},
};

PvfPrecheck::PvfPrecheck(
std::shared_ptr<crypto::Hasher> hasher,
std::shared_ptr<blockchain::BlockTree> block_tree,
std::shared_ptr<ValidatorSignerFactory> signer_factory,
std::shared_ptr<runtime::ParachainHost> parachain_api,
std::shared_ptr<runtime::ModuleFactory> module_factory,
std::shared_ptr<PvfPool> pvf_pool,
std::shared_ptr<runtime::Executor> executor,
PvfThreadPool &pvf_thread_pool,
std::shared_ptr<offchain::OffchainWorkerFactory> offchain_worker_factory,
Expand All @@ -55,7 +35,7 @@ namespace kagome::parachain {
block_tree_{std::move(block_tree)},
signer_factory_{std::move(signer_factory)},
parachain_api_{std::move(parachain_api)},
module_factory_{std::move(module_factory)},
pvf_pool_{std::move(pvf_pool)},
executor_{std::move(executor)},
offchain_worker_factory_{std::move(offchain_worker_factory)},
offchain_worker_pool_{std::move(offchain_worker_pool)},
Expand All @@ -65,7 +45,6 @@ namespace kagome::parachain {
BOOST_ASSERT(block_tree_ != nullptr);
BOOST_ASSERT(signer_factory_ != nullptr);
BOOST_ASSERT(parachain_api_ != nullptr);
BOOST_ASSERT(module_factory_ != nullptr);
BOOST_ASSERT(executor_ != nullptr);
BOOST_ASSERT(offchain_worker_factory_ != nullptr);
BOOST_ASSERT(offchain_worker_pool_ != nullptr);
Expand Down Expand Up @@ -119,14 +98,12 @@ namespace kagome::parachain {
continue;
}
auto &code_zstd = *code_zstd_res.value();
ParachainRuntime code;
auto timer = metric_pvf_preparation_time.timer();
auto res = [&]() -> outcome::result<void> {
OUTCOME_TRY(runtime::uncompressCodeIfNeeded(code_zstd, code));
OUTCOME_TRY(module_factory_->make(code));
OUTCOME_TRY(config, sessionParams(*parachain_api_, block.hash));
OUTCOME_TRY(
pvf_pool_->pool()->precompile(code_hash, code_zstd, config));
return outcome::success();
}();
timer.reset();
if (res) {
SL_VERBOSE(logger_, "approve {}", code_hash);
} else {
Expand Down
7 changes: 4 additions & 3 deletions core/parachain/pvf/precheck.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@ namespace kagome::parachain {

namespace kagome::runtime {
class Executor;
class ModuleFactory;
} // namespace kagome::runtime

namespace kagome::parachain {
class PvfPool;

/// Signs pvf check statement for every new head.
class PvfPrecheck : public std::enable_shared_from_this<PvfPrecheck> {
public:
Expand All @@ -50,7 +51,7 @@ namespace kagome::parachain {
std::shared_ptr<blockchain::BlockTree> block_tree,
std::shared_ptr<ValidatorSignerFactory> signer_factory,
std::shared_ptr<runtime::ParachainHost> parachain_api,
std::shared_ptr<runtime::ModuleFactory> module_factory,
std::shared_ptr<PvfPool> pvf_pool,
std::shared_ptr<runtime::Executor> executor,
PvfThreadPool &pvf_thread_pool,
std::shared_ptr<offchain::OffchainWorkerFactory>
Expand All @@ -70,7 +71,7 @@ namespace kagome::parachain {
std::shared_ptr<blockchain::BlockTree> block_tree_;
std::shared_ptr<ValidatorSignerFactory> signer_factory_;
std::shared_ptr<runtime::ParachainHost> parachain_api_;
std::shared_ptr<runtime::ModuleFactory> module_factory_;
std::shared_ptr<PvfPool> pvf_pool_;
std::shared_ptr<runtime::Executor> executor_;
std::shared_ptr<offchain::OffchainWorkerFactory> offchain_worker_factory_;
std::shared_ptr<offchain::OffchainWorkerPool> offchain_worker_pool_;
Expand Down
16 changes: 7 additions & 9 deletions core/parachain/pvf/pvf_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "common/visitor.hpp"
#include "metrics/histogram_timer.hpp"
#include "parachain/pvf/module_precompiler.hpp"
#include "parachain/pvf/pool.hpp"
#include "parachain/pvf/pvf_thread_pool.hpp"
#include "parachain/pvf/pvf_worker_types.hpp"
#include "parachain/pvf/run_worker.hpp"
Expand All @@ -23,7 +24,6 @@
#include "runtime/common/uncompress_code_if_needed.hpp"
#include "runtime/executor.hpp"
#include "runtime/module.hpp"
#include "runtime/module_factory.hpp"
#include "runtime/module_repository.hpp"
#include "runtime/runtime_code_provider.hpp"
#include "runtime/runtime_instances_pool.hpp"
Expand Down Expand Up @@ -149,8 +149,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::shared_ptr<runtime::ModuleFactory> module_factory,
std::shared_ptr<runtime::InstrumentWasm> instrument,
std::shared_ptr<PvfPool> pvf_pool,
std::shared_ptr<blockchain::BlockTree> block_tree,
std::shared_ptr<crypto::Sr25519Provider> sr25519_provider,
std::shared_ptr<runtime::ParachainHost> parachain_api,
Expand All @@ -169,14 +168,11 @@ namespace kagome::parachain {
executor_{std::move(executor)},
ctx_factory_{std::move(ctx_factory)},
log_{log::createLogger("PVF Executor", "pvf_executor")},
runtime_cache_{std::make_shared<runtime::RuntimeInstancesPoolImpl>(
std::move(module_factory),
std::move(instrument),
config_.runtime_instance_cache_size)},
pvf_pool_{std::move(pvf_pool)},
precompiler_{std::make_shared<ModulePrecompiler>(
ModulePrecompiler::Config{config_.precompile_threads_num},
parachain_api_,
runtime_cache_,
pvf_pool_,
hasher_)},
pvf_thread_handler_{pvf_thread_pool.handler(*app_state_manager)},
app_configuration_{std::move(app_configuration)} {
Expand Down Expand Up @@ -335,13 +331,15 @@ namespace kagome::parachain {
constexpr auto name = "validate_block";
if (not app_configuration_->usePvfSubprocess()) {
CB_TRY(auto instance,
runtime_cache_->instantiateFromCode(
pvf_pool_->pool()->instantiateFromCode(
code_hash, code_zstd, executor_params));
CB_TRY(auto ctx,
ctx_factory_->ephemeral(
instance, storage::trie::kEmptyRootHash, executor_params));
return cb(executor_->call<ValidationResult>(ctx, name, params));
}
CB_TRYV(
pvf_pool_->pool()->precompile(code_hash, code_zstd, executor_params));

PvfWorkerInput input{
pvf_runtime_engine(*app_configuration_),
Expand Down
Loading
Loading