diff --git a/NEWS.md b/NEWS.md index a9bbcbb..09a11ff 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,7 @@ # clusters (development version) +* Inside a Slurm job (`.slurmAllocation()` non-NULL) the host headroom is not applied: no `options(clusters.memoryHeadroom)`, no `options(clusters.keepFreeCores)` and none of `freeCores()`'s `fraction` (all the job's CPUs are free, whether or not it is the whole node). A job whose memory limit cannot be read keeps the headroom, since its memory is then the node's. The job owns its allocation, so the headroom only wasted it (GPSC 2026-11-14: a 64-CPU, 454G whole-node job with 437 GB free capped 10 GB workers at 39, and a 40-worker fit waited over an hour). `.limitToJob()` flags the probe (`in_slurm_job`, not a ledger column), which `.fitCapacity()` reads, so `reserveCapacity()` follows too. Outside Slurm nothing changes. + * A fit no longer reads and rewrites the shared `hostSpeed.rds` and `fitMemory.rds` after every generation, which took 2.9 s with all workers idle at 367k rows (8 s at a full 30-day window; 0.03 s on an empty file). `DEoptimIterative()` keeps its records in memory and writes them at each rebalance (`options(clusters.rebalanceEvery)`, default 100 generations), which reads them, and when the fit ends, under the same lock; the files, columns and record semantics are unchanged, so other versions keep sharing them. A fit that is killed loses the records since its last write. * `plan_psock_min()` mirrors every library of `libPath` (or `.libPaths()`) to the other hosts, in order, not only the first; R's own library is skipped. A single `libPath` behaves as before. A worker's `.libPaths()` now starts with those libraries, in order; the per-user library (`R_LIBS_USER`) comes after them, not first, so what is installed there no longer shadows the run's libraries. diff --git a/R/clusterSetup.R b/R/clusterSetup.R index 44fc462..a85b641 100644 --- a/R/clusterSetup.R +++ b/R/clusterSetup.R @@ -32,7 +32,7 @@ #' @param runName The `runName` the fit will be given in [DEoptimIterative()]. A host gets at most as many #' workers as its free memory holds, at the memory per worker that fit recorded the last time it ran; see #' [plan_psock_min()] for what is used without a record (`options(clusters.workerMemoryGB)`, unset: no cap). -#' Hosts keep `options(clusters.memoryHeadroom)` (default 0.1) of their memory free. When memory leaves +#' Hosts keep `options(clusters.memoryHeadroom)` (default 0.1) of their memory free (none inside a Slurm job, whose allocation is its own). When memory leaves #' fewer workers than `nCoresNeeded`, the cluster is built smaller (with a message naming the hosts) and #' `NP` follows the workers. #' @param controlArgs Named list of further [DEoptim::DEoptim.control()] settings (for example diff --git a/R/estimateCluster.R b/R/estimateCluster.R index 152997f..69f6b44 100644 --- a/R/estimateCluster.R +++ b/R/estimateCluster.R @@ -10,7 +10,8 @@ #' run fastest ([.speedAllocate()]: the hardware model [.workerSpeed()] times each host's measured speed, #' [.hostSpeedFactor()]), and optionally launches the final cluster with the computed worker counts. #' Every host keeps `options(clusters.keepFreeCores)` (default 2) of its threads free of all workers and -#' other load, and `options(clusters.memoryHeadroom)` (default 0.1) of its memory; a running cluster asks +#' other load, and `options(clusters.memoryHeadroom)` (default 0.1) of its memory (inside a Slurm job, which owns its +#' CPUs and memory, keeps neither when its memory limit is known: all its CPUs and memory are free); a running cluster asks #' again by the same rule every `options(clusters.rebalanceEvery)` generations (see [.rebalanceFn()]). #' #' This is intentionally minimal—no `master` argument, no diagnostics or fallbacks. @@ -44,7 +45,8 @@ #' `options(clusters.workerMemoryGB)` (another fit's record is never used); when that is unset (the #' default) no host is capped by memory. For FireSense DEoptim workers 14 fits: peak memory was median 4.5 GB, 90th percentile about #' 6.5 GB, maximum 14.1 GB (457 workers on 15 hosts, 2026-10-09). -#' Every host keeps `options(clusters.memoryHeadroom)` (default 0.1) of its memory free. +#' Every host keeps `options(clusters.memoryHeadroom)` (default 0.1) of its memory free, except inside a Slurm job, +#' which keeps none (its allocation is its own). #' #' @return A list with: #' - `probe`: per-host capacity & load, @@ -488,7 +490,7 @@ plan_psock_min <- function( loadavg = attr(freeC, "loadavg"), mem_available_gb = tryCatch(.procMemoryGB("MemAvailable", "/proc/meminfo"), error = function(e) NA_real_), mem_total_gb = tryCatch(.procMemoryGB("MemTotal", "/proc/meminfo"), error = function(e) NA_real_) - ), job, systemCores = attr(freeC, "maxCores")) + ), job) } ## The node table: one row per host from what each said of itself (`caps`, .probeHost()'s lists) @@ -506,6 +508,7 @@ plan_psock_min <- function( loadavg_15 = unname(cap$loadavg["15min"]), mem_available_gb = if (is.null(cap$mem_available_gb)) NA_real_ else cap$mem_available_gb, mem_total_gb = if (is.null(cap$mem_total_gb)) NA_real_ else cap$mem_total_gb, + in_slurm_job = isTRUE(cap$inSlurmJob), # see .limitToJob(); not a ledger column stringsAsFactors = FALSE ) }, hosts, caps)) @@ -516,7 +519,7 @@ plan_psock_min <- function( ## workers go within these cores is .speedAllocate()'s, by speed. ## ## Cores: the probed free cores, less what other clusters booked (see freeCoresLessReserved()), and never -## more than the host's threads less `getOption("clusters.keepFreeCores", 2)` less `busy`, the workers already +## more than the host's threads less .keepFreeCores() (none in a Slurm job) less `busy`, the workers already ## on the host: other clusters' bookings or the load average, whichever is larger. That cap is hard, whatever ## the load window or the reservations say (2026-10-13: load that no cluster had booked was discounted by ## freeCores()'s fraction only, so a 16-thread host with 10 of other load could still be given 5 workers). @@ -526,7 +529,7 @@ plan_psock_min <- function( ## ## Memory limits the cores too, when the memory per worker is known (`memPerWorkerGB`, see ## .memPerWorkerGB(); one number, or `c(median, max)`): a host keeps `getOption("clusters.memoryHeadroom", 0.1)` -## of its memory free, and its workers are at most the largest n with `(n - 1) * median + max <= available +## of its memory free (none inside a Slurm job, which owns its allocation), and its workers are at most the largest n with `(n - 1) * median + max <= available ## memory - headroom` (see .memBudgetGB()): what the host uses now plus the new workers stays within 0.9 of its memory. Available memory is the probed MemAvailable, less what other ## builds booked and their workers have not yet grown into (see freeCoresLessReserved()), plus what `own` ## workers use. `ram_lost` is the cores memory took away. @@ -561,7 +564,7 @@ plan_psock_min <- function( ## the hard cap: every worker on the host, this cluster's and all other load, within the threads less the ## cores kept for the host's other users if (!is.null(nodes$cores_total)) { - cap <- pmax(as.numeric(nodes$cores_total) - getOption("clusters.keepFreeCores", 2) - nodes$busy, 0) + cap <- pmax(as.numeric(nodes$cores_total) - .keepFreeCores(nodes) - nodes$busy, 0) cap[is.na(cap)] <- Inf nodes$free_est <- pmin(nodes$free_est, cap) } @@ -577,7 +580,8 @@ plan_psock_min <- function( nodes$speedFactor <- .hostSpeedFactor(nodes, speeds, beta = beta) nodes$ram_lost <- 0 if (useMemory) { - headroom <- getOption("clusters.memoryHeadroom", 0.1) * as.numeric(nodes$mem_total_gb) + ## a host that is a Slurm job's own keeps none: its cgroup enforces the memory (see .limitToJob()) + headroom <- ifelse(.inSlurmJob(nodes), 0, getOption("clusters.memoryHeadroom", 0.1)) * as.numeric(nodes$mem_total_gb) byMemory <- .memWorkersFitting(nodes$mem_free_gb - headroom, mem) capped <- !is.na(byMemory) & byMemory < floor(nodes$free_est) # a host that cannot say is not capped nodes$ram_lost[capped] <- floor(nodes$free_est[capped]) - byMemory[capped] @@ -592,6 +596,15 @@ plan_psock_min <- function( nodes } +## Which hosts (rows of a node table) are a Slurm job's own allocation (see .limitToJob()); FALSE for a table +## without the column (built by tests or an older clusters) +.inSlurmJob <- function(nodes) + if (is.null(nodes$in_slurm_job)) rep(FALSE, NROW(nodes)) else nodes$in_slurm_job %in% TRUE + +## The cores each host keeps free of workers for its other users: getOption("clusters.keepFreeCores", 2), none on +## a Slurm job's own allocation, which has no other users +.keepFreeCores <- function(nodes) ifelse(.inSlurmJob(nodes), 0, getOption("clusters.keepFreeCores", 2)) + #' How fast each worker on a host runs, relative to one worker alone #' #' A worker runs at full speed while the host's workers are no more than its memory modules (`dimms`) and its diff --git a/R/reservations.R b/R/reservations.R index bf2d7a3..85ed5c0 100644 --- a/R/reservations.R +++ b/R/reservations.R @@ -241,7 +241,7 @@ releaseCores <- function(id = NULL, pid = Sys.getpid(), #' load of 30-35 (2026-10-02). Read as free, that gap was booked by every later build, until hosts #' with 48 threads carried 50-52 workers. So `free_est` is also capped at `cores_total` less every #' worker booked on the host, absorbed or not, and less `getOption("clusters.keepFreeCores", 2)` cores -#' left for the host's other users, when `nodes` has `cores_total`. +#' left for the host's other users (none inside a Slurm job), when `nodes` has `cores_total`. #' #' Memory is booked the same way. When `nodes` has `mem_free_gb` (the host's `MemAvailable`), each #' reservation's `memGB` is subtracted by the same unabsorbed share: a new cluster's workers have not grown @@ -295,9 +295,9 @@ freeCoresLessReserved <- function(nodes, nodes$reserved_gb <- reservedGB } free <- as.numeric(nodes$free_est) - round(subtract, 3) - ## every host keeps getOption("clusters.keepFreeCores", 2) cores for its other users, whatever its load + ## every host keeps .keepFreeCores() cores for its other users, whatever its load (none in a Slurm job) if (!is.null(nodes$cores_total)) - free <- pmin(free, as.numeric(nodes$cores_total) - getOption("clusters.keepFreeCores", 2) - reserved) + free <- pmin(free, as.numeric(nodes$cores_total) - .keepFreeCores(nodes) - reserved) nodes$free_est <- pmax(free, 0) nodes } diff --git a/R/reserveCapacity.R b/R/reserveCapacity.R index d7796b2..f211690 100644 --- a/R/reserveCapacity.R +++ b/R/reserveCapacity.R @@ -7,7 +7,7 @@ #' and a fit placed its workers on a host the predictions had already filled (2026-10-13). #' `reserveCapacity()` books such a job in the same ledger, under the same caps as a build (`.fitCapacity()`: #' every worker and other load within the host's threads less `options(clusters.keepFreeCores)`, and memory -#' used plus the job's within `1 - options(clusters.memoryHeadroom)` of the host's), so a build that follows +#' used plus the job's within `1 - options(clusters.memoryHeadroom)` of the host's; neither inside a Slurm job), so a build that follows #' subtracts the job's cores and memory ([freeCoresLessReserved()]). It waits like a build waits, up to #' `waitSeconds` (`options(clusters.waitForCores)`), until the whole of `cores` and `memGB` fits on one of #' the `hosts`, probing every `interval` seconds, and then stops with the shortfall. Among several `hosts` diff --git a/R/slurmAllocation.R b/R/slurmAllocation.R index 3581c18..45dc079 100644 --- a/R/slurmAllocation.R +++ b/R/slurmAllocation.R @@ -49,18 +49,18 @@ } ## A host's probe (`cap`, as .probeHost() builds it) limited to the job it runs in (`job`, from -## .slurmAllocation()). Memory is the lower of the node's and the job's. Cores: a job given fewer CPUs -## (`cores_total`) than the node has (`systemCores`) shares it, so its CPUs are all free (the cgroup keeps others off -## them; the node's load average is other jobs' and says nothing of them), and the node's load is not -## carried into `busy` (see .fitCapacity()). What the job's own clusters have booked is in the reservations -## ledger, which counts it (see freeCoresLessReserved()). A job given every CPU is the node's: probed as before. -.limitToJob <- function(cap, job, systemCores) { +## .slurmAllocation()). Memory is the lower of the node's and the job's. A job owns its allocation (the cgroup +## enforces its memory and keeps others off its CPUs), so the headroom that protects a shared workstation from +## other users' growth is not applied: every CPU of the job is free, whole node or not (what the job's own +## clusters use is in the reservations ledger, see freeCoresLessReserved(); the load average says nothing more), +## and `inSlurmJob` tells .fitCapacity() to keep no memory headroom, but only when the job's memory limit is known +## (otherwise `mem_total_gb` is the node's, which other jobs may share). +.limitToJob <- function(cap, job) { if (is.null(job)) return(cap) ## cores_total is parallelly::availableCores(): the job's CPUs, read from Slurm and its cgroup - if (isTRUE(cap$cores_total < systemCores)) { - cap$free_est <- cap$cores_total - cap$loadavg[] <- 0 - } + cap$free_est <- cap$cores_total + cap$loadavg[] <- 0 + cap$inSlurmJob <- !is.na(job$memLimitGB) cap$mem_available_gb <- .minKnown(cap$mem_available_gb, job$memAvailableGB) cap$mem_total_gb <- .minKnown(cap$mem_total_gb, job$memLimitGB) cap diff --git a/man/clusterSetup.Rd b/man/clusterSetup.Rd index a514984..f19e7bb 100644 --- a/man/clusterSetup.Rd +++ b/man/clusterSetup.Rd @@ -59,7 +59,7 @@ cluster and no \code{NP}, \code{NP} is its number of rows.} \item{runName}{The \code{runName} the fit will be given in \code{\link[=DEoptimIterative]{DEoptimIterative()}}. A host gets at most as many workers as its free memory holds, at the memory per worker that fit recorded the last time it ran; see \code{\link[=plan_psock_min]{plan_psock_min()}} for what is used without a record (\code{options(clusters.workerMemoryGB)}, unset: no cap). -Hosts keep \code{options(clusters.memoryHeadroom)} (default 0.1) of their memory free. When memory leaves +Hosts keep \code{options(clusters.memoryHeadroom)} (default 0.1) of their memory free (none inside a Slurm job, whose allocation is its own). When memory leaves fewer workers than \code{nCoresNeeded}, the cluster is built smaller (with a message naming the hosts) and \code{NP} follows the workers.} } diff --git a/man/freeCoresLessReserved.Rd b/man/freeCoresLessReserved.Rd index 2a10ade..1f66f72 100644 --- a/man/freeCoresLessReserved.Rd +++ b/man/freeCoresLessReserved.Rd @@ -55,7 +55,7 @@ while each generation's slowest evaluation finishes, so a host carrying 50 booke load of 30-35 (2026-10-02). Read as free, that gap was booked by every later build, until hosts with 48 threads carried 50-52 workers. So \code{free_est} is also capped at \code{cores_total} less every worker booked on the host, absorbed or not, and less \code{getOption("clusters.keepFreeCores", 2)} cores -left for the host's other users, when \code{nodes} has \code{cores_total}. +left for the host's other users (none inside a Slurm job), when \code{nodes} has \code{cores_total}. Memory is booked the same way. When \code{nodes} has \code{mem_free_gb} (the host's \code{MemAvailable}), each reservation's \code{memGB} is subtracted by the same unabsorbed share: a new cluster's workers have not grown diff --git a/man/plan_psock_min.Rd b/man/plan_psock_min.Rd index 31cfcf6..cf85a41 100644 --- a/man/plan_psock_min.Rd +++ b/man/plan_psock_min.Rd @@ -65,7 +65,8 @@ median and largest worker. With no record for it, each worker is budgeted at \code{options(clusters.workerMemoryGB)} (another fit's record is never used); when that is unset (the default) no host is capped by memory. For FireSense DEoptim workers 14 fits: peak memory was median 4.5 GB, 90th percentile about 6.5 GB, maximum 14.1 GB (457 workers on 15 hosts, 2026-10-09). -Every host keeps \code{options(clusters.memoryHeadroom)} (default 0.1) of its memory free.} +Every host keeps \code{options(clusters.memoryHeadroom)} (default 0.1) of its memory free, except inside a Slurm job, +which keeps none (its allocation is its own).} } \value{ A list with: @@ -87,7 +88,8 @@ installs & loads \code{pkgsNeeded} in that user library, measures per-host capac run fastest (\code{\link[=.speedAllocate]{.speedAllocate()}}: the hardware model \code{\link[=.workerSpeed]{.workerSpeed()}} times each host's measured speed, \code{\link[=.hostSpeedFactor]{.hostSpeedFactor()}}), and optionally launches the final cluster with the computed worker counts. Every host keeps \code{options(clusters.keepFreeCores)} (default 2) of its threads free of all workers and -other load, and \code{options(clusters.memoryHeadroom)} (default 0.1) of its memory; a running cluster asks +other load, and \code{options(clusters.memoryHeadroom)} (default 0.1) of its memory (inside a Slurm job, which owns its +CPUs and memory, keeps neither when its memory limit is known: all its CPUs and memory are free); a running cluster asks again by the same rule every \code{options(clusters.rebalanceEvery)} generations (see \code{\link[=.rebalanceFn]{.rebalanceFn()}}). This is intentionally minimal—no \code{master} argument, no diagnostics or fallbacks. diff --git a/man/reserveCapacity.Rd b/man/reserveCapacity.Rd index 67c06e4..13715a0 100644 --- a/man/reserveCapacity.Rd +++ b/man/reserveCapacity.Rd @@ -66,7 +66,7 @@ long-running job in an ordinary R process -- a FireSense prediction is one SpaDE and a fit placed its workers on a host the predictions had already filled (2026-10-13). \code{reserveCapacity()} books such a job in the same ledger, under the same caps as a build (\code{.fitCapacity()}: every worker and other load within the host's threads less \code{options(clusters.keepFreeCores)}, and memory -used plus the job's within \code{1 - options(clusters.memoryHeadroom)} of the host's), so a build that follows +used plus the job's within \code{1 - options(clusters.memoryHeadroom)} of the host's; neither inside a Slurm job), so a build that follows subtracts the job's cores and memory (\code{\link[=freeCoresLessReserved]{freeCoresLessReserved()}}). It waits like a build waits, up to \code{waitSeconds} (\code{options(clusters.waitForCores)}), until the whole of \code{cores} and \code{memGB} fits on one of the \code{hosts}, probing every \code{interval} seconds, and then stops with the shortfall. Among several \code{hosts} diff --git a/tests/testthat/test-slurmAllocation.R b/tests/testthat/test-slurmAllocation.R index 8e02cbd..7694aeb 100644 --- a/tests/testthat/test-slurmAllocation.R +++ b/tests/testthat/test-slurmAllocation.R @@ -30,7 +30,7 @@ test_that("outside Slurm there is no job allocation and the probe is unchanged", withr::local_envvar(SLURM_JOB_ID = NA, SLURM_MEM_PER_NODE = NA) expect_null(clusters:::.slurmAllocation()) cap <- nodeCap() - expect_identical(clusters:::.limitToJob(cap, NULL, systemCores = 64L), cap) + expect_identical(clusters:::.limitToJob(cap, NULL), cap) }) test_that("the allocation is read from Slurm's variables and the job's cgroup", { @@ -62,12 +62,12 @@ test_that("without a cgroup limit the job's memory is SLURM_MEM_PER_NODE (MB)", expect_true(is.na(job$memLimitGB) && is.na(job$memAvailableGB)) }) -test_that("41 CPUs and 200 GB of a 64-CPU node give at most 41 - keepFreeCores workers and 200 GB", { +test_that("41 CPUs and 200 GB of a 64-CPU node give all 41 workers (no cores kept free in a job) and 200 GB", { withLedger() cg <- mockCgroup() withr::local_envvar(SLURM_JOB_ID = "77", SLURM_MEM_PER_NODE = NA) job <- clusters:::.slurmAllocation(procCgroup = cg$proc, cgroupRoot = cg$root) - limited <- clusters:::.limitToJob(nodeCap(cores = 41L), job, systemCores = 64L) + limited <- clusters:::.limitToJob(nodeCap(cores = 41L), job) expect_equal(limited$cores_total, 41L) expect_lte(limited$cores_physical, 41) expect_lte(limited$mem_total_gb, 200) @@ -75,32 +75,32 @@ test_that("41 CPUs and 200 GB of a 64-CPU node give at most 41 - keepFreeCores w ## other users' load on the node is not this job's expect_true(all(limited$loadavg == 0)) nodes <- clusters:::.nodeTable("localhost", list(limited)) - keep <- getOption("clusters.keepFreeCores", 2) + withr::local_options(clusters.keepFreeCores = 2) got <- suppressMessages(clusters:::.fitCapacity(nodes, total = 100, load_memory = "5min")) - expect_equal(got$free_est, 41 - keep) - ## 12 GB workers: (180 GB available - 10% of 200 GB) / 12 = 13, fewer than the cores allow + expect_equal(got$free_est, 41) + ## 12 GB workers: 180 GB available / 12 = 15 (a job keeps no memory headroom), fewer than the cores allow got <- suppressMessages(clusters:::.fitCapacity(nodes, total = 100, load_memory = "5min", memPerWorkerGB = 12)) - expect_equal(got$free_est, floor((180 - 0.1 * 200) / 12)) + expect_equal(got$free_est, floor(180 / 12)) ## the node as the probe saw it, unlimited, gave the job nothing (load 50 of 64) and counted the node's memory whole <- clusters:::.nodeTable("localhost", list(nodeCap())) - expect_lt(suppressMessages(clusters:::.fitCapacity(whole, total = 100, load_memory = "5min"))$free_est, 41 - keep) + expect_lt(suppressMessages(clusters:::.fitCapacity(whole, total = 100, load_memory = "5min"))$free_est, 41) }) test_that("the job's own workers, in the ledger, still come off its allocation", { withLedger() withr::local_envvar(SLURM_JOB_ID = "77") job <- list(memLimitGB = 200, memAvailableGB = 200) - nodes <- clusters:::.nodeTable("localhost", list(clusters:::.limitToJob(nodeCap(cores = 41L), job, systemCores = 64L))) + nodes <- clusters:::.nodeTable("localhost", list(clusters:::.limitToJob(nodeCap(cores = 41L), job))) reserveCores(data.frame(host = "localhost", assign = 20L), id = clusters:::.newReservationId()) got <- suppressMessages(clusters:::.fitCapacity(nodes, total = 100, load_memory = "5min")) expect_equal(got$reserved, 20L) - expect_equal(got$free_est, 41 - 2 - 20) + expect_equal(got$free_est, 41 - 20) }) test_that("another job's booking on the same node is not this job's", { withLedger() job <- list(memLimitGB = 200, memAvailableGB = 200) - nodes <- clusters:::.nodeTable("localhost", list(clusters:::.limitToJob(nodeCap(cores = 41L), job, systemCores = 64L))) + nodes <- clusters:::.nodeTable("localhost", list(clusters:::.limitToJob(nodeCap(cores = 41L), job))) withr::with_envvar(c(SLURM_JOB_ID = "88"), reserveCores(data.frame(host = "localhost", assign = 20L), id = clusters:::.newReservationId())) ## a booking with no job (another version's, or outside Slurm) cannot be told apart and counts @@ -108,7 +108,7 @@ test_that("another job's booking on the same node is not this job's", { withr::with_envvar(c(SLURM_JOB_ID = "77"), { got <- suppressMessages(clusters:::.fitCapacity(nodes, total = 100, load_memory = "5min")) expect_equal(got$reserved, 3L) - expect_equal(got$free_est, 41 - 2 - 3) + expect_equal(got$free_est, 41 - 3) }) ## outside Slurm all three count, as before withr::with_envvar(c(SLURM_JOB_ID = NA), { @@ -117,18 +117,37 @@ test_that("another job's booking on the same node is not this job's", { }) }) -test_that("a job given the whole node, and a probe outside Slurm, are as before", { +test_that("a job given the whole node is its own: all its CPUs free, no memory headroom, and a probe outside Slurm is as before", { withLedger() cg <- mockCgroup(max = as.character(490 * 1024^3), current = as.character(5 * 1024^3)) withr::local_envvar(SLURM_JOB_ID = "77", SLURM_MEM_PER_NODE = NA) job <- clusters:::.slurmAllocation(procCgroup = cg$proc, cgroupRoot = cg$root) cap <- nodeCap(avail = 480, load = 10) - expect_identical(clusters:::.limitToJob(cap, job, systemCores = 64L), cap) + got <- clusters:::.limitToJob(cap, job) + expect_true(got$inSlurmJob) + expect_equal(got$free_est, 64L) + expect_true(all(got$loadavg == 0)) + expect_equal(got$mem_available_gb, 480) ## the same node with less memory free than the cgroup says (other processes) is not raised - cap <- nodeCap(avail = 100, load = 10) - expect_identical(clusters:::.limitToJob(cap, job, systemCores = 64L), cap) + expect_equal(clusters:::.limitToJob(nodeCap(avail = 100, load = 10), job)$mem_available_gb, 100) ## the live probe outside Slurm withr::local_envvar(SLURM_JOB_ID = NA) cap <- clusters:::.probeHost("5min", 0.9) expect_equal(cap$cores_total, as.integer(parallelly::availableCores())) }) + +test_that("inside a Slurm job 454 GB, 437 GB free and 10 GB workers hold 43 workers; outside Slurm the headroom keeps 39", { + withLedger() + withr::local_options(clusters.keepFreeCores = 0) + job <- list(memLimitGB = 454, memAvailableGB = 437) + cap <- nodeCap(avail = 437, total = 454, load = 20) + capacity <- function(cap) + suppressMessages(clusters:::.fitCapacity(clusters:::.nodeTable("localhost", list(cap)), total = 40, + load_memory = "5min", memPerWorkerGB = 10))$free_est + expect_equal(capacity(clusters:::.limitToJob(cap, job)), 43) + ## the same numbers on a shared workstation: 437 - 10% of 454 = 391.6 GB holds 39 + expect_equal(capacity(cap), 39) + expect_false("inSlurmJob" %in% names(cap)) + ## a job whose memory limit is unknown is sized on the node's memory, which other jobs may share: headroom kept + expect_equal(capacity(clusters:::.limitToJob(cap, list(memLimitGB = NA_real_, memAvailableGB = NA_real_))), 39) +})