perf(linux): cut OpenGL RSS via isolated auth/Vulkan helpers, malloc_trim, 1024 atlas (v0.1.3)
This commit is contained in:
parent
19ceef9f74
commit
0b2561503f
9 changed files with 175 additions and 23 deletions
3
src/bin/yandex_music_auth.rs
Normal file
3
src/bin/yandex_music_auth.rs
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
fn main() {
|
||||
yandex_music_rs::native::run_auth_webview_subprocess();
|
||||
}
|
||||
|
|
@ -13,7 +13,7 @@ use crate::engine::types::{
|
|||
Color, DisplayList, Pos2, Rect, Vec2, GLYPH_ATLAS_TEXTURE_ID,
|
||||
};
|
||||
|
||||
const ATLAS_SIZE: u32 = 2048;
|
||||
const ATLAS_SIZE: u32 = 1024;
|
||||
|
||||
/// The 4 official Yandex Music 5.129.0 font roles loaded from `assets/fonts/`.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
|
|
|
|||
38
src/main.rs
38
src/main.rs
|
|
@ -169,6 +169,32 @@ fn init_opengl_backend(
|
|||
Ok((window, RenderBackend::OpenGl(gl)))
|
||||
}
|
||||
|
||||
/// Probe hardware Vulkan in an isolated short-lived subprocess on Linux so Mesa's software
|
||||
/// `libvulkan_lvp.so` + `libLLVM.so` (~80–100 MB) are NEVER mapped into the main OpenGL player
|
||||
/// process on legacy GPUs without hardware Vulkan (e.g. Intel HD Graphics 3000).
|
||||
fn has_hardware_vulkan_isolated() -> bool {
|
||||
#[cfg(target_os = "linux")]
|
||||
{
|
||||
use std::process::{Command, Stdio};
|
||||
if let Ok(exe) = std::env::current_exe() {
|
||||
if let Ok(status) = Command::new(exe)
|
||||
.arg("--probe-vulkan")
|
||||
.stdin(Stdio::null())
|
||||
.stdout(Stdio::null())
|
||||
.stderr(Stdio::null())
|
||||
.status()
|
||||
{
|
||||
return status.success();
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
#[cfg(not(target_os = "linux"))]
|
||||
{
|
||||
VulkanContext::has_hardware_vulkan()
|
||||
}
|
||||
}
|
||||
|
||||
struct AppRunner {
|
||||
runtime: tokio::runtime::Runtime,
|
||||
// Drop GPU backend BEFORE the native winit Window so Vulkan/GL surfaces destroy cleanly!
|
||||
|
|
@ -413,7 +439,12 @@ impl AppRunner {
|
|||
let is_max = window.is_maximized();
|
||||
let elapsed = shell.elapsed_secs();
|
||||
|
||||
let had_splash = shell.splash.is_some();
|
||||
let (frame, win_cmd) = shell.render_frame(logical, sf, elapsed, is_max);
|
||||
if had_splash && shell.splash.is_none() {
|
||||
telemetry::PerfTelemetry::trim_working_set();
|
||||
shell.app.perf.sample_memory();
|
||||
}
|
||||
|
||||
// Sync cursor icon
|
||||
if frame.cursor != self.last_cursor {
|
||||
|
|
@ -561,7 +592,7 @@ impl ApplicationHandler for AppRunner {
|
|||
RendererPreference::Vulkan => init_vulkan_backend(event_loop, init_w, init_h)
|
||||
.expect("Failed to initialize Vulkan renderer (--renderer=vulkan)"),
|
||||
RendererPreference::Auto => {
|
||||
if VulkanContext::has_hardware_vulkan() {
|
||||
if has_hardware_vulkan_isolated() {
|
||||
match init_vulkan_backend(event_loop, init_w, init_h) {
|
||||
Ok(pair) => pair,
|
||||
Err(vk_err) => {
|
||||
|
|
@ -1028,6 +1059,11 @@ fn acquire_single_instance_lock() -> bool {
|
|||
}
|
||||
|
||||
fn main() {
|
||||
// Isolated Vulkan HW probe subprocess (keeps libvulkan_lvp / libLLVM out of main Linux RSS)
|
||||
if std::env::args().any(|a| a == "--probe-vulkan") {
|
||||
std::process::exit(if VulkanContext::has_hardware_vulkan() { 0 } else { 1 });
|
||||
}
|
||||
|
||||
// Isolated zero-resident-RAM WebView2 OAuth subprocess (exits immediately after login!)
|
||||
if std::env::args().any(|a| a == "--auth-webview") {
|
||||
run_auth_webview_subprocess();
|
||||
|
|
|
|||
|
|
@ -451,13 +451,9 @@ mod imp {
|
|||
mod imp {
|
||||
use super::NativeEvent;
|
||||
use crate::api::models::Track;
|
||||
use crate::api::signing::extract_oauth_token;
|
||||
use crate::api::YANDEX_OAUTH_URL;
|
||||
use gtk::prelude::*;
|
||||
use std::io::{BufRead, BufReader, Write};
|
||||
use std::io::{BufRead, BufReader};
|
||||
use std::process::{Command, Stdio};
|
||||
use winit::window::Window;
|
||||
use wry::{WebViewBuilder, WebViewBuilderExtUnix};
|
||||
|
||||
const OAUTH_TOKEN_PREFIX: &str = "YM_OAUTH_TOKEN:";
|
||||
|
||||
|
|
@ -480,8 +476,17 @@ mod imp {
|
|||
winit::window::Icon::from_rgba(img.into_raw(), w, h).ok()
|
||||
}
|
||||
|
||||
/// Entry point for the short-lived `--auth-webview` subprocess on Linux (GTK3 + WebKitGTK).
|
||||
/// Entry point for the short-lived `yandex-music-auth` subprocess on Linux (GTK3 + WebKitGTK).
|
||||
/// Compiled ONLY when `--features linux-webview` is enabled so the main player binary has
|
||||
/// zero `libwebkit2gtk` / `libjavascriptcoregtk` / `libgtk-3` DT_NEEDED dependencies!
|
||||
#[cfg(feature = "linux-webview")]
|
||||
pub fn run_auth_webview_subprocess() {
|
||||
use crate::api::signing::extract_oauth_token;
|
||||
use crate::api::YANDEX_OAUTH_URL;
|
||||
use gtk::prelude::*;
|
||||
use std::io::Write;
|
||||
use wry::{WebViewBuilder, WebViewBuilderExtUnix};
|
||||
|
||||
// Avoid blank WebKitGTK window on NVIDIA/Wayland/VM drivers during OAuth login.
|
||||
if std::env::var_os("WEBKIT_DISABLE_DMABUF_RENDERER").is_none() {
|
||||
std::env::set_var("WEBKIT_DISABLE_DMABUF_RENDERER", "1");
|
||||
|
|
@ -554,14 +559,38 @@ mod imp {
|
|||
gtk::main();
|
||||
}
|
||||
|
||||
/// Spawn the isolated `--auth-webview` subprocess and return the captured OAuth token.
|
||||
#[cfg(not(feature = "linux-webview"))]
|
||||
pub fn run_auth_webview_subprocess() {
|
||||
eprintln!("yandex-music-auth helper required");
|
||||
std::process::exit(2);
|
||||
}
|
||||
|
||||
/// Spawn the isolated `yandex-music-auth` helper binary (or `--auth-webview` if built with
|
||||
/// `linux-webview`) and return the captured OAuth token.
|
||||
pub fn spawn_auth_webview_subprocess() -> anyhow::Result<String> {
|
||||
let exe = std::env::current_exe()?;
|
||||
let mut child = Command::new(exe)
|
||||
.arg("--auth-webview")
|
||||
let sibling_helper = exe.with_file_name("yandex-music-auth");
|
||||
|
||||
let mut child = if sibling_helper.is_file() {
|
||||
Command::new(&sibling_helper)
|
||||
.stdout(Stdio::piped())
|
||||
.stderr(Stdio::null())
|
||||
.spawn()?
|
||||
} else if let Ok(c) = Command::new("yandex-music-auth")
|
||||
.stdout(Stdio::piped())
|
||||
.stderr(Stdio::null())
|
||||
.spawn()?;
|
||||
.spawn()
|
||||
{
|
||||
c
|
||||
} else if cfg!(feature = "linux-webview") {
|
||||
Command::new(&exe)
|
||||
.arg("--auth-webview")
|
||||
.stdout(Stdio::piped())
|
||||
.stderr(Stdio::null())
|
||||
.spawn()?
|
||||
} else {
|
||||
anyhow::bail!("WebKitGTK auth helper (yandex-music-auth) unavailable");
|
||||
};
|
||||
|
||||
let stdout = child
|
||||
.stdout
|
||||
|
|
|
|||
|
|
@ -61,7 +61,17 @@ impl PerfTelemetry {
|
|||
}
|
||||
}
|
||||
|
||||
#[cfg(not(windows))]
|
||||
#[cfg(all(target_os = "linux", target_env = "gnu"))]
|
||||
pub fn trim_working_set() {
|
||||
extern "C" {
|
||||
fn malloc_trim(pad: usize) -> i32;
|
||||
}
|
||||
unsafe {
|
||||
let _ = malloc_trim(0);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(any(windows, all(target_os = "linux", target_env = "gnu"))))]
|
||||
pub fn trim_working_set() {}
|
||||
|
||||
#[cfg(windows)]
|
||||
|
|
@ -86,7 +96,62 @@ impl PerfTelemetry {
|
|||
}
|
||||
}
|
||||
|
||||
#[cfg(not(windows))]
|
||||
#[cfg(target_os = "linux")]
|
||||
pub fn sample_memory(&mut self) {
|
||||
fn parse_kb_line(line: &str, prefix: &str) -> Option<u64> {
|
||||
let rest = line.strip_prefix(prefix)?;
|
||||
rest.split_whitespace().next()?.parse::<u64>().ok()
|
||||
}
|
||||
|
||||
if let Ok(rollup) = std::fs::read_to_string("/proc/self/smaps_rollup") {
|
||||
let mut rss_kb: Option<u64> = None;
|
||||
let mut pss_kb: Option<u64> = None;
|
||||
let mut priv_clean_kb: u64 = 0;
|
||||
let mut priv_dirty_kb: u64 = 0;
|
||||
let mut has_priv = false;
|
||||
for line in rollup.lines() {
|
||||
if let Some(v) = parse_kb_line(line, "Rss:") {
|
||||
rss_kb = Some(v);
|
||||
} else if let Some(v) = parse_kb_line(line, "Pss:") {
|
||||
pss_kb = Some(v);
|
||||
} else if let Some(v) = parse_kb_line(line, "Private_Clean:") {
|
||||
priv_clean_kb = v;
|
||||
has_priv = true;
|
||||
} else if let Some(v) = parse_kb_line(line, "Private_Dirty:") {
|
||||
priv_dirty_kb = v;
|
||||
has_priv = true;
|
||||
}
|
||||
}
|
||||
if let Some(rss) = rss_kb {
|
||||
self.working_set_mb = rss as f32 / 1024.0;
|
||||
let priv_kb = if has_priv {
|
||||
priv_clean_kb.saturating_add(priv_dirty_kb)
|
||||
} else {
|
||||
pss_kb.unwrap_or(rss)
|
||||
};
|
||||
self.private_mb = priv_kb as f32 / 1024.0;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if let Ok(status) = std::fs::read_to_string("/proc/self/status") {
|
||||
let mut rss_kb: Option<u64> = None;
|
||||
let mut anon_kb: Option<u64> = None;
|
||||
for line in status.lines() {
|
||||
if let Some(v) = parse_kb_line(line, "VmRSS:") {
|
||||
rss_kb = Some(v);
|
||||
} else if let Some(v) = parse_kb_line(line, "RssAnon:") {
|
||||
anon_kb = Some(v);
|
||||
}
|
||||
}
|
||||
if let Some(rss) = rss_kb {
|
||||
self.working_set_mb = rss as f32 / 1024.0;
|
||||
self.private_mb = anon_kb.unwrap_or(rss) as f32 / 1024.0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(any(windows, target_os = "linux")))]
|
||||
pub fn sample_memory(&mut self) {
|
||||
self.working_set_mb = 28.0;
|
||||
self.private_mb = 22.0;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue