From e1b2ad13b0a892f0990ae90cd30ad02f63a37006 Mon Sep 17 00:00:00 2001 From: VHSgunzo Date: Mon, 23 Dec 2024 17:39:13 +0300 Subject: [PATCH] Add cache management options for DwarFS Add cache management options for DwarFS (to reduce RAM usage) Add DWARFS_CACHESIZE env var for set DwarFS cachesize Fix squashfuse speed degradation --- Cargo.toml | 2 +- README.md | 2 ++ build.rs | 2 +- src/main.rs | 18 ++++++++++++++++++ 4 files changed, 22 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 680dad0..4dad2ee 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uruntime" -version = "0.0.9" +version = "0.1.1" readme = "README.md" license = "MIT" repository = "https://github.com/VHSgunzo/uruntime" diff --git a/README.md b/README.md index e6eac1f..7213b92 100644 --- a/README.md +++ b/README.md @@ -100,6 +100,7 @@ See [Build step in ci.yml](https://github.com/VHSgunzo/uruntime/blob/main/.githu using extract and run option for reuse extracted data TMPDIR=/path Specifies a custom path for mounting or extracting the image FUSERMOUNT_PROG=/path Specifies a custom path for fusermount + DWARFS_CACHESIZE=128M Size of the block cache, in bytes for DwarFS (suffixes K, M, G) ``` * **AppImage runtime usage** @@ -156,4 +157,5 @@ See [Build step in ci.yml](https://github.com/VHSgunzo/uruntime/blob/main/.githu TMPDIR=/path Specifies a custom path for mounting or extracting the image FUSERMOUNT_PROG=/path Specifies a custom path for fusermount TARGET_APPIMAGE=/path Operate on a target AppImage rather than this file itself + DWARFS_CACHESIZE=128M Size of the block cache, in bytes for DwarFS (suffixes K, M, G) ``` diff --git a/build.rs b/build.rs index ad5a8fb..7f21023 100644 --- a/build.rs +++ b/build.rs @@ -20,7 +20,7 @@ fn main() { let assets = IndexMap::from([ #[cfg(feature = "squashfs")] - ("squashfuse-upx", format!("https://github.com/VHSgunzo/squashfuse-static/releases/download/v0.5.2.r6.g4289904/squashfuse-{arch}-upx")), + ("squashfuse-upx", format!("https://github.com/VHSgunzo/squashfuse-static/releases/download/v0.5.2.r6.g4289904/squashfuse-glibc-{arch}-upx")), #[cfg(feature = "squashfs")] ("unsquashfs-upx", format!("https://github.com/VHSgunzo/squashfs-tools-static/releases/download/v4.6.1/unsquashfs-{arch}-upx")), #[cfg(feature = "mksquashfs")] diff --git a/src/main.rs b/src/main.rs index b478c04..0da5afe 100644 --- a/src/main.rs +++ b/src/main.rs @@ -20,6 +20,8 @@ use signal_hook::{consts::{SIGINT, SIGTERM, SIGQUIT, SIGHUP}, iterator::Signals} const URUNTIME_VERSION: &str = env!("CARGO_PKG_VERSION"); +#[cfg(feature = "dwarfs")] +const DWARFS_CACHESIZE: &str = "128M"; #[derive(Debug)] @@ -327,6 +329,17 @@ fn create_tmp_dirs(dirs: Vec<&PathBuf>) -> Result<()> { Err(Error::last_os_error()) } +#[cfg(feature = "dwarfs")] +fn get_dwfs_cachesize() -> String { + let cachesize_env = get_env_var("DWARFS_CACHESIZE"); + if cachesize_env.is_empty() { + DWARFS_CACHESIZE.into() + } else { + let opts: Vec<&str> = cachesize_env.split(',').collect(); + opts.first().unwrap_or(&DWARFS_CACHESIZE).to_string() + } +} + fn mount_image(embed: &Embed, image: &Image, mount_dir: PathBuf) { check_fuse(); @@ -340,6 +353,8 @@ fn mount_image(embed: &Embed, image: &Image, mount_dir: PathBuf) { let num_threads = num_cpus::get(); embed.dwarfs(vec!["-f".into(), "-o".into(), "ro,nodev,noatime,clone_fd".into(), + "-o".into(), "cache_files,no_cache_image".into(), + "-o".into(), format!("cachesize={}", get_dwfs_cachesize()), "-o".into(), format!("uid={uid},gid={gid}"), "-o".into(), format!("offset={}", image.offset), "-o".into(), format!("workers={num_threads}"), @@ -404,6 +419,7 @@ fn extract_image(embed: &Embed, image: &Image, mut extract_dir: PathBuf, is_extr let mut exec_args = vec![ "--input".into(), image_path, "--log-level=error".into(), + format!("--cache-size={}", get_dwfs_cachesize()), format!("--image-offset={}", image.offset), format!("--num-workers={}", num_cpus::get()), "--output".into(), extract_dir, @@ -559,6 +575,8 @@ fn print_usage(portable_home: &PathBuf, portable_config: &PathBuf) { FUSERMOUNT_PROG=/path Specifies a custom path for fusermount", arg_pfx.to_uppercase()); #[cfg(feature = "appimage")] println!(" TARGET_APPIMAGE=/path Operate on a target {self_name} rather than this file itself"); + #[cfg(feature = "dwarfs")] + println!(" DWARFS_CACHESIZE=128M Size of the block cache, in bytes for DwarFS (suffixes K, M, G)"); } fn main() {