Skip to content

Commit

Permalink
feat: show cpu gpu temperatures (#47)
Browse files Browse the repository at this point in the history
Description
---
Implement monitoring and display for cpu and gpu temperatures
Motivation and Context

---
As mining is heavy operation for hardware we want to see at least their
temperatures to know if it safe to continue mining as high temperatures
may cause damages

How Has This Been Tested?
---
Manually
What process can a PR reviewer use to test or verify this change?

---
Open settings tab and you should see Hardware temperatures category
<!-- Checklist -->
<!-- 1. Is the title of your PR in the form that would make nice release
notes? The title, excluding the conventional commit
tag, will be included exactly as is in the CHANGELOG, so please think
about it carefully. -->


Breaking Changes
---

- [x] None
- [ ] Requires data directory on base node to be deleted
- [ ] Requires hard fork
- [ ] Other - Please specify

<!-- Does this include a breaking change? If so, include this line as a
footer -->
<!-- BREAKING CHANGE: Description what the user should do, e.g. delete a
database, resync the chain -->
  • Loading branch information
Misieq01 authored Aug 13, 2024
1 parent f725d7b commit eae24b3
Show file tree
Hide file tree
Showing 11 changed files with 397 additions and 19 deletions.
46 changes: 46 additions & 0 deletions src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ libsqlite3-sys = { version = "0.25.1", features = ["bundled"] }
log = "0.4.22"
rand = "0.8.5"
device_query = "2.1.0"
nvml-wrapper = "0.10.0"

[features]
# This feature is used for production builds or when a dev server is not specified, DO NOT REMOVE!!
Expand Down
62 changes: 59 additions & 3 deletions src-tauri/src/cpu_miner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ use crate::mm_proxy_manager::MmProxyManager;
use crate::xmrig::http_api::XmrigHttpApiClient;
use crate::xmrig_adapter::{XmrigAdapter, XmrigNodeConnection};
use crate::{
CpuMinerConfig, CpuMinerConnection, CpuMinerConnectionStatus, CpuMinerStatus, ProgressTracker,
CpuCoreTemperature, CpuMinerConfig, CpuMinerConnection, CpuMinerConnectionStatus,
CpuMinerStatus, ProgressTracker,
};
use log::warn;
use std::path::PathBuf;
use sysinfo::{CpuRefreshKind, RefreshKind, System};
use sysinfo::{Component, Components, CpuRefreshKind, RefreshKind, System};
use tari_core::transactions::tari_amount::MicroMinotari;
use tari_shutdown::{Shutdown, ShutdownSignal};
use tauri::async_runtime::JoinHandle;
Expand All @@ -26,6 +27,7 @@ pub(crate) struct CpuMiner {
watcher_task: Option<JoinHandle<Result<(), anyhow::Error>>>,
miner_shutdown: Shutdown,
api_client: Option<XmrigHttpApiClient>,
cpu_temperatures: Vec<CpuCoreTemperature>,
}

impl CpuMiner {
Expand All @@ -34,6 +36,7 @@ impl CpuMiner {
watcher_task: None,
miner_shutdown: Shutdown::new(),
api_client: None,
cpu_temperatures: Vec::new(),
}
}

Expand Down Expand Up @@ -156,10 +159,60 @@ impl CpuMiner {
}

pub async fn status(
&self,
&mut self,
network_hash_rate: u64,
block_reward: MicroMinotari,
) -> Result<CpuMinerStatus, anyhow::Error> {
let components = Components::new_with_refreshed_list();

let cpu_components: Vec<&Component> = components
.iter()
.filter(|component| component.label().contains("Core"))
.collect();

let mut cpu_temperatures: Vec<CpuCoreTemperature> = cpu_components
.iter()
.map(|component| CpuCoreTemperature {
id: component
.label()
.split(" ")
.last()
.unwrap()
.parse()
.unwrap(),
label: component
.label()
.split(" ")
.skip(1)
.collect::<Vec<&str>>()
.join(" ")
.to_string(),
temperature: component.temperature(),
max_temperature: component.max(),
})
.collect();

cpu_temperatures.sort();

if self.cpu_temperatures.is_empty() {
self.cpu_temperatures = cpu_temperatures.clone()
} else {
for (i, component) in cpu_temperatures.clone().iter().enumerate() {
let position = self
.cpu_temperatures
.iter()
.position(|x| x.id == component.id)
.unwrap();
self.cpu_temperatures[position].temperature = component.temperature;
if component.temperature > self.cpu_temperatures[position].max_temperature {
self.cpu_temperatures[position].max_temperature =
self.cpu_temperatures[i].temperature;
}
}
}

self.cpu_temperatures.sort();

let mut s =
System::new_with_specifics(RefreshKind::new().with_cpu(CpuRefreshKind::everything()));

Expand All @@ -171,6 +224,7 @@ impl CpuMiner {
let cpu_brand = s.cpus().get(0).map(|cpu| cpu.brand()).unwrap_or("Unknown");

let cpu_usage = s.global_cpu_usage() as u32;
// let cpu_temperature = s.

match &self.api_client {
Some(client) => {
Expand All @@ -192,6 +246,7 @@ impl CpuMiner {
&& xmrig_status.hashrate.total[0].unwrap() > 0.0,
hash_rate,
cpu_usage: cpu_usage as u32,
cpu_temperatures: self.cpu_temperatures.clone(),
cpu_brand: cpu_brand.to_string(),
estimated_earnings: MicroMinotari(estimated_earnings).as_u64(),
connection: CpuMinerConnectionStatus {
Expand All @@ -207,6 +262,7 @@ impl CpuMiner {
None => Ok(CpuMinerStatus {
is_mining: false,
hash_rate: 0.0,
cpu_temperatures: self.cpu_temperatures.clone(),
cpu_usage: cpu_usage as u32,
cpu_brand: cpu_brand.to_string(),
estimated_earnings: 0,
Expand Down
81 changes: 81 additions & 0 deletions src-tauri/src/gpu_miner.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
use log::info;
use nvml_wrapper::{
enum_wrappers::device::{TemperatureSensor, TemperatureThreshold},
Nvml,
};

use crate::{GpuMinerHardwareStatus, GpuMinerStatus};

const LOG_TARGET: &str = "tari::universe::cpu_miner";

pub(crate) struct GpuMiner {
nvml: Nvml,
status: GpuMinerStatus,
}

impl GpuMiner {
pub fn new() -> Self {
Self {
nvml: Nvml::init().unwrap(),
status: GpuMinerStatus::from(GpuMinerStatus {
hardware_statuses: Vec::new(),
}),
}
}

pub fn start(&mut self) {
info!(target: LOG_TARGET, "Starting GPU miner");
// Start the GPU miner
}

pub fn stop(&mut self) {
info!(target: LOG_TARGET, "Stopping GPU miner");
// Stop the GPU miner
}

pub fn status(&mut self) -> GpuMinerStatus {
if self.status.hardware_statuses.is_empty() {
let devices_count = self.nvml.device_count().unwrap();

for i in 0..devices_count {
let device = self.nvml.device_by_index(i).unwrap();

let uuid = device.uuid().unwrap();
let name = device.name().unwrap();
let temperature = device.temperature(TemperatureSensor::Gpu).unwrap();
let load = device.utilization_rates().unwrap().gpu;

self.status.hardware_statuses.push(GpuMinerHardwareStatus {
uuid,
name,
temperature,
max_temperature: temperature,
load,
});
}
}

self.status.hardware_statuses = self
.status
.hardware_statuses
.iter()
.map(|status| {
let device = self.nvml.device_by_uuid(status.uuid.clone()).unwrap();

let temperature = device.temperature(TemperatureSensor::Gpu).unwrap();
let load = device.utilization_rates().unwrap().gpu;
let max_temperature = status.max_temperature.max(temperature);

GpuMinerHardwareStatus {
uuid: status.uuid.clone(),
name: status.name.clone(),
temperature,
max_temperature,
load,
}
})
.collect();

self.status.clone()
}
}
Loading

0 comments on commit eae24b3

Please sign in to comment.