nros_node/config.rs
1//! Build-time configurable constants.
2//!
3//! Values are set via environment variables at build time.
4//! See build.rs for env var names and defaults.
5
6include!(concat!(env!("OUT_DIR"), "/nros_node_config.rs"));
7
8/// One executor sched task's own sizing, as `build.rs` derived it from
9/// the sizing descriptor's `[[sched_task]]` row: the callback slots, node slots and
10/// arena its members need, and the parameters they declare (`None`: the sched task
11/// stated no count, so it carves the image's store). Read through
12/// [`SCHED_TASK_SIZINGS`]; `crate::executor::sched_task_sizing` turns a row into the
13/// `ExecutorSizing` its executor is carved at.
14#[derive(Clone, Copy, Debug, PartialEq, Eq)]
15pub struct SchedTaskSizing {
16 /// The sched task's name, as the entry's sched task table spells it.
17 pub name: &'static str,
18 pub cbs: usize,
19 pub arena: usize,
20 pub nodes: usize,
21 pub params: Option<usize>,
22}
23
24/// The most callbacks one executor of this image holds: the larger of the
25/// default table ([`MAX_CBS`]) and the one executor a level runner opens over
26/// every sched task's callbacks (the sum of [`SCHED_TASK_SIZINGS`]`[..].cbs`).
27pub const MAX_EXECUTOR_CBS: usize = {
28 let mut sum = 0usize;
29 let mut i = 0;
30 while i < SCHED_TASK_SIZINGS.len() {
31 sum += SCHED_TASK_SIZINGS[i].cbs;
32 i += 1;
33 }
34 if sum > MAX_CBS { sum } else { MAX_CBS }
35};
36
37// Issue 2541 -- the slot masks are `SLOT_WORDS` `u64` words, sized from the
38// image. An image of 64 callbacks or fewer gets exactly one word, the layout
39// it had when the masks were a bare `u64`. nros-node's own unit tests take two
40// words whatever the build derived, so the multi-word path is what they run.
41const SLOT_WORDS_FLOOR: usize = if cfg!(test) { 2 } else { 1 };
42
43/// `u64` words in one callback slot mask: enough for [`MAX_EXECUTOR_CBS`].
44pub const SLOT_WORDS: usize = {
45 let derived = MAX_EXECUTOR_CBS.div_ceil(64);
46 if derived > SLOT_WORDS_FLOOR {
47 derived
48 } else {
49 SLOT_WORDS_FLOOR
50 }
51};
52
53/// The most callbacks one executor can hold: one bit per slot in each of its
54/// slot masks ([`SLOT_WORDS`] words of 64).
55pub const CALLBACK_SLOT_CAPACITY: usize = 64 * SLOT_WORDS;
56
57// Issue 2541 -- the capacity is derived from the very counts checked here, so
58// these only trip if the derivation and the counts disagree. The slot index
59// itself travels as a `u8` (`DescIdx`, and the `try_process` slot argument a
60// dispatch hands every entry), so 256 is the ceiling the masks cannot raise.
61const _: () = {
62 assert!(
63 MAX_CBS <= CALLBACK_SLOT_CAPACITY,
64 "NROS_EXECUTOR_MAX_CBS is above the callback slots one executor's masks hold"
65 );
66 assert!(
67 MAX_EXECUTOR_CBS <= CALLBACK_SLOT_CAPACITY,
68 "this image's sched tasks declare more callbacks together than the one \
69 executor a level runner opens over them holds"
70 );
71 assert!(
72 CALLBACK_SLOT_CAPACITY <= 256,
73 "one executor holds at most 256 callbacks: a callback slot index is a u8 \
74 (lower NROS_EXECUTOR_MAX_CBS, or split the sched tasks across executors)"
75 );
76};
77
78/// Default arena bytes for a per-entry executor holding `cbs`
79/// callback slots. Scales the build-time [`ARENA_SIZE`] (which is sized for the
80/// default [`MAX_CBS`]) linearly by `cbs`, so a per-entry [`ExecutorSizing`] with
81/// a caller-chosen `cbs` gets the same per-slot arena budget the global default
82/// used — a fat entry (more callbacks) gets a larger arena, a lean one a smaller
83/// one — without a workspace-global `NROS_EXECUTOR_ARENA_BYTES`. Floored at the
84/// full default so no per-entry executor is ever smaller than a single-slot
85/// build would have been (the arena is a worst-case upper bound; over-provision
86/// is safe, under-provision fails entity creation).
87///
88/// # The ratio is for an UNDECLARED image only
89///
90/// The scaling above reads `ARENA_SIZE` as a budget for `MAX_CBS` slots, so
91/// `ARENA_SIZE / MAX_CBS` is one slot's worth and `cbs` of them is `cbs` slots'
92/// worth. That is exactly what `ARENA_SIZE` is while nothing declared: the
93/// derivation budgets `max_cbs` worst-case entries plus a base overhead.
94///
95/// It stopped being that once the build learned to size an image that DECLARES its
96/// entities. There `nros-node/build.rs` sums the model per KIND — subscriptions
97/// at their own depth and type, timers at 64 bytes, services, action clients —
98/// and `MAX_CBS` is a separate knob that takes no part. Dividing by it then
99/// divides by a number with no relationship to the sum, and multiplying by
100/// `cbs` scales a total that was never per-slot. Measured on
101/// `packages/testing/nros-bench/large-msg-baremetal` with one declared
102/// subscription: the model is 14,424 bytes and `arena_size_for(2)` returned
103/// 7,212. Half.
104///
105/// So when the image declares, this asks the model: `ARENA_SIZE` IS the model
106/// (unfloored, and `executor::arena_oracle` refuses at
107/// compile time to let a stated knob sit below it), and every per-entry executor gets all of it.
108///
109/// **That over-provisions a SCHED-TASK boot**, where one sched task holds a subset of the
110/// image and now carries the whole model. Deliberate, and the direction is the
111/// safe one: the alternative needs the ENTRY's own per-kind counts where the
112/// macro emits the sizing, and those do not reach it today (a recorded
113/// choice). An entry that wants less states its own
114/// [`ExecutorSizing`] and holds it to what it registers with
115/// [`ExecutorSizing::assert_covers_model`], which is a compile-time check the
116/// ratio never had.
117///
118/// [`ExecutorSizing`]: crate::executor::ExecutorSizing
119/// [`ExecutorSizing::assert_covers_model`]: crate::executor::ExecutorSizing::assert_covers_model
120pub const fn arena_size_for(cbs: usize) -> usize {
121 // `!= 0` and not `> 0`: on a build that declares nothing the generated
122 // const IS the literal `0`, and clippy's `absurd_extreme_comparisons`
123 // const-propagates it and refuses `> 0` as always-false under
124 // `-D warnings`. Same predicate, and it reads the same way -- `0` means
125 // "no model", never "a model of nothing".
126 if arena_model::REQUIRED != 0 {
127 return ARENA_SIZE;
128 }
129 let scaled = (cbs * ARENA_SIZE).div_ceil(if MAX_CBS == 0 { 1 } else { MAX_CBS });
130 // Never below one default slot's worth so a tiny `cbs` still has headroom for
131 // the base overhead the derivation folds into `ARENA_SIZE`.
132 let floor = ARENA_SIZE.div_ceil(if MAX_CBS == 0 { 1 } else { MAX_CBS });
133 if scaled < floor { floor } else { scaled }
134}
135
136#[cfg(test)]
137mod arena_size_for_tests {
138 use super::{ARENA_SIZE, MAX_CBS, arena_model, arena_size_for};
139
140 /// Every number in this module goes through here, and that is not style.
141 ///
142 /// The build script emits `arena_model::REQUIRED` as a LITERAL, and on a
143 /// build that declares nothing the literal is `0` — the minimum of
144 /// `usize`. clippy then const-folds every comparison against it and
145 /// `absurd_extreme_comparisons` / `assertions_on_constants` refuse the
146 /// whole module under `-D warnings`, which is what `check-workspace-all`
147 /// runs. `black_box` hides only the CONSTANT-ness; the value, and every
148 /// assertion's meaning, is unchanged. Deleting it does not weaken a test,
149 /// it stops the crate compiling.
150 fn opaque(v: usize) -> usize {
151 core::hint::black_box(v)
152 }
153
154 /// issue 1290 — a per-entry arena must cover the model the image declares.
155 ///
156 /// This is the property `arena_size_for` violated: it scaled `ARENA_SIZE`
157 /// by `cbs / MAX_CBS` on an image where `ARENA_SIZE` is a per-KIND sum and
158 /// `MAX_CBS` takes no part in it, so the image's only executor got a
159 /// fraction of its own requirement.
160 ///
161 /// It is only a claim about a build that DECLARED — `REQUIRED` is 0
162 /// otherwise, and then the ratio is the right arithmetic over the right
163 /// number. The `node-std-tests` lane runs this test a second time with the
164 /// reference island's declared shape in the environment, which is what
165 /// makes the first branch reachable at all; the `assert!` on `REQUIRED`
166 /// there is what stops that run passing vacuously.
167 #[test]
168 fn a_per_entry_arena_covers_the_declared_model() {
169 if opaque(arena_model::REQUIRED) == 0 {
170 // Nothing declared: `ARENA_SIZE` is a budget over `MAX_CBS` slots,
171 // so the ratio is meaningful and a full-width entry gets all of it.
172 assert_eq!(opaque(arena_size_for(MAX_CBS)), opaque(ARENA_SIZE));
173 return;
174 }
175 for cbs in 0..=MAX_CBS.max(1) * 2 {
176 assert!(
177 opaque(arena_size_for(cbs)) >= opaque(arena_model::REQUIRED),
178 "arena_size_for({cbs}) = {} is below the {} bytes this image's \
179 declared entities are modelled at — every registration past it \
180 fails with BufferTooSmall (issue 1290)",
181 arena_size_for(cbs),
182 arena_model::REQUIRED,
183 );
184 }
185 }
186
187 /// The POSITIVE CONTROL, and the reason the test above is not free.
188 ///
189 /// `#[ignore]`d because it can only say anything in a build that DECLARED,
190 /// and a `cargo test` of this crate declares nothing. `node-std-tests` runs
191 /// it with the reference island's shape in the environment; the first
192 /// assertion is what stops that run passing vacuously if the declaration
193 /// ever stops arriving.
194 ///
195 /// The arithmetic this replaced is RESTATED here — the one place in the
196 /// tree it may be, because what is being asserted is precisely that it was
197 /// WRONG. Without it a reader cannot tell whether the property above holds
198 /// because the fix works or because the numbers never collided.
199 #[test]
200 #[ignore = "needs NROS_ENTITY_COUNT_* in the build environment; run by `just check node-std-tests`"]
201 fn the_scaling_this_replaced_was_short_of_the_declared_model() {
202 assert!(
203 opaque(arena_model::REQUIRED) > opaque(arena_model::FLOOR),
204 "this build declared no entities (REQUIRED = {}, FLOOR = {}), so \
205 nothing below is a claim about anything. Set NROS_ENTITY_COUNT_* \
206 for the build, as `just check node-std-tests` does (issue 1290).",
207 arena_model::REQUIRED,
208 arena_model::FLOOR,
209 );
210 // Above the floor and with no knob stated, the derived arena IS the
211 // model — which is what makes the comparison below arithmetic rather
212 // than luck.
213 assert_eq!(opaque(ARENA_SIZE), opaque(arena_model::REQUIRED));
214 assert!(
215 opaque(MAX_CBS) >= 2,
216 "the ratio is only a ratio for MAX_CBS >= 2"
217 );
218
219 let divisor = MAX_CBS;
220 let scaled = |cbs: usize| {
221 let s = (cbs * ARENA_SIZE).div_ceil(divisor);
222 let floor = ARENA_SIZE.div_ceil(divisor);
223 if s < floor { floor } else { s }
224 };
225 // One slot short of the full width is where it bit: the bench sizes its
226 // ONE executor with `arena_size_for(2)` against a `MAX_CBS` of 4.
227 let cbs = MAX_CBS - 1;
228 assert!(
229 opaque(scaled(cbs)) < opaque(arena_model::REQUIRED),
230 "the cbs/MAX_CBS scaling gave {} for cbs={cbs} against a declared \
231 model of {} — it was not short here, so this test is asserting \
232 nothing",
233 scaled(cbs),
234 arena_model::REQUIRED,
235 );
236 assert!(
237 opaque(arena_size_for(cbs)) >= opaque(arena_model::REQUIRED),
238 "arena_size_for({cbs}) = {} still does not cover the declared model \
239 of {} (issue 1290)",
240 arena_size_for(cbs),
241 arena_model::REQUIRED,
242 );
243 }
244}