Skip to content

Commit 1244360

Browse files
committed
Add DWARFS_READAHEAD env var
Refactoring
1 parent 8d87189 commit 1244360

File tree

3 files changed

+25
-42
lines changed

3 files changed

+25
-42
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "uruntime"
3-
version = "0.2.8"
3+
version = "0.2.9"
44
readme = "README.md"
55
license = "MIT"
66
repository = "https://github.com/VHSgunzo/uruntime"

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ See [Build step in ci.yml](https://github.com/VHSgunzo/uruntime/blob/main/.githu
110110
DWARFS_WORKERS=2 Number of worker threads for DwarFS (default: equal CPU threads)
111111
DWARFS_CACHESIZE=512M Size of the block cache, in bytes for DwarFS (suffixes K, M, G)
112112
DWARFS_BLOCKSIZE=512K Size of the block file I/O, in bytes for DwarFS (suffixes K, M, G)
113+
DWARFS_READAHEAD=16M Set readahead size, in bytes for DwarFS (suffixes K, M, G)
113114
```
114115

115116
* **AppImage runtime usage**
@@ -175,6 +176,7 @@ See [Build step in ci.yml](https://github.com/VHSgunzo/uruntime/blob/main/.githu
175176
DWARFS_WORKERS=2 Number of worker threads for DwarFS (default: equal CPU threads)
176177
DWARFS_CACHESIZE=512M Size of the block cache, in bytes for DwarFS (suffixes K, M, G)
177178
DWARFS_BLOCKSIZE=512K Size of the block file I/O, in bytes for DwarFS (suffixes K, M, G)
179+
DWARFS_READAHEAD=16M Set readahead size, in bytes for DwarFS (suffixes K, M, G)
178180
```
179181

180182
### **Built-in configuration:**
@@ -210,4 +212,4 @@ sed -i 's|URUNTIME_MOUNT=[0-9]|URUNTIME_MOUNT=0|' /path/uruntime
210212
211213
# URUNTIME_MOUNT=1 - Enable unmounting of the mount directory (default)
212214
sed -i 's|URUNTIME_MOUNT=[0-9]|URUNTIME_MOUNT=1|' /path/uruntime
213-
```
215+
```

src/main.rs

+21-40
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ const MAX_EXTRACT_SELF_SIZE: u64 = 350 * 1024 * 1024; // 350 MB
2828
const DWARFS_CACHESIZE: &str = "512M";
2929
#[cfg(feature = "dwarfs")]
3030
const DWARFS_BLOCKSIZE: &str = "512K";
31+
#[cfg(feature = "dwarfs")]
32+
const DWARFS_READAHEAD: &str = "16M";
3133

3234
cfg_if! {
3335
if #[cfg(feature = "appimage")] {
@@ -389,36 +391,13 @@ fn create_tmp_dirs(dirs: Vec<&PathBuf>) -> Result<()> {
389391
}
390392

391393
#[cfg(feature = "dwarfs")]
392-
fn get_dwfs_cachesize() -> String {
393-
let cachesize_env = get_env_var("DWARFS_CACHESIZE");
394-
if cachesize_env.is_empty() {
395-
DWARFS_CACHESIZE.into()
396-
} else {
397-
let opts: Vec<&str> = cachesize_env.split(',').collect();
398-
opts.first().unwrap_or(&DWARFS_CACHESIZE).to_string()
399-
}
400-
}
401-
402-
#[cfg(feature = "dwarfs")]
403-
fn get_dwfs_blocksize() -> String {
404-
let blocksize_env = get_env_var("DWARFS_BLOCKSIZE");
405-
if blocksize_env.is_empty() {
406-
DWARFS_BLOCKSIZE.into()
407-
} else {
408-
let opts: Vec<&str> = blocksize_env.split(',').collect();
409-
opts.first().unwrap_or(&DWARFS_BLOCKSIZE).to_string()
410-
}
411-
}
412-
413-
#[cfg(feature = "dwarfs")]
414-
fn get_dwfs_workers() -> String {
415-
let num_threads = num_cpus::get().to_string();
416-
let workers_env = get_env_var("DWARFS_WORKERS");
417-
if workers_env.is_empty() {
418-
num_threads
394+
fn get_dwfs_option(option: &str, default: &str) -> String {
395+
let option_env = get_env_var(option);
396+
if option_env.is_empty() {
397+
default.into()
419398
} else {
420-
let opts: Vec<&str> = workers_env.split(',').collect();
421-
opts.first().unwrap_or(&num_threads.as_str()).to_string()
399+
let opts: Vec<&str> = option_env.split(',').collect();
400+
opts.first().unwrap_or(&default).to_string()
422401
}
423402
}
424403

@@ -436,10 +415,11 @@ fn mount_image(embed: &Embed, image: &Image, mount_dir: PathBuf) {
436415
embed.dwarfs(vec!["-f".into(),
437416
"-o".into(), "ro,nodev,noatime,clone_fd".into(),
438417
"-o".into(), "cache_files,no_cache_image".into(),
439-
"-o".into(), format!("cachesize={}", get_dwfs_cachesize()),
440-
"-o".into(), format!("blocksize={}", get_dwfs_blocksize()),
418+
"-o".into(), format!("cachesize={}", get_dwfs_option("DWARFS_CACHESIZE", DWARFS_CACHESIZE)),
419+
"-o".into(), format!("blocksize={}", get_dwfs_option("DWARFS_BLOCKSIZE", DWARFS_BLOCKSIZE)),
420+
"-o".into(), format!("readahead={}", get_dwfs_option("DWARFS_READAHEAD", DWARFS_READAHEAD)),
441421
"-o".into(), "tidy_strategy=time,tidy_interval=500ms,tidy_max_age=1s".into(),
442-
"-o".into(), format!("workers={}", get_dwfs_workers()),
422+
"-o".into(), format!("workers={}", get_dwfs_option("DWARFS_WORKERS", &num_cpus::get().to_string())),
443423
"-o".into(), format!("uid={uid},gid={gid}"),
444424
"-o".into(), format!("offset={}", image.offset),
445425
"-o".into(), "debuglevel=error".into(),
@@ -502,9 +482,9 @@ fn extract_image(embed: &Embed, image: &Image, mut extract_dir: PathBuf, is_extr
502482
let mut exec_args = vec![
503483
"--input".into(), image_path,
504484
"--log-level=error".into(),
505-
format!("--cache-size={}", get_dwfs_cachesize()),
485+
format!("--cache-size={}", get_dwfs_option("DWARFS_CACHESIZE", DWARFS_CACHESIZE)),
506486
format!("--image-offset={}", image.offset),
507-
format!("--num-workers={}", get_dwfs_workers()),
487+
format!("--num-workers={}", get_dwfs_option("DWARFS_WORKERS", &num_cpus::get().to_string())),
508488
"--output".into(), extract_dir,
509489
"--stdout-progress".into()
510490
];
@@ -707,12 +687,13 @@ fn print_usage(portable_home: &PathBuf, portable_config: &PathBuf) {
707687
FUSERMOUNT_PROG=/path Specifies a custom path for fusermount
708688
TARGET_{}=/path Operate on a target {SELF_NAME} rather than this file itself",
709689
ARG_PFX.to_uppercase(), SELF_NAME.to_uppercase());
710-
#[cfg(feature = "dwarfs")]
711-
println!(" DWARFS_WORKERS=2 Number of worker threads for DwarFS (default: equal CPU threads)");
712-
#[cfg(feature = "dwarfs")]
713-
println!(" DWARFS_CACHESIZE=512M Size of the block cache, in bytes for DwarFS (suffixes K, M, G)");
714-
#[cfg(feature = "dwarfs")]
715-
println!(" DWARFS_BLOCKSIZE=512K Size of the block file I/O, in bytes for DwarFS (suffixes K, M, G)");
690+
#[cfg(feature = "dwarfs")]
691+
{
692+
println!(" DWARFS_WORKERS=2 Number of worker threads for DwarFS (default: equal CPU threads)
693+
DWARFS_CACHESIZE=512M Size of the block cache, in bytes for DwarFS (suffixes K, M, G)
694+
DWARFS_BLOCKSIZE=512K Size of the block file I/O, in bytes for DwarFS (suffixes K, M, G)
695+
DWARFS_READAHEAD=16M Set readahead size, in bytes for DwarFS (suffixes K, M, G)");
696+
}
716697
}
717698

718699
fn main() {

0 commit comments

Comments
 (0)