Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add: GPU Insights #78

Merged
merged 8 commits into from
Mar 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"@radix-ui/react-slider": "^1.2.3",
"@radix-ui/react-slot": "^1.1.2",
"@radix-ui/react-switch": "^1.1.3",
"@radix-ui/react-tabs": "^1.1.3",
"@radix-ui/react-tooltip": "^1.1.8",
"@radix-ui/react-visually-hidden": "^1.1.2",
"@tailwindcss/vite": "^4.0.8",
Expand Down
43 changes: 42 additions & 1 deletion src-tauri/src/commands/hardware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ pub struct AppState {
pub gpu_history: Arc<Mutex<VecDeque<f32>>>,
pub process_cpu_histories: Arc<Mutex<HashMap<Pid, VecDeque<f32>>>>,
pub process_memory_histories: Arc<Mutex<HashMap<Pid, VecDeque<f32>>>>,
#[allow(dead_code)]
pub nv_gpu_usage_histories: Arc<Mutex<HashMap<String, VecDeque<f32>>>>,
#[allow(dead_code)]
pub nv_gpu_temperature_histories: Arc<Mutex<HashMap<String, VecDeque<i32>>>>,
}

///
Expand Down Expand Up @@ -367,6 +371,8 @@ pub fn initialize_system(
memory_history: Arc<Mutex<VecDeque<f32>>>,
process_cpu_histories: Arc<Mutex<HashMap<Pid, VecDeque<f32>>>>,
process_memory_histories: Arc<Mutex<HashMap<Pid, VecDeque<f32>>>>,
nv_gpu_usage_histories: Arc<Mutex<HashMap<String, VecDeque<f32>>>>,
nv_gpu_temperature_histories: Arc<Mutex<HashMap<String, VecDeque<i32>>>>,
) {
thread::spawn(move || loop {
{
Expand Down Expand Up @@ -430,8 +436,43 @@ pub fn initialize_system(
memory_history.push_back(memory_usage);
}
}
}

// GPU使用率の履歴を保存
{
let mut nv_gpu_usage_histories = nv_gpu_usage_histories.lock().unwrap();
let mut nv_gpu_temperature_histories =
nv_gpu_temperature_histories.lock().unwrap();

let gpus = match nvapi::PhysicalGpu::enumerate() {
Ok(gpus) => gpus,
Err(_) => continue,
};

for (name, gpu) in gpus
.iter()
.map(|gpu| (gpu.full_name().unwrap_or("Unknown".to_string()), gpu))
{
let usage_history = nv_gpu_usage_histories.entry(name.clone()).or_default();

if usage_history.len() >= HISTORY_CAPACITY {
usage_history.pop_front();
}
usage_history
.push_back(nvidia_gpu_service::get_gpu_usage_from_physical_gpu(gpu));

let temperature_history = nv_gpu_temperature_histories
.entry(name.clone())
.or_default();

if temperature_history.len() >= HISTORY_CAPACITY {
temperature_history.pop_front();
}
temperature_history.push_back(
nvidia_gpu_service::get_gpu_temperature_from_physical_gpu(gpu),
);
}
}
}
thread::sleep(Duration::from_secs(SYSTEM_INFO_INIT_INTERVAL));
});
}
34 changes: 34 additions & 0 deletions src-tauri/src/database/gpu_archive.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use crate::structs;
use crate::utils;
use sqlx::sqlite::SqlitePool;

pub async fn get_pool() -> Result<SqlitePool, sqlx::Error> {
let dir_path = utils::file::get_app_data_dir("hv-database.db");
let database_url = format!("sqlite:{}", dir_path.to_str().unwrap());

let pool = SqlitePool::connect(&database_url).await?;

Ok(pool)
}

pub async fn insert(data: structs::hardware_archive::GpuData) -> Result<(), sqlx::Error> {
let pool = get_pool().await?;

sqlx::query(
"INSERT INTO GPU_DATA_ARCHIVE (gpu_name, usage_avg, usage_max, usage_min, temperature_avg, temperature_max, temperature_min, timestamp)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)",
).bind(data.gpu_name).bind(data.usage_avg).bind(data.usage_max).bind(data.usage_min).bind(data.temperature_avg).bind(data.temperature_max).bind(data.temperature_min).bind(chrono::Utc::now()).execute(&pool).await?;

Ok(())
}

pub async fn delete_old_data(refresh_interval_days: u32) -> Result<(), sqlx::Error> {
let pool = get_pool().await?;

sqlx::query("DELETE FROM GPU_DATA_ARCHIVE WHERE timestamp < $1")
.bind(chrono::Utc::now() - chrono::Duration::days(refresh_interval_days as i64))
.execute(&pool)
.await?;

Ok(())
}
10 changes: 9 additions & 1 deletion src-tauri/src/database/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,13 @@ pub fn get_migrations() -> Vec<Migration> {
sql:
"CREATE TABLE DATA_ARCHIVE (id INTEGER PRIMARY KEY, cpu_avg INTEGER, cpu_max INTEGER, cpu_min INTEGER, ram_avg INTEGER, ram_max INTEGER, ram_min INTEGER, timestamp DATETIME);",
kind: MigrationKind::Up,
}]
},
Migration {
version: 2,
description: "create_gpu_tables",
sql:
"CREATE TABLE GPU_DATA_ARCHIVE (id INTEGER PRIMARY KEY, gpu_name TEXT, usage_avg INTEGER, usage_max INTEGER, usage_min INTEGER, temperature_avg INTEGER, temperature_max INTEGER, temperature_min INTEGER, timestamp DATETIME);",
kind: MigrationKind::Up,
}
]
}
1 change: 1 addition & 0 deletions src-tauri/src/database/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod gpu_archive;
pub mod hardware_archive;
pub mod migration;
8 changes: 8 additions & 0 deletions src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ pub fn run() {
let gpu_history = Arc::new(Mutex::new(VecDeque::with_capacity(60)));
let process_cpu_histories = Arc::new(Mutex::new(HashMap::new()));
let process_memory_histories = Arc::new(Mutex::new(HashMap::new()));
let nv_gpu_usage_histories = Arc::new(Mutex::new(HashMap::new()));
let nv_gpu_temperature_histories = Arc::new(Mutex::new(HashMap::new()));

let state = hardware::AppState {
system: Arc::clone(&system),
Expand All @@ -44,6 +46,8 @@ pub fn run() {
gpu_history: Arc::clone(&gpu_history),
process_cpu_histories: Arc::clone(&process_cpu_histories),
process_memory_histories: Arc::clone(&process_memory_histories),
nv_gpu_usage_histories: Arc::clone(&nv_gpu_usage_histories),
nv_gpu_temperature_histories: Arc::clone(&nv_gpu_temperature_histories),
};

let settings = app_state.settings.lock().unwrap().clone();
Expand All @@ -54,6 +58,8 @@ pub fn run() {
memory_history.clone(),
process_cpu_histories,
process_memory_histories,
nv_gpu_usage_histories.clone(),
nv_gpu_temperature_histories.clone(),
);

// ハードウェアアーカイブサービスの開始
Expand All @@ -62,6 +68,8 @@ pub fn run() {
services::hardware_archive_service::start_hardware_archive_service(
Arc::clone(&cpu_history),
Arc::clone(&memory_history),
Arc::clone(&nv_gpu_usage_histories),
Arc::clone(&nv_gpu_temperature_histories),
),
);
}
Expand Down
Loading
Loading