Skip to content

Commit

Permalink
Merge branch 'main' into handle_firewall_rules_on_windows
Browse files Browse the repository at this point in the history
  • Loading branch information
brianp authored Nov 21, 2024
2 parents 431f4b3 + 3a71981 commit 677a23c
Show file tree
Hide file tree
Showing 13 changed files with 360 additions and 162 deletions.
1 change: 1 addition & 0 deletions public/locales/en/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"cpu-warning": "Sustained high CPU usage may cause overheating, and hardware failure.",
"gpu-power-level": "GPU Power",
"gpu-warning": "Sustained high GPU usage may cause overheating, and hardware failure.",
"save-changes": "Save Changes",
"saved": "Applying Changes",
"title": "Custom Mode",
"warning": "Warning"
Expand Down
38 changes: 21 additions & 17 deletions src-tauri/src/app_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ pub struct AppConfigFromFile {
#[serde(default = "default_custom_max_cpu_usage")]
custom_max_cpu_usage: Option<u32>,
#[serde(default = "default_custom_max_gpu_usage")]
custom_max_gpu_usage: Option<u32>,
custom_max_gpu_usage: Vec<GpuThreads>,
#[serde(default = "default_true")]
auto_update: bool,
#[serde(default = "default_true")]
Expand Down Expand Up @@ -92,7 +92,7 @@ impl Default for AppConfigFromFile {
should_auto_launch: false,
application_language: default_application_language(),
custom_max_cpu_usage: None,
custom_max_gpu_usage: None,
custom_max_gpu_usage: vec![],
paper_wallet_enabled: true,
use_tor: true,
eco_mode_cpu_options: Vec::new(),
Expand Down Expand Up @@ -163,6 +163,12 @@ impl MiningMode {
}
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct GpuThreads {
pub gpu_name: String,
pub max_gpu_threads: u32,
}

#[allow(clippy::struct_excessive_bools)]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub(crate) struct AppConfig {
Expand Down Expand Up @@ -194,7 +200,7 @@ pub(crate) struct AppConfig {
mmproxy_use_monero_fail: bool,
mmproxy_monero_nodes: Vec<String>,
custom_max_cpu_usage: Option<u32>,
custom_max_gpu_usage: Option<u32>,
custom_max_gpu_usage: Vec<GpuThreads>,
auto_update: bool,
custom_power_levels_enabled: bool,
sharing_enabled: bool,
Expand Down Expand Up @@ -223,7 +229,7 @@ impl AppConfig {
application_language: default_application_language(),
use_tor: true,
custom_max_cpu_usage: None,
custom_max_gpu_usage: None,
custom_max_gpu_usage: vec![],
paper_wallet_enabled: true,
reset_earnings: false,
eco_mode_cpu_options: Vec::new(),
Expand Down Expand Up @@ -289,7 +295,7 @@ impl AppConfig {
self.mmproxy_monero_nodes = config.mmproxy_monero_nodes;
self.mmproxy_use_monero_fail = config.mmproxy_use_monero_fail;
self.custom_max_cpu_usage = config.custom_max_cpu_usage;
self.custom_max_gpu_usage = config.custom_max_gpu_usage;
self.custom_max_gpu_usage = config.custom_max_gpu_usage.clone();
self.auto_update = config.auto_update;
self.reset_earnings = config.reset_earnings;
self.custom_power_levels_enabled = config.custom_power_levels_enabled;
Expand Down Expand Up @@ -378,7 +384,7 @@ impl AppConfig {
&mut self,
mode: String,
custom_max_cpu_usage: Option<u32>,
custom_max_gpu_usage: Option<u32>,
custom_max_gpu_usage: Vec<GpuThreads>,
) -> Result<(), anyhow::Error> {
let new_mode = match mode.as_str() {
"Eco" => MiningMode::Eco,
Expand All @@ -390,10 +396,8 @@ impl AppConfig {
self.update_config_file().await?;
if let Some(custom_max_cpu_usage) = custom_max_cpu_usage {
self.set_max_cpu_usage(custom_max_cpu_usage).await?;
}
if let Some(custom_max_gpu_usage) = custom_max_gpu_usage {
self.set_max_gpu_usage(custom_max_gpu_usage).await?;
}
};
self.set_max_gpu_usage(custom_max_gpu_usage).await?;
Ok(())
}
pub async fn set_display_mode(&mut self, display_mode: String) -> Result<(), anyhow::Error> {
Expand All @@ -412,15 +416,15 @@ impl AppConfig {
self.mode
}

pub fn custom_gpu_usage(&self) -> Option<u32> {
self.custom_max_gpu_usage
pub fn custom_gpu_usage(&self) -> Vec<GpuThreads> {
self.custom_max_gpu_usage.clone()
}

pub async fn set_max_gpu_usage(
&mut self,
custom_max_gpu_usage: u32,
custom_max_gpu_usage: Vec<GpuThreads>,
) -> Result<(), anyhow::Error> {
self.custom_max_gpu_usage = Some(custom_max_gpu_usage);
self.custom_max_gpu_usage = custom_max_gpu_usage.clone();
self.update_config_file().await?;
Ok(())
}
Expand Down Expand Up @@ -624,7 +628,7 @@ impl AppConfig {
application_language: self.application_language.clone(),
paper_wallet_enabled: self.paper_wallet_enabled,
custom_max_cpu_usage: self.custom_max_cpu_usage,
custom_max_gpu_usage: self.custom_max_gpu_usage,
custom_max_gpu_usage: self.custom_max_gpu_usage.clone(),
use_tor: self.use_tor,
reset_earnings: self.reset_earnings,
eco_mode_cpu_options: self.eco_mode_cpu_options.clone(),
Expand Down Expand Up @@ -655,8 +659,8 @@ fn default_custom_max_cpu_usage() -> Option<u32> {
None
}

fn default_custom_max_gpu_usage() -> Option<u32> {
None
fn default_custom_max_gpu_usage() -> Vec<GpuThreads> {
vec![]
}

fn default_mode() -> String {
Expand Down
49 changes: 36 additions & 13 deletions src-tauri/src/gpu_miner.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use std::{path::PathBuf, sync::Arc};

use log::info;
use serde::Deserialize;
use tari_common_types::tari_address::TariAddress;
use tari_core::transactions::tari_amount::MicroMinotari;
use tari_shutdown::ShutdownSignal;
use tokio::sync::RwLock;

use crate::app_config::GpuThreads;
use crate::binaries::{Binaries, BinaryResolver};
use crate::gpu_miner_adapter::GpuNodeSource;
use crate::process_utils;
Expand All @@ -18,19 +20,37 @@ use crate::{
const SHA_BLOCKS_PER_DAY: u64 = 360;
const LOG_TARGET: &str = "tari::universe::gpu_miner";

#[derive(Debug, Deserialize)]
pub struct GpuStatusJson {
pub gpu_devices: Vec<GpuConfig>,
}

#[derive(Debug, Deserialize, Clone)]
#[allow(dead_code)]
pub(crate) struct GpuConfig {
pub device_index: u32,
pub device_name: String,
pub is_available: bool,
pub grid_size: u32,
pub max_grid_size: u32,
pub block_size: u32,
}

pub(crate) struct GpuMiner {
watcher: Arc<RwLock<ProcessWatcher<GpuMinerAdapter>>>,
is_available: bool,
gpu_devices: Vec<GpuConfig>,
excluded_gpu_devices: Vec<u8>,
}

impl GpuMiner {
pub fn new() -> Self {
let adapter = GpuMinerAdapter::new();
let adapter = GpuMinerAdapter::new(vec![]);
let process_watcher = ProcessWatcher::new(adapter);
Self {
watcher: Arc::new(RwLock::new(process_watcher)),
is_available: false,
gpu_devices: vec![],
excluded_gpu_devices: vec![],
}
}
Expand All @@ -46,10 +66,11 @@ impl GpuMiner {
log_path: PathBuf,
mining_mode: MiningMode,
coinbase_extra: String,
custom_gpu_grid_size: Option<u16>,
custom_gpu_grid_size: Vec<GpuThreads>,
) -> Result<(), anyhow::Error> {
let mut process_watcher = self.watcher.write().await;
process_watcher.adapter.tari_address = tari_address;
process_watcher.adapter.gpu_devices = self.gpu_devices.clone();
process_watcher
.adapter
.set_mode(mining_mode, custom_gpu_grid_size);
Expand Down Expand Up @@ -132,21 +153,16 @@ impl GpuMiner {
pub async fn detect(&mut self, config_dir: PathBuf) -> Result<(), anyhow::Error> {
info!(target: LOG_TARGET, "Verify if gpu miner can work on the system");

let output_file = config_dir
.join("gpuminer")
.join("gpu_status.json")
.to_string_lossy()
.to_string();
let args: Vec<String> = vec![
"--detect".to_string(),
"true".to_string(),
"--config".to_string(),
config_dir
.join("gpuminer")
.join("config.json")
.to_string_lossy()
.to_string(),
"--gpu-status-file".to_string(),
config_dir
.join("gpuminer")
.join("gpu_status.json")
.to_string_lossy()
.to_string(),
output_file.clone(),
];
let gpuminer_bin = BinaryResolver::current()
.read()
Expand All @@ -158,6 +174,9 @@ impl GpuMiner {
let child = process_utils::launch_child_process(&gpuminer_bin, &config_dir, None, &args)?;
let output = child.wait_with_output().await?;
info!(target: LOG_TARGET, "Gpu detect exit code: {:?}", output.status.code().unwrap_or_default());
let gpu_settings = std::fs::read_to_string(output_file)?;
let gpu_settings: GpuStatusJson = serde_json::from_str(&gpu_settings)?;
self.gpu_devices = gpu_settings.gpu_devices;
match output.status.code() {
Some(0) => {
self.is_available = true;
Expand All @@ -184,4 +203,8 @@ impl GpuMiner {
self.excluded_gpu_devices = excluded_gpu_devices;
Ok(())
}

pub async fn get_gpu_devices(&self) -> Result<Vec<GpuConfig>, anyhow::Error> {
Ok(self.gpu_devices.clone())
}
}
58 changes: 47 additions & 11 deletions src-tauri/src/gpu_miner_adapter.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use crate::app_config::GpuThreads;
use crate::gpu_miner::GpuConfig;
use crate::port_allocator::PortAllocator;
use crate::process_adapter::HealthStatus;
use crate::process_adapter::ProcessStartupSpec;
Expand All @@ -7,6 +9,8 @@ use async_trait::async_trait;
use log::{info, warn};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::ops::Div;
use std::ops::Mul;
use std::path::PathBuf;
use std::time::Instant;
use tari_common::configuration::Network;
Expand All @@ -23,9 +27,6 @@ use crate::{

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

pub const ECO_MODE_GPU_GRID_SIZE: u16 = 2;
pub const LUDICROUS_MODE_GPU_GRID_SIZE: u16 = 900; // TODO: In future will allow user to configure this, but for now let's not burn the gpu too much

pub enum GpuNodeSource {
BaseNode { port: u16 },
P2Pool { port: u16 },
Expand All @@ -34,29 +35,56 @@ pub enum GpuNodeSource {
pub(crate) struct GpuMinerAdapter {
pub(crate) tari_address: TariAddress,
// Value ranges 1 - 1000
pub(crate) gpu_grid_size: u16,
pub(crate) gpu_grid_size: Vec<GpuThreads>,
pub(crate) node_source: Option<GpuNodeSource>,
pub(crate) coinbase_extra: String,
pub(crate) excluded_gpu_devices: Vec<u8>,
pub(crate) gpu_devices: Vec<GpuConfig>,
}

impl GpuMinerAdapter {
pub fn new() -> Self {
pub fn new(gpu_devices: Vec<GpuConfig>) -> Self {
Self {
tari_address: TariAddress::default(),
gpu_grid_size: ECO_MODE_GPU_GRID_SIZE,
gpu_grid_size: gpu_devices
.iter()
.map(|x| GpuThreads {
gpu_name: x.device_name.clone(),
max_gpu_threads: x.max_grid_size,
})
.collect(),
node_source: None,
coinbase_extra: "tari-universe".to_string(),
excluded_gpu_devices: vec![],
gpu_devices,
}
}

pub fn set_mode(&mut self, mode: MiningMode, custom_max_gpu_grid_size: Option<u16>) {
pub fn set_mode(&mut self, mode: MiningMode, custom_max_gpus_grid_size: Vec<GpuThreads>) {
match mode {
MiningMode::Eco => self.gpu_grid_size = ECO_MODE_GPU_GRID_SIZE,
MiningMode::Ludicrous => self.gpu_grid_size = LUDICROUS_MODE_GPU_GRID_SIZE,
MiningMode::Eco => {
self.gpu_grid_size = self
.gpu_devices
.iter()
.map(|device| GpuThreads {
gpu_name: device.device_name.clone(),
max_gpu_threads: device.max_grid_size.mul(100).div(333),
})
.collect()
}
MiningMode::Ludicrous => {
self.gpu_grid_size = self
.gpu_devices
.iter()
.map(|device| GpuThreads {
gpu_name: device.device_name.clone(),
// get 90% of max grid size
max_gpu_threads: device.max_grid_size.mul(100).div(111),
})
.collect()
}
MiningMode::Custom => {
self.gpu_grid_size = custom_max_gpu_grid_size.unwrap_or(ECO_MODE_GPU_GRID_SIZE)
self.gpu_grid_size = custom_max_gpus_grid_size;
}
}
}
Expand Down Expand Up @@ -97,6 +125,13 @@ impl ProcessAdapter for GpuMinerAdapter {
}
};

let grid_size = self
.gpu_grid_size
.iter()
.map(|x| x.max_gpu_threads.clone().to_string())
.collect::<Vec<_>>()
.join(",");

let mut args: Vec<String> = vec![
"--tari-address".to_string(),
self.tari_address.to_string(),
Expand All @@ -111,7 +146,7 @@ impl ProcessAdapter for GpuMinerAdapter {
"--http-server-port".to_string(),
http_api_port.to_string(),
"--grid-size".to_string(),
self.gpu_grid_size.to_string(),
grid_size.clone(),
"--log-config-file".to_string(),
config_dir
.join("gpuminer")
Expand Down Expand Up @@ -254,6 +289,7 @@ impl GpuMinerStatusMonitor {
});
}
};

Ok(GpuMinerStatus {
is_mining: true,
estimated_earnings: 0,
Expand Down
Loading

0 comments on commit 677a23c

Please sign in to comment.