From 4612cbdfaaa8e1e713aa9f4bc5b19ba3fa721f4d Mon Sep 17 00:00:00 2001 From: Dominic Date: Sat, 18 May 2024 12:52:08 +0200 Subject: [PATCH] allow compiling out systemd-run support --- Cargo.toml | 4 ++++ src/main.rs | 13 ++++++++++--- src/render/ffmpeg.rs | 6 ++++-- src/render/mod.rs | 32 +++++++++++++++++++------------- 4 files changed, 37 insertions(+), 18 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 3a03999..bc0f991 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,3 +17,7 @@ serde = { version = "1.0.188", features = ["derive"] } serde_with = "3.4" svgwriter = "0.1" toml = { package = "basic-toml", version = "0.1.4" } + +[features] +default = ["mem_limit"] +mem_limit = [] diff --git a/src/main.rs b/src/main.rs index b21fbdd..1e46727 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,15 +16,17 @@ use iotro::Language; use rational::Rational; use serde::{Deserialize, Serialize}; use serde_with::{serde_as, DisplayFromStr}; +#[cfg(feature = "mem_limit")] +use std::sync::RwLock; use std::{ collections::BTreeSet, fmt::Display, fs, io::{self, BufRead as _, Write}, - str::FromStr, - sync::RwLock + str::FromStr }; +#[cfg(feature = "mem_limit")] static MEM_LIMIT: RwLock = RwLock::new(String::new()); #[derive(Debug, Parser)] @@ -49,6 +51,7 @@ struct Args { #[clap(short = 'L', long, default_value = "de")] lang: Language<'static>, + #[cfg(feature = "mem_limit")] /// The memory limit for external tools like ffmpeg. #[clap(short, long, default_value = "12G")] mem_limit: String, @@ -231,7 +234,11 @@ fn ask_time(question: impl Display) -> Time { fn main() { let args = Args::parse(); - *(MEM_LIMIT.write().unwrap()) = args.mem_limit; + + #[cfg(feature = "mem_limit")] + { + *(MEM_LIMIT.write().unwrap()) = args.mem_limit; + } // process arguments let directory = args.directory.canonicalize_utf8().unwrap(); diff --git a/src/render/ffmpeg.rs b/src/render/ffmpeg.rs index 0a04ce6..cb42b53 100644 --- a/src/render/ffmpeg.rs +++ b/src/render/ffmpeg.rs @@ -105,8 +105,10 @@ impl FfmpegOutput { const QUALITY: &str = "22"; if venc { let vcodec = match (self.format, vaapi) { - (FfmpegOutputFormat::Av1Flac, false) | (FfmpegOutputFormat::Av1Opus, false) => "libsvtav1", - (FfmpegOutputFormat::Av1Flac, true) | (FfmpegOutputFormat::Av1Opus, true) => "av1_vaapi", + (FfmpegOutputFormat::Av1Flac, false) + | (FfmpegOutputFormat::Av1Opus, false) => "libsvtav1", + (FfmpegOutputFormat::Av1Flac, true) + | (FfmpegOutputFormat::Av1Opus, true) => "av1_vaapi", (FfmpegOutputFormat::AvcAac, false) => "h264", (FfmpegOutputFormat::AvcAac, true) => "h264_vaapi" }; diff --git a/src/render/mod.rs b/src/render/mod.rs index 1bfd603..cfb79d8 100644 --- a/src/render/mod.rs +++ b/src/render/mod.rs @@ -9,7 +9,7 @@ use crate::{ iotro::{intro, outro}, render::ffmpeg::{Ffmpeg, FfmpegInput}, time::{format_date, Time}, - Project, ProjectSourceMetadata, Resolution, MEM_LIMIT + Project, ProjectSourceMetadata, Resolution }; use anyhow::{bail, Context}; use camino::{Utf8Path as Path, Utf8PathBuf as PathBuf}; @@ -39,18 +39,24 @@ const FF_LOGO_SIZE: usize = 128; const LOGO_SIZE: usize = 96; fn cmd() -> Command { - // we use systemd-run to limit the process memory - // I tried others like ulimit, chpst or isolate, but none worked - let mut cmd = Command::new("systemd-run"); - cmd.arg("--scope") - .arg("-q") - .arg("--expand-environment=no") - .arg("-p") - .arg(format!("MemoryMax={}", MEM_LIMIT.read().unwrap())) - .arg("--user"); - // we use busybox ash for having a shell that outputs commands with -x - cmd.arg("busybox") - .arg("ash") + #[cfg(feature = "mem_limit")] + let mut cmd = { + // we use systemd-run to limit the process memory + // I tried others like ulimit, chpst or isolate, but none worked + let mut cmd = Command::new("systemd-run"); + cmd.arg("--scope") + .arg("-q") + .arg("--expand-environment=no") + .arg("-p") + .arg(format!("MemoryMax={}", crate::MEM_LIMIT.read().unwrap())) + .arg("--user"); + // we use busybox ash for having a shell that outputs commands with -x + cmd.arg("busybox"); + cmd + }; + #[cfg(not(feature = "mem_limit"))] + let mut cmd = Command::new("busybox"); + cmd.arg("ash") .arg("-exuo") .arg("pipefail") .arg("-c")