Everything in Rust on Servo 0.7 (trimmed: no WebXR, WebGPU, multiprocess): tabs and windows, address bar with suggestions, history, bookmarks and reading list, downloads with resume and external download managers, encrypted password vault with autofill, ad blocker (adblock-rust, 22 lists), VPN through Tor or a proxy and secure DNS via a local bridge proxy, picture in picture, Discord Rich Presence, media player and visualizer, spell check, site apps, user scripts and styles, import from other browsers, gamepads, Web Bluetooth, and every setting of the original Nacre in a plain egui interface. Not compiled yet; docs/HANDOFF.md has the first build checklist. Co-Authored-By: Claude Opus 5.5 <[email protected]>
137 lines
4.5 KiB
Rust
137 lines
4.5 KiB
Rust
//! Performance: memory use, low-memory mode, energy saver and battery state.
|
|
//!
|
|
//! Low-memory mode is on by itself on PCs with about 4 GB of RAM or two CPU threads (setting
|
|
//! "Auto"). It lowers Servo's thread and memory limits and makes background tabs rest sooner.
|
|
|
|
use crate::store::settings::{AutoOnOff, EnergySaver, Settings};
|
|
|
|
const GB: u64 = 1024 * 1024 * 1024;
|
|
|
|
/// (total, available) system memory in bytes.
|
|
pub fn system_memory() -> (u64, u64) {
|
|
#[cfg(target_os = "linux")]
|
|
{
|
|
let text = std::fs::read_to_string("/proc/meminfo").unwrap_or_default();
|
|
let field = |name: &str| -> u64 {
|
|
text.lines().find(|l| l.starts_with(name)).and_then(|l| l.split_whitespace().nth(1)).and_then(|v| v.parse::<u64>().ok()).map(|kb| kb * 1024).unwrap_or(0)
|
|
};
|
|
(field("MemTotal:"), field("MemAvailable:"))
|
|
}
|
|
#[cfg(not(target_os = "linux"))]
|
|
{
|
|
(0, 0)
|
|
}
|
|
}
|
|
|
|
/// Memory used by Nacre (one process; Servo runs in it): proportional set size, else resident.
|
|
pub fn own_memory() -> u64 {
|
|
#[cfg(target_os = "linux")]
|
|
{
|
|
if let Ok(text) = std::fs::read_to_string("/proc/self/smaps_rollup") {
|
|
let pss: u64 = text.lines().find(|l| l.starts_with("Pss:")).and_then(|l| l.split_whitespace().nth(1)).and_then(|v| v.parse().ok()).unwrap_or(0);
|
|
if pss > 0 {
|
|
return pss * 1024;
|
|
}
|
|
}
|
|
let statm = std::fs::read_to_string("/proc/self/statm").unwrap_or_default();
|
|
let pages: u64 = statm.split_whitespace().nth(1).and_then(|v| v.parse().ok()).unwrap_or(0);
|
|
pages * 4096
|
|
}
|
|
#[cfg(not(target_os = "linux"))]
|
|
{
|
|
0
|
|
}
|
|
}
|
|
|
|
/// Number of threads Nacre runs (shown on the memory row).
|
|
pub fn own_threads() -> usize {
|
|
#[cfg(target_os = "linux")]
|
|
{
|
|
std::fs::read_dir("/proc/self/task").map(|d| d.count()).unwrap_or(0)
|
|
}
|
|
#[cfg(not(target_os = "linux"))]
|
|
{
|
|
0
|
|
}
|
|
}
|
|
|
|
pub fn cpu_threads() -> usize {
|
|
std::thread::available_parallelism().map(|n| n.get()).unwrap_or(2)
|
|
}
|
|
|
|
pub fn low_end() -> bool {
|
|
let (total, _) = system_memory();
|
|
(total > 0 && total <= 5 * GB) || cpu_threads() <= 2
|
|
}
|
|
|
|
pub fn lite(settings: &Settings) -> bool {
|
|
match settings.perf.lite {
|
|
AutoOnOff::On => true,
|
|
AutoOnOff::Off => false,
|
|
AutoOnOff::Auto => low_end(),
|
|
}
|
|
}
|
|
|
|
/// The system is short of memory: free some right away.
|
|
pub fn under_pressure() -> bool {
|
|
let (total, available) = system_memory();
|
|
total > 0 && available < total / 10
|
|
}
|
|
|
|
/// Running on battery power (Linux: /sys/class/power_supply).
|
|
pub fn on_battery() -> bool {
|
|
#[cfg(target_os = "linux")]
|
|
{
|
|
let Ok(dir) = std::fs::read_dir("/sys/class/power_supply") else { return false };
|
|
let mut has_battery = false;
|
|
for entry in dir.flatten() {
|
|
let path = entry.path();
|
|
let kind = std::fs::read_to_string(path.join("type")).unwrap_or_default();
|
|
match kind.trim() {
|
|
"Mains" | "USB" => {
|
|
if std::fs::read_to_string(path.join("online")).is_ok_and(|v| v.trim() == "1") {
|
|
return false;
|
|
}
|
|
},
|
|
"Battery" => {
|
|
has_battery = true;
|
|
if std::fs::read_to_string(path.join("status")).is_ok_and(|s| s.trim() == "Charging" || s.trim() == "Full") {
|
|
return false;
|
|
}
|
|
},
|
|
_ => {},
|
|
}
|
|
}
|
|
has_battery
|
|
}
|
|
#[cfg(not(target_os = "linux"))]
|
|
{
|
|
false
|
|
}
|
|
}
|
|
|
|
pub fn energy_saver(settings: &Settings) -> bool {
|
|
match settings.perf.energy {
|
|
EnergySaver::On => true,
|
|
EnergySaver::Off => false,
|
|
EnergySaver::Battery => on_battery(),
|
|
}
|
|
}
|
|
|
|
/// Minutes after which hidden tabs freeze and unload, taking low-memory mode and energy saver into account.
|
|
pub fn tab_rest_minutes(settings: &Settings) -> (u32, u32) {
|
|
let (mut freeze, mut discard) = (settings.perf.freeze_min, settings.perf.discard_min);
|
|
if lite(settings) || energy_saver(settings) {
|
|
freeze = if freeze == 0 { 0 } else { freeze.min(1) };
|
|
discard = if discard == 0 { 0 } else { discard.min(15) };
|
|
}
|
|
if under_pressure() && discard > 0 {
|
|
discard = discard.min(5);
|
|
}
|
|
(freeze, discard)
|
|
}
|
|
|
|
/// Whether animations (new tab background, ad removal, artwork spin) should stand still.
|
|
pub fn still(settings: &Settings) -> bool {
|
|
settings.perf.still_ntp || lite(settings) || energy_saver(settings)
|
|
}
|