Skip to content

Commit

Permalink
feat: add gpu detect & log config (#14)
Browse files Browse the repository at this point in the history
Within this pr:
1. add log config log4rs & logging bug fixes
2. add `detect` feature to check if gpu mining is available
  • Loading branch information
karczuRF authored Sep 11, 2024
1 parent 72dbe4d commit 1c2e40f
Show file tree
Hide file tree
Showing 8 changed files with 160 additions and 45 deletions.
19 changes: 19 additions & 0 deletions 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ opencl-sys = "*"
axum = "0.7.5"
thiserror = "1.0.63"
log = "0.4.22"
log4rs = "1.3.0"

[features]
default = []
Expand Down
48 changes: 48 additions & 0 deletions log4rs_sample.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# A sample log configuration file for running in release mode.
#
# See https://docs.rs/log4rs/0.8.3/log4rs/encode/pattern/index.html for deciphering the log pattern. The log format
# used in this sample configuration prints messages as:
# timestamp [target] LEVEL message
refresh_rate: 30 seconds
appenders:
# An appender named "stdout" that writes to stdout
stdout:
kind: console
encoder:
pattern: "{d(%H:%M)} {h({l}):5} {m}{n}"
filters:
- kind: threshold
level: info

# An appender named "base_layer" that writes to a file with a custom pattern encoder
xtrgpuminer:
kind: rolling_file
path: "{{log_dir}}/log/xtrgpuminer.log"
policy:
kind: compound
trigger:
kind: size
limit: 10mb
roller:
kind: fixed_window
base: 1
count: 5
pattern: "{{log_dir}}/log/xtrgpuminer.{}.log"
encoder:
pattern: "{d(%Y-%m-%d %H:%M:%S.%f)} {l:5} {m} // {f}:{L}{n}"

# Set the default logging level to "info"
root:
level: info
appenders:
- stdout
- xtrgpuminer

loggers:
# Route log events common to every application to all appenders
tari::gpuminer:
level: info
appenders:
- stdout
- default
additive: false
5 changes: 5 additions & 0 deletions src/cuda_engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ use cust::{
prelude::{Module, *},
};

use log::{error, info, warn};
use std::time::Instant;
const LOG_TARGET: &str = "tari::gpuminer::cuda";
#[derive(Clone)]
pub struct CudaEngine {}

Expand All @@ -25,6 +27,7 @@ impl EngineImpl for CudaEngine {
type Function = CudaFunction;

fn init(&mut self) -> Result<(), anyhow::Error> {
info!(target: LOG_TARGET, "Init CUDA Engine");
cust::init(CudaFlags::empty())?;
Ok(())
}
Expand All @@ -42,6 +45,7 @@ impl EngineImpl for CudaEngine {
}

fn create_main_function(&self, context: &Self::Context) -> Result<Self::Function, anyhow::Error> {
info!(target: LOG_TARGET, "Create CUDA main function");
let module = Module::from_ptx(
include_str!("../cuda/keccak.ptx"),
&[ModuleJitOption::GenerateLineInfo(true)],
Expand All @@ -61,6 +65,7 @@ impl EngineImpl for CudaEngine {
block_size: u32,
grid_size: u32,
) -> Result<(Option<u64>, u32, u64), Error> {
info!(target: LOG_TARGET, "CUDA: start mining");
let output = vec![0u64; 5];
let mut output_buf = output.as_slice().as_dbuf()?;

Expand Down
49 changes: 45 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::str::FromStr;
use std::{cmp, fs};
use std::{convert::TryInto, env::current_dir, path::PathBuf, sync::Arc, thread, time::Instant};

use anyhow::{anyhow, Context as AnyContext, Error};
use anyhow::{anyhow, Context as AnyContext};
use clap::Parser;
#[cfg(feature = "nvidia")]
use cust::{
Expand All @@ -15,6 +15,7 @@ use minotari_app_grpc::tari_rpc::{
use num_format::{Locale, ToFormattedString};
use sha3::Digest;
use tari_common::configuration::Network;
use tari_common::initialize_logging;
use tari_common_types::{tari_address::TariAddress, types::FixedHash};
use tari_core::{
blocks::BlockHeader,
Expand Down Expand Up @@ -55,13 +56,14 @@ mod p2pool_client;
mod stats_store;
mod tari_coinbase;

const LOG_TARGET: &str = "tari::universe::gpu_miner"; //TODO set log target
const LOG_TARGET: &str = "tari::gpuminer";

#[tokio::main]
async fn main() {
match main_inner().await {
Ok(()) => {
info!(target: LOG_TARGET, "Starting gpu_miner");
info!(target: LOG_TARGET, "Starting gpu_miner successfully");
std::process::exit(0);
},
Err(err) => {
error!(target: LOG_TARGET, "Gpu_miner error: {}", err);
Expand Down Expand Up @@ -109,13 +111,35 @@ struct Cli {
/// Coinbase extra data
#[arg(long)]
coinbase_extra: Option<String>,

/// (Optional) log config file
#[arg(long, alias = "log-config-file", value_name = "log-config-file")]
log_config_file: Option<PathBuf>,

/// (Optional) log dir
#[arg(long, alias = "log-dir", value_name = "log-dir")]
log_dir: Option<PathBuf>,

/// (Optional) log dir
#[arg(short = 'd', long, alias = "detect")]
detect: Option<bool>,
}

async fn main_inner() -> Result<(), anyhow::Error> {
let cli = Cli::parse();

let benchmark = cli.benchmark;
if cli.log_config_file.is_some() && cli.log_dir.is_some() {
if let Err(e) = initialize_logging(
&cli.log_config_file.as_ref().cloned().unwrap_or_default(),
&cli.log_dir.as_ref().cloned().unwrap_or_default(),
include_str!("../log4rs_sample.yml"),
) {
eprintln!("Gpu main_inner error: {}", e);
return Err(e.into());
}
}

let benchmark = cli.benchmark;
let mut config = match ConfigFile::load(&cli.config.as_ref().cloned().unwrap_or_else(|| {
let mut path = current_dir().expect("no current directory");
path.push("config.json");
Expand Down Expand Up @@ -179,6 +203,7 @@ async fn main_inner() -> Result<(), anyhow::Error> {
let http_server_config = Config::new(config.http_server_port);
info!(target: LOG_TARGET, "HTTP server runs on port: {}", &http_server_config.port);
let http_server = HttpServer::new(shutdown.to_signal(), http_server_config, stats_store.clone());
info!(target: LOG_TARGET, "HTTP server enabled");
tokio::spawn(async move {
if let Err(error) = http_server.start().await {
println!("Failed to start HTTP server: {error:?}");
Expand All @@ -188,6 +213,22 @@ async fn main_inner() -> Result<(), anyhow::Error> {
}

let num_devices = gpu_engine.num_devices()?;

// just create the context to test if it can run
if let Some(detect) = cli.detect {
let gpu = gpu_engine.clone();

if num_devices > 0 {
if let Err(e) = gpu.create_context(0) {
return Err(e.into());
}
} else {
warn!(target: LOG_TARGET, "No gpu device detected");
return Err(anyhow::anyhow!("No gpu device detected"));
}
return Ok(());
}

let mut threads = vec![];
for i in 0..num_devices {
let c = config.clone();
Expand Down
28 changes: 14 additions & 14 deletions src/node_client.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::p2pool_client::P2poolClientWrapper;
use anyhow::anyhow;
use log::{error, info, warn};
use minotari_app_grpc::tari_rpc::sha_p2_pool_client::ShaP2PoolClient;
use minotari_app_grpc::tari_rpc::{
base_node_client::BaseNodeClient, pow_algo::PowAlgos, Block, Empty, GetNewBlockResult, NewBlockTemplate,
Expand All @@ -9,9 +10,8 @@ use std::time::Duration;
use tari_common_types::tari_address::TariAddress;
use tonic::async_trait;
use tonic::transport::Channel;
use log::{error, info, warn};

const LOG_TARGET: &str = "tari::universe::gpu_miner";//TODO set log target
const LOG_TARGET: &str = "tari::gpuminer::node-client";

pub(crate) struct BaseNodeClientWrapper {
client: BaseNodeClient<tonic::transport::Channel>,
Expand All @@ -27,7 +27,7 @@ impl BaseNodeClientWrapper {
Ok(res_client) => {
info!(target: LOG_TARGET, "Connected successfully");
client = Some(res_client)
}
},
Err(error) => {
error!(target: LOG_TARGET,"Failed to connect to base node: {:?}", error);
println!("Failed to connect to base node: {error:?}");
Expand All @@ -50,14 +50,14 @@ impl NodeClient for BaseNodeClientWrapper {
// dbg!(res);
Ok(0)
}

async fn get_block_template(&mut self) -> Result<NewBlockTemplateResponse, anyhow::Error> {
info!(target: LOG_TARGET, "Getting node block template");
let res = self
.client
.get_new_block_template(tonic::Request::new({
NewBlockTemplateRequest {
max_weight: 0,
.client
.get_new_block_template(tonic::Request::new({
NewBlockTemplateRequest {
max_weight: 0,
algo: Some(PowAlgo {
pow_algo: PowAlgos::Sha3x.into(),
}),
Expand All @@ -67,14 +67,14 @@ impl NodeClient for BaseNodeClientWrapper {
info!(target: LOG_TARGET, "Done getting node block template");
Ok(res.into_inner())
}

async fn get_new_block(&mut self, template: NewBlockTemplate) -> Result<NewBlockResult, anyhow::Error> {
info!(target: LOG_TARGET, "Getting new block template");
let res = self.client.get_new_block(tonic::Request::new(template)).await?;
info!(target: LOG_TARGET, "Done getting new block template");
Ok(NewBlockResult::try_from(res.into_inner())?)
}

async fn submit_block(&mut self, block: Block) -> Result<(), anyhow::Error> {
info!(target: LOG_TARGET, "Submitting block");
// dbg!(&block);
Expand All @@ -88,11 +88,11 @@ impl NodeClient for BaseNodeClientWrapper {
#[async_trait]
pub trait NodeClient {
async fn get_version(&mut self) -> Result<u64, anyhow::Error>;

async fn get_block_template(&mut self) -> Result<NewBlockTemplateResponse, anyhow::Error>;

async fn get_new_block(&mut self, template: NewBlockTemplate) -> Result<NewBlockResult, anyhow::Error>;

async fn submit_block(&mut self, block: Block) -> Result<(), anyhow::Error>;
}

Expand Down Expand Up @@ -148,7 +148,7 @@ impl Client {
Client::P2Pool(client) => client.get_version().await,
}
}

pub async fn get_block_template(&mut self) -> Result<NewBlockTemplateResponse, anyhow::Error> {
match self {
Client::BaseNode(client) => client.get_block_template().await,
Expand Down
Loading

0 comments on commit 1c2e40f

Please sign in to comment.