feat(#2754): hive-wide CPUWeight= / IOWeight= for agent containers
`CPUQuota=`/`MemoryMax=` are hard caps: they throttle an agent even when
the host is idle, so they are the wrong tool for "be polite under
contention". The cgroup v2 relative shares are, and neither was wired.
Adds `services.hyperhive.{agentCpuWeight,agentIoWeight}` (1..=10000,
default 80) threaded through the existing drop-in path: HiveEnv ->
write_dropins -> WriteResourceLimits -> hyperhive-limits.conf, next to
the caps already there. Hive-wide only, as the operator scoped it on the
issue: no per-agent override, no resource-limits.json field, no
dashboard form.
The default of 80 is below the kernel's 100, so agent containers yield
to everything *not* on this drop-in path -- host services and the infra
containers (hive-ci, hive-forge, hive-gateway, hive-matrix). It does not
rank agents against each other; they all carry the same weight.
`WriteResourceLimits` gains two `#[serde(default)]` fields, and the
writer treats weight 0 as "not configured" and omits the line, so an
older hive-c0re talking to a newer hive-priv still produces the exact
pre-weights drop-in. The body is extracted into `limits_dropin_body` so
that is covered by a test rather than asserted by eye.
This commit is contained in:
parent
80650041d9
commit
e407fa93df
8 changed files with 221 additions and 16 deletions
|
|
@ -85,6 +85,14 @@ pub struct Coordinator {
|
|||
pub agent_cpu_quota: String,
|
||||
/// Per-agent systemd `MemoryMax=` value (e.g. `"4G"`). Same drop-in.
|
||||
pub agent_memory_max: String,
|
||||
/// Per-agent systemd `CPUWeight=` (cgroup v2 `cpu.weight`, 1..=10000).
|
||||
/// Same drop-in. A *relative share* under contention, not a cap — see
|
||||
/// the `agentCpuWeight` NixOS option for the full semantics.
|
||||
pub agent_cpu_weight: u32,
|
||||
/// Per-agent systemd `IOWeight=` (cgroup v2 `io.weight`, 1..=10000).
|
||||
/// Same drop-in, same relative-share semantics as
|
||||
/// [`Self::agent_cpu_weight`].
|
||||
pub agent_io_weight: u32,
|
||||
/// Operator-tunable model→price table backing the hive-wide cost
|
||||
/// estimate on the ST4TS tab. Set via `services.hyperhive.modelPrices`
|
||||
/// and passed to `hive-c0re serve --model-prices <json>`. Models not
|
||||
|
|
@ -210,6 +218,12 @@ pub struct HiveEnv {
|
|||
pub agent_cpu_quota: String,
|
||||
/// Per-agent systemd `MemoryMax=` value (e.g. `"4G"`).
|
||||
pub agent_memory_max: String,
|
||||
/// Per-agent systemd `CPUWeight=` — cgroup v2 `cpu.weight`, 1..=10000.
|
||||
/// Relative share under contention, not a cap; see `agentCpuWeight`.
|
||||
pub agent_cpu_weight: u32,
|
||||
/// Per-agent systemd `IOWeight=` — cgroup v2 `io.weight`, 1..=10000.
|
||||
/// Same semantics as [`Self::agent_cpu_weight`]; see `agentIoWeight`.
|
||||
pub agent_io_weight: u32,
|
||||
}
|
||||
|
||||
impl Default for HiveEnv {
|
||||
|
|
@ -227,6 +241,11 @@ impl Default for HiveEnv {
|
|||
]),
|
||||
agent_cpu_quota: "200%".to_string(),
|
||||
agent_memory_max: "4G".to_string(),
|
||||
// Slightly below the kernel default of 100, so agent
|
||||
// containers yield to host services + the infra containers
|
||||
// (which carry no drop-in and stay at 100) under contention.
|
||||
agent_cpu_weight: 80,
|
||||
agent_io_weight: 80,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -457,6 +476,8 @@ impl Coordinator {
|
|||
context_window_tokens,
|
||||
agent_cpu_quota,
|
||||
agent_memory_max,
|
||||
agent_cpu_weight,
|
||||
agent_io_weight,
|
||||
} = env;
|
||||
let broker = Broker::open(db_path).context("open broker")?;
|
||||
let approvals = Approvals::open(db_path).context("open approvals")?;
|
||||
|
|
@ -501,6 +522,8 @@ impl Coordinator {
|
|||
context_window_tokens,
|
||||
agent_cpu_quota,
|
||||
agent_memory_max,
|
||||
agent_cpu_weight,
|
||||
agent_io_weight,
|
||||
model_prices,
|
||||
agents: Mutex::new(HashMap::new()),
|
||||
transient: Mutex::new(HashMap::new()),
|
||||
|
|
@ -532,6 +555,8 @@ impl Coordinator {
|
|||
context_window_tokens: self.context_window_tokens.clone(),
|
||||
agent_cpu_quota: self.agent_cpu_quota.clone(),
|
||||
agent_memory_max: self.agent_memory_max.clone(),
|
||||
agent_cpu_weight: self.agent_cpu_weight,
|
||||
agent_io_weight: self.agent_io_weight,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -25,7 +25,14 @@ pub async fn write_dropins(name: &str, hive: &HiveEnv, paths: &AgentPaths) -> Re
|
|||
set_nspawn_flags(&container, &paths.agent, &paths.claude, &paths.notes).await?;
|
||||
let (cpu_quota, memory_max) =
|
||||
crate::resource_limits::effective(name, &hive.agent_cpu_quota, &hive.agent_memory_max);
|
||||
set_resource_limits(&container, &cpu_quota, &memory_max).await?;
|
||||
set_resource_limits(
|
||||
&container,
|
||||
&cpu_quota,
|
||||
&memory_max,
|
||||
hive.agent_cpu_weight,
|
||||
hive.agent_io_weight,
|
||||
)
|
||||
.await?;
|
||||
systemd_daemon_reload().await
|
||||
}
|
||||
|
||||
|
|
@ -34,8 +41,21 @@ pub async fn write_dropins(name: &str, hive: &HiveEnv, paths: &AgentPaths) -> Re
|
|||
/// `meta/resource-limits.json` where set, the hive-wide defaults
|
||||
/// otherwise. Goes under `/run/systemd/system/...` so it's ephemeral
|
||||
/// (regenerated on every spawn / rebuild).
|
||||
async fn set_resource_limits(container: &str, cpu_quota: &str, memory_max: &str) -> Result<()> {
|
||||
crate::priv_client::write_resource_limits(container, memory_max, cpu_quota).await
|
||||
///
|
||||
/// The weights are hive-wide (`services.hyperhive.agentCpuWeight` /
|
||||
/// `agentIoWeight`) — unlike the caps they have no per-agent override in
|
||||
/// `meta/resource-limits.json`, so they come straight off [`HiveEnv`].
|
||||
async fn set_resource_limits(
|
||||
container: &str,
|
||||
cpu_quota: &str,
|
||||
memory_max: &str,
|
||||
cpu_weight: u32,
|
||||
io_weight: u32,
|
||||
) -> Result<()> {
|
||||
crate::priv_client::write_resource_limits(
|
||||
container, memory_max, cpu_quota, cpu_weight, io_weight,
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
async fn systemd_daemon_reload() -> Result<()> {
|
||||
|
|
|
|||
|
|
@ -110,6 +110,17 @@ enum Cmd {
|
|||
/// container. Set via `services.hyperhive.agentMemoryMax`.
|
||||
#[arg(long)]
|
||||
agent_memory_max: Option<String>,
|
||||
/// Override: systemd `CPUWeight=` applied to every agent container
|
||||
/// via the same drop-in — a cgroup v2 relative share under
|
||||
/// contention (1..=10000), not a cap. Set via
|
||||
/// `services.hyperhive.agentCpuWeight`.
|
||||
#[arg(long)]
|
||||
agent_cpu_weight: Option<u32>,
|
||||
/// Override: systemd `IOWeight=` applied to every agent container,
|
||||
/// the block-IO counterpart of `--agent-cpu-weight`. Set via
|
||||
/// `services.hyperhive.agentIoWeight`.
|
||||
#[arg(long)]
|
||||
agent_io_weight: Option<u32>,
|
||||
/// Override: per-model USD prices (per million tokens) for the
|
||||
/// hive-wide ST4TS cost estimate, as a JSON object mapping a
|
||||
/// model-family short name to `{input, output, cache_read,
|
||||
|
|
@ -147,6 +158,8 @@ async fn main() -> Result<()> {
|
|||
context_window_tokens,
|
||||
agent_cpu_quota,
|
||||
agent_memory_max,
|
||||
agent_cpu_weight,
|
||||
agent_io_weight,
|
||||
model_prices,
|
||||
build_slots,
|
||||
} => {
|
||||
|
|
@ -184,6 +197,12 @@ async fn main() -> Result<()> {
|
|||
if let Some(v) = agent_memory_max {
|
||||
sc.env.agent_memory_max = v;
|
||||
}
|
||||
if let Some(v) = agent_cpu_weight {
|
||||
sc.env.agent_cpu_weight = v;
|
||||
}
|
||||
if let Some(v) = agent_io_weight {
|
||||
sc.env.agent_io_weight = v;
|
||||
}
|
||||
if let Some(v) = model_prices {
|
||||
sc.model_prices =
|
||||
serde_json::from_str(&v).context("--model-prices: invalid JSON")?;
|
||||
|
|
|
|||
|
|
@ -174,11 +174,15 @@ pub async fn write_resource_limits(
|
|||
container: &str,
|
||||
memory_max: &str,
|
||||
cpu_quota: &str,
|
||||
cpu_weight: u32,
|
||||
io_weight: u32,
|
||||
) -> Result<()> {
|
||||
ok(call(&PrivRequest::WriteResourceLimits {
|
||||
container: container.to_owned(),
|
||||
memory_max: memory_max.to_owned(),
|
||||
cpu_quota: cpu_quota.to_owned(),
|
||||
cpu_weight,
|
||||
io_weight,
|
||||
})
|
||||
.await?)
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue