aien-sovereign-core-mv
AienOS host runtime: inference scheduling, paged KV state, the aien CLI and Spark verification tooling. Its inference engine is in alpha testing (DGX Spark GB10); Omega is the implementation destination.
Brought by drakestapleton
aien-sovereign-core-mv / crates / spark-harness / src / scheduler / permits.rs
| 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | |
| 16 | //! Resource permits: CPU slots, memory bytes and a concurrency count. |
| 17 | //! |
| 18 | //! A task acquires all three at dispatch or none of them. Permits are held |
| 19 | //! exactly while the task is `Provisional` (see `execution.rs`). |
| 20 | |
| 21 | use serde::{Deserialize, Serialize}; |
| 22 | |
| 23 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] |
| 24 | #[serde(deny_unknown_fields)] |
| 25 | pub struct ResourceLimits { |
| 26 | pub cpu_slots: u32, |
| 27 | pub memory_bytes: u64, |
| 28 | pub max_concurrency: u32, |
| 29 | } |
| 30 | |
| 31 | impl Default for ResourceLimits { |
| 32 | fn default() -> Self { |
| 33 | ResourceLimits { |
| 34 | cpu_slots: 4, |
| 35 | memory_bytes: 8 << 30, |
| 36 | max_concurrency: 4, |
| 37 | } |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | /// What one task needs while it runs. |
| 42 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] |
| 43 | #[serde(deny_unknown_fields)] |
| 44 | pub struct ResourceRequest { |
| 45 | pub cpu_slots: u32, |
| 46 | pub memory_bytes: u64, |
| 47 | } |
| 48 | |
| 49 | impl ResourceLimits { |
| 50 | /// Could this request ever run, with the whole budget free? |
| 51 | pub fn admits(&self, req: ResourceRequest) -> bool { |
| 52 | self.max_concurrency >= 1 |
| 53 | && req.cpu_slots <= self.cpu_slots |
| 54 | && req.memory_bytes <= self.memory_bytes |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | /// Permits currently handed out. |
| 59 | #[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize)] |
| 60 | pub struct PermitPool { |
| 61 | pub cpu_in_use: u32, |
| 62 | pub memory_in_use: u64, |
| 63 | pub running: u32, |
| 64 | } |
| 65 | |
| 66 | impl PermitPool { |
| 67 | /// Take all permits for `req`, or none. |
| 68 | pub fn try_acquire(&mut self, limits: &ResourceLimits, req: ResourceRequest) -> bool { |
| 69 | let fits = self.running < limits.max_concurrency |
| 70 | && self.cpu_in_use.saturating_add(req.cpu_slots) <= limits.cpu_slots |
| 71 | && self.memory_in_use.saturating_add(req.memory_bytes) <= limits.memory_bytes; |
| 72 | if fits { |
| 73 | self.running += 1; |
| 74 | self.cpu_in_use += req.cpu_slots; |
| 75 | self.memory_in_use += req.memory_bytes; |
| 76 | } |
| 77 | fits |
| 78 | } |
| 79 | |
| 80 | pub fn release(&mut self, req: ResourceRequest) { |
| 81 | self.running = self.running.saturating_sub(1); |
| 82 | self.cpu_in_use = self.cpu_in_use.saturating_sub(req.cpu_slots); |
| 83 | self.memory_in_use = self.memory_in_use.saturating_sub(req.memory_bytes); |
| 84 | } |
| 85 | } |