Files
pezkuwi-subxt/substrate/client/sysinfo/src/lib.rs
T
Oliver Tale-Yazdi 9980d314b1 Follow ups for benchmark machine (#11270)
* Follow ups for the MachineCmd

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Fix CI

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Review fixes

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Add to node-template

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Fix test with feature flag

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Review fixes

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Lower disk requirements

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Add ExecutionLimit to the disk benchmarks

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* fmt

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Add doc

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Review fixes

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Rename DISK_WRITE_LIMIT -> DEFAULT_DISK_EXECUTION_LIMIT

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Rename POLKADOT_REFERENCE_HARDWARE -> SUBSTRATE_REFERENCE_HARDWARE

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Fix build profile + add license

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Remove deps

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Set tolerance to 10%

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Fix tests

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Ignore test

I cannot reproduce the CI error, even with the full command:
cargo test --workspace --locked --release --verbose --features runtime-benchmarks --manifest-path ./bin/node/cli/Cargo.toml

I will put an 'ignore' on that test for now, since it works for me and is worth having.

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Remove test

Still cannot reproduce the error and it fails in the CI.
Removing it now.

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

Co-authored-by: Shawn Tabrizi <shawntabrizi@gmail.com>
2022-04-26 14:31:26 +00:00

153 lines
5.3 KiB
Rust

// This file is part of Substrate.
// Copyright (C) 2022 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//! This crate contains the code necessary to gather basic hardware
//! and software telemetry information about the node on which we're running.
use futures::prelude::*;
use std::time::Duration;
mod sysinfo;
#[cfg(target_os = "linux")]
mod sysinfo_linux;
pub use sysinfo::{
benchmark_cpu, benchmark_disk_random_writes, benchmark_disk_sequential_writes,
benchmark_memory, benchmark_sr25519_verify, gather_hwbench, gather_sysinfo,
};
/// The operating system part of the current target triplet.
pub const TARGET_OS: &str = include_str!(concat!(env!("OUT_DIR"), "/target_os.txt"));
/// The CPU ISA architecture part of the current target triplet.
pub const TARGET_ARCH: &str = include_str!(concat!(env!("OUT_DIR"), "/target_arch.txt"));
/// The environment part of the current target triplet.
pub const TARGET_ENV: &str = include_str!(concat!(env!("OUT_DIR"), "/target_env.txt"));
/// Hardware benchmark results for the node.
#[derive(Clone, Debug, serde::Serialize)]
pub struct HwBench {
/// The CPU speed, as measured in how many MB/s it can hash using the BLAKE2b-256 hash.
pub cpu_hashrate_score: u64,
/// Memory bandwidth in MB/s, calculated by measuring the throughput of `memcpy`.
pub memory_memcpy_score: u64,
/// Sequential disk write speed in MB/s.
pub disk_sequential_write_score: Option<u64>,
/// Random disk write speed in MB/s.
pub disk_random_write_score: Option<u64>,
}
/// Limit the execution time of a benchmark.
pub enum ExecutionLimit {
/// Limit by the maximal duration.
MaxDuration(Duration),
/// Limit by the maximal number of iterations.
MaxIterations(usize),
/// Limit by the maximal duration and maximal number of iterations.
Both { max_iterations: usize, max_duration: Duration },
}
impl ExecutionLimit {
/// Creates a new execution limit with the passed seconds as duration limit.
pub fn from_secs_f32(secs: f32) -> Self {
Self::MaxDuration(Duration::from_secs_f32(secs))
}
/// Returns the duration limit or `MAX` if none is present.
pub fn max_duration(&self) -> Duration {
match self {
Self::MaxDuration(d) => *d,
Self::Both { max_duration, .. } => *max_duration,
_ => Duration::from_secs(u64::MAX),
}
}
/// Returns the iterations limit or `MAX` if none is present.
pub fn max_iterations(&self) -> usize {
match self {
Self::MaxIterations(d) => *d,
Self::Both { max_iterations, .. } => *max_iterations,
_ => usize::MAX,
}
}
}
/// Prints out the system software/hardware information in the logs.
pub fn print_sysinfo(sysinfo: &sc_telemetry::SysInfo) {
log::info!("💻 Operating system: {}", TARGET_OS);
log::info!("💻 CPU architecture: {}", TARGET_ARCH);
if !TARGET_ENV.is_empty() {
log::info!("💻 Target environment: {}", TARGET_ENV);
}
if let Some(ref cpu) = sysinfo.cpu {
log::info!("💻 CPU: {}", cpu);
}
if let Some(core_count) = sysinfo.core_count {
log::info!("💻 CPU cores: {}", core_count);
}
if let Some(memory) = sysinfo.memory {
log::info!("💻 Memory: {}MB", memory / (1024 * 1024));
}
if let Some(ref linux_kernel) = sysinfo.linux_kernel {
log::info!("💻 Kernel: {}", linux_kernel);
}
if let Some(ref linux_distro) = sysinfo.linux_distro {
log::info!("💻 Linux distribution: {}", linux_distro);
}
if let Some(is_virtual_machine) = sysinfo.is_virtual_machine {
log::info!("💻 Virtual machine: {}", if is_virtual_machine { "yes" } else { "no" });
}
}
/// Prints out the results of the hardware benchmarks in the logs.
pub fn print_hwbench(hwbench: &HwBench) {
log::info!("🏁 CPU score: {}MB/s", hwbench.cpu_hashrate_score);
log::info!("🏁 Memory score: {}MB/s", hwbench.memory_memcpy_score);
if let Some(score) = hwbench.disk_sequential_write_score {
log::info!("🏁 Disk score (seq. writes): {}MB/s", score);
}
if let Some(score) = hwbench.disk_random_write_score {
log::info!("🏁 Disk score (rand. writes): {}MB/s", score);
}
}
/// Initializes the hardware benchmarks telemetry.
pub fn initialize_hwbench_telemetry(
telemetry_handle: sc_telemetry::TelemetryHandle,
hwbench: HwBench,
) -> impl std::future::Future<Output = ()> {
let mut connect_stream = telemetry_handle.on_connect_stream();
async move {
let payload = serde_json::to_value(&hwbench)
.expect("the `HwBench` can always be serialized into a JSON object; qed");
let mut payload = match payload {
serde_json::Value::Object(map) => map,
_ => unreachable!("the `HwBench` always serializes into a JSON object; qed"),
};
payload.insert("msg".into(), "sysinfo.hwbench".into());
while connect_stream.next().await.is_some() {
telemetry_handle.send_telemetry(sc_telemetry::SUBSTRATE_INFO, payload.clone());
}
}
}