From bfbe1fe18b9d380c62d90d9c4191c8ab6ad9952d Mon Sep 17 00:00:00 2001
From: Guanqun Lu <guanqun.lu@gmail.com>
Date: Wed, 15 Aug 2018 16:04:44 +0800
Subject: [PATCH] send memory usage and cpu usage to telemetry (#499)

* send memory usage and cpu usage to telemetry

Fixes #443, however, it doesn't send IO usage, as it's not available in
this crate.

* fixes according to Gav's comments

* fix grumbles, send basictypes in telemetry
---
 substrate/Cargo.lock                     | 13 +++++++++++++
 substrate/substrate/cli/Cargo.toml       |  1 +
 substrate/substrate/cli/src/informant.rs | 16 +++++++++++++++-
 substrate/substrate/cli/src/lib.rs       |  1 +
 substrate/substrate/telemetry/src/lib.rs |  2 +-
 5 files changed, 31 insertions(+), 2 deletions(-)

diff --git a/substrate/Cargo.lock b/substrate/Cargo.lock
index 3a14f985466..e044fbd7d15 100644
--- a/substrate/Cargo.lock
+++ b/substrate/Cargo.lock
@@ -2680,6 +2680,7 @@ dependencies = [
  "substrate-runtime-primitives 0.1.0",
  "substrate-service 0.3.0",
  "substrate-telemetry 0.3.0",
+ "sysinfo 0.5.7 (registry+https://github.com/rust-lang/crates.io-index)",
  "time 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)",
  "tokio 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
@@ -3328,6 +3329,17 @@ dependencies = [
  "unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
 
+[[package]]
+name = "sysinfo"
+version = "0.5.7"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "cfg-if 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
+ "libc 0.2.41 (registry+https://github.com/rust-lang/crates.io-index)",
+ "rayon 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
+ "winapi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
 [[package]]
 name = "take"
 version = "0.1.0"
@@ -4222,6 +4234,7 @@ dependencies = [
 "checksum strsim 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bb4f380125926a99e52bc279241539c018323fab05ad6368b56f93d9369ff550"
 "checksum subtle 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "dc7f6353c2ee5407358d063a14cccc1630804527090a6fb5a9489ce4924280fb"
 "checksum syn 0.14.1 (registry+https://github.com/rust-lang/crates.io-index)" = "6dfd71b2be5a58ee30a6f8ea355ba8290d397131c00dfa55c3d34e6e13db5101"
+"checksum sysinfo 0.5.7 (registry+https://github.com/rust-lang/crates.io-index)" = "394abcf30852ac00878ab01642b13668db48d166d945f250c7bdbb9e12d75ad0"
 "checksum take 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b157868d8ac1f56b64604539990685fa7611d8fa9e5476cf0c02cf34d32917c5"
 "checksum take_mut 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "f764005d11ee5f36500a149ace24e00e3da98b0158b3e2d53a7495660d3f4d60"
 "checksum target_info 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c63f48baada5c52e65a29eef93ab4f8982681b67f9e8d29c7b05abcfec2b9ffe"
diff --git a/substrate/substrate/cli/Cargo.toml b/substrate/substrate/cli/Cargo.toml
index 723c568ba32..176d59bbe75 100644
--- a/substrate/substrate/cli/Cargo.toml
+++ b/substrate/substrate/cli/Cargo.toml
@@ -22,6 +22,7 @@ tokio = "0.1.7"
 futures = "0.1.17"
 fdlimit = "0.1"
 exit-future = "0.1"
+sysinfo = "0.5.7"
 substrate-client = { path = "../../substrate/client" }
 substrate-extrinsic-pool = { path = "../../substrate/extrinsic-pool" }
 substrate-network = { path = "../../substrate/network" }
diff --git a/substrate/substrate/cli/src/informant.rs b/substrate/substrate/cli/src/informant.rs
index d980e74bb81..2cc61a8e83e 100644
--- a/substrate/substrate/cli/src/informant.rs
+++ b/substrate/substrate/cli/src/informant.rs
@@ -22,6 +22,7 @@ use futures::{Future, Stream};
 use service::{Service, Components};
 use tokio::runtime::TaskExecutor;
 use tokio::timer::Interval;
+use sysinfo::{get_current_pid, ProcessExt, System, SystemExt};
 use network::{SyncState, SyncProvider};
 use client::BlockchainEvents;
 use runtime_primitives::traits::{Header, As};
@@ -41,6 +42,9 @@ pub fn start<C>(service: &Service<C>, exit: ::exit_future::Exit, handle: TaskExe
 	let txpool = service.extrinsic_pool();
 	let mut last_number = None;
 
+	let mut sys = System::new();
+	let self_pid = get_current_pid();
+
 	let display_notifications = interval.map_err(|e| debug!("Timer error: {:?}", e)).for_each(move |_| {
 		let sync_status = network.status();
 
@@ -65,17 +69,27 @@ pub fn start<C>(service: &Service<C>, exit: ::exit_future::Exit, handle: TaskExe
 				Colour::White.paint(format!("{}", best_number)),
 				hash
 			);
+
+			// get cpu usage and memory usage of this process
+			let (cpu_usage, memory) = if sys.refresh_process(self_pid) {
+				let proc = sys.get_process(self_pid).expect("Above refresh_process succeeds, this should be Some(), qed");
+				(proc.cpu_usage(), proc.memory())
+			} else { (0.0, 0) };
+
 			telemetry!(
 				"system.interval";
 				"status" => format!("{}{}", status, target),
 				"peers" => num_peers,
 				"height" => best_number,
 				"best" => ?hash,
-				"txcount" => txpool_status.transaction_count
+				"txcount" => txpool_status.transaction_count,
+				"cpu" => cpu_usage,
+				"memory" => memory
 			);
 		} else {
 			warn!("Error getting best block information");
 		}
+
 		Ok(())
 	});
 
diff --git a/substrate/substrate/cli/src/lib.rs b/substrate/substrate/cli/src/lib.rs
index a1064cccc44..f2dee51e454 100644
--- a/substrate/substrate/cli/src/lib.rs
+++ b/substrate/substrate/cli/src/lib.rs
@@ -30,6 +30,7 @@ extern crate futures;
 extern crate tokio;
 extern crate names;
 extern crate backtrace;
+extern crate sysinfo;
 
 extern crate substrate_client as client;
 extern crate substrate_network as network;
diff --git a/substrate/substrate/telemetry/src/lib.rs b/substrate/substrate/telemetry/src/lib.rs
index 4782f244e11..1054b7b397d 100644
--- a/substrate/substrate/telemetry/src/lib.rs
+++ b/substrate/substrate/telemetry/src/lib.rs
@@ -14,7 +14,7 @@
 // You should have received a copy of the GNU General Public License
 // along with Substrate.  If not, see <http://www.gnu.org/licenses/>.
 
-//! Telemtetry utils.
+//! Telemetry utils.
 //! 
 //! `telemetry` macro be used from whereever in the Substrate codebase
 //! in order to send real-time logging information to the telemetry
-- 
GitLab