-
Notifications
You must be signed in to change notification settings - Fork 125
feat(cgroup): add pids/cpu controllers with bandwidth throttling #1156
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
78287b4
feat(starry-kernel): scaffold cgroup v2 core types and per-controller…
dee28da
feat(starry-kernel): expose cgroupfs via pseudofs and auto-mount /cgroup
fa95991
feat(starry-kernel): wire cgroup pids limit and procs tracking into t…
b9320fe
test(cgroup): add cgroup-pids TDD test for pids controller verification
d8e9b81
fix(cgroup): fix pids controller write/read and procs tracking
7d3cc38
feat(cgroup): implement per-process cgroup tracking for pids controller
cb0f7c7
feat(cgroup): cpu controller infrastructure — weight, bandwidth, thro…
32fb4e8
fix(cgroup): sync cpu.weight to scheduler on cgroup migration + yield…
f5b2720
fix(cgroup): mark cpu.max throttle as deferred, clean debug infra
04a101b
style: apply rustfmt to cgroup cpu, cgroupfs, and proc files
ea99f9f
fix(cgroup): review fixes - try_fork CAS, exit underflow, ESRCH, exis…
4749445
fix(cgroup): resolve rebase conflicts and remove deferred API calls
a595433
fix(cgroup): address reviewer feedback - remove redundant CpuState fi…
757ede5
fix(cgroup): address reviewer feedback - pick_next_task perf, do_exit…
a93eccc
fix: restore .cargo/config.toml optional include
4ddc374
style: apply cargo fmt
09582c4
fix(cgroup): clippy clone_on_copy, init pids.fork, remove deferred AP…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| //! cgroup v2 core data structures. | ||
|
|
||
| use alloc::{ | ||
| collections::BTreeMap, | ||
| format, | ||
| string::{String, ToString}, | ||
| sync::{Arc, Weak}, | ||
| vec::Vec, | ||
| }; | ||
|
|
||
| use ax_kspin::SpinNoIrq; | ||
| use ax_lazyinit::LazyInit; | ||
| use axfs_ng_vfs::{VfsError, VfsResult}; | ||
|
|
||
| use super::{cpu::CpuState, pids::PidsState}; | ||
|
|
||
| /// A cgroup node in the hierarchy. | ||
| #[allow(dead_code)] | ||
| pub struct CgroupNode { | ||
| /// Directory name (e.g. "my-cgroup"). | ||
| pub name: String, | ||
| /// Full path from root (e.g. "/my-cgroup"). | ||
| pub path: String, | ||
| /// Child cgroups. | ||
| pub children: SpinNoIrq<BTreeMap<String, Arc<CgroupNode>>>, | ||
| /// PIDs in this cgroup. | ||
| pub procs: SpinNoIrq<Vec<u32>>, | ||
| /// Registered controller names (e.g. "pids", "cpu"). | ||
| pub controllers: Vec<String>, | ||
| /// Parent (None for root). | ||
| pub parent: Option<Weak<CgroupNode>>, | ||
| /// Pids controller state. | ||
| pub pids: Arc<PidsState>, | ||
| pub cpu: Arc<CpuState>, | ||
| } | ||
|
|
||
| impl CgroupNode { | ||
| pub fn new_root() -> Arc<Self> { | ||
| Arc::new(Self { | ||
| name: String::new(), | ||
| path: "/".to_string(), | ||
| children: SpinNoIrq::new(BTreeMap::new()), | ||
| procs: SpinNoIrq::new(Vec::new()), | ||
| controllers: Vec::new(), | ||
| parent: None, | ||
| pids: Arc::new(PidsState::new()), | ||
| cpu: Arc::new(CpuState::new()), | ||
| }) | ||
| } | ||
|
|
||
| /// Create a child cgroup under this node. | ||
| pub fn create_child(self: &Arc<Self>, name: &str) -> VfsResult<Arc<CgroupNode>> { | ||
| let mut children = self.children.lock(); | ||
| if children.contains_key(name) { | ||
| return Err(VfsError::AlreadyExists); | ||
| } | ||
| let child_path = if self.path == "/" { | ||
| format!("/{}", name) | ||
| } else { | ||
| format!("{}/{}", self.path, name) | ||
| }; | ||
| let child = Arc::new(CgroupNode { | ||
| name: name.to_string(), | ||
| path: child_path, | ||
| children: SpinNoIrq::new(BTreeMap::new()), | ||
| procs: SpinNoIrq::new(Vec::new()), | ||
| controllers: Vec::new(), | ||
| parent: Some(Arc::downgrade(self)), | ||
| pids: Arc::new(PidsState::new()), | ||
| cpu: Arc::new(CpuState::new()), | ||
| }); | ||
| children.insert(name.to_string(), child); | ||
| Ok(children.get(name).unwrap().clone()) | ||
| } | ||
|
|
||
| /// List controller names. | ||
| pub fn controller_list(&self) -> String { | ||
| let mut list = alloc::vec!["pids".to_string(), "cpu".to_string()]; | ||
| list.extend(self.controllers.iter().cloned()); | ||
| list.join(" ") | ||
| } | ||
| } | ||
|
|
||
| /// Global cgroup v2 root. | ||
| pub static GLOBAL_CGROUP_ROOT: LazyInit<Arc<CgroupNode>> = LazyInit::new(); | ||
|
|
||
| pub fn init() { | ||
| GLOBAL_CGROUP_ROOT.init_once(CgroupNode::new_root()); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| //! cgroup v2 cpu controller. | ||
|
SongShiQ marked this conversation as resolved.
|
||
| //! | ||
| //! Provides file interfaces for cpu.weight and cpu.max. | ||
|
|
||
| use core::sync::atomic::{AtomicI64, AtomicU64}; | ||
|
|
||
| /// Per-cgroup cpu.max bandwidth state. | ||
| /// This is the single source of truth for quota/period. | ||
| pub struct BandwidthState { | ||
| pub quota: AtomicI64, | ||
| pub period: AtomicI64, | ||
| pub consumed: AtomicI64, | ||
| pub nr_periods: AtomicU64, | ||
| pub nr_throttled: AtomicU64, | ||
| pub throttled_usec: AtomicU64, | ||
| pub period_start: AtomicU64, | ||
| } | ||
|
|
||
| impl BandwidthState { | ||
| pub fn new() -> Self { | ||
| Self { | ||
| quota: AtomicI64::new(-1), | ||
| period: AtomicI64::new(100_000), | ||
| consumed: AtomicI64::new(0), | ||
| nr_periods: AtomicU64::new(0), | ||
| nr_throttled: AtomicU64::new(0), | ||
| throttled_usec: AtomicU64::new(0), | ||
| period_start: AtomicU64::new(0), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Per-cgroup cpu controller state. | ||
| /// | ||
| /// `weight` controls relative CPU share (1-10000, default 100). | ||
| /// `bandwidth` holds the cpu.max quota/period and runtime stats. | ||
| pub struct CpuState { | ||
| pub weight: AtomicI64, | ||
| pub bandwidth: BandwidthState, | ||
| } | ||
|
|
||
| impl CpuState { | ||
| pub fn new() -> Self { | ||
| Self { | ||
| weight: AtomicI64::new(100), | ||
| bandwidth: BandwidthState::new(), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Called on every scheduler timer tick to consume quota and throttle. | ||
| /// Currently deferred — requires ax_task tick hook API. | ||
| /// TODO: Re-implement when ax_task::set_tick_hook is available. | ||
| #[allow(dead_code)] | ||
| pub fn bandwidth_tick() { | ||
| // Placeholder: actual implementation requires ax_task tick hook | ||
| // which is deferred in this PR. | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.