Skip to content

Commit f561456

Browse files
authored
Merge pull request #1279 from jyn514/not-enough-ram
Don't execute a build when not enough memory is available
2 parents e8a6303 + df50879 commit f561456

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

src/config.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ pub struct Config {
5555
pub(crate) toolchain: String,
5656
pub(crate) build_cpu_limit: Option<u32>,
5757
pub(crate) include_default_targets: bool,
58+
pub(crate) disable_memory_limit: bool,
5859
}
5960

6061
impl Config {
@@ -101,6 +102,7 @@ impl Config {
101102
toolchain: env("CRATESFYI_TOOLCHAIN", "nightly".to_string())?,
102103
build_cpu_limit: maybe_env("DOCS_RS_BUILD_CPU_LIMIT")?,
103104
include_default_targets: env("DOCSRS_INCLUDE_DEFAULT_TARGETS", true)?,
105+
disable_memory_limit: env("DOCSRS_DISABLE_MEMORY_LIMIT", false)?,
104106
})
105107
}
106108
}

src/docbuilder/rustwide_builder.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use crate::utils::{copy_doc_dir, parse_rustc_version, CargoMetadata, GithubUpdat
1111
use crate::{db::blacklist::is_blacklisted, utils::MetadataPackage};
1212
use crate::{Config, Context, Index, Metrics, Storage};
1313
use docsrs_metadata::{Metadata, DEFAULT_TARGETS, HOST_TARGET};
14+
use failure::bail;
1415
use failure::ResultExt;
1516
use log::{debug, info, warn, LevelFilter};
1617
use postgres::Client;
@@ -309,6 +310,24 @@ impl RustwideBuilder {
309310
}
310311

311312
let limits = Limits::for_crate(&mut conn, name)?;
313+
#[cfg(target_os = "linux")]
314+
if !self.config.disable_memory_limit {
315+
let mem_info = procfs::Meminfo::new().context("failed to read /proc/meminfo")?;
316+
let available = mem_info
317+
.mem_available
318+
.expect("kernel version too old for determining memory limit");
319+
if limits.memory() as u64 > available {
320+
bail!("not enough memory to build {} {}: needed {} MiB, have {} MiB\nhelp: set DOCSRS_DISABLE_MEMORY_LIMIT=true to force a build",
321+
name, version, limits.memory() / 1024 / 1024, available / 1024 / 1024
322+
);
323+
} else {
324+
debug!(
325+
"had enough memory: {} MiB > {} MiB",
326+
limits.memory() / 1024 / 1024,
327+
available / 1024 / 1024
328+
);
329+
}
330+
}
312331

313332
let mut build_dir = self.workspace.build_dir(&format!("{}-{}", name, version));
314333
build_dir.purge()?;

0 commit comments

Comments
 (0)