From a2afadb123b0f87d127c82da2dc430f43b12ddfe Mon Sep 17 00:00:00 2001
From: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
Date: Fri, 3 Jun 2022 21:46:14 +0200
Subject: [PATCH] Add host info to weight templates (#11583)

* Add host info to weight templates

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

* cargo run --quiet --profile=production  --features=runtime-benchmarks --manifest-path=bin/node/cli/Cargo.toml -- benchmark pallet --chain=dev --steps=50 --repeat=20 --pallet=frame_system --extrinsic=* --execution=wasm --wasm-execution=compiled --heap-pages=4096 --output=./frame/system/src/weights.rs --template=./.maintain/frame-weight-template.hbs

Co-authored-by: Parity Bot <admin@parity.io>
---
 substrate/.maintain/frame-weight-template.hbs |  1 +
 substrate/Cargo.lock                          | 11 +++++
 substrate/frame/system/src/weights.rs         | 24 ++++++-----
 .../utils/frame/benchmarking-cli/Cargo.toml   |  1 +
 .../benchmarking-cli/src/overhead/cmd.rs      |  6 ++-
 .../benchmarking-cli/src/overhead/template.rs |  6 +++
 .../benchmarking-cli/src/overhead/weights.hbs |  1 +
 .../frame/benchmarking-cli/src/pallet/mod.rs  |  5 +++
 .../benchmarking-cli/src/pallet/template.hbs  |  1 +
 .../benchmarking-cli/src/pallet/writer.rs     |  4 ++
 .../frame/benchmarking-cli/src/shared/mod.rs  | 43 +++++++++++++++++++
 .../frame/benchmarking-cli/src/storage/cmd.rs |  6 ++-
 .../benchmarking-cli/src/storage/template.rs  |  6 +++
 .../benchmarking-cli/src/storage/weights.hbs  |  1 +
 14 files changed, 103 insertions(+), 13 deletions(-)

diff --git a/substrate/.maintain/frame-weight-template.hbs b/substrate/.maintain/frame-weight-template.hbs
index 34852daf7d4..73608b372eb 100644
--- a/substrate/.maintain/frame-weight-template.hbs
+++ b/substrate/.maintain/frame-weight-template.hbs
@@ -19,6 +19,7 @@
 //!
 //! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION {{version}}
 //! DATE: {{date}}, STEPS: `{{cmd.steps}}`, REPEAT: {{cmd.repeat}}, LOW RANGE: `{{cmd.lowest_range_values}}`, HIGH RANGE: `{{cmd.highest_range_values}}`
+//! HOSTNAME: `{{hostname}}`, CPU: `{{cpuname}}`
 //! EXECUTION: {{cmd.execution}}, WASM-EXECUTION: {{cmd.wasm_execution}}, CHAIN: {{cmd.chain}}, DB CACHE: {{cmd.db_cache}}
 
 // Executed Command:
diff --git a/substrate/Cargo.lock b/substrate/Cargo.lock
index 17d2e825965..42accdb455c 100644
--- a/substrate/Cargo.lock
+++ b/substrate/Cargo.lock
@@ -2112,6 +2112,7 @@ dependencies = [
  "frame-benchmarking",
  "frame-support",
  "frame-system",
+ "gethostname",
  "handlebars",
  "hash-db",
  "hex",
@@ -2596,6 +2597,16 @@ dependencies = [
  "version_check",
 ]
 
+[[package]]
+name = "gethostname"
+version = "0.2.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c1ebd34e35c46e00bb73e81363248d627782724609fe1b6396f553f68fe3862e"
+dependencies = [
+ "libc",
+ "winapi",
+]
+
 [[package]]
 name = "getrandom"
 version = "0.1.16"
diff --git a/substrate/frame/system/src/weights.rs b/substrate/frame/system/src/weights.rs
index bdd2d64b211..a9126e4bf5b 100644
--- a/substrate/frame/system/src/weights.rs
+++ b/substrate/frame/system/src/weights.rs
@@ -18,11 +18,12 @@
 //! Autogenerated weights for frame_system
 //!
 //! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev
-//! DATE: 2022-05-23, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]`
+//! DATE: 2022-06-02, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]`
+//! HOSTNAME: `ci3`, CPU: `Intel(R) Core(TM) i7-7700K CPU @ 4.20GHz`
 //! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 1024
 
 // Executed Command:
-// ./target/production/substrate
+// target/production/substrate
 // benchmark
 // pallet
 // --chain=dev
@@ -32,8 +33,9 @@
 // --extrinsic=*
 // --execution=wasm
 // --wasm-execution=compiled
-// --template=./.maintain/frame-weight-template.hbs
+// --heap-pages=4096
 // --output=./frame/system/src/weights.rs
+// --template=./.maintain/frame-weight-template.hbs
 
 #![cfg_attr(rustfmt, rustfmt_skip)]
 #![allow(unused_parens)]
@@ -66,7 +68,7 @@ impl<T: crate::Config> WeightInfo for SubstrateWeight<T> {
 	// Storage: System Digest (r:1 w:1)
 	// Storage: unknown [0x3a686561707061676573] (r:0 w:1)
 	fn set_heap_pages() -> Weight {
-		(5_453_000 as Weight)
+		(5_367_000 as Weight)
 			.saturating_add(T::DbWeight::get().reads(1 as Weight))
 			.saturating_add(T::DbWeight::get().writes(2 as Weight))
 	}
@@ -74,21 +76,21 @@ impl<T: crate::Config> WeightInfo for SubstrateWeight<T> {
 	fn set_storage(i: u32, ) -> Weight {
 		(0 as Weight)
 			// Standard Error: 1_000
-			.saturating_add((600_000 as Weight).saturating_mul(i as Weight))
+			.saturating_add((603_000 as Weight).saturating_mul(i as Weight))
 			.saturating_add(T::DbWeight::get().writes((1 as Weight).saturating_mul(i as Weight)))
 	}
 	// Storage: Skipped Metadata (r:0 w:0)
 	fn kill_storage(i: u32, ) -> Weight {
 		(0 as Weight)
 			// Standard Error: 1_000
-			.saturating_add((510_000 as Weight).saturating_mul(i as Weight))
+			.saturating_add((513_000 as Weight).saturating_mul(i as Weight))
 			.saturating_add(T::DbWeight::get().writes((1 as Weight).saturating_mul(i as Weight)))
 	}
 	// Storage: Skipped Metadata (r:0 w:0)
 	fn kill_prefix(p: u32, ) -> Weight {
 		(0 as Weight)
 			// Standard Error: 1_000
-			.saturating_add((916_000 as Weight).saturating_mul(p as Weight))
+			.saturating_add((1_026_000 as Weight).saturating_mul(p as Weight))
 			.saturating_add(T::DbWeight::get().writes((1 as Weight).saturating_mul(p as Weight)))
 	}
 }
@@ -106,7 +108,7 @@ impl WeightInfo for () {
 	// Storage: System Digest (r:1 w:1)
 	// Storage: unknown [0x3a686561707061676573] (r:0 w:1)
 	fn set_heap_pages() -> Weight {
-		(5_453_000 as Weight)
+		(5_367_000 as Weight)
 			.saturating_add(RocksDbWeight::get().reads(1 as Weight))
 			.saturating_add(RocksDbWeight::get().writes(2 as Weight))
 	}
@@ -114,21 +116,21 @@ impl WeightInfo for () {
 	fn set_storage(i: u32, ) -> Weight {
 		(0 as Weight)
 			// Standard Error: 1_000
-			.saturating_add((600_000 as Weight).saturating_mul(i as Weight))
+			.saturating_add((603_000 as Weight).saturating_mul(i as Weight))
 			.saturating_add(RocksDbWeight::get().writes((1 as Weight).saturating_mul(i as Weight)))
 	}
 	// Storage: Skipped Metadata (r:0 w:0)
 	fn kill_storage(i: u32, ) -> Weight {
 		(0 as Weight)
 			// Standard Error: 1_000
-			.saturating_add((510_000 as Weight).saturating_mul(i as Weight))
+			.saturating_add((513_000 as Weight).saturating_mul(i as Weight))
 			.saturating_add(RocksDbWeight::get().writes((1 as Weight).saturating_mul(i as Weight)))
 	}
 	// Storage: Skipped Metadata (r:0 w:0)
 	fn kill_prefix(p: u32, ) -> Weight {
 		(0 as Weight)
 			// Standard Error: 1_000
-			.saturating_add((916_000 as Weight).saturating_mul(p as Weight))
+			.saturating_add((1_026_000 as Weight).saturating_mul(p as Weight))
 			.saturating_add(RocksDbWeight::get().writes((1 as Weight).saturating_mul(p as Weight)))
 	}
 }
diff --git a/substrate/utils/frame/benchmarking-cli/Cargo.toml b/substrate/utils/frame/benchmarking-cli/Cargo.toml
index 5f30e21b298..c4549f5c543 100644
--- a/substrate/utils/frame/benchmarking-cli/Cargo.toml
+++ b/substrate/utils/frame/benchmarking-cli/Cargo.toml
@@ -56,6 +56,7 @@ sp-runtime = { version = "6.0.0", path = "../../../primitives/runtime" }
 sp-state-machine = { version = "0.12.0", path = "../../../primitives/state-machine" }
 sp-storage = { version = "6.0.0", path = "../../../primitives/storage" }
 sp-trie = { version = "6.0.0", path = "../../../primitives/trie" }
+gethostname = "0.2.3"
 
 [features]
 default = ["db", "sc-client-db/runtime-benchmarks"]
diff --git a/substrate/utils/frame/benchmarking-cli/src/overhead/cmd.rs b/substrate/utils/frame/benchmarking-cli/src/overhead/cmd.rs
index 3cf28198686..357c89d97a5 100644
--- a/substrate/utils/frame/benchmarking-cli/src/overhead/cmd.rs
+++ b/substrate/utils/frame/benchmarking-cli/src/overhead/cmd.rs
@@ -35,7 +35,7 @@ use crate::{
 		bench::{Benchmark, BenchmarkParams, BenchmarkType},
 		template::TemplateData,
 	},
-	shared::WeightParams,
+	shared::{HostInfoParams, WeightParams},
 };
 
 /// Benchmark the execution overhead per-block and per-extrinsic.
@@ -64,6 +64,10 @@ pub struct OverheadParams {
 	#[allow(missing_docs)]
 	#[clap(flatten)]
 	pub bench: BenchmarkParams,
+
+	#[allow(missing_docs)]
+	#[clap(flatten)]
+	pub hostinfo: HostInfoParams,
 }
 
 /// Used by the benchmark to build signed extrinsics.
diff --git a/substrate/utils/frame/benchmarking-cli/src/overhead/template.rs b/substrate/utils/frame/benchmarking-cli/src/overhead/template.rs
index 44e2c1f02e3..33c2c799903 100644
--- a/substrate/utils/frame/benchmarking-cli/src/overhead/template.rs
+++ b/substrate/utils/frame/benchmarking-cli/src/overhead/template.rs
@@ -47,6 +47,10 @@ pub(crate) struct TemplateData {
 	version: String,
 	/// Date that the template was filled out.
 	date: String,
+	/// Hostname of the machine that executed the benchmarks.
+	hostname: String,
+	/// CPU name of the machine that executed the benchmarks.
+	cpuname: String,
 	/// Command line arguments that were passed to the CLI.
 	args: Vec<String>,
 	/// Params of the executed command.
@@ -73,6 +77,8 @@ impl TemplateData {
 			runtime_name: cfg.chain_spec.name().into(),
 			version: VERSION.into(),
 			date: chrono::Utc::now().format("%Y-%m-%d (Y/M/D)").to_string(),
+			hostname: params.hostinfo.hostname(),
+			cpuname: params.hostinfo.cpuname(),
 			args: env::args().collect::<Vec<String>>(),
 			params: params.clone(),
 			stats: stats.clone(),
diff --git a/substrate/utils/frame/benchmarking-cli/src/overhead/weights.hbs b/substrate/utils/frame/benchmarking-cli/src/overhead/weights.hbs
index 6d3ae471d1c..f8312b00525 100644
--- a/substrate/utils/frame/benchmarking-cli/src/overhead/weights.hbs
+++ b/substrate/utils/frame/benchmarking-cli/src/overhead/weights.hbs
@@ -17,6 +17,7 @@
 
 //! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION {{version}}
 //! DATE: {{date}}
+//! HOSTNAME: `{{hostname}}`, CPU: `{{cpuname}}`
 //!
 //! SHORT-NAME: `{{short_name}}`, LONG-NAME: `{{long_name}}`, RUNTIME: `{{runtime_name}}`
 //! WARMUPS: `{{params.bench.warmup}}`, REPEAT: `{{params.bench.repeat}}`
diff --git a/substrate/utils/frame/benchmarking-cli/src/pallet/mod.rs b/substrate/utils/frame/benchmarking-cli/src/pallet/mod.rs
index 227c9b2f8a7..7beaf321a29 100644
--- a/substrate/utils/frame/benchmarking-cli/src/pallet/mod.rs
+++ b/substrate/utils/frame/benchmarking-cli/src/pallet/mod.rs
@@ -18,6 +18,7 @@
 mod command;
 mod writer;
 
+use crate::shared::HostInfoParams;
 use sc_cli::{
 	ExecutionStrategy, WasmExecutionMethod, WasmtimeInstantiationStrategy,
 	DEFAULT_WASMTIME_INSTANTIATION_STRATEGY, DEFAULT_WASM_EXECUTION_METHOD,
@@ -91,6 +92,10 @@ pub struct PalletCmd {
 	#[clap(long)]
 	pub template: Option<PathBuf>,
 
+	#[allow(missing_docs)]
+	#[clap(flatten)]
+	pub hostinfo_params: HostInfoParams,
+
 	/// Which analysis function to use when outputting benchmarks:
 	/// * min-squares (default)
 	/// * median-slopes
diff --git a/substrate/utils/frame/benchmarking-cli/src/pallet/template.hbs b/substrate/utils/frame/benchmarking-cli/src/pallet/template.hbs
index ea734e16591..a2f75c7b0a3 100644
--- a/substrate/utils/frame/benchmarking-cli/src/pallet/template.hbs
+++ b/substrate/utils/frame/benchmarking-cli/src/pallet/template.hbs
@@ -3,6 +3,7 @@
 //!
 //! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION {{version}}
 //! DATE: {{date}}, STEPS: `{{cmd.steps}}`, REPEAT: {{cmd.repeat}}, LOW RANGE: `{{cmd.lowest_range_values}}`, HIGH RANGE: `{{cmd.highest_range_values}}`
+//! HOSTNAME: `{{hostname}}`, CPU: `{{cpuname}}`
 //! EXECUTION: {{cmd.execution}}, WASM-EXECUTION: {{cmd.wasm_execution}}, CHAIN: {{cmd.chain}}, DB CACHE: {{cmd.db_cache}}
 
 // Executed Command:
diff --git a/substrate/utils/frame/benchmarking-cli/src/pallet/writer.rs b/substrate/utils/frame/benchmarking-cli/src/pallet/writer.rs
index 515582d8c6d..dc38fbcfc17 100644
--- a/substrate/utils/frame/benchmarking-cli/src/pallet/writer.rs
+++ b/substrate/utils/frame/benchmarking-cli/src/pallet/writer.rs
@@ -43,6 +43,8 @@ const TEMPLATE: &str = include_str!("./template.hbs");
 struct TemplateData {
 	args: Vec<String>,
 	date: String,
+	hostname: String,
+	cpuname: String,
 	version: String,
 	pallet: String,
 	instance: String,
@@ -322,6 +324,8 @@ pub fn write_results(
 		let hbs_data = TemplateData {
 			args: args.clone(),
 			date: date.clone(),
+			hostname: cmd.hostinfo_params.hostname(),
+			cpuname: cmd.hostinfo_params.cpuname(),
 			version: VERSION.to_string(),
 			pallet: pallet.to_string(),
 			instance: instance.to_string(),
diff --git a/substrate/utils/frame/benchmarking-cli/src/shared/mod.rs b/substrate/utils/frame/benchmarking-cli/src/shared/mod.rs
index f959c285a34..33189792c40 100644
--- a/substrate/utils/frame/benchmarking-cli/src/shared/mod.rs
+++ b/substrate/utils/frame/benchmarking-cli/src/shared/mod.rs
@@ -25,7 +25,10 @@ pub use record::BenchRecord;
 pub use stats::{StatSelect, Stats};
 pub use weight_params::WeightParams;
 
+use clap::Args;
 use rand::prelude::*;
+use sc_sysinfo::gather_sysinfo;
+use serde::Serialize;
 
 /// A Handlebars helper to add an underscore after every 3rd character,
 /// i.e. a separator for large numbers.
@@ -89,3 +92,43 @@ pub fn check_build_profile() -> Result<(), String> {
 		Ok(())
 	}
 }
+
+/// Parameters to configure how the host info will be determined.
+#[derive(Debug, Default, Serialize, Clone, PartialEq, Args)]
+#[clap(rename_all = "kebab-case")]
+pub struct HostInfoParams {
+	/// Manually override the hostname to use.
+	#[clap(long)]
+	pub hostname_override: Option<String>,
+
+	/// Specify a fallback hostname if no-one could be detected automatically.
+	///
+	/// Note: This only exists to make the `hostname` function infallible.
+	#[clap(long, default_value = "<UNKNOWN>")]
+	pub hostname_fallback: String,
+
+	/// Specify a fallback CPU name if no-one could be detected automatically.
+	///
+	/// Note: This only exists to make the `cpuname` function infallible.
+	#[clap(long, default_value = "<UNKNOWN>")]
+	pub cpuname_fallback: String,
+}
+
+impl HostInfoParams {
+	/// Returns the hostname of the machine.
+	///
+	/// Can be used to track on which machine a benchmark was run.
+	pub fn hostname(&self) -> String {
+		self.hostname_override
+			.clone()
+			.or(gethostname::gethostname().into_string().ok())
+			.unwrap_or(self.hostname_fallback.clone())
+	}
+
+	/// Returns the CPU name of the current machine.
+	///
+	/// Can be used to track on which machine a benchmark was run.
+	pub fn cpuname(&self) -> String {
+		gather_sysinfo().cpu.unwrap_or(self.cpuname_fallback.clone())
+	}
+}
diff --git a/substrate/utils/frame/benchmarking-cli/src/storage/cmd.rs b/substrate/utils/frame/benchmarking-cli/src/storage/cmd.rs
index 23222dbd120..b8264dc0092 100644
--- a/substrate/utils/frame/benchmarking-cli/src/storage/cmd.rs
+++ b/substrate/utils/frame/benchmarking-cli/src/storage/cmd.rs
@@ -34,7 +34,7 @@ use sp_runtime::generic::BlockId;
 use std::{fmt::Debug, path::PathBuf, sync::Arc};
 
 use super::template::TemplateData;
-use crate::shared::{new_rng, WeightParams};
+use crate::shared::{new_rng, HostInfoParams, WeightParams};
 
 /// Benchmark the storage speed of a chain snapshot.
 #[derive(Debug, Parser)]
@@ -63,6 +63,10 @@ pub struct StorageParams {
 	#[clap(flatten)]
 	pub weight_params: WeightParams,
 
+	#[allow(missing_docs)]
+	#[clap(flatten)]
+	pub hostinfo: HostInfoParams,
+
 	/// Skip the `read` benchmark.
 	#[clap(long)]
 	pub skip_read: bool,
diff --git a/substrate/utils/frame/benchmarking-cli/src/storage/template.rs b/substrate/utils/frame/benchmarking-cli/src/storage/template.rs
index 8365c3841d4..20fbd58134f 100644
--- a/substrate/utils/frame/benchmarking-cli/src/storage/template.rs
+++ b/substrate/utils/frame/benchmarking-cli/src/storage/template.rs
@@ -41,6 +41,10 @@ pub(crate) struct TemplateData {
 	version: String,
 	/// Date that the template was filled out.
 	date: String,
+	/// Hostname of the machine that executed the benchmarks.
+	hostname: String,
+	/// CPU name of the machine that executed the benchmarks.
+	cpuname: String,
 	/// Command line arguments that were passed to the CLI.
 	args: Vec<String>,
 	/// Storage params of the executed command.
@@ -65,6 +69,8 @@ impl TemplateData {
 			runtime_name: cfg.chain_spec.name().into(),
 			version: VERSION.into(),
 			date: chrono::Utc::now().format("%Y-%m-%d (Y/M/D)").to_string(),
+			hostname: params.hostinfo.hostname(),
+			cpuname: params.hostinfo.cpuname(),
 			args: env::args().collect::<Vec<String>>(),
 			params: params.clone(),
 			..Default::default()
diff --git a/substrate/utils/frame/benchmarking-cli/src/storage/weights.hbs b/substrate/utils/frame/benchmarking-cli/src/storage/weights.hbs
index 51878141d14..8c19aaa0dff 100644
--- a/substrate/utils/frame/benchmarking-cli/src/storage/weights.hbs
+++ b/substrate/utils/frame/benchmarking-cli/src/storage/weights.hbs
@@ -17,6 +17,7 @@
 
 //! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION {{version}}
 //! DATE: {{date}}
+//! HOSTNAME: `{{hostname}}`, CPU: `{{cpuname}}`
 //!
 //! DATABASE: `{{db_name}}`, RUNTIME: `{{runtime_name}}`
 //! BLOCK-NUM: `{{block_number}}`
-- 
GitLab