Skip to content

Commit

Permalink
update cpu.weight ot be within range
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanFlurry committed Nov 16, 2023
1 parent 77b80e1 commit 376c39a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 24 deletions.
27 changes: 13 additions & 14 deletions svc/pkg/mm/worker/src/workers/lobby_create/nomad_job.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{convert::TryInto, collections::HashMap};
use std::{collections::HashMap, convert::TryInto};

use chirp_worker::prelude::*;
use proto::backend::{
Expand Down Expand Up @@ -56,8 +56,7 @@ pub fn gen_lobby_docker_job(
use nomad_client::models::*;

// runc-compatible resources
let cpu =
tier.rivet_cores_numerator as u64 * 1_000 / tier.rivet_cores_denominator as u64 * 1_000; // Millicore (1/1000 of a core)
let cpu = tier.rivet_cores_numerator as u64 * 1_000 / tier.rivet_cores_denominator as u64; // Millicore (1/1000 of a core)
let memory = tier.memory * (1024 * 1024); // bytes
let memory_max = tier.memory_max * (1024 * 1024); // bytes

Expand All @@ -76,18 +75,18 @@ pub fn gen_lobby_docker_job(
} else {
None
},
memory_mb: Some((
TryInto::<i64>::try_into(memory)?
/ (1024 * 1024)
- util_job::TASK_CLEANUP_MEMORY as i64
).try_into()?),
memory_mb: Some(
(TryInto::<i64>::try_into(memory)? / (1024 * 1024)
- util_job::TASK_CLEANUP_MEMORY as i64)
.try_into()?,
),
// Allow oversubscribing memory by 50% of the reserved
// memory if using less than the node's total memory
memory_max_mb: Some((
TryInto::<i64>::try_into(memory_max)?
/ (1024 * 1024)
- util_job::TASK_CLEANUP_MEMORY as i64
).try_into()?),
memory_max_mb: Some(
(TryInto::<i64>::try_into(memory_max)? / (1024 * 1024)
- util_job::TASK_CLEANUP_MEMORY as i64)
.try_into()?,
),
disk_mb: Some(tier.disk as i32), // TODO: Is this deprecated?
..Resources::new()
};
Expand Down Expand Up @@ -226,7 +225,7 @@ pub fn gen_lobby_docker_job(
"RIVET_MAX_PLAYERS_PARTY",
template_env_var("NOMAD_META_MAX_PLAYERS_PARTY"),
),
// CPU in millishares
// CPU in millicores
//
// < 1000 is for fractional CPU
// > 1000 is for whole CPU, will always be 1000 increments
Expand Down
35 changes: 25 additions & 10 deletions svc/pkg/mm/worker/src/workers/lobby_create/oci_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,23 @@ const CPU_PERIOD: u64 = 100000;

/// Generates base config.json for an OCI bundle.
pub fn config(cpu: u64, memory: u64, memory_max: u64, env: Vec<String>) -> serde_json::Value {
// CPU shares is a relative weight. It doesn't matter what unit we pass here as
// long as the ratios between the containers are correct.
//
// Corresponds to cpu.weight in cgroups. Must be [1, 10_000]
//
// We divide by 8 in order to make sure the CPU shares are within bounds. `cpu` is measured in
// millishares, so 1_000 = 1 core. For a range of 32d1 (32_000) to 1d16 (62), we divide by 8
// to make the range 3_200 to 6.
let mut cpu_shares = cpu / 10;
if cpu_shares > 10_000 {
cpu_shares = 10_000;
tracing::warn!(?cpu_shares, "cpu_shares > 10_000");
} else if cpu_shares < 1 {
cpu_shares = 1;
tracing::warn!(?cpu_shares, "cpu_shares < 1");
}

// This is a modified version of the default config.json generated by containerd.
//
// Some values will be overridden at runtime by the values in the OCI bundle's config.json.
Expand Down Expand Up @@ -58,20 +75,18 @@ pub fn config(cpu: u64, memory: u64, memory_max: u64, env: Vec<String>) -> serde
"resources": {
"devices": linux_resources_devices(),
"cpu": {
// CPU shares is a relative weight. It doesn't matter what unit we pass here as
// long as the ratios between the containers are correct.
"shares": cpu,
"shares": cpu_shares,
// If `quota` is greater than `period`, it is allowed to use multiple cores.
//
// Read more: https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/6/html/resource_management_guide/sec-cpu
"quota": CPU_PERIOD * cpu / 1_000,
"period": CPU_PERIOD,
// "quota": CPU_PERIOD * cpu / 1_000,
// "period": CPU_PERIOD,
// Use the env var for the CPU since Nomad handles assigning CPUs to each task
"cpus": if cpu >= 1_000 {
Some(template_env_var("NOMAD_CPU_CORES"))
} else {
None
}
// "cpus": if cpu >= 1_000 {
// Some(template_env_var("NOMAD_CPU_CORES"))
// } else {
// None
// }
},
// Docker: https://github.com/moby/moby/blob/777e9f271095685543f30df0ff7a12397676f938/daemon/daemon_unix.go#L75
"memory": {
Expand Down

0 comments on commit 376c39a

Please sign in to comment.