allow compiling out systemd-run support

This commit is contained in:
Dominic 2024-05-18 12:52:08 +02:00
parent 0d98ab21f0
commit 4612cbdfaa
Signed by: msrd0
GPG key ID: AAF7C8430CA3345D
4 changed files with 37 additions and 18 deletions

View file

@ -17,3 +17,7 @@ serde = { version = "1.0.188", features = ["derive"] }
serde_with = "3.4" serde_with = "3.4"
svgwriter = "0.1" svgwriter = "0.1"
toml = { package = "basic-toml", version = "0.1.4" } toml = { package = "basic-toml", version = "0.1.4" }
[features]
default = ["mem_limit"]
mem_limit = []

View file

@ -16,15 +16,17 @@ use iotro::Language;
use rational::Rational; use rational::Rational;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_with::{serde_as, DisplayFromStr}; use serde_with::{serde_as, DisplayFromStr};
#[cfg(feature = "mem_limit")]
use std::sync::RwLock;
use std::{ use std::{
collections::BTreeSet, collections::BTreeSet,
fmt::Display, fmt::Display,
fs, fs,
io::{self, BufRead as _, Write}, io::{self, BufRead as _, Write},
str::FromStr, str::FromStr
sync::RwLock
}; };
#[cfg(feature = "mem_limit")]
static MEM_LIMIT: RwLock<String> = RwLock::new(String::new()); static MEM_LIMIT: RwLock<String> = RwLock::new(String::new());
#[derive(Debug, Parser)] #[derive(Debug, Parser)]
@ -49,6 +51,7 @@ struct Args {
#[clap(short = 'L', long, default_value = "de")] #[clap(short = 'L', long, default_value = "de")]
lang: Language<'static>, lang: Language<'static>,
#[cfg(feature = "mem_limit")]
/// The memory limit for external tools like ffmpeg. /// The memory limit for external tools like ffmpeg.
#[clap(short, long, default_value = "12G")] #[clap(short, long, default_value = "12G")]
mem_limit: String, mem_limit: String,
@ -231,7 +234,11 @@ fn ask_time(question: impl Display) -> Time {
fn main() { fn main() {
let args = Args::parse(); let args = Args::parse();
*(MEM_LIMIT.write().unwrap()) = args.mem_limit;
#[cfg(feature = "mem_limit")]
{
*(MEM_LIMIT.write().unwrap()) = args.mem_limit;
}
// process arguments // process arguments
let directory = args.directory.canonicalize_utf8().unwrap(); let directory = args.directory.canonicalize_utf8().unwrap();

View file

@ -105,8 +105,10 @@ impl FfmpegOutput {
const QUALITY: &str = "22"; const QUALITY: &str = "22";
if venc { if venc {
let vcodec = match (self.format, vaapi) { let vcodec = match (self.format, vaapi) {
(FfmpegOutputFormat::Av1Flac, false) | (FfmpegOutputFormat::Av1Opus, false) => "libsvtav1", (FfmpegOutputFormat::Av1Flac, false)
(FfmpegOutputFormat::Av1Flac, true) | (FfmpegOutputFormat::Av1Opus, true) => "av1_vaapi", | (FfmpegOutputFormat::Av1Opus, false) => "libsvtav1",
(FfmpegOutputFormat::Av1Flac, true)
| (FfmpegOutputFormat::Av1Opus, true) => "av1_vaapi",
(FfmpegOutputFormat::AvcAac, false) => "h264", (FfmpegOutputFormat::AvcAac, false) => "h264",
(FfmpegOutputFormat::AvcAac, true) => "h264_vaapi" (FfmpegOutputFormat::AvcAac, true) => "h264_vaapi"
}; };

View file

@ -9,7 +9,7 @@ use crate::{
iotro::{intro, outro}, iotro::{intro, outro},
render::ffmpeg::{Ffmpeg, FfmpegInput}, render::ffmpeg::{Ffmpeg, FfmpegInput},
time::{format_date, Time}, time::{format_date, Time},
Project, ProjectSourceMetadata, Resolution, MEM_LIMIT Project, ProjectSourceMetadata, Resolution
}; };
use anyhow::{bail, Context}; use anyhow::{bail, Context};
use camino::{Utf8Path as Path, Utf8PathBuf as PathBuf}; use camino::{Utf8Path as Path, Utf8PathBuf as PathBuf};
@ -39,18 +39,24 @@ const FF_LOGO_SIZE: usize = 128;
const LOGO_SIZE: usize = 96; const LOGO_SIZE: usize = 96;
fn cmd() -> Command { fn cmd() -> Command {
// we use systemd-run to limit the process memory #[cfg(feature = "mem_limit")]
// I tried others like ulimit, chpst or isolate, but none worked let mut cmd = {
let mut cmd = Command::new("systemd-run"); // we use systemd-run to limit the process memory
cmd.arg("--scope") // I tried others like ulimit, chpst or isolate, but none worked
.arg("-q") let mut cmd = Command::new("systemd-run");
.arg("--expand-environment=no") cmd.arg("--scope")
.arg("-p") .arg("-q")
.arg(format!("MemoryMax={}", MEM_LIMIT.read().unwrap())) .arg("--expand-environment=no")
.arg("--user"); .arg("-p")
// we use busybox ash for having a shell that outputs commands with -x .arg(format!("MemoryMax={}", crate::MEM_LIMIT.read().unwrap()))
cmd.arg("busybox") .arg("--user");
.arg("ash") // 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("-exuo")
.arg("pipefail") .arg("pipefail")
.arg("-c") .arg("-c")